Skip to content

Commit d2d47d6

Browse files
committed
Read-user commands test
1 parent 202aa2d commit d2d47d6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/test/cli.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as assert from 'assert';
77
import * as path from 'path';
8+
import * as os from 'os';
89
import { devContainerDown, devContainerUp, shellExec } from './testUtils';
910

1011
const pkg = require('../../package.json');
@@ -99,6 +100,28 @@ describe('Dev Containers CLI', function () {
99100
await shellExec(`docker rm -f ${containerId}`);
100101
}
101102
});
103+
104+
it('run-user-commands should fail gracefully when no config in current directory and no container-id', async () => {
105+
const tempDir = path.join(os.tmpdir(), 'devcontainer-run-test-' + Date.now());
106+
await shellExec(`mkdir -p ${tempDir}`);
107+
const absoluteTmpPath = path.resolve(__dirname, 'tmp');
108+
const absoluteCli = `npx --prefix ${absoluteTmpPath} devcontainer`;
109+
const originalCwd = process.cwd();
110+
try {
111+
process.chdir(tempDir);
112+
let success = false;
113+
try {
114+
await shellExec(`${absoluteCli} run-user-commands`);
115+
success = true;
116+
} catch (error) {
117+
assert.equal(error.error.code, 1, 'Should fail with exit code 1');
118+
}
119+
assert.equal(success, false, 'expect non-successful call');
120+
} finally {
121+
process.chdir(originalCwd);
122+
await shellExec(`rm -rf ${tempDir}`);
123+
}
124+
});
102125
});
103126

104127
describe('Command read-configuration', () => {
@@ -155,5 +178,42 @@ describe('Dev Containers CLI', function () {
155178
const response = JSON.parse(res.stdout);
156179
assert.strictEqual(response.configuration.remoteEnv.SUBFOLDER_CONFIG_REMOTE_ENV, 'true');
157180
});
181+
182+
it('should use current directory for read-configuration when no workspace-folder provided', async () => {
183+
const testFolder = `${__dirname}/configs/image`;
184+
const absoluteTmpPath = path.resolve(__dirname, 'tmp');
185+
const absoluteCli = `npx --prefix ${absoluteTmpPath} devcontainer`;
186+
const originalCwd = process.cwd();
187+
try {
188+
process.chdir(testFolder);
189+
const res = await shellExec(`${absoluteCli} read-configuration`);
190+
const response = JSON.parse(res.stdout);
191+
assert.equal(response.configuration.image, 'ubuntu:latest');
192+
} finally {
193+
process.chdir(originalCwd);
194+
}
195+
});
196+
197+
it('should fail gracefully when no workspace-folder and no config in current directory', async () => {
198+
const tempDir = path.join(os.tmpdir(), 'devcontainer-test-' + Date.now());
199+
await shellExec(`mkdir -p ${tempDir}`);
200+
const absoluteTmpPath = path.resolve(__dirname, 'tmp');
201+
const absoluteCli = `npx --prefix ${absoluteTmpPath} devcontainer`;
202+
const originalCwd = process.cwd();
203+
try {
204+
process.chdir(tempDir);
205+
let success = false;
206+
try {
207+
await shellExec(`${absoluteCli} read-configuration`);
208+
success = true;
209+
} catch (error) {
210+
assert.equal(error.error.code, 1, 'Should fail with exit code 1');
211+
}
212+
assert.equal(success, false, 'expect non-successful call');
213+
} finally {
214+
process.chdir(originalCwd);
215+
await shellExec(`rm -rf ${tempDir}`);
216+
}
217+
});
158218
});
159219
});

0 commit comments

Comments
 (0)