From 73c84e720e01df14dfd466ca81ed368a39e7b6dc Mon Sep 17 00:00:00 2001 From: Ilya Borovitinov Date: Sat, 24 Apr 2021 15:13:32 +0300 Subject: [PATCH] feat: add an option to include file line --- README.md | 1 + lib/formatter.ex | 15 ++++++++++++--- test/formatter_test.exs | 12 ++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 560328f..63c4e1b 100644 --- a/README.md +++ b/README.md @@ -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 ``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: diff --git a/lib/formatter.ex b/lib/formatter.ex index 9a051fd..c1c3a6d 100644 --- a/lib/formatter.ex +++ b/lib/formatter.ex @@ -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, @@ -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 diff --git a/test/formatter_test.exs b/test/formatter_test.exs index 0fdb963..ed048d0 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -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)