path_coverage 0.0.2 → 0.2.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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +330 -84
  3. data/exe/path_coverage_collector +12 -1
  4. data/lib/path_coverage/calls.rb +106 -0
  5. data/lib/path_coverage/collector.rb +88 -22
  6. data/lib/path_coverage/condition.rb +77 -3
  7. data/lib/path_coverage/configuration.rb +52 -12
  8. data/lib/path_coverage/configuration_paths.rb +56 -0
  9. data/lib/path_coverage/console_summary.rb +77 -0
  10. data/lib/path_coverage/gaps.rb +25 -9
  11. data/lib/path_coverage/html/index.html +3 -14
  12. data/lib/path_coverage/html/side.js +36 -6
  13. data/lib/path_coverage/html/sort.js +27 -21
  14. data/lib/path_coverage/html/style.css +32 -1
  15. data/lib/path_coverage/html/tabs.js +15 -0
  16. data/lib/path_coverage/html_index.rb +105 -19
  17. data/lib/path_coverage/html_index_head.rb +49 -0
  18. data/lib/path_coverage/html_report.rb +44 -12
  19. data/lib/path_coverage/html_tabs.rb +26 -0
  20. data/lib/path_coverage/html_tree.rb +47 -17
  21. data/lib/path_coverage/json_reader.rb +94 -0
  22. data/lib/path_coverage/json_writer.rb +44 -8
  23. data/lib/path_coverage/kernel.rb +53 -45
  24. data/lib/path_coverage/kinds/base.rb +71 -0
  25. data/lib/path_coverage/kinds/block.rb +136 -0
  26. data/lib/path_coverage/kinds/case.rb +89 -0
  27. data/lib/path_coverage/kinds/chain.rb +45 -0
  28. data/lib/path_coverage/kinds/choice.rb +68 -0
  29. data/lib/path_coverage/kinds/contains.rb +111 -0
  30. data/lib/path_coverage/kinds/safe.rb +151 -0
  31. data/lib/path_coverage/kinds/write.rb +54 -0
  32. data/lib/path_coverage/kinds.rb +37 -0
  33. data/lib/path_coverage/manifest.rb +93 -26
  34. data/lib/path_coverage/manifest_blocks.rb +96 -0
  35. data/lib/path_coverage/manifest_case.rb +54 -0
  36. data/lib/path_coverage/manifest_chains.rb +76 -0
  37. data/lib/path_coverage/manifest_choices.rb +64 -32
  38. data/lib/path_coverage/manifest_folding.rb +91 -0
  39. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  40. data/lib/path_coverage/manifest_writes.rb +67 -0
  41. data/lib/path_coverage/merge.rb +19 -3
  42. data/lib/path_coverage/post.rb +8 -8
  43. data/lib/path_coverage/recorder.rb +43 -0
  44. data/lib/path_coverage/recorder_blocks.rb +68 -0
  45. data/lib/path_coverage/recorder_cases.rb +23 -0
  46. data/lib/path_coverage/recorder_chains.rb +40 -0
  47. data/lib/path_coverage/recorder_writes.rb +26 -0
  48. data/lib/path_coverage/render.rb +58 -46
  49. data/lib/path_coverage/result.rb +19 -0
  50. data/lib/path_coverage/rewriter.rb +64 -37
  51. data/lib/path_coverage/rewriter_nested.rb +62 -0
  52. data/lib/path_coverage/site.rb +6 -1
  53. data/lib/path_coverage/site_paths.rb +9 -46
  54. data/lib/path_coverage/site_tree.rb +31 -36
  55. data/lib/path_coverage/summary.rb +64 -17
  56. data/lib/path_coverage/totals.rb +29 -0
  57. data/lib/path_coverage/version.rb +1 -1
  58. metadata +35 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: acac92dac750f3680a27856c6e410469f26c48c6ec23d6363d8b22caba5bc30c
4
- data.tar.gz: c19de09501f409d8209ddb1071f37e44b926d9a4381ec302a6c460961ff162f8
3
+ metadata.gz: c68ba875b18ba000eb6be70f5c7bcb294ab4d50915868b913825c3f53bc60a93
4
+ data.tar.gz: 2beaba1e32cd4110ff9d04d3ab06e8c689395748566502a092175d8a339c9a5d
5
5
  SHA512:
6
- metadata.gz: 2ec2945b4797a6b1f51f558c2cf2348307e1b6da68e5812c8ad4dfcab51f85926716f425c695cfd7456fec679d17fabf2e6b1400f7f6e131b0ccd9608aed5bce
7
- data.tar.gz: 196816d6db1d908d2c578f7c547a9464025187d03c344537b741106faee39aa6c0c1127759456989442497fbc7a33f773a0ca096f92e5aaa599cbec9949f27f6
6
+ metadata.gz: 6539c5ce97bd892ee616d0b1f780c79f18624381a404d3e6d2c60f6245957243b5ac7c395e130496fbbb2ba0ac6d69b4e9daf8e4fd235cb124d184aa3f8e5ad2
7
+ data.tar.gz: f72f62b796ad07d0290f56a88bc920ef1088b81abc04c6667ae1ee612e97499cc2f40ca6689acb1e8df35ebff607b463e8ec5ef271de11227bcf01017e98bb42
data/README.md CHANGED
@@ -1,58 +1,25 @@
1
1
  # path_coverage
2
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/cyber-dojo/path_coverage)](LICENSE.md)
6
+
3
7
  Path coverage for Ruby, to run alongside SimpleCov's line and branch
4
8
  coverage.
5
9
 
6
- Gem: https://rubygems.org/gems/path_coverage (which shows the latest
7
- version).
8
-
9
- Status: sites cover `&&`, `||`, `and`, `or`, the ternary `?:`, and
10
- `if`/`elsif`/`else`: each leaf operand of a condition, however its
11
- operators are chained, nested or grouped. Method paths cover `if`
12
- decisions. Decisions hidden inside calls come next (see Roadmap).
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 `if` decisions.
16
+ More decisions hidden inside calls come next (see Roadmap).
13
17
 
14
18
  Demo: https://cyber-dojo.github.io/path_coverage/ shows the HTML report
15
19
  for the mock source tree in `demo/`. `make demo` builds and opens the
16
20
  same report locally.
17
21
 
18
- Take this method:
19
-
20
- ```ruby
21
- def fee(user, order)
22
- total = 100
23
- if user.member? && order.paid?
24
- total -= 5
25
- end
26
- if order.big?
27
- total -= 10
28
- end
29
- total
30
- end
31
- ```
32
-
33
- Tests that run both `if`s both ways reach every line and every branch,
34
- so SimpleCov, which reads Ruby's built-in `Coverage`, reports 100%. Yet
35
- they can still miss paths, at two scopes, and path_coverage lists each
36
- path no test took.
37
-
38
- ## Within each decision
39
-
40
- The first `if` can go three ways: member and paid, member and not
41
- paid, not a member. A member who has not paid may never have been
42
- tried.
43
-
44
- ![The first if's grid: a column per way it can go, each headed by
45
- its count, the member who has not paid at 0](docs/decision.png)
46
-
47
- ## Across a method's decisions
48
-
49
- The two `if`s can go four ways together. A paying member with a small
50
- order may never have been tried.
51
-
52
- ![fee's grid: a column per way its two ifs can go together, each
53
- headed by its count, true then false at 0 and selected](docs/method.png)
54
-
55
- ## Use
22
+ ## Getting started
56
23
 
57
24
  ```ruby
58
25
  # test coverage config, before any application code is required
@@ -64,14 +31,101 @@ end
64
31
  PathCoverage.start do
65
32
  root APP_DIR # cover globs and site names are relative to it
66
33
  cover 'source/**/*.rb' # each matching file is instrumented as it loads
34
+ cover 'test/**/*.rb' # more than one cover glob may be given
35
+ skip 'source/vendor/**/*.rb' # except these, though a cover glob matches them
67
36
  coverage_dir '/tmp/coverage' # where path_coverage.json is written
68
- measure :conditions, :paths # sites alone unless :paths is named too
69
- report :gaps, :html # the reporters run at exit (default :gaps)
37
+ measure :decisions, :methods # :decisions alone unless :methods is named
38
+ report :gaps, :html # the reporters run at exit (default :gaps);
39
+ # also :summary and :post
40
+ group 'code', 'source/**/*.rb' # each group a tab of its own on index.html
41
+ group 'test', 'test/**/*.rb'
42
+ ignore '&&=' # operators whose sites are not measured
43
+ treat 'name', as: :present # a call to name is never nil
44
+ treat 'any?', as: :nonempty # any? is true when its receiver has items
70
45
  end
71
46
  ```
