patchloom 0.11.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Unit tests for tidy check/fix.

use super::check::{check_file, collect_issues};
use super::fix::eol_mode_to_str;
use super::*;
use crate::cli::global::GlobalFlags;
use crate::exit;
use tempfile::TempDir;

#[test]
fn detects_missing_final_newline() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("no_newline.txt");
    std::fs::write(&file, b"hello").unwrap();

    let issues = check_file(&file, true, None, true);
    assert!(
        issues.iter().any(|i| i.issue == "missing final newline"),
        "expected missing final newline issue, got: {issues:?}"
    );
}

#[test]
fn empty_file_no_missing_newline() {
    // Regression: empty files should not be flagged for "missing final newline"
    // since they have no content at all.
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("empty.txt");
    std::fs::write(&file, b"").unwrap();

    let issues = check_file(&file, true, None, true);
    assert!(
        !issues.iter().any(|i| i.issue == "missing final newline"),
        "empty file should not be flagged for missing final newline: {issues:?}"
    );
}

#[test]
fn detects_mixed_line_endings() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("mixed.txt");
    // First line uses CRLF, second uses bare LF.
    std::fs::write(&file, b"line1\r\nline2\nline3\n").unwrap();

    let issues = check_file(&file, true, None, true);
    assert!(
        issues.iter().any(|i| i.issue == "mixed line endings"),
        "expected mixed line endings issue, got: {issues:?}"
    );
}

#[test]
fn detects_trailing_whitespace() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("trailing.txt");
    std::fs::write(&file, b"hello   \nworld\n").unwrap();

    let issues = check_file(&file, true, None, true);
    let trailing: Vec<_> = issues
        .iter()
        .filter(|i| i.issue == "trailing whitespace")
        .collect();
    assert_eq!(trailing.len(), 1, "expected 1 trailing whitespace issue");
    assert_eq!(trailing[0].line, Some(1));
}

#[test]
fn clean_file_produces_no_issues_and_exit_zero() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("clean.txt");
    std::fs::write(&file, b"hello\nworld\n").unwrap();

    let issues = check_file(&file, true, None, true);
    assert!(issues.is_empty(), "expected no issues for clean file");

    let global = GlobalFlags::test_with_cwd(tmp.path());
    let args = TidyArgs {
        action: TidyAction::Check {
            paths: vec![".".to_string()],
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);
}

#[test]
fn multiple_issues_in_one_file() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("multi.txt");
    // Missing final newline + trailing whitespace on line 1 + mixed endings.
    std::fs::write(&file, b"hello \r\nworld\nfoo").unwrap();

    let issues = check_file(&file, true, None, true);
    let issue_types: Vec<&str> = issues.iter().map(|i| i.issue).collect();
    assert!(
        issue_types.contains(&"missing final newline"),
        "missing final newline not found in {issue_types:?}"
    );
    assert!(
        issue_types.contains(&"mixed line endings"),
        "mixed line endings not found in {issue_types:?}"
    );
    assert!(
        issue_types.contains(&"trailing whitespace"),
        "trailing whitespace not found in {issue_types:?}"
    );
}

#[test]
fn binary_files_are_skipped() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("binary.bin");
    // Binary content with NUL bytes, missing final newline, trailing ws.
    let mut data = b"hello \nworld".to_vec();
    data.insert(3, 0x00);
    std::fs::write(&file, &data).unwrap();

    let issues = check_file(&file, true, None, true);
    assert!(
        issues.is_empty(),
        "expected no issues for binary file, got: {issues:?}"
    );
}

#[test]
fn large_binary_files_are_skipped_via_header_probe() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("large.bin");
    let mut data = vec![b'a'; 10_000];
    data[4096] = 0;
    std::fs::write(&file, &data).unwrap();

    let issues = check_file(&file, true, None, true);
    assert!(
        issues.is_empty(),
        "expected no issues for large binary file, got: {issues:?}"
    );
}

#[test]
fn glob_filtering_works() {
    let tmp = TempDir::new().unwrap();
    // Create two files: one .rs and one .txt. The .txt has an issue.
    let rs_file = tmp.path().join("clean.rs");
    std::fs::write(&rs_file, b"fn main() {}\n").unwrap();

    let txt_file = tmp.path().join("dirty.txt");
    std::fs::write(&txt_file, b"no newline").unwrap();

    // Filter to only *.rs — should find no issues.
    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.glob = vec!["*.rs".to_string()];

    let issues = collect_issues(&[".".to_string()], &global).unwrap();
    assert!(
        issues.is_empty(),
        "expected no issues when filtering to *.rs, got: {issues:?}"
    );

    // Filter to only *.txt — should find the missing newline.
    global.glob = vec!["*.txt".to_string()];
    let issues = collect_issues(&[".".to_string()], &global).unwrap();
    assert!(
        !issues.is_empty(),
        "expected issues when filtering to *.txt"
    );
    assert!(issues.iter().any(|i| i.issue == "missing final newline"));
}

#[test]
fn check_returns_changes_detected_for_dirty_file() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("no_newline.txt");
    std::fs::write(&file, b"hello").unwrap();

    let global = GlobalFlags::test_with_cwd(tmp.path());
    let args = TidyArgs {
        action: TidyAction::Check {
            paths: vec![".".to_string()],
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::CHANGES_DETECTED);
}

#[test]
fn fix_adds_missing_newline() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("no_nl.txt");
    std::fs::write(&file, b"hello").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.ensure_final_newline = true;
    global.apply = true;

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read(&file).unwrap();
    assert!(
        content.ends_with(b"\n"),
        "expected file to end with newline after fix"
    );
}

/// Bare `tidy fix --apply` must fix issues that bare `tidy check` reports
/// (final newline + trailing whitespace). fixrealloop feature gap.
#[test]
fn fix_defaults_match_check_parity() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("messy.txt");
    // Trailing spaces and no final newline — both reported by tidy check.
    std::fs::write(&file, b"line  ").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.apply = true;
    // Deliberately leave ensure_final_newline / trim_trailing_whitespace false
    // so the check-parity defaults must kick in.

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read_to_string(&file).unwrap();
    assert_eq!(
        content, "line\n",
        "bare tidy fix should trim trailing ws and add final newline"
    );
}

/// Explicit single-policy flag must not auto-enable the other defaults.
#[test]
fn fix_explicit_trim_only_does_not_force_final_newline() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("trim_only.txt");
    std::fs::write(&file, b"line  ").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.apply = true;
    global.trim_trailing_whitespace = true;
    // ensure_final_newline left false; because an explicit policy flag is set,
    // check-parity defaults must not force final newline.

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read_to_string(&file).unwrap();
    assert_eq!(
        content, "line",
        "explicit --trim-trailing-whitespace only should not force final newline"
    );
}

#[test]
fn fix_normalizes_eol() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("crlf.txt");
    std::fs::write(&file, b"line1\r\nline2\r\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.normalize_eol = Some(crate::cli::global::EolMode::Lf);
    global.apply = true;

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read(&file).unwrap();
    assert!(
        !content.windows(2).any(|w| w == b"\r\n"),
        "expected no CRLF sequences after fix"
    );
}

#[test]
fn fix_trims_trailing_whitespace() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("trailing.txt");
    std::fs::write(&file, b"hello   \nworld\t\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.trim_trailing_whitespace = true;
    global.apply = true;

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read_to_string(&file).unwrap();
    assert_eq!(content, "hello\nworld\n");
}

#[test]
fn fix_is_idempotent_on_clean_file() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("clean.txt");
    std::fs::write(&file, b"hello\nworld\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.ensure_final_newline = true;
    global.trim_trailing_whitespace = true;
    global.normalize_eol = Some(crate::cli::global::EolMode::Lf);
    global.apply = true;

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read_to_string(&file).unwrap();
    assert_eq!(content, "hello\nworld\n");
}

#[test]
fn fix_dry_run_returns_changes_detected_when_dirty() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("no_nl.txt");
    std::fs::write(&file, b"hello").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.ensure_final_newline = true;
    // No --apply: default dry-run mode.

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::CHANGES_DETECTED);

    // File must not be modified in dry-run mode.
    let content = std::fs::read(&file).unwrap();
    assert_eq!(content, b"hello");
}

#[test]
fn fix_dry_run_returns_success_when_clean() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("clean.txt");
    std::fs::write(&file, b"hello\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.ensure_final_newline = true;
    // No --apply: default dry-run mode.

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);
}

#[test]
fn fix_apply_creates_backup_session() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("no_nl.txt");
    std::fs::write(&file, b"hello").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.ensure_final_newline = true;
    global.apply = true;

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let backup_dir = tmp.path().join(".patchloom").join("backups");
    assert!(
        backup_dir.exists(),
        "backup directory should exist after tidy fix --apply"
    );
    let sessions: Vec<_> = std::fs::read_dir(&backup_dir)
        .unwrap()
        .filter_map(|e| e.ok())
        .collect();
    assert!(
        !sessions.is_empty(),
        "at least one backup session should be created"
    );
}

