fallow-config 3.46.0

Configuration types and workspace discovery for fallow codebase intelligence
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
use super::*;

use AnalysisRequest::{Default as D, Excluded as X, Named as N};
use AnalysisSelection::{Run, Skipped};
use ConfigKey::{AuditDeadCode, AuditDupes, AuditHealth, DuplicatesEnabled};
use PlannedAnalysis::{DeadCode, Duplication, Health};
use RunCommand::{Audit, Combined};
use SkipReason::{Config, NotRequested};

const ANALYSES: [PlannedAnalysis; 3] = [DeadCode, Duplication, Health];
const COMMANDS: [RunCommand; 2] = [Combined, Audit];
const REQUESTS: [AnalysisRequest; 3] = [N, D, X];
const KEY_VALUES: [Option<bool>; 3] = [None, Some(true), Some(false)];

/// The `audit.*` key column of a rule row.
#[derive(Clone, Copy)]
enum Key {
    /// Every value.
    Any,
    /// The key is not set.
    Unset,
    /// The key is `true`.
    On,
    /// The key is `false`.
    Off,
}

impl Key {
    const fn matches(self, value: Option<bool>) -> bool {
        matches!(
            (self, value),
            (Self::Any, _)
                | (Self::Unset, None)
                | (Self::On, Some(true))
                | (Self::Off, Some(false))
        )
    }
}

/// One row of the rule for one analysis. `None` in a column matches every
/// value. `key` is the `audit.*` key of the analysis itself.
struct Row {
    analysis: Option<PlannedAnalysis>,
    command: Option<RunCommand>,
    request: AnalysisRequest,
    enabled: Option<bool>,
    key: Key,
    expected: AnalysisSelection,
}

const fn row(
    analysis: Option<PlannedAnalysis>,
    command: Option<RunCommand>,
    request: AnalysisRequest,
    enabled: Option<bool>,
    key: Key,
    expected: AnalysisSelection,
) -> Row {
    Row {
        analysis,
        command,
        request,
        enabled,
        key,
        expected,
    }
}

/// The rule, as literal rows. Each combination of inputs matches one row.
const RULE: [Row; 16] = [
    row(None, None, N, None, Key::Any, Run),
    row(None, None, X, None, Key::Any, Skipped(NotRequested)),
    row(Some(DeadCode), Some(Combined), D, None, Key::Any, Run),
    row(Some(Health), Some(Combined), D, None, Key::Any, Run),
    row(
        Some(Duplication),
        Some(Combined),
        D,
        Some(true),
        Key::Any,
        Run,
    ),
    row(
        Some(Duplication),
        Some(Combined),
        D,
        Some(false),
        Key::Any,
        Skipped(Config(DuplicatesEnabled)),
    ),
    row(Some(DeadCode), Some(Audit), D, None, Key::Unset, Run),
    row(Some(DeadCode), Some(Audit), D, None, Key::On, Run),
    row(
        Some(DeadCode),
        Some(Audit),
        D,
        None,
        Key::Off,
        Skipped(Config(AuditDeadCode)),
    ),
    row(Some(Health), Some(Audit), D, None, Key::Unset, Run),
    row(Some(Health), Some(Audit), D, None, Key::On, Run),
    row(
        Some(Health),
        Some(Audit),
        D,
        None,
        Key::Off,
        Skipped(Config(AuditHealth)),
    ),
    row(
        Some(Duplication),
        Some(Audit),
        D,
        None,
        Key::Off,
        Skipped(Config(AuditDupes)),
    ),
    // `audit.dupes: true` wins over `duplicates.enabled: false`.
    row(Some(Duplication), Some(Audit), D, None, Key::On, Run),
    row(
        Some(Duplication),
        Some(Audit),
        D,
        Some(true),
        Key::Unset,
        Run,
    ),
    row(
        Some(Duplication),
        Some(Audit),
        D,
        Some(false),
        Key::Unset,
        Skipped(Config(DuplicatesEnabled)),
    ),
];

fn duplicates(enabled: bool) -> DuplicatesConfig {
    DuplicatesConfig {
        enabled,
        ..DuplicatesConfig::default()
    }
}

fn audit(dead_code: Option<bool>, health: Option<bool>, dupes: Option<bool>) -> AuditConfig {
    AuditConfig {
        dead_code,
        health,
        dupes,
        ..AuditConfig::default()
    }
}

fn plan(enabled: bool, audit_config: &AuditConfig, request: RunRequest) -> RunPlan {
    plan_run(
        PlanSections {
            duplicates: &duplicates(enabled),
            audit: audit_config,
        },
        request,
    )
}

