cargo-rail 0.13.4

Graph-aware testing, dependency unification, and crate extraction for Rust monorepos
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
//! Unify configuration - controls workspace dependency unification behavior

use serde::{Deserialize, Serialize};

/// Unify configuration - controls workspace dependency unification behavior
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnifyConfig {
  /// Handle path dependencies? (default: true)
  /// If false, path dependencies are excluded from unification
  #[serde(default = "default_include_paths")]
  pub include_paths: bool,

  /// Handle renamed dependencies (package = "...")? (default: false)
  /// Renamed deps are tricky to unify correctly, opt-in only
  #[serde(default)]
  pub include_renamed: bool,

  /// Pin transitive-only deps with fragmented features? (default: false)
  /// This is cargo-rail's workspace-hack replacement
  /// When enabled, transitive deps with multiple feature sets are pinned in workspace.dependencies
  /// Only enable if your project uses cargo-hakari or a workspace-hack crate
  #[serde(default)]
  pub pin_transitives: bool,

  /// Where to put pinned transitive dev-deps? (default: "root")
  /// Options: "root" or a path like "crates/foo"
  #[serde(default = "default_transitive_host")]
  pub transitive_host: TransitiveFeatureHost,

  /// Dependencies to exclude from unification (safety hatch)
  ///
  /// Workspace-member dependencies are handled as connected cohorts.
  /// Excluding one member excludes the full cohort atomically to avoid
  /// local-vs-registry split graphs.
  #[serde(default)]
  pub exclude: Vec<String>,

  /// Dependencies to force-include in unification (safety hatch)
  ///
  /// Workspace-member cohorts are auto-included by cargo-rail to prevent
  /// single-user threshold splits; this option is mainly for non-member deps.
  #[serde(default)]
  pub include: Vec<String>,

  /// Maximum number of backups to keep (default: 3)
  /// Older backups are automatically cleaned up after successful unify operations
  #[serde(default = "default_max_backups")]
  pub max_backups: usize,

  /// Compute and write MSRV to workspace manifest? (default: true)
  /// When enabled, cargo-rail computes the maximum rust-version from all
  /// resolved dependencies and writes it to [workspace.package].rust-version
  #[serde(default = "default_true")]
  pub msrv: bool,

  /// Enforce MSRV inheritance on workspace members (default: false)
  ///
  /// When enabled, `cargo rail unify` ensures every workspace member's Cargo.toml
  /// has `package.rust-version = { workspace = true }`, so that
  /// `[workspace.package].rust-version` is actually enforced across the workspace.
  ///
  /// Note: this requires `msrv = true` and a workspace MSRV that exists or can be computed.
  #[serde(default)]
  pub enforce_msrv_inheritance: bool,

  /// How to determine the final MSRV value (default: "max")
  /// - "deps": Use maximum from dependencies only (original behavior)
  /// - "workspace": Preserve existing rust-version, warn if deps need higher
  /// - "max": Take max(workspace, deps) - explicit workspace setting wins if higher
  #[serde(default)]
  pub msrv_source: MsrvSource,

  /// Prune features not referenced in source code? (default: true)
  /// When enabled, analyzes the resolved dependency graph to detect features
  /// that are declared but never enabled by any consumer across all targets.
  /// This produces the absolute leanest feature set for the workspace.
  #[serde(default = "default_true")]
  pub prune_dead_features: bool,

  /// Features to preserve from dead feature pruning (glob patterns supported)
  /// Use this to keep features intended for future use or external consumers.
  /// Examples: ["future-api", "unstable-*", "bench*"]
  #[serde(default)]
  pub preserve_features: Vec<String>,

  /// Strict version compatibility checking (default: true)
  /// When true, version mismatches between member manifests and existing
  /// workspace.dependencies are reported as blocking errors.
  /// When false, they are warnings only.
  #[serde(default = "default_true")]
  pub strict_version_compat: bool,

  /// How to handle exact version pins like "=0.8.0" (default: "warn")
  /// - "skip": Exclude exact-pinned deps from unification
  /// - "preserve": Keep the exact pin operator in workspace.dependencies
  /// - "warn": Convert to caret but emit a warning
  #[serde(default)]
  pub exact_pin_handling: ExactPinHandling,

  /// How to handle major version conflicts (default: "warn")
  /// - "warn": Skip unification and emit a warning (both versions stay in graph)
  /// - "bump": Force unify to highest resolved version (user accepts breakage risk)
  #[serde(default)]
  pub major_version_conflict: MajorVersionConflict,

  /// Detect unused dependencies in workspace members (default: true)
  /// When enabled, uses two signals:
  /// - Graph-level: declared deps absent from the resolved cargo graph.
  /// - Source-level: rustc `unused_crate_dependencies` diagnostics for deps
  ///   that are resolved but never referenced in source.
  ///
  /// Optional deps and deps behind unconfigured target constraints are
  /// conservatively skipped to avoid false positives.
  #[serde(default = "default_true")]
  pub detect_unused: bool,

  /// Cache compiler diagnostics for target-aware unused-dependency detection (default: true)
  /// When enabled, rustc unused-crate diagnostics are persisted in
  /// `target/cargo-rail/cache/compiler-diags-v1.json` and reused across runs.
  #[serde(default = "default_true")]
  pub compiler_diag_cache: bool,

  /// Automatically remove unused dependencies when applying (default: true)
  /// Requires detect_unused = true. When enabled, unused deps are removed
  /// from member Cargo.toml files during unify.
  #[serde(default = "default_true")]
  pub remove_unused: bool,

  /// Detect undeclared feature dependencies (default: true)
  /// When enabled, compares resolved features against declared features in Cargo.toml
  /// to find crates that rely on Cargo's feature unification to "borrow" features
  /// from other workspace members. After unification, standalone builds of these
  /// crates will fail. Reports as warnings to help fix before unification.
  #[serde(default = "default_true")]
  pub detect_undeclared_features: bool,

  /// Auto-fix undeclared feature dependencies (default: true)
  /// When enabled (and detect_undeclared_features is true), automatically adds
  /// missing features to each crate's Cargo.toml instead of just warning.
  /// This produces a cleaner graph where standalone builds work correctly.
  #[serde(default = "default_true")]
  pub fix_undeclared_features: bool,

  /// Patterns for features to skip in undeclared feature detection (glob supported)
  /// Default: ["default", "std", "alloc", "*_backend", "*_impl"]
  /// These are features that are typically not actionable or are implementation details.
  #[serde(default = "default_skip_undeclared_patterns")]
  pub skip_undeclared_patterns: Vec<String>,

  /// Sort dependencies alphabetically when writing Cargo.toml files (default: true)
  /// When false, preserves existing order and appends new deps at end.
  #[serde(default = "default_true")]
  pub sort_dependencies: bool,
}

