test_bench 1.2.0.10 → 2.0.0.0.pre1

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/lib/test_bench/cli.rb +268 -18
  3. data/lib/test_bench/controls/file.rb +5 -0
  4. data/lib/test_bench/controls/path.rb +1 -11
  5. data/lib/test_bench/controls/random.rb +7 -0
  6. data/lib/test_bench/controls/result.rb +1 -1
  7. data/lib/test_bench/controls/stdin.rb +27 -0
  8. data/lib/test_bench/controls.rb +7 -24
  9. data/lib/test_bench/test_bench.rb +23 -77
  10. data/lib/test_bench.rb +1 -35
  11. metadata +16 -55
  12. data/lib/test_bench/cli/parse_arguments.rb +0 -181
  13. data/lib/test_bench/controls/caller_location.rb +0 -5
  14. data/lib/test_bench/controls/depth.rb +0 -21
  15. data/lib/test_bench/controls/device.rb +0 -27
  16. data/lib/test_bench/controls/directory.rb +0 -15
  17. data/lib/test_bench/controls/error.rb +0 -35
  18. data/lib/test_bench/controls/fixture.rb +0 -29
  19. data/lib/test_bench/controls/output/batch_data.rb +0 -52
  20. data/lib/test_bench/controls/output/detail_setting.rb +0 -29
  21. data/lib/test_bench/controls/output/escape_code.rb +0 -23
  22. data/lib/test_bench/controls/output/exercise.rb +0 -7
  23. data/lib/test_bench/controls/output/log_level.rb +0 -7
  24. data/lib/test_bench/controls/output/newline_character.rb +0 -16
  25. data/lib/test_bench/controls/output/print_error.rb +0 -19
  26. data/lib/test_bench/controls/output/styling.rb +0 -29
  27. data/lib/test_bench/controls/output/summary/error.rb +0 -28
  28. data/lib/test_bench/controls/output/summary/session.rb +0 -20
  29. data/lib/test_bench/controls/pattern.rb +0 -33
  30. data/lib/test_bench/controls/test_file.rb +0 -5
  31. data/lib/test_bench/controls/time.rb +0 -89
  32. data/lib/test_bench/deactivation_variants.rb +0 -11
  33. data/lib/test_bench/environment/boolean.rb +0 -40
  34. data/lib/test_bench/fixtures/configure_receiver.rb +0 -80
  35. data/lib/test_bench/fixtures.rb +0 -5
  36. data/lib/test_bench/output/batch_data.rb +0 -17
  37. data/lib/test_bench/output/buffer.rb +0 -114
  38. data/lib/test_bench/output/log.rb +0 -27
  39. data/lib/test_bench/output/print_error.rb +0 -163
  40. data/lib/test_bench/output/raw.rb +0 -363
  41. data/lib/test_bench/output/summary/session.rb +0 -94
  42. data/lib/test_bench/output/summary.rb +0 -146
  43. data/lib/test_bench/output/timer/substitute.rb +0 -45
  44. data/lib/test_bench/output/timer.rb +0 -75
  45. data/lib/test_bench/output/writer/dependency.rb +0 -12
  46. data/lib/test_bench/output/writer/sgr.rb +0 -54
  47. data/lib/test_bench/output/writer/substitute.rb +0 -38
  48. data/lib/test_bench/output/writer.rb +0 -192
  49. data/lib/test_bench/output.rb +0 -21
  50. data/lib/test_bench/run/substitute.rb +0 -36
  51. data/lib/test_bench/run.rb +0 -113
  52. data/script/bench +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf623714bd1a7e461fd2519de2a0315efc6823674ad4bb66d91057fed3feb504
4
- data.tar.gz: e501647a1755496cf9b6fd17c56c068094adf617b8090bea51c869741997d671
3
+ metadata.gz: 929185b5e91b95d52c3f5f118b7e38967ada55759d4f12b1e298ce44604f5dfb
4
+ data.tar.gz: af4a0aa5829a85bf41976eb16bbc41e8ce991517749130105d2ebedda13e168a
5
5
  SHA512:
6
- metadata.gz: 22baa0a0872e4030083134355946e890b0c663c9bb4e3026020f77e9e1e1e179f9e61ee499e8cedc8db90a4ee020f9025dc74f67addda27f820109420f2a3339
7
- data.tar.gz: c4b93134a82c2e2a381aec39e9fdc03fbccc905e98066f0b965cdfe343f483466accff6c7cb99cc461c168e447a488ac03127d057078daf1bb85e7aabb86873b
6
+ metadata.gz: 80f71eaeee2781b7408e40bd98570753beb40ebf0855481049bb7fa1f5675ead28e337d3d510b334559a470dd8b268bd1cb32a09bc78dbf28535643e9c884c3a
7
+ data.tar.gz: 578d27227eba55273bd47bd7a5802ad8d12b2806cc0dbff5caac79b325013053a1d70350b24e2331ebb8a66b870afa24e414961c3af8124a932fdc63527553a8
@@ -1,43 +1,293 @@
1
1
  module TestBench
2
- module CLI
3
- def self.call(tests_directory=nil, exclude_file_pattern: nil)
4
- tests_directory ||= Defaults.tests_directory
2
+ class CLI
3
+ ArgumentError = Class.new(RuntimeError)
5
4
 
6
- path_arguments = ParseArguments.()
5
+ attr_reader :arguments
7
6
 
8
- read_stdin = $stdin.stat.pipe?
7
+ attr_accessor :exit_code
9
8
 
10
- if read_stdin && $stdin.eof?
11
- STDERR.puts "$stdin is a pipe, but no data was written to it; no test files will be run"
9
+ def env
10
+ @env ||= {}
11
+ end
12
+ attr_writer :env
13
+
14
+ def program_name
15
+ @program_name ||= Defaults.program_name
16
+ end
17
+ attr_writer :program_name
18
+
19
+ def version
20
+ @version ||= Defaults.version
21
+ end
22
+ attr_writer :version
23
+
24
+ def stdin
25
+ @stdin ||= STDIN
26
+ end
27
+ attr_writer :stdin
28
+
29
+ def writer
30
+ @writer ||= Output::Writer::Substitute.build
31
+ end
32
+ attr_writer :writer
33
+
34
+ def run
35
+ @run ||= Run::Substitute.build
36
+ end
37
+ attr_writer :run
38
+
39
+ def random
40
+ @random ||= Random::Substitute.build
41
+ end
42
+ attr_writer :random
43
+
44
+ def initialize(*arguments)
45
+ @arguments = arguments
46
+ end
47
+
48
+ def self.build(arguments=nil, env: nil)
49
+ arguments ||= ::ARGV
50
+ env ||= ::ENV
51
+
52
+ instance = new(*arguments)
53
+ instance.env = env
54
+ Output::Writer.configure(instance)
55
+ Run.configure(instance)
56
+ instance
57
+ end
58
+
59
+ def self.call(arguments=nil, env: nil)
60
+ instance = build(arguments, env:)
61
+
62
+ session = instance.run.session
63
+ Session::Store.reset(session)
64
+
65
+ instance.()
66
+ end
67
+
68
+ def call
69
+ parse_arguments
70
+
71
+ if exit_code?
72
+ return exit_code
12
73
  end
13
74
 
14
- Run.(exclude: exclude_file_pattern) do |run|
15
- if read_stdin
16
- until $stdin.eof?
17
- path = $stdin.gets.chomp
75
+ writer.puts(RUBY_DESCRIPTION)
76
+ writer.puts("Random Seed: #{random.seed.to_s(36)}")
77
+ writer.puts
78
+
79
+ default_path = Defaults.tests_directory
80
+
81
+ result = run.! do |run|
82
+ if not stdin.tty?
83
+ until stdin.eof?
84
+ path = stdin.gets(chomp: true)
18
85
 
