Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KenshoFujisaki/463a4e754eefc6e77dce to your computer and use it in GitHub Desktop.
Save KenshoFujisaki/463a4e754eefc6e77dce to your computer and use it in GitHub Desktop.
変更行を含むRSpecのメソッド開始行を取得する
#!/usr/bin/ruby
# require: diff-lines.sh: https://gist.github.com/KenshoFujisaki/169326eb2fdde5182153
# constants
MAX_FILE_LINES = 1000000
# get args
unless ARGV.length == 2
puts "usage: #{$0} [before_revision_hash] [after_revision_hash]"
puts "e.g.: #{$0} '212dfa0' '0bc3dc7'"
exit 1
end
before_rev = ARGV[0]
after_rev = ARGV[1]
# filepath_difflines = {filepath => [diff_linenumber1, diff_linenumber2,..]}
begin
# set target path/method
target_path = `pwd`.chomp # + "/spec/"
target_method_regex = "describe"
filepath_difflines={}
`git --no-pager diff #{before_rev}..#{after_rev} --no-ext-diff -U#{MAX_FILE_LINES} #{target_path}/ \
| diff-lines.sh \
| grep --color=never -E '^[^\"].*\:[0-9]+\:[\+|\-]'`.
split("\n").
map{|e| e.split(":").slice(0..1)}.
uniq.
each{|filepath, line|
filepath_difflines[filepath] ||= [];
filepath_difflines[filepath] << line.to_i}
rescue => e
p e
exit 1
end
# checkout after revision
begin
original_branch=`git branch | awk '/^\*/{print $2}'`.chomp
`git stash; git checkout -q #{after_rev}; git stash pop`
rescue => e
p e
`git stash; git checkout #{original_branch}; git stash pop`
exit 1
end
# filepath_with_methodlinenumber := ["filepath:linenumber"]
begin
filepath_with_methodlinenumber = filepath_difflines.
map{|filepath, difflines|
# def_line = [["def ***", def_linenumber]]
def_line = `cat #{filepath} | grep --color=never -nE "\ +(#{target_method_regex})\ "`.split("\n").
map{|elm| [elm.split(" ")[2], elm.split(":")[0].to_i] }
def_line.push(["eof", MAX_FILE_LINES])
difflines.map{|diffline|
def_line.each_with_index.select{|_def_line, i|
diffline >= _def_line[1] && diffline < def_line[i+1][1]
}.first
}.uniq.compact.map{|diffline|
"#{filepath}:#{diffline[1]}"
}.first
}
# stdout
# spec/controllers/hoge_controller_spec.rb:27
# spec/controllers/fuga_controller_spec.rb:67
puts filepath_with_methodlinenumber
rescue => e
p e
`git stash; git checkout #{original_branch}; git stash pop`
exit 1
end
# checkout original revision
`git stash; git checkout -q #{original_branch}; git stash pop`
@KenshoFujisaki
Copy link
Author

以下のように利用することで、修正したRSpecのメソッドについてのみテスト実行します.

spring rspec -fd -c `ruby get_modifying_file_with_method_beginning_linenumber.rb`

これは,例えば,以下のように実行されます.

spring rspec -fd -c spec/controllers/hoge_controller_spec.rb:27 spec/controllers/fuga_controller_spec.rb:67

@KenshoFujisaki
Copy link
Author

git diffに対して行数出力にパスが通っている必要があります.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment