Class: Testowl::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/testowl/monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Monitor

Returns a new instance of Monitor.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/testowl/monitor.rb', line 6

def initialize(options)
  @options = options
  if File.exist?("spec/spec_helper.rb")
    @runner = RspecRunner.new
    @test_dir = "spec"
    @test_suffix = "spec"
    puts "TestOwl #{VERSION} has detected RSpec"
  elsif File.exist?("test/test_helper.rb")
    @runner = TestUnitRunner.new
    @test_dir = "test"
    @test_suffix = "test"
    puts "TestOwl #{VERSION} has detected Test Unit"
  else
    raise "Can't find either spec_helper.rb or test_helper.rb and this owl is confused."
  end
end

Instance Attribute Details

#test_dirObject (readonly)

Returns the value of attribute test_dir.



4
5
6
# File 'lib/testowl/monitor.rb', line 4

def test_dir
  @test_dir
end

#test_suffixObject (readonly)

Returns the value of attribute test_suffix.



4
5
6
# File 'lib/testowl/monitor.rb', line 4

def test_suffix
  @test_suffix
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/testowl/monitor.rb', line 23

def run
  script = Watchr::Script.new
  # Watch the scripts themselves
  script.watch("#{test_dir}/.*/*_#{test_suffix}\.rb") do |match|
    system("clear")
    puts "Detected change in #{match[0]}"
    run_test(match[0])
  end
  # Watch models
  script.watch("app/models/(.*)\.rb") do |match|
    if @options[:just_tests]
      puts "Ignoring change in #{match[0]}"
    else
      system("clear")
      puts "Detected change in #{match[0]}"
      run_model(match[1], "triggered by #{match[0]}")
    end
  end
  # Watch controllers
  script.watch("app/controllers/(.*)_controller\.rb") do |match|
    if @options[:just_tests]
      puts "Ignoring change in #{match[0]}"
    else
      system("clear")
      puts "Detected change in #{match[0]}"
      run_controller(match[1], "triggered by #{match[0]}")
    end
  end
  puts "Waiting for code changes..."
  Watchr::Controller.new(script, Watchr.handler.new).run
end

#run_controller(controller_name, reason) ⇒ Object



68
69
70
71
72
# File 'lib/testowl/monitor.rb', line 68

def run_controller(controller_name, reason)
  tester = Tester.new(@runner, reason)
  tester.add Dir["#{test_dir}/**/#{controller_name}_controller_#{test_suffix}.rb"]
  tester.run
end

#run_model(model_name, reason) ⇒ Object



61
62
63
64
65
66
# File 'lib/testowl/monitor.rb', line 61

def run_model(model_name, reason)
  tester = Tester.new(@runner, reason)
  tester.add Dir["#{test_dir}/**/#{model_name}_#{test_suffix}.rb"]
  tester.add Dir["#{test_dir}/**/#{model_name.pluralize}_controller_#{test_suffix}.rb"]
  tester.run
end

#run_test(file) ⇒ Object



55
56
57
58
59
# File 'lib/testowl/monitor.rb', line 55

def run_test(file)
  tester = Tester.new(@runner, "has been updated")
  tester.add file
  tester.run
end

#tests_for_model(model) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/testowl/monitor.rb', line 74

def tests_for_model(model)
  tests = []
  tests += Dir["#{test_dir}/**/#{model.pluralize}_controller_#{test_suffix}.rb"]
  tests += Dir["#{test_dir}/**/#{model}_#{test_suffix}.rb"]
  class_name = model.classify
  count = 0
  `grep #{class_name} -R app/controllers/* | grep '# Dependencies'`.lines.each do |line|
    tests += Dir[line.split(':').first.sub(/^app/, test_suffix).sub(/\.rb$/, "_#{test_suffix}.rb")]
    count += 1
  end
  puts "Found 1 dependency" if count == 1
  puts "Found #{count} dependencies" if count > 1
  tests
end