Class: RSpactor::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rspactor/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
16
17
# File 'lib/rspactor/runner.rb', line 12

def initialize(dir, options = {})
  @dir = dir
  @options = options
  @rspec = File.exists?(File.join(dir, 'spec'))
  read_git_head
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



10
11
12
# File 'lib/rspactor/runner.rb', line 10

def dir
  @dir
end

#inspectorObject (readonly)

Returns the value of attribute inspector.



10
11
12
# File 'lib/rspactor/runner.rb', line 10

def inspector
  @inspector
end

#interactorObject (readonly)

Returns the value of attribute interactor.



10
11
12
# File 'lib/rspactor/runner.rb', line 10

def interactor
  @interactor
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/rspactor/runner.rb', line 10

def options
  @options
end

#rspecObject (readonly)

Returns the value of attribute rspec.



10
11
12
# File 'lib/rspactor/runner.rb', line 10

def rspec
  @rspec
end

Class Method Details

.start(options = {}) ⇒ Object



5
6
7
8
# File 'lib/rspactor/runner.rb', line 5

def self.start(options = {})
  run_in = options.delete(:run_in) || Dir.pwd
  new(run_in, options).start
end

Instance Method Details

#last_run_failed?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rspactor/runner.rb', line 86

def last_run_failed?
  @last_run_failed == false
end

#load_dotfileObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/rspactor/runner.rb', line 42

def load_dotfile
  dotfile = File.join(ENV['HOME'], '.rspactor')
  if File.exists?(dotfile)
    begin
      Kernel.load dotfile
    rescue => e
      $stderr.puts "Error while loading #{dotfile}: #{e}"
    end
  end
end

#run_all_testsObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspactor/runner.rb', line 53

def run_all_tests
  if rspec?
    run_spec_command(File.join(dir, 'spec'))
  else
    patterns = ["test/unit/**/*_test.rb", "test/lib/**/*_test.rb", 
      "test/functional/**/*_test.rb", "test/integration/**/*_test.rb"]
    
    patterns.each do |pattern|
      run_test_command(Dir[pattern])
    end
  end
end

#run_spec_command(paths) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/rspactor/runner.rb', line 66

def run_spec_command(paths)
  paths = Array(paths)
  if paths.empty?
    @last_run_failed = nil
  else
    cmd = [ruby_opts, spec_runner, paths, spec_opts].flatten.join(' ')
    @last_run_failed = run_command(cmd)
  end
end

#run_test_command(paths) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/rspactor/runner.rb', line 76

def run_test_command(paths)
  paths = Array(paths)
  if paths.empty?
    @last_run_failed = nil
  else
    cmd = [ruby_opts, test_runner, paths].flatten.join(' ')
    @last_run_failed = run_command(cmd)
  end
end

#startObject



19
20
21
22
23
24
# File 'lib/rspactor/runner.rb', line 19

def start
  load_dotfile
  puts "** RSpactor is now watching at '#{dir}'"
  start_interactor
  start_listener
end

#start_interactorObject



26
27
28
29
30
31
# File 'lib/rspactor/runner.rb', line 26

def start_interactor
  @interactor = Interactor.new
  aborted = @interactor.wait_for_enter_key("** Hit <enter> to skip initial spec run", 3)
  @interactor.start_termination_handler
  run_all_tests unless aborted
end

#start_listenerObject



33
34
35
36
37
38
39
40
# File 'lib/rspactor/runner.rb', line 33

def start_listener
  @inspector_class = rspec? ? RSpecInspector : TestInspector
  @inspector = @inspector_class.new(dir)

  Listener.new(@inspector_class::EXTENSIONS) do |files|
    test_changed_files(files) unless git_head_changed?
  end.run(dir)
end