|
| 1 | +/** |
| 2 | + * Unit tests for the helpers module, src/helpers.ts |
| 3 | + */ |
| 4 | +import { jest } from '@jest/globals' |
| 5 | +import * as core from '../__fixtures__/core.js' |
| 6 | + |
| 7 | +// Mock fs module |
| 8 | +const mockExistsSync = jest.fn() |
| 9 | +const mockReadFileSync = jest.fn() |
| 10 | + |
| 11 | +jest.unstable_mockModule('fs', () => ({ |
| 12 | + existsSync: mockExistsSync, |
| 13 | + readFileSync: mockReadFileSync |
| 14 | +})) |
| 15 | + |
| 16 | +jest.unstable_mockModule('@actions/core', () => core) |
| 17 | + |
| 18 | +// Import the module being tested |
| 19 | +const { loadContentFromFileOrInput } = await import('../src/helpers.js') |
| 20 | + |
| 21 | +describe('helpers.ts', () => { |
| 22 | + beforeEach(() => { |
| 23 | + jest.clearAllMocks() |
| 24 | + }) |
| 25 | + |
| 26 | + describe('loadContentFromFileOrInput', () => { |
| 27 | + it('loads content from file when file path is provided', () => { |
| 28 | + const filePath = '/path/to/file.txt' |
| 29 | + const fileContent = 'File content here' |
| 30 | + |
| 31 | + core.getInput.mockImplementation((name: string) => { |
| 32 | + if (name === 'file-input') return filePath |
| 33 | + if (name === 'content-input') return '' |
| 34 | + return '' |
| 35 | + }) |
| 36 | + |
| 37 | + mockExistsSync.mockReturnValue(true) |
| 38 | + mockReadFileSync.mockReturnValue(fileContent) |
| 39 | + |
| 40 | + const result = loadContentFromFileOrInput('file-input', 'content-input') |
| 41 | + |
| 42 | + expect(core.getInput).toHaveBeenCalledWith('file-input') |
| 43 | + expect(mockExistsSync).toHaveBeenCalledWith(filePath) |
| 44 | + expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8') |
| 45 | + expect(result).toBe(fileContent) |
| 46 | + }) |
| 47 | + |
| 48 | + it('throws error when file path is provided but file does not exist', () => { |
| 49 | + const filePath = '/path/to/nonexistent.txt' |
| 50 | + |
| 51 | + core.getInput.mockImplementation((name: string) => { |
| 52 | + if (name === 'file-input') return filePath |
| 53 | + if (name === 'content-input') return '' |
| 54 | + return '' |
| 55 | + }) |
| 56 | + |
| 57 | + mockExistsSync.mockReturnValue(false) |
| 58 | + |
| 59 | + expect(() => { |
| 60 | + loadContentFromFileOrInput('file-input', 'content-input') |
| 61 | + }).toThrow('File for file-input was not found: /path/to/nonexistent.txt') |
| 62 | + |
| 63 | + expect(mockExistsSync).toHaveBeenCalledWith(filePath) |
| 64 | + expect(mockReadFileSync).not.toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it('uses content input when file path is empty', () => { |
| 68 | + const contentInput = 'Direct content input' |
| 69 | + |
| 70 | + core.getInput.mockImplementation((name: string) => { |
| 71 | + if (name === 'file-input') return '' |
| 72 | + if (name === 'content-input') return contentInput |
| 73 | + return '' |
| 74 | + }) |
| 75 | + |
| 76 | + const result = loadContentFromFileOrInput('file-input', 'content-input') |
| 77 | + |
| 78 | + expect(core.getInput).toHaveBeenCalledWith('file-input') |
| 79 | + expect(core.getInput).toHaveBeenCalledWith('content-input') |
| 80 | + expect(mockExistsSync).not.toHaveBeenCalled() |
| 81 | + expect(mockReadFileSync).not.toHaveBeenCalled() |
| 82 | + expect(result).toBe(contentInput) |
| 83 | + }) |
| 84 | + |
| 85 | + it('prefers file path over content input when both are provided', () => { |
| 86 | + const filePath = '/path/to/file.txt' |
| 87 | + const fileContent = 'File content' |
| 88 | + const contentInput = 'Direct content input' |
| 89 | + |
| 90 | + core.getInput.mockImplementation((name: string) => { |
| 91 | + if (name === 'file-input') return filePath |
| 92 | + if (name === 'content-input') return contentInput |
| 93 | + return '' |
| 94 | + }) |
| 95 | + |
| 96 | + mockExistsSync.mockReturnValue(true) |
| 97 | + mockReadFileSync.mockReturnValue(fileContent) |
| 98 | + |
| 99 | + const result = loadContentFromFileOrInput('file-input', 'content-input') |
| 100 | + |
| 101 | + expect(result).toBe(fileContent) |
| 102 | + expect(mockExistsSync).toHaveBeenCalledWith(filePath) |
| 103 | + expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8') |
| 104 | + }) |
| 105 | + |
| 106 | + it('uses default value when neither file nor content is provided', () => { |
| 107 | + const defaultValue = 'Default content' |
| 108 | + |
| 109 | + core.getInput.mockImplementation(() => '') |
| 110 | + |
| 111 | + const result = loadContentFromFileOrInput( |
| 112 | + 'file-input', |
| 113 | + 'content-input', |
| 114 | + defaultValue |
| 115 | + ) |
| 116 | + |
| 117 | + expect(result).toBe(defaultValue) |
| 118 | + expect(mockExistsSync).not.toHaveBeenCalled() |
| 119 | + expect(mockReadFileSync).not.toHaveBeenCalled() |
| 120 | + }) |
| 121 | + |
| 122 | + it('throws error when neither file nor content is provided and no default', () => { |
| 123 | + core.getInput.mockImplementation(() => '') |
| 124 | + |
| 125 | + expect(() => { |
| 126 | + loadContentFromFileOrInput('file-input', 'content-input') |
| 127 | + }).toThrow('Neither file-input nor content-input was set') |
| 128 | + |
| 129 | + expect(mockExistsSync).not.toHaveBeenCalled() |
| 130 | + expect(mockReadFileSync).not.toHaveBeenCalled() |
| 131 | + }) |
| 132 | + |
| 133 | + it('handles undefined inputs correctly', () => { |
| 134 | + const defaultValue = 'Default content' |
| 135 | + |
| 136 | + core.getInput.mockImplementation(() => undefined as any) |
| 137 | + |
| 138 | + const result = loadContentFromFileOrInput( |
| 139 | + 'file-input', |
| 140 | + 'content-input', |
| 141 | + defaultValue |
| 142 | + ) |
| 143 | + |
| 144 | + expect(result).toBe(defaultValue) |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments