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

Ignore single next line #301

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ defmodule MyModule do
end
```

Use comment `coveralls-ignore-next-line` to ignore only the next line.

```elixir
defmodule MyModule do
def covered do
# coveralls-ignore-next-line
"ignored"
"covered"
end
end
```

### Silence OTP Cover Warnings
To remove OTP warnings about modules or specific logging, you can copy the `cover.erl` file under `src/` of your Elixir project and modify it to remove the warnings, as a tentative solution.

Expand Down
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