github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/cmd/getgo/main_test.go (about)

     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.
     4  
     5  //go:build !plan9
     6  // +build !plan9
     7  
     8  package main
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"os"
    15  	"os/exec"
    16  	"testing"
    17  )
    18  
    19  func TestMain(m *testing.M) {
    20  	if os.Getenv("GO_GETGO_TEST_IS_GETGO") != "" {
    21  		main()
    22  		os.Exit(0)
    23  	}
    24  
    25  	if os.Getenv("GOGET_INTEGRATION") == "" {
    26  		fmt.Fprintln(os.Stderr, "main_test: Skipping integration tests with GOGET_INTEGRATION unset")
    27  		return
    28  	}
    29  
    30  	// Don't let these environment variables confuse the test.
    31  	os.Unsetenv("GOBIN")
    32  	os.Unsetenv("GOPATH")
    33  	os.Unsetenv("GIT_ALLOW_PROTOCOL")
    34  	os.Unsetenv("PATH")
    35  
    36  	os.Exit(m.Run())
    37  }
    38  
    39  func createTmpHome(t *testing.T) string {
    40  	tmpd, err := ioutil.TempDir("", "testgetgo")
    41  	if err != nil {
    42  		t.Fatalf("creating test tempdir failed: %v", err)
    43  	}
    44  
    45  	os.Setenv("HOME", tmpd)
    46  	return tmpd
    47  }
    48  
    49  // doRun runs the test getgo command, recording stdout and stderr and
    50  // returning exit status.
    51  func doRun(t *testing.T, args ...string) error {
    52  	exe, err := os.Executable()
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	t.Helper()
    57  
    58  	t.Logf("running getgo %v", args)
    59  	var stdout, stderr bytes.Buffer
    60  	cmd := exec.Command(exe, args...)
    61  	cmd.Stdout = &stdout
    62  	cmd.Stderr = &stderr
    63  	cmd.Env = append(os.Environ(), "GO_GETGO_TEST_IS_GETGO=1")
    64  	status := cmd.Run()
    65  	if stdout.Len() > 0 {
    66  		t.Log("standard output:")
    67  		t.Log(stdout.String())
    68  	}
    69  	if stderr.Len() > 0 {
    70  		t.Log("standard error:")
    71  		t.Log(stderr.String())
    72  	}
    73  	return status
    74  }
    75  
    76  func TestCommandVerbose(t *testing.T) {
    77  	tmpd := createTmpHome(t)
    78  	defer os.RemoveAll(tmpd)
    79  
    80  	err := doRun(t, "-v")
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	// make sure things are in path
    85  	shellConfig, err := shellConfigFile()
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	b, err := ioutil.ReadFile(shellConfig)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	home, err := getHomeDir()
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	expected := fmt.Sprintf(`
    99  export PATH=$PATH:%s/.go/bin
   100  
   101  export GOPATH=%s/go
   102  
   103  export PATH=$PATH:%s/go/bin
   104  `, home, home, home)
   105  
   106  	if string(b) != expected {
   107  		t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
   108  	}
   109  }
   110  
   111  func TestCommandPathExists(t *testing.T) {
   112  	tmpd := createTmpHome(t)
   113  	defer os.RemoveAll(tmpd)
   114  
   115  	// run once
   116  	err := doRun(t, "-skip-dl")
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	// make sure things are in path
   121  	shellConfig, err := shellConfigFile()
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	b, err := ioutil.ReadFile(shellConfig)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	home, err := getHomeDir()
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  
   134  	expected := fmt.Sprintf(`
   135  export GOPATH=%s/go
   136  
   137  export PATH=$PATH:%s/go/bin
   138  `, home, home)
   139  
   140  	if string(b) != expected {
   141  		t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
   142  	}
   143  
   144  	// run twice
   145  	if err := doRun(t, "-skip-dl"); err != nil {
   146  		t.Fatal(err)
   147  	}
   148  
   149  	b, err = ioutil.ReadFile(shellConfig)
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  
   154  	if string(b) != expected {
   155  		t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
   156  	}
   157  }