Skip to content

Commit e598c5d

Browse files
committed
fix(cli): terminate github actions command files
1 parent 88ca009 commit e598c5d

2 files changed

Lines changed: 82 additions & 10 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
6+
import { setGithubActionsOutputAndEnvVars } from "./githubActions.js";
7+
8+
describe("setGithubActionsOutputAndEnvVars", () => {
9+
const originalGithubEnv = process.env.GITHUB_ENV;
10+
const originalGithubOutput = process.env.GITHUB_OUTPUT;
11+
let tmpDir: string | undefined;
12+
13+
afterEach(() => {
14+
if (originalGithubEnv === undefined) {
15+
delete process.env.GITHUB_ENV;
16+
} else {
17+
process.env.GITHUB_ENV = originalGithubEnv;
18+
}
19+
20+
if (originalGithubOutput === undefined) {
21+
delete process.env.GITHUB_OUTPUT;
22+
} else {
23+
process.env.GITHUB_OUTPUT = originalGithubOutput;
24+
}
25+
26+
if (tmpDir) {
27+
rmSync(tmpDir, { recursive: true, force: true });
28+
tmpDir = undefined;
29+
}
30+
});
31+
32+
it("terminates GitHub Actions env and output entries with newlines", () => {
33+
tmpDir = mkdtempSync(join(tmpdir(), "trigger-github-actions-"));
34+
const envFile = join(tmpDir, "env");
35+
const outputFile = join(tmpDir, "output");
36+
process.env.GITHUB_ENV = envFile;
37+
process.env.GITHUB_OUTPUT = outputFile;
38+
39+
setGithubActionsOutputAndEnvVars({
40+
envVars: {
41+
deploymentId: "dep_123",
42+
imageTag: "v1",
43+
},
44+
outputs: {
45+
deploymentId: "dep_123",
46+
needsPromotion: "true",
47+
},
48+
});
49+
50+
expect(readFileSync(envFile, "utf8")).toMatch(/^deploymentId=dep_123\r?\nimageTag=v1\r?\n$/);
51+
expect(readFileSync(outputFile, "utf8")).toMatch(
52+
/^deploymentId=dep_123\r?\nneedsPromotion=true\r?\n$/
53+
);
54+
});
55+
56+
it("does not append content when there are no env vars or outputs", () => {
57+
tmpDir = mkdtempSync(join(tmpdir(), "trigger-github-actions-"));
58+
const envFile = join(tmpDir, "env");
59+
const outputFile = join(tmpDir, "output");
60+
writeFileSync(envFile, "");
61+
writeFileSync(outputFile, "");
62+
process.env.GITHUB_ENV = envFile;
63+
process.env.GITHUB_OUTPUT = outputFile;
64+
65+
setGithubActionsOutputAndEnvVars({
66+
envVars: {},
67+
outputs: {},
68+
});
69+
70+
expect(readFileSync(envFile, "utf8")).toBe("");
71+
expect(readFileSync(outputFile, "utf8")).toBe("");
72+
});
73+
});
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import { appendFileSync } from "node:fs";
2+
import { EOL } from "node:os";
3+
4+
function formatGithubActionsCommandFile(entries: Record<string, string>) {
5+
const lines = Object.entries(entries).map(([key, value]) => `${key}=${value}`);
6+
7+
return lines.length > 0 ? `${lines.join(EOL)}${EOL}` : "";
8+
}
29

310
export function setGithubActionsOutputAndEnvVars({
411
envVars,
@@ -9,19 +16,11 @@ export function setGithubActionsOutputAndEnvVars({
916
}) {
1017
// Set environment variables
1118
if (process.env.GITHUB_ENV) {
12-
const contents = Object.entries(envVars)
13-
.map(([key, value]) => `${key}=${value}`)
14-
.join("\n");
15-
16-
appendFileSync(process.env.GITHUB_ENV, contents);
19+
appendFileSync(process.env.GITHUB_ENV, formatGithubActionsCommandFile(envVars));
1720
}
1821

1922
// Set outputs
2023
if (process.env.GITHUB_OUTPUT) {
21-
const contents = Object.entries(outputs)
22-
.map(([key, value]) => `${key}=${value}`)
23-
.join("\n");
24-
25-
appendFileSync(process.env.GITHUB_OUTPUT, contents);
24+
appendFileSync(process.env.GITHUB_OUTPUT, formatGithubActionsCommandFile(outputs));
2625
}
2726
}

0 commit comments

Comments
 (0)