dev_scripts/testing.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
use std::path::PathBuf;
use alpm_buildinfo::cli::ValidateArgs;
use anyhow::{Context, Result};
use colored::Colorize;
use log::{debug, info};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use strum::IntoEnumIterator;
use crate::{cli::TestFileType, sync::PackageRepositories, ui::get_progress_bar};
static PKGSRC_DIR: &str = "pkgsrc";
static PACKAGES_DIR: &str = "packages";
static DATABASES_DIR: &str = "databases";
/// This is the entry point for running validation tests of parsers on ALPM metadata files.
pub struct TestRunner {
pub test_data_dir: PathBuf,
pub file_type: TestFileType,
}
impl TestRunner {
/// Run validation on all local test files that have been downloaded via the
/// `test-files download` command.
pub fn run_tests(&self) -> Result<()> {
let test_files = self.find_files_of_type().context(format!(
"Failed to detect files for type {}",
self.file_type
))?;
info!(
"Found {} {} files for testing",
test_files.len(),
self.file_type
);
let progress_bar = get_progress_bar(test_files.len() as u64);
// Run the validate subcommand for all files in parallel.
let asserts: Vec<(PathBuf, Result<()>)> = test_files
.into_par_iter()
.map(|file| {
let result = match self.file_type {
TestFileType::BuildInfo => alpm_buildinfo::validate(ValidateArgs {
file: Some(file.clone()),
schema: None,
})
.map_err(|err| err.into()),
TestFileType::SrcInfo => unimplemented!(),
TestFileType::PkgInfo => unimplemented!(),
TestFileType::MTree => unimplemented!(),
TestFileType::RemoteDesc => unimplemented!(),
TestFileType::RemoteFiles => unimplemented!(),
TestFileType::LocalDesc => unimplemented!(),
TestFileType::LocalFiles => unimplemented!(),
};
progress_bar.inc(1);
(file, result)
})
.collect();
// Finish the progress_bar
progress_bar.finish_with_message("Validation run finished.");
// Get all files and the respective error for which validation failed.
let failures: Vec<(PathBuf, anyhow::Error)> = asserts
.into_iter()
.filter_map(|(path, result)| {
if let Err(err) = result {
Some((path, err))
} else {
None
}
})
.collect();
if !failures.is_empty() {
for (index, failure) in failures.into_iter().enumerate() {
let index = format!("[{index}]").bold().red();
info!(
"{index} {} with error:\n {}\n",
failure.0.to_string_lossy().bold(),
failure.1
);
}
}
Ok(())
}
/// Searches the download directory for all files of the given type.
///
/// Returns a list of Paths that were found in the process.
pub fn find_files_of_type(&self) -> Result<Vec<PathBuf>> {
let mut files = Vec::new();
// First up, determine which folders we should look at while searching for files.
let type_folders = match self.file_type {
// All package related file types are nested in the subdirectories of the respective
// package's package repository.
TestFileType::BuildInfo | TestFileType::PkgInfo | TestFileType::MTree => {
PackageRepositories::iter()
.map(|repo| self.test_data_dir.join(PACKAGES_DIR).join(repo.to_string()))
.collect()
}
TestFileType::SrcInfo => vec![self.test_data_dir.join(PKGSRC_DIR)],
// The `desc` and `files` file types are nested in the subdirectories of the respective
// package's package repository.
TestFileType::RemoteDesc | TestFileType::RemoteFiles => PackageRepositories::iter()
.map(|repo| {
self.test_data_dir
.join(DATABASES_DIR)
.join(repo.to_string())
})
.collect(),
TestFileType::LocalDesc | TestFileType::LocalFiles => {
unimplemented!();
}
};
for folder in type_folders {
debug!("Looking for files in {folder:?}");
// Each top-level folder contains a number of sub-folders where each sub-folder
// represents a single package. Check if the file we're interested in exists
// for said package. If so, add it to the list
for pkg_folder in
std::fs::read_dir(&folder).context(format!("Failed to read folder {folder:?}"))?
{
let pkg_folder = pkg_folder?;
let file_path = pkg_folder.path().join(self.file_type.to_string());
if file_path.exists() {
files.push(file_path);
}
}
}
Ok(files)
}
}
#[cfg(test)]
mod tests {
use std::fs::OpenOptions;
use std::{collections::HashSet, fs::create_dir};
use rstest::rstest;
use super::*;
const PKG_NAMES: &[&str] = &[
"pipewire-alsa-1:1.0.7-1-x86_64",
"xorg-xvinfo-1.1.5-1-x86_64",
"acl-2.3.2-1-x86_64",
"archlinux-keyring-20240520-1-any",
];
/// Ensure that files can be found in case they're nested inside
/// sub-subdirectories if the directory structure is:
/// `target-dir/packages/${pacman-repo}/${package-name}`
#[rstest]
#[case(TestFileType::BuildInfo)]
#[case(TestFileType::PkgInfo)]
#[case(TestFileType::MTree)]
fn test_find_files_for_packages(#[case] file_type: TestFileType) -> Result<()> {
// Create a temporary directory for testing.
let tmp_dir = tempfile::tempdir()?;
let packages_dir = tmp_dir.path().join(PACKAGES_DIR);
create_dir(&packages_dir)?;
// The list of files we're expecting to find.
let mut expected_files = HashSet::new();
// Create a test file for each repo.
for (index, repo) in PackageRepositories::iter().enumerate() {
// Create the repository folder
let repo_dir = packages_dir.join(repo.to_string());
create_dir(&repo_dir)?;
// Create a package subfolder inside that repository folder.
let pkg = PKG_NAMES[index];
let pkg_dir = repo_dir.join(pkg);
create_dir(&pkg_dir)?;
// Touch the file inside the package folder.
let file_path = pkg_dir.join(file_type.to_string());
OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&file_path)?;
expected_files.insert(file_path);
}
// Run the logic to find the files in question.
let runner = TestRunner {
test_data_dir: tmp_dir.path().to_owned(),
file_type,
};
let found_files = HashSet::from_iter(runner.find_files_of_type()?.into_iter());
assert_eq!(
found_files, expected_files,
"Expected that all created package files are also found."
);
Ok(())
}
/// Ensure that files can be found in case they're nested inside
/// sub-subdirectories if the directory structure is:
/// `target-dir/databases/${pacman-repo}/${package-name}`
#[rstest]
#[case(TestFileType::RemoteFiles)]
#[case(TestFileType::RemoteDesc)]
fn test_find_files_for_databases(#[case] file_type: TestFileType) -> Result<()> {
// Create a temporary directory for testing.
let tmp_dir = tempfile::tempdir()?;
let databases_dir = tmp_dir.path().join(DATABASES_DIR);
create_dir(&databases_dir)?;
// The list of files we're expecting to find.
let mut expected_files = HashSet::new();
// Create a test file for each repo.
for (index, repo) in PackageRepositories::iter().enumerate() {
// Create the repository folder
let repo_dir = databases_dir.join(repo.to_string());
create_dir(&repo_dir)?;
// Create a package subfolder inside that repository folder.
let pkg = PKG_NAMES[index];
let pkg_dir = repo_dir.join(pkg);
create_dir(&pkg_dir)?;
// Touch the file inside the package folder.
let file_path = pkg_dir.join(file_type.to_string());
OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&file_path)?;
expected_files.insert(file_path);
}
// Run the logic to find the files in question.
let runner = TestRunner {
test_data_dir: tmp_dir.path().to_owned(),
file_type,
};
let found_files = HashSet::from_iter(runner.find_files_of_type()?.into_iter());
assert_eq!(
found_files, expected_files,
"Expected that all created databases files are also found."
);
Ok(())
}
/// Ensure that files can be found in case they're nested inside
/// sub-subdirectories if the directory structure is:
/// `target-dir/pkgsrc/${package-name}`
#[rstest]
#[case(TestFileType::SrcInfo)]
fn test_find_files_for_pkgsrc(#[case] file_type: TestFileType) -> Result<()> {
// Create a temporary directory for testing.
let tmp_dir = tempfile::tempdir()?;
let pkgsrc_dir = tmp_dir.path().join(PKGSRC_DIR);
create_dir(&pkgsrc_dir)?;
// The list of files we're expecting to find.
let mut expected_files = HashSet::new();
// Create one subdirectory for each package name.
// Then create the file in question for that package.
for pkg in PKG_NAMES {
let pkg_dir = pkgsrc_dir.join(pkg);
create_dir(&pkg_dir)?;
// Touch the file inside the package folder.
let file_path = pkg_dir.join(file_type.to_string());
OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&file_path)?;
expected_files.insert(file_path);
}
// Run the logic to find the files in question.
let runner = TestRunner {
test_data_dir: tmp_dir.path().to_owned(),
file_type,
};
let found_files = HashSet::from_iter(runner.find_files_of_type()?.into_iter());
assert_eq!(
found_files, expected_files,
"Expected that all created pkgsrc files are also found."
);
Ok(())
}
}