v0.1.4-pre.016-fix.002

This commit is contained in:
2026-08-16 19:16:19 +02:00
parent 3021996658
commit fd200f5335
3 changed files with 119 additions and 10 deletions

View File

@@ -1,8 +1,73 @@
// file: crates/ksp-logging-lib/tests/ownership.rs
// version: 1
// version: 2
//! Integration audit ensuring KSP crates do not bypass the logging facade.
fn manifest_declares_direct_dependency(manifest: &str, dependency_name: &str) -> bool {
let mut section = "";
for raw_line in manifest.lines() {
let line = raw_line.trim();
if line.starts_with('[') && line.ends_with(']') {
section = &line[1..line.len() - 1];
let direct_table_suffix = std::format!(".dependencies.{dependency_name}");
if section == std::format!("dependencies.{dependency_name}")
|| section == std::format!("dev-dependencies.{dependency_name}")
|| section == std::format!("build-dependencies.{dependency_name}")
|| section.ends_with(direct_table_suffix.as_str())
{
return true;
}
continue;
}
if line.is_empty() || line.starts_with('#') {
continue;
}
let dependency_section = section == "dependencies"
|| section == "dev-dependencies"
|| section == "build-dependencies"
|| section.ends_with(".dependencies")
|| section.ends_with(".dev-dependencies")
|| section.ends_with(".build-dependencies");
if !dependency_section {
continue;
}
let assignment = line.split_once('=');
let name = match assignment {
std::option::Option::Some((name, _)) => name.trim(),
std::option::Option::None => continue,
};
if name == dependency_name || name == std::format!("{dependency_name}.workspace") {
return true;
}
}
return false;
}
fn source_uses_direct_path(source: &str, path: &str) -> bool {
let mut search_start = 0usize;
while search_start < source.len() {
let remaining = &source[search_start..];
let relative_index = match remaining.find(path) {
std::option::Option::Some(index) => index,
std::option::Option::None => return false,
};
let index = search_start + relative_index;
if index == 0 {
return true;
}
let previous = source[..index].chars().next_back();
let embedded_in_identifier = match previous {
std::option::Option::Some(character) => character.is_ascii_alphanumeric() || character == '_',
std::option::Option::None => false,
};
if !embedded_in_identifier {
return true;
}
search_start = index + path.len();
}
return false;
}
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 {
@@ -25,6 +90,20 @@ fn collect_rust_files(directory: &std::path::Path, files: &mut std::vec::Vec<std
}
}
#[test]
fn dependency_scanner_distinguishes_tauri_plugin_tracing_from_tracing() {
let plugin_only = "[dependencies]\ntauri-plugin-tracing.workspace = true\n";
let direct_workspace = "[dependencies]\ntracing.workspace = true\n";
let direct_inline = "[dependencies]\ntracing = { workspace = true }\n";
let direct_table = "[dependencies.tracing]\nworkspace = true\n";
assert!(!manifest_declares_direct_dependency(plugin_only, "tracing"));
assert!(manifest_declares_direct_dependency(direct_workspace, "tracing"));
assert!(manifest_declares_direct_dependency(direct_inline, "tracing"));
assert!(manifest_declares_direct_dependency(direct_table, "tracing"));
assert!(!source_uses_direct_path("let plugin = tauri_plugin_tracing::Builder::new();", "tracing::"));
assert!(source_uses_direct_path("let span = tracing::info_span!(\"audit\");", "tracing::"));
}
#[test]
fn workspace_crates_do_not_bypass_ksp_logging_facade() {
let logging_manifest_directory = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@@ -56,13 +135,17 @@ fn workspace_crates_do_not_bypass_ksp_logging_facade() {
std::result::Result::Ok(manifest) => manifest,
std::result::Result::Err(_) => continue,
};
assert!(!manifest_declares_direct_dependency(manifest.as_str(), "tracing"), "{} depends directly on tracing", manifest_path.display(),);
assert!(
!manifest.contains("tracing.workspace") && !manifest.contains("\ntracing =") && !manifest.contains("[dependencies.tracing]"),
"{} depends directly on tracing",
!manifest_declares_direct_dependency(manifest.as_str(), "tracing-subscriber"),
"{} depends directly on tracing-subscriber",
manifest_path.display(),
);
assert!(
!manifest_declares_direct_dependency(manifest.as_str(), "tracing-appender"),
"{} depends directly on tracing-appender",
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);
@@ -73,9 +156,13 @@ fn workspace_crates_do_not_bypass_ksp_logging_facade() {
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());
assert!(!source_uses_direct_path(source.as_str(), "tracing::"), "{} bypasses ksp-logging-lib via tracing", rust_file.display());
assert!(
!source_uses_direct_path(source.as_str(), "tracing_subscriber::"),
"{} bypasses ksp-logging-lib via tracing-subscriber",
rust_file.display(),
);
assert!(!source_uses_direct_path(source.as_str(), "tracing_appender::"), "{} bypasses ksp-logging-lib via tracing-appender", rust_file.display(),);
}
}
}