path_coverage 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +286 -199
- data/exe/path_coverage_report +4 -0
- data/lib/path_coverage/call_trace.rb +88 -0
- data/lib/path_coverage/calls.rb +49 -43
- data/lib/path_coverage/card_context.rb +104 -0
- data/lib/path_coverage/card_forks.rb +44 -0
- data/lib/path_coverage/card_gaps.rb +125 -0
- data/lib/path_coverage/card_scopes.rb +37 -0
- data/lib/path_coverage/card_tree.rb +79 -66
- data/lib/path_coverage/card_way_lists.rb +72 -0
- data/lib/path_coverage/card_ways.rb +123 -67
- data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
- data/lib/path_coverage/condition.rb +14 -14
- data/lib/path_coverage/configuration.rb +7 -4
- data/lib/path_coverage/configuration_limits.rb +52 -0
- data/lib/path_coverage/configuration_paths.rb +7 -7
- data/lib/path_coverage/console_summary.rb +24 -22
- data/lib/path_coverage/{site.rb → decision.rb} +2 -2
- data/lib/path_coverage/decision_locations.rb +76 -0
- data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
- data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
- data/lib/path_coverage/fold_branches.rb +18 -0
- data/lib/path_coverage/fold_cells.rb +42 -0
- data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
- data/lib/path_coverage/gaps.rb +33 -15
- data/lib/path_coverage/json_reader.rb +54 -19
- data/lib/path_coverage/json_reports.rb +102 -0
- data/lib/path_coverage/json_writer.rb +27 -12
- data/lib/path_coverage/kernel.rb +56 -58
- data/lib/path_coverage/kinds/base.rb +16 -16
- data/lib/path_coverage/kinds/block.rb +16 -15
- data/lib/path_coverage/kinds/case.rb +14 -12
- data/lib/path_coverage/kinds/chain.rb +40 -11
- data/lib/path_coverage/kinds/choice.rb +4 -3
- data/lib/path_coverage/kinds/contains.rb +40 -32
- data/lib/path_coverage/kinds/safe.rb +38 -26
- data/lib/path_coverage/kinds/write.rb +6 -6
- data/lib/path_coverage/kinds.rb +2 -2
- data/lib/path_coverage/leaving.rb +44 -0
- data/lib/path_coverage/loop_forks.rb +57 -0
- data/lib/path_coverage/loop_nodes.rb +55 -0
- data/lib/path_coverage/manifest.rb +92 -58
- data/lib/path_coverage/manifest_blocks.rb +5 -26
- data/lib/path_coverage/manifest_case.rb +2 -2
- data/lib/path_coverage/manifest_chains.rb +8 -8
- data/lib/path_coverage/manifest_choices.rb +23 -20
- data/lib/path_coverage/manifest_disabled.rb +97 -0
- data/lib/path_coverage/manifest_folding.rb +43 -40
- data/lib/path_coverage/manifest_ignoring.rb +6 -6
- data/lib/path_coverage/manifest_writes.rb +11 -10
- data/lib/path_coverage/measured_by.rb +20 -0
- data/lib/path_coverage/merge.rb +7 -7
- data/lib/path_coverage/method_loops.rb +128 -0
- data/lib/path_coverage/method_statements.rb +130 -127
- data/lib/path_coverage/method_ways.rb +142 -0
- data/lib/path_coverage/paths_report.rb +16 -16
- data/lib/path_coverage/recorder.rb +57 -36
- data/lib/path_coverage/recorder_blocks.rb +17 -17
- data/lib/path_coverage/recorder_cases.rb +12 -11
- data/lib/path_coverage/recorder_chains.rb +12 -12
- data/lib/path_coverage/recorder_writes.rb +9 -9
- data/lib/path_coverage/render.rb +58 -51
- data/lib/path_coverage/result.rb +32 -31
- data/lib/path_coverage/rewriter.rb +39 -27
- data/lib/path_coverage/rewriter_nested.rb +13 -13
- data/lib/path_coverage/runs_report.rb +84 -0
- data/lib/path_coverage/source_lines.rb +16 -16
- data/lib/path_coverage/source_report.rb +100 -0
- data/lib/path_coverage/stable_leaves.rb +162 -0
- data/lib/path_coverage/statement_arms.rb +31 -30
- data/lib/path_coverage/summary.rb +4 -62
- data/lib/path_coverage/totals.rb +10 -8
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/walk.rb +106 -0
- data/lib/path_coverage/way_algebras.rb +127 -0
- data/lib/path_coverage/ways_report.rb +91 -0
- data/lib/path_coverage/ways_totals.rb +46 -0
- data/lib/path_coverage/within_methods.rb +135 -80
- metadata +33 -28
- data/lib/path_coverage/card.rb +0 -14
- data/lib/path_coverage/html/file.html +0 -33
- data/lib/path_coverage/html/index.html +0 -29
- data/lib/path_coverage/html/settings.js +0 -50
- data/lib/path_coverage/html/side.js +0 -599
- data/lib/path_coverage/html/sort.js +0 -34
- data/lib/path_coverage/html/style.css +0 -484
- data/lib/path_coverage/html/tabs.js +0 -15
- data/lib/path_coverage/html/theme.js +0 -27
- data/lib/path_coverage/html_branch.rb +0 -30
- data/lib/path_coverage/html_cards.rb +0 -88
- data/lib/path_coverage/html_cells.rb +0 -71
- data/lib/path_coverage/html_file.rb +0 -123
- data/lib/path_coverage/html_index.rb +0 -128
- data/lib/path_coverage/html_index_head.rb +0 -29
- data/lib/path_coverage/html_labels.rb +0 -16
- data/lib/path_coverage/html_methods.rb +0 -66
- data/lib/path_coverage/html_nav.rb +0 -67
- data/lib/path_coverage/html_report.rb +0 -131
- data/lib/path_coverage/html_statements.rb +0 -186
- data/lib/path_coverage/html_tabs.rb +0 -26
- data/lib/path_coverage/html_template.rb +0 -26
data/lib/path_coverage/render.rb
CHANGED
|
@@ -1,88 +1,95 @@
|
|
|
1
|
+
require_relative 'card_context'
|
|
1
2
|
require_relative 'console_summary'
|
|
2
3
|
require_relative 'gaps'
|
|
3
|
-
require_relative 'html_report'
|
|
4
4
|
require_relative 'json_reader'
|
|
5
|
+
require_relative 'json_reports'
|
|
5
6
|
require_relative 'manifest'
|
|
6
7
|
require_relative 'paths_report'
|
|
7
8
|
require_relative 'post'
|
|
8
|
-
require_relative '
|
|
9
|
+
require_relative 'decision_paths'
|
|
9
10
|
require_relative 'within_methods'
|
|
10
11
|
|
|
11
12
|
module PathCoverage
|
|
12
|
-
# The reporters, run from operands alone:
|
|
13
|
-
# run recorded beyond them, they can run in
|
|
13
|
+
# The JSON reports and the reporters, run from operands alone:
|
|
14
|
+
# needing nothing the run recorded beyond them, they can run in
|
|
15
|
+
# another process.
|
|
14
16
|
module Render
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
# reached: as Recorder#reached
|
|
18
|
-
# { full path => {source:,
|
|
17
|
+
# Writes the JSON reports, then runs each reporter chosen in
|
|
18
|
+
# config. operands, and taken's decision_paths: and outside:, are
|
|
19
|
+
# as Result gives them, and its reached: as Recorder#reached
|
|
20
|
+
# gives it; files is { full path => {source:, decisions:} } for
|
|
21
|
+
# each covered file.
|
|
19
22
|
def self.run(operands, files, config, **taken)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
recorded = taken.merge(
|
|
24
|
+
decision_paths: every_decision(
|
|
25
|
+
taken.fetch(:decision_paths, {}), files
|
|
26
|
+
),
|
|
27
|
+
outside: taken.fetch(:outside, {})
|
|
28
|
+
)
|
|
29
|
+
JsonReports.write(operands, files, config, **recorded)
|
|
30
|
+
reporters(operands, files, config, recorded)
|
|
31
|
+
end
|
|
28
32
|
|
|
29
|
-
|
|
33
|
+
# Runs each reporter chosen in config, from operands, files,
|
|
34
|
+
# and recorded, as run has them.
|
|
35
|
+
def self.reporters(operands, files, config, recorded)
|
|
36
|
+
paths, reached = recorded.values_at(:decision_paths, :reached)
|
|
37
|
+
runs = {
|
|
38
|
+
gaps: -> { gaps(operands, files, config, recorded) },
|
|
39
|
+
summary: -> { summary(files, config, paths, reached) },
|
|
40
|
+
ways: -> { print File.read(JsonReports.ways_path(config)) },
|
|
41
|
+
post: -> { post(operands, files, config, paths) }
|
|
42
|
+
}
|
|
43
|
+
runs.slice(*config.reporters).each_value(&:call)
|
|
30
44
|
end
|
|
31
45
|
|
|
32
46
|
# Runs the :post reporter: operands, and the paths report of
|
|
33
|
-
#
|
|
34
|
-
def self.post(operands, files, config,
|
|
35
|
-
report = PathsReport.of(
|
|
47
|
+
# decision_paths in files, with config's fingerprint.
|
|
48
|
+
def self.post(operands, files, config, decision_paths)
|
|
49
|
+
report = PathsReport.of(decision_paths, files)
|
|
36
50
|
url = config.collector_url
|
|
37
51
|
fingerprint = config.fingerprint
|
|
38
52
|
Post.deliver(operands, url, report, fingerprint:)
|
|
39
53
|
end
|
|
40
54
|
|
|
41
|
-
# Runs the :html and :summary reporters, each when chosen.
|
|
42
|
-
def self.html_and_summary(operands, files, config, recorded)
|
|
43
|
-
site_paths, reached = recorded
|
|
44
|
-
reporters = config.reporters
|
|
45
|
-
if reporters.include?(:html)
|
|
46
|
-
HtmlReport.write(operands, files, config, recorded)
|
|
47
|
-
end
|
|
48
|
-
return unless reporters.include?(:summary)
|
|
49
|
-
|
|
50
|
-
cards = WithinMethods.cards(files, config, site_paths, reached)
|
|
51
|
-
summary(config, site_paths, cards)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
55
|
# Prints the summary lines: the paths taken of all possible
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
puts ConsoleSummary.lines(
|
|
56
|
+
# alone, and combined, each def's card of files as decision_paths
|
|
57
|
+
# and reached have it; then any operators ignored.
|
|
58
|
+
def self.summary(files, config, decision_paths, reached)
|
|
59
|
+
cards = WithinMethods.cards(files, config, decision_paths,
|
|
60
|
+
reached)
|
|
61
|
+
puts ConsoleSummary.lines(decision_paths, cards,
|
|
62
|
+
config.groups) +
|
|
61
63
|
ConsoleSummary.ignored_lines(config.ignored)
|
|
62
64
|
end
|
|
63
65
|
|
|
64
|
-
#
|
|
66
|
+
# decision_paths with every decision in files that it lacks, eg
|
|
65
67
|
# from a JSON report with no paths, given each possible
|
|
66
68
|
# path at a count of zero: unable to say which paths such
|
|
67
|
-
# a
|
|
68
|
-
def self.
|
|
69
|
+
# a decision took, the report claims none.
|
|
70
|
+
def self.every_decision(decision_paths, files)
|
|
69
71
|
none = files.values.each_with_object({}) do |parsed, all|
|
|
70
|
-
parsed[:
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
end
|
|
72
|
+
parsed[:decisions].each do |decision, condition|
|
|
73
|
+
possible = DecisionPaths.possible(condition)
|
|
74
|
+
all[decision] = possible.map { |path| { path:, count: 0 } }
|
|
74
75
|
end
|
|
75
76
|
end
|
|
76
|
-
none.merge(
|
|
77
|
+
none.merge(decision_paths)
|
|
77
78
|
end
|
|
78
79
|
|
|
79
|
-
# Prints the gap lines: each path a
|
|
80
|
+
# Prints the gap lines: each path a decision never took, and each
|
|
80
81
|
# path taken that a treatment rules out, as outside lists
|
|
81
|
-
# them, each leaf named by its text in files
|
|
82
|
-
|
|
82
|
+
# them, each leaf named by its text in files; then each path
|
|
83
|
+
# taken, or return or raise reached, that its method rules
|
|
84
|
+
# out, as CardContext.ruled_out finds them in files under
|
|
85
|
+
# config. recorded is as run has it.
|
|
86
|
+
def self.gaps(operands, files, config, recorded)
|
|
83
87
|
texts = PathsReport.texts(files)
|
|
84
|
-
paths =
|
|
88
|
+
paths, outside, reached =
|
|
89
|
+
recorded.values_at(:decision_paths, :outside, :reached)
|
|
85
90
|
lines = Gaps.lines(operands, paths:, texts:, outside:)
|
|
91
|
+
ruled = CardContext.ruled_out(files, config, paths, reached)
|
|
92
|
+
lines += Gaps.ruled_by_method(ruled, texts)
|
|
86
93
|
lines.each { |line| puts line }
|
|
87
94
|
end
|
|
88
95
|
|
data/lib/path_coverage/result.rb
CHANGED
|
@@ -1,78 +1,79 @@
|
|
|
1
|
-
require_relative '
|
|
1
|
+
require_relative 'decision_paths'
|
|
2
2
|
|
|
3
3
|
module PathCoverage
|
|
4
|
-
# What a run found: every
|
|
4
|
+
# What a run found: every decision the manifest knows of, with
|
|
5
5
|
# the counts its operands were recorded with. Every
|
|
6
6
|
# reporter builds on this.
|
|
7
7
|
class Result
|
|
8
8
|
NEVER_RAN = { truthy: 0, falsy: 0 }.freeze
|
|
9
9
|
|
|
10
|
-
#
|
|
11
|
-
# counts is { [
|
|
12
|
-
# {
|
|
10
|
+
# decisions is { decision => Condition }, from the manifest;
|
|
11
|
+
# counts is { [decision, index] => counts } and decision_paths is
|
|
12
|
+
# { decision => { path => evaluations } }, both from the
|
|
13
13
|
# recorder.
|
|
14
|
-
def initialize(
|
|
15
|
-
@
|
|
14
|
+
def initialize(decisions, counts, decision_paths = {})
|
|
15
|
+
@decisions = decisions
|
|
16
16
|
@counts = counts
|
|
17
|
-
@
|
|
17
|
+
@decision_paths = decision_paths
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
# {
|
|
21
|
-
# of each
|
|
22
|
-
# evaluations that took it, zero for one never
|
|
20
|
+
# { decision => [{ path:, count: }, ...] }: every possible path
|
|
21
|
+
# of each decision, as DecisionPaths gives them, with the
|
|
22
|
+
# number of evaluations that took it, zero for one never
|
|
23
|
+
# taken.
|
|
23
24
|
def paths
|
|
24
|
-
@
|
|
25
|
-
taken = @
|
|
26
|
-
all =
|
|
25
|
+
@decisions.to_h do |decision, condition|
|
|
26
|
+
taken = @decision_paths.fetch(decision, {})
|
|
27
|
+
all = DecisionPaths.possible(condition).map do |path|
|
|
27
28
|
{ path:, count: taken.fetch(path, 0) }
|
|
28
29
|
end
|
|
29
|
-
[
|
|
30
|
+
[decision, all]
|
|
30
31
|
end
|
|
31
32
|
end
|
|
32
33
|
|
|
33
|
-
# {
|
|
34
|
-
# that is none of its
|
|
35
|
-
# nil though treated as present; only
|
|
34
|
+
# { decision => { path => evaluations } } of each path taken
|
|
35
|
+
# that is none of its decision's possible paths, eg a &. receiver
|
|
36
|
+
# nil though treated as present; only decisions with one. Such a
|
|
36
37
|
# path shows a treatment to be wrong, so it is reported, never
|
|
37
38
|
# dropped.
|
|
38
39
|
def outside
|
|
39
|
-
all = @
|
|
40
|
-
taken = @
|
|
41
|
-
[
|
|
40
|
+
all = @decisions.to_h do |decision, condition|
|
|
41
|
+
taken = @decision_paths.fetch(decision, {})
|
|
42
|
+
[decision, taken.except(*DecisionPaths.possible(condition))]
|
|
42
43
|
end
|
|
43
44
|
all.reject { |_, paths| paths.empty? }
|
|
44
45
|
end
|
|
45
46
|
|
|
46
|
-
# The paths taken, as Render.run takes them:
|
|
47
|
+
# The paths taken, as Render.run takes them: decision_paths:,
|
|
47
48
|
# as paths gives them, and outside:.
|
|
48
49
|
def taken
|
|
49
|
-
{
|
|
50
|
+
{ decision_paths: paths, outside: }
|
|
50
51
|
end
|
|
51
52
|
|
|
52
|
-
# {
|
|
53
|
+
# { decision => [counts of operand 0, counts of operand 1, ...] },
|
|
53
54
|
# with zero counts for an operand that never ran, and
|
|
54
55
|
# only: naming the one outcome a fixed leaf can have.
|
|
55
56
|
def operands
|
|
56
|
-
@
|
|
57
|
+
@decisions.to_h do |decision, condition|
|
|
57
58
|
leaves = condition.leaves.each_with_index.map do |range, i|
|
|
58
|
-
leaf(
|
|
59
|
+
leaf(decision, condition, range, i)
|
|
59
60
|
end
|
|
60
|
-
[
|
|
61
|
+
[decision, leaves]
|
|
61
62
|
end
|
|
62
63
|
end
|
|
63
64
|
|
|
64
65
|
private
|
|
65
66
|
|
|
66
67
|
# The counts of one leaf, with only: when it is fixed.
|
|
67
|
-
def leaf(
|
|
68
|
-
counts = counts_of(
|
|
68
|
+
def leaf(decision, condition, range, index)
|
|
69
|
+
counts = counts_of(decision, index)
|
|
69
70
|
only = condition.fixed[range]
|
|
70
71
|
only ? counts.merge(only:) : counts
|
|
71
72
|
end
|
|
72
73
|
|
|
73
74
|
# The recorded counts of one operand, or zeros.
|
|
74
|
-
def counts_of(
|
|
75
|
-
@counts.fetch([
|
|
75
|
+
def counts_of(decision, index)
|
|
76
|
+
@counts.fetch([decision, index], NEVER_RAN)
|
|
76
77
|
end
|
|
77
78
|
end
|
|
78
79
|
end
|
|
@@ -7,17 +7,28 @@ module PathCoverage
|
|
|
7
7
|
# outcome when the code runs. It inserts text at the byte
|
|
8
8
|
# ranges the manifest found, and never parses.
|
|
9
9
|
module Rewriter
|
|
10
|
-
# Returns source with each operand in
|
|
11
|
-
# record call, each
|
|
10
|
+
# Returns source with each operand in decisions wrapped in a
|
|
11
|
+
# record call, each decision as a whole in a leave call that
|
|
12
12
|
# enters it first, and each statement in reaches, { name =>
|
|
13
13
|
# offset }, after a reach call that counts its runs. Only
|
|
14
14
|
# text is inserted, never a newline, so backtrace line
|
|
15
15
|
# numbers still match the file on disk.
|
|
16
|
-
def self.rewrite(source,
|
|
17
|
-
inserts =
|
|
18
|
-
|
|
16
|
+
def self.rewrite(source, decisions, reaches = {}, passes = {})
|
|
17
|
+
inserts = decisions.flat_map do |decision, condition|
|
|
18
|
+
decision_inserts(decision, condition)
|
|
19
|
+
end
|
|
20
|
+
apply(source.dup, inserts + reach_inserts(reaches) +
|
|
21
|
+
pass_inserts(passes))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Example: { "k.rb:4:16" => 60 } puts a pass call at 60.
|
|
25
|
+
# The inserts that put a pass call, a statement of its own,
|
|
26
|
+
# first in each loop body in passes, { name => offset }.
|
|
27
|
+
def self.pass_inserts(passes)
|
|
28
|
+
rank = [Float::INFINITY, LAYERS.index(:reach)]
|
|
29
|
+
passes.map do |name, at|
|
|
30
|
+
[at, "PathCoverage.pass(#{name.inspect}); ", rank]
|
|
19
31
|
end
|
|
20
|
-
apply(source.dup, inserts + reach_inserts(reaches))
|
|
21
32
|
end
|
|
22
33
|
|
|
23
34
|
# Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
|
|
@@ -36,58 +47,59 @@ module PathCoverage
|
|
|
36
47
|
end
|
|
37
48
|
end
|
|
38
49
|
|
|
39
|
-
# The inserts for one
|
|
50
|
+
# The inserts for one decision, as its kind gives them, eg its
|
|
40
51
|
# leaves' records and its leave; a block method's call or a
|
|
41
52
|
# write is noted as if nested at leaf 0.
|
|
42
|
-
def self.
|
|
43
|
-
Kinds.of(condition.operator).
|
|
53
|
+
def self.decision_inserts(decision, condition)
|
|
54
|
+
Kinds.of(condition.operator).decision_inserts(decision,
|
|
55
|
+
condition)
|
|
44
56
|
end
|
|
45
57
|
|
|
46
|
-
# The inserts that wrap each of
|
|
47
|
-
# call, innermost of all; a leaf of a nested
|
|
58
|
+
# The inserts that wrap each of decision's leaves in a record
|
|
59
|
+
# call, innermost of all; a leaf of a nested decision that needs
|
|
48
60
|
# a call of its own, eg a &. chain's, in the call
|
|
49
|
-
# RewriterNested gives it, or in none, and that
|
|
61
|
+
# RewriterNested gives it, or in none, and that decision's own
|
|
50
62
|
# inserts too.
|
|
51
|
-
def self.record_inserts(
|
|
52
|
-
nested = RewriterNested.calls(
|
|
63
|
+
def self.record_inserts(decision, condition)
|
|
64
|
+
nested = RewriterNested.calls(decision, condition)
|
|
53
65
|
leaves = condition.leaves
|
|
54
66
|
records = leaves.each_with_index.flat_map do |range, i|
|
|
55
|
-
call = nested.fetch(range) { record_call(
|
|
67
|
+
call = nested.fetch(range) { record_call(decision, i) }
|
|
56
68
|
next [] unless call
|
|
57
69
|
|
|
58
70
|
wrap(call, range, condition.commands.include?(range), :leaf)
|
|
59
71
|
end
|
|
60
|
-
records + RewriterNested.inserts(
|
|
72
|
+
records + RewriterNested.inserts(decision, condition)
|
|
61
73
|
end
|
|
62
74
|
|
|
63
75
|
WORD_OPERATORS = %w[and or].freeze
|
|
64
76
|
|
|
65
|
-
# The inserts that wrap
|
|
77
|
+
# The inserts that wrap decision as a whole in a leave call,
|
|
66
78
|
# outside its leaves' record calls, whose first argument
|
|
67
|
-
# enters the
|
|
79
|
+
# enters the decision before any leaf runs, so each evaluation
|
|
68
80
|
# fills in a trail of its own. An and or or is held in
|
|
69
81
|
# parentheses, being, like a command call, no bare argument.
|
|
70
|
-
def self.trail_inserts(
|
|
71
|
-
enter = "PathCoverage.enter(#{
|
|
82
|
+
def self.trail_inserts(decision, condition)
|
|
83
|
+
enter = "PathCoverage.enter(#{decision.inspect}, " \
|
|
72
84
|
"#{condition.leaves.size})"
|
|
73
85
|
call = "PathCoverage.leave(#{enter}, "
|
|
74
86
|
wrapped = Kinds.of(condition.operator).trailed(condition)
|
|
75
87
|
nested = wrapped.is_a?(Condition)
|
|
76
88
|
word = nested && WORD_OPERATORS.include?(wrapped.operator)
|
|
77
|
-
wrap(call, nested ? wrapped.extent : wrapped, word, :
|
|
89
|
+
wrap(call, nested ? wrapped.extent : wrapped, word, :decision)
|
|
78
90
|
end
|
|
79
91
|
|
|
80
92
|
# The opening of the record call around operand index of
|
|
81
|
-
# the
|
|
82
|
-
def self.record_call(
|
|
83
|
-
"PathCoverage.record(#{
|
|
93
|
+
# the decision.
|
|
94
|
+
def self.record_call(decision, index)
|
|
95
|
+
"PathCoverage.record(#{decision.inspect}, #{index}, "
|
|
84
96
|
end
|
|
85
97
|
|
|
86
98
|
# The layers of inserts, innermost first, that break a tie
|
|
87
99
|
# between inserts wrapping the same range: a leaf's call, a
|
|
88
|
-
# nested kind's call around its whole, eg end_items, a
|
|
100
|
+
# nested kind's call around its whole, eg end_items, a decision's
|
|
89
101
|
# leave, and a statement's reach.
|
|
90
|
-
LAYERS = %i[leaf kind
|
|
102
|
+
LAYERS = %i[leaf kind decision reach].freeze
|
|
91
103
|
|
|
92
104
|
# The two inserts that wrap the code at range in call. Their
|
|
93
105
|
# order, [size, rank], nests them among inserts at the same
|
|
@@ -112,7 +124,7 @@ module PathCoverage
|
|
|
112
124
|
# opening inserts go in lowest order first, leaving the
|
|
113
125
|
# highest outermost, then closing ones highest order first,
|
|
114
126
|
# so the innermost call closes first. Eg a ternary whose
|
|
115
|
-
# predicate is a leaf: the
|
|
127
|
+
# predicate is a leaf: the decision's leave, wrapping the whole
|
|
116
128
|
# ternary, opens outside the record wrapping its predicate.
|
|
117
129
|
def self.apply(source, inserts)
|
|
118
130
|
sorted = inserts.sort_by do |offset, _, order, close|
|
|
@@ -2,38 +2,38 @@ require_relative 'condition'
|
|
|
2
2
|
require_relative 'kinds'
|
|
3
3
|
|
|
4
4
|
module PathCoverage
|
|
5
|
-
# The part of the rewriter that instruments the
|
|
6
|
-
# another, eg the &. chain in ok && user&.admin?. The
|
|
7
|
-
# around them enters and leaves; each nested
|
|
8
|
-
# noted as that
|
|
5
|
+
# The part of the rewriter that instruments the decisions nested in
|
|
6
|
+
# another, eg the &. chain in ok && user&.admin?. The decision
|
|
7
|
+
# around them enters and leaves; each nested decision's leaves are
|
|
8
|
+
# noted as that decision's, at their place among its leaves, by the
|
|
9
9
|
# calls its kind gives in place of plain records.
|
|
10
10
|
module RewriterNested
|
|
11
11
|
# { leaf range => the opening of its call, nil for none } for
|
|
12
|
-
# the leaves of every
|
|
12
|
+
# the leaves of every decision nested in condition whose kind has
|
|
13
13
|
# calls of its own, condition itself included.
|
|
14
|
-
def self.calls(
|
|
14
|
+
def self.calls(decision, condition)
|
|
15
15
|
leaves = condition.leaves
|
|
16
16
|
rooted(condition).reduce({}) do |calls, inner|
|
|
17
17
|
kind = Kinds.of(inner.operator)
|
|
18
|
-
calls.merge(kind.calls(
|
|
18
|
+
calls.merge(kind.calls(decision, inner, leaves))
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
# The inserts, besides its leaves' calls, for every
|
|
22
|
+
# The inserts, besides its leaves' calls, for every decision
|
|
23
23
|
# nested in condition whose kind has calls of its own,
|
|
24
24
|
# condition itself included, as its kind gives them.
|
|
25
|
-
def self.inserts(
|
|
25
|
+
def self.inserts(decision, condition)
|
|
26
26
|
leaves = condition.leaves
|
|
27
27
|
rooted(condition).flat_map do |inner|
|
|
28
28
|
at = leaves.index(inner.operands.first)
|
|
29
29
|
kind = Kinds.of(inner.operator)
|
|
30
|
-
kind.inserts(
|
|
30
|
+
kind.inserts(decision, inner, at, condition.commands)
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
# condition alone when its kind has calls of its own, eg a
|
|
35
|
-
# block method's call that is a
|
|
36
|
-
# then noted at their places from 0; else the
|
|
35
|
+
# block method's call that is a decision of its own, its leaves
|
|
36
|
+
# then noted at their places from 0; else the decisions nested in
|
|
37
37
|
# it.
|
|
38
38
|
def self.rooted(condition)
|
|
39
39
|
return [condition] if own_calls?(condition)
|
|
@@ -41,7 +41,7 @@ module PathCoverage
|
|
|
41
41
|
nested(condition)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
# The
|
|
44
|
+
# The decisions nested in condition whose kind has calls of its
|
|
45
45
|
# own, in its operands and in its arms' conditions.
|
|
46
46
|
def self.nested(condition)
|
|
47
47
|
condition.operands.flat_map do |operand|
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require_relative 'configuration'
|
|
3
|
+
require_relative 'render'
|
|
4
|
+
require_relative 'ways_totals'
|
|
5
|
+
|
|
6
|
+
module PathCoverage
|
|
7
|
+
# The path_coverage_report command: reports saved runs' JSON
|
|
8
|
+
# reports as one, each loop a decision only when asked.
|
|
9
|
+
module RunsReport
|
|
10
|
+
HELP = <<~HELP.freeze
|
|
11
|
+
Usage: path_coverage_report [--loops] [--max-paths N] SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON...
|
|
12
|
+
|
|
13
|
+
Reports the saved runs RUN_JSON... as one, writing the report into
|
|
14
|
+
REPORT_DIR, then prints each method's untested paths and the totals.
|
|
15
|
+
Exits 0 when every path is taken, and 1 when any is not.
|
|
16
|
+
|
|
17
|
+
SOURCE_ROOT the directory holding the covered source
|
|
18
|
+
GLOBS comma-separated globs of the covered files, relative to SOURCE_ROOT
|
|
19
|
+
REPORT_DIR the directory it writes its report into
|
|
20
|
+
RUN_JSON... one or more saved runs' JSON files, space-separated
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
-h Show this help
|
|
24
|
+
--loops Count each loop as a decision: 0 items or 1+ items
|
|
25
|
+
--max-paths N Fail each method with more than N paths
|
|
26
|
+
(default 4096, at most 65536)
|
|
27
|
+
|
|
28
|
+
Example:
|
|
29
|
+
path_coverage_report --max-paths 8 /app 'lib/**/*.rb' report runs/*.json
|
|
30
|
+
HELP
|
|
31
|
+
|
|
32
|
+
# Reports the runs argv names, [--loops] [--max-paths N]
|
|
33
|
+
# SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON..., as HELP says,
|
|
34
|
+
# printing the untested paths, then the totals, read from the
|
|
35
|
+
# JSON it wrote, so a run with no untested path still prints
|
|
36
|
+
# them; -h prints HELP. Returns the exit status: 1 when the
|
|
37
|
+
# report fails, as WaysTotals.failed? says, else 0.
|
|
38
|
+
def self.main(argv)
|
|
39
|
+
return help if argv.first == '-h'
|
|
40
|
+
|
|
41
|
+
options, (source_root, globs, report_dir, *runs) = options(argv)
|
|
42
|
+
config = configured(source_root, globs, report_dir, options)
|
|
43
|
+
Render.from_json(config, runs)
|
|
44
|
+
written = JSON.parse(File.read(config.json_path))
|
|
45
|
+
puts WaysTotals.lines(written)
|
|
46
|
+
WaysTotals.failed?(written) ? 1 : 0
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Prints HELP; returns 0.
|
|
50
|
+
def self.help
|
|
51
|
+
print HELP
|
|
52
|
+
0
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# [{ loops:, max_paths: }, the rest of argv] of argv's leading
|
|
56
|
+
# options; loops false and max_paths the default when not
|
|
57
|
+
# given.
|
|
58
|
+
def self.options(argv)
|
|
59
|
+
options = { loops: false, max_paths: Configuration::MAX_PATHS }
|
|
60
|
+
rest = argv.dup
|
|
61
|
+
while %w[--loops --max-paths].include?(rest.first)
|
|
62
|
+
flag = rest.shift
|
|
63
|
+
next options[:loops] = true if flag == '--loops'
|
|
64
|
+
|
|
65
|
+
options[:max_paths] = Integer(rest.shift)
|
|
66
|
+
end
|
|
67
|
+
[options, rest]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The configuration reporting the files globs, comma-separated,
|
|
71
|
+
# match under source_root into report_dir, with the :ways
|
|
72
|
+
# reporter and options' loops and max_paths.
|
|
73
|
+
def self.configured(source_root, globs, report_dir, options)
|
|
74
|
+
Configuration.new do
|
|
75
|
+
root source_root
|
|
76
|
+
globs.split(',').each { cover _1 }
|
|
77
|
+
coverage_dir report_dir
|
|
78
|
+
report :ways
|
|
79
|
+
max_paths options[:max_paths]
|
|
80
|
+
loops if options[:loops]
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -5,11 +5,11 @@ module PathCoverage
|
|
|
5
5
|
# conditions on it.
|
|
6
6
|
module SourceLines
|
|
7
7
|
# [{number:, text:, status:, span:}] for each line of
|
|
8
|
-
# source. A line in a
|
|
8
|
+
# source. A line in a decision's span shares one button with
|
|
9
9
|
# the span's other lines; status and span are nil on a line
|
|
10
10
|
# with no condition.
|
|
11
|
-
def self.of(source,
|
|
12
|
-
marks = marks(source,
|
|
11
|
+
def self.of(source, decisions, operands)
|
|
12
|
+
marks = marks(source, decisions, operands)
|
|
13
13
|
source.lines.each_with_index.map do |text, index|
|
|
14
14
|
number = index + 1
|
|
15
15
|
mark = marks.fetch(number, {})
|
|
@@ -20,28 +20,28 @@ module PathCoverage
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
# { line number => {status:, span:} } for each line a
|
|
23
|
+
# { line number => {status:, span:} } for each line a decision
|
|
24
24
|
# covers, every line of a merged span sharing that span.
|
|
25
|
-
def self.marks(source,
|
|
26
|
-
blocks = merged(spans_of(source,
|
|
25
|
+
def self.marks(source, decisions, operands)
|
|
26
|
+
blocks = merged(spans_of(source, decisions, operands))
|
|
27
27
|
blocks.each_with_object({}) do |(span, status), marks|
|
|
28
28
|
span.each { |number| marks[number] = { status:, span: } }
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
# [span, status] for each
|
|
32
|
+
# [span, status] for each decision: from the line it starts on
|
|
33
33
|
# to the line its last operand ends on.
|
|
34
|
-
def self.spans_of(source,
|
|
35
|
-
|
|
34
|
+
def self.spans_of(source, decisions, operands)
|
|
35
|
+
decisions.map do |decision, condition|
|
|
36
36
|
last = condition.leaves.last
|
|
37
|
-
span = line_of(
|
|
38
|
-
[span, status_of(operands.fetch(
|
|
37
|
+
span = line_of(decision)..end_line(source, last)
|
|
38
|
+
[span, status_of(operands.fetch(decision))]
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# The spans merged wherever they overlap, in line order. A
|
|
43
|
-
# merged span has :gaps if any of its
|
|
44
|
-
# covered
|
|
43
|
+
# merged span has :gaps if any of its decisions has, so a
|
|
44
|
+
# covered decision never hides another's gaps.
|
|
45
45
|
def self.merged(spans)
|
|
46
46
|
sorted = spans.sort_by { |span, _| span.begin }
|
|
47
47
|
sorted.each_with_object([]) do |(span, status), blocks|
|
|
@@ -66,9 +66,9 @@ module PathCoverage
|
|
|
66
66
|
gaps.empty? ? :covered : :gaps
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
# The line a
|
|
70
|
-
def self.line_of(
|
|
71
|
-
|
|
69
|
+
# The line a decision starts on, from its path:line:column name.
|
|
70
|
+
def self.line_of(decision)
|
|
71
|
+
decision[/:(\d+):\d+\z/, 1].to_i
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
# The line of source on which byte range ends.
|