clippier 0.2.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
use clippier::OutputType;
use clippier::feature_validator::{FeatureValidator, ValidatorConfig};
use switchy_fs::TempDir;

/// Helper to create a test workspace for override testing
fn create_override_test_workspace() -> TempDir {
    let temp_dir = switchy_fs::tempdir().unwrap();
    let root_path = temp_dir.path();

    // Create workspace Cargo.toml
    let workspace_cargo = r#"[workspace]
members = ["pkg_a", "pkg_b", "pkg_c"]

[workspace.dependencies]
anyhow = "1.0"
"#;
    switchy_fs::sync::write(root_path.join("Cargo.toml"), workspace_cargo).unwrap();

    // Create pkg_b with test-feature
    switchy_fs::sync::create_dir(root_path.join("pkg_b")).unwrap();
    switchy_fs::sync::create_dir(root_path.join("pkg_b/src")).unwrap();
    switchy_fs::sync::write(root_path.join("pkg_b/src/lib.rs"), "").unwrap();
    let pkg_b_cargo = r#"[package]
name = "pkg_b"
version = "0.1.0"

[features]
test-feature = []
"#;
    switchy_fs::sync::write(root_path.join("pkg_b/Cargo.toml"), pkg_b_cargo).unwrap();

    // Create pkg_c with test-feature
    switchy_fs::sync::create_dir(root_path.join("pkg_c")).unwrap();
    switchy_fs::sync::create_dir(root_path.join("pkg_c/src")).unwrap();
    switchy_fs::sync::write(root_path.join("pkg_c/src/lib.rs"), "").unwrap();
    let pkg_c_cargo = r#"[package]
name = "pkg_c"
version = "0.1.0"

[features]
test-feature = []
"#;
    switchy_fs::sync::write(root_path.join("pkg_c/Cargo.toml"), pkg_c_cargo).unwrap();

    // Create pkg_a (depends on pkg_b and pkg_c but doesn't propagate test-feature)
    switchy_fs::sync::create_dir(root_path.join("pkg_a")).unwrap();
    switchy_fs::sync::create_dir(root_path.join("pkg_a/src")).unwrap();
    switchy_fs::sync::write(root_path.join("pkg_a/src/lib.rs"), "").unwrap();
    let pkg_a_cargo = r#"[package]
name = "pkg_a"
version = "0.1.0"

[dependencies]
pkg_b = { path = "../pkg_b" }
pkg_c = { path = "../pkg_c" }

[features]
test-feature = []
"#;
    switchy_fs::sync::write(root_path.join("pkg_a/Cargo.toml"), pkg_a_cargo).unwrap();

    temp_dir
}

#[switchy_async::test]
async fn test_workspace_clippier_toml_array_expansion() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Create workspace-level clippier.toml with array syntax
    let clippier_config = r#"
[[feature-validation.override]]
feature = "test-feature"
dependencies = ["pkg_b", "pkg_c"]
type = "allow-missing"
reason = "Testing array expansion in workspace config"
"#;
    switchy_fs::sync::write(root_path.join("clippier.toml"), clippier_config).unwrap();

    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    // Should have no errors because the array override suppresses both missing propagations
    assert_eq!(
        result.errors.len(),
        0,
        "Workspace-level array override should suppress errors"
    );

    // Verify override summary
    let summary = result.override_summary.as_ref().unwrap();
    assert_eq!(
        summary.total_applied, 2,
        "Should have 2 overrides applied (array expanded)"
    );
}

#[switchy_async::test]
async fn test_package_clippier_toml_array_expansion() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Create package-level clippier.toml with array syntax
    let clippier_config = r#"
[[feature-validation.override]]
feature = "test-feature"
dependencies = ["pkg_b", "pkg_c"]
type = "allow-missing"
reason = "Testing array expansion in package config"
"#;
    switchy_fs::sync::write(root_path.join("pkg_a/clippier.toml"), clippier_config).unwrap();

    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    // Should have no errors
    assert_eq!(
        result.errors.len(),
        0,
        "Package-level array override should suppress errors"
    );

    // Verify override summary
    let summary = result.override_summary.as_ref().unwrap();
    assert_eq!(summary.total_applied, 2, "Should have 2 overrides applied");

    // Verify overridden errors count
    assert_eq!(
        result.overridden_errors.len(),
        2,
        "Should have 2 overridden errors"
    );
}

#[switchy_async::test]
async fn test_cargo_metadata_array_expansion() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Create pkg_a with Cargo.toml metadata override using array syntax
    let pkg_a_cargo = r#"[package]
name = "pkg_a"
version = "0.1.0"

[package.metadata.clippier.feature-validation]
override = [
    { feature = "test-feature", dependencies = ["pkg_b", "pkg_c"], type = "allow-missing", reason = "Testing array in Cargo.toml metadata" }
]

[dependencies]
pkg_b = { path = "../pkg_b" }
pkg_c = { path = "../pkg_c" }

[features]
test-feature = []
"#;
    switchy_fs::sync::write(root_path.join("pkg_a/Cargo.toml"), pkg_a_cargo).unwrap();

    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    // Should have no errors
    assert_eq!(
        result.errors.len(),
        0,
        "Cargo.toml metadata array override should suppress errors"
    );

    // Verify 2 overrides were applied (expanded from array)
    let summary = result.override_summary.as_ref().unwrap();
    assert_eq!(
        summary.total_applied, 2,
        "Should have 2 overrides applied from Cargo.toml metadata"
    );
}

#[switchy_async::test]
async fn test_single_vs_array_equivalence() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Test 1: Single dependency syntax
    let clippier_single = r#"
[[feature-validation.override]]
feature = "test-feature"
dependency = "pkg_b"
type = "allow-missing"
reason = "Single syntax"

[[feature-validation.override]]
feature = "test-feature"
dependency = "pkg_c"
type = "allow-missing"
reason = "Single syntax"
"#;
    switchy_fs::sync::write(root_path.join("clippier.toml"), clippier_single).unwrap();

    let config1 = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator1 = FeatureValidator::new(Some(root_path.to_path_buf()), config1).unwrap();
    let result1 = validator1.validate().unwrap();

    // Test 2: Array syntax
    let clippier_array = r#"
[[feature-validation.override]]
feature = "test-feature"
dependencies = ["pkg_b", "pkg_c"]
type = "allow-missing"
reason = "Array syntax"
"#;
    switchy_fs::sync::write(root_path.join("clippier.toml"), clippier_array).unwrap();

    let config2 = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator2 = FeatureValidator::new(Some(root_path.to_path_buf()), config2).unwrap();
    let result2 = validator2.validate().unwrap();

    // Both should produce the same result
    assert_eq!(result1.errors.len(), result2.errors.len());
    assert_eq!(
        result1.override_summary.as_ref().unwrap().total_applied,
        result2.override_summary.as_ref().unwrap().total_applied
    );
    assert_eq!(result1.valid_packages, result2.valid_packages);
}

#[switchy_async::test]
async fn test_array_with_wildcards() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Use wildcard pattern in array
    let clippier_config = r#"
[[feature-validation.override]]
feature = "test-feature"
dependencies = ["pkg_*"]
type = "allow-missing"
reason = "Wildcard pattern in array"
"#;
    switchy_fs::sync::write(root_path.join("clippier.toml"), clippier_config).unwrap();

    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    // Should suppress errors for pkg_b and pkg_c via wildcard
    assert_eq!(
        result.errors.len(),
        0,
        "Wildcard in array should match multiple packages"
    );
}

#[switchy_async::test]
async fn test_mixed_array_and_single_entries() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Mix array and single syntax in same config
    let clippier_config = r#"
[[feature-validation.override]]
feature = "test-feature"
dependencies = ["pkg_b", "pkg_c"]
type = "allow-missing"
reason = "Array syntax"
"#;
    switchy_fs::sync::write(root_path.join("clippier.toml"), clippier_config).unwrap();

    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default()
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    assert_eq!(result.errors.len(), 0);
    let summary = result.override_summary.as_ref().unwrap();
    assert_eq!(summary.total_applied, 2);
}

#[switchy_async::test]
async fn test_empty_array_handling() {
    // Test that empty arrays don't cause crashes (though they're not useful)
    let toml_str = r#"
feature = "test-feature"
dependencies = []
type = "allow-missing"
reason = "Empty array test"
"#;

    let result: Result<clippier::feature_validator::OverrideConfigEntry, _> =
        toml::from_str(toml_str);
    assert!(result.is_ok(), "Empty array should parse successfully");

    let entry = result.unwrap();
    assert_eq!(
        entry.dependency.to_vec().len(),
        0,
        "Empty array should produce empty vec"
    );
}

#[switchy_async::test]
async fn test_validation_without_overrides() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Don't create any override configs
    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::test_default() // Use test_default which disables overrides
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    // Should have errors for pkg_a not propagating to pkg_b and pkg_c
    assert!(
        !result.errors.is_empty(),
        "Should have validation errors without overrides"
    );

    // Find pkg_a errors
    let pkg_a_errors = result.errors.iter().find(|e| e.package == "pkg_a");
    assert!(pkg_a_errors.is_some(), "pkg_a should have errors");

    let errors = &pkg_a_errors.unwrap().errors;
    assert!(!errors.is_empty(), "pkg_a should have feature errors");

    let test_feature_error = errors.iter().find(|e| e.feature == "test-feature");
    assert!(
        test_feature_error.is_some(),
        "Should have error for test-feature"
    );

    let missing = &test_feature_error.unwrap().missing_propagations;
    assert_eq!(missing.len(), 2, "Should have 2 missing propagations");
}

#[switchy_async::test]
async fn test_validation_with_overrides_enabled() {
    let temp_dir = create_override_test_workspace();
    let root_path = temp_dir.path();

    // Create override with array syntax
    let clippier_config = r#"
[[feature-validation.override]]
feature = "test-feature"
dependencies = ["pkg_b", "pkg_c"]
type = "allow-missing"
reason = "Integration test for array overrides"
"#;
    switchy_fs::sync::write(root_path.join("pkg_a/clippier.toml"), clippier_config).unwrap();

    let config = ValidatorConfig {
        features: Some(vec!["test-feature".to_string()]),
        skip_features: None,
        workspace_only: true,
        output_format: OutputType::Raw,
        ..ValidatorConfig::default() // Default enables overrides
    };

    let validator = FeatureValidator::new(Some(root_path.to_path_buf()), config).unwrap();
    let result = validator.validate().unwrap();

    // Should have no errors (all suppressed)
    assert_eq!(
        result.errors.len(),
        0,
        "All errors should be suppressed by overrides"
    );

    // Should have override summary
    assert!(
        result.override_summary.is_some(),
        "Should have override summary"
    );
    let summary = result.override_summary.as_ref().unwrap();
    assert_eq!(summary.total_applied, 2, "Should apply 2 overrides");

    // Should have overridden errors listed
    assert!(
        !result.overridden_errors.is_empty(),
        "Should have overridden errors"
    );
    let overridden = &result.overridden_errors;
    assert_eq!(overridden.len(), 2, "Should have 2 overridden errors");

    // Verify the overridden errors are for the right dependencies
    let has_pkg_b = overridden.iter().any(|e| e.dependency == "pkg_b");
    let has_pkg_c = overridden.iter().any(|e| e.dependency == "pkg_c");
    assert!(has_pkg_b, "Should have overridden error for pkg_b");
    assert!(has_pkg_c, "Should have overridden error for pkg_c");
}