@@ -3,6 +3,45 @@ require 'find'
33
44API_COVERAGE_INDEX = File . join ( 'coverage_api' , 'index.html' )
55
6+ def normalize_frontend_path ( path , frontend_root )
7+ return path if path . start_with? ( frontend_root + "/" )
8+ return path . sub ( %r{^.*?/frontend/} , frontend_root + "/" ) if path . include? ( "/frontend/" )
9+ path
10+ end
11+
12+ def load_lcov_coverage ( path , frontend_root )
13+ return { } unless File . exist? ( path )
14+ files = { }
15+ current = nil
16+ File . foreach ( path ) do |raw_line |
17+ line = raw_line . strip
18+ case line
19+ when /^SF:(.+)/
20+ file_path = normalize_frontend_path ( Regexp . last_match ( 1 ) , frontend_root )
21+ current = files [ file_path ] = {
22+ lines : { covered : 0 , total : 0 } ,
23+ branches : { covered : 0 , total : 0 } ,
24+ functions : { covered : 0 , total : 0 } ,
25+ }
26+ when /^LH:(\d +)/
27+ current [ :lines ] [ :covered ] = Regexp . last_match ( 1 ) . to_i if current
28+ when /^LF:(\d +)/
29+ current [ :lines ] [ :total ] = Regexp . last_match ( 1 ) . to_i if current
30+ when /^BRH:(\d +)/
31+ current [ :branches ] [ :covered ] = Regexp . last_match ( 1 ) . to_i if current
32+ when /^BRF:(\d +)/
33+ current [ :branches ] [ :total ] = Regexp . last_match ( 1 ) . to_i if current
34+ when /^FNH:(\d +)/
35+ current [ :functions ] [ :covered ] = Regexp . last_match ( 1 ) . to_i if current
36+ when /^FNF:(\d +)/
37+ current [ :functions ] [ :total ] = Regexp . last_match ( 1 ) . to_i if current
38+ when "end_of_record"
39+ current = nil
40+ end
41+ end
42+ files
43+ end
44+
645namespace :check_file_coverage do
746 desc "Check test coverage for one or more app files after running `rspec`. " +
847 "Usage: rake check_file_coverage:api app/models/device.rb"
@@ -72,61 +111,22 @@ namespace :check_file_coverage do
72111 desc "Check frontend file coverage after running `bun test`. " +
73112 "Usage: rake check_file_coverage:frontend frontend/app.tsx"
74113 task fe : :environment do
75- FRONTEND_ROOT = 'frontend'
76- COVERAGE_ROOT = 'coverage_fe'
77- LCOV_FILE_PATH = File . join ( COVERAGE_ROOT , 'lcov.info' )
114+ frontend_root = 'frontend'
115+ coverage_root = 'coverage_fe'
116+ lcov_file_path = File . join ( coverage_root , 'lcov.info' )
78117
79118 task_name = Rake . application . top_level_tasks . first
80119 task_index = ARGV . index ( task_name )
81120 paths_args = ARGV . drop ( task_index + 1 )
82121
83- def normalize_frontend_path ( path )
84- return path if path . start_with? ( FRONTEND_ROOT + "/" )
85- return path . sub ( %r{^.*?/frontend/} , FRONTEND_ROOT + "/" ) if path . include? ( "/frontend/" )
86- path
87- end
88-
89- def load_lcov_coverage ( path )
90- return { } unless File . exist? ( path )
91- files = { }
92- current = nil
93- File . foreach ( path ) do |raw_line |
94- line = raw_line . strip
95- case line
96- when /^SF:(.+)/
97- file_path = normalize_frontend_path ( Regexp . last_match ( 1 ) )
98- current = files [ file_path ] = {
99- lines : { covered : 0 , total : 0 } ,
100- branches : { covered : 0 , total : 0 } ,
101- functions : { covered : 0 , total : 0 } ,
102- }
103- when /^LH:(\d +)/
104- current [ :lines ] [ :covered ] = Regexp . last_match ( 1 ) . to_i if current
105- when /^LF:(\d +)/
106- current [ :lines ] [ :total ] = Regexp . last_match ( 1 ) . to_i if current
107- when /^BRH:(\d +)/
108- current [ :branches ] [ :covered ] = Regexp . last_match ( 1 ) . to_i if current
109- when /^BRF:(\d +)/
110- current [ :branches ] [ :total ] = Regexp . last_match ( 1 ) . to_i if current
111- when /^FNH:(\d +)/
112- current [ :functions ] [ :covered ] = Regexp . last_match ( 1 ) . to_i if current
113- when /^FNF:(\d +)/
114- current [ :functions ] [ :total ] = Regexp . last_match ( 1 ) . to_i if current
115- when "end_of_record"
116- current = nil
117- end
118- end
119- files
120- end
121-
122- lcov_coverage = load_lcov_coverage ( LCOV_FILE_PATH )
122+ lcov_coverage = load_lcov_coverage ( lcov_file_path , frontend_root )
123123 abort ( "Run `bun test` first." ) if lcov_coverage . empty?
124124
125125 if paths_args . empty?
126126 paths = lcov_coverage . keys
127127 else
128128 paths = paths_args . map do |path |
129- normalize_frontend_path ( path . sub ( %r{^#{ Regexp . escape ( COVERAGE_ROOT ) } /} , '' ) )
129+ normalize_frontend_path ( path . sub ( %r{^#{ Regexp . escape ( coverage_root ) } /} , '' ) , frontend_root )
130130 end
131131 end
132132
0 commit comments