Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const yargs = require('yargs')
.default('e', 'utf8')
.alias('e', 'encoding')
.describe('e', 'Encoding of files/piped data.')
// Not .choices() as node takes the name of an encoding in any case, like `UTF8`
.check((argv) => Buffer.isEncoding(argv.encoding) || `Unknown encoding: ${argv.encoding}`)

.alias('E', 'engine')
.describe(
Expand Down
7 changes: 7 additions & 0 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ export {version} from '../package.json';
const runtime = process.versions.bun ? 'Bun' : 'Node';
const runtimeEngine = process.versions.bun ? 'JSC' : 'V8';

function validateEncoding(encoding = 'utf8') {
if (!Buffer.isEncoding(encoding)) {
die('Unknown encoding:', encoding);
}
}

// Returns a promise that resolves when all files are done
export async function engine(config: any = {engine: 'default'}) {
outputConfig(config);
validateEncoding(config.encoding);

step('Displaying steps for:');
step(config);
Expand Down
10 changes: 10 additions & 0 deletions test/cli/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ else
fi


# -e
# An unknown encoding stops the run with an error, both for files and for piped data (#690)
reset
assert_failure "rexreplace o x my.file -e nope"
assert "cat my.file" 'foobar'
assert_failure "printf foo | rexreplace o x -e nope"
assert "printf foo | rexreplace o x -e nope 2>&1 | grep -v 'exited with code' | tail -1" 'Unknown encoding: nope'
assert "printf foo | rexreplace o x -e LATIN1" 'fxx'


# -b
reset
rexreplace o x my.file --keep-backup
Expand Down
29 changes: 20 additions & 9 deletions test/js/encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,26 @@ describe('--encoding', () => {
);
});

test.todo(
'BUG #690: an unknown encoding gives an error message instead of crashing',
async () => {
fs.writeFileSync('my.file', 'foobar');
const result = await rr('o', 'x', {includeGlob: ['my.file'], encoding: 'nope'});
expect(result.exit).toBe(1);
expect(result.err).toContain('nope');
}
);
// The error message, and not the stack trace from deep inside node, when an encoding is unknown (#690)
const unknownEncoding = {out: '', err: ' ❌ Unknown encoding: nope\n\n', exit: 1};

test('stops the run with an error if it is unknown (#690)', async () => {
fs.writeFileSync('my.file', 'foobar');
expect(await rr('o', 'x', {includeGlob: ['my.file'], encoding: 'nope'})).toEqual(
unknownEncoding
);
expect(fs.readFileSync('my.file', 'utf8')).toBe('foobar');
});

test('stops the run with an error if it is unknown, also for piped data (#690)', async () => {
expect(await rr('o', 'x', {pipedData: 'foo', encoding: 'nope'})).toEqual(unknownEncoding);
});

test('is known in any case', async () => {
fs.writeFileSync('my.file', latin1());
await rr('æ', 'ø', {includeGlob: ['my.file'], encoding: 'LATIN1'});
expect(fs.readFileSync('my.file')).toEqual(Buffer.from([0x66, 0xf8, 0x6f]));
});

describe('combined', () => {
test.each([
Expand Down
Loading