Skip to content

Commit

Permalink
Merge pull request #10 from k1LoW/use-line-directive
Browse files Browse the repository at this point in the history
Prepend line directive to copied source files and add test
  • Loading branch information
tenntenn committed Mar 1, 2024
2 parents 98f62c2 + bb4411c commit 6b80891
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
35 changes: 35 additions & 0 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -50,6 +51,17 @@ func WithModules(t *testing.T, srcdir string, modfile io.Reader) (dir string) {
}

for _, file := range files {
// Prepend line directive to .go files
if filepath.Ext(file.Name()) == ".go" {
fn := filepath.Join(path, file.Name())
rel, err := filepath.Rel(dir, fn)
if err != nil {
t.Fatal("cannot get relative path:", err)
}
if err := prependToFile(fn, fmt.Sprintf("//line %s:1\n", rel)); err != nil {
t.Fatal("cannot prepend line directive:", err)
}
}
if file.Name() == "go.mod" {
if modfile != nil {
fn := filepath.Join(path, "go.mod")
Expand Down Expand Up @@ -85,6 +97,29 @@ func WithModules(t *testing.T, srcdir string, modfile io.Reader) (dir string) {
return dir
}

func prependToFile(filename string, ld string) error {
f, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
return err
}
defer f.Close()

b, err := io.ReadAll(f)
if err != nil {
return err
}
if _, err := f.Seek(0, 0); err != nil {
return err
}
if _, err := f.WriteString(ld + "\n"); err != nil {
return err
}
if _, err := f.Write(b); err != nil {
return err
}
return nil
}

// ModFile opens a mod file with the path and fixes versions by the version fixer.
// If the path is direcotry, ModFile opens go.mod which is under the path.
func ModFile(t *testing.T, path string, fix modfile.VersionFixer) io.Reader {
Expand Down
33 changes: 33 additions & 0 deletions mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package testutil

import (
"os"
"path/filepath"
"testing"

"golang.org/x/tools/go/analysis/analysistest"
)

func TestWithModules(t *testing.T) {
t.Parallel()

t.Run("The line directive is appended to the go source codes", func(t *testing.T) {
testdata := WithModules(t, analysistest.TestData(), nil)
tests := []struct {
path string
want string
}{
{filepath.Join(testdata, "src", "a", "a.go"), "//line src/a/a.go:1"},
{filepath.Join(testdata, "src", "a", "b", "b.go"), "//line src/a/b/b.go:1"},
}
for _, tt := range tests {
b, err := os.ReadFile(tt.path)
if err != nil {
t.Fatal(err)
}
if got := string(b[:len(tt.want)]); got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
}
})
}
4 changes: 4 additions & 0 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package a

func a() {
}
4 changes: 4 additions & 0 deletions testdata/src/a/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package b

func b() {
}
3 changes: 3 additions & 0 deletions testdata/src/a/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module a

go 1.21.0

0 comments on commit 6b80891

Please sign in to comment.