forked from parroty/excoveralls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.ex
311 lines (255 loc) · 8.65 KB
/
tasks.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
defmodule Mix.Tasks.Coveralls do
@moduledoc """
Provides an entry point for displaying
coveralls.io from local server.
"""
use Mix.Task
@shortdoc "Display the test coverage"
@preferred_cli_env :test
defmodule Runner do
def run(task, args) do
Mix.Task.run(task, args)
end
end
def run(args) do
{options, _, _} = OptionParser.parse(args, switches: [help: :boolean], aliases: [h: :help])
if options[:help] do
ExCoveralls.Task.Util.print_help_message
else
do_run(args, [type: "local"])
end
end
@doc """
Provides the logic to switch the parameters for ExCoveralls.run/3.
"""
def do_run(args, options) do
if Mix.Project.config[:test_coverage][:tool] != ExCoveralls do
raise ExCoveralls.InvalidConfigError,
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, subdir: :string, rootdir: :string, flagname: :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
test_task = Mix.Project.config[:test_coverage][:test_task] || "test"
all_options =
if all_options[:umbrella] do
sub_apps = ExCoveralls.SubApps.parse(Mix.Dep.Umbrella.loaded)
all_options ++ [sub_apps: sub_apps, apps_path: Mix.Project.config[:apps_path]]
else
all_options
end
ExCoveralls.ConfServer.start
ExCoveralls.ConfServer.set(all_options ++ [args: args])
ExCoveralls.StatServer.start
Runner.run(test_task, ["--cover"] ++ args)
if all_options[:umbrella] do
type = options[:type] || "local"
ExCoveralls.StatServer.get
|> MapSet.to_list
|> ExCoveralls.analyze(type, options)
end
end
def parse_common_options(args, common_options) do
common_switches = Keyword.get(common_options, :switches, [])
common_aliases = Keyword.get(common_options, :aliases, [])
{common_options, _remaining, _invalid} = OptionParser.parse(args, common_options)
# the switches that excoveralls supports
supported_switches = Enum.map(Keyword.keys(common_switches), fn(s) -> String.replace("--#{s}", "_", "-") end)
++ Enum.map(Keyword.keys(common_aliases), fn(s) -> "-#{s}" end)
# Get the remaining args to pass onto cover, excluding ExCoveralls-specific args.
# Not using OptionParser for this because it splits things up in unfortunate ways.
{remaining, _} = List.foldl(args, {[], nil}, fn(arg, {acc, last}) ->
cond do
# don't include switches for ExCoveralls
Enum.member?(supported_switches, arg) -> {acc, arg}
# also drop any values that follow ExCoveralls switches
!String.starts_with?(arg, "-") && Enum.member?(supported_switches, last) -> {acc, nil}
# leaving just the switches and values intended for cover
true -> {acc ++ [arg], nil}
end
end)
sub_dir_set? = not (common_options[:subdir] in [nil, ""])
root_dir_set? = not (common_options[:rootdir] in [nil, ""])
if sub_dir_set? and root_dir_set? do
raise ExCoveralls.InvalidOptionError,
message: "subdir and rootdir options are exclusive. please specify only one of them."
end
{remaining, common_options}
end
defmodule Detail do
@moduledoc """
Provides an entry point for displaying coveralls information
with source code details.
"""
use Mix.Task
@shortdoc "Display the test coverage with source detail"
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [ type: "local", detail: true ])
end
end
defmodule Html do
@moduledoc """
Provides an entry point for displaying coveralls information
with source code details as an HTML report.
"""
use Mix.Task
@shortdoc "Display the test coverage with source detail as an HTML report"
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [ type: "html" ])
end
end
defmodule Xml do
@moduledoc """
Provides an entry point for outputting coveralls information
as a XML file.
"""
use Mix.Task
@shortdoc "Output the test coverage as a XML file"
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [ type: "xml" ])
end
end
defmodule Json do
@moduledoc """
Provides an entry point for outputting coveralls information
as a JSON file.
"""
use Mix.Task
@shortdoc "Output the test coverage as a JSON file"
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [ type: "json" ])
end
end
defmodule Lcov do
@moduledoc """
Provides an entry point for outputting coveralls information
as a Lcov file.
"""
use Mix.Task
@shortdoc "Output the test coverage as a Lcov file"
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [ type: "lcov" ])
end
end
defmodule Travis do
@moduledoc """
Provides an entry point for travis's script.
"""
use Mix.Task
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [type: "travis"])
end
end
defmodule Github do
@moduledoc """
Provides an entry point for github's script.
"""
use Mix.Task
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [type: "github"])
end
end
defmodule Gitlab do
@moduledoc """
Provides an entry point for gitlab's script.
"""
use Mix.Task
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, type: "gitlab")
end
end
defmodule Circle do
@moduledoc """
Provides an entry point for CircleCI's script.
"""
use Mix.Task
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [type: "circle"])
end
end
defmodule Semaphore do
@moduledoc """
Provides an entry point for SemaphoreCI's script.
"""
use Mix.Task
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [type: "semaphore"])
end
end
defmodule Drone do
@moduledoc """
Provides an entry point for DroneCI's script.
"""
use Mix.Task
@preferred_cli_env :test
def run(args) do
Mix.Tasks.Coveralls.do_run(args, [type: "drone"])
end
end
defmodule Post do
@moduledoc """
Provides an entry point for posting test coverage to
coveralls.io from the local server.
"""
use Mix.Task
@shortdoc "Post the test coverage to coveralls"
@default_service_name "excoveralls"
@preferred_cli_env :test
def run(args) do
switches = [
filter: :string,
umbrella: :boolean,
verbose: :boolean,
pro: :boolean,
parallel: :boolean,
rootdir: :string,
subdir: :string,
build: :string,
]
aliases = [f: :filter, u: :umbrella, v: :verbose]
{remaining, options} = Mix.Tasks.Coveralls.parse_common_options(
args,
switches: switches ++ [sha: :string, token: :string, committer: :string, branch: :string, message: :string, name: :string, flagname: :string],
aliases: aliases ++ [n: :name, b: :branch, c: :committer, m: :message, s: :sha, t: :token]
)
Mix.Tasks.Coveralls.do_run(remaining,
[ type: "post",
endpoint: Application.get_env(:excoveralls, :endpoint),
token: extract_token(options),
service_name: extract_service_name(options),
service_number: options[:build] || "",
branch: options[:branch] || "",
committer: options[:committer] || "",
sha: options[:sha] || "",
message: options[:message] || "[no commit message]",
umbrella: options[:umbrella],
verbose: options[:verbose],
parallel: options[:parallel],
flag_name: options[:flagname] || "",
rootdir: options[:rootdir] || "",
subdir: options[:subdir] || ""
])
end
def extract_service_name(options) do
options[:name] || System.get_env("COVERALLS_SERVICE_NAME") || @default_service_name
end
def extract_token(options) do
case options[:token] || System.get_env("COVERALLS_REPO_TOKEN") || "" do
"" -> raise ExCoveralls.InvalidOptionError,
message: "Token is NOT specified in the argument nor environment variable."
token -> token
end
end
end
end