From 0edaffcdd60600941ebf7cc8be5deff5f7cafc1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Thu, 2 Feb 2023 02:27:16 +0100 Subject: [PATCH] Simplify loading of repos_key config --- lib/hex/config.ex | 4 ++-- lib/hex/repo.ex | 19 +++++++++------- lib/hex/state.ex | 5 +---- test/hex/repo_test.exs | 49 +++++++++++++++++++++++++++++++----------- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/lib/hex/config.ex b/lib/hex/config.ex index 62a46766..cd2d7e2c 100644 --- a/lib/hex/config.ex +++ b/lib/hex/config.ex @@ -104,8 +104,8 @@ defmodule Hex.Config do term end - def read_repos(config, repo_key) do - hexpm = Hex.Repo.nostate_default_hexpm_repo(repo_key) + def read_repos(config) do + hexpm = Hex.Repo.default_hexpm_repo() (config[:"$repos"] || %{}) |> Hex.Repo.merge_hexpm(hexpm) diff --git a/lib/hex/repo.ex b/lib/hex/repo.ex index d5612cbe..5e1701b5 100644 --- a/lib/hex/repo.ex +++ b/lib/hex/repo.ex @@ -23,8 +23,10 @@ defmodule Hex.Repo do case Map.fetch(repos, repo) do {:ok, config} when repo == "hexpm" -> - hexpm = default_hexpm_repo() - {:ok, %{config | url: hexpm.url || config.url, trusted: hexpm.trusted}} + hexpm = hexpm_repo() + url = hexpm.url || config.url + auth_key = hexpm.auth_key || config.auth_key + {:ok, %{config | url: url, trusted: hexpm.trusted, auth_key: auth_key}} {:ok, config} -> {:ok, config} @@ -56,23 +58,24 @@ defmodule Hex.Repo do |> Map.put(:trusted, Map.has_key?(repo, :auth_key) or source.trusted) end - def default_hexpm_repo() do + def hexpm_repo() do trusted_mirror_url = Hex.State.fetch!(:trusted_mirror_url) mirror_url = Hex.State.fetch!(:mirror_url) + auth_key = Hex.State.fetch!(:repos_key) %{ url: trusted_mirror_url || mirror_url, public_key: @hexpm_public_key, - auth_key: nil, + auth_key: auth_key, trusted: trusted_mirror_url != nil or mirror_url == nil } end - def nostate_default_hexpm_repo(auth_key) do + def default_hexpm_repo() do %{ url: @hexpm_url, public_key: @hexpm_public_key, - auth_key: auth_key, + auth_key: nil, trusted: true } end @@ -102,7 +105,7 @@ defmodule Hex.Repo do ) end - def merge_hexpm(repos, hexpm \\ default_hexpm_repo()) do + def merge_hexpm(repos, hexpm \\ hexpm_repo()) do Map.update(repos, "hexpm", hexpm, &Map.merge(hexpm, &1)) end @@ -144,7 +147,7 @@ defmodule Hex.Repo do end def clean_hexpm(repos) do - hexpm = default_hexpm_repo() + hexpm = hexpm_repo() repo = Map.get(repos, "hexpm", hexpm) repo = clean_repo(repo, hexpm) diff --git a/lib/hex/state.ex b/lib/hex/state.ex index fece12b8..850fa653 100644 --- a/lib/hex/state.ex +++ b/lib/hex/state.ex @@ -150,14 +150,11 @@ defmodule Hex.State do {key, load_config_value(global_config, project_config, spec)} end) - {_source, repos_key} = Map.fetch!(state, :repos_key) - Map.merge(state, %{ clean_pass: {:computed, true}, httpc_profile: {:computed, :hex}, pbkdf2_iters: {:computed, @pbkdf2_iters}, - repos: {:computed, Hex.Config.read_repos(global_config, repos_key)}, - repos_key: {:computed, repos_key}, + repos: {:computed, Hex.Config.read_repos(global_config)}, ssl_version: {:computed, ssl_version()}, shell_process: {:computed, nil} }) diff --git a/test/hex/repo_test.exs b/test/hex/repo_test.exs index 04ec551f..b8c9fd5b 100644 --- a/test/hex/repo_test.exs +++ b/test/hex/repo_test.exs @@ -48,7 +48,7 @@ defmodule Hex.RepoTest do test "get public key" do bypass = Bypass.open() repos = Hex.State.fetch!(:repos) - hexpm = Hex.Repo.default_hexpm_repo() + hexpm = Hex.Repo.hexpm_repo() repos = put_in(repos["hexpm"].url, "http://localhost:#{bypass.port}") Hex.State.put(:repos, repos) @@ -73,9 +73,15 @@ defmodule Hex.RepoTest do end test "fetch_repo/1" do + assert Hex.Repo.fetch_repo("foo") == :error + assert {:ok, - %{auth_key: nil, public_key: _, trusted: true, url: "http://localhost:4043/repo"}} = - Hex.Repo.fetch_repo("hexpm") + %{ + auth_key: nil, + public_key: _, + trusted: true, + url: "http://localhost:4043/repo" + }} = Hex.Repo.fetch_repo("hexpm") assert {:ok, %{ @@ -85,25 +91,42 @@ defmodule Hex.RepoTest do url: "http://localhost:4043/repo/repos/acme" }} = Hex.Repo.fetch_repo("hexpm:acme") - assert Hex.Repo.fetch_repo("foo") == :error - Hex.State.put(:trusted_mirror_url, "http://example.com") + Hex.State.put(:repos_key, "key") - assert {:ok, %{auth_key: nil, public_key: _, trusted: true, url: "http://example.com"}} = - Hex.Repo.fetch_repo("hexpm") + assert {:ok, + %{ + auth_key: "key", + public_key: _, + trusted: true, + url: "http://example.com" + }} = Hex.Repo.fetch_repo("hexpm") assert {:ok, - %{auth_key: nil, public_key: _, trusted: true, url: "http://example.com/repos/acme"}} = - Hex.Repo.fetch_repo("hexpm:acme") + %{ + auth_key: "key", + public_key: _, + trusted: true, + url: "http://example.com/repos/acme" + }} = Hex.Repo.fetch_repo("hexpm:acme") Hex.State.put(:trusted_mirror_url, nil) Hex.State.put(:mirror_url, "http://example.com") - assert {:ok, %{auth_key: nil, public_key: _, trusted: false, url: "http://example.com"}} = - Hex.Repo.fetch_repo("hexpm") + assert {:ok, + %{ + auth_key: "key", + public_key: _, + trusted: false, + url: "http://example.com" + }} = Hex.Repo.fetch_repo("hexpm") assert {:ok, - %{auth_key: nil, public_key: _, trusted: false, url: "http://example.com/repos/acme"}} = - Hex.Repo.fetch_repo("hexpm:acme") + %{ + auth_key: "key", + public_key: _, + trusted: false, + url: "http://example.com/repos/acme" + }} = Hex.Repo.fetch_repo("hexpm:acme") end end