Skip to content
Merged
Changes from 1 commit
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
89 changes: 47 additions & 42 deletions lib/mix/test/mix/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,51 +178,56 @@ defmodule Mix.UtilsTest do
end
end

test "verify that write_according_to_opts/3 works as expected" do
# ignore any error from this call.

test_out = "test.out"
test_out_bak = "test.out.bak"
hello_world = "Hello World!"
new_hello_world = "New Hello World!"
default_out = "default.out"

# make sure none of the files exist - ignore errors
File.rm(test_out)
File.rm(test_out_bak)
File.rm(default_out)

# no optional override - write to the specified default file
assert Mix.Utils.write_according_to_opts!(test_out, [hello_world], []) == test_out
assert File.read!(test_out) == hello_world

# no optional override - write to the specified default file again, with old file backed up
assert Mix.Utils.write_according_to_opts!(test_out, [new_hello_world], []) == test_out
assert File.read!(test_out) == new_hello_world
assert File.read!(test_out_bak) == hello_world

# cleanup
File.rm!(test_out)
File.rm!(test_out_bak)

# with optional override - write to the specified default file
assert Mix.Utils.write_according_to_opts!(default_out, [hello_world], output: test_out) ==
test_out

assert File.read!(test_out) == hello_world

# with optional override - write to the specified default file again, with old file backed up
assert Mix.Utils.write_according_to_opts!(default_out, [new_hello_world], output: test_out) ==
test_out
describe "write_according_to_opts/3" do
test "verify that file writes with backups work as expected" do
test_out = "test.out"
test_out_bak = "test.out.bak"
hello_world = "Hello World!"
new_hello_world = "New Hello World!"
default_out = "default.out"

# ignore any error from this call.
in_tmp("write to default file", fn ->
# no optional override - write to the specified default file
assert Mix.Utils.write_according_to_opts!(test_out, [hello_world], []) == test_out
assert File.read!(test_out) == hello_world

# no optional override - write to the specified default file again, with old file backed up
assert Mix.Utils.write_according_to_opts!(test_out, [new_hello_world], []) == test_out
assert File.read!(test_out) == new_hello_world
assert File.read!(test_out_bak) == hello_world
end)

in_tmp("write to optional file override", fn ->
# with optional override - write to the specified default file
assert Mix.Utils.write_according_to_opts!(default_out, [hello_world], output: test_out) ==
test_out

assert File.read!(test_out) == hello_world

# with optional override - write to the specified default file again, with old file backed up
assert Mix.Utils.write_according_to_opts!(default_out, [new_hello_world],
output: test_out
) ==
test_out

assert File.read!(test_out) == new_hello_world
assert File.read!(test_out_bak) == hello_world
end)
end

assert File.read!(test_out) == new_hello_world
assert File.read!(test_out_bak) == hello_world
test "verify that writing to STDOUT works as expected" do
in_tmp("make sure we don't actually write to disk", fn ->
output =
ExUnit.CaptureIO.capture_io(fn ->
Mix.Utils.write_according_to_opts!("the_file.txt", ["some text"], output: "-")
end)

# cleanup again
File.rm!(test_out)
File.rm!(test_out_bak)
assert output == "some text"

# we don't test the handling of "-" here, we test it implicitly in xref_test.exs
assert File.exists?("the_file.txt") == false
end)
end
end

defp assert_ebin_symlinked_or_copied(result) do
Expand Down