path_coverage 0.1.0 → 0.3.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +315 -107
  3. data/lib/path_coverage/calls.rb +55 -23
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +33 -3
  6. data/lib/path_coverage/configuration.rb +43 -22
  7. data/lib/path_coverage/configuration_paths.rb +58 -0
  8. data/lib/path_coverage/console_summary.rb +75 -0
  9. data/lib/path_coverage/gaps.rb +32 -13
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +6 -18
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -413
  14. data/lib/path_coverage/html/sort.js +24 -21
  15. data/lib/path_coverage/html/style.css +339 -207
  16. data/lib/path_coverage/html/tabs.js +15 -0
  17. data/lib/path_coverage/html/theme.js +27 -0
  18. data/lib/path_coverage/html_branch.rb +30 -0
  19. data/lib/path_coverage/html_cards.rb +111 -0
  20. data/lib/path_coverage/html_cells.rb +66 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +95 -41
  23. data/lib/path_coverage/html_index_head.rb +29 -0
  24. data/lib/path_coverage/html_labels.rb +23 -0
  25. data/lib/path_coverage/html_methods.rb +66 -0
  26. data/lib/path_coverage/html_nav.rb +50 -50
  27. data/lib/path_coverage/html_report.rb +69 -56
  28. data/lib/path_coverage/html_statements.rb +166 -0
  29. data/lib/path_coverage/html_syntax.rb +156 -0
  30. data/lib/path_coverage/html_tabs.rb +26 -0
  31. data/lib/path_coverage/html_template.rb +6 -0
  32. data/lib/path_coverage/html_tree.rb +59 -74
  33. data/lib/path_coverage/json_reader.rb +101 -0
  34. data/lib/path_coverage/json_writer.rb +52 -7
  35. data/lib/path_coverage/kernel.rb +55 -53
  36. data/lib/path_coverage/kinds/base.rb +71 -0
  37. data/lib/path_coverage/kinds/block.rb +137 -0
  38. data/lib/path_coverage/kinds/case.rb +92 -0
  39. data/lib/path_coverage/kinds/chain.rb +45 -0
  40. data/lib/path_coverage/kinds/choice.rb +68 -0
  41. data/lib/path_coverage/kinds/contains.rb +113 -0
  42. data/lib/path_coverage/kinds/safe.rb +153 -0
  43. data/lib/path_coverage/kinds/write.rb +54 -0
  44. data/lib/path_coverage/kinds.rb +37 -0
  45. data/lib/path_coverage/manifest.rb +60 -20
  46. data/lib/path_coverage/manifest_blocks.rb +48 -10
  47. data/lib/path_coverage/manifest_case.rb +2 -3
  48. data/lib/path_coverage/manifest_chains.rb +76 -0
  49. data/lib/path_coverage/manifest_choices.rb +32 -18
  50. data/lib/path_coverage/manifest_folding.rb +91 -0
  51. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  52. data/lib/path_coverage/manifest_writes.rb +67 -0
  53. data/lib/path_coverage/merge.rb +19 -3
  54. data/lib/path_coverage/method_statements.rb +300 -0
  55. data/lib/path_coverage/post.rb +8 -8
  56. data/lib/path_coverage/recorder.rb +20 -79
  57. data/lib/path_coverage/recorder_blocks.rb +68 -0
  58. data/lib/path_coverage/recorder_cases.rb +23 -0
  59. data/lib/path_coverage/recorder_chains.rb +40 -0
  60. data/lib/path_coverage/recorder_writes.rb +26 -0
  61. data/lib/path_coverage/render.rb +56 -47
  62. data/lib/path_coverage/result.rb +19 -0
  63. data/lib/path_coverage/rewriter.rb +69 -107
  64. data/lib/path_coverage/rewriter_nested.rb +62 -0
  65. data/lib/path_coverage/site_paths.rb +9 -58
  66. data/lib/path_coverage/site_tree.rb +21 -57
  67. data/lib/path_coverage/statement_arms.rb +161 -0
  68. data/lib/path_coverage/summary.rb +41 -17
  69. data/lib/path_coverage/totals.rb +6 -12
  70. data/lib/path_coverage/version.rb +1 -1
  71. data/lib/path_coverage/within_methods.rb +92 -0
  72. metadata +40 -11
  73. data/lib/path_coverage/html/toggle.js +0 -12
  74. data/lib/path_coverage/html_paths.rb +0 -138
  75. data/lib/path_coverage/path_gaps.rb +0 -41
  76. data/lib/path_coverage/path_manifest.rb +0 -110
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7146cc322217826aadc2bd040b560b5e77d6fad7b6de4d2ae43e8bdfaa893940
4
- data.tar.gz: 29135bab12a1cc92d1a15dd8d383d3e79d75b3b8716fff7efa95fbbb6f8c3b34
3
+ metadata.gz: fa9b979dc64b90e8ab047df2074c999067a5992bf62067c1e2975921a95a635b
4
+ data.tar.gz: 540e4226258cdcff171e560defc1ade28089a9901c2ed0578758cbc0e56484ba
5
5
  SHA512:
