summaryrefslogtreecommitdiffstats
path: root/spec/watchr.rb
blob: c0f1d0257de34de59070c267c32729699b1be7b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
ENV["WATCHR"] = "1"
ENV['AUTOTEST'] = 'true'

def run_comp(cmd)
  puts cmd
  results = []
  old_sync = $stdout.sync
  $stdout.sync = true
  line = []
  begin
    open("| #{cmd}", "r") do |f|
      until f.eof? do
        c = f.getc
        putc c
        line << c
        if c == ?\n
          results << if RUBY_VERSION >= "1.9" then
                       line.join
                     else
                       line.pack "c*"
                     end
          line.clear
        end
      end
    end
  ensure
    $stdout.sync = old_sync
  end
  results.join
end

def clear
  #system("clear")
end

def growl(message, status)
  # Strip the color codes
  message.gsub!(/\[\d+m/, '')

  growlnotify = `which growlnotify`.chomp
  return if growlnotify.empty?
  title = "Watchr Test Results"
  image = status == :pass ? "autotest/images/pass.png" : "autotest/images/fail.png"
  options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
  system %(#{growlnotify} #{options} &)
end

def file2specs(file)
  %w{spec/unit spec/integration}.collect { |d|
    file.sub('lib/puppet', d).sub(".rb", "_spec.rb")
  }.find_all { |f|
    File.exist?(f)
  }
end

def file2test(file)
  result = file.sub('lib/puppet', 'test')
  return nil unless File.exist?(result)
  result
end

def run_spec(command)
  clear
  result = run_comp(command).split("\n").last
  status = result.include?('0 failures') ? :pass : :fail
  growl result, status
end

def run_test(command)
  clear
  result = run_comp(command).split("\n").last
  status = result.include?('0 failures, 0 errors') ? :pass : :fail
  growl result.split("\n").last rescue nil
end

def run_test_file(file)
  run_test(%Q(#{file}))
end

def run_spec_files(files)
  files = Array(files)
  return if files.empty?
  if File.exist?(File.expand_path("~/.rspec")) then
    opts = ''                   # use the user defaults
  else
    opts = File.readlines('spec/spec.opts').collect { |l| l.chomp }.join(" ")
  end
  begin
    run_spec("rspec #{opts} --tty #{files.join(' ')}")
  rescue => detail
    puts "Failed to load #{files}: #{detail}"
  end
end

def run_all_tests
  run_test("rake unit")
end

def run_all_specs
  run_spec_files "spec"
end

def run_suite
  run_all_specs
  run_all_tests
end

watch('spec/spec_helper.rb') { run_all_specs }
watch(%r{^spec/(unit|integration)/.*\.rb$}) { |md| run_spec_files(md[0]) }
watch(%r{^lib/puppet/(.*)\.rb$}) { |md|
  run_spec_files(file2specs(md[0]))
  if t = file2test(md[0])
    run_test_file(t)
  end
}
watch(%r{^spec/lib/spec.*}) { |md| run_all_specs }
watch(%r{^spec/lib/monkey_patches/.*}) { |md| run_all_specs }
watch(%r{test/.+\.rb}) { |md|
  if md[0] =~ /\/lib\//
    run_all_tests
  else
    run_test_file(md[0])
  end
}

# Ctrl-\
Signal.trap 'QUIT' do
  puts " --- Running all tests ---\n\n"
  run_suite
end

@interrupted = false

# Ctrl-C
Signal.trap 'INT' do
  if @interrupted
    @wants_to_quit = true
    abort("\n")
  else
    puts "Interrupt a second time to quit; wait for rerun of tests"
    @interrupted = true
    Kernel.sleep 1.5
    # raise Interrupt, nil # let the run loop catch it
    run_suite
  end
end