Skip to content

Commit 007f981

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":3,"ref":"refs/changes/41/1214341/3","targetBranch":"master"}
1 parent 3ba376f commit 007f981

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-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,69 @@
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+
# Note that "whoami" on Windows prints "domain\username" so it's unhelpful.
11+
[!windows] [exec:whoami] exec whoami
12+
cp stdout whoami.stdout
13+
exec cue export -e username --out text -T username.cue
14+
stdout '^[^"]+$'
15+
[!windows] [exec:whoami] cmp stdout whoami.stdout
16+
[exec:hostname] exec hostname
17+
cp stdout hostname.stdout
18+
exec cue export -e hostname --out text -T hostname.cue
19+
stdout '^[^"]+$'
20+
[exec:hostname] cmp stdout hostname.stdout
21+
22+
# A random number is not deterministic by nature,
23+
# but we can validate that two consecutive runs with a random 128-bit integer
24+
# are extremely unlikely to result in the same random number.
25+
exec cue export -T rand.cue
26+
stdout '"rand": \d+'
27+
cp stdout rand-previous.stdout
28+
exec cue export -T rand.cue
29+
! cmp stdout rand-previous.stdout
30+
-- cwd_subdir/input.cue --
31+
package p
32+
33+
import (
34+
"path"
35+
"time"
36+
)
37+
38+
// The current time is constantly changing,
39+
// so simply check that it's valid and between 2020 and 3000.
40+
_now: string @tag(now,var=now)
41+
_parts: time.Split(_now)
42+
nowValidYear: _parts.year > 2020 && _parts.year < 3000
43+
44+
// The current directory will include a temporary directory,
45+
// so extract the basename only, which we can control here.
46+
// Convert Windows path separators to slashes first, for consistent results.
47+
_cwd: string @tag(cwd,var=cwd)
48+
cwdBase: path.Base(path.ToSlash(_cwd, path.Windows), path.Unix)
49+
50+
// os and arch are just Go's GOOS and GOARCH, validated via cmpenv.
51+
os: string @tag(os,var=os)
52+
arch: string @tag(arch,var=arch)
53+
54+
-- username.cue --
55+
username: string @tag(username,var=username)
56+
57+
-- hostname.cue --
58+
hostname: string @tag(hostname,var=hostname)
59+
60+
-- rand.cue --
61+
rand: int @tag(rand,var=rand)
62+
63+
-- export.stdout --
64+
{
65+
"nowValidYear": true,
66+
"cwdBase": "cwd_subdir",
67+
"os": "${GOOS}",
68+
"arch": "${GOARCH}"
69+
}

0 commit comments

Comments
 (0)