const fn own_key(analysis: PlannedAnalysis, config: &AuditConfig) -> Option<bool> {
    match analysis {
        PlannedAnalysis::DeadCode => config.dead_code,
        PlannedAnalysis::Duplication => config.dupes,
        PlannedAnalysis::Health => config.health,
    }
}

const fn request_of(request: RunRequest, analysis: PlannedAnalysis) -> AnalysisRequest {
    match analysis {
        PlannedAnalysis::DeadCode => request.dead_code,
        PlannedAnalysis::Duplication => request.duplication,
        PlannedAnalysis::Health => request.health,
    }
}

const fn decision_of(plan: RunPlan, analysis: PlannedAnalysis) -> AnalysisSelection {
    match analysis {
        PlannedAnalysis::DeadCode => plan.dead_code,
        PlannedAnalysis::Duplication => plan.duplication,
        PlannedAnalysis::Health => plan.health,
    }
}

fn matches_column<T: PartialEq + Copy>(column: Option<T>, value: T) -> bool {
    column.is_none_or(|expected| expected == value)
}

/// Every command, request triple, `duplicates.enabled` value and `audit.*`
/// triple. Each analysis of each plan equals the one rule row that matches.
#[test]
fn plan_run_follows_the_rule_table_for_every_input() {
    let mut used = [false; RULE.len()];
    for command in COMMANDS {
        for dead_code in REQUESTS {
            for duplication in REQUESTS {
                for health in REQUESTS {
                    let request = RunRequest {
                        command,
                        dead_code,
                        duplication,
                        health,
                    };
                    check_every_config(request, &mut used);
                }
            }
        }
    }
    assert!(used.iter().all(|row| *row), "unused rule rows: {used:?}");
}

fn check_every_config(request: RunRequest, used: &mut [bool; RULE.len()]) {
    for enabled in [true, false] {
        for dead_code in KEY_VALUES {
            for health in KEY_VALUES {
                for dupes in KEY_VALUES {
                    let config = audit(dead_code, health, dupes);
                    let plan = plan(enabled, &config, request);
                    for analysis in ANALYSES {
                        let matching: Vec<usize> = RULE
                            .iter()
                            .enumerate()
                            .filter(|(_, row)| {
                                matches_column(row.analysis, analysis)
                                    && matches_column(row.command, request.command)
                                    && row.request == request_of(request, analysis)
                                    && matches_column(row.enabled, enabled)
                                    && row.key.matches(own_key(analysis, &config))
                            })
                            .map(|(index, _)| index)
                            .collect();
                        let [index] = matching[..] else {
                            panic!(
                                "{analysis:?} {request:?} {enabled} {config:?}: rows {matching:?}"
                            );
                        };
                        used[index] = true;
                        assert_eq!(
                            decision_of(plan, analysis),
                            RULE[index].expected,
                            "{analysis:?} {request:?} enabled {enabled} {config:?}"
                        );
                    }
                }
            }
        }
    }
}

#[test]
fn audit_dupes_true_runs_duplication_with_duplicates_enabled_false() {
    let request = RunRequest::from_options(Audit, None, None, None);
    let plan = plan(false, &audit(None, None, Some(true)), request);
    assert_eq!(plan.duplication, Run);
}

#[test]
fn combined_ignores_the_audit_keys() {
    let request = RunRequest::from_lists(Combined, &[], &[]);
    let config = audit(Some(false), Some(false), Some(false));
    let plan = plan(true, &config, request);
    assert_eq!(
        plan,
        RunPlan {
            dead_code: Run,
            duplication: Run,
            health: Run,
        }
    );
    assert!(plan.runs_any());
}

#[test]
fn reads_config_only_when_a_default_request_meets_a_key() {
    assert!(RunRequest::from_lists(Combined, &[], &[]).reads_config());
    assert!(!RunRequest::from_lists(Combined, &[], &[Duplication]).reads_config());
    assert!(!RunRequest::from_lists(Combined, &[Duplication, Health], &[]).reads_config());
    assert!(RunRequest::from_lists(Audit, &[], &[Duplication]).reads_config());
    assert!(!RunRequest::from_lists(Audit, &[DeadCode], &[]).reads_config());
    assert!(!RunRequest::all_named(Audit).reads_config());
    assert!(!RunRequest::all_named(Combined).reads_config());
}

