| 1 | package ci |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestExpandVariables(t *testing.T) { |
| 6 | env := map[string]string{ |
| 7 | "HOST": "db.local", "PORT": "5432", "CI_PROJECT_NAME": "app", |
| 8 | "URL": "postgres://$HOST:${PORT}/${CI_PROJECT_NAME}", |
| 9 | "PASS": "pa$$w0rd$HOST", |
| 10 | "COST": "$$5 en $ONBEKEND en ${ONBEKEND2}", |
| 11 | "A": "x$B", "B": "y$A", |
| 12 | "NEST": "<$URL>", |
| 13 | } |
| 14 | got := ExpandVariables(env, map[string]bool{"URL": true, "COST": true, "A": true, "B": true, "NEST": true, "PASS": false}) |
| 15 | if got["URL"] != "postgres://db.local:5432/app" { |
| 16 | t.Fatalf("URL=%q", got["URL"]) |
| 17 | } |
| 18 | if got["NEST"] != "<postgres://db.local:5432/app>" { |
| 19 | t.Fatalf("NEST=%q", got["NEST"]) |
| 20 | } |
| 21 | if got["COST"] != "$5 en $ONBEKEND en ${ONBEKEND2}" { |
| 22 | t.Fatalf("COST=%q", got["COST"]) |
| 23 | } |
| 24 | if _, ok := got["PASS"]; ok { |
| 25 | t.Fatal("PASS zonder expand mag niet veranderen") |
| 26 | } |
| 27 | if env["PASS"] != "pa$$w0rd$HOST" { |
| 28 | t.Fatal("env aangepast") |
| 29 | } |
| 30 | // cyclus: geen hang, verwijzing blijft letterlijk |
| 31 | if got["A"] == "" || got["B"] == "" { |
| 32 | t.Fatalf("cyclus: %q %q", got["A"], got["B"]) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestExpandVariablesOff(t *testing.T) { |
| 37 | env := map[string]string{"P": "a$b$$c", "b": "X"} |
| 38 | if got := ExpandVariables(env, nil); len(got) != 0 { |
| 39 | t.Fatalf("%v", got) |
| 40 | } |
| 41 | } |