Skip to content

Commit

Permalink
Improve error message when trying to publish existing package with pe…
Browse files Browse the repository at this point in the history
…rmissions (#1021)

Co-authored-by: Justin Ye <hi_justin@hotmail.com>
  • Loading branch information
ericmj and Coreastreet authored Apr 16, 2024
1 parent 720322f commit 98f008e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/mix/tasks/hex.publish.ex
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ defmodule Mix.Tasks.Hex.Publish do
meta = build.meta
%{tarball: tarball, outer_checksum: checksum} = Hex.Tar.create!(meta, meta.files, :memory)
dry_run? = Keyword.get(opts, :dry_run, false)
opts = [{:name, Map.fetch!(build.meta, :name)} | opts]

if dry_run? do
:ok
Expand All @@ -509,6 +510,24 @@ defmodule Mix.Tasks.Hex.Publish do
Hex.Shell.info("Package published to #{location} (#{checksum})")
:ok

{:ok, {403, _, _}} = result ->
Hex.Shell.info("")
Hex.Shell.error("Publishing failed")
package = Keyword.fetch!(opts, :name)

case Hex.API.Package.get(organization, package, auth) do
{:ok, {code, _, _}} when code in 200..299 ->
Hex.Shell.error("""
Package with name #{Keyword.fetch!(opts, :name)} already exists. \
Make sure you are authenticated and have permissions to publish the package.\
""")

_ ->
Hex.Utils.print_error_result(result)
end

:error

other ->
Hex.Shell.info("")
Hex.Shell.error("Publishing failed")
Expand Down
26 changes: 26 additions & 0 deletions test/mix/tasks/hex.publish_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,32 @@ defmodule Mix.Tasks.Hex.PublishTest do
end)
end

test "try create existing package without permissions" do
Process.put(:hex_test_app_name, :ex_doc)
Mix.Project.push(ReleaseSimple.MixProject)

in_tmp(fn ->
set_home_tmp()
File.write!("mix.exs", "mix.exs")
File.write!("myfile.txt", "hello")
setup_auth("user2", "hunter42")
send(self(), {:mix_shell_input, :prompt, "hunter42"})

assert catch_throw(
Mix.Tasks.Hex.Publish.run(["package", "--no-progress", "--replace", "--yes"])
) == {:exit_code, 1}

message =
"Package with name ex_doc already exists. " <>
"Make sure you are authenticated and have permissions to publish the package."

assert_received {:mix_shell, :error, ["Publishing failed"]}
assert_received {:mix_shell, :error, [^message]}
end)
after
purge([ReleaseSimple.MixProject])
end

test "publish docs with invalid filename" do
Mix.Project.push(DocsFilenameError.MixProject)

Expand Down

0 comments on commit 98f008e

Please sign in to comment.