From 79a28917f47df9c228e57446932ca4e609063d76 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 3 Mar 2025 13:19:23 -0800 Subject: [PATCH 1/5] default for XDG_RUNTIME_DIR for mac/linux --- .vscode/launch.json | 17 +++-- .../testing/testController/common/utils.ts | 21 ++++- .../testing/testController/utils.unit.test.ts | 76 +++++++++++++++++++ 3 files changed, 104 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1e983413c8d4..28cbe9cd8c2d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -144,14 +144,15 @@ "stopOnEntry": false, "sourceMaps": true, "args": [ - "./out/test/**/*.unit.test.js", - "--require=out/test/unittests.js", - "--ui=tdd", - "--recursive", - "--colors", - //"--grep", "", - "--timeout=300000" - ], + "./out/test/**/*.unit.test.js", + "--require=out/test/unittests.js", + "--ui=tdd", + "--recursive", + "--colors", + "--grep", + "getTempDir tests", + "--timeout=300000" + ], "outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"], "preLaunchTask": "Compile", "skipFiles": ["/**"] diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index e3b37bf74e40..2ef26e0999ed 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -38,6 +38,22 @@ interface ExecutionResultMessage extends Message { params: ExecutionTestPayload; } +/** + * Retrieves the path to the temporary directory. + * + * On Windows, it returns the default temporary directory. + * On macOS/Linux, it prefers the `XDG_RUNTIME_DIR` environment variable if set, + * otherwise, it falls back to the default temporary directory. + * + * @returns {string} The path to the temporary directory. + */ +function getTempDir(): string { + if (process.platform === 'win32') { + return os.tmpdir(); // Default Windows behavior + } + return process.env.XDG_RUNTIME_DIR || os.tmpdir(); // Prefer XDG_RUNTIME_DIR on macOS/Linux +} + /** * Writes an array of test IDs to a temporary file. * @@ -50,11 +66,12 @@ export async function writeTestIdsFile(testIds: string[]): Promise { const tempName = `test-ids-${randomSuffix}.txt`; // create temp file let tempFileName: string; + const tempDir: string = getTempDir(); try { traceLog('Attempting to use temp directory for test ids file, file name:', tempName); - tempFileName = path.join(os.tmpdir(), tempName); + tempFileName = path.join(tempDir, tempName); // attempt access to written file to check permissions - await fs.promises.access(os.tmpdir()); + await fs.promises.access(tempDir); } catch (error) { // Handle the error when accessing the temp directory traceError('Error accessing temp directory:', error, ' Attempt to use extension root dir instead'); diff --git a/src/test/testing/testController/utils.unit.test.ts b/src/test/testing/testController/utils.unit.test.ts index ff1a0c707678..ececfd1bc556 100644 --- a/src/test/testing/testController/utils.unit.test.ts +++ b/src/test/testing/testController/utils.unit.test.ts @@ -48,3 +48,79 @@ suite('writeTestIdsFile tests', () => { assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n'))); }); }); + +suite('getTempDir tests', () => { + let sandbox: sinon.SinonSandbox; + let originalPlatform: NodeJS.Platform; + let originalEnv: NodeJS.ProcessEnv; + + setup(() => { + sandbox = sinon.createSandbox(); + originalPlatform = process.platform; + originalEnv = process.env; + }); + + teardown(() => { + sandbox.restore(); + Object.defineProperty(process, 'platform', { value: originalPlatform }); + process.env = originalEnv; + }); + + test('should use os.tmpdir on Windows', async () => { + // Force platform to be Windows + Object.defineProperty(process, 'platform', { value: 'win32' }); + + const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/windows/temp/path'); + + const testIds = ['test1', 'test2', 'test3']; + sandbox.stub(fs.promises, 'access').resolves(); + sandbox.stub(fs.promises, 'writeFile').resolves(); + + // This will use getTempDir internally + const result = await writeTestIdsFile(testIds); + + assert.ok(result.startsWith('/windows/temp/path')); + assert.strictEqual(tmpDirStub.callCount, 1); + }); + + test('should use XDG_RUNTIME_DIR on non-Windows if available', async () => { + // Force platform to be Linux + Object.defineProperty(process, 'platform', { value: 'linux' }); + + // Set up XDG_RUNTIME_DIR + process.env = { ...process.env, XDG_RUNTIME_DIR: '/xdg/runtime/dir' }; + + const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/fallback/tmp/dir'); + + const testIds = ['test1', 'test2', 'test3']; + sandbox.stub(fs.promises, 'access').resolves(); + sandbox.stub(fs.promises, 'writeFile').resolves(); + + // This will use getTempDir internally + const result = await writeTestIdsFile(testIds); + + assert.ok(result.startsWith('/xdg/runtime/dir')); + assert.strictEqual(tmpDirStub.callCount, 0); // tmpdir should not be called + }); + + test('should fall back to os.tmpdir on non-Windows if XDG_RUNTIME_DIR not available', async () => { + // Force platform to be macOS + Object.defineProperty(process, 'platform', { value: 'darwin' }); + + // Ensure XDG_RUNTIME_DIR is not set + process.env = { ...process.env }; + delete process.env.XDG_RUNTIME_DIR; + + const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/fallback/tmp/dir'); + + const testIds = ['test1', 'test2', 'test3']; + sandbox.stub(fs.promises, 'access').resolves(); + sandbox.stub(fs.promises, 'writeFile').resolves(); + + // This will use getTempDir internally + const result = await writeTestIdsFile(testIds); + + assert.ok(result.startsWith('/fallback/tmp/dir')); + assert.strictEqual(tmpDirStub.callCount, 1); + }); +}); From f73870f23c3c33939ba08165e1b4a77d4a5a0cbb Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 3 Mar 2025 13:24:40 -0800 Subject: [PATCH 2/5] fix --- .vscode/launch.json | 17 ++++++++--------- .../testing/testController/common/utils.ts | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 28cbe9cd8c2d..1e983413c8d4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -144,15 +144,14 @@ "stopOnEntry": false, "sourceMaps": true, "args": [ - "./out/test/**/*.unit.test.js", - "--require=out/test/unittests.js", - "--ui=tdd", - "--recursive", - "--colors", - "--grep", - "getTempDir tests", - "--timeout=300000" - ], + "./out/test/**/*.unit.test.js", + "--require=out/test/unittests.js", + "--ui=tdd", + "--recursive", + "--colors", + //"--grep", "", + "--timeout=300000" + ], "outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"], "preLaunchTask": "Compile", "skipFiles": ["/**"] diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index 2ef26e0999ed..9923d7ec3e12 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -40,11 +40,11 @@ interface ExecutionResultMessage extends Message { /** * Retrieves the path to the temporary directory. - * + * * On Windows, it returns the default temporary directory. * On macOS/Linux, it prefers the `XDG_RUNTIME_DIR` environment variable if set, * otherwise, it falls back to the default temporary directory. - * + * * @returns {string} The path to the temporary directory. */ function getTempDir(): string { From 52148926738d389f81dcc857978a9db3ef48e046 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 3 Mar 2025 13:37:23 -0800 Subject: [PATCH 3/5] remove incorrectly designed tests --- .../testing/testController/utils.unit.test.ts | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/src/test/testing/testController/utils.unit.test.ts b/src/test/testing/testController/utils.unit.test.ts index ececfd1bc556..4f2e3ac4942c 100644 --- a/src/test/testing/testController/utils.unit.test.ts +++ b/src/test/testing/testController/utils.unit.test.ts @@ -66,23 +66,6 @@ suite('getTempDir tests', () => { process.env = originalEnv; }); - test('should use os.tmpdir on Windows', async () => { - // Force platform to be Windows - Object.defineProperty(process, 'platform', { value: 'win32' }); - - const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/windows/temp/path'); - - const testIds = ['test1', 'test2', 'test3']; - sandbox.stub(fs.promises, 'access').resolves(); - sandbox.stub(fs.promises, 'writeFile').resolves(); - - // This will use getTempDir internally - const result = await writeTestIdsFile(testIds); - - assert.ok(result.startsWith('/windows/temp/path')); - assert.strictEqual(tmpDirStub.callCount, 1); - }); - test('should use XDG_RUNTIME_DIR on non-Windows if available', async () => { // Force platform to be Linux Object.defineProperty(process, 'platform', { value: 'linux' }); @@ -90,8 +73,6 @@ suite('getTempDir tests', () => { // Set up XDG_RUNTIME_DIR process.env = { ...process.env, XDG_RUNTIME_DIR: '/xdg/runtime/dir' }; - const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/fallback/tmp/dir'); - const testIds = ['test1', 'test2', 'test3']; sandbox.stub(fs.promises, 'access').resolves(); sandbox.stub(fs.promises, 'writeFile').resolves(); @@ -100,27 +81,5 @@ suite('getTempDir tests', () => { const result = await writeTestIdsFile(testIds); assert.ok(result.startsWith('/xdg/runtime/dir')); - assert.strictEqual(tmpDirStub.callCount, 0); // tmpdir should not be called - }); - - test('should fall back to os.tmpdir on non-Windows if XDG_RUNTIME_DIR not available', async () => { - // Force platform to be macOS - Object.defineProperty(process, 'platform', { value: 'darwin' }); - - // Ensure XDG_RUNTIME_DIR is not set - process.env = { ...process.env }; - delete process.env.XDG_RUNTIME_DIR; - - const tmpDirStub = sandbox.stub(os, 'tmpdir').returns('/fallback/tmp/dir'); - - const testIds = ['test1', 'test2', 'test3']; - sandbox.stub(fs.promises, 'access').resolves(); - sandbox.stub(fs.promises, 'writeFile').resolves(); - - // This will use getTempDir internally - const result = await writeTestIdsFile(testIds); - - assert.ok(result.startsWith('/fallback/tmp/dir')); - assert.strictEqual(tmpDirStub.callCount, 1); }); }); From 99454564c077dd5f98efb7c71eddb1effb70a409 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 3 Mar 2025 13:51:16 -0800 Subject: [PATCH 4/5] fix statement for which path selected --- src/test/testing/testController/utils.unit.test.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/testing/testController/utils.unit.test.ts b/src/test/testing/testController/utils.unit.test.ts index 4f2e3ac4942c..78d7b331b34f 100644 --- a/src/test/testing/testController/utils.unit.test.ts +++ b/src/test/testing/testController/utils.unit.test.ts @@ -2,7 +2,6 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import * as fs from 'fs'; import * as path from 'path'; -import * as os from 'os'; import { writeTestIdsFile } from '../../../client/testing/testController/common/utils'; import { EXTENSION_ROOT_DIR } from '../../../client/constants'; @@ -21,11 +20,13 @@ suite('writeTestIdsFile tests', () => { const testIds = ['test1', 'test2', 'test3']; const writeFileStub = sandbox.stub(fs.promises, 'writeFile').resolves(); - const result = await writeTestIdsFile(testIds); - - const tmpDir = os.tmpdir(); + // Set up XDG_RUNTIME_DIR + process.env = { + ...process.env, + XDG_RUNTIME_DIR: '/xdg/runtime/dir', + }; - assert.ok(result.startsWith(tmpDir)); + await writeTestIdsFile(testIds); assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n'))); }); From 6a5569992b77afe1f8bca5e0bba17e9899310326 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 3 Mar 2025 14:11:46 -0800 Subject: [PATCH 5/5] add ignore for windows --- src/test/testing/testController/utils.unit.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/testing/testController/utils.unit.test.ts b/src/test/testing/testController/utils.unit.test.ts index 78d7b331b34f..4d2af9da3d5a 100644 --- a/src/test/testing/testController/utils.unit.test.ts +++ b/src/test/testing/testController/utils.unit.test.ts @@ -68,6 +68,9 @@ suite('getTempDir tests', () => { }); test('should use XDG_RUNTIME_DIR on non-Windows if available', async () => { + if (process.platform === 'win32') { + return; + } // Force platform to be Linux Object.defineProperty(process, 'platform', { value: 'linux' });