Skip to content

Commit db62a6a

Browse files
committed
fix: native code coverage
1 parent 58e2b6c commit db62a6a

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ set(CMAKE_CXX_STANDARD 17)
1111
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1212

1313
if(COVERAGE)
14-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
15-
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}")
14+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
15+
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} --coverage")
16+
add_compile_definitions(COVERAGE)
1617
message("-- Enabled code coverage")
1718
endif()
1819

lib/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import bindings from 'bindings'
33

44
const moduleRoot = bindings.getRoot(fileURLToPath(import.meta.url))
55
export const {
6+
_coverageFlush,
67
_testAsync,
78
Chain,
89
Decoder,

src/index.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "mappings/native_iterator.hpp"
66
#include <napi.h>
77

8+
#ifdef COVERAGE
9+
extern "C" void __gcov_dump(void);
10+
#endif
11+
812
namespace flac_bindings {
913

1014
using namespace Napi;
@@ -68,6 +72,15 @@ namespace flac_bindings {
6872
InstanceValue("Chain", initMetadata2Chain(env, *this), napi_enumerable),
6973
InstanceValue("Iterator", initMetadata2Iterator(env, *this), napi_enumerable),
7074
InstanceValue("fns", initFns(env), napi_enumerable),
75+
#ifdef COVERAGE
76+
InstanceValue(
77+
"_coverageFlush",
78+
Function::New(env, [] (const CallbackInfo& info) {
79+
__gcov_dump();
80+
return info.Env().Undefined();
81+
}, "_coverageFlush")
82+
),
83+
#endif
7184
});
7285

7386
exports.Freeze();

test/helper/setup.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/* eslint-disable import-x/no-extraneous-dependencies */
22
import * as matchers from 'jest-extended'
3-
import { expect } from 'vitest'
3+
import { afterAll, expect } from 'vitest'
44

55
expect.extend(matchers)
6+
7+
afterAll(async () => {
8+
// force flush code coverage from C++ code
9+
const { _coverageFlush } = await import('../../lib/api.js')
10+
_coverageFlush?.()
11+
})

0 commit comments

Comments
 (0)