class CucumberLint::Cli

A class that takes in the content that make up a cucumber feature and can determine if it is formatted and output the formatted content

Public Class Methods

execute(args) click to toggle source
# File lib/cucumber_lint/cli.rb, line 10
def self.execute args
  new(args).execute!
end
new(args, out: STDOUT) click to toggle source
# File lib/cucumber_lint/cli.rb, line 15
def initialize args, out: STDOUT
  @out = out

  opts = extract_args args
  @config = load_config fix: opts[:fix]
  @results = OpenStruct.new(total: 0, passed: 0, failed: 0, written: 0, deleted: 0, errors: [])
end

Public Instance Methods

execute!() click to toggle source
# File lib/cucumber_lint/cli.rb, line 24
def execute!
  Dir.glob('./features/**/*.feature').sort.each do |filename|
    lint_feature filename
  end

  output_results
  exit 1 unless @results.errors.empty?
end

Private Instance Methods

add_result(status, errors) click to toggle source
# File lib/cucumber_lint/cli.rb, line 37
def add_result status, errors
  @results.total += 1
  @results[status] += 1
  @results.errors += errors
  @out.print output_for_status(status)
end
extract_args(args) click to toggle source
# File lib/cucumber_lint/cli.rb, line 63
def extract_args args
  valid_args = ['--fix']

  if (args - valid_args).empty?
    { fix: args.count == 1 }
  else
    @out.puts "error: unsupported option(s): #{args.join(' ')}".red
    @out.puts 'usage: cucumber_lint'
    @out.puts '   or: cucumber_lint --fix'
    exit 1
  end
end
file_counts() click to toggle source
# File lib/cucumber_lint/cli.rb, line 77
def file_counts
  [:passed, :written, :deleted, :failed].map do |status|
    if status == :passed || @results[status] > 0
      "#{@results[status]} #{status}".colorize(output_color_for status)
    end
  end.compact.join(', ')
end
lint_feature(filename) click to toggle source
# File lib/cucumber_lint/cli.rb, line 45
def lint_feature filename
  linted_file = LintedFile.new filename

  linter = FeatureLinter.new config: @config, linted_file: linted_file
  linter.lint

  add_result linted_file.resolve, linted_file.errors
end
load_config(fix: Config.new dir: Pathname.new('.').realpath, fix: fix) click to toggle source
# File lib/cucumber_lint/cli.rb, line 55
def load_config fix:
  Config.new dir: Pathname.new('.').realpath, fix: fix
rescue UnsupportedStyle => e
  @out.puts e.message.red
  exit 1
end
output_color_for(status) click to toggle source
# File lib/cucumber_lint/cli.rb, line 105
def output_color_for status
  case status
  when :passed then :green
  when :failed then :red
  else :yellow
  end
end
output_counts() click to toggle source
# File lib/cucumber_lint/cli.rb, line 92
def output_counts
  @out.print "\n\n"
  @out.print "#{@results.total} file#{'s' if @results.total != 1} inspected"
  @out.print " (#{file_counts})" if @results.total > 0
  @out.print "\n"
end
output_errors() click to toggle source
# File lib/cucumber_lint/cli.rb, line 86
def output_errors
  @out.print "\n\n"
  @out.print @results.errors.join("\n").red
end
output_for_status(status) click to toggle source
# File lib/cucumber_lint/cli.rb, line 122
def output_for_status status
  output_letter_for(status).colorize output_color_for(status)
end
output_letter_for(status) click to toggle source
# File lib/cucumber_lint/cli.rb, line 113
def output_letter_for status
  case status
  when :passed then '.'
  when :failed then 'F'
  when :written then 'W'
  when :deleted then 'D'
  end
end
output_results() click to toggle source
# File lib/cucumber_lint/cli.rb, line 100
def output_results
  output_errors unless @results.errors.empty?
  output_counts
end