Class: TestBench::Executable::ParseArguments

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/executable/parse_arguments.rb

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_arguments) ⇒ ParseArguments

Returns a new instance of ParseArguments.



22
23
24
# File 'lib/test_bench/executable/parse_arguments.rb', line 22

def initialize(raw_arguments)
  @raw_arguments = raw_arguments
end

Instance Attribute Details

#envObject



11
12
13
# File 'lib/test_bench/executable/parse_arguments.rb', line 11

def env
  @env ||= {}
end

#raw_argumentsObject (readonly)

Returns the value of attribute raw_arguments.



20
21
22
# File 'lib/test_bench/executable/parse_arguments.rb', line 20

def raw_arguments
  @raw_arguments
end

#writerObject



6
7
8
# File 'lib/test_bench/executable/parse_arguments.rb', line 6

def writer
  @writer ||= Output::Writer::Substitute.build
end

Class Method Details

.build(arguments = nil, env: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/test_bench/executable/parse_arguments.rb', line 26

def self.build(arguments=nil, env: nil)
  arguments ||= ::ARGV
  env ||= ::ENV

  options_env_var = env['TEST_BENCH_OPTIONS']
  if not options_env_var.nil?
    env_arguments = Shellwords.split(options_env_var)
    arguments.unshift(*env_arguments)
  end

  instance = new(arguments)

  instance.env = env

  Output::Writer.configure(instance)

  instance
end

.call(arguments = nil, env: nil) ⇒ Object



45
46
47
48
# File 'lib/test_bench/executable/parse_arguments.rb', line 45

def self.call(arguments=nil, env: nil)
  instance = build(arguments, env:)
  instance.()
end

Instance Method Details

#callObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/test_bench/executable/parse_arguments.rb', line 50

def call
  loop do
    switch = next_switch

    case switch
    when '--', nil
      break

    when '-a', '--abort-on-failure'
      env['TEST_BENCH_ABORT_ON_FAILURE'] = 'on'

    when '-x', '--exclude'
      exclude_pattern = require_next_argument(switch)

      env['TEST_BENCH_EXCLUDE_FILE_PATTERN'] = [
        env['TEST_BENCH_EXCLUDE_FILE_PATTERN'],
        exclude_pattern
      ].compact.join(':')
    when '-X', '--no-exclude'
      env['TEST_BENCH_EXCLUDE_FILE_PATTERN'] = ''

    when '-s', '--strict'
      env['TEST_BENCH_STRICT'] = 'on'
    when '-S', '--no-strict'
      env['TEST_BENCH_STRICT'] = 'off'

    when '-r', '--require'
      library = require_next_argument(switch)
      require(library)
    when '-I', '--include'
      load_path = require_next_argument(switch)
      if not $LOAD_PATH.include?(load_path)
        $LOAD_PATH << load_path
      end

    when '--random-seed'
      seed = require_next_argument(switch)
      env['TEST_BENCH_RANDOM_SEED'] = seed

    when '-b', '--omit-backtrace'
      omit_pattern = require_next_argument(switch)

      env['TEST_BENCH_OMIT_BACKTRACE_PATTERN'] = [
        env['TEST_BENCH_OMIT_BACKTRACE_PATTERN'],
        omit_pattern
      ].compact.join(':')

    when '-d', '--detail'
      env['TEST_BENCH_DETAIL'] = 'on'
    when '-D', '--no-detail'
      env['TEST_BENCH_DETAIL'] = 'off'

    when '--device'
      device = require_next_argument(switch)
      env['TEST_BENCH_OUTPUT_DEVICE'] = device

    when '-l', '--output-level'
      output_level = require_next_argument(switch)
      env['TEST_BENCH_OUTPUT_LEVEL'] = output_level
    when '-q', '--quiet'
      env['TEST_BENCH_OUTPUT_LEVEL'] = 'not-passing'

    when '-o', '--output-styling'
      env['TEST_BENCH_OUTPUT_STYLING'] = 'on'
    when '-O', '--no-output-styling'
      env['TEST_BENCH_OUTPUT_STYLING'] = 'off'

    when '--no-summary'
      env['TEST_BENCH_OUTPUT_SUMMARY'] = 'off'

    when '-h', '--help'
      writer.write(<<~TEXT)
Usage: #{Defaults.program_name} [options] [paths]

Informational Options:
  Help:
-h, --help
    Print this help message and exit immediately

Execution Options:
  Abort On Failure:
-a, --abort-on-failure
    Stops execution if a test fails or a test file aborts

  Exclude File Patterns:
-x, --exclude PATTERN
    Exclude test files that match PATTERN
    If multiple --exclude arguments are supplied, then files that match any will be excluded
-X, --no-exclude
    Don't exclude any files
Default: '*_init.rb'

  Strict:
-s, --strict
    Prohibit skipped tests and contexts, and require at least one test to be performed
-S, --no-strict
    Relax strictness
Default: non strict, unless TEST_BENCH_STRICT is set to 'on'

  Require Library:
-r, --require LIBRARY
    Require LIBRARY before running any files
-I, --include DIR
    Add DIR to the load path

  Random Seed:
--random-seed SEED
    Pseudorandom number seed

Output Options:
  Backtrace Formatting:
-b, --omit-backtrace PATTERN
    Omits backtrace frames that match PATTERN
    If multiple --omit-backtrace arguments are supplied, then frames that match any will be omitted

  Detail:
-d, --detail
    Always show details
-D, --no-detail
    Never show details
Default: print details when their surrounding context failed, unless TEST_DETAIL is set to 'on' or 'off'

  Device:
--device DEVICE
    stderr: redirect output to standard error
    null: don't write any output
Default: stdout

  Verbosity:
-l, --output-level LEVEL
    all: print output from every file
    not-passing: print output from files that skip tests and contexts or don't perform any tests
    failure: print output only from files that failed or aborted
    abort: print output only from file that aborted
-q, --quiet
    Sets output verbosity level to 'not-passing'
Default: all

  Styling:
-o, --output-styling
    Enable output text styling
-O, --no-output-styling
    Disable output text styling
Default: enabled if the output device is an interactive terminal

  Summary:
--no-summary
    Don't print summary after running files

Paths to test files (and directories containing test files) can be given after any command line arguments or via STDIN (or both).

If no paths are given, the directory '#{Defaults.path}' is scanned for test files.

The following environment variables can also control execution:

  TEST_BENCH_ABORT_ON_FAILURE           See --abort-on-failure
  TEST_BENCH_EXCLUDE_FILE_PATTERN       See --exclude
  TEST_BENCH_OUTPUT_SUMMARY             See --no-summary
  TEST_BENCH_STRICT                     See --strict
  TEST_BENCH_RANDOM_SEED                See --random-seed
  TEST_BENCH_FILTER_BACKTRACE_PATTERN   See --filter-backtrace
  TEST_BENCH_DETAIL                     See --detail
  TEST_BENCH_OUTPUT_DEVICE              See --device
  TEST_BENCH_OUTPUT_LEVEL               See --output-level
  TEST_BENCH_OUTPUT_STYLING             See --output-styling
  TEST_BENCH_DEFAULT_TEST_PATH          Specifies default path
  TEST_BENCH_OPTIONS                    Evaluated as command line arguments similar to RUBYOPT

      TEXT
      exit(true)

    else
      raise Error, "Incorrect switch #{switch}"
    end
  end

  remaining_arguments
end

#next_argumentObject



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/test_bench/executable/parse_arguments.rb', line 244

def next_argument
  next_argument = raw_arguments.shift

  if next_argument.nil?
    nil
  elsif switch?(next_argument)
    raw_arguments.unshift(next_argument)
    nil
  else
    next_argument
  end
end

#next_switchObject



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/test_bench/executable/parse_arguments.rb', line 257

def next_switch
  until raw_arguments.empty?
    argument = raw_arguments.shift

    if switch?(argument)
      return argument
    end

    non_switch_arguments << argument
  end

  nil
end

#non_switch_argumentsObject



16
17
18
# File 'lib/test_bench/executable/parse_arguments.rb', line 16

def non_switch_arguments
  @non_switch_arguments ||= []
end

#remaining_argumentsObject Also known as: arguments



229
230
231
# File 'lib/test_bench/executable/parse_arguments.rb', line 229

def remaining_arguments
  non_switch_arguments + raw_arguments
end

#require_next_argument(switch_text) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/test_bench/executable/parse_arguments.rb', line 234

def require_next_argument(switch_text)
  argument = next_argument

  if argument.nil?
    raise Error, "Switch #{switch_text} requires an argument"
  end

  argument
end

#switch?(argument) ⇒ Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/test_bench/executable/parse_arguments.rb', line 271

def switch?(argument)
  argument.start_with?('-')
end