lintian-brush 0.182.0

Automatic lintian issue fixer
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
use crate::declare_detector;
use crate::diagnostic::{Action, Deb822Action, Diagnostic, IndentPattern, ParagraphSelector};
use crate::licenses::{COMMON_LICENSES_DIR, FULL_LICENSE_NAME};
use crate::{FixerError, FixerPreferences, LintianIssue, Visibility};
use debian_copyright::License;
use debian_workspace::Workspace;
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

lazy_static! {
    static ref SPDX_RENAMES: HashMap<&'static str, &'static str> = {
        let mut m = HashMap::new();
        m.insert("BSD", "BSD-3-clause");
        m
    };
    static ref CANONICAL_NAMES: HashMap<&'static str, &'static str> = {
        let mut m = HashMap::new();
        m.insert("CC0", "CC0-1.0");
        m
    };
    static ref BLURBS: HashMap<&'static str, &'static str> = {
        let mut m = HashMap::new();
        m.insert(
            "CC0-1.0",
            "\
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.",
        );

        m.insert(
            "Apache-2.0",
            "\
Licensed under the Apache License, Version 2.0 (the \"License\");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an \"AS IS\" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.",
        );

        m.insert(
            "GPL-2+",
            "\
This package is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>",
        );

        m.insert(
            "GPL-3+",
            "\
This package is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>",
        );

        m
    };
    static ref WHITESPACE_RE: Regex = Regex::new(r"[\n\t ]+").unwrap();
    static ref CANONICAL_RE: Regex = Regex::new(r"^([A-Za-z0-9]+)(-[0-9\.]+)?(\+)?$").unwrap();
}

fn normalize_license_text(text: &str) -> String {
    WHITESPACE_RE.replace_all(text.trim(), " ").to_string()
}

fn load_common_license(name: &str) -> Option<String> {
    let path = Path::new(COMMON_LICENSES_DIR).join(name);
    fs::read_to_string(path)
        .ok()
        .map(|text| normalize_license_text(&text))
}

fn load_common_licenses() -> Vec<(String, String)> {
    let mut licenses = Vec::new();

    // Special handling for CC0-1.0
    if let Some(text) = load_common_license("CC0-1.0") {
        // Remove "Legal Code " from CC0 text
        let text = text.replace("Legal Code ", "");
        licenses.push(("CC0-1.0".to_string(), text));
    }

    // Load other common licenses
    if let Ok(entries) = fs::read_dir(COMMON_LICENSES_DIR) {
        for entry in entries.flatten() {
            if let Ok(name) = entry.file_name().into_string() {
                if name == "CC0-1.0" {
                    continue; // Already handled
                }
                if let Some(text) = load_common_license(&name) {
                    let spdx_name = SPDX_RENAMES
                        .get(name.as_str())
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| name.clone());
                    licenses.push((spdx_name, text));
                }
            }
        }
    }

    licenses
}

fn drop_debian_file_reference(text: &str) -> Option<String> {
    text.to_lowercase()
        .find("on debian systems, ")
        .map(|pos| text[..pos].trim().to_string())
}

fn debian_file_reference(name: &str, filename: &str) -> String {
    let text = format!(
        "On Debian systems, the full text of the {} can be found in the file `/usr/share/common-licenses/{}'.",
        name, filename
    );

    // Wrap to 78 characters
    textwrap::fill(&text, 78)
}

fn find_common_license_from_fulltext(text: &str) -> Option<String> {
    // Don't bother for anything that's short
    if text.lines().count() < 15 {
        return None;
    }

    let normalized = normalize_license_text(text);
    let normalized_without_ref =
        drop_debian_file_reference(&normalized).unwrap_or_else(|| normalized.clone());

    let common_licenses = load_common_licenses();
    for (shortname, fulltext) in &common_licenses {
        if fulltext == &normalized || fulltext == &normalized_without_ref {
            return Some(shortname.clone());
        }
    }

    None
}

fn find_common_license_from_blurb(text: &str) -> Option<String> {
    let normalized = normalize_license_text(text);
    let normalized_without_ref = drop_debian_file_reference(&normalized);

    for (name, blurb) in BLURBS.iter() {
        let normalized_blurb = normalize_license_text(blurb);
        if normalized == normalized_blurb {
            return Some(name.to_string());
        }
        if let Some(ref text_without_ref) = normalized_without_ref {
            if text_without_ref == &normalized_blurb {
                return Some(name.to_string());
            }
        }
    }

    None
}

