|
5 | 5 |
|
6 | 6 | import * as assert from 'assert'; |
7 | 7 | import * as path from 'path'; |
| 8 | +import * as os from 'os'; |
8 | 9 | import { BuildKitOption, commandMarkerTests, devContainerDown, devContainerStop, devContainerUp, pathExists, shellBufferExec, shellExec, shellPtyExec } from './testUtils'; |
9 | 10 |
|
10 | 11 | const pkg = require('../../package.json'); |
@@ -209,7 +210,7 @@ export function describeTests1({ text, options }: BuildKitOption) { |
209 | 210 | } |
210 | 211 |
|
211 | 212 | export function describeTests2({ text, options }: BuildKitOption) { |
212 | | - |
| 213 | + |
213 | 214 | describe('Dev Containers CLI', function () { |
214 | 215 | this.timeout('300s'); |
215 | 216 |
|
@@ -422,6 +423,60 @@ export function describeTests2({ text, options }: BuildKitOption) { |
422 | 423 |
|
423 | 424 | await shellExec(`docker rm -f ${response.containerId}`); |
424 | 425 | }); |
| 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 | + }); |
425 | 480 | }); |
426 | 481 | }); |
427 | 482 | } |
0 commit comments