6
- metadata.gz: 4ebf32a5b048eb5d3d1b1cbbca0e2ed9c217e429481d1bd77a242f0a3baff83ee7b83a2b4a4cd99f6a6611885d1c165a56b6bd6f814be28c4cb6a92cffbc8282
7
- data.tar.gz: 9ee6e1f2aea70b34ffb38ec5fa16a52f92ae5daf0a680727903db858fcdb557c244d0ae61a048e1a7bc09d99128c0b7068dd408686b2aca683b9f037afe3f6f3
6
+ metadata.gz: 822d8277455c05c7ac74af3e9e97625bca3884aa1f6d54fdaf625e112d28b210a929d8ade9c4941b9635f403daf2d1a87a1232741cf36d715dbfa568e36d39fc
7
+ data.tar.gz: e0ca639c839185f833e1e99bc5b12ea1f7e6d45b8281d5a43764135c37c975509463fd7a342f0a8ec7e3b9d3fe4a3eeab041c9c8bdc47e2195f6df34b467618c
data/README.md CHANGED
@@ -7,58 +7,138 @@
7
7
  Path coverage for Ruby, to run alongside SimpleCov's line and branch
8
8
  coverage.
9
9
 
10
- Gem: https://rubygems.org/gems/path_coverage (which shows the latest
11
- version).
12
-
13
10
  Status: sites cover `&&`, `||`, `and`, `or`, the ternary `?:`,
14
11
  `if`/`elsif`/`else` and `unless` (modifiers included), `case`/`when`
15
12
  with a subject, and the block methods `any?` `all?` `none?` `find`
16
13
  `detect` `find_index` `take_while` `select` `filter` `reject`
17
- `partition` `count` `filter_map`. Method paths cover `if` decisions.
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.
18
17
  More decisions hidden inside calls come next (see Roadmap).
19
18
 
20
19
  Demo: https://cyber-dojo.github.io/path_coverage/ shows the HTML report
21
20
  for the mock source tree in `demo/`. `make demo` builds and opens the
22
21
  same report locally.
23
22
 
24
- Take this method:
23
+ ## Overview
24
+
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
25
28
 
26
29
  ```ruby
27
- def fee(user, order)
28
- total = 100
29
- if user.member? && order.paid?
30
- total -= 5
31
- end
32
- if order.big?
33
- total -= 10
34
- end
35
- total
36
- end
30
+ name.size > 2 && name.ascii_only?
37
31
  ```
38
32
 
