Skip to content

Commit c51ac49

Browse files
CopilotTheaxiom
andcommitted
Fix test_check_command to use explicit config and format code
Co-authored-by: Theaxiom <57013+Theaxiom@users.noreply.github.com>
1 parent ea6ac57 commit c51ac49

File tree

10 files changed

+533
-196
lines changed

10 files changed

+533
-196
lines changed

src/analyzer/mod.rs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ impl Analyzer {
7373
}
7474

7575
let effective_severity = config.effective_severity(category, rule);
76-
pattern_engine.add_rule(rule, effective_severity).map_err(|e| {
77-
GuardianError::config(format!(
78-
"Failed to add rule '{}' in category '{}': {}",
79-
rule.id, category_name, e
80-
))
81-
})?;
76+
pattern_engine
77+
.add_rule(rule, effective_severity)
78+
.map_err(|e| {
79+
GuardianError::config(format!(
80+
"Failed to add rule '{}' in category '{}': {}",
81+
rule.id, category_name, e
82+
))
83+
})?;
8284
}
8385
}
8486

@@ -92,7 +94,12 @@ impl Analyzer {
9294
let path_filter = PathFilter::new(config.paths.patterns.clone(), ignore_file)
9395
.map_err(|e| GuardianError::config(format!("Failed to create path filter: {e}")))?;
9496

95-
Ok(Self { config, pattern_engine, path_filter, rust_analyzer: RustAnalyzer::new() })
97+
Ok(Self {
98+
config,
99+
pattern_engine,
100+
path_filter,
101+
rust_analyzer: RustAnalyzer::new(),
102+
})
96103
}
97104

