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

Use bisect PPX instead of Jest coverage #261

Merged
merged 12 commits into from
Apr 26, 2020
Merged

Conversation

jihchi
Copy link
Contributor

@jihchi jihchi commented Apr 19, 2020

Relevant issue: #156

This is an attempt to try bisect ppx for code coverage and see how it goes.

@jihchi jihchi changed the title Use bisect PPX instead of Jest coverage WIP: Use bisect PPX instead of Jest coverage Apr 19, 2020
@jihchi
Copy link
Contributor Author

jihchi commented Apr 19, 2020

First successful coverage report by bisect ppx: https://coveralls.io/builds/30172931

@@ -2,6 +2,9 @@ open Jest;
open Expect;
open Relude.Globals;

[@coverage exclude_file];
afterAll(Bisect.Runtime.write_coverage_data);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh :/ I think we need some better way of doing this globally in a Jest project. The problem I ran into is that when Jest exits, it somehow doesn't trigger process.on('exit') hooks.

Also, a near-future version of Bisect will support excluding files by pattern in the ppx-flags. In the meantime, you could use an exclusions file: https://github.com/aantron/bisect_ppx/blob/master/doc/advanced.md#Excluding

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime, you could use an exclusions file: https://github.com/aantron/bisect_ppx/blob/master/doc/advanced.md#Excluding

I've tried without success, could you please advise?

I added a dune file in the root of project:

(preprocess (pps bisect_ppx --exclusions bisect.exclude))
(preprocessor_deps (file bisect.exclude))

And added a bisect.exclude file next to dune:

file regexp "*_test.bs.js"
file regexp "node_modules/*"

Copy link

@aantron aantron Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood that you are building with BuckleScript, so this should go into bsconfig.json, not a dune file. I think (haven't tried) you can pass the option like this:

"ppx-flags": [["bisect_ppx/ppx", "--exclusions", "bisect.exclude"]]

like so.

Then, the file patterns are regexps, not globs, so

file regexp ".*_test.bs.js"

and you won't be able to exclude node_modules from your project, because instrumentation of bs-bastet is controlled from the upstream node_modules/bs-bastet/bsconfig.json, and is best disabled by rescript-lang/rescript#3761.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like bisect has not supported relative path of exclusion file:

"ppx-flags": [["bisect_ppx/ppx", "--exclusions", "bisect.exclude"]]

where got errors:

> relude@0.61.0 build /Users/jihchi/Repos/relude
> bsb -make-world

[11/11] Building src/runtime/bucklescript/runtime-Bisect.cmj
[81/81] Building bastet/src/Test-BsBastet.cmj
[5/5] Building src/jest.cmj
[1/456] Building __tests__/Relude_Eq_test.reast
FAILED: __tests__/Relude_Eq_test.reast
/Users/jihchi/Repos/relude/node_modules/bs-platform/darwin/bsc.exe  -w +A-4-40-42 -warn-error +A -color always -ppx '/Users/jihchi/Repos/relude/node_modules/bisect_ppx/ppx --exclusions bisect.exclude' -bs-no-version-header -bs-super-errors -o __tests__/Relude_Eq_test.reast -bs-syntax-only -bs-binary-ast /Users/jihchi/Repos/relude/__tests__/Relude_Eq_test.re
File "_none_", line 1:
Error: I/O error: bisect.exclude: No such file or directory

  We've found a bug for you!
  /Users/jihchi/Repos/relude/__tests__/Relude_Eq_test.re

  Error while running external preprocessor
Command line: /Users/jihchi/Repos/relude/node_modules/bisect_ppx/ppx --exclusions bisect.exclude '/var/folders/wm/lp071lgd09nb3_mv_9pthcs00000gn/T/camlppx7aa76b' '/var/folders/wm/lp071lgd09nb3_mv_9pthcs00000gn/T/camlppx94f0b2'

( ...omitted repeated error with different re file... ) 

And then, I changed to absolute path:

"ppx-flags": [
  ["bisect_ppx/ppx", "--exclusions", "/Users/jihchi/Repos/relude/bisect.exclude"]
]

where got another errors:

> relude@0.61.0 build /Users/jihchi/Repos/relude
> bsb -make-world

[11/11] Building src/runtime/bucklescript/runtime-Bisect.cmj
[81/81] Building bastet/src/Test-BsBastet.cmj
[5/5] Building src/jest.cmj
[1/456] Building __tests__/Relude_Eq_test.reast
FAILED: __tests__/Relude_Eq_test.reast
/Users/jihchi/Repos/relude/node_modules/bs-platform/darwin/bsc.exe  -w +A-4-40-42 -warn-error +A -color always -ppx '/Users/jihchi/Repos/relude/node_modules/bisect_ppx/ppx --exclusions /Users/jihchi/Repos/relude/bisect.exclude' -bs-no-version-header -bs-super-errors -o __tests__/Relude_Eq_test.reast -bs-syntax-only -bs-binary-ast /Users/jihchi/Repos/relude/__tests__/Relude_Eq_test.re
open Jest
open Expect
open Relude.Globals
[@@@coverage exclude_file]
;;afterAll Bisect.Runtime.write_coverage_data
( ... omitted re content ...)

File "/var/folders/wm/lp071lgd09nb3_mv_9pthcs00000gn/T/camlppx931ed4", line 1:
Error: I can't decide whether /var/folders/wm/lp071lgd09nb3_mv_9pthcs00000gn/T/camlppx931ed4 is an implement
ation or interface file

  We've found a bug for you!
  /Users/jihchi/Repos/relude/__tests__/Relude_Eq_test.re

  Error while running external preprocessor
Command line: /Users/jihchi/Repos/relude/node_modules/bisect_ppx/ppx --exclusions /Users/jihchi/Repos/relude/bisect.exclude '/var/folders/wm/lp071lgd09nb3_mv_9pthcs00000gn/T/camlppx08d86e' '/var/folders/wm/lp071lgd09nb3_mv_9pthcs00000gn/T/camlppx931ed4'

( ...omitted repeated error with different re file... ) 

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are probably two issues:

  • BuckleScript is running Bisect from some unexpected directory (Bisect does support relative paths).
  • There is something wrong with assumptions Bisect makes about the order of arguments coming from BuckleScript.

Could you push your latest code, that triggers these errors, into this branch, and I'll fix as much of this as I can in Bisect?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok aantron/bisect_ppx#310 and aantron/bisect_ppx#311 fixed it at least in my testing. I'm waiting for CI to build binaries, and then I'll push release 2.3.2 to npm. I looked at the report, and it looks like test cases were excluded successfully. bs-bastet is still included, but that's waiting on BuckleScript.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.3.2 is in npm with the fixes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the report, and it looks like test cases were excluded successfully.

I guess that's not what I checked, since the exclude attributes were still inside each test file :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the report, and it looks like test cases were excluded successfully.

I guess that's not what I checked, since the exclude attributes were still inside each test file :/

--exclusions works with version >=2.3.2. thanks for speedy fixing on bisect ppx!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I was wrong about bisect.exclude, it should be .re extension:

relude/bisect.exclude

Lines 1 to 2 in 777d04d

file regexp ".*_test\.re$"
file regexp ".*\/testUtils\/.*\.re$"

Latest code coverage report: https://coveralls.io/builds/30182377

package.json Outdated
@@ -32,6 +33,7 @@
"license": "MIT",
"devDependencies": {
"@glennsl/bs-jest": "^0.5.1",
"bisect_ppx": "^2.3.1",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bisect_ppx has to be a regular dep; see aantron/bisect_ppx#309 (comment). This is because the ppx-flags in bsconfig.json cause Bisect to always be called. It's just when BISECT_ENABLE=yes is missing, Bisect leaves the code clean and uninstrumented. But the code still gets passed through Bisect.

Once BuckleScript releases dev ppx-flags (rescript-lang/rescript#3761) in the next release, it will be correct to move bisect_ppx to devDependencies, which is where it properly belongs :)

@aantron
Copy link

aantron commented Apr 20, 2020

If you can find a way to add an exit hook to Jest in one place, please let me know. Having to add afterAll to each file really is awkward. I'd be happy to offer better instructions. We had a discussion about that starting here recently: aantron/bisect_ppx#307 (comment).

Bisect will need a command-line flag for excluding files soon, as part of the improved future BuckleScript integration I've linked too many times :) I might as well add that now, so you can avoid a separate exclusions file, which is also awkward. It's going to allow passing regexps on the command line (i.e. through ppx-flags).

@aantron
Copy link

aantron commented Apr 20, 2020

Bisect_ppx master now allows dropping the bisect.exclude file in favor of

  "ppx-flags": [
    ["bisect_ppx/ppx",
      "--exclude-files", ".*_test\\.re",
      "--exclude-files", ".*\/testUtils\/.*\\.re"]
  ]

but I want to hold off on a release until the Jest exit hook issue.

@mlms13
Copy link
Member

mlms13 commented Apr 20, 2020

Just wanted to thank you both for the work you're doing on this! I'm excited to see where it ends up.

@andywhite37
Copy link
Member

Yeah, thank you for taking a stab at this!

@aantron
Copy link

aantron commented Apr 21, 2020

Ok, bisect_ppx@2.4.0 is now in npm. In addition to being able to delete bisect.exclude in favor of ppx-flags, you should also be able to delete jest.setup.js, because it has been upstreamed into Bisect (thanks!). In package.json,

"setupFilesAfterEnv": ["bisect_ppx/src/runtime/bucklescript/jest.js"]

should be enough.

Thanks for pushing this so we can smooth out the rough corners of the integration.

There is still some work left on the BuckleScript side to exclude bs-bastet and make Bisect into a true dev-only dependency.

@jihchi
Copy link
Contributor Author

jihchi commented Apr 21, 2020

This PR is better and cleaner now!

Thanks @aantron for the help on bisect ppx and make this happen!

@jihchi
Copy link
Contributor Author

jihchi commented Apr 21, 2020

the coverage report is also better and more sense now, take src/Relude_NonEmpty.re for example:

  1. Bisect (new, generated by npx bisect-ppx-report html)
  2. Jest (old)

@aantron
Copy link

aantron commented Apr 21, 2020

Great :)

Is that a comparison of Bisect (new) and Jest (old)? I got confused because of the big screenshot :)

@aantron
Copy link

aantron commented Apr 21, 2020

BTW, it's best to use the local HTML report (bisect-ppx-report html), because it shows expression-level coverage. Coveralls only supports per-line coverage, which can be confusing when there are several expressions on one line, or it can be not so clear what exactly about an expression triggered it to be instrumented.

@aantron
Copy link

aantron commented Apr 21, 2020

I basically use Coveralls only for the GitHub status and/or nag comment, or if anyone wants to examine the coverage without generating a report locally. As a developer, I use the HTML report during development (or the summary report for super quick iteration when I'm not trying to cover specific problem lines exactly yet).

@jihchi jihchi marked this pull request as ready for review April 25, 2020 05:46
@jihchi jihchi changed the title WIP: Use bisect PPX instead of Jest coverage Use bisect PPX instead of Jest coverage Apr 25, 2020
@mlms13
Copy link
Member

mlms13 commented Apr 26, 2020

I notice the title is no longer "WIP" and the PR is no longer marked as a draft! :) How are we feeling about this? I glanced through the PR, and I trust the two of you to self-review more than I trust myself on this one, but it looks good to me.

@aantron
Copy link

aantron commented Apr 26, 2020

It looks right to me.

@mlms13
Copy link
Member

mlms13 commented Apr 26, 2020

Oh wow, I just noticed that this gives Coveralls everything it needs to show the real report:

This is super exciting, and I'm ready to merge if you feel good about it @jihchi

@andywhite37
Copy link
Member

I think my only reservation is that bisect-ppx has to be listed as a dependency at this time, rather than a devDependency. How close are they to supporting ppxs as devDependencies? If it's a long way off, we can probably just merge this, but I'd rather it be a devDependency.

@aantron
Copy link

aantron commented Apr 26, 2020

@bobzhang has suggested that the feature needed for Bisect_ppx to become a devDependency may be included in the next BuckleScript release.

@mlms13
Copy link
Member

mlms13 commented Apr 26, 2020

Since bastet already has this listed as a dependency (for the same reason), we're already bringing this as a transitive dependency, so I'm not too concerned about having to add it to our package.json (though I agree it would be nice for Bucklescript to support dev ppxs).

@andywhite37
Copy link
Member

Ok, we might as well just merge it then. I'll open an issue to make it a devDependency when that is possible to do.

@mlms13 mlms13 merged commit 0f70f29 into reazen:master Apr 26, 2020
@jihchi jihchi deleted the use_bisect branch April 27, 2020 00:54
@jihchi
Copy link
Contributor Author

jihchi commented Apr 27, 2020

Thanks again @aantron for the help.

Thanks @mlms13 , @andywhite37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants