github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/cmd/getgo/path_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 "io/ioutil" 12 "os" 13 "path/filepath" 14 "strings" 15 "testing" 16 ) 17 18 func TestAppendPath(t *testing.T) { 19 tmpd, err := ioutil.TempDir("", "go") 20 if err != nil { 21 t.Fatal(err) 22 } 23 defer os.RemoveAll(tmpd) 24 25 if err := os.Setenv("HOME", tmpd); err != nil { 26 t.Fatal(err) 27 } 28 29 GOPATH := os.Getenv("GOPATH") 30 if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil { 31 t.Fatal(err) 32 } 33 34 shellConfig, err := shellConfigFile() 35 if err != nil { 36 t.Fatal(err) 37 } 38 b, err := ioutil.ReadFile(shellConfig) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 expected := "export PATH=" + pathVar + envSeparator + filepath.Join(GOPATH, "bin") 44 if strings.TrimSpace(string(b)) != expected { 45 t.Fatalf("expected: %q, got %q", expected, strings.TrimSpace(string(b))) 46 } 47 48 // Check that appendToPATH is idempotent. 49 if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil { 50 t.Fatal(err) 51 } 52 b, err = ioutil.ReadFile(shellConfig) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if strings.TrimSpace(string(b)) != expected { 57 t.Fatalf("expected: %q, got %q", expected, strings.TrimSpace(string(b))) 58 } 59 }