Skip to content

Commit 503ee0e

Browse files
committed
fix: refactor path tests
1 parent 773fe09 commit 503ee0e

File tree

1 file changed

+41
-56
lines changed

1 file changed

+41
-56
lines changed

test/path.test.js

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const {
66
normalize
77
} = require("../lib/util/path");
88

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+
914
describe("checkImportsExportsFieldTarget", () => {
1015
/**
1116
* @type {string[]}
@@ -35,75 +40,55 @@ describe("checkImportsExportsFieldTarget", () => {
3540
});
3641

3742
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+
);
5952
});
6053
});
6154

6255
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 = "";
6458

65-
afterEach(() => {
66-
path.sep = pathSepDefault;
59+
expect(getType(pathToNormalize)).toBe(PathType.Empty);
60+
expect(normalize(pathToNormalize)).toBe("");
6761
});
6862

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`;
7165

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+
);
9272
});
9373

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);
9679

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+
);
10286
});
10387

10488
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");
10893
});
10994
});

0 commit comments

Comments
 (0)