garbage-code-hunter 0.2.2

A humorous Rust code quality detector that roasts your garbage code
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Test the CLI interface by running the actual binary
#[test]
fn test_cli_help() {
    let output = Command::new("cargo")
        .args(["run", "--", "analyze", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("garbage-code-hunter") || stdout.contains("Analyze"));
    assert!(stdout.contains("--verbose"));
    assert!(stdout.contains("--lang"));
    assert!(stdout.contains("--markdown"));
}

#[test]
fn test_cli_version_info() {
    let output = Command::new("cargo")
        .args(["run", "--", "--version"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("garbage-code-hunter")
            && (stdout.contains("0.2.1") || stdout.contains("0.2.2"))
    );
}

#[test]
fn test_cli_with_garbage_file() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("garbage.rs");

    let garbage_code = r#"
fn main() {
    let data = "hello";
    let temp = 42;
    let a = 10;
    let result = Some(42).unwrap();
}
"#;

    fs::write(&file_path, garbage_code).expect("Failed to write test file");

    let output = Command::new("cargo")
        .args(["run", "--", "analyze", file_path.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain some analysis output
    assert!(stdout.contains("垃圾代码") || stdout.contains("Garbage Code"));
}

#[test]
fn test_cli_english_output() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(&file_path, "fn main() { let thing = \"test\"; }")
        .expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--lang",
            "en-US",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain English output
    assert!(stdout.contains("Garbage Code Hunter") || stdout.contains("variable"));
}

#[test]
fn test_cli_chinese_output() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(&file_path, "fn main() { let thing = \"test\"; }")
        .expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--lang",
            "zh-CN",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain Chinese output
    assert!(stdout.contains("垃圾代码") || stdout.contains("变量"));
}

/// Objective: Verify analyze JSON exposes issues and Style IR summary.
/// Invariants: JSON output must remain parseable and include stable summary fields.
#[test]
fn test_cli_json_includes_style_ir_summary() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(
        &file_path,
        "fn main() { let result = Some(42).unwrap(); println!(\"debug\"); }",
    )
    .expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--format",
            "json",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "analyze JSON command should exit successfully"
    );
    let parsed: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("analyze JSON output should be valid JSON");

    assert!(
        parsed
            .get("issues")
            .and_then(|value| value.as_array())
            .is_some(),
        "analyze JSON should include an issues array"
    );
    assert_eq!(
        parsed
            .get("schema_version")
            .and_then(|value| value.as_str()),
        Some("1.0")
    );
    assert!(
        parsed
            .get("files")
            .and_then(|value| value.as_array())
            .is_some(),
        "analyze JSON should include a files array"
    );
    assert_eq!(
        parsed
            .get("summary")
            .and_then(|value| value.get("issue_count"))
            .and_then(|value| value.as_u64()),
        parsed
            .get("issues")
            .and_then(|value| value.as_array())
            .map(|issues| issues.len() as u64)
    );
    let summary = parsed
        .get("style_ir_summary")
        .expect("analyze JSON should include style_ir_summary");
    assert_eq!(
        summary.get("language").and_then(|value| value.as_str()),
        Some("Rust")
    );
    assert!(
        summary.get("thresholds").is_some(),
        "style_ir_summary should include thresholds"
    );
    assert!(
        summary
            .get("panic_call_count")
            .and_then(|value| value.as_u64())
            .unwrap_or_default()
            >= 1,
        "style_ir_summary should expose panic call count"
    );
}

#[test]
fn test_cli_markdown_output() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(&file_path, "fn main() { let data = \"test\"; }").expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--markdown",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain markdown formatting
    assert!(stdout.contains("#") && (stdout.contains("|") || stdout.contains("**")));
}

#[test]
fn test_cli_summary_only() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(&file_path, "fn main() { let data = \"test\"; }").expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--summary",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Summary mode should be shorter
    assert!(!stdout.is_empty());
}

#[test]
fn test_cli_verbose_mode() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(
        &file_path,
        "fn main() { let data = \"test\"; let temp = 42; }",
    )
    .expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--verbose",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Verbose mode should contain detailed analysis
    assert!(stdout.contains("MUTATION ANALYSIS") || stdout.contains("Personality"));
}

#[test]
fn test_cli_issues_limit() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    let code_with_many_issues = r#"
fn main() {
    let data = "test";
    let temp = 42;
    let info = vec![1, 2, 3];
    let obj = String::new();
    let a = 1;
    let b = 2;
    let c = 3;
}
"#;

    fs::write(&file_path, code_with_many_issues).expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--issues",
            "2",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
}

#[test]
fn test_cli_exclude_patterns() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    // Create files that should be excluded
    let excluded_file = temp_dir.path().join("test_excluded.rs");
    let included_file = temp_dir.path().join("included.rs");

    fs::write(&excluded_file, "fn main() { let data = \"test\"; }")
        .expect("Failed to write excluded file");
    fs::write(&included_file, "fn main() { let temp = 42; }")
        .expect("Failed to write included file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--exclude",
            "test_*",
            temp_dir.path().to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    // The excluded file should not appear in the analysis
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(!stdout.contains("test_excluded.rs"));
}

#[test]
fn test_cli_harsh_mode() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("test.rs");

    fs::write(&file_path, "fn main() { let data = \"test\"; }").expect("Failed to write test file");

    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--harsh",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
}

#[test]
fn test_cli_nonexistent_file() {
    let output = Command::new("cargo")
        .args(["run", "--", "analyze", "nonexistent_file.rs"])
        .output()
        .expect("Failed to execute command");

    // Should handle nonexistent files gracefully
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should show clean code message or handle gracefully
    assert!(
        stdout.contains("垃圾")
            || stdout.contains("Garbage")
            || stdout.contains("clean")
            || stdout.contains("Wow")
    );
}

#[test]
fn test_cli_empty_directory() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    let output = Command::new("cargo")
        .args(["run", "--", "analyze", temp_dir.path().to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should handle empty directories gracefully
    assert!(
        stdout.contains("垃圾")
            || stdout.contains("Garbage")
            || stdout.contains("clean")
            || stdout.contains("Wow")
    );
}

#[test]
fn test_cli_invalid_rust_file() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let file_path = temp_dir.path().join("invalid.rs");

    // Write invalid Rust code
    fs::write(&file_path, "this is not valid rust code { } } {")
        .expect("Failed to write invalid file");

    let output = Command::new("cargo")
        .args(["run", "--", "analyze", file_path.to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    // Should handle invalid Rust files gracefully
    assert!(output.status.success());
}

#[test]
fn test_cli_brief_mode() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let file_path = temp_dir.path().join("garbage.rs");
    fs::write(
        &file_path,
        "fn main() { let x = 42; let y = Some(1).unwrap(); }",
    )
    .expect("Failed to write test file");

    // Brief mode should skip boss file and mutation chains
    let output = Command::new("cargo")
        .args([
            "run",
            "--",
            "analyze",
            "--brief",
            file_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success(), "brief mode should exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain personality and verdict (non-brief sections)
    assert!(
        stdout.contains("Personality") || stdout.contains("VERDICT"),
        "brief output should contain personality/verdict"
    );

    // Should NOT contain boss file section
    assert!(
        !stdout.contains("FINAL BOSS"),
        "brief output should not contain FINAL BOSS section"
    );
}