39
- Tests that run both `if`s both ways reach every line and every branch,
40
- so SimpleCov, which reads Ruby's built-in `Coverage`, reports 100%. Yet
41
- they can still miss paths, at two scopes, and path_coverage lists each
42
- path no test took.
33
+ has three paths: true then true, true then false, and false
34
+ alone, as `&&` then skips its right side.
35
+
36
+ Line and branch coverage can be 100% while a path was never
37
+ taken. path_coverage counts how often each path was taken:
38
+ within each site, and within each method, through its sites.
39
+
40
+ As the run exits it writes its report:
41
+ - `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
+ ### Boolean: `T` `F`
61
+
62
+ Many parts of a site, such as `name.size > 2`, are true or false.
63
+
64
+ ![A file's page: line 2, name.size > 2 && name.ascii_only?, its card open to its left](docs/example-outcome.png)
65
+
66
+ - `T` the expression was true.
67
+ - `F` the expression was false.
68
+
69
+ In this example, there was no test which exercised the path where `name.size > 2` was `true`, and
70
+ `name.ascii_only?` was `false`.
71
+
72
+ ### Unreachable: blank
73
+
74
+ Some parts of a site are not always evaluated.
75
+
76
+ ![A file's page: line 2, @x = dee || dum, its card open to its left](docs/example-unreachable.png)
77
+
78
+ - "blank" the expression is never evaluated.
79
+
80
+ In the second column `dee` was `T`, so because of `||`'s short-circuiting, `dum` is never evaluated.
43
81
 
44
- ## Within each decision
82
+ ### Taken: `✓`
45
83
 
46
- The first `if` can go three ways: member and paid, member and not
47
- paid, not a member. A member who has not paid may never have been
48
- tried.
84
+ An `if` takes one of its arms.
49
85
 
50
- ![The first if's grid: a column per way it can go, each headed by
51
- its count, the member who has not paid at 0](docs/decision.png)
86
+ ![A file's page: line 2, if paid, its card open to its left](docs/example-taken.png)
52
87
 
53
- ## Across a method's decisions
88
+ - `✓` marks the column whose path took that "arm"
54
89
 
55
- The two `if`s can go four ways together. A paying member with a small
56
- order may never have been tried.
90
+ The `paid` row is the `if`'s predicate, `T` or `F`. Below it is one row
91
+ per arm, named `then` and `else`. The first column, `paid` false, took `else`; the
92
+ second, `paid` true, took `then`.
57
93
 
58
- ![fee's grid: a column per way its two ifs can go together, each
59
- headed by its count, true then false at 0 and selected](docs/method.png)
94
+ ### Navigation: `∅` `●`
60
95
 
61
- ## Use
96
+ `&.` skips the rest of its chain when its receiver is nil.
97
+
98
+ ![A file's page: line 2, user&.account&.owner, its card open to its left](docs/example-nil.png)
99
+
100
+ - `∅` the receiver was nil, so `&.` skipped the rest of the chain.
101
+ - `●` the receiver was not nil, so `&.` made the call.
102
+
103
+ In this example, there was no test which exercised the path where
104
+ `user` was nil.
105
+
106
+ ### Count: `0` `1` `*`
107
+
108
+ A block method can call its block on any number of items.
109
+
110
+ ![A file's page: line 2, ids.any? { |id| id.odd? }, its card open to its left](docs/example-count.png)
111
+
112
+ - `0` the block was called with no items.
113
+ - `1` the block was called with one item.
114
+ - `*` the block was called with two or more items.
115
+
116
+ In this example, there was no test which exercised the path where
117
+ the block was called with two or more items and every answer was
118
+ `false`.
119
+
120
+ ### Both: `TF`
121
+
122
+ A block's answers can differ from item to item.
123
+
124
+ ![A file's page: line 2, ids.select { |id| id.odd? }, its card open to its left](docs/example-both.png)
125
+
126
+ - `T` every answer of the block was true.
127
+ - `F` every answer of the block was false.
128
+ - `TF` the block's answers were both true and false.
129
+
130
+ In this example, there was no test which exercised the path where
131
+ the block was called with two or more items and every answer was
132
+ `false`.
133
+
134
+ ### Lots of paths!
135
+
136
+ Ruby code can be very compact so short expressions can generate lots of paths!
137
+
138
+ ![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)
139
+
140
+
141
+ ## Getting started
62
142
 
63
143
  ```ruby
64
144
  # test coverage config, before any application code is required
@@ -70,14 +150,20 @@ end
70
150
  PathCoverage.start do
71
151
  root APP_DIR # cover globs and site names are relative to it
72
152
  cover 'source/**/*.rb' # each matching file is instrumented as it loads
153
+ cover 'test/**/*.rb' # more than one cover glob may be given
154
+ skip 'source/vendor/**/*.rb' # except these, though a cover glob matches them
73
155
  coverage_dir '/tmp/coverage' # where path_coverage.json is written
