From 1a531bfb87eb49c156c558eda52e89e9e75c897b Mon Sep 17 00:00:00 2001 From: Jeffery Date: Tue, 30 Jun 2026 18:22:27 +0800 Subject: [PATCH] =?UTF-8?q?test(logger):=20=E8=A3=9C=E4=B8=8A=E9=9A=8E?= =?UTF-8?q?=E6=AE=B5=E5=89=8D=E7=B6=B4=E8=88=87=20forStage=20=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6=E4=B8=A6=E8=AA=BF=E6=95=B4=20silentLog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/test/index.test.js | 8 ++++++-- app/test/logger.test.js | 31 +++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/test/index.test.js b/app/test/index.test.js index c9210b1..8773851 100644 --- a/app/test/index.test.js +++ b/app/test/index.test.js @@ -5,8 +5,12 @@ const assert = require('node:assert/strict'); const { main } = require('../index'); -// 安靜的 log 記錄器,避免測試輸出雜訊 -const silentLog = { section() {}, info() {}, error() {} }; +// 安靜的 log 記錄器,避免測試輸出雜訊;forStage 回傳同樣安靜的子記錄器 +const silentLog = { + info() {}, + error() {}, + forStage() { return { info() {}, error() {} }; }, +}; const okConfig = { serverUrl: 'https://gitea.example.com', diff --git a/app/test/logger.test.js b/app/test/logger.test.js index b466e12..a4f4136 100644 --- a/app/test/logger.test.js +++ b/app/test/logger.test.js @@ -29,24 +29,39 @@ function capture(run) { const TIME = /\[\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\]/; -test('section 於 stdout 輸出標題與 50 寬分隔線區塊', () => { - const { out, err } = capture(() => logger.section('參數檢查')); - assert.equal(out, `\n${'='.repeat(50)}\n參數檢查\n${'-'.repeat(50)}\n`); - assert.equal(err, ''); -}); - -test('info 以 [INF][時間]: 格式輸出至 stdout', () => { +test('info 未帶 stage 時以 [INF][時間]: 格式輸出至 stdout', () => { const { out, err } = capture(() => logger.info('IS_BETA=false')); assert.match(out, new RegExp(`^\\[INF\\]${TIME.source}: IS_BETA=false\\n$`)); assert.equal(err, ''); }); -test('error 以 [ERR][時間]: 格式輸出至 stderr,且不寫入 stdout', () => { +test('info 帶 stage 時以 [階段][INF][時間]: 格式輸出至 stdout', () => { + const { out, err } = capture(() => logger.info('IS_BETA=false', '參數檢查')); + assert.match(out, new RegExp(`^\\[參數檢查\\]\\[INF\\]${TIME.source}: IS_BETA=false\\n$`)); + assert.equal(err, ''); +}); + +test('error 未帶 stage 時以 [ERR][時間]: 格式輸出至 stderr,且不寫入 stdout', () => { const { out, err } = capture(() => logger.error('GITEA_SERVER_URL 未設定')); assert.match(err, new RegExp(`^\\[ERR\\]${TIME.source}: GITEA_SERVER_URL 未設定\\n$`)); assert.equal(out, ''); }); +test('error 帶 stage 時以 [階段][ERR][時間]: 格式輸出至 stderr', () => { + const { err } = capture(() => logger.error('讀取失敗', '取得舊版本')); + assert.match(err, new RegExp(`^\\[取得舊版本\\]\\[ERR\\]${TIME.source}: 讀取失敗\\n$`)); +}); + +test('forStage 回傳的子記錄器自動帶入階段名稱', () => { + const staged = logger.forStage('計算版本號'); + const { out, err } = capture(() => { + staged.info('NEW_VERSION=0.0.6'); + staged.error('計算錯誤'); + }); + assert.match(out, new RegExp(`^\\[計算版本號\\]\\[INF\\]${TIME.source}: NEW_VERSION=0.0.6\\n$`)); + assert.match(err, new RegExp(`^\\[計算版本號\\]\\[ERR\\]${TIME.source}: 計算錯誤\\n$`)); +}); + test('info/error 的時間戳為 yyyy/MM/dd HH:mm:ss 格式', () => { const { out } = capture(() => logger.info('x')); assert.match(out, TIME);