Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Thursday, June 08, 2006

Prevent Yourself From Writing Bad Code

In an attempt to further train myself to constantly run unit tests, I have come up with a system that almost prevents me from checking in bad code into my Rails subversion repositories. It comes in two pieces.

First is RAILS_ROOT/lib/tasks/svn.rake:

desc "Checkin your changes after running the tests"
Rake::TestTask.new(:changed => [ :prepare_test_database ]) do |t|
info = `svn info`
since = Time.parse(info[/Last Changed Date: .*/])
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
recent_tests('app/models/*.rb', 'test/unit', since) +
recent_tests('app/controllers/*.rb', 'test/functional', since)

t.libs << 'test'
t.verbose = true
t.test_files = touched.uniq

desc "Check-in work"
task :ci do
exec("rake", "setup_svn") if `which svnorig`[/no svnorig/]

tests = `rake changed`
puts tests
exec("svnorig", *YAML::load(ENV['MESSAGE'])) unless tests[/rake aborted/]

desc "Install subversion alias"
task :setup_svn do
svn = `which svn`.strip
exec("sudo mv #{svn} #{svn}orig && sudo ln -s #{File.expand_path(RAILS_ROOT)}/bin/svn #{svn} && echo 'All set, use svn ci as you normally would'")

Next is RAILS_ROOT/bin/svn:
#!/usr/bin/env ruby
require 'yaml'

if (ARGV[0] == "ci" || ARGV[0] == "commit") && File.exists?("lib/tasks/svn.rake")
ENV['MESSAGE'] = ARGV.to_yaml
exec("rake", "ci")
exec("svnorig", *ARGV)

To initialize this process, and you only have to do this once per machine you use it on, you simply type:
rake setup_svn

When prompted by sudo, type your user password and you are done.

Now, whenever you are in a Rails root directory and the file lib/tasks/svn.rake exists, svn ci does the following:

  • Find all files that you have touched since the last commit
  • Run and show the unit tests of all of those files
  • If the unit tests pass, commit the changes as expected
  • If the unit tests fail, exit

This all assumes that you have been good and wrote solid unit tests, but as the good programmer I know you are, that should be a valid assumption. Enjoy!

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!


Anonymous Anonymous said...

This is pretty cool!

6:24 AM, October 01, 2006

Anonymous Anonymous said...

longchamp handbags
coach outlet
true religion canada
oakley sunglasses
mulberry handbags
reebok shoes
burberry outlet
ralph lauren outlet
ralph lauren polo
ralph lauren
hermes belt for sale
versace sunglasses on sale
fitflops clearance
nike air max 90
thomas sabo outlet
pandora outlet
tory burch outlet
coach outlet
kobe shoes
oakley sunglasses
ghd hair straighteners
ray ban sunglasses
coach outlet online
tory burch shoes
coach outlet
ralph lauren outlet
mulberry handbags
michael kors outlet online
discount oakley sunglasses
burberry sunglasses on sale
ralph lauren pas cher
replica watches
oakley sunglasses
chrome hearts outlet online
longchamp pliage

6:21 PM, May 30, 2016

Blogger chenmeinv0 said...

adidas superstar trainers
toms shoes outlet
louis vuitton handbags
louis vuitton purses
michael kors outlet online
ugg boots clearance
louis vuitton outlet stores
ugg boots
warriors jerseys
timberland boots

10:39 PM, January 03, 2017

Blogger Đào Duy Quang Blog said...

Chung cư Green Pearl nằm trong quần thể Dự án Green Pearl tại số 378 Minh Khai, Vĩnh Tuy, Hai Bà Trưng, Hà Nội là một vị trí đắc địa thuộc cửa ngõ phía Nam của Thủ đô, tiếp giáp với đường Minh và KĐT times city. Căn hộ chung cư cao cấp Green Pearl sẽ mở ra không gian sống xanh, trong lành, KHÔNG ồn ào, KHÔNG khói bụi ngay trong lòng đô thị.
Chung cư Green Pearl

7:20 AM, August 27, 2017

Blogger Unknown said...

ralph lauren outlet
salomon boots
moncler outlet
rolex watches
replica watches
roshe run
ghd hair straighteners
canada goose pas cher

2:54 AM, November 20, 2017

Blogger Unknown said...

After looking at a handful of the blog articles on your site, I seriously like your technique of blogging. I book marked it to my bookmark site list and will be checking back in the near future. Please visit my web site as well and tell me your opinion. If you love online games aquí está la página friv

4:06 AM, March 23, 2018

Blogger Unknown said...

I don't apperceive what it is about the bendable blow and matte finish, but I accept a anemic atom for printed suede Christisn Louboutin Outlet. One of the best brands to cull this off is YSL Wallet Outlet. I accept to acquaint you about a backpack attraction of mine. I am in fact mad at myself that I did not splurge on the Ralph Lauren Polo Outlet Croc Muse Two which has two colors that I admire with a sueced croc pattern. To see a formed suede accomplishment off a Red Bottoms Outlet that I already adore, my affection melts. Replica Watches are additionally pretty reflective of their user's persona. Amazing furthermore multi-colored Replica Watches are commonly worn because of the younger. Conversely, exquisite dress Swiss Rolex Replica are regularly sported by highly developed gentlemen. And then usually there's the luxurious Swiss Replica Watches worn by the individuals who're properly to complete. Check out observing your mates furthermore you can before long see how the layouts are actually ranging.

2:02 AM, April 26, 2018

Blogger نور الهدى said...

نقل عفش من الرياض الى الامارات

1:36 PM, December 08, 2018

Blogger steve said...

افضل شركة نقل عفش من جدة الى الرياض شركة نقل عفش من جدة الى الرياض
شركة نقل عفش من جدة الى دبي شركة شحن عفش من السعودية الى دبي

معلم دهانات بالرياض

ارخص شركة نقل عفش بمكة
شركة نقل عفش من جدة الى الاردن شركة شحن عفش من السعودية الى الاردن

1:19 PM, July 08, 2019

Blogger samaher said...

شركة دهانات بالرياض
شركة صيانة مكيفات بالرياض
شركة تنظيف مكيفات بالرياض
شركة صيانة افران بالرياض
شركة ديكورات بالرياض

9:41 AM, July 25, 2019

Blogger unkown said...

You are doing a great job. Now we are sharing with you all the Tresurebox store products which you will be buy online in a single click. Buy Cheap Furniture nz online in a single click,

2:19 AM, November 28, 2019

Blogger hussainaadil said...

It's my pleasure being here on your blog. A debt of gratitude is in order for offering this intriguing data to us and I wanna come beck here for all the more new posts from you.
Make My Assignment
Math Homework Help

10:59 PM, December 18, 2019

Blogger Eminay said...

Researching these stories is blogging's most fun, and I hate cutting it short just to get more stories out. Commercial Cleaning Services in Dubai fills in as the solid, trusted and disentangled cleaning center for the Middle East cleaning industry visit!

2:39 AM, January 27, 2020

Blogger Unknown said...

غسيل خزانات بمكة غسيل خزانات بمكة
غسيل خزانات بجدة غسيل خزانات بجدة
غسيل خزانات بالدمام غسيل خزانات بالدمام

3:55 PM, February 04, 2020


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


If you like this blog, you might also like top photography schools.