Hey.
For know after running coverage I have html report like:
src/dir1/ ~coverage report~
src/dir2/ ~coverage report~
src/dir1/subdir1/ ~coverage report~
src/dir3/subdir3/ ~coverage report~
src/dir3/subdir3/subsubdir3/ ~coverage report~
framework/1/ ~coverage report~
framework/1/2/ ~coverage report~
framework/1/2/3 ~coverage report~
framework/2/ ~coverage report~
Is it possible to have overall main directories coverage? I mean in the end I want it as:
src/ ~coverage report~
framework/ ~coverage report~
So for now I couldn't find a proper solution for that..
No there isn't a solution for this now.
:(
Maybe is it possible somehow to get all that collumns and merge them manually? Maybe to append javascript to the report and count inside?
You can solve it with a little jq magic.
Take the following folder structure, and assume that both projects are written in node.js and have an istanbul report generated for them.
~/Projects/ $ ls -1
someProject/
anotherProject/
First we combine the coverage reports in the two projects using jq: (I'll break the command down below)
~/Projects/ $ jq -s 'reduce .[] as $report ({}; . + $report)' someProject/coverage/coverage.json anotherProject/coverage/coverage.json > coverage.json
The result of that will be a new coverage.json file in ~/Projects/coverage.json.
~/Projects/ $ ls -1
someProject/
anotherProject/
coverage.json
Next, we will use istanbuls report tool to generate a new lcov report based on that. You could generate any report you wish, this is just an example. I'm using one of the projects istanbul binaries to avoid installing it globally... The --include flag is defaulted to a glob pattern, which will take a while to traverse all your folders if you have a big directory, so you might aswell just specify the file that you are going to use.
~/Projects/ $ ./someProject/node_modules/.bin/istanbul report lcov --include coverage.json
Which will give you the following:
~/Projects/ $ ls -1
someProject/
anotherProject/
coverage.json
coverage/
~/Projects/ $ tree coverage
coverage
โโโ lcov.info
โโโ lcov-report
โโโ index.html
โโโ someProject
โย ย โโโ ...
โโโ anotherProject
โย ย โโโ ...
โโโ prettify.css
โโโ prettify.js
~/Projects/ $ google-chrome coverage/lvoc-report/index.html
That should do it :-)
The jq command may seem a bit overwhelming at first, but it's not that bad once you get accustomed to it.
$ jq -s 'reduce .[] as $report ({}; . + $report)' someProject/coverage/coverage.json anotherProject/coverage/coverage.json > coverage.json
-s puts jq in slurp mode, which will convert all json files given to it and put it into an array.
'reduce .[] as $report ({}; . + $report)' is the real magic. We pass alk the coverage.json files we had into jq and this method. The -s mode reads those files in and puts them in a list. The + operator is a lazy object merge, where it just takes all properties from the two objects and adds to a third object - it adds the properties from the first object first, and then the second; so if two keys clash the latter will overwride the first. Which means that the jq syntax is roughly the same as the following javascript code, given that the variable input is our array of input javascript coverage report objects.
input.reduce(function (previousValue, currentValue) {
Object.keys(currentValue).forEach(function (key) {
previousValue[key] = currentValue[key];
});
return previousValue;
}, {});
The second argument to the reduce call in the above javascript code is the initial value for the reduce. If you don't provide it, it will be the first value of the array in vanilla js, but jq requires you to provide the initial value in the reduce method, so it's set explicitly here to be as close as possible to what happens in jq.
If you are too lazy to type out the file names you can turn up the heat in bash and use the find command in backticks to glob out your coverage.json files.
$ jq -s 'reduce .[] as $report ({}; . + $report)' `find . -mindepth 3 -maxdepth 3 -type f -name coverage.json` > coverage.json
-type f means it will look for files, and -name coverage.json means that those files must also be called coverage.json to be considered. The mindepth and maxdepth options are the key here - those decide how nested they must at least or at most be.
$ find . -mindepth 3 -maxdepth 3 -type f -name coverage.json
./someProject/coverage/coverage.json
./anotherProject/coverage/coverage.json
It will not find the below files because of the nesting levels:
./coverage.json
./coverage/coverage.json
./someProject/node_modules/someDepencency/coverage/coverage.json
The above works because the coverage.json file generated is an object with a property for each file. The property name is the absolute path to the given file. That is essentially the most important detail needed to understand why the above works, which I managed to forget anyway. It's getting late... :-)
Most helpful comment
You can solve it with a little jq magic.
Take the following folder structure, and assume that both projects are written in node.js and have an istanbul report generated for them.
First we combine the coverage reports in the two projects using
jq: (I'll break the command down below)The result of that will be a new coverage.json file in
~/Projects/coverage.json.Next, we will use istanbuls report tool to generate a new lcov report based on that. You could generate any report you wish, this is just an example. I'm using one of the projects istanbul binaries to avoid installing it globally... The --include flag is defaulted to a glob pattern, which will take a while to traverse all your folders if you have a big directory, so you might aswell just specify the file that you are going to use.
Which will give you the following:
That should do it :-)
The jq command may seem a bit overwhelming at first, but it's not that bad once you get accustomed to it.
-sputs jq in slurp mode, which will convert all json files given to it and put it into an array.'reduce .[] as $report ({}; . + $report)'is the real magic. We pass alk the coverage.json files we had into jq and this method. The-smode reads those files in and puts them in a list. The+operator is a lazy object merge, where it just takes all properties from the two objects and adds to a third object - it adds the properties from the first object first, and then the second; so if two keys clash the latter will overwride the first. Which means that the jq syntax is roughly the same as the following javascript code, given that the variableinputis our array of input javascript coverage report objects.The second argument to the reduce call in the above javascript code is the initial value for the reduce. If you don't provide it, it will be the first value of the array in vanilla js, but jq requires you to provide the initial value in the reduce method, so it's set explicitly here to be as close as possible to what happens in jq.
If you are too lazy to type out the file names you can turn up the heat in bash and use the find command in backticks to glob out your coverage.json files.
-type fmeans it will look for files, and-name coverage.jsonmeans that those files must also be called coverage.json to be considered. The mindepth and maxdepth options are the key here - those decide how nested they must at least or at most be.It will not find the below files because of the nesting levels: