github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/opts/env_test.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 "testing" 8 9 "gotest.tools/v3/assert" 10 ) 11 12 func TestValidateEnv(t *testing.T) { 13 type testCase struct { 14 value string 15 expected string 16 err error 17 } 18 tests := []testCase{ 19 { 20 value: "a", 21 expected: "a", 22 }, 23 { 24 value: "something", 25 expected: "something", 26 }, 27 { 28 value: "_=a", 29 expected: "_=a", 30 }, 31 { 32 value: "env1=value1", 33 expected: "env1=value1", 34 }, 35 { 36 value: "_env1=value1", 37 expected: "_env1=value1", 38 }, 39 { 40 value: "env2=value2=value3", 41 expected: "env2=value2=value3", 42 }, 43 { 44 value: "env3=abc!qwe", 45 expected: "env3=abc!qwe", 46 }, 47 { 48 value: "env_4=value 4", 49 expected: "env_4=value 4", 50 }, 51 { 52 value: "PATH", 53 expected: fmt.Sprintf("PATH=%v", os.Getenv("PATH")), 54 }, 55 { 56 value: "=a", 57 err: fmt.Errorf("invalid environment variable: =a"), 58 }, 59 { 60 value: "PATH=", 61 expected: "PATH=", 62 }, 63 { 64 value: "PATH=something", 65 expected: "PATH=something", 66 }, 67 { 68 value: "asd!qwe", 69 expected: "asd!qwe", 70 }, 71 { 72 value: "1asd", 73 expected: "1asd", 74 }, 75 { 76 value: "123", 77 expected: "123", 78 }, 79 { 80 value: "some space", 81 expected: "some space", 82 }, 83 { 84 value: " some space before", 85 expected: " some space before", 86 }, 87 { 88 value: "some space after ", 89 expected: "some space after ", 90 }, 91 { 92 value: "=", 93 err: fmt.Errorf("invalid environment variable: ="), 94 }, 95 } 96 97 if runtime.GOOS == "windows" { 98 // Environment variables are case in-sensitive on Windows 99 tests = append(tests, testCase{ 100 value: "PaTh", 101 expected: fmt.Sprintf("PaTh=%v", os.Getenv("PATH")), 102 err: nil, 103 }) 104 } 105 106 for _, tc := range tests { 107 tc := tc 108 t.Run(tc.value, func(t *testing.T) { 109 actual, err := ValidateEnv(tc.value) 110 111 if tc.err == nil { 112 assert.NilError(t, err) 113 } else { 114 assert.Error(t, err, tc.err.Error()) 115 } 116 assert.Equal(t, actual, tc.expected) 117 }) 118 } 119 }