74
- measure :decisions, :methods # :decisions alone unless :methods is named
75
- report :gaps, :html # the reporters run at exit (default :gaps)
156
+ report :gaps, :html # the reporters run at exit (default :gaps);
157
+ # also :summary and :post
158
+ group 'code', 'source/**/*.rb' # each group a tab of its own on index.html
159
+ group 'test', 'test/**/*.rb'
160
+ ignore '&&=' # operators whose sites are not measured
161
+ treat 'name', as: :present # a call to name is never nil
162
+ treat 'any?', as: :nonempty # any? is true when its receiver has items
76
163
  end
77
164
  ```
78
165
 
79
- A misspelt setting or an unknown reporter is refused by name, with the list
80
- of the ones there are. Calling `start` again replaces the configuration.
166
+ ## Use when you cannot edit the code!
81
167
 
82
168
  A run whose code cannot be changed, such as a script in a `docker build`
83
169
  step, can be covered from its environment alone:
@@ -86,6 +172,7 @@ step, can be covered from its environment alone:
86
172
  RUBYOPT=-rpath_coverage/auto
87
173
  PATH_COVERAGE_ROOT=/app # root
88
174
  PATH_COVERAGE_COVER=src/**/*.rb,lib/*.rb # cover, comma-separated
175
+ PATH_COVERAGE_SKIP=src/vendor/**/*.rb # skip, comma-separated, optional
89
176
  PATH_COVERAGE_DIR=/tmp/paths # coverage_dir
90
177
  PATH_COVERAGE_COLLECTOR=http://host.docker.internal:4570/runs
91
178
  ```
@@ -122,27 +209,33 @@ As the process exits:
122
209
 
123
210
  - `path_coverage.json` is always written to `coverage_dir`: every site
124
211
  of every covered file, with each operand's true and false counts under
125
- `sites`, and each site's paths under `paths`. It is the base the other
126
- reporters read, so they can also be run later, in another process, from
212
+ `sites`, each site's paths under `paths`, each method's card under
213
+ `cards`, the statements reached under `reached`, and the paths taken
214
+ of all possible under `totals`, as [taken, possible]: `decisions`,
215
+ `methods`, and, with groups, each group's
216
+ own under `groups`, eg `"groups": {"code": {"decisions": [481, 567]}}`.
217
+ It also names the kinds of site measured under `kinds`, any `ignored`
218
+ operators and `treats`, and any paths taken that a treatment rules
219
+ out under `outside`.
220
+ It is the base the other reporters read, so they can also be run
221
+ later, in another process, from
127
222
  it alone: `PathCoverage::Render.from_json(config)`, where `config` is a
128
223
  `PathCoverage::Configuration` built from the same settings block.
129
224
  Given several runs' reports, `Render.from_json(config, paths)` adds
130
225
  their counts together first, so the report shows only the gaps that
131
- every run left.
132
- - `:gaps` prints one line per path never taken, in manifest order: a
133
- site's naming the outcome of each leaf on it, a method's the outcome
134
- of each decision on it, by line:
226
+ every run left. A report whose kinds, `ignored` or `treats` differ
227
+ from `config`'s is refused, as its sites list their paths
228
+ differently.
229
+ - `:gaps` prints one line per site path never taken, in manifest
230
+ order, naming the outcome of each leaf on it:
135
231
 
136
232
  ```text
137
233
  source/kata.rb:3:23 path never taken: `ok?(id)` true, `new?(id)` false
138
234
  source/kata.rb:4:24 never reached
139
- source/fee.rb:2:2 fee path never taken: line 4 true, line 7 false
140
235
  ```
141
236
 
142
237
  ### Decisions
143
238
 
144
- Measured always, as `:decisions`: the paths within each decision.
145
-
146
239
  A site is named by the `path:line:column` where it starts, the path
147
240
  relative to `root`. A site in a covered file the run never loaded is
148
241
  still reported, as `never reached`.
