|
| 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 | +}); |
0 commit comments