Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape newlines in generated Dockerfiles #194

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
----------

- Escape newlines. (@MisterDA #192 #194, reported by @kit-ty-kate)
- Add Ubuntu 23.10. (@MisterDA #189)
- Deprecate Ubuntu 22.10 it is now EOL. (@tmcgilchrist #184).
- Support `--start-interval` in `HEALTCHECK` Dockerfile instruction.
Expand Down
8 changes: 5 additions & 3 deletions src/dockerfile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ let json_array_of_list sl =

let string_of_shell_or_exec ~escape (t : shell_or_exec) =
match t with
| `Shell s -> s
| `Shell s -> escape_string ~char_to_escape:'\n' ~escape s
| `Shells [] -> ""
| `Shells [ s ] -> s
| `Shells l -> String.concat (" && " ^ String.make 1 escape ^ "\n ") l
| `Shells [ s ] -> escape_string ~char_to_escape:'\n' ~escape s
| `Shells l ->
List.map (escape_string ~char_to_escape:'\n' ~escape) l
|> String.concat (" && " ^ String.make 1 escape ^ "\n ")
| `Exec sl -> json_array_of_list sl

let quote_env_var = escape_string ~char_to_escape:'"'
Expand Down
11 changes: 11 additions & 0 deletions test/dockerfile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ RUN script
Alcotest.(check' string) ~msg:"complete" ~expected ~actual;
()

let test_issue_192 () =
let open Dockerfile in
let actual = from "alpine" @@ run "ls\n/" |> string_of_t
and expected = {|FROM alpine
RUN ls\
/
|} in
Alcotest.(check' string) ~msg:"escape newlines" ~expected ~actual;
()

let () =
Alcotest.(
run "test"
Expand All @@ -118,5 +128,6 @@ let () =
test_string_of_t_formatting_simple_image;
test_case "string_of_t" `Quick
test_string_of_t_formatting_multiple_images;
test_case "test_issue_192" `Quick test_issue_192;
] );
])