Skip to content

Instantly share code, notes, and snippets.

@CNBorn
Created September 12, 2011 03:29
Show Gist options
  • Save CNBorn/1210520 to your computer and use it in GitHub Desktop.
Save CNBorn/1210520 to your computer and use it in GitHub Desktop.
Ruby Testboy
#!/usr/local/bin/ruby
require 'net/smtp'
##########################################
#Ruby Testboy - Runs Django Test for you
#
# If there are any unitest failed, testboy will send out the error messages immediately
# to the recipients you set below.
#
#SETUP:
# 1) put your project path/virtualenv path below.
# 2) put the app name with which you want to test against in 'djangoapp'
# 3) put the recipient mails in 'recipients'
# 4) set up a cron to run this script and write the output information to
# 'the log' filename
# 5) Done!
#
#########################################
#http://snippets.dzone.com/posts/show/2362#
def send_email(from, from_alias, to, to_alias, subject, message)
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
#{message}
END_OF_MESSAGE
Net::SMTP.start('localhost') do |smtp|
smtp.send_message msg, from, to
end
end
begin
## SETUP
virtualenv_path = "/usr/local/python-env/djangoproj-deploy/"
python_bin = virtualenv_path + "bin/python "
python_virtualenv_bin = virtualenv_path + "bin/activate_this.py"
djangoproject_path = "/usr/local/djangoproj-deploy/"
djangoproject_cmd_test = djangoproject_path + "manage.py test --pythonpath=/usr/local/python-env/djangoproj-deploy/lib/python2.5/site-packages/ --traceback --noinput "
djangoapp = ['app1', 'app2']
recipents = ['[email protected]']
#'the log' file
the_log_file = "error.log"
### END OF SETUP
system python_bin + python_virtualenv_bin
errormessages = Array.new
djangoapp.each do
|appname|
test_cmd = python_bin + djangoproject_cmd_test + appname
test_result = system test_cmd
if not test_result
#there is some errors and fails, run the test again to accquire the error messages.
errormessages << "#{test_cmd}"
end
end
if errormessages.length > 0
#It's sending out last time's results!
#Results
file = File.open(the_log_file, "r")
mtime = file.mtime
content = ""
file.each {|line| content << line }
recipents.each{|user_to|
send_email("root@djangoproj", "Ruby Testboy", user_to, "", "DjangoProj - UnitTest FAILED - "+mtime.to_s, "-----------------------\n15m ago results:\n\n" + errormessages.to_s+"\n\n"+content)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment