Skip to content

Commit 1fffedd

Browse files
author
Jay Conrod
committed
cmd/go: add -testsum flag to update go.sum in script tests
-testsum may be set to "tidy", "listm", or "listall". When set, TestScript runs 'go mod tidy', 'go list -m -mod=mod all', or 'go list -mod=mod all' at the beginning of each test that has a go.mod file in its root directory. If the test passes and go.mod or go.sum was updated, TestScript will rewrite the test file with the initial content of go.mod and go.sum (after the above command). This is useful for writing tests that need a working go.sum and for fixing tests that rely on -mod=mod. For #41302 Change-Id: I63a5667621a5082ccedfc1bff33c3969c29e8b3d Reviewed-on: https://go-review.googlesource.com/c/go/+/336150 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/341932 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 0d01934 commit 1fffedd

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/cmd/go/script_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"bytes"
1212
"context"
1313
"errors"
14+
"flag"
1415
"fmt"
1516
"go/build"
1617
"internal/testenv"
@@ -35,6 +36,8 @@ import (
3536
"cmd/internal/sys"
3637
)
3738

39+
var testSum = flag.String("testsum", "", `may be tidy, listm, or listall. If set, TestScript generates a go.sum file at the beginning of each test and updates test files if they pass.`)
40+
3841
// TestScript runs the tests in testdata/script/*.txt.
3942
func TestScript(t *testing.T) {
4043
testenv.MustHaveGoBuild(t)
@@ -269,6 +272,22 @@ func (ts *testScript) run() {
269272
ts.mark = ts.log.Len()
270273
}
271274

275+
// With -testsum, if a go.mod file is present in the test's initial
276+
// working directory, run 'go mod tidy'.
277+
if *testSum != "" {
278+
if ts.updateSum(a) {
279+
defer func() {
280+
if ts.t.Failed() {
281+
return
282+
}
283+
data := txtar.Format(a)
284+
if err := os.WriteFile(ts.file, data, 0666); err != nil {
285+
ts.t.Errorf("rewriting test file: %v", err)
286+
}
287+
}()
288+
}
289+
}
290+
272291
// Run script.
273292
// See testdata/script/README for documentation of script form.
274293
script := string(a.Comment)
@@ -1341,6 +1360,68 @@ func (ts *testScript) parse(line string) command {
13411360
return cmd
13421361
}
13431362

1363+
// updateSum runs 'go mod tidy', 'go list -mod=mod -m all', or
1364+
// 'go list -mod=mod all' in the test's current directory if a file named
1365+
// "go.mod" is present after the archive has been extracted. updateSum modifies
1366+
// archive and returns true if go.mod or go.sum were changed.
1367+
func (ts *testScript) updateSum(archive *txtar.Archive) (rewrite bool) {
1368+
gomodIdx, gosumIdx := -1, -1
1369+
for i := range archive.Files {
1370+
switch archive.Files[i].Name {
1371+
case "go.mod":
1372+
gomodIdx = i
1373+
case "go.sum":
1374+
gosumIdx = i
1375+
}
1376+
}
1377+
if gomodIdx < 0 {
1378+
return false
1379+
}
1380+
1381+
switch *testSum {
1382+
case "tidy":
1383+
ts.cmdGo(success, []string{"mod", "tidy"})
1384+
case "listm":
1385+
ts.cmdGo(success, []string{"list", "-m", "-mod=mod", "all"})
1386+
case "listall":
1387+
ts.cmdGo(success, []string{"list", "-mod=mod", "all"})
1388+
default:
1389+
ts.t.Fatalf(`unknown value for -testsum %q; may be "tidy", "listm", or "listall"`, *testSum)
1390+
}
1391+
1392+
newGomodData, err := os.ReadFile(filepath.Join(ts.cd, "go.mod"))
1393+
if err != nil {
1394+
ts.t.Fatalf("reading go.mod after -testsum: %v", err)
1395+
}
1396+
if !bytes.Equal(newGomodData, archive.Files[gomodIdx].Data) {
1397+
archive.Files[gomodIdx].Data = newGomodData
1398+
rewrite = true
1399+
}
1400+
1401+
newGosumData, err := os.ReadFile(filepath.Join(ts.cd, "go.sum"))
1402+
if err != nil && !os.IsNotExist(err) {
1403+
ts.t.Fatalf("reading go.sum after -testsum: %v", err)
1404+
}
1405+
switch {
1406+
case os.IsNotExist(err) && gosumIdx >= 0:
1407+
// go.sum was deleted.
1408+
rewrite = true
1409+
archive.Files = append(archive.Files[:gosumIdx], archive.Files[gosumIdx+1:]...)
1410+
case err == nil && gosumIdx < 0:
1411+
// go.sum was created.
1412+
rewrite = true
1413+
gosumIdx = gomodIdx + 1
1414+
archive.Files = append(archive.Files, txtar.File{})
1415+
copy(archive.Files[gosumIdx+1:], archive.Files[gosumIdx:])
1416+
archive.Files[gosumIdx] = txtar.File{Name: "go.sum", Data: newGosumData}
1417+
case err == nil && gosumIdx >= 0 && !bytes.Equal(newGosumData, archive.Files[gosumIdx].Data):
1418+
// go.sum was changed.
1419+
rewrite = true
1420+
archive.Files[gosumIdx].Data = newGosumData
1421+
}
1422+
return rewrite
1423+
}
1424+
13441425
// diff returns a formatted diff of the two texts,
13451426
// showing the entire text and the minimum line-level
13461427
// additions and removals to turn text1 into text2.

0 commit comments

Comments
 (0)