golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/buildlet/buildlet_test.go (about) 1 // Copyright 2015 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 package main 6 7 import ( 8 "os" 9 "runtime" 10 "testing" 11 ) 12 13 func TestPathEnv(t *testing.T) { 14 for _, c := range []struct { 15 goos string // default "linux" 16 wd string // default "/workdir" 17 env []string 18 path []string 19 want string 20 noOp bool 21 }{ 22 { // No change to PATH 23 env: []string{"A=1", "PATH=/bin:/usr/bin", "B=2"}, 24 path: []string{"$PATH"}, 25 want: "PATH=/bin:/usr/bin", 26 noOp: true, 27 }, 28 { // Test that $EMPTY rewrites the path to be empty 29 env: []string{"A=1", "PATH=/bin:/usr/bin", "B=2"}, 30 path: []string{"$EMPTY"}, 31 want: "PATH=", 32 }, 33 { // Test that clearing an already-unset PATH is a no-op 34 env: []string{"A=1", "B=2"}, 35 path: []string{"$EMPTY"}, 36 want: "PATH=", 37 noOp: true, 38 }, 39 { // Test $WORKDIR expansion 40 env: []string{"A=1", "PATH=/bin:/usr/bin", "B=2"}, 41 path: []string{"/go/bin", "$WORKDIR/foo"}, 42 want: "PATH=/go/bin:/workdir/foo", 43 }, 44 { // Test $PATH expansion 45 env: []string{"A=1", "PATH=/bin:/usr/bin", "B=2"}, 46 path: []string{"/go/bin", "$PATH", "$WORKDIR/foo"}, 47 want: "PATH=/go/bin:/bin:/usr/bin:/workdir/foo", 48 }, 49 { // Test $PATH expansion (prepend only) 50 env: []string{"A=1", "PATH=/bin:/usr/bin", "B=2"}, 51 path: []string{"/go/bin", "/a/b", "$PATH"}, 52 want: "PATH=/go/bin:/a/b:/bin:/usr/bin", 53 }, 54 { // Test $PATH expansion (append only) 55 env: []string{"A=1", "PATH=/bin:/usr/bin", "B=2"}, 56 path: []string{"$PATH", "/go/bin", "/a/b"}, 57 want: "PATH=/bin:/usr/bin:/go/bin:/a/b", 58 }, 59 { // Test that empty $PATH expansion is a no-op 60 env: []string{"A=1", "B=2"}, 61 path: []string{"$PATH"}, 62 want: "PATH=", 63 noOp: true, 64 }, 65 { // Test that empty $PATH expansion does not add extraneous separators 66 env: []string{"A=1", "B=2"}, 67 path: []string{"$PATH", "$WORKDIR/foo"}, 68 want: "PATH=/workdir/foo", 69 }, 70 { // Test that in case of multiple PATH entries we modify the last one, 71 // not the first. 72 env: []string{"PATH=/bin:/usr/bin", "PATH=/bin:/usr/bin:/usr/local/bin"}, 73 path: []string{"$WORKDIR/foo", "$PATH"}, 74 want: "PATH=/workdir/foo:/bin:/usr/bin:/usr/local/bin", 75 }, 76 { // Test that Windows reads the existing variable regardless of case 77 goos: "windows", 78 wd: `C:\workdir`, 79 env: []string{"A=1", `PaTh=C:\Go\bin;C:\windows`, "B=2"}, 80 path: []string{"$PATH", `$WORKDIR\foo`}, 81 want: `PATH=C:\Go\bin;C:\windows;C:\workdir\foo`, 82 }, 83 { // Test that plan9 uses plan9 separators and "path" instead of "PATH" 84 goos: "plan9", 85 env: []string{"path=/bin\x00/usr/bin", "PATH=/bananas"}, 86 path: []string{"$PATH", "$WORKDIR/foo"}, 87 want: "path=/bin\x00/usr/bin\x00/workdir/foo", 88 }, 89 } { 90 goos := c.goos 91 if goos == "" { 92 goos = "linux" 93 } 94 wd := c.wd 95 if wd == "" { 96 wd = "/workdir" 97 } 98 got, gotOk := pathEnv(goos, c.env, c.path, wd) 99 wantOk := !c.noOp 100 if got != c.want || gotOk != wantOk { 101 t.Errorf("pathEnv(%q, %q, %q, %q) =\n\t%q, %t\nwant:\n\t%q, %t", goos, c.env, c.path, wd, got, gotOk, c.want, wantOk) 102 } 103 } 104 } 105 106 func TestPathListSeparator(t *testing.T) { 107 sep := pathListSeparator(runtime.GOOS) 108 want := string(os.PathListSeparator) 109 if sep != want { 110 t.Errorf("pathListSeparator(%q) = %q; want %q", runtime.GOOS, sep, want) 111 } 112 }