Skip to content

Commit

Permalink
Ignore single next line
Browse files Browse the repository at this point in the history
Allows to ignore the coverage for the following line of code,
by putting a new type of comment `# coveralls-ignore-next-line`.

Ignoring a single line is a common use case, which currently can be done by wrapping the code
between the "start" and "stop" comments. If we can achieve the same with just one comment,
this produces less visual noise and leaves more space for the useful code.

Resolves parroty#219
  • Loading branch information
RKushnir committed Feb 19, 2023
1 parent 6890fdc commit 8a0ec98
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
21 changes: 15 additions & 6 deletions lib/excoveralls/ignore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule ExCoveralls.Ignore do
defp do_filter(%{name: name, source: source, coverage: coverage}) do
lines = String.split(source, "\n")
list = Enum.zip(lines, coverage)
|> Enum.map_reduce(false, &check_and_swap/2)
|> Enum.map_reduce(:no_ignore, &check_and_swap/2)
|> elem(0)
|> List.zip
|> Enum.map(&Tuple.to_list(&1))
Expand All @@ -33,18 +33,27 @@ defmodule ExCoveralls.Ignore do
defp parse_filter_list([lines, coverage]), do: [Enum.join(lines, "\n"), coverage]

defp coverage_for_line({line, coverage}, ignore) do
if ignore == false do
if ignore == :no_ignore do
{line, coverage}
else
{line, nil}
end
end

defp ignore_next?(line, ignore) do
case Regex.run(~r/coveralls-ignore-(start|stop)/, line, capture: :all_but_first) do
["start"] -> true
["stop"] -> false
_sth -> ignore
case Regex.run(~r/coveralls-ignore-(start|stop|next-line)/, line, capture: :all_but_first) do
["start"] -> :ignore_block
["stop"] -> :no_ignore
["next-line"] ->
case ignore do
:ignore_block -> ignore
_sth -> :ignore_line
end
_sth ->
case ignore do
:ignore_line -> :no_ignore
_sth -> ignore
end
end
end

Expand Down
64 changes: 56 additions & 8 deletions test/ignore_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ExCoveralls.IgnoreTest do
use ExUnit.Case
alias ExCoveralls.Ignore

@content """
@block_content """
defmodule Test do
def test do
end
Expand All @@ -12,15 +12,63 @@ defmodule ExCoveralls.IgnoreTest do
#coveralls-ignore-stop
end
"""
@counts [0, 0, 0, nil, 0, 0, nil, 0, 0]
@source_info [%{name: "test/fixtures/test.ex",
source: @content,
coverage: @counts
@block_counts [0, 0, 0, nil, 0, 0, nil, 0, 0]
@block_source_info [%{name: "test/fixtures/test.ex",
source: @block_content,
coverage: @block_counts
}]

test "filter ignored lines returns valid list" do
info = Ignore.filter(@source_info) |> Enum.at(0)
assert(info[:source] == @content)
@single_line_content """
defmodule Test do
def test do
end
#coveralls-ignore-next-line
def test_ignored do
end
def test_not_ignored do
end
end
"""
@single_line_counts [0, 0, 0, nil, 0, 0, 0, 0, 0, 0]
@single_line_source_info [%{name: "test/fixtures/test.ex",
source: @single_line_content,
coverage: @single_line_counts
}]

@mixed_content """
defmodule Test do
def test do
end
#coveralls-ignore-start
def test_ignored do
#coveralls-ignore-next-line
end
#coveralls-ignore-stop
def test_not_ignored do
end
end
"""
@mixed_counts [0, 0, 0, nil, 0, nil, 0, nil, 0, 0, 0, 0]
@mixed_source_info [%{name: "test/fixtures/test.ex",
source: @mixed_content,
coverage: @mixed_counts
}]

test "filter ignored lines with start/stop block returns valid list" do
info = Ignore.filter(@block_source_info) |> Enum.at(0)
assert(info[:source] == @block_content)
assert(info[:coverage] == [0, 0, 0, nil, nil, nil, nil, 0, 0])
end

test "filter ignored lines with next-line returns valid list" do
info = Ignore.filter(@single_line_source_info) |> Enum.at(0)
assert(info[:source] == @single_line_content)
assert(info[:coverage] == [0, 0, 0, nil, nil, 0, 0, 0, 0, 0])
end

test "filter ignored lines with next-line inside start/stop block returns valid list" do
info = Ignore.filter(@mixed_source_info) |> Enum.at(0)
assert(info[:source] == @mixed_content)
assert(info[:coverage] == [0, 0, 0, nil, nil, nil, nil, nil, 0, 0, 0, 0])
end
end

0 comments on commit 8a0ec98

Please sign in to comment.