fn canonical_license_id(license_id: &str) -> String {
    if let Some(caps) = CANONICAL_RE.captures(license_id) {
        let family = caps.get(1).unwrap().as_str();
        let mut version = caps
            .get(2)
            .map(|m| &m.as_str()[1..])
            .unwrap_or("1")
            .to_string();
        let plus = caps.get(3).map(|m| m.as_str()).unwrap_or("");

        // Remove trailing .0
        while version.ends_with(".0") {
            version = version[..version.len() - 2].to_string();
        }

        format!("{}-{}{}", family, version, plus)
    } else {
        tracing::warn!("Unable to get canonical name for {:?}", license_id);
        license_id.to_string()
    }
}

/// Compute the License field's new full value (synopsis + body) for a
/// rewrite. The DEP-5 License field is `<name>\n <body>` with blank
/// lines encoded as `.` (handled by `encode_field_text` upstream).
fn build_license_field(name: &str, body: &str) -> String {
    format!(
        "{}\n{}",
        name,
        debian_copyright::lossless::encode_field_text(body)
    )
}

/// What we're going to do to a single license paragraph.
struct LicensePlan {
    /// Original synopsis (used to address the paragraph).
    original_synopsis: String,
    /// New License field value (`new_synopsis\n<body>`).
    new_license_field: String,
}