impl Default for UnifyConfig {
  fn default() -> Self {
    Self {
      include_paths: default_include_paths(),
      include_renamed: false,
      pin_transitives: false,
      transitive_host: default_transitive_host(),
      exclude: Vec::new(),
      include: Vec::new(),
      max_backups: default_max_backups(),
      msrv: true,
      enforce_msrv_inheritance: false,
      msrv_source: MsrvSource::default(),
      prune_dead_features: true,
      preserve_features: Vec::new(),
      strict_version_compat: true,
      exact_pin_handling: ExactPinHandling::default(),
      major_version_conflict: MajorVersionConflict::default(),
      detect_unused: true,
      compiler_diag_cache: true,
      remove_unused: true,
      detect_undeclared_features: true,
      fix_undeclared_features: true,
      skip_undeclared_patterns: default_skip_undeclared_patterns(),
      sort_dependencies: true,
    }
  }
}

impl UnifyConfig {
  /// Check if a dependency should be excluded from unification
  pub fn should_exclude(&self, dep_name: &str) -> bool {
    self.exclude.iter().any(|e| e == dep_name)
  }

  /// Check if a dependency should be force-included in unification
  pub fn should_include(&self, dep_name: &str) -> bool {
    self.include.iter().any(|i| i == dep_name)
  }

