|
| 1 | +import { expect, mock, test } from 'bun:test' |
| 2 | +import { writeFile } from 'node:fs/promises' |
| 3 | +import { getBooleanInput, getInput, setFailed } from '@actions/core' |
| 4 | + |
| 5 | +const mockWriteFile = mock(writeFile) |
| 6 | +const mockGetInput = mock(getInput) |
| 7 | +const mockSetFailed = mock(setFailed) |
| 8 | +const mockGetBooleanInput = mock(getBooleanInput) |
| 9 | + |
| 10 | +mock.module('node:fs/promises', () => ({ |
| 11 | + writeFile: mockWriteFile, |
| 12 | +})) |
| 13 | + |
| 14 | +mock.module('@actions/core', () => ({ |
| 15 | + getInput: mockGetInput, |
| 16 | + setFailed: mockSetFailed, |
| 17 | + getBooleanInput: mockGetBooleanInput, |
| 18 | +})) |
| 19 | + |
| 20 | +import { run } from '../run' |
| 21 | + |
| 22 | +test('should write filtered environment variables to file', async () => { |
| 23 | + const testPrefix = 'TEST_' |
| 24 | + const testOutput = 'test.env' |
| 25 | + |
| 26 | + process.env.TEST_VAR1 = 'value1' |
| 27 | + process.env.TEST_VAR2 = 'value2' |
| 28 | + process.env.OTHER_VAR = 'other' |
| 29 | + |
| 30 | + mockGetInput.mockImplementation((name: string) => { |
| 31 | + switch (name as 'prefix' | 'output') { |
| 32 | + case 'prefix': |
| 33 | + return testPrefix |
| 34 | + case 'output': |
| 35 | + return testOutput |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + mockWriteFile.mockResolvedValue(undefined) |
| 40 | + mockGetBooleanInput.mockReturnValue(false) |
| 41 | + |
| 42 | + await run() |
| 43 | + |
| 44 | + expect(mockGetInput).toHaveBeenCalledWith('prefix') |
| 45 | + expect(mockGetInput).toHaveBeenCalledWith('output') |
| 46 | + expect(mockGetBooleanInput).toHaveBeenCalledWith('remove-prefix') |
| 47 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 48 | + testOutput, |
| 49 | + 'TEST_VAR1=value1\nTEST_VAR2=value2', |
| 50 | + ) |
| 51 | +}) |
| 52 | + |
| 53 | +test('should handle writeFile error', async () => { |
| 54 | + const testPrefix = 'TEST_' |
| 55 | + const testOutput = 'test.env' |
| 56 | + const testError = 'Write failed' |
| 57 | + |
| 58 | + process.env.TEST_VAR = 'value' |
| 59 | + |
| 60 | + mockGetInput.mockImplementation((name: string) => { |
| 61 | + switch (name as 'prefix' | 'output') { |
| 62 | + case 'prefix': |
| 63 | + return testPrefix |
| 64 | + case 'output': |
| 65 | + return testOutput |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + mockWriteFile.mockRejectedValue(testError) |
| 70 | + mockGetBooleanInput.mockReturnValue(false) |
| 71 | + |
| 72 | + await run() |
| 73 | + |
| 74 | + expect(mockSetFailed).toHaveBeenCalledWith(testError) |
| 75 | + process.exitCode = 0 |
| 76 | +}) |
| 77 | + |
| 78 | +test('should handle empty environment variables', async () => { |
| 79 | + const testPrefix = 'NONEXISTENT_' |
| 80 | + const testOutput = 'empty.env' |
| 81 | + |
| 82 | + mockGetInput.mockImplementation((name: string) => { |
| 83 | + switch (name as 'prefix' | 'output') { |
| 84 | + case 'prefix': |
| 85 | + return testPrefix |
| 86 | + case 'output': |
| 87 | + return testOutput |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + mockWriteFile.mockResolvedValue(undefined) |
| 92 | + mockGetBooleanInput.mockReturnValue(false) |
| 93 | + |
| 94 | + await run() |
| 95 | + |
| 96 | + expect(mockWriteFile).toHaveBeenCalledWith(testOutput, '') |
| 97 | +}) |
| 98 | + |
| 99 | +test('should filter environment variables by prefix', async () => { |
| 100 | + const testPrefix = 'API_' |
| 101 | + const testOutput = 'api.env' |
| 102 | + |
| 103 | + process.env.API_KEY = 'secret123' |
| 104 | + process.env.API_URL = 'https://api.example.com' |
| 105 | + process.env.DATABASE_URL = 'postgres://localhost' |
| 106 | + process.env.API_TIMEOUT = '5000' |
| 107 | + |
| 108 | + mockGetInput.mockImplementation((name: string) => { |
| 109 | + switch (name as 'prefix' | 'output') { |
| 110 | + case 'prefix': |
| 111 | + return testPrefix |
| 112 | + case 'output': |
| 113 | + return testOutput |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + mockWriteFile.mockResolvedValue(undefined) |
| 118 | + mockGetBooleanInput.mockReturnValue(false) |
| 119 | + await run() |
| 120 | + |
| 121 | + const expectedContent = [ |
| 122 | + 'API_KEY=secret123', |
| 123 | + 'API_URL=https://api.example.com', |
| 124 | + 'API_TIMEOUT=5000', |
| 125 | + ].join('\n') |
| 126 | + |
| 127 | + expect(mockWriteFile).toHaveBeenCalledWith(testOutput, expectedContent) |
| 128 | +}) |
| 129 | + |
| 130 | +test('should remove prefix from environment variables', async () => { |
| 131 | + const testPrefix = 'API_' |
| 132 | + const testOutput = 'api.env' |
| 133 | + const testRemovePrefix = true |
| 134 | + process.env.API_KEY = 'secret123' |
| 135 | + process.env.API_URL = 'https://api.example.com' |
| 136 | + process.env.API_TIMEOUT = '5000' |
| 137 | + |
| 138 | + mockGetInput.mockImplementation((name: string) => { |
| 139 | + switch (name as 'prefix' | 'output') { |
| 140 | + case 'prefix': |
| 141 | + return testPrefix |
| 142 | + case 'output': |
| 143 | + return testOutput |
| 144 | + } |
| 145 | + }) |
| 146 | + |
| 147 | + mockWriteFile.mockResolvedValue(undefined) |
| 148 | + mockGetBooleanInput.mockReturnValue(testRemovePrefix) |
| 149 | + |
| 150 | + await run() |
| 151 | + |
| 152 | + const expectedContent = [ |
| 153 | + 'KEY=secret123', |
| 154 | + 'URL=https://api.example.com', |
| 155 | + 'TIMEOUT=5000', |
| 156 | + ].join('\n') |
| 157 | + |
| 158 | + expect(mockWriteFile).toHaveBeenCalledWith(testOutput, expectedContent) |
| 159 | +}) |
0 commit comments