Skip to content

Commit

Permalink
VIP - update deps and add hex_core for test and fix some tests
Browse files Browse the repository at this point in the history
new update breaks my test based on wojtekmach/req@84308db

based on wojtekmach/req#375
  • Loading branch information
shahryarjb committed Jun 24, 2024
1 parent 087fe9c commit c5991c6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 57 deletions.
28 changes: 19 additions & 9 deletions lib/installer/downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,32 @@ defmodule MishkaInstaller.Installer.Downloader do
"""
@spec download(download_type, pkg) :: okey_return | error_return
def download(:hex, %{app: app, tag: tag_name}) when not is_nil(tag_name) do
case build_url("https://repo.hex.pm/tarballs/#{app}-#{tag_name}.tar", decode_body: false) do
case build_url("https://repo.hex.pm/tarballs/#{app}-#{tag_name}.tar") do
%Req.Response{status: 200, body: body} ->
case :erl_tar.extract({:binary, body}, [:memory]) do
{:ok, files} ->
converted = Map.new(files, fn {key, value} -> {to_string(key), value} end)
{:ok, converted["contents.tar.gz"]}

{:error, _reason} ->
mix_global_err("The format of this file is not correct. Use standard format.")
end
converted = Map.new(body, fn {key, value} -> {to_string(key), value} end)
{:ok, converted["contents.tar.gz"]}

_ ->
mix_global_err()
end
end

# def download(:hex, %{app: app, tag: tag_name}) when not is_nil(tag_name) do
# case build_url("https://repo.hex.pm/tarballs/#{app}-#{tag_name}.tar", decode_body: false) do
# %Req.Response{status: 200, body: body} ->
# case :erl_tar.extract({:binary, body}, [:memory]) do
# {:ok, files} ->
# converted = Map.new(files, fn {key, value} -> {to_string(key), value} end)
# {:ok, converted["contents.tar.gz"]}

# {:error, _reason} ->
# mix_global_err("The format of this file is not correct. Use standard format.")
# end

# _ ->
# mix_global_err()
# end
# end
def download(:github, %{path: path, branch: {branch, git: true}}) do
case build_url(
"#{@github_codeload_path}/#{String.trim(path)}/legacy.tar.gz/refs/heads/#{branch}"
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ defmodule MishkaInstaller.MixProject do
defp deps do
[
{:phoenix_pubsub, "~> 2.1.3"},
{:req, "~> 0.5.0"},
{:req, "~> 0.5.1"},
{:plug, "~> 1.16"},

# Extra tools
{:mishka_developer_tools, github: "mishka-group/mishka_developer_tools"},
{:telemetry, "~> 1.2.1"},

# Dev and Test dependencies
{:ex_doc, "~> 0.34.0", only: :dev, runtime: false}
{:ex_doc, "~> 0.34.0", only: :dev, runtime: false},
{:hex_core, "~> 0.10.2", only: :test}
]
end

Expand Down
5 changes: 3 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.34.0", "ab95e0775db3df71d30cf8d78728dd9261c355c81382bcd4cefdc74610bef13e", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "60734fb4c1353f270c3286df4a0d51e65a2c1d9fba66af3940847cc65a8066d7"},
"finch": {:hex, :finch, "0.18.0", "944ac7d34d0bd2ac8998f79f7a811b21d87d911e77a786bc5810adb75632ada4", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "69f5045b042e531e53edc2574f15e25e735b522c37e2ddb766e15b979e03aa65"},
"hex_core": {:hex, :hex_core, "0.10.2", "1a80c487d0ebf57e6371721335f37689773f4df477e04bc2e962226f32d280a7", [:rebar3], [], "hexpm", "e4de8b979548637ae9e2e4d6363a1ca114488617cf7e11af3dd98aa333d596d2"},
"hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
Expand All @@ -17,8 +18,8 @@
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
"plug": {:hex, :plug, "1.16.0", "1d07d50cb9bb05097fdf187b31cf087c7297aafc3fed8299aac79c128a707e47", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cbf53aa1f5c4d758a7559c0bd6d59e286c2be0c6a1fac8cc3eee2f638243b93e"},
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
"req": {:hex, :req, "0.5.0", "6d8a77c25cfc03e06a439fb12ffb51beade53e3fe0e2c5e362899a18b50298b3", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "dda04878c1396eebbfdec6db6f3d4ca609e5c8846b7ee88cc56eb9891406f7a3"},
"req": {:hex, :req, "0.5.1", "90584216d064389a4ff2d4279fe2c11ff6c812ab00fa01a9fb9d15457f65ba70", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "7ea96a1a95388eb0fefa92d89466cdfedba24032794e5c1147d78ec90db7edca"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
}
119 changes: 75 additions & 44 deletions test/installer/downloader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,104 +4,111 @@ defmodule MishkaInstallerTest.Installer.DownloaderTest do

setup do
Application.put_env(:mishka_installer, :downloader_req_options, plug: {Req.Test, Downloader})
:ok

{:ok, %{tarball: tarball}} =
:hex_tarball.create(
%{
"name" => "foo",
"version" => "1.0.0"
},
[
{~c"mix.exs",
"""
defmodule Foo.MixProject do
use Mix.Project
def project do
[app: :foo, version: "1.0.0"]
end
end
"""}
]
)

%{tarball: tarball}
end

describe "Download Mock Test ===>" do
test "Downloade hex with version" do
tar_file_path = "test/support/mishka_developer_tools-0.1.5.tar"
tar_binary = File.read!(tar_file_path)
test "Downloade hex with version with creating tarball", %{tarball: tarball} do
tar_expect_200(tarball)

Req.Test.expect(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream")
|> Plug.Conn.put_resp_header(
"content-disposition",
"attachment; filename=\"#{Path.basename(tar_file_path)}\""
)
|> Plug.Conn.send_resp(200, tar_binary)
end)
{:ok, _body} = Downloader.download(:hex, %{app: "mishka_installer", tag: "0.0.4"})

tar_expect_400(tarball)

{:error, _error} =
assert Downloader.download(:hex, %{app: "mishka_installer", tag: "0.0.4"})
end

test "Downloade hex with version", %{tarball: tarball} do
tar_expect_200(tarball)

{:ok, _body} = Downloader.download(:hex, %{app: "mishka_installer", tag: "0.0.4"})

Req.Test.expect(Downloader, &Plug.Conn.send_resp(&1, 400, "file body"))
tar_expect_400(tarball)

{:error, _error} =
assert Downloader.download(:hex, %{app: "mishka_installer", tag: "0.0.4"})
end

test "Downloade github branch" do
Req.Test.stub(Downloader, fn conn ->
Req.Test.text(conn, "file body")
end)
test "Downloade github branch", %{tarball: tarball} do
tar_stub_200(tarball)

{:ok, "file body"} =
{:ok, _body} =
assert Downloader.download(:github, %{
path: "mishka_installer",
branch: {"master", git: true}
})

{:ok, "file body"} =
{:ok, _body} =
assert Downloader.download(:github, %{path: "mishka_installer", branch: "master"})

Req.Test.expect(Downloader, &Plug.Conn.send_resp(&1, 400, "file body"))
Req.Test.expect(Downloader, &Plug.Conn.send_resp(&1, 400, "file body"))
tar_expect_400(tarball)

{:error, _error} =
assert Downloader.download(:github, %{
path: "mishka_installer",
branch: {"master", git: true}
})

tar_expect_400(tarball)

{:error, _error} =
assert Downloader.download(:github, %{path: "mishka_installer", branch: "master"})
end

test "Downloade github release/tag" do
Req.Test.stub(Downloader, fn conn ->
Req.Test.text(conn, "file body")
end)
test "Downloade github release/tag", %{tarball: tarball} do
tar_stub_200(tarball)

{:ok, "file body"} =
{:ok, _body} =
assert Downloader.download(:github, %{path: "mishka_installer", release: "0.0.4"})

{:ok, "file body"} =
{:ok, _body} =
assert Downloader.download(:github, %{path: "mishka_installer", tag: "0.0.4"})

Req.Test.expect(Downloader, &Plug.Conn.send_resp(&1, 400, "file body"))
Req.Test.expect(Downloader, &Plug.Conn.send_resp(&1, 400, "file body"))
tar_expect_400(tarball)

{:error, _error} =
assert Downloader.download(:github, %{path: "mishka_installer", release: "0.0.4"})

tar_expect_400(tarball)

{:error, _error} =
assert Downloader.download(:github, %{path: "mishka_installer", tag: "0.0.4"})
end

test "Download hex without version" do
test "Download hex without version", %{tarball: tarball} do
Req.Test.expect(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.send_resp(200, Jason.encode!(%{"latest_stable_version" => "0.0.4"}))
end)

tar_file_path = "test/support/mishka_developer_tools-0.1.5.tar"
tar_binary = File.read!(tar_file_path)

Req.Test.expect(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream")
|> Plug.Conn.put_resp_header(
"content-disposition",
"attachment; filename=\"#{Path.basename(tar_file_path)}\""
)
|> Plug.Conn.send_resp(200, tar_binary)
end)
tar_expect_200(tarball)

{:ok, _body} =
assert Downloader.download(:hex, %{app: "mishka_installer"})

Req.Test.expect(Downloader, &Plug.Conn.send_resp(&1, 400, "file body"))
tar_expect_400(tarball)

{:error, _error} = assert Downloader.download(:hex, %{app: "mishka_installer"})
end
Expand Down Expand Up @@ -251,4 +258,28 @@ defmodule MishkaInstallerTest.Installer.DownloaderTest do
{:error, _error} = assert Downloader.get_mix(:url, %{path: url})
end
end

defp tar_expect_200(tarball) do
Req.Test.expect(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream", nil)
|> Plug.Conn.send_resp(200, tarball)
end)
end

defp tar_stub_200(tarball) do
Req.Test.stub(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream", nil)
|> Plug.Conn.send_resp(200, tarball)
end)
end

defp tar_expect_400(tarball) do
Req.Test.expect(Downloader, fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/octet-stream", nil)
|> Plug.Conn.send_resp(400, tarball)
end)
end
end

0 comments on commit c5991c6

Please sign in to comment.