Skip to content

Commit 23d7349

Browse files
fix: add check+test for gcov installed
1 parent 3eb8ad5 commit 23d7349

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/helpers/gcov.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import glob from 'fast-glob'
2-
import { runExternalProgram } from "./util"
2+
import { isProgramInstalled, runExternalProgram } from "./util"
33

44
export function generateGcovCoverageFiles(projectRoot: string, include: string[] = [], ignore: string[] = [], gcovArgs = '',) {
5+
if (!isProgramInstalled('gcov')) {
6+
throw new Error('gcov is not installed, cannot process files')
7+
}
8+
59
const globstar = (pattern: string) => `**/${pattern}`
610
const gcovInclude = ['*.gcno', ...include].map(globstar)
711
const gcovIgnore = ['bower_components', 'node_modules', 'vendor', ...ignore].map(globstar)

test/helpers/gcov.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { generateGcovCoverageFiles } from '../../src/helpers/gcov'
66
describe('generateGcovCoverageFiles()', () => {
77
afterEach(() => {
88
td.reset()
9-
})
9+
})
1010

1111
it('should find and run on test/fixtures/gcov/main.gcno', () => {
1212
const projectRoot = process.cwd()
@@ -21,18 +21,30 @@ Lines executed:0.00% of 11`)
2121
})
2222
it('should find no gcno files and return an error', () => {
2323
const projectRoot = process.cwd()
24-
expect(() => {generateGcovCoverageFiles(projectRoot, [], ['test'])}).toThrowError(/No gcov files found/)
24+
expect(() => { generateGcovCoverageFiles(projectRoot, [], ['test']) }).toThrowError(/No gcov files found/)
2525

2626

2727
})
2828

2929
it('should pass gcov arguments directly through', () => {
3030
const spawnSync = td.replace(childProcess, 'spawnSync')
31+
td.when(spawnSync('gcov')).thenReturn({
32+
stdout: 'gcov installed',
33+
error: null
34+
})
3135
td.when(spawnSync('gcov', td.matchers.contains('NEWGCOVARG'))).thenReturn({
32-
stdout: 'Matched',
36+
stdout: 'Matched',
3337
})
3438

3539
const projectRoot = process.cwd()
36-
expect(generateGcovCoverageFiles(projectRoot,[],[],'NEWGCOVARG')).toEqual("Matched")
40+
expect(generateGcovCoverageFiles(projectRoot, [], [], 'NEWGCOVARG')).toEqual("Matched")
41+
})
42+
43+
it('should return an error when gcov is not installed', () => {
44+
const spawnSync = td.replace(childProcess, 'spawnSync')
45+
td.when(spawnSync('gcov')).thenReturn({ error: "Command 'gcov' not found" })
46+
47+
const projectRoot = process.cwd()
48+
expect(() => generateGcovCoverageFiles(projectRoot, [], [], 'NEWGCOVARG')).toThrowError(/gcov is not installed/)
3749
})
3850
})

0 commit comments

Comments
 (0)