diff --git a/README.md b/README.md index aa585f86..770ea319 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ end defp deps do [ {:excoveralls, "~> 0.10", only: :test}, + {:hackney, "~> 1.16", only: :test} # only required if posting to the coveralls.io service ] end ``` diff --git a/lib/excoveralls/poster.ex b/lib/excoveralls/poster.ex index fba50f3d..e357e85a 100644 --- a/lib/excoveralls/poster.ex +++ b/lib/excoveralls/poster.ex @@ -22,40 +22,56 @@ defmodule ExCoveralls.Poster do end end - defp send_file(file_name, options) do - Application.ensure_all_started(:hackney) - endpoint = options[:endpoint] || "https://coveralls.io" - - response = - :hackney.request( - :post, - "#{endpoint}/api/v1/jobs", - [], - {:multipart, - [ - {:file, file_name, {"form-data", [{"name", "json_file"}, {"filename", file_name}]}, - [{"Content-Type", "gzip/json"}]} - ]}, - [{:recv_timeout, 10_000}] - ) + if Code.ensure_loaded?(:hackney) do + defp send_file(file_name, options) do + Application.ensure_all_started(:hackney) + endpoint = options[:endpoint] || "https://coveralls.io" - case response do - {:ok, status_code, _, _} when status_code in 200..299 -> - {:ok, "Successfully uploaded the report to '#{endpoint}'."} + response = + :hackney.request( + :post, + "#{endpoint}/api/v1/jobs", + [], + {:multipart, + [ + {:file, file_name, {"form-data", [{"name", "json_file"}, {"filename", file_name}]}, + [{"Content-Type", "gzip/json"}]} + ]}, + [{:recv_timeout, 10_000}] + ) + + case response do + {:ok, status_code, _, _} when status_code in 200..299 -> + {:ok, "Successfully uploaded the report to '#{endpoint}'."} + + {:ok, status_code, _, client} -> + {:ok, body} = :hackney.body(client) - {:ok, status_code, _, client} -> - {:ok, body} = :hackney.body(client) + {:error, + "Failed to upload the report to '#{endpoint}' (reason: status_code = #{status_code}, body = #{ + body + })."} + + {:error, reason} when reason in [:timeout, :connect_timeout] -> + {:ok, + "Unable to upload the report to '#{endpoint}' due to a timeout. Not failing the build."} + + {:error, reason} -> + {:error, "Failed to upload the report to '#{endpoint}' (reason: #{inspect(reason)})."} + end + end + else + defp send_file(_, _) do + raise """ + missing :hackney dependency - {:error, - "Failed to upload the report to '#{endpoint}' (reason: status_code = #{status_code}, body = #{ - body - })."} + ExCoveralls requires :hackney to post to the coveralls.io service. - {:error, reason} when reason in [:timeout, :connect_timeout] -> - {:ok, "Unable to upload the report to '#{endpoint}' due to a timeout. Not failing the build."} + Please add :hackney to your mix.exs :deps list and run: - {:error, reason} -> - {:error, "Failed to upload the report to '#{endpoint}' (reason: #{inspect(reason)})."} + mix deps.get + mix deps.clean --build excoveralls + """ end end end diff --git a/mix.exs b/mix.exs index f36e009b..d39e5d02 100644 --- a/mix.exs +++ b/mix.exs @@ -39,7 +39,7 @@ defmodule ExCoveralls.Mixfile do def deps do [ {:jason, "~> 1.0"}, - {:hackney, "~> 1.16"}, + {:hackney, "~> 1.16", optional: true}, {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, {:meck, "~> 0.8", only: :test}, {:mock, "~> 0.3.6", only: :test}