licenses 0.6.0

Cargo subcommand for collecting licenses.
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
use crate::GlobalArgs;
use crate::file_io::FileIO;
use crate::licenses::status::LicenseStatus;
use anyhow::Context;
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, PartialEq, Eq, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
    pub global: GlobalArgs,
    #[serde(rename = "crates")]
    pub crate_configs: HashMap<String, CrateConfig>,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct CrateConfig {
    pub skip: Vec<String>,
    pub allow: Option<LicenseStatus>,
    pub include: Vec<IncludedLicense>,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Clone)]
#[serde(untagged)]
pub enum IncludedLicense {
    Text { name: String, text: String },
}

impl GlobalArgs {
    fn merge(&mut self, other: Self) {
        self.dev |= other.dev;
        self.build |= other.build;
        self.all_features |= other.all_features;
        self.no_default_features |= other.no_default_features;
        if other.depth.is_some() {
            self.depth = other.depth;
        }
        self.feature.extend(other.feature);
        self.exclude.extend(other.exclude);
        self.ignore.extend(other.ignore);
    }
}

pub fn load_config(file_io: &impl FileIO, mut global_args: GlobalArgs) -> anyhow::Result<Config> {
    if let Some(path) = global_args.config.take() {
        let mut config = parse_config(&file_io.read_file(&path)?)?;
        config.crate_configs = normalised_crate_names(config.crate_configs);
        config.global.merge(global_args);
        Ok(config)
    } else {
        Ok(Config {
            global: global_args,
            crate_configs: HashMap::new(),
        })
    }
}

fn parse_config(contents: &str) -> anyhow::Result<Config> {
    toml::from_str(contents).context("failed to parse config")
}

fn normalised_crate_names(crates: HashMap<String, CrateConfig>) -> HashMap<String, CrateConfig> {
    crates
        .into_iter()
        .map(|(crate_name, config)| (crate_name.replace('-', "_"), config))
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::GlobalArgs;
    use crate::config::{Config, CrateConfig, IncludedLicense, load_config, parse_config};
    use crate::file_io::FileIOSpy;
    use crate::licenses::status::LicenseStatus;
    use std::collections::HashMap;
    use std::path::PathBuf;

    #[test]
    fn empty_config_is_valid() {
        assert_eq!(config_with_crates([]), parse_config("").unwrap());
    }

    #[test]
    fn config_with_invalid_heading_is_invalid() {
        let contents = r"[invalid]";
        assert!(parse_config(contents).is_err());
    }

    #[test]
    fn config_with_valid_heading_but_no_skipped_files_is_valid() {
        for contents in [
            r"[crates.anyhow]",
            r"[global]
        [crates.anyhow]",
        ] {
            assert_eq!(
                config_with_crates([("anyhow", crate_config(&[], &[], None))]),
                parse_config(contents).unwrap()
            );
        }
    }

    #[test]
    fn config_with_valid_heading_and_allowed_warning_is_valid() {
        let contents = r#"
        [crates]
        anyhow = { allow = "too few" }"#;
        assert_eq!(
            config_with_crates([(
                "anyhow",
                crate_config(&[], &[], Some(LicenseStatus::TooFew))
            )]),
            parse_config(contents).unwrap()
        );
    }

    #[test]
    fn config_with_invalid_key_pair_is_invalid() {
        for contents in [
            r#"[crates.anyhow]
            lemon = "cheese""#,
            r#"[global] 
            config = "not allowed""#,
        ] {
            assert!(parse_config(contents).is_err());
        }
    }

    #[test]
    fn config_with_valid_heading_and_single_skipped_file_is_valid() {
        for contents in [
            r#"[crates.anyhow]
            skip = ["COPYING"]"#,
            r#"[crates]
            anyhow = { skip = ["COPYING"]}"#,
        ] {
            assert_eq!(
                config_with_crates([("anyhow", crate_config(&["COPYING"], &[], None))]),
                parse_config(contents).unwrap()
            );
        }
    }

    #[test]
    fn config_with_valid_heading_and_included_text_license_is_valid() {
        for contents in [
            r#"[crates.anyhow]
            include = [{ name = "LICENSE", text = "some license text" }]"#,
            r#"[crates]
            anyhow = { include = [{ name = "LICENSE", text = "some license text" }]}"#,
        ] {
            assert_eq!(
                config_with_crates([(
                    "anyhow",
                    crate_config(
                        &[],
                        &[IncludedLicense::Text {
                            name: "LICENSE".to_string(),
                            text: "some license text".to_string()
                        }],
                        None
                    )
                )]),
                parse_config(contents).unwrap()
            );
        }
    }

    #[test]
    fn config_with_multiple_valid_headings_and_multiple_skipped_files() {
        let contents = r#"
        [crates.anyhow]
        skip = ["COPYING"]
        [crates.another]
        skip = ["LICENSE-WRONG","COPYRIGHT"]"#;
        assert_eq!(
            config_with_crates([
                ("anyhow", crate_config(&["COPYING"], &[], None)),
                (
                    "another",
                    crate_config(&["LICENSE-WRONG", "COPYRIGHT"], &[], None)
                )
            ]),
            parse_config(contents).unwrap()
        );
    }

    #[test]
    fn config_with_comments_are_valid() {
        let contents = r#"
        [crates.anyhow]
        skip = ["COPYING"] # a comment"#;
        assert_eq!(
            config_with_crates([("anyhow", crate_config(&["COPYING"], &[], None))]),
            parse_config(contents).unwrap()
        );
    }

    #[test]
    fn config_with_duplicate_headings_are_invalid() {
        let contents = r#"
        [crates.anyhow]
        skip = ["COPYING"]
        [crates.anyhow]
        skip = ["LICENSE-WRONG","COPYRIGHT"]"#;
        assert!(parse_config(contents).is_err());
    }

    #[test]
    fn config_supports_all_global_args() {
        let contents = r#"
        [global]
        dev = true # a comment
        build = false
        depth = 1
        all-features = true
        no-default-features = true
        feature = ["feature"]
        exclude = ["test"]
        ignore = ["crate1","crate2"]"#;
        assert_eq!(
            Config {
                global: GlobalArgs {
                    dev: true,
                    build: false,
                    depth: Some(1),
                    all_features: true,
                    no_default_features: true,
                    feature: vec!["feature".to_string()],
                    exclude: vec!["test".to_string()],
                    ignore: vec!["crate1".to_string(), "crate2".to_string()],
                    config: None,
                },
                crate_configs: HashMap::new(),
            },
            parse_config(contents).unwrap()
        );
    }

    #[test]
    fn global_args_merges_correctly() {
        let mut global_args_1 = GlobalArgs {
            dev: true,
            build: false,
            depth: Some(10),
            all_features: true,
            no_default_features: false,
            feature: vec!["feature1".to_string()],
            exclude: vec!["test".to_string()],
            ignore: vec![],
            config: None,
        };
        let global_args_2 = GlobalArgs {
            dev: false,
            build: true,
            depth: Some(20),
            all_features: false,
            no_default_features: true,
            feature: vec!["feature2".to_string()],
            exclude: vec![],
            ignore: vec!["lemon".to_string()],
            config: None,
        };
        global_args_1.merge(global_args_2);
        assert_eq!(
            GlobalArgs {
                dev: true,
                build: true,
                depth: Some(20),
                all_features: true,
                no_default_features: true,
                feature: vec!["feature1".to_string(), "feature2".to_string()],
                exclude: vec!["test".to_string()],
                ignore: vec!["lemon".to_string()],
                config: None,
            },
            global_args_1
        );
    }

    #[test]
    fn errors_if_config_path_set_and_read_file_fails() {
        let file_io_spy = FileIOSpy::default();
        file_io_spy
            .read_file
            .returns
            .set([Err(anyhow::anyhow!("deliberate test error"))]);

        let global_args = GlobalArgs {
            config: Some(PathBuf::from("path")),
            ..Default::default()
        };

        assert_eq!(
            "deliberate test error",
            load_config(&file_io_spy, global_args)
                .unwrap_err()
                .to_string()
        );
    }

    #[test]
    fn never_uses_file_io_if_config_path_not_set() {
        let file_io_spy = FileIOSpy::default();

        assert!(load_config(&file_io_spy, GlobalArgs::default()).is_ok());

        assert!(file_io_spy.read_file.arguments.take().is_empty());
    }

    #[test]
    fn crates_in_config_are_normalised() {
        let file_io_spy = FileIOSpy::default();

        let contents = r"
        [crates.normalise-me]";

        file_io_spy
            .read_file
            .returns
            .set([Ok(contents.to_string())]);

        assert_eq!(
            config_with_crates([("normalise_me", crate_config(&[], &[], None))]),
            load_config(
                &file_io_spy,
                GlobalArgs {
                    config: Some(PathBuf::from("path")),
                    ..Default::default()
                }
            )
            .unwrap()
        );
    }

    fn crate_config(
        skipped: &[&str],
        included: &[IncludedLicense],
        allow: Option<LicenseStatus>,
    ) -> CrateConfig {
        CrateConfig {
            skip: skipped.iter().map(ToString::to_string).collect(),
            allow,
            include: included.to_vec(),
        }
    }

    fn config_with_crates<I>(crates: I) -> Config
    where
        I: IntoIterator<Item = (&'static str, CrateConfig)>,
    {
        Config {
            global: GlobalArgs::default(),
            crate_configs: crates
                .into_iter()
                .map(|(k, v)| (k.to_string(), v))
                .collect(),
        }
    }

    #[test]
    fn config_with_all_allowed_statuses() {
        for (input, expected) in [
            (r#"allow = "empty""#, LicenseStatus::Empty),
            (r#"allow = "none declared""#, LicenseStatus::NoneDeclared),
            (r#"allow = "too few""#, LicenseStatus::TooFew),
        ] {
            let contents = format!("[crates.test]\n{input}");
            let config = parse_config(&contents).unwrap();
            assert_eq!(
                Some(&expected),
                config.crate_configs.get("test").unwrap().allow.as_ref()
            );
        }
    }

    #[test]
    fn merge_preserves_first_depth_when_second_is_none() {
        let mut args1 = GlobalArgs {
            depth: Some(5),
            ..Default::default()
        };
        let args2 = GlobalArgs {
            depth: None,
            ..Default::default()
        };
        args1.merge(args2);
        // When second has None depth, first keeps its value
        // Looking at merge: `if other.depth.is_some() { self.depth = other.depth; }`
        assert_eq!(Some(5), args1.depth);
    }

    #[test]
    fn merge_overwrites_depth_when_second_has_value() {
        let mut args1 = GlobalArgs {
            depth: Some(5),
            ..Default::default()
        };
        let args2 = GlobalArgs {
            depth: Some(10),
            ..Default::default()
        };
        args1.merge(args2);
        assert_eq!(Some(10), args1.depth);
    }

    #[test]
    fn merge_combines_features() {
        let mut args1 = GlobalArgs {
            feature: vec!["f1".to_string()],
            ..Default::default()
        };
        let args2 = GlobalArgs {
            feature: vec!["f2".to_string()],
            ..Default::default()
        };
        args1.merge(args2);
        assert_eq!(vec!["f1".to_string(), "f2".to_string()], args1.feature);
    }

    #[test]
    fn merge_booleans_are_or() {
        let mut args1 = GlobalArgs {
            dev: false,
            build: true,
            ..Default::default()
        };
        let args2 = GlobalArgs {
            dev: true,
            build: false,
            ..Default::default()
        };
        args1.merge(args2);
        assert!(args1.dev);
        assert!(args1.build);
    }

    #[test]
    fn config_with_multiple_included_licenses() {
        let contents = r#"
        [crates.example]
        include = [
            { name = "LICENSE-A", text = "text a" },
            { name = "LICENSE-B", text = "text b" }
        ]"#;
        let config = parse_config(contents).unwrap();
        assert_eq!(2, config.crate_configs["example"].include.len());
    }
}