diff --git a/.gitignore b/.gitignore index 2b6132c8f..136fbb735 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ target enums/target -tests/repositories *~ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..2461c52e7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,12 @@ +[submodule "tests/repositories/rca-output"] + path = tests/repositories/rca-output + url = https://github.com/SoftengPoliTo/rca-output +[submodule "tests/repositories/DeepSpeech"] + path = tests/repositories/DeepSpeech + url = https://github.com/mozilla/DeepSpeech +[submodule "tests/repositories/rust"] + path = tests/repositories/rust + url = https://github.com/rust-lang/rust.git +[submodule "tests/repositories/pdf.js"] + path = tests/repositories/pdf.js + url = https://github.com/mozilla/pdf.js diff --git a/Cargo.lock b/Cargo.lock index 714271224..a75f0ec95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1273,6 +1273,7 @@ dependencies = [ "num-derive", "num-format", "num-traits", + "once_cell", "petgraph", "pretty_assertions", "regex", diff --git a/Cargo.toml b/Cargo.toml index 864372b63..a4d484055 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ crossbeam = { version = "^0.8", features = ["crossbeam-channel"] } fxhash = "0.2" globset = "^0.4" lazy_static = "^1.3" +once_cell = "1.12.1" num = "^0.4" num-derive = "^0.3" num-format = "^0.4" diff --git a/tests/repositories/DeepSpeech b/tests/repositories/DeepSpeech new file mode 160000 index 000000000..f2e9c8588 --- /dev/null +++ b/tests/repositories/DeepSpeech @@ -0,0 +1 @@ +Subproject commit f2e9c85880dff94115ab510cde9ca4af7ee51c19 diff --git a/tests/repositories/pdf.js b/tests/repositories/pdf.js new file mode 160000 index 000000000..a2ae56f39 --- /dev/null +++ b/tests/repositories/pdf.js @@ -0,0 +1 @@ +Subproject commit a2ae56f394acb485ce7067a75342ac60da17f171 diff --git a/tests/repositories/rca-output b/tests/repositories/rca-output new file mode 160000 index 000000000..16033b9fd --- /dev/null +++ b/tests/repositories/rca-output @@ -0,0 +1 @@ +Subproject commit 16033b9fd6f4e0ce866df88d127e8c5180f0d0ed diff --git a/tests/repositories/rust b/tests/repositories/rust new file mode 160000 index 000000000..f1edd0429 --- /dev/null +++ b/tests/repositories/rust @@ -0,0 +1 @@ +Subproject commit f1edd0429582dd29cccacaf50fd134b05593bd9c diff --git a/tests/test.rs b/tests/test.rs index 3694d849d..b3c8645a7 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,5 +1,6 @@ use globset::GlobSet; use globset::{Glob, GlobSetBuilder}; +use once_cell::sync::Lazy; use rust_code_analysis::LANG; use rust_code_analysis::*; use std::fs::*; @@ -7,16 +8,11 @@ use std::io::BufReader; use std::path::Path; use std::path::PathBuf; use std::process; -use std::process::Command; -use std::sync::Once; - -// Synchronization primitive -static INIT: Once = Once::new(); #[derive(Debug)] struct Config { language: Option, - output_folder: String, + output_folder: PathBuf, } fn 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<()> { }; // Build json file path - let file_path = Path::new(&cfg.output_folder) - .join(path.strip_prefix("./").unwrap()) - .into_os_string() - .into_string() - .unwrap() - + ".json"; + let mut file_path = cfg.output_folder.join(path.strip_prefix("./").unwrap()); + file_path.set_extension("json"); // Produce and compare metrics only if json file exists - if Path::new(&file_path).exists() { + if file_path.exists() { // Get FuncSpace struct let funcspace_struct = get_function_spaces(&language, source, &path, None).unwrap(); @@ -182,32 +174,15 @@ fn compare_f64(f1: f64, f2: &serde_json::Value) { } } -const OUTPUT_FOLDER: &str = "./tests/repositories/rca-output"; +static REPO: Lazy<&Path> = Lazy::new(|| Path::new("./tests/repositories")); /// Produces metrics runtime and compares them with previously generated json files -fn compare_rca_output_with_files( - repo_branch: &str, - repo_url: &str, - repo_folder: &str, - include: &[&str], -) { - // The first test clones the repository - // Next tests wait here until the repository is cloned - INIT.call_once(|| { - clone_repository( - "main", - "https://github.com/SoftengPoliTo/rca-output.git", - OUTPUT_FOLDER, - ); - }); - - clone_repository(repo_branch, repo_url, repo_folder); - +fn compare_rca_output_with_files(repo_name: &str, include: &[&str]) { let num_jobs = 4; let cfg = Config { language: None, - output_folder: OUTPUT_FOLDER.to_owned(), + output_folder: REPO.join("rca-output"), }; let mut gsbi = GlobSetBuilder::new(); @@ -218,7 +193,7 @@ fn compare_rca_output_with_files( let files_data = FilesData { include: gsbi.build().unwrap(), exclude: GlobSet::empty(), - paths: vec![Path::new(repo_folder).to_path_buf()], + paths: vec![REPO.join(repo_name)], }; 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( } } -/// Runs a git clone command -fn clone_repository(branch: &str, url: &str, destination: &str) { - if !Path::new(destination).exists() { - Command::new("git") - .args([ - "clone", - "--depth", - "1", - "--branch", - branch, - url, - destination, - ]) - .output() - .expect("Git clone failed"); - } -} - #[test] fn test_deepspeech() { - compare_rca_output_with_files( - "v0.9.3", - "https://github.com/mozilla/DeepSpeech.git", - "./tests/repositories/DeepSpeech", - &["*.cc", "*.cpp", "*.h", "*.hh"], - ); + compare_rca_output_with_files("DeepSpeech", &["*.cc", "*.cpp", "*.h", "*.hh"]); } #[test] fn test_pdfjs() { - compare_rca_output_with_files( - "v2.12.313", - "https://github.com/mozilla/pdf.js.git", - "./tests/repositories/pdf.js", - &["*.js"], - ); + compare_rca_output_with_files("pdf.js", &["*.js"]); } #[test] fn test_rust_library() { - compare_rca_output_with_files( - "1.57.0", - "https://github.com/rust-lang/rust.git", - "./tests/repositories/rust", - &["*/library/*.rs"], - ); + compare_rca_output_with_files("rust", &["*/library/*.rs"]); }