Skip to content

Commit

Permalink
fix: improve readability of log from child process
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Apr 26, 2022
1 parent 3da861e commit 4c79631
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/util/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/util/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})
})

Expand All @@ -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'])
})
})

0 comments on commit 4c79631

Please sign in to comment.