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("Usage: \#{Defaults.program_name} [options] [paths]\n\nInformational Options:\n  Help:\n-h, --help\n    Print this help message and exit immediately\n\nExecution Options:\n  Abort On Failure:\n-a, --abort-on-failure\n    Stops execution if a test fails or a test file aborts\n\n  Exclude File Patterns:\n-x, --exclude PATTERN\n    Exclude test files that match PATTERN\n    If multiple --exclude arguments are supplied, then files that match any will be excluded\n-X, --no-exclude\n    Don't exclude any files\nDefault: '*_init.rb'\n\n  Strict:\n-s, --strict\n    Prohibit skipped tests and contexts, and require at least one test to be performed\n-S, --no-strict\n    Relax strictness\nDefault: non strict, unless TEST_BENCH_STRICT is set to 'on'\n\n  Require Library:\n-r, --require LIBRARY\n    Require LIBRARY before running any files\n-I, --include DIR\n    Add DIR to the load path\n\n  Random Seed:\n--random-seed SEED\n    Pseudorandom number seed\n\nOutput Options:\n  Backtrace Formatting:\n-b, --omit-backtrace PATTERN\n    Omits backtrace frames that match PATTERN\n    If multiple --omit-backtrace arguments are supplied, then frames that match any will be omitted\n\n  Detail:\n-d, --detail\n    Always show details\n-D, --no-detail\n    Never show details\nDefault: print details when their surrounding context failed, unless TEST_DETAIL is set to 'on' or 'off'\n\n  Device:\n--device DEVICE\n    stderr: redirect output to standard error\n    null: don't write any output\nDefault: stdout\n\n  Verbosity:\n-l, --output-level LEVEL\n    all: print output from every file\n    not-passing: print output from files that skip tests and contexts or don't perform any tests\n    failure: print output only from files that failed or aborted\n    abort: print output only from file that aborted\n-q, --quiet\n    Sets output verbosity level to 'not-passing'\nDefault: all\n\n  Styling:\n-o, --output-styling\n    Enable output text styling\n-O, --no-output-styling\n    Disable output text styling\nDefault: enabled if the output device is an interactive terminal\n\n  Summary:\n--no-summary\n    Don't print summary after running files\n\nPaths to test files (and directories containing test files) can be given after any command line arguments or via STDIN (or both).\n\nIf no paths are given, the directory '\#{Defaults.path}' is scanned for test files.\n\nThe following environment variables can also control execution:\n\n  TEST_BENCH_ABORT_ON_FAILURE           See --abort-on-failure\n  TEST_BENCH_EXCLUDE_FILE_PATTERN       See --exclude\n  TEST_BENCH_OUTPUT_SUMMARY             See --no-summary\n  TEST_BENCH_STRICT                     See --strict\n  TEST_BENCH_RANDOM_SEED                See --random-seed\n  TEST_BENCH_FILTER_BACKTRACE_PATTERN   See --filter-backtrace\n  TEST_BENCH_DETAIL                     See --detail\n  TEST_BENCH_OUTPUT_DEVICE              See --device\n  TEST_BENCH_OUTPUT_LEVEL               See --output-level\n  TEST_BENCH_OUTPUT_STYLING             See --output-styling\n  TEST_BENCH_DEFAULT_TEST_PATH          Specifies default path\n  TEST_BENCH_OPTIONS                    Evaluated as command line arguments similar to RUBYOPT\n\n      TEXT\n      exit(true)\n\n    else\n      raise Error, \"Incorrect switch \#{switch}\"\n    end\n  end\n\n  remaining_arguments\nend\n")

#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