Class: CucumberJunitToJson::App

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

Overview

The Cucumber Junit to Json app class that handles std inputs and command line args

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



22
23
24
25
26
27
28
# File 'lib/cucumber_junit_to_json/app.rb', line 22

def initialize(options = {})
  @stdin  = options[:stdin] || STDIN
  @stdout = options[:stdout] || STDOUT
  @stderr = options[:stderr] || STDERR
  @feature_id = 1
  @scenario_id = 1
end

Instance Attribute Details

#feature_parserObject (readonly)

Returns the value of attribute feature_parser.



30
31
32
# File 'lib/cucumber_junit_to_json/app.rb', line 30

def feature_parser
  @feature_parser
end

#junit_parserObject (readonly)

Returns the value of attribute junit_parser.



30
31
32
# File 'lib/cucumber_junit_to_json/app.rb', line 30

def junit_parser
  @junit_parser
end

#stderrObject (readonly)

Returns the value of attribute stderr.



30
31
32
# File 'lib/cucumber_junit_to_json/app.rb', line 30

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



30
31
32
# File 'lib/cucumber_junit_to_json/app.rb', line 30

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



30
31
32
# File 'lib/cucumber_junit_to_json/app.rb', line 30

def stdout
  @stdout
end

Instance Method Details

#run(*args) ⇒ Object



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
# File 'lib/cucumber_junit_to_json/app.rb', line 32

def run(*args)
  options = parse_args(args)
  @junit_parser = CucumberJunitToJson::Parsers::JunitParser.new(options.junit_dir)
  @feature_parser = CucumberJunitToJson::Parsers::FeatureParser.new(options.feature_dir)
  output_file = options.output_file || 'cucumber.json'
  features = []
  unless File.file?(output_file)
    output_directory = File.dirname(output_file)
    FileUtils.mkdir_p(output_directory) unless File.directory?(output_directory)
    FileUtils.touch(output_file)
    raise Error, "Could not create output file #{output_file}" unless File.file?(output_file)
  end
  # if we are dealing with a directory of xml files
  if File.directory?(junit_parser.path_to_junit)
    Find.find(junit_parser.path_to_junit) do |path_to_file|
      next unless File.file?(path_to_file) && File.extname(path_to_file) == '.xml'
      features.push(convert_to_json(path_to_file))
    end
    # if we are dealing with just a single xml file
  elsif File.exist?(junit_parser.path_to_junit)
    features.push(convert_to_json(junit_parser.path_to_junit))
  end
  open(output_file, 'w') { |f| f.write(features.to_json) }
  0
rescue Error, OptionParser::ParseError => error
  stderr.puts error.message
  1
end