1 // Copyright 2017 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.
25 exeSuffix string // ".exe" on Windows
29 if runtime.GOOS == "windows" {
34 // TestMain creates a getgo command for testing purposes and
35 // deletes it after the tests have been run.
36 func TestMain(m *testing.M) {
37 if os.Getenv("GOGET_INTEGRATION") == "" {
38 fmt.Fprintln(os.Stderr, "main_test: Skipping integration tests with GOGET_INTEGRATION unset")
42 args := []string{"build", "-tags", testbin, "-o", testbin + exeSuffix}
43 out, err := exec.Command("go", args...).CombinedOutput()
45 fmt.Fprintf(os.Stderr, "building %s failed: %v\n%s", testbin, err, out)
49 // Don't let these environment variables confuse the test.
52 os.Unsetenv("GIT_ALLOW_PROTOCOL")
57 os.Remove(testbin + exeSuffix)
62 func createTmpHome(t *testing.T) string {
63 tmpd, err := ioutil.TempDir("", "testgetgo")
65 t.Fatalf("creating test tempdir failed: %v", err)
68 os.Setenv("HOME", tmpd)
72 // doRun runs the test getgo command, recording stdout and stderr and
73 // returning exit status.
74 func doRun(t *testing.T, args ...string) error {
75 var stdout, stderr bytes.Buffer
76 t.Logf("running %s %v", testbin, args)
77 cmd := exec.Command("./"+testbin+exeSuffix, args...)
80 cmd.Env = os.Environ()
83 t.Log("standard output:")
84 t.Log(stdout.String())
87 t.Log("standard error:")
88 t.Log(stderr.String())
93 func TestCommandVerbose(t *testing.T) {
94 tmpd := createTmpHome(t)
95 defer os.RemoveAll(tmpd)
101 // make sure things are in path
102 shellConfig, err := shellConfigFile()
106 b, err := ioutil.ReadFile(shellConfig)
110 home, err := getHomeDir()
115 expected := fmt.Sprintf(`
116 export PATH=$PATH:%s/.go/bin
120 export PATH=$PATH:%s/go/bin
123 if string(b) != expected {
124 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
128 func TestCommandPathExists(t *testing.T) {
129 tmpd := createTmpHome(t)
130 defer os.RemoveAll(tmpd)
133 err := doRun(t, "-skip-dl")
137 // make sure things are in path
138 shellConfig, err := shellConfigFile()
142 b, err := ioutil.ReadFile(shellConfig)
146 home, err := getHomeDir()
151 expected := fmt.Sprintf(`
154 export PATH=$PATH:%s/go/bin
157 if string(b) != expected {
158 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
162 if err := doRun(t, "-skip-dl"); err != nil {
166 b, err = ioutil.ReadFile(shellConfig)
171 if string(b) != expected {
172 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))