Skip to content

Commit eebcefc

Browse files
committed
cmd/cue: add testscript showing input unmarshal behavior with cue cmd
Given that we currently support defining `cue cmd` tasks by simply inserting `kind` or `$id` string fields, without referencing imported tool/... packages, it is possible for input data unmarshalled into a regular field to become a runnable task. This testscript shows the current behavior in a variety of these edge cases, including a hidden `_id` field which we will soon start using as well. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I7b8d5002b7d98d821702d7eb52c23e3f5c3f61ca Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214985 Reviewed-by: Paul Jolly <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent e145a14 commit eebcefc

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# `cue cmd` supports referring to tasks from tool/... packages via the
2+
# kind or $id string fields. This can result in surprising behavior
3+
# when unmarshalling data as a regular field alongside other tasks,
4+
# as the data can then declare further tasks to be run.
5+
6+
# Decoding input data containing a `kind` field outside other task values.
7+
exec cue cmd inputKindOutside
8+
stdout -count=1 '^hello$'
9+
stdout -count=1 '^command from input$'
10+
11+
# Decoding input data containing a `$id` field outside other task values.
12+
exec cue cmd inputDollarIdOutside
13+
stdout -count=1 '^hello$'
14+
stdout -count=1 '^command from input$'
15+
16+
# Decoding input data containing a `_id` field outside other task values.
17+
exec cue cmd inputUnderscoreIdOutside
18+
stdout -count=1 '^hello$'
19+
! stdout '^command from input$'
20+
21+
# Decoding input data containing a `$id` field inside a task value.
22+
exec cue cmd inputDollarIdInside
23+
stdout -count=1 '^hello$'
24+
! stdout '^command from input$'
25+
26+
# Decoding input data containing a `$id` field outside other task values
27+
# and as a hidden field.
28+
exec cue cmd inputDollarIdOutsideHidden
29+
stdout -count=1 '^hello$'
30+
! stdout '^command from input$'
31+
32+
# Use tool/cli.Print by copy pasting its definition, including the `$id` field.
33+
exec cue cmd useCliPrintCopy
34+
stdout -count=1 '^hello$'
35+
36+
-- input_kind.json --
37+
{"cmd": {"kind": "print", "text": "command from input"}, "data": "hello"}
38+
-- input_dollar_id.json --
39+
{"cmd": {"$id": "tool/cli.Print", "text": "command from input"}, "data": "hello"}
40+
-- input_underscore_id.json --
41+
{"cmd": {"_id": "tool/cli.Print", "text": "command from input"}, "data": "hello"}
42+
-- in_tool.cue --
43+
package p
44+
45+
import (
46+
"tool/cli"
47+
"tool/file"
48+
"encoding/json"
49+
)
50+
51+
command: inputKindOutside: {
52+
input: file.Read & {
53+
filename: "input_kind.json"
54+
}
55+
unmarshal: json.Unmarshal(input.contents)
56+
display: cli.Print & {
57+
text: unmarshal.data
58+
}
59+
}
60+
command: inputDollarIdOutside: {
61+
input: file.Read & {
62+
filename: "input_dollar_id.json"
63+
}
64+
unmarshal: json.Unmarshal(input.contents)
65+
display: cli.Print & {
66+
text: unmarshal.data
67+
}
68+
}
69+
command: inputUnderscoreIdOutside: {
70+
input: file.Read & {
71+
filename: "input_underscore_id.json"
72+
}
73+
unmarshal: json.Unmarshal(input.contents)
74+
display: cli.Print & {
75+
text: unmarshal.data
76+
}
77+
}
78+
command: inputDollarIdInside: {
79+
input: file.Read & {
80+
filename: "input_dollar_id.json"
81+
}
82+
display: cli.Print & {
83+
unmarshal: json.Unmarshal(input.contents)
84+
text: unmarshal.data
85+
}
86+
}
87+
command: inputDollarIdOutsideHidden: {
88+
input: file.Read & {
89+
filename: "input_dollar_id.json"
90+
}
91+
_unmarshal: json.Unmarshal(input.contents)
92+
display: cli.Print & {
93+
text: _unmarshal.data
94+
}
95+
}
96+
97+
command: useCliPrintCopy: cliPrintCopy & {
98+
text: "hello"
99+
}
100+
cliPrintCopy: {
101+
$id: _id
102+
_id: *"tool/cli.Print" | "print"
103+
text: string
104+
}

0 commit comments

Comments
 (0)