Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions actions/setup/js/action_conclusion_otlp.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,34 @@ const { run } = req("./action_conclusion_otlp.cjs");
// Shared mock function — patched onto the module exports in beforeEach
const mockSendJobConclusionSpan = vi.fn();

/** Env vars read by this module — cleared before each test */
const MANAGED_ENV_VARS = ["OTEL_EXPORTER_OTLP_ENDPOINT", "INPUT_JOB_NAME", "INPUT_JOB-NAME", "GITHUB_AW_OTEL_JOB_START_MS"];

describe("action_conclusion_otlp.cjs", () => {
let originalEnv;
/** @type {Record<string, string | undefined>} */
let originalEnv = {};

beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(console, "log").mockImplementation(() => {});
mockSendJobConclusionSpan.mockResolvedValue(undefined);
// Patch the shared CJS exports object — run() accesses this at call time
sendOtlpModule.sendJobConclusionSpan = mockSendJobConclusionSpan;

originalEnv = {
OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
INPUT_JOB_NAME: process.env.INPUT_JOB_NAME,
"INPUT_JOB-NAME": process.env["INPUT_JOB-NAME"],
GITHUB_AW_OTEL_JOB_START_MS: process.env.GITHUB_AW_OTEL_JOB_START_MS,
};
delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
delete process.env.INPUT_JOB_NAME;
delete process.env["INPUT_JOB-NAME"];
delete process.env.GITHUB_AW_OTEL_JOB_START_MS;
originalEnv = Object.fromEntries(MANAGED_ENV_VARS.map(key => [key, process.env[key]]));
MANAGED_ENV_VARS.forEach(key => delete process.env[key]);
});

afterEach(() => {
vi.restoreAllMocks();
sendOtlpModule.sendJobConclusionSpan = originalSendJobConclusionSpan;

if (originalEnv.OTEL_EXPORTER_OTLP_ENDPOINT !== undefined) {
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = originalEnv.OTEL_EXPORTER_OTLP_ENDPOINT;
} else {
delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
}
if (originalEnv.INPUT_JOB_NAME !== undefined) {
process.env.INPUT_JOB_NAME = originalEnv.INPUT_JOB_NAME;
} else {
delete process.env.INPUT_JOB_NAME;
}
if (originalEnv["INPUT_JOB-NAME"] !== undefined) {
process.env["INPUT_JOB-NAME"] = originalEnv["INPUT_JOB-NAME"];
} else {
delete process.env["INPUT_JOB-NAME"];
}
if (originalEnv.GITHUB_AW_OTEL_JOB_START_MS !== undefined) {
process.env.GITHUB_AW_OTEL_JOB_START_MS = originalEnv.GITHUB_AW_OTEL_JOB_START_MS;
} else {
delete process.env.GITHUB_AW_OTEL_JOB_START_MS;
}
MANAGED_ENV_VARS.forEach(key => {
const value = originalEnv[key];
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
});
});
Comment on lines 25 to 46
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new env cleanup deletes the managed variables in beforeEach/afterEach without restoring their prior values. If any of these env vars are set by the test runner/CI (or by other tests running in the same Vitest worker), this file will permanently unset them for subsequent tests, causing cross-test flakiness. Consider capturing the original values for MANAGED_ENV_VARS at the start of each test (or suite) and restoring them in afterEach, or switch the tests to use vi.stubEnv for all env mutations so vi.unstubAllEnvs() actually reverts changes.

Copilot uses AI. Check for mistakes.

it("should export run as a function", () => {
Expand Down Expand Up @@ -177,6 +159,14 @@ describe("action_conclusion_otlp.cjs", () => {

expect(mockSendJobConclusionSpan).toHaveBeenCalledWith("gh-aw.job.conclusion", { startMs: undefined });
});

it("should pass startMs: undefined when GITHUB_AW_OTEL_JOB_START_MS is a negative number", async () => {
process.env.GITHUB_AW_OTEL_JOB_START_MS = "-1000";

await run();

expect(mockSendJobConclusionSpan).toHaveBeenCalledWith("gh-aw.job.conclusion", { startMs: undefined });
});
});
});

Expand Down
2 changes: 2 additions & 0 deletions actions/setup/js/safe_outputs_handlers.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ describe("safe_outputs_handlers", () => {
});

process.env.GITHUB_BASE_REF = "master";
process.env.GITHUB_HEAD_REF = "feature/test-change";
process.env.GITHUB_REF_NAME = "feature/test-change";
try {
const result = await handlers.createPullRequestHandler({
Expand All @@ -543,6 +544,7 @@ describe("safe_outputs_handlers", () => {
);
} finally {
delete process.env.GITHUB_BASE_REF;
delete process.env.GITHUB_HEAD_REF;
delete process.env.GITHUB_REF_NAME;
}
});
Expand Down
Loading