Skip to content

Commit 3fbe2bc

Browse files
committed
exec test cases
1 parent 7533b0b commit 3fbe2bc

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/test/cli.exec.base.ts

Lines changed: 56 additions & 1 deletion
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 { BuildKitOption, commandMarkerTests, devContainerDown, devContainerStop, devContainerUp, pathExists, shellBufferExec, shellExec, shellPtyExec } from './testUtils';
910

1011
const pkg = require('../../package.json');
@@ -209,7 +210,7 @@ export function describeTests1({ text, options }: BuildKitOption) {
209210
}
210211

211212
export function describeTests2({ text, options }: BuildKitOption) {
212-
213+
213214
describe('Dev Containers CLI', function () {
214215
this.timeout('300s');
215216

@@ -422,6 +423,60 @@ export function describeTests2({ text, options }: BuildKitOption) {
422423

423424
await shellExec(`docker rm -f ${response.containerId}`);
424425
});
426+
427+
describe.only('Command exec with default workspace', () => {
428+
it('should fail gracefully when no config in current directory and no container-id', async () => {
429+
const tempDir = path.join(os.tmpdir(), 'devcontainer-exec-test-' + Date.now());
430+
await shellExec(`mkdir -p ${tempDir}`);
431+
const originalCwd = process.cwd();
432+
const absoluteTmpPath = path.resolve(__dirname, 'tmp');
433+
const absoluteCli = `npx --prefix ${absoluteTmpPath} devcontainer`;
434+
try {
435+
process.chdir(tempDir);
436+
let success = false;
437+
try {
438+
// Test exec without --workspace-folder (should default to current directory with no config)
439+
await shellExec(`${absoluteCli} exec echo "test"`);
440+
success = true;
441+
} catch (error) {
442+
console.log('Caught error as expected: ', error.stderr);
443+
// Should fail because there's no container or config
444+
assert.equal(error.error.code, 1, 'Should fail with exit code 1');
445+
}
446+
assert.equal(success, false, 'expect non-successful call');
447+
} finally {
448+
process.chdir(originalCwd);
449+
await shellExec(`rm -rf ${tempDir}`);
450+
}
451+
});
452+
453+
describe('with valid config in current directory', () => {
454+
let containerId: string | null = null;
455+
const testFolder = `${__dirname}/configs/image`;
456+
457+
beforeEach(async () => {
458+
containerId = (await devContainerUp(cli, testFolder, options)).containerId;
459+
});
460+
461+
afterEach(async () => await devContainerDown({ containerId }));
462+
463+
it('should execute command successfully when using current directory', async () => {
464+
const originalCwd = process.cwd();
465+
const absoluteTmpPath = path.resolve(__dirname, 'tmp');
466+
const absoluteCli = `npx --prefix ${absoluteTmpPath} devcontainer`;
467+
try {
468+
process.chdir(testFolder);
469+
// Test exec without --workspace-folder (should default to current directory)
470+
const res = await shellExec(`${absoluteCli} exec echo "hello world"`);
471+
assert.strictEqual(res.error, null);
472+
assert.match(res.stdout, /hello world/);
473+
} finally {
474+
process.chdir(originalCwd);
475+
}
476+
});
477+
});
478+
479+
});
425480
});
426481
});
427482
}

0 commit comments

Comments
 (0)