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
use crate::declare_detector;
use crate::diagnostic::{Action, ActionPlan, Deb822Action, Diagnostic, ParagraphSelector};
use crate::{FixerError, FixerPreferences, LintianIssue, Visibility};
use debian_workspace::Workspace;
use std::collections::HashSet;
use std::path::PathBuf;
use std::str::FromStr;

pub fn detect(
    ws: &dyn Workspace,
    preferences: &FixerPreferences,
) -> Result<Vec<Diagnostic>, FixerError> {
    let control_rel = PathBuf::from("debian/control");
    let editor = match ws.parsed_control() {
        Ok(c) => c,
        Err(debian_workspace::Error::NotFound) => return Ok(Vec::new()),
        Err(_) => return Ok(Vec::new()),
    };

    // Get the compat_release from preferences, defaulting to "sid"
    let compat_release = preferences.compat_release.as_deref().unwrap_or("sid");

    // Get the oldest dpkg version for the compat release
    let oldest_dpkg_version = debian_analyzer::release_info::dpkg_versions
        .get(compat_release)
        .cloned();

    let dpkg_1_22_13 = debversion::Version::from_str("1.22.13").unwrap();

    // Check if we're targeting dpkg >= 1.22.13
    let default_priority_is_optional = if let Some(ref dpkg_version) = oldest_dpkg_version {
        dpkg_version >= &dpkg_1_22_13
    } else {
        // For sid/unstable, assume the latest behavior
        true
    };

    // If source already has Priority, we might want to remove it if it's "optional" and dpkg >= 1.22.13
    if let Some(source) = editor.source() {
        if let Some(priority) = source.as_deb822().get("Priority") {
            if priority == "optional" && default_priority_is_optional {
                // Remove redundant Priority: optional from source stanza
                let issue = LintianIssue::source_with_info(
                    "redundant-field",
                    Visibility::Info,
                    vec!["debian/control Source Priority".to_string()],
                );
                let plans = vec![ActionPlan {
                    label: "Remove redundant Priority: optional from source stanza.".to_string(),
                    opinionated: false,
                    certainty: None,
                    actions: vec![Action::Deb822(Deb822Action::RemoveField {
                        file: control_rel.clone(),
                        paragraph: ParagraphSelector::Source,
                        field: "Priority".to_string(),
                    })],
                }];
                let diagnostic = Diagnostic {
                    issue: Some(issue),
                    message:
                        "Priority: optional in source stanza is redundant with dpkg >= 1.22.13 and can be removed."
                            .to_string(),
                    plans,
                    certainty: Some(crate::Certainty::Confident),
                    patch_name: None,
                };
                return Ok(vec![diagnostic]);
            }
            return Ok(Vec::new());
        }
    }

    let mut binary_priorities = HashSet::new();
    let mut missing_priorities = Vec::new();
    let mut any_explicit = false;

    // Collect binaries to process
    let binaries: Vec<_> = editor.binaries().collect();

    for binary in &binaries {
        let paragraph = binary.as_deb822();
        let package_name = paragraph.get("Package").unwrap_or_default().to_string();

        if let Some(priority) = paragraph.get("Priority") {
            binary_priorities.insert(priority.to_string());
            any_explicit = true;
        } else {
            missing_priorities.push(package_name);
            binary_priorities.insert("optional".to_string());
            // Since it's missing, it's not explicit yet, but we're implicitly adding "optional".
        }
    }

    let mut diagnostics = Vec::new();

    // If all binaries have the same priority, move it to source (only if it's not the default)
    if binary_priorities.len() == 1 {
        let common_priority = binary_priorities.iter().next().unwrap().clone();

        // If we added it implicitly to all missing ones, it's as if they were explicit
        // if we are going to write it to source anyway.
        // But wait! If none had it explicitly, we only write to source if we actually needed to add it (i.e. !default_priority_is_optional).
        if (any_explicit || !default_priority_is_optional)
            && (common_priority != "optional" || !default_priority_is_optional)
        {
            let mut actions = vec![Action::Deb822(Deb822Action::SetField {
                file: control_rel.clone(),
                paragraph: ParagraphSelector::Source,
                field: "Priority".to_string(),
                value: common_priority.clone(),
            })];

            for binary in &binaries {
                let package_name = binary
                    .as_deb822()
                    .get("Package")
                    .unwrap_or_default()
                    .to_string();
                if binary.as_deb822().get("Priority").is_some() {
                    actions.push(Action::Deb822(Deb822Action::RemoveField {
                        file: control_rel.clone(),
                        paragraph: ParagraphSelector::Binary {
                            package: package_name,
                        },
                        field: "Priority".to_string(),
                    }));
                }
            }

            let issue = if !missing_priorities.is_empty() {
                Some(LintianIssue::source_with_info(
                    "recommended-field",
                    Visibility::Warning,
                    vec![format!("debian/control Priority")],
                ))
            } else {
                None
            };

            let plans = vec![ActionPlan {
                label: "Set priority in source stanza, since it is the same for all packages."
                    .to_string(),
                opinionated: false,
                certainty: None,
                actions,
            }];

            diagnostics.push(Diagnostic {
                issue,
                message: "Set priority in source stanza, since it is the same for all packages."
                    .to_string(),
                plans,
                certainty: Some(crate::Certainty::Confident),
                patch_name: None,
            });
        }
    }

    if diagnostics.is_empty() && !default_priority_is_optional {
        // We couldn't move it to source, so we must add it to the binaries that are missing it
        for package_name in missing_priorities {
            let issue = LintianIssue::source_with_info(
                "recommended-field",
                Visibility::Warning,
                vec!["debian/control Priority".to_string()],
            );
            let plans = vec![ActionPlan {
                label: "Set priority to 'optional' for this binary package.".to_string(),
                opinionated: false,
                certainty: None,
                actions: vec![Action::Deb822(Deb822Action::SetField {
                    file: control_rel.clone(),
                    paragraph: ParagraphSelector::Binary {
                        package: package_name.clone(),
                    },
                    field: "Priority".to_string(),
                    value: "optional".to_string(),
                })],
            }];
            diagnostics.push(Diagnostic {
                issue: Some(issue),
                message: format!(
                    "Binary package '{}' is missing a Priority field.",
                    package_name
                ),
                plans,
                certainty: None,
                patch_name: None,
            });
        }
    }

    Ok(diagnostics)
}

