|
6 | 6 | normalize
|
7 | 7 | } = require("../lib/util/path");
|
8 | 8 |
|
| 9 | +// It's enough to use path.sep for tests because the repository has Windows test-runners, |
| 10 | +// but for understanding what to expect, we should know about platform and path-type in tests. |
| 11 | +const isWin32 = process.platform === "win32"; |
| 12 | +const currentPathType = isWin32 ? "win32" : "posix"; |
| 13 | + |
9 | 14 | describe("checkImportsExportsFieldTarget", () => {
|
10 | 15 | /**
|
11 | 16 | * @type {string[]}
|
@@ -35,75 +40,55 @@ describe("checkImportsExportsFieldTarget", () => {
|
35 | 40 | });
|
36 | 41 |
|
37 | 42 | describe("getPath", () => {
|
38 |
| - let pathSepDefault = path.sep; |
39 |
| - |
40 |
| - afterAll(() => { |
41 |
| - path.sep = pathSepDefault; |
42 |
| - }); |
43 |
| - |
44 |
| - ["win32", "posix"].forEach(platform => { |
45 |
| - const relativePathType = |
46 |
| - platform === "win32" ? "RelativeWin" : "RelativePosix"; |
47 |
| - const separator = platform === "win32" ? "\\" : "/"; |
48 |
| - |
49 |
| - it(`should resolve PathType.${relativePathType} for paths if path.sep is ${platform} (${separator})`, () => { |
50 |
| - path.sep = separator; |
51 |
| - |
52 |
| - expect(getType(".")).toBe(PathType[relativePathType]); |
53 |
| - expect(getType("..")).toBe(PathType[relativePathType]); |
54 |
| - expect(getType(`..${path.sep}`)).toBe(PathType[relativePathType]); |
55 |
| - expect(getType(`..${path.sep}test${path.sep}index.js`)).toBe( |
56 |
| - PathType[relativePathType] |
57 |
| - ); |
58 |
| - }); |
| 43 | + const relativePathType = isWin32 ? "RelativeWin" : "RelativePosix"; |
| 44 | + |
| 45 | + it(`should resolve PathType.${relativePathType} for paths if path.sep is ${currentPathType} (${path.sep})`, () => { |
| 46 | + expect(getType(".")).toBe(PathType[relativePathType]); |
| 47 | + expect(getType("..")).toBe(PathType[relativePathType]); |
| 48 | + expect(getType(`..${path.sep}`)).toBe(PathType[relativePathType]); |
| 49 | + expect(getType(`..${path.sep}test${path.sep}index.js`)).toBe( |
| 50 | + PathType[relativePathType] |
| 51 | + ); |
59 | 52 | });
|
60 | 53 | });
|
61 | 54 |
|
62 | 55 | describe("normalize", () => {
|
63 |
| - let pathSepDefault = path.sep; |
| 56 | + it(`should correctly normalize for empty path if path.sep is ${currentPathType} (${path.sep})`, () => { |
| 57 | + const pathToNormalize = ""; |
64 | 58 |
|
65 |
| - afterEach(() => { |
66 |
| - path.sep = pathSepDefault; |
| 59 | + expect(getType(pathToNormalize)).toBe(PathType.Empty); |
| 60 | + expect(normalize(pathToNormalize)).toBe(""); |
67 | 61 | });
|
68 | 62 |
|
69 |
| - ["win32", "posix"].forEach(platform => { |
70 |
| - const separator = platform === "win32" ? "\\" : "/"; |
| 63 | + it(`should correctly normalize for relative path if path.sep is ${currentPathType} (${path.sep})`, () => { |
| 64 | + const pathToNormalize = `..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`; |
71 | 65 |
|
72 |
| - it(`should correctly normalize for relative/empty paths if path.sep is ${platform} (${separator})`, () => { |
73 |
| - path.sep = separator; |
74 |
| - |
75 |
| - expect(normalize("")).toBe(""); |
76 |
| - expect( |
77 |
| - normalize( |
78 |
| - `..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js` |
79 |
| - ) |
80 |
| - ).toBe(`..${path.sep}hello${path.sep}test.js`); |
81 |
| - }); |
82 |
| - }); |
83 |
| - |
84 |
| - it("should correctly normalize for PathType.AbsoluteWin", () => { |
85 |
| - path.sep = "\\"; |
86 |
| - |
87 |
| - expect( |
88 |
| - normalize( |
89 |
| - `..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js` |
90 |
| - ) |
91 |
| - ).toBe(`..${path.sep}hello${path.sep}test.js`); |
| 66 | + expect(getType(pathToNormalize)).toBe( |
| 67 | + isWin32 ? PathType.RelativeWin : PathType.RelativePosix |
| 68 | + ); |
| 69 | + expect(normalize(pathToNormalize)).toBe( |
| 70 | + `..${path.sep}hello${path.sep}test.js` |
| 71 | + ); |
92 | 72 | });
|
93 | 73 |
|
94 |
| - it("should correctly normalize for PathType.AbsolutePosix", () => { |
95 |
| - path.sep = "/"; |
| 74 | + it(`should correctly normalize for absolute path if path.sep is ${currentPathType} (${path.sep})`, () => { |
| 75 | + const basePath = `${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`; |
| 76 | + const getAbsolutePathPrefixBasedOnPlatform = pathStr => |
| 77 | + isWin32 ? `X:${pathStr}` : pathStr; |
| 78 | + const pathToNormalize = getAbsolutePathPrefixBasedOnPlatform(basePath); |
96 | 79 |
|
97 |
| - expect( |
98 |
| - normalize( |
99 |
| - `..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js` |
100 |
| - ) |
101 |
| - ).toBe(`..${path.sep}hello${path.sep}test.js`); |
| 80 | + expect(getType(pathToNormalize)).toBe( |
| 81 | + isWin32 ? PathType.AbsoluteWin : PathType.AbsolutePosix |
| 82 | + ); |
| 83 | + expect(normalize(pathToNormalize)).toBe( |
| 84 | + getAbsolutePathPrefixBasedOnPlatform(`${path.sep}hello${path.sep}test.js`) |
| 85 | + ); |
102 | 86 | });
|
103 | 87 |
|
104 | 88 | it("should correctly normalize for PathType.Normal", () => {
|
105 |
| - expect(normalize("enhancedResolve/lib/util/../index")).toBe( |
106 |
| - "enhancedResolve/lib/index" |
107 |
| - ); |
| 89 | + const pathToNormalize = "enhancedResolve/lib/util/../index"; |
| 90 | + |
| 91 | + expect(getType(pathToNormalize)).toBe(PathType.Normal); |
| 92 | + expect(normalize(pathToNormalize)).toBe("enhancedResolve/lib/index"); |
108 | 93 | });
|
109 | 94 | });
|
0 commit comments