Skip to content

Commit 3912833

Browse files
authored
Add some more unit tests for "utilities" module (swiftlang#1156)
* Lower utility tests to the unit level * Add some more unit tests for "utilities" module * Pull out some functions so platform specific branches need not be mocked * Add some more test cases for functions without coverage * Result 88% coverage between unit and integration suites * Leave everything under test/ out of coverage report
1 parent 90aff0e commit 3912833

File tree

4 files changed

+241
-155
lines changed

4 files changed

+241
-155
lines changed

.vscode-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module.exports = defineConfig({
8484
],
8585
coverage: {
8686
includeAll: true,
87-
exclude: ["**/test/unit-tests/**", "**/test/integration-tests/**"],
87+
exclude: ["**/test/**"],
8888
reporter: ["text", "lcov"], // "lcov" also generates HTML
8989
},
9090
});

src/utilities/utilities.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,40 @@ export function swiftRuntimeEnv(
3030
base: NodeJS.ProcessEnv | boolean = process.env,
3131
runtimePath: string = configuration.runtimePath
3232
): { [key: string]: string } | undefined {
33-
if (runtimePath === "") {
34-
return undefined;
35-
}
3633
const key = swiftLibraryPathKey();
3734
const separator = process.platform === "win32" ? ";" : ":";
3835
switch (base) {
3936
case false:
40-
return { [key]: runtimePath };
37+
base = {};
38+
break;
4139
case true:
42-
return { [key]: `${runtimePath}${separator}\${env:${key}}` };
40+
base = { [key]: `\${env:${key}}` };
41+
break;
4342
default:
44-
return base[key]
45-
? { [key]: `${runtimePath}${separator}${base[key]}` }
46-
: { [key]: runtimePath };
43+
break;
4744
}
45+
return runtimeEnv(base, key, runtimePath, separator);
46+
}
47+
48+
export function runtimeEnv(
49+
base: NodeJS.ProcessEnv,
50+
key: string,
51+
value: string,
52+
separator: string
53+
): { [key: string]: string } | undefined {
54+
if (value === "") {
55+
return undefined;
56+
}
57+
return base[key] ? { [key]: `${value}${separator}${base[key]}` } : { [key]: value };
4858
}
4959

