Open kenjones-cisco opened 7 years ago
Hi,
Thanks for your questions.
The plugin use the report generated by gocov-xml
. We used gocov
to convert a coverage profile generate by go test.
But we can't launch go test
for all packages in one time (if you have a any idea or solution for do that,...). So we need to launch go test
and generate the coverage profile and send this profile to gocov
for convert and the conversion to gocov-xml
.
It's not a problem. I used Files.walk for explore the project and visit directories.
How did you do for generate a single coverage file ? If it's possible I will can modify the plugin to use a single coverage file.
Generating single coverage.xml
workdir=cover
profile="$workdir/cover.out"
mode=count
for pkg in $(glide nv);
do
for subpkg in $(go list "${pkg}");
do
f="$workdir/$(echo "$subpkg" | tr / -).cover"
go test -v -covermode="$mode" -coverprofile="$f" "$subpkg" >> test.out
done
done
set -- "$workdir"/*.cover
if [ ! -f "$1" ]; then
echo "No Test Cases"; exit 0
fi
echo "mode: $mode" >"$profile"
grep -h -v "^mode:" "$workdir"/*.cover >>"$profile"
rm -f test.xml coverage.xml
go2xunit -input test.out -output test.xml
gocov convert "$profile" | gocov-xml > coverage.xml
That is how I have it scripted to generate the test.xml
and coverage.xml
that I have been using.
My alternate approach would be just to write the coverage.xml files into the cover/ directory as I don't like the idea of writing any files to my actual package directories.
@thibaultfalque I guess that making sure to search first a single cover file in the root directory else look for multiple cover files in subdirectories could do the trick.
@kenjones-cisco thanks for the script. Would you agree that we display it in the README page of the script?
Sure, no problem.
FYI, to make it not specific to glide, you should can use this:
workdir=cover
profile="$workdir/cover.out"
mode=count
for pkg in $(go list ./...);
do
f="$workdir/$(echo "$pkg" | tr / -).cover"
go test -v -covermode="$mode" -coverprofile="$f" "$pkg" >> test.out
done
set -- "$workdir"/*.cover
if [ ! -f "$1" ]; then
rm -f "$results" || :
echo "No Test Cases"; exit 0
fi
echo "mode: $mode" >"$profile"
grep -h -v "^mode:" "$workdir"/*.cover >>"$profile"
rm -f test.xml coverage.xml
go2xunit -input test.out -output test.xml
gocov convert "$profile" | gocov-xml > coverage.xml
@thibaultfalque are we done here?
See the PR by @Kernle32DLL #50
For a bit more context what gocov test ./...
does - it internally does exactly that. Its walks trough the folders, and executes go test
, and gocov convert
. The nice thing is, that this method aggregates the results, and creates a single coverage.xml
, without some shell magic (which is good, since this way it can be used across platforms).
Also also - Golang 1.10 will bring support for go test ./... -coverprofile...
, which was not possible so far (hence all the extra hops with executing go test
in each package separately).
Thanks @Kernle32DLL for your explanation
Description
In the README I noticed the following section:
Currently I'm able to generate a single coverage.xml file for an entire project with multiple packages within the same project. And cobertura within Jenkins is able to read and process the file and show all of the packages.
Example single coverage.xml:
Is there a technical limitation within SonarQube that requires the coverage to be done as one coverage.xml per package? Or is this rule imposed by the plugin?
As a follow up, does the coverage.xml have to be in the same directory as the code? Or is it possible to leverage a
cover
directory that has a directory for each package and the coverage.xml file exists within that directory?Example Directory structure for coverage: