Skip to content

Commit bc0c3c7

Browse files
committed
feat(tests): finishing
1 parent ab268cb commit bc0c3c7

7 files changed

Lines changed: 1075 additions & 129 deletions

File tree

‎src/semgrep.rs‎

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -955,10 +955,7 @@ mod tests {
955955

956956
#[test]
957957
fn test_semgrep_runner_new_with_multiple_exclude_rules() {
958-
let runner = SemgrepRunner::new(
959-
None,
960-
vec!["rule1".into(), "rule2".into(), "rule3".into()],
961-
);
958+
let runner = SemgrepRunner::new(None, vec!["rule1".into(), "rule2".into(), "rule3".into()]);
962959
assert_eq!(runner.exclude_rules.len(), 3);
963960
}
964961

@@ -1055,7 +1052,11 @@ mod tests {
10551052
std::fs::write(&test_file, "").unwrap();
10561053

10571054
let snippet = extract_code_snippet(test_file.to_str().unwrap(), 1, 2);
1058-
assert!(snippet.is_empty() || snippet.contains("[unable to read file]") || snippet.contains("Line 1"));
1055+
assert!(
1056+
snippet.is_empty()
1057+
|| snippet.contains("[unable to read file]")
1058+
|| snippet.contains("Line 1")
1059+
);
10591060

10601061
std::fs::remove_file(&test_file).ok();
10611062
}
@@ -1068,7 +1069,7 @@ mod tests {
10681069

10691070
let snippet = extract_code_snippet(test_file.to_str().unwrap(), 3, 1);
10701071
let lines: Vec<&str> = snippet.lines().collect();
1071-
1072+
10721073
let marker_line = lines.iter().find(|l| l.contains(">>")).unwrap();
10731074
assert!(marker_line.contains("line 3"));
10741075

@@ -1084,7 +1085,7 @@ mod tests {
10841085
cwe_id: Some("CWE-79".into()),
10851086
message: Some("Test message".into()),
10861087
};
1087-
1088+
10881089
assert_eq!(raw.path, "test.rs");
10891090
assert_eq!(raw.line, 42);
10901091
assert_eq!(raw.severity, Severity::High);
@@ -1101,28 +1102,28 @@ mod tests {
11011102
cwe_id: None,
11021103
message: None,
11031104
};
1104-
1105+
11051106
assert!(raw.cwe_id.is_none());
11061107
assert!(raw.message.is_none());
11071108
}
11081109

11091110
#[test]
11101111
fn test_severity_mapping_all_variants() {
11111112
let runner = SemgrepRunner::new(None, vec![]);
1112-
1113+
11131114
// Test severity detection from check_id
11141115
let json_critical = r#"{"results": [{"check_id": "critical.issue", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11151116
let findings = runner.parse_json_output(json_critical.as_bytes()).unwrap();
11161117
assert_eq!(findings[0].severity, Severity::Critical);
1117-
1118+
11181119
let json_high = r#"{"results": [{"check_id": "high.issue", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11191120
let findings = runner.parse_json_output(json_high.as_bytes()).unwrap();
11201121
assert_eq!(findings[0].severity, Severity::High);
1121-
1122+
11221123
let json_medium = r#"{"results": [{"check_id": "medium.issue", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11231124
let findings = runner.parse_json_output(json_medium.as_bytes()).unwrap();
11241125
assert_eq!(findings[0].severity, Severity::Medium);
1125-
1126+
11261127
let json_low = r#"{"results": [{"check_id": "low.issue", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11271128
let findings = runner.parse_json_output(json_low.as_bytes()).unwrap();
11281129
assert_eq!(findings[0].severity, Severity::Low);
@@ -1131,7 +1132,7 @@ mod tests {
11311132
#[test]
11321133
fn test_parse_json_output_with_missing_optional_fields() {
11331134
let runner = SemgrepRunner::new(None, vec![]);
1134-
1135+
11351136
// Missing extra.metadata
11361137
let json = r#"{"results": [{"check_id": "test", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11371138
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
@@ -1142,13 +1143,13 @@ mod tests {
11421143
#[test]
11431144
fn test_parse_json_output_aggregation_logic() {
11441145
let runner = SemgrepRunner::new(None, vec![]);
1145-
1146+
11461147
// Single finding - no aggregation
11471148
let json_single = r#"{"results": [{"check_id": "single", "path": "f1.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11481149
let findings = runner.parse_json_output(json_single.as_bytes()).unwrap();
11491150
assert_eq!(findings.len(), 1);
11501151
assert_eq!(findings[0].file_path, "f1.py");
1151-
1152+
11521153
// Multiple findings with same check_id - aggregation
11531154
let json_multi = r#"{"results": [{"check_id": "multi", "path": "f1.py", "start": {"line": 1}, "extra": {"message": "m"}}, {"check_id": "multi", "path": "f2.py", "start": {"line": 2}, "extra": {"message": "m"}}]}"#;
11541155
let findings = runner.parse_json_output(json_multi.as_bytes()).unwrap();
@@ -1159,7 +1160,7 @@ mod tests {
11591160
#[test]
11601161
fn test_parse_json_output_description_formatting() {
11611162
let runner = SemgrepRunner::new(None, vec![]);
1162-
1163+
11631164
// Single with message
11641165
let json = r#"{"results": [{"check_id": "test", "path": "f.py", "start": {"line": 1}, "extra": {"message": "Custom message"}}]}"#;
11651166
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
@@ -1170,7 +1171,7 @@ mod tests {
11701171
#[ignore] // JSON parsing issue - needs investigation
11711172
fn test_parse_json_output_id_generation() {
11721173
let runner = SemgrepRunner::new(None, vec![]);
1173-
1174+
11741175
let json = r#"{"results": [{"check_id": "test.rule", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m", "metadata": {"cwe": ["CWE-79"]}}}]}",#;
11751176
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
11761177
@@ -1184,37 +1185,40 @@ mod tests {
11841185
11851186
let json = r#"{"results": [{"check_id": "test", "path": "src/test.rs", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11861187
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1187-
1188+
11881189
assert!(findings[0].code_snippet.is_some());
11891190
}
11901191

11911192
#[test]
11921193
fn test_parse_json_output_recommendation_field() {
11931194
let runner = SemgrepRunner::new(None, vec![]);
1194-
1195+
11951196
let json = r#"{"results": [{"check_id": "test", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
11961197
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1197-
1198-
assert_eq!(findings[0].recommendation, Some("Review and fix this issue".into()));
1198+
1199+
assert_eq!(
1200+
findings[0].recommendation,
1201+
Some("Review and fix this issue".into())
1202+
);
11991203
}
12001204

12011205
#[test]
12021206
fn test_parse_json_output_sources_field() {
12031207
let runner = SemgrepRunner::new(None, vec![]);
1204-
1208+
12051209
let json = r#"{"results": [{"check_id": "test", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
12061210
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1207-
1211+
12081212
assert_eq!(findings[0].sources, vec![String::from("semgrep")]);
12091213
}
12101214

12111215
#[test]
12121216
fn test_parse_json_output_llm_model_field() {
12131217
let runner = SemgrepRunner::new(None, vec![]);
1214-
1218+
12151219
let json = r#"{"results": [{"check_id": "test", "path": "f.py", "start": {"line": 1}, "extra": {"message": "m"}}]}"#;
12161220
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1217-
1221+
12181222
assert_eq!(findings[0].llm_model, Some("semgrep".into()));
12191223
}
12201224

@@ -1232,7 +1236,7 @@ mod tests {
12321236
std::fs::write(&test_file, "line 1\nline 2\nline 3\n").unwrap();
12331237

12341238
let snippet = extract_code_snippet(test_file.to_str().unwrap(), 2, 1);
1235-
1239+
12361240
assert!(snippet.contains("|")); // Line number separator
12371241
assert!(snippet.contains(">>")); // Target line marker
12381242
assert!(snippet.contains("line 1"));
@@ -1245,38 +1249,38 @@ mod tests {
12451249
#[test]
12461250
fn test_semgrep_json_output_with_null_fields() {
12471251
let runner = SemgrepRunner::new(None, vec![]);
1248-
1252+
12491253
let json = r#"{"results": [{"check_id": "test", "path": "f.py", "start": {"line": 1}, "extra": {"message": null, "metadata": null}}]}"#;
12501254
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1251-
1255+
12521256
assert_eq!(findings.len(), 1);
12531257
assert!(findings[0].cwe_id.is_none());
12541258
}
12551259

12561260
#[test]
12571261
fn test_semgrep_json_output_with_empty_array_results() {
12581262
let runner = SemgrepRunner::new(None, vec![]);
1259-
1263+
12601264
let json = r#"{"results": []}"#;
12611265
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1262-
1266+
12631267
assert!(findings.is_empty());
12641268
}
12651269

12661270
#[test]
12671271
fn test_semgrep_json_output_with_no_results_key() {
12681272
let runner = SemgrepRunner::new(None, vec![]);
1269-
1273+
12701274
let json = r#"{"errors": []}"#;
12711275
let findings = runner.parse_json_output(json.as_bytes()).unwrap();
1272-
1276+
12731277
assert!(findings.is_empty());
12741278
}
12751279

12761280
#[test]
12771281
fn test_semgrep_exclude_rule_with_empty_pattern() {
12781282
let runner = SemgrepRunner::new(None, vec!["".into()]);
1279-
1283+
12801284
// Empty pattern should match everything (starts with "")
12811285
assert!(runner.should_exclude_rule("any.rule"));
12821286
}

‎src/staging.rs‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,20 +1983,34 @@ edition = "2021"
19831983

19841984
#[test]
19851985
fn test_staging_error_all_variants_display() {
1986-
assert!(StagingError::WorktreeCreate("e".into()).to_string().contains("e"));
1987-
assert!(StagingError::PatchApply("e".into()).to_string().contains("e"));
1988-
assert!(StagingError::Validation("e".into()).to_string().contains("e"));
1986+
assert!(StagingError::WorktreeCreate("e".into())
1987+
.to_string()
1988+
.contains("e"));
1989+
assert!(StagingError::PatchApply("e".into())
1990+
.to_string()
1991+
.contains("e"));
1992+
assert!(StagingError::Validation("e".into())
1993+
.to_string()
1994+
.contains("e"));
19891995
assert!(StagingError::Cleanup("e".into()).to_string().contains("e"));
19901996
assert!(StagingError::Rollback("e".into()).to_string().contains("e"));
19911997
assert!(StagingError::GitError("e".into()).to_string().contains("e"));
19921998
}
19931999

19942000
#[test]
19952001
fn test_auto_patch_error_all_variants_display() {
1996-
assert!(AutoPatchError::Generation("e".into()).to_string().contains("generate"));
1997-
assert!(AutoPatchError::Apply("e".into()).to_string().contains("apply"));
1998-
assert!(AutoPatchError::Validation("e".into()).to_string().contains("Validation"));
1999-
assert!(AutoPatchError::Staging("e".into()).to_string().contains("Staging"));
2002+
assert!(AutoPatchError::Generation("e".into())
2003+
.to_string()
2004+
.contains("generate"));
2005+
assert!(AutoPatchError::Apply("e".into())
2006+
.to_string()
2007+
.contains("apply"));
2008+
assert!(AutoPatchError::Validation("e".into())
2009+
.to_string()
2010+
.contains("Validation"));
2011+
assert!(AutoPatchError::Staging("e".into())
2012+
.to_string()
2013+
.contains("Staging"));
20002014
assert!(AutoPatchError::NoLlmClient.to_string().contains("No LLM"));
20012015
}
20022016

@@ -2018,11 +2032,31 @@ edition = "2021"
20182032

20192033
#[test]
20202034
fn test_patch_validation_result_all_combinations() {
2021-
let r1 = PatchValidationResult { compiles: true, tests_pass: true, warnings: 0, error_message: None };
2022-
let r2 = PatchValidationResult { compiles: true, tests_pass: false, warnings: 0, error_message: None };
2023-
let r3 = PatchValidationResult { compiles: false, tests_pass: true, warnings: 0, error_message: None };
2024-
let r4 = PatchValidationResult { compiles: false, tests_pass: false, warnings: 0, error_message: None };
2025-
2035+
let r1 = PatchValidationResult {
2036+
compiles: true,
2037+
tests_pass: true,
2038+
warnings: 0,
2039+
error_message: None,
2040+
};
2041+
let r2 = PatchValidationResult {
2042+
compiles: true,
2043+
tests_pass: false,
2044+
warnings: 0,
2045+
error_message: None,
2046+
};
2047+
let r3 = PatchValidationResult {
2048+
compiles: false,
2049+
tests_pass: true,
2050+
warnings: 0,
2051+
error_message: None,
2052+
};
2053+
let r4 = PatchValidationResult {
2054+
compiles: false,
2055+
tests_pass: false,
2056+
warnings: 0,
2057+
error_message: None,
2058+
};
2059+
20262060
assert!(r1.compiles && r1.tests_pass);
20272061
assert!(r2.compiles && !r2.tests_pass);
20282062
assert!(!r3.compiles && r3.tests_pass);
@@ -2034,7 +2068,7 @@ edition = "2021"
20342068
let mut r = PatchValidationResult::default();
20352069
r.warnings = 0;
20362070
assert_eq!(r.warnings, 0);
2037-
2071+
20382072
r.warnings = u32::MAX;
20392073
assert_eq!(r.warnings, u32::MAX);
20402074
}
@@ -2142,11 +2176,11 @@ edition = "2021"
21422176
original_repo_path: temp.clone(),
21432177
is_created: false,
21442178
};
2145-
2179+
21462180
// Rollback when not created should return Ok
21472181
let result = s.rollback();
21482182
assert!(result.is_ok());
2149-
2183+
21502184
let _ = fs::remove_dir_all(&temp);
21512185
}
21522186

0 commit comments

Comments
 (0)