Skip to content
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

Prepend line directive to copied source files and add test #10

Merged
merged 7 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference from #9 is that the path is not absolute (does not have / prefix).

{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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go 1.21と書くとgo 1.21rc2が含まれてしまうので、go 1.21.0の方が良いかもです。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tenntenn
ありがとうございます!修正しました!

Loading