patchloom 0.8.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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Project configuration file support (.patchloom.toml).
//!
//! Searches from the working directory upward for a `.patchloom.toml` file and
//! parses it into [`ProjectConfig`]. CLI flags override config file values.

use serde::Deserialize;
use std::path::{Path, PathBuf};

use crate::write::WritePolicyOverride;

/// Project-level configuration loaded from `.patchloom.toml`.
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct ProjectConfig {
    pub write_policy: WritePolicyOverride,
    pub exclude: Exclude,
    pub output: Output,
    pub tx: TxConfig,
    pub defaults: Defaults,
    pub format: FormatConfig,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct Defaults {
    /// Default to --apply mode. When true, write commands apply changes without needing --apply flag.
    pub apply: Option<bool>,
    /// Default format command (e.g., "cargo fmt"). Applied after --apply unless overridden by CLI --format.
    pub format: Option<String>,
}

/// Format configuration: per-extension formatters and auto-format toggle.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct FormatConfig {
    /// When true, run formatters automatically after --apply without needing --format.
    pub auto: Option<bool>,
    /// Single catch-all formatter command (runs on the entire cwd).
    pub command: Option<String>,
    /// Per-extension formatter commands. Keys are file extensions (without dot),
    /// values are shell commands. The file path is appended as the last argument.
    #[serde(default)]
    pub by_extension: std::collections::HashMap<String, String>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct TxConfig {
    pub strict: Option<bool>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct Exclude {
    #[serde(default)]
    pub globs: Vec<String>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct Output {
    pub color: Option<String>,
}

/// Search for `.patchloom.toml` starting from `start` and walking up to the
/// filesystem root. Returns the parsed config and its directory, or `None` if
/// no config file is found.
pub fn find_and_load(start: &Path) -> Option<(ProjectConfig, PathBuf)> {
    let mut dir = start.to_path_buf();
    loop {
        let candidate = dir.join(".patchloom.toml");
        if candidate.is_file() {
            let content = match std::fs::read_to_string(&candidate) {
                Ok(c) => c,
                Err(e) => {
                    eprintln!("warning: could not read {}: {}", candidate.display(), e);
                    return None;
                }
            };
            match toml_edit::de::from_str::<ProjectConfig>(&content) {
                Ok(config) => return Some((config, dir)),
                Err(e) => {
                    eprintln!("warning: malformed {}: {}", candidate.display(), e);
                    return None;
                }
            }
        }
        // Stop at repo root to avoid loading configs from parent directories.
        if dir.join(".git").exists() {
            return None;
        }
        if !dir.pop() {
            return None;
        }
    }
}

/// Merge a [`ProjectConfig`] into [`GlobalFlags`](crate::cli::global::GlobalFlags), applying config values only
/// where the CLI did not explicitly set them.
///
/// Apply config values as defaults into GlobalFlags (CLI flags still win where set).
/// Available for library tx execution as well as CLI.
pub fn apply_config(global: &mut crate::cli::global::GlobalFlags, config: &ProjectConfig) {
    // Write policy: config provides defaults, CLI flags win.
    if !global.ensure_final_newline && config.write_policy.ensure_final_newline == Some(true) {
        global.ensure_final_newline = true;
    }
    if global.normalize_eol.is_none() {
        match config.write_policy.normalize_eol.as_deref() {
            Some("lf") => global.normalize_eol = Some(crate::cli::global::EolMode::Lf),
            Some("crlf") => global.normalize_eol = Some(crate::cli::global::EolMode::Crlf),
            Some("cr") => global.normalize_eol = Some(crate::cli::global::EolMode::Cr),
            Some("keep") => {} // explicit keep = default behavior, no-op
            Some(invalid) => {
                eprintln!("{}", invalid_normalize_eol_warning(invalid));
            }
            None => {}
        }
    }
    if !global.trim_trailing_whitespace
        && config.write_policy.trim_trailing_whitespace == Some(true)
    {
        global.trim_trailing_whitespace = true;
    }
    if !global.collapse_blanks && config.write_policy.collapse_blanks == Some(true) {
        global.collapse_blanks = true;
    }
    if !global.respect_editorconfig && config.write_policy.respect_editorconfig == Some(true) {
        global.respect_editorconfig = true;
    }

    // Defaults: config provides defaults, CLI flags win.
    // Only apply config's defaults.apply if no explicit mode flag was set.
    // If the user passed --check or --diff, they explicitly want a non-apply mode
    // and the config should not override that.
    if !global.apply
        && !global.check
        && !global.diff
        && !global.confirm
        && config.defaults.apply == Some(true)
    {
        global.apply = true;
    }
    if global.format.is_none() {
        // CLI --format wins, then defaults.format, then format.command (with auto=true)
        if let Some(ref fmt) = config.defaults.format {
            global.format = Some(fmt.clone());
        } else if config.format.auto == Some(true)
            && let Some(ref cmd) = config.format.command
        {
            global.format = Some(cmd.clone());
        }
    }

    // Exclude globs: prepend config globs before user excludes.
    if !config.exclude.globs.is_empty() {
        let mut merged = config.exclude.globs.clone();
        merged.append(&mut global.exclude);
        global.exclude = merged;
    }

    // Color: config provides default, CLI wins.
    if matches!(global.color, crate::cli::global::ColorMode::Auto)
        && let Some(ref color) = config.output.color
    {
        global.color = match color.as_str() {
            "always" => crate::cli::global::ColorMode::Always,
            "never" => crate::cli::global::ColorMode::Never,
            "auto" => crate::cli::global::ColorMode::Auto,
            invalid => {
                eprintln!("{}", invalid_output_color_warning(invalid));
                crate::cli::global::ColorMode::Auto
            }
        };
    }
}

fn invalid_normalize_eol_warning(invalid: &str) -> String {
    format!(
        "warning: invalid write_policy.normalize_eol value {invalid:?}; expected \"lf\", \"crlf\", or \"cr\""
    )
}

fn invalid_output_color_warning(invalid: &str) -> String {
    format!(
        "warning: invalid output.color value {invalid:?}; expected \"always\" or \"never\". Using \"auto\"."
    )
}

/// A cached project configuration that can be loaded once and reused across
/// multiple operations. This avoids re-reading `.patchloom.toml` from disk
/// on every API call.
///
/// # Thread safety
///
/// `CachedConfig` is `Send + Sync` and can be shared across threads via
/// `Arc<CachedConfig>`.
///
/// # Example
///
/// ```rust,no_run
/// use patchloom::config::CachedConfig;
/// use std::path::Path;
///
/// let config = CachedConfig::load(Path::new("/my/project"));
/// // Use config.project_config() in multiple API calls...
/// ```
#[derive(Debug)]
pub struct CachedConfig {
    config: ProjectConfig,
    config_dir: Option<PathBuf>,
}

impl CachedConfig {
    /// Load configuration from the given directory, searching upward for
    /// `.patchloom.toml`. Returns a `CachedConfig` with defaults if no
    /// config file is found.
    pub fn load(start: &Path) -> Self {
        match find_and_load(start) {
            Some((config, dir)) => Self {
                config,
                config_dir: Some(dir),
            },
            None => Self {
                config: ProjectConfig::default(),
                config_dir: None,
            },
        }
    }

    /// Access the loaded project configuration.
    pub fn project_config(&self) -> &ProjectConfig {
        &self.config
    }

    /// The directory containing the `.patchloom.toml` file, if one was found.
    pub fn config_dir(&self) -> Option<&Path> {
        self.config_dir.as_deref()
    }
}

// Static assertion: CachedConfig must be Send + Sync for cross-thread sharing.
const _: () = {
    fn _assert<T: Send + Sync>() {}
    let _ = _assert::<CachedConfig>;
};

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn find_and_load_from_dir() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            r#"
[write_policy]
ensure_final_newline = true
normalize_eol = "lf"
collapse_blanks = true

[exclude]
globs = ["target/**"]

[output]
color = "always"
"#,
        )
        .unwrap();

        let (config, found_dir) = find_and_load(dir.path()).unwrap();
        assert_eq!(found_dir, dir.path());
        assert_eq!(config.write_policy.ensure_final_newline, Some(true));
        assert_eq!(config.write_policy.normalize_eol.as_deref(), Some("lf"));
        assert_eq!(config.write_policy.collapse_blanks, Some(true));
        assert_eq!(config.exclude.globs, vec!["target/**"]);
        assert_eq!(config.output.color.as_deref(), Some("always"));
    }

    #[test]
    fn find_and_load_walks_up() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            "[write_policy]\nensure_final_newline = true\n",
        )
        .unwrap();

        let sub = dir.path().join("sub/deep");
        std::fs::create_dir_all(&sub).unwrap();

        let (config, found_dir) = find_and_load(&sub).unwrap();
        assert_eq!(found_dir, dir.path());
        assert_eq!(config.write_policy.ensure_final_newline, Some(true));
    }

    #[test]
    fn find_and_load_tx_strict_override() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join(".patchloom.toml"), "[tx]\nstrict = false\n").unwrap();

        let (config, _) = find_and_load(dir.path()).unwrap();
        assert_eq!(config.tx.strict, Some(false));
    }

    #[test]
    fn find_and_load_returns_none_when_missing() {
        let dir = TempDir::new().unwrap();
        assert!(find_and_load(dir.path()).is_none());
    }

    #[test]
    fn find_and_load_stops_at_git_boundary() {
        let dir = TempDir::new().unwrap();
        // Place a config in the parent directory.
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            "[write_policy]\nensure_final_newline = true\n",
        )
        .unwrap();

        // Create a child directory that is a separate git repo root.
        let child = dir.path().join("child");
        std::fs::create_dir_all(&child).unwrap();
        std::fs::create_dir_all(child.join(".git")).unwrap();

        // Searching from child should NOT find the parent's config.
        assert!(find_and_load(&child).is_none());
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_sets_defaults() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                ensure_final_newline: Some(true),
                normalize_eol: Some("lf".into()),
                trim_trailing_whitespace: Some(true),
                collapse_blanks: Some(true),
                respect_editorconfig: Some(true),
            },
            exclude: Exclude {
                globs: vec!["target/**".into()],
            },
            output: Output {
                color: Some("always".into()),
            },
            tx: TxConfig::default(),
            defaults: Defaults::default(),
            format: FormatConfig::default(),
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);

        assert!(global.ensure_final_newline);
        assert!(matches!(
            global.normalize_eol,
            Some(crate::cli::global::EolMode::Lf)
        ));
        assert!(global.trim_trailing_whitespace);
        assert!(global.collapse_blanks);
        assert!(global.respect_editorconfig);
        assert_eq!(global.exclude, vec!["target/**"]);
        assert!(
            global.glob.is_empty(),
            "exclude globs must not leak into include globs"
        );
        assert!(matches!(
            global.color,
            crate::cli::global::ColorMode::Always
        ));
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_cli_flags_win() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                ensure_final_newline: Some(true),
                ..WritePolicyOverride::default()
            },
            exclude: Exclude {
                globs: vec!["config_glob".into()],
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags {
            ensure_final_newline: true,
            glob: vec!["user_glob".into()],
            color: crate::cli::global::ColorMode::Never,
            ..crate::cli::global::GlobalFlags::default()
        };
        apply_config(&mut global, &config);

        // CLI flag already set, config doesn't override
        assert!(global.ensure_final_newline);
        // Config exclude globs go to exclude, not include globs
        assert_eq!(global.exclude, vec!["config_glob"]);
        // User include globs remain unchanged
        assert_eq!(global.glob, vec!["user_glob"]);
        // CLI color wins
        assert!(matches!(global.color, crate::cli::global::ColorMode::Never));
    }

    #[test]
    fn find_and_load_rejects_unknown_keys() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            "[write_policy]\nensur_final_newline = true\n",
        )
        .unwrap();

        // Typo in key name is rejected, not silently ignored.
        assert!(find_and_load(dir.path()).is_none());
    }

    #[test]
    fn find_and_load_returns_none_on_malformed_toml() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            "this is not valid { toml [",
        )
        .unwrap();

        // Malformed TOML returns None but prints a warning to stderr.
        assert!(find_and_load(dir.path()).is_none());
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_unknown_eol_value_ignored() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                normalize_eol: Some("CRLF".into()), // uppercase: not recognized
                ..WritePolicyOverride::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);

        assert!(global.normalize_eol.is_none());
        let warning = invalid_normalize_eol_warning("CRLF");
        assert!(warning.contains("CRLF"));
        assert!(warning.contains("\"lf\""));
        assert!(warning.contains("\"crlf\""));
        assert!(warning.contains("\"cr\""));
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_crlf_eol() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                normalize_eol: Some("crlf".into()),
                ..WritePolicyOverride::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);

        assert!(matches!(
            global.normalize_eol,
            Some(crate::cli::global::EolMode::Crlf)
        ));
    }

    #[test]
    #[cfg(feature = "cli")]
    // Regression: color = "auto" in config produced a spurious warning because
    // the match only handled "always" and "never", falling through to the
    // invalid-value catch-all for "auto".
    fn apply_config_color_auto_no_warning() {
        let config = ProjectConfig {
            output: Output {
                color: Some("auto".into()),
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);
        assert!(matches!(global.color, crate::cli::global::ColorMode::Auto));
    }

    #[test]
    fn apply_config_unknown_color_value_stays_auto() {
        let config = ProjectConfig {
            output: Output {
                color: Some("yes".into()), // not "always" or "never"
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);

        assert!(matches!(global.color, crate::cli::global::ColorMode::Auto));
        let warning = invalid_output_color_warning("yes");
        assert!(warning.contains("yes"));
        assert!(warning.contains("always"));
        assert!(warning.contains("never"));
    }

    #[test]
    fn cached_config_load_with_file() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            "[write_policy]\nensure_final_newline = true\n",
        )
        .unwrap();

        let cached = CachedConfig::load(dir.path());
        assert_eq!(
            cached.project_config().write_policy.ensure_final_newline,
            Some(true)
        );
        assert_eq!(cached.config_dir(), Some(dir.path()));
    }

    #[test]
    fn cached_config_load_defaults_when_missing() {
        let dir = TempDir::new().unwrap();

        let cached = CachedConfig::load(dir.path());
        assert_eq!(
            cached.project_config().write_policy.ensure_final_newline,
            None
        );
        assert!(cached.config_dir().is_none());
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_respect_editorconfig_from_config() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                respect_editorconfig: Some(true),
                ..WritePolicyOverride::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        assert!(!global.respect_editorconfig);
        apply_config(&mut global, &config);
        assert!(global.respect_editorconfig);
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_apply_default_from_config() {
        let config = ProjectConfig {
            defaults: Defaults {
                apply: Some(true),
                ..Defaults::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        assert!(!global.apply);
        apply_config(&mut global, &config);
        assert!(global.apply);
    }

    // Regression: defaults.apply must not override explicit --check or --diff.
    #[test]
    fn apply_config_apply_default_respects_check_flag() {
        let config = ProjectConfig {
            defaults: Defaults {
                apply: Some(true),
                ..Defaults::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags {
            check: true, // user explicitly passed --check
            ..crate::cli::global::GlobalFlags::default()
        };
        apply_config(&mut global, &config);
        assert!(
            !global.apply,
            "defaults.apply should not override explicit --check"
        );
    }

    #[test]
    fn apply_config_apply_default_respects_diff_flag() {
        let config = ProjectConfig {
            defaults: Defaults {
                apply: Some(true),
                ..Defaults::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags {
            diff: true, // user explicitly passed --diff
            ..crate::cli::global::GlobalFlags::default()
        };
        apply_config(&mut global, &config);
        assert!(
            !global.apply,
            "defaults.apply should not override explicit --diff"
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_format_default_from_config() {
        let config = ProjectConfig {
            defaults: Defaults {
                format: Some("cargo fmt --all".into()),
                ..Defaults::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        assert!(global.format.is_none());
        apply_config(&mut global, &config);
        assert_eq!(global.format.as_deref(), Some("cargo fmt --all"));
    }

    #[test]
    fn find_and_load_with_defaults() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            r#"
[defaults]
apply = true
format = "cargo fmt"

[write_policy]
respect_editorconfig = true
"#,
        )
        .unwrap();

        let (config, found_dir) = find_and_load(dir.path()).unwrap();
        assert_eq!(found_dir, dir.path());
        assert_eq!(config.defaults.apply, Some(true));
        assert_eq!(config.defaults.format.as_deref(), Some("cargo fmt"));
        assert_eq!(config.write_policy.respect_editorconfig, Some(true));
    }

    #[test]
    fn find_and_load_format_config() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            r#"
[format]
auto = true
command = "treefmt"

[format.by_extension]
rs = "rustfmt"
go = "gofmt -w"
ts = "prettier --write"
"#,
        )
        .unwrap();

        let (config, _) = find_and_load(dir.path()).unwrap();
        assert_eq!(config.format.auto, Some(true));
        assert_eq!(config.format.command.as_deref(), Some("treefmt"));
        assert_eq!(config.format.by_extension.len(), 3);
        assert_eq!(config.format.by_extension.get("rs").unwrap(), "rustfmt");
        assert_eq!(config.format.by_extension.get("go").unwrap(), "gofmt -w");
        assert_eq!(
            config.format.by_extension.get("ts").unwrap(),
            "prettier --write"
        );
    }

    #[test]
    fn find_and_load_format_auto_without_command() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join(".patchloom.toml"),
            r#"
[format]
auto = true

[format.by_extension]
rs = "rustfmt"
"#,
        )
        .unwrap();

        let (config, _) = find_and_load(dir.path()).unwrap();
        assert_eq!(config.format.auto, Some(true));
        assert!(config.format.command.is_none());
        assert_eq!(config.format.by_extension.get("rs").unwrap(), "rustfmt");
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_format_auto_command_sets_global_format() {
        let config = ProjectConfig {
            format: FormatConfig {
                auto: Some(true),
                command: Some("treefmt".into()),
                ..FormatConfig::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        assert!(global.format.is_none());
        apply_config(&mut global, &config);
        assert_eq!(global.format.as_deref(), Some("treefmt"));
    }

    /// Regression: normalize_eol = "keep" in config must be accepted (no-op).
    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_keep_eol_is_noop() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                normalize_eol: Some("keep".into()),
                ..WritePolicyOverride::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);

        // "keep" means "don't normalize" which is the default (None).
        assert!(
            global.normalize_eol.is_none(),
            "keep should not set normalize_eol"
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_cr_eol() {
        let config = ProjectConfig {
            write_policy: WritePolicyOverride {
                normalize_eol: Some("cr".into()),
                ..WritePolicyOverride::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);

        assert!(matches!(
            global.normalize_eol,
            Some(crate::cli::global::EolMode::Cr)
        ));
    }

    #[test]
    #[cfg(feature = "cli")]
    fn apply_config_defaults_format_wins_over_format_command() {
        let config = ProjectConfig {
            defaults: Defaults {
                format: Some("cargo fmt --all".into()),
                ..Defaults::default()
            },
            format: FormatConfig {
                auto: Some(true),
                command: Some("treefmt".into()),
                ..FormatConfig::default()
            },
            ..ProjectConfig::default()
        };
        let mut global = crate::cli::global::GlobalFlags::default();
        apply_config(&mut global, &config);
        // defaults.format takes priority over format.command
        assert_eq!(global.format.as_deref(), Some("cargo fmt --all"));
    }
}