Class: Moto::ParameterParser

Inherits:
Object
  • Object
show all
Defined in:
lib/parameter_parser.rb

Class Method Summary collapse

Class Method Details

.config_test_generatorObject



63
64
65
# File 'lib/parameter_parser.rb', line 63

def self.config_test_generator
  Moto::Config::Manager.config_moto[:test_generator]
end

.config_test_runnerObject



59
60
61
# File 'lib/parameter_parser.rb', line 59

def self.config_test_runner
  Moto::Config::Manager.config_moto[:test_runner]
end

.generate_parse(argv) ⇒ Object

Parses attributes passed to the application when run by ‘moto generate’



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/parameter_parser.rb', line 165

def self.generate_parse(argv)
  options = {}

  OptionParser.new do |opts|
    opts.on('-t', '--test Test') {|v| options[:dir] = v}
    opts.on('-a', '--appname AppName') {|v| options[:app_name] = v}
    opts.on('-b', '--baseclass BaseClass') {|v| options[:base_class] = v}
    opts.on('-f', '--force') {options[:force] = true}
  end.parse!

  self.prepare_config(options)

  options[:dir] = options[:dir].underscore

  if options[:app_name].nil?
    options[:app_name] = 'MotoApp'
  end

  return options
end

.parse_user_input(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/parameter_parser.rb', line 16

def self.parse_user_input(argv)
  begin

    mode_selector = Moto::Modes::ModeSelector.new

    # TODO: IMPORTANT ISSUE
    # xxx_parse functions should not return parsed arguments and pass them to functions
    # but instead all should inject proper values into Moto::Config::Manager.config_moto[:xxx][:yyy]
    # on which all further components should relay

    if argv[0] == '--version'
      mode_selector.version
    elsif argv[0] == 'run' && argv.length > 1
      mode_selector.run(run_parse(argv))
    elsif argv[0] == 'generate' && argv.length > 1
      mode_selector.generate(generate_parse(argv))
    elsif argv[0] == 'validate' && argv.length > 1
      mode_selector.validate(validate_parse(argv))
    else
      show_help
    end

  rescue SystemExit => e
    Kernel.exit(e.status)
  rescue Exception => e
    puts e.message + "\n\n"
    puts e.backtrace.join("\n")
  end
end

.prepare_config(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/parameter_parser.rb', line 46

def self.prepare_config(options)
  Moto::Config::Manager.load_configuration(options[:config_name], options[:environment])

  if !config_test_runner
    Moto::Config::Manager.config_moto[:test_runner] = {}
  end

  if !config_test_generator
    Moto::Config::Manager.config_moto[:test_generator] = {}
  end

end

.run_parse(argv) ⇒ Object



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
# File 'lib/parameter_parser.rb', line 67

def self.run_parse(argv)
  # Default options
  options = {}
  options[:listeners] = []
  options[:mwui_path] = nil
  options[:mwui_assignee_id] = nil
  options[:stop_on] = {error: false, fail: false, skip: false}

  # Parse arguments
  OptionParser.new do |opts|
    opts.on('-c', '--config Config')              {|v| options[:config_name] = v}
    opts.on('-e', '--environment Environment')    {|v| options[:environment] = v}
    opts.on('-t', '--tests Tests', Array)         {|v| options[:tests] = v}
    opts.on('-g', '--tags Tags', Array)           {|v| options[:tags] = v}
    opts.on('-f', '--filters Filters', Array)     {|v| options[:filters] = v}
    opts.on('-l', '--listeners Listeners', Array) {|v| options[:listeners] = v}
    opts.on('--mwui-path MwuiPath')               {|v| options[:mwui_path] = v}
    opts.on('--mwui-assignee-id MwuiAssignee')    {|v| options[:mwui_assignee_id] = v}
    opts.on('-c', '--config Config')              {|v| options[:config_name] = v}
    opts.on('--threads ThreadCount', Integer)     {|v| options[:threads] = v}
    opts.on('--attempts AttemptCount', Integer)   {|v| options[:attempts] = v}
    opts.on('--stop-on-error')                    {options[:stop_on][:error] = true}
    opts.on('--stop-on-fail')                     {options[:stop_on][:fail] = true}
    opts.on('--stop-on-skip')                     {options[:stop_on][:skip] = true}
    opts.on('--dry-run')                          {options[:dry_run] = true}
    opts.on('-x', '--explicit-errors')            {options[:explicit_errors] = true}
    opts.on('--log-level LogLevel')               {|v| options[:log_level] = v}
    opts.on('--param-name ParamName')             {|v| options[:param_name] = v}
  end.parse!

  self.prepare_config(options)

  if options[:tests]
    options[:tests].each do |path|
      path.sub!(%r{\/$}, '') # remove trailing "/"
    end
  end

  # Runner configuration

  Moto::Config::Manager.config_moto[:test_runner][:thread_count] = options[:threads] if options[:threads]
  Moto::Config::Manager.config_moto[:test_runner][:test_attempt_max] = options[:attempts] if options[:attempts]
  Moto::Config::Manager.config_moto[:test_runner][:dry_run] = options[:dry_run] if options[:dry_run]
  Moto::Config::Manager.config_moto[:test_runner][:explicit_errors] = options[:explicit_errors] if options[:explicit_errors]

  # Test log level parsing

  if options[:log_level]
    Moto::Config::Manager.config_moto[:test_runner][:log_level] = case options[:log_level].downcase
    when 'info'   then Logger::INFO
    when 'warn'   then Logger::WARN
    when 'error'  then  Logger::ERROR
    when 'fatal'  then Logger::FATAL
    else Logger::DEBUG
    end
  else
    Moto::Config::Manager.config_moto[:test_runner][:log_level] = Logger::DEBUG
  end

  # Generator configuration
  Moto::Config::Manager.config_moto[:test_generator][:param_name] = options[:param_name] if options[:param_name]

  return options
end

.show_helpObject



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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/parameter_parser.rb', line 186

def self.show_help
  puts "" "
  Moto (#{Moto::VERSION}) CLI Help:
  moto --version     Display current version



  Exemplary usage:
     moto run PARAMTERES
     moto generate PARAMETERS
     moto validate PARAMETERS



  =========
  MOTO RUN:
  =========
   -t, --tests       Path of tests to be executed. Root: moto-phq/tests/<TESTS PATH>
   -g, --tags        Tags of tests to be executed.
                     Use # MOTO_TAGS: TAGNAME in test to assign tag.
   -f, --filters     Tags that filter tests passed via -t parameter.
                     Only tests in appropriate directory, having all of the specified tags will be executed.
                     Use # MOTO_TAGS: TAGNAME1 in test to assign tag.
                     Use ~ to filter tests that do not contain specific tag, e.g. ~tag


   -e, --environment Environment constants and tests parametrized in certain way depend on this.
                     Without this param only `config/environment/common.rb` will be loaded.
                     If provided moto will try to load additionally `config/environment/NAME.rb` and apply changes
                     on top of already loaded common configuration.
   -c, --config      Name of the config, without extension, to be loaded from MotoApp/config/CONFIG_NAME.rb
                     Default: moto (which loads: MotoApp/config/moto.rb)


   -l, --listeners   Reporters to be used.
                     Defaults are Moto::Reporting::Listeners::ConsoleDots, Moto::Reporting::Listeners::JunitXml
                     One reporter that is always used: Moto::Reporting::Listeners::KernelCode

   --threads         Thread count. Run tests in parallel.
   --attempts        Attempt count. Max number of test execution times if failed.

   --stop-on-error   Moto will stop test execution when an error is encountered in test results
   --stop-on-fail    Moto will stop test execution when a failure is encountered in test results
   --stop-on-skip    Moto will stop test execution when a skip is encountered in test results

   --dry-run         Moto will list all test cases which would be run with provided arguments

   --x, explicit-errors   Use for development of tests - each code error in test will be caught,
                          logged as would normally do but will be also additionaly re-thrown.
                          This will obviously stop execution of selected set of tests but will provide
                          full stack and error message to the developer.

   --log-level       Defines level at which Logger works. Use one of following: debug, info, warn, error, fatal.

   --param-name      Only parameters that contain provided string will be executed.

   --mwui-path           [MotoWebUI 2.0] Directory path (eg. /regression/areaname) to which results of the current run
                         should be aggregated.
                         Required when specifying MotoWebUI as one of the listeners.
   --mwui-assignee-id    [MotoWebUI 2.0] ID of a person responsible for current test run.
                         Can have a default value set in config/webui section.



  ==============
  MOTO VALIDATE:
  ==============
   -t, --tests       Path of tests to be validate. Root: moto-phq/tests/<TESTS PATH>
   -g, --tags        Tags of tests to be validated.
                     Use # MOTO_TAGS: TAGNAME in test to assign tag.
   -f, --filters     Tags that filter tests passed via -t parameter.
                     Only tests in appropriate directory, having all of the specified tags will be executed.
                     Use # MOTO_TAGS: TAGNAME1 in test to assign tag.
                     Use ~ to filter tests that do not contain specific tag, e.g. ~tag

   -c, --config      Name of the config, without extension, to be loaded from MotoApp/config/CONFIG_NAME.rb
                     Default: moto (which loads: MotoApp/config/moto.rb)

   -l, --listeners   Reporters to be used.
                     Defaults are Moto::Reporting::Listeners::ConsoleDots, Moto::Reporting::Listeners::JunitXml
                     One reporter that is always used: Moto::Reporting::Listeners::KernelCode

   -p, --tagregexpos       Regex which will be matched against tags joined on ','.
                           Validation will pass if there is a match.
   -n, --tagregexneg       Regex which will be matched against tags joined on ','.
                           Validation will pass if there is no match.
   -h, --hastags           Validates if tests have #MOTO_TAGS with any tags.
   -d, --hasdescription    Validates if tests have #DESC with any text.
   -w, --tagwhitelist      Only tags from the whitelist will be allowed.
                           Provide in format: tag1,tag2,tag3 etc.

   --mwui-path           [MotoWebUI 2.0] Directory path (eg. /regression/areaname) to which results of the current run
                         should be aggregated.
                         Required when specifying MotoWebUI as one of the listeners.
   --mwui-assignee-id    [MotoWebUI 2.0] ID of a person responsible for current test run.
                         Can have a default value set in config/webui section.



  ==============
  MOTO GENERATE:
  ==============
   -t, --test        Path and name of the test to be created.
                     Examples:
                     -ttest_name          will create MotoApp/tests/test_name/test_name.rb
                     -tdir/test_name      will create MotoApp/tests/dir/test_name/test_name.rb
   -a, --appname     Name of the application. Will be also used as topmost module in test file.
                     Default: MotoApp
   -b, --baseclass   File, without extension, with base class from which test will derive. Assumes one class per file.
                     Examples:
                     -btest_base          will use the file in MotoApp/lib/test/test_base.rb
                     -bsubdir/test_base   will use the file in MotoApp/lib/test/subdir/test_base.rb
                     By default class will derive from Moto::Test
   -f, --force       Forces generator to overwrite previously existing class file in specified location.
                     You have been warned.
  " ""
end

.validate_parse(argv) ⇒ Object



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
# File 'lib/parameter_parser.rb', line 132

def self.validate_parse(argv)
  # Default options
  options = {}
  options[:listeners] = []

  # Parse arguments
  OptionParser.new do |opts|
    opts.on('-c', '--config Config')                    {|v| options[:config_name] = v}
    opts.on('-t', '--tests Tests', Array)               {|v| options[:tests] = v}
    opts.on('-g', '--tags Tags', Array)                 {|v| options[:tags] = v}
    opts.on('-f', '--filters Filters', Array)           {|v| options[:filters] = v}
    opts.on('-l', '--listeners Listeners', Array)       {|v| options[:listeners] = v}
    opts.on('--mwui-path MwuiPath')                     {|v| options[:mwui_path] = v}
    opts.on('--mwui-assignee-id MwuiAssignee')          {|v| options[:mwui_assignee_id] = v}
    opts.on('-p', '--tagregexpos RegexPositive')        {|v| options[:validator_regex_positive] = v}
    opts.on('-n', '--tagregexneg RegexNegative')        {|v| options[:validator_regex_negative] = v}
    opts.on('-h', '--hastags')                          {|v| options[:validate_has_tags] = v}
    opts.on('-d', '--hasdescription')                   {|v| options[:validate_has_description] = v}
    opts.on('-w', '--tagwhitelist TagWhitelist', Array) {|v| options[:tag_whitelist] = v}
  end.parse!

  self.prepare_config(options)

  if options[:tests]
    options[:tests].each do |path|
      path.sub!(%r{\/$}, '') # remove trailing "/"
    end
  end

  return options
end