19
86
  next if path.empty?
20
87
 
21
- run.path(path)
88
+ run << path
22
89
  end
23
90
  end
24
91
 
25
- if path_arguments.empty?
26
- unless read_stdin
27
- run.path(tests_directory)
92
+ arguments.each do |path|
93
+ run << path
94
+ end
95
+
96
+ if not run.ran?
97
+ run << default_path
98
+ end
99
+ end
100
+
101
+ self.exit_code = self.class.exit_code(result)
102
+
103
+ exit_code
104
+ end
105
+
106
+ def parse_arguments
107
+ argument_index = 0
108
+
109
+ until argument_index == arguments.count
110
+ next_argument = next_argument(argument_index)
111
+
112
+ if not next_argument.start_with?('-')
113
+ argument_index += 1
114
+ next
115
+ end
116
+
117
+ switch = next_argument!(argument_index)
118
+
119
+ case switch
120
+ when '--'
121
+ break
122
+
123
+ when '-h', '--help'
124
+ print_help_text
125
+
126
+ self.exit_code = 0
127
+
128
+ when '-v', '--version'
129
+ writer.puts "TestBench Version: #{version}"
130
+
131
+ self.exit_code = 0
132
+
133
+ when '-d', '--detail', '--no-detail'
134
+ if not negated?(switch)
135
+ detail_policy_text = 'on'
136
+ else
137
+ detail_policy_text = 'off'
28
138
  end
29
- else
30
- path_arguments.each do |path|
31
- run.path(path)
139
+
140
+ detail_policy = detail_policy_text.to_sym
141
+ Session::Output::Detail.assure_detail(detail_policy)
142
+
143
+ env['TEST_BENCH_DETAIL'] = detail_policy_text
144
+
145
+ when '-x', '--exclude', '--no-exclude'
146
+ if not negated?(switch)
147
+ exclude_file_pattern_text = switch_value!(argument_index, switch)
148
+ else
149
+ exclude_file_pattern_text = ''
150
+ end
151
+
152
+ env['TEST_BENCH_EXCLUDE_FILE_PATTERN'] = exclude_file_pattern_text
153
+
154
+ when '-f', '--only-failure', '--no-only-failure'
155
+ if not negated?(switch)
156
+ only_failure_text = 'on'
157
+ else
158
+ only_failure_text = 'off'
159
+ end
160
+
161
+ env['TEST_BENCH_ONLY_FAILURE'] = only_failure_text
162
+
163
+ when '-o', '--output-styling'
164
+ output_styling_text = switch_value(argument_index) do
165
+ 'on'
32
166
  end
167
+
168
+ output_styling = output_styling_text.to_sym
169
+ Output::Writer::Styling.assure_styling(output_styling)
170
+
171
+ env['TEST_BENCH_OUTPUT_STYLING'] = output_styling_text
172
+
173
+ when '-s', '--seed'
174
+ seed_text = switch_value!(argument_index, switch)
175
+
176
+ begin
177
+ Integer(seed_text)
178
+ rescue
179
+ raise ArgumentError, "Seed switch must be an integer (Seed: #{seed_text.inspect})"
180
+ end
181
+
182
+ env['TEST_BENCH_SEED'] = seed_text
183
+
184
+ when '-r', '--require'
185
+ library = switch_value!(argument_index, switch)
186
+
187
+ require library
188
+
189
+ else
190
+ raise ArgumentError, "Unknown switch #{switch.inspect}"
33
191
  end
34
192
  end
35
193
  end
36
194
 