72
47
 
73
- A misspelt setting or an unknown reporter is refused by name, with the list
74
- of the ones there are. Calling `start` again replaces the configuration.
48
+
49
+ ## Reading the HTML
50
+
51
+ Clicking a line number on a file's HTML page opens that line's popout.
52
+
53
+ Each popout has two sections:
54
+ - on the left is a hierarchical representation of the code fragment.
55
+ - on the right is one column for each possible path through that fragment.
56
+ - at the top of the column is the count of that path. Look for zeros!
57
+ - the symbols in the column align with their fragment and are explained below.
58
+
59
+ ### Boolean: `T` `F`
60
+
61
+ ![A file's page: line 2, name.size > 2 && name.ascii_only?, its button pressed, its popout open beside it](docs/example-outcome.png)
62
+
63
+ - `T` the expression was true.
64
+ - `F` the expression was false.
65
+
66
+ In this example, there was no test which exercised the path where `name.size > 2` was `true`, and
67
+ `name.ascii_only?` was `false`.
68
+
69
+ ### Unreachable: blank
70
+
71
+ ![A file's page: line 2, @x = dee || dum, its button pressed, its popout open beside it](docs/example-unreachable.png)
72
+
73
+ - "blank" the expression is never evaluated.
74
+
75
+ In the second column `dee` was `T`, so because of `||`'s short-circuiting, `dum` is never evaluated.
76
+
77
+ ### Taken: `✓`
78
+
79
+ ![A file's page: line 2, if paid, its button pressed, its popout open beside it](docs/example-taken.png)
80
+
81
+ - `✓` marks the column whose path took that "arm"
82
+
83
+ The `paid` row is the `if`'s predicate, `T` or `F`. Below it is one row
84
+ per arm, named `then` and `else`. The first column, `paid` false, took `else`; the
85
+ second, `paid` true, took `then`.
86
+
87
+ ### Navigation: `∅` `●`
88
+
89
+ ![A file's page: line 2, user&.account&.owner, its button pressed, its popout open beside it](docs/example-nil.png)
90
+
91
+ - `∅` the receiver was nil, so `&.` skipped the rest of the chain.
92
+ - `●` the receiver was not nil, so `&.` made the call.
93
+
94
+ In this example, there was no test which exercised the path where
95
+ `user` was nil.
96
+
97
+ ### Count: `0` `1` `*`
98
+
99
+ ![A file's page: line 2, ids.any? { |id| id.odd? }, its button pressed, its popout open beside it](docs/example-count.png)
100
+
101
+ - `0` the block was called with no items.
102
+ - `1` the block was called with one item.
103
+ - `*` the block was called with two or more items.
104
+
105
+ In this example, there was no test which exercised the path where
106
+ the block was called with two or more items and every answer was
107
+ `false`.
108
+
109
+ ### Both: `TF`
110
+
111
+ ![A file's page: line 2, ids.select { |id| id.odd? }, its button pressed, its popout open beside it](docs/example-both.png)
112
+
113
+ - `T` every answer of the block was true.
114
+ - `F` every answer of the block was false.
115
+ - `TF` the block's answers were both true and false.
116
+
117
+ In this example, there was no test which exercised the path where
118
+ the block was called with two or more items and every answer was
119
+ `false`.
120
+
121
+ ### Lots of paths!
122
+
123
+ Ruby code can be very compact so short expressions can generate lots of paths!
124
+
125
+ ![A file's page: line 2, if user&.admin? && ids.select { |id| id.odd? }.any?, its button pressed, its popout open beside it](docs/example-overview.png)
126
+
127
+
128
+ ## Use when you cannot edit the code!
75
129
 
76
130
  A run whose code cannot be changed, such as a script in a `docker build`
77
131
  step, can be covered from its environment alone:
@@ -80,6 +134,7 @@ step, can be covered from its environment alone:
80
134
  RUBYOPT=-rpath_coverage/auto
81
135
  PATH_COVERAGE_ROOT=/app # root
82
136
  PATH_COVERAGE_COVER=src/**/*.rb,lib/*.rb # cover, comma-separated
137
+ PATH_COVERAGE_SKIP=src/vendor/**/*.rb # skip, comma-separated, optional
83
138
  PATH_COVERAGE_DIR=/tmp/paths # coverage_dir
84
139
  PATH_COVERAGE_COLLECTOR=http://host.docker.internal:4570/runs
85
140
  ```
@@ -93,9 +148,18 @@ each report POSTed to it as a numbered file, and once every run is done
93
148
  they render as one report:
94
149
 
95
150
  ```sh
96
- path_coverage_collector 4570 /tmp/paths/runs # until stopped
151
+ path_coverage_collector 4570 /tmp/paths/runs # until sent TERM
97
152
  ```
98
153
 
154
+ - A report is saved before it is answered 200, so a 200 means it is on
155
+ disk. A report cut short, eg by a sender dying mid-send, is answered
156
+ 400 and not saved. One that cannot be saved is answered 500.
157
+ - `GET /alive` answers 200 while the collector runs.
158
+ - `GET /ready` answers 200 when its dir is writable, 503 when not. Wait
159
+ for it before starting the runs.
160
+ - On TERM it stops accepting connections, finishes the report in hand,
161
+ and exits 0.
162
+
99
163
  ```ruby
100
164
  runs = Dir['/tmp/paths/runs/*.json']
101
165
  PathCoverage::Render.from_json(config, runs)
@@ -107,13 +171,23 @@ As the process exits:
107
171
 
108
172
  - `path_coverage.json` is always written to `coverage_dir`: every site
109
173
  of every covered file, with each operand's true and false counts under
110
- `sites`, and each site's paths under `paths`. It is the base the other
111
- reporters read, so they can also be run later, in another process, from
174
+ `sites`, each site's paths under `paths`, each method's paths taken
175
+ under `method_paths` when `:methods` is measured, and the paths taken
176
+ of all possible under `totals`, as [taken, possible]: `decisions`,
177
+ `methods` when `:methods` is measured, and, with groups, each group's
178
+ own under `groups`, eg `"groups": {"code": {"decisions": [481, 567]}}`.
179
+ It also names the kinds of site measured under `kinds`, any `ignored`
180
+ operators and `treats`, and any paths taken that a treatment rules
181
+ out under `outside`.
182
+ It is the base the other reporters read, so they can also be run
183
+ later, in another process, from
112
184
  it alone: `PathCoverage::Render.from_json(config)`, where `config` is a
113
185
  `PathCoverage::Configuration` built from the same settings block.
114
186
  Given several runs' reports, `Render.from_json(config, paths)` adds
115
187
  their counts together first, so the report shows only the gaps that
116
- every run left.
188
+ every run left. A report whose kinds, `ignored` or `treats` differ
189
+ from `config`'s is refused, as its sites list their paths
190
+ differently.
117
191
  - `:gaps` prints one line per path never taken, in manifest order: a
118
192
  site's naming the outcome of each leaf on it, a method's the outcome
119
193
  of each decision on it, by line:
@@ -124,7 +198,10 @@ source/kata.rb:4:24 never reached
124
198
  source/fee.rb:2:2 fee path never taken: line 4 true, line 7 false
125
199
  ```
126
200
 
127
- ### Sites
201
+ ### Decisions
202
+
203
+ The paths within each decision are always measured, whatever `measure`
204
+ names.
128
205
 
129
206
  A site is named by the `path:line:column` where it starts, the path
130
207
  relative to `root`. A site in a covered file the run never loaded is
@@ -151,17 +228,8 @@ should read the JSON, whose `paths` give each site's leaves as
151
228
  ```
152
229
 
153
230
  A site is an outermost condition. Its leaves are numbered left to right
154
- across the whole of it, nested conditions included:
155
-
156
- ```ruby
157
- ok?(id) && new?(id) && id.size == 6 # one && site, leaves 0 1 2
158
- ok?(id) && (new?(id) || id.size == 6) # an || nested in an && site, 0 1 2
159
- ok?(id) && new?(id) || id.size == 6 # an && nested in an || site, 0 1 2
160
- ids.any? { |k| k && k.ok? } && ok?(id) # two sites: the block's is its own
161
- ok?(id) ? :ok : :no # a ?: site, its predicate leaf 0
162
- ok?(id) && new?(id) ? :ok : :no # an && nested in a ?: site, 0 1
163
- ok?(id) ? new?(id) && id.size == 6 : :no # one ?: site, its then arm 1 2
164
- ```
231
+ across the whole of it, nested conditions included. A condition inside a
232
+ block is a site of its own.
165
233
 
166
234
  A ternary is a site even when its predicate is a single leaf. An arm
167
235
  holding a condition of `&&`, `||`, `and`, `or` or `?:` is part of the
@@ -170,12 +238,45 @@ paths run through whichever arm the predicate picks. Any other arm, like a
170
238
  block, is not part of it, though a condition inside it is a site of its
171
239
  own.
172
240
 
173
- An `if` (with its `end`, not a modifier) is a site too, even when its
174
- predicate is a single leaf: its predicate nests in it, an `&&` there
241
+ An `if` or an `unless`, a modifier included, is a site too, even when
242
+ its predicate is a single leaf: its predicate nests in it, an `&&` there
175
243
  being no site of its own, and its `then` and `else` arms are plain, as
176
- they are statements. Its extent is the `if` keyword to the end of its
177
- predicate, so a site inside an arm keeps its own button. An `elsif` is a
178
- site of its own.
244
+ they are statements; an `unless` takes its `then` arm when its predicate
245
+ is false. It is named at its keyword, and its extent is its keyword to
246
+ the end of its predicate, so a site inside an arm keeps its own button.
247
+ An `elsif` is a site of its own.
248
+
249
+ A `case` with a subject is a site whose leaves are its `when` patterns,
250
+ tried in order until one matches. Each pattern is wrapped in a stand-in
251
+ whose `===` calls the pattern's `===` once, exactly as the `case` would,
252
+ and notes whether it matched. A `case` with no subject, or with a splat
253
+ among its patterns, is no site.
254
+
255
+ A call to `any?` `all?` `none?` `find` `detect` `find_index`
256
+ `take_while` `select` `filter` `reject` `partition` `count` or
257
+ `filter_map`, with no arguments and a block of one expression, is a site
258
+ named at its method. Its item count is noted from the block's own calls,
259
+ so the receiver is never asked its size, which could consume a sequence
260
+ that can be read only once.
261
+
262
+ A `||=` or `&&=` write is a site, whatever its target: a variable of any
263
+ kind, a constant, an attribute (`self.size ||= n`) or an index
264
+ (`store[key] ||= key.upcase`). Its one leaf is its target, and its right
265
+ side is an arm, run when the target is false for `||=`, true for `&&=`,
266
+ as an `unless` or an `if` runs its arm. The target is never read again,
267
+ since reading `store[key]` could call a method twice: its outcome follows
268
+ from whether the right side ran. So `@clock ||= Clock.now` called only
269
+ once, as a fresh object, leaves the path with `@clock` true a gap.
270
+
271
+ A `&.` chain is a site, named where it starts. Its leaves are the
272
+ receivers of its `&.` calls. `false` counts as not nil, as `&.` calls on
273
+ it.
274
+
275
+ A chain inside a condition, eg `ok?(id) && user&.admin?` or
276
+ `if user&.admin?`, folds into its site, as one expression: its value is
277
+ one more leaf, true or false, reached when every receiver is not nil. A
278
+ write through `&.`, eg `box&.size ||= 3`, is no site, as a nil `box`
279
+ skips it whole and its target is never looked at.
179
280
 
180
281
  A chain of one operator is flat; a change of operator, by precedence or by
181
282
  parentheses, nests. Parentheses are not part of the tree, since they decide
@@ -193,9 +294,21 @@ constants, since one can hold nil or false (`VERBOSE = false`). `nil` and
193
294
  @clock = options[:clock] || MonotonicClock.new # leaf 1: never evaluated, or no gap
194
295
  ```
195
296
 
297
+ Any other leaf, a call among them, is taken to have either outcome, as its
298
+ form alone cannot say which it returns. So a call that never returns nil
299
+ or false still leaves a path needing it falsy, which no test can take:
300
+
301
+ ```ruby
302
+ methods: paths && methods_of(path, methods, paths) # [true, false] impossible
303
+ methods: paths ? methods_of(path, methods, paths) : nil # the same, no such path
304
+ ```
305
+
306
+ Written as a choice, the same code has no such path. Reporting a gap that
307
+ cannot be closed is the lesser error than hiding one that can.
308
+
196
309
  ### Methods
197
310
 
198
- With `measure :conditions, :paths`, each method with two or more
311
+ With `measure :decisions, :methods`, each method with two or more
199
312
  decisions also counts each call's path through them: the outcome of each
200
313
  decision it reached, in order. A method is named by the `path:line:column`
201
314
  of its `def`, then its name. Each decision is one outcome on the path,
@@ -204,11 +317,27 @@ site's columns stay its own leaves' combinations, and a method's its own
204
317
  decisions', rather than multiplying together. A decision whose arm always
205
318
  leaves the method ends the path there.
206
319
 
320
+ The JSON report's `method_paths` lists each method's paths taken, with
321
+ their counts, so a later render, or several runs' reports merged, still
322
+ shows them. A report with none, eg from a run that did not measure
323
+ `:methods`, renders as no method path taken:
324
+
325
+ ```json
326
+ "method_paths": {
327
+ "source/fee.rb:2:2": [
328
+ { "path": [true, false], "count": 1 }
329
+ ]
330
+ }
331
+ ```
332
+
207
333
  ### Reporters
208
334
 
209
335
  - `:html` writes `index.html` to `coverage_dir`: the covered files with
210
- their sites, outcomes seen (with a percent) and outcomes missing, each
211
- column sortable. Each file name opens `files/<path>.html`, the file's
336
+ their sites and their paths taken of all possible, within each decision
337
+ and, when `:methods` is measured, within each method (eg
338
+ `9 / 28 (32%)`, or `-` for a file with no method of two or more
339
+ decisions), each column sortable, and a Total row for every file
340
+ together. Each file name opens `files/<path>.html`, the file's
212
341
  source. On a line holding a site the line number is a button (blue
213
342
  and outlined when every outcome was seen, solid orange when any is
214
343
  missing) that opens a grid of the site's paths, on the left. Its rows
@@ -230,6 +359,25 @@ leaves the method ends the path there.
230
359
  goes through an `http_proxy`, and gives up in silence on a collector
231
360
  that cannot be reached or does not answer within a second, so the
232
361
  run's output and exit status are unchanged.
362
+ - `:summary` prints each total as SimpleCov prints its line and branch
363
+ coverage, the methods line only when `:methods` is measured:
364
+
365
+ ```text
366
+ Path coverage within decisions: 527 / 631 (83.52%)
367
+ Path coverage within methods: 44 / 52 (84.62%)
368
+ ```
369
+
370
+ With groups, each group is a heading, its own totals indented under it,
371
+ and there is no total over every group.
372
+
373
+ ```text
374
+ code:
375
+ Path coverage within decisions: 481 / 567 (84.83%)
376
+ Path coverage within methods: 41 / 49 (83.67%)
377
+ test:
378
+ Path coverage within decisions: 65 / 84 (77.38%)
379
+ Path coverage within methods: 3 / 3 (100.00%)
380
+ ```
233
381
 
234
382
  ## How it works
235
383
 
@@ -272,20 +420,65 @@ rewrite happens in memory; no file on disk changes.
272
420
  At exit the manifests parsed during the run are reused; only covered files
273
421
  the run never loaded are read and parsed then.
274
422
 
423
+ ## Ignoring and treating
424
+
425
+ `ignore '&&=', '||='` stops `&&=` and `||=` writes being measured.
426
+ A site inside such a write is still measured, eg the `a && b` in
427
+ `@m ||= a && b`. Ignoring `&&` does not ignore `and`, nor does ignoring
428
+ `||` ignore `or`. The summary and `index.html` list what is ignored.
429
+
430
+ `treat` tells path_coverage what you know about a method, so paths that
431
+ cannot happen are not listed as gaps. Add `in: 'source/models/**'` to
432
+ limit a treatment to the files that glob matches.
433
+
434
+ - `treat 'name', as: :present`: a call to `name` is never nil, so
435
+ `user.name&.size` has no path with `user.name` nil.
436
+ - `treat 'any?', as: :nonempty`: `x.any?` is true exactly when `x` has
437
+ items. When `x` is a call to `select`, `reject`, `filter`,
438
+ `filter_map`, `partition` or `take_while`, the path that call took
439
+ often decides whether it kept any items. Paths where `any?` disagrees
440
+ with that are not listed, eg `ids.select { |i| i.odd? }.any?` true
441
+ with `ids` empty.
442
+
443
+ A run where a treatment turns out to be wrong is reported:
444
+
445
+ ```text
446
+ source/user.rb:4:9 path taken that a treatment rules out: `user.name` nil
447
+ ```
448
+
275
449
  ## Roadmap
276
450
 
277
451
  - decisions hidden inside calls, each with its own outcome classes:
278
- - `any?` / `all?` / `none?`: empty, decided by the first item, decided
279
- later, never decided
280
- - `find` / `detect`: found vs nil
452
+ - `one?`, whose last answer does not decide it, and block methods
453
+ with blocks of more than one expression
281
454
  - `fetch(k, default)`, `dig`, `Hash#[]` with a default: present vs default
282
- - `x || default` in value context, `&.` safe navigation: nil vs not nil
283
- - `||=` and `&&=`, on variables, attributes and index targets: the
284
- right side evaluated vs skipped, counted without evaluating the
285
- target twice
455
+ - `x || default` in value context: nil vs not nil
286
456
  - `x rescue y` modifier: raised vs not raised
287
- - `unless`, modifier `if`, `case`/`when` and `in` patterns
457
+ - `case` with no subject, and `case`/`in` patterns
288
458
  - regex matches: nil vs MatchData
459
+ - `treat 'methods_of', as: :truthy`, so each call to it counts as a
460
+ leaf that is only truthy, as `Foo.new` is, and a run where it returns
461
+ nil or false is reported. Later, treatments could come from the
462
+ project's RBS signatures (`sig/*.rbs`) too.
463
+ - exceptions the code itself names: a `begin` or `def` with `rescue`
464
+ clauses as a site, like a `case`, whose paths are no raise and each
465
+ clause that rescued, eg `rescue IOError` never taken. Branch coverage
466
+ ignores `rescue`, and tests reach these paths with real input. A raise
467
+ that nothing names, from any call, stays out, as every leaf would gain
468
+ a third outcome that only fault injection could take.
469
+ - an evaluation that raised part way through a site, eg
470
+ `ok?(id) && fetch(id)` where `fetch` raises, reported as a path that
471
+ raised rather than dropped as unfinished.
472
+ - complexity maximums, reported and gated, since the possible paths
473
+ count how much there is to test, not only how much was tested. A
474
+ file's total measures its size, so each is a maximum over smaller
475
+ parts:
476
+ - paths in one decision, eg 6 for a `select` block
477
+ - paths through one method's decisions, eg 2 top-level ifs give 4
478
+ - paths within each decision, summed over every decision in one
479
+ method, nested ones included, eg decisions of 2, 3 and 15 paths
480
+ sum to 20. Every method has one, and it grows as the method gains
481
+ conditions, where the paths through a method multiply.
289
482
 
290
483
  ## Constraints
291
484
 
@@ -299,13 +492,48 @@ the run never loaded are read and parsed then.
299
492
  `make all` runs, in a Docker image built from the repo's `Dockerfile`:
300
493
 
301
494
  - `make lint`: rubocop over `lib/`, `test/` and `bin/` alike
302
- - `make test`: the tests, then a check that the coverage report has zero
303
- missed lines and branches in both the code and the tests, within the
304
- bounds in `test/coverage_metrics_params.json`
495
+ - `make test`: the tests, with path_coverage measuring its own `lib/` and
496
+ `test/`, then a check that SimpleCov's report has zero missed lines and
497
+ branches in both the code and the tests, and that every path within the
498
+ tests' methods was taken, within the bounds in
499
+ `test/coverage_metrics_params.json` (a path bound is eg
500
+ `test.paths.methods.missed`, and a missing path report denies it)
305
501
  (`make test tids='6D1K'` runs only matching tests, and skips the check)
306
502
  - `make test_check`: shunit2 tests showing that a breached bound fails the
307
503
  check with a non-zero exit
308
504
 
505
+ Each run prints its seed; `SEED=n make test` replays that run's order of
506
+ files and tests. When the coverage check fails, it names each line and
507
+ branch missed.
508
+
509
+ A run ends with each group, code and test, as a heading over its four
510
+ figures, SimpleCov's and path_coverage's together, then where each report
511
+ is, each with a code and a test tab, then the check's verdict:
512
+
513
+ ```text
514
+ code:
515
+ Line coverage: 1270 / 1270 (100.00%)
516
+ Branch coverage: 321 / 321 (100.00%)
517
+ Path coverage within decisions: 481 / 567 (84.83%)
518
+ Path coverage within methods: 41 / 49 (83.67%)
519
+ test:
520
+ Line coverage: 2250 / 2250 (100.00%)
521
+ Branch coverage: 43 / 43 (100.00%)
522
+ Path coverage within decisions: 65 / 84 (77.38%)
523
+ Path coverage within methods: 3 / 3 (100.00%)
524
+ Coverage report: .../reports/index.html
525
+ Path coverage report: .../reports/paths/index.html
526
+ coverage: ALLOWED
527
+ ```
528
+
529
+ `bin/check_coverage_metrics.rb` prints the groups, reading SimpleCov's
530
+ figures from `reports/coverage_metrics.json` and path_coverage's from the
531
+ `totals` in `reports/paths/path_coverage.json`; SimpleCov's own console
532
+ lines, totals over every group, are silenced. The tool measuring paths is
533
+ a copy of `lib/` renamed `PathCoverageSelf` (`test/self_coverage.rb`), so
534
+ the tests start and restart `PathCoverage` as always. SimpleCov's figures
535
+ for `lib/` are the same with it as without.
536
+
309
537
  `make demo` (not part of `make all`) runs `demo/run.rb` through
310
538
  `bin/build_demo.sh`, writing the report to `site/` and opening it. The
311
539
  Pages workflow (`.github/workflows/pages.yml`) runs the same script on
@@ -314,3 +542,21 @@ each push to `main` and publishes `site/`.
314
542
  `bin/capture_readme_images.sh` rebuilds the demo and saves this README's
315
543
  two screenshots, `docs/decision.png` and `docs/method.png`, from it with
316
544
  headless Chrome. Run it again whenever the report's look changes.
545
+
546
+
547
+ ## Within each decision
548
+
549
+ The first `if` can go three ways: member and paid, member and not
550
+ paid, not a member. A member who has not paid may never have been
551
+ tried.
552
+
553
+ ![The first if's grid: a column per way it can go, each headed by
554
+ its count, the member who has not paid at 0](docs/decision.png)
555
+
556
+ ## Within each method
557
+
558
+ The two `if`s can go four ways together. A paying member with a small
559
+ order may never have been tried.
560
+
561
+ ![fee's grid: a column per way its two ifs can go together, each
562
+ headed by its count, true then false at 0 and selected](docs/method.png)
@@ -9,7 +9,13 @@ if ARGV.first == '-h'
9
9
 
10
10
  Listens on PORT and saves each JSON report the :post reporter
11
11
  sends as DIR/run-0001.json, DIR/run-0002.json, and so on,
12
- creating DIR if need be. Runs until stopped.
12
+ creating DIR if need be. A 200 means the report is saved.
13
+
14
+ GET /alive answers 200 while it runs.
15
+ GET /ready answers 200 when DIR is writable, 503 when not.
16
+
17
+ Runs until sent TERM. It then stops accepting connections,
18
+ finishes the report in hand, and exits 0.
13
19
 
14
20
  Options:
15
21
  -h Show this help
@@ -23,4 +29,9 @@ end
23
29
  port, dir = ARGV
24
30
  FileUtils.mkdir_p(dir)
25
31
  server = TCPServer.new('0.0.0.0', Integer(port))
32
+ # Closing the server on TERM makes serve return once the
33
+ # report in hand, if any, is saved and answered. The close
34
+ # runs in a thread of its own, as the trap runs on the thread
35
+ # that may be waiting in accept, where a close deadlocks.
36
+ Signal.trap('TERM') { Thread.new { server.close } }
26
37
  PathCoverage::Collector.serve(server, dir)