| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package testfiles_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | |
| 13 | "golang.org/x/tools/go/analysis" |
| 14 | "golang.org/x/tools/go/analysis/analysistest" |
| 15 | "golang.org/x/tools/internal/testenv" |
| 16 | "golang.org/x/tools/internal/testfiles" |
| 17 | "golang.org/x/tools/internal/versions" |
| 18 | "golang.org/x/tools/txtar" |
| 19 | ) |
| 20 | |
| 21 | func TestTestDir(t *testing.T) { |
| 22 | testenv.NeedsGo1Point(t, 23) |
| 23 | |
| 24 | // Files are initially {go.mod.test,sub.test/sub.go.test}. |
| 25 | fs := os.DirFS(filepath.Join(analysistest.TestData(), "versions")) |
| 26 | tmpdir := testfiles.CopyToTmp(t, fs, |
| 27 | "go.mod.test,go.mod", // After: {go.mod,sub.test/sub.go.test} |
| 28 | "sub.test/sub.go.test,sub.test/abc", // After: {go.mod,sub.test/abc} |
| 29 | "sub.test,sub", // After: {go.mod,sub/abc} |
| 30 | "sub/abc,sub/sub.go", // After: {go.mod,sub/sub.go} |
| 31 | ) |
| 32 | |
| 33 | filever := &analysis.Analyzer{ |
| 34 | Name: "filever", |
| 35 | Doc: "reports file go versions", |
| 36 | Run: func(pass *analysis.Pass) (any, error) { |
| 37 | for _, file := range pass.Files { |
| 38 | ver := versions.FileVersion(pass.TypesInfo, file) |
| 39 | name := filepath.Base(pass.Fset.Position(file.Package).Filename) |
| 40 | pass.Reportf(file.Package, "%s@%s", name, ver) |
| 41 | } |
| 42 | return nil, nil |
| 43 | }, |
| 44 | } |
| 45 | res := analysistest.Run(t, tmpdir, filever, "golang.org/fake/versions", "golang.org/fake/versions/sub") |
| 46 | got := 0 |
| 47 | for _, r := range res { |
| 48 | got += len(r.Diagnostics) |
| 49 | } |
| 50 | |
| 51 | if want := 4; got != want { |
| 52 | t.Errorf("Got %d diagnostics. wanted %d", got, want) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestTestDirErrors(t *testing.T) { |
| 57 | const input = ` |
| 58 | -- one.txt -- |
| 59 | one |
| 60 | ` |
| 61 | // Files are initially {go.mod.test,sub.test/sub.go.test}. |
| 62 | fs, err := txtar.FS(txtar.Parse([]byte(input))) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | directive := "no comma to split on" |
| 68 | intercept := &fatalIntercept{t, nil} |
| 69 | func() { |
| 70 | defer func() { // swallow panics from fatalIntercept.Fatal |
| 71 | if r := recover(); r != intercept { |
| 72 | panic(r) |
| 73 | } |
| 74 | }() |
| 75 | testfiles.CopyToTmp(intercept, fs, directive) |
| 76 | }() |
| 77 | |
| 78 | got := fmt.Sprint(intercept.fatalfs) |
| 79 | want := `[rename directive "no comma to split on" does not contain delimiter ","]` |
| 80 | if got != want { |
| 81 | t.Errorf("CopyToTmp(%q) had the Fatal messages %q. wanted %q", directive, got, want) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // helper for TestTestDirErrors |
| 86 | type fatalIntercept struct { |
| 87 | testing.TB |
| 88 | fatalfs []string |
| 89 | } |
| 90 | |
| 91 | func (i *fatalIntercept) Fatalf(format string, args ...any) { |
| 92 | i.fatalfs = append(i.fatalfs, fmt.Sprintf(format, args...)) |
| 93 | // Do not mark the test as failing, but fail early. |
| 94 | panic(i) |
| 95 | } |