# test_modified.rake # by Josh Susser (josh@hasmanythrough.com) # v0.1 - 2006/01/29 # # Run tests for all versioned (using Subversion) controller and model files # that have modifications to be committed to the repository. Also runs # modified tests. Handles files in subdirectories properly. # # svn status codes that tests are run for: A C M R ? # # Usage: # 1) Drop this file into /lib/tasks # 2) In your directory, type "rake modified" # return subset of lines that indicate modified files, stripped of status codes # lines should be an array of output lines from 'svn status' command def modified_files(status_lines) status_lines.find_all {|s| s[0..0] =~ /A|C|M|R|\?/}.map {|t| t[7..-1]} end # return path of test corresponding to app file in base directory def test_path(path, base_dir, test_dir) base_dir += "/" unless base_dir[-1..-1] == "/" test_dir += "/" unless test_dir[-1..-1] == "/" test_dir + path[(base_dir.length)..(-1 * " .rb".length)] + "_test.rb" end desc 'Test files for subversion commit' Rake::TestTask.new(:modified => [:prepare_test_database]) do |t| tests = modified_files(`svn status test`.split("\n")) models = modified_files(`svn status app/models`.split("\n")) controllers = modified_files(`svn status app/controllers`.split("\n")) tests = (tests + models.collect {|m| test_path(m, "app/models", "test/unit")} + controllers.collect {|c| test_path(c, "app/controllers", "test/functional")} ).uniq if tests.empty? then puts "No modifications to test." t.libs << 'test' t.test_files = [] else t.libs << 'test' t.test_files = tests t.verbose = true end end