test_bench-bootstrap 2.1.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/test_bench/bootstrap.rb +186 -74
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce89e18ca0c6494f546f09c4b8786d7aca2e2bdc435c74b9a0a6cb2786f86481
4
- data.tar.gz: bba0af0bcee52f86fbb2e58aaa589d8ba7f847ce8cf0e86e08c0b7d086fc381b
3
+ metadata.gz: b4e2e358c29830ece30c5a9516c4cf96fe2d6748cb1a3c88e9d3d05727c66f9a
4
+ data.tar.gz: d07c4e27d9c6440427ff716ff8fe6cf405940287417c5f0f69e90560d6c140da
5
5
  SHA512:
6
- metadata.gz: 7631713647f4c2199926af78af8e509fbb96ba247fcee7c20e6b12cacf48307eb26244acc5b2e9522e40c093e8f5d6d1bba52acec8c47e92db15a50facd32b3d
7
- data.tar.gz: 88944e3be2c9fdd414fd22a5e8ce18e70fdbe840b2482919a68225c8b43d757d748617bb8086628a890a4b60d35e25a0cf56ccf23d5246143d35c8c0ec6dd261
6
+ metadata.gz: 787ba756e146d4b72a0a7dd265df1baa3c4ddab27bf78e3e0d271dd1b333a7006e1e486b0369c9727ffed7bf15adb9e150fe8a13b7f743b2833a0b78ee9cf881
7
+ data.tar.gz: faf41a1355cf5cc3a20cc46aa25cd1e1b62b4bb69da66ef25bc20b2efdacabdfe6d0f736908120f6ecab24f7b011d4ecee36bfa94ce78d0271da878260023681
@@ -4,15 +4,94 @@ module TestBench
4
4
  Object.include(Fixture)
5
5
  end
6
6
 
7
+ if RUBY_ENGINE != 'mruby'
8
+ class Abort < SystemExit
9
+ def self.build
10
+ new(1)
11
+ end
12
+ end
13
+ end
14
+
15
+ class Abort
16
+ def self.call
17
+ Output.raw_write("#{Bootstrap} is aborting\n")
18
+ instance = build
19
+ raise instance
20
+ end
21
+ end
22
+
23
+ module Backtrace
24
+ if RUBY_ENGINE != 'mruby'
25
+ def self.frame(frame_index)
26
+ frame_index += 1
27
+
28
+ caller[frame_index]
29
+ end
30
+ end
31
+ end
32
+
33
+ class AssertionFailure < RuntimeError
34
+ def self.build(frame_index=nil)
35
+ frame_index ||= 0
36
+
37
+ frame = Backtrace.frame(frame_index)
38
+
39
+ instance = new
40
+ instance.set_backtrace([frame])
41
+ instance
42
+ end
43
+
44
+ def message
45
+ "Assertion failed"
46
+ end
47
+ end
48
+
49
+ module Path
50
+ if RUBY_ENGINE != 'mruby'
51
+ def self.match?(pattern, string)
52
+ ::File.fnmatch?(pattern, string)
53
+ end
54
+
55
+ def self.search(path, include_pattern=nil, exclude_pattern=nil)
56
+ files = []
57
+
58
+ if ::File.directory?(path)
59
+ search_directory(path, files, include_pattern, exclude_pattern)
60
+ elsif ::File.exist?(path)
61
+ files << path
62
+ else
63
+ raise LoadError, "no such file or directory -- #{path}"
64
+ end
65
+
66
+ files
67
+ end
68
+
69
+ def self.search_directory(dir, files, include_pattern=nil, exclude_pattern=nil)
70
+ include_pattern ||= '*.rb'
71
+
72
+ ::Dir[::File.join(dir, '**', '*')].each do |path|
73
+ next if ::File.directory?(path)
74
+
75
+ if match?(include_pattern, path)
76
+ if exclude_pattern.nil? || !match?(exclude_pattern, path)
77
+ files << path
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
7
85
  module Fixture
8
86
  def assert(value)
9
87
  unless value
10
- raise AssertionFailure.build(caller.first)
88
+ raise AssertionFailure.build(1)
11
89
  end
12
90
  end
13
91
 
14
92
  def assert_raises(error_class=nil, &block)
15
93
  begin
94
+ Output.raw_write("assert_raises\n")
16
95
  block.()
17
96
 
18
97
  rescue (error_class || StandardError) => error
@@ -25,12 +104,12 @@ module TestBench
25
104
  end
26
105
  end
27
106
 
28
- raise AssertionFailure.build(caller.first)
107
+ raise AssertionFailure.build(1)
29
108
  end
30
109
 
31
110
  def refute(value)
32
111
  if value
33
- raise AssertionFailure.build(caller.first)
112
+ raise AssertionFailure.build(1)
34
113
  end
35
114
  end
36
115
 
@@ -42,7 +121,7 @@ module TestBench
42
121
  raise error
43
122
  end
44
123
 
45
- raise AssertionFailure.build(caller.first)
124
+ raise AssertionFailure.build(1)
46
125
  end
