Skip to content

Instantly share code, notes, and snippets.

@kuboon
Created February 28, 2025 01:21
Show Gist options
  • Save kuboon/7eec1fb00b48e3a2d9da384f479f3e24 to your computer and use it in GitHub Desktop.
Save kuboon/7eec1fb00b48e3a2d9da384f479f3e24 to your computer and use it in GitHub Desktop.
RSpec.configure do |config|
config.before(:suite) { Coverage.start(oneshot_lines: true) }
config.after :suite do
coverage_data = Coverage.result(stop: true, clear: true)
uncovered_lines(coverage_data)
end
end
def parse_git_diff(base_branch)
diff_output, _stderr, _status = Open3.capture3("git diff #{base_branch} --unified=0")
modified_lines = Hash.new { |hash, key| hash[key] = [] }
file_name = nil
diff_output.each_line do |line|
if line =~ %r{^\+\+\+ b/(.+)$}
file_name = Regexp.last_match(1) # ファイル名を取得
elsif line =~ /^@@ .* \+(\d+)(?:,(\d+))? @@/ && file_name
start_line = Regexp.last_match(1).to_i
line_count = (Regexp.last_match(2) || 1).to_i
modified_lines[file_name].concat((start_line...(start_line + line_count)).to_a)
end
end
modified_lines
end
def uncovered_lines(coverage_data_)
coverage_data = coverage_data_.filter{|k,_| k.starts_with? Dir.pwd }
# 変更された行を取得する
base_branch = "master" # 必要に応じて変更
modified_lines = parse_git_diff(base_branch)
# 未実行の変更行を特定
uncovered_lines = {}
modified_lines.each do |file, lines|
coverage_entry = coverage_data.find { |f, _l| file.ends_with?(f) }
if coverage_entry.nil?
uncovered_lines[file] = lines
next
end
uncovered = lines.reject { _1.in? coverage_entry[1][:oneshot_lines] }
uncovered_lines[file] = uncovered unless uncovered.empty?
end
# 結果出力
puts "未実行の変更行:"
uncovered_lines.each do |file, lines|
puts "#{file}: #{lines.join(', ')}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment