clippier 0.3.0

MoosicBox clippier package
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
//! Transform context providing workspace metadata and analysis capabilities.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use toml::Value;

use crate::WorkspaceContext;

type BoxError = Box<dyn std::error::Error + Send + Sync>;

/// Context available to transform scripts
#[derive(Clone)]
pub struct TransformContext {
    packages: BTreeMap<String, PackageInfo>,
}

/// Package metadata
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PackageInfo {
    /// Package name as defined in `Cargo.toml`.
    pub name: String,
    /// Absolute package path inside the workspace.
    pub path: PathBuf,
    /// Parsed `Cargo.toml` content for this package.
    #[serde(skip, default = "default_cargo_toml")]
    pub cargo_toml: Value,
    /// Package feature definitions keyed by feature name.
    pub features: BTreeMap<String, Vec<String>>,
    /// Dependencies declared across dependency sections.
    pub dependencies: Vec<DependencyInfo>,
}

#[must_use]
fn default_cargo_toml() -> Value {
    Value::Table(toml::map::Map::new())
}

/// Dependency information
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DependencyInfo {
    /// Dependency crate name.
    pub name: String,
    /// Whether this dependency is marked as optional.
    pub optional: bool,
    /// Whether this dependency points to another workspace member.
    pub workspace_member: bool,
    /// Explicit dependency features enabled for this dependency.
    pub features: Vec<String>,
}

impl TransformContext {
    /// Create a new transform context by analyzing the workspace
    ///
    /// # Errors
    ///
    /// * Workspace root not found
    /// * Failed to read package metadata
    /// * Invalid Cargo.toml files
    pub fn new(workspace_root: &Path) -> Result<Self, BoxError> {
        let workspace = WorkspaceContext::new(workspace_root)?;

        // Load all package metadata
        let mut packages = BTreeMap::new();
        workspace.ensure_fully_loaded();

        // Get all workspace members
        let members = get_workspace_members(&workspace, workspace_root)?;

        for (name, path) in members {
            let cargo_path = path.join("Cargo.toml");
            if !switchy_fs::exists(&cargo_path) {
                continue;
            }

            let cargo_toml: Value =
                toml::from_str(&switchy_fs::sync::read_to_string(&cargo_path)?)?;

            let features = extract_features(&cargo_toml);
            let dependencies = extract_dependencies(&cargo_toml, &workspace, &path);

            packages.insert(
                name.clone(),
                PackageInfo {
                    name,
                    path,
                    cargo_toml,
                    features,
                    dependencies,
                },
            );
        }

        Ok(Self { packages })
    }

    /// Get package metadata by name
    #[must_use]
    pub fn get_package(&self, name: &str) -> Option<&PackageInfo> {
        self.packages.get(name)
    }

    /// Check if a name is a workspace member
    #[must_use]
    pub fn is_workspace_member(&self, name: &str) -> bool {
        self.packages.contains_key(name)
    }

    /// Get all package names
    #[must_use]
    pub fn get_all_packages(&self) -> Vec<String> {
        self.packages.keys().cloned().collect()
    }

    /// Check if a package depends on another package
    #[must_use]
    pub fn package_depends_on(&self, package: &str, dependency: &str) -> bool {
        self.packages
            .get(package)
            .is_some_and(|pkg| pkg.dependencies.iter().any(|dep| dep.name == dependency))
    }

    /// Check if a feature exists in a package
    #[must_use]
    pub fn feature_exists(&self, package: &str, feature: &str) -> bool {
        self.packages
            .get(package)
            .is_some_and(|pkg| pkg.features.contains_key(feature))
    }
}

impl PackageInfo {
    /// Check if this package depends on another package
    #[must_use]
    pub fn depends_on(&self, dep_name: &str) -> bool {
        self.dependencies.iter().any(|dep| dep.name == dep_name)
    }

    /// Check if a feature exists
    #[must_use]
    pub fn has_feature(&self, feature: &str) -> bool {
        self.features.contains_key(feature)
    }

    /// Get feature definition
    #[must_use]
    pub fn feature_definition(&self, feature: &str) -> Option<&Vec<String>> {
        self.features.get(feature)
    }

    /// Get dependencies activated by a feature
    #[must_use]
    pub fn feature_activates_dependencies(&self, feature: &str) -> Vec<DependencyInfo> {
        let Some(feature_def) = self.features.get(feature) else {
            return vec![];
        };

        let mut activated_deps = vec![];

        for entry in feature_def {
            if entry.contains('/') {
                let parts: Vec<_> = entry.split('/').collect();
                let dep_name = parts[0].trim_end_matches('?');
                let dep_feature = parts[1];

                // Find the dependency
                if let Some(dep) = self.dependencies.iter().find(|d| d.name == dep_name) {
                    let mut dep_info = dep.clone();
                    dep_info.features = vec![dep_feature.to_string()];
                    activated_deps.push(dep_info);
                }
            }
        }

        activated_deps
    }

