Module: Cukesparse

Defined in:
lib/cukesparse.rb,
lib/cukesparse/version.rb

Constant Summary collapse

VERSION =
"2.1.7"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/cukesparse.rb', line 7

def command
  @command
end

.configObject

Returns the value of attribute config.



7
8
9
# File 'lib/cukesparse.rb', line 7

def config
  @config
end

.config_fileObject

Returns current config file



10
11
12
# File 'lib/cukesparse.rb', line 10

def config_file
  @config_file
end

.parametersObject

Returns the value of attribute parameters.



7
8
9
# File 'lib/cukesparse.rb', line 7

def parameters
  @parameters
end

.taskObject

Returns the value of attribute task.



7
8
9
# File 'lib/cukesparse.rb', line 7

def task
  @task
end

Class Method Details

.add_multiple(key, val) ⇒ Object

Add multiple options to key

Parameters:

  • key (Symbol)

    the key to store in options

  • val (String)

    the arguments passed in for key



23
24
25
26
27
28
29
# File 'lib/cukesparse.rb', line 23

def add_multiple key, val
  return (@parameters[key] ||= []).push '--' + key + ' ' + val unless val.is_a? Array

  val.each do |v|
    (@parameters[key] ||= []).push '--' + key + ' ' + v
  end
end

.argvObject

Returns current argv



15
16
17
# File 'lib/cukesparse.rb', line 15

def argv
  ARGV
end

.build_commandObject

Builds the command line string



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

def build_command
  unless @task.empty? && @parameters.empty?
    @command.push 'bundle exec cucumber'
    @command.push '--require features/'
    @command.push task['feature_order'].join(' ') if task.has_key? 'feature_order'
    @parameters.each { |k,v| @command.push(v) }
    @command.push task['defaults'].join(' ')  if task.has_key? 'defaults'
  end

  return debug if @parameters.has_key? 'debug'
  return system(@command.join(' '))
end

.check_for_parametersObject

Checks parameters and returns boolean



70
71
72
# File 'lib/cukesparse.rb', line 70

def check_for_parameters
  return puts 'WARN: No parameters passed to cukesparse'.yellow unless @parameters.any?
end

.check_for_taskObject

Checks for task in arguments



60
61
62
63
64
65
66
67
# File 'lib/cukesparse.rb', line 60

def check_for_task
  return abort 'ERROR: Your tasks.yml file is empty!'.red.underline unless @config
  task = argv & @config.keys

  return abort 'ERROR: No task was passed to cukesparse!'.red.underline if task.empty?
  return puts 'WARN: Multiple tasks have been passed!'.yellow if task.length > 1
  return @task = @config[task[0]]
end

.debugObject

Outputs the debug information



75
76
77
78
79
80
81
82
83
84
# File 'lib/cukesparse.rb', line 75

def debug
  puts 'DEBUG: Outputting ARGV passed'.yellow
  puts argv.inspect
  puts 'DEBUG: Outputting parsed config file'.yellow
  puts @config.inspect
  puts 'DEBUG: Outputting parameters created'.yellow
  puts @parameters.inspect
  puts 'DEBUG: Outputting command created'.yellow
  puts @command.join(' ')
end

.executeObject

Executes cukesparse by checking arguments passed



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cukesparse.rb', line 32

def execute
  load_config
  return puts 'Cukesparse - a simple command line parser to pass arguments into Cucumber!'.yellow if argv.empty?
  return puts "You have the following tasks within your config file: #{@config.keys.join(', ')}".yellow if argv.dup.shift == 'tasks'

  parse_argv
  check_for_task
  check_for_parameters
  set_cucumber_defaults
  set_runtime_defaults
  build_command
end

.load_configObject

Loads the config file



87
88
89
90
91
92
93
# File 'lib/cukesparse.rb', line 87

def load_config
  @config = YAML.load_file config_file
rescue Psych::SyntaxError
  abort 'Your tasks file did not parse as expected!'.red.underline
rescue Errno::ENOENT
  abort 'Your tasks file is missing!'.red.underline
end

.parse_argvObject

Parses the options passed via command line



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

def parse_argv
  cli argv,
  # Cucumber options
  '-t'            => lambda{ |t| add_multiple('tags', t) },
  '-n --name'     => lambda{ |n| add_multiple('name', n) },
  '-f --format'   => ->(f){ @parameters['format']    = "--format #{f}" },
  '-d --dry-run'  => ->{ @parameters['dry_run']      = "--dry-run" },
  '-v --verbose'  => ->{ @parameters['verbose']      = "--verbose" },
  '-s --strict'   => ->{ @parameters['strict']       = "--strict" },
  '-g --guess'    => ->{ @parameters['guess']        = "--guess" },
  '-x --expand'   => ->{ @parameters['expand']       = "--expand" },

  # All options below have been added for custom project but can be used for example
  # Global options
  '-e --environment'  => ->(e){ @parameters['environment'] = "ENVIRONMENT=#{e}" },
  '-l --loglevel'     => ->(l){ @parameters['log_level']   = "LOG_LEVEL=#{l}" },
  '-c --controller'   => ->(c){ @parameters['controller']  = "CONTROLLER=#{c}" },
  '-h --headless'     => ->{ @parameters['headless']       = "HEADLESS=TRUE" },

  # Database options
  '--cleanup'         => ->{ @parameters['cleanup']    = "CLEANUP=TRUE" },
  '--no-cleanup'      => ->{ @parameters['cleanup']    = "CLEANUP=FALSE" },
  '--database'        => ->{ @parameters['database']   = "DATABASE=TRUE" },
  '--jenkins'         => ->{ @parameters['jenkins']    = "JENKINS=TRUE" },

  # Retry options
  '--retries'         => ->(r){ @parameters['retries'] = "RETRIES=#{r}" },
  '--timeout'         => ->(t){ @parameters['timeout'] = "TIMEOUT=#{t}" },

  # Driver Options
  '--screen'          => ->(s){ split_parameters(s, 'screen') },
  '--position'        => ->(p){ split_parameters(p, 'position') },
  '--screenwidth'     => ->(w){ @parameters['screen_width']  = "SCREENWIDTH=#{w}" },
  '--screenheight'    => ->(h){ @parameters['screen_height'] = "SCREENHEIGHT=#{h}" },
  '--xposition'       => ->(x){ @parameters['xposition']     = "XPOSITION=#{x}" },
  '--yposition'       => ->(y){ @parameters['yposition']     = "YPOSITION=#{y}" },
  '-H --highlight'    => ->{ @parameters['highlight']        = "HIGHLIGHT=TRUE" },

  # Debug
  '--debug' => ->{ @parameters['debug']  = "DEBUG=TRUE" }
rescue
  abort 'Error processing passed CLI arguments!'.red.underline
end

.reset!Object

Resets cukesparse



205
206
207
208
209
210
# File 'lib/cukesparse.rb', line 205

def reset!
  @config     = {}
  @parameters = {}
  @task       = {}
  @command    = []
end

.set_cucumber_defaultsObject

Updates parameters based on config cucumber defaults



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cukesparse.rb', line 157

def set_cucumber_defaults
  return puts 'WARN: The task has no cucumber defaults!'.yellow unless @task.has_key? 'cucumber_defaults'

  @task['cucumber_defaults'].each do |key, val|
    case key
    when 'tags', 'name'
      add_multiple key, val
    when 'format'
      unless @parameters.has_key? key
        @parameters[key] = '--' + key.downcase + ' ' + val.to_s
      end
    when 'strict', 'verbose', 'guess', 'expand'
      unless @parameters.has_key? key && @parameters[key] == true
        @parameters[key] = '--' + key.downcase
      end
    when 'dry_run'
      unless @parameters.has_key? key && @parameters[key] == true
        @parameters[key] = '--dry-run'
      end
    else
     puts "WARN: The cucumber default #{key} isn't a known option!".yellow
    end
  end
end

.set_runtime_defaultsObject

Updates parameters based on config runtime defaults



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cukesparse.rb', line 141

def set_runtime_defaults
  return puts 'WARN: The task has no runtime defaults!'.yellow unless @task.has_key? 'runtime_defaults'

  @task['runtime_defaults'].each do |key, val|
    case key
    when 'screen', 'position'
      split_parameters(val, key)
    else
      unless @parameters.has_key? key
        @parameters[key] = key.upcase + '=' + val.to_s
      end
    end
  end
end

.split_parameters(params, sym) ⇒ Object

Splits parameters passed

Parameters:

  • params (String)

    the parameters passed

  • sym (Symbol)

    the symbol passed



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cukesparse.rb', line 186

def split_parameters params, sym
  params = params.split '/'
  if params.length == 1
    abort "ERROR: You have not passed enough parameters in the #{sym.to_s} command line argument!".red.underline
  elsif params.length > 2
    abort "ERROR: You have passed to many parameters in the #{sym.to_s} command line argument!".red.underline
  end

  case sym
  when 'screen'
    @parameters['screenwidth']  = "SCREENWIDTH=#{params[0]}"
    @parameters['screenheight'] = "SCREENHEIGHT=#{params[1]}"
  when 'position'
    @parameters['xposition'] = "XPOSITION=#{params[0]}"
    @parameters['yposition'] = "YPOSITION=#{params[1]}"
  end
end