Skip to content

Commit

Permalink
go/packages/packagestest: reflect new modules.txt requirements
Browse files Browse the repository at this point in the history
To unblock CL 572200 failed tests, we need to update testIssue37629.

Change-Id: I765f71e873897a06c47162c4a84baddcb3fb0bc0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/581755
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
samthanawalla committed May 7, 2024
1 parent a943a14 commit b426bc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 10 additions & 2 deletions go/packages/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2432,11 +2432,19 @@ func testIssue37629(t *testing.T, exporter packagestest.Exporter) {

exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "golang.org/fake",
Files: map[string]interface{}{
Files: map[string]any{
"c/c2.go": `package c`,
"a/a.go": `package a; import "b.com/b"; const A = b.B`,
"vendor/b.com/b/b.go": `package b; const B = 4`,
}}})
"vendor/modules.txt": `# b.com/b v1.0.0
## explicit
b.com/b`,
}}, {
Name: "b.com/b@v1.0.0",
Files: map[string]any{
"arbitrary.txt": "",
}},
})
rootDir := filepath.Dir(filepath.Dir(exported.File("golang.org/fake", "a/a.go")))
exported.Config.Overlay = map[string][]byte{
filepath.Join(rootDir, "c/c.go"): []byte(`package c; import "golang.org/fake/a"; const C = a.A`),
Expand Down
10 changes: 6 additions & 4 deletions go/packages/packagestest/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package packagestest

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -98,7 +99,8 @@ func (modules) Finalize(exported *Exported) error {
}

exported.written[exported.primary]["go.mod"] = filepath.Join(primaryDir, "go.mod")
primaryGomod := "module " + exported.primary + "\nrequire (\n"
var primaryGomod bytes.Buffer
fmt.Fprintf(&primaryGomod, "module %s\nrequire (\n", exported.primary)
for other := range exported.written {
if other == exported.primary {
continue
Expand All @@ -110,10 +112,10 @@ func (modules) Finalize(exported *Exported) error {
other = v.module
version = v.version
}
primaryGomod += fmt.Sprintf("\t%v %v\n", other, version)
fmt.Fprintf(&primaryGomod, "\t%v %v\n", other, version)
}
primaryGomod += ")\n"
if err := os.WriteFile(filepath.Join(primaryDir, "go.mod"), []byte(primaryGomod), 0644); err != nil {
fmt.Fprintf(&primaryGomod, ")\n")
if err := os.WriteFile(filepath.Join(primaryDir, "go.mod"), primaryGomod.Bytes(), 0644); err != nil {
return err
}

Expand Down

0 comments on commit b426bc7

Please sign in to comment.