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

[WIP] feature: add cirrus-ci #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/excoveralls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule ExCoveralls do
alias ExCoveralls.Circle
alias ExCoveralls.Semaphore
alias ExCoveralls.Drone
alias ExCoveralls.Cirrus
alias ExCoveralls.Local
alias ExCoveralls.Html
alias ExCoveralls.Json
Expand All @@ -22,6 +23,7 @@ defmodule ExCoveralls do
@type_circle "circle"
@type_semaphore "semaphore"
@type_drone "drone"
@type_cirrus "cirrus"
@type_local "local"
@type_html "html"
@type_json "json"
Expand Down Expand Up @@ -89,6 +91,13 @@ defmodule ExCoveralls do
Drone.execute(stats, options)
end

@doc """
Logic for posting from cirrus-ci server
"""
def analyze(stats, @type_cirrus, options) do
Cirrus.execute(stats, options)
end

@doc """
Logic for local stats display, without posting server
"""
Expand Down
54 changes: 54 additions & 0 deletions lib/excoveralls/cirrus.ex
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
14 changes: 14 additions & 0 deletions lib/mix/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ defmodule Mix.Tasks.Coveralls do
end
end

defmodule Cirrus do
@moduledoc """
Provides an entry point for CirrusCI's script.
"""

use Mix.Task

@preferred_cli_env :test

def run(args) do
Mix.Tasks.Coveralls.do_run(args, [type: "cirrus"])
end
end

defmodule Post do
@moduledoc """
Provides an entry point for posting test coverage to
Expand Down
64 changes: 64 additions & 0 deletions test/cirrus_test.exs
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
5 changes: 5 additions & 0 deletions test/excoveralls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ defmodule ExCoverallsTest do
assert called ExCoveralls.Drone.execute(@stats,[])
end

test_with_mock "analyze cirrus", ExCoveralls.Cirrus, [execute: fn(_,_) -> nil end] do
ExCoveralls.analyze(@stats, "cirrus", [])
assert called ExCoveralls.Cirrus.execute(@stats,[])
end

test_with_mock "analyze local", ExCoveralls.Local, [execute: fn(_,_) -> nil end] do
ExCoveralls.analyze(@stats, "local", [])
assert called ExCoveralls.Local.execute(@stats,[])
Expand Down