path_coverage 0.4.0 → 0.6.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.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
data/README.md CHANGED
@@ -1,176 +1,150 @@
1
- # path_coverage
2
-
3
- [![Gem Version](https://img.shields.io/gem/v/path_coverage)](https://rubygems.org/gems/path_coverage)
4
- [![Downloads](https://img.shields.io/gem/dt/path_coverage)](https://rubygems.org/gems/path_coverage)
5
- [![License](https://img.shields.io/github/license/path-coverage/ruby)](LICENSE.md)
1
+ # <img src="https://avatars.githubusercontent.com/u/353243247?v=4" alt="" width="32" height="32"> path-coverage/ruby
6
2
 
7
3
  Path coverage for Ruby, to run alongside SimpleCov's line and branch
8
4
  coverage.
9
5
 
10
- Status: sites cover `&&`, `||`, `and`, `or`, the ternary `?:`,
11
- `if`/`elsif`/`else` and `unless` (modifiers included), `case`/`when`
12
- with a subject, and the block methods `any?` `all?` `none?` `find`
13
- `detect` `find_index` `take_while` `select` `filter` `reject`
14
- `partition` `count` `filter_map`, the writes `||=` and `&&=`, and `&.`
15
- safe-navigation chains. Method paths cover every site at a method's
16
- top level.
17
- More decisions hidden inside calls come next (see Roadmap).
6
+ [![Gem Version](https://img.shields.io/gem/v/path_coverage)](https://rubygems.org/gems/path_coverage)
7
+ [![Downloads](https://img.shields.io/gem/dt/path_coverage)](https://rubygems.org/gems/path_coverage)
8
+ [![License](https://img.shields.io/github/license/path-coverage/ruby)](LICENSE.md)
18
9
 
19
- Demo: https://path-coverage.github.io/ruby/ shows the HTML report
20
- for the mock source tree in `demo/`. `make demo` builds and opens the
21
- same report locally.
10
+ - [Overview](#overview)
11
+ - [Getting started](#getting-started)
12
+ - [Ignoring and treating](#ignoring-and-treating)
13
+ - [Use when you cannot edit the code!](#use-when-you-cannot-edit-the-code)
14
+ - [What a run reports](#what-a-run-reports)
15
+ - [How it works](#how-it-works)
16
+ - [Roadmap](#roadmap)
17
+ - [Constraints](#constraints)
18
+ - [Development](#development)
22
19
 
23
20
  ## Overview
24
21
 
25
- A *site* is one decision in your code: an `&&`, `||`, `if`,
26
- `case`, `&.` chain, `||=`, or a block method such as `any?`.
27
- Each way it can go is a *path*. For example
22
+ A *decision* is a choice in your code, eg an `&&`, `||`, `if`,
23
+ `case`, etc. Each way it can go is a *path*. For example
28
24
 
29
25
  ```ruby
30
- name.size > 2 && name.ascii_only?
26
+ def discount(member, coupon)
27
+ member || coupon ? 10 : 0
28
+ end
31
29
  ```
32
30
 
33
- has three paths: true then true, true then false, and false
34
- alone, as `&&` then skips its right side.
31
+ has three paths: a member, a non-member with a coupon, and
32
+ neither. Tests calling `discount(true, nil)` and
33
+ `discount(false, nil)` cover every line and both branches of the
34
+ `?:`, yet no test ever tried a coupon.
35
35
 
36
36
  Line and branch coverage can be 100% while a path was never
37
37
  taken. path_coverage counts how often each path was taken:
38
- within each site, and within each method, through its sites.
38
+ alone, each decision's paths on their own, and combined, through
39
+ each method's decisions.
39
40
 
40
- As the run exits it writes its report:
41
+ As the run exits it writes its report as two JSON files:
41
42
  - `path_coverage.json`, for AIs and other tools.
42
- - `index.html`, with a page per file, for people.
43
-
44
- ## Reading the HTML
45
-
46
- Clicking a `def` line on a file's HTML page opens that method's
47
- *card* to its left. One card is always open.
48
-
49
- A card has a row for each site in the method, by `line:col`,
50
- with its counts of paths not taken and taken.
51
-
52
- Clicking a row's `▼` opens it:
53
- - on the left is the site's code, as a tree.
54
- - on the right is one column for each possible path through it.
55
- - the column's head is `≀0` for a path never taken, and `≀*`
56
- for one taken (hover for its count). Look for zeros!
57
- - the symbols in a column line up with the tree's rows, and
58
- are explained below.
59
-
60
- Hovering over code in the open card's method opens the tree holding
61
- that code, its row lit, or, for a `return` or `raise`, lights its row.
62
- A long piece of code scrolls within its row.
63
-
64
- ### Boolean: `T` `F`
65
-
66
- Many parts of a site, such as `name.size > 2`, are true or false.
67
-
68
- ![A file's page: line 2, name.size > 2 && name.ascii_only?, its card open to its left](docs/example-outcome.png)
69
-
70
- - `T` the expression was true.
71
- - `F` the expression was false.
72
-
73
- In this example, there was no test which exercised the path where `name.size > 2` was `true`, and
74
- `name.ascii_only?` was `false`.
75
-
76
- ### Unreachable: blank
77
-
78
- Some parts of a site are not always evaluated.
79
-
80
- ![A file's page: line 2, @x = dee || dum, its card open to its left](docs/example-unreachable.png)
81
-
82
- - "blank" the expression is never evaluated.
83
-
84
- In the second column `dee` was `T`, so because of `||`'s short-circuiting, `dum` is never evaluated.
85
-
86
- ### Taken: `✓`
87
-
88
- A `?:` takes one of its arms.
89
-
90
- ![A file's page: line 2, paid ? 'paid' : 'due', its card open to its left](docs/example-taken.png)
91
-
92
- - `✓` marks the column whose path goes to that arm
93
-
94
- The `paid` row is the `?:`'s predicate, `T` or `F`. Below it is one row
95
- per arm, `? 'paid'` and `: 'due'`. The first column, `paid` false, never
96
- taken, goes to `: 'due'`; the second, `paid` true, to `? 'paid'`.
97
-
98
- An `if`, `unless`, `case` or `||=` shows no arm rows: which arm a path
99
- takes follows from its predicate, and the code in an arm is a row of
100
- its own on the card.
101
-
102
- ### Navigation: `∅` `●`
103
-
104
- `&.` skips the rest of its chain when its receiver is nil.
105
-
106
- ![A file's page: line 2, user&.account&.owner, its card open to its left](docs/example-nil.png)
43
+ - `path_coverage.source.json`, what drawing each file's code needs.
107
44
 
108
- - `∅` the receiver was nil, so `&.` skipped the rest of the chain.
109
- - `●` the receiver was not nil, so `&.` made the call.
110
-
111
- In this example, there was no test which exercised the path where
112
- `user` was nil.
113
-
114
- ### Count: `0` `1` `*`
115
-
116
- A block method can call its block on any number of items.
45
+ ## Getting started
117
46
 
118
- ![A file's page: line 2, ids.any? { |id| id.odd? }, its card open to its left](docs/example-count.png)
47
+ ```ruby
48
+ # test coverage config, before any application code is required
49
+ require 'simplecov'
50
+ require 'path_coverage'
51
+ SimpleCov.start do
52
+ # ...
53
+ end
54
+ PathCoverage.start do
55
+ root APP_DIR # cover globs and decision names are relative to it
56
+ cover 'source/**/*.rb' # each matching file is instrumented as it loads
57
+ cover 'test/**/*.rb' # more than one cover glob may be given
58
+ skip 'source/vendor/**/*.rb' # except these, though a cover glob matches them
59
+ coverage_dir '/tmp/coverage' # where path_coverage.json is written
60
+ report :gaps, :summary # the reporters run at exit (default :gaps);
61
+ # also :ways and :post
62
+ group 'code', 'source/**/*.rb' # each group its own totals
63
+ group 'test', 'test/**/*.rb'
64
+ ignore '&&=' # operators whose decisions are not measured
65
+ max_paths 4096 # list each method's paths up to this many
66
+ # (the default; at most 65536)
67
+ loops # count each loop as a decision: 0 items
68
+ # or 1+ items (off by default)
69
+ end
70
+ ```
119
71
 
120
- - `0` the block was called with no items.
121
- - `1` the block was called with one item.
122
- - `*` the block was called with two or more items.
72
+ ## Ignoring and treating
123
73
 
124
- In this example, there was no test which exercised the path where
125
- the block was called with two or more items and every answer was
126
- `false`.
74
+ `ignore '&&=', '||='` stops `&&=` and `||=` writes being measured.
75
+ A decision inside such a write is still measured, eg the `a && b` in
76
+ `@m ||= a && b`. Ignoring `&&` does not ignore `and`, nor does ignoring
77
+ `||` ignore `or`. The summary and the JSON report list what is
78
+ ignored.
127
79
 
128
- ### Both: `TF`
80
+ `treat` tells path_coverage what you know about a method, so paths that
81
+ cannot happen are not listed as gaps. Add `in: 'source/models/**'` to
82
+ limit a treatment to the files that glob matches.
129
83
 
130
- A block's answers can differ from item to item.
84
+ - `treat 'name', as: :present`: a call to `name` is never nil, so
85
+ `user.name&.size` has no path with `user.name` nil.
86
+ - `treat 'any?', as: :nonempty`: `x.any?` is true exactly when `x` has
87
+ items. When `x` is a call to `select`, `reject`, `filter`,
88
+ `filter_map`, `partition` or `take_while`, the path that call took
89
+ often decides whether it kept any items. Paths where `any?` disagrees
90
+ with that are not listed, eg `ids.select { |i| i.odd? }.any?` true
91
+ with `ids` empty.
131
92
 
132
- ![A file's page: line 2, ids.select { |id| id.odd? }, its card open to its left](docs/example-both.png)
93
+ A run where a treatment turns out to be wrong is reported:
133
94
 
134
- - `T` every answer of the block was true.
135
- - `F` every answer of the block was false.
136
- - `TF` the block's answers were both true and false.
95
+ ```text
96
+ source/user.rb:4:9 path taken that a treatment rules out: `user.name` nil
97
+ ```
137
98
 
138
- In this example, there was no test which exercised the path where
139
- the block was called with two or more items and every answer was
140
- `false`.
99
+ ### Not analysing a block
141
100
 
142
- ### Lots of paths!
101
+ Sometimes a block of code is not worth testing path by path, for example
102
+ logging. Put `# path: disable` before the block and `# path: enable` after
103
+ it. The lines between them still run, but they are not analysed: any
104
+ decision in them is ignored, and they add no paths to the method they
105
+ are in.
143
106
 
144
- Ruby code can be very compact so short expressions can generate lots of paths!
107
+ The line straight after `# path: disable` must start with `# reason:` and
108
+ say why the block is not analysed:
145
109
 
146
- ![A file's page: line 2, if user&.admin? && ids.select { |id| id.odd? }.any?, its card open to its left](docs/example-overview.png)
110
+ ```ruby
111
+ def ship(order, log)
112
+ if order[:express]
113
+ order[:days] = 1
114
+ end
115
+ # path: disable
116
+ # reason: logging is not under test
117
+ if log.verbose?
118
+ log.lines << order[:id]
119
+ end
120
+ # path: enable
121
+ order
122
+ end
123
+ ```
147
124
 
125
+ Without the `# path: disable` and `# path: enable` comments, `ship()` has
126
+ 4 paths: each `if` can be true or false. With them, the second `if` is
127
+ ignored, so `ship()` has 2 paths: `order[:express]` true, and
128
+ `order[:express]` false.
148
129
 
149
- ## Getting started
130
+ Only a real comment that is the whole of its line is a marker. The same
131
+ text inside a string, eg a heredoc, is just text: it disables nothing,
132
+ and is never refused.
150
133
 
151
134
  ```ruby
152
- # test coverage config, before any application code is required
153
- require 'simplecov'
154
- require 'path_coverage'
155
- SimpleCov.start do
156
- # ...
157
- end
158
- PathCoverage.start do
159
- root APP_DIR # cover globs and site names are relative to it
160
- cover 'source/**/*.rb' # each matching file is instrumented as it loads
161
- cover 'test/**/*.rb' # more than one cover glob may be given
162
- skip 'source/vendor/**/*.rb' # except these, though a cover glob matches them
163
- coverage_dir '/tmp/coverage' # where path_coverage.json is written
164
- report :gaps, :html # the reporters run at exit (default :gaps);
165
- # also :summary and :post
166
- group 'code', 'source/**/*.rb' # each group a tab of its own on index.html
167
- group 'test', 'test/**/*.rb'
168
- ignore '&&=' # operators whose sites are not measured
169
- treat 'name', as: :present # a call to name is never nil
170
- treat 'any?', as: :nonempty # any? is true when its receiver has items
171
- end
135
+ HELP = <<~TEXT
136
+ # path: disable
137
+ TEXT
138
+ # a heredoc, not a comment: the methods below are still analysed
172
139
  ```
173
140
 
141
+ Each of these is refused, with the file and line it is on:
142
+
143
+ - a `# path: disable` without a `# reason:` line straight after it
144
+ - a `# path: disable` without a `# path: enable` after it
145
+ - a statement that starts between the two comments and ends after the
146
+ `# path: enable`
147
+
174
148
  ## Use when you cannot edit the code!
175
149
 
176
150
  A run whose code cannot be changed, such as a script in a `docker build`
@@ -190,8 +164,8 @@ starts nothing and prints nothing, so an image can set `RUBYOPT` for every
190
164
  run and measure only those given a collector.
191
165
 
192
166
  The gem's `path_coverage_collector` command is that collector. It saves
193
- each report POSTed to it as a numbered file, and once every run is done
194
- they render as one report:
167
+ each report POSTed to it as a numbered file; once every run is done,
168
+ report them as one with `path_coverage_report`, below.
195
169
 
196
170
  ```sh
197
171
  path_coverage_collector 4570 /tmp/paths/runs # until sent TERM
@@ -206,36 +180,78 @@ path_coverage_collector 4570 /tmp/paths/runs # until sent TERM
206
180
  - On TERM it stops accepting connections, finishes the report in hand,
207
181
  and exits 0.
208
182
 
183
+ ### Reporting saved runs
184
+
185
+ Once every run is done, report them as one with `path_coverage_report`:
186
+
187
+ ```text
188
+ path_coverage_report [--loops] [--max-paths N] SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON...
189
+ ```
190
+
191
+ - SOURCE_ROOT the directory holding the covered source
192
+ - GLOBS comma-separated globs of the covered files, relative to SOURCE_ROOT
193
+ - REPORT_DIR the directory it writes its report into
194
+ - RUN_JSON... one or more saved runs' JSON files, space-separated, eg runs/*.json
195
+ - --loops count each loop as a decision: 0 items or 1+ items
196
+ - --max-paths fail each method with more than N paths (default 4096, at most 65536)
197
+
198
+ With `--loops`, a loop counts as a decision only when its body has a
199
+ statement that runs first on each pass. These loops never do:
200
+
209
201
  ```ruby
210
- runs = Dir['/tmp/paths/runs/*.json']
211
- PathCoverage::Render.from_json(config, runs)
202
+ x += 1 while x < 9 # a modifier loop: its body is written first
203
+ lines.each { } # an empty loop
204
+ urls.each do |url| # a loop whose only statements are rescued
205
+ rescue Timeout::Error
206
+ retry_later(url)
207
+ end
208
+ ```
209
+
210
+ It exits 0 when every path is taken, and 1 when any is not.
211
+
212
+ Every path taken:
213
+
214
+ ```text
215
+ $ path_coverage_report /app 'lib/**/*.rb' report runs/*.json
216
+ Paths: 2 / 2 (100.00%)
217
+ Functions over --max-paths=4096: 0
218
+ ```
219
+
220
+ Some paths untested, and a method over --max-paths:
221
+
222
+ ```text
223
+ $ path_coverage_report --max-paths 8 /app 'lib/**/*.rb' report runs/*.json
224
+ lib/fees.rb:2 fee() has 16 paths, more than --max-paths allows.
225
+ Split it into smaller functions.
226
+ lib/orders.rb:2 total() has 1 untested path.
227
+ Add tests that take each of these paths:
228
+ 1. line 5 `order[:paid]` false
229
+ Paths: 1 / 2 (50.00%)
230
+ Functions over --max-paths=8: 1
212
231
  ```
213
232
 
233
+ Each run's JSON names the kinds of decision it measured. A version that
234
+ measures other kinds lists a decision's paths differently, so a run whose
235
+ kinds differ from those of the installed version is refused. So is a run
236
+ made with `ignore` or `treat`, as the command takes neither.
237
+
214
238
  ## What a run reports
215
239
 
216
240
  As the process exits:
217
241
 
218
- - `path_coverage.json` is always written to `coverage_dir`: every site
219
- of every covered file, with each operand's true and false counts under
220
- `sites`, each site's paths under `paths`, each method's card under
221
- `cards`, how each card's rows nest under `tree`, the statements
222
- reached under `reached`, and the paths taken
223
- of all possible under `totals`, as [taken, possible]: `decisions`,
224
- `methods`, and, with groups, each group's
225
- own under `groups`, eg `"groups": {"code": {"decisions": [481, 567]}}`.
226
- It also names the kinds of site measured under `kinds`, any `ignored`
227
- operators and `treats`, and any paths taken that a treatment rules
228
- out under `outside`.
229
- It is the base the other reporters read, so they can also be run
242
+ - `path_coverage.json` and `path_coverage.source.json` are always
243
+ written to `coverage_dir`. Their keys, with examples, are in
244
+ [path-coverage/schemas](https://github.com/path-coverage/schemas#path_coveragejson).
245
+ `path_coverage.json` is the base the other reporters read, so they can also be run
230
246
  later, in another process, from
231
247
  it alone: `PathCoverage::Render.from_json(config)`, where `config` is a
232
248
  `PathCoverage::Configuration` built from the same settings block.
233
249
  Given several runs' reports, `Render.from_json(config, paths)` adds
234
250
  their counts together first, so the report shows only the gaps that
235
251
  every run left. A report whose kinds, `ignored` or `treats` differ
236
- from `config`'s is refused, as its sites list their paths
252
+ from `config`'s is refused, as its decisions list their paths
237
253
  differently.
238
- - `:gaps` prints one line per site path never taken, in manifest
254
+ - `:gaps` prints one line per decision path never taken, in manifest
239
255
  order, naming the outcome of each leaf on it:
240
256
 
241
257
  ```text
@@ -245,65 +261,52 @@ source/kata.rb:4:24 never reached
245
261
 
246
262
  ### Decisions
247
263
 
248
- A site is named by the `path:line:column` where it starts, the path
264
+ A decision is named by the `path:line:column` where it starts, the path
249
265
  relative to `root`; but an `if` or `unless` at its keyword, a `?:` at
250
- its `?`, and a block method's call at its method, so no two sites
251
- share a name. A site in a covered file the run never loaded is still
266
+ its `?`, and a block method's call at its method, so no two decisions
267
+ share a name. A decision in a covered file the run never loaded is still
252
268
  reported, as `never reached`.
253
269
 
254
- A path is the outcome of each leaf one evaluation of the site reached, in
270
+ A path is the outcome of each leaf one evaluation of the decision reached, in
255
271
  order; a leaf that short-circuiting skipped is not on it. Each leaf is
256
272
  quoted as a Markdown code span, on one line, its fence one backtick longer
257
273
  than any run of backticks inside it. The gap lines are for people; a tool
258
- should read the JSON, whose `paths` give each site's leaves as
259
- `line:column` spans, and each possible path with its count:
260
-
261
- ```json
262
- "paths": {
263
- "source/kata.rb:3:23": {
264
- "leaves": [["3:23", "3:30"], ["3:34", "3:42"]],
265
- "taken": [
266
- { "path": [true, true], "count": 2 },
267
- { "path": [true, false], "count": 0 },
268
- { "path": [false, null], "count": 1 }
269
- ]
270
- }
271
- }
272
- ```
274
+ should read the JSON, whose `paths` give each decision's leaves as
275
+ `line:column` spans, and each possible path with its count.
273
276
 
274
- A site is an outermost condition. Its leaves are numbered left to right
277
+ A decision is an outermost condition. Its leaves are numbered left to right
275
278
  across the whole of it, nested conditions included. A condition inside a
276
- block is a site of its own.
279
+ block is a decision of its own.
277
280
 
278
- A ternary is a site even when its predicate is a single leaf. An arm
281
+ A ternary is a decision even when its predicate is a single leaf. An arm
279
282
  holding a condition of `&&`, `||`, `and`, `or` or `?:` is part of the
280
- ternary's site, its leaves numbered after the predicate's, so the site's
283
+ ternary's decision, its leaves numbered after the predicate's, so the decision's
281
284
  paths run through whichever arm the predicate picks. Any other arm, like a
282
- block, is not part of it, though a condition inside it is a site of its
285
+ block, is not part of it, though a condition inside it is a decision of its
283
286
  own.
284
287
 
285
- An `if` or an `unless`, a modifier included, is a site too, even when
288
+ An `if` or an `unless`, a modifier included, is a decision too, even when
286
289
  its predicate is a single leaf: its predicate nests in it, an `&&` there
287
- being no site of its own, and its `then` and `else` arms are plain, as
290
+ being no decision of its own, and its `then` and `else` arms are plain, as
288
291
  they are statements; an `unless` takes its `then` arm when its predicate
289
292
  is false. It is named at its keyword, and its extent is its keyword to
290
- the end of its predicate, so a site inside an arm keeps its own button.
291
- An `elsif` is a site of its own.
293
+ the end of its predicate, so a decision inside an arm keeps its own button.
294
+ An `elsif` is a decision of its own.
292
295
 
293
- A `case` with a subject is a site whose leaves are its `when` patterns,
296
+ A `case` with a subject is a decision whose leaves are its `when` patterns,
294
297
  tried in order until one matches. Each pattern is wrapped in a stand-in
295
298
  whose `===` calls the pattern's `===` once, exactly as the `case` would,
296
299
  and notes whether it matched. A `case` with no subject, or with a splat
297
- among its patterns, is no site.
300
+ among its patterns, is no decision.
298
301
 
299
302
  A call to `any?` `all?` `none?` `find` `detect` `find_index`
300
303
  `take_while` `select` `filter` `reject` `partition` `count` or
301
- `filter_map`, with no arguments and a block of one expression, is a site
304
+ `filter_map`, with no arguments and a block of one expression, is a decision
302
305
  named at its method. Its item count is noted from the block's own calls,
303
306
  so the receiver is never asked its size, which could consume a sequence
304
307
  that can be read only once.
305
308
 
306
- A `||=` or `&&=` write is a site, whatever its target: a variable of any
309
+ A `||=` or `&&=` write is a decision, whatever its target: a variable of any
307
310
  kind, a constant, an attribute (`self.size ||= n`) or an index
308
311
  (`store[key] ||= key.upcase`). Its one leaf is its target, and its right
309
312
  side is an arm, run when the target is false for `||=`, true for `&&=`,
@@ -312,14 +315,14 @@ since reading `store[key]` could call a method twice: its outcome follows
312
315
  from whether the right side ran. So `@clock ||= Clock.now` called only
313
316
  once, as a fresh object, leaves the path with `@clock` true a gap.
314
317
 
315
- A `&.` chain is a site, named where it starts. Its leaves are the
318
+ A `&.` chain is a decision, named where it starts. Its leaves are the
316
319
  receivers of its `&.` calls. `false` counts as not nil, as `&.` calls on
317
320
  it.
318
321
 
319
322
  A chain inside a condition, eg `ok?(id) && user&.admin?` or
320
- `if user&.admin?`, folds into its site, as one expression: its value is
323
+ `if user&.admin?`, folds into its decision, as one expression: its value is
321
324
  one more leaf, true or false, reached when every receiver is not nil. A
322
- write through `&.`, eg `box&.size ||= 3`, is no site, as a nil `box`
325
+ write through `&.`, eg `box&.size ||= 3`, is no decision, as a nil `box`
323
326
  skips it whole and its target is never looked at.
324
327
 
325
328
  A chain of one operator is flat; a change of operator, by precedence or by
@@ -352,76 +355,58 @@ cannot be closed is the lesser error than hiding one that can.
352
355
 
353
356
  ### Methods
354
357
 
355
- Each method has a card, named by the `path:line:column` of its `def`,
356
- with a row for each statement that shapes its paths: each site, and
358
+ Each method has a flow, named by the `path:line:column` of its `def`,
359
+ with a row for each statement that shapes its paths: each decision, and
357
360
  each `return` or `raise`, which ends a path. Any other statement, eg
358
- `x = 1`, runs exactly when what holds it does, so is no row. A site's
361
+ `x = 1`, runs exactly when what holds it does, so is no row. A decision's
359
362
  row counts its paths; a `return` or `raise` row is one path, taken
360
- when it is reached. A method's total adds up its rows. Its title says
361
- how many times it was called, so a method never called is a gap,
362
- even one with no rows. A file's code outside any method gets a card
363
+ when it is reached. A method's total adds up its rows. Its `calls`
364
+ count how many times it was called, so a method never called is a gap,
365
+ even one with no rows. A file's code outside any method gets a flow
363
366
  of its own.
364
367
 
365
- The JSON report's `cards` hold each card's rows, its `ways` (the ways
366
- one call can go through it) and its `calls`:
367
-
368
- ```json
369
- "cards": {
370
- "label.rb:1:0": {
371
- "name": "label", "first": 1, "last": 7, "ways": 2, "calls": 1,
372
- "rows": [
373
- { "at": "2:02", "top": "if", "paths": 2, "taken": 1, "site": "label.rb:2:2" },
374
- { "at": "3:04", "top": "return", "paths": 1, "taken": 1, "site": null },
375
- { "at": "5:04", "top": "return", "paths": 1, "taken": 0, "site": null }
376
- ]
377
- }
378
- }
379
- ```
368
+ The JSON report's `flows` hold each flow's rows, its `ways` and its
369
+ `calls`, and its `tree` says how those rows nest.
380
370
 
381
- Its `tree` says how those rows nest, by file, then card, then row.
382
- A row inside a site names that site as its `parent`, the arm it is
383
- `in`, and `on`, the indices of the parent's paths that run it, in the
384
- order `paths` lists them. A row at the method's top has none of these.
385
-
386
- ```json
387
- "tree": {
388
- "label.rb": {
389
- "1:0": {
390
- "2:2": { "parent": null, "in": null, "on": null, "leaf": null, "gives": null },
391
- "3:4": { "parent": "2:2", "in": "then", "on": [0], "leaf": null, "gives": null },
392
- "5:4": { "parent": "2:2", "in": "else", "on": [1], "leaf": null, "gives": null }
393
- }
394
- }
395
- }
396
- ```
397
-
398
- A row inside one of its parent's leaves names that `leaf`, by index.
399
- A row that is the whole of that leaf, like the `any?` here, also
400
- `gives` the leaf's outcome on each of its own paths, `null` where not
401
- known:
371
+ A method's flow counts only the paths its method can take. A
372
+ local variable the method never writes, eg a parameter, read as a
373
+ leaf outside any block, has one outcome all through a call, so
402
374
 
403
375
  ```ruby
404
- def led?(xs)
405
- xs.any? { |x| x.odd? } || xs.empty?
376
+ def fee(member, big)
377
+ return 0 if member
378
+ total = member ? 90 : 100 # its true path: none, member is false here
379
+ big ? total - 10 : total
406
380
  end
407
381
  ```
408
382
 
409
- ```json
410
- "2:5": { "parent": "2:2", "in": null, "on": [0, 1, 2], "leaf": 0,
411
- "gives": [false, true, false, true, false] }
412
- ```
383
+ the `?:` row has 1 path, not 2, its ways are 3, not 5, and Paths
384
+ combined counts 5, not 6. Paths alone, and the gap lines, still
385
+ list each decision's paths on its own. An instance variable or a
386
+ call may change between two reads, so is never taken to be the
387
+ same; nor is any local of a method calling `binding`, `eval`,
388
+ `send` or the like. A path taken that the method rules out is
389
+ counted, and printed:
413
390
 
414
- Its 5 paths are 0 items, then 1 item and 2+ items, each with a last
415
- answer true or false.
391
+ ```text
392
+ flip.rb:5:11 path taken that its method rules out: `member` true
393
+ ```
416
394
 
417
395
  ### Reporters
418
396
 
419
- - `:html` writes `index.html` to `coverage_dir`: a sortable row per
420
- covered file, with its sites and its paths taken of all possible
421
- within each method (eg `9 / 28 32%`, or `-` for a file with no
422
- method of two or more decisions), and a Total row. Each file name
423
- opens `files/<path>.html`, the file's source with its cards, as
424
- Reading the HTML shows.
397
+ - `:ways` prints, for each method with a path through it that no run
398
+ took, the choices a test must make to take each one, every leaf by its
399
+ source text; a loop is named by its line and how many items it ran:
400
+
401
+ ```text
402
+ lib/orders.rb:2 total() has 1 untested path.
403
+ Add tests that take each of these paths:
404
+ 1. line 5 `order[:paid]` false
405
+ ```
406
+
407
+ Every run writes these lines to `path_coverage.ways.txt` in the
408
+ coverage dir, whichever reporters print, so a later check can show them.
409
+
425
410
  - `:post` sends the JSON report, as an HTTP POST, to the url set by
426
411
  `collector 'http://...'`. It is for a run whose own files are thrown
427
412
  away, such as a failing `docker build` step. It prints nothing, never
@@ -432,8 +417,8 @@ answer true or false.
432
417
  coverage:
433
418
 
434
419
  ```text
435
- Path coverage within decisions: 527 / 631 (83.52%)
436
- Path coverage within methods: 44 / 52 (84.62%)
420
+ Paths alone: 527 / 631 (83.52%)
421
+ Paths combined: 44 / 52 (84.62%)
437
422
  ```
438
423
 
439
424
  With groups, each group is a heading, its own totals indented under it,
@@ -441,19 +426,19 @@ Path coverage within methods: 44 / 52 (84.62%)
441
426
 
442
427
  ```text
443
428
  code:
444
- Path coverage within decisions: 481 / 567 (84.83%)
445
- Path coverage within methods: 41 / 49 (83.67%)
429
+ Paths alone: 481 / 567 (84.83%)
430
+ Paths combined: 41 / 49 (83.67%)
446
431
  test:
447
- Path coverage within decisions: 65 / 84 (77.38%)
448
- Path coverage within methods: 3 / 3 (100.00%)
432
+ Paths alone: 65 / 84 (77.38%)
433
+ Paths combined: 3 / 3 (100.00%)
449
434
  ```
450
435
 
451
436
  ## How it works
452
437
 
453
438
  At load time each covered file is read and parsed once, with Prism. The
454
- parse gives the manifest: each site's condition tree and the byte range of
439
+ parse gives the manifest: each decision's condition tree and the byte range of
455
440
  each of its leaves. The rewriter then inserts a record call around each
456
- leaf, and a leave call around the whole site, whose first argument enters
441
+ leaf, and a leave call around the whole decision, whose first argument enters
457
442
  it, without parsing again, and the result is compiled in place of the
458
443
  file:
459
444
 
@@ -463,10 +448,10 @@ ok?(id) && new?(id)
463
448
  PathCoverage.leave(PathCoverage.enter("k.rb:2:2", 2), PathCoverage.record("k.rb:2:2", 0, ok?(id)) && PathCoverage.record("k.rb:2:2", 1, new?(id)))
464
449
  ```
465
450
 
466
- An `if` site's leave call wraps its predicate alone, as its path is
451
+ An `if` decision's leave call wraps its predicate alone, as its path is
467
452
  decided there, so an arm that returns or raises still counts it.
468
- Each statement that is no site gets a reach call before it, counting
469
- its run, for its card's row.
453
+ Each statement that is no decision gets a reach call before it, counting
454
+ its run, for its flow's row.
470
455
 
471
456
  A command call such as `ok? id` (after `and`) is not valid as a bare
472
457
  argument, so that leaf alone gets parentheses of its own, and so does a
@@ -477,7 +462,7 @@ totals. So a command call, or an `and` or `or`, written over several lines
477
462
  can add to SimpleCov's relevant lines; nothing else does.
478
463
 
479
464
  `record` counts whether its value was truthy or falsy, and notes it on the
480
- site's current path; `enter` starts that path, and `leave` counts it.
465
+ decision's current path; `enter` starts that path, and `leave` counts it.
481
466
  Each returns its value unchanged, so short-circuiting and the value of
482
467
  the expression are preserved. Paths are kept per fiber, so threads never
483
468
  mix their outcomes; an evaluation cut short by a raise counts no path,
@@ -488,33 +473,17 @@ rewrite happens in memory; no file on disk changes.
488
473
  At exit the manifests parsed during the run are reused; only covered files
489
474
  the run never loaded are read and parsed then.
490
475
 
491
- ## Ignoring and treating
492
-
493
- `ignore '&&=', '||='` stops `&&=` and `||=` writes being measured.
494
- A site inside such a write is still measured, eg the `a && b` in
495
- `@m ||= a && b`. Ignoring `&&` does not ignore `and`, nor does ignoring
496
- `||` ignore `or`. The summary and `index.html` list what is ignored.
497
-
498
- `treat` tells path_coverage what you know about a method, so paths that
499
- cannot happen are not listed as gaps. Add `in: 'source/models/**'` to
500
- limit a treatment to the files that glob matches.
501
-
502
- - `treat 'name', as: :present`: a call to `name` is never nil, so
503
- `user.name&.size` has no path with `user.name` nil.
504
- - `treat 'any?', as: :nonempty`: `x.any?` is true exactly when `x` has
505
- items. When `x` is a call to `select`, `reject`, `filter`,
506
- `filter_map`, `partition` or `take_while`, the path that call took
507
- often decides whether it kept any items. Paths where `any?` disagrees
508
- with that are not listed, eg `ids.select { |i| i.odd? }.any?` true
509
- with `ids` empty.
510
-
511
- A run where a treatment turns out to be wrong is reported:
476
+ ## Roadmap
512
477
 
513
- ```text
514
- source/user.rb:4:9 path taken that a treatment rules out: `user.name` nil
515
- ```
478
+ Decisions covered now: `&&`, `||`, `and`, `or`, the ternary `?:`,
479
+ `if`/`elsif`/`else` and `unless` (modifiers included), `case`/`when`
480
+ with a subject, and the block methods `any?` `all?` `none?` `find`
481
+ `detect` `find_index` `take_while` `select` `filter` `reject`
482
+ `partition` `count` `filter_map`, the writes `||=` and `&&=`, and `&.`
483
+ safe-navigation chains. Method paths cover every decision at a method's
484
+ top level.
516
485
 
517
- ## Roadmap
486
+ Next:
518
487
 
519
488
  - decisions hidden inside calls, each with its own outcome classes:
520
489
  - `one?`, whose last answer does not decide it, and block methods
@@ -529,12 +498,12 @@ source/user.rb:4:9 path taken that a treatment rules out: `user.name` nil
529
498
  nil or false is reported. Later, treatments could come from the
530
499
  project's RBS signatures (`sig/*.rbs`) too.
531
500
  - exceptions the code itself names: a `begin` or `def` with `rescue`
532
- clauses as a site, like a `case`, whose paths are no raise and each
501
+ clauses as a decision, like a `case`, whose paths are no raise and each
533
502
  clause that rescued, eg `rescue IOError` never taken. Branch coverage
534
503
  ignores `rescue`, and tests reach these paths with real input. A raise
535
504
  that nothing names, from any call, stays out, as every leaf would gain
536
505
  a third outcome that only fault injection could take.
537
- - an evaluation that raised part way through a site, eg
506
+ - an evaluation that raised part way through a decision, eg
538
507
  `ok?(id) && fetch(id)` where `fetch` raises, reported as a path that
539
508
  raised rather than dropped as unfinished.
540
509
  - complexity maximums, reported and gated, since the possible paths
@@ -565,7 +534,7 @@ source/user.rb:4:9 path taken that a treatment rules out: `user.name` nil
565
534
  branches in both the code and the tests, and that every path within the
566
535
  tests' methods was taken, within the bounds in
567
536
  `test/coverage_metrics_params.json` (a path bound is eg
568
- `test.paths.methods.missed`, and a missing path report denies it)
537
+ `test.paths.combined.missed`, and a missing path report denies it)
569
538
  (`make test tids='6D1K'` runs only matching tests, and skips the check)
570
539
  - `make test_check`: shunit2 tests showing that a breached bound fails the
571
540
  check with a non-zero exit
@@ -582,13 +551,13 @@ is, each with a code and a test tab, then the check's verdict:
582
551
  code:
583
552
  Line coverage: 1270 / 1270 (100.00%)
584
553
  Branch coverage: 321 / 321 (100.00%)
585
- Path coverage within decisions: 481 / 567 (84.83%)
586
- Path coverage within methods: 41 / 49 (83.67%)
554
+ Paths alone: 481 / 567 (84.83%)
555
+ Paths combined: 41 / 49 (83.67%)
587
556
  test:
588
557
  Line coverage: 2250 / 2250 (100.00%)
589
558
  Branch coverage: 43 / 43 (100.00%)
590
- Path coverage within decisions: 65 / 84 (77.38%)
591
- Path coverage within methods: 3 / 3 (100.00%)
559
+ Paths alone: 65 / 84 (77.38%)
560
+ Paths combined: 3 / 3 (100.00%)
592
561
  SimpleCov report: .../reports/index.html
593
562
  PathCov report: .../reports/paths/index.html
594
563
  coverage: ALLOWED
@@ -602,12 +571,3 @@ a copy of `lib/` renamed `PathCoverageSelf` (`test/self_coverage.rb`), so
602
571
  the tests start and restart `PathCoverage` as always. SimpleCov's figures
603
572
  for `lib/` are the same with it as without.
604
573
 
605
- `make demo` (not part of `make all`) runs `demo/run.rb` through
606
- `bin/build_demo.sh`, writing the report to `site/` and opening it. The
607
- Pages workflow (`.github/workflows/pages.yml`) runs the same script on
608
- each push to `main` and publishes `site/`.
609
-
610
- `bin/capture_card_images.sh` runs the examples in `demo/cards/` and
611
- saves this README's screenshots, `docs/example-*.png`, with headless
612
- Chrome. Run it again whenever the card's look changes.
613
-