Skip to content

Commit

Permalink
Use Path.safe_relative and warn
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara committed Jan 16, 2024
1 parent d42e9a9 commit 64ce95c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/ex_unit/lib/ex_unit/filters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule ExUnit.Filters do
end

defp extract_line_numbers(file_path) do
case Path.relative_to_cwd(file_path) |> String.split(":") do
case normalize_relative_path(file_path) |> String.split(":") do
[path] ->
{path, []}

Expand All @@ -66,6 +66,17 @@ defmodule ExUnit.Filters do
end
end

defp normalize_relative_path(file_path) do
case Path.safe_relative(file_path) do
{:ok, path} ->
path

:error ->
IO.warn("invalid file path given as ExUnit filter: #{file_path}", [])
file_path
end
end

defp to_line_number(str) do
case Integer.parse(str) do
{x, ""} when x > 0 -> x
Expand Down
20 changes: 20 additions & 0 deletions lib/ex_unit/test/ex_unit/filters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,24 @@ defmodule ExUnit.FiltersTest do
assert output =~ "invalid line number given as ExUnit filter: -987"
end
end

test "file outside of the current directory" do
external_path = "../other_project/test/path.exs"

output =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert ExUnit.Filters.parse_path(external_path) == {external_path, []}
end)

assert output =~ "invalid file path given as ExUnit filter: ../other_project/test/path.exs"

output =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert ExUnit.Filters.parse_path("#{external_path}:123") ==
{external_path,
[{:exclude, [:test]}, {:include, [location: {external_path, 123}]}]}
end)

assert output =~ "invalid file path given as ExUnit filter: ../other_project/test/path.exs"
end
end

0 comments on commit 64ce95c

Please sign in to comment.