5060
/** Return environment variable to update for runtime library search path */
5161
export function swiftLibraryPathKey(): string {
52-
switch (process.platform) {
62+
return swiftPlatformLibraryPathKey(process.platform);
63+
}
64+
65+
export function swiftPlatformLibraryPathKey(platform: NodeJS.Platform): string {
66+
switch (platform) {
5367
case "win32":
5468
return "Path";
5569
case "darwin":

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

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ import * as assert from "assert";
1616
import * as Stream from "stream";
1717
import {
1818
execFileStreamOutput,
19-
getRepositoryName,
2019
execSwift,
2120
getSwiftExecutable,
22-
stringArrayInEnglish,
23-
regexEscapedString,
24-
hashString,
25-
getErrorDescription,
2621
} from "../../../src/utilities/utilities";
2722

2823
suite("Utilities Test Suite", () => {
@@ -46,144 +41,4 @@ suite("Utilities Test Suite", () => {
4641
assert(result.includes("Swift version"));
4742
assert.strictEqual(result, stdout);
4843
});
49-
50-
suite("getRepositoryName", () => {
51-
test("regular url", () => {
52-
assert.strictEqual(
53-
getRepositoryName("https://github.com/swiftlang/vscode-swift.git"),
54-
"vscode-swift"
55-
);
56-
});
57-
58-
test("url does not end in .git", () => {
59-
assert.strictEqual(
60-
getRepositoryName("https://github.com/swiftlang/vscode-swift"),
61-
"vscode-swift"
62-
);
63-
});
64-
65-
test("URL contains a trailing slash", () => {
66-
assert.strictEqual(
67-
getRepositoryName("https://github.com/swiftlang/vscode-swift.git/"),
68-
"vscode-swift"
69-
);
70-
});
71-
test("Name contains a dot", () => {
72-
assert.strictEqual(
73-
getRepositoryName("https://github.com/swiftlang/vscode.swift.git"),
74-
"vscode.swift"
75-
);
76-
});
77-
78-
test("Name contains .git", () => {
79-
assert.strictEqual(
80-
getRepositoryName("https://github.com/swiftlang/vscode.git.git"),
81-
"vscode.git"
82-
);
83-
});
84-
});
85-
86-
suite("getErrorDescription", () => {
87-
test('should return "No error provided" when the error is null or undefined', () => {
88-
assert.strictEqual(getErrorDescription(null), "No error provided");
89-
assert.strictEqual(getErrorDescription(undefined), "No error provided");
90-
});
91-
92-
test("should return the stderr property if present", () => {
93-
const errorWithStderr = { stderr: "This is an error from stderr" };
94-
const result = getErrorDescription(errorWithStderr);
95-
assert.strictEqual(result, "This is an error from stderr");
96-
});
97-
98-
test("should return the error property if present", () => {
99-
const errorWithErrorProperty = { error: "This is an error message" };
100-
const result = getErrorDescription(errorWithErrorProperty);
101-
assert.strictEqual(result, JSON.stringify("This is an error message"));
102-
});
103-
104-
test("should return the message property if the error is an instance of Error", () => {
105-
const standardError = new Error("This is a standard error message");
106-
const result = getErrorDescription(standardError);
107-
assert.strictEqual(result, "This is a standard error message");
108-
});
109-
110-
test("should return a stringified version of the error if it is an object without stderr or error properties", () => {
111-
const genericObjectError = { message: "Generic error", code: 500 };
112-
const result = getErrorDescription(genericObjectError);
113-
assert.strictEqual(result, JSON.stringify(genericObjectError));
114-
});
115-
116-
test("should return a stringified version of the error if it is a string", () => {
117-
const stringError = "This is a string error";
118-
const result = getErrorDescription(stringError);
119-
assert.strictEqual(result, JSON.stringify(stringError));
120-
});
121-
122-
test("should return a stringified version of the error if it is a number", () => {
123-
const numericError = 404;
124-
const result = getErrorDescription(numericError);
125-
assert.strictEqual(result, JSON.stringify(numericError));
126-
});
127-
128-
test("should return a stringified version of an array if passed as error", () => {
129-
const arrayError = ["Error in item 1", "Error in item 2"];
130-
const result = getErrorDescription(arrayError);
131-
assert.strictEqual(result, JSON.stringify(arrayError));
132-
});
133-
});
134-
135-
suite("hashString", () => {
136-
test("empty string", () => {
137-
assert.strictEqual(hashString(""), 3338908027751811);
138-
});
139-
140-
test("non empty string", () => {
141-
assert.strictEqual(hashString("foo"), 6104293464250660);
142-
});
143-
});
144-
145-
suite("stringArrayInEnglish", () => {
146-
test("should return a single element unchanged", () => {
147-
assert.strictEqual(stringArrayInEnglish(["a"]), "a");
148-
});
149-
150-
test("should use 'and' to concatinate two elements", () => {
151-
assert.strictEqual(stringArrayInEnglish(["a", "b"]), "a and b");
152-
});
153-
154-
test("should handle three or more elements", () => {
155-
assert.strictEqual(stringArrayInEnglish(["a", "b", "c"]), "a, b and c");
156-
});
157-
});
158-
159-
suite("regexEscapedString", () => {
160-
test("should escape special regex characters in a string", () => {
161-
assert.strictEqual(
162-
regexEscapedString("a.b(c)d[e]f$g^h?i|j/k:l"),
163-
"a\\.b\\(c\\)d\\[e\\]f\\$g\\^h\\?i\\|j\\/k\\:l"
164-
);
165-
});
166-
167-
test("should not escape characters that are not special regex characters", () => {
168-
assert.strictEqual(regexEscapedString("abcde12345"), "abcde12345");
169-
});
170-
171-
test("should escape a string that contains only special regex characters", () => {
172-
assert.strictEqual(
173-
regexEscapedString(".^$|()?[]/:"),
174-
"\\.\\^\\$\\|\\(\\)\\?\\[\\]\\/\\:"
175-
);
176-
});
177-
178-
test("should escape a string that omits some characters", () => {
179-
assert.strictEqual(
180-
regexEscapedString(".^$|()?[]/:", new Set(["^", "$", "a"])),
181-
"\\.^$\\|\\(\\)\\?\\[\\]\\/\\:"
182-
);
183-
});
184-
185-
test("should return an empty string when input is an empty string", () => {
186-
assert.strictEqual(regexEscapedString(""), "");
187-
});
188-
});
18944
});

0 commit comments

Comments
 (0)