    /// Check if a feature is skipped on a specific OS (from clippier.toml)
    #[must_use]
    pub fn skips_feature_on_os(&self, feature: &str, os: &str) -> bool {
        let clippier_path = self.path.join("clippier.toml");
        let Ok(content) = switchy_fs::sync::read_to_string(clippier_path) else {
            return false;
        };

        let Ok(conf) = toml::from_str::<Value>(&content) else {
            return false;
        };

        // Check if any OS config has skip-features containing this feature
        if let Some(configs) = conf.get("config").and_then(|c| c.as_array()) {
            for config in configs {
                if let Some(config_os) = config.get("os").and_then(|o| o.as_str())
                    && (config_os == os || os.contains(config_os))
                    && let Some(skip_features) = config.get("skip-features")
                    && let Some(arr) = skip_features.as_array()
                {
                    for skip_feature in arr {
                        if skip_feature.as_str() == Some(feature) {
                            return true;
                        }
                    }
                }
            }
        }

        false
    }

    /// Get all features
    #[must_use]
    pub fn get_all_features(&self) -> Vec<String> {
        self.features.keys().cloned().collect()
    }
}

/// Extract features from Cargo.toml
#[must_use]
fn extract_features(cargo_toml: &Value) -> BTreeMap<String, Vec<String>> {
    let Some(features_table) = cargo_toml.get("features").and_then(|f| f.as_table()) else {
        return BTreeMap::new();
    };

    let mut features = BTreeMap::new();

    for (name, value) in features_table {
        if let Some(arr) = value.as_array() {
            let feature_list: Vec<String> = arr
                .iter()
                .filter_map(|v| v.as_str().map(ToString::to_string))
                .collect();
            features.insert(name.clone(), feature_list);
        }
    }

    features
}

/// Extract dependencies from Cargo.toml
#[must_use]
fn extract_dependencies(
    cargo_toml: &Value,
    workspace: &WorkspaceContext,
    _package_path: &Path,
) -> Vec<DependencyInfo> {
    let mut dependencies = vec![];

    let sections = ["dependencies", "dev-dependencies", "build-dependencies"];

    for section in &sections {
        let Some(deps_table) = cargo_toml.get(section).and_then(|d| d.as_table()) else {
            continue;
        };

        for (name, value) in deps_table {
            let optional = value
                .as_table()
                .and_then(|table| table.get("optional"))
                .and_then(Value::as_bool)
                .unwrap_or(false);

            let features = value
                .as_table()
                .and_then(|table| table.get("features"))
                .and_then(Value::as_array)
                .map_or_else(Vec::new, |feat_array| {
                    feat_array
                        .iter()
                        .filter_map(|v| v.as_str().map(ToString::to_string))
                        .collect()
                });

            let workspace_member = workspace.is_member_by_name(name);

            dependencies.push(DependencyInfo {
                name: name.clone(),
                optional,
                workspace_member,
                features,
            });
        }
    }

    dependencies
}

