badness 0.5.0

A language server, formatter, and linter for LaTeX
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
//! `badness.toml` configuration: schema, file loading, and ancestor-walk
//! discovery.
//!
//! The CLI is the only consumer; the library API (`format_with_style`,
//! `check_paths_with_style`, the linter) continues to take a fully-resolved
//! [`FormatStyle`] / [`ExcludeFilter`](crate::file_discovery::ExcludeFilter) /
//! rule selection.
//!
//! Two deliberate design choices:
//!
//! - **Excludes use the Ruff model** (`exclude` + `extend-exclude`): a present
//!   `exclude` *replaces* the built-in
//!   [`DEFAULT_EXCLUDE`] set; `extend-exclude` is always *additive* on top.
//! - **`[format]` carries `wrap`, not `line-ending`** (the LaTeX paragraph
//!   line-break policy). `wrap` is optional: when omitted the formatter falls back
//!   to each file kind's default ([`FileKind::default_wrap`](crate::file_discovery::FileKind::default_wrap)),
//!   so it is resolved per file, not baked into [`FormatStyle`] here.
//!
//! There is no `[index]` section (badness has no R-package index) and no
//! `[lint]`-driven network egress key.

use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::formatter::{FormatStyle, WrapMode};

pub const CONFIG_FILE_NAME: &str = "badness.toml";

const MIN_WIDTH: u32 = 1;
const MAX_WIDTH: u32 = 1000;

const DEFAULT_LINE_WIDTH: u32 = 80;
const DEFAULT_INDENT_WIDTH: u32 = 2;

/// Built-in exclude patterns applied when `exclude` is unset (a present `exclude`
/// replaces them). Kept deliberately small: badness only ever processes
/// `.tex`/`.sty`/`.cls`/`.dtx`/`.ins`/`.bib`, so most generated-file noise never
/// reaches discovery anyway. Tune as real-world LaTeX trees demand. `extend-exclude`
/// is always layered on top of whichever base is in effect.
pub const DEFAULT_EXCLUDE: &[&str] = &[".git/"];

#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct Config {
    /// Gitignore-style patterns to exclude from directory discovery, resolved
    /// relative to the directory containing this `badness.toml`. Applies to *both*
    /// `format` and `lint` (which share one file walk), so it is a top-level key,
    /// not nested under `[format]`.
    ///
    /// When present it **replaces** the built-in [`DEFAULT_EXCLUDE`] set (Ruff's
    /// `exclude` semantics); when absent the defaults apply. Either way,
    /// [`extend_exclude`](Self::extend_exclude) is added on top.
    #[serde(default)]
    pub exclude: Option<Vec<String>>,
    /// Gitignore-style patterns added *in addition to* whichever base set
    /// [`exclude`](Self::exclude) selects (Ruff's `extend-exclude` semantics). Use
    /// this to skip a few extra paths without restating the defaults.
    #[serde(default)]
    pub extend_exclude: Vec<String>,
    #[serde(default)]
    pub format: FormatConfig,
    #[serde(default)]
    pub lint: LintConfig,
}

