From d2ce0e3cd0a4556160bea21bf30fd51767094d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 19 Aug 2024 17:45:16 +0100 Subject: [PATCH] cmd/cue: add testscript to cover exporting CUE comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Much like encoding_empty.txtar, this is a high-level test for cmd/cue's ability to export CUE comments in different encodings, including to CUE. For #1180. For #3342. Signed-off-by: Daniel Martí Change-Id: I306c16d590b9c1faa45f8e083d094838cf9635ed Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199689 Unity-Result: CUE porcuepine Reviewed-by: Paul Jolly TryBot-Result: CUEcueckoo --- .../testdata/script/encoding_comments.txtar | 276 ++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 cmd/cue/cmd/testdata/script/encoding_comments.txtar diff --git a/cmd/cue/cmd/testdata/script/encoding_comments.txtar b/cmd/cue/cmd/testdata/script/encoding_comments.txtar new file mode 100644 index 00000000000..608c3babb0c --- /dev/null +++ b/cmd/cue/cmd/testdata/script/encoding_comments.txtar @@ -0,0 +1,276 @@ +# Test that the various encodings support encoding comments from CUE. +# We cover multiple output modes (def, eval, export) as well as multiple encodings, +# and also comments coming from definitions, unifications, and disjunctions. +# Note that some encodings have no support for comments, like JSON. +# This is meant mainly as an end-to-end smoke test; specific edge cases +# should be tested in each encoding package. + +# TODO(mvdan): test importing comments into CUE as well. +# TODO(mvdan): comments at the start and end of a file are not exported. +# TODO(mvdan): comments at the start of a struct are not exported. +# TODO(mvdan): comments attached to a list or its elements are not exported. +# TODO(mvdan): comments between fields or list elements are not exported. + +exec cue def --out cue . +cmp stdout def-cue.stdout + +# TODO(mvdan): why does eval not include comments when def and export do? +exec cue eval --out cue . +cmp stdout eval-cue.stdout + +exec cue export --out cue . +cmp stdout export-cue.stdout + +exec cue export --out json . +cmp stdout export-json.stdout + +# TODO(mvdan): YAML should support exporting comments. +exec cue export --out yaml . +cmp stdout export-yaml.stdout + +# TODO(mvdan): TOML should support exporting comments. +exec cue export --out toml . +cmp stdout export-toml.stdout + +-- basic.cue -- +// comment at the start of a file. + +// Package p is an example package. +package p + +// top-level comment before a simple field. +foo: "bar" +// top-level comment after a simple field. + +// top-level comment before a struct +struct: { + // comment at the start of a struct. + + // comment in a struct field + field1: "message1" + + // comment between struct fields. + + field2: "message2" + + // comment at the end of a struct. +} + +// top-level comment before a list. +list: [ + // comment at the start of a list. + + 1, + // comment in a list element. + 2, + + // comment between list elements. + + 3, + + // comment at the end of a list. +] + +// comment at the end of a file. +-- schema.cue -- +package p + +// Schema declares a schema. +#Schema: { + // name is the schema name. + name?: string +} +-- data.cue -- +package p + +// Data fits the schema. +Data: #Schema & { + // name is a specific name. + name: "Foo" +} + +// implicitUnified is any non-empty string. +implicitUnified: string & != "" + +// implicitUnified is some string value. +implicitUnified: "some value" + +// explicitUnified1 sets a default value. +explicitUnified1: string | *"some default" + +// explicitUnified2 sets a value. +explicitUnified2: "some value" + +// explicitUnified unifies two values. +explicitUnified: explicitUnified1 & explicitUnified2 + +// disjunction1 has a default. +_disjunction1: string | *"some default" + +// disjunction2 has no default. +_disjunction2: int + +// disjunction is like a sum type. +disjunction: _disjunction1 | _disjunction2 + +-- def-cue.stdout -- +// Package p is an example package. +package p + +// top-level comment before a simple field. +foo: "bar" + +// top-level comment before a struct +struct: { + // comment in a struct field + field1: "message1" + field2: "message2" + + // comment at the end of a struct. +} + +// Data fits the schema. +Data: #Schema & { + // name is a specific name. + name: "Foo" +} + +// Schema declares a schema. +#Schema: { + // name is the schema name. + name?: string +} + +// top-level comment before a list. +list: [1, 2, 3] + +// implicitUnified is any non-empty string. + +// implicitUnified is some string value. +implicitUnified: "some value" + +// explicitUnified1 sets a default value. +explicitUnified1: string | *"some default" + +// explicitUnified2 sets a value. +explicitUnified2: "some value" + +// explicitUnified unifies two values. +explicitUnified: explicitUnified1 & explicitUnified2 + +// disjunction1 has a default. +_disjunction1: string | *"some default" + +// disjunction2 has no default. +_disjunction2: int + +// disjunction is like a sum type. +disjunction: _disjunction1 | _disjunction2 +-- eval-cue.stdout -- +foo: "bar" +struct: { + field1: "message1" + field2: "message2" +} +Data: { + name: "Foo" +} +#Schema: {} +list: [1, 2, 3] +implicitUnified: "some value" +explicitUnified1: "some default" +explicitUnified2: "some value" +explicitUnified: "some value" +disjunction: "some default" +-- export-cue.stdout -- +// top-level comment before a simple field. +foo: "bar" + +// top-level comment before a struct +struct: { + // comment in a struct field + field1: "message1" + field2: "message2" + + // comment at the end of a struct. +} + +// Data fits the schema. +Data: { + // name is the schema name. + + // name is a specific name. + name: "Foo" +} + +// top-level comment before a list. +list: [1, 2, 3] + +// implicitUnified is any non-empty string. + +// implicitUnified is some string value. +implicitUnified: "some value" + +// explicitUnified1 sets a default value. +explicitUnified1: "some default" + +// explicitUnified2 sets a value. +explicitUnified2: "some value" + +// explicitUnified unifies two values. +explicitUnified: "some value" + +// disjunction is like a sum type. +disjunction: "some default" +-- export-json.stdout -- +{ + "foo": "bar", + "struct": { + "field1": "message1", + "field2": "message2" + }, + "Data": { + "name": "Foo" + }, + "list": [ + 1, + 2, + 3 + ], + "implicitUnified": "some value", + "explicitUnified1": "some default", + "explicitUnified2": "some value", + "explicitUnified": "some value", + "disjunction": "some default" +} +-- export-yaml.stdout -- +foo: bar +struct: + field1: message1 + field2: message2 +Data: + name: Foo +list: + - 1 + - 2 + - 3 +implicitUnified: some value +explicitUnified1: some default +explicitUnified2: some value +explicitUnified: some value +disjunction: some default +-- export-toml.stdout -- +disjunction = 'some default' +explicitUnified = 'some value' +explicitUnified1 = 'some default' +explicitUnified2 = 'some value' +foo = 'bar' +implicitUnified = 'some value' +list = [1, 2, 3] + +[Data] +name = 'Foo' + +[struct] +field1 = 'message1' +field2 = 'message2'