|
| 1 | +import * as core from "@actions/core"; |
| 2 | +import * as index from "./index"; |
| 3 | + |
| 4 | +jest.mock("@actions/core"); |
| 5 | + |
| 6 | +afterEach(() => { |
| 7 | + jest.resetAllMocks(); |
| 8 | + delete process.env["RUNNER_ENVIRONMENT"]; |
| 9 | + delete process.env["AGENT_ISSELFHOSTED"]; |
| 10 | +}); |
| 11 | + |
| 12 | +describe("resolveInstallDependencies function", () => { |
| 13 | + let coreInfoMock: jest.Mock; |
| 14 | + let coreWarningMock: jest.Mock; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + coreInfoMock = core.info as jest.Mock; |
| 18 | + coreWarningMock = core.warning as jest.Mock; |
| 19 | + }); |
| 20 | + |
| 21 | + // for explicit 'true' should return true |
| 22 | + it("returns true when input is explicitly 'true'", () => { |
| 23 | + const result = index.resolveInstallDependencies('true'); |
| 24 | + expect(result).toBe(true); |
| 25 | + expect(coreInfoMock).toHaveBeenCalledWith('install-system-dependencies explicitly set to true'); |
| 26 | + expect(coreWarningMock).not.toHaveBeenCalled(); |
| 27 | + }); |
| 28 | + |
| 29 | + // for explicit 'TRUE' (uppercase) should also return true |
| 30 | + it("returns true when input is 'TRUE' (case insensitive)", () => { |
| 31 | + const result = index.resolveInstallDependencies('TRUE'); |
| 32 | + expect(result).toBe(true); |
| 33 | + expect(coreInfoMock).toHaveBeenCalledWith('install-system-dependencies explicitly set to true'); |
| 34 | + }); |
| 35 | + |
| 36 | + // for explicit 'false' should return false |
| 37 | + it("returns false when input is explicitly 'false'", () => { |
| 38 | + const result = index.resolveInstallDependencies('false'); |
| 39 | + expect(result).toBe(false); |
| 40 | + expect(coreInfoMock).toHaveBeenCalledWith('install-system-dependencies explicitly set to false'); |
| 41 | + expect(coreWarningMock).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + // for explicit 'FALSE' (uppercase) should also return false |
| 45 | + it("returns false when input is 'FALSE' (case insensitive)", () => { |
| 46 | + const result = index.resolveInstallDependencies('FALSE'); |
| 47 | + expect(result).toBe(false); |
| 48 | + expect(coreInfoMock).toHaveBeenCalledWith('install-system-dependencies explicitly set to false'); |
| 49 | + }); |
| 50 | + |
| 51 | + // for 'auto' mode on GitHub-hosted runner should return true |
| 52 | + it("returns true for 'auto' on GitHub-hosted runner", () => { |
| 53 | + process.env["RUNNER_ENVIRONMENT"] = "github-hosted"; |
| 54 | + process.env["AGENT_ISSELFHOSTED"] = "0"; |
| 55 | + |
| 56 | + const result = index.resolveInstallDependencies('auto'); |
| 57 | + expect(result).toBe(true); |
| 58 | + expect(coreInfoMock).toHaveBeenCalledWith('Auto-detected runner type: GitHub-hosted'); |
| 59 | + expect(coreInfoMock).toHaveBeenCalledWith('System dependencies will be installed (auto mode)'); |
| 60 | + expect(coreWarningMock).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + // for 'auto' mode on self-hosted runner should return false |
| 64 | + it("returns false for 'auto' on self-hosted runner", () => { |
| 65 | + process.env["RUNNER_ENVIRONMENT"] = "self-hosted"; |
| 66 | + process.env["AGENT_ISSELFHOSTED"] = "1"; |
| 67 | + |
| 68 | + const result = index.resolveInstallDependencies('auto'); |
| 69 | + expect(result).toBe(false); |
| 70 | + expect(coreInfoMock).toHaveBeenCalledWith('Auto-detected runner type: self-hosted'); |
| 71 | + expect(coreInfoMock).toHaveBeenCalledWith('System dependencies will not be installed (auto mode)'); |
| 72 | + expect(coreWarningMock).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + // for empty string should behave like 'auto' on GitHub-hosted |
| 76 | + it("returns true for empty string on GitHub-hosted runner (defaults to auto)", () => { |
| 77 | + process.env["RUNNER_ENVIRONMENT"] = "github-hosted"; |
| 78 | + process.env["AGENT_ISSELFHOSTED"] = "0"; |
| 79 | + |
| 80 | + const result = index.resolveInstallDependencies(''); |
| 81 | + expect(result).toBe(true); |
| 82 | + expect(coreInfoMock).toHaveBeenCalledWith('Auto-detected runner type: GitHub-hosted'); |
| 83 | + }); |
| 84 | + |
| 85 | + // for empty string should behave like 'auto' on self-hosted |
| 86 | + it("returns false for empty string on self-hosted runner (defaults to auto)", () => { |
| 87 | + process.env["RUNNER_ENVIRONMENT"] = "self-hosted"; |
| 88 | + process.env["AGENT_ISSELFHOSTED"] = "1"; |
| 89 | + |
| 90 | + const result = index.resolveInstallDependencies(''); |
| 91 | + expect(result).toBe(false); |
| 92 | + expect(coreInfoMock).toHaveBeenCalledWith('Auto-detected runner type: self-hosted'); |
| 93 | + }); |
| 94 | + |
| 95 | + // 'AUTO' (uppercase) should work as 'auto' on GitHub-hosted |
| 96 | + it("returns true for 'AUTO' on GitHub-hosted runner (case insensitive)", () => { |
| 97 | + process.env["RUNNER_ENVIRONMENT"] = "github-hosted"; |
| 98 | + process.env["AGENT_ISSELFHOSTED"] = "0"; |
| 99 | + |
| 100 | + const result = index.resolveInstallDependencies('AUTO'); |
| 101 | + expect(result).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + // for any invalid input should return false with warning |
| 105 | + it("returns false for invalid input and logs warning", () => { |
| 106 | + const result = index.resolveInstallDependencies('invalid-value'); |
| 107 | + expect(result).toBe(false); |
| 108 | + expect(coreWarningMock).toHaveBeenCalledWith('Invalid value for install-system-dependencies: invalid-value. Defaulting to false.'); |
| 109 | + expect(coreInfoMock).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + // numeric invalid input test |
| 113 | + it("returns false for numeric input", () => { |
| 114 | + const result = index.resolveInstallDependencies('123'); |
| 115 | + expect(result).toBe(false); |
| 116 | + expect(coreWarningMock).toHaveBeenCalledWith('Invalid value for install-system-dependencies: 123. Defaulting to false.'); |
| 117 | + }); |
| 118 | + |
| 119 | + |
| 120 | + //considering removing these |
| 121 | + // edge cases like where RUNNER_ENVIRONMENT is not set |
| 122 | + it("returns false for 'auto' when RUNNER_ENVIRONMENT is not set", () => |
| 123 | + const result = index.resolveInstallDependencies('auto'); |
| 124 | + // Should return false because isGitHubHosted will be false |
| 125 | + expect(result).toBe(false); |
| 126 | + expect(coreInfoMock).toHaveBeenCalledWith('Auto-detected runner type: self-hosted'); |
| 127 | + }); |
| 128 | + |
| 129 | + // edge case where AGENT_ISSELFHOSTED = "1" but RUNNER_ENVIRONMENT = "github-hosted" |
| 130 | + it("returns false when AGENT_ISSELFHOSTED is '1' even if RUNNER_ENVIRONMENT is 'github-hosted'", () => { |
| 131 | + process.env["RUNNER_ENVIRONMENT"] = "github-hosted"; |
| 132 | + process.env["AGENT_ISSELFHOSTED"] = "1"; // Self-hosted indicator |
| 133 | + |
| 134 | + const result = index.resolveInstallDependencies('auto'); |
| 135 | + // Should treat as self-hosted because AGENT_ISSELFHOSTED = "1" |
| 136 | + expect(result).toBe(false); |
| 137 | + expect(coreInfoMock).toHaveBeenCalledWith('Auto-detected runner type: self-hosted'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments