From 1de31500001dce39a6968e8197852a0cc8ec01b2 Mon Sep 17 00:00:00 2001 From: aliko Date: Fri, 5 Apr 2024 23:21:47 +0200 Subject: [PATCH 1/2] not starting with -d when incompatible flags are set --- src/index.ts | 13 +++--- src/v2.ts | 13 +++--- test/v2/compose.test.ts | 79 ++++++++++++++++++++++++++++++++- test/v2/docker-compose-echo.yml | 8 ++++ 4 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 test/v2/docker-compose-echo.yml diff --git a/src/index.ts b/src/index.ts index f2bb09df..960c7ac5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -246,13 +246,16 @@ const shouldUseDefaultNonInteractiveFlag = function ( options: IDockerComposeOptions = {} ): boolean { const commandOptions = options.commandOptions || [] + const noDetachModeFlags = [ + '--abort-on-container-exit', + '--no-start', + '--attach', + '--attach-dependencies', + '--exit-code-from' + ] const containsOtherNonInteractiveFlag = commandOptions.reduce( (memo: boolean, item: string | string[]) => { - return ( - memo && - !item.includes('--abort-on-container-exit') && - !item.includes('--no-start') - ) + return memo && noDetachModeFlags.every((flag) => !item.includes(flag)) }, true ) diff --git a/src/v2.ts b/src/v2.ts index 2d317ab0..193459db 100644 --- a/src/v2.ts +++ b/src/v2.ts @@ -341,13 +341,16 @@ const shouldUseDefaultNonInteractiveFlag = function ( options: IDockerComposeOptions = {} ): boolean { const commandOptions = options.commandOptions || [] + const noDetachModeFlags = [ + '--abort-on-container-exit', + '--no-start', + '--attach', + '--attach-dependencies', + '--exit-code-from' + ] const containsOtherNonInteractiveFlag = commandOptions.reduce( (memo: boolean, item: string | string[]) => { - return ( - memo && - !item.includes('--abort-on-container-exit') && - !item.includes('--no-start') - ) + return memo && noDetachModeFlags.every((flag) => !item.includes(flag)) }, true ) diff --git a/test/v2/compose.test.ts b/test/v2/compose.test.ts index de2389ce..218cc5a7 100644 --- a/test/v2/compose.test.ts +++ b/test/v2/compose.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' +import { describe, it, expect, beforeEach, afterEach, vi, beforeAll } from 'vitest' import Docker, { ContainerInfo } from 'dockerode' import * as compose from '../../src/v2' import * as path from 'path' @@ -1101,3 +1101,80 @@ describe('passed callback fn', (): void => { await compose.downAll(config) }) }) + +describe('when upAll is called', () => { + // relying on bash echo to know when a container is up and has its stdout forwarded to this process (aka, not --detach) + const ECHO_MSG = 'hello from a container tst msg' + const options2test = [ + ['--attach', 'echo', true], + ['--exit-code-from', 'echo', true], + ['--abort-on-container-exit', 'echo', true], + ['--wait', 'echo', false] + ] + + beforeAll(async () => { + await compose.downAll({ + cwd: path.join(__dirname), + log: logOutput, + config: 'docker-compose-echo.yml' + }) + }) + + afterEach(async () => { + await compose.kill({ + cwd: path.join(__dirname), + log: logOutput, + config: 'docker-compose-echo.yml' + }) + }) + + options2test.forEach((optPair) => { + it(`with ${optPair[0]}, a container gets started ${ + optPair[2] ? 'not' : '' + } in the detached mode`, () => { + return new Promise((resolve, reject) => { + let doneFlag = false + compose + .upAll({ + cwd: path.join(__dirname), + log: logOutput, + config: 'docker-compose-echo.yml', + commandOptions: [[optPair[0], optPair[1]]], + callback: (chunk, streamType) => { + if ( + streamType === 'stdout' && + !doneFlag && + chunk.toString().includes('|') // else these are set-up messages, not echos from a container + ) { + doneFlag = true + expect(chunk.toString().includes(ECHO_MSG)).toBe(optPair[2]) + optPair[2] + ? resolve('all ok') + : reject( + `Container was run ${ + optPair[2] ? '' : 'not' + } in the detached mode.` + ) + } + } + }) + .then(() => { + optPair[2] + ? reject( + `Container was run ${ + optPair[2] ? 'not' : '' + } in the detached mode.` + ) + : resolve('all ok') + }) + .catch((e) => { + if (e.exitCode === 137) { + // swallowing this reject -- so it doesn't complain about being SIGKILLed + return + } + throw e // else re-throwing + }) + }) + }) + }) +}) diff --git a/test/v2/docker-compose-echo.yml b/test/v2/docker-compose-echo.yml new file mode 100644 index 00000000..11559fa2 --- /dev/null +++ b/test/v2/docker-compose-echo.yml @@ -0,0 +1,8 @@ +version: '2' + +services: + echo: + image: bash:devel-alpine3.19@sha256:cf3e4f54a774e9a896512766aa94934f9898444fa50827370767d461e6e7a41b + container_name: compose_test_web_2_hello_world + command: bash -c 'for i in {1..10}; do sleep 0.5 && echo "hello from a container tst msg"; done' + From 6a49a3894dd442cda84d160f94ada2c15371ef83 Mon Sep 17 00:00:00 2001 From: aliko Date: Sun, 7 Apr 2024 23:37:53 +0200 Subject: [PATCH 2/2] tests - happy linter --- test/v2/compose.test.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/v2/compose.test.ts b/test/v2/compose.test.ts index 218cc5a7..def991d4 100644 --- a/test/v2/compose.test.ts +++ b/test/v2/compose.test.ts @@ -1,4 +1,12 @@ -import { describe, it, expect, beforeEach, afterEach, vi, beforeAll } from 'vitest' +import { + describe, + it, + expect, + beforeEach, + afterEach, + vi, + beforeAll +} from 'vitest' import Docker, { ContainerInfo } from 'dockerode' import * as compose from '../../src/v2' import * as path from 'path'