/// Compute the rewrite plans for a copyright file along with the lintian
/// issues that motivate them. The detector emits one diagnostic per
/// (issue, paragraph-affected) pair, all carrying the same final SetField
/// for that paragraph - so override filtering is per-issue and the
/// applier deduplicates by value-equality.
pub fn detect(
    ws: &dyn Workspace,
    _preferences: &FixerPreferences,
) -> Result<Vec<Diagnostic>, FixerError> {
    let copyright_rel = PathBuf::from("debian/copyright");
    let copyright = match ws.parsed_copyright() {
        Ok(c) => c,
        Err(debian_workspace::Error::NotFound) => return Ok(Vec::new()),
        Err(e) => {
            tracing::debug!("debian/copyright is not machine-readable: {}", e);
            return Ok(Vec::new());
        }
    };

    // Per-license-paragraph plans, keyed by original synopsis.
    let mut plans: HashMap<String, LicensePlan> = HashMap::new();
    // Issues attributed to a license-paragraph rewrite (by original synopsis).
    let mut paragraph_issues: HashMap<String, Vec<LintianIssue>> = HashMap::new();
    // Renames recorded purely from SPDX_RENAMES (no body change).
    let mut renames: HashMap<String, String> = HashMap::new();
    // For description formatting.
    let mut updated: HashSet<String> = HashSet::new();

    for para in copyright.iter_licenses() {
        let Some(synopsis) = para.name() else {
            continue;
        };
        let Some(text) = para.text() else {
            continue;
        };
        if text.is_empty() {
            continue;
        }

        // 1. Try to replace full license text with blurb.
        if let Some(license_matched) = find_common_license_from_fulltext(&text) {
            let canonical_id = canonical_license_id(&synopsis);
            let found_blurb = BLURBS
                .iter()
                .find(|(shortname, _)| canonical_id == canonical_license_id(shortname))
                .map(|(_, blurb)| *blurb);

            if let Some(blurb) = found_blurb {
                let license_name = FULL_LICENSE_NAME
                    .get(license_matched.as_str())
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| license_matched.clone());
                let reference = debian_file_reference(&license_name, &license_matched);
                let text_with_reference = format!("{}\n\n{}", blurb, reference);

                let mut issues: Vec<LintianIssue> = Vec::new();
                if license_matched == "Apache-2.0" {
                    issues.push(LintianIssue::source_with_info(
                        "copyright-file-contains-full-apache-2-license",
                        Visibility::Error,
                        vec![license_matched.clone()],
                    ));
                }
                if license_matched.starts_with("GFDL-") {
                    issues.push(LintianIssue::source_with_info(
                        "copyright-file-contains-full-gfdl-license",
                        Visibility::Error,
                        vec![license_matched.clone()],
                    ));
                }
                if license_matched.starts_with("GPL-") {
                    issues.push(LintianIssue::source_with_info(
                        "copyright-file-contains-full-gpl-license",
                        Visibility::Error,
                        vec![license_matched.clone()],
                    ));
                }
                issues.push(LintianIssue::source_with_info(
                    "copyright-does-not-refer-to-common-license-file",
                    Visibility::Warning,
                    vec![license_matched.clone()],
                ));
                let specific_tag = if license_matched.starts_with("Apache-2") {
                    Some("copyright-not-using-common-license-for-apache2")
                } else if license_matched.starts_with("GPL-") {
                    Some("copyright-not-using-common-license-for-gpl")
                } else if license_matched.starts_with("LGPL-") {
                    Some("copyright-not-using-common-license-for-lgpl")
                } else if license_matched.starts_with("GFDL-") {
                    Some("copyright-not-using-common-license-for-gfdl")
                } else {
                    None
                };
                if let Some(tag) = specific_tag {
                    issues.push(LintianIssue::source_with_info(
                        tag,
                        Visibility::Error,
                        vec![license_matched.clone()],
                    ));
                }

                plans.insert(
                    synopsis.clone(),
                    LicensePlan {
                        original_synopsis: synopsis.clone(),
                        new_license_field: build_license_field(
                            &license_matched,
                            &text_with_reference,
                        ),
                    },
                );
                paragraph_issues
                    .entry(synopsis.clone())
                    .or_default()
                    .extend(issues);
                updated.insert(license_matched.clone());
                if synopsis != license_matched {
                    renames.insert(synopsis.clone(), license_matched.clone());
                }
                continue;
            } else {
                tracing::debug!(
                    "Found full license text for {}, but unknown synopsis {} ({})",
                    license_matched,
                    synopsis,
                    canonical_id
                );
            }
        }

        // Check for SPDX renames even when no full text match
        if let Some(&new_name) = SPDX_RENAMES.get(synopsis.as_str()) {
            renames.insert(synopsis.clone(), new_name.to_string());
        } else {
            let common_license_path = Path::new(COMMON_LICENSES_DIR).join(&synopsis);
            if common_license_path.exists() {
                tracing::debug!(
                    "A common license shortname ({}) is used, but license text not recognized.",
                    synopsis
                );
            }
        }

        // 2. No fulltext match - try adding a reference to a recognised blurb.
        if let Some(common_license) = find_common_license_from_blurb(&text) {
            if text.contains(COMMON_LICENSES_DIR) {
                continue;
            }
            if let Some(comment) = para.comment() {
                if comment.contains(COMMON_LICENSES_DIR) {
                    continue;
                }
            }
            if let Some(license_ref) = para.as_deb822().get("License-Reference") {
                if license_ref.contains(COMMON_LICENSES_DIR) {
                    continue;
                }
            }
            if let Some(x_comment) = para.as_deb822().get("X-Comment") {
                if x_comment.contains(COMMON_LICENSES_DIR) {
                    continue;
                }
            }

            let license_name = FULL_LICENSE_NAME
                .get(common_license.as_str())
                .map(|s| s.to_string())
                .unwrap_or_else(|| common_license.clone());
            let reference = debian_file_reference(&license_name, &common_license);
            let new_text = format!("{}\n\n{}", text, reference);
            let new_synopsis = if common_license == synopsis {
                synopsis.clone()
            } else {
                common_license.clone()
            };

            let mut issues: Vec<LintianIssue> = Vec::new();
            issues.push(LintianIssue::source_with_info(
                "copyright-does-not-refer-to-common-license-file",
                Visibility::Warning,
                vec![common_license.clone()],
            ));
            let specific_tag = if common_license.starts_with("Apache-2") {
                Some("copyright-not-using-common-license-for-apache2")
            } else if common_license.starts_with("GPL-") {
                Some("copyright-not-using-common-license-for-gpl")
            } else if common_license.starts_with("LGPL-") {
                Some("copyright-not-using-common-license-for-lgpl")
            } else if common_license.starts_with("GFDL-") {
                Some("copyright-not-using-common-license-for-gfdl")
            } else {
                None
            };
            if let Some(tag) = specific_tag {
                issues.push(LintianIssue::source_with_info(
                    tag,
                    Visibility::Error,
                    vec![common_license.clone()],
                ));
            }

            plans.insert(
                synopsis.clone(),
                LicensePlan {
                    original_synopsis: synopsis.clone(),
                    new_license_field: build_license_field(&new_synopsis, &new_text),
                },
            );
            paragraph_issues
                .entry(synopsis.clone())
                .or_default()
                .extend(issues);
            updated.insert(common_license.clone());
            if synopsis != common_license {
                renames.insert(synopsis.clone(), common_license);
            }
        }
    }

    // For licenses we plan to rename but with no body change yet (those
    // that came from SPDX_RENAMES alone), add a synopsis-only plan so the
    // License paragraph gets renamed.
    for (orig, new_name) in &renames {
        if plans.contains_key(orig) {
            continue;
        }
        // Look up the original License paragraph's body to preserve it.
        let Some(para) = copyright
            .iter_licenses()
            .find(|p| p.name().as_deref() == Some(orig.as_str()))
        else {
            continue;
        };
        let Some(text) = para.text() else { continue };
        plans.insert(
            orig.clone(),
            LicensePlan {
                original_synopsis: orig.clone(),
                new_license_field: build_license_field(new_name, &text),
            },
        );
        // No new lintian issues - this is just a downstream rename.
    }

    if plans.is_empty() {
        return Ok(Vec::new());
    }

    // Build the description that the framework will use.
    let renames_not_updated: HashSet<String> = renames
        .values()
        .filter(|v| !updated.contains(*v))
        .cloned()
        .collect();
    let description = build_description(&updated, &renames, &renames_not_updated);

    let mut diagnostics: Vec<Diagnostic> = Vec::new();

    // For each plan, emit one diagnostic per issue (each carrying the
    // SetField for the License paragraph). Plans without lintian issues
    // (downstream renames) get a single untagged diagnostic.
    for plan in plans.values() {
        let action = Action::Deb822(Deb822Action::SetFieldWithIndent {
            file: copyright_rel.clone(),
            paragraph: ParagraphSelector::CopyrightLicense {
                name: plan.original_synopsis.clone(),
            },
            field: "License".to_string(),
            value: plan.new_license_field.clone(),
            indent: IndentPattern::Fixed { spaces: 1 },
        });

        match paragraph_issues.get(&plan.original_synopsis) {
            Some(issues) if !issues.is_empty() => {
                for issue in issues {
                    let problem_description = describe_issue(issue);
                    diagnostics.push(Diagnostic::with_actions(
                        issue.clone(),
                        problem_description,
                        description.clone(),
                        vec![action.clone()],
                    ));
                }
            }
            _ => {
                diagnostics.push(Diagnostic::untagged(
                    format!(
                        "License paragraph {} should reference a common license file.",
                        plan.original_synopsis
                    ),
                    description.clone(),
                    vec![action],
                ));
            }
        }
    }

    // Now update Files paragraphs that reference renamed licenses.
    for para in copyright.iter_files() {
        let Some(license) = para.license() else {
            continue;
        };
        let (license_name, body): (String, Option<String>) = match license {
            License::Name(name) => (name, None),
            License::Named(name, body) => (name, Some(body)),
            License::Text(_) => continue,
        };
        let Some(new_name) = renames.get(&license_name) else {
            continue;
        };
        let Some(glob) = para.as_deb822().get("Files") else {
            continue;
        };
        let new_value = match &body {
            Some(b) if !b.is_empty() => build_license_field(new_name, b),
            _ => new_name.clone(),
        };
        let action = Action::Deb822(Deb822Action::SetFieldWithIndent {
            file: copyright_rel.clone(),
            paragraph: ParagraphSelector::CopyrightFiles { glob },
            field: "License".to_string(),
            value: new_value,
            indent: IndentPattern::Fixed { spaces: 1 },
        });
        diagnostics.push(Diagnostic::untagged(
            "License name in Files paragraph needs renaming.".to_string(),
            description.clone(),
            vec![action],
        ));
    }

    Ok(diagnostics)
}

