Skip to content

Commit d707163

Browse files
committed
fix: shell quoting of files
Use $'' to correctly insert literals. This technically loses posix compatibility, but is supported by all major shells. Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent bd9deda commit d707163

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased
22
----------
33

4+
- Allow spaces in cram test paths (#4980, fixes #4162, @rgrinberg)
5+
46
- Fix `foreign_stubs` inside a `tests` stanza. Previously, dune would crash
57
when this field was present (#4942, fix #4946, @rgrinberg)
68

src/dune_rules/cram_exec.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ let translate_path_for_sh =
111111
On Windows, we still generate a [sh] script so we need to quote using Unix
112112
conventions. *)
113113
let quote_for_sh fn =
114+
(* we lose some portability as [$'] isn't posix. I don't see a better way to
115+
do this *)
114116
let buf = Buffer.create (String.length fn + 2) in
115-
Buffer.add_char buf '\'';
117+
Buffer.add_string buf "$'";
116118
String.iter fn ~f:(function
117-
| '\'' -> Buffer.add_string buf "'\\''"
119+
| '\'' -> Buffer.add_string buf "\'"
120+
| '\\' -> Buffer.add_string buf "\\\\"
118121
| c -> Buffer.add_char buf c);
119122
Buffer.add_char buf '\'';
120123
Buffer.contents buf
@@ -353,7 +356,7 @@ let create_sh_script cram_stanzas ~temp_dir : sh_script Fiber.t =
353356
in
354357
fprln oc ". %s > %s 2>&1" user_shell_code_file_sh_path
355358
user_shell_code_output_file_sh_path;
356-
fprln oc {|printf "%%d\0%%s\0" $? $%s >> %s|}
359+
fprln oc {|printf "%%d\0%%s\0" $? "$%s" >> %s|}
357360
Action_exec._BUILD_PATH_PREFIX_MAP metadata_file_sh_path;
358361
Cram_lexer.Command
359362
{ command = lines

test/blackbox-tests/test-cases/gh4162.t

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Cram doesn't like tests in paths
1414
$ echo foo
1515

1616
$ dune build @foo
17-
sh (internal) (exit 1)
18-
(cd _build/.sandbox/54ffcedad804d81955c7b33d18d42e68/default && /run/current-system/sw/bin/sh /var/folders/nc/x9_nmmsj0rjbfyzxb_kjk6qr0000gn/T/build_95f84b_dune/dune_cram_01b015_.foo.t/main.sh)
19-
/var/folders/nc/x9_nmmsj0rjbfyzxb_kjk6qr0000gn/T/build_95f84b_dune/dune_cram_01b015_.foo.t/main.sh: line 5: printf: bbb/_build/.sandbox/54ffcedad804d81955c7b33d18d42e68/default:$TESTCASE_ROOT=$TESTCASE_ROOT/aaa: invalid number
20-
-> required by alias foo
17+
File "foo.t", line 1, characters 0-0:
18+
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
2119
[1]

0 commit comments

Comments
 (0)