Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docs output if set in project's config #1026

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/mix/tasks/hex.publish.ex
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,12 @@ defmodule Mix.Tasks.Hex.Publish do
end

defp docs_dir do
docs_output = docs_output_from_config()

cond do
docs_output ->
docs_output

File.exists?("doc") ->
"doc"

Expand All @@ -483,6 +488,14 @@ defmodule Mix.Tasks.Hex.Publish do
end
end

defp docs_output_from_config() do
case Keyword.get(Mix.Project.config(), :docs) do
nil -> nil
config when is_list(config) -> config[:output]
config when is_function(config, 0) -> config.()[:output]
end
end

defp create_release(build, organization, auth, opts) do
meta = build.meta
%{tarball: tarball, outer_checksum: checksum} = Hex.Tar.create!(meta, meta.files, :memory)
Expand Down
95 changes: 95 additions & 0 deletions test/mix/tasks/hex.publish_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,53 @@ defmodule Mix.Tasks.Hex.PublishTest do
end
end

defmodule DocsOutputConfigured.MixProject do
def project do
[
app: :ex_doc,
version: "0.1.0",
aliases: [docs: [&docs/1]],
docs: [output: "my_docs"]
]
end

defp docs(_) do
File.mkdir_p!("my_docs")
File.write!("my_docs/index.html", "the index")
end
end

defmodule DocsOutputConfiguredFunction.MixProject do
def project do
[
app: :ex_doc,
version: "0.1.0",
aliases: [docs: [&docs/1]],
docs: &docs_config/0
]
end

defp docs(_) do
File.mkdir_p!("my_docs")
File.write!("my_docs/index.html", "the index")
end

defp docs_config, do: [output: "my_docs"]
end

defmodule DocsOutputNoOutput.MixProject do
def project do
[
app: :ex_doc,
version: "0.1.0",
aliases: [docs: [&docs/1]],
docs: [output: "my_docs"]
]
end

defp docs(_), do: :ok
end

test "ensure user exists" do
Process.put(:hex_test_app_name, :publish_ensure_user_exists)
Mix.Project.push(ReleaseSimple.MixProject)
Expand Down Expand Up @@ -211,6 +258,54 @@ defmodule Mix.Tasks.Hex.PublishTest do
end)
end

test "raises if output folder is missing" do
Mix.Project.push(DocsOutputNoOutput.MixProject)

in_tmp(fn ->
set_home_tmp()
setup_auth("user", "hunter42")

error_msg = "File not found: my_docs/index.html"

assert_raise Mix.Error, error_msg, fn ->
send(self(), {:mix_shell_input, :prompt, "hunter42"})
Mix.Tasks.Hex.Publish.run(["docs", "--no-progress", "--replace"])

refute_received {:mix_shell, :info, ["Docs published to https://hexdocs.pm/ex_doc/0.1.0"]}
end
end)
end

test "publishes docs with different output configured" do
Mix.Project.push(DocsOutputConfigured.MixProject)

in_tmp(fn ->
set_home_tmp()
setup_auth("user", "hunter42")

send(self(), {:mix_shell_input, :prompt, "hunter42"})
Mix.Tasks.Hex.Publish.run(["docs", "--no-progress", "--replace"])

assert_received {:mix_shell, :info,
["Docs published to http://localhost:4043/docs/ex_doc-0.1.0.tar.gz"]}
end)
end

test "publishes docs when docs config is a function" do
Mix.Project.push(DocsOutputConfiguredFunction.MixProject)

in_tmp(fn ->
set_home_tmp()
setup_auth("user", "hunter42")

send(self(), {:mix_shell_input, :prompt, "hunter42"})
Mix.Tasks.Hex.Publish.run(["docs", "--no-progress", "--replace"])

assert_received {:mix_shell, :info,
["Docs published to http://localhost:4043/docs/ex_doc-0.1.0.tar.gz"]}
end)
end

test "docs when package is not published yet" do
Mix.Project.push(DocsError.MixProject)

Expand Down
Loading