  /// Check if a feature should be preserved from dead feature pruning
  ///
  /// Supports glob patterns (e.g., "unstable-*", "bench*")
  pub fn should_preserve_feature(&self, feature_name: &str) -> bool {
    self.preserve_features.iter().any(|pattern| {
      if pattern.contains('*') || pattern.contains('?') || pattern.contains('[') {
        // Use glob matching for patterns with wildcards
        glob::Pattern::new(pattern)
          .map(|p| p.matches(feature_name))
          .unwrap_or(false)
      } else {
        // Exact match for literal patterns
        pattern == feature_name
      }
    })
  }

  /// Check if a feature should be skipped in undeclared feature detection
  ///
  /// Supports glob patterns (e.g., "*_backend", "*_impl")
  pub fn should_skip_undeclared_feature(&self, feature_name: &str) -> bool {
    self.skip_undeclared_patterns.iter().any(|pattern| {
      if pattern.contains('*') || pattern.contains('?') || pattern.contains('[') {
        // Use glob matching for patterns with wildcards
        glob::Pattern::new(pattern)
          .map(|p| p.matches(feature_name))
          .unwrap_or(false)
      } else {
        // Exact match for literal patterns
        pattern == feature_name
      }
    })
  }

  /// Validate unify configuration against the workspace
  ///
  /// Checks:
  /// - transitive_host path exists if configured as a path (not "root")
  /// - transitive_host path contains a Cargo.toml (is a valid crate/workspace)
  pub fn validate(&self, workspace_root: &std::path::Path) -> Result<(), crate::error::ConfigError> {
    // Only validate if pin_transitives is enabled and transitive_host is a path
    if self.pin_transitives
      && let TransitiveFeatureHost::Path(p) = &self.transitive_host
    {
      // Check for path traversal (security/consistency)
      if p.contains("..") {
        return Err(crate::error::ConfigError::InvalidValue {
          field: "unify.transitive_host".to_string(),
          message: format!("path '{}' contains '..' traversal, which is not allowed", p),
        });
      }

      // Check path is not absolute
      if std::path::Path::new(p).is_absolute() {
        return Err(crate::error::ConfigError::InvalidValue {
          field: "unify.transitive_host".to_string(),
          message: format!("path '{}' is absolute, must be relative to workspace root", p),
        });
      }

      // Check directory exists
      let full_path = workspace_root.join(p);
      if !full_path.exists() {
        return Err(crate::error::ConfigError::InvalidValue {
          field: "unify.transitive_host".to_string(),
          message: format!("path '{}' does not exist", p),
        });
      }

      // Check Cargo.toml exists at that path
      let cargo_toml = full_path.join("Cargo.toml");
      if !cargo_toml.exists() {
        return Err(crate::error::ConfigError::InvalidValue {
          field: "unify.transitive_host".to_string(),
          message: format!("path '{}' does not contain a Cargo.toml", p),
        });
      }
    }

    Ok(())
  }
}

// Helper Types

/// How to determine the final MSRV (Minimum Supported Rust Version)
///
/// Controls how cargo-rail computes the rust-version to write to [workspace.package].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MsrvSource {
  /// Use maximum from dependencies only (original behavior)
  ///
  /// Computes the highest rust-version from all resolved dependencies.
  /// Overwrites any existing workspace rust-version.
  Deps,
  /// Preserve existing workspace rust-version
  ///
  /// Keeps the existing [workspace.package].rust-version unchanged.
  /// Emits a warning if dependencies require a higher version.
  Workspace,
  /// Take max(workspace, deps) - default
  ///
  /// Uses the higher of the existing workspace rust-version or the
  /// maximum from dependencies. Your explicit workspace setting wins
  /// if it requires a higher Rust version than your dependencies.
  #[default]
  Max,
}

/// How to handle exact version pins ("=x.y.z") during unification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ExactPinHandling {
  /// Exclude exact-pinned deps from unification entirely
  Skip,
  /// Preserve the exact pin operator in workspace.dependencies
  Preserve,
  /// Convert to caret (^) but emit a warning (default)
  #[default]
  Warn,
}

