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

support code coverage when testing #352

Open
andrewrk opened this issue May 2, 2017 · 8 comments
Open

support code coverage when testing #352

andrewrk opened this issue May 2, 2017 · 8 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. fuzzing
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented May 2, 2017

Add a --coverage argument to zig test and have it generate a nice report.

Related:

@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label May 2, 2017
@andrewrk andrewrk added this to the 0.2.0 milestone May 2, 2017
@andrewrk andrewrk modified the milestones: 0.2.0, 0.3.0 Oct 19, 2017
@PavelVozenilek
Copy link

Based on my unpleasant experience with gcov (GCC coverage tool) in foolish drive for 100% coverage: there should be some easy way to label certain lines or whole functions as "doesn't need coverage". All kinds of unlikely situations, impossible to emulate.

gcov doesn't support anything like this, so I write a tool which scanned all C source files, searched for "no coverage" tags and removed these lines from gcov output.

It looked like:

if (...)  {
    exit(1); //--   (comment "//--" means no coverage for this line)
}

// The whole function is taken out of coverage via the //-->> and //<<-- brackets
void foo(void) { //-->>
  ...
} //<<--

There should be also some way to skip coverage checking for 3pp libraries, w/o touching their source code.

@andrewrk andrewrk modified the milestones: 0.3.0, 0.4.0 Feb 28, 2018
@tiehuis
Copy link
Member

tiehuis commented May 17, 2018

Just though I'd mention that kcov works perfectly as is with zig at the moment. I think coverage in general is slightly more useful than it normally would be for zig as well, since it clearly shows what segments of code were never compiled. This helps with compile-time evaluation heavy bits.

Only steps required are:

zig test bigint.zig
kcov kcov-out ./zig-cache/test
firefox kcov-out/index.html

2018-05-17-192750_912x675_scrot

@andrewrk
Copy link
Member Author

Oh that's really interesting with the defers in yellow. The number tells you how many different ways there were to exit the block, and it only goes green if you manage to hit them all (in this case by having each try activate the error handling). Same thing with try, it'll be yellow unless you managed to test the error return.

The defer thing might change with #283

@andrewrk
Copy link
Member Author

I tried this using zig test on the std lib. It's really quite useful, but I see some confusing output. In my test results I see:

Test 341/466 os.path.join...OK
Test 342/466 os.path.isAbsoluteWindows...OK
Test 343/466 os.path.isAbsolutePosix...OK
Test 344/466 os.path.windowsParsePath...OK
Test 345/466 os.path.resolve...OK
Test 346/466 os.path.resolveWindows...OK
Test 347/466 os.path.resolvePosix...OK
Test 348/466 os.path.dirnamePosix...OK
Test 349/466 os.path.dirnameWindows...OK
Test 350/466 os.path.basename...OK
Test 351/466 os.path.relative...OK
Test 352/466 os.path.real...OK

but in the coverage report I see no coverage:

screenshot_2018-05-17_11-02-50

I wonder what's going on here.

@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Feb 5, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 11, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Oct 17, 2019
@meme meme mentioned this issue Sep 17, 2020
@andrewrk andrewrk added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Oct 9, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 1.0.0 Oct 9, 2020
@piyushchauhan2011
Copy link

piyushchauhan2011 commented Jul 31, 2022

Looking forward to this option 🚀

@Pyrolistical
Copy link
Contributor

kcov still works, the instructions have changed slightly as ./zig-cache/test doesn't exist.

rm -rf zig-cache/o
zig test lib/std/bit_set.zig
kcov kcov-out ./zig-cache/o/*/test
open kcov-out/index.html

It is very slow on my M1 Macbook Air. Took over 5 minutes.

@paulocoghi
Copy link

It would be interesting to generate a coverage report based on the kcov one* where the different logical possibilities and ramifications are informed in detail, as Jest does:
(informing the coverage of "statements", "branch", "functions", "lines" and their logical ramifications)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |                   
 index.js  |     100 |      100 |     100 |     100 |                   
 server.js |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------
Test Suites: 2 passed, 2 total
Tests:       11 passed, 11 total
Snapshots:   0 total
Time:        3.735 s, estimated 5 s
Ran all test suites.

*(kcov seems to provide everything that's necessary for this)

@perillo
Copy link
Contributor

perillo commented Mar 2, 2024

I tried this using zig test on the std lib. It's really quite useful, but I see some confusing output. In my test results I see:

Test 341/466 os.path.join...OK
Test 342/466 os.path.isAbsoluteWindows...OK
Test 343/466 os.path.isAbsolutePosix...OK
Test 344/466 os.path.windowsParsePath...OK
Test 345/466 os.path.resolve...OK
Test 346/466 os.path.resolveWindows...OK
Test 347/466 os.path.resolvePosix...OK
Test 348/466 os.path.dirnamePosix...OK
Test 349/466 os.path.dirnameWindows...OK
Test 350/466 os.path.basename...OK
Test 351/466 os.path.relative...OK
Test 352/466 os.path.real...OK

but in the coverage report I see no coverage:

I wonder what's going on here.

kcov uses DWARF debugging information for compiled programs to make it possible to collect coverage information without special compiler switches.

In https://www.youtube.com/watch?v=1QMHbp5LUKg Simon Kagstrom says that he found that debugging information is often broken on Linux systems, because kcov uses debug information that are not usually used by debuggers (and thus not well tested).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. fuzzing
Projects
None yet
Development

No branches or pull requests

7 participants