11use globset:: GlobSet ;
22use globset:: { Glob , GlobSetBuilder } ;
3+ use once_cell:: sync:: Lazy ;
34use rust_code_analysis:: LANG ;
45use rust_code_analysis:: * ;
56use std:: fs:: * ;
67use std:: io:: BufReader ;
78use std:: path:: Path ;
89use std:: path:: PathBuf ;
910use std:: process;
10- use std:: process:: Command ;
11- use std:: sync:: Once ;
12-
13- // Synchronization primitive
14- static INIT : Once = Once :: new ( ) ;
1511
1612#[ derive( Debug ) ]
1713struct Config {
1814 language : Option < LANG > ,
19- output_folder : String ,
15+ output_folder : PathBuf ,
2016}
2117
2218fn act_on_file ( path : PathBuf , cfg : & Config ) -> std:: io:: Result < ( ) > {
@@ -37,15 +33,11 @@ fn act_on_file(path: PathBuf, cfg: &Config) -> std::io::Result<()> {
3733 } ;
3834
3935 // Build json file path
40- let file_path = Path :: new ( & cfg. output_folder )
41- . join ( path. strip_prefix ( "./" ) . unwrap ( ) )
42- . into_os_string ( )
43- . into_string ( )
44- . unwrap ( )
45- + ".json" ;
36+ let mut file_path = cfg. output_folder . join ( path. strip_prefix ( "./" ) . unwrap ( ) ) ;
37+ file_path. set_extension ( "json" ) ;
4638
4739 // Produce and compare metrics only if json file exists
48- if Path :: new ( & file_path) . exists ( ) {
40+ if file_path. exists ( ) {
4941 // Get FuncSpace struct
5042 let funcspace_struct = get_function_spaces ( & language, source, & path, None ) . unwrap ( ) ;
5143
@@ -182,32 +174,15 @@ fn compare_f64(f1: f64, f2: &serde_json::Value) {
182174 }
183175}
184176
185- const OUTPUT_FOLDER : & str = "./tests/repositories/rca-output" ;
177+ static REPO : Lazy < & Path > = Lazy :: new ( || & Path :: new ( "./tests/repositories" ) ) ;
186178
187179/// Produces metrics runtime and compares them with previously generated json files
188- fn compare_rca_output_with_files (
189- repo_branch : & str ,
190- repo_url : & str ,
191- repo_folder : & str ,
192- include : & [ & str ] ,
193- ) {
194- // The first test clones the repository
195- // Next tests wait here until the repository is cloned
196- INIT . call_once ( || {
197- clone_repository (
198- "main" ,
199- "https://github.com/SoftengPoliTo/rca-output.git" ,
200- OUTPUT_FOLDER ,
201- ) ;
202- } ) ;
203-
204- clone_repository ( repo_branch, repo_url, repo_folder) ;
205-
180+ fn compare_rca_output_with_files ( repo_name : & str , include : & [ & str ] ) {
206181 let num_jobs = 4 ;
207182
208183 let cfg = Config {
209184 language : None ,
210- output_folder : OUTPUT_FOLDER . to_owned ( ) ,
185+ output_folder : REPO . join ( "rca-output" ) ,
211186 } ;
212187
213188 let mut gsbi = GlobSetBuilder :: new ( ) ;
@@ -218,7 +193,7 @@ fn compare_rca_output_with_files(
218193 let files_data = FilesData {
219194 include : gsbi. build ( ) . unwrap ( ) ,
220195 exclude : GlobSet :: empty ( ) ,
221- paths : vec ! [ Path :: new ( repo_folder ) . to_path_buf ( ) ] ,
196+ paths : vec ! [ REPO . join ( repo_name ) ] ,
222197 } ;
223198
224199 if let Err ( e) = ConcurrentRunner :: new ( num_jobs, act_on_file) . run ( cfg, files_data) {
@@ -227,50 +202,17 @@ fn compare_rca_output_with_files(
227202 }
228203}
229204
230- /// Runs a git clone command
231- fn clone_repository ( branch : & str , url : & str , destination : & str ) {
232- if !Path :: new ( destination) . exists ( ) {
233- Command :: new ( "git" )
234- . args ( [
235- "clone" ,
236- "--depth" ,
237- "1" ,
238- "--branch" ,
239- branch,
240- url,
241- destination,
242- ] )
243- . output ( )
244- . expect ( "Git clone failed" ) ;
245- }
246- }
247-
248205#[ test]
249206fn test_deepspeech ( ) {
250- compare_rca_output_with_files (
251- "v0.9.3" ,
252- "https://github.com/mozilla/DeepSpeech.git" ,
253- "./tests/repositories/DeepSpeech" ,
254- & [ "*.cc" , "*.cpp" , "*.h" , "*.hh" ] ,
255- ) ;
207+ compare_rca_output_with_files ( "DeepSpeech" , & [ "*.cc" , "*.cpp" , "*.h" , "*.hh" ] ) ;
256208}
257209
258210#[ test]
259211fn test_pdfjs ( ) {
260- compare_rca_output_with_files (
261- "v2.12.313" ,
262- "https://github.com/mozilla/pdf.js.git" ,
263- "./tests/repositories/pdf.js" ,
264- & [ "*.js" ] ,
265- ) ;
212+ compare_rca_output_with_files ( "pdf.js" , & [ "*.js" ] ) ;
266213}
267214
268215#[ test]
269216fn test_rust_library ( ) {
270- compare_rca_output_with_files (
271- "1.57.0" ,
272- "https://github.com/rust-lang/rust.git" ,
273- "./tests/repositories/rust" ,
274- & [ "*/library/*.rs" ] ,
275- ) ;
217+ compare_rca_output_with_files ( "rust" , & [ "*/library/*.rs" ] ) ;
276218}
0 commit comments