bashcov 4.0.0 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf955fd7135b192e4da886b5f8348f73422e06fd658bfdf11935533f3522f197
4
- data.tar.gz: d63c79fce3a315693f96a32a6a4fe7a40c94ea32c3bfd7a0019c9a94cb67c145
3
+ metadata.gz: e79dc939af127bef6f99cd953e085033f167a5221e332a133951e476f9b63f6e
4
+ data.tar.gz: 0aaf4dfc8062e7a14efa1b7ccfd1f2dc8ac7e86b53f79c31500019cfae3ab97d
5
5
  SHA512:
6
- metadata.gz: b7e891c4b1b9d272c0c11f44bb008deda2aff88f33bf5d0c9068d5f4a396f7efaa1bd915610b498126d07bef2e9512b2a4efd88dd76cef19d58b9d89d796b51b
7
- data.tar.gz: 7a26df75b930a634741ef9f9e6a1d23c80be4a05dd1164623c7f1b907f3ae76450ed3694fe2d64201e59171fdde8638af9db726853849fc644acf8309bc583e0
6
+ metadata.gz: f04d9cf50ddd7a95843676be55deb850201dea08ac7ef33bc6e7adfc17befe7e8ef14cc79a7899a2e529e058beea1930895344dfba915f6c3b8063f0faca5d49
7
+ data.tar.gz: 43ff857b123ca2c5fa85bb03dc707ea4a52c0601bfc3eab1fc94863136298a12614315b2b369569c74071e97ab88525599780bbaa344a862d43a0b285c0da004
data/CHANGELOG.md CHANGED
@@ -1,7 +1,24 @@
1
- ## Unreleased ([changes](https://github.com/infertux/bashcov/compare/v4.0.0...master))
1
+ ## Unreleased ([changes](https://github.com/infertux/bashcov/compare/v4.0.2...master))
2
2
 
3
3
  * TBD
4
4
 
