Skip to content

Commit

Permalink
Merge pull request #1845 from reviewdog/normalize-rel-path
Browse files Browse the repository at this point in the history
Normalize path in related locations
  • Loading branch information
haya14busa authored Jul 23, 2024
2 parents 1e5fa6f + b251a1b commit 63f8cc2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### :sparkles: Release Note <!-- optional -->

### :rocket: Enhancements
- ...
- [#1845](https://github.com/reviewdog/reviewdog/pull/1845) Normalize file path in `related_locations` too.

### :bug: Fixes
- ...
Expand Down
1 change: 0 additions & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func FilterCheck(results []*rdf.Diagnostic, diff []*diff.FileDiff, strip int,
for _, result := range results {
check := &FilteredDiagnostic{Diagnostic: result, SourceLines: make(map[int]string)}
loc := result.GetLocation()
loc.Path = pathutil.NormalizePath(loc.GetPath(), cwd, "")
startLine := int(loc.GetRange().GetStart().GetLine())
endLine := int(loc.GetRange().GetEnd().GetLine())
if endLine == 0 {
Expand Down
18 changes: 18 additions & 0 deletions pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package pathutil
import (
"path/filepath"
"strings"

"github.com/reviewdog/reviewdog/proto/rdf"
)

// NormalizePath return normalized path with workdir and relative path to
Expand Down Expand Up @@ -42,6 +44,22 @@ func NormalizeDiffPath(diffpath string, strip int) string {
return filepath.ToSlash(filepath.Clean(path))
}

// NormalizePathInResults normalize file path in RDFormat results.
func NormalizePathInResults(results []*rdf.Diagnostic, cwd string) {
for _, result := range results {
normalizeLocation(result.GetLocation(), cwd)
for _, rel := range result.GetRelatedLocations() {
normalizeLocation(rel.GetLocation(), cwd)
}
}
}

func normalizeLocation(loc *rdf.Location, cwd string) {
if loc != nil {
loc.Path = NormalizePath(loc.GetPath(), cwd, "")
}
}

func contains(path, base string) bool {
ps := splitPathList(path)
bs := splitPathList(base)
Expand Down
49 changes: 49 additions & 0 deletions pathutil/pathutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pathutil

import (
"strings"
"testing"

"github.com/reviewdog/reviewdog/proto/rdf"
)

func TestNormalizePathInResults(t *testing.T) {
cwd := "/path/to/cwd"
results := []*rdf.Diagnostic{
{
Location: &rdf.Location{
Path: cwd + "/" + "sample_1_abs.txt",
},
},
{
Location: &rdf.Location{
Path: "sample_2_rel.txt",
},
},
{
RelatedLocations: []*rdf.RelatedLocation{
{
Location: &rdf.Location{
Path: cwd + "/" + "sample_related_1_abs.txt",
},
},
{
Location: &rdf.Location{
Path: "sample_related_2_rel.txt",
},
},
},
},
}
NormalizePathInResults(results, cwd)
for _, result := range results {
if strings.HasPrefix(result.GetLocation().GetPath(), cwd) {
t.Errorf("path unexpectedly contain prefix: %s", result.GetLocation().GetPath())
}
for _, rel := range result.GetRelatedLocations() {
if strings.HasPrefix(rel.GetLocation().GetPath(), cwd) {
t.Errorf("related locations path unexpectedly contain prefix: %s", rel.GetLocation().GetPath())
}
}
}
}
3 changes: 3 additions & 0 deletions reviewdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/reviewdog/reviewdog/diff"
"github.com/reviewdog/reviewdog/filter"
"github.com/reviewdog/reviewdog/parser"
"github.com/reviewdog/reviewdog/pathutil"
"github.com/reviewdog/reviewdog/proto/rdf"
)

Expand Down Expand Up @@ -73,6 +74,8 @@ func (w *Reviewdog) runFromResult(ctx context.Context, results []*rdf.Diagnostic
return err
}

pathutil.NormalizePathInResults(results, wd)

checks := filter.FilterCheck(results, filediffs, strip, wd, w.filterMode)
hasViolations := false

Expand Down

0 comments on commit 63f8cc2

Please sign in to comment.