1.8 KiB · text History 1a4b866
defmodule Mix.Tasks.Vigil.TestTaskTest do
  use Vigil.DataCase, async: false

  import ExUnit.CaptureIO
  import Ecto.Query
  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures

  alias Vigil.Issues.Issue

  setup do
    Mix.shell(Mix.Shell.IO)
    on_exit(fn -> Mix.shell(Mix.Shell.IO) end)
    owner = user_fixture()
    org = organization_fixture(owner)
    project = project_fixture(org_scope(owner, org), %{"name" => "web"})
    %{org: org, project: project}
  end

  defp issues(project),
    do: Repo.all(from i in Issue, where: i.project_id == ^project.id, order_by: i.short_id)

  test "an error and a message become two issues; again, their counts go up", %{
    org: org,
    project: project
  } do
    out = capture_io(fn -> Mix.Tasks.Vigil.Test.run([org.slug, "web"]) end)
    assert out =~ "Sent 2 event(s)"
    assert [%{type: :error, count: 1}, %{type: :default, count: 1}] = issues(project)

    capture_io(fn ->
      Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--count", "3"])
    end)

    assert [%{type: :error, count: 4}, %{count: 1}] = issues(project)
  end

  test "--unique makes new issues each run", %{org: org, project: project} do
    capture_io(fn ->
      Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--unique"])
    end)

    capture_io(fn ->
      Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--unique"])
    end)

    assert [_, _] = issues(project)
  end

  test "unknown org or project says so", %{org: org} do
    assert_raise Mix.Error, ~r/No organization/, fn ->
      Mix.Tasks.Vigil.Test.run(["nope", "web"])
    end

    assert_raise Mix.Error, ~r/No project/, fn -> Mix.Tasks.Vigil.Test.run([org.slug, "nope"]) end
    assert_raise Mix.Error, ~r/Usage/, fn -> Mix.Tasks.Vigil.Test.run([]) end
  end
end