Class: QUnited::Driver::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/qunited/driver/base.rb

Direct Known Subclasses

PhantomJs, Rhino

Constant Summary collapse

SUPPORT_DIR =

Path of the common (to all drivers) supporting files directory

File.expand_path('../support', __FILE__)
TEST_RESULT_START_TOKEN =
'QUNITED_TEST_RESULT_START_TOKEN'
TEST_RESULT_END_TOKEN =
'QUNITED_TEST_RESULT_END_TOKEN'
TEST_RESULT_REGEX =
/#{TEST_RESULT_START_TOKEN}(.*?)#{TEST_RESULT_END_TOKEN}/m
COFFEESCRIPT_EXTENSIONS =
['coffee', 'cs']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_files, test_files) ⇒ Base

Initialize the driver with source and test files. The files can be described either with glob patterns or arrays of file names.



38
39
40
41
42
# File 'lib/qunited/driver/base.rb', line 38

def initialize(source_files, test_files)
  @source_files = normalize_files source_files
  @test_files = normalize_files test_files
  @fixture_files = []
end

Instance Attribute Details

#fixture_filesObject

Returns the value of attribute fixture_files.



16
17
18
# File 'lib/qunited/driver/base.rb', line 16

def fixture_files
  @fixture_files
end

#formatterObject

Returns the value of attribute formatter.



16
17
18
# File 'lib/qunited/driver/base.rb', line 16

def formatter
  @formatter
end

#resultsObject (readonly)

Returns the value of attribute results.



15
16
17
# File 'lib/qunited/driver/base.rb', line 15

def results
  @results
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



15
16
17
# File 'lib/qunited/driver/base.rb', line 15

def source_files
  @source_files
end

#test_filesObject (readonly)

Returns the value of attribute test_files.



15
16
17
# File 'lib/qunited/driver/base.rb', line 15

def test_files
  @test_files
end

Class Method Details

.available?Boolean

Overridden in subclasses to return true if the underlying library is installed

Returns:

  • (Boolean)


32
33
34
# File 'lib/qunited/driver/base.rb', line 32

def self.available?
  false
end

.which(cmd) ⇒ Object

Finds an executable on the PATH. Returns the absolute path of the executable if found, otherwise nil.



20
21
22
23
24
25
26
27
28
29
# File 'lib/qunited/driver/base.rb', line 20

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    end
  end
  return nil
end

Instance Method Details

#commandObject



44
45
46
# File 'lib/qunited/driver/base.rb', line 44

def command
  raise 'not implemented'
end

#nameObject



92
93
94
# File 'lib/qunited/driver/base.rb', line 92

def name
  self.class.name.split('::')[-1]
end

#runObject



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
# File 'lib/qunited/driver/base.rb', line 48

def run
  @tests_file = Tempfile.new(['tests_page', '.html'])
  @tests_file.write(tests_page_content)
  @tests_file.close

  send_to_formatter(:start)

  @results = []

  Open3.popen3(command) do |stdin, stdout, stderr|
    results_collector = ResultsCollector.new(stdout)

    results_collector.on_test_result do |result|
      @results << result
      method = result.passed? ? :test_passed : :test_failed
      send_to_formatter(method, result)
    end

    results_collector.on_non_test_result_line do |line|
      # Drivers sometimes print error messages to stdout. If we are not reading
      # a test result then redirect any output to stderr
      $stderr.puts(line)
    end

    results_collector.collect_results

    err = stderr.read
    $stderr.puts(err) if err && !err.empty?
  end

  send_to_formatter(:stop)
  send_to_formatter(:summarize)

  @results
end

#support_file_contents(filename) ⇒ Object



88
89
90
# File 'lib/qunited/driver/base.rb', line 88

def support_file_contents(filename)
  IO.read(support_file_path(filename))
end

#support_file_path(filename) ⇒ Object



84
85
86
# File 'lib/qunited/driver/base.rb', line 84

def support_file_path(filename)
  File.join(SUPPORT_DIR, filename)
end