Skip to content

Commit

Permalink
Run merge for mix gettext.extract pot files even if they are unchanged (
Browse files Browse the repository at this point in the history
#385)

Resolves #377
  • Loading branch information
maennchen authored Dec 8, 2023
1 parent e508160 commit 0a19236
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/gettext/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ defmodule Gettext.Extractor do
# %{path => {:merged, :unchanged | %Messages{}}, path => {:unmerged, :unchanged | %Messages{}}, path => {:new, %Messages{}}}
Map.merge(pot_files, po_structs, &merge_existing_and_extracted(&1, &2, &3, gettext_config))
|> Enum.map(&tag_files(&1, gettext_config))
|> Enum.reject(&match?({_, {_, :unchanged}}, &1))
|> Enum.map(&dump_tagged_file/1)
end

Expand Down Expand Up @@ -273,7 +272,8 @@ defmodule Gettext.Extractor do
# This function "dumps" merged files and unmerged files without any changes,
# and dumps new POT files adding an informative comment to them. This doesn't
# write anything to disk, it just returns `{path, contents}` tuples.
defp dump_tagged_file({path, {_tag, po}}), do: {path, PO.compose(po)}
defp dump_tagged_file({path, {_tag, :unchanged}}), do: {path, :unchanged}
defp dump_tagged_file({path, {_tag, po}}), do: {path, {:changed, PO.compose(po)}}

defp prune_unmerged(path, gettext_config) do
merge_or_unchanged(path, %Messages{messages: []}, gettext_config)
Expand Down
6 changes: 3 additions & 3 deletions lib/mix/tasks/gettext.extract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule Mix.Tasks.Gettext.Extract do
end

defp run_message_extraction(pot_files, opts, args) do
for {path, contents} <- pot_files do
for {path, {:changed, contents}} <- pot_files do
File.mkdir_p!(Path.dirname(path))
File.write!(path, contents)
Mix.shell().info("Extracted #{Path.relative_to_cwd(path)}")
Expand All @@ -89,9 +89,9 @@ defmodule Mix.Tasks.Gettext.Extract do
end

defp run_up_to_date_check(pot_files) do
not_extracted_paths = for {path, _contents} <- pot_files, do: path
not_extracted_paths = for {path, {:changed, _contents}} <- pot_files, do: path

if pot_files == [] do
if not_extracted_paths == [] do
:ok
else
Mix.raise("""
Expand Down
12 changes: 8 additions & 4 deletions test/gettext/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ defmodule Gettext.ExtractorTest do
Extractor.merge_pot_files(extracted_po_structs, [paths.tomerge, paths.ignored], [])

# Unchanged files are not returned
assert List.keyfind(structs, paths.ignored, 0) == nil
assert {_path, :unchanged} = List.keyfind(structs, paths.ignored, 0)

{_, contents} = List.keyfind(structs, paths.tomerge, 0)
assert {_, {:changed, contents}} = List.keyfind(structs, paths.tomerge, 0)

assert IO.iodata_to_binary(contents) == """
msgid "foo"
Expand All @@ -45,7 +45,7 @@ defmodule Gettext.ExtractorTest do
msgstr ""
"""

{_, contents} = List.keyfind(structs, paths.new, 0)
assert {_, {:changed, contents}} = List.keyfind(structs, paths.new, 0)
contents = IO.iodata_to_binary(contents)

assert contents =~ """
Expand Down Expand Up @@ -385,7 +385,11 @@ defmodule Gettext.ExtractorTest do
assert [] = Extractor.pot_files(:unknown, [])

pot_files = Extractor.pot_files(:test_application, [])
dumped = Enum.map(pot_files, fn {k, v} -> {k, IO.iodata_to_binary(v)} end)

dumped =
pot_files
|> Enum.reject(&match?({_path, :unchanged}, &1))
|> Enum.map(fn {k, {:changed, v}} -> {k, IO.iodata_to_binary(v)} end)

# We check that dumped strings end with the `expected` string because
# there's the informative comment at the start of each dumped string.
Expand Down
4 changes: 4 additions & 0 deletions test/mix/tasks/gettext.extract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
msgid "other"
msgstr ""
"""

capture_io(fn ->
Mix.Project.in_project(test, tmp_dir, fn _module -> run(["--merge"]) end)
end) =~ "Wrote priv/gettext/it/LC_MESSAGES/my_domain.po"
end

test "--check-up-to-date should fail if no POT files have been created",
Expand Down

0 comments on commit 0a19236

Please sign in to comment.