47
126
 
48
127
  def context(prose=nil, &block)
@@ -62,9 +141,9 @@ module TestBench
62
141
  block.()
63
142
 
64
143
  rescue => error
65
- Fixture.print_error(error)
144
+ Output.error(error)
66
145
 
67
- raise Failure.build
146
+ Abort.()
68
147
  end
69
148
  end
70
149
 
@@ -85,10 +164,10 @@ module TestBench
85
164
 
86
165
  rescue => error
87
166
  Output.indent(prose, sgr_codes: [0x1, 0x31]) do
88
- Fixture.print_error(error)
167
+ Output.error(error)
89
168
  end
90
169
 
91
- raise Failure.build
170
+ Abort.()
92
171
  end
93
172
  end
94
173
 
@@ -100,6 +179,10 @@ module TestBench
100
179
  Output.write(text)
101
180
  end
102
181
 
182
+ def detail(text)
183
+ comment(text)
184
+ end
185
+
103
186
  def fixture(cls, *args, **kwargs, &block)
104
187
  fixture = TestBench::Fixture.(cls, *args, **kwargs, &block)
105
188
 
@@ -107,66 +190,79 @@ module TestBench
107
190
 
108
191
  assert(passed)
109
192
  end
193
+ end
194
+
195
+ module Output
196
+ extend self
197
+
198
+ def write(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil)
199
+ indent(text, device: device, sgr_code: sgr_code, sgr_codes: sgr_codes, tab_indent: tab_indent)
200
+ end
110
201
 
111
- def self.print_error(error)
112
- omit_backtrace_pattern = ENV['TEST_BENCH_OMIT_BACKTRACE_PATTERN']
113
- omit_backtrace_pattern ||= %r{test_bench/bootstrap\.rb}
202
+ def error(error)
203
+ omit_backtrace_pattern = Defaults.omit_backtrace_pattern
114
204
 
115
205
  omitting = false
116
206
 
117
- Output.write("\e[1mTraceback\e[22m (most recent call last):", sgr_code: 0x31)
207
+ write("\e[1mTraceback\e[22m (most recent call last):", sgr_code: 0x31)
118
208
 
119
209
  rjust_length = error.backtrace.length.to_s.length
120
210
 
121
- error.backtrace[1..-1].reverse_each.with_index do |line, index|
122
- line = line.dup
211
+ reverse_backtrace = error.backtrace[1..-1].reverse
123
212
 
124
- line.chomp!
213
+ reverse_backtrace.each_with_index do |frame, index|
214
+ frame = frame.dup
215
+ frame.chomp!
125
216
 
126
- if omit_backtrace_pattern.match?(line)
217
+ previous_frame = frame
218
+
219
+ file, _ = frame.split(':', 2)
220
+
221
+ line = ' ' * rjust_length
222
+
223
+ index_text = index.to_s
224
+ index_range = (-index_text.length..-1)
225
+
226
+ if Path.match?(omit_backtrace_pattern, file)
127
227
  if omitting
128
228
  next
129
229
  else
130
230
  omitting = true
131
231
 
132
- header = index.to_s.gsub(/./, '?').rjust(rjust_length, ' ')
232
+ line[index_range] = '?' * index_text.length
233
+ line += ": *omitted*"
133
234
 
134
- Output.write("#{header}: *omitted*", sgr_codes: [0x2, 0x3, 0x31], tab_indent: true)
235
+ write(line, sgr_codes: [0x2, 0x3, 0x31], tab_indent: true)
135
236
  end
136
237
  else
137
238
  omitting = false
138
239
 
139
- header = index.to_s.rjust(rjust_length, ' ')
240
+ line[index_range] = index_text
241
+ line += ": #{frame}"
140
242
 
141
- Output.write("#{header}: #{line}", sgr_code: 0x31, tab_indent: true)
243
+ write(line, sgr_code: 0x31, tab_indent: true)
142
244
  end
143
245
  end
144
246
 
145
247
  if error.message.empty?
146
248
  if error.instance_of?(RuntimeError)
147
- Output.write("#{error.backtrace[0]}: \e[1;4munhandled exception\e[24;22m", sgr_code: 0x31)
249
+ write("#{error.backtrace[0]}: \e[1;4munhandled exception\e[24;22m", sgr_code: 0x31)
148
250
  return
149
251
  end
150
252
 
151
253
  error.message = error.class
152
254
  end
153
255
 
154
- Output.write("#{error.backtrace[0]} \e[1m#{error} (\e[4m#{error.class}\e[24m)\e[22m", sgr_code: 0x31)
256
+ write("#{error.backtrace[0]}: \e[1m#{error} (\e[4m#{error.class}\e[24m)\e[22m", sgr_code: 0x31)
155
257
  end
156
- end
157
258
 
158
- module Output
159
- extend self
160
-
161
- def write(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil)
162
- indent(text, device: device, sgr_code: sgr_code, sgr_codes: sgr_codes, tab_indent: tab_indent)
259
+ def newline
260
+ write('')
163
261
  end
164
262
 
