Skip to content

Commit f8b4d0e

Browse files
authored
Merge pull request #289 from duffelhq/always-allow-subdir-rootdir
allow subdir and rootdir to be applied to all tasks and always apply to paths
2 parents d715fb2 + 7665099 commit f8b4d0e

7 files changed

Lines changed: 100 additions & 92 deletions

File tree

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Add the following parameters.
3131
- It's an optional setting for skipping `MIX_ENV=test` part when executing `mix coveralls` tasks.
3232
- `test_coverage: [test_task: "espec"]` if you use Espec instead of default ExUnit.
3333
- `:excoveralls` in the deps function.
34-
- `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.
34+
- `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.
3535

3636
```elixir
3737
def project do
@@ -133,6 +133,17 @@ Usage: mix coveralls <Options>
133133
-o (--output-dir) Write coverage information to output dir.
134134
-u (--umbrella) Show overall coverage for umbrella project.
135135
-v (--verbose) Show json string for posting.
136+
--subdir Git repo sub directory: This will be added to the the front of file path, use if your covered
137+
file paths reside within a subfolder of the git repo. Example: If your source file path is
138+
"test.ex", and your git repo root is one directory up making the file's relative path
139+
"src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io)
140+
--rootdir This will be stripped from the file path in order to resolve the relative path of this repo's
141+
files. It should be the path to your git repo's root on your CI build environment. This is not
142+
needed if your source file path is already relative. It's used to pull the source file from the
143+
github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex",
144+
and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from
145+
coveralls.io)
146+
136147

137148
Usage: mix coveralls.detail [--filter file-name-pattern]
138149
Used to display coverage with detail
@@ -158,16 +169,6 @@ Usage: mix coveralls.post <Options>
158169
-s (--sha) Commit SHA (required when not using Travis)
159170
--build Service number ('BUILDS' column at coveralls.io page)
160171
--parallel coveralls.io 'parallel' option (See coveralls.io API Reference)
161-
--subdir Git repo sub directory: This will be added to the the front of file path, use if your covered
162-
file paths reside within a subfolder of the git repo. Example: If your source file path is
163-
"test.ex", and your git repo root is one directory up making the file's relative path
164-
"src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io)
165-
--rootdir This will be stripped from the file path in order to resolve the relative path of this repo's
166-
files. It should be the path to your git repo's root on your CI build environment. This is not
167-
needed if your source file path is already relative. It's used to pull the source file from the
168-
github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex",
169-
and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from
170-
coveralls.io)
171172
```
172173
173174
### [mix coveralls.travis] Post coverage from travis

lib/excoveralls.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,26 @@ defmodule ExCoveralls do
4444
end
4545

4646
def execute(options, compile_path) do
47-
stats = Cover.modules() |> Stats.report() |> Enum.map(&Enum.into(&1, %{}))
47+
stats =
48+
Cover.modules() |>
49+
Stats.report() |>
50+
Enum.map(&Enum.into(&1, %{}))
4851

4952
if options[:umbrella] do
5053
store_stats(stats, options, compile_path)
5154
else
52-
analyze(stats, options[:type] || "local", options)
55+
Stats.update_paths(stats, options) |>
56+
analyze(options[:type] || "local", options)
5357
end
5458
end
5559

5660
defp store_stats(stats, options, compile_path) do
5761
{sub_app_name, _sub_app_path} =
5862
ExCoveralls.SubApps.find(options[:sub_apps], compile_path)
59-
stats = Stats.append_sub_app_name(stats, sub_app_name, options[:apps_path])
60-
Enum.each(stats, fn(stat) -> StatServer.add(stat) end)
63+
64+
Stats.append_sub_app_name(stats, sub_app_name, options[:apps_path]) |>
65+
Stats.update_paths(options) |>
66+
Enum.each(fn(stat) -> StatServer.add(stat) end)
6167
end
6268

6369
@doc """

lib/excoveralls/stats.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ defmodule ExCoveralls.Stats do
9696
end)
9797
end
9898

99+
@doc """
100+
Updates the paths to take into account the subdir and rootdir options
101+
"""
102+
def update_paths(stats, options) do
103+
sub_dir_set? = not (options[:subdir] in [nil, ""])
104+
root_dir_set? = not (options[:rootdir] in [nil, ""])
105+
106+
cond do
107+
sub_dir_set? ->
108+
stats
109+
|> Enum.map(fn m -> %{m | name: options[:subdir] <> Map.get(m, :name)} end)
110+
111+
root_dir_set? ->
112+
stats
113+
|> Enum.map(fn m -> %{m | name: String.trim_leading(Map.get(m, :name), options[:rootdir])} end)
114+
true -> stats
115+
end
116+
end
117+
99118
@doc """
100119
Returns total line counts of the specified source file.
101120
"""

lib/excoveralls/task/util.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Usage: mix coveralls <Options>
1616
-o (--output-dir) Write coverage information to output dir.
1717
-u (--umbrella) Show overall coverage for umbrella project.
1818
-v (--verbose) Show json string for posting.
19+
--subdir Git repo sub directory: This will be added to the the front of file path, use if your covered
20+
file paths reside within a subfolder of the git repo. Example: If your source file path is
21+
"test.ex", and your git repo root is one directory up making the file's relative path
22+
"src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io)
23+
--rootdir This will be stripped from the file path in order to resolve the relative path of this repo's
24+
files. It should be the path to your git repo's root on your CI build environment. This is not
25+
needed if your source file path is already relative. It's used to pull the source file from the
26+
github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex",
27+
and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from
28+
coveralls.io)
1929
2030
Usage: mix coveralls.detail [--filter file-name-pattern]
2131
Used to display coverage with detail
@@ -44,16 +54,6 @@ Usage: mix coveralls.post <Options>
4454
-s (--sha) Commit SHA (required when not using Travis)
4555
--build Service number ('BUILDS' column at coveralls.io page)
4656
--parallel coveralls.io 'parallel' option (See coveralls.io API Reference)
47-
--subdir Git repo sub directory: This will be added to the the front of file path, use if your covered
48-
file paths reside within a subfolder of the git repo. Example: If your source file path is
49-
"test.ex", and your git repo root is one directory up making the file's relative path
50-
"src/lib/test.ex", then the sub directory should be: "src/lib" (from coveralls.io)
51-
--rootdir This will be stripped from the file path in order to resolve the relative path of this repo's
52-
files. It should be the path to your git repo's root on your CI build environment. This is not
53-
needed if your source file path is already relative. It's used to pull the source file from the
54-
github repo, so must be exact. Example: If your source file path is "/home/runs/app/test.ex",
55-
and your git repo resides in "app", then the root path should be: "/home/runs/app/" (from
56-
coveralls.io)
5757
"""
5858
end
5959
end

lib/mix/tasks.ex

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Mix.Tasks.Coveralls do
3333
message: "Please specify 'test_coverage: [tool: ExCoveralls]' in the 'project' section of mix.exs"
3434
end
3535

36-
switches = [filter: :string, umbrella: :boolean, verbose: :boolean, pro: :boolean, parallel: :boolean, sort: :string, output_dir: :string]
36+
switches = [filter: :string, umbrella: :boolean, verbose: :boolean, pro: :boolean, parallel: :boolean, sort: :string, output_dir: :string, subdir: :string, rootdir: :string]
3737
aliases = [f: :filter, u: :umbrella, v: :verbose, o: :output_dir]
3838
{args, common_options} = parse_common_options(args, switches: switches, aliases: aliases)
3939
all_options = options ++ common_options
@@ -58,7 +58,6 @@ defmodule Mix.Tasks.Coveralls do
5858

5959
ExCoveralls.StatServer.get
6060
|> MapSet.to_list
61-
|> get_stats(all_options)
6261
|> ExCoveralls.analyze(type, options)
6362
end
6463
end
@@ -94,22 +93,6 @@ defmodule Mix.Tasks.Coveralls do
9493
{remaining, common_options}
9594
end
9695

