| 1 | // Copyright 2018 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 | /* |
| 6 | Package packagestest creates temporary projects on disk for testing go tools on. |
| 7 | |
| 8 | By changing the exporter used, you can create projects for multiple build |
| 9 | systems from the same description, and run the same tests on them in many |
| 10 | cases. |
| 11 | |
| 12 | # Example |
| 13 | |
| 14 | As an example of packagestest use, consider the following test that runs |
| 15 | the 'go list' command on the specified modules: |
| 16 | |
| 17 | // TestGoList exercises the 'go list' command in module mode and in GOPATH mode. |
| 18 | func TestGoList(t *testing.T) { packagestest.TestAll(t, testGoList) } |
| 19 | func testGoList(t *testing.T, x packagestest.Exporter) { |
| 20 | e := packagestest.Export(t, x, []packagestest.Module{ |
| 21 | { |
| 22 | Name: "gopher.example/repoa", |
| 23 | Files: map[string]interface{}{ |
| 24 | "a/a.go": "package a", |
| 25 | }, |
| 26 | }, |
| 27 | { |
| 28 | Name: "gopher.example/repob", |
| 29 | Files: map[string]interface{}{ |
| 30 | "b/b.go": "package b", |
| 31 | }, |
| 32 | }, |
| 33 | }) |
| 34 | defer e.Cleanup() |
| 35 | |
| 36 | cmd := exec.Command("go", "list", "gopher.example/...") |
| 37 | cmd.Dir = e.Config.Dir |
| 38 | cmd.Env = e.Config.Env |
| 39 | out, err := cmd.Output() |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | t.Logf("'go list gopher.example/...' with %s mode layout:\n%s", x.Name(), out) |
| 44 | } |
| 45 | |
| 46 | TestGoList uses TestAll to exercise the 'go list' command with all |
| 47 | exporters known to packagestest. Currently, packagestest includes |
| 48 | exporters that produce module mode layouts and GOPATH mode layouts. |
| 49 | Running the test with verbose output will print: |
| 50 | |
| 51 | === RUN TestGoList |
| 52 | === RUN TestGoList/GOPATH |
| 53 | === RUN TestGoList/Modules |
| 54 | --- PASS: TestGoList (0.21s) |
| 55 | --- PASS: TestGoList/GOPATH (0.03s) |
| 56 | main_test.go:36: 'go list gopher.example/...' with GOPATH mode layout: |
| 57 | gopher.example/repoa/a |
| 58 | gopher.example/repob/b |
| 59 | --- PASS: TestGoList/Modules (0.18s) |
| 60 | main_test.go:36: 'go list gopher.example/...' with Modules mode layout: |
| 61 | gopher.example/repoa/a |
| 62 | gopher.example/repob/b |
| 63 | */ |
| 64 | package packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/ |
| 65 | |
| 66 | import ( |
| 67 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 68 | "flagscip-go gomod github.com/golang/go/src go1.25.0 flag/" |
| 69 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 70 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 71 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 72 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 73 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 74 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 75 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 76 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 77 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 78 | |
| 79 | "golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/" |
| 80 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 81 | "golang.org/x/tools/internal/expectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/" |
| 82 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 83 | ) |
| 84 | |
| 85 | var ( |
| 86 | skipCleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/skipCleanup. = flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Boolscip-go gomod github.com/golang/go/src go1.25.0 flag/Bool().("skip-cleanup", false, "Do not delete the temporary export folders") // for debugging |
| 87 | ) |
| 88 | |
| 89 | // ErrUnsupported indicates an error due to an operation not supported on the |
| 90 | // current platform. |
| 91 | var ErrUnsupportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/ErrUnsupported. = errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("operation is not supported") |
| 92 | |
| 93 | // Module is a representation of a go module. |
| 94 | type Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module# struct { |
| 95 | // Name is the base name of the module as it would be in the go.mod file. |
| 96 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name. string |
| 97 | // Files is the set of source files for all packages that make up the module. |
| 98 | // The keys are the file fragment that follows the module name, the value can |
| 99 | // be a string or byte slice, in which case it is the contents of the |
| 100 | // file, otherwise it must be a Writer function. |
| 101 | Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files. map[string]any |
| 102 | |
| 103 | // Overlay is the set of source file overlays for the module. |
| 104 | // The keys are the file fragment as in the Files configuration. |
| 105 | // The values are the in memory overlay content for the file. |
| 106 | Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Overlay. map[string][]byte |
| 107 | } |
| 108 | |
| 109 | // A Writer is a function that writes out a test file. |
| 110 | // It is provided the name of the file to write, and may return an error if it |
| 111 | // cannot write the file. |
| 112 | // These are used as the content of the Files map in a Module. |
| 113 | type Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Writer# func(filename string) error |
| 114 | |
| 115 | // Exported is returned by the Export function to report the structure that was produced on disk. |
| 116 | type Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported# struct { |
| 117 | // Config is a correctly configured packages.Config ready to be passed to packages.Load. |
| 118 | // Exactly what it will contain varies depending on the Exporter being used. |
| 119 | Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config. *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# |
| 120 | |
| 121 | // Modules is the module description that was used to produce this exported data set. |
| 122 | Modulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Modules. []Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module# |
| 123 | |
| 124 | ExpectFileSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#ExpectFileSet. *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet# // The file set used when parsing expectations |
| 125 | |
| 126 | Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Exporter. Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter# // the exporter used |
| 127 | tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp. string // the temporary directory that was exported to |
| 128 | primaryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#primary. string // the first non GOROOT module that was exported |
| 129 | writtenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#written. map[string]map[string]string // the full set of exported files |
| 130 | notesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#notes. []*expectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/.Notescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/expect`/Note# // The list of expectations extracted from go source files |
| 131 | markersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#markers. map[string]astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range# // The set of markers extracted from go source files |
| 132 | } |
| 133 | |
| 134 | // Exporter implementations are responsible for converting from the generic description of some |
| 135 | // test data to a driver specific file layout. |
| 136 | type Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter# interface { |
| 137 | // Name reports the name of the exporter, used in logging and sub-test generation. |
| 138 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Name.() string |
| 139 | // Filename reports the system filename for test data source file. |
| 140 | // It is given the base directory, the module the file is part of and the filename fragment to |
| 141 | // work from. |
| 142 | Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Filename.(exported *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#, module, fragment string) string |
| 143 | // Finalize is called once all files have been written to write any extra data needed and modify |
| 144 | // the Config to match. It is handed the full list of modules that were encountered while writing |
| 145 | // files. |
| 146 | Finalizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Finalize.(exported *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#) error |
| 147 | } |
| 148 | |
| 149 | // All is the list of known exporters. |
| 150 | // This is used by TestAll to run tests with all the exporters. |
| 151 | var Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/All. = []Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#{GOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/GOPATH., Modulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Modules.} |
| 152 | |
| 153 | // TestAll invokes the testing function once for each exporter registered in |
| 154 | // the All global. |
| 155 | // Each exporter will be run as a sub-test named after the exporter being used. |
| 156 | func TestAllscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/TestAll().(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#, f func(*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#, Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#)) { |
| 157 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 158 | for _, e := range Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/All. { |
| 159 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(e.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Name.(), func(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#) { |
| 160 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 161 | f(t, e) |
| 162 | }) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // BenchmarkAll invokes the testing function once for each exporter registered in |
| 167 | // the All global. |
| 168 | // Each exporter will be run as a sub-test named after the exporter being used. |
| 169 | func BenchmarkAllscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/BenchmarkAll().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#, f func(*testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#, Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#)) { |
| 170 | b.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 171 | for _, e := range Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/All. { |
| 172 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().(e.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Name.(), func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 173 | b.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 174 | f(b, e) |
| 175 | }) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Export is called to write out a test directory from within a test function. |
| 180 | // It takes the exporter and the build system agnostic module descriptions, and |
| 181 | // uses them to build a temporary directory. |
| 182 | // It returns an Exported with the results of the export. |
| 183 | // The Exported.Config is prepared for loading from the exported data. |
| 184 | // You must invoke Exported.Cleanup on the returned value to clean up. |
| 185 | // The file deletion in the cleanup can be skipped by setting the skip-cleanup |
| 186 | // flag when invoking the test, allowing the temporary directory to be left for |
| 187 | // debugging tests. |
| 188 | // |
| 189 | // If the Writer for any file within any module returns an error equivalent to |
| 190 | // ErrUnspported, Export skips the test. |
| 191 | func Exportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Export().(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#, exporter Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#, modules []Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#) *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported# { |
| 192 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Helper().() |
| 193 | if exporter == Modulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Modules. { |
| 194 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go") |
| 195 | } |
| 196 | |
| 197 | dirname := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(t.Namescip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Name().(), "/", "_", -1) |
| 198 | dirname = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(dirname, "#", "_", -1) // duplicate subtests get a #NNN suffix. |
| 199 | temp, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirTemp().("", dirname) |
| 200 | if err != nil { |
| 201 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 202 | } |
| 203 | exported := &Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#{ |
| 204 | Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config.: &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#{ |
| 205 | Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.: temp, |
| 206 | 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().(), "GOPACKAGESDRIVER=off", "GOROOT="), // Clear GOROOT to work around #32849. |
| 207 | Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Overlay.: make(map[string][]byte), |
| 208 | Testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Tests.: true, |
| 209 | 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`/.LoadImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/LoadImports., |
| 210 | }, |
| 211 | Modulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Modules.: modules, |
| 212 | Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Exporter.: exporter, |
| 213 | tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp.: temp, |
| 214 | primaryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#primary.: modules[0].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name., |
| 215 | writtenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#written.: map[string]map[string]string{}, |
| 216 | ExpectFileSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#ExpectFileSet.: tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().(), |
| 217 | } |
| 218 | if testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Verbosescip-go gomod github.com/golang/go/src go1.25.0 testing/Verbose().() { |
| 219 | exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Logf. = t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Logf(). |
| 220 | } |
| 221 | defer func() { |
| 222 | if t.Failedscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Failed().() || t.Skippedscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Skipped().() { |
| 223 | exported.Cleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Cleanup().() |
| 224 | } |
| 225 | }() |
| 226 | for _, module := range modules { |
| 227 | // Create all parent directories before individual files. If any file is a |
| 228 | // symlink to a directory, that directory must exist before the symlink is |
| 229 | // created or else it may be created with the wrong type on Windows. |
| 230 | // (See https://golang.org/issue/39183.) |
| 231 | for fragment := range module.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files. { |
| 232 | fullpath := exporter.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Filename.(exported, module.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name., filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/FromSlash().(fragment)) |
| 233 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(fullpath), 0755); err != nil { |
| 234 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | for fragment, value := range module.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files. { |
| 239 | fullpath := exporter.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Filename.(exported, module.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name., filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/FromSlash().(fragment)) |
| 240 | written, ok := exported.writtenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#written.[module.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name.] |
| 241 | if !ok { |
| 242 | written = map[string]string{} |
| 243 | exported.writtenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#written.[module.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name.] = written |
| 244 | } |
| 245 | written[fragment] = fullpath |
| 246 | switch value := value.(type) { |
| 247 | case Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Writer#: |
| 248 | if err := value(fullpath); err != nil { |
| 249 | if errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, ErrUnsupportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/ErrUnsupported.) { |
| 250 | t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Skip().(err) |
| 251 | } |
| 252 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 253 | } |
| 254 | case string: |
| 255 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(fullpath, []byte(value), 0644); err != nil { |
| 256 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 257 | } |
| 258 | default: |
| 259 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatalf().("Invalid type %T in files, must be string or Writer", value) |
| 260 | } |
| 261 | } |
| 262 | for fragment, value := range module.Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Overlay. { |
| 263 | fullpath := exporter.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Filename.(exported, module.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name., filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/FromSlash().(fragment)) |
| 264 | exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Overlay.[fullpath] = value |
| 265 | } |
| 266 | } |
| 267 | if err := exporter.Finalizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exporter#Finalize.(exported); err != nil { |
| 268 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatal().(err) |
| 269 | } |
| 270 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackagesEnv().(t, exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.) |
| 271 | return exported |
| 272 | } |
| 273 | |
| 274 | // Script returns a Writer that writes out contents to the file and sets the |
| 275 | // executable bit on the created file. |
| 276 | // It is intended for source files that are shell scripts. |
| 277 | func Scriptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Script().(contents string) Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Writer# { |
| 278 | return func(filename string) error { |
| 279 | return osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filename, []byte(contents), 0755) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Link returns a Writer that creates a hard link from the specified source to |
| 284 | // the required file. |
| 285 | // This is used to link testdata files into the generated testing tree. |
| 286 | // |
| 287 | // If hard links to source are not supported on the destination filesystem, the |
| 288 | // returned Writer returns an error for which errors.Is(_, ErrUnsupported) |
| 289 | // returns true. |
| 290 | func Linkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Link().(source string) Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Writer# { |
| 291 | return func(filename string) error { |
| 292 | linkErr := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Linkscip-go gomod github.com/golang/go/src go1.25.0 os/Link().(source, filename) |
| 293 | |
| 294 | if linkErr != nil && !builderMustSupportLinksscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/builderMustSupportLinks().() { |
| 295 | // Probe to figure out whether Link failed because the Link operation |
| 296 | // isn't supported. |
| 297 | if stat, err := openAndStatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/openAndStat().(source); err == nil { |
| 298 | if err := createEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/createEmpty().(filename, stat.Modescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#Mode().()); err == nil { |
| 299 | // Successfully opened the source and created the destination, |
| 300 | // but the result is empty and not a hard-link. |
| 301 | return &osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathErrorscip-go gomod github.com/golang/go/src go1.25.0 os/PathError#{Opscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Op.: "Link", Pathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Path.: filename, Errscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Err.: ErrUnsupportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/ErrUnsupported.} |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return linkErr |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Symlink returns a Writer that creates a symlink from the specified source to the |
| 311 | // required file. |
| 312 | // This is used to link testdata files into the generated testing tree. |
| 313 | // |
| 314 | // If symlinks to source are not supported on the destination filesystem, the |
| 315 | // returned Writer returns an error for which errors.Is(_, ErrUnsupported) |
| 316 | // returns true. |
| 317 | func Symlinkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Symlink().(source string) Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Writer# { |
| 318 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(source, ".") { |
| 319 | if absSource, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(source); err == nil { |
| 320 | if _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(source); !osscip-go gomod github.com/golang/go/src go1.25.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/IsNotExist().(err) { |
| 321 | source = absSource |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | return func(filename string) error { |
| 326 | symlinkErr := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Symlinkscip-go gomod github.com/golang/go/src go1.25.0 os/Symlink().(source, filename) |
| 327 | |
| 328 | if symlinkErr != nil && !builderMustSupportLinksscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/builderMustSupportLinks().() { |
| 329 | // Probe to figure out whether Symlink failed because the Symlink |
| 330 | // operation isn't supported. |
| 331 | fullSource := source |
| 332 | if !filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.IsAbsscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/IsAbs().(source) { |
| 333 | // Compute the target path relative to the parent of filename, not the |
| 334 | // current working directory. |
| 335 | fullSource = 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().(filename, "..", source) |
| 336 | } |
| 337 | stat, err := openAndStatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/openAndStat().(fullSource) |
| 338 | mode := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.0 os/ModePerm. |
| 339 | if err == nil { |
| 340 | mode = stat.Modescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#Mode().() |
| 341 | } else if !errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, osscip-go gomod github.com/golang/go/src go1.25.0 os/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/ErrNotExist.) { |
| 342 | // We couldn't open the source, but it might exist. We don't expect to be |
| 343 | // able to portably create a symlink to a file we can't see. |
| 344 | return symlinkErr |
| 345 | } |
| 346 | |
| 347 | if err := createEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/createEmpty().(filename, mode|0644); err == nil { |
| 348 | // Successfully opened the source (or verified that it does not exist) and |
| 349 | // created the destination, but we couldn't create it as a symlink. |
| 350 | // Probably the OS just doesn't support symlinks in this context. |
| 351 | return &osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathErrorscip-go gomod github.com/golang/go/src go1.25.0 os/PathError#{Opscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Op.: "Symlink", Pathscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Path.: filename, Errscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/PathError#Err.: ErrUnsupportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/ErrUnsupported.} |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return symlinkErr |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // builderMustSupportLinks reports whether we are running on a Go builder |
| 360 | // that is known to support hard and symbolic links. |
| 361 | func builderMustSupportLinksscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/builderMustSupportLinks().() bool { |
| 362 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GO_BUILDER_NAME") == "" { |
| 363 | // Any OS can be configured to mount an exotic filesystem. |
| 364 | // Don't make assumptions about what users are running. |
| 365 | return false |
| 366 | } |
| 367 | |
| 368 | switch runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. { |
| 369 | case "windows", "plan9": |
| 370 | // Some versions of Windows and all versions of plan9 do not support |
| 371 | // symlinks by default. |
| 372 | return false |
| 373 | |
| 374 | default: |
| 375 | // All other platforms should support symlinks by default, and our builders |
| 376 | // should not do anything unusual that would violate that. |
| 377 | return true |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // openAndStat attempts to open source for reading. |
| 382 | func openAndStatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/openAndStat().(source string) (osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 os/FileInfo#, error) { |
| 383 | src, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(source) |
| 384 | if err != nil { |
| 385 | return nil, err |
| 386 | } |
| 387 | stat, err := src.Statscip-go gomod github.com/golang/go/src go1.25.0 os/File#Stat().() |
| 388 | src.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() |
| 389 | if err != nil { |
| 390 | return nil, err |
| 391 | } |
| 392 | return stat, nil |
| 393 | } |
| 394 | |
| 395 | // createEmpty creates an empty file or directory (depending on mode) |
| 396 | // at dst, with the same permissions as mode. |
| 397 | func createEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/createEmpty().(dst string, mode osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileModescip-go gomod github.com/golang/go/src go1.25.0 os/FileMode#) error { |
| 398 | if mode.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#IsDir().() { |
| 399 | return osscip-go gomod github.com/golang/go/src go1.25.0 os/.Mkdirscip-go gomod github.com/golang/go/src go1.25.0 os/Mkdir().(dst, mode.Permscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#Perm().()) |
| 400 | } |
| 401 | |
| 402 | f, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 os/OpenFile().(dst, osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.25.0 os/O_WRONLY.|osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.25.0 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_EXCLscip-go gomod github.com/golang/go/src go1.25.0 os/O_EXCL., mode.Permscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#Perm().()) |
| 403 | if err != nil { |
| 404 | return err |
| 405 | } |
| 406 | if err := f.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().(); err != nil { |
| 407 | osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(dst) // best-effort |
| 408 | return err |
| 409 | } |
| 410 | |
| 411 | return nil |
| 412 | } |
| 413 | |
| 414 | // Copy returns a Writer that copies a file from the specified source to the |
| 415 | // required file. |
| 416 | // This is used to copy testdata files into the generated testing tree. |
| 417 | func Copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Copy().(source string) Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Writer# { |
| 418 | return func(filename string) error { |
| 419 | stat, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(source) |
| 420 | if err != nil { |
| 421 | return err |
| 422 | } |
| 423 | if !stat.Modescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#Mode().().IsRegularscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#IsRegular().() { |
| 424 | // cannot copy non-regular files (e.g., directories, |
| 425 | // symlinks, devices, etc.) |
| 426 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cannot copy non regular file %s", source) |
| 427 | } |
| 428 | return copyFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/copyFile().(filename, source, stat.Modescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#Mode().().Permscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#Perm().()) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | func copyFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/copyFile().(dest, source string, perm osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileModescip-go gomod github.com/golang/go/src go1.25.0 os/FileMode#) error { |
| 433 | src, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(source) |
| 434 | if err != nil { |
| 435 | return err |
| 436 | } |
| 437 | defer src.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() |
| 438 | |
| 439 | dst, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 os/OpenFile().(dest, osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.25.0 os/O_WRONLY.|osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.25.0 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_EXCLscip-go gomod github.com/golang/go/src go1.25.0 os/O_EXCL., perm) |
| 440 | if err != nil { |
| 441 | return err |
| 442 | } |
| 443 | |
| 444 | _, err = ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Copyscip-go gomod github.com/golang/go/src go1.25.0 io/Copy().(dst, src) |
| 445 | if closeErr := dst.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().(); err == nil { |
| 446 | err = closeErr |
| 447 | } |
| 448 | return err |
| 449 | } |
| 450 | |
| 451 | // GroupFilesByModules attempts to map directories to the modules within each directory. |
| 452 | // This function assumes that the folder is structured in the following way: |
| 453 | // |
| 454 | // dir/ |
| 455 | // primarymod/ |
| 456 | // *.go files |
| 457 | // packages |
| 458 | // go.mod (optional) |
| 459 | // modules/ |
| 460 | // repoa/ |
| 461 | // mod1/ |
| 462 | // *.go files |
| 463 | // packages |
| 464 | // go.mod (optional) |
| 465 | // |
| 466 | // It scans the directory tree anchored at root and adds a Copy writer to the |
| 467 | // map for every file found. |
| 468 | // This is to enable the common case in tests where you have a full copy of the |
| 469 | // package in your testdata. |
| 470 | func GroupFilesByModulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/GroupFilesByModules().(root string) ([]Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#, error) { |
| 471 | root = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/FromSlash().(root) |
| 472 | primarymodPath := 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().(root, "primarymod") |
| 473 | |
| 474 | _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(primarymodPath) |
| 475 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/IsNotExist().(err) { |
| 476 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("could not find primarymod folder within %s", root) |
| 477 | } |
| 478 | |
| 479 | primarymod := &Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#{ |
| 480 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name.: root, |
| 481 | Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files.: make(map[string]any), |
| 482 | Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Overlay.: make(map[string][]byte), |
| 483 | } |
| 484 | mods := map[string]*Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#{ |
| 485 | root: primarymod, |
| 486 | } |
| 487 | modules := []Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#{*primarymod} |
| 488 | |
| 489 | if err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Walk().(primarymodPath, func(path string, info osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 os/FileInfo#, err error) error { |
| 490 | if err != nil { |
| 491 | return err |
| 492 | } |
| 493 | if info.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 494 | return nil |
| 495 | } |
| 496 | fragment, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(primarymodPath, path) |
| 497 | if err != nil { |
| 498 | return err |
| 499 | } |
| 500 | primarymod.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files.[filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(fragment)] = Copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Copy().(path) |
| 501 | return nil |
| 502 | }); err != nil { |
| 503 | return nil, err |
| 504 | } |
| 505 | |
| 506 | modulesPath := 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().(root, "modules") |
| 507 | if _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(modulesPath); osscip-go gomod github.com/golang/go/src go1.25.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/IsNotExist().(err) { |
| 508 | return modules, nil |
| 509 | } |
| 510 | |
| 511 | var currentRepo, currentModule string |
| 512 | updateCurrentModule := func(dir string) { |
| 513 | if dir == currentModule { |
| 514 | return |
| 515 | } |
| 516 | // Handle the case where we step into a nested directory that is a module |
| 517 | // and then step out into the parent which is also a module. |
| 518 | // Example: |
| 519 | // - repoa |
| 520 | // - moda |
| 521 | // - go.mod |
| 522 | // - v2 |
| 523 | // - go.mod |
| 524 | // - what.go |
| 525 | // - modb |
| 526 | for dir != root { |
| 527 | if mods[dir] != nil { |
| 528 | currentModule = dir |
| 529 | return |
| 530 | } |
| 531 | dir = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(dir) |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Walk().(modulesPath, func(path string, info osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 os/FileInfo#, err error) error { |
| 536 | if err != nil { |
| 537 | return err |
| 538 | } |
| 539 | enclosingDir := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(path) |
| 540 | // If the path is not a directory, then we want to add the path to |
| 541 | // the files map of the currentModule. |
| 542 | if !info.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 543 | updateCurrentModule(enclosingDir) |
| 544 | fragment, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(currentModule, path) |
| 545 | if err != nil { |
| 546 | return err |
| 547 | } |
| 548 | mods[currentModule].Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files.[filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(fragment)] = Copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Copy().(path) |
| 549 | return nil |
| 550 | } |
| 551 | // If the path is a directory and it's enclosing folder is equal to |
| 552 | // the modules folder, then the path is a new repo. |
| 553 | if enclosingDir == modulesPath { |
| 554 | currentRepo = path |
| 555 | return nil |
| 556 | } |
| 557 | // If the path is a directory and it's enclosing folder is not the same |
| 558 | // as the current repo and it is not of the form `v1`,`v2`,... |
| 559 | // then the path is a folder/package of the current module. |
| 560 | if enclosingDir != currentRepo && !versionSuffixREscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/versionSuffixRE..MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(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().(path)) { |
| 561 | return nil |
| 562 | } |
| 563 | // If the path is a directory and it's enclosing folder is the current repo |
| 564 | // then the path is a new module. |
| 565 | module, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(modulesPath, path) |
| 566 | if err != nil { |
| 567 | return err |
| 568 | } |
| 569 | mods[path] = &Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#{ |
| 570 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name.: filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(module), |
| 571 | Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files.: make(map[string]any), |
| 572 | Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Overlay.: make(map[string][]byte), |
| 573 | } |
| 574 | currentModule = path |
| 575 | modules = append(modules, *mods[path]) |
| 576 | return nil |
| 577 | }); err != nil { |
| 578 | return nil, err |
| 579 | } |
| 580 | return modules, nil |
| 581 | } |
| 582 | |
| 583 | // MustCopyFileTree returns a file set for a module based on a real directory tree. |
| 584 | // It scans the directory tree anchored at root and adds a Copy writer to the |
| 585 | // map for every file found. It skips copying files in nested modules. |
| 586 | // This is to enable the common case in tests where you have a full copy of the |
| 587 | // package in your testdata. |
| 588 | // This will panic if there is any kind of error trying to walk the file tree. |
| 589 | func MustCopyFileTreescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/MustCopyFileTree().(root string) map[string]any { |
| 590 | result := map[string]any{} |
| 591 | if err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Walk().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/FromSlash().(root), func(path string, info osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 os/FileInfo#, err error) error { |
| 592 | if err != nil { |
| 593 | return err |
| 594 | } |
| 595 | if info.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 596 | // skip nested modules. |
| 597 | if path != root { |
| 598 | if fi, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(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().(path, "go.mod")); err == nil && !fi.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 599 | return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/SkipDir. |
| 600 | } |
| 601 | } |
| 602 | return nil |
| 603 | } |
| 604 | fragment, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(root, path) |
| 605 | if err != nil { |
| 606 | return err |
| 607 | } |
| 608 | result[filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(fragment)] = Copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Copy().(path) |
| 609 | return nil |
| 610 | }); err != nil { |
| 611 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Panicscip-go gomod github.com/golang/go/src go1.25.0 log/Panic().(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().("MustCopyFileTree failed: %v", err)) |
| 612 | } |
| 613 | return result |
| 614 | } |
| 615 | |
| 616 | // Cleanup removes the temporary directory (unless the --skip-cleanup flag was set) |
| 617 | // It is safe to call cleanup multiple times. |
| 618 | func (e *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#) Cleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Cleanup().() { |
| 619 | if e.tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp. == "" { |
| 620 | return |
| 621 | } |
| 622 | if *skipCleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/skipCleanup. { |
| 623 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Skipping cleanup of temp dir: %s", e.tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp.) |
| 624 | return |
| 625 | } |
| 626 | // Make everything read-write so that the Module exporter's module cache can be deleted. |
| 627 | filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Walk().(e.tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp., func(path string, info osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 os/FileInfo#, err error) error { |
| 628 | if err != nil { |
| 629 | return nil |
| 630 | } |
| 631 | if info.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 632 | osscip-go gomod github.com/golang/go/src go1.25.0 os/.Chmodscip-go gomod github.com/golang/go/src go1.25.0 os/Chmod().(path, 0777) |
| 633 | } |
| 634 | return nil |
| 635 | }) |
| 636 | osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(e.tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp.) // ignore errors |
| 637 | e.tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp. = "" |
| 638 | } |
| 639 | |
| 640 | // Temp returns the temporary directory that was generated. |
| 641 | func (e *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#) Tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Temp().() string { |
| 642 | return e.tempscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#temp. |
| 643 | } |
| 644 | |
| 645 | // File returns the full path for the given module and file fragment. |
| 646 | func (e *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#) Filescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#File().(module, fragment string) string { |
| 647 | if m := e.writtenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#written.[module]; m != nil { |
| 648 | return m[fragment] |
| 649 | } |
| 650 | return "" |
| 651 | } |
| 652 | |
| 653 | // FileContents returns the contents of the specified file. |
| 654 | // It will use the overlay if the file is present, otherwise it will read it |
| 655 | // from disk. |
| 656 | func (e *Exportedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#) FileContentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#FileContents().(filename string) ([]byte, error) { |
| 657 | if content, found := e.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Overlay.[filename]; found { |
| 658 | return content, nil |
| 659 | } |
| 660 | content, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filename) |
| 661 | if err != nil { |
| 662 | return nil, err |
| 663 | } |
| 664 | return content, nil |
| 665 | } |