#[test]
fn eol_mode_conversion_round_trips() {
    use crate::cli::global::EolMode;
    assert_eq!(eol_mode_to_str(EolMode::Lf), "lf");
    assert_eq!(eol_mode_to_str(EolMode::Crlf), "crlf");
    assert_eq!(eol_mode_to_str(EolMode::Cr), "cr");
    assert_eq!(eol_mode_to_str(EolMode::Keep), "keep");
}

/// Regression: `tidy check --quiet --json` must still emit JSON output.
/// The old code gated render_issues entirely on `!global.quiet`, which
/// suppressed JSON/JSONL output. Only human-readable text should be
/// suppressed by --quiet.
#[test]
fn check_quiet_still_emits_json() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("no_nl.txt");
    std::fs::write(&file, b"hello").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.quiet = true;
    global.json = true;

    // Collect issues directly (render_issues is called by run()).
    let issues = collect_issues(&[".".to_string()], &global).unwrap();
    assert!(!issues.is_empty(), "should detect issues even when quiet");

    // Verify that run() returns CHANGES_DETECTED (not SUCCESS) with --quiet.
    let args = TidyArgs {
        action: TidyAction::Check {
            paths: vec![".".to_string()],
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(
        code,
        exit::CHANGES_DETECTED,
        "exit code must reflect issues even with --quiet"
    );
}

/// #1176: collapse_blanks flag must be forwarded from global flags
/// into TidyFix operations so the tx engine can apply it.
#[test]
fn collapse_blanks_flag_forwarded_to_tidy_fix() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("blank.txt");
    // File with consecutive blank lines.
    std::fs::write(&file, "a\n\n\n\nb\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.apply = true;
    global.collapse_blanks = true;

    let args = TidyArgs {
        action: TidyAction::Fix {
            paths: vec![".".to_string()],
            dedent: None,
            indent: None,
            lines: None,
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(code, exit::SUCCESS);

    let content = std::fs::read_to_string(&file).unwrap();
    // Consecutive blank lines should be collapsed to at most one.
    assert_eq!(content, "a\n\nb\n");
}

/// Regression: `tidy check --normalize-eol lf` must detect files with
/// consistent CRLF line endings (not just mixed endings). Without this,
/// agents think CRLF files are clean when a LF target is specified.
#[test]
fn check_detects_crlf_when_eol_target_is_lf() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("crlf.txt");
    std::fs::write(&file, b"line1\r\nline2\r\n").unwrap();

    // Without --normalize-eol: no EOL issue (consistent CRLF is fine).
    let issues = check_file(&file, true, None, true);
    assert!(
        !issues.iter().any(|i| i.issue.contains("normalization")),
        "no normalization issue without eol target: {issues:?}"
    );

    // With --normalize-eol lf: should detect CRLF as needing normalization.
    let issues = check_file(&file, true, Some(crate::write::EolMode::Lf), true);
    assert!(
        issues
            .iter()
            .any(|i| i.issue.contains("normalization to LF")),
        "expected LF normalization issue, got: {issues:?}"
    );

    // End-to-end via run(): should return CHANGES_DETECTED.
    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.normalize_eol = Some(crate::write::EolMode::Lf);
    let args = TidyArgs {
        action: TidyAction::Check {
            paths: vec![".".to_string()],
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(
        code,
        exit::CHANGES_DETECTED,
        "tidy check --normalize-eol lf must return CHANGES_DETECTED for CRLF file"
    );
}

/// `tidy check --normalize-eol crlf` must detect files with bare LF.
#[test]
fn check_detects_lf_when_eol_target_is_crlf() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("lf.txt");
    std::fs::write(&file, b"line1\nline2\n").unwrap();

    let issues = check_file(&file, true, Some(crate::write::EolMode::Crlf), true);
    assert!(
        issues
            .iter()
            .any(|i| i.issue.contains("normalization to CRLF")),
        "expected CRLF normalization issue, got: {issues:?}"
    );
}

/// Files that already match the target EOL should not be flagged.
#[test]
fn check_no_eol_issue_when_already_matching() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("lf.txt");
    std::fs::write(&file, b"line1\nline2\n").unwrap();

    let issues = check_file(&file, true, Some(crate::write::EolMode::Lf), true);
    assert!(
        !issues.iter().any(|i| i.issue.contains("normalization")),
        "LF file with LF target should not be flagged: {issues:?}"
    );
}

/// Regression: `tidy check --respect-editorconfig` must detect EOL
/// mismatches defined in `.editorconfig`.  Without this fix,
/// `collect_issues()` only passed `global.normalize_eol` (which is
/// `None` without explicit `--normalize-eol`), so editorconfig's
/// `end_of_line` setting was silently ignored in check mode.
#[test]
fn check_respect_editorconfig_detects_eol_mismatch() {
    let tmp = TempDir::new().unwrap();

    // .editorconfig: *.txt must use CRLF
    std::fs::write(
        tmp.path().join(".editorconfig"),
        "root = true\n\n[*.txt]\nend_of_line = crlf\n",
    )
    .unwrap();

    // File has LF endings (mismatches editorconfig)
    let file = tmp.path().join("test.txt");
    std::fs::write(&file, b"line1\nline2\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.respect_editorconfig = true;

    let args = TidyArgs {
        action: TidyAction::Check {
            paths: vec![".".to_string()],
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(
        code,
        exit::CHANGES_DETECTED,
        "tidy check --respect-editorconfig must detect LF when editorconfig says CRLF"
    );
}

/// Regression: `tidy check --respect-editorconfig` must suppress trailing
/// whitespace diagnostics for file types where `.editorconfig` declares
/// `trim_trailing_whitespace = false` (e.g. `[*.md]`).  Without this fix,
/// `check_file()` always flagged trailing whitespace regardless of the
/// editorconfig setting, causing false positives on Markdown files where
/// trailing spaces are intentional (e.g. hard line breaks).
#[test]
fn check_respect_editorconfig_suppresses_trailing_ws() {
    let tmp = TempDir::new().unwrap();

    // .editorconfig: *.md must NOT trim trailing whitespace
    std::fs::write(
        tmp.path().join(".editorconfig"),
        "root = true\n\n[*.md]\ntrim_trailing_whitespace = false\n",
    )
    .unwrap();

    // Markdown file with intentional trailing whitespace (hard line break)
    let file = tmp.path().join("readme.md");
    std::fs::write(&file, "Hello  \nWorld\n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.respect_editorconfig = true;

    let issues = collect_issues(&[".".to_string()], &global).unwrap();
    assert!(
        !issues.iter().any(|i| i.issue == "trailing whitespace"),
        "trailing whitespace should not be reported when editorconfig says \
         trim_trailing_whitespace = false, got: {issues:?}"
    );

    // End-to-end: should return SUCCESS (no issues)
    let args = TidyArgs {
        action: TidyAction::Check {
            paths: vec![".".to_string()],
        },
        write: Default::default(),
    };
    let code = run(args, &global).unwrap();
    assert_eq!(
        code,
        exit::SUCCESS,
        "tidy check --respect-editorconfig must not flag trailing ws \
         when editorconfig says trim_trailing_whitespace = false"
    );
}

/// `tidy check --respect-editorconfig` still flags trailing whitespace
/// when editorconfig explicitly sets `trim_trailing_whitespace = true`.
#[test]
fn check_respect_editorconfig_flags_trailing_ws_when_true() {
    let tmp = TempDir::new().unwrap();

    std::fs::write(
        tmp.path().join(".editorconfig"),
        "root = true\n\n[*.rs]\ntrim_trailing_whitespace = true\n",
    )
    .unwrap();

    let file = tmp.path().join("main.rs");
    std::fs::write(&file, "fn main()  \n").unwrap();

    let mut global = GlobalFlags::test_with_cwd(tmp.path());
    global.respect_editorconfig = true;

    let issues = collect_issues(&[".".to_string()], &global).unwrap();
    assert!(
        issues.iter().any(|i| i.issue == "trailing whitespace"),
        "trailing whitespace should still be flagged when editorconfig says \
         trim_trailing_whitespace = true, got: {issues:?}"
    );
}

/// `check_file` with `check_trailing_ws = false` suppresses trailing ws.
#[test]
fn check_file_skips_trailing_ws_when_disabled() {
    let tmp = TempDir::new().unwrap();
    let file = tmp.path().join("test.txt");
    std::fs::write(&file, "hello   \nworld\n").unwrap();

    // With check_trailing_ws = true: should find trailing whitespace
    let issues = check_file(&file, true, None, true);
    assert!(
        issues.iter().any(|i| i.issue == "trailing whitespace"),
        "should find trailing ws with check_trailing_ws=true"
    );

    // With check_trailing_ws = false: should NOT find trailing whitespace
    let issues = check_file(&file, true, None, false);
    assert!(
        !issues.iter().any(|i| i.issue == "trailing whitespace"),
        "should not find trailing ws with check_trailing_ws=false"
    );
}