@@ -168,17 +261,8 @@ should read the JSON, whose `paths` give each site's leaves as
168
261
  ```
169
262
 
170
263
  A site is an outermost condition. Its leaves are numbered left to right
171
- across the whole of it, nested conditions included:
172
-
173
- ```ruby
174
- ok?(id) && new?(id) && id.size == 6 # one && site, leaves 0 1 2
175
- ok?(id) && (new?(id) || id.size == 6) # an || nested in an && site, 0 1 2
176
- ok?(id) && new?(id) || id.size == 6 # an && nested in an || site, 0 1 2
177
- ids.map { |k| k && k.ok? } && ok?(id) # two sites: the block's is its own
178
- ok?(id) ? :ok : :no # a ?: site, its predicate leaf 0
179
- ok?(id) && new?(id) ? :ok : :no # an && nested in a ?: site, 0 1
180
- ok?(id) ? new?(id) && id.size == 6 : :no # one ?: site, its then arm 1 2
181
- ```
264
+ across the whole of it, nested conditions included. A condition inside a
265
+ block is a site of its own.
182
266
 
183
267
  A ternary is a site even when its predicate is a single leaf. An arm
184
268
  holding a condition of `&&`, `||`, `and`, `or` or `?:` is part of the
@@ -201,16 +285,31 @@ whose `===` calls the pattern's `===` once, exactly as the `case` would,
201
285
  and notes whether it matched. A `case` with no subject, or with a splat
202
286
  among its patterns, is no site.
203
287
 
204
- A call to a block method, with no arguments and a block of one
205
- expression, is a site named at its method. Its two leaves are its
206
- receiver, showing how many items its block was called with (`0 items`,
207
- `1 item` or `2+ items`), and its block's expression: for `any?` `all?`
208
- `none?` `find` `detect` `find_index` and `take_while`, which stop once
209
- their block decides, its last answer; for `select` `filter` `reject`
210
- `partition` `count` and `filter_map`, which call it with every item,
211
- whether its answers were `all true`, `all false` or `both`. Both are
212
- noted from the block's own calls, so the receiver is never asked for its
213
- size, which could consume a sequence that can be read only once.
288
+ A call to `any?` `all?` `none?` `find` `detect` `find_index`
289
+ `take_while` `select` `filter` `reject` `partition` `count` or
290
+ `filter_map`, with no arguments and a block of one expression, is a site
291
+ named at its method. Its item count is noted from the block's own calls,
292
+ so the receiver is never asked its size, which could consume a sequence
293
+ that can be read only once.
294
+
295
+ A `||=` or `&&=` write is a site, whatever its target: a variable of any
296
+ kind, a constant, an attribute (`self.size ||= n`) or an index
297
+ (`store[key] ||= key.upcase`). Its one leaf is its target, and its right
298
+ side is an arm, run when the target is false for `||=`, true for `&&=`,
299
+ as an `unless` or an `if` runs its arm. The target is never read again,
300
+ since reading `store[key]` could call a method twice: its outcome follows
301
+ from whether the right side ran. So `@clock ||= Clock.now` called only
302
+ once, as a fresh object, leaves the path with `@clock` true a gap.
303
+
304
+ A `&.` chain is a site, named where it starts. Its leaves are the
305
+ receivers of its `&.` calls. `false` counts as not nil, as `&.` calls on
306
+ it.
307
+
308
+ A chain inside a condition, eg `ok?(id) && user&.admin?` or
309
+ `if user&.admin?`, folds into its site, as one expression: its value is
310
+ one more leaf, true or false, reached when every receiver is not nil. A
311
+ write through `&.`, eg `box&.size ||= 3`, is no site, as a nil `box`
312
+ skips it whole and its target is never looked at.
214
313
 
215
314
  A chain of one operator is flat; a change of operator, by precedence or by
216
315
  parentheses, nests. Parentheses are not part of the tree, since they decide
@@ -228,46 +327,74 @@ constants, since one can hold nil or false (`VERBOSE = false`). `nil` and
228
327
  @clock = options[:clock] || MonotonicClock.new # leaf 1: never evaluated, or no gap
229
328
  ```
230
329
 
330
+ Any other leaf, a call among them, is taken to have either outcome, as its
331
+ form alone cannot say which it returns. So a call that never returns nil
332
+ or false still leaves a path needing it falsy, which no test can take:
333
+
334
+ ```ruby
335
+ methods: paths && methods_of(path, methods, paths) # [true, false] impossible
336
+ methods: paths ? methods_of(path, methods, paths) : nil # the same, no such path
337
+ ```
338
+
339
+ Written as a choice, the same code has no such path. Reporting a gap that
340
+ cannot be closed is the lesser error than hiding one that can.
341
+
231
342
  ### Methods
232
343
 
233
- With `measure :decisions, :methods`, each method with two or more
234
- decisions also counts each call's path through them: the outcome of each
235
- decision it reached, in order. A method is named by the `path:line:column`
236
- of its `def`, then its name. Each decision is one outcome on the path,
237
- however many leaves its predicate has; those are its site's to show. So a
238
- site's columns stay its own leaves' combinations, and a method's its own
239
- decisions', rather than multiplying together. A decision whose arm always
240
- leaves the method ends the path there.
344
+ Each method has a card, named by the `path:line:column` of its `def`,
345
+ with a row per top-level statement. A row that is a site counts its
346
+ paths; any other row is one path, taken when the statement is reached.
347
+ A method's total adds up its rows. A file's code outside any method
348
+ gets a card of its own.
349
+
350
+ The JSON report's `cards` hold each card's rows:
351
+
352
+ ```json
353
+ "cards": {
354
+ "label.rb:1:0": {
355
+ "name": "label", "first": 1, "last": 7,
356
+ "rows": [
357
+ { "at": "2:02", "top": "if", "paths": 2, "taken": 1, "site": "label.rb:2:2" },
358
+ { "at": "3:04", "top": "value", "paths": 1, "taken": 1, "site": null },
359
+ { "at": "5:04", "top": "value", "paths": 1, "taken": 0, "site": null }
360
+ ]
361
+ }
362
+ }
363
+ ```
241
364
 
242
365
  ### Reporters
243
366
 
244
- - `:html` writes `index.html` to `coverage_dir`: the covered files with
245
- their sites and their paths taken of all possible, within each decision
246
- and, when `:methods` is measured, across a method's decisions (eg
247
- `9 / 28 (32%)`, or `-` for a file with no method of two or more
248
- decisions), each column sortable, and a Total row for every file
249
- together. Each file name opens `files/<path>.html`, the file's
250
- source. On a line holding a site the line number is a button (blue
251
- and outlined when every outcome was seen, solid orange when any is
252
- missing) that opens a grid of the site's paths, on the left. Its rows
253
- are the site's tree: its operators, nested as Ruby groups them, a
254
- ternary's arms marked `?` and `:`, an `if`'s named `then` and `else`,
255
- and its leaves. Its columns are the site's paths, never taken first,
256
- each headed by its count, orange when zero; a leaf shows its outcome on
257
- each path, T or F, blank where the path never reached it, and a plain
258
- arm a dot on each path that took it. A site written over several lines
259
- has one button over all of its line numbers. Grids with gaps start
260
- open. A header that stays at the top of the page lists every site as a
261
- link to its line, covered sites first, then those with gaps, and arrows
262
- beside each site with gaps jump to the previous and next one, wrapping
263
- round at either end. Measuring paths, each method's grid of its
264
- decisions' paths opens on the right.
367
+ - `:html` writes `index.html` to `coverage_dir`: a sortable row per
368
+ covered file, with its sites and its paths taken of all possible
369
+ within each method (eg `9 / 28 32%`, or `-` for a file with no
370
+ method of two or more decisions), and a Total row. Each file name
371
+ opens `files/<path>.html`, the file's source with its cards, as
372
+ Reading the HTML shows.
265
373
  - `:post` sends the JSON report, as an HTTP POST, to the url set by
266
374
  `collector 'http://...'`. It is for a run whose own files are thrown
267
375
  away, such as a failing `docker build` step. It prints nothing, never
268
376
  goes through an `http_proxy`, and gives up in silence on a collector
269
377
  that cannot be reached or does not answer within a second, so the
270
378
  run's output and exit status are unchanged.
379
+ - `:summary` prints each total as SimpleCov prints its line and branch
380
+ coverage:
381
+
382
+ ```text
383
+ Path coverage within decisions: 527 / 631 (83.52%)
384
+ Path coverage within methods: 44 / 52 (84.62%)
385
+ ```
386
+
387
+ With groups, each group is a heading, its own totals indented under it,
388
+ and there is no total over every group.
389
+
390
+ ```text
391
+ code:
392
+ Path coverage within decisions: 481 / 567 (84.83%)
393
+ Path coverage within methods: 41 / 49 (83.67%)
394
+ test:
395
+ Path coverage within decisions: 65 / 84 (77.38%)
396
+ Path coverage within methods: 3 / 3 (100.00%)
397
+ ```
271
398
 
