forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileUtils-test.js
More file actions
211 lines (166 loc) · 9.4 KB
/
FileUtils-test.js
File metadata and controls
211 lines (166 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, it, expect, beforeEach, afterEach, spyOn */
/*unittests: FileUtils*/
define(function (require, exports, module) {
var FileUtils = require("file/FileUtils");
describe("FileUtils", function () {
describe("convertWindowsPathToUnixPath", function () {
var origPlatform;
beforeEach(function () {
origPlatform = brackets.platform;
});
afterEach(function () {
brackets.platform = origPlatform;
});
it("should convert a Windows path to a Unix-style path when on Windows", function () {
brackets.platform = "win";
expect(FileUtils.convertWindowsPathToUnixPath("C:\\foo\\bar\\baz.txt")).toBe("C:/foo/bar/baz.txt");
});
it("should not modify a native path when on Mac, even if it has backslashes", function () {
brackets.platform = "mac";
expect(FileUtils.convertWindowsPathToUnixPath("/some/back\\slash/path.txt")).toBe("/some/back\\slash/path.txt");
});
});
describe("convertToNativePath", function () {
it("should convert windows URL syntax to Windows native path.", function () {
expect(FileUtils.convertToNativePath("/C:/foo/bar/baz.txt")).toBe("C:/foo/bar/baz.txt");
});
it("should not modify a windows non-URL path syntax.", function () {
expect(FileUtils.convertToNativePath("C:/foo/bar/baz.txt")).toBe("C:/foo/bar/baz.txt");
});
it("should not modify a posix path.", function () {
expect(FileUtils.convertToNativePath("/foo/bar/baz.txt")).toBe("/foo/bar/baz.txt");
expect(FileUtils.convertToNativePath("foo/bar/baz.txt")).toBe("foo/bar/baz.txt");
expect(FileUtils.convertToNativePath("/foo/bar/")).toBe("/foo/bar/");
expect(FileUtils.convertToNativePath("foo/bar/")).toBe("foo/bar/");
});
});
describe("getDirectoryPath", function () {
it("should get the parent directory of a normalized win file path", function () {
expect(FileUtils.getDirectoryPath("C:/foo/bar/baz.txt")).toBe("C:/foo/bar/");
});
it("should get the parent directory of a posix file path", function () {
expect(FileUtils.getDirectoryPath("/foo/bar/baz.txt")).toBe("/foo/bar/");
});
it("should return the unchanged directory of a normalized win directory path", function () {
expect(FileUtils.getDirectoryPath("C:/foo/bar/")).toBe("C:/foo/bar/");
});
it("should return the unchanged directory of a posix directory path", function () {
expect(FileUtils.getDirectoryPath("/foo/bar/")).toBe("/foo/bar/");
});
it("should return the unchanged directory of a root path", function () {
expect(FileUtils.getDirectoryPath("C:/")).toBe("C:/");
expect(FileUtils.getDirectoryPath("/")).toBe("/");
});
});
describe("getParentPath", function () {
it("should get the parent directory of a normalized file path", function () {
expect(FileUtils.getParentPath("C:/foo/bar/baz.txt")).toBe("C:/foo/bar/");
expect(FileUtils.getParentPath("/foo/bar/baz.txt")).toBe("/foo/bar/");
});
it("should return the parent directory of a normalized directory path", function () {
expect(FileUtils.getParentPath("C:/foo/bar/")).toBe("C:/foo/");
expect(FileUtils.getParentPath("/foo/bar/")).toBe("/foo/");
});
it("should return '' given a root path", function () {
expect(FileUtils.getParentPath("C:/")).toBe("");
expect(FileUtils.getParentPath("/")).toBe("");
});
});
describe("getBaseName", function () {
it("should get the file name of a normalized win file path", function () {
expect(FileUtils.getBaseName("C:/foo/bar/baz.txt")).toBe("baz.txt");
});
it("should get the file name of a posix file path", function () {
expect(FileUtils.getBaseName("/foo/bar/baz.txt")).toBe("baz.txt");
});
it("should return the directory name of a normalized win directory path", function () {
expect(FileUtils.getBaseName("C:/foo/bar/")).toBe("bar");
});
it("should return the directory name of a posix directory path", function () {
expect(FileUtils.getBaseName("C:/foo/bar/")).toBe("bar");
});
it("should return the file name of a path containing #", function () {
expect(FileUtils.getBaseName("C:/foo/bar/#baz/jaz.txt")).toBe("jaz.txt");
expect(FileUtils.getBaseName("C:/foo/bar/baz/#jaz.txt")).toBe("#jaz.txt");
});
it("should return the directory name of a path containing #", function () {
expect(FileUtils.getBaseName("C:/foo/bar/#baz/jaz/")).toBe("jaz");
expect(FileUtils.getBaseName("C:/foo/bar/baz/#jaz/")).toBe("#jaz");
});
});
describe("getFileExtension", function () {
it("should get the extension of a normalized win file path", function () {
expect(FileUtils.getFileExtension("C:/foo/bar/baz.txt")).toBe("txt");
});
it("should get the extension of a posix file path", function () {
expect(FileUtils.getFileExtension("/foo/bar/baz.txt")).toBe("txt");
});
it("should return empty extension for a normalized win directory path", function () {
expect(FileUtils.getFileExtension("C:/foo/bar/")).toBe("");
});
it("should return empty extension for a posix directory path", function () {
expect(FileUtils.getFileExtension("bar")).toBe("");
});
it("should return the extension of a filename containing .", function () {
expect(FileUtils.getFileExtension("C:/foo/bar/.baz/jaz.txt")).toBe("txt");
expect(FileUtils.getFileExtension("foo/bar/baz/.jaz.txt")).toBe("txt");
expect(FileUtils.getFileExtension("foo.bar.baz..jaz.txt")).toBe("txt");
});
});
describe("getFilenameWithoutExtension", function () {
it("should remove last extension segment only", function () {
expect(FileUtils.getFilenameWithoutExtension("foo.txt")).toBe("foo");
expect(FileUtils.getFilenameWithoutExtension("foo.min.txt")).toBe("foo.min");
expect(FileUtils.getFilenameWithoutExtension("foo")).toBe("foo");
expect(FileUtils.getFilenameWithoutExtension(".foo")).toBe("");
});
});
describe("encodeFilePath", function () {
it("should encode symbols in path", function () {
expect(FileUtils.encodeFilePath("#?@test&\".js")).toBe("%23%3F%40test%26%22.js");
});
it("should work with a common path", function () {
expect(FileUtils.encodeFilePath("C:/test/$data.txt")).toBe("C%3A/test/%24data.txt");
});
it("should work with a path with no special symbols", function () {
expect(FileUtils.encodeFilePath("/Applications/Test/test.html")).toBe("/Applications/Test/test.html");
});
});
describe("compareFilenames", function () {
it("should compare filenames using German rules", function () {
spyOn(brackets, "getLocale").and.returnValue("de-DE");
// Should be like this: Äckerman, Adler, Rossi, Xavier
expect(FileUtils.compareFilenames("Äckerman", "Adler", false)).toBeLessThan(0);
expect(FileUtils.compareFilenames("Adler", "Rossi", false)).toBeLessThan(0);
expect(FileUtils.compareFilenames("Rossi", "Xavier", false)).toBeLessThan(0);
});
it("should compare filenames using Swedish rules", function () {
spyOn(brackets, "getLocale").and.returnValue("sv");
// Should be like this: Adler, Rossie, Xavier, Äckerman
expect(FileUtils.compareFilenames("Adler", "Rossie", false)).toBeLessThan(0);
expect(FileUtils.compareFilenames("Rossie", "Xavier", false)).toBeLessThan(0);
expect(FileUtils.compareFilenames("Xavier", "Äckerman", false)).toBeLessThan(0);
});
});
});
});