| 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 provides utilities for writing Go tests with files |
| 6 | // in testdata. |
| 7 | package testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/ |
| 8 | |
| 9 | import ( |
| 10 | "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/" |
| 11 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 12 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 15 | |
| 16 | "golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/" |
| 17 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 18 | "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/" |
| 19 | ) |
| 20 | |
| 21 | // CopyToTmp copies the files and directories in src to a new temporary testing |
| 22 | // directory dst, and returns dst on success. |
| 23 | // |
| 24 | // After copying the files, it processes each of the 'old,new,' rename |
| 25 | // directives in order. Each rename directive moves the relative path "old" |
| 26 | // to the relative path "new" within the directory. |
| 27 | // |
| 28 | // Renaming allows tests to hide files whose names have |
| 29 | // special meaning, such as "go.mod" files or "testdata" directories |
| 30 | // from the go command, or ill-formed Go source files from gofmt. |
| 31 | // |
| 32 | // For example if we copy the directory testdata: |
| 33 | // |
| 34 | // testdata/ |
| 35 | // go.mod.test |
| 36 | // a/a.go |
| 37 | // b/b.go |
| 38 | // |
| 39 | // with the rename "go.mod.test,go.mod", the resulting files will be: |
| 40 | // |
| 41 | // dst/ |
| 42 | // go.mod |
| 43 | // a/a.go |
| 44 | // b/b.go |
| 45 | func CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/CopyToTmp().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, src fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FS#, rename ...string) string { |
| 46 | dstdir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#TempDir().() |
| 47 | |
| 48 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.CopyFSscip-go gomod github.com/golang/go/src go1.25.0 os/CopyFS().(dstdir, src); err != nil { |
| 49 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 50 | } |
| 51 | for _, r := range rename { |
| 52 | old, new, found := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(r, ",") |
| 53 | if !found { |
| 54 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatalf().("rename directive %q does not contain delimiter %q", r, ",") |
| 55 | } |
| 56 | oldpath := 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().(dstdir, old) |
| 57 | newpath := 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().(dstdir, new) |
| 58 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Renamescip-go gomod github.com/golang/go/src go1.25.0 os/Rename().(oldpath, newpath); err != nil { |
| 59 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return dstdir |
| 64 | } |
| 65 | |
| 66 | // ExtractTxtarFileToTmp read a txtar archive on a given path, |
| 67 | // extracts it to a temporary directory, and returns the |
| 68 | // temporary directory. |
| 69 | func ExtractTxtarFileToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/ExtractTxtarFileToTmp().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, file string) string { |
| 70 | ar, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.ParseFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/ParseFile().(file) |
| 71 | if err != nil { |
| 72 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 73 | } |
| 74 | |
| 75 | 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().(ar) |
| 76 | if err != nil { |
| 77 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 78 | } |
| 79 | return CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/CopyToTmp().(t, fs) |
| 80 | } |
| 81 | |
| 82 | // LoadPackages loads typed syntax for all packages that match the |
| 83 | // patterns, interpreted relative to the archive root. |
| 84 | // |
| 85 | // The packages must be error-free. |
| 86 | func LoadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/LoadPackages().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, ar *txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#, patterns ...string) []*packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package# { |
| 87 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackages().(t) |
| 88 | |
| 89 | 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().(ar) |
| 90 | if err != nil { |
| 91 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 92 | } |
| 93 | dir := CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/CopyToTmp().(t, fs) |
| 94 | |
| 95 | cfg := &packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#{ |
| 96 | Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Mode.: packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedSyntax. | |
| 97 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedTypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedTypesInfo. | |
| 98 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedDepsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedDeps. | |
| 99 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedName. | |
| 100 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedFiles. | |
| 101 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedImports. | |
| 102 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedCompiledGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedCompiledGoFiles. | |
| 103 | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedTypes., |
| 104 | Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.: dir, |
| 105 | Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.: append(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Environscip-go gomod github.com/golang/go/src go1.25.0 os/Environ().(), |
| 106 | "GO111MODULES=on", |
| 107 | "GOPATH=", |
| 108 | "GOWORK=off", |
| 109 | "GOPROXY=off"), |
| 110 | } |
| 111 | pkgs, err := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Load().(cfg, patterns...) |
| 112 | if err != nil { |
| 113 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 114 | } |
| 115 | if num := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.PrintErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/PrintErrors().(pkgs); num > 0 { |
| 116 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatalf().("packages contained %d errors", num) |
| 117 | } |
| 118 | return pkgs |
| 119 | } |