Skip to content

Commit e71bfd6

Browse files
authored
default to XDG_RUNTIME_DIR for mac/linux in temp file for testing comms (#24859)
fixes #24406
1 parent 0c4a30d commit e71bfd6

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

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

+19-2
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

+44-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as assert from 'assert';
22
import * as sinon from 'sinon';
33
import * as fs from 'fs';
44
import * as path from 'path';
5-
import * as os from 'os';
65
import { writeTestIdsFile } from '../../../client/testing/testController/common/utils';
76
import { EXTENSION_ROOT_DIR } from '../../../client/constants';
87

@@ -21,11 +20,13 @@ suite('writeTestIdsFile tests', () => {
2120
const testIds = ['test1', 'test2', 'test3'];
2221
const writeFileStub = sandbox.stub(fs.promises, 'writeFile').resolves();
2322

24-
const result = await writeTestIdsFile(testIds);
25-
26-
const tmpDir = os.tmpdir();
23+
// Set up XDG_RUNTIME_DIR
24+
process.env = {
25+
...process.env,
26+
XDG_RUNTIME_DIR: '/xdg/runtime/dir',
27+
};
2728

28-
assert.ok(result.startsWith(tmpDir));
29+
await writeTestIdsFile(testIds);
2930

3031
assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n')));
3132
});
@@ -48,3 +49,41 @@ suite('writeTestIdsFile tests', () => {
4849
assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n')));
4950
});
5051
});
52+
53+
suite('getTempDir tests', () => {
54+
let sandbox: sinon.SinonSandbox;
55+
let originalPlatform: NodeJS.Platform;
56+
let originalEnv: NodeJS.ProcessEnv;
57+
58+
setup(() => {
59+
sandbox = sinon.createSandbox();
60+
originalPlatform = process.platform;
61+
originalEnv = process.env;
62+
});
63+
64+
teardown(() => {
65+
sandbox.restore();
66+
Object.defineProperty(process, 'platform', { value: originalPlatform });
67+
process.env = originalEnv;
68+
});
69+
70+
test('should use XDG_RUNTIME_DIR on non-Windows if available', async () => {
71+
if (process.platform === 'win32') {
72+
return;
73+
}
74+
// Force platform to be Linux
75+
Object.defineProperty(process, 'platform', { value: 'linux' });
76+
77+
// Set up XDG_RUNTIME_DIR
78+
process.env = { ...process.env, XDG_RUNTIME_DIR: '/xdg/runtime/dir' };
79+
80+
const testIds = ['test1', 'test2', 'test3'];
81+
sandbox.stub(fs.promises, 'access').resolves();
82+
sandbox.stub(fs.promises, 'writeFile').resolves();
83+
84+
// This will use getTempDir internally
85+
const result = await writeTestIdsFile(testIds);
86+
87+
assert.ok(result.startsWith('/xdg/runtime/dir'));
88+
});
89+
});

0 commit comments

Comments
 (0)