38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { maskSecrets, run } from './util.js';
|
|
|
|
test('maskSecrets: 遮蔽出現的祕密字串', () => {
|
|
const out = maskSecrets('token is abcd1234 here', ['abcd1234']);
|
|
assert.equal(out, 'token is *** here');
|
|
});
|
|
|
|
test('maskSecrets: 遮蔽 URL 內嵌的 token', () => {
|
|
const out = maskSecrets('https://oauth2:abcd1234@host/repo.git', ['abcd1234']);
|
|
assert.ok(!out.includes('abcd1234'));
|
|
});
|
|
|
|
test('maskSecrets: 太短(<4)的祕密不遮蔽以免誤傷', () => {
|
|
assert.equal(maskSecrets('abc here', ['abc']), 'abc here');
|
|
});
|
|
|
|
test('maskSecrets: 多個祕密與空值都安全處理', () => {
|
|
const out = maskSecrets('aaaa bbbb', ['aaaa', '', undefined, 'bbbb']);
|
|
assert.equal(out, '*** ***');
|
|
});
|
|
|
|
test('maskSecrets: 非字串輸入回傳空字串', () => {
|
|
assert.equal(maskSecrets(null), '');
|
|
});
|
|
|
|
test('run: 非零結束碼不丟例外並回傳 status', () => {
|
|
const r = run('node', ['-e', 'process.exit(3)']);
|
|
assert.equal(r.status, 3);
|
|
});
|
|
|
|
test('run: 指令不存在時回傳 status=1 與錯誤訊息', () => {
|
|
const r = run('a-command-that-does-not-exist-xyz', []);
|
|
assert.equal(r.status, 1);
|
|
assert.ok(r.stderr.length > 0);
|
|
});
|