| 1 | import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | |
| 3 | import { |
| 4 | getGitBranch, |
| 5 | getGitHubActionsRepoUrl, |
| 6 | isGitHubActions, |
| 7 | } from "./git.js"; |
| 8 | |
| 9 | describe("git utilities - GitHub Actions detection", () => { |
| 10 | beforeEach(() => { |
| 11 | // Clear environment variables |
| 12 | delete process.env.GITHUB_ACTIONS; |
| 13 | delete process.env.GITHUB_REPOSITORY; |
| 14 | delete process.env.GITHUB_SERVER_URL; |
| 15 | }); |
| 16 | |
| 17 | describe("isGitHubActions", () => { |
| 18 | it("should return true when GITHUB_ACTIONS is 'true'", () => { |
| 19 | process.env.GITHUB_ACTIONS = "true"; |
| 20 | expect(isGitHubActions()).toBe(true); |
| 21 | }); |
| 22 | |
| 23 | it("should return false when GITHUB_ACTIONS is not set", () => { |
| 24 | expect(isGitHubActions()).toBe(false); |
| 25 | }); |
| 26 | |
| 27 | it("should return false when GITHUB_ACTIONS is set to other values", () => { |
| 28 | process.env.GITHUB_ACTIONS = "false"; |
| 29 | expect(isGitHubActions()).toBe(false); |
| 30 | }); |
| 31 | }); |
| 32 | |
| 33 | describe("getGitHubActionsRepoUrl", () => { |
| 34 | it("should return null when not in GitHub Actions", () => { |
| 35 | expect(getGitHubActionsRepoUrl()).toBeNull(); |
| 36 | }); |
| 37 | |
| 38 | it("should return null when GITHUB_REPOSITORY is not set", () => { |
| 39 | process.env.GITHUB_ACTIONS = "true"; |
| 40 | expect(getGitHubActionsRepoUrl()).toBeNull(); |
| 41 | }); |
| 42 | |
| 43 | it("should return GitHub URL with default server", () => { |
| 44 | process.env.GITHUB_ACTIONS = "true"; |
| 45 | process.env.GITHUB_REPOSITORY = "owner/repo"; |
| 46 | |
| 47 | expect(getGitHubActionsRepoUrl()).toBe("https://github.com/owner/repo"); |
| 48 | }); |
| 49 | |
| 50 | it("should return GitHub URL with custom server", () => { |
| 51 | process.env.GITHUB_ACTIONS = "true"; |
| 52 | process.env.GITHUB_REPOSITORY = "owner/repo"; |
| 53 | process.env.GITHUB_SERVER_URL = "https://github.enterprise.com"; |
| 54 | |
| 55 | expect(getGitHubActionsRepoUrl()).toBe( |
| 56 | "https://github.enterprise.com/owner/repo", |
| 57 | ); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe("getRepoUrl - GitHub Actions priority", () => { |
| 62 | it("should prioritize GitHub Actions environment variables", () => { |
| 63 | process.env.GITHUB_ACTIONS = "true"; |
| 64 | process.env.GITHUB_REPOSITORY = "owner/repo"; |
| 65 | |
| 66 | // Since we can't easily mock git commands, we'll rely on the fact that |
| 67 | // GitHub Actions detection should take priority and return immediately |
| 68 | const result = getGitHubActionsRepoUrl(); |
| 69 | expect(result).toBe("https://github.com/owner/repo"); |
| 70 | }); |
| 71 | |
| 72 | it("should work with GitHub Enterprise Server", () => { |
| 73 | process.env.GITHUB_ACTIONS = "true"; |
| 74 | process.env.GITHUB_REPOSITORY = "enterprise/repo"; |
| 75 | process.env.GITHUB_SERVER_URL = "https://git.company.com"; |
| 76 | |
| 77 | const result = getGitHubActionsRepoUrl(); |
| 78 | expect(result).toBe("https://git.company.com/enterprise/repo"); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe("getGitBranch", () => { |
| 83 | it("should return null when not in a git repo or when git command fails", () => { |
| 84 | // Since we can't easily mock git commands in this test environment, |
| 85 | // we just test that the function returns either a string or null |
| 86 | const result = getGitBranch(); |
| 87 | expect(typeof result === "string" || result === null).toBe(true); |
| 88 | }); |
| 89 | }); |
| 90 | }); |