Skip to content

Commit 19f369f

Browse files
authored
Add --config to build command (microsoft/vscode-remote-release#8068)
1 parent ede5c3a commit 19f369f

7 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/spec-node/devContainersSpecCLI.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ function buildOptions(y: Argv) {
452452
'docker-path': { type: 'string', description: 'Docker CLI path.' },
453453
'docker-compose-path': { type: 'string', description: 'Docker Compose CLI path.' },
454454
'workspace-folder': { type: 'string', required: true, description: 'Workspace folder path. The devcontainer.json will be looked up relative to this path.' },
455+
'config': { type: 'string', description: 'devcontainer.json path. The default is to use .devcontainer/devcontainer.json or, if that does not exist, .devcontainer.json in the workspace folder.' },
455456
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level.' },
456457
'log-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text' as 'text', description: 'Log format.' },
457458
'no-cache': { type: 'boolean', default: false, description: 'Builds the image with `--no-cache`.' },
@@ -486,6 +487,7 @@ async function doBuild({
486487
'docker-path': dockerPath,
487488
'docker-compose-path': dockerComposePath,
488489
'workspace-folder': workspaceFolderArg,
490+
config: configParam,
489491
'log-level': logLevel,
490492
'log-format': logFormat,
491493
'no-cache': buildNoCache,
@@ -505,7 +507,7 @@ async function doBuild({
505507
};
506508
try {
507509
const workspaceFolder = path.resolve(process.cwd(), workspaceFolderArg);
508-
const configFile: URI | undefined = /* config ? URI.file(path.resolve(process.cwd(), config)) : */ undefined; // TODO
510+
const configFile: URI | undefined = configParam ? URI.file(path.resolve(process.cwd(), configParam)) : undefined;
509511
const overrideConfigFile: URI | undefined = /* overrideConfig ? URI.file(path.resolve(process.cwd(), overrideConfig)) : */ undefined;
510512
const addCacheFroms = addCacheFrom ? (Array.isArray(addCacheFrom) ? addCacheFrom as string[] : [addCacheFrom]) : [];
511513
const additionalFeatures = additionalFeaturesJson ? jsonc.parse(additionalFeaturesJson) as Record<string, string | boolean | Record<string, string | boolean>> : {};

src/test/cli.build.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as assert from 'assert';
88
import * as path from 'path';
99
import * as os from 'os';
1010
import { buildKitOptions, shellExec } from './testUtils';
11+
import { ImageDetails } from '../spec-shutdown/dockerUtils';
12+
import { envListToObj } from '../spec-node/utils';
1113

1214
const pkg = require('../../package.json');
1315

@@ -249,5 +251,14 @@ describe('Dev Containers CLI', function () {
249251

250252
await shellExec(`docker rm -f ${containerId}`);
251253
});
254+
255+
it('should build with config in subfolder', async () => {
256+
const res = await shellExec(`${cli} build --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json --image-name test-subfolder-config`);
257+
const response = JSON.parse(res.stdout);
258+
assert.strictEqual(response.outcome, 'success');
259+
260+
const details = JSON.parse((await shellExec(`docker inspect test-subfolder-config`)).stdout)[0] as ImageDetails;
261+
assert.strictEqual(envListToObj(details.Config.Env).SUBFOLDER_CONFIG_IMAGE_ENV, 'true');
262+
});
252263
});
253264
});

src/test/cli.exec.base.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ export function describeTests2({ text, options }: BuildKitOption) {
363363
assert.equal(success, false, 'expect non-successful call');
364364
});
365365
}
366+
367+
it('should exec with config in subfolder', async () => {
368+
const upRes = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json`);
369+
const response = JSON.parse(upRes.stdout);
370+
assert.strictEqual(response.outcome, 'success');
371+
372+
const execRes = await shellExec(`${cli} exec --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json bash -c 'printenv SUBFOLDER_CONFIG_REMOTE_ENV'`);
373+
assert.strictEqual(execRes.stdout.trim(), 'true');
374+
375+
await shellExec(`docker rm -f ${response.containerId}`);
376+
});
366377
});
367378
});
368379
}

src/test/cli.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ describe('Dev Containers CLI', function () {
5252
}
5353
assert.equal(success, false, 'expect non-successful call');
5454
});
55+
56+
it('should run with config in subfolder', async () => {
57+
const upRes = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json --skip-post-create`);
58+
const upResponse = JSON.parse(upRes.stdout);
59+
assert.strictEqual(upResponse.outcome, 'success');
60+
61+
await shellExec(`docker exec ${upResponse.containerId} bash -c '! test -f /subfolderConfigPostCreateCommand.txt'`);
62+
63+
const runRes = await shellExec(`${cli} run-user-commands --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json`);
64+
const runResponse = JSON.parse(runRes.stdout);
65+
assert.strictEqual(runResponse.outcome, 'success');
66+
67+
await shellExec(`docker exec ${upResponse.containerId} test -f /subfolderConfigPostCreateCommand.txt`);
68+
69+
await shellExec(`docker rm -f ${upResponse.containerId}`);
70+
});
5571
});
5672

5773
describe('Command read-configuration', () => {
@@ -102,5 +118,11 @@ describe('Dev Containers CLI', function () {
102118
await shellExec(`docker rm -f ${containerId}`);
103119
}
104120
});
121+
122+
it('should read config in subfolder', async () => {
123+
const res = await shellExec(`${cli} read-configuration --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json`);
124+
const response = JSON.parse(res.stdout);
125+
assert.strictEqual(response.configuration.remoteEnv.SUBFOLDER_CONFIG_REMOTE_ENV, 'true');
126+
});
105127
});
106128
});

src/test/cli.up.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,15 @@ describe('Dev Containers CLI', function () {
184184

185185
await shellExec(`docker rm -f ${containerId}`);
186186
});
187+
188+
it('should run with config in subfolder', async () => {
189+
const upRes = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/dockerfile-without-features --config ${__dirname}/configs/dockerfile-without-features/.devcontainer/subfolder/devcontainer.json`);
190+
const response = JSON.parse(upRes.stdout);
191+
assert.strictEqual(response.outcome, 'success');
192+
193+
await shellExec(`docker exec ${response.containerId} test -f /subfolderConfigPostCreateCommand.txt`);
194+
195+
await shellExec(`docker rm -f ${response.containerId}`);
196+
});
187197
});
188198
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM ubuntu:latest
2+
3+
ENV SUBFOLDER_CONFIG_IMAGE_ENV=true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile"
4+
},
5+
"remoteEnv": {
6+
"SUBFOLDER_CONFIG_REMOTE_ENV": "true"
7+
},
8+
"postCreateCommand": "touch /subfolderConfigPostCreateCommand.txt"
9+
}

0 commit comments

Comments
 (0)