Skip to content

Commit

Permalink
ExUnit.Filters: changed parse_path() to return :location tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
azizk committed Oct 1, 2023
1 parent 0621940 commit e427a0c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 45 deletions.
18 changes: 11 additions & 7 deletions lib/ex_unit/lib/ex_unit/filters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ defmodule ExUnit.Filters do
"""
@spec parse_path(String.t()) :: {String.t(), Keyword.t()}
def parse_path(file) do
case extract_line_numbers(file) do
case extract_locations(file) do
{path, []} -> {path, []}
{path, line_tuples} -> {path, exclude: [:test], include: line_tuples}
{path, locations} -> {path, exclude: [:test], include: locations}
end
end

defp extract_line_numbers(file) do
defp extract_locations(file) do
case String.split(file, ":") do
[part] ->
{part, []}

[path | parts] ->
{path_parts, line_numbers} = Enum.split_while(parts, &(not valid_line_number?(&1)))

line_tuples = for n <- line_numbers, valid_line_number(n), do: {:line, n}

path = Enum.join([path | path_parts], ":")

{path, line_tuples}
locations = for n <- line_numbers, valid_line_number(n), do: {:location, {path, n}}

{path, locations}
end
end

Expand Down Expand Up @@ -186,7 +186,11 @@ defmodule ExUnit.Filters do
end
end

defp has_tag({:line, line}, %{line: _, describe_line: describe_line} = tags, collection) do
defp has_tag(
{:location, {_file, line}},
%{line: _, describe_line: describe_line} = tags,
collection
) do
line = to_integer(line)

cond do
Expand Down
108 changes: 71 additions & 37 deletions lib/ex_unit/test/ex_unit/filters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,46 +192,80 @@ defmodule ExUnit.FiltersTest do
end

test "file paths with line numbers" do
assert ExUnit.Filters.parse_path("test/some/path.exs:123") ==
{"test/some/path.exs", [exclude: [:test], include: [line: "123"]]}

assert ExUnit.Filters.parse_path("test/some/path.exs") == {"test/some/path.exs", []}

assert ExUnit.Filters.parse_path("test/some/path.exs:123notreallyalinenumber123") ==
{"test/some/path.exs:123notreallyalinenumber123", []}

assert ExUnit.Filters.parse_path("C:\\some\\path.exs:123") ==
{"C:\\some\\path.exs", [exclude: [:test], include: [line: "123"]]}

assert ExUnit.Filters.parse_path("C:\\some\\path.exs") == {"C:\\some\\path.exs", []}

assert ExUnit.Filters.parse_path("C:\\some\\path.exs:123notreallyalinenumber123") ==
{"C:\\some\\path.exs:123notreallyalinenumber123", []}

assert ExUnit.Filters.parse_path("test/some/path.exs:123:456") ==
{"test/some/path.exs", [exclude: [:test], include: [line: "123", line: "456"]]}

assert ExUnit.Filters.parse_path("C:\\some\\path.exs:123:456") ==
{"C:\\some\\path.exs", [exclude: [:test], include: [line: "123", line: "456"]]}

assert ExUnit.Filters.parse_path("test/some/path.exs:123notalinenumber123:456") ==
{"test/some/path.exs:123notalinenumber123",
[exclude: [:test], include: [line: "456"]]}

assert ExUnit.Filters.parse_path("test/some/path.exs:123:456notalinenumber456") ==
{"test/some/path.exs", [{:exclude, [:test]}, {:include, [line: "123"]}]}

assert ExUnit.Filters.parse_path("C:\\some\\path.exs:123notalinenumber123:456") ==
{"C:\\some\\path.exs:123notalinenumber123",
[exclude: [:test], include: [line: "456"]]}

assert ExUnit.Filters.parse_path("C:\\some\\path.exs:123:456notalinenumber456") ==
{"C:\\some\\path.exs", [{:exclude, [:test]}, {:include, [line: "123"]}]}
unix_path = "test/some/path.exs"

assert ExUnit.Filters.parse_path("#{unix_path}:123") ==
{unix_path, [exclude: [:test], include: [location: {unix_path, "123"}]]}

assert ExUnit.Filters.parse_path(unix_path) == {unix_path, []}

assert ExUnit.Filters.parse_path("#{unix_path}:123notreallyalinenumber123") ==
{"#{unix_path}:123notreallyalinenumber123", []}

windows_path = "C:\\some\\path.exs"

assert ExUnit.Filters.parse_path("#{windows_path}:123") ==
{windows_path, [exclude: [:test], include: [location: {windows_path, "123"}]]}

assert ExUnit.Filters.parse_path(windows_path) == {windows_path, []}

assert ExUnit.Filters.parse_path("#{windows_path}:123notreallyalinenumber123") ==
{"#{windows_path}:123notreallyalinenumber123", []}

assert ExUnit.Filters.parse_path("#{unix_path}:123:456") ==
{unix_path,
[
exclude: [:test],
include: [location: {unix_path, "123"}, location: {unix_path, "456"}]
]}

assert ExUnit.Filters.parse_path("#{windows_path}:123:456") ==
{windows_path,
[
exclude: [:test],
include: [location: {windows_path, "123"}, location: {windows_path, "456"}]
]}

assert ExUnit.Filters.parse_path("#{unix_path}:123notalinenumber123:456") ==
{"#{unix_path}:123notalinenumber123",
[
exclude: [:test],
include: [location: {"#{unix_path}:123notalinenumber123", "456"}]
]}

assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert ExUnit.Filters.parse_path("#{unix_path}:123:456notalinenumber456") ==
{unix_path,
[
{:exclude, [:test]},
{:include, [location: {unix_path, "123"}]}
]}
end) =~ "invalid line number given as ExUnit filter: 456notalinenumber456"

assert ExUnit.Filters.parse_path("#{windows_path}:123notalinenumber123:456") ==
{"#{windows_path}:123notalinenumber123",
[
exclude: [:test],
include: [location: {"#{windows_path}:123notalinenumber123", "456"}]
]}

assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert ExUnit.Filters.parse_path("#{windows_path}:123:456notalinenumber456") ==
{windows_path,
[
{:exclude, [:test]},
{:include, [location: {windows_path, "123"}]}
]}
end) =~ "invalid line number given as ExUnit filter: 456notalinenumber456"

output =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert ExUnit.Filters.parse_path("test/some/path.exs:123:0:-789:456") ==
{"test/some/path.exs", [exclude: [:test], include: [line: "123", line: "456"]]}
assert ExUnit.Filters.parse_path("#{unix_path}:123:0:-789:456") ==
{unix_path,
[
exclude: [:test],
include: [location: {unix_path, "123"}, location: {unix_path, "456"}]
]}
end)

assert output =~ "invalid line number given as ExUnit filter: 0"
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/test/mix/tasks/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ defmodule Mix.Tasks.TestTest do
assert output =~ """
==> bar
Excluding tags: [:test]
Including tags: [line: \"10\"]
Including tags: [location: {\"test/bar_tests.exs\", \"10\"}]
.
"""
Expand Down

0 comments on commit e427a0c

Please sign in to comment.