Skip to content

Commit 073d23c

Browse files
nichtsundniemandmvdan
authored andcommitted
cmd/cue/cmd: show incorrect handling of binary files when embedded
These new tests show that CUE will treat input bytes as UTF-8 when embedding a file with `type=binary`. Since txtar can only store files with valid UTF8, and including any non-printable characters in a txtar is error-prone, add a new strconv-unquote command to use Go string quoting instead. For #3741. Signed-off-by: nichtsundniemand <[email protected]> Change-Id: Ic2703d80b922d66d6d37b8044e2b8a986aa58b2d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1209312 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 880023b commit 073d23c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

cmd/cue/cmd/script_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ func TestScript(t *testing.T) {
238238
ts.Check(err)
239239
}
240240
},
241+
// strconv-unquote treats each argument as a go quoted string, unquotes it and prints
242+
// them, separated by newlines, to stdout
243+
"strconv-unquote": func(ts *testscript.TestScript, neg bool, args []string) {
244+
if neg {
245+
ts.Fatalf("usage: strconv-unquote args...")
246+
}
247+
for _, quoted := range args {
248+
s, err := strconv.Unquote(quoted)
249+
ts.Check(err)
250+
fmt.Fprintln(ts.Stdout(), s)
251+
}
252+
},
241253
},
242254
Setup: func(e *testscript.Env) error {
243255
// If a testscript loads CUE packages but forgot to set up a cue.mod,

cmd/cue/cmd/testdata/script/embed.txtar

+36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ env CUE_EXPERIMENT=embed=0
44
cmp stderr out/noembed
55
env CUE_EXPERIMENT=
66

7+
# Create test files for type=binary using strconv-unquote as txtar can only hold UTF8.
8+
strconv-unquote '"\xef\xbb\xbfHello, 世界"'
9+
cp stdout test-utf8-bom.binary
10+
strconv-unquote "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7"
11+
cp stdout test-nonutf8.binary
12+
713
exec cue eval
814
cmp stdout out/eval
915

@@ -39,6 +45,12 @@ f: _ @embed(file="openapi.json", type=openapi)
3945

4046
g: _ @embed(file="openapi.json") // test no auto mode!
4147

48+
h: _ @embed(file="test-utf8-bom.binary", type=binary)
49+
50+
i: _ @embed(file="test-utf8-bom.binary", type=text)
51+
52+
j: _ @embed(file="test-nonutf8.binary", type=binary)
53+
4254
special: {
4355
// These are all valid.
4456
underscoreFile: _ @embed(file="y/_test.json")
@@ -213,6 +225,18 @@ g: {
213225
}
214226
}
215227
}
228+
h: '''
229+
Hello, 世界
230+
231+
'''
232+
i: """
233+
Hello, 世界
234+
235+
"""
236+
j: '''
237+
��������
238+
239+
'''
216240
special: {
217241
underscoreFile: {
218242
z: 45
@@ -273,6 +297,18 @@ g: {
273297
}
274298
}
275299
}
300+
h: '''
301+
Hello, 世界
302+
303+
'''
304+
i: """
305+
Hello, 世界
306+
307+
"""
308+
j: '''
309+
��������
310+
311+
'''
276312
special: {
277313
// These are all valid.
278314
underscoreFile: z: 45

0 commit comments

Comments
 (0)