impl Config {
    /// The final, ordered exclude pattern list: the base set (configured
    /// `exclude`, or [`DEFAULT_EXCLUDE`] when unset) followed by `extend-exclude`.
    /// `extra` (the CLI `--exclude` patterns) is appended last so command-line
    /// excludes always layer on top. The
    /// [`ExcludeFilter`](crate::file_discovery::ExcludeFilter) compiles this list.
    pub fn exclude_patterns(&self, extra: &[String]) -> Vec<String> {
        let mut patterns: Vec<String> = match &self.exclude {
            Some(patterns) => patterns.clone(),
            None => DEFAULT_EXCLUDE.iter().map(|p| p.to_string()).collect(),
        };
        patterns.extend(self.extend_exclude.iter().cloned());
        patterns.extend(extra.iter().cloned());
        patterns
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct FormatConfig {
    #[serde(default = "default_line_width")]
    pub line_width: u32,
    #[serde(default = "default_indent_width")]
    pub indent_width: u32,
    /// The paragraph line-break policy. See [`WrapModeConfig`]. When omitted, the
    /// formatter uses each file kind's default
    /// ([`FileKind::default_wrap`](crate::file_discovery::FileKind::default_wrap)):
    /// `.sty`/`.cls`/`.dtx`/`.ins` → `preserve`, `.tex` → `reflow`.
    #[serde(default)]
    pub wrap: Option<WrapModeConfig>,
}

impl Default for FormatConfig {
    fn default() -> Self {
        Self {
            line_width: DEFAULT_LINE_WIDTH,
            indent_width: DEFAULT_INDENT_WIDTH,
            wrap: None,
        }
    }
}

/// The `wrap` key under `[format]`. A thin, serde-named mirror of [`WrapMode`]
/// (the formatter's own type), kept separate so the TOML spelling (`kebab-case`)
/// is a config concern, not baked into the formatter API — the same split as the
/// CLI's `WrapArg` in `cli.rs`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum WrapModeConfig {
    /// Greedy fill: wrap words to the line width.
    Reflow,
    /// One sentence per line. (Not yet implemented — behaves like `preserve`.)
    Sentence,
    /// Semantic line breaks (sembr.org). (Not yet implemented — like `preserve`.)
    Semantic,
    /// Leave authored line breaks untouched.
    Preserve,
}

impl From<WrapModeConfig> for WrapMode {
    fn from(value: WrapModeConfig) -> Self {
        match value {
            WrapModeConfig::Reflow => WrapMode::Reflow,
            WrapModeConfig::Sentence => WrapMode::Sentence,
            WrapModeConfig::Semantic => WrapMode::Semantic,
            WrapModeConfig::Preserve => WrapMode::Preserve,
        }
    }
}

impl FormatConfig {
    /// Validate values, returning a [`ConfigError::InvalidValue`] with the
    /// originating file path (when known) for diagnostics.
    pub fn validate(&self, path: Option<&Path>) -> Result<(), ConfigError> {
        validate_width("line-width", self.line_width, path)?;
        validate_width("indent-width", self.indent_width, path)?;
        Ok(())
    }
}

fn default_line_width() -> u32 {
    DEFAULT_LINE_WIDTH
}

fn default_indent_width() -> u32 {
    DEFAULT_INDENT_WIDTH
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct LintConfig {
    /// Explicit allowlist of rule IDs. When `Some`, only these rules run.
    /// Unknown rule IDs are reported at lint-time, not at config parse-time.
    #[serde(default)]
    pub select: Option<Vec<String>>,
    /// Rule IDs to disable. Applied on top of either `select` (subtracts) or the
    /// default rule set.
    #[serde(default)]
    pub ignore: Vec<String>,
}

impl From<&FormatConfig> for FormatStyle {
    /// Maps the two width knobs. `wrap` is left at [`WrapMode::default`] as a
    /// placeholder: the effective wrap is resolved *per file* by the caller (CLI
    /// flag → configured `wrap` → file-kind default), so this field is always
    /// overwritten before the style reaches the formatter.
    fn from(config: &FormatConfig) -> Self {
        FormatStyle {
            line_width: config.line_width as usize,
            indent_width: config.indent_width as usize,
            wrap: WrapMode::default(),
        }
    }
}

#[derive(Debug)]
pub enum ConfigError {
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
    Parse {
        path: PathBuf,
        line: usize,
        column: usize,
        message: String,
    },
    InvalidValue {
        path: Option<PathBuf>,
        field: &'static str,
        message: String,
    },
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io { path, source } => {
                write!(f, "failed to read {}: {source}", path.display())
            }
            Self::Parse {
                path,
                line,
                column,
                message,
            } => write!(f, "{}:{line}:{column}: {message}", path.display()),
            Self::InvalidValue {
                path,
                field,
                message,
            } => match path {
                Some(path) => write!(f, "{}: invalid `{field}`: {message}", path.display()),
                None => write!(f, "invalid `{field}`: {message}"),
            },
        }
    }
}

impl std::error::Error for ConfigError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            _ => None,
        }
    }
}

