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

Include all source files in the report #275

Merged
merged 5 commits into from
Dec 4, 2014
Merged

Include all source files in the report #275

merged 5 commits into from
Dec 4, 2014

Conversation

gustavnikolaj
Copy link
Contributor

I wanted to have all my source files included in the report, no matter if they were tested or not, to provide a truthful picture of the actual coverage in the test suite, without doing weird hacks.

I tried using the --preload-sources option, but that had the unwanted side effect that it made it seem like you had quite a bit of extra coverage, which is the result of actually requireing the code.

So instead I tried implementing it by instrumenting the files which have not already been visited after the report has been built, and add those. The only workaround I needed to make was to reset the count of visits on FunctionDeclarations as those were given a count of one when instrumenting, to compensate for function hoisting.

The feature is hidden behind the --include-all-sources flag.

I feel that this pull request covers the case of #142, in a simpler way than what you outlined in #142 (comment). Am I missing something?

@gotwarlost
Copy link
Owner

Nicely done.

  • There is no need to create new instances of the instrumenter and transformer, is there? Please re-use existing objects so that everything is initialized in one place.
  • Please add a test (or hijack any test for preload sources to test this; see below)
  • I think it is weird to have both --preload-sources and --include-all-sources for the same command. Since your implementation is what people expect, let's do the following:
    • support both options and maintain backwards compatibility of interface
    • if --preload-sources is true just make include-all-sources true. Print a message to stderr saying that the preload option is deprecated and to use the new option instead.
  • Ensure that istanbul configuration is loaded correctly and there is a way to set your option in the config file (I think you have already done this, but it's been a while since I've looked at the config code so not sure)

hidden behind the --include-all-sources flag

After the coverage report has been built, manually instrument the files
which have not been visited already, and add those to the report.

When instrumenting the code manually, reset the count in the coverState
object. FunctionDeclaration is given a visit number of 1 when
instrumenting to compensate for function hoisting. We don't want that
in this case, as the file was not actually loaded.
@gustavnikolaj
Copy link
Contributor Author

Thanks for the very thorough feedback! Totally agree with everything - I was a bit lazy when first sending this pull request, as I wanted to verify the solution before putting in too much work. :-)

  • I rebased the pull request onto current master.
  • I looked into the code, and there is no reason why I cannot reuse the instrumenter and transformer. The state in the instrumenter is reset every time transformer (or rather instrumentAstSync) is called. So I removed that.
  • I wrote a test for the feature based on the preload sources test. Which also ensures that the flag works from the command line.
  • I added a test to ensure that the default value is false and that it is set per default. I did not find any excisting test loading configuration files
  • I removed everything related to the preload sources feature, except some basic configuration stuff, to be able to warn when I get the flag.

I'm not sure about the way that I chose to warn the users when using the preload-sources switch - there might be a better place to do it. But it was the best place I could find / think of.

@@ -24,6 +24,7 @@ function defaultConfig() {
'complete-copy': false,
'save-baseline': false,
'baseline-file': './coverage/coverage-baseline.json',
'include-all-sources': false,
'preload-sources': false
Copy link
Owner

Choose a reason for hiding this comment

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

Can we remove the preload-sources key from the default config safely? (that object is printed as help for the default configuration: istanbul help config). I don't believe this will impact anything but this needs to be verified.

@gotwarlost
Copy link
Owner

Excellent work! Could you attempt to remove the preload-sources option from the config (see my line comment). Just make sure nothing breaks because of this.

@gustavnikolaj
Copy link
Contributor Author

Removing preload-sources from the default config means that it wont be set in the object. I suspect that the reason is that no config option should not have a default value, which makes kind of sense. If this is unintended and not considered a feature, we could change that too, but I'd say that it's outside the scope of this pull request.

So it's still present in the parsing of options passed to run-with-cover.js, to be propagated through to the configuration loader. And it's still present in the configuration loader, as it has to be there to be allowed to have effect.

We might be able to golf down the number of "preload-sources" occurances in the code by moving the warning logic into run-with-cover.js - but it seems to be more proper to have that check in the configuration loading code, as that would catch all occurances of that flag - and not just those passed on the commandline to the istanbul cover method. I'm not sure if there's any other way of doing it, as that is how I normally use the tool - but I assume that you could pass it in via a yaml config file aswell? If I'm right about that, moving it out of the configuration code would mean that the warning would go unnoticed for people using config files, and that's not optimal of course.

I'll be more than willing to change it, if any of my conclusions are wrong. :-)

@gotwarlost
Copy link
Owner

I'm going to merge this and deal with that specific issue later. Thanks for all the hard work!

gotwarlost added a commit that referenced this pull request Dec 4, 2014
…dedFilesInReport

Include all source files in the report
@gotwarlost gotwarlost merged commit 06ff06d into gotwarlost:master Dec 4, 2014
@gustavnikolaj gustavnikolaj deleted the feature/includeAllNonExcludedFilesInReport branch December 4, 2014 23:04
@gustavnikolaj
Copy link
Contributor Author

No problem! It's a pleasure to be able to contribute a little back to a project that you use a lot :-)

@gotwarlost
Copy link
Owner

Available in v0.3.5

@gustavnikolaj
Copy link
Contributor Author

Yay! Thanks :-)

@slonoed
Copy link

slonoed commented Apr 10, 2015

Hi all!

I need some help with this. I use babel and es6 syntax. I pass --compilers js:babel/register to mocha, but other files (required by istanbul) not parsed.

@ghost
Copy link

ghost commented Apr 10, 2015

Is there an example somewhere of how to use this flag? I've tried

istanbul cover --include-all-sources jasmine
and
istanbul cover --include-all-sources --include-pattern=**/*.js jasmine

but I'm not seeing any difference in output… I'm using v0.3.13.

Cheers!

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