|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// The build context is the legacy top-level "context", else build.context, else |
| 9 | +// none (TS getDockerContextPath, spec-node/utils.ts: `'dockerFile' in config ? |
| 10 | +// config.context : config.build.context`). |
| 11 | +func TestBuildContextPrefersTheLegacyProperty(t *testing.T) { |
| 12 | + for in, want := range map[string]string{ |
| 13 | + `{"dockerFile":"D","context":"legacy","build":{"context":"build"}}`: "legacy", |
| 14 | + `{"build":{"context":".."}}`: "..", |
| 15 | + `{"build":{"dockerfile":"Dockerfile"}}`: "", |
| 16 | + `{}`: "", |
| 17 | + } { |
| 18 | + var c DevContainer |
| 19 | + if err := json.Unmarshal([]byte(in), &c); err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + if got := c.BuildContext(); got != want { |
| 23 | + t.Errorf("BuildContext(%s) = %q, want %q", in, got, want) |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// A lifecycle command marshals back as it was written - the string, array or |
| 29 | +// object TS keeps as-is in the metadata label - and an absent one as null. |
| 30 | +func TestLifecycleCommandMarshalsAsWritten(t *testing.T) { |
| 31 | + for _, in := range []string{`"echo hi"`, `["echo","hi"]`, `{"a":"echo a"}`, `null`} { |
| 32 | + var c LifecycleCommand |
| 33 | + if err := json.Unmarshal([]byte(in), &c); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + got, err := json.Marshal(c) |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + if string(got) != in { |
| 41 | + t.Errorf("marshal(%s) = %s, want it unchanged", in, got) |
| 42 | + } |
| 43 | + } |
| 44 | + if got, err := json.Marshal(LifecycleCommand{}); err != nil || string(got) != "null" { |
| 45 | + t.Errorf("marshal of no command = %s, %v; want null", got, err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// A single dockerComposeFile marshals as the string, more than one as the array |
| 50 | +// (the `string | string[]` shape the spec gives the property). |
| 51 | +func TestStringOrStringsMarshalsOneAsAString(t *testing.T) { |
| 52 | + for in, want := range map[string]string{ |
| 53 | + `"a.yml"`: `"a.yml"`, |
| 54 | + `["a.yml"]`: `"a.yml"`, |
| 55 | + `["a.yml","b.yml"]`: `["a.yml","b.yml"]`, |
| 56 | + } { |
| 57 | + var s StringOrStrings |
| 58 | + if err := json.Unmarshal([]byte(in), &s); err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + got, err := json.Marshal(s) |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + if string(got) != want { |
| 66 | + t.Errorf("marshal(%s) = %s, want %s", in, got, want) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments