| 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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 10 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 11 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 12 | |
| 13 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 14 | "golang.org/x/tools/go/analysis/analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/" |
| 15 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 16 | "golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/" |
| 17 | "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/" |
| 18 | "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/" |
| 19 | ) |
| 20 | |
| 21 | func TestTestDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/TestTestDir().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 22 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGo1Pointscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGo1Point().(t, 23) |
| 23 | |
| 24 | // Files are initially {go.mod.test,sub.test/sub.go.test}. |
| 25 | fs := osscip-go gomod github.com/golang/go/src go1.25.0 os/.DirFSscip-go gomod github.com/golang/go/src go1.25.0 os/DirFS().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.TestDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/TestData.(), "versions")) |
| 26 | tmpdir := testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/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 := &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 34 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "filever", |
| 35 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "reports file go versions", |
| 36 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: func(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 37 | for _, file := range pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. { |
| 38 | ver := versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.FileVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/FileVersion().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., file) |
| 39 | name := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(pass.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(file.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package.).Filenamescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Filename.) |
| 40 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(file.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package., "%s@%s", name, ver) |
| 41 | } |
| 42 | return nil, nil |
| 43 | }, |
| 44 | } |
| 45 | res := analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/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.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Diagnostics.) |
| 49 | } |
| 50 | |
| 51 | if want := 4; got != want { |
| 52 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Got %d diagnostics. wanted %d", got, want) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestTestDirErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/TestTestDirErrors().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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 := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(input))) |
| 63 | if err != nil { |
| 64 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 65 | } |
| 66 | |
| 67 | directive := "no comma to split on" |
| 68 | intercept := &fatalInterceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/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 | testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/CopyToTmp().(intercept, fs, directive) |
| 76 | }() |
| 77 | |
| 78 | got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(intercept.fatalfsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#fatalfs.) |
| 79 | want := `[rename directive "no comma to split on" does not contain delimiter ","]` |
| 80 | if got != want { |
| 81 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("CopyToTmp(%q) had the Fatal messages %q. wanted %q", directive, got, want) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // helper for TestTestDirErrors |
| 86 | type fatalInterceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept# struct { |
| 87 | testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#TB. |
| 88 | fatalfsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#fatalfs. []string |
| 89 | } |
| 90 | |
| 91 | func (i *fatalInterceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#) Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#Fatalf().(format string, args ...any) { |
| 92 | i.fatalfsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#fatalfs. = append(i.fatalfsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles_test`/fatalIntercept#fatalfs., fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(format, args...)) |
| 93 | // Do not mark the test as failing, but fail early. |
| 94 | panic(i) |
| 95 | } |