Skip to content

Commit

Permalink
add travis and tasks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Mar 14, 2019
1 parent bf81e7e commit c15fde8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: elixir

elixir:
- 1.7.4
- 1.8.1

otp_release:
- 20.3
before_script:
- if elixir -v | grep -q 1.8; then mix format --check-formatted; fi # only enforce formatting for Elixir ~> 1.8

script:
- mix coveralls.safe_travis
- mix credo --strict

after_script:
- MIX_ENV=test mix inch.report
18 changes: 18 additions & 0 deletions lib/mix/tasks/safe_travis.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if Mix.env() == :test do
defmodule Mix.Tasks.Coveralls.SafeTravis do
@moduledoc false
alias Mix.Tasks.Coveralls

use Mix.Task

@preferred_cli_env :test
@shortdoc "A safe `coveralls.travis` variant that doesn't crash on failed upload."

def run(args) do
Coveralls.Travis.run(args)
rescue
e in ExCoveralls.ReportUploadError ->
Mix.shell().error("Failed coveralls upload: #{Exception.message(e)}")
end
end
end
54 changes: 54 additions & 0 deletions test/mix/tasks_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule Mix.Tasks.OpenStates.BuildTest do
use ExUnit.Case
import ExUnit.CaptureIO
import Mock
alias Mix.Tasks.{Coveralls, Coveralls.SafeTravis, Coveralls.Travis}
alias Mix.Tasks.{Credo, Docs, Format, Inch, OpenStates.Build}

describe "mix open_states.build" do
test "runs build task" do
with_mocks([
{Coveralls.Html, [], [run: fn _ -> nil end]},
{Credo, [], [run: fn _ -> nil end]},
{Docs, [], [run: fn _ -> nil end]},
{Inch, [], [run: fn _ -> nil end]}
]) do
Build.run(["--no-format"])
assert_called(Coveralls.Html.run([]))
assert_called(Credo.run(["--strict"]))
assert_called(Docs.run([]))
assert_called(Inch.run([]))
end
end

test "runs the formatter when Elixir >= 1.8" do
if System.version() >= "1.8" do
with_mock(Format, run: fn _ -> nil end) do
assert capture_io(fn -> Build.run_formatter([]) end)
|> String.contains?("Running formatter")

assert_called(Format.run(["--check-equivalent"]))
end
else
assert_raise RuntimeError, ~r/Elixir version must be >= 1.8/, fn ->
Build.run_formatter([])
end
end
end
end

describe "mix coveralls.safe_travis" do
test "runs" do
with_mock Travis, run: fn _ -> nil end do
SafeTravis.run(["hello"])
assert_called(Travis.run(["hello"]))
end
end

test "catches ExCoveralls.ReportUploadError" do
with_mock Travis, run: fn _ -> raise ExCoveralls.ReportUploadError end do
assert capture_io(:stderr, fn -> SafeTravis.run(["hello"]) end)
end
end
end
end

0 comments on commit c15fde8

Please sign in to comment.