From 1e227328f25b99772c2ac95279aa19b64788bfcb Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 26 Jun 2026 14:05:08 +0800 Subject: [PATCH] =?UTF-8?q?test(calculate-version):=20=E8=A3=9C=20reposito?= =?UTF-8?q?ry=20=E9=82=8A=E7=95=8C=E6=B8=AC=E8=A9=A6=E4=B8=A6=E5=90=88?= =?UTF-8?q?=E4=BD=B5=E4=B8=B2=E6=B5=81=E6=94=94=E6=88=AA=E8=BC=94=E5=8A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 補上 GITEA_REPOSITORY 空段/缺斜線/多段等邊界格式的拋錯測試 - 將 logger 測試的 captureStdout/captureStderr 合併為單一 captureStream Co-Authored-By: Claude Opus 4.8 (1M context) --- app/test/config.test.js | 9 +++++++++ app/test/logger.test.js | 33 +++++++++++---------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/app/test/config.test.js b/app/test/config.test.js index c41bed6..d673eab 100644 --- a/app/test/config.test.js +++ b/app/test/config.test.js @@ -95,6 +95,15 @@ test('loadConfig 於 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', () => { const config = loadConfig({ GITEA_SERVER_URL: 'https://gitea.example.com', diff --git a/app/test/logger.test.js b/app/test/logger.test.js index 390b4dc..6484a58 100644 --- a/app/test/logger.test.js +++ b/app/test/logger.test.js @@ -5,51 +5,40 @@ const assert = require('node:assert/strict'); const logger = require('../logger'); -// 暫時攔截 process.stdout.write,回傳期間內寫出的內容 -function captureStdout(fn) { - const original = process.stdout.write; +// 暫時攔截指定標準串流('stdout' / 'stderr')的 write,回傳期間內寫出的內容; +// 結束時於 finally 還原原本的 write,避免狀態污染 +function captureStream(streamName, fn) { + const stream = process[streamName]; + const original = stream.write; let out = ''; - process.stdout.write = (chunk) => { out += chunk; return true; }; + stream.write = (chunk) => { out += chunk; return true; }; try { fn(); } finally { - process.stdout.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; + stream.write = original; } return out; } test('section 以主/次分隔線包夾標題輸出至 stdout', () => { - const out = captureStdout(() => logger.section('參數檢查')); + const out = captureStream('stdout', () => logger.section('參數檢查')); const line = '='.repeat(50); const subline = '-'.repeat(50); assert.equal(out, `\n${line}\n參數檢查\n${subline}\n`); }); 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'); }); 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'); }); test('error 僅輸出不終止行程,呼叫後仍可繼續執行', () => { let reached = false; - captureStderr(() => { logger.error('still alive'); reached = true; }); + captureStream('stderr', () => { logger.error('still alive'); reached = true; }); assert.equal(reached, true); });