File: main_test.go

package info (click to toggle)
golang-github-rogpeppe-go-internal 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,184 kB
  • sloc: makefile: 6
file content (117 lines) | stat: -rw-r--r-- 2,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"testing"

	"github.com/rogpeppe/go-internal/gotooltest"
	"github.com/rogpeppe/go-internal/internal/os/execpath"
	"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
	os.Exit(testscript.RunMain(m, map[string]func() int{
		"testscript": main1,
	}))
}

func TestScripts(t *testing.T) {
	if _, err := exec.LookPath("go"); err != nil {
		t.Fatalf("need go in PATH for these tests")
	}

	var stderr bytes.Buffer
	cmd := exec.Command("go", "env", "GOMOD")
	cmd.Stderr = &stderr
	out, err := cmd.Output()
	if err != nil {
		t.Fatalf("failed to run %v: %v\n%s", strings.Join(cmd.Args, " "), err, stderr.String())
	}
	gomod := string(out)

	if gomod == "" {
		t.Fatalf("apparently we are not running in module mode?")
	}

	p := testscript.Params{
		Dir: "testdata",
		Setup: func(env *testscript.Env) error {
			env.Vars = append(env.Vars,
				"GOINTERNALMODPATH="+filepath.Dir(gomod),
				"GONOSUMDB=*",
			)
			return nil
		},
		Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
			"dropgofrompath": dropgofrompath,
			"setfilegoproxy": setfilegoproxy,
			"expandone":      expandone,
		},
	}
	if err := gotooltest.Setup(&p); err != nil {
		t.Fatal(err)
	}
	testscript.Run(t, p)
}

func dropgofrompath(ts *testscript.TestScript, neg bool, args []string) {
	if neg {
		ts.Fatalf("unsupported: ! dropgofrompath")
	}
	var newPath []string
	for _, d := range filepath.SplitList(ts.Getenv("PATH")) {
		getenv := func(k string) string {
			// Note that Windows and Plan9 use lowercase "path".
			if strings.ToUpper(k) == "PATH" {
				return d
			}
			return ts.Getenv(k)
		}
		if _, err := execpath.Look("go", getenv); err != nil {
			newPath = append(newPath, d)
		}
	}
	ts.Setenv("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
}

func setfilegoproxy(ts *testscript.TestScript, neg bool, args []string) {
	if neg {
		ts.Fatalf("unsupported: ! setfilegoproxy")
	}
	path := args[0]
	path = filepath.ToSlash(path)
	// probably sufficient to just handle spaces
	path = strings.Replace(path, " ", "%20", -1)
	if runtime.GOOS == "windows" {
		path = "/" + path
	}
	ts.Setenv("GOPROXY", "file://"+path)
}

// expandone takes a single glob-style argument that should expand to
// a single file, otherwise the command fails
func expandone(ts *testscript.TestScript, neg bool, args []string) {
	if len(args) != 1 {
		ts.Fatalf("expandone: expected a single argument")
	}
	if neg {
		ts.Fatalf("unsupported: ! expandone")
	}
	glob := ts.MkAbs(args[0])
	matches, err := filepath.Glob(glob)
	if err != nil {
		ts.Fatalf("expandone: failed to glob %q: %v", glob, err)
	}
	if n := len(matches); n != 1 {
		ts.Fatalf("expandone: %q matched %v files, not 1", glob, n)
	}
}