Skip to content

Commit 79a2891

Browse files
committed
default for XDG_RUNTIME_DIR for mac/linux
1 parent 054e682 commit 79a2891

File tree

3 files changed

+104
-10
lines changed

3 files changed

+104
-10
lines changed

.vscode/launch.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@
144144
"stopOnEntry": false,
145145
"sourceMaps": true,
146146
"args": [
147-
"./out/test/**/*.unit.test.js",
148-
"--require=out/test/unittests.js",
149-
"--ui=tdd",
150-
"--recursive",
151-
"--colors",
152-
//"--grep", "<suite name>",
153-
"--timeout=300000"
154-
],
147+
"./out/test/**/*.unit.test.js",
148+
"--require=out/test/unittests.js",
149+
"--ui=tdd",
150+
"--recursive",
151+
"--colors",
152+
"--grep",
153+
"getTempDir tests",
154+
"--timeout=300000"
155+
],
155156
"outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"],
156157
"preLaunchTask": "Compile",
157158
"skipFiles": ["<node_internals>/**"]

src/client/testing/testController/common/utils.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ interface ExecutionResultMessage extends Message {
3838
params: ExecutionTestPayload;
3939
}
4040

41+
/**
42+
* Retrieves the path to the temporary directory.
43+
*
44+
* On Windows, it returns the default temporary directory.
45+
* On macOS/Linux, it prefers the `XDG_RUNTIME_DIR` environment variable if set,
46+
* otherwise, it falls back to the default temporary directory.
47+
*
48+
* @returns {string} The path to the temporary directory.
49+
*/
50+
function getTempDir(): string {
51+
if (process.platform === 'win32') {
52+
return os.tmpdir(); // Default Windows behavior
53+
}
54+
return process.env.XDG_RUNTIME_DIR || os.tmpdir(); // Prefer XDG_RUNTIME_DIR on macOS/Linux
55+
}
56+
4157
/**
4258
* Writes an array of test IDs to a temporary file.
4359
*
@@ -50,11 +66,12 @@ export async function writeTestIdsFile(testIds: string[]): Promise<string> {
5066
const tempName = `test-ids-${randomSuffix}.txt`;
5167
// create temp file
5268
let tempFileName: string;
69+
const tempDir: string = getTempDir();
5370
try {
5471
traceLog('Attempting to use temp directory for test ids file, file name:', tempName);
55-
tempFileName = path.join(os.tmpdir(), tempName);
72+
tempFileName = path.join(tempDir, tempName);
5673
// attempt access to written file to check permissions
57-
await fs.promises.access(os.tmpdir());
74+
await fs.promises.access(tempDir);
5875
} catch (error) {
5976
// Handle the error when accessing the temp directory
6077
traceError('Error accessing temp directory:', error, ' Attempt to use extension root dir instead');

src/test/testing/testController/utils.unit.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,79 @@ suite('writeTestIdsFile tests', () => {
4848
assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n')));
4949
});
5050
});
51+
52+
suite('getTempDir tests', () => {
53+
let sandbox: sinon.SinonSandbox;
54+
let originalPlatform: NodeJS.Platform;
55+
let originalEnv: NodeJS.ProcessEnv;
56+
57+
setup(() => {
58+
sandbox = sinon.createSandbox();
59+
originalPlatform = process.platform;
60+
originalEnv = process.env;
61+
});
62+
63+
teardown(() => {
64+
sandbox.restore();
65+
Object.defineProperty(process, 'platform', { value: originalPlatform });
66+
process.env = originalEnv;
67+
});
68+
69+
test('should use os.tmpdir on Windows', async () => {
70+
// Force platform to be Windows
71+
Object.defineProperty(process, 'platform', { value: 'win32' });
72+
73+
const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/windows/temp/path');
74+
75+
const testIds = ['test1', 'test2', 'test3'];
76+
sandbox.stub(fs.promises, 'access').resolves();
77+
sandbox.stub(fs.promises, 'writeFile').resolves();
78+
79+
// This will use getTempDir internally
80+
const result = await writeTestIdsFile(testIds);
81+
82+
assert.ok(result.startsWith('/windows/temp/path'));
83+
assert.strictEqual(tmpDirStub.callCount, 1);
84+
});
85+
86+
test('should use XDG_RUNTIME_DIR on non-Windows if available', async () => {
87+
// Force platform to be Linux
88+
Object.defineProperty(process, 'platform', { value: 'linux' });
89+
90+
// Set up XDG_RUNTIME_DIR
91+
process.env = { ...process.env, XDG_RUNTIME_DIR: '/xdg/runtime/dir' };
92+
93+
const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/fallback/tmp/dir');
94+
95+
const testIds = ['test1', 'test2', 'test3'];
96+
sandbox.stub(fs.promises, 'access').resolves();
97+
sandbox.stub(fs.promises, 'writeFile').resolves();
98+
99+
// This will use getTempDir internally
100+
const result = await writeTestIdsFile(testIds);
101+
102+
assert.ok(result.startsWith('/xdg/runtime/dir'));
103+
assert.strictEqual(tmpDirStub.callCount, 0); // tmpdir should not be called
104+
});
105+
106+
test('should fall back to os.tmpdir on non-Windows if XDG_RUNTIME_DIR not available', async () => {
107+
// Force platform to be macOS
108+
Object.defineProperty(process, 'platform', { value: 'darwin' });
109+
110+
// Ensure XDG_RUNTIME_DIR is not set
111+
process.env = { ...process.env };
112+
delete process.env.XDG_RUNTIME_DIR;
113+
114+
const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/fallback/tmp/dir');
115+
116+
const testIds = ['test1', 'test2', 'test3'];
117+
sandbox.stub(fs.promises, 'access').resolves();
118+
sandbox.stub(fs.promises, 'writeFile').resolves();
119+
120+
// This will use getTempDir internally
121+
const result = await writeTestIdsFile(testIds);
122+
123+
assert.ok(result.startsWith('/fallback/tmp/dir'));
124+
assert.strictEqual(tmpDirStub.callCount, 1);
125+
});
126+
});

0 commit comments

Comments
 (0)