| 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 testfiles |
| 8 |
|
| 9 |
import ( |
| 10 |
"io/fs" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"golang.org/x/tools/go/packages" |
| 17 |
"golang.org/x/tools/txtar" |
| 18 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 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 CopyToTmp(t testing.TB, src fs.FS, rename ...string) string { |
| 46 |
dstdir := t.TempDir() |
| 47 |
|
| 48 |
if err := os.CopyFS(dstdir, src); err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
for _, r := range rename { |
| 52 |
old, new, found := strings.Cut(r, ",") |
| 53 |
if !found { |
| 54 |
t.Fatalf("rename directive %q does not contain delimiter %q", r, ",") |
| 55 |
} |
| 56 |
oldpath := filepath.Join(dstdir, old) |
| 57 |
newpath := filepath.Join(dstdir, new) |
| 58 |
if err := os.Rename(oldpath, newpath); err != nil { |
| 59 |
t.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 ExtractTxtarFileToTmp(t testing.TB, file string) string { |
| 70 |
ar, err := txtar.ParseFile(file) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
fs, err := txtar.FS(ar) |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
return 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 LoadPackages(t testing.TB, ar *txtar.Archive, patterns ...string) []*packages.Package { |
| 87 |
testenv.NeedsGoPackages(t) |
| 88 |
|
| 89 |
fs, err := txtar.FS(ar) |
| 90 |
if err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
dir := CopyToTmp(t, fs) |
| 94 |
|
| 95 |
cfg := &packages.Config{ |
| 96 |
Mode: packages.NeedSyntax | |
| 97 |
packages.NeedTypesInfo | |
| 98 |
packages.NeedDeps | |
| 99 |
packages.NeedName | |
| 100 |
packages.NeedFiles | |
| 101 |
packages.NeedImports | |
| 102 |
packages.NeedCompiledGoFiles | |
| 103 |
packages.NeedTypes, |
| 104 |
Dir: dir, |
| 105 |
Env: append(os.Environ(), |
| 106 |
"GO111MODULES=on", |
| 107 |
"GOPATH=", |
| 108 |
"GOWORK=off", |
| 109 |
"GOPROXY=off"), |
| 110 |
} |
| 111 |
pkgs, err := packages.Load(cfg, patterns...) |
| 112 |
if err != nil { |
| 113 |
t.Fatal(err) |
| 114 |
} |
| 115 |
if num := packages.PrintErrors(pkgs); num > 0 { |
| 116 |
t.Fatalf("packages contained %d errors", num) |
| 117 |
} |
| 118 |
return pkgs |
| 119 |
} |