Skip to content

Commit

Permalink
Support :warn_if_outdated in deps (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach authored Jan 21, 2025
1 parent 96c8f54 commit f963a8f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:

env:
HEXPM_OTP: OTP-27.2
HEXPM_ELIXIR: v1.17.3
HEXPM_ELIXIR: v1.18.1
HEXPM_PATH: hexpm
HEXPM_ELIXIR_PATH: hexpm_elixir
HEXPM_OTP_PATH: hexpm_otp
Expand Down
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
35 changes: 35 additions & 0 deletions lib/hex/remote_converger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ 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
{:ok, requirement} = Version.parse_requirement(requirement)
{:ok, versions} = Registry.versions(repo, name)

versions =
versions
|> Enum.filter(fn vsn -> vsn.pre == [] and Version.match?(vsn, requirement) end)
|> Enum.sort(&(Version.compare(&1, &2) == :gt))

with [latest_version | _] <- versions,
{:hex, _name, version, _checksum1, _managers, _, ^repo, _checksum2} <-
Map.fetch!(new_lock, String.to_atom(name)),
:gt <- Version.compare(latest_version, version) do
{name, latest_version}
else
_ ->
nil
end
end
|> Enum.filter(& &1)

if deps_to_warn != [] do
IO.warn(
[
"the following deps are outdated and set \"warn_if_outdated: true\":\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
2 changes: 1 addition & 1 deletion lib/hex/scm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule Hex.SCM do
mix_keys = ~w[app env compile optional only targets override manager runtime system_env]a

# https://hex.pm/docs/usage#options
hex_keys = ~w[hex repo organization]a
hex_keys = ~w[hex repo organization warn_if_outdated]a

internal_keys = ~w[dest lock build]a

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", warn_if_outdated: true},
{:ecto, "3.3.1", warn_if_outdated: true},
{:ecto_sql, "3.3.2", warn_if_outdated: true}
]
]
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
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 f963a8f

Please sign in to comment.