Sha256: 9b9c13e526f629ecb736cd9652fd9853d00144c7b0d2760bebff558a95970de3

Contents?: true

Size: 1.83 KB

Versions: 8

Compression:

Stored size: 1.83 KB

Contents

module LearnTest
  class Runner
    attr_reader :repo, :options
    SERVICE_URL = 'http://ironbroker-v2.flatironschool.com'

    def initialize(repo, options = {})
      @repo = repo
      @options = options
      die if !strategy
    end

    def run
      strategy.check_dependencies
      strategy.configure
      strategy.run
      if !help_option_present? && strategy.push_results?
        push_results(strategy)
      end
      strategy.cleanup unless keep_results?
    end

    def files
      @files ||= Dir.entries('.')
    end

    def keep_results?
      @keep_results ||= options[:keep] || !!options.delete('--keep')
    end

    def strategy
      @strategy ||= strategies.map{ |s| s.new(self) }.detect(&:detect)
    end

    private

    def connection
      @connection ||= Faraday.new(url: SERVICE_URL) do |faraday|
        faraday.adapter  Faraday.default_adapter
      end
    end

    def strategies
      [
        LearnTest::Strategies::Jasmine,
        LearnTest::Strategies::GreenOnion,
        LearnTest::Strategies::Rspec,
        LearnTest::Strategies::Karma,
        LearnTest::Strategies::Protractor,
        LearnTest::Strategies::JavaJunit,
        LearnTest::Strategies::Mocha,
        LearnTest::Strategies::PythonUnittest
      ]
    end

    def push_results(strategy)
      begin
        connection.post do |req|
          req.url(strategy.service_endpoint)
          req.headers['Content-Type'] = 'application/json'
          req.body = Oj.dump(strategy.results, mode: :compat)
        end
      rescue Faraday::ConnectionFailed
        puts 'There was a problem connecting to Learn. Not pushing test results.'.red
      end
    end

    def help_option_present?
      options.include?('-h') || options.include?('--help')
    end

    def die
      puts "This directory doesn't appear to have any specs in it."
      exit
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
learn-test-2.3.3 lib/learn_test/runner.rb
learn-test-2.3.2 lib/learn_test/runner.rb
learn-test-2.3.1 lib/learn_test/runner.rb
learn-test-2.3.0 lib/learn_test/runner.rb
learn-test-2.2.2 lib/learn_test/runner.rb
learn-test-2.2.1 lib/learn_test/runner.rb
learn-test-2.2.0 lib/learn_test/runner.rb
learn-test-2.1.2 lib/learn_test/runner.rb