Skip to content

Commit

Permalink
Simplify version matching code
Browse files Browse the repository at this point in the history
This slightly modifies the semantics for the sake of replacing a
slightly non-obvious function with a call to Version.match?/3. It also
fixes a credo warning about Enum.filter that would have added a couple
lines of code to fix.

The end result is that if you're on a prerelease, you'll get notified to
upgrade to a later prerelease even if it's not the same major, minor,
patch version. This seems like a highly unlikely use case given our
history of releases with this project.
  • Loading branch information
fhunleth committed Jan 27, 2025
1 parent 0fa91e6 commit 81c83a1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Run `mix dialyzer --format short` for strings
[
{"lib/nerves_bootstrap/update_checker.ex:7:9:unknown_function Function Hex.start/0 does not exist."},
{"lib/nerves_bootstrap/update_checker.ex:8:45:unknown_function Function Hex.API.Package.get/2 does not exist."}
{"lib/nerves_bootstrap/update_checker.ex:9:9:unknown_function Function Hex.start/0 does not exist."},
{"lib/nerves_bootstrap/update_checker.ex:10:45:unknown_function Function Hex.API.Package.get/2 does not exist."}
]
26 changes: 6 additions & 20 deletions lib/nerves_bootstrap/update_checker.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Nerves.Bootstrap.UpdateChecker do
@moduledoc false

@doc """
Check for a nerves_bootstrap release
"""
Expand Down Expand Up @@ -30,9 +32,11 @@ defmodule Nerves.Bootstrap.UpdateChecker do

@spec select_update([Version.t()], Version.t()) :: Version.t() | nil
def select_update(releases, current_version) do
req = Version.parse_requirement!("> #{current_version}")
allow_pre = current_version.pre != []

releases
|> Enum.filter(&(Version.compare(&1, current_version) == :gt))
|> Enum.filter(pre_release_filter(current_version))
|> Enum.filter(&Version.match?(&1, req, allow_pre: allow_pre))
|> Enum.sort(&(Version.compare(&1, &2) == :gt))
|> List.first()
end
Expand All @@ -58,22 +62,4 @@ defmodule Nerves.Bootstrap.UpdateChecker do
:reset
])
end

# Return a function that filters releases based on whether the current version is a pre-release
defp pre_release_filter(%{pre: []}) do
&(Map.get(&1, :pre) == [])
end

defp pre_release_filter(%{major: major, minor: minor, patch: patch}) do
fn
%{pre: []} ->
true

%{major: ^major, minor: ^minor, patch: ^patch} ->
true

_ ->
false
end
end
end
5 changes: 2 additions & 3 deletions test/nerves_bootstrap/update_checker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ defmodule UpdateChecker.UpdateCheckerTest do
current_version = Version.parse!("1.0.0-rc.0")

releases = [
"1.1.0-rc.0",
"1.0.0-rc.1",
"0.8.2",
"1.0.0-rc.0",
"0.8.1"
"0.8.1",
"1.0.0-rc.1"
]

releases = Enum.map(releases, &Version.parse!/1)
Expand Down

0 comments on commit 81c83a1

Please sign in to comment.