about summary refs log tree commit diff
path: root/nixpkgs/pkgs/test/nixpkgs-check-by-name/src/ratchet.rs
blob: c12f1ead25402d098c6619bca1273f993ab1bc71 (plain) (blame)
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
//! This module implements the ratchet checks, see ../README.md#ratchet-checks
//!
//! Each type has a `compare` method that validates the ratchet checks for that item.

use crate::nixpkgs_problem::NixpkgsProblem;
use crate::structure;
use crate::validation::{self, Validation, Validation::Success};
use std::collections::HashMap;

/// The ratchet value for the entirety of Nixpkgs.
#[derive(Default)]
pub struct Nixpkgs {
    /// The ratchet values for each package in `pkgs/by-name`
    pub packages: HashMap<String, Package>,
}

impl Nixpkgs {
    /// Validates the ratchet checks for Nixpkgs
    pub fn compare(optional_from: Option<Self>, to: Self) -> Validation<()> {
        validation::sequence_(
            // We only loop over the current attributes,
            // we don't need to check ones that were removed
            to.packages.into_iter().map(|(name, attr_to)| {
                let attr_from = if let Some(from) = &optional_from {
                    from.packages.get(&name)
                } else {
                    // This pretends that if there's no base version to compare against, all
                    // attributes existed without conforming to the new strictness check for
                    // backwards compatibility.
                    // TODO: Remove this case. This is only needed because the `--base`
                    // argument is still optional, which doesn't need to be once CI is updated
                    // to pass it.
                    Some(&Package {
                        empty_non_auto_called: EmptyNonAutoCalled::Invalid,
                    })
                };
                Package::compare(&name, attr_from, &attr_to)
            }),
        )
    }
}

/// The ratchet value for a single package in `pkgs/by-name`
pub struct Package {
    /// The ratchet value for the check for non-auto-called empty arguments
    pub empty_non_auto_called: EmptyNonAutoCalled,
}

impl Package {
    /// Validates the ratchet checks for a single package defined in `pkgs/by-name`
    pub fn compare(name: &str, optional_from: Option<&Self>, to: &Self) -> Validation<()> {
        EmptyNonAutoCalled::compare(
            name,
            optional_from.map(|x| &x.empty_non_auto_called),
            &to.empty_non_auto_called,
        )
    }
}

/// The ratchet value of a single package in `pkgs/by-name`
/// for the non-auto-called empty argument check of a single.
///
/// This checks that packages defined in `pkgs/by-name` cannot be overridden
/// with an empty second argument like `callPackage ... { }`.
#[derive(PartialEq, PartialOrd)]
pub enum EmptyNonAutoCalled {
    Invalid,
    Valid,
}

impl EmptyNonAutoCalled {
    /// Validates the non-auto-called empty argument ratchet check for a single package defined in `pkgs/by-name`
    fn compare(name: &str, optional_from: Option<&Self>, to: &Self) -> Validation<()> {
        let from = optional_from.unwrap_or(&Self::Valid);
        if to >= from {
            Success(())
        } else {
            NixpkgsProblem::WrongCallPackage {
                relative_package_file: structure::relative_file_for_package(name),
                package_name: name.to_owned(),
            }
            .into()
        }
    }
}