165
263
  def indent(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil, &block)
166
- device ||= $stdout
167
-
168
264
  unless text.nil?
169
- sgr_codes = Array(sgr_codes)
265
+ sgr_codes ||= []
170
266
  unless sgr_code.nil?
171
267
  sgr_codes << sgr_code
172
268
  end
@@ -179,9 +275,9 @@ module TestBench
179
275
  text = "\e[#{sgr_codes.join(';')}m#{text}\e[0m"
180
276
  end
181
277
 
182
- text = "#{"\t" if tab_indent}#{' ' * indentation}#{text}"
278
+ text = "#{"\t" if tab_indent}#{' ' * indentation}#{text}\n"
183
279
 
184
- device.puts(text)
280
+ raw_write(text, device)
185
281
  end
186
282
 
187
283
  return if block.nil?
@@ -195,67 +291,83 @@ module TestBench
195
291
  end
196
292
  end
197
293
 
198
- def indentation
199
- @indentation ||= 0
200
- end
201
- attr_writer :indentation
202
- end
294
+ def raw_write(text, device=nil)
295
+ device ||= self.device
203
296
 
204
- class AssertionFailure < RuntimeError
205
- def self.build(caller_location=nil)
206
- caller_location ||= caller(0)
207
-
208
- instance = new
209
- instance.set_backtrace([caller_location])
210
- instance
297
+ device.write(text)
211
298
  end
212
299
 
213
- def message
214
- "Assertion failed"
300
+ def indentation
301
+ @indentation ||= 0
215
302
  end
216
- end
303
+ attr_writer :indentation
217
304
 
218
- class Failure < SystemExit
219
- def self.build
220
- new(1, "TestBench::Bootstrap is aborting")
305
+ def device
306
+ @device ||= Defaults.output_device
221
307
  end
222
308
  end
223
309
 
224
310
  module Run
225
- def self.call(argv=nil, exclude_file_pattern: nil)
226
- argv ||= ::ARGV
227
-
228
- exclude_file_pattern = ENV['TEST_BENCH_EXCLUDE_FILE_PATTERN']
229
- exclude_file_pattern ||= %r{automated_init\.rb}
311
+ def self.call(paths=nil, exclude_pattern: nil)
312
+ paths ||= []
313
+ exclude_pattern ||= Defaults.exclude_file_pattern
230
314
 
231
- if argv.empty?
232
- tests_dir = ENV['TEST_BENCH_TESTS_DIR'] || 'test/automated'
233
-
234
- file_patterns = [File.join(tests_dir, '**', '*.rb')]
235
- else
236
- file_patterns = argv
315
+ if paths.is_a?(String)
316
+ paths = [paths]
237
317
  end
238
318
 
239
- file_patterns.each do |file_pattern|
240
- if File.directory?(file_pattern)
241
- file_pattern = File.join(file_pattern, '**/*.rb')
242
- end
243
-
244
- files = Dir.glob(file_pattern).reject do |file|
245
- File.basename(file).match?(exclude_file_pattern)
246
- end
319
+ if paths.empty?
320
+ paths << Defaults.tests_dir
321
+ end
247
322
 
248
- files.sort.each do |file|
249
- puts "Running #{file}"
323
+ paths.each do |path|
324
+ Path.search(path, '*.rb', exclude_pattern).each do |file|
325
+ Output.write "Running #{file}"
250
326
 
251
327
  begin
252
- load file
328
+ load(file)
253
329
  ensure
254
- puts
330
+ Output.newline
255
331
  end
256
332
  end
257
333
  end
258
334
  end
259
335
  end
336
+
337
+ module Defaults
338
+ def self.get(env_var, default)
339
+ if env.key?(env_var)
340
+ env[env_var]
341
+ else
342
+ default
343
+ end
344
+ end
345
+
346
+ def self.exclude_file_pattern
347
+ get('TEST_BENCH_EXCLUDE_FILE_PATTERN', '*_init.rb')
348
+ end
349
+
350
+ def self.omit_backtrace_pattern
351
+ get('TEST_BENCH_OMIT_BACKTRACE_PATTERN', '*/test_bench/bootstrap.rb')
352
+ end
353
+
354
+ def self.tests_dir
355
+ get('TEST_BENCH_TESTS_DIRECTORY', 'test/automated')
356
+ end
357
+
358
+ if RUBY_ENGINE == 'mruby'
359
+ def self.env
360
+ {}
361
+ end
362
+ else
363
+ def self.output_device
364
+ $stdout
365
+ end
366
+
367
+ def self.env
368
+ ::ENV
369
+ end
370
+ end
371
+ end
260
372
  end
261
373
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test_bench-bootstrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Ladd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-10 00:00:00.000000000 Z
11
+ date: 2022-08-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: nathanladd+github@gmail.com
@@ -36,7 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
38
  requirements: []
39
- rubygems_version: 3.1.4
39
+ rubygems_version: 3.3.4
40
40
  signing_key:
41
41
  specification_version: 4
42
42
  summary: A minimal test framework for testing TestBench