fn describe_issue(issue: &LintianIssue) -> String {
    match issue.tag.as_deref() {
        Some("copyright-file-contains-full-apache-2-license") => {
            "debian/copyright contains full Apache-2 license text.".to_string()
        }
        Some("copyright-file-contains-full-gfdl-license") => {
            "debian/copyright contains full GFDL license text.".to_string()
        }
        Some("copyright-file-contains-full-gpl-license") => {
            "debian/copyright contains full GPL license text.".to_string()
        }
        Some("copyright-does-not-refer-to-common-license-file") => {
            "debian/copyright does not refer to common license file.".to_string()
        }
        Some("copyright-not-using-common-license-for-apache2") => {
            "debian/copyright does not use common license file for Apache-2.".to_string()
        }
        Some("copyright-not-using-common-license-for-gpl") => {
            "debian/copyright does not use common license file for GPL.".to_string()
        }
        Some("copyright-not-using-common-license-for-lgpl") => {
            "debian/copyright does not use common license file for LGPL.".to_string()
        }
        Some("copyright-not-using-common-license-for-gfdl") => {
            "debian/copyright does not use common license file for GFDL.".to_string()
        }
        _ => "License paragraph should refer to a common license file.".to_string(),
    }
}

fn build_description(
    updated: &HashSet<String>,
    renames: &HashMap<String, String>,
    renames_not_updated: &HashSet<String>,
) -> String {
    let mut done: Vec<String> = Vec::new();
    if !updated.is_empty() {
        let mut sorted: Vec<_> = updated.iter().cloned().collect();
        sorted.sort();
        done.push(format!(
            "refer to common license file for {}",
            sorted.join(", ")
        ));
    }
    if !renames_not_updated.is_empty() {
        let mut rename_strs: Vec<String> = renames
            .iter()
            .filter(|(_, new)| renames_not_updated.contains(*new))
            .map(|(old, new)| format!("{} (was: {})", new, old))
            .collect();
        rename_strs.sort();
        done.push(format!(
            "use common license names: {}",
            rename_strs.join(", ")
        ));
    }
    if done.is_empty() {
        return "Update copyright file.".to_string();
    }
    let joined = done.join("; ");
    let mut chars = joined.chars();
    match chars.next() {
        Some(c) => format!("{}{}.", c.to_uppercase(), chars.as_str()),
        None => "Update copyright file.".to_string(),
    }
}

