Skip to content

Instantly share code, notes, and snippets.

@dylanvaughn
Created June 1, 2012 21:18
Show Gist options
  • Save dylanvaughn/2855191 to your computer and use it in GitHub Desktop.
Save dylanvaughn/2855191 to your computer and use it in GitHub Desktop.
watchr config file for Python based apps (Pyramid in my case). Simulates behavior of autotest in Ruby.
require 'rbconfig'
GREEN = 'green' unless defined?(GREEN)
RED = 'red' unless defined?(RED)
def set_state(condition)
%x[echo #{condition} > ./.build_state]
end
def state_is?(condition)
current_state == condition
end
def current_state
%x[cat ./.build_state].chomp
end
def run_and_notify(cmd)
output = %x[#{cmd}]
if output =~ /FAILED \(errors=(\d+)\)/
set_state(RED) if current_state == GREEN
subject = "#{$1} FAILED TEST#{"S" unless $1 == 1}!"
errors = output.scan(/^(ERROR: .*)$/)
desc = "\n#{errors.join("\n")}"
send_message(subject, desc, 'critical')
print output.gsub(/^(writing|reading|running).*\n/, '')
else
set_state(GREEN) if current_state == RED
end
end
def send_message(subject, desc='', priority='normal')
os = RbConfig::CONFIG['host_os']
if os =~ /linux/
%x[notify-send --urgency=#{priority} '#{subject}' '#{desc}']
elsif os =~ /mac|darwin/
# growl support here
else
print "#{priority} : #{msg}"
end
end
def sanity_check(file)
beginning_state = current_state
run_test(file)
if beginning_state == RED and current_state == GREEN
send_message "#{file} all green! Running all tests..."
run_all_tests
if current_state == GREEN
send_message 'All fixed!', 'Good work!'
end
end
end
def run_all_tests
print "Running all tests..."
$stdout.flush
run_and_notify("python setup.py test -q 2>&1")
puts "done"
end
def run_test(file)
mod = file.gsub(/\//, '.').sub(/\.py$/, '')
unless File.exist?(file)
puts "ERROR: #{file} does not exist."
return
end
print "Running #{file}..."
$stdout.flush
run_and_notify "python setup.py test -s #{mod} -q 2>&1"
puts "done."
end
watch("run-all-tests.txt") do |match|
run_all_tests
end
# Put your watch statements below
# Here is an example
watch("properflow/tests/.*\.py$") do |match|
sanity_check match[0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment