Class: Cucumber::Cli::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/cli/configuration.rb

Constant Summary collapse

FORMATS =
%w{pretty profile progress rerun}
DEFAULT_FORMAT =
'pretty'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_stream = STDOUT, error_stream = STDERR) ⇒ Configuration

Returns a new instance of Configuration.



12
13
14
15
16
17
18
19
# File 'lib/cucumber/cli/configuration.rb', line 12

def initialize(out_stream = STDOUT, error_stream = STDERR)
  @out_stream   = out_stream
  @error_stream = error_stream

  @paths          = []
  @options        = default_options
  @active_format  = DEFAULT_FORMAT
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/cucumber/cli/configuration.rb', line 10

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



9
10
11
# File 'lib/cucumber/cli/configuration.rb', line 9

def paths
  @paths
end

Instance Method Details

#build_formatter_broadcaster(step_mother) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cucumber/cli/configuration.rb', line 195

def build_formatter_broadcaster(step_mother)
  return Formatter::Pretty.new(step_mother, nil, @options) if @options[:autoformat]
  formatters = @options[:formats].map do |format, out|
    if String === out # file name
      out = File.open(out, Cucumber.file_mode('w'))
      at_exit do
        out.flush
        out.close
      end
    end
  
    begin
      formatter_class = formatter_class(format)
      formatter_class.new(step_mother, out, @options)
    rescue Exception => e
      exit_with_error("Error creating formatter: #{format}", e)
    end
  end

  broadcaster = Broadcaster.new(formatters)
  broadcaster.options = @options
  return broadcaster
end

#diff_enabled?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/cucumber/cli/configuration.rb', line 172

def diff_enabled?
  @options[:diff_enabled]
end

#feature_filesObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/cucumber/cli/configuration.rb', line 245

def feature_files
  potential_feature_files = @paths.map do |path|
    path = path.gsub(/\\/, '/') # In case we're on windows. Globs don't work with backslashes.
    path = path.chomp('/')
    File.directory?(path) ? Dir["#{path}/**/*.feature"] : path
  end.flatten.uniq

  @options[:excludes].each do |exclude|
    potential_feature_files.reject! do |path|
      path =~ /#{Regexp.escape(exclude)}/
    end
  end

  potential_feature_files
end

#files_to_requireObject



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cucumber/cli/configuration.rb', line 232

def files_to_require
  requires = @options[:require] || feature_dirs
  files = requires.map do |path|
    path = path.gsub(/\\/, '/') # In case we're on windows. Globs don't work with backslashes.
    File.directory?(path) ? Dir["#{path}/**/*.rb"] : path
  end.flatten.uniq
  sorted_files = files.sort { |a,b| (b =~ %r{/support/} || -1) <=>  (a =~ %r{/support/} || -1) }.reject{|f| f =~ /^http/}
  env_files = sorted_files.select {|f| f =~ %r{/support/env.rb} }
  files = env_files + sorted_files.reject {|f| f =~ %r{/support/env.rb} }
  files.reject! {|f| f =~ %r{/support/env.rb} } if @options[:dry_run]
  files
end

#formatter_class(format) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cucumber/cli/configuration.rb', line 219

def formatter_class(format)
  case format
    when 'html'     then Formatter::Html
    when 'pretty'   then Formatter::Pretty
    when 'profile'  then Formatter::Profile
    when 'progress' then Formatter::Progress
    when 'rerun'    then Formatter::Rerun
    when 'usage'    then Formatter::Usage
  else
    constantize(format)
  end
end

#guess?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/cucumber/cli/configuration.rb', line 168

def guess?
  @options[:guess]
end

#load_languageObject



176
177
178
179
180
181
182
# File 'lib/cucumber/cli/configuration.rb', line 176

def load_language
  if Cucumber.language_incomplete?(@options[:lang])
    list_keywords_and_exit(@options[:lang])
  else
    Cucumber.load_language(@options[:lang])
  end
end

#parse!(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
151
152
153
154
155
156
157
158
# File 'lib/cucumber/cli/configuration.rb', line 21

def parse!(args)
  @args = args
  return parse_args_from_profile('default') if @args.empty?
  @args.extend(::OptionParser::Arguable)

  @args.options do |opts|
    opts.banner = ["Usage: cucumber [options] [ [FILE|DIR|URL][:LINE[:LINE]*] ]+", "",
      "Examples:",
      "cucumber examples/i18n/en/features",
      "cucumber --language it examples/i18n/it/features/somma.feature:6:98:113",
      "cucumber -n -i http://rubyurl.com/eeCl", "", "",
    ].join("\n")
    opts.on("-r LIBRARY|DIR", "--require LIBRARY|DIR", 
      "Require files before executing the features. If this",
      "option is not specified, all *.rb files that are",
      "siblings or below the features will be loaded auto-",
      "matically. Automatic loading is disabled when this",
      "option is specified, and all loading becomes explicit.",
      "Files under directories named \"support\" are always",
      "loaded first.",
      "This option can be specified multiple times.") do |v|
      @options[:require] ||= []
      @options[:require] << v
    end
    opts.on("-l LANG", "--language LANG", 
      "Specify language for features (Default: #{@options[:lang]})",
      %{Run with "--language help" to see all languages},
      %{Run with "--language LANG help" to list keywords for LANG}) do |v|
      if v == 'help'
        list_languages
      elsif args==['help']
        list_keywords_and_exit(v)
      else
        @options[:lang] = v
      end
    end
    opts.on("-f FORMAT", "--format FORMAT", 
      "How to format features (Default: #{DEFAULT_FORMAT})",
      "Available formats: #{FORMATS.join(", ")}",
      "You can also provide your own formatter classes as long",
      "as they have been previously required using --require or",
      "if they are in the folder structure such that cucumber",
      "will require them automatically.", 
      "This option can be specified multiple times.") do |v|
      @options[:formats][v] = @out_stream
      @active_format = v
    end
    opts.on("-o", "--out FILE", 
      "Write output to a file instead of STDOUT. This option",
      "applies to the previously specified --format, or the",
      "default format if no format is specified.") do |v|
      @options[:formats][@active_format] = v
    end
    opts.on("-t TAGS", "--tags TAGS", 
      "Only execute the features or scenarios with the specified tags.",
      "TAGS must be comma-separated without spaces. Prefix tags with ~ to",
      "exclude features or scenarios having that tag. Tags can be specified",
      "with or without the @ prefix.") do |v|
      @options[:include_tags], @options[:exclude_tags] = *parse_tags(v)
    end
    opts.on("-s SCENARIO", "--scenario SCENARIO", 
      "Only execute the scenario with the given name. If this option",
      "is given more than once, run all the specified scenarios.") do |v|
      @options[:scenario_names] << v
    end
    opts.on("-e", "--exclude PATTERN", "Don't run feature files matching PATTERN") do |v|
      @options[:excludes] << v
    end
    opts.on("-p", "--profile PROFILE", "Pull commandline arguments from cucumber.yml.") do |v|
      parse_args_from_profile(v)
    end
    opts.on("-c", "--[no-]color",
      "Whether or not to use ANSI color in the output. Cucumber decides",
      "based on your platform and the output destination if not specified.") do |v|
      Term::ANSIColor.coloring = v
    end
    opts.on("-d", "--dry-run", "Invokes formatters without executing the steps.",
      "This also omits the loading of your support/env.rb file if it exists.",
      "Implies --quiet.") do
      @options[:dry_run] = true
      @quiet = true
    end
    opts.on("-a", "--autoformat DIRECTORY", 
      "Reformats (pretty prints) feature files and write them to DIRECTORY.",
      "Be careful if you choose to overwrite the originals.",
      "Implies --dry-run --formatter pretty.") do |directory|
      @options[:autoformat] = directory
      Term::ANSIColor.coloring = false
      @options[:dry_run] = true
      @quiet = true
    end
    opts.on("-m", "--no-multiline", 
      "Don't print multiline strings and tables under steps.") do
      @options[:no_multiline] = true
    end
    opts.on("-n", "--no-source", 
      "Don't print the file and line of the step definition with the steps.") do
      @options[:source] = false
    end
    opts.on("-i", "--no-snippets", "Don't print snippets for pending steps.") do
      @options[:snippets] = false
    end
    opts.on("-q", "--quiet", "Alias for --no-snippets --no-source.") do
      @quiet = true
    end
    opts.on("-b", "--backtrace", "Show full backtrace for all errors.") do
      Exception.cucumber_full_backtrace = true
    end
    opts.on("-S", "--strict", "Fail if there are any undefined steps.") do
      @options[:strict] = true
    end
    opts.on("-v", "--verbose", "Show the files and features loaded.") do
      @options[:verbose] = true
    end
    opts.on("-g", "--guess", "Guess best match for Ambiguous steps.") do
      @options[:guess] = true
    end
    opts.on("--no-diff", "Disable diff output on failing expectations.") do
      @options[:diff_enabled] = false
    end
    opts.on_tail("--version", "Show version.") do
      @out_stream.puts VERSION::STRING
      Kernel.exit
    end
    opts.on_tail("-h", "--help", "You're looking at it.") do
      @out_stream.puts opts.help
      Kernel.exit
    end
  end.parse!

  @options[:formats]['pretty'] = @out_stream if @options[:formats].empty?

  @options[:snippets] = true if !@quiet && @options[:snippets].nil?
  @options[:source]   = true if !@quiet && @options[:source].nil?

  # Whatever is left after option parsing is the FILE arguments
  @paths += args
end

#parse_tags(tag_string) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/cucumber/cli/configuration.rb', line 184

def parse_tags(tag_string)
  tag_names = tag_string.split(",")
  excludes, includes = tag_names.partition{|tag| tag =~ /^~/}
  excludes = excludes.map{|tag| tag[1..-1]}

  # Strip @
  includes = includes.map{|tag| tag =~ /^@(.*)/ ? $1 : tag}
  excludes = excludes.map{|tag| tag =~ /^@(.*)/ ? $1 : tag}
  [includes, excludes]
end

#strict?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/cucumber/cli/configuration.rb', line 164

def strict?
  @options[:strict]
end

#verbose?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/cucumber/cli/configuration.rb', line 160

def verbose?
  @options[:verbose]
end