test(calculate-version): 補 repository 邊界測試並合併串流攔截輔助

- 補上 GITEA_REPOSITORY 空段/缺斜線/多段等邊界格式的拋錯測試
- 將 logger 測試的 captureStdout/captureStderr 合併為單一 captureStream

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 14:05:08 +08:00
co-authored by Claude Opus 4.8
parent da83f4fc30
commit 1e227328f2
2 changed files with 20 additions and 22 deletions
+9
View File
@@ -95,6 +95,15 @@ test('loadConfig 於 GITEA_REPOSITORY 格式錯誤或含路徑穿越時拋錯',
}), /GITEA_REPOSITORY 格式錯誤/); }), /GITEA_REPOSITORY 格式錯誤/);
}); });
test('loadConfig 於 GITEA_REPOSITORY 邊界格式(空段/缺斜線/多段)皆拋錯', () => {
for (const repo of ['noslash', 'owner/', '/repo', 'a/b/c', 'owner//repo']) {
assert.throws(() => loadConfig({
GITEA_SERVER_URL: 'https://gitea.example.com',
GITEA_REPOSITORY: repo,
}), /GITEA_REPOSITORY 格式錯誤/, `應拒絕:${repo}`);
}
});
test('loadConfig 接受含點號的合法 owner/repo', () => { test('loadConfig 接受含點號的合法 owner/repo', () => {
const config = loadConfig({ const config = loadConfig({
GITEA_SERVER_URL: 'https://gitea.example.com', GITEA_SERVER_URL: 'https://gitea.example.com',
+11 -22
View File
@@ -5,51 +5,40 @@ const assert = require('node:assert/strict');
const logger = require('../logger'); const logger = require('../logger');
// 暫時攔截 process.stdout.write,回傳期間內寫出的內容 // 暫時攔截指定標準串流('stdout' / 'stderr')的 write,回傳期間內寫出的內容
function captureStdout(fn) { // 結束時於 finally 還原原本的 write,避免狀態污染
const original = process.stdout.write; function captureStream(streamName, fn) {
const stream = process[streamName];
const original = stream.write;
let out = ''; let out = '';
process.stdout.write = (chunk) => { out += chunk; return true; }; stream.write = (chunk) => { out += chunk; return true; };
try { try {
fn(); fn();
} finally { } finally {
process.stdout.write = original; stream.write = original;
}
return out;
}
// 暫時攔截 process.stderr.write,回傳期間內寫出的內容
function captureStderr(fn) {
const original = process.stderr.write;
let out = '';
process.stderr.write = (chunk) => { out += chunk; return true; };
try {
fn();
} finally {
process.stderr.write = original;
} }
return out; return out;
} }
test('section 以主/次分隔線包夾標題輸出至 stdout', () => { test('section 以主/次分隔線包夾標題輸出至 stdout', () => {
const out = captureStdout(() => logger.section('參數檢查')); const out = captureStream('stdout', () => logger.section('參數檢查'));
const line = '='.repeat(50); const line = '='.repeat(50);
const subline = '-'.repeat(50); const subline = '-'.repeat(50);
assert.equal(out, `\n${line}\n參數檢查\n${subline}\n`); assert.equal(out, `\n${line}\n參數檢查\n${subline}\n`);
}); });
test('info 以 [info] 前綴與換行輸出至 stdout', () => { test('info 以 [info] 前綴與換行輸出至 stdout', () => {
const out = captureStdout(() => logger.info('IS_BETA=false')); const out = captureStream('stdout', () => logger.info('IS_BETA=false'));
assert.equal(out, '[info] IS_BETA=false\n'); assert.equal(out, '[info] IS_BETA=false\n');
}); });
test('error 以 [error] 前綴與換行輸出至 stderr', () => { test('error 以 [error] 前綴與換行輸出至 stderr', () => {
const out = captureStderr(() => logger.error('GITEA_SERVER_URL 未設定')); const out = captureStream('stderr', () => logger.error('GITEA_SERVER_URL 未設定'));
assert.equal(out, '[error] GITEA_SERVER_URL 未設定\n'); assert.equal(out, '[error] GITEA_SERVER_URL 未設定\n');
}); });
test('error 僅輸出不終止行程,呼叫後仍可繼續執行', () => { test('error 僅輸出不終止行程,呼叫後仍可繼續執行', () => {
let reached = false; let reached = false;
captureStderr(() => { logger.error('still alive'); reached = true; }); captureStream('stderr', () => { logger.error('still alive'); reached = true; });
assert.equal(reached, true); assert.equal(reached, true);
}); });