~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to spec/watchr.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ENV["WATCHR"] = "1"
 
2
ENV['AUTOTEST'] = 'true'
 
3
 
 
4
def run_comp(cmd)
 
5
  puts cmd
 
6
  results = []
 
7
  old_sync = $stdout.sync
 
8
  $stdout.sync = true
 
9
  line = []
 
10
  begin
 
11
    open("| #{cmd}", "r") do |f|
 
12
      until f.eof? do
 
13
        c = f.getc
 
14
        putc c
 
15
        line << c
 
16
        if c == ?\n
 
17
          results << if RUBY_VERSION >= "1.9" then
 
18
                       line.join
 
19
                     else
 
20
                       line.pack "c*"
 
21
                     end
 
22
          line.clear
 
23
        end
 
24
      end
 
25
    end
 
26
  ensure
 
27
    $stdout.sync = old_sync
 
28
  end
 
29
  results.join
 
30
end
 
31
 
 
32
def clear
 
33
  #system("clear")
 
34
end
 
35
 
 
36
def growl(message, status)
 
37
  # Strip the color codes
 
38
  message.gsub!(/\[\d+m/, '')
 
39
 
 
40
  growlnotify = `which growlnotify`.chomp
 
41
  return if growlnotify.empty?
 
42
  title = "Watchr Test Results"
 
43
  image = status == :pass ? "autotest/images/pass.png" : "autotest/images/fail.png"
 
44
  options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
 
45
  system %(#{growlnotify} #{options} &)
 
46
end
 
47
 
 
48
def file2specs(file)
 
49
  %w{spec/unit spec/integration}.collect { |d|
 
50
    file.sub('lib/puppet', d).sub(".rb", "_spec.rb")
 
51
  }.find_all { |f|
 
52
    File.exist?(f)
 
53
  }
 
54
end
 
55
 
 
56
def file2test(file)
 
57
  result = file.sub('lib/puppet', 'test')
 
58
  return nil unless File.exist?(result)
 
59
  result
 
60
end
 
61
 
 
62
def run_spec(command)
 
63
  clear
 
64
  result = run_comp(command).split("\n").last
 
65
  status = result.include?('0 failures') ? :pass : :fail
 
66
  growl result, status
 
67
end
 
68
 
 
69
def run_test(command)
 
70
  clear
 
71
  result = run_comp(command).split("\n").last
 
72
  status = result.include?('0 failures, 0 errors') ? :pass : :fail
 
73
  growl result.split("\n").last rescue nil
 
74
end
 
75
 
 
76
def run_test_file(file)
 
77
  run_test(%Q(#{file}))
 
78
end
 
79
 
 
80
def run_spec_files(files)
 
81
  files = Array(files)
 
82
  return if files.empty?
 
83
  if File.exist?(File.expand_path("~/.rspec")) then
 
84
    opts = ''                   # use the user defaults
 
85
  else
 
86
    opts = File.readlines('spec/spec.opts').collect { |l| l.chomp }.join(" ")
 
87
  end
 
88
  begin
 
89
    run_spec("rspec #{opts} --tty #{files.join(' ')}")
 
90
  rescue => detail
 
91
    puts "Failed to load #{files}: #{detail}"
 
92
  end
 
93
end
 
94
 
 
95
def run_all_tests
 
96
  run_test("rake unit")
 
97
end
 
98
 
 
99
def run_all_specs
 
100
  run_spec_files "spec"
 
101
end
 
102
 
 
103
def run_suite
 
104
  run_all_specs
 
105
  run_all_tests
 
106
end
 
107
 
 
108
watch('spec/spec_helper.rb') { run_all_specs }
 
109
watch(%r{^spec/(unit|integration)/.*\.rb$}) { |md| run_spec_files(md[0]) }
 
110
watch(%r{^lib/puppet/(.*)\.rb$}) { |md|
 
111
  run_spec_files(file2specs(md[0]))
 
112
  if t = file2test(md[0])
 
113
    run_test_file(t)
 
114
  end
 
115
}
 
116
watch(%r{^spec/lib/spec.*}) { |md| run_all_specs }
 
117
watch(%r{^spec/lib/monkey_patches/.*}) { |md| run_all_specs }
 
118
watch(%r{test/.+\.rb}) { |md|
 
119
  if md[0] =~ /\/lib\//
 
120
    run_all_tests
 
121
  else
 
122
    run_test_file(md[0])
 
123
  end
 
124
}
 
125
 
 
126
# Ctrl-\
 
127
Signal.trap 'QUIT' do
 
128
  puts " --- Running all tests ---\n\n"
 
129
  run_suite
 
130
end
 
131
 
 
132
@interrupted = false
 
133
 
 
134
# Ctrl-C
 
135
Signal.trap 'INT' do
 
136
  if @interrupted
 
137
    @wants_to_quit = true
 
138
    abort("\n")
 
139
  else
 
140
    puts "Interrupt a second time to quit; wait for rerun of tests"
 
141
    @interrupted = true
 
142
    Kernel.sleep 1.5
 
143
    # raise Interrupt, nil # let the run loop catch it
 
144
    run_suite
 
145
  end
 
146
end