5
+ ## v4.0.2, 2026-11-04 ([changes](https://github.com/infertux/bashcov/compare/v4.0.1...v4.0.2))
6
+
7
+ * [BUGFIX] Keep xtrace writes blocking so coverage records are not dropped (fixes #113)
8
+ * [BUGFIX] Skip malformed xtrace records instead of aborting coverage (fixes #112)
9
+
10
+ ## v4.0.1, 2026-10-26 ([changes](https://github.com/infertux/bashcov/compare/v4.0.0...v4.0.1))
11
+
12
+ * [BUGFIX] Fix coverage of multiline strings on Bash < 5.3 (fixes #108)
13
+ * [BUGFIX] Fix coverage of multiline strings opened after a line continuation
14
+ * [BUGFIX] Handle escaped characters in multiline double-quoted strings
15
+ * [BUGFIX] Stop heredoc matches at the first closing delimiter
16
+ * [BUGFIX] Only allow tab-indented heredoc delimiters after `<<-`
17
+ * [BUGFIX] Recognize heredoc delimiters quoted with double quotes or a backslash
18
+ * [MISC] Speed up lexer on files with multiline constructs
19
+ * [MISC] Make sure we use compatible cucumber and aruba releases
20
+ * [MISC] Add link to Buy Me a Coffee
21
+
5
22
  ## v4.0.0, 2026-08-25 ([changes](https://github.com/infertux/bashcov/compare/v3.3.0...v4.0.0))
6
23
 
7
24
  * [BUGFIX] Fix build for Nix
data/Gemfile.nix.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bashcov (4.0.0)
4
+ bashcov (4.0.2)
5
5
  simplecov (~> 1.1)
6
6
 
7
7
  GEM
@@ -17,7 +17,7 @@ GEM
17
17
  ast (2.4.3)
18
18
  base64 (0.3.0)
19
19
  benchmark (0.5.0)
20
- bigdecimal (4.1.2)
20
+ bigdecimal (4.1.3)
21
21
  builder (3.3.0)
22
22
  bundler-audit (0.9.3)
23
23
  bundler (>= 1.2.0)
@@ -51,26 +51,26 @@ GEM
51
51
  diff-lcs (1.6.2)
52
52
  erb (6.0.7)
53
53
  ffi (1.16.3)
54
- io-console (0.9.2)
54
+ io-console (0.9.4)
55
55
  irb (1.18.0)
56
56
  pp (>= 0.6.0)
57
57
  prism (>= 1.3.0)
58
58
  rdoc (>= 4.0.0)
59
59
  reline (>= 0.4.2)
60
- json (2.21.2)
60
+ json (3.1.0)
61
61
  language_server-protocol (3.17.0.6)
62
62
  lint_roller (1.1.0)
63
63
  logger (1.7.0)
64
64
  memoist3 (1.0.0)
65
65
  mini_mime (1.1.5)
66
66
  multi_test (1.1.0)
67
- parallel (2.1.0)
67
+ parallel (2.3.0)
68
68
  parser (3.3.12.0)
69
69
  ast (~> 2.4.1)
70
70
  racc
71
71
  pp (0.6.4)
72
72
  prettyprint
73
- prettyprint (0.2.0)
73
+ prettyprint (0.2.1)
74
74
  prism (1.9.0)
75
75
  racc (1.8.1)
76
76
  rainbow (3.1.1)
@@ -79,12 +79,12 @@ GEM
79
79
  logger
80
80
  prism (>= 1.6.0)
81
81
  tsort
82
- rdoc (8.0.0)
82
+ rdoc (8.1.0)
83
83
  erb
84
84
  prism (>= 1.6.0)
85
85
  rbs (>= 4.0.0)
86
86
  tsort
87
- regexp_parser (2.12.0)
87
+ regexp_parser (2.13.1)
88
88
  reline (0.7.0)
89
89
  io-console (~> 0.5)
90
90
  rspec (3.13.2)
@@ -100,14 +100,14 @@ GEM
100
100
  diff-lcs (>= 1.2.0, < 2.0)
101
101
  rspec-support (~> 3.13.0)
102
102
  rspec-support (3.13.7)
103
- rubocop (1.90.0)
103
+ rubocop (1.92.0)
104
104
  json (>= 2.3)
105
105
  language_server-protocol (~> 3.17.0.2)
106
106
  lint_roller (~> 1.1.0)
107
107
  parallel (>= 1.10)
108
108
  parser (>= 3.3.0.2)
109
109
  rainbow (>= 2.2.2, < 4.0)
110
- regexp_parser (>= 2.9.3, < 3.0)
110
+ regexp_parser (>= 2.11.3, < 3.0)
111
111
  rubocop-ast (>= 1.49.0, < 2.0)
112
112
  ruby-progressbar (~> 1.7)
113
113
  unicode-display_width (>= 2.4.0, < 4.0)
@@ -122,15 +122,15 @@ GEM
122
122
  regexp_parser (>= 2.0)
123
123
  rubocop (~> 1.86, >= 1.86.2)
124
124
  ruby-progressbar (1.13.0)
125
- simplecov (1.1.1)
125
+ simplecov (1.3.2)
126
126
  sys-uname (1.5.1)
127
127
  ffi (~> 1.1)
128
128
  memoist3 (~> 1.0.0)
129
129
  thor (1.5.0)
130
130
  tsort (0.2.0)
131
- unicode-display_width (3.2.0)
132
- unicode-emoji (~> 4.1)
133
- unicode-emoji (4.2.0)
131
+ unicode-display_width (3.3.0)
132
+ unicode-emoji (~> 4.3)
133
+ unicode-emoji (4.3.0)
134
134
  yard (0.9.45)
135
135
 
136
136
  PLATFORMS
@@ -147,11 +147,11 @@ PLATFORMS
147
147
  x86_64-linux-musl
148
148
 
149
149
  DEPENDENCIES
150
- aruba
150
+ aruba (~> 2.4.1)
151
151
  bashcov!
152
152
  benchmark
153
153
  bundler-audit
154
- cucumber
154
+ cucumber (~> 11.1.1)
155
155
  ffi (~> 1.16.0)
156
156
  irb
157
157
  rake
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Bashcov
2
2
 
3
+ [![Buy Me a Coffee](https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg)](https://buymeacoffee.com/infertux)
3
4
  [![Funding](https://img.shields.io/liberapay/patrons/infertux.svg?logo=liberapay)](https://liberapay.com/infertux/donate)
4
5
  [![Gem Version](https://img.shields.io/gem/v/bashcov.svg)](https://rubygems.org/gems/bashcov)
5
6
  [![Build Status](https://github.com/infertux/bashcov/actions/workflows/ci.yml/badge.svg)](https://github.com/infertux/bashcov/actions/workflows/ci.yml)
@@ -59,7 +60,7 @@ For development tips, see [the hacking guide](./HACKING.md).
59
60
 
60
61
  ## Sponsorship
61
62
 
62
- Bashcov was [created in 2012](https://github.com/infertux/bashcov/commit/f65e65e5aa3377beb334beee9924136a34a913e8) and it needs your help. I have been maintaining the project for over a decade and keeping it working with new releases of Bash and Ruby takes time. If you use Bashcov professionally, please consider supporting it on [Liberapay](https://liberapay.com/infertux) through your employer or directly. Thank you for supporting *Free and Open-Source Software*.
63
+ Bashcov was [created in 2012](https://github.com/infertux/bashcov/commit/f65e65e5aa3377beb334beee9924136a34a913e8) and it needs your help. I have been maintaining the project for over a decade and keeping it working with new releases of Bash and Ruby takes time. If you use Bashcov professionally, please consider supporting it on [Buy Me a Coffee](https://buymeacoffee.com/infertux) or [Liberapay](https://liberapay.com/infertux) through your employer or directly. Thank you for supporting *Free and Open-Source Software*.
63
64
 
64
65
  ## License
65
66
 
data/flake.nix CHANGED
@@ -206,7 +206,7 @@
206
206
  pname = "bashcov";
207
207
  gemdir = self;
208
208
  lockfile = ./Gemfile.nix.lock;
209
- version = "4.0.0";
209
+ version = "4.0.2";
210
210
  postBuild = ''
211
211
  for gem in $out/${ruby.gemPath}/bundler/gems/*; do
212
212
  ln -sfrT "$gem" $out/${ruby.gemPath}/gems/"''${gem##*/}"
data/gemset.nix CHANGED
@@ -38,7 +38,7 @@
38
38
  path = ./.;
39
39
  type = "path";
40
40
  };
41
- version = "4.0.0";
41
+ version = "4.0.2";
42
42
  };
43
43
  benchmark = {
44
44
  groups = ["development"];
@@ -55,10 +55,10 @@
55
55
  platforms = [];
56
56
  source = {
57
57
  remotes = ["https://rubygems.org"];
58
- sha256 = "1g9zi8c4i7g8zz0c3hxrw6mblrjvgn7akys60clb9si7c1k1gljk";
58
+ sha256 = "0rng45p4vc3f6nac44cnylwch7rj4zsff3icdz6c7garwpjy3sv1";
59
59
  type = "gem";
60
60
  };
61
- version = "4.1.2";
61
+ version = "4.1.3";
62
62
  };
63
63
  builder = {
64
64
  groups = ["default" "development"];
@@ -211,10 +211,10 @@
211
211
  platforms = [];
212
212
  source = {
213
213
  remotes = ["https://rubygems.org"];
214
- sha256 = "026v93kja19bfslnwi9xfq2dj4r89kkwdprim4whjg6h3n4lz9zg";
214
+ sha256 = "1cgvhm0d4awr8j42b8v1nl1jfgx45ihv8mylargfi3lj697k00x4";
215
215
  type = "gem";
216
216
  };
217
- version = "0.9.2";
217
+ version = "0.9.4";
218
218
  };
219
219
  irb = {
220
220
  dependencies = ["pp" "prism" "rdoc" "reline"];
@@ -232,10 +232,10 @@
232
232
  platforms = [];
233
233
  source = {
234
234
  remotes = ["https://rubygems.org"];
235
- sha256 = "0shwgjqbj856mb6m9kgkpy08nhym2gdvc2yaprlimfmky9y3n78z";
235
+ sha256 = "1n1v9rhb19cnf1a89s6yvvhw394jy8qnqx9sg8bn12g5zpwcrrrw";
236
236
  type = "gem";
237
237
  };
238
- version = "2.21.2";
238
+ version = "3.1.0";
239
239
  };
240
240
  language_server-protocol = {
241
241
  groups = ["default" "development"];
@@ -302,10 +302,10 @@
302
302
  platforms = [];
303
303
  source = {
304
304
  remotes = ["https://rubygems.org"];
305
- sha256 = "0mlkn1vhh9lr7vljibpgspwsswk7mzm8nw6bbr616c9fbj35hlmk";
305
+ sha256 = "0vvs2sar7wnw70j1jc1kv1k23m5grc0ci3bbrcf6mkh18683wnpp";
306
306
  type = "gem";
307
307
  };
308
- version = "2.1.0";
308
+ version = "2.3.0";
309
309
  };
310
310
  parser = {
311
311
  dependencies = ["ast" "racc"];
@@ -334,10 +334,10 @@
334
334
  platforms = [];
335
335
  source = {
336
336
  remotes = ["https://rubygems.org"];
337
- sha256 = "14zicq3plqi217w6xahv7b8f7aj5kpxv1j1w98344ix9h5ay3j9b";
337
+ sha256 = "1m5pqkp8rdk0bkxxz3bp3vn8pn3bd9a97dkl8wfh1kyplrzdamfm";
338
338
  type = "gem";
339
339
  };
340
- version = "0.2.0";
340
+ version = "0.2.1";
341
341
  };
342
342
  prism = {
343
343
  groups = ["default" "development"];
@@ -396,20 +396,20 @@
396
396
  platforms = [];
397
397
  source = {
398
398
  remotes = ["https://rubygems.org"];
399
- sha256 = "0sf4909q2mr9z0rpygv94z12b0yamg07gz8cba2mi5k3m448rgq3";
399
+ sha256 = "02pn63i2r5z44hcfhxbaag9rvqsyznsliivnn7f3dzaw1a43gz5y";
400
400
  type = "gem";
401
401
  };
402
- version = "8.0.0";
402
+ version = "8.1.0";
403
403
  };
404
404
  regexp_parser = {
405
405
  groups = ["default" "development"];
406
406
  platforms = [];
407
407
  source = {
408
408
  remotes = ["https://rubygems.org"];
409
- sha256 = "1fwfw26a32rps78920nn29shqg2zmqv72i89j1fap41isshida9m";
409
+ sha256 = "0jxl6c5cagjz5i5n9jplhdy2izad6wapmwgfhqggb22nqfvvdvas";
410
410
  type = "gem";
411
411
  };
412
- version = "2.12.0";
412
+ version = "2.13.1";
413
413
  };
414
414
  reline = {
415
415
  dependencies = ["io-console"];
@@ -482,10 +482,10 @@
482
482
  platforms = [];
483
483
  source = {
484
484
  remotes = ["https://rubygems.org"];
485
- sha256 = "0hl2pxa6nydnh3jmgrjkdgmsj19r5yblgialym74w5f5nmjw1d4y";
485
+ sha256 = "1q2nih91fjbv6j1qqxfhlc480zyhz94rniyh469vsw1fzd0q1xia";
486
486
  type = "gem";
487
487
  };
488
- version = "1.90.0";
488
+ version = "1.92.0";
489
489
  };
490
490
  rubocop-ast = {
491
491
  dependencies = ["parser" "prism"];
@@ -535,10 +535,10 @@
535
535
  platforms = [];
536
536
  source = {
537
537
  remotes = ["https://rubygems.org"];
538
- sha256 = "0y0ya82s3zi2wmkadnmb3l9r13mbsvd1g63n9mlp8bhb7zqmx0i5";
538
+ sha256 = "118fdpg7h1ny1fz6kmiyz6gccvwmb23h6bhsi1hdpf7ib4byq8ia";
539
539
  type = "gem";
540
540
  };
541
- version = "1.1.1";
541
+ version = "1.3.2";
542
542
  };
543
543
  sys-uname = {
544
544
  dependencies = ["ffi" "memoist3"];
@@ -577,20 +577,20 @@
577
577
  platforms = [];
578
578
  source = {
579
579
  remotes = ["https://rubygems.org"];
580
- sha256 = "0hiwhnqpq271xqari6mg996fgjps42sffm9cpk6ljn8sd2srdp8c";
580
+ sha256 = "04xnl0zwpjjvxipd6rdnxkm11pfhbhhi3179yzv51nqi9dmacyjb";
581
581
  type = "gem";
582
582
  };
583
- version = "3.2.0";
583
+ version = "3.3.0";
584
584
  };
585
585
  unicode-emoji = {
586
586
  groups = ["default" "development"];
587
587
  platforms = [];
588
588
  source = {
589
589
  remotes = ["https://rubygems.org"];
590
- sha256 = "03zqn207zypycbz5m9mn7ym763wgpk7hcqbkpx02wrbm1wank7ji";
590
+ sha256 = "1kp89lja8ii7l6f0bq5vdxq1p3kxr3a2qmmhdc38qdwh6akjzh0i";
591
591
  type = "gem";
592
592
  };
593
- version = "4.2.0";
593
+ version = "4.3.0";
594
594
  };
595
595
  yard = {
596
596
  groups = ["development"];
@@ -29,17 +29,19 @@ module Bashcov
29
29
  # @param [Regexp] start_match a +Regexp+ that, when matched against the
30
30
  # input stream, signifies the beginning of the next series of fields to
31
31
  # yield
32
- # @yieldparam [String] field each field extracted from the stream. If
33
- # +start_match+ is matched with fewer than +field_count+ fields yielded
34
- # since the last match, yields empty strings until +field_count+ is
35
- # reached.
32
+ # @yieldparam [String] field each field extracted from the stream. Exactly
33
+ # +field_count+ fields are yielded for each +start_match+ match: if fewer
34
+ # fields follow the match, yields empty strings until +field_count+ is
35
+ # reached; if more follow (e.g. a traced command that contains the
36
+ # delimiter), the extra fields are dropped so the next series of fields
37
+ # stays aligned.
36
38
  def each(delimiter, field_count, start_match, &block)
37
39
  return enum_for(__method__, delimiter, field_count, start_match) unless block_given?
38
40
 
39
41
  chunked = each_field(delimiter).chunk(&chunk_matches(start_match))
40
42
 
41
43
  yield_fields = lambda do |(_, chunk)|
42
- chunk.each(&block)
44
+ chunk.first(field_count).each(&block)
43
45
  (field_count - chunk.size).times { yield "" }
44
46
  end
45
47
 
data/lib/bashcov/lexer.rb CHANGED
@@ -15,6 +15,34 @@ module Bashcov
15
15
  # Lines containing only one of these keywords are irrelevant for coverage
16
16
  IGNORE_IS = %w[esac if then else elif fi while do done { } ;; ( )].freeze
17
17
 
18
+ # Constructs spanning multiple lines, as +[regexp, forward]+ pairs tried in
19
+ # order. Regexps are anchored with +\G+ to match from a given line offset.
20
+ MULTILINE_PATTERNS = [
21
+ # multi-line arrays
22
+ [/\G[^\n]*\b=\([^()]*\)/, false],
23
+
24
+ # heredoc, possibly with a quoted delimiter (e.g. 'EOF', "EOF" or \EOF) -
25
+ # ends at the first line holding only the delimiter, which `<<-` also
26
+ # allows to be indented with tabs
27
+ [/\G[^\n]+<<[ \t]*\\?['"]?(\w+)['"]?[^\n]*\n(?:[^\n]*\n)*?\1$/, true],
28
+ [/\G[^\n]+<<-[ \t]*\\?['"]?(\w+)['"]?[^\n]*\n(?:[^\n]*\n)*?\t*\1$/, true],
29
+
30
+ # multiline string concatenated with backslashes
31
+ [/\G[^\n]+\\$(\s*['"][^'"]*['"]\s*\\$){1,}\s*['"][^'"]*['"]\s*$/, true],
32
+
33
+ # multiline string concatenated with newlines, possibly opened after a
34
+ # line continuation (but never after an unescaped newline) - this must
35
+ # come before simple line continuations which would claim the opening
36
+ # line otherwise - unlike single quotes, double quotes may contain escaped
37
+ # characters such as \"
38
+ *[false, true].product([["'", /[^']*/], ['"', /(?:[^"\\]|\\.)*/m]]).map do |forward, (char, body)|
39
+ [/\G[^\n]+(?:[ \t=]|\\\n[ \t]*)+#{char}#{body}#{char}/m, forward]
40
+ end,
41
+
42
+ # simple line continuations with backslashes
43
+ [/\G([^\n&|;]*[^\\&|;](\\\\)*\\\n)+[^\n&|;]*[^\n\\&|;](\\\\)*$/, true],
44
+ ].freeze
45
+
18
46
  # @param [String] filename File to analyze
19
47
  # @param [Hash] coverage Coverage with executed lines marked
20
48
  # @raise [ArgumentError] if the given +filename+ is invalid.
@@ -28,60 +56,46 @@ module Bashcov
28
56
  # Process and complete initial coverage.
29
57
  # @return [void]
30
58
  def complete_coverage
31
- lines = File.read(@filename).encode("utf-8", invalid: :replace).lines
32
-
33
- lines.each_with_index do |line, lineno|
34
- # multi-line arrays
35
- mark_multiline(
36
- lines, lineno,
37
- /\A[^\n]*\b=\([^()]*\)/,
38
- forward: false,
39
- )
40
-
41
- # heredoc
42
- mark_multiline(
43
- lines, lineno,
44
- /\A[^\n]+<<-?\s*'?(\w+)'?.*$.*\1/m,
45
- )
46
-
47
- # multiline string concatenated with backslashes
48
- mark_multiline(
49
- lines, lineno,
50
- /\A[^\n]+\\$(\s*['"][^'"]*['"]\s*\\$){1,}\s*['"][^'"]*['"]\s*$/,
51
- )
52
-
53
- # simple line continuations with backslashes
54
- mark_multiline(
55
- lines, lineno,
56
- /\A([^\n&|;]*[^\\&|;](\\\\)*\\\n)+[^\n&|;]*[^\n\\&|;](\\\\)*$/,
57
- )
58
-
59
- # multiline string concatenated with newlines
60
- [false, true].each do |direction|
61
- %w[' "].each do |char|
62
- mark_multiline(
63
- lines, lineno,
64
- /\A[^\n]+[\s=]+#{char}[^#{char}]*#{char}/m,
65
- forward: direction,
66
- )
67
- end
68
- end
69
-
70
- mark_line(line, lineno)
59
+ content = File.read(@filename).encode("utf-8", invalid: :replace)
60
+ lines = content.lines
61
+ offsets = lines.each_with_object([0]) { |line, memo| memo << (memo.last + line.size) }
62
+
63
+ lineno = 0
64
+ while lineno < lines.size
65
+ # skip ahead past a multiline construct once its lines are marked, but
66
+ # recheck its last line as it may open another one, e.g. `a'; b='`
67
+ last = mark_multilines(content, offsets[lineno], lineno) || lineno
68
+ lineno.upto(last) { |sub_lineno| mark_line(lines[sub_lineno], sub_lineno) }
69
+ lineno = (last > lineno ? last : last + 1)
71
70
  end
72
71
  end
73
72
 
74
73
  private
75
74
 
76
- def mark_multiline(lines, lineno, regexp, forward: true)
77
- # skip if we are already inside marked multiline string
78
- return unless @coverage[lineno + 1].nil?
75
+ # @return [Integer, nil] last line of the multiline construct starting at
76
+ # +lineno+, if any
77
+ def mark_multilines(content, offset, lineno)
78
+ MULTILINE_PATTERNS.each do |regexp, forward|
79
+ last = mark_multiline(content, offset, lineno, regexp, forward: forward)
80
+ return last if last
81
+ end
82
+
83
+ nil
84
+ end
79
85
 
80
- seek_forward = lines[lineno..].join
81
- return unless (multiline_match = seek_forward.match(regexp))
86
+ def mark_multiline(content, offset, lineno, regexp, forward: true)
87
+ # +regexp+ is anchored with \G so it only matches from +offset+
88
+ return unless (multiline_match = content.match(regexp, offset))
82
89
 
83
90
  length = multiline_match.to_s.count($/)
84
91
  first, last = lineno + 1, lineno + length
92
+
93
+ # skip if we are already inside marked multiline string - only inner
94
+ # lines are checked since Bash may report the hit on the last line
95
+ # (e.g. `readonly` before Bash 5.3), which is also the next line when
96
+ # the string spans only two lines
97
+ return unless first.upto(last - 1).all? { |i| @coverage[i].nil? }
98
+
85
99
  range = (forward ? first.upto(last) : (last - 1).downto(first - 1))
86
100
  reference_lineno = (forward ? first - 1 : last)
87
101
 
@@ -92,6 +106,8 @@ module Bashcov
92
106
  # mark related lines with the same coverage as the reference line
93
107
  @coverage[sub_lineno] ||= @coverage[reference_lineno]
94
108
  end
109
+
110
+ last if length.positive?
95
111
  end
96
112
 
97
113
  def mark_line(line, lineno)
@@ -57,25 +57,16 @@ module Bashcov
57
57
  with_xtrace_flag do
58
58
  command_pid = Process.spawn env, *@command, options # spawn the command
59
59
 
60
- begin
61
- # start processing the xtrace output
62
- xtrace_thread = Thread.new { @xtrace.read }
60
+ # start processing the xtrace output
61
+ xtrace_thread = Thread.new { @xtrace.read }
63
62
 
64
- Process.wait command_pid
63
+ Process.wait command_pid
65
64
 
66
- @xtrace.close
65
+ @xtrace.close
67
66
 
68
- @coverage = xtrace_thread.value # wait for the thread to return
69
- rescue XtraceError => e
70
- write_warning <<-WARNING
71
- encountered an error parsing Bash's output (error was:
72
- #{e.message}). This can occur if your script or its path contains
73
- the sequence #{Xtrace.delimiter.inspect}, or if your script unsets
74
- LINENO. Aborting early; coverage report will be incomplete.
75
- WARNING
67
+ @coverage = xtrace_thread.value # wait for the thread to return
76
68
 
77
- @coverage = e.files
78
- end
69
+ warn_skipped_records
79
70
  end
80
71
 
81
72
  temp_file&.unlink
@@ -97,6 +88,19 @@ module Bashcov
97
88
 
98
89
  private
99
90
 
91
+ def warn_skipped_records
92
+ return if @xtrace.skipped_count.zero?
93
+
94
+ write_warning <<-WARNING
95
+ skipped #{@xtrace.skipped_count} malformed line(s) in Bash's output
96
+ (first error was: #{@xtrace.first_skip_reason}). This can occur if your
97
+ script prints its own PS4 (e.g. with `declare -p` or `set`), if your
98
+ script or its path contains the sequence #{Xtrace.delimiter.inspect},
99
+ or if your script unsets LINENO. Coverage of the affected lines may be
100
+ incomplete.
101
+ WARNING
102
+ end
103
+
100
104
  def write_warning(message)
101
105
  warn [
102
106
  Bashcov.program_name,
@@ -3,5 +3,5 @@
3
3
  # :nodoc:
4
4
  module Bashcov
5
5
  # Current Bashcov version
6
- VERSION = "4.0.0"
6
+ VERSION = "4.0.2"
7
7
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "io/nonblock"
3
4
  require "pathname"
4
5
  require "securerandom"
5
6
 
@@ -57,6 +58,8 @@ module Bashcov
57
58
  @field_stream = field_stream
58
59
 
59
60
  @read, @write = IO.pipe
61
+ # Keep Bash's xtrace writes blocking so records are not dropped when the pipe fills.
62
+ @write.nonblock = false
60
63
 
61
64
  # Tracks coverage for each file under test
62
65
  @files ||= {}
@@ -64,22 +67,35 @@ module Bashcov
64
67
  # Stacks for updating working directory changes
65
68
  @pwd_stack ||= []
66
69
  @oldpwd_stack ||= []
70
+
71
+ @skipped_count = 0
67
72
  end
68
73
 
74
+ # [Integer] Number of malformed +PS4+ records skipped by {#read}
75
+ attr_reader :skipped_count
76
+
77
+ # [String, nil] Why the first malformed +PS4+ record was skipped
78
+ attr_reader :first_skip_reason
79
+
69
80
  # @return [Fixnum] File descriptor of the write end of the pipe
70
81
  def file_descriptor
71
82
  @write.fileno
72
83
  end
73
84
 
74
- # Closes the pipe for writing.
85
+ # Closes the parent's write end after the command exits, also ending any
86
+ # error-recovery drain without waiting for inherited background writers.
75
87
  # @return [void]
76
88
  def close
77
89
  @write.close
90
+ @read.close if @draining
78
91
  end
79
92
 
80
93
  # Read fields extracted from Bash's debugging output
81
94
  # @return [Hash<Pathname, Array<Integer, nil>>] A hash mapping Bash scripts
82
95
  # to Simplecov-style coverage stats
96
+ # @raise [StandardError] any error while parsing, raised only after
97
+ # draining until the command exits or the pipe reaches EOF. Malformed
98
+ # records are skipped (see {#skipped_count}) rather than raised.
83
99
  def read
84
100
  @field_stream.read = @read
85
101
 
@@ -96,36 +112,44 @@ module Bashcov
96
112
  parse_hit!(*hit)
97
113
  end
98
114
 
99
- @read.close unless @read.closed?
100
-
101
115
  @files
116
+ rescue StandardError
117
+ # Keep draining so Bash's blocking xtrace writes cannot fill the pipe and
118
+ # hang the run, or hit a closed pipe and die of SIGPIPE. #close ends the
119
+ # drain once the command exits, so background writers are not waited on.
120
+ @draining = true
121
+ begin
122
+ nil while !@write.closed? && @read.read(65_536)
123
+ rescue IOError
124
+ raise unless @write.closed? # Only ignore closure after the command exits.
125
+ end
126
+ raise
127
+ ensure
128
+ @read.close unless @read.closed?
102
129
  end
103
130
 
104
131
  private
105
132
 
106
133
  # Parses the expanded {ps4} fields and updates the coverage-tracking
107
- # {@files} hash
134
+ # {@files} hash. A record whose +LINENO+ is not composed solely of digits
135
+ # is skipped: it is usually text that merely looks like the start of a
136
+ # {ps4}, such as the output of +declare -p+ or +set+ being traced, and
137
+ # {FieldStream#each} resynchronizes on the next start of a {ps4}.
108
138
  # @overload parse_hit!(lineno, bash_source, pwd, oldpwd)
109
139
  # @param [String] lineno expanded +LINENO+
110
140
  # @param [Pathname] bash_source expanded +BASH_SOURCE+
111
141
  # @param [Pathname] pwd expanded +PWD+
112
142
  # @param [Pathname] oldpwd expanded +OLDPWD+
113
143
  # @return [void]
114
- # @raise [XtraceError] when +lineno+ is not composed solely of digits,
115
- # indicating that something has gone wrong with parsing the +PS4+ fields
116
144
  def parse_hit!(lineno, *paths)
117
- # if +LINENO+ isn't a series of digits, add +@files+ to the exception in
118
- # order to propagate the existing coverage data back to the
119
- # {Bashcov::Runner} instance
120
145
  if /\A\d+\z/.match?(lineno)
121
146
  lineno = lineno.to_i
122
147
  elsif lineno == "${LINENO-}"
123
148
  # the variable doesn't expand on line misses so we can safely ignore it
124
149
  return
125
150
  else
126
- raise XtraceError.new(
127
- "expected integer for LINENO, got #{lineno.inspect}", @files,
128
- )
151
+ skip_record!("expected integer for LINENO, got #{lineno.inspect}")
152
+ return
129
153
  end
130
154
 
131
155
  bash_source, pwd, oldpwd = paths.map { |p| Pathname.new(p) }
@@ -143,6 +167,14 @@ module Bashcov
143
167
  @files[script][index] += 1
144
168
  end
145
169
 
170
+ # Records that a malformed record was skipped
171
+ # @param [String] reason why the record was skipped
172
+ # @return [void]
173
+ def skip_record!(reason)
174
+ @skipped_count += 1
175
+ @first_skip_reason = reason if @skipped_count == 1
176
+ end
177
+
146
178
  # Scans entries in the +PWD+ stack, checking whether +entry/$BASH_SOURCE+
147
179
  # refers to an existing file. Scans the stack in reverse on the assumption
148
180
  # that more-recent entries are more plausible candidates for base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bashcov
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric Félizard
@@ -27,16 +27,16 @@ dependencies:
27
27
  name: aruba
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
- - - ">="
30
+ - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '0'
32
+ version: 2.4.1
33
33
  type: :development
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: 2.4.1
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: benchmark
42
42
  requirement: !ruby/object:Gem::Requirement
@@ -69,16 +69,16 @@ dependencies:
69
69
  name: cucumber
70
70
  requirement: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - ">="
72
+ - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '0'
74
+ version: 11.1.1
75
75
  type: :development
76
76
  prerelease: false
77
77
  version_requirements: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - ">="
79
+ - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '0'
81
+ version: 11.1.1
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: ffi
84
84
  requirement: !ruby/object:Gem::Requirement
@@ -268,7 +268,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
268
  - !ruby/object:Gem::Version
269
269
  version: '0'
270
270
  requirements: []
271
- rubygems_version: 4.0.10
271
+ rubygems_version: 4.0.17
272
272
  specification_version: 4
273
273
  summary: Code coverage tool for Bash
274
274
  test_files: []