impl Config {
    /// Parse a `badness.toml` from disk and validate it.
    pub fn load_from(path: &Path) -> Result<Self, ConfigError> {
        let text = fs::read_to_string(path).map_err(|source| ConfigError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        Self::parse_str(&text, path)
    }

    fn parse_str(text: &str, path: &Path) -> Result<Self, ConfigError> {
        let config: Self = toml::from_str(text).map_err(|err| {
            let (line, column) = match err.span() {
                Some(span) => byte_offset_to_line_col(text, span.start),
                None => (1, 1),
            };
            ConfigError::Parse {
                path: path.to_path_buf(),
                line,
                column,
                message: err.message().to_string(),
            }
        })?;
        config.validate(Some(path))?;
        Ok(config)
    }

    fn validate(&self, path: Option<&Path>) -> Result<(), ConfigError> {
        self.format.validate(path)
    }

    /// Walk `start` and its ancestors looking for a `badness.toml`. Stops at the
    /// first match or at a directory that contains a `.git` entry (repo root),
    /// whichever comes first. Returns `None` if neither is found before the
    /// filesystem root.
    pub fn discover(start: &Path) -> Result<Option<(PathBuf, Self)>, ConfigError> {
        let canonical = start.canonicalize().map_err(|source| ConfigError::Io {
            path: start.to_path_buf(),
            source,
        })?;
        for dir in canonical.ancestors() {
            let candidate = dir.join(CONFIG_FILE_NAME);
            if candidate.is_file() {
                let config = Self::load_from(&candidate)?;
                return Ok(Some((candidate, config)));
            }
            if dir.join(".git").exists() {
                return Ok(None);
            }
        }
        Ok(None)
    }

    /// CLI resolution. Returns the final config plus the source path of the loaded
    /// file (for diagnostics and to root exclude patterns), if any. CLI flag
    /// overrides for the formatter/lint knobs are applied by the caller after this
    /// returns.
    pub fn resolve(
        explicit: Option<&Path>,
        no_config: bool,
        anchor: &Path,
    ) -> Result<(Self, Option<PathBuf>), ConfigError> {
        if no_config {
            return Ok((Self::default(), None));
        }
        if let Some(path) = explicit {
            let config = Self::load_from(path)?;
            return Ok((config, Some(path.to_path_buf())));
        }
        match Self::discover(anchor)? {
            Some((path, config)) => Ok((config, Some(path))),
            None => Ok((Self::default(), None)),
        }
    }
}

fn validate_width(field: &'static str, value: u32, path: Option<&Path>) -> Result<(), ConfigError> {
    if !(MIN_WIDTH..=MAX_WIDTH).contains(&value) {
        return Err(ConfigError::InvalidValue {
            path: path.map(Path::to_path_buf),
            field,
            message: format!("must be between {MIN_WIDTH} and {MAX_WIDTH}, got {value}"),
        });
    }
    Ok(())
}

fn byte_offset_to_line_col(source: &str, offset: usize) -> (usize, usize) {
    let mut line = 1usize;
    let mut column = 1usize;
    let clamped = offset.min(source.len());
    for ch in source[..clamped].chars() {
        if ch == '\n' {
            line += 1;
            column = 1;
        } else {
            column += 1;
        }
    }
    (line, column)
}

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

    fn parse(text: &str) -> Result<Config, ConfigError> {
        Config::parse_str(text, Path::new("badness.toml"))
    }

    #[test]
    fn default_config_matches_format_style_default_widths() {
        let config = Config::default();
        let style = FormatStyle::from(&config.format);
        assert_eq!(style.line_width, FormatStyle::default().line_width);
        assert_eq!(style.indent_width, FormatStyle::default().indent_width);
    }

    #[test]
    fn empty_file_yields_defaults() {
        let config = parse("").expect("parse");
        assert_eq!(config, Config::default());
    }

    #[test]
    fn parses_minimal_format_section() {
        let config = parse("[format]\nline-width = 100\n").expect("parse");
        let style = FormatStyle::from(&config.format);
        assert_eq!(style.line_width, 100);
        assert_eq!(style.indent_width, 2);
    }

    #[test]
    fn parses_indent_width() {
        let config = parse("[format]\nindent-width = 4\n").expect("parse");
        let style = FormatStyle::from(&config.format);
        assert_eq!(style.indent_width, 4);
        assert_eq!(style.line_width, 80);
    }

    #[test]
    fn wrap_defaults_to_none() {
        let config = parse("[format]\n").expect("parse");
        assert_eq!(config.format.wrap, None);
    }

    #[test]
    fn parses_wrap_variants() {
        for (key, expected) in [
            ("reflow", WrapModeConfig::Reflow),
            ("sentence", WrapModeConfig::Sentence),
            ("semantic", WrapModeConfig::Semantic),
            ("preserve", WrapModeConfig::Preserve),
        ] {
            let text = format!("[format]\nwrap = \"{key}\"\n");
            let config = parse(&text).unwrap_or_else(|e| panic!("parse {key}: {e}"));
            assert_eq!(config.format.wrap, Some(expected), "for {key}");
            assert_eq!(WrapMode::from(expected), expected_wrap_mode(key));
        }
    }

