Method: RScout.run_suite

Defined in:
lib/rscout.rb

.run_suite(dir) ⇒ Object



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

def run_suite(dir)
  verbose = options[:verbose]
  gemfile = File.join(dir, 'Gemfile')
  configfile = File.join(dir, 'config', 'rscout.yml')

  Bundler.with_clean_env do
    Dir.chdir(dir) do
      yaml = Hashie::Mash.new(YAML.load_file configfile)
      config = yaml[env]

      output = Hashie::Mash.new({
        txt: StringIO.new,
        html: StringIO.new,
        json: StringIO.new,
        stdout: nil,
        results: nil,
        error: Hashie::Mash.new({backtrace:[], message:nil})
      })

      failed = false
      begin
        html_formatter = RSpec::Core::Formatters::HtmlFormatter.new output.html
        txt_formatter = RSpec::Core::Formatters::DocumentationFormatter.new output.txt
        json_formatter = RSpec::Core::Formatters::JsonFormatter.new output.json

        reporter = RSpec::Core::Reporter.new(json_formatter, txt_formatter, html_formatter)

        rspec = RSpec.configuration
        rspec.instance_variable_set(:@reporter, reporter)

        tests = Dir.glob File.join(dir, 'spec/**/*_spec.rb')

        rspec_task = lambda { RSpec::Core::Runner.run tests }

        if verbose
          rspec_task.call
        else
          output.stdout = capture_stdout &rspec_task
        end

        output.results = json_formatter.output_hash

        failed = output.results[:summary][:failure_count] > 0
        failure_count = output.results[:summary][:failure_count].to_s
      rescue => e
        failed = true
        logger.error "Exception encountered while running RSpec: #{e.message}"
        logger.error e.backtrace
        output.error = e
      ensure
        output.txt.close unless output.txt.closed?
        output.html.close unless output.html.closed?
        output.json.close unless output.json.closed?
      end

      if failed
        logger.info "Tests failed."
        send_failure_notifications config, env, output
      end

      failed
    end
  end
end