-
Notifications
You must be signed in to change notification settings - Fork 146
New: Ensure code coverage is met #651
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
Closed
naveensrinivasan
wants to merge
2
commits into
slsa-framework:main
from
naveensrinivasan:naveen/feat/code-coverage
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"github.com/slsa-framework/slsa-github-generator/github":70.4, | ||
"github.com/slsa-framework/slsa-github-generator/internal/builders/generic": 52.3, | ||
"github.com/slsa-framework/slsa-github-generator/internal/builders/go":17.1, | ||
"github.com/slsa-framework/slsa-github-generator/internal/errors":100.0, | ||
"github.com/slsa-framework/slsa-github-generator/internal/utils":72.1, | ||
"github.com/slsa-framework/slsa-github-generator/signing/envelope":82.4, | ||
"github.com/slsa-framework/slsa-github-generator/slsa":54.6 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Go Coverage tool | ||
|
||
The goal of the coverage tool is to measure the coverage of the code base for Golang. | ||
|
||
## Usage | ||
Execute the following command to get the coverage and store it in a file: | ||
1. `go test -coverprofile=coverage ./... | run ./hack/codecoverage/main.go coverage.json 70` | ||
2. The coverage.json contains the percentage of the code coverage that is required to pass for certain packages. This is usually because they don't match the desired coverage. | ||
```json | ||
{ | ||
"github.com/slsa-framework/slsa-github-generator/github":70.4, | ||
"github.com/slsa-framework/slsa-github-generator/internal/builders/generic": 52.3, | ||
"github.com/slsa-framework/slsa-github-generator/internal/builders/go":17.1, | ||
"github.com/slsa-framework/slsa-github-generator/internal/errors":100.0, | ||
"github.com/slsa-framework/slsa-github-generator/internal/utils":72.1, | ||
"github.com/slsa-framework/slsa-github-generator/signing/envelope":82.4, | ||
"github.com/slsa-framework/slsa-github-generator/slsa":54.6 | ||
} | ||
``` | ||
3. The `COVERAGE_PERCENTAGE` is the percentage of the code coverage that is required to pass for all the packages except the ones that are mentioned in the `THRESHOLD_FILE`. | ||
4. The coverage tool will fail if the coverage is below the threshold for any package. | ||
``` shell | ||
2022/07/29 16:14:41 github.com/slsa-framework/slsa-github-generator/pkg/foo is below the threshold of 71.000000 | ||
exit status 1 | ||
``` | ||
|
||
### Design choices | ||
|
||
1. The coverage tool should not depend on any other tools. It should work of the results from the `go test` command. | ||
2. Coverage threshold should be configurable for each repository - for example `70%` within the repository. | ||
3. A setting file should override the coverage threshold for a given package within the repository. `github.com/foo/bar/xyz : 61` | ||
4. The coverage tool should use native `go` tools and shouldn't depend on external vendors. | ||
5. The coverage tool should be configurable as part of the PR to fail if the desired threshold is not met. | ||
6. Contributors should be able to run it locally if desired before doing a PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/slsa-framework/slsa-github-generator/hack/coverage | ||
|
||
go 1.18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2022 SLSA Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 3 { | ||
log.Fatal("Usage: <threshold-file> <coverage-percent>") | ||
} | ||
thresholdFile := os.Args[1] | ||
if thresholdFile == "" { | ||
log.Fatalf("The thresholdfile cannot be empty.") | ||
} | ||
thresholdMap, err := parseCoverageThreshold(thresholdFile) | ||
if err != nil { | ||
log.Fatalf("Error parsing threshold file: %v", err) | ||
} | ||
coveragePercentage := os.Args[2] | ||
if coveragePercentage == "" { | ||
log.Fatalf("The coverage percentage cannot be empty.") | ||
} | ||
coveragePercentageFloat, err := strconv.ParseFloat(coveragePercentage, 64) | ||
if err != nil { | ||
log.Fatalf("Error parsing coverage percentage: %v", err) | ||
} | ||
// read stream from stdin | ||
scanner := bufio.NewScanner(os.Stdin) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.Contains(line, "coverage: ") { | ||
parts := strings.Fields(line) | ||
if len(parts) < 5 { | ||
continue | ||
} | ||
percentage, err := strconv.ParseFloat(strings.Trim(parts[4], "%"), 64) | ||
if err != nil { | ||
log.Fatalf("invalid line: %s", line) | ||
} | ||
pack := parts[1] | ||
if val, ok := thresholdMap[pack]; ok { | ||
if percentage < val { | ||
log.Fatalf("coverage for %s is below threshold: %f < %f", pack, percentage, val) | ||
} | ||
continue | ||
} | ||
// if the package is not in the threshold map, then we check if the coverage is below the threshold | ||
if percentage < coveragePercentageFloat { | ||
log.Fatalf("coverage for %s is below threshold: %f < %f", pack, percentage, coveragePercentageFloat) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// parseCoverageThreshold parses the threshold file and returns a map. | ||
func parseCoverageThreshold(fileName string) (map[string]float64, error) { | ||
// Here is an example of the threshold file: | ||
/* | ||
{ | ||
"github.com/foo/bar/pkg/cryptoutils": 71.2, | ||
} | ||
*/ | ||
f, err := os.Open(fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
thresholdMap := make(map[string]float64) | ||
if err := json.NewDecoder(f).Decode(&thresholdMap); err != nil { | ||
return nil, err | ||
} | ||
return thresholdMap, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the folder called "hack"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is way harder, and there are existing tools like VSCode, and Go compiler which can do that.
Yes, if we don't meet 70% coverage.
As a dev, I would like to run the checks locally, and this gives that option
The goal is to figure out the code coverage without using external dependencies. The code coverage is provided by VSCode and Go compiler. This isn't the design of this tool. Only then the code coverage is met. High-quality tests are critical for having confidence.
Right now, we are flying blind without knowing what the coverage of the project is. Any time new code gets added/updated, the project or the PR reviewer isn't aware of the test coverage.
What would be your suggestion to address this concern? Something like codecov would also suffice. Do you think we should enable an online tool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason why running
go test
locally does not suffice?If codecov does it and they don't require write permissions, that sounds like an easy win, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bobcallaway Do you have thoughts on codecov or similar other online tools?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing we can do is open an HTML report of code coverage per file using the native go tools. Generate the coverage profile with go test, and then run
go tool cover
. We can make this a non-gating CI that allows reviewers (who ultimately should decide what can/cant be testable) to see if newly added code was tested.The CI job can also add a comment decomenting coverage % in each pkg. If all of this can be done without new code to maintain, that would be great.
e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A high code coverage percentage does not guarantee high quality in the test coverage
More important than the percentage of lines covered is human judgment over the actual lines of code (and behaviors) that aren’t being covered
Qualitative feedback is nice. Imposing threshold does not help IMO. I've seen this in scorecard: when the coverage goes down in a PR, we just let the PR go thru because that's the best we can do. Over time we don't even look at it because it's just noise and not actionable.
Having a general view of code coverage is great. Having it in a (blocking) PR as a percentage number provides no benefits and is not actionable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK cool! I understand if you have strong opinions on this 👍. I will close this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand your argument @laurentsimon -
we just let the PR go thru because thats the best we can do
- why not ask the contributor to add unit test coverage for what they're proposing to not erode the baseline percentage?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can go either way on making this a blocking pre-submit, but I agree there are merits either way.
I think that the biggest problem would be for a change that doesn't necessarily merit unit-tests or where unit-tests are especially hard to write, it might make doing it on that particular PR overly expensive just to maintain an arbitrary coverage baseline. We also have e2e tests that might cover certain scenarios better than unit-tests.
A better rule might be that we need to hit a baseline coverage % before we do a release?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good thing to enforce on "pre-release" -- if we do this it would be good to have a report generated per PR because accumulating tests at the end is a lot harder than per-change.
I think the problem was that per-file doesn't give reviewer or author enough actionable guidance. I still like the HTML report generated to help the reviewer evaluate test coverage of any added lines.