/// Get all workspace members
fn get_workspace_members(
    _workspace: &WorkspaceContext,
    workspace_root: &Path,
) -> Result<BTreeMap<String, PathBuf>, BoxError> {
    let mut members = BTreeMap::new();

    let workspace_cargo = workspace_root.join("Cargo.toml");
    let content = switchy_fs::sync::read_to_string(workspace_cargo)?;
    let workspace_toml: Value = toml::from_str(&content)?;

    if let Some(member_patterns) = workspace_toml
        .get("workspace")
        .and_then(|w| w.get("members"))
        .and_then(|m| m.as_array())
    {
        for pattern in member_patterns {
            if let Some(pattern_str) = pattern.as_str() {
                // Simple glob expansion - handle patterns like "packages/*"
                if pattern_str.contains('*') {
                    let parts: Vec<_> = pattern_str.split('/').collect();

                    // Handle patterns like "packages/*" or "*"
                    if parts.last() == Some(&"*") || parts.last() == Some(&"**") {
                        let base_path = if parts.len() > 1 {
                            workspace_root.join(parts[..parts.len() - 1].join("/"))
                        } else {
                            workspace_root.to_path_buf()
                        };

                        // Scan directories in the base path
                        if switchy_fs::exists(&base_path)
                            && let Ok(entries) = switchy_fs::sync::read_dir_sorted(&base_path)
                        {
                            for entry in entries {
                                if entry.file_type().is_ok_and(|ft| ft.is_dir()) {
                                    let member_path = entry.path();
                                    let cargo_path = member_path.join("Cargo.toml");
                                    if switchy_fs::exists(&cargo_path)
                                        && let Some(name) =
                                            WorkspaceContext::read_package_name(&member_path)
                                    {
                                        members.insert(name, member_path);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    let member_path = workspace_root.join(pattern_str);
                    if switchy_fs::exists(&member_path)
                        && let Some(name) = WorkspaceContext::read_package_name(&member_path)
                    {
                        members.insert(name, member_path);
                    }
                }
            }
        }
    }

    Ok(members)
}

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

    #[test]
    fn test_extract_features_with_valid_features() {
        let cargo_toml = toml::from_str::<Value>(
            r#"
[features]
default = ["feature1"]
feature1 = ["dep1/feature1"]
feature2 = []
"#,
        )
        .unwrap();

        let features = extract_features(&cargo_toml);

        assert_eq!(features.len(), 3);
        assert_eq!(features.get("default"), Some(&vec!["feature1".to_string()]));
        assert_eq!(
            features.get("feature1"),
            Some(&vec!["dep1/feature1".to_string()])
        );
        assert_eq!(features.get("feature2"), Some(&vec![]));
    }

    #[test]
    fn test_extract_features_empty_cargo_toml() {
        let cargo_toml = toml::from_str::<Value>("[package]\nname = \"test\"").unwrap();
        let features = extract_features(&cargo_toml);
        assert!(features.is_empty());
    }

    #[test]
    fn test_extract_features_non_array_values() {
        let cargo_toml = toml::from_str::<Value>(
            r#"
[features]
valid = ["dep1"]
invalid = "string_value"
"#,
        )
        .unwrap();

        let features = extract_features(&cargo_toml);
        assert_eq!(features.len(), 1);
        assert!(features.contains_key("valid"));
        assert!(!features.contains_key("invalid"));
    }

    #[test]
    fn test_package_info_depends_on() {
        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features: BTreeMap::new(),
            dependencies: vec![
                DependencyInfo {
                    name: "dep1".to_string(),
                    optional: false,
                    workspace_member: false,
                    features: vec![],
                },
                DependencyInfo {
                    name: "dep2".to_string(),
                    optional: true,
                    workspace_member: true,
                    features: vec!["feature1".to_string()],
                },
            ],
        };

        assert!(pkg_info.depends_on("dep1"));
        assert!(pkg_info.depends_on("dep2"));
        assert!(!pkg_info.depends_on("dep3"));
    }

    #[test]
    fn test_package_info_has_feature() {
        let mut features = BTreeMap::new();
        features.insert("feature1".to_string(), vec![]);
        features.insert("feature2".to_string(), vec!["dep1/feature2".to_string()]);

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features,
            dependencies: vec![],
        };

        assert!(pkg_info.has_feature("feature1"));
        assert!(pkg_info.has_feature("feature2"));
        assert!(!pkg_info.has_feature("feature3"));
    }

    #[test]
    fn test_package_info_feature_definition() {
        let mut features = BTreeMap::new();
        features.insert(
            "test_feature".to_string(),
            vec!["dep1/feature1".to_string(), "dep2/feature2".to_string()],
        );

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features,
            dependencies: vec![],
        };

        let def = pkg_info.feature_definition("test_feature");
        assert!(def.is_some());
        assert_eq!(def.unwrap().len(), 2);

        assert!(pkg_info.feature_definition("nonexistent").is_none());
    }

    #[test]
    fn test_feature_activates_dependencies_basic() {
        let mut features = BTreeMap::new();
        features.insert(
            "test_feature".to_string(),
            vec![
                "dep1/feature1".to_string(),
                "standalone_feature".to_string(),
            ],
        );

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features,
            dependencies: vec![DependencyInfo {
                name: "dep1".to_string(),
                optional: false,
                workspace_member: false,
                features: vec![],
            }],
        };

        let activated = pkg_info.feature_activates_dependencies("test_feature");
        assert_eq!(activated.len(), 1);
        assert_eq!(activated[0].name, "dep1");
        assert_eq!(activated[0].features, vec!["feature1".to_string()]);
    }

    #[test]
    fn test_feature_activates_dependencies_with_optional() {
        let mut features = BTreeMap::new();
        features.insert(
            "test_feature".to_string(),
            vec!["dep1?/feature1".to_string(), "dep2/feature2".to_string()],
        );

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features,
            dependencies: vec![
                DependencyInfo {
                    name: "dep1".to_string(),
                    optional: true,
                    workspace_member: false,
                    features: vec![],
                },
                DependencyInfo {
                    name: "dep2".to_string(),
                    optional: false,
                    workspace_member: true,
                    features: vec![],
                },
            ],
        };

        let activated = pkg_info.feature_activates_dependencies("test_feature");
        assert_eq!(activated.len(), 2);
        assert_eq!(activated[0].name, "dep1");
        assert_eq!(activated[0].features, vec!["feature1".to_string()]);
        assert_eq!(activated[1].name, "dep2");
        assert_eq!(activated[1].features, vec!["feature2".to_string()]);
    }

    #[test]
    fn test_feature_activates_dependencies_nonexistent_feature() {
        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features: BTreeMap::new(),
            dependencies: vec![],
        };

        let activated = pkg_info.feature_activates_dependencies("nonexistent");
        assert!(activated.is_empty());
    }

    #[test]
    fn test_feature_activates_dependencies_no_slash() {
        let mut features = BTreeMap::new();
        features.insert(
            "test_feature".to_string(),
            vec!["standalone".to_string(), "another_standalone".to_string()],
        );

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features,
            dependencies: vec![],
        };

        let activated = pkg_info.feature_activates_dependencies("test_feature");
        assert!(activated.is_empty());
    }

    #[test]
    fn test_skips_feature_on_os_no_clippier_toml() {
        let temp_dir = switchy_fs::tempdir().unwrap();
        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: temp_dir.path().to_path_buf(),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features: BTreeMap::new(),
            dependencies: vec![],
        };

        assert!(!pkg_info.skips_feature_on_os("feature1", "linux"));
    }

    #[test]
    fn test_skips_feature_on_os_with_config() {
        let temp_dir = switchy_fs::tempdir().unwrap();
        let clippier_toml = r#"
[[config]]
os = "windows"
skip-features = ["windows_only_feature"]

[[config]]
os = "linux"
skip-features = ["linux_only_feature"]
"#;
        switchy_fs::sync::write(temp_dir.path().join("clippier.toml"), clippier_toml).unwrap();

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: temp_dir.path().to_path_buf(),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features: BTreeMap::new(),
            dependencies: vec![],
        };

        assert!(pkg_info.skips_feature_on_os("windows_only_feature", "windows"));
        assert!(pkg_info.skips_feature_on_os("linux_only_feature", "linux"));
        assert!(!pkg_info.skips_feature_on_os("windows_only_feature", "linux"));
        assert!(!pkg_info.skips_feature_on_os("nonexistent_feature", "linux"));
    }

    #[test]
    fn test_skips_feature_on_os_malformed_toml() {
        let temp_dir = switchy_fs::tempdir().unwrap();
        switchy_fs::sync::write(
            temp_dir.path().join("clippier.toml"),
            "invalid toml content [[[",
        )
        .unwrap();

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: temp_dir.path().to_path_buf(),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features: BTreeMap::new(),
            dependencies: vec![],
        };

        assert!(!pkg_info.skips_feature_on_os("feature1", "linux"));
    }

    #[test]
    fn test_get_all_features() {
        let mut features = BTreeMap::new();
        features.insert("feature1".to_string(), vec![]);
        features.insert("feature2".to_string(), vec!["dep1/feature2".to_string()]);
        features.insert("feature3".to_string(), vec![]);

        let pkg_info = PackageInfo {
            name: "test_pkg".to_string(),
            path: PathBuf::from("/test"),
            cargo_toml: Value::Table(toml::map::Map::new()),
            features,
            dependencies: vec![],
        };

        let all_features = pkg_info.get_all_features();
        assert_eq!(all_features.len(), 3);
        assert!(all_features.contains(&"feature1".to_string()));
        assert!(all_features.contains(&"feature2".to_string()));
        assert!(all_features.contains(&"feature3".to_string()));
    }

    #[test]
    fn test_extract_dependencies_all_sections() {
        let cargo_toml = toml::from_str::<Value>(
            r#"
[package]
name = "test_pkg"

[dependencies]
regular_dep = "1.0"
optional_dep = { version = "1.0", optional = true, features = ["feature1"] }

[dev-dependencies]
dev_dep = "2.0"

[build-dependencies]
build_dep = { version = "3.0", features = ["build_feature"] }
"#,
        )
        .unwrap();

        let temp_dir = switchy_fs::tempdir().unwrap();
        switchy_fs::sync::write(
            temp_dir.path().join("Cargo.toml"),
            "[workspace]\nmembers = []",
        )
        .unwrap();

        let workspace = WorkspaceContext::new(temp_dir.path()).unwrap();
        let deps = extract_dependencies(&cargo_toml, &workspace, temp_dir.path());

        assert_eq!(deps.len(), 4);

        let regular_dep = deps.iter().find(|d| d.name == "regular_dep").unwrap();
        assert!(!regular_dep.optional);
        assert!(regular_dep.features.is_empty());

        let optional_dep = deps.iter().find(|d| d.name == "optional_dep").unwrap();
        assert!(optional_dep.optional);
        assert_eq!(optional_dep.features, vec!["feature1".to_string()]);

        let dev_dep = deps.iter().find(|d| d.name == "dev_dep").unwrap();
        assert!(!dev_dep.optional);

        let build_dep = deps.iter().find(|d| d.name == "build_dep").unwrap();
        assert_eq!(build_dep.features, vec!["build_feature".to_string()]);
    }

    #[test]
    fn test_extract_dependencies_empty() {
        let cargo_toml = toml::from_str::<Value>("[package]\nname = \"test_pkg\"").unwrap();

        let temp_dir = switchy_fs::tempdir().unwrap();
        switchy_fs::sync::write(
            temp_dir.path().join("Cargo.toml"),
            "[workspace]\nmembers = []",
        )
        .unwrap();

        let workspace = WorkspaceContext::new(temp_dir.path()).unwrap();
        let deps = extract_dependencies(&cargo_toml, &workspace, temp_dir.path());

        assert!(deps.is_empty());
    }

    #[test]
    fn test_get_workspace_members_simple() {
        let temp_dir = switchy_fs::tempdir().unwrap();
        let root = temp_dir.path();

        // Create workspace
        switchy_fs::sync::write(
            root.join("Cargo.toml"),
            r#"
[workspace]
members = ["pkg1", "pkg2"]
"#,
        )
        .unwrap();

        // Create pkg1
        let pkg1 = root.join("pkg1");
        switchy_fs::sync::create_dir_all(&pkg1).unwrap();
        switchy_fs::sync::write(pkg1.join("Cargo.toml"), "[package]\nname = \"pkg1\"").unwrap();

        // Create pkg2
        let pkg2 = root.join("pkg2");
        switchy_fs::sync::create_dir_all(&pkg2).unwrap();
        switchy_fs::sync::write(pkg2.join("Cargo.toml"), "[package]\nname = \"pkg2\"").unwrap();

        let workspace = WorkspaceContext::new(root).unwrap();
        let members = get_workspace_members(&workspace, root).unwrap();

        assert_eq!(members.len(), 2);
        assert!(members.contains_key("pkg1"));
        assert!(members.contains_key("pkg2"));
    }

    #[test]
    fn test_get_workspace_members_with_glob() {
        let temp_dir = switchy_fs::tempdir().unwrap();
        let root = temp_dir.path();

        // Create workspace with glob pattern
        switchy_fs::sync::write(
            root.join("Cargo.toml"),
            r#"
[workspace]
members = ["packages/*"]
"#,
        )
        .unwrap();

        // Create packages directory
        let packages_dir = root.join("packages");
        switchy_fs::sync::create_dir_all(&packages_dir).unwrap();

        // Create pkg1
        let pkg1 = packages_dir.join("pkg1");
        switchy_fs::sync::create_dir_all(&pkg1).unwrap();
        switchy_fs::sync::write(pkg1.join("Cargo.toml"), "[package]\nname = \"pkg1\"").unwrap();

        // Create pkg2
        let pkg2 = packages_dir.join("pkg2");
        switchy_fs::sync::create_dir_all(&pkg2).unwrap();
        switchy_fs::sync::write(pkg2.join("Cargo.toml"), "[package]\nname = \"pkg2\"").unwrap();

        let workspace = WorkspaceContext::new(root).unwrap();
        let members = get_workspace_members(&workspace, root).unwrap();

        assert_eq!(members.len(), 2);
        assert!(members.contains_key("pkg1"));
        assert!(members.contains_key("pkg2"));
    }

    #[test]
    fn test_get_workspace_members_missing_cargo_toml() {
        let temp_dir = switchy_fs::tempdir().unwrap();
        let root = temp_dir.path();

        switchy_fs::sync::write(
            root.join("Cargo.toml"),
            r#"
[workspace]
members = ["pkg1"]
"#,
        )
        .unwrap();

        // Create directory without Cargo.toml
        switchy_fs::sync::create_dir_all(root.join("pkg1")).unwrap();

        let workspace = WorkspaceContext::new(root).unwrap();
        let members = get_workspace_members(&workspace, root).unwrap();

        assert!(members.is_empty());
    }
}