Class: TestBench::CLI::ParseArguments

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

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ ParseArguments

Returns a new instance of ParseArguments.



18
19
20
# File 'lib/test_bench/cli/parse_arguments.rb', line 18

def initialize(argv)
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



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

def argv
  @argv
end

#envObject



8
9
10
# File 'lib/test_bench/cli/parse_arguments.rb', line 8

def env
  @env ||= {}
end

#output_deviceObject



13
14
15
# File 'lib/test_bench/cli/parse_arguments.rb', line 13

def output_device
  @output_device ||= StringIO.new
end

Class Method Details

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



22
23
24
25
26
27
28
29
30
# File 'lib/test_bench/cli/parse_arguments.rb', line 22

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

  instance = new(argv)
  instance.output_device = Output::Writer::Defaults.device
  instance.env = env
  instance
end

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



32
33
34
35
# File 'lib/test_bench/cli/parse_arguments.rb', line 32

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

.program_nameObject



162
163
164
# File 'lib/test_bench/cli/parse_arguments.rb', line 162

def self.program_name
  $PROGRAM_NAME
end

.versionObject



166
167
168
169
170
171
172
# File 'lib/test_bench/cli/parse_arguments.rb', line 166

def self.version
  if Object.const_defined?(:Gem)
    spec = Gem.loaded_specs['test_bench']
  end

  spec&.version || '(unknown)'
end

Instance Method Details

#assure_pattern(pattern_text) ⇒ Object



152
153
154
155
156
# File 'lib/test_bench/cli/parse_arguments.rb', line 152

def assure_pattern(pattern_text)
  Regexp.new(pattern_text.to_s)
rescue RegexpError
  raise Error, "Invalid regular expression pattern (Pattern: #{pattern_text.inspect})"
end

#callObject



37
38
39
# File 'lib/test_bench/cli/parse_arguments.rb', line 37

def call
  option_parser.parse(argv)
end

#none_patternObject



158
159
160
# File 'lib/test_bench/cli/parse_arguments.rb', line 158

def none_pattern
  /\z./
end

#option_parserObject



41
42
43
44
45
46
47
48
49
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
# File 'lib/test_bench/cli/parse_arguments.rb', line 41

def option_parser
  @option_parser ||= OptionParser.new do |parser|
    parser.banner = "Usage: #{self.class.program_name} [options] [paths]"

    parser.separator('')
    parser.separator("Informational Options")

    parser.on('-h', '--help', "Print this help message and exit successfully") do
      output_device.puts(parser.help)

      raise SystemExit.new(0)
    end

    parser.on('-V', '--version', "Print version and exit successfully") do
      output_device.puts <<TEXT
test-bench (#{self.class.program_name}) version #{self.class.version}
TEXT

      raise SystemExit.new(0)
    end

    parser.separator('')
    parser.separator("Configuration Options")

    parser.on('-a', '--[no-]abort-on-error', %{Exit immediately after any test failure or error (Default: #{TestBench::Defaults.abort_on_error ? 'on' : 'off'})}) do |abort_on_error|
      env['TEST_BENCH_ABORT_ON_ERROR'] = abort_on_error ? 'on' : 'off'
    end

    parser.on('-d', '--[no-]detail [DETAIL]', %{Always show (or hide) details (Default: #{Output::Raw::Defaults.detail})}) do |detail|
      if detail.nil?
        detail = 'on'
      elsif detail == true
        detail = 'on'
      elsif detail == false
        detail = 'off'
      end

      env['TEST_BENCH_DETAIL'] = detail
    end

    parser.on('-x', '--[no-]exclude PATTERN', %{Do not execute test files matching PATTERN (Default: #{Run::Defaults.exclude_pattern.inspect})}) do |pattern_text|
      if pattern_text == false
        pattern_text = self.none_pattern
      end

      assure_pattern(pattern_text)

      env['TEST_BENCH_EXCLUDE_FILE_PATTERN'] = pattern_text
    end

    parser.on('-l', '--log-level LEVEL', %{Set the internal logging level to LEVEL (Default: #{Output::Log::Defaults.level})}) do |level_text|
      level = level_text.to_sym

      Fixture::Output::Log.assure_level(level)

      env['TEST_BENCH_LOG_LEVEL'] = level_text
    end

    parser.on('-o', '--[no-]omit-backtrace PATTERN', %{Omit backtrace frames matching PATTERN (Default: #{Output::PrintError::Defaults.omit_backtrace_pattern.inspect})}) do |pattern_text|
      if pattern_text == false
        pattern_text = self.none_pattern
      end

      assure_pattern(pattern_text)

      env['TEST_BENCH_OMIT_BACKTRACE_PATTERN'] = pattern_text
    end

    parser.on('-s', '--output-styling [on|off|detect]', %{Render output coloring and font styling escape codes (Default: #{Output::Writer::Defaults.styling})}) do |styling_text|
      styling_text ||= 'on'

      styling = styling_text.to_sym

      Output::Writer.assure_styling_setting(styling)

      env['TEST_BENCH_OUTPUT_STYLING'] = styling_text
    end

    parser.on('-p', '--[no-]permit-deactivated-tests', %{Do not fail the test run if there are deactivated tests or contexts, e.g. _test or _context (Default: #{!TestBench::Defaults.fail_deactivated_tests ? 'on' : 'off'})}) do |permit_deactivated_tests|
      env['TEST_BENCH_FAIL_DEACTIVATED_TESTS'] = !permit_deactivated_tests ? 'on' : 'off'
    end

    parser.on('-r', '--[no-]reverse-backtraces', %{Reverse order of backtraces when printing errors (Default: #{Output::PrintError::Defaults.reverse_backtraces ? 'on' : 'off'})}) do |reverse_backtraces|
      env['TEST_BENCH_REVERSE_BACKTRACES'] = reverse_backtraces ? 'on' : 'off'
    end

    parser.on('-v', '--[no-]verbose', %{Increase output verbosity (Default: #{Output::Raw::Defaults.verbose ? 'on' : 'off'})}) do |verbose|
      env['TEST_BENCH_VERBOSE'] = verbose ? 'on' : 'off'
    end

    parser.separator(<<TEXT)

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, a default path (#{Defaults.tests_directory}) is scanned for test files.

The following environment variables can also control execution:

#{parser.summary_indent}TEST_BENCH_ABORT_ON_ERROR          Same as -a or --abort-on-error
#{parser.summary_indent}TEST_BENCH_DETAIL                  Same as -d or --detail
#{parser.summary_indent}TEST_BENCH_EXCLUDE_FILE_PATTERN    Same as -x or --exclude-file-pattern
#{parser.summary_indent}TEST_BENCH_LOG_LEVEL               Same as -l or --log-level
#{parser.summary_indent}TEST_BENCH_OMIT_BACKTRACE_PATTERN  Same as -o or --omit-backtrace-pattern
#{parser.summary_indent}TEST_BENCH_OUTPUT_STYLING          Same as -s or --output-styling
#{parser.summary_indent}TEST_BENCH_FAIL_DEACTIVATED_TESTS  Opposite of -p or --permit-deactivated-tests
#{parser.summary_indent}TEST_BENCH_REVERSE_BACKTRACES      Same as -r or --reverse-backtraces
#{parser.summary_indent}TEST_BENCH_VERBOSE                 Same as -v or --reverse-backtraces

TEXT
  end
end