-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule ExCoveralls.Cirrus do | ||
@moduledoc """ | ||
Handles cirrus-ci integration with coveralls. | ||
""" | ||
alias ExCoveralls.Poster | ||
|
||
def execute(stats, options) do | ||
json = generate_json(stats, Enum.into(options, %{})) | ||
if options[:verbose] do | ||
IO.puts json | ||
end | ||
Poster.execute(json) | ||
end | ||
|
||
def generate_json(stats, options \\ %{}) | ||
def generate_json(stats, _options) do | ||
Jason.encode!(%{ | ||
service_job_id: get_job_id(), | ||
service_name: "cirrus", | ||
repo_token: get_repo_token(), | ||
source_files: stats, | ||
git: generate_git_info() | ||
}) | ||
end | ||
|
||
defp generate_git_info do | ||
%{head: %{ | ||
message: get_message(), | ||
id: get_sha() | ||
}, | ||
branch: get_branch() | ||
} | ||
end | ||
|
||
defp get_branch do | ||
System.get_env("CIRRUS_BRANCH") | ||
end | ||
|
||
defp get_job_id do | ||
System.get_env("CIRRUS_BUILD_ID") | ||
end | ||
|
||
defp get_sha do | ||
System.get_env("CIRRUS_CHANGE_IN_REPO") | ||
end | ||
|
||
defp get_message do | ||
System.get_env("CIRRUS_CHANGE_MESSAGE") | ||
end | ||
|
||
defp get_repo_token do | ||
System.get_env("COVERALLS_REPO_TOKEN") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule ExCoveralls.CirrusTest do | ||
use ExUnit.Case | ||
import Mock | ||
alias ExCoveralls.Cirrus | ||
|
||
@content "defmodule Test do\n def test do\n end\nend\n" | ||
@counts [0, 1, nil, nil] | ||
@source_info [%{name: "test/fixtures/test.ex", source: @content, coverage: @counts}] | ||
|
||
setup do | ||
# Capture existing values | ||
orig_vars = | ||
~w(CIRRUS_BRANCH CIRRUS_BUILD_ID CIRRUS_CHANGE_IN_REPO CIRRUS_CHANGE_MESSAGE COVERALLS_REPO_TOKEN) | ||
|> Enum.map(fn var -> {var, System.get_env(var)} end) | ||
|
||
on_exit(fn -> | ||
# Reset env vars | ||
for {k, v} <- orig_vars do | ||
if v != nil do | ||
System.put_env(k, v) | ||
else | ||
System.delete_env(k) | ||
end | ||
end | ||
end) | ||
|
||
# No additional context | ||
{:ok, []} | ||
end | ||
|
||
test_with_mock "execute", ExCoveralls.Poster, execute: fn _ -> "result" end do | ||
assert(Cirrus.execute(@source_info, []) == "result") | ||
end | ||
|
||
test "generate json for cirrus" do | ||
json = Cirrus.generate_json(@source_info) | ||
assert(json =~ ~r/service_job_id/) | ||
assert(json =~ ~r/service_name/) | ||
assert(json =~ ~r/source_files/) | ||
assert(json =~ ~r/git/) | ||
end | ||
|
||
test "generate from env vars" do | ||
System.put_env("CIRRUS_BRANCH", "branch") | ||
System.put_env("CIRRUS_BUILD_ID", "id") | ||
System.put_env("CIRRUS_CHANGE_MESSAGE", "Initial commit") | ||
System.put_env("CIRRUS_CHANGE_IN_REPO", "sha1") | ||
System.put_env("COVERALLS_REPO_TOKEN", "token") | ||
|
||
{:ok, payload} = Jason.decode(Cirrus.generate_json(@source_info)) | ||
%{"git" => %{"branch" => branch, "head" => %{"message" => message, "id" => id}}} = payload | ||
|
||
assert(branch == "branch") | ||
assert(id == "sha1") | ||
assert(message == "Initial commit") | ||
assert(payload["service_job_id"] == "id") | ||
assert(payload["repo_token"] == "token") | ||
end | ||
|
||
test "submits as `cirrus`" do | ||
parsed = Cirrus.generate_json(@source_info) |> Jason.decode!() | ||
assert(%{"service_name" => "cirrus"} = parsed) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters