Skip to content

Commit 346daf4

Browse files
mvdancueckoo
authored andcommitted
cmd/cue: add a testscript for --inject-vars
The feature is documented in `cue help injection` but we lacked any sort of reasonable integration tests. This results from having answered questions about this feature a few times recently on Slack and Discord, and feeling a bit uneasy about how we had no tests to rely on. Moreover, the testscript also doubles as something we can point people to as proof that it works. Signed-off-by: Daniel Martí <[email protected]> Change-Id: If6cd81041357727fbab913c60abb06f4af9e48c2 Dispatch-Trailer: {"type":"trybot","CL":1214341,"patchset":1,"ref":"refs/changes/41/1214341/1","targetBranch":"master"}
1 parent 3ba376f commit 346daf4

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

cmd/cue/cmd/testdata/script/cmd_tags.txtar

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ cmp stdout expect-stdout
66
exec cue cmd tag tags.cue tags_tool.cue -t prod -t name=bar
77
cmp stdout expect-stdout
88

9-
# Verify that the new global -t flag added as a fix for issue 2510 above
10-
# works with the explicit or implicit "cmd" sub-command,
11-
# but not with other sub-commands like "fmt".
9+
# Verify that the global -t flag works with commands like "cmd"
10+
# or "eval", but not "fmt".
11+
exec cue -t prod -t name=bar cmd tag tags.cue tags_tool.cue
12+
cmp stdout expect-stdout
1213
exec cue eval -t name=bar tags.cue
1314
stdout 'name: *"bar"'
1415
exec cue -t name=bar eval tags.cue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Test each of the variables injected by --inject-vars/-T.
2+
cd cwd_subdir
3+
exec cue export -T
4+
cmpenv stdout ../export.stdout
5+
cd ..
6+
7+
# Validate the username and hostname against the standard Linux tools.
8+
# We could validate against os/user.Current and os.Hostname,
9+
# but that's the cmd/cue implementation, so it'd be a bit of a silly test.
10+
[exec:whoami] exec whoami
11+
cp stdout whoami.stdout
12+
exec cue export -e username --out text -T username.cue
13+
stdout '^[^"]+$'
14+
[exec:whoami] cmp stdout whoami.stdout
15+
[exec:hostname] exec hostname
16+
cp stdout hostname.stdout
17+
exec cue export -e hostname --out text -T hostname.cue
18+
stdout '^[^"]+$'
19+
[exec:hostname] cmp stdout hostname.stdout
20+
21+
# A random number is not deterministic by nature,
22+
# but we can validate that two consecutive runs with a random 128-bit integer
23+
# are extremely unlikely to result in the same random number.
24+
exec cue export -T rand.cue
25+
stdout '"rand": \d+'
26+
cp stdout rand-previous.stdout
27+
exec cue export -T rand.cue
28+
! cmp stdout rand-previous.stdout
29+
-- cwd_subdir/input.cue --
30+
package p
31+
32+
import (
33+
"path"
34+
"time"
35+
)
36+
37+
// The current time is constantly changing,
38+
// so simply check that it's valid and between 2020 and 3000.
39+
_now: string @tag(now,var=now)
40+
_parts: time.Split(_now)
41+
nowValidYear: _parts.year > 2020 && _parts.year < 3000
42+
43+
// The current directory will include a temporary directory,
44+
// so extract the basename only, which we can control here.
45+
_cwd: string @tag(cwd,var=cwd)
46+
cwdBase: path.Base(_cwd, path.Unix)
47+
48+
// os and arch are just Go's GOOS and GOARCH, validated via cmpenv.
49+
os: string @tag(os,var=os)
50+
arch: string @tag(arch,var=arch)
51+
52+
-- username.cue --
53+
username: string @tag(username,var=username)
54+
55+
-- hostname.cue --
56+
hostname: string @tag(hostname,var=hostname)
57+
58+
-- rand.cue --
59+
rand: int @tag(rand,var=rand)
60+
61+
-- export.stdout --
62+
{
63+
"nowValidYear": true,
64+
"cwdBase": "cwd_subdir",
65+
"os": "${GOOS}",
66+
"arch": "${GOARCH}"
67+
}

0 commit comments

Comments
 (0)