declare_detector! {
    name: "no-priority-field",
    tags: ["recommended-field"],
    triggers: [
        debian_workspace::Trigger::Deb822Field {
            file: "debian/control",
            paragraph_key: "Source",
            field: "Priority",
        },
        debian_workspace::Trigger::Deb822Field {
            file: "debian/control",
            paragraph_key: "Package",
            field: "Package",
        },
        debian_workspace::Trigger::Deb822Field {
            file: "debian/control",
            paragraph_key: "Package",
            field: "Priority",
        },
    ],
    detect: |ws, prefs| detect(ws, prefs),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::detector::Detector;
    use std::fs;
    use tempfile::TempDir;

    fn make_fixer() -> DetectorImpl {
        DetectorImpl
    }

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

        let control_content = "Source: foo\n\nPackage: blah\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("bullseye".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        assert!(result.is_ok());

        let updated_content = fs::read_to_string(&control_path).unwrap();
        assert_eq!(
            updated_content,
            "Source: foo\nPriority: optional\n\nPackage: blah\n"
        );
    }

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

        let control_content = "Source: foo\n\nPackage: blah\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("trixie".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        assert!(matches!(result, Err(FixerError::NoChanges)));

        let updated_content = fs::read_to_string(&control_path).unwrap();
        assert_eq!(updated_content, "Source: foo\n\nPackage: blah\n");
    }

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

        let control_content =
            "Source: foo\n\nPackage: foo\nPriority: optional\n\nPackage: foo-doc\nPriority: optional\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("bullseye".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        assert!(result.is_ok());

        let updated_content = fs::read_to_string(&control_path).unwrap();
        assert_eq!(
            updated_content,
            "Source: foo\nPriority: optional\n\nPackage: foo\n\nPackage: foo-doc\n"
        );
    }

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

        let control_content =
            "Source: foo\n\nPackage: foo\nPriority: optional\n\nPackage: foo-doc\nPriority: optional\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("trixie".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        // With dpkg >= 1.22.13, Priority: optional in binaries doesn't need to be moved to source
        assert!(matches!(result, Err(FixerError::NoChanges)));

        let updated_content = fs::read_to_string(&control_path).unwrap();
        // The Priority fields should remain unchanged
        assert_eq!(updated_content, "Source: foo\n\nPackage: foo\nPriority: optional\n\nPackage: foo-doc\nPriority: optional\n");
    }

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

        let control_content = "Source: foo\nPriority: optional\n\nPackage: foo\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("trixie".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        assert!(result.is_ok());

        let updated_content = fs::read_to_string(&control_path).unwrap();
        assert_eq!(updated_content, "Source: foo\n\nPackage: foo\n");
    }

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

        let control_content = "Source: foo\nPriority: optional\n\nPackage: foo\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("bullseye".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        assert!(matches!(result, Err(FixerError::NoChanges)));
    }

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

        let control_content = "Source: foo\nPriority: important\n\nPackage: foo\n";
        let control_path = debian_dir.join("control");
        fs::write(&control_path, control_content).unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let mut preferences = crate::FixerPreferences::default();
        preferences.compat_release = Some("trixie".to_string());
        let result = {
            let ws = debian_workspace::fs_workspace::FsWorkspace::new(
                temp_dir.path(),
                Some("foo".into()),
                Some(version.clone()),
            );
            fixer.apply(&ws, &preferences)
        };
        // Priority: important should not be removed even with new dpkg
        assert!(matches!(result, Err(FixerError::NoChanges)));

        let updated_content = fs::read_to_string(&control_path).unwrap();
        assert_eq!(
            updated_content,
            "Source: foo\nPriority: important\n\nPackage: foo\n"
        );
    }

    #[test]
    fn test_no_change_when_no_file() {
        let temp_dir = TempDir::new().unwrap();

        let fixer = make_fixer();
        let version: crate::Version = "1.0".parse().unwrap();
        let ws = debian_workspace::fs_workspace::FsWorkspace::new(
            temp_dir.path(),
            Some("test-package".into()),
            Some(version.clone()),
        );
        let result = fixer.apply(&ws, &Default::default());
        assert!(matches!(result, Err(FixerError::NoChanges)));
    }
}