Skip to content

Commit 0ca8dcf

Browse files
authored
Add some more unit tests for "filesystem" utility module (#1157)
* Add some more unit tests for "filesystem" utility module * Lower some tests from integration to the unit level * Template the expandFilePathTilda function to not mock platform paths * Add more tests * Filx spelling
1 parent ee88108 commit 0ca8dcf

File tree

4 files changed

+77
-29
lines changed

4 files changed

+77
-29
lines changed

src/toolchain/toolchain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as vscode from "vscode";
2020
import configuration from "../configuration";
2121
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
2222
import { execFile, execSwift } from "../utilities/utilities";
23-
import { expandFilePathTilda, pathExists } from "../utilities/filesystem";
23+
import { expandFilePathTilde, pathExists } from "../utilities/filesystem";
2424
import { Version } from "../utilities/version";
2525
import { BuildFlags } from "./BuildFlags";
2626
import { Sanitizer } from "./Sanitizer";
@@ -536,7 +536,7 @@ export class SwiftToolchain {
536536
}
537537
// swift may be a symbolic link
538538
const realSwift = await fs.realpath(swift);
539-
const swiftPath = expandFilePathTilda(path.dirname(realSwift));
539+
const swiftPath = expandFilePathTilde(path.dirname(realSwift));
540540
return await this.getSwiftEnvPath(swiftPath);
541541
} catch {
542542
throw Error("Failed to find swift executable");

src/utilities/filesystem.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,22 @@ export function isPathInsidePath(subpath: string, parent: string): boolean {
5858
* @param filepath File path
5959
* @returns full path
6060
*/
61-
export function expandFilePathTilda(filepath: string): string {
62-
if (process.platform !== "win32" && filepath[0] === "~" && process.env.HOME) {
63-
return path.join(process.env.HOME, filepath.slice(1));
61+
export function expandFilePathTilde(
62+
filepath: string,
63+
directory: string | null = process.env.HOME ?? null,
64+
platform: NodeJS.Platform = process.platform
65+
): string {
66+
// Guard no expanding on windows
67+
if (platform === "win32") {
68+
return filepath;
6469
}
65-
return filepath;
70+
// Guard tilde is present
71+
if (filepath[0] !== "~") {
72+
return filepath;
73+
}
74+
// Guard we know home directory
75+
if (!directory) {
76+
return filepath;
77+
}
78+
return path.join(directory, filepath.slice(1));
6679
}

test/integration-tests/utilities/filesystem.test.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,9 @@
1414

1515
import * as assert from "assert";
1616
import * as path from "path";
17-
import {
18-
isPathInsidePath,
19-
expandFilePathTilda,
20-
fileExists,
21-
pathExists,
22-
} from "../../../src/utilities/filesystem";
17+
import { fileExists, pathExists } from "../../../src/utilities/filesystem";
2318

2419
suite("File System Utilities Test Suite", () => {
25-
test("isPathInsidePath", () => {
26-
assert(isPathInsidePath("/home/user/package", "/home/user/"));
27-
assert(isPathInsidePath("/home/user/package/test", "/home/user/"));
28-
assert(isPathInsidePath("/home/user/", "/home/user/"));
29-
assert(isPathInsidePath("/home/user/.build", "/home/user/"));
30-
assert(!isPathInsidePath("/home/user/package", "/home/user/package2"));
31-
assert(!isPathInsidePath("/home/user/package/.build", "/home/user/package2/.build"));
32-
assert(!isPathInsidePath("/home/user/package/", "/home/user/package/.build"));
33-
});
34-
35-
test("expandFilePathTilda", () => {
36-
const homeDir = process.env.HOME;
37-
assert(expandFilePathTilda("~/Test"), `${homeDir}/Test`);
38-
assert(expandFilePathTilda("/Users/John/Test"), `/Users/John/Test`);
39-
assert(expandFilePathTilda("/Users/~/Test"), `/Users/~/Test`);
40-
});
41-
4220
test("fileExists", async () => {
4321
assert(await fileExists(__filename));
4422
assert(!(await fileExists(__dirname)));
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2024 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import { isPathInsidePath, expandFilePathTilde } from "../../../src/utilities/filesystem";
16+
import { expect } from "chai";
17+
18+
suite("File System Utilities Unit Test Suite", () => {
19+
test("isPathInsidePath", () => {
20+
expect(isPathInsidePath("/home/user/package", "/home/user/")).to.be.true;
21+
expect(isPathInsidePath("/home/user/package/test", "/home/user/")).to.be.true;
22+
expect(isPathInsidePath("/home/user/", "/home/user/")).to.be.true;
23+
expect(isPathInsidePath("/home/user/.build", "/home/user/")).to.be.true;
24+
expect(isPathInsidePath("/home/user/package", "/home/user/package2")).to.be.false;
25+
expect(isPathInsidePath("/home/user/package/.build", "/home/user/package2/.build")).to.be
26+
.false;
27+
expect(isPathInsidePath("/home/user/package/", "/home/user/package/.build")).to.be.false;
28+
});
29+
30+
suite("expandFilePathTilde", () => {
31+
test("expands tilde", () => {
32+
expect(expandFilePathTilde("~/Test", "/Users/John", "darwin")).to.equal(
33+
"/Users/John/Test"
34+
);
35+
});
36+
37+
test("no tilde present", () => {
38+
expect(expandFilePathTilde("/Users/John/Test", "/Users/John2", "darwin")).to.equal(
39+
"/Users/John/Test"
40+
);
41+
});
42+
43+
test("tilde not first character", () => {
44+
expect(expandFilePathTilde("/Users/~/Test", "/Users/John", "darwin")).to.equal(
45+
"/Users/~/Test"
46+
);
47+
});
48+
49+
test("don't know the home directory", () => {
50+
expect(expandFilePathTilde("~/Test", null, "darwin")).to.equal("~/Test");
51+
});
52+
53+
test("don't resolve tilde on Windows", () => {
54+
expect(expandFilePathTilde("~/Test", "C:\\Users\\John", "win32")).to.equal("~/Test");
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)