    fn expected_wrap_mode(key: &str) -> WrapMode {
        match key {
            "reflow" => WrapMode::Reflow,
            "sentence" => WrapMode::Sentence,
            "semantic" => WrapMode::Semantic,
            "preserve" => WrapMode::Preserve,
            other => panic!("unexpected {other}"),
        }
    }

    #[test]
    fn rejects_unknown_wrap() {
        let err = parse("[format]\nwrap = \"smart\"\n").expect_err("unknown variant");
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn rejects_unknown_top_level_table() {
        let err = parse("[formatt]\nline-width = 80\n").expect_err("unknown table");
        match err {
            ConfigError::Parse { message, .. } => {
                assert!(message.contains("formatt"), "got: {message}");
            }
            other => panic!("expected Parse error, got {other:?}"),
        }
    }

    #[test]
    fn rejects_unknown_field_in_format() {
        let err = parse("[format]\nline-widht = 80\n").expect_err("unknown field");
        match err {
            ConfigError::Parse { message, .. } => {
                assert!(message.contains("line-widht"), "got: {message}");
            }
            other => panic!("expected Parse error, got {other:?}"),
        }
    }

    #[test]
    fn rejects_snake_case_keys() {
        // We use kebab-case in the schema; snake_case must be rejected so users get
        // a clear error instead of silent fallthrough to defaults.
        let err = parse("[format]\nline_width = 80\n").expect_err("snake_case");
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn rejects_zero_line_width() {
        let err = parse("[format]\nline-width = 0\n").expect_err("zero width");
        match err {
            ConfigError::InvalidValue { field, message, .. } => {
                assert_eq!(field, "line-width");
                assert!(message.contains('0'));
            }
            other => panic!("expected InvalidValue, got {other:?}"),
        }
    }

    #[test]
    fn rejects_huge_line_width() {
        let err = parse("[format]\nline-width = 10000\n").expect_err("too big");
        assert!(matches!(
            err,
            ConfigError::InvalidValue {
                field: "line-width",
                ..
            }
        ));
    }

    #[test]
    fn rejects_negative_width_as_parse_error() {
        // u32 deserialization rejects negatives at the type layer.
        let err = parse("[format]\nline-width = -1\n").expect_err("negative");
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn exclude_defaults_to_none_and_uses_builtin_set() {
        let config = Config::default();
        assert_eq!(config.exclude, None);
        assert!(config.extend_exclude.is_empty());
        // With no `exclude`, the base list is the built-in defaults.
        assert_eq!(
            config.exclude_patterns(&[]),
            DEFAULT_EXCLUDE
                .iter()
                .map(|p| p.to_string())
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn present_exclude_replaces_defaults() {
        let config = parse("exclude = [\"vendor/\"]\n").expect("parse");
        assert_eq!(
            config.exclude.as_deref(),
            Some(&["vendor/".to_string()][..])
        );
        assert_eq!(config.exclude_patterns(&[]), vec!["vendor/".to_string()]);
    }

    #[test]
    fn empty_exclude_drops_defaults() {
        let config = parse("exclude = []\n").expect("parse");
        assert_eq!(config.exclude.as_deref(), Some(&[][..]));
        assert!(config.exclude_patterns(&[]).is_empty());
    }

    #[test]
    fn extend_exclude_is_additive_over_defaults() {
        let config = parse("extend-exclude = [\"build/\"]\n").expect("parse");
        let mut expected: Vec<String> = DEFAULT_EXCLUDE.iter().map(|p| p.to_string()).collect();
        expected.push("build/".to_string());
        assert_eq!(config.exclude_patterns(&[]), expected);
    }

    #[test]
    fn extend_exclude_layers_on_present_exclude_then_cli() {
        let config =
            parse("exclude = [\"vendor/\"]\nextend-exclude = [\"build/\"]\n").expect("parse");
        assert_eq!(
            config.exclude_patterns(&["tmp/".to_string()]),
            vec![
                "vendor/".to_string(),
                "build/".to_string(),
                "tmp/".to_string(),
            ]
        );
    }

    #[test]
    fn rejects_exclude_under_format() {
        // `exclude` is a top-level key (it governs both format and lint), never
        // nested under `[format]`.
        let err = parse("[format]\nexclude = [\"x\"]\n").expect_err("exclude is top-level");
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn accepts_empty_lint_section() {
        let config = parse("[lint]\n").expect("parse");
        assert_eq!(config.lint, LintConfig::default());
    }

    #[test]
    fn rejects_unknown_field_in_lint() {
        let err = parse("[lint]\nstyle = \"strict\"\n").expect_err("unknown field");
        assert!(matches!(err, ConfigError::Parse { .. }));
    }

    #[test]
    fn parses_lint_select() {
        let config = parse("[lint]\nselect = [\"duplicate-label\"]\n").expect("parse");
        assert_eq!(
            config.lint.select.as_deref(),
            Some(&["duplicate-label".to_string()][..])
        );
    }

    #[test]
    fn parses_lint_ignore() {
        let config = parse("[lint]\nignore = [\"deprecated-command\"]\n").expect("parse");
        assert_eq!(config.lint.ignore, vec!["deprecated-command".to_string()]);
    }

    #[test]
    fn parse_error_reports_file_path_and_line() {
        let path = Path::new("/tmp/oops.toml");
        let err = Config::parse_str("[format]\nbogus = 1\n", path).expect_err("bad field");
        let rendered = err.to_string();
        assert!(rendered.starts_with("/tmp/oops.toml:"));
    }

    #[test]
    fn load_from_missing_file_returns_io_error() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("nope.toml");
        let err = Config::load_from(&path).expect_err("missing file");
        assert!(matches!(err, ConfigError::Io { .. }));
    }

    #[test]
    fn discover_finds_config_in_parent() {
        let dir = tempdir().unwrap();
        fs::write(
            dir.path().join(CONFIG_FILE_NAME),
            "[format]\nline-width = 70\n",
        )
        .unwrap();
        let nested = dir.path().join("a").join("b");
        fs::create_dir_all(&nested).unwrap();

        let (path, config) = Config::discover(&nested).expect("discover").expect("found");
        assert_eq!(
            path,
            dir.path().canonicalize().unwrap().join(CONFIG_FILE_NAME)
        );
        assert_eq!(config.format.line_width, 70);
    }

    #[test]
    fn discover_stops_at_git_boundary() {
        let dir = tempdir().unwrap();
        // Ancestor sets a config we must NOT pick up.
        fs::write(
            dir.path().join(CONFIG_FILE_NAME),
            "[format]\nline-width = 70\n",
        )
        .unwrap();
        let repo = dir.path().join("repo");
        fs::create_dir_all(repo.join(".git")).unwrap();
        let nested = repo.join("src");
        fs::create_dir_all(&nested).unwrap();

        let found = Config::discover(&nested).expect("discover");
        assert!(
            found.is_none(),
            "should stop at .git boundary, got {found:?}"
        );
    }

    #[test]
    fn discover_prefers_config_at_repo_root() {
        let dir = tempdir().unwrap();
        let repo = dir.path().join("repo");
        fs::create_dir_all(repo.join(".git")).unwrap();
        fs::write(repo.join(CONFIG_FILE_NAME), "[format]\nline-width = 70\n").unwrap();
        let nested = repo.join("src");
        fs::create_dir_all(&nested).unwrap();

        let (path, config) = Config::discover(&nested).expect("discover").expect("found");
        assert_eq!(path, repo.canonicalize().unwrap().join(CONFIG_FILE_NAME));
        assert_eq!(config.format.line_width, 70);
    }

    #[test]
    fn resolve_no_config_returns_defaults() {
        let dir = tempdir().unwrap();
        fs::write(
            dir.path().join(CONFIG_FILE_NAME),
            "[format]\nline-width = 20\n",
        )
        .unwrap();
        let (config, source) = Config::resolve(None, true, dir.path()).expect("resolve");
        assert_eq!(config, Config::default());
        assert!(source.is_none());
    }

    #[test]
    fn resolve_explicit_overrides_discovery() {
        let dir = tempdir().unwrap();
        fs::write(
            dir.path().join(CONFIG_FILE_NAME),
            "[format]\nline-width = 20\n",
        )
        .unwrap();
        let explicit = dir.path().join("custom.toml");
        fs::write(&explicit, "[format]\nline-width = 40\n").unwrap();

        let (config, source) =
            Config::resolve(Some(&explicit), false, dir.path()).expect("resolve");
        assert_eq!(config.format.line_width, 40);
        assert_eq!(source.as_deref(), Some(explicit.as_path()));
    }

    #[test]
    fn resolve_discovers_when_no_explicit_and_not_disabled() {
        let dir = tempdir().unwrap();
        fs::write(
            dir.path().join(CONFIG_FILE_NAME),
            "[format]\nline-width = 50\n",
        )
        .unwrap();
        let (config, source) = Config::resolve(None, false, dir.path()).expect("resolve");
        assert_eq!(config.format.line_width, 50);
        assert!(source.is_some());
    }
}