Skip to content

Commit

Permalink
Support :warn_if_outdated in deps
Browse files Browse the repository at this point in the history
Example:

    $ mix deps.get
    Resolving Hex dependencies...
    Resolution completed in 0.0s
    Unchanged:
      ecto 3.3.1
      ecto_sql 3.3.2
      ex_doc 0.1.0
      postgrex 0.2.1
    warning: the following deps set `warn_if_outdated: true` and are outdated:

     * ecto 3.3.2 is available
     * ecto_sql 3.3.3 is available
  • Loading branch information
wojtekmach committed Jan 13, 2025
1 parent cc4d87a commit 261583d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/hex/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ defmodule Hex.Mix do
requirement: dep.requirement,
app: Atom.to_string(dep.app),
from: Path.relative_to_cwd(dep.from),
dependencies: []
dependencies: [],
warn_if_outdated: dep.opts[:warn_if_outdated]
}
]
else
Expand Down
33 changes: 33 additions & 0 deletions lib/hex/remote_converger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,39 @@ defmodule Hex.RemoteConverger do
verify_resolved(resolved, old_lock)
new_lock = Hex.Mix.to_lock(resolved)
Hex.SCM.prefetch(new_lock)

deps_to_warn =
for %{repo: repo, name: name, requirement: requirement, warn_if_outdated: true} <- requests do
requirement = Version.parse_requirement!(requirement)
{:ok, versions} = Registry.versions(repo, name)

latest_version =
versions
|> Enum.filter(&Version.match?(&1, requirement))
|> Enum.sort({:desc, Version})
|> List.first()

{:hex, _name, version, _chhecksum, _managers, _, ^repo, _checksum} =
Map.fetch!(new_lock, String.to_atom(name))

if Version.compare(latest_version, version) == :gt do
{name, latest_version}
end
end
|> Enum.filter(& &1)

if deps_to_warn != [] do
IO.warn(
[
"the following deps set `warn_if_outdated: true` and are outdated:\n\n",
Enum.map_join(deps_to_warn, "\n", fn {name, version} ->
" * #{name} #{version} is available"
end)
],
[]
)
end

lock_merge(lock, new_lock)
end

Expand Down
50 changes: 50 additions & 0 deletions test/hex/remote_converger_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule Hex.RemoteConvergetTest do
use HexTest.IntegrationCase

defmodule OutdatedDepsBefore.MixProject do
def project do
[
app: :outdated_deps,
version: "0.1.0",
deps: [
{:postgrex, "0.2.1"},
{:ecto, "3.3.1"},
{:ecto_sql, "3.3.2"}
]
]
end
end

defmodule OutdatedDepsAfter.MixProject do
def project do
[
app: :outdated_deps,
version: "0.1.0",
deps: [
{:postgrex, ">= 0.0.0", warn_if_outdated: true},
{:ecto, ">= 0.0.0", warn_if_outdated: true},
{:ecto_sql, ">= 0.0.0", warn_if_outdated: true}
]
]
end
end

test "deps with warn_if_outdated: true" do

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (21.3, 1.8.1)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (21.3, 1.7.2)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (21.3, 1.6.6)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (22.3, 1.9.4)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (21.3, 1.7.2)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (21.3, 1.6.6)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (21.3, 1.8.1)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)

Check failure on line 32 in test/hex/remote_converger_test.exs

View workflow job for this annotation

GitHub Actions / Test (22.3, 1.9.4)

test deps with warn_if_outdated: true (Hex.RemoteConvergetTest)
in_tmp(fn ->
Mix.Project.push(OutdatedDepsBefore.MixProject)
:ok = Mix.Tasks.Deps.Get.run([])

Mix.Project.pop()
Mix.Project.push(OutdatedDepsAfter.MixProject)

output =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
:ok = Mix.Tasks.Deps.Get.run([])
end)

assert output =~ "ecto 3.3.2 is available"
assert output =~ "ecto_sql 3.3.3 is available"
refute output =~ "postgrex"
end)
end
end

0 comments on commit 261583d

Please sign in to comment.