























@@ -8,6 +8,7 @@ import {
88runWithDiagnosticTraceContext,
99} from "../infra/diagnostic-trace-context.js";
1010import { getChildLogger, getLogger, resetLogger, setLoggerOverride } from "../logging.js";
11+import { withEnv } from "../test-utils/env.js";
1112import { createSuiteLogPathTracker } from "./log-test-helpers.js";
1213import { testApi as loggerTest } from "./logger.js";
1314import { createDiagnosticLogRecordCapture } from "./test-helpers/diagnostic-log-capture.js";
@@ -16,9 +17,6 @@ const secret = "sk-testsecret1234567890abcd";
1617const TRACE_ID = "4bf92f3577b34da6a3ce929d0e0e4736";
1718const SPAN_ID = "00f067aa0ba902b7";
1819const logPathTracker = createSuiteLogPathTracker("openclaw-log-redaction-");
19-const originalConfigPath = process.env.OPENCLAW_CONFIG_PATH;
20-const originalHome = process.env.HOME;
21-const originalTestFileLog = process.env.OPENCLAW_TEST_FILE_LOG;
22202321beforeAll(async () => {
2422await logPathTracker.setup();
@@ -29,21 +27,6 @@ beforeEach(() => {
2927});
30283129afterEach(() => {
32-if (originalConfigPath === undefined) {
33-delete process.env.OPENCLAW_CONFIG_PATH;
34-} else {
35-process.env.OPENCLAW_CONFIG_PATH = originalConfigPath;
36-}
37-if (originalHome === undefined) {
38-delete process.env.HOME;
39-} else {
40-process.env.HOME = originalHome;
41-}
42-if (originalTestFileLog === undefined) {
43-delete process.env.OPENCLAW_TEST_FILE_LOG;
44-} else {
45-process.env.OPENCLAW_TEST_FILE_LOG = originalTestFileLog;
46-}
4730resetDiagnosticEventsForTest();
4831resetDiagnosticTraceContextForTest();
4932resetLogger();
@@ -113,14 +96,15 @@ describe("file log redaction", () => {
11396},
11497}),
11598);
116-process.env.OPENCLAW_CONFIG_PATH = configPath;
117-setLoggerOverride({ level: "info", file: logPath });
118-119-getLogger().info({
120-token: "token-value-1234567890",
121-access: "ya29.fake-access-token-with-enough-length",
122-password: "abcd-efgh-ijkl-mnop",
123-message: `Authorization: Bearer ${secret}`,
99+withEnv({ OPENCLAW_CONFIG_PATH: configPath }, () => {
100+setLoggerOverride({ level: "info", file: logPath });
101+102+getLogger().info({
103+token: "token-value-1234567890",
104+access: "ya29.fake-access-token-with-enough-length",
105+password: "abcd-efgh-ijkl-mnop",
106+message: `Authorization: Bearer ${secret}`,
107+});
124108});
125109126110const content = fs.readFileSync(logPath, "utf8");
@@ -142,22 +126,22 @@ describe("file log redaction", () => {
142126},
143127}),
144128);
145-process.env.OPENCLAW_CONFIG_PATH = configPath;
146-process.env.OPENCLAW_TEST_FILE_LOG = "1";
147-148-getLogger().info({ message: "configured log path works" });
129+withEnv({ OPENCLAW_CONFIG_PATH: configPath, OPENCLAW_TEST_FILE_LOG: "1" }, () => {
130+getLogger().info({ message: "configured log path works" });
131+});
149132150133const content = fs.readFileSync(logPath, "utf8");
151134expect(content).toContain("configured log path works");
152135});
153136154137it("expands leading tilde in logging.file", () => {
155138const home = path.join(path.dirname(logPathTracker.nextPath()), "home");
156-process.env.HOME = home;
157139158-expect(loggerTest.resolveActiveLogFile("~/custom-openclaw.log")).toBe(
159-path.join(home, "custom-openclaw.log"),
160-);
140+withEnv({ HOME: home }, () => {
141+expect(loggerTest.resolveActiveLogFile("~/custom-openclaw.log")).toBe(
142+path.join(home, "custom-openclaw.log"),
143+);
144+});
161145});
162146163147it("writes trace context as top-level JSONL fields", () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。