/// How to handle major version conflicts during unification
///
/// Major version conflicts occur when the same dependency is declared with
/// different major versions across workspace members (e.g., `serde = "1.0"` and `serde = "2.0"`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MajorVersionConflict {
  /// Skip unification and emit a warning (default)
  ///
  /// Both versions remain in the build graph. This is the safe choice when
  /// you want to avoid breaking changes but may result in duplicate compilation.
  #[default]
  Warn,
  /// Force unify to the highest resolved version
  ///
  /// Uses the highest version from the resolved metadata across all target triples.
  /// This works in ~85% of cases; the remaining ~15% may break the codebase.
  /// Use when you want the leanest build graph and accept breakage risk.
  Bump,
}

/// Configuration for where to add dev-dependencies when consolidating transitive features
#[derive(Debug, Clone, PartialEq, Default)]
pub enum TransitiveFeatureHost {
  /// Use workspace root Cargo.toml (default)
  #[default]
  Root,
  /// Use a specific member crate (relative path from workspace root)
  Path(String),
}

// Custom serialization/deserialization for TransitiveFeatureHost
impl Serialize for TransitiveFeatureHost {
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: serde::Serializer,
  {
    match self {
      TransitiveFeatureHost::Root => serializer.serialize_str("root"),
      TransitiveFeatureHost::Path(path) => serializer.serialize_str(path),
    }
  }
}

impl<'de> Deserialize<'de> for TransitiveFeatureHost {
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: serde::Deserializer<'de>,
  {
    struct TransitiveFeatureHostVisitor;

    impl serde::de::Visitor<'_> for TransitiveFeatureHostVisitor {
      type Value = TransitiveFeatureHost;

      fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("'root' or a path string")
      }

      fn visit_str<E>(self, value: &str) -> Result<TransitiveFeatureHost, E>
      where
        E: serde::de::Error,
      {
        match value {
          "root" => Ok(TransitiveFeatureHost::Root),
          path => Ok(TransitiveFeatureHost::Path(path.to_string())),
        }
      }
    }

    deserializer.deserialize_any(TransitiveFeatureHostVisitor)
  }
}

// Default Functions

fn default_max_backups() -> usize {
  3
}

fn default_include_paths() -> bool {
  true
}

fn default_transitive_host() -> TransitiveFeatureHost {
  TransitiveFeatureHost::Root
}

pub(crate) fn default_true() -> bool {
  true
}

fn default_skip_undeclared_patterns() -> Vec<String> {
  const PATTERNS: &[&str] = &["default", "std", "alloc", "*_backend", "*_impl"];
  PATTERNS.iter().map(|&s| String::from(s)).collect()
}

// Tests

