-
Notifications
You must be signed in to change notification settings - Fork 157
Implemented combine coverprofile in the case of rerun failed #387
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
Open
yihuaf
wants to merge
5
commits into
gotestyourself:main
Choose a base branch
from
yihuaf:feature/yihuaf/rerun-cov-combine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright (c) 2015, Wade Simmons | ||
| // All rights reserved. | ||
|
|
||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are met: | ||
|
|
||
| // 1. Redistributions of source code must retain the above copyright notice, this | ||
| // list of conditions and the following disclaimer. | ||
| // 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| // this list of conditions and the following disclaimer in the documentation | ||
| // and/or other materials provided with the distribution. | ||
|
|
||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
| // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| // gocovmerge takes the results from multiple `go test -coverprofile` runs and | ||
| // merges them into one profile | ||
|
|
||
| // Taken from: https://github.com/wadey/gocovmerge @ b5bfa59ec0adc420475f97f89b58045c721d761c | ||
|
|
||
| package coverprofile | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "sort" | ||
|
|
||
| "golang.org/x/tools/cover" | ||
| ) | ||
|
|
||
| func mergeProfiles(p *cover.Profile, merge *cover.Profile) { | ||
| if p.Mode != merge.Mode { | ||
| log.Fatalf("cannot merge profiles with different modes") | ||
| } | ||
| // Since the blocks are sorted, we can keep track of where the last block | ||
| // was inserted and only look at the blocks after that as targets for merge | ||
| startIndex := 0 | ||
| for _, b := range merge.Blocks { | ||
| startIndex = mergeProfileBlock(p, b, startIndex) | ||
| } | ||
| } | ||
|
|
||
| func mergeProfileBlock(p *cover.Profile, pb cover.ProfileBlock, startIndex int) int { | ||
| sortFunc := func(i int) bool { | ||
| pi := p.Blocks[i+startIndex] | ||
| return pi.StartLine >= pb.StartLine && (pi.StartLine != pb.StartLine || pi.StartCol >= pb.StartCol) | ||
| } | ||
|
|
||
| i := 0 | ||
| if !sortFunc(i) { | ||
| i = sort.Search(len(p.Blocks)-startIndex, sortFunc) | ||
| } | ||
| i += startIndex | ||
| if i < len(p.Blocks) && p.Blocks[i].StartLine == pb.StartLine && p.Blocks[i].StartCol == pb.StartCol { | ||
| if p.Blocks[i].EndLine != pb.EndLine || p.Blocks[i].EndCol != pb.EndCol { | ||
| log.Fatalf("OVERLAP MERGE: %v %v %v", p.FileName, p.Blocks[i], pb) | ||
| } | ||
| switch p.Mode { | ||
| case "set": | ||
| p.Blocks[i].Count |= pb.Count | ||
| case "count", "atomic": | ||
| p.Blocks[i].Count += pb.Count | ||
| default: | ||
| log.Fatalf("unsupported covermode: '%s'", p.Mode) | ||
| } | ||
| } else { | ||
| if i > 0 { | ||
| pa := p.Blocks[i-1] | ||
| if pa.EndLine >= pb.EndLine && (pa.EndLine != pb.EndLine || pa.EndCol > pb.EndCol) { | ||
| log.Fatalf("OVERLAP BEFORE: %v %v %v", p.FileName, pa, pb) | ||
| } | ||
| } | ||
| if i < len(p.Blocks)-1 { | ||
| pa := p.Blocks[i+1] | ||
| if pa.StartLine <= pb.StartLine && (pa.StartLine != pb.StartLine || pa.StartCol < pb.StartCol) { | ||
| log.Fatalf("OVERLAP AFTER: %v %v %v", p.FileName, pa, pb) | ||
| } | ||
| } | ||
| p.Blocks = append(p.Blocks, cover.ProfileBlock{}) | ||
| copy(p.Blocks[i+1:], p.Blocks[i:]) | ||
| p.Blocks[i] = pb | ||
| } | ||
| return i + 1 | ||
| } | ||
|
|
||
| func addProfile(profiles []*cover.Profile, p *cover.Profile) []*cover.Profile { | ||
| i := sort.Search(len(profiles), func(i int) bool { return profiles[i].FileName >= p.FileName }) | ||
| if i < len(profiles) && profiles[i].FileName == p.FileName { | ||
| mergeProfiles(profiles[i], p) | ||
| } else { | ||
| profiles = append(profiles, nil) | ||
| copy(profiles[i+1:], profiles[i:]) | ||
| profiles[i] = p | ||
| } | ||
| return profiles | ||
| } | ||
|
|
||
| func dumpProfiles(profiles []*cover.Profile, out io.Writer) { | ||
| if len(profiles) == 0 { | ||
| return | ||
| } | ||
| fmt.Fprintf(out, "mode: %s\n", profiles[0].Mode) | ||
| for _, p := range profiles { | ||
| for _, b := range p.Blocks { | ||
| fmt.Fprintf(out, "%s:%d.%d,%d.%d %d %d\n", p.FileName, b.StartLine, | ||
| b.StartCol, b.EndLine, b.EndCol, b.NumStmt, b.Count) | ||
| } | ||
| } | ||
| } | ||
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,78 @@ | ||
| package coverprofile | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "golang.org/x/tools/cover" | ||
| ) | ||
|
|
||
| // ParseCoverProfile parse the coverprofile file from the flag | ||
| func ParseCoverProfile(args []string) (bool, string) { | ||
| for _, arg := range args { | ||
| if strings.HasPrefix(arg, "-coverprofile=") { | ||
| return true, strings.TrimPrefix(arg, "-coverprofile=") | ||
| } | ||
| } | ||
|
|
||
| return false, "" | ||
| } | ||
|
|
||
| // WriteCoverProfile writes the cover profile to the file | ||
| func WriteCoverProfile(profiles []*cover.Profile, filename string) error { | ||
| // Create a tmp file to write the merged profiles to. Then use os.Rename to | ||
| // atomically move the file to the main profile to mimic the effect of | ||
| // atomic replacement of the file. Note, we can't put the file on tempfs | ||
| // using the all the nice utilities around tempfiles. In places like docker | ||
| // containers, calling os.Rename on a file that is on tempfs to a file on | ||
| // normal filesystem partition will fail with errno 18 invalid cross device | ||
| // link. | ||
| tempFile := fmt.Sprintf("%s.tmp", filename) | ||
| f, err := os.Create(tempFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create coverprofile file: %v", err) | ||
| } | ||
|
|
||
| dumpProfiles(profiles, f) | ||
| if err := f.Close(); err != nil { | ||
| return fmt.Errorf("failed to close temp file %s: %v", tempFile, err) | ||
| } | ||
|
|
||
| if err := os.Rename(tempFile, filename); err != nil { | ||
| return fmt.Errorf("failed to rename temp file %s to %s: %v", tempFile, filename, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // CombineProfiles combines the cover profiles together | ||
| func CombineProfiles(this []*cover.Profile, others ...*cover.Profile) []*cover.Profile { | ||
| merged := this | ||
| for _, p := range others { | ||
| merged = addProfile(merged, p) | ||
| } | ||
| return merged | ||
| } | ||
|
|
||
| // A helper function to expose the destination of the merged profile so we | ||
| // testing is easier. | ||
| func combine(main string, others []*cover.Profile, out string) error { | ||
| mainProfiles, err := cover.ParseProfiles(main) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse coverprofile %s: %v", main, err) | ||
| } | ||
|
|
||
| merged := mainProfiles | ||
|
|
||
| for _, other := range others { | ||
| merged = CombineProfiles(mainProfiles, other) | ||
| } | ||
|
|
||
| return WriteCoverProfile(merged, out) | ||
| } | ||
|
|
||
| // Combine merges the `others` cover profile with the main cover profile file | ||
| func Combine(main string, others []*cover.Profile) error { | ||
| return combine(main, others, main) | ||
| } |
Oops, something went wrong.
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.
I think we may need to copy the license file
(https://github.com/wadey/gocovmerge/blob/master/LICENSE) into
internal/coverprofileto comply with the license.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 added the LICENSE to the file header since it is only the file we copied over. Please let me know if this is good enough.