195
+ def print_help_text
196
+ writer.write <<~TEXT
197
+ Usage: #{program_name} [options] [paths]
198
+
199
+ Informational Options:
200
+ \t-h, --help Print this help message and exit successfully
201
+ \t-v, --version Print version and exit successfully
202
+
203
+ Configuration Options:
204
+ \t-d, --[no]detail Always show (or hide) details (Default: #{Session::Output::Detail.default})
205
+ \t-x, --[no-]exclude PATTERN Do not execute test files matching PATTERN (Default: #{Run::GetFiles::Defaults.exclude_file_pattern.inspect})
206
+ \t-f, --[no-]only-failure Don't display output for test files that pass (Default: #{Run::Output::File::Defaults.only_failure ? 'on' : 'off'})
207
+ \t-o, --output-styling [on|off|detect]
208
+ \t Render output coloring and font styling escape codes (Default: #{Output::Writer::Styling.default})
209
+ \t-s, --seed NUMBER Sets pseudo-random number seed (Default: not set)
210
+
211
+ Other Options:
212
+ \t-r, --require LIBRARY Require LIBRARY before running any files
213
+
214
+ Paths to test files (and directories containing test files) can be given after any command line arguments or via STDIN (or both).
215
+
216
+ If no paths are given, a default path (#{Defaults.tests_directory}) is scanned for test files.
217
+
218
+ The following environment variables can also control execution:
219
+
220
+ \tTEST_BENCH_DETAIL Same as -d or --detail
221
+ \tTEST_BENCH_EXCLUDE_FILE_PATTERN Same as -x or --exclude-file-pattern
222
+ \tTEST_BENCH_ONLY_FAILURE Same as -f or --only-failure
223
+ \tTEST_BENCH_OUTPUT_STYLING Same as -o or --output-styling
224
+ \tTEST_BENCH_SEED Same as -s or --seed
225
+
226
+ TEXT
227
+ end
228
+
229
+ def negated?(switch)
230
+ switch.start_with?('--no-')
231
+ end
232
+
233
+ def exit_code?
234
+ !exit_code.nil?
235
+ end
236
+
237
+ def switch_value!(argument_index, argument)
238
+ switch_value(argument_index) do
239
+ raise ArgumentError, "Argument #{argument.inspect} requires an argument"
240
+ end
241
+ end
242
+
243
+ def switch_value(argument_index, &no_value_action)
244
+ next_value = next_argument(argument_index)
245
+
246
+ if next_value.nil? || next_value.start_with?('-')
247
+ switch_value = nil
248
+
249
+ return no_value_action.()
250
+ else
251
+ switch_value = next_argument!(argument_index)
252
+
253
+ return switch_value
254
+ end
255
+ end
256
+
257
+ def next_argument(argument_index)
258
+ arguments[argument_index]
259
+ end
260
+
261
+ def next_argument!(argument_index)
262
+ arguments.delete_at(argument_index)
263
+ end
264
+
265
+ def self.exit_code(result)
266
+ if result == true
267
+ 0
268
+ elsif result == false
269
+ 1
270
+ else
271
+ 2
272
+ end
273
+ end
274
+
37
275
  module Defaults
38
276
  def self.tests_directory
39
277
  ENV.fetch('TEST_BENCH_TESTS_DIRECTORY', 'test/automated')
40
278
  end
279
+
280
+ def self.program_name
281
+ $PROGRAM_NAME || 'bench'
282
+ end
283
+
284
+ def self.version
285
+ if Object.const_defined?(:Gem)
286
+ spec = Gem.loaded_specs['test_bench']
287
+ end
288
+
289
+ spec&.version || '(unknown)'
290
+ end
41
291
  end
42
292
  end
43
293
  end
@@ -0,0 +1,5 @@
1
+ module TestBench
2
+ module Controls
3
+ File = Run::Controls::File
4
+ end
5
+ end
@@ -1,15 +1,5 @@
1
1
  module TestBench
2
2
  module Controls
3
- module Path
4
- def self.example
5
- TestFile.filename
6
- end
7
-
8
- def self.alternate
9
- TestFile::Alternate.filename
10
- end
11
-
12
- Directory = Controls::Directory
13
- end
3
+ Path = Run::Controls::Path
14
4
  end
15
5
  end
@@ -0,0 +1,7 @@
1
+ module TestBench
2
+ module Controls
3
+ module Random
4
+ Seed = TestBench::Random::Controls::Seed
5
+ end
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  module TestBench
2
2
  module Controls
3
- Result = TestBench::Fixture::Controls::Result
3
+ Result = Run::Controls::Result
4
4
  end
5
5
  end
@@ -0,0 +1,27 @@
1
+ module TestBench
2
+ module Controls
3
+ module Stdin
4
+ module Create
5
+ def self.call(*paths)
6
+ if paths.empty?
7
+ paths = Path.examples
8
+ end
9
+
10
+ contents = <<~TEXT
11
+ #{paths.join("\n")}
12
+ TEXT
13
+
14
+ file = File::Create.(contents:)
15
+
16
+ ::File.open(file, 'r')
17
+ end
18
+
19
+ def self.contents
20
+ <<~TEXT
21
+ #{Path.example}
22
+ TEXT
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,28 +1,11 @@
1
- unless RUBY_ENGINE == 'mruby'
2
- require 'securerandom'
3
- end
1
+ require 'test_bench/random/controls'
2
+ require 'test_bench/run/controls'
4
3
 
5
- require 'test_bench/fixture/controls'
4
+ require 'test_bench/controls/result'
6
5
 
7
- require 'test_bench/controls/caller_location'
8
- require 'test_bench/controls/depth'
9
- require 'test_bench/controls/device'
10
- require 'test_bench/controls/directory'
11
- require 'test_bench/controls/error'
12
- require 'test_bench/controls/fixture'
13
6
  require 'test_bench/controls/path'
14
- require 'test_bench/controls/pattern'
15
- require 'test_bench/controls/result'
16
- require 'test_bench/controls/test_file'
17
- require 'test_bench/controls/time'
18
7
 
19
- require 'test_bench/controls/output/escape_code'
20
- require 'test_bench/controls/output/newline_character'
21
- require 'test_bench/controls/output/styling'
22
- require 'test_bench/controls/output/print_error'
23
- require 'test_bench/controls/output/exercise'
24
- require 'test_bench/controls/output/summary/session'
25
- require 'test_bench/controls/output/summary/error'
26
- require 'test_bench/controls/output/batch_data'
27
- require 'test_bench/controls/output/detail_setting'
28
- require 'test_bench/controls/output/log_level'
8
+ require 'test_bench/controls/file'
9
+ require 'test_bench/controls/stdin'
10
+
11
+ require 'test_bench/controls/random'
@@ -1,104 +1,50 @@
1
1
  module TestBench
2
- def self.session
3
- @session ||= build_session.tap do |session|
4
- at_exit do
5
- exit_code = exit_code(session)
6
-
7
- exit(exit_code) unless exit_code.zero?
8
- end
9
- end
10
- end
11
- singleton_class.attr_writer(:session)
12
-
13
- def self.output
14
- session.output
15
- end
16
-
17
- def self.set_output(output, session: nil)
18
- session ||= self.session
19
-
20
- if output.is_a?(Array)
21
- output = Fixture::Output::Multiple.build(*output)
22
- end
23
-
24
- session.output = output
25
- end
26
- singleton_class.alias_method :output=, :set_output
27
-
28
- def self.activate(receiver=nil, session: nil)
2
+ def self.activate(receiver=nil, session_store: nil)
29
3
  receiver ||= TOPLEVEL_BINDING.receiver
4
+ session_store ||= Session::Store.instance
30
5
 
31
- fixture(session, receiver: receiver)
32
- end
33
-
34
- def self.evaluate(session: nil, &block)
35
- fixture = fixture(session)
6
+ receiver.extend(Fixture)
7
+ receiver.extend(DeactivatedVariants)
36
8
 
37
- fixture.context do
38
- fixture.instance_exec(&block)
39
- end
9
+ receiver.extend(TestSession)
10
+ receiver.test_session_store = session_store
40
11
  end
41
12
 
42
13
  def self.context(title=nil, session: nil, &block)
43
- evaluate(session: session) do
14
+ evaluate(session:) do
44
15
  context(title) do
45
16
  instance_exec(&block)
46
17
  end
47
18
  end
48
19
  end
49
20
 
50
- def self.fixture(session=nil, receiver: nil)
51
- session ||= self.session
52
- receiver ||= Object.new
53
-
54
- receiver.extend(Fixture)
55
- receiver.extend(DeactivationVariants)
56
-
57
- receiver.test_session = session
58
-
59
- receiver
60
- end
61
-
62
- def self.build_session(output: nil, abort_on_error: nil, **args)
63
- output ||= Output.build
64
- error_policy = session_error_policy(abort_on_error)
21
+ def self.evaluate(session: nil, &block)
22
+ fixture = TestBench::Fixture::Evaluate.build(session:, &block)
23
+ fixture.extend(DeactivatedVariants)
24
+ fixture.()
65
25
 
66
- Fixture::Session.build(output: output, error_policy: error_policy, **args)
26
+ fixture.test_session.passed?
67
27
  end
68
28
 
69
- def self.session_error_policy(abort_on_error=nil)
70
- abort_on_error = Defaults.abort_on_error if abort_on_error.nil?
71
-
72
- if abort_on_error
73
- :abort
74
- else
75
- :rescue
76
- end
29
+ def self.session
30
+ Session::Store.get
77
31
  end
78
32
 
79
- def self.exit_code(session, fail_deactivated_tests: nil)
80
- if fail_deactivated_tests.nil?
81
- fail_deactivated_tests = Defaults.fail_deactivated_tests
33
+ module DeactivatedVariants
34
+ def _context(title=nil, &)
35
+ context(title)
82
36
  end
83
37
 
84
- if session.failed?
85
- 1
86
- elsif session.skipped?
87
- fail_deactivated_tests ? 2 : 0
88
- else
89
- 0
38
+ def _test(title=nil, &)
39
+ test(title)
90
40
  end
91
41
  end
92
42
 
93
- Session = Fixture::Session
94
-
95
- module Defaults
96
- def self.abort_on_error
97
- Environment::Boolean.fetch('TEST_BENCH_ABORT_ON_ERROR', false)
98
- end
43
+ module TestSession
44
+ attr_accessor :test_session_store
99
45
 
100
- def self.fail_deactivated_tests
101
- Environment::Boolean.fetch('TEST_BENCH_FAIL_DEACTIVATED_TESTS', true)
46
+ def test_session
47
+ test_session_store.fetch
102
48
  end
103
49
  end
104
50
  end
data/lib/test_bench.rb CHANGED
@@ -1,39 +1,5 @@
1
- unless RUBY_ENGINE == 'mruby'
2
- require 'optionparser'
3
- end
4
-
5
- require 'test_bench/fixture'
6
-
7
- require 'test_bench/environment/boolean'
8
-
9
- require 'test_bench/output/writer'
10
- require 'test_bench/output/writer/sgr'
11
- require 'test_bench/output/writer/substitute'
12
- require 'test_bench/output/writer/dependency'
13
-
14
- require 'test_bench/output/timer'
15
- require 'test_bench/output/timer/substitute'
16
-
17
- require 'test_bench/output/print_error'
18
-
19
- require 'test_bench/output/summary'
20
- require 'test_bench/output/summary/session'
21
-
22
- require 'test_bench/output/batch_data'
23
-
24
- require 'test_bench/output/raw'
25
-
26
- require 'test_bench/output/buffer'
27
-
28
- require 'test_bench/output/log'
29
-
30
- require 'test_bench/output'
1
+ require 'test_bench/run'
31
2
 
32
3
  require 'test_bench/test_bench'
33
- require 'test_bench/deactivation_variants'
34
-
35
- require 'test_bench/run'
36
- require 'test_bench/run/substitute'
37
4
 
38
- require 'test_bench/cli/parse_arguments'
39
5
  require 'test_bench/cli'