| package utils | |
| import ( | |
| "github.com/google/go-cmp/cmp" | |
| "os" | |
| "testing" | |
| ) | |
| func TestSourceFile(t *testing.T) { | |
| os.Setenv("Z", "12") | |
| contents := ` | |
| W="a b c" | |
| X=10 | |
| WEIRD_STRING='a"bc' | |
| KERNEL_VERSION_REGEX='6\.1\.1.*' | |
| echo some stdout message | |
| Q[0]=3 | |
| echo some stderr message >&2 | |
| Q[1]=4 | |
| echo Y=12 # should be ignored since we don't read from stdout or stdenv when sourcing | |
| export Y=11 | |
| ` | |
| env, err := SourceString(contents) | |
| if err != nil { | |
| t.Fatalf("%v", err) | |
| } | |
| expected := map[string]string{ | |
| "WEIRD_STRING": `a\"bc`, | |
| "KERNEL_VERSION_REGEX": `6\\.1\\.1.*`, | |
| "W": "a b c", | |
| "X": "10", | |
| "Y": "11", | |
| "Z": "12", | |
| "Q": `([0]="3" [1]="4")`, | |
| } | |
| actual := make(map[string]string) | |
| for key := range expected { | |
| actual[key] = env[key] | |
| } | |
| diff := cmp.Diff(expected, actual) | |
| if len(diff) > 0 { | |
| t.Fatalf("expected value did not equal actual\ndiff: %s", diff) | |
| } | |
| } | |
| func TestArrayElements(t *testing.T) { | |
| arrayString := `X=([0]="abc" [1]="def" [2]="gh\"i" [3]="6\\.1\\.1.*")` | |
| elements := ArrayElements(arrayString) | |
| expected := map[string]string{ | |
| "0": "abc", | |
| "1": "def", | |
| "2": `gh\"i`, | |
| "3": `6\.1\.1.*`, | |
| } | |
| diff := cmp.Diff(expected, elements) | |
| if len(diff) > 0 { | |
| t.Fatalf("expected value did not equal actual\ndiff: %s", diff) | |
| } | |
| } |