Skip to content

Commit

Permalink
cmd/cue: add more test case for exporting to a filename
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: I92b1cbea50a5da4bd8d70edb86031eebb5a60b19
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1169708
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
  • Loading branch information
mvdan committed Sep 25, 2023
1 parent cbdd996 commit 388f9a1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func TestX(t *testing.T) {
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"cue": MainTest,
// Until https://github.com/rogpeppe/go-internal/issues/93 is fixed,
// or we have some other way to use "exec" without caring about success,
// this is an easy way for us to mimic `? exec cue`.
"cue_exitzero": func() int {
MainTest()
return 0
},
"cue_stdinpipe": func() int {
cwd, _ := os.Getwd()
if err := mainTestStdinPipe(); err != nil {
Expand Down
30 changes: 30 additions & 0 deletions cmd/cue/cmd/testdata/script/export_force.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,40 @@ stderr 'error writing "test.yml": file already exists'
exec cue export --force -o test.yml file.cue
cmp test.yml test.yml.golden

# With or without --force, we should fail to output to a file inside a missing directory.
# TODO: these should not succeed; the following commit fixes the bug.
exec cue export -o /definitely/does/not/exist/test.yml file.cue
exec cue export --force -o /definitely/does/not/exist/test.yml file.cue

# Two concurrent exports to the same new file without --force;
# only one should succeed. We use a relatively slow bit of CUE
# to make it likely that both export operations begin before either
# has finished and created the resulting output file.
# Since it's a coin toss which command wins the race,
# we allow both to fail but expect the joint stderr to contain exactly one error.
# TODO: this is currently racy; the following commit fixes the bug.
# exec cue_exitzero export -o conflict.yml slow.cue &
# exec cue_exitzero export -o conflict.yml slow.cue &
# wait
# stderr -count=1 'error writing "conflict.yml": file already exists'
# exists conflict.yml

# Now with --force; the two commands should always succeed.
exec cue export --force -o conflict_force.yml slow.cue &
exec cue export --force -o conflict_force.yml slow.cue &
wait
exists conflict_force.yml

-- file.cue --
package hello

#who: "World"
message: "Hello \(#who)!"
-- test.yml.golden --
message: Hello World!
-- slow.cue --
package hello

import "list"

out: list.Repeat(["x"], 2000)

0 comments on commit 388f9a1

Please sign in to comment.