|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestReadConfigurationReportsTheFirstFeatureInDeclarationOrder(t *testing.T) { |
| 10 | + legacy9 := "Legacy feature '9' not supported." |
| 11 | + refusedZeta := "ERR: Feature '../zeta' could not be processed." |
| 12 | + for name, tc := range map[string]struct { |
| 13 | + features, override, additional string |
| 14 | + want string |
| 15 | + }{ |
| 16 | + "declaration order": {features: `{"../zeta":{},"../alpha":{}}`, want: refusedZeta}, |
| 17 | + "integer-like ids first": {features: `{"../zeta":{},"10":{},"9":{}}`, want: legacy9}, |
| 18 | + "override adds a Feature after": {features: `{"../zeta":{}}`, override: `{"features":{"../alpha":{}}}`, want: refusedZeta}, |
| 19 | + "override integer-like id first": {features: `{"../zeta":{}}`, override: `{"features":{"9":{}}}`, want: legacy9}, |
| 20 | + "additional Features after config": {features: `{"../zeta":{}}`, additional: `{"9":{},"../alpha":{}}`, want: refusedZeta}, |
| 21 | + "additional integer-like id first": {features: `{"../zeta":{}}`, additional: `{"../alpha":{},"10":{},"9":{}}`, want: refusedZeta}, |
| 22 | + } { |
| 23 | + t.Run(name, func(t *testing.T) { |
| 24 | + startFakeEngine(t, nil) |
| 25 | + files := map[string]string{".devcontainer/devcontainer.json": `{"image":"alpine:3","features":` + tc.features + `}`} |
| 26 | + if tc.override != "" { |
| 27 | + files["override.json"] = tc.override |
| 28 | + } |
| 29 | + ws := writeConfigFixture(t, files) |
| 30 | + args := []string{"--workspace-folder", ws, "--include-features-configuration"} |
| 31 | + if tc.override != "" { |
| 32 | + args = append(args, "--override-config", filepath.Join(ws, "override.json")) |
| 33 | + } |
| 34 | + if tc.additional != "" { |
| 35 | + args = append(args, "--additional-features", tc.additional) |
| 36 | + } |
| 37 | + stdout, err := readConfigurationStdout(args...) |
| 38 | + if err == nil || !strings.HasPrefix(err.Error(), tc.want) || stdout != "" { |
| 39 | + t.Fatalf("stdout = %q, err = %v; want %q", stdout, err, tc.want) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestReadConfigurationReportsAnAdditionalIntegerLikeIDAfterTheConfigs(t *testing.T) { |
| 46 | + startFakeEngine(t, nil) |
| 47 | + ws := writeConfigFixture(t, map[string]string{".devcontainer/devcontainer.json": `{"image":"alpine:3","features":{"zz":{}}}`}) |
| 48 | + _, err := readConfigurationStdout("--workspace-folder", ws, "--include-features-configuration", "--additional-features", `{"9":{}}`) |
| 49 | + if err == nil || !strings.HasPrefix(err.Error(), "Legacy feature 'zz' not supported.") { |
| 50 | + t.Fatalf("err = %v; want the config's legacy Feature zz first", err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestFeaturesResolveDependenciesListsFeaturesInDeclarationOrder(t *testing.T) { |
| 55 | + ids := []string{"zeta", "alpha", "mid", "beta", "omega"} |
| 56 | + local := map[string]string{} |
| 57 | + var decl []string |
| 58 | + for _, id := range ids { |
| 59 | + local[id] = `{"id":"` + id + `","version":"1.0.0"}` |
| 60 | + decl = append(decl, `"./`+id+`":{}`) |
| 61 | + } |
| 62 | + ws, _ := resolveDepsWorkspace(t, "{"+strings.Join(decl, ",")+"}", local) |
| 63 | + out, err := runResolveDeps(t, "--workspace-folder", ws) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + prev := -1 |
| 68 | + for _, id := range ids { |
| 69 | + i := strings.Index(out, "[./"+id+"<") |
| 70 | + if i <= prev { |
| 71 | + t.Fatalf("mermaid nodes not in declaration order %v:\n%s", ids, out) |
| 72 | + } |
| 73 | + prev = i |
| 74 | + } |
| 75 | +} |
0 commit comments