#[test]
fn from_lists_names_only_and_excludes_skip() {
    let only = RunRequest::from_lists(Combined, &[Duplication, Health], &[]);
    assert_eq!((only.dead_code, only.duplication, only.health), (X, N, N));

    let skip = RunRequest::from_lists(Audit, &[], &[DeadCode]);
    assert_eq!((skip.dead_code, skip.duplication, skip.health), (X, D, D));
    assert_eq!(skip.command, Audit);

    let none = RunRequest::from_lists(Combined, &[], &[]);
    assert_eq!((none.dead_code, none.duplication, none.health), (D, D, D));
    assert!(ANALYSES.iter().all(|analysis| none.requested(*analysis)));
    assert!(!skip.requested(DeadCode));
}

#[test]
fn from_booleans_names_a_lone_analysis_only() {
    let alone = RunRequest::from_booleans(Combined, false, true, false);
    assert_eq!(
        (alone.dead_code, alone.duplication, alone.health),
        (X, N, X)
    );

    let pair = RunRequest::from_booleans(Combined, true, true, false);
    assert_eq!((pair.dead_code, pair.duplication, pair.health), (D, D, X));

    let all = RunRequest::from_booleans(Combined, true, true, true);
    assert_eq!((all.dead_code, all.duplication, all.health), (D, D, D));

    let lone_dead_code = RunRequest::from_booleans(Combined, true, false, false);
    assert_eq!(
        (
            lone_dead_code.dead_code,
            lone_dead_code.duplication,
            lone_dead_code.health
        ),
        (N, X, X)
    );
}

#[test]
fn from_options_maps_each_option() {
    assert_eq!(AnalysisRequest::from_option(Some(true)), N);
    assert_eq!(AnalysisRequest::from_option(Some(false)), X);
    assert_eq!(AnalysisRequest::from_option(None), D);
    let request = RunRequest::from_options(Audit, Some(true), None, Some(false));
    assert_eq!(
        (request.dead_code, request.duplication, request.health),
        (N, D, X)
    );
    assert_eq!(
        RunRequest::all_named(Audit),
        RunRequest::from_options(Audit, Some(true), Some(true), Some(true))
    );
}

#[test]
fn selection_reports_the_key_that_turned_it_off() {
    assert!(Skipped(Config(AuditDupes)).off_by(AuditDupes));
    assert!(Skipped(Config(DuplicatesEnabled)).off_by(DuplicatesEnabled));
    assert!(!Skipped(Config(AuditDupes)).off_by(DuplicatesEnabled));
    assert!(!Skipped(NotRequested).off_by(DuplicatesEnabled));
    assert!(!Run.off_by(DuplicatesEnabled));
}

#[test]
fn runs_by_default_is_the_combined_default_plan() {
    for enabled in [true, false] {
        let config = duplicates(enabled);
        let plan = plan_run(
            PlanSections {
                duplicates: &config,
                audit: &AuditConfig::default(),
            },
            RunRequest::from_lists(Combined, &[], &[]),
        );
        assert_eq!(config.runs_by_default(), plan.duplication.runs());
        assert_eq!(config.runs_by_default(), enabled);
    }
}

#[test]
fn plan_sections_point_at_the_config_sections() {
    let config = FallowConfig {
        duplicates: duplicates(false),
        audit: audit(Some(false), None, None),
        ..FallowConfig::default()
    };
    let sections = config.plan_sections();
    assert!(!sections.duplicates.enabled);
    assert_eq!(sections.audit.dead_code, Some(false));
}

mod proptests {
    use super::*;
    use proptest::prelude::*;

    fn arb_request() -> impl Strategy<Value = AnalysisRequest> {
        prop_oneof![Just(N), Just(D), Just(X)]
    }

    fn arb_command() -> impl Strategy<Value = RunCommand> {
        prop_oneof![Just(Combined), Just(Audit)]
    }

    fn arb_key() -> impl Strategy<Value = Option<bool>> {
        prop_oneof![Just(None), Just(Some(true)), Just(Some(false))]
    }

    fn arb_config() -> impl Strategy<Value = (bool, AuditConfig)> {
        (any::<bool>(), arb_key(), arb_key(), arb_key()).prop_map(
            |(enabled, dead_code, health, dupes)| (enabled, audit(dead_code, health, dupes)),
        )
    }

    proptest! {
        /// A request that does not read the config gives one plan under
        /// every config.
        #[test]
        fn a_request_that_reads_no_config_ignores_the_config(
            command in arb_command(),
            dead_code in arb_request(),
            duplication in arb_request(),
            health in arb_request(),
            first in arb_config(),
            second in arb_config(),
        ) {
            let request = RunRequest { command, dead_code, duplication, health };
            prop_assume!(!request.reads_config());
            prop_assert_eq!(
                plan(first.0, &first.1, request),
                plan(second.0, &second.1, request)
            );
        }
    }
}