|
| 1 | +package config |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestEnvsExecute(t *testing.T) { |
| 6 | + cfg := Config{ |
| 7 | + Shell: "bash", |
| 8 | + WorkDir: ".", |
| 9 | + } |
| 10 | + |
| 11 | + t.Run("resolves env entries sequentially", func(t *testing.T) { |
| 12 | + envs := &Envs{} |
| 13 | + envs.Set("ENGINE", Env{Name: "ENGINE", Value: "docker"}) |
| 14 | + envs.Set("COMPOSE", Env{Name: "COMPOSE", Sh: `echo "${ENGINE}-compose"`}) |
| 15 | + |
| 16 | + err := envs.Execute(cfg, nil) |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("unexpected execute error: %s", err) |
| 19 | + } |
| 20 | + |
| 21 | + if got := envs.Mapping["COMPOSE"].Value; got != "docker-compose" { |
| 22 | + t.Fatalf("expected COMPOSE=docker-compose, got %q", got) |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("uses base env for sh evaluation", func(t *testing.T) { |
| 27 | + envs := &Envs{} |
| 28 | + envs.Set("COMPOSE", Env{Name: "COMPOSE", Sh: `echo "${ENGINE}-compose"`}) |
| 29 | + |
| 30 | + err := envs.Execute(cfg, map[string]string{"ENGINE": "docker"}) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("unexpected execute error: %s", err) |
| 33 | + } |
| 34 | + |
| 35 | + if got := envs.Mapping["COMPOSE"].Value; got != "docker-compose" { |
| 36 | + t.Fatalf("expected COMPOSE=docker-compose, got %q", got) |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("resolved lets env overrides process env", func(t *testing.T) { |
| 41 | + t.Setenv("ENGINE", "podman") |
| 42 | + |
| 43 | + envs := &Envs{} |
| 44 | + envs.Set("ENGINE", Env{Name: "ENGINE", Value: "docker"}) |
| 45 | + envs.Set("COMPOSE", Env{Name: "COMPOSE", Sh: `echo "${ENGINE}-compose"`}) |
| 46 | + |
| 47 | + err := envs.Execute(cfg, nil) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("unexpected execute error: %s", err) |
| 50 | + } |
| 51 | + |
| 52 | + if got := envs.Mapping["COMPOSE"].Value; got != "docker-compose" { |
| 53 | + t.Fatalf("expected COMPOSE=docker-compose, got %q", got) |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("keeps cached values after first execution", func(t *testing.T) { |
| 58 | + envs := &Envs{} |
| 59 | + envs.Set("COMPOSE", Env{Name: "COMPOSE", Sh: `echo "${ENGINE}-compose"`}) |
| 60 | + |
| 61 | + err := envs.Execute(cfg, map[string]string{"ENGINE": "docker"}) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("unexpected execute error: %s", err) |
| 64 | + } |
| 65 | + |
| 66 | + err = envs.Execute(cfg, map[string]string{"ENGINE": "podman"}) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("unexpected execute error: %s", err) |
| 69 | + } |
| 70 | + |
| 71 | + if got := envs.Mapping["COMPOSE"].Value; got != "docker-compose" { |
| 72 | + t.Fatalf("expected cached COMPOSE=docker-compose, got %q", got) |
| 73 | + } |
| 74 | + }) |
| 75 | +} |
0 commit comments