Created
July 19, 2015 11:37
-
-
Save pacojp/a5ec8a182592d7234587 to your computer and use it in GitHub Desktop.
run only edited test method by watchr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'diff/lcs' | |
require 'diff/lcs/string' | |
require 'digest/md5' | |
require 'fileutils' | |
require 'shellwords' | |
@project_name = "hogehoge" | |
watch( 'test/test_.*\.rb' ) {|md| system("rake test") } | |
# ..... | |
watch( 'test/integration/(.*)_test\.rb' ) do |md| | |
run_test_by_diff("test/integration/#{md[1]}_test.rb") | |
end | |
def run_test_by_diff(file) | |
snapshot = "/tmp/.watchr.#{@project_name}-#{Digest::MD5.hexdigest(file) }" | |
cmd = %|ruby -I"lib:test" #{file.shellescape}| | |
cmd_plus = "" | |
if File.exist?(snapshot) | |
diff_result = File.read(snapshot).diff(File.read(file)) | |
if diff_result.empty? | |
puts "FILE NOT CHANGED!!" | |
return | |
end | |
test_methods = get_test_methods(file, diff_result) | |
# if all changes looks like inside TEST | |
unless test_methods.include?(nil) | |
if test_methods.size > 1 | |
methods_regex = "/^(#{test_methods.join("|")})$/" | |
cmd_plus = " -n #{methods_regex.shellescape}" | |
else | |
cmd_plus = " -n #{test_methods.first.shellescape}" | |
end | |
end | |
end | |
FileUtils.cp file, snapshot | |
cmd += cmd_plus | |
puts "call: #{cmd}" | |
system(cmd) | |
end | |
def get_test_methods(file, diff_result) | |
ret = [] | |
all_lines = File.read(file).lines.to_a | |
diff_result.each do |r| | |
ret << get_test_method(all_lines, r) | |
end | |
ret.uniq | |
end | |
def get_test_method(lines, r) | |
size_at = 0 | |
line_at = 0 | |
lines.each do |l| | |
size_at += l.size | |
line_at += 1 | |
if size_at >= r[0].position | |
break | |
end | |
end | |
i = line_at | |
while (i > 0) do | |
i -= 1 | |
# test "****" do | |
if lines[i] =~ /\A\s+test\s+(["']|%\|)(.+?)(["']|\|)\s+do\s*\z/ | |
return "test_#{$2}" | |
end | |
# def test_**** do | |
if lines[i] =~ /\A\s+def\s+(test_.+?)\z/ | |
return $1 | |
end | |
# def **** | |
if lines[i] =~ /\A\s+def\s+/ | |
return nil | |
end | |
end | |
nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment