82 lines
3.9 KiB
Rust
82 lines
3.9 KiB
Rust
// file: crates/ksp-logging-lib/tests/ownership.rs
|
|
// version: 1
|
|
|
|
//! Integration audit ensuring KSP crates do not bypass the logging facade.
|
|
|
|
fn collect_rust_files(directory: &std::path::Path, files: &mut std::vec::Vec<std::path::PathBuf>) {
|
|
let entries_result = std::fs::read_dir(directory);
|
|
let entries = match entries_result {
|
|
std::result::Result::Ok(entries) => entries,
|
|
std::result::Result::Err(_) => return,
|
|
};
|
|
for entry_result in entries {
|
|
let entry = match entry_result {
|
|
std::result::Result::Ok(entry) => entry,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
collect_rust_files(path.as_path(), files);
|
|
continue;
|
|
}
|
|
if path.extension().and_then(std::ffi::OsStr::to_str) == std::option::Option::Some("rs") {
|
|
files.push(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn workspace_crates_do_not_bypass_ksp_logging_facade() {
|
|
let logging_manifest_directory = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
let workspace_root = match logging_manifest_directory.parent().and_then(std::path::Path::parent) {
|
|
std::option::Option::Some(root) => root,
|
|
std::option::Option::None => return,
|
|
};
|
|
let crates_directory = workspace_root.join("crates");
|
|
let entries_result = std::fs::read_dir(crates_directory.as_path());
|
|
assert!(entries_result.is_ok(), "unable to inspect workspace crates at {}", crates_directory.display());
|
|
let entries = match entries_result {
|
|
std::result::Result::Ok(entries) => entries,
|
|
std::result::Result::Err(_) => return,
|
|
};
|
|
for entry_result in entries {
|
|
let entry = match entry_result {
|
|
std::result::Result::Ok(entry) => entry,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
let crate_path = entry.path();
|
|
if !crate_path.is_dir() || entry.file_name() == std::ffi::OsStr::new("ksp-logging-lib") {
|
|
continue;
|
|
}
|
|
let manifest_path = crate_path.join("Cargo.toml");
|
|
if manifest_path.exists() {
|
|
let manifest_result = std::fs::read_to_string(manifest_path.as_path());
|
|
assert!(manifest_result.is_ok(), "unable to read {}", manifest_path.display());
|
|
let manifest = match manifest_result {
|
|
std::result::Result::Ok(manifest) => manifest,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
assert!(
|
|
!manifest.contains("tracing.workspace") && !manifest.contains("\ntracing =") && !manifest.contains("[dependencies.tracing]"),
|
|
"{} depends directly on tracing",
|
|
manifest_path.display(),
|
|
);
|
|
assert!(!manifest.contains("tracing-subscriber"), "{} depends directly on tracing-subscriber", manifest_path.display());
|
|
assert!(!manifest.contains("tracing-appender"), "{} depends directly on tracing-appender", manifest_path.display());
|
|
}
|
|
let mut rust_files = std::vec::Vec::new();
|
|
collect_rust_files(crate_path.as_path(), &mut rust_files);
|
|
for rust_file in rust_files {
|
|
let source_result = std::fs::read_to_string(rust_file.as_path());
|
|
assert!(source_result.is_ok(), "unable to read {}", rust_file.display());
|
|
let source = match source_result {
|
|
std::result::Result::Ok(source) => source,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
assert!(!source.contains("tracing::"), "{} bypasses ksp-logging-lib via tracing", rust_file.display());
|
|
assert!(!source.contains("tracing_subscriber::"), "{} bypasses ksp-logging-lib via tracing-subscriber", rust_file.display());
|
|
assert!(!source.contains("tracing_appender::"), "{} bypasses ksp-logging-lib via tracing-appender", rust_file.display());
|
|
}
|
|
}
|
|
}
|