|
| 1 | +import * as fs from "node:fs/promises"; |
| 2 | +import * as os from "node:os"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { jest } from "@jest/globals"; |
| 5 | + |
| 6 | +import * as core from "../__fixtures__/core.js"; |
| 7 | +jest.unstable_mockModule("@actions/core", () => core); |
| 8 | + |
| 9 | +const { main } = await import("../src/lib.js"); |
| 10 | + |
| 11 | +const createEchoCli = async (tmpdir: string) => { |
| 12 | + await fs.writeFile( |
| 13 | + path.resolve(tmpdir, "trunk-analytics-cli"), |
| 14 | + `#!/bin/bash |
| 15 | + echo -n $@`, |
| 16 | + ); |
| 17 | +}; |
| 18 | + |
| 19 | +describe("Arguments", () => { |
| 20 | + afterEach(() => { |
| 21 | + jest.resetAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("Forwards inputs - upload", async () => { |
| 25 | + core.getInput.mockImplementation((name) => { |
| 26 | + switch (name) { |
| 27 | + case "junit-paths": |
| 28 | + return "junit.xml"; |
| 29 | + case "org-slug": |
| 30 | + return "org"; |
| 31 | + case "token": |
| 32 | + return "token"; |
| 33 | + case "cli-version": |
| 34 | + return "0.0.0"; |
| 35 | + default: |
| 36 | + return ""; |
| 37 | + } |
| 38 | + }); |
| 39 | + const tmpdir = await fs.mkdtemp( |
| 40 | + path.resolve(os.tmpdir(), "trunk-analytics-uploader-test-"), |
| 41 | + ); |
| 42 | + await createEchoCli(tmpdir); |
| 43 | + const command = await main(tmpdir); |
| 44 | + expect(command).toMatch( |
| 45 | + `${tmpdir}/trunk-analytics-cli upload --junit-paths "junit.xml" --org-url-slug "org" --token "token" --repo-root "."`, |
| 46 | + ); |
| 47 | + await fs.rm(tmpdir, { recursive: true, force: true }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("Forwards inputs - test", async () => { |
| 51 | + core.getInput.mockImplementation((name) => { |
| 52 | + switch (name) { |
| 53 | + case "junit-paths": |
| 54 | + return "junit.xml"; |
| 55 | + case "org-slug": |
| 56 | + return "org"; |
| 57 | + case "token": |
| 58 | + return "token"; |
| 59 | + case "cli-version": |
| 60 | + return "0.0.0"; |
| 61 | + case "run": |
| 62 | + return "exit 0"; |
| 63 | + default: |
| 64 | + return ""; |
| 65 | + } |
| 66 | + }); |
| 67 | + const tmpdir = await fs.mkdtemp( |
| 68 | + path.resolve(os.tmpdir(), "trunk-analytics-uploader-test-"), |
| 69 | + ); |
| 70 | + await createEchoCli(tmpdir); |
| 71 | + const command = await main(tmpdir); |
| 72 | + expect(command).toMatch( |
| 73 | + `${tmpdir}/trunk-analytics-cli test --junit-paths "junit.xml" --org-url-slug "org" --token "token" --repo-root "." -- exit 0`, |
| 74 | + ); |
| 75 | + await fs.rm(tmpdir, { recursive: true, force: true }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments