fix(#141): strip leading './' from coverage-check paths
Non-git tools (e.g. `find`, `ls`) can emit paths with a "./" prefix. Without stripping this, "./cmd/foo.go" would not match the glob "cmd/**", producing a false-positive uncovered-file failure. Fix: add strings.TrimPrefix(f, "./") after backslash normalization. Test: TestRunValidateDocmap_DotSlashPrefix Addresses MINOR finding in review #4175.
This commit is contained in:
@@ -178,6 +178,9 @@ func runValidateDocmap(args []string) int {
|
|||||||
// Normalize Windows-style backslashes to forward slashes so that
|
// Normalize Windows-style backslashes to forward slashes so that
|
||||||
// changed-file paths from git on Windows match doc-map globs.
|
// changed-file paths from git on Windows match doc-map globs.
|
||||||
f = strings.ReplaceAll(f, "\\", "/")
|
f = strings.ReplaceAll(f, "\\", "/")
|
||||||
|
// Strip a leading "./" emitted by non-git tools (e.g. `find`) so that
|
||||||
|
// paths like "./cmd/foo.go" match doc-map globs written as "cmd/**".
|
||||||
|
f = strings.TrimPrefix(f, "./")
|
||||||
if !review.FileCoveredByDocMap(cfg, f) {
|
if !review.FileCoveredByDocMap(cfg, f) {
|
||||||
uncovered = append(uncovered, f)
|
uncovered = append(uncovered, f)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -624,3 +624,28 @@ func TestValidateDocmapPath_NonRegularFile(t *testing.T) {
|
|||||||
t.Errorf("expected regular-file rejection in stderr, got %q", stderr)
|
t.Errorf("expected regular-file rejection in stderr, got %q", stderr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRunValidateDocmap_DotSlashPrefix verifies that paths emitted with a
|
||||||
|
// leading "./" (e.g. from `find` or `ls`) match doc-map globs correctly.
|
||||||
|
// Without TrimPrefix, "./cmd/foo.go" would not match the pattern "cmd/**".
|
||||||
|
func TestRunValidateDocmap_DotSlashPrefix(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
makeDocFile(t, dir, "docs/foo.md")
|
||||||
|
|
||||||
|
docmap := makeDocmapInDir(t, dir, `
|
||||||
|
mappings:
|
||||||
|
- paths:
|
||||||
|
- "cmd/**"
|
||||||
|
docs:
|
||||||
|
- docs/foo.md
|
||||||
|
`)
|
||||||
|
|
||||||
|
// File with a leading "./" should be treated as covered.
|
||||||
|
code, _, stderr := stdinValidateDocmap(t,
|
||||||
|
"./cmd/foo.go\n",
|
||||||
|
[]string{"--docmap", docmap, "--repo-root", dir},
|
||||||
|
)
|
||||||
|
if code != 0 {
|
||||||
|
t.Errorf("expected exit 0 for './' prefixed covered file, got %d; stderr: %q", code, stderr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user