Skip to content

Commit b4f9285

Browse files
committed
Fix config dir test
1 parent 6af8bb9 commit b4f9285

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

src/auth.test.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,33 @@ suite("Authenticate", () => {
1616

1717
teardown(() => utils.resetEnv())
1818

19+
const assertDir = (actual: string, expected: string) => {
20+
assert.match(actual, new RegExp(expected.replace(/\\/g, "\\$&") + "$"))
21+
}
22+
1923
const assertDirs = (dir: string) => {
20-
assert.match(auth.getConfigDir("linux"), new RegExp(path.join(dir, ".config$")))
21-
assert.match(auth.getConfigDir("freebsd"), new RegExp(path.join(dir, ".config$")))
22-
assert.match(auth.getConfigDir("win32"), new RegExp(path.join(dir, "AppData/Roaming$")))
23-
assert.match(auth.getConfigDir("darwin"), new RegExp(path.join(dir, "Library/Application Support$")))
24+
assertDir(auth.getConfigDir("linux"), path.join(dir, ".config"))
25+
assertDir(auth.getConfigDir("freebsd"), path.join(dir, ".config"))
26+
assertDir(auth.getConfigDir("win32"), path.join(dir, "AppData/Roaming"))
27+
assertDir(auth.getConfigDir("darwin"), path.join(dir, "Library/Application Support"))
2428
}
2529

2630
test("getConfigDir", async () => {
2731
// Make sure local config mocks work.
2832
const tmpDir = await utils.tmpdir(tmpPath)
2933
utils.setEnv("HOME", tmpDir)
34+
utils.setEnv("USERPROFILE", tmpDir)
35+
utils.setEnv("XDG_CONFIG_HOME", undefined)
36+
utils.setEnv("APPDATA", undefined)
3037
assertDirs(tmpDir)
3138

3239
// Make sure the global mock also works. For example the Linux temp config
3340
// directory looks like: /tmp/coder/tests/config/tmp-Dzfqwl/home/.config
3441
// This runs after the local mock to make sure environment variables are
3542
// being restored correctly.
3643
utils.resetEnv()
44+
utils.setEnv("XDG_CONFIG_HOME", undefined)
45+
utils.setEnv("APPDATA", undefined)
3746
assertDirs("tests/config/.+/home")
3847
})
3948

src/test/runTest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function main() {
1919
const tmpPath = "tests/config"
2020
await utils.clean(tmpPath)
2121
const temp = await utils.tmpdir(tmpPath)
22-
process.env.HOME = path.join(temp, "home")
22+
process.env.HOME = process.env.USERPROFILE = path.join(temp, "home")
2323

2424
// The folder containing the Extension Manifest package.json
2525
// Passed to `--extensionDevelopmentPath`

src/utils.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ suite("Utils", () => {
5555
assert.strictEqual(process.env[key], undefined)
5656
utils.setEnv(key, "baz")
5757
assert.strictEqual(process.env[key], "baz")
58+
utils.setEnv(key, undefined)
59+
assert.strictEqual(process.env[key], undefined)
60+
utils.setEnv(key, "baz")
5861
utils.resetEnv()
5962
assert.strictEqual(process.env[key], undefined)
6063
})

src/utils.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,17 @@ export const resetEnv = (): void => {
147147
/**
148148
* Set an environment variable that will be reset on a call to `resetEnv`.
149149
*/
150-
export const setEnv = (key: string, value: string): void => {
150+
export const setEnv = (key: string, value: string | undefined): void => {
151151
const original = process.env[key]
152-
process.env[key] = value
152+
// You cannot set process.env properties to undefined as they will just be set
153+
// it to the literal string "undefined" so delete instead.
154+
if (typeof value === "undefined") {
155+
delete process.env[key]
156+
} else {
157+
process.env[key] = value
158+
}
153159
envResets.push(() => {
154-
// You cannot set process.env properties to undefined as they will just be
155-
// set it to the literal string "undefined" so delete instead.
160+
// Same deal with undefined here.
156161
if (typeof original === "undefined") {
157162
delete process.env[key]
158163
} else {

0 commit comments

Comments
 (0)