Skip to content

Commit

Permalink
feat: add an option to include file line
Browse files Browse the repository at this point in the history
  • Loading branch information
icehaunter committed Apr 24, 2021
1 parent 9569cb0 commit 73c84e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -73,6 +73,7 @@ The JUnit style XML report for this project looks like this:
- `report_dir` (binary - default `Mix.Project.app_path()`): the directory to which the formatter will write the report. Do not end it with a slash. **IMPORTANT!!** `JUnitFormatter` will **NOT** create the directory. If you are pointing to a directory that is outside _build then it is your duty to clean it and to be sure it exists.
- `prepend_project_name?` (boolean - default `false`): tells if the report file should have the name of the project as a prefix. See the "Umbrella" part of the documentation.
- `include_filename?` (boolean - default `false`): dictates whether `<testcase>`s in the XML report should include a "file" attribute of the relative path to the file of the test. Note that this defaults to false because not all JUnit ingesters will accept a file attribute.
- `include_file_line?` (boolean - default `false`): only has effect when `include_filename?` is `true`. Dictates whether `file` attribute should include line of the test after a colon (e.g. `test/file_test.exs:123`).

Example configuration:

Expand Down
15 changes: 12 additions & 3 deletions lib/formatter.ex
Expand Up @@ -191,7 +191,7 @@ defmodule JUnitFormatter do
time: format_time(test.time)
]

attrs = maybe_add_filename(attrs, test.tags.file)
attrs = maybe_add_filename(attrs, test.tags.file, test.tags.line)

{
:testcase,
Expand Down Expand Up @@ -225,9 +225,18 @@ defmodule JUnitFormatter do
defp message({type, reason, _}) when is_atom(type), do: "#{type}: #{inspect(reason)}"
defp message({type, reason, _}), do: "#{inspect(type)}: #{inspect(reason)}"

defp maybe_add_filename(attrs, path) do
defp maybe_add_filename(attrs, path, line) do
if Application.get_env(:junit_formatter, :include_filename?) do
Keyword.put(attrs, :file, Path.relative_to_cwd(path))
path = Path.relative_to_cwd(path)

file =
if Application.get_env(:junit_formatter, :include_file_line?) do
"#{path}:#{line}"
else
path
end

Keyword.put(attrs, :file, file)
else
attrs
end
Expand Down
12 changes: 12 additions & 0 deletions test/formatter_test.exs
Expand Up @@ -251,6 +251,18 @@ defmodule FormatterTest do
assert xpath(output, ~x{//testsuite/testcase/@file}s) == "test/formatter_test.exs"
end

test "has file attribute with line when configured to" do
defsuite do
test "it will fail", do: assert(false)
end

put_config(:include_filename?, true)
put_config(:include_file_line?, true)
output = run_and_capture_output()

assert xpath(output, ~x{//testsuite/testcase/@file}s) == "test/formatter_test.exs:256"
end

test "does not have file attribute when not configured to" do
defsuite do
test "it will fail", do: assert(false)
Expand Down

0 comments on commit 73c84e7

Please sign in to comment.