Skip to content

Commit

Permalink
fix: refactor path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davakh committed Mar 6, 2024
1 parent 773fe09 commit 503ee0e
Showing 1 changed file with 41 additions and 56 deletions.
97 changes: 41 additions & 56 deletions test/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const {
normalize
} = require("../lib/util/path");

// It's enough to use path.sep for tests because the repository has Windows test-runners,
// but for understanding what to expect, we should know about platform and path-type in tests.
const isWin32 = process.platform === "win32";
const currentPathType = isWin32 ? "win32" : "posix";

describe("checkImportsExportsFieldTarget", () => {
/**
* @type {string[]}
Expand Down Expand Up @@ -35,75 +40,55 @@ describe("checkImportsExportsFieldTarget", () => {
});

describe("getPath", () => {
let pathSepDefault = path.sep;

afterAll(() => {
path.sep = pathSepDefault;
});

["win32", "posix"].forEach(platform => {
const relativePathType =
platform === "win32" ? "RelativeWin" : "RelativePosix";
const separator = platform === "win32" ? "\\" : "/";

it(`should resolve PathType.${relativePathType} for paths if path.sep is ${platform} (${separator})`, () => {
path.sep = separator;

expect(getType(".")).toBe(PathType[relativePathType]);
expect(getType("..")).toBe(PathType[relativePathType]);
expect(getType(`..${path.sep}`)).toBe(PathType[relativePathType]);
expect(getType(`..${path.sep}test${path.sep}index.js`)).toBe(
PathType[relativePathType]
);
});
const relativePathType = isWin32 ? "RelativeWin" : "RelativePosix";

it(`should resolve PathType.${relativePathType} for paths if path.sep is ${currentPathType} (${path.sep})`, () => {
expect(getType(".")).toBe(PathType[relativePathType]);
expect(getType("..")).toBe(PathType[relativePathType]);
expect(getType(`..${path.sep}`)).toBe(PathType[relativePathType]);
expect(getType(`..${path.sep}test${path.sep}index.js`)).toBe(
PathType[relativePathType]
);
});
});

describe("normalize", () => {
let pathSepDefault = path.sep;
it(`should correctly normalize for empty path if path.sep is ${currentPathType} (${path.sep})`, () => {
const pathToNormalize = "";

afterEach(() => {
path.sep = pathSepDefault;
expect(getType(pathToNormalize)).toBe(PathType.Empty);
expect(normalize(pathToNormalize)).toBe("");
});

["win32", "posix"].forEach(platform => {
const separator = platform === "win32" ? "\\" : "/";
it(`should correctly normalize for relative path if path.sep is ${currentPathType} (${path.sep})`, () => {
const pathToNormalize = `..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`;

it(`should correctly normalize for relative/empty paths if path.sep is ${platform} (${separator})`, () => {
path.sep = separator;

expect(normalize("")).toBe("");
expect(
normalize(
`..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`
)
).toBe(`..${path.sep}hello${path.sep}test.js`);
});
});

it("should correctly normalize for PathType.AbsoluteWin", () => {
path.sep = "\\";

expect(
normalize(
`..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`
)
).toBe(`..${path.sep}hello${path.sep}test.js`);
expect(getType(pathToNormalize)).toBe(
isWin32 ? PathType.RelativeWin : PathType.RelativePosix
);
expect(normalize(pathToNormalize)).toBe(
`..${path.sep}hello${path.sep}test.js`
);
});

it("should correctly normalize for PathType.AbsolutePosix", () => {
path.sep = "/";
it(`should correctly normalize for absolute path if path.sep is ${currentPathType} (${path.sep})`, () => {
const basePath = `${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`;
const getAbsolutePathPrefixBasedOnPlatform = pathStr =>
isWin32 ? `X:${pathStr}` : pathStr;
const pathToNormalize = getAbsolutePathPrefixBasedOnPlatform(basePath);

expect(
normalize(
`..${path.sep}hello${path.sep}world${path.sep}..${path.sep}test.js`
)
).toBe(`..${path.sep}hello${path.sep}test.js`);
expect(getType(pathToNormalize)).toBe(
isWin32 ? PathType.AbsoluteWin : PathType.AbsolutePosix
);
expect(normalize(pathToNormalize)).toBe(
getAbsolutePathPrefixBasedOnPlatform(`${path.sep}hello${path.sep}test.js`)
);
});

it("should correctly normalize for PathType.Normal", () => {
expect(normalize("enhancedResolve/lib/util/../index")).toBe(
"enhancedResolve/lib/index"
);
const pathToNormalize = "enhancedResolve/lib/util/../index";

expect(getType(pathToNormalize)).toBe(PathType.Normal);
expect(normalize(pathToNormalize)).toBe("enhancedResolve/lib/index");
});
});

0 comments on commit 503ee0e

Please sign in to comment.