Skip to content

Commit

Permalink
Use absolute paths to read source files
Browse files Browse the repository at this point in the history
  • Loading branch information
konradreiche committed Dec 29, 2023
1 parent 81357f0 commit f1624a7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func command(stdin io.Reader, stdout io.Writer) error {
if err != nil {
return err
}
return printDiff(stdout, profiles, moduleInfo.modulePath)
return printDiff(stdout, profiles, moduleInfo)
}

func parseCoverProfiles(stdin io.Reader) ([]*cover.Profile, error) {
Expand Down
23 changes: 23 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ func TestCommand(t *testing.T) {
}
})

t.Run("from-file-in-subdirectory", func(t *testing.T) {
if err := os.Chdir("testdata"); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.Chdir(projectPath); err != nil {
t.Fatal(err)
}
})
os.Args[1] = filepath.Join(projectPath, "testdata/coverage.out")

var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
t.Fatal(err)
}

got := stdout.String()
want := readFile(t, "coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})

t.Run("outside-go-module", func(t *testing.T) {
changeDir(t, "..")

Expand Down
18 changes: 10 additions & 8 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (
"golang.org/x/tools/cover"
)

func printDiff(stdout io.Writer, profiles []*cover.Profile, modulePath string) error {
func printDiff(stdout io.Writer, profiles []*cover.Profile, moduleInfo moduleInfo) error {
for _, profile := range profiles {
// create file path using absolute path to module
filepath := strings.ReplaceAll(profile.FileName, modulePath+"/", "")

b, err := os.ReadFile(filepath)
// use absolute path to module to read source code
absPath := strings.ReplaceAll(profile.FileName, moduleInfo.modulePath, moduleInfo.moduleDir)
b, err := os.ReadFile(absPath)
if err != nil {
return fmt.Errorf("os.ReadFile: %w", err)
}
Expand All @@ -34,10 +33,13 @@ func printDiff(stdout io.Writer, profiles []*cover.Profile, modulePath string) e
}
}

// use relative path to source code for diff headers
relPath := strings.ReplaceAll(profile.FileName, moduleInfo.modulePath+"/", "")

// print diff file headers
fmt.Fprintf(stdout, "diff --git a/%s b/%s\n", filepath, filepath)
fmt.Fprintf(stdout, "--- a/%s\n", filepath)
fmt.Fprintf(stdout, "+++ b/%s\n", filepath)
fmt.Fprintf(stdout, "diff --git a/%s b/%s\n", relPath, relPath)
fmt.Fprintf(stdout, "--- a/%s\n", relPath)
fmt.Fprintf(stdout, "+++ b/%s\n", relPath)

// print diff index header
fmt.Fprintf(stdout, "@@ -%d,%d +%d,%d @@ %s\n", 0, 0, len(lines), 0, lines[0])
Expand Down
9 changes: 4 additions & 5 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"path/filepath"
"runtime"
"runtime/debug"
"testing"

"golang.org/x/tools/cover"
Expand All @@ -20,13 +19,13 @@ func TestPrintDiff(t *testing.T) {
if err != nil {
t.Fatal(err)
}
bi, ok := debug.ReadBuildInfo()
if !ok {
t.Fatal("binary build information not available")
moduleInfo, err := findModuleInfo()
if err != nil {
t.Fatal(err)
}

var b bytes.Buffer
if err := printDiff(&b, profiles, bi.Path); err != nil {
if err := printDiff(&b, profiles, moduleInfo); err != nil {
t.Fatal(err)
}
}

0 comments on commit f1624a7

Please sign in to comment.