From a89190f9034043cfb2c55150117a7166481849a1 Mon Sep 17 00:00:00 2001 From: karolyp Date: Fri, 14 Jan 2022 14:11:03 +0100 Subject: [PATCH] [#178] Fix quiet trim issue, add tests. --- src/index.ts | 34 ++++++++++++++++++++++++---------- test/index.test.ts | 30 ++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index ca1176a3..4b62c705 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(), @@ -417,7 +431,7 @@ export const ps = async function ( ): Promise> { try { const result = await execCompose('ps', [], options) - const data = mapPsOutput(result.out) + const data = mapPsOutput(result.out, options) return { ...result, data diff --git a/test/index.test.ts b/test/index.test.ts index b243734f..7045caa4 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -554,7 +554,7 @@ test('config show data for docker-compose files (services)', async (): Promise { }) }) +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 => { const config = { cwd: path.join(__dirname),