github.com/rogpeppe/go-internal@v1.12.1-0.20240509064211-c8567cf8e95f/cmd/txtar-addmod/script_test.go (about) 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 package main 6 7 import ( 8 "fmt" 9 "os" 10 "testing" 11 12 "github.com/rogpeppe/go-internal/goproxytest" 13 "github.com/rogpeppe/go-internal/gotooltest" 14 "github.com/rogpeppe/go-internal/testscript" 15 ) 16 17 var proxyURL string 18 19 func TestMain(m *testing.M) { 20 os.Exit(testscript.RunMain(gobinMain{m}, map[string]func() int{ 21 "txtar-addmod": main1, 22 })) 23 } 24 25 type gobinMain struct { 26 m *testing.M 27 } 28 29 func (m gobinMain) Run() int { 30 // Start the Go proxy server running for all tests. 31 srv, err := goproxytest.NewServer("testdata/mod", "") 32 if err != nil { 33 fmt.Fprintf(os.Stderr, "cannot start proxy: %v", err) 34 return 1 35 } 36 proxyURL = srv.URL 37 38 return m.m.Run() 39 } 40 41 func TestScripts(t *testing.T) { 42 p := testscript.Params{ 43 Dir: "testdata", 44 Setup: func(e *testscript.Env) error { 45 e.Vars = append(e.Vars, 46 "GOPROXY="+proxyURL, 47 "GONOSUMDB=*", 48 ) 49 return nil 50 }, 51 } 52 if err := gotooltest.Setup(&p); err != nil { 53 t.Fatal(err) 54 } 55 testscript.Run(t, p) 56 }