|
| 1 | +import { assert } from 'chai'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { getCLIHost } from '../spec-common/cliHost'; |
| 6 | +import { buildAndExtendDockerCompose } from '../spec-node/dockerCompose'; |
| 7 | +import { nullLog } from '../spec-utils/log'; |
| 8 | +import { testSubstitute } from './testUtils'; |
| 9 | + |
| 10 | +describe('dockerignore handling', () => { |
| 11 | + it('copies Dockerfile-specific dockerignore files next to generated compose Dockerfiles', async () => { |
| 12 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), 'dockerignore-compose-')); |
| 13 | + const workspace = path.join(root, 'workspace'); |
| 14 | + const devcontainerDir = path.join(workspace, '.devcontainer', 'app'); |
| 15 | + const composeFile = path.join(devcontainerDir, 'docker-compose.yaml'); |
| 16 | + const dockerfile = path.join(devcontainerDir, 'dev.Dockerfile'); |
| 17 | + const sourceDockerIgnore = `${dockerfile}.dockerignore`; |
| 18 | + const dockerIgnoreContent = '*\n!/app/requirements.txt\n'; |
| 19 | + let generatedFolder: string | undefined; |
| 20 | + |
| 21 | + try { |
| 22 | + await fs.mkdir(devcontainerDir, { recursive: true }); |
| 23 | + await fs.writeFile(dockerfile, 'FROM ubuntu:24.04\nRUN echo hello\n'); |
| 24 | + await fs.writeFile(sourceDockerIgnore, dockerIgnoreContent); |
| 25 | + await fs.writeFile(composeFile, [ |
| 26 | + 'services:', |
| 27 | + ' app:', |
| 28 | + ' build:', |
| 29 | + ' context: ../..', |
| 30 | + ' dockerfile: .devcontainer/app/dev.Dockerfile', |
| 31 | + '', |
| 32 | + ].join('\n')); |
| 33 | + |
| 34 | + const fakeDocker = path.join(root, 'fake-docker'); |
| 35 | + await fs.writeFile(fakeDocker, `#!/bin/sh |
| 36 | +set -eu |
| 37 | +mode="" |
| 38 | +for arg in "$@"; do |
| 39 | + case "$arg" in |
| 40 | + config) mode="config" ;; |
| 41 | + build) mode="build" ;; |
| 42 | + esac |
| 43 | +done |
| 44 | +if [ "$1" = "inspect" ]; then |
| 45 | + printf '%s' '[{"Id":"img","Architecture":"amd64","Os":"linux","Config":{"User":"","Env":[],"Labels":{}}}]' |
| 46 | + exit 0 |
| 47 | +fi |
| 48 | +if [ "$1" = "compose" ] && [ "$mode" = "config" ]; then |
| 49 | + cat <<'EOF' |
| 50 | +services: |
| 51 | + app: |
| 52 | + build: |
| 53 | + context: ${workspace} |
| 54 | + dockerfile: .devcontainer/app/dev.Dockerfile |
| 55 | +EOF |
| 56 | + exit 0 |
| 57 | +fi |
| 58 | +if [ "$1" = "compose" ] && [ "$mode" = "build" ]; then |
| 59 | + exit 0 |
| 60 | +fi |
| 61 | +printf 'unexpected %s\n' "$*" >&2 |
| 62 | +exit 1 |
| 63 | +`); |
| 64 | + await fs.chmod(fakeDocker, 0o755); |
| 65 | + |
| 66 | + const cliHost = await getCLIHost(workspace, async () => undefined, false); |
| 67 | + const common = { |
| 68 | + cliHost, |
| 69 | + env: process.env, |
| 70 | + output: nullLog, |
| 71 | + package: { name: 'test', version: '0.0.0' }, |
| 72 | + persistedFolder: path.join(root, 'persisted'), |
| 73 | + skipPersistingCustomizationsFromFeatures: false, |
| 74 | + omitSyntaxDirective: false, |
| 75 | + } as any; |
| 76 | + const params = { |
| 77 | + common, |
| 78 | + dockerCLI: fakeDocker, |
| 79 | + dockerComposeCLI: async () => ({ version: '2.20.0', cmd: fakeDocker, args: ['compose'] }), |
| 80 | + dockerEnv: process.env, |
| 81 | + isPodman: false, |
| 82 | + buildKitVersion: undefined, |
| 83 | + dockerEngineVersion: undefined, |
| 84 | + isTTY: false, |
| 85 | + buildPlatformInfo: { os: 'linux', arch: 'amd64' }, |
| 86 | + targetPlatformInfo: { os: 'linux', arch: 'amd64' }, |
| 87 | + } as any; |
| 88 | + const config = { service: 'app' }; |
| 89 | + |
| 90 | + const result = await buildAndExtendDockerCompose( |
| 91 | + { config, raw: config, substitute: testSubstitute } as any, |
| 92 | + 'proj', |
| 93 | + params, |
| 94 | + [composeFile], |
| 95 | + undefined, |
| 96 | + [], |
| 97 | + [], |
| 98 | + false, |
| 99 | + common.persistedFolder, |
| 100 | + 'docker-compose.devcontainer.build', |
| 101 | + '', |
| 102 | + {}, |
| 103 | + true, |
| 104 | + undefined, |
| 105 | + true |
| 106 | + ); |
| 107 | + |
| 108 | + assert.lengthOf(result.additionalComposeOverrideFiles, 1); |
| 109 | + const override = await fs.readFile(result.additionalComposeOverrideFiles[0], 'utf8'); |
| 110 | + const match = override.match(/dockerfile: (.+)/); |
| 111 | + assert.isNotNull(match); |
| 112 | + const generatedDockerfile = match![1].trim(); |
| 113 | + const generatedDockerIgnore = `${generatedDockerfile}.dockerignore`; |
| 114 | + |
| 115 | + generatedFolder = path.dirname(generatedDockerfile); |
| 116 | + assert.strictEqual(await fs.readFile(generatedDockerIgnore, 'utf8'), dockerIgnoreContent); |
| 117 | + } finally { |
| 118 | + await fs.rm(root, { recursive: true, force: true }); |
| 119 | + if (generatedFolder) { |
| 120 | + await fs.rm(generatedFolder, { recursive: true, force: true }); |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | +}); |
0 commit comments