|
| 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