// https://clang.llvm.org/docs/SourceBasedCodeCoverage.html int __llvm_profile_runtime = 0; void __llvm_profile_initialize_file(void); constchar *__llvm_profile_get_filename(void); void __llvm_profile_set_filename(constchar *); int __llvm_profile_write_file(void); int __llvm_profile_register_write_file_atexit(void); constchar *__llvm_profile_get_path_prefix(void); #endif/* PROFILE_INSTRPROFILING_H_ */
2.指定覆盖率数据文件的输出路径,建议在程序启动时调用
1 2 3 4 5 6 7 8 9
let name = "\(moduleName).profraw" let fileManager = FileManager.default do { let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) let filePath: NSString = documentDirectory.appendingPathComponent(name).path as NSString __llvm_profile_set_filename(filePath.utf8String) } catch { print(error) }
File.open(diff_file).each do |line| # 新的文件改动标识 if line.start_with? 'diff --git' # 判断是白名单格式的文件 if white_adpator(line, white_list) whiteFile = true next else whiteFile = false next end end
if whiteFile == false next end
if line.start_with? '+++' # 提取文件路径 file_path = line[/\/.*/, 0][1..-1] if file_path current_file = file_path file_map[current_file] = [] end end
if line.start_with? '@@' # 提取新增代码行,格式为 " +66,5" change = line[/\+.*?\s{1}/, 0] # 消除"+" change = change[1..-1] # flat if change.include? ',' base_line = change.split(',')[0].to_i delta = change.split(',')[1].to_i delta.times { |i| file_map[current_file].push(base_line + i) if current_file} else file_map[current_file].push(change.to_i) if current_file end end end
# 该类中的每一行信息的标识 # DA:20,1 if line.start_with? 'DA:' line_number = line.split("DA:")[1] real_line = line_number.split(",")[0].to_i # puts "gather_file_lines:#{gather_file_lines}" # puts "real_line: #{real_line}" if gather_file_lines.include?(real_line) # puts "gather_line: #{line}" coverage_gather_file.syswrite(line) end else coverage_gather_file.syswrite(line) end end
return coverage_gather_file end
end
if __FILE__ == $0 include GitUtil opts = Trollop::options do opt :diff_file, 'Path for diff file', :type => :string opt :coverage_info_file, 'Path for covage info file', :type => :string end
Trollop::die :diff_file, 'must be provided' if opts[:diff_file].nil? Trollop::die :coverage_info_file, 'must be provided' if opts[:coverage_info_file].nil?