98105
/// Create an analyzer with default configuration
@@ -120,23 +127,29 @@ impl Analyzer {
120127
let mut all_violations = Vec::new();
121128

122129
// Apply pattern matching
123-
let matches = self.pattern_engine.analyze_file(file_path, &content).map_err(|e| {
124-
GuardianError::analysis(
125-
file_path.display().to_string(),
126-
format!("Pattern analysis failed: {e}"),
127-
)
128-
})?;
130+
let matches = self
131+
.pattern_engine
132+
.analyze_file(file_path, &content)
133+
.map_err(|e| {
134+
GuardianError::analysis(
135+
file_path.display().to_string(),
136+
format!("Pattern analysis failed: {e}"),
137+
)
138+
})?;
129139

130140
all_violations.extend(self.pattern_engine.matches_to_violations(matches));
131141

132142
// Apply Rust-specific analysis for .rs files
133143
if self.rust_analyzer.handles_file(file_path) {
134-
let rust_violations = self.rust_analyzer.analyze(file_path, &content).map_err(|e| {
135-
GuardianError::analysis(
136-
file_path.display().to_string(),
137-
format!("Rust analysis failed: {e}"),
138-
)
139-
})?;
144+
let rust_violations = self
145+
.rust_analyzer
146+
.analyze(file_path, &content)
147+
.map_err(|e| {
148+
GuardianError::analysis(
149+
file_path.display().to_string(),
150+
format!("Rust analysis failed: {e}"),
151+
)
152+
})?;
140153
all_violations.extend(rust_violations);
141154
}
142155

@@ -238,18 +251,20 @@ impl Analyzer {
238251
let violations = Arc::new(Mutex::new(Vec::new()));
239252
let errors = Arc::new(Mutex::new(Vec::new()));
240253

241-
files.par_iter().for_each(|file_path| match self.analyze_file(file_path) {
242-
Ok(file_violations) => {
243-
if let Ok(mut v) = violations.lock() {
244-
v.extend(file_violations);
254+
files
255+
.par_iter()
256+
.for_each(|file_path| match self.analyze_file(file_path) {
257+
Ok(file_violations) => {
258+
if let Ok(mut v) = violations.lock() {
259+
v.extend(file_violations);
260+
}
245261
}
246-
}
247-
Err(e) => {
248-
if let Ok(mut errs) = errors.lock() {
249-
errs.push((file_path.clone(), e));
262+
Err(e) => {
263+
if let Ok(mut errs) = errors.lock() {
264+
errs.push((file_path.clone(), e));
265+
}
250266
}
251-
}
252-
});
267+
});
253268

254269
// Handle errors
255270
let errors = Arc::try_unwrap(errors)
@@ -548,14 +563,20 @@ impl Analyzer {
548563
})?;
549564

550565
// Test max_files limitation
551-
let options = AnalysisOptions { max_files: Some(1), ..Default::default() };
566+
let options = AnalysisOptions {
567+
max_files: Some(1),
568+
..Default::default()
569+
};
552570

553571
let report = self.analyze_directory(root, &options)?;
554572

555573
if report.summary.total_files != 1 {
556574
return Err(GuardianError::analysis(
557575
"validation".to_string(),
558-
format!("Expected 1 file with max_files=1, got {}", report.summary.total_files),
576+
format!(
577+
"Expected 1 file with max_files=1, got {}",
578+
report.summary.total_files
579+
),
559580
));
560581
}
561582

src/analyzer/rust.rs

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ pub struct RustAnalyzer {
2727
impl RustAnalyzer {
2828
/// Create a new Rust analyzer with default settings
2929
pub fn new() -> Self {
30-
Self { analyze_tests: false, check_quality_headers: true }
30+
Self {
31+
analyze_tests: false,
32+
check_quality_headers: true,
33+
}
3134
}
3235

3336
/// Create a Rust analyzer that also analyzes test files
3437
pub fn with_tests() -> Self {
35-
Self { analyze_tests: true, check_quality_headers: true }
38+
Self {
39+
analyze_tests: true,
40+
check_quality_headers: true,
41+
}
3642
}
3743

3844
/// Find all unimplemented macros in the file
@@ -157,7 +163,11 @@ impl FileAnalyzer for RustAnalyzer {
157163
}
158164

159165
fn handles_file(&self, file_path: &Path) -> bool {
160-
file_path.extension().and_then(|ext| ext.to_str()).map(|ext| ext == "rs").unwrap_or(false)
166+
file_path
167+
.extension()
168+
.and_then(|ext| ext.to_str())
169+
.map(|ext| ext == "rs")
170+
.unwrap_or(false)
161171
}
162172
}
163173

@@ -276,18 +286,24 @@ impl EmptyOkReturnVisitor {
276286

277287
fn is_result_type(&self, ty: &syn::Type) -> bool {
278288
match ty {
279-
syn::Type::Path(type_path) => {
280-
type_path.path.segments.last().map(|seg| seg.ident == "Result").unwrap_or(false)
281-
}
289+
syn::Type::Path(type_path) => type_path
290+
.path
291+
.segments
292+
.last()
293+
.map(|seg| seg.ident == "Result")
294+
.unwrap_or(false),
282295
_ => false,
283296
}
284297
}
285298

286299
fn is_option_type(&self, ty: &syn::Type) -> bool {
287300
match ty {
288-
syn::Type::Path(type_path) => {
289-
type_path.path.segments.last().map(|seg| seg.ident == "Option").unwrap_or(false)
290-
}
301+
syn::Type::Path(type_path) => type_path
302+
.path
303+
.segments
304+
.last()
305+
.map(|seg| seg.ident == "Option")
306+
.unwrap_or(false),
291307
_ => false,
292308
}
293309
}
@@ -309,7 +325,13 @@ impl EmptyOkReturnVisitor {
309325
if let syn::Expr::Call(call) = expr {
310326
// Check if it's Ok(...) with trivial arguments
311327
if let syn::Expr::Path(path) = &*call.func {
312-
if path.path.segments.last().map(|seg| seg.ident == "Ok").unwrap_or(false) {
328+
if path
329+
.path
330+
.segments
331+
.last()
332+
.map(|seg| seg.ident == "Ok")
333+
.unwrap_or(false)
334+
{
313335
// Ok() with no args is trivial
314336
if call.args.is_empty() {
315337
return true;
@@ -338,7 +360,13 @@ impl EmptyOkReturnVisitor {
338360
if let syn::Expr::Call(call) = expr {
339361
// Check if it's Some(...) with trivial arguments
340362
if let syn::Expr::Path(path) = &*call.func {
341-
if path.path.segments.last().map(|seg| seg.ident == "Some").unwrap_or(false) {
363+
if path
364+
.path
365+
.segments
366+
.last()
367+
.map(|seg| seg.ident == "Some")
368+
.unwrap_or(false)
369+
{
342370
// Some(()) with unit type is trivial
343371
if call.args.len() == 1 {
344372
if let syn::Expr::Tuple(tuple) = &call.args[0] {
@@ -408,8 +436,10 @@ fn another_function() {
408436

409437
let violations = self.analyze(Path::new("test.rs"), content)?;
410438

411-
let unimplemented_violations: Vec<_> =
412-
violations.iter().filter(|v| v.rule_id.contains("unimplemented")).collect();
439+
let unimplemented_violations: Vec<_> = violations
440+
.iter()
441+
.filter(|v| v.rule_id.contains("unimplemented"))
442+
.collect();
413443

414444
if unimplemented_violations.is_empty() {
415445
return Err(GuardianError::analysis(
@@ -446,8 +476,10 @@ fn perform_operation() -> i32 {
446476

447477
let violations = self.analyze(Path::new("test.rs"), content)?;
448478

449-
let empty_violations: Vec<_> =
450-
violations.iter().filter(|v| v.rule_id == "empty_ok_return").collect();
479+
let empty_violations: Vec<_> = violations
480+
.iter()
481+
.filter(|v| v.rule_id == "empty_ok_return")
482+
.collect();
451483

452484
if empty_violations.is_empty() {
453485
return Err(GuardianError::analysis(
@@ -457,8 +489,9 @@ fn perform_operation() -> i32 {
457489
}
458490

459491
// Verify it caught the empty function
460-
let has_empty_function =
461-
empty_violations.iter().any(|v| v.message.contains("empty_function"));
492+
let has_empty_function = empty_violations
493+
.iter()
494+
.any(|v| v.message.contains("empty_function"));
462495

463496
if !has_empty_function {
464497
return Err(GuardianError::analysis(
@@ -502,8 +535,10 @@ mod tests {
502535

503536
let violations = self.analyze(Path::new("test.rs"), content)?;
504537

505-
let unimplemented_violations: Vec<_> =
506-
violations.iter().filter(|v| v.rule_id.contains("unimplemented")).collect();
538+
let unimplemented_violations: Vec<_> = violations
539+
.iter()
540+
.filter(|v| v.rule_id.contains("unimplemented"))
541+
.collect();
507542

508543
// Should find exactly one violation (from regular_function)
509544
if unimplemented_violations.len() != 1 {
@@ -534,8 +569,10 @@ fn main() {
534569
"#;
535570

536571
let violations = self.analyze(Path::new("src/main.rs"), content_without_header)?;
537-
let missing_header_violations: Vec<_> =
538-
violations.iter().filter(|v| v.rule_id == "quality_header_missing").collect();
572+
let missing_header_violations: Vec<_> = violations
573+
.iter()
574+
.filter(|v| v.rule_id == "quality_header_missing")
575+
.collect();
539576

540577
if missing_header_violations.is_empty() {
541578
return Err(GuardianError::analysis(
@@ -559,8 +596,10 @@ fn main() {
559596
"#;
560597

561598
let violations = self.analyze(Path::new("src/main.rs"), content_with_header)?;
562-
let header_violations: Vec<_> =
563-
violations.iter().filter(|v| v.rule_id == "quality_header_missing").collect();
599+
let header_violations: Vec<_> = violations
600+
.iter()
601+
.filter(|v| v.rule_id == "quality_header_missing")
602+
.collect();
564603

565604
if !header_violations.is_empty() {
566605
return Err(GuardianError::analysis(
@@ -583,7 +622,10 @@ fn main() {
583622
// This is acceptable behavior - the file would fail to compile anyway
584623
if !violations.is_empty() {
585624
// Log this as interesting but don't fail - pattern matching might still work
586-
tracing::debug!("Found {} violations in invalid syntax file", violations.len());
625+
tracing::debug!(
626+
"Found {} violations in invalid syntax file",
627+
violations.len()
628+
);
587629
}
588630

589631
Ok(())

src/cache/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ impl FileCache {
362362
impl Default for CacheMetadata {
363363
fn default() -> Self {
364364
let now = current_timestamp();
365-
Self { created_at: now, updated_at: now, hits: 0, misses: 0 }
365+
Self {
366+
created_at: now,
367+
updated_at: now,
368+
hits: 0,
369+
misses: 0,
370+
}
366371
}
367372
}
368373

src/config/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ impl GuardianConfig {
294294
if rule.case_sensitive {
295295
regex::Regex::new(&rule.pattern)
296296
} else {
297-
regex::RegexBuilder::new(&rule.pattern).case_insensitive(true).build()
297+
regex::RegexBuilder::new(&rule.pattern)
298+
.case_insensitive(true)
299+
.build()
298300
}
299301
.map_err(|e| {
300302
GuardianError::config(format!(
@@ -311,15 +313,16 @@ impl GuardianConfig {
311313

312314
/// Get all enabled rules across all categories
313315
pub fn enabled_rules(&self) -> impl Iterator<Item = (&String, &PatternCategory, &PatternRule)> {
314-
self.patterns.iter().filter(|(_, category)| category.enabled).flat_map(
315-
|(name, category)| {
316+
self.patterns
317+
.iter()
318+
.filter(|(_, category)| category.enabled)
319+
.flat_map(|(name, category)| {
316320
category
317321
.rules
318322
.iter()
319323
.filter(|rule| rule.enabled)
320324
.map(move |rule| (name, category, rule))
321-
},
322-
)
325+
})
323326
}
324327

325328
/// Get effective severity for a rule (rule override or category default)
@@ -412,7 +415,9 @@ pub struct ConfigBuilder {
412415
impl ConfigBuilder {
413416
/// Create a new builder with default configuration
414417
pub fn new() -> Self {
415-
Self { config: GuardianConfig::default() }
418+
Self {
419+
config: GuardianConfig::default(),
420+
}
416421
}
417422

418423
/// Add a path pattern
@@ -483,7 +488,11 @@ impl GuardianConfig {
483488
.ignore_file(".guardian_ignore")
484489
.build()?;
485490

486-
if !evolved_config.paths.patterns.contains(&"guardian/evolution/**".to_string()) {
491+
if !evolved_config
492+
.paths
493+
.patterns
494+
.contains(&"guardian/evolution/**".to_string())
495+
{
487496
return Err(GuardianError::config(
488497
"Configuration evolution failed to integrate new patterns".to_string(),
489498
));

0 commit comments

Comments
 (0)