Skip to content

Commit 8d6c4c0

Browse files
authored
Fix rustler_precompiled.download mix task - use correct function (#83)
* Fix rustler_precompiled.download mix task - use correct function There was a missing piece in the refactor to add support for headers in v0.8. This is a fix for #82 * Apply suggestions from code review
1 parent 47743ca commit 8d6c4c0

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix return of `available_nifs/1`. It was returning only URLs.
13+
14+
- Fix the "rustler_precompiled.download" mix task to use the correct
15+
function for the download of artifacts.
16+
17+
### Changed
18+
19+
- Print SHA256 of artifacts when downloading them from the mix task.
20+
1021
## [0.8.0] - 2024-08-28
1122

1223
### Added

lib/mix/tasks/rustler_precompiled.download.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@ defmodule Mix.Tasks.RustlerPrecompiled.Download do
5353
)
5454
end
5555

56-
urls =
56+
nifs_with_urls =
5757
cond do
5858
options[:all] ->
59-
RustlerPrecompiled.available_nif_urls(module)
59+
RustlerPrecompiled.available_nifs(module)
6060

6161
options[:only_local] ->
62-
RustlerPrecompiled.current_target_nif_urls(module)
62+
RustlerPrecompiled.current_target_nifs(module)
6363

6464
true ->
6565
raise "you need to specify either \"--all\" or \"--only-local\" flags"
6666
end
6767

68-
result = RustlerPrecompiled.download_nif_artifacts_with_checksums!(urls, options)
68+
result = RustlerPrecompiled.download_nif_artifacts_with_checksums!(nifs_with_urls, options)
6969

70-
if options[:print] do
70+
if Keyword.get(options, :print, true) do
7171
result
7272
|> Enum.map(fn map ->
7373
{Path.basename(Map.fetch!(map, :path)), Map.fetch!(map, :checksum)}

lib/rustler_precompiled.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ defmodule RustlerPrecompiled do
291291
nif_module
292292
|> metadata_file()
293293
|> read_map_from_file()
294-
|> nif_urls_from_metadata()
294+
|> nifs_from_metadata()
295295
|> case do
296-
{:ok, urls} ->
297-
urls
296+
{:ok, nifs_with_urls} ->
297+
nifs_with_urls
298298

299299
{:error, wrong_meta} ->
300300
raise "metadata about current target for the module #{inspect(nif_module)} is not available. " <>
@@ -956,12 +956,12 @@ defmodule RustlerPrecompiled do
956956
# Download a list of files from URLs and calculate its checksum.
957957
# Returns a list with details of the download and the checksum of each file.
958958
@doc false
959-
def download_nif_artifacts_with_checksums!(urls, options \\ []) do
959+
def download_nif_artifacts_with_checksums!(nifs_with_urls, options \\ []) do
960960
ignore_unavailable? = Keyword.get(options, :ignore_unavailable, false)
961961
attempts = max_retries(options)
962962

963963
download_results =
964-
for {lib_name, url} <- urls,
964+
for {lib_name, url} <- nifs_with_urls,
965965
do: {lib_name, with_retry(fn -> download_nif_artifact(url) end, attempts)}
966966

967967
cache_dir = cache_dir("precompiled_nifs")

test/rustler_precompiled_test.exs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,71 @@ defmodule RustlerPrecompiledTest do
968968
end
969969
end
970970

971+
describe "get NIFs from metadata and download them" do
972+
setup do
973+
root_path = File.cwd!()
974+
nif_fixtures_dir = Path.join(root_path, "test/fixtures")
975+
checksum_sample_file = Path.join(nif_fixtures_dir, "checksum-sample-file.exs")
976+
checksum_sample = File.read!(checksum_sample_file)
977+
978+
{:ok, nif_fixtures_dir: nif_fixtures_dir, checksum_sample: checksum_sample}
979+
end
980+
981+
@tag :tmp_dir
982+
test "downloading precompiled NIFs for publishing a package", %{
983+
tmp_dir: tmp_dir,
984+
checksum_sample: checksum_sample,
985+
nif_fixtures_dir: nif_fixtures_dir
986+
} do
987+
bypass = Bypass.open()
988+
989+
in_tmp(tmp_dir, fn ->
990+
File.write!("checksum-Elixir.RustlerPrecompilationExample.Native.exs", checksum_sample)
991+
992+
Bypass.expect_once(bypass, fn conn ->
993+
file_name = List.last(conn.path_info)
994+
file = File.read!(Path.join([nif_fixtures_dir, "precompiled_nifs", file_name]))
995+
996+
Plug.Conn.resp(conn, 200, file)
997+
end)
998+
999+
result =
1000+
capture_log(fn ->
1001+
config =
1002+
RustlerPrecompiled.Config.new(
1003+
otp_app: :rustler_precompiled,
1004+
module: RustlerPrecompilationExample.Native,
1005+
base_cache_dir: tmp_dir,
1006+
base_url: "http://localhost:#{bypass.port}/download",
1007+
version: "0.2.0",
1008+
crate: "example",
1009+
targets: @available_targets,
1010+
nif_versions: @default_nif_versions,
1011+
force_build: false
1012+
)
1013+
1014+
{:ok, metadata} = RustlerPrecompiled.build_metadata(config)
1015+
1016+
assert {:ok, nifs_with_urls} = RustlerPrecompiled.nifs_from_metadata(metadata)
1017+
1018+
assert [{lib_name, {url, []}} = first | _] = nifs_with_urls
1019+
1020+
assert "libexample" <> _ = lib_name
1021+
assert "http://localhost" <> _ = url
1022+
1023+
results =
1024+
RustlerPrecompiled.download_nif_artifacts_with_checksums!([first], all: true)
1025+
1026+
assert [%{path: _, lib_name: _, checksum: _, checksum_algo: _}] = results
1027+
end)
1028+
1029+
assert result =~ "Downloading"
1030+
assert result =~ "http://localhost:#{bypass.port}/download"
1031+
assert result =~ "NIF cached at"
1032+
end)
1033+
end
1034+
end
1035+
9711036
describe "nif_urls_from_metadata/1" do
9721037
test "builds a list of tar gz urls and its variants" do
9731038
base_url =

0 commit comments

Comments
 (0)