From 4c796315e89ce580fcd3ee91420326bf0c09b8fa Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Tue, 26 Apr 2022 13:20:00 +0000 Subject: [PATCH] fix: improve readability of log from child process --- src/util/spawn.ts | 19 ++++++++++--------- test/util/spawn.ts | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/util/spawn.ts b/src/util/spawn.ts index 630aea3..ec8c7ef 100644 --- a/src/util/spawn.ts +++ b/src/util/spawn.ts @@ -16,17 +16,18 @@ export function spawn(cmd: string, args: string[] = [], options: child_process.S } function sendBuffered (type: keyof typeof buffer, force = false) { const nPos = buffer[type].lastIndexOf('\n') - let send - if (force && buffer[type] && buffer[type].substr(-1) !== '\n') { - send = buffer[type] + '\n' - } else if (!force && nPos < 0) { + if (!force && nPos < 0 || !buffer[type]) { return - } else { - send = buffer[type] } - if (send && send.trim()) { - (type === 'out' ? core.debug : core.warning)(send) - buffer[type] = buffer[type].slice(send.length) + const slice = force ? buffer[type] : buffer[type].slice(0, nPos + 1) + const lines = slice.split('\n') + if (lines.some(ln => ln.trim())) { + const logger = type === 'out' ? core.debug : core.warning + if (lines[lines.length -1].trim() === '') { + lines.pop() + } + lines.forEach(ln => logger(ln)) + buffer[type] = buffer[type].slice(slice.length) } } diff --git a/test/util/spawn.ts b/test/util/spawn.ts index 6fd0e41..764007a 100644 --- a/test/util/spawn.ts +++ b/test/util/spawn.ts @@ -51,10 +51,10 @@ test('reject on error', () => { test('defer stdout to debug', () => { coreDebug = [] - spawnMock = jest.fn(() => realSpawn(process.execPath, ['-e', `process.stdout.write('foo')`])) + spawnMock = jest.fn(() => realSpawn(process.execPath, ['-e', `process.stdout.write('foo\\nbar')`])) return spawn('foo', ['bar', 'baz'], { uid: 123456 }).finally(() => { - expect(coreDebug).toEqual(['foo\n']) + expect(coreDebug).toEqual(['foo', 'bar']) }) }) @@ -69,9 +69,9 @@ test('resolve to stdout', () => { test('defer stderr to warning', () => { coreWarning = [] - spawnMock = jest.fn(() => realSpawn(process.execPath, ['-e', `process.stderr.write('foo')`])) + spawnMock = jest.fn(() => realSpawn(process.execPath, ['-e', `process.stderr.write('foo\\nbar')`])) return spawn('foo', ['bar', 'baz'], { uid: 123456 }).finally(() => { - expect(coreWarning).toEqual(['foo\n']) + expect(coreWarning).toEqual(['foo', 'bar']) }) })