Skip to content

Commit 86c0691

Browse files
committed
Add tests
1 parent 886d471 commit 86c0691

File tree

13 files changed

+21887
-20389
lines changed

13 files changed

+21887
-20389
lines changed

__fixtures__/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const debug = jest.fn<typeof core.debug>()
55
export const error = jest.fn<typeof core.error>()
66
export const info = jest.fn<typeof core.info>()
77
export const getInput = jest.fn<typeof core.getInput>()
8+
export const getBooleanInput = jest.fn<typeof core.getBooleanInput>()
89
export const setOutput = jest.fn<typeof core.setOutput>()
910
export const setFailed = jest.fn<typeof core.setFailed>()
1011
export const warning = jest.fn<typeof core.warning>()

__tests__/helpers.test.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)