declare_detector! {
    name: "common-license",
    tags: [
        "copyright-does-not-refer-to-common-license-file",
        "copyright-file-contains-full-apache-2-license",
        "copyright-file-contains-full-gfdl-license",
        "copyright-file-contains-full-gpl-license",
        "copyright-not-using-common-license-for-apache2",
        "copyright-not-using-common-license-for-gfdl",
        "copyright-not-using-common-license-for-gpl",
        "copyright-not-using-common-license-for-lgpl"
    ],
    triggers: [
        debian_workspace::Trigger::Deb822Field {
            file: "debian/copyright",
            paragraph_key: "License",
            field: "License",
        },
        debian_workspace::Trigger::Deb822Field {
            file: "debian/copyright",
            paragraph_key: "Files",
            field: "License",
        },
    ],
    detect: |ws, prefs| detect(ws, prefs),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::detector::Detector;
    use crate::{FixerPreferences, Version};
    use debian_copyright::lossless::Copyright;
    use std::path::Path;
    use tempfile::TempDir;

    fn run_apply(base: &Path) -> Result<crate::FixerResult, FixerError> {
        let v: Version = "1.0".parse().unwrap();
        let adapter = DetectorImpl;
        {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                base,
                Some("test".into()),
                Some(v.clone()),
            );
            adapter.apply(&ws, &FixerPreferences::default())
        }
    }

    #[test]
    fn test_no_copyright() {
        let temp_dir = TempDir::new().unwrap();
        assert!(matches!(
            run_apply(temp_dir.path()),
            Err(FixerError::NoChanges)
        ));
    }

    #[test]
    fn test_not_machine_readable() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();
        let debian_dir = base_path.join("debian");
        fs::create_dir(&debian_dir).unwrap();

        fs::write(
            debian_dir.join("copyright"),
            "This is not a machine-readable copyright file.\n",
        )
        .unwrap();

        assert!(matches!(run_apply(base_path), Err(FixerError::NoChanges)));
    }

    #[test]
    fn test_blurb_no_leading_spaces() {
        // Verify that blurbs don't have leading spaces
        let apache_blurb = BLURBS.get("Apache-2.0").unwrap();
        let first_line = apache_blurb.lines().next().unwrap();
        assert_eq!(
            first_line.chars().next().unwrap(),
            'L',
            "First line should start with 'L' not a space"
        );
        assert!(
            !first_line.starts_with(' '),
            "Blurb should not have leading spaces"
        );
    }

    #[test]
    fn test_set_license_encoding() {
        // Test that set_license() properly encodes text
        let input = r#"Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/

License: Test
 Old text here
"#;
        let (copyright, _) = Copyright::from_str_relaxed(input).unwrap();

        for mut para in copyright.iter_licenses() {
            let new_text = "Line one\nLine two\n\nLine after blank";
            para.set_license(&License::Named("Test".to_string(), new_text.to_string()));
        }

        let output = copyright.to_string();

        let expected = r#"Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/

License: Test
 Line one
 Line two
 .
 Line after blank
"#;
        assert_eq!(output, expected);
    }
}