|
| 1 | +import os from "node:os"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { checkMacOSVersion } from "../check-macos-version"; |
| 4 | + |
| 5 | +vi.mock("node:os"); |
| 6 | + |
| 7 | +const mockOs = vi.mocked(os); |
| 8 | + |
| 9 | +describe("checkMacOSVersion", () => { |
| 10 | + beforeEach(() => { |
| 11 | + vi.clearAllMocks(); |
| 12 | + vi.unstubAllEnvs(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should not throw on non-macOS platforms", () => { |
| 16 | + vi.spyOn(process, "platform", "get").mockReturnValue("linux"); |
| 17 | + |
| 18 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should not throw on macOS 13.5.0", () => { |
| 22 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 23 | + mockOs.release.mockReturnValue("22.6.0"); |
| 24 | + |
| 25 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should not throw on macOS 14.0.0", () => { |
| 29 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 30 | + mockOs.release.mockReturnValue("23.0.0"); |
| 31 | + |
| 32 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should not throw on macOS 13.6.0", () => { |
| 36 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 37 | + mockOs.release.mockReturnValue("22.7.0"); |
| 38 | + |
| 39 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should throw error on macOS 12.7.6", () => { |
| 43 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 44 | + vi.stubEnv("CI", ""); |
| 45 | + mockOs.release.mockReturnValue("21.6.0"); |
| 46 | + |
| 47 | + expect(() => checkMacOSVersion({ shouldThrow: true })).toThrow( |
| 48 | + "Unsupported macOS version: The Cloudflare Workers runtime cannot run on the current version of macOS (12.6.0)" |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should throw error on macOS 13.4.0", () => { |
| 53 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 54 | + vi.stubEnv("CI", ""); |
| 55 | + mockOs.release.mockReturnValue("22.4.0"); |
| 56 | + |
| 57 | + expect(() => checkMacOSVersion({ shouldThrow: true })).toThrow( |
| 58 | + "Unsupported macOS version: The Cloudflare Workers runtime cannot run on the current version of macOS (13.4.0)" |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should handle invalid Darwin version format gracefully", () => { |
| 63 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 64 | + mockOs.release.mockReturnValue("invalid-version"); |
| 65 | + |
| 66 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should handle very old Darwin versions gracefully", () => { |
| 70 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 71 | + mockOs.release.mockReturnValue("19.6.0"); |
| 72 | + |
| 73 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should not throw when CI environment variable is set to 'true'", () => { |
| 77 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 78 | + vi.stubEnv("CI", "true"); |
| 79 | + mockOs.release.mockReturnValue("21.6.0"); |
| 80 | + |
| 81 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should not throw when CI environment variable is set to '1'", () => { |
| 85 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 86 | + vi.stubEnv("CI", "1"); |
| 87 | + mockOs.release.mockReturnValue("21.6.0"); |
| 88 | + |
| 89 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should not throw when CI environment variable is set to 'yes'", () => { |
| 93 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 94 | + vi.stubEnv("CI", "yes"); |
| 95 | + mockOs.release.mockReturnValue("21.6.0"); |
| 96 | + |
| 97 | + expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow(); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe("checkMacOSVersion with shouldThrow=false", () => { |
| 102 | + beforeEach(() => { |
| 103 | + vi.clearAllMocks(); |
| 104 | + vi.unstubAllEnvs(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should not warn on non-macOS platforms", () => { |
| 108 | + vi.spyOn(process, "platform", "get").mockReturnValue("linux"); |
| 109 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 110 | + |
| 111 | + checkMacOSVersion({ shouldThrow: false }); |
| 112 | + |
| 113 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should not warn on macOS 13.5.0", () => { |
| 117 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 118 | + mockOs.release.mockReturnValue("22.6.0"); |
| 119 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 120 | + |
| 121 | + checkMacOSVersion({ shouldThrow: false }); |
| 122 | + |
| 123 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should warn on macOS 12.7.6", () => { |
| 127 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 128 | + vi.stubEnv("CI", ""); |
| 129 | + mockOs.release.mockReturnValue("21.6.0"); |
| 130 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 131 | + |
| 132 | + checkMacOSVersion({ shouldThrow: false }); |
| 133 | + |
| 134 | + expect(warnSpy).toHaveBeenCalledWith( |
| 135 | + expect.stringContaining( |
| 136 | + "⚠️ Warning: Unsupported macOS version detected (12.6.0)" |
| 137 | + ) |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should warn on macOS 13.4.0", () => { |
| 142 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 143 | + vi.stubEnv("CI", ""); |
| 144 | + mockOs.release.mockReturnValue("22.4.0"); |
| 145 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 146 | + |
| 147 | + checkMacOSVersion({ shouldThrow: false }); |
| 148 | + |
| 149 | + expect(warnSpy).toHaveBeenCalledWith( |
| 150 | + expect.stringContaining( |
| 151 | + "⚠️ Warning: Unsupported macOS version detected (13.4.0)" |
| 152 | + ) |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should not warn when CI environment variable is set to 'true'", () => { |
| 157 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 158 | + vi.stubEnv("CI", "true"); |
| 159 | + mockOs.release.mockReturnValue("21.6.0"); |
| 160 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 161 | + |
| 162 | + checkMacOSVersion({ shouldThrow: false }); |
| 163 | + |
| 164 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should not warn when CI environment variable is set to '1'", () => { |
| 168 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 169 | + vi.stubEnv("CI", "1"); |
| 170 | + mockOs.release.mockReturnValue("21.6.0"); |
| 171 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 172 | + |
| 173 | + checkMacOSVersion({ shouldThrow: false }); |
| 174 | + |
| 175 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + |
| 178 | + it("should not warn when CI environment variable is set to 'yes'", () => { |
| 179 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 180 | + vi.stubEnv("CI", "yes"); |
| 181 | + mockOs.release.mockReturnValue("21.6.0"); |
| 182 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 183 | + |
| 184 | + checkMacOSVersion({ shouldThrow: false }); |
| 185 | + |
| 186 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 187 | + }); |
| 188 | + |
| 189 | + it("should not warn on invalid Darwin version format", () => { |
| 190 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 191 | + mockOs.release.mockReturnValue("invalid-version"); |
| 192 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 193 | + |
| 194 | + checkMacOSVersion({ shouldThrow: false }); |
| 195 | + |
| 196 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 197 | + }); |
| 198 | +}); |
0 commit comments