#[test]
fn test_transitive_feature_host_path() {
  // Test that path format works with simplified config
  let toml = r#"
      include_paths = true
      include_renamed = false
      pin_transitives = false
      transitive_host = "path/to/crate"
      exclude = []
      include = []
    "#;

  let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
  assert_eq!(
    config.transitive_host,
    TransitiveFeatureHost::Path("path/to/crate".to_string())
  );
}

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

  #[test]
  fn test_unify_config_defaults() {
    let config = UnifyConfig::default();
    assert!(config.include_paths); // Default: true
    assert!(!config.include_renamed); // Default: false
    assert!(!config.pin_transitives); // Default: false (only true for hakari users)
    assert_eq!(config.transitive_host, TransitiveFeatureHost::Root);
    assert!(config.exclude.is_empty());
    assert!(config.include.is_empty());
    assert!(config.msrv); // Default: true
    assert!(config.detect_unused); // Default: true
    assert!(config.compiler_diag_cache); // Default: true
    assert!(config.remove_unused); // Default: true
  }

  #[test]
  fn test_unify_config_should_exclude() {
    let config = UnifyConfig {
      exclude: vec!["tokio".to_string(), "serde".to_string()],
      ..Default::default()
    };
    assert!(config.should_exclude("tokio"));
    assert!(config.should_exclude("serde"));
    assert!(!config.should_exclude("regex"));
  }

  #[test]
  fn test_unify_config_should_include() {
    let config = UnifyConfig {
      include: vec!["special-dep".to_string()],
      ..Default::default()
    };
    assert!(config.should_include("special-dep"));
    assert!(!config.should_include("normal-dep"));
  }

  #[test]
  fn test_transitive_feature_host_in_full_config() {
    let toml = r#"
      include_paths = true
      include_renamed = false
      pin_transitives = true
      transitive_host = "root"
      exclude = []
      include = []
    "#;

    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.transitive_host, TransitiveFeatureHost::Root);
    assert!(config.include_paths);
    assert!(config.pin_transitives);
  }

  #[test]
  fn test_unify_config_default_transitive_host() {
    let config = UnifyConfig::default();
    assert_eq!(config.transitive_host, TransitiveFeatureHost::Root);
    assert!(!config.pin_transitives); // Default is false (opt-in for hakari users)
  }

  #[test]
  fn test_prune_dead_features_default() {
    let config = UnifyConfig::default();
    assert!(config.prune_dead_features); // Default: true
  }

  #[test]
  fn test_prune_dead_features_parsing() {
    let toml = r#"prune_dead_features = true"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.prune_dead_features);

    let toml = r#"prune_dead_features = false"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.prune_dead_features);
  }

  #[test]
  fn test_strict_version_compat_default() {
    let config = UnifyConfig::default();
    assert!(config.strict_version_compat); // Default is true
  }

  #[test]
  fn test_exact_pin_handling_default() {
    let config = UnifyConfig::default();
    assert_eq!(config.exact_pin_handling, ExactPinHandling::Warn);
  }

  #[test]
  fn test_exact_pin_handling_parsing() {
    let toml = r#"exact_pin_handling = "skip""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.exact_pin_handling, ExactPinHandling::Skip);

    let toml = r#"exact_pin_handling = "preserve""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.exact_pin_handling, ExactPinHandling::Preserve);

    let toml = r#"exact_pin_handling = "warn""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.exact_pin_handling, ExactPinHandling::Warn);
  }

  #[test]
  fn test_detect_unused_default() {
    let config = UnifyConfig::default();
    assert!(config.detect_unused); // Default: true
    assert!(config.compiler_diag_cache); // Default: true
    assert!(config.remove_unused); // Default: true
  }

  #[test]
  fn test_new_config_options_parsing() {
    let toml = r#"
      strict_version_compat = false
      exact_pin_handling = "preserve"
      detect_unused = true
      compiler_diag_cache = false
      remove_unused = true
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.strict_version_compat);
    assert_eq!(config.exact_pin_handling, ExactPinHandling::Preserve);
    assert!(config.detect_unused);
    assert!(!config.compiler_diag_cache);
    assert!(config.remove_unused);
  }

  #[test]
  fn test_major_version_conflict_default() {
    let config = UnifyConfig::default();
    assert_eq!(config.major_version_conflict, MajorVersionConflict::Warn);
  }

  #[test]
  fn test_major_version_conflict_parsing() {
    let toml = r#"major_version_conflict = "warn""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.major_version_conflict, MajorVersionConflict::Warn);

    let toml = r#"major_version_conflict = "bump""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.major_version_conflict, MajorVersionConflict::Bump);
  }

  #[test]
  fn test_major_version_conflict_with_other_options() {
    let toml = r#"
      strict_version_compat = false
      exact_pin_handling = "preserve"
      major_version_conflict = "bump"
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.strict_version_compat);
    assert_eq!(config.exact_pin_handling, ExactPinHandling::Preserve);
    assert_eq!(config.major_version_conflict, MajorVersionConflict::Bump);
  }

  #[test]
  fn test_transitive_host_validate_root() {
    // Root should always be valid
    let config = UnifyConfig {
      pin_transitives: true,
      transitive_host: TransitiveFeatureHost::Root,
      ..Default::default()
    };
    let workspace = std::env::current_dir().unwrap();
    assert!(config.validate(&workspace).is_ok());
  }

  #[test]
  fn test_transitive_host_validate_valid_path() {
    // src/ exists and doesn't have a Cargo.toml - but pin_transitives=false skips validation
    let config = UnifyConfig {
      pin_transitives: false,
      transitive_host: TransitiveFeatureHost::Path("src".to_string()),
      ..Default::default()
    };
    let workspace = std::env::current_dir().unwrap();
    assert!(config.validate(&workspace).is_ok());
  }

  #[test]
  fn test_transitive_host_validate_nonexistent_path() {
    let config = UnifyConfig {
      pin_transitives: true,
      transitive_host: TransitiveFeatureHost::Path("nonexistent/path".to_string()),
      ..Default::default()
    };
    let workspace = std::env::current_dir().unwrap();
    let result = config.validate(&workspace);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(matches!(err, crate::error::ConfigError::InvalidValue { .. }));
  }

  #[test]
  fn test_transitive_host_validate_path_traversal() {
    let config = UnifyConfig {
      pin_transitives: true,
      transitive_host: TransitiveFeatureHost::Path("../somewhere".to_string()),
      ..Default::default()
    };
    let workspace = std::env::current_dir().unwrap();
    let result = config.validate(&workspace);
    assert!(result.is_err());
    let err = result.unwrap_err();
    if let crate::error::ConfigError::InvalidValue { message, .. } = err {
      assert!(message.contains(".."));
    } else {
      panic!("Expected InvalidValue error");
    }
  }

  #[test]
  fn test_preserve_features_default() {
    let config = UnifyConfig::default();
    assert!(config.preserve_features.is_empty());
  }

  #[test]
  fn test_preserve_features_parsing() {
    let toml = r#"
      preserve_features = ["future-api", "unstable-*", "bench*"]
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.preserve_features.len(), 3);
    assert!(config.preserve_features.contains(&"future-api".to_string()));
    assert!(config.preserve_features.contains(&"unstable-*".to_string()));
    assert!(config.preserve_features.contains(&"bench*".to_string()));
  }

  #[test]
  fn test_should_preserve_feature_exact_match() {
    let config = UnifyConfig {
      preserve_features: vec!["future-api".to_string(), "experimental".to_string()],
      ..Default::default()
    };
    assert!(config.should_preserve_feature("future-api"));
    assert!(config.should_preserve_feature("experimental"));
    assert!(!config.should_preserve_feature("other-feature"));
  }

  #[test]
  fn test_should_preserve_feature_glob_wildcard() {
    let config = UnifyConfig {
      preserve_features: vec!["unstable-*".to_string()],
      ..Default::default()
    };
    assert!(config.should_preserve_feature("unstable-api"));
    assert!(config.should_preserve_feature("unstable-feature"));
    assert!(config.should_preserve_feature("unstable-"));
    assert!(!config.should_preserve_feature("unstable")); // No trailing dash
    assert!(!config.should_preserve_feature("stable-api"));
  }

  #[test]
  fn test_should_preserve_feature_glob_suffix() {
    let config = UnifyConfig {
      preserve_features: vec!["bench*".to_string()],
      ..Default::default()
    };
    assert!(config.should_preserve_feature("bench"));
    assert!(config.should_preserve_feature("benchmark"));
    assert!(config.should_preserve_feature("benchmarks"));
    assert!(!config.should_preserve_feature("prebench"));
  }

  #[test]
  fn test_should_preserve_feature_glob_question_mark() {
    let config = UnifyConfig {
      preserve_features: vec!["test-?".to_string()],
      ..Default::default()
    };
    assert!(config.should_preserve_feature("test-a"));
    assert!(config.should_preserve_feature("test-1"));
    assert!(!config.should_preserve_feature("test-ab")); // Two chars
    assert!(!config.should_preserve_feature("test-")); // No char
  }

  #[test]
  fn test_should_preserve_feature_multiple_patterns() {
    let config = UnifyConfig {
      preserve_features: vec!["future-api".to_string(), "unstable-*".to_string(), "bench*".to_string()],
      ..Default::default()
    };
    // Exact match
    assert!(config.should_preserve_feature("future-api"));
    // Glob matches
    assert!(config.should_preserve_feature("unstable-feature"));
    assert!(config.should_preserve_feature("benchmark"));
    // Non-matches
    assert!(!config.should_preserve_feature("stable-api"));
    assert!(!config.should_preserve_feature("other"));
  }

  #[test]
  fn test_should_preserve_feature_empty_list() {
    let config = UnifyConfig::default();
    assert!(!config.should_preserve_feature("any-feature"));
  }

  #[test]
  fn test_msrv_source_default() {
    let config = UnifyConfig::default();
    assert_eq!(config.msrv_source, MsrvSource::Max);
  }

  #[test]
  fn test_msrv_source_parsing_deps() {
    let toml = r#"msrv_source = "deps""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.msrv_source, MsrvSource::Deps);
  }

  #[test]
  fn test_msrv_source_parsing_workspace() {
    let toml = r#"msrv_source = "workspace""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.msrv_source, MsrvSource::Workspace);
  }

  #[test]
  fn test_msrv_source_parsing_max() {
    let toml = r#"msrv_source = "max""#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.msrv_source, MsrvSource::Max);
  }

  #[test]
  fn test_msrv_source_with_msrv_enabled() {
    let toml = r#"
      msrv = true
      msrv_source = "workspace"
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.msrv);
    assert_eq!(config.msrv_source, MsrvSource::Workspace);
  }

  #[test]
  fn test_msrv_source_with_msrv_disabled() {
    let toml = r#"
      msrv = false
      msrv_source = "deps"
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.msrv);
    assert_eq!(config.msrv_source, MsrvSource::Deps);
  }

  #[test]
  fn test_detect_undeclared_features_default() {
    let config = UnifyConfig::default();
    assert!(config.detect_undeclared_features); // Default: true
  }

  #[test]
  fn test_fix_undeclared_features_default() {
    let config = UnifyConfig::default();
    assert!(config.fix_undeclared_features); // Default: true
  }

  #[test]
  fn test_detect_undeclared_features_parsing_true() {
    let toml = r#"detect_undeclared_features = true"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.detect_undeclared_features);
  }

  #[test]
  fn test_detect_undeclared_features_parsing_false() {
    let toml = r#"detect_undeclared_features = false"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.detect_undeclared_features);
  }

  #[test]
  fn test_fix_undeclared_features_parsing_true() {
    let toml = r#"fix_undeclared_features = true"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.fix_undeclared_features);
  }

  #[test]
  fn test_fix_undeclared_features_parsing_false() {
    let toml = r#"fix_undeclared_features = false"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.fix_undeclared_features);
  }

  #[test]
  fn test_undeclared_features_both_options() {
    let toml = r#"
      detect_undeclared_features = true
      fix_undeclared_features = false
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.detect_undeclared_features);
    assert!(!config.fix_undeclared_features);
  }

  #[test]
  fn test_undeclared_features_with_other_options() {
    let toml = r#"
      detect_unused = true
      remove_unused = true
      detect_undeclared_features = true
      fix_undeclared_features = true
      prune_dead_features = false
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.detect_unused);
    assert!(config.remove_unused);
    assert!(config.detect_undeclared_features);
    assert!(config.fix_undeclared_features);
    assert!(!config.prune_dead_features);
  }

  #[test]
  fn test_undeclared_features_detect_disabled_fix_enabled() {
    // Edge case: fix enabled but detect disabled
    // This is a valid config but fix won't do anything if detect is off
    let toml = r#"
      detect_undeclared_features = false
      fix_undeclared_features = true
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.detect_undeclared_features);
    assert!(config.fix_undeclared_features);
  }

  // skip_undeclared_patterns Tests

  #[test]
  fn test_skip_undeclared_patterns_default() {
    let config = UnifyConfig::default();
    assert!(!config.skip_undeclared_patterns.is_empty());
    assert!(config.skip_undeclared_patterns.contains(&"default".to_string()));
    assert!(config.skip_undeclared_patterns.contains(&"std".to_string()));
    assert!(config.skip_undeclared_patterns.contains(&"alloc".to_string()));
    assert!(config.skip_undeclared_patterns.contains(&"*_backend".to_string()));
    assert!(config.skip_undeclared_patterns.contains(&"*_impl".to_string()));
  }

  #[test]
  fn test_skip_undeclared_patterns_parsing() {
    let toml = r#"
      skip_undeclared_patterns = ["default", "std", "custom-*"]
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert_eq!(config.skip_undeclared_patterns.len(), 3);
    assert!(config.skip_undeclared_patterns.contains(&"default".to_string()));
    assert!(config.skip_undeclared_patterns.contains(&"std".to_string()));
    assert!(config.skip_undeclared_patterns.contains(&"custom-*".to_string()));
  }

  #[test]
  fn test_skip_undeclared_patterns_empty() {
    let toml = r#"
      skip_undeclared_patterns = []
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.skip_undeclared_patterns.is_empty());
  }

  #[test]
  fn test_should_skip_undeclared_feature_exact_match() {
    let config = UnifyConfig {
      skip_undeclared_patterns: vec!["default".to_string(), "std".to_string()],
      ..Default::default()
    };
    assert!(config.should_skip_undeclared_feature("default"));
    assert!(config.should_skip_undeclared_feature("std"));
    assert!(!config.should_skip_undeclared_feature("derive"));
  }

  #[test]
  fn test_should_skip_undeclared_feature_glob_suffix() {
    let config = UnifyConfig {
      skip_undeclared_patterns: vec!["*_backend".to_string()],
      ..Default::default()
    };
    assert!(config.should_skip_undeclared_feature("sqlite_backend"));
    assert!(config.should_skip_undeclared_feature("postgres_backend"));
    assert!(config.should_skip_undeclared_feature("_backend")); // Just suffix
    assert!(!config.should_skip_undeclared_feature("backend"));
    assert!(!config.should_skip_undeclared_feature("backend_"));
  }

  #[test]
  fn test_should_skip_undeclared_feature_glob_prefix() {
    let config = UnifyConfig {
      skip_undeclared_patterns: vec!["unstable-*".to_string()],
      ..Default::default()
    };
    assert!(config.should_skip_undeclared_feature("unstable-api"));
    assert!(config.should_skip_undeclared_feature("unstable-internal"));
    assert!(config.should_skip_undeclared_feature("unstable-")); // Just prefix
    assert!(!config.should_skip_undeclared_feature("unstable"));
  }

  #[test]
  fn test_should_skip_undeclared_feature_glob_question_mark() {
    let config = UnifyConfig {
      skip_undeclared_patterns: vec!["test-?".to_string()],
      ..Default::default()
    };
    assert!(config.should_skip_undeclared_feature("test-1"));
    assert!(config.should_skip_undeclared_feature("test-a"));
    assert!(!config.should_skip_undeclared_feature("test-12"));
    assert!(!config.should_skip_undeclared_feature("test-"));
  }

  #[test]
  fn test_should_skip_undeclared_feature_multiple_patterns() {
    let config = UnifyConfig {
      skip_undeclared_patterns: vec![
        "default".to_string(),
        "std".to_string(),
        "*_backend".to_string(),
        "*_impl".to_string(),
      ],
      ..Default::default()
    };
    assert!(config.should_skip_undeclared_feature("default"));
    assert!(config.should_skip_undeclared_feature("std"));
    assert!(config.should_skip_undeclared_feature("sqlite_backend"));
    assert!(config.should_skip_undeclared_feature("sync_impl"));
    assert!(!config.should_skip_undeclared_feature("derive"));
    assert!(!config.should_skip_undeclared_feature("serde"));
  }

  #[test]
  fn test_should_skip_undeclared_feature_empty_patterns() {
    let config = UnifyConfig {
      skip_undeclared_patterns: vec![],
      ..Default::default()
    };
    // Nothing should be skipped with empty patterns
    assert!(!config.should_skip_undeclared_feature("default"));
    assert!(!config.should_skip_undeclared_feature("std"));
    assert!(!config.should_skip_undeclared_feature("anything"));
  }

  // sort_dependencies Tests

  #[test]
  fn test_sort_dependencies_default() {
    let config = UnifyConfig::default();
    assert!(config.sort_dependencies); // Default is true (alphabetical)
  }

  #[test]
  fn test_sort_dependencies_parsing_true() {
    let toml = r#"sort_dependencies = true"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.sort_dependencies);
  }

  #[test]
  fn test_sort_dependencies_parsing_false() {
    let toml = r#"sort_dependencies = false"#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(!config.sort_dependencies);
  }

  #[test]
  fn test_sort_dependencies_with_other_options() {
    let toml = r#"
      detect_unused = true
      remove_unused = true
      sort_dependencies = false
    "#;
    let config: UnifyConfig = toml_edit::de::from_str(toml).unwrap();
    assert!(config.detect_unused);
    assert!(config.remove_unused);
    assert!(!config.sort_dependencies);
  }
}