272
399
  ## How it works
273
400
 
@@ -286,9 +413,8 @@ PathCoverage.leave(PathCoverage.enter("k.rb:2:2", 2), PathCoverage.record("k.rb:
286
413
 
287
414
  An `if` site's leave call wraps its predicate alone, as its path is
288
415
  decided there, so an arm that returns or raises still counts it.
289
- Measuring paths, each method starts a path at its entry, and each
290
- decision's predicate is wrapped in a decide call too, outside its site's
291
- calls.
416
+ Each statement that is no site gets a reach call before it, counting
417
+ its run, for its card's row.
292
418
 
293
419
  A command call such as `ok? id` (after `and`) is not valid as a bare
294
420
  argument, so that leaf alone gets parentheses of its own, and so does a
@@ -310,19 +436,65 @@ rewrite happens in memory; no file on disk changes.
310
436
  At exit the manifests parsed during the run are reused; only covered files
311
437
  the run never loaded are read and parsed then.
312
438
 
439
+ ## Ignoring and treating
440
+
441
+ `ignore '&&=', '||='` stops `&&=` and `||=` writes being measured.
442
+ A site inside such a write is still measured, eg the `a && b` in
443
+ `@m ||= a && b`. Ignoring `&&` does not ignore `and`, nor does ignoring
444
+ `||` ignore `or`. The summary and `index.html` list what is ignored.
445
+
446
+ `treat` tells path_coverage what you know about a method, so paths that
447
+ cannot happen are not listed as gaps. Add `in: 'source/models/**'` to
448
+ limit a treatment to the files that glob matches.
449
+
450
+ - `treat 'name', as: :present`: a call to `name` is never nil, so
451
+ `user.name&.size` has no path with `user.name` nil.
452
+ - `treat 'any?', as: :nonempty`: `x.any?` is true exactly when `x` has
453
+ items. When `x` is a call to `select`, `reject`, `filter`,
454
+ `filter_map`, `partition` or `take_while`, the path that call took
455
+ often decides whether it kept any items. Paths where `any?` disagrees
456
+ with that are not listed, eg `ids.select { |i| i.odd? }.any?` true
457
+ with `ids` empty.
458
+
459
+ A run where a treatment turns out to be wrong is reported:
460
+
461
+ ```text
462
+ source/user.rb:4:9 path taken that a treatment rules out: `user.name` nil
463
+ ```
464
+
313
465
  ## Roadmap
314
466
 
315
467
  - decisions hidden inside calls, each with its own outcome classes:
316
468
  - `one?`, whose last answer does not decide it, and block methods
317
469
  with blocks of more than one expression
318
470
  - `fetch(k, default)`, `dig`, `Hash#[]` with a default: present vs default
319
- - `x || default` in value context, `&.` safe navigation: nil vs not nil
320
- - `||=` and `&&=`, on variables, attributes and index targets: the
321
- right side evaluated vs skipped, counted without evaluating the
322
- target twice
471
+ - `x || default` in value context: nil vs not nil
323
472
  - `x rescue y` modifier: raised vs not raised
324
473
  - `case` with no subject, and `case`/`in` patterns
325
474
  - regex matches: nil vs MatchData
475
+ - `treat 'methods_of', as: :truthy`, so each call to it counts as a
476
+ leaf that is only truthy, as `Foo.new` is, and a run where it returns
477
+ nil or false is reported. Later, treatments could come from the
478
+ project's RBS signatures (`sig/*.rbs`) too.
479
+ - exceptions the code itself names: a `begin` or `def` with `rescue`
480
+ clauses as a site, like a `case`, whose paths are no raise and each
481
+ clause that rescued, eg `rescue IOError` never taken. Branch coverage
482
+ ignores `rescue`, and tests reach these paths with real input. A raise
483
+ that nothing names, from any call, stays out, as every leaf would gain
484
+ a third outcome that only fault injection could take.
485
+ - an evaluation that raised part way through a site, eg
486
+ `ok?(id) && fetch(id)` where `fetch` raises, reported as a path that
487
+ raised rather than dropped as unfinished.
488
+ - complexity maximums, reported and gated, since the possible paths
489
+ count how much there is to test, not only how much was tested. A
490
+ file's total measures its size, so each is a maximum over smaller
491
+ parts:
492
+ - paths in one decision, eg 6 for a `select` block
493
+ - paths through one method's decisions, eg 2 top-level ifs give 4
494
+ - paths within each decision, summed over every decision in one
495
+ method, nested ones included, eg decisions of 2, 3 and 15 paths
496
+ sum to 20. Every method has one, and it grows as the method gains
497
+ conditions, where the paths through a method multiply.
326
498
 
327
499
  ## Constraints
328
500
 
@@ -336,18 +508,54 @@ the run never loaded are read and parsed then.
336
508
  `make all` runs, in a Docker image built from the repo's `Dockerfile`:
337
509
 
338
510
  - `make lint`: rubocop over `lib/`, `test/` and `bin/` alike
339
- - `make test`: the tests, then a check that the coverage report has zero
340
- missed lines and branches in both the code and the tests, within the
341
- bounds in `test/coverage_metrics_params.json`
511
+ - `make test`: the tests, with path_coverage measuring its own `lib/` and
512
+ `test/`, then a check that SimpleCov's report has zero missed lines and
513
+ branches in both the code and the tests, and that every path within the
514
+ tests' methods was taken, within the bounds in
515
+ `test/coverage_metrics_params.json` (a path bound is eg
516
+ `test.paths.methods.missed`, and a missing path report denies it)
342
517
  (`make test tids='6D1K'` runs only matching tests, and skips the check)
343
518
  - `make test_check`: shunit2 tests showing that a breached bound fails the
344
519
  check with a non-zero exit
345
520
 
521
+ Each run prints its seed; `SEED=n make test` replays that run's order of
522
+ files and tests. When the coverage check fails, it names each line and
523
+ branch missed.
524
+
525
+ A run ends with each group, code and test, as a heading over its four
526
+ figures, SimpleCov's and path_coverage's together, then where each report
527
+ is, each with a code and a test tab, then the check's verdict:
528
+
529
+ ```text
530
+ code:
531
+ Line coverage: 1270 / 1270 (100.00%)
532
+ Branch coverage: 321 / 321 (100.00%)
533
+ Path coverage within decisions: 481 / 567 (84.83%)
534
+ Path coverage within methods: 41 / 49 (83.67%)
535
+ test:
536
+ Line coverage: 2250 / 2250 (100.00%)
537
+ Branch coverage: 43 / 43 (100.00%)
538
+ Path coverage within decisions: 65 / 84 (77.38%)
539
+ Path coverage within methods: 3 / 3 (100.00%)
540
+ SimpleCov report: .../reports/index.html
541
+ PathCov report: .../reports/paths/index.html
542
+ coverage: ALLOWED
543
+ ```
544
+
545
+ `bin/check_coverage_metrics.rb` prints the groups, reading SimpleCov's
546
+ figures from `reports/coverage_metrics.json` and path_coverage's from the
547
+ `totals` in `reports/paths/path_coverage.json`; SimpleCov's own console
548
+ lines, totals over every group, are silenced. The tool measuring paths is
549
+ a copy of `lib/` renamed `PathCoverageSelf` (`test/self_coverage.rb`), so
550
+ the tests start and restart `PathCoverage` as always. SimpleCov's figures
551
+ for `lib/` are the same with it as without.
552
+
346
553
  `make demo` (not part of `make all`) runs `demo/run.rb` through
347
554
  `bin/build_demo.sh`, writing the report to `site/` and opening it. The
348
555
  Pages workflow (`.github/workflows/pages.yml`) runs the same script on
349
556
  each push to `main` and publishes `site/`.
350
557
 
351
- `bin/capture_readme_images.sh` rebuilds the demo and saves this README's
352
- two screenshots, `docs/decision.png` and `docs/method.png`, from it with
353
- headless Chrome. Run it again whenever the report's look changes.
558
+ `bin/capture_card_images.sh` runs the examples in `demo/cards/` and
559
+ saves this README's screenshots, `docs/example-*.png`, with headless
560
+ Chrome. Run it again whenever the card's look changes.
561
+