diff --git a/README.md b/README.md index 03edcc5..6bb75bc 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Add the following parameters. - It's an optional setting for skipping `MIX_ENV=test` part when executing `mix coveralls` tasks. - `test_coverage: [test_task: "espec"]` if you use Espec instead of default ExUnit. - `:excoveralls` in the deps function. -- `Applicaton.put_env(:excoveralls, :base_path, "/bash/path")` an optional config if you want to set the application root path explicitly. By default this is the directory that the mix.exs file is in. +- `Application.put_env(:excoveralls, :base_path, "/bash/path")` an optional config if you want to set the application root path explicitly. By default this is the directory that the mix.exs file is in. ```elixir def project do @@ -133,6 +133,17 @@ Usage: mix coveralls -o (--output-dir) Write coverage information to output dir. -u (--umbrella) Show overall coverage for umbrella project. -v (--verbose) Show json string for posting. + --subdir Git repo sub directory: This will be added to the the front of file path, use if your covered + file paths reside within a subfolder of the git repo. Example: If your source file path is + "test.ex", and your git repo root is one directory up making the file's relative path + "src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io) + --rootdir This will be stripped from the file path in order to resolve the relative path of this repo's + files. It should be the path to your git repo's root on your CI build environment. This is not + needed if your source file path is already relative. It's used to pull the source file from the + github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex", + and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from + coveralls.io) + Usage: mix coveralls.detail [--filter file-name-pattern] Used to display coverage with detail @@ -158,16 +169,6 @@ Usage: mix coveralls.post -s (--sha) Commit SHA (required when not using Travis) --build Service number ('BUILDS' column at coveralls.io page) --parallel coveralls.io 'parallel' option (See coveralls.io API Reference) - --subdir Git repo sub directory: This will be added to the the front of file path, use if your covered - file paths reside within a subfolder of the git repo. Example: If your source file path is - "test.ex", and your git repo root is one directory up making the file's relative path - "src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io) - --rootdir This will be stripped from the file path in order to resolve the relative path of this repo's - files. It should be the path to your git repo's root on your CI build environment. This is not - needed if your source file path is already relative. It's used to pull the source file from the - github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex", - and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from - coveralls.io) ``` ### [mix coveralls.travis] Post coverage from travis diff --git a/lib/excoveralls.ex b/lib/excoveralls.ex index 97480ee..b97c65e 100644 --- a/lib/excoveralls.ex +++ b/lib/excoveralls.ex @@ -44,20 +44,26 @@ defmodule ExCoveralls do end def execute(options, compile_path) do - stats = Cover.modules() |> Stats.report() |> Enum.map(&Enum.into(&1, %{})) + stats = + Cover.modules() |> + Stats.report() |> + Enum.map(&Enum.into(&1, %{})) if options[:umbrella] do store_stats(stats, options, compile_path) else - analyze(stats, options[:type] || "local", options) + Stats.update_paths(stats, options) |> + analyze(options[:type] || "local", options) end end defp store_stats(stats, options, compile_path) do {sub_app_name, _sub_app_path} = ExCoveralls.SubApps.find(options[:sub_apps], compile_path) - stats = Stats.append_sub_app_name(stats, sub_app_name, options[:apps_path]) - Enum.each(stats, fn(stat) -> StatServer.add(stat) end) + + Stats.append_sub_app_name(stats, sub_app_name, options[:apps_path]) |> + Stats.update_paths(options) |> + Enum.each(fn(stat) -> StatServer.add(stat) end) end @doc """ diff --git a/lib/excoveralls/stats.ex b/lib/excoveralls/stats.ex index b9cc992..75e0b20 100644 --- a/lib/excoveralls/stats.ex +++ b/lib/excoveralls/stats.ex @@ -96,6 +96,25 @@ defmodule ExCoveralls.Stats do end) end + @doc """ + Updates the paths to take into account the subdir and rootdir options + """ + def update_paths(stats, options) do + sub_dir_set? = not (options[:subdir] in [nil, ""]) + root_dir_set? = not (options[:rootdir] in [nil, ""]) + + cond do + sub_dir_set? -> + stats + |> Enum.map(fn m -> %{m | name: options[:subdir] <> Map.get(m, :name)} end) + + root_dir_set? -> + stats + |> Enum.map(fn m -> %{m | name: String.trim_leading(Map.get(m, :name), options[:rootdir])} end) + true -> stats + end + end + @doc """ Returns total line counts of the specified source file. """ diff --git a/lib/excoveralls/task/util.ex b/lib/excoveralls/task/util.ex index 0b4f123..91b6c82 100644 --- a/lib/excoveralls/task/util.ex +++ b/lib/excoveralls/task/util.ex @@ -16,6 +16,16 @@ Usage: mix coveralls -o (--output-dir) Write coverage information to output dir. -u (--umbrella) Show overall coverage for umbrella project. -v (--verbose) Show json string for posting. + --subdir Git repo sub directory: This will be added to the the front of file path, use if your covered + file paths reside within a subfolder of the git repo. Example: If your source file path is + "test.ex", and your git repo root is one directory up making the file's relative path + "src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io) + --rootdir This will be stripped from the file path in order to resolve the relative path of this repo's + files. It should be the path to your git repo's root on your CI build environment. This is not + needed if your source file path is already relative. It's used to pull the source file from the + github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex", + and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from + coveralls.io) Usage: mix coveralls.detail [--filter file-name-pattern] Used to display coverage with detail @@ -44,16 +54,6 @@ Usage: mix coveralls.post -s (--sha) Commit SHA (required when not using Travis) --build Service number ('BUILDS' column at coveralls.io page) --parallel coveralls.io 'parallel' option (See coveralls.io API Reference) - --subdir Git repo sub directory: This will be added to the the front of file path, use if your covered - file paths reside within a subfolder of the git repo. Example: If your source file path is - "test.ex", and your git repo root is one directory up making the file's relative path - "src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io) - --rootdir This will be stripped from the file path in order to resolve the relative path of this repo's - files. It should be the path to your git repo's root on your CI build environment. This is not - needed if your source file path is already relative. It's used to pull the source file from the - github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex", - and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from - coveralls.io) """ end end diff --git a/lib/mix/tasks.ex b/lib/mix/tasks.ex index 5523421..e8d8342 100644 --- a/lib/mix/tasks.ex +++ b/lib/mix/tasks.ex @@ -33,7 +33,7 @@ defmodule Mix.Tasks.Coveralls do message: "Please specify 'test_coverage: [tool: ExCoveralls]' in the 'project' section of mix.exs" end - switches = [filter: :string, umbrella: :boolean, verbose: :boolean, pro: :boolean, parallel: :boolean, sort: :string, output_dir: :string] + switches = [filter: :string, umbrella: :boolean, verbose: :boolean, pro: :boolean, parallel: :boolean, sort: :string, output_dir: :string, subdir: :string, rootdir: :string] aliases = [f: :filter, u: :umbrella, v: :verbose, o: :output_dir] {args, common_options} = parse_common_options(args, switches: switches, aliases: aliases) all_options = options ++ common_options @@ -58,7 +58,6 @@ defmodule Mix.Tasks.Coveralls do ExCoveralls.StatServer.get |> MapSet.to_list - |> get_stats(all_options) |> ExCoveralls.analyze(type, options) end end @@ -94,22 +93,6 @@ defmodule Mix.Tasks.Coveralls do {remaining, common_options} end - def get_stats(stats, options) do - sub_dir_set? = not (options[:subdir] in [nil, ""]) - root_dir_set? = not (options[:rootdir] in [nil, ""]) - - cond do - sub_dir_set? -> - stats - |> Enum.map(fn m -> %{m | name: options[:subdir] <> Map.get(m, :name)} end) - - root_dir_set? -> - stats - |> Enum.map(fn m -> %{m | name: String.trim_leading(Map.get(m, :name), options[:rootdir])} end) - true -> stats - end - end - defmodule Detail do @moduledoc """ Provides an entry point for displaying coveralls information diff --git a/test/mix/tasks_test.exs b/test/mix/tasks_test.exs index ce94a96..68f5c99 100644 --- a/test/mix/tasks_test.exs +++ b/test/mix/tasks_test.exs @@ -313,53 +313,4 @@ defmodule Mix.Tasks.CoverallsTest do System.put_env("COVERALLS_REPO_TOKEN", org_name) end end - - describe "get_stats/2" do - @test_path_1 "apps/umbrella1_app1/lib/umbrella1_app1.ex" - @test_path_2 "apps/umbrella1_app2/lib/umbrella1_app2.ex" - @test_stats [ - %{ - coverage: [], - name: @test_path_1, - source: "dummy_source2" - }, - %{ - coverage: [], - name: @test_path_2, - source: "dummy_source1" - } - ] - - test "subdir is added to filepath" do - result = - Mix.Tasks.Coveralls.get_stats(@test_stats, [rootdir: "", subdir: "umbrella1/"]) - |> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1/") end) - |> Enum.all?(fn v -> v end) - assert result - end - - test "rootdir is removed from filepath" do - result = - Mix.Tasks.Coveralls.get_stats(@test_stats, [rootdir: "apps/", subdir: ""]) - |> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1_app") end) - |> Enum.all?(fn v -> v end) - assert result - end - - test "filepath is untouched when no options for rootdir/subdir" do - result = - Mix.Tasks.Coveralls.get_stats(@test_stats, [rootdir: "", subdir: ""]) - |> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end) - |> Enum.all?(fn v -> v end) - assert result - end - - test "filepath is untouched when options for rootdir/subdir does not exist" do - result = - Mix.Tasks.Coveralls.get_stats(@test_stats, []) - |> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end) - |> Enum.all?(fn v -> v end) - assert result - end - end end diff --git a/test/stats_test.exs b/test/stats_test.exs index d02ba47..e6d6867 100644 --- a/test/stats_test.exs +++ b/test/stats_test.exs @@ -136,4 +136,52 @@ defmodule ExCoveralls.StatsTest do assert(results.coverage == 66.7) end + describe "update_stats/2" do + @test_path_1 "apps/umbrella1_app1/lib/umbrella1_app1.ex" + @test_path_2 "apps/umbrella1_app2/lib/umbrella1_app2.ex" + @test_stats [ + %{ + coverage: [], + name: @test_path_1, + source: "dummy_source2" + }, + %{ + coverage: [], + name: @test_path_2, + source: "dummy_source1" + } + ] + + test "subdir is added to filepath" do + result = + Stats.update_paths(@test_stats, [rootdir: "", subdir: "umbrella1/"]) + |> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1/") end) + |> Enum.all?(fn v -> v end) + assert result + end + + test "rootdir is removed from filepath" do + result = + Stats.update_paths(@test_stats, [rootdir: "apps/", subdir: ""]) + |> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1_app") end) + |> Enum.all?(fn v -> v end) + assert result + end + + test "filepath is untouched when no options for rootdir/subdir" do + result = + Stats.update_paths(@test_stats, [rootdir: "", subdir: ""]) + |> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end) + |> Enum.all?(fn v -> v end) + assert result + end + + test "filepath is untouched when options for rootdir/subdir does not exist" do + result = + Stats.update_paths(@test_stats, []) + |> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end) + |> Enum.all?(fn v -> v end) + assert result + end + end end