Skip to content

Commit

Permalink
allow subdir and rootdir to be applied to all tasks and apply even if…
Browse files Browse the repository at this point in the history
… umbrella not selected
  • Loading branch information
SamHutchings committed Sep 26, 2022
1 parent d715fb2 commit 7665099
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 92 deletions.
23 changes: 12 additions & 11 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +133,17 @@ Usage: mix coveralls <Options>
-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
Expand All @@ -158,16 +169,6 @@ Usage: mix coveralls.post <Options>
-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
Expand Down
14 changes: 10 additions & 4 deletions lib/excoveralls.ex
Expand Up @@ -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 """
Expand Down
19 changes: 19 additions & 0 deletions lib/excoveralls/stats.ex
Expand Up @@ -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.
"""
Expand Down
20 changes: 10 additions & 10 deletions lib/excoveralls/task/util.ex
Expand Up @@ -16,6 +16,16 @@ Usage: mix coveralls <Options>
-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
Expand Down Expand Up @@ -44,16 +54,6 @@ Usage: mix coveralls.post <Options>
-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
19 changes: 1 addition & 18 deletions lib/mix/tasks.ex
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
49 changes: 0 additions & 49 deletions test/mix/tasks_test.exs
Expand Up @@ -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
48 changes: 48 additions & 0 deletions test/stats_test.exs
Expand Up @@ -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

0 comments on commit 7665099

Please sign in to comment.