Skip to content

Commit

Permalink
fix: finally fix the sorted option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Timmer committed May 15, 2024
1 parent 8bf8f7e commit 9afea6f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 31 deletions.
5 changes: 4 additions & 1 deletion lib/mix_edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ defmodule MixEdit do

deps =
if sorted do
Enum.sort_by(deps, fn {_, _, [{{_, _, [dep]}, _}]} -> dep end)
Enum.sort_by(deps, fn
{_, _, [{{_, _, [dep]}, _}]} -> dep
{_, _, [{_, _, [dep]}, _, _]} -> dep
end)
else
deps
end
Expand Down
96 changes: 66 additions & 30 deletions test/mix_add_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,37 +203,77 @@ defmodule MixEditTest do
end
end

test "sorting the dependency list works" do
deps = """
defmodule Test.MixProject do
use Mix.Project
def project do
[
version: "0.1.0",
start_permanent: Mix.env() == :prod,
deps: deps()
]
describe "sorting the dependency list works" do
test "simple" do
mix_project = """
defmodule Test.MixProject do
use Mix.Project
def project do
[
version: "0.1.0",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
defp deps do
[
{:jason, "~> 0.0.0"},
{:ex_doc, ">= 0.0.0"},
{:sweet_xml, ">= 0.0.0"}
]
end
end
"""

defp deps do
[
{:jason, "~> 0.0.0"},
{:ex_doc, ">= 0.0.0"},
{:sweet_xml, ">= 0.0.0"}
]
assert mix_project_sort(mix_project, testing: [version: ">= 0.0.0"]) =~
"defp deps do [{:ex_doc, \">= 0.0.0\"}, {:jason, \"~> 0.0.0\"}, {:sweet_xml, \">= 0.0.0\"}, {:testing, \">= 0.0.0\"}] end"
end

test "extra tags" do
mix_project = """
defmodule MoreTags.MixProject do
use Mix.Project
def project do
[
version: "0.1.0",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:jason, "~> 1.0"},
{:credo, "~> 1.7.1", only: [:dev]},
{:dialyxir, "~> 1.4", only: [:dev], runtime: false},
{:excoveralls, "~> 0.17", only: [:dev, :test]},
]
end
end
"""

assert mix_project_sort(mix_project, testing: [version: ">= 0.0.0"]) =~
Enum.join([
"deps do [{:credo, \"~> 1.7.1\", only: [:dev]}, {:dialyxir, \"~> 1.4\", only: [:dev], runtime: false}, ",
"{:excoveralls, \"~> 0.17\", only: [:dev, :test]}, {:jason, \"~> 1.0\"}, {:testing, \">= 0.0.0\"}] end"
])
end
"""
end

defp mix_project_sort(mix_project, deps) do
opts = %{
dependencies: [testing: [version: ">= 0.0.0"]],
dependencies: deps,
sorted: true,
app: :test,
method: :add
}

{quoted, comments} = MixEdit.quote_string!(deps)
{quoted, comments} = MixEdit.quote_string!(mix_project)

{new_file, info} =
Macro.prewalk(
Expand All @@ -244,16 +284,12 @@ defmodule MixEditTest do

assert {:add, [testing: [version: ">= 0.0.0"]]} == info

stringified =
new_file
|> Code.Formatter.to_algebra(comments: comments)
|> Inspect.Algebra.format(:infinity)
|> IO.iodata_to_binary()
|> String.replace("\n", " ")
|> String.replace(~r/\s+/, " ")

assert stringified =~
"defp deps do [{:ex_doc, \">= 0.0.0\"}, {:jason, \"~> 0.0.0\"}, {:sweet_xml, \">= 0.0.0\"}, {:testing, \">= 0.0.0\"}] end"
new_file
|> Code.Formatter.to_algebra(comments: comments)
|> Inspect.Algebra.format(:infinity)
|> IO.iodata_to_binary()
|> String.replace("\n", " ")
|> String.replace(~r/\s+/, " ")
end

defp unwrap_deps({{:__block__, _, [deps]}, _}), do: deps
Expand Down

0 comments on commit 9afea6f

Please sign in to comment.