| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"os/exec" |
| 10 |
"path/filepath" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/rogpeppe/go-internal/gotooltest" |
| 16 |
"github.com/rogpeppe/go-internal/internal/os/execpath" |
| 17 |
"github.com/rogpeppe/go-internal/testscript" |
| 18 |
) |
| 19 |
|
| 20 |
func TestMain(m *testing.M) { |
| 21 |
testscript.Main(m, map[string]func(){ |
| 22 |
"testscript": main, |
| 23 |
}) |
| 24 |
} |
| 25 |
|
| 26 |
func TestScripts(t *testing.T) { |
| 27 |
if _, err := exec.LookPath("go"); err != nil { |
| 28 |
t.Fatalf("need go in PATH for these tests") |
| 29 |
} |
| 30 |
|
| 31 |
var stderr bytes.Buffer |
| 32 |
cmd := exec.Command("go", "env", "GOMOD") |
| 33 |
cmd.Stderr = &stderr |
| 34 |
out, err := cmd.Output() |
| 35 |
if err != nil { |
| 36 |
t.Fatalf("failed to run %v: %v\n%s", strings.Join(cmd.Args, " "), err, stderr.String()) |
| 37 |
} |
| 38 |
gomod := string(out) |
| 39 |
|
| 40 |
if gomod == "" { |
| 41 |
t.Fatalf("apparently we are not running in module mode?") |
| 42 |
} |
| 43 |
|
| 44 |
p := testscript.Params{ |
| 45 |
Dir: "testdata", |
| 46 |
Setup: func(env *testscript.Env) error { |
| 47 |
env.Vars = append(env.Vars, |
| 48 |
"GOINTERNALMODPATH="+filepath.Dir(gomod), |
| 49 |
"GONOSUMDB=*", |
| 50 |
) |
| 51 |
return nil |
| 52 |
}, |
| 53 |
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ |
| 54 |
"dropgofrompath": dropgofrompath, |
| 55 |
"setfilegoproxy": setfilegoproxy, |
| 56 |
"expandone": expandone, |
| 57 |
}, |
| 58 |
} |
| 59 |
if err := gotooltest.Setup(&p); err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
testscript.Run(t, p) |
| 63 |
} |
| 64 |
|
| 65 |
func dropgofrompath(ts *testscript.TestScript, neg bool, args []string) { |
| 66 |
if neg { |
| 67 |
ts.Fatalf("unsupported: ! dropgofrompath") |
| 68 |
} |
| 69 |
var newPath []string |
| 70 |
for _, d := range filepath.SplitList(ts.Getenv("PATH")) { |
| 71 |
getenv := func(k string) string { |
| 72 |
|
| 73 |
if strings.ToUpper(k) == "PATH" { |
| 74 |
return d |
| 75 |
} |
| 76 |
return ts.Getenv(k) |
| 77 |
} |
| 78 |
if _, err := execpath.Look("go", getenv); err != nil { |
| 79 |
newPath = append(newPath, d) |
| 80 |
} |
| 81 |
} |
| 82 |
ts.Setenv("PATH", strings.Join(newPath, string(filepath.ListSeparator))) |
| 83 |
} |
| 84 |
|
| 85 |
func setfilegoproxy(ts *testscript.TestScript, neg bool, args []string) { |
| 86 |
if neg { |
| 87 |
ts.Fatalf("unsupported: ! setfilegoproxy") |
| 88 |
} |
| 89 |
path := args[0] |
| 90 |
path = filepath.ToSlash(path) |
| 91 |
|
| 92 |
path = strings.Replace(path, " ", "%20", -1) |
| 93 |
if runtime.GOOS == "windows" { |
| 94 |
path = "/" + path |
| 95 |
} |
| 96 |
ts.Setenv("GOPROXY", "file://"+path) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
func expandone(ts *testscript.TestScript, neg bool, args []string) { |
| 102 |
if len(args) != 1 { |
| 103 |
ts.Fatalf("expandone: expected a single argument") |
| 104 |
} |
| 105 |
if neg { |
| 106 |
ts.Fatalf("unsupported: ! expandone") |
| 107 |
} |
| 108 |
glob := ts.MkAbs(args[0]) |
| 109 |
matches, err := filepath.Glob(glob) |
| 110 |
if err != nil { |
| 111 |
ts.Fatalf("expandone: failed to glob %q: %v", glob, err) |
| 112 |
} |
| 113 |
if n := len(matches); n != 1 { |
| 114 |
ts.Fatalf("expandone: %q matched %v files, not 1", glob, n) |
| 115 |
} |
| 116 |
} |