From 14624454196370e08309d4f0b0463b494e4df9ca Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 6 Aug 2023 06:30:23 -0700 Subject: Code coverage for `bun test` (#3975) * WIP code coverage initial commit * almost works * one approach * Code Coverage * Update WebKit * it works but is not yet accurate * skip double ascii check * wrapper * it works but i'm not sure what to do about blocks * hide blocks for now * Update ZigSourceProvider.cpp * Create coverage.md * Update nav.ts --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/bindings/InternalModuleRegistry.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/bun.js/bindings/InternalModuleRegistry.cpp') diff --git a/src/bun.js/bindings/InternalModuleRegistry.cpp b/src/bun.js/bindings/InternalModuleRegistry.cpp index e6b574d7b..841360502 100644 --- a/src/bun.js/bindings/InternalModuleRegistry.cpp +++ b/src/bun.js/bindings/InternalModuleRegistry.cpp @@ -12,6 +12,20 @@ namespace Bun { +extern "C" bool BunTest__shouldGenerateCodeCoverage(BunString sourceURL); +extern "C" void ByteRangeMapping__generate(BunString sourceURL, BunString code, int sourceID); + +static void maybeAddCodeCoverage(JSC::VM& vm, const JSC::SourceCode& code) +{ +#ifdef BUN_DEBUG + bool isCodeCoverageEnabled = !!vm.controlFlowProfiler(); + bool shouldGenerateCodeCoverage = isCodeCoverageEnabled && BunTest__shouldGenerateCodeCoverage(Bun::toString(code.provider()->sourceURL())); + if (shouldGenerateCodeCoverage) { + ByteRangeMapping__generate(Bun::toString(code.provider()->sourceURL()), Bun::toString(code.provider()->source().toStringWithoutCopying()), code.provider()->asID()); + } +#endif +} + // The `INTERNAL_MODULE_REGISTRY_GENERATE` macro handles inlining code to compile and run a // JS builtin that acts as a module. In debug mode, we use a different implementation that reads // from the developer's filesystem. This allows reloading code without recompiling bindings. @@ -20,7 +34,7 @@ namespace Bun { auto throwScope = DECLARE_THROW_SCOPE(vm); \ auto&& origin = SourceOrigin(WTF::URL(makeString("builtin://"_s, moduleName))); \ SourceCode source = JSC::makeSource(SOURCE, origin, moduleName); \ - \ + maybeAddCodeCoverage(vm, source); \ JSFunction* func \ = JSFunction::create( \ vm, \ -- cgit v1.2.3