Skip to content

Commit 388f9a1

Browse files
committed
cmd/cue: add more test case for exporting to a filename
First, a case where we export to a path which cannot be created. In the case of this test, because the parent directory does not exist, but there could be other reasons as well, like a permission error. "cue export -o" swallows that type of error, hence the bug. The test is inverted until the next commit fixes the bug. Second, a case where two export commands write to the same new file. This is racy without the --force flag currently, since the encoder checks for file presence via os.Stat first, and only later creates the file when the export has finished. The slightly slow bit of CUE makes the race likely to cause failures, but it's still racy behavior until fixed nonetheless, so the test is commented out until the next commit. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I92b1cbea50a5da4bd8d70edb86031eebb5a60b19 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1169708 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent cbdd996 commit 388f9a1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

cmd/cue/cmd/script_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ func TestX(t *testing.T) {
177177
func TestMain(m *testing.M) {
178178
os.Exit(testscript.RunMain(m, map[string]func() int{
179179
"cue": MainTest,
180+
// Until https://github.com/rogpeppe/go-internal/issues/93 is fixed,
181+
// or we have some other way to use "exec" without caring about success,
182+
// this is an easy way for us to mimic `? exec cue`.
183+
"cue_exitzero": func() int {
184+
MainTest()
185+
return 0
186+
},
180187
"cue_stdinpipe": func() int {
181188
cwd, _ := os.Getwd()
182189
if err := mainTestStdinPipe(); err != nil {

cmd/cue/cmd/testdata/script/export_force.txtar

+30
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,40 @@ stderr 'error writing "test.yml": file already exists'
1212
exec cue export --force -o test.yml file.cue
1313
cmp test.yml test.yml.golden
1414

15+
# With or without --force, we should fail to output to a file inside a missing directory.
16+
# TODO: these should not succeed; the following commit fixes the bug.
17+
exec cue export -o /definitely/does/not/exist/test.yml file.cue
18+
exec cue export --force -o /definitely/does/not/exist/test.yml file.cue
19+
20+
# Two concurrent exports to the same new file without --force;
21+
# only one should succeed. We use a relatively slow bit of CUE
22+
# to make it likely that both export operations begin before either
23+
# has finished and created the resulting output file.
24+
# Since it's a coin toss which command wins the race,
25+
# we allow both to fail but expect the joint stderr to contain exactly one error.
26+
# TODO: this is currently racy; the following commit fixes the bug.
27+
# exec cue_exitzero export -o conflict.yml slow.cue &
28+
# exec cue_exitzero export -o conflict.yml slow.cue &
29+
# wait
30+
# stderr -count=1 'error writing "conflict.yml": file already exists'
31+
# exists conflict.yml
32+
33+
# Now with --force; the two commands should always succeed.
34+
exec cue export --force -o conflict_force.yml slow.cue &
35+
exec cue export --force -o conflict_force.yml slow.cue &
36+
wait
37+
exists conflict_force.yml
38+
1539
-- file.cue --
1640
package hello
1741

1842
#who: "World"
1943
message: "Hello \(#who)!"
2044
-- test.yml.golden --
2145
message: Hello World!
46+
-- slow.cue --
47+
package hello
48+
49+
import "list"
50+
51+
out: list.Repeat(["x"], 2000)

0 commit comments

Comments
 (0)