Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Anonymous961 committed May 17, 2024
2 parents 9e0c5f3 + dfeb166 commit a242994
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
quote,
quotePowerShell,
noquote,
ensureEol,
} from './util.js'

export interface Shell {
Expand Down Expand Up @@ -663,7 +664,7 @@ export function log(entry: LogEntry) {
case 'stdout':
case 'stderr':
if (!entry.verbose) return
process.stderr.write(entry.data)
process.stderr.write(ensureEol(entry.data))
break
case 'cd':
if (!$.verbose) return
Expand Down
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,11 @@ export function getCallerLocationFromString(stackString = 'unknown') {
?.trim() || stackString
)
}

export function ensureEol(buffer: Buffer): Buffer {
if (buffer.toString('utf8', buffer.length - 1) !== '\n') {
return Buffer.concat([buffer, Buffer.from('\n')])
}

return buffer
}
18 changes: 18 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getCallerLocationFromString,
tempdir,
tempfile,
ensureEol,
} from '../build/util.js'

describe('util', () => {
Expand Down Expand Up @@ -165,3 +166,20 @@ test('tempfile() creates temporary files', () => {
assert.match(tf, /\/zx-.+\/bar\.txt$/)
assert.equal(fs.readFileSync(tf, 'utf-8'), 'bar')
})

test('ensureEol() should ensure buffer ends with a newline character', () => {
// Test case 1: Buffer without newline
const buffer1 = Buffer.from('Hello, world!')
const result1 = ensureEol(buffer1).toString()
assert.strictEqual(result1, 'Hello, world!\n')

// Test case 2: Buffer with newline
const buffer2 = Buffer.from('Hello, world!\n')
const result2 = ensureEol(buffer2).toString()
assert.strictEqual(result2, 'Hello, world!\n')

// Test case 3: Empty buffer
const buffer3 = Buffer.from('')
const result3 = ensureEol(buffer3).toString()
assert.strictEqual(result3, '\n')
})

0 comments on commit a242994

Please sign in to comment.