Skip to content
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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@
* Executes docker compose command with common options
*/
export const execCompose = (
command,

Check warning on line 283 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'command' should be typed
args,

Check warning on line 284 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 All @@ -307,8 +307,8 @@
let executablePath: string
let executableArgs: string[] = []

if (executable?.standalone && !executable.executablePath) {
executablePath = 'docker-compose'
if (executable?.standalone) {
executablePath = executable.executablePath || 'docker-compose'
} else {
executablePath = executable?.executablePath || 'docker'
const executableOptions = executable?.options || []
Expand Down
69 changes: 68 additions & 1 deletion test/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
beforeEach,
afterEach,
vi,
beforeAll
beforeAll,
MockInstance
} from 'vitest'
import Docker, { ContainerInfo } from 'dockerode'
import * as compose from '../src'
import childProcess from 'child_process'
import * as path from 'path'
import { readFile } from 'fs'
import { mapPsOutput, mapImListOutput } from '../src'
Expand Down Expand Up @@ -165,7 +167,7 @@
await compose.logs('non_existent_service', {
cwd: path.join(__dirname)
})
} catch (error: any) {

Check warning on line 170 in test/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 @@ -357,7 +359,7 @@
let errMsg
try {
await compose.exec('proxy', 'cat /etc/os-release', opts)
} catch (err: any) {

Check warning on line 362 in test/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 @@ -716,7 +718,7 @@

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

const running = await getRunningContainers()

Check warning on line 721 in test/compose.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'running' is assigned a value but never used

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

const running = await getRunningContainers()

Check warning on line 748 in test/compose.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'running' is assigned a value but never used

expect(std.exitCode).toBe(0)
expect(std.data.services.length).toBe(2)
Expand Down Expand Up @@ -1202,3 +1204,68 @@
})
})
})

describe('executable path resolution', (): void => {
let spawnSpy: MockInstance<typeof childProcess.spawn>

beforeEach((): void => {
const mockProc = {
on: vi.fn(),
stdout: { on: vi.fn(), pipe: vi.fn() },
stderr: { on: vi.fn(), pipe: vi.fn() },
stdin: { write: vi.fn(), end: vi.fn() }
}

mockProc.on.mockImplementation((event, cb) => {
if (event === 'exit') {
setTimeout(() => cb(0), 0)
}
})

spawnSpy = vi
.spyOn(childProcess, 'spawn')
.mockReturnValue((mockProc as unknown) as childProcess.ChildProcess)
})

afterEach((): void => {
spawnSpy.mockRestore()
})

// spawn is always called with a third argument ({ cwd, env }) so we use
// expect.objectContaining({}) to match it without being brittle about its contents.
it('uses custom executablePath in standalone mode without appending compose', async (): Promise<void> => {
await compose.execCompose('up', [], {
executable: {
standalone: true,
executablePath: '/custom/path/docker-compose'
}
})
expect(spawnSpy).toHaveBeenCalledWith(
'/custom/path/docker-compose',
['up'],
expect.objectContaining({})
)
})

it('defaults to docker-compose in standalone mode when no executablePath is provided', async (): Promise<void> => {
await compose.execCompose('up', [], {
executable: { standalone: true }
})
expect(spawnSpy).toHaveBeenCalledWith(
'docker-compose',
['up'],
expect.objectContaining({})
)
})

it('appends compose subcommand when not in standalone mode', async (): Promise<void> => {
await compose.execCompose('up', [], {
executable: { executablePath: '/custom/docker' }
})
expect(spawnSpy).toHaveBeenCalledWith(
'/custom/docker',
['compose', 'up'],
expect.objectContaining({})
)
})
})
Loading