Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle comma-separated file paths #40

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coverage/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion coverage/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ async function run(): Promise<void> {

const patterns = core
.getInput('files', { required: true })
.split(' ')
.split(',')
.map(file => file.trim())
.filter(Boolean)

const patternString = patterns.join('\n')
const globber = await glob.create(patternString)
Expand Down
94 changes: 87 additions & 7 deletions coverage/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,23 @@ vi.mock('@actions/tool-cache', () => ({
}))

vi.mock('@actions/glob', () => ({
create: vi.fn().mockImplementation(async pattern => ({
glob: async () => {
if (pattern === 'src/**/*.ts') {
return ['src/main.ts', 'src/utils.ts']
create: vi.fn().mockImplementation(async (patternString: string) => {
const patterns = patternString.split('\n').filter(Boolean)
return {
glob: async () => {
let files: string[] = []
for (const p of patterns) {
if (p === 'nonexistent/**/*.js') {
} else if (p === 'src/**/*.ts') {
files = files.concat(['src/main.ts', 'src/utils.ts'])
} else {
files = files.concat([p])
}
}
return files
}
return [pattern]
}
}))
})
}))

describe('Coverage Action - main.ts', () => {
Expand All @@ -76,7 +85,9 @@ describe('Coverage Action - main.ts', () => {
getInputSpy = vi.fn()
exitSpy = vi
.spyOn(process, 'exit')
.mockImplementation((code?: number | undefined) => undefined as never) as unknown as ReturnType<typeof vi.spyOn>
.mockImplementation(
(code?: number | undefined) => undefined as never
) as unknown as ReturnType<typeof vi.spyOn>

vi.mocked(core.getInput).mockImplementation(getInputSpy)

Expand Down Expand Up @@ -119,4 +130,73 @@ describe('Coverage Action - main.ts', () => {
expect.arrayContaining(['src/main.ts', 'src/utils.ts'])
)
})

it('should handle multiple files separated by commas', async () => {
getInputSpy.mockImplementation((name: string) => {
if (name === 'coverage-token') return 'abc123'
if (name === 'files') return 'src/main.ts,src/utils.ts'
return ''
})
vi.mocked(core.getBooleanInput).mockReturnValue(false)

await runWithTracing()

expect(exec.exec).toHaveBeenCalled()
const [tool, args] = vi.mocked(exec.exec).mock.calls[0]
expect(tool).toBe('qlty')
expect(args).toContain('src/main.ts')
expect(args).toContain('src/utils.ts')
})

it('should handle multiple patterns, including a glob, separated by commas', async () => {
getInputSpy.mockImplementation((name: string) => {
if (name === 'coverage-token') return 'another-token'
if (name === 'files') return 'src/main.ts,src/**/*.ts'
return ''
})
vi.mocked(core.getBooleanInput).mockReturnValue(false)

await runWithTracing()

expect(exec.exec).toHaveBeenCalled()
const [tool, args] = vi.mocked(exec.exec).mock.calls[0]
expect(tool).toBe('qlty')
expect(args).toEqual(
expect.arrayContaining(['src/main.ts', 'src/utils.ts'])
)
expect(args).not.toContain('src/main.ts src/**/*.ts')
})

it('should handle a pattern that matches no files', async () => {
getInputSpy.mockImplementation((name: string) => {
if (name === 'coverage-token') return 'abc123'
if (name === 'files') return 'nonexistent/**/*.js'
return ''
})
vi.mocked(core.getBooleanInput).mockReturnValue(false)

await runWithTracing()

expect(exec.exec).toHaveBeenCalled()
const [tool, args] = vi.mocked(exec.exec).mock.calls[0]

expect(args).not.toContain('nonexistent/**/*.js')
})

it('should handle repeated patterns by removing duplicates', async () => {
getInputSpy.mockImplementation((name: string) => {
if (name === 'coverage-token') return 'abc123'
if (name === 'files') return 'src/main.ts,src/main.ts,src/**/*.ts'
return ''
})
vi.mocked(core.getBooleanInput).mockReturnValue(false)

await runWithTracing()

expect(exec.exec).toHaveBeenCalled()
const [tool, args] = vi.mocked(exec.exec).mock.calls[0]
expect(tool).toBe('qlty')
expect(args.filter(arg => arg === 'src/main.ts').length).toBe(1)
expect(args).toContain('src/utils.ts')
})
})
Loading