Skip to content

Commit

Permalink
[PDMLab#178] Fix quiet trim issue, add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
karolyp authored and Karoly Pakozdi committed Jan 14, 2022
1 parent de15f71 commit 1799b7d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
34 changes: 24 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,33 @@ export type DockerComposePsResult = {
}>
}

export const mapPsOutput = (output: string): DockerComposePsResult => {
export const mapPsOutput = (
output: string,
options?: IDockerComposeOptions
): DockerComposePsResult => {
let isQuiet = false
if (options?.commandOptions) {
isQuiet =
options.commandOptions.includes('-q') ||
options.commandOptions.includes('--quiet')
}
const services = output
.split(`\n`)
.filter(nonEmptyString)
.filter((_, index) => index > 1)
.filter((_, index) => isQuiet || index > 1)
.map((line) => {
const [
nameFragment,
commandFragment,
stateFragment,
untypedPortsFragment
] = line.split(/\s{3,}/)

let nameFragment = line
let commandFragment = ''
let stateFragment = ''
let untypedPortsFragment = ''
if (!isQuiet) {
;[
nameFragment,
commandFragment,
stateFragment,
untypedPortsFragment
] = line.split(/\s{3,}/)
}
return {
name: nameFragment.trim(),
command: commandFragment.trim(),
Expand Down Expand Up @@ -417,7 +431,7 @@ export const ps = async function (
): Promise<TypedDockerComposeResult<DockerComposePsResult>> {
try {
const result = await execCompose('ps', [], options)
const data = mapPsOutput(result.out)
const data = mapPsOutput(result.out, options)
return {
...result,
data
Expand Down
30 changes: 28 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ test('config show data for docker-compose files (services)', async (): Promise<v
})

expect(std.data.services.length).toBe(5)
expect(std.data.services[0]).toBe('build_test_1')
expect(std.data.services[0]).toContain('build_test_1')
expect(std.err).toBeFalsy()
})

Expand All @@ -566,7 +566,7 @@ test('config show data for docker-compose files (volumes)', async (): Promise<vo
})

expect(std.data.volumes.length).toBe(1)
expect(std.data.volumes[0]).toBe('db-data')
expect(std.data.volumes[0]).toContain('db-data')
expect(std.err).toBeFalsy()
})

Expand Down Expand Up @@ -724,6 +724,32 @@ test('parse ps output', () => {
})
})

test('ps output when quiet', () => {
const output = `64848fc721dfeff435edc7d4bb42e2f0e0a10d0c7602b73729a7fd7b09b7586f
aed60ce17575e69c56cc4cb07eeba89b5d7b7b2b307c8b87f3363db6af850719
f49548fa0b1f88846b78c65c6ea7f802bcbdfb2cf10204497eb89ba622d7715b
`
const psOut = mapPsOutput(output, { commandOptions: ['-q'] })

expect(psOut.services[0]).toEqual(
expect.objectContaining({
name: '64848fc721dfeff435edc7d4bb42e2f0e0a10d0c7602b73729a7fd7b09b7586f'
})
)

expect(psOut.services[1]).toEqual(
expect.objectContaining({
name: 'aed60ce17575e69c56cc4cb07eeba89b5d7b7b2b307c8b87f3363db6af850719'
})
)

expect(psOut.services[2]).toEqual(
expect.objectContaining({
name: 'f49548fa0b1f88846b78c65c6ea7f802bcbdfb2cf10204497eb89ba622d7715b'
})
)
})

test('ensure progress callback is called', async (): Promise<void> => {
const config = {
cwd: path.join(__dirname),
Expand Down

0 comments on commit 1799b7d

Please sign in to comment.