Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 27 additions & 1 deletion lib/mix/lib/mix/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,33 @@ defmodule Mix.Utils do
[" ", parent, quoted(name), edge_info, ?\n]
end

defp quoted(data), do: [?", to_string(data), ?"]
defp quoted(data) do
string = to_string(data)
escape_dot_string(string, <<?">>)
end

# Escape a string for DOT format according to GraphViz specification https://graphviz.org/doc/info/lang.html
# - Only quotes need escaping
# - The ending quote should not be escaped (which requires an even of trailing backslashes)
defp escape_dot_string(<<?\\, ?\\, rest::binary>>, acc) do
escape_dot_string(rest, <<acc::binary, ?\\, ?\\>>)
end

defp escape_dot_string(<<?", rest::binary>>, acc) do
escape_dot_string(rest, <<acc::binary, ?\\, ?">>)
end

defp escape_dot_string(<<?\\>>, acc) do
<<acc::binary, ?\\, ?\\, ?">>
end

defp escape_dot_string(<<char, rest::binary>>, acc) do
escape_dot_string(rest, <<acc::binary, char>>)
end

defp escape_dot_string(<<>>, acc) do
<<acc::binary, ?">>
end

@doc false
@deprecated "Use Macro.underscore/1 instead"
Expand Down
92 changes: 92 additions & 0 deletions lib/mix/test/mix/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,98 @@ defmodule Mix.UtilsTest do
end
end

describe "write_dot_graph!/4" do
test "preserves newlines and other control characters" do
in_tmp("dot_newlines", fn ->
callback = fn node -> {{node, nil}, []} end

Mix.Utils.write_dot_graph!("graph.dot", "graph", ["foo \nbar\r\nbaz"], callback, [])

assert File.read!("graph.dot") == """
digraph "graph" {
"foo
bar\r
baz"
}
"""
end)
end

test "quote and backslash combinations" do
in_tmp("dot_complex", fn ->
callback = fn node -> {{node, nil}, []} end

test_cases = [
# "fo"o" -> "fo\"o"
{"fo\"o", "fo\\\"o"},
# "fo\"o" -> "fo\\\"o"
{"fo\\\"o", "fo\\\\\"o"},
# "fo\o" -> "fo\o"
{"fo\\o", "fo\\o"},
# "fo\\o" -> "fo\\o"
{"fo\\\\o", "fo\\\\o"},
# "fo\\\o" -> "fo\\\o"
{"fo\\\\\\o", "fo\\\\\\o"}
]

Enum.each(test_cases, fn {input, expected} ->
Mix.Utils.write_dot_graph!("graph.dot", "graph", [input], callback, [])
content = File.read!("graph.dot")
assert content == "digraph \"graph\" {\n \"#{expected}\"\n}\n"
end)
end)
end

test "escapes backslash at end of string" do
in_tmp("dot_end_backslash", fn ->
callback = fn node -> {{node, nil}, []} end

test_cases = [
# "fo\" -> "fo\\" (add backslash)
{"fo\\", "fo\\\\"},
# "fo\\" -> "fo\\" (already valid)
{"fo\\\\", "fo\\\\"},
# "fo\\\" -> "fo\\\\" (add backslash)
{"fo\\\\\\", "fo\\\\\\\\"}
]

Enum.each(test_cases, fn {input, expected} ->
Mix.Utils.write_dot_graph!("graph.dot", "graph", [input], callback, [])
content = File.read!("graph.dot")
assert content == "digraph \"graph\" {\n \"#{expected}\"\n}\n"
end)
end)
end

test "handles empty strings" do
in_tmp("dot_empty", fn ->
callback = fn node -> {{node, nil}, []} end

Mix.Utils.write_dot_graph!("graph.dot", "graph", [""], callback, [])

assert File.read!("graph.dot") == """
digraph "graph" {
""
}
"""
end)
end

test "handles edge labels with escaping" do
in_tmp("dot_edge_labels", fn ->
callback = fn node -> {{node, "edge \"label\""}, []} end

Mix.Utils.write_dot_graph!("graph.dot", "graph", ["node"], callback, [])

assert File.read!("graph.dot") == """
digraph "graph" {
"node" [label="edge \\"label\\""]
}
"""
end)
end
end

defp assert_ebin_symlinked_or_copied(result) do
case result do
{:ok, paths} ->
Expand Down
Loading