Skip to content

Commit

Permalink
Use ExUnit's tmp_dir when possible (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide authored Jan 21, 2023
1 parent e5ba065 commit a5ef194
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 147 deletions.
18 changes: 9 additions & 9 deletions test/gettext/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ defmodule Gettext.ExtractorTest do
alias Expo.Messages
alias Gettext.Extractor

@pot_path "../../tmp/" |> Path.expand(__DIR__) |> Path.relative_to_cwd()

describe "merge_pot_files/2" do
test "merges two POT files" do
@tag :tmp_dir
test "merges two POT files", %{tmp_dir: tmp_dir} do
paths = %{
tomerge: Path.join(@pot_path, "tomerge.pot"),
ignored: Path.join(@pot_path, "ignored.pot"),
new: Path.join(@pot_path, "new.pot")
tomerge: Path.join(tmp_dir, "tomerge.pot"),
ignored: Path.join(tmp_dir, "ignored.pot"),
new: Path.join(tmp_dir, "new.pot")
}

extracted_po_structs = [
Expand Down Expand Up @@ -55,8 +54,9 @@ defmodule Gettext.ExtractorTest do
"""
end

test "reports the filename if syntax error" do
path = Path.join(@pot_path, "syntax_error.pot")
@tag :tmp_dir
test "reports the filename if syntax error", %{tmp_dir: tmp_dir} do
path = Path.join(tmp_dir, "syntax_error.pot")

write_file(path, """
msgid "foo"
Expand All @@ -65,7 +65,7 @@ defmodule Gettext.ExtractorTest do
msgstr ""
""")

message = "tmp/syntax_error.pot:3: syntax error before: msgid"
message = ~r/syntax_error\.pot:3: syntax error before: msgid/

assert_raise Expo.PO.SyntaxError, message, fn ->
Extractor.merge_pot_files([{path, %Messages{messages: []}}], [path], [])
Expand Down
8 changes: 4 additions & 4 deletions test/gettext/merger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule Gettext.MergerTest do

@opts fuzzy: true, fuzzy_threshold: 0.8
@autogenerated_flags [["elixir-format"]]
@pot_path "../../tmp/" |> Path.expand(__DIR__) |> Path.relative_to_cwd()

describe "merge/2" do
test "headers from the old file are kept" do
Expand Down Expand Up @@ -535,9 +534,10 @@ defmodule Gettext.MergerTest do
end
end

test "new_po_file/2" do
pot_path = Path.join(@pot_path, "new_po_file.pot")
new_po_path = Path.join(@pot_path, "it/LC_MESSAGES/new_po_file.po")
@tag :tmp_dir
test "new_po_file/2", %{tmp_dir: tmp_dir} do
pot_path = Path.join(tmp_dir, "new_po_file.pot")
new_po_path = Path.join(tmp_dir, "it/LC_MESSAGES/new_po_file.po")

write_file(pot_path, """
## Stripme!
Expand Down
81 changes: 37 additions & 44 deletions test/mix/tasks/gettext.extract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ defmodule Mix.Tasks.Gettext.ExtractTest do

import ExUnit.CaptureIO

@priv_path "../../../tmp/gettext.extract" |> Path.expand(__DIR__) |> Path.relative_to_cwd()
@moduletag :tmp_dir

setup_all do
# To suppress the `redefining module MyApp` warnings for the test modules
Code.compiler_options(ignore_module_conflict: true)
:ok
end

setup do
File.rm_rf!(tmp_path("/"))
test "extracting and extracting with --merge", %{test: test, tmp_dir: tmp_dir} = context do
create_test_mix_file(context)

:ok
end

test "extracting and extracting with --merge", %{test: test} do
create_test_mix_file(test)

write_file("lib/my_app.ex", """
write_file(context, "lib/my_app.ex", """
defmodule MyApp.Gettext do
use Gettext, otp_app: #{inspect(test)}
end
Expand All @@ -33,12 +27,12 @@ defmodule Mix.Tasks.Gettext.ExtractTest do

output =
capture_io(fn ->
Mix.Project.in_project(test, tmp_path("/"), fn _module -> run([]) end)
Mix.Project.in_project(test, tmp_dir, fn _module -> run([]) end)
end)

assert output =~ "Extracted priv/gettext/default.pot"

assert read_file("priv/gettext/default.pot") =~ """
assert read_file(context, "priv/gettext/default.pot") =~ """
#: lib/my_app.ex:7
#, elixir-autogen, elixir-format
msgid "hello"
Expand All @@ -47,31 +41,32 @@ defmodule Mix.Tasks.Gettext.ExtractTest do

# Test --merge too.

write_file("lib/other.ex", """
write_file(context, "lib/other.ex", """
defmodule MyApp.Other do
require MyApp.Gettext
def foo(), do: MyApp.Gettext.dgettext("my_domain", "other")
end
""")

write_file("priv/gettext/it/LC_MESSAGES/my_domain.po", "")
write_file(context, "priv/gettext/it/LC_MESSAGES/my_domain.po", "")

capture_io(fn ->
Mix.Project.in_project(test, tmp_path("/"), fn _module -> run(["--merge"]) end)
Mix.Project.in_project(test, tmp_dir, fn _module -> run(["--merge"]) end)
end)

assert read_file("priv/gettext/it/LC_MESSAGES/my_domain.po") == """
assert read_file(context, "priv/gettext/it/LC_MESSAGES/my_domain.po") == """
#: lib/other.ex:3
#, elixir-autogen, elixir-format
msgid "other"
msgstr ""
"""
end

test "--check-up-to-date should fail if no POT files have been created", %{test: test} do
create_test_mix_file(test)
test "--check-up-to-date should fail if no POT files have been created",
%{test: test, tmp_dir: tmp_dir} = context do
create_test_mix_file(context)

write_file("lib/my_app.ex", """
write_file(context, "lib/my_app.ex", """
defmodule MyApp.Gettext do
use Gettext, otp_app: #{inspect(test)}
end
Expand All @@ -82,7 +77,7 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
end
""")

write_file("lib/other.ex", """
write_file(context, "lib/other.ex", """
defmodule MyApp.Other do
require MyApp.Gettext
def foo(), do: MyApp.Gettext.dgettext("my_domain", "other")
Expand All @@ -99,17 +94,18 @@ defmodule Mix.Tasks.Gettext.ExtractTest do

capture_io(fn ->
assert_raise Mix.Error, expected_message, fn ->
Mix.Project.in_project(test, tmp_path("/"), fn _module ->
Mix.Project.in_project(test, tmp_dir, fn _module ->
run(["--check-up-to-date"])
end)
end
end)
end

test "--check-up-to-date should pass if nothing changed", %{test: test} do
create_test_mix_file(test, write_reference_comments: false)
test "--check-up-to-date should pass if nothing changed",
%{test: test, tmp_dir: tmp_dir} = context do
create_test_mix_file(context, write_reference_comments: false)

write_file("lib/my_app.ex", """
write_file(context, "lib/my_app.ex", """
defmodule MyApp.Gettext do
use Gettext, otp_app: #{inspect(test)}
end
Expand All @@ -121,20 +117,21 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
""")

capture_io(fn ->
Mix.Project.in_project(test, tmp_path("/"), fn _module ->
Mix.Project.in_project(test, tmp_dir, fn _module ->
run([])
end)

Mix.Project.in_project(test, tmp_path("/"), fn _module ->
Mix.Project.in_project(test, tmp_dir, fn _module ->
run(["--check-up-to-date"])
end)
end)
end

test "--check-up-to-date should fail if POT files are outdated", %{test: test} do
create_test_mix_file(test)
test "--check-up-to-date should fail if POT files are outdated",
%{test: test, tmp_dir: tmp_dir} = context do
create_test_mix_file(context)

write_file("lib/my_app.ex", """
write_file(context, "lib/my_app.ex", """
defmodule MyApp.Gettext do
use Gettext, otp_app: #{inspect(test)}
end
Expand All @@ -145,18 +142,18 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
end
""")

write_file("lib/other.ex", """
write_file(context, "lib/other.ex", """
defmodule MyApp.Other do
require MyApp.Gettext
def foo(), do: MyApp.Gettext.dgettext("my_domain", "other")
end
""")

capture_io(fn ->
Mix.Project.in_project(test, tmp_path("/"), fn _module -> run([]) end)
Mix.Project.in_project(test, tmp_dir, fn _module -> run([]) end)
end)

write_file("lib/my_app.ex", """
write_file(context, "lib/my_app.ex", """
defmodule MyApp.Gettext do
use Gettext, otp_app: #{inspect(test)}
end
Expand All @@ -176,20 +173,20 @@ defmodule Mix.Tasks.Gettext.ExtractTest do

capture_io(fn ->
assert_raise Mix.Error, expected_message, fn ->
Mix.Project.in_project(test, tmp_path("/"), fn _module ->
Mix.Project.in_project(test, tmp_dir, fn _module ->
run(["--check-up-to-date"])
end)
end
end)
end

defp create_test_mix_file(test, gettext_config \\ []) do
write_file("mix.exs", """
defp create_test_mix_file(context, gettext_config \\ []) do
write_file(context, "mix.exs", """
defmodule MyApp.MixProject do
use Mix.Project
def project() do
[app: #{inspect(test)}, version: "0.1.0", gettext: #{inspect(gettext_config)}]
[app: #{inspect(context.test)}, version: "0.1.0", gettext: #{inspect(gettext_config)}]
end
def application() do
Expand All @@ -199,18 +196,14 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
""")
end

defp write_file(path, contents) do
path = tmp_path(path)
defp write_file(context, path, contents) do
path = Path.join(context.tmp_dir, path)
File.mkdir_p!(Path.dirname(path))
File.write!(path, contents)
end

defp read_file(path) do
path |> tmp_path() |> File.read!()
end

defp tmp_path(path) do
Path.join(@priv_path, path)
defp read_file(context, path) do
context.tmp_dir |> Path.join(path) |> File.read!()
end

defp run(args) do
Expand Down
Loading

0 comments on commit a5ef194

Please sign in to comment.