Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not starting with -d when incompatible flags are set #273

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@
* Executes docker-compose command with common options
*/
export const execCompose = (
command,

Check warning on line 164 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'command' should be typed
args,

Check warning on line 165 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'args' should be typed
options: IDockerComposeOptions = {}
): Promise<IDockerComposeResult> =>
new Promise((resolve, reject): void => {
Expand Down Expand Up @@ -246,13 +246,16 @@
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
)
Expand Down
13 changes: 8 additions & 5 deletions src/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@
* Executes docker compose command with common options
*/
export const execCompose = (
command,

Check warning on line 259 in src/v2.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'command' should be typed
args,

Check warning on line 260 in src/v2.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'args' should be typed
options: IDockerComposeOptions = {}
): Promise<IDockerComposeResult> =>
new Promise((resolve, reject): void => {
Expand Down Expand Up @@ -341,13 +341,16 @@
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
)
Expand Down
87 changes: 86 additions & 1 deletion test/v2/compose.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
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'
Expand Down Expand Up @@ -157,7 +165,7 @@
await compose.logs('non_existent_service', {
cwd: path.join(__dirname)
})
} catch (error: any) {

Check warning on line 168 in test/v2/compose.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
failedResult = error.exitCode
}
expect(failedResult).toBe(1)
Expand Down Expand Up @@ -349,7 +357,7 @@
let errMsg
try {
await compose.exec('proxy', 'cat /etc/os-release', opts)
} catch (err: any) {

Check warning on line 360 in test/v2/compose.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
errMsg = err.err
}
expect(errMsg).toContain('is paused')
Expand Down Expand Up @@ -708,7 +716,7 @@

const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput })

const running = await getRunningContainers()

Check warning on line 719 in test/v2/compose.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'running' is assigned a value but never used

expect(std.err).toBeFalsy()
expect(std.data.services.length).toBe(2)
Expand All @@ -735,7 +743,7 @@
commandOptions: [['--format', 'json']]
})

const running = await getRunningContainers()

Check warning on line 746 in test/v2/compose.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'running' is assigned a value but never used

expect(std.err).toBeFalsy()
expect(std.data.services.length).toBe(2)
Expand Down Expand Up @@ -1101,3 +1109,80 @@
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
})
})
})
})
})
8 changes: 8 additions & 0 deletions test/v2/docker-compose-echo.yml
Original file line number Diff line number Diff line change
@@ -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'

Loading