97-
def get_stats(stats, options) do
98-
sub_dir_set? = not (options[:subdir] in [nil, ""])
99-
root_dir_set? = not (options[:rootdir] in [nil, ""])
100-
101-
cond do
102-
sub_dir_set? ->
103-
stats
104-
|> Enum.map(fn m -> %{m | name: options[:subdir] <> Map.get(m, :name)} end)
105-
106-
root_dir_set? ->
107-
stats
108-
|> Enum.map(fn m -> %{m | name: String.trim_leading(Map.get(m, :name), options[:rootdir])} end)
109-
true -> stats
110-
end
111-
end
112-
11396
defmodule Detail do
11497
@moduledoc """
11598
Provides an entry point for displaying coveralls information

test/mix/tasks_test.exs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -313,53 +313,4 @@ defmodule Mix.Tasks.CoverallsTest do
313313
System.put_env("COVERALLS_REPO_TOKEN", org_name)
314314
end
315315
end
316-
317-
describe "get_stats/2" do
318-
@test_path_1 "apps/umbrella1_app1/lib/umbrella1_app1.ex"
319-
@test_path_2 "apps/umbrella1_app2/lib/umbrella1_app2.ex"
320-
@test_stats [
321-
%{
322-
coverage: [],
323-
name: @test_path_1,
324-
source: "dummy_source2"
325-
},
326-
%{
327-
coverage: [],
328-
name: @test_path_2,
329-
source: "dummy_source1"
330-
}
331-
]
332-
333-
test "subdir is added to filepath" do
334-
result =
335-
Mix.Tasks.Coveralls.get_stats(@test_stats, [rootdir: "", subdir: "umbrella1/"])
336-
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1/") end)
337-
|> Enum.all?(fn v -> v end)
338-
assert result
339-
end
340-
341-
test "rootdir is removed from filepath" do
342-
result =
343-
Mix.Tasks.Coveralls.get_stats(@test_stats, [rootdir: "apps/", subdir: ""])
344-
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1_app") end)
345-
|> Enum.all?(fn v -> v end)
346-
assert result
347-
end
348-
349-
test "filepath is untouched when no options for rootdir/subdir" do
350-
result =
351-
Mix.Tasks.Coveralls.get_stats(@test_stats, [rootdir: "", subdir: ""])
352-
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end)
353-
|> Enum.all?(fn v -> v end)
354-
assert result
355-
end
356-
357-
test "filepath is untouched when options for rootdir/subdir does not exist" do
358-
result =
359-
Mix.Tasks.Coveralls.get_stats(@test_stats, [])
360-
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end)
361-
|> Enum.all?(fn v -> v end)
362-
assert result
363-
end
364-
end
365316
end

test/stats_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,52 @@ defmodule ExCoveralls.StatsTest do
136136
assert(results.coverage == 66.7)
137137
end
138138

139+
describe "update_stats/2" do
140+
@test_path_1 "apps/umbrella1_app1/lib/umbrella1_app1.ex"
141+
@test_path_2 "apps/umbrella1_app2/lib/umbrella1_app2.ex"
142+
@test_stats [
143+
%{
144+
coverage: [],
145+
name: @test_path_1,
146+
source: "dummy_source2"
147+
},
148+
%{
149+
coverage: [],
150+
name: @test_path_2,
151+
source: "dummy_source1"
152+
}
153+
]
154+
155+
test "subdir is added to filepath" do
156+
result =
157+
Stats.update_paths(@test_stats, [rootdir: "", subdir: "umbrella1/"])
158+
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1/") end)
159+
|> Enum.all?(fn v -> v end)
160+
assert result
161+
end
162+
163+
test "rootdir is removed from filepath" do
164+
result =
165+
Stats.update_paths(@test_stats, [rootdir: "apps/", subdir: ""])
166+
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "umbrella1_app") end)
167+
|> Enum.all?(fn v -> v end)
168+
assert result
169+
end
170+
171+
test "filepath is untouched when no options for rootdir/subdir" do
172+
result =
173+
Stats.update_paths(@test_stats, [rootdir: "", subdir: ""])
174+
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end)
175+
|> Enum.all?(fn v -> v end)
176+
assert result
177+
end
178+
179+
test "filepath is untouched when options for rootdir/subdir does not exist" do
180+
result =
181+
Stats.update_paths(@test_stats, [])
182+
|> Enum.map(fn m -> assert String.starts_with?(m[:name], "apps/umbrella1_app") end)
183+
|> Enum.all?(fn v -> v end)
184+
assert result
185+
end
186+
end
139187
end

0 commit comments

Comments
 (0)