From 8079151c1917ed0f7ae467e3da5a5a705662e917 Mon Sep 17 00:00:00 2001 From: Matthew Sackman Date: Mon, 8 Jul 2024 13:53:56 +0100 Subject: [PATCH] tools/trim: convert inline tests to txtar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit txtar is the preferred mechanism for tests that drive cue. Convert all existing trim tests to txtar. The txtar tests run as part of the existing TestTrimFiles target (renamed from TestData to be a little more descriptive). Remove TestX because the TxTarTest code supports DebugArchive which allows easy overriding for the purposes of experimentation. Signed-off-by: Matthew Sackman Change-Id: I985f2e27b32fe01fb6b36b5e124cbb5c986c180e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197348 Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo --- .../defaults_can_remove_non-defaults.txtar | 8 + .../do_not_overmark_comprehension.txtar | 27 ++ tools/trim/testdata/do_not_remove_field.txtar | 7 + tools/trim/testdata/issue303.txtar | 9 + tools/trim/testdata/list_removal_1.txtar | 15 + tools/trim/testdata/list_removal_2.txtar | 13 + .../optional_does_not_remove_required.txtar | 23 ++ .../remove_due_to_simplification.txtar | 45 +++ .../remove_implied_interpolations.txtar | 18 ++ .../testdata/remove_top-level_struct.txtar | 29 ++ tools/trim/trim_test.go | 297 +----------------- 11 files changed, 195 insertions(+), 296 deletions(-) create mode 100644 tools/trim/testdata/defaults_can_remove_non-defaults.txtar create mode 100644 tools/trim/testdata/do_not_overmark_comprehension.txtar create mode 100644 tools/trim/testdata/do_not_remove_field.txtar create mode 100644 tools/trim/testdata/issue303.txtar create mode 100644 tools/trim/testdata/list_removal_1.txtar create mode 100644 tools/trim/testdata/list_removal_2.txtar create mode 100644 tools/trim/testdata/optional_does_not_remove_required.txtar create mode 100644 tools/trim/testdata/remove_due_to_simplification.txtar create mode 100644 tools/trim/testdata/remove_implied_interpolations.txtar create mode 100644 tools/trim/testdata/remove_top-level_struct.txtar diff --git a/tools/trim/testdata/defaults_can_remove_non-defaults.txtar b/tools/trim/testdata/defaults_can_remove_non-defaults.txtar new file mode 100644 index 00000000000..5ec4136a7c7 --- /dev/null +++ b/tools/trim/testdata/defaults_can_remove_non-defaults.txtar @@ -0,0 +1,8 @@ +# TODO: make this optional +-- in.cue -- +foo: [string]: a: *1 | int +foo: b: a: 1 +-- out/trim -- +== in.cue +foo: [string]: a: *1 | int +foo: b: {} diff --git a/tools/trim/testdata/do_not_overmark_comprehension.txtar b/tools/trim/testdata/do_not_overmark_comprehension.txtar new file mode 100644 index 00000000000..bee9e3e7b44 --- /dev/null +++ b/tools/trim/testdata/do_not_overmark_comprehension.txtar @@ -0,0 +1,27 @@ +-- in.cue -- +foo: multipath: { + t: [string]: { x: 5 } + + // Don't remove u! + t: u: { x: 5 } +} + +group: { + for k, v in foo { + comp: "\(k)": v + } +} +-- out/trim -- +== in.cue +foo: multipath: { + t: [string]: {x: 5} + + // Don't remove u! + t: u: {} +} + +group: { + for k, v in foo { + comp: "\(k)": v + } +} diff --git a/tools/trim/testdata/do_not_remove_field.txtar b/tools/trim/testdata/do_not_remove_field.txtar new file mode 100644 index 00000000000..68d78e4e3ad --- /dev/null +++ b/tools/trim/testdata/do_not_remove_field.txtar @@ -0,0 +1,7 @@ +-- in.cue -- +{[_]: x: "hello"} +a: x: "hello" +-- out/trim -- +== in.cue +{[_]: x: "hello"} +a: {} diff --git a/tools/trim/testdata/issue303.txtar b/tools/trim/testdata/issue303.txtar new file mode 100644 index 00000000000..f749f11966f --- /dev/null +++ b/tools/trim/testdata/issue303.txtar @@ -0,0 +1,9 @@ +-- in.cue -- +foo: c: true +foo: #M +#M: c?: bool +-- out/trim -- +== in.cue +foo: c: true +foo: #M +#M: c?: bool diff --git a/tools/trim/testdata/list_removal_1.txtar b/tools/trim/testdata/list_removal_1.txtar new file mode 100644 index 00000000000..aa395a67a27 --- /dev/null +++ b/tools/trim/testdata/list_removal_1.txtar @@ -0,0 +1,15 @@ +-- in.cue -- +service: [string]: { + ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] +} +service: a: { + ports: [{a: 1}, {a: 1, extra: 3}, {}, { extra: 3 }] +} +-- out/trim -- +== in.cue +service: [string]: { + ports: [{a: 1}, {a: 1}, ...{extra: 3}] +} +service: a: { + ports: [{}, {extra: 3}, {}, {}] +} diff --git a/tools/trim/testdata/list_removal_2.txtar b/tools/trim/testdata/list_removal_2.txtar new file mode 100644 index 00000000000..bd6d84e228e --- /dev/null +++ b/tools/trim/testdata/list_removal_2.txtar @@ -0,0 +1,13 @@ +-- in.cue -- +service: [string]: { + ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] +} +service: a: { + ports: [{a: 1}, {a: 1,}] +} +-- out/trim -- +== in.cue +service: [string]: { + ports: [{a: 1}, {a: 1}, ...{extra: 3}] +} +service: a: {} diff --git a/tools/trim/testdata/optional_does_not_remove_required.txtar b/tools/trim/testdata/optional_does_not_remove_required.txtar new file mode 100644 index 00000000000..3b1d8aa7cf3 --- /dev/null +++ b/tools/trim/testdata/optional_does_not_remove_required.txtar @@ -0,0 +1,23 @@ +-- in.cue -- +a: ["aFoo"]: 3 +a: aFoo: _ + +a: { + {["aFoo"]: 3} + aFoo: _ +} + +["aFoo"]: 3 +aFoo: _ +-- out/trim -- +== in.cue +a: ["aFoo"]: 3 +a: aFoo: _ + +a: { + {["aFoo"]: 3} + aFoo: _ +} + +["aFoo"]: 3 +aFoo: _ diff --git a/tools/trim/testdata/remove_due_to_simplification.txtar b/tools/trim/testdata/remove_due_to_simplification.txtar new file mode 100644 index 00000000000..eb9ad6913fd --- /dev/null +++ b/tools/trim/testdata/remove_due_to_simplification.txtar @@ -0,0 +1,45 @@ +-- in.cue -- +foo: [string]: { + t: [string]: { + x: >=0 & <=5 + } +} + +foo: multipath: { + t: [string]: { + // Combined with the other constraints, we know the value must be 5 and + // thus the entry below can be eliminated. + x: >=5 & <=8 & int + } + + t: u: { x: 5 } +} + +group: { + for k, v in foo { + comp: "\(k)": v + } +} +-- out/trim -- +== in.cue +foo: [string]: { + t: [string]: { + x: >=0 & <=5 + } +} + +foo: multipath: { + t: [string]: { + // Combined with the other constraints, we know the value must be 5 and + // thus the entry below can be eliminated. + x: >=5 & <=8 & int + } + + t: u: {} +} + +group: { + for k, v in foo { + comp: "\(k)": v + } +} diff --git a/tools/trim/testdata/remove_implied_interpolations.txtar b/tools/trim/testdata/remove_implied_interpolations.txtar new file mode 100644 index 00000000000..2bbf9b6d085 --- /dev/null +++ b/tools/trim/testdata/remove_implied_interpolations.txtar @@ -0,0 +1,18 @@ +-- in.cue -- +foo: [string]: { + a: string + b: "--\(a)--" +} +foo: entry: { + a: "insert" + b: "--insert--" +} +-- out/trim -- +== in.cue +foo: [string]: { + a: string + b: "--\(a)--" +} +foo: entry: { + a: "insert" +} diff --git a/tools/trim/testdata/remove_top-level_struct.txtar b/tools/trim/testdata/remove_top-level_struct.txtar new file mode 100644 index 00000000000..d54dc12391b --- /dev/null +++ b/tools/trim/testdata/remove_top-level_struct.txtar @@ -0,0 +1,29 @@ +-- in.cue -- +a: b: 3 +for k, v in a { + c: "\(k)": v +} +c: b: 3 + +z: { + + a: b: 3 + for k, v in a { + c: "\(k)": v + } + c: b: 3 +} +-- out/trim -- +== in.cue +a: b: 3 +for k, v in a { + c: "\(k)": v +} + +z: { + + a: b: 3 + for k, v in a { + c: "\(k)": v + } +} diff --git a/tools/trim/trim_test.go b/tools/trim/trim_test.go index ddb8df4fdc6..217f90ff80c 100644 --- a/tools/trim/trim_test.go +++ b/tools/trim/trim_test.go @@ -17,14 +17,8 @@ package trim import ( "testing" - "golang.org/x/tools/txtar" - - "cuelang.org/go/cue" - "cuelang.org/go/cue/ast" "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/errors" - "cuelang.org/go/cue/format" - "cuelang.org/go/cue/parser" "cuelang.org/go/internal/core/runtime" "cuelang.org/go/internal/cuetdtest" "cuelang.org/go/internal/cuetxtar" @@ -37,255 +31,9 @@ var ( matrix = cuetdtest.DefaultOnlyMatrix ) -func TestFiles(t *testing.T) { - testCases := []struct { - name string - in string - out string - }{{ - name: "optional does not remove required", - in: ` - a: ["aFoo"]: 3 - a: aFoo: _ - - a: { - {["aFoo"]: 3} - aFoo: _ - } - - ["aFoo"]: 3 - aFoo: _ - `, - out: `a: ["aFoo"]: 3 -a: aFoo: _ - -a: { - {["aFoo"]: 3} - aFoo: _ -} - -["aFoo"]: 3 -aFoo: _ -`, - }, { - // TODO: make this optional - name: "defaults can remove non-defaults", - in: ` - foo: [string]: a: *1 | int - foo: b: a: 1 - `, - out: `foo: [string]: a: *1 | int -foo: b: {} -`, - }, { - name: "remove top-level struct", - in: ` - a: b: 3 - for k, v in a { - c: "\(k)": v - } - c: b: 3 - - z: { - - a: b: 3 - for k, v in a { - c: "\(k)": v - } - c: b: 3 - } - `, - out: `a: b: 3 -for k, v in a { - c: "\(k)": v -} - -z: { - - a: b: 3 - for k, v in a { - c: "\(k)": v - } -} -`, - }, { - name: "do not remove field", - in: ` - {[_]: x: "hello"} - a: x: "hello" - `, - out: ` -{[_]: x: "hello"} -a: {} -`, - }, { - name: "issue303", - in: ` - foo: c: true - foo: #M - #M: c?: bool - `, - out: `foo: c: true -foo: #M -#M: c?: bool -`, - }, { - name: "remove due to simplification", - in: ` -foo: [string]: { - t: [string]: { - x: >=0 & <=5 - } -} - -foo: multipath: { - t: [string]: { - // Combined with the other constraints, we know the value must be 5 and - // thus the entry below can be eliminated. - x: >=5 & <=8 & int - } - - t: u: { x: 5 } -} - -group: { - for k, v in foo { - comp: "\(k)": v - } -} - - `, - out: `foo: [string]: { - t: [string]: { - x: >=0 & <=5 - } -} - -foo: multipath: { - t: [string]: { - - x: >=5 & <=8 & int - } - - t: u: {} -} - -group: { - for k, v in foo { - comp: "\(k)": v - } -} -`, - }, { - name: "list removal", - in: ` - service: [string]: { - ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] - } - service: a: { - ports: [{a: 1}, {a: 1, extra: 3}, {}, { extra: 3 }] - } -`, - out: `service: [string]: { - ports: [{a: 1}, {a: 1}, ...{extra: 3}] -} -service: a: { - ports: [{}, {extra: 3}, {}, {}] -} -`, - }, { - name: "list removal", - in: ` - service: [string]: { - ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] - } - service: a: { - ports: [{a: 1}, {a: 1,}] - } - `, - out: `service: [string]: { - ports: [{a: 1}, {a: 1}, ...{extra: 3}] -} -service: a: {} -`, - }, { - name: "do not overmark comprehension", - in: ` -foo: multipath: { - t: [string]: { x: 5 } - - // Don't remove u! - t: u: { x: 5 } -} - -group: { - for k, v in foo { - comp: "\(k)": v - } -} - - `, - out: `foo: multipath: { - t: [string]: {x: 5} - - t: u: {} -} - -group: { - for k, v in foo { - comp: "\(k)": v - } -} -`, - }, { - name: "remove implied interpolations", - in: ` - foo: [string]: { - a: string - b: "--\(a)--" - } - foo: entry: { - a: "insert" - b: "--insert--" - } - `, - out: `foo: [string]: { - a: string - b: "--\(a)--" -} -foo: entry: { - a: "insert" -} -`, - }} - for _, tc := range testCases { - matrix.Run(t, tc.name, func(t *cuetdtest.M) { - f, err := parser.ParseFile("test", tc.in) - if err != nil { - t.Fatal(err) - } - r := cuecontext.New() - t.UpdateRuntime((*runtime.Runtime)(r)) - v := r.BuildFile(f) - if err := v.Err(); err != nil { - t.Fatal(err) - } - err = Files([]*ast.File{f}, v, &Config{Trace: false}) - if err != nil { - t.Fatal(err) - } - - out := formatNode(t.T, f) - if got := string(out); got != tc.out { - t.Errorf("\ngot:\n%s\nwant:\n%s", got, tc.out) - } - }) - } -} - const trace = false -func TestData(t *testing.T) { +func TestTrimFiles(t *testing.T) { test := cuetxtar.TxTarTest{ Root: "./testdata", Name: "trim", @@ -313,46 +61,3 @@ func TestData(t *testing.T) { } }) } - -func formatNode(t *testing.T, n ast.Node) []byte { - t.Helper() - - b, err := format.Node(n) - if err != nil { - t.Fatal(err) - } - return b -} - -// For debugging, do not remove. -func TestX(t *testing.T) { - in := ` --- in.cue -- -` - - t.Skip() - - a := txtar.Parse([]byte(in)) - instances := cuetxtar.Load(a, t.TempDir()) - - inst := cue.Build(instances)[0] - if inst.Err != nil { - t.Fatal(inst.Err) - } - - files := instances[0].Files - - Debug = true - - err := Files(files, inst, &Config{ - Trace: true, - }) - if err != nil { - t.Fatal(err) - } - - for _, f := range files { - b := formatNode(t, f) - t.Error(string(b)) - } -}