|
| 1 | +package envpath |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNewStack(t *testing.T) { |
| 9 | + |
| 10 | + // Initialize a new Stack from the existing env |
| 11 | + env := map[string]string{ |
| 12 | + "PATH": "/init-path", |
| 13 | + } |
| 14 | + stack := Stack(env) |
| 15 | + if len(stack.keys) == 0 { |
| 16 | + t.Errorf("Stack has no keys but should have %s", InitPathEnv) |
| 17 | + } |
| 18 | + if len(stack.keys) != 1 { |
| 19 | + t.Errorf("Stack has should have exactly one key (%s) but has %d keys. Keys are: %s", |
| 20 | + InitPathEnv, len(stack.keys), strings.Join(stack.keys, ", ")) |
| 21 | + } |
| 22 | + |
| 23 | + // Each testStep below is applied in order, and the resulting env |
| 24 | + // is used implicitly as input into the subsequent test step. |
| 25 | + // |
| 26 | + // These test steps are NOT independent! These are not "test cases" that |
| 27 | + // would usually be independent. |
| 28 | + testSteps := []struct { |
| 29 | + projectHash string |
| 30 | + nixEnvPath string |
| 31 | + preservePathStack bool |
| 32 | + expectedKeysLength int |
| 33 | + expectedEnv map[string]string |
| 34 | + }{ |
| 35 | + { |
| 36 | + projectHash: "fooProjectHash", |
| 37 | + nixEnvPath: "/foo1:/foo2", |
| 38 | + preservePathStack: false, |
| 39 | + expectedKeysLength: 2, |
| 40 | + expectedEnv: map[string]string{ |
| 41 | + "PATH": "/foo1:/foo2:/init-path", |
| 42 | + InitPathEnv: "/init-path", |
| 43 | + Key("fooProjectHash"): "/foo1:/foo2", |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + projectHash: "barProjectHash", |
| 48 | + nixEnvPath: "/bar1:/bar2", |
| 49 | + preservePathStack: false, |
| 50 | + expectedKeysLength: 3, |
| 51 | + expectedEnv: map[string]string{ |
| 52 | + "PATH": "/bar1:/bar2:/foo1:/foo2:/init-path", |
| 53 | + InitPathEnv: "/init-path", |
| 54 | + Key("fooProjectHash"): "/foo1:/foo2", |
| 55 | + Key("barProjectHash"): "/bar1:/bar2", |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + projectHash: "fooProjectHash", |
| 60 | + nixEnvPath: "/foo3:/foo2", |
| 61 | + preservePathStack: false, |
| 62 | + expectedKeysLength: 3, |
| 63 | + expectedEnv: map[string]string{ |
| 64 | + "PATH": "/foo3:/foo2:/bar1:/bar2:/init-path", |
| 65 | + InitPathEnv: "/init-path", |
| 66 | + Key("fooProjectHash"): "/foo3:/foo2", |
| 67 | + Key("barProjectHash"): "/bar1:/bar2", |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + projectHash: "barProjectHash", |
| 72 | + nixEnvPath: "/bar3:/bar2", |
| 73 | + preservePathStack: true, |
| 74 | + expectedKeysLength: 3, |
| 75 | + expectedEnv: map[string]string{ |
| 76 | + "PATH": "/foo3:/foo2:/bar3:/bar2:/init-path", |
| 77 | + InitPathEnv: "/init-path", |
| 78 | + Key("fooProjectHash"): "/foo3:/foo2", |
| 79 | + Key("barProjectHash"): "/bar3:/bar2", |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, testStep := range testSteps { |
| 85 | + t.Run( |
| 86 | + testStep.nixEnvPath, func(t *testing.T) { |
| 87 | + |
| 88 | + // Push to stack and update PATH env |
| 89 | + stack.Push(env, testStep.projectHash, testStep.nixEnvPath, testStep.preservePathStack) |
| 90 | + env["PATH"] = stack.Path(env) |
| 91 | + |
| 92 | + if len(stack.keys) != testStep.expectedKeysLength { |
| 93 | + t.Errorf("Stack should have exactly %d keys but has %d keys. Keys are: %s", |
| 94 | + testStep.expectedKeysLength, len(stack.keys), strings.Join(stack.keys, ", ")) |
| 95 | + } |
| 96 | + for k, v := range testStep.expectedEnv { |
| 97 | + if env[k] != v { |
| 98 | + t.Errorf("env[%s] should be %s but is %s", k, v, env[k]) |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments