Skip to content

Commit b75d38e

Browse files
aliko-strAlexZeitler
authored andcommitted
not starting with -d when incompatible flags are set
1 parent 3ebe9ef commit b75d38e

4 files changed

Lines changed: 102 additions & 11 deletions

File tree

src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,16 @@ const shouldUseDefaultNonInteractiveFlag = function (
246246
options: IDockerComposeOptions = {}
247247
): boolean {
248248
const commandOptions = options.commandOptions || []
249+
const noDetachModeFlags = [
250+
'--abort-on-container-exit',
251+
'--no-start',
252+
'--attach',
253+
'--attach-dependencies',
254+
'--exit-code-from'
255+
]
249256
const containsOtherNonInteractiveFlag = commandOptions.reduce(
250257
(memo: boolean, item: string | string[]) => {
251-
return (
252-
memo &&
253-
!item.includes('--abort-on-container-exit') &&
254-
!item.includes('--no-start')
255-
)
258+
return memo && noDetachModeFlags.every((flag) => !item.includes(flag))
256259
},
257260
true
258261
)

src/v2.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,16 @@ const shouldUseDefaultNonInteractiveFlag = function (
341341
options: IDockerComposeOptions = {}
342342
): boolean {
343343
const commandOptions = options.commandOptions || []
344+
const noDetachModeFlags = [
345+
'--abort-on-container-exit',
346+
'--no-start',
347+
'--attach',
348+
'--attach-dependencies',
349+
'--exit-code-from'
350+
]
344351
const containsOtherNonInteractiveFlag = commandOptions.reduce(
345352
(memo: boolean, item: string | string[]) => {
346-
return (
347-
memo &&
348-
!item.includes('--abort-on-container-exit') &&
349-
!item.includes('--no-start')
350-
)
353+
return memo && noDetachModeFlags.every((flag) => !item.includes(flag))
351354
},
352355
true
353356
)

test/v2/compose.test.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
1+
import { describe, it, expect, beforeEach, afterEach, vi, beforeAll } from 'vitest'
22
import Docker, { ContainerInfo } from 'dockerode'
33
import * as compose from '../../src/v2'
44
import * as path from 'path'
@@ -1101,3 +1101,80 @@ describe('passed callback fn', (): void => {
11011101
await compose.downAll(config)
11021102
})
11031103
})
1104+
1105+
describe('when upAll is called', () => {
1106+
// relying on bash echo to know when a container is up and has its stdout forwarded to this process (aka, not --detach)
1107+
const ECHO_MSG = 'hello from a container tst msg'
1108+
const options2test = [
1109+
['--attach', 'echo', true],
1110+
['--exit-code-from', 'echo', true],
1111+
['--abort-on-container-exit', 'echo', true],
1112+
['--wait', 'echo', false]
1113+
]
1114+
1115+
beforeAll(async () => {
1116+
await compose.downAll({
1117+
cwd: path.join(__dirname),
1118+
log: logOutput,
1119+
config: 'docker-compose-echo.yml'
1120+
})
1121+
})
1122+
1123+
afterEach(async () => {
1124+
await compose.kill({
1125+
cwd: path.join(__dirname),
1126+
log: logOutput,
1127+
config: 'docker-compose-echo.yml'
1128+
})
1129+
})
1130+
1131+
options2test.forEach((optPair) => {
1132+
it(`with ${optPair[0]}, a container gets started ${
1133+
optPair[2] ? 'not' : ''
1134+
} in the detached mode`, () => {
1135+
return new Promise((resolve, reject) => {
1136+
let doneFlag = false
1137+
compose
1138+
.upAll({
1139+
cwd: path.join(__dirname),
1140+
log: logOutput,
1141+
config: 'docker-compose-echo.yml',
1142+
commandOptions: [[optPair[0], optPair[1]]],
1143+
callback: (chunk, streamType) => {
1144+
if (
1145+
streamType === 'stdout' &&
1146+
!doneFlag &&
1147+
chunk.toString().includes('|') // else these are set-up messages, not echos from a container
1148+
) {
1149+
doneFlag = true
1150+
expect(chunk.toString().includes(ECHO_MSG)).toBe(optPair[2])
1151+
optPair[2]
1152+
? resolve('all ok')
1153+
: reject(
1154+
`Container was run ${
1155+
optPair[2] ? '' : 'not'
1156+
} in the detached mode.`
1157+
)
1158+
}
1159+
}
1160+
})
1161+
.then(() => {
1162+
optPair[2]
1163+
? reject(
1164+
`Container was run ${
1165+
optPair[2] ? 'not' : ''
1166+
} in the detached mode.`
1167+
)
1168+
: resolve('all ok')
1169+
})
1170+
.catch((e) => {
1171+
if (e.exitCode === 137) {
1172+
// swallowing this reject -- so it doesn't complain about being SIGKILLed
1173+
return
1174+
}
1175+
throw e // else re-throwing
1176+
})
1177+
})
1178+
})
1179+
})
1180+
})

test/v2/docker-compose-echo.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '2'
2+
3+
services:
4+
echo:
5+
image: bash:devel-alpine3.19@sha256:cf3e4f54a774e9a896512766aa94934f9898444fa50827370767d461e6e7a41b
6+
container_name: compose_test_web_2_hello_world
7+
command: bash -c 'for i in {1..10}; do sleep 0.5 && echo "hello from a container tst msg"; done'
8+

0 commit comments

Comments
 (0)