Skip to content

Commit 53c3767

Browse files
committed
Introduce bogus ratchet check ensuring that no new Nix string files are introduced
1 parent 30686e7 commit 53c3767

File tree

18 files changed

+86
-10
lines changed

18 files changed

+86
-10
lines changed

src/files.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@ pub fn check_files(
1313
nixpkgs_path: &Path,
1414
nix_file_store: &mut NixFileStore,
1515
) -> validation::Result<BTreeMap<RelativePathBuf, ratchet::File>> {
16-
process_nix_files(nixpkgs_path, nix_file_store, |_nix_file| {
17-
// Noop for now, only boilerplate to make it easier to add future file-based checks
18-
Ok(Success(ratchet::File {}))
16+
process_nix_files(nixpkgs_path, nix_file_store, |nix_file| {
17+
// Bogus ratchet check towards enforcing that no files are strings
18+
let file_is_str = match nix_file.syntax_root.expr() {
19+
// This happens if the file can't be parsed, in which case we can't really decide
20+
// whether it's a string or not
21+
None => ratchet::RatchetState::NonApplicable,
22+
// The expression is a string, not allowed for new files and for existing files to be
23+
// changed to a string
24+
Some(Str(_)) => ratchet::RatchetState::Loose(
25+
npv_170::FileIsAString::new(
26+
RelativePathBuf::from_path(nix_file.path.strip_prefix(nixpkgs_path).unwrap())
27+
.unwrap(),
28+
)
29+
.into(),
30+
),
31+
// This is good
32+
Some(_) => ratchet::RatchetState::Tight,
33+
};
34+
Ok(Success(ratchet::File { file_is_str }))
1935
})
2036
}
2137

src/problem/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub mod npv_161;
3434
pub mod npv_162;
3535
pub mod npv_163;
3636

37+
pub mod npv_170;
38+
3739
#[derive(Clone, Display, EnumFrom)]
3840
pub enum Problem {
3941
/// NPV-100: attribute is not defined but it should be defined automatically
@@ -123,6 +125,8 @@ pub enum Problem {
123125
NewTopLevelPackageShouldBeByNameWithCustomArgument(
124126
npv_163::NewTopLevelPackageShouldBeByNameWithCustomArgument,
125127
),
128+
129+
FileIsAString(npv_170::FileIsAString),
126130
}
127131

128132
fn indent_definition(column: usize, definition: &str) -> String {

src/problem/npv_170.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::fmt;
2+
3+
use derive_new::new;
4+
use indoc::writedoc;
5+
use relative_path::RelativePathBuf;
6+
7+
#[derive(Clone, new)]
8+
pub struct FileIsAString {
9+
#[new(into)]
10+
file: RelativePathBuf,
11+
}
12+
13+
impl fmt::Display for FileIsAString {
14+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
let Self { file } = self;
16+
writedoc!(
17+
f,
18+
"
19+
- File {file} is a string, which is not allowed anymore
20+
",
21+
)
22+
}
23+
}

src/ratchet.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,19 @@ impl Package {
6262
}
6363
}
6464

65-
pub struct File {}
65+
pub struct File {
66+
pub file_is_str: RatchetState<FileIsStr>,
67+
}
6668

6769
impl File {
6870
/// Validates the ratchet checks for a top-level package
69-
pub fn compare(
70-
_name: &RelativePath,
71-
_optional_from: Option<&Self>,
72-
_to: &Self,
73-
) -> Validation<()> {
74-
Success(())
71+
pub fn compare(name: &RelativePath, optional_from: Option<&Self>, to: &Self) -> Validation<()> {
72+
// TODO: It's not great how RatchetState::compare requires a str, it should be more generic
73+
RatchetState::<FileIsStr>::compare(
74+
name.as_str(),
75+
optional_from.map(|x| &x.file_is_str),
76+
&to.file_is_str,
77+
)
7578
}
7679
}
7780

@@ -126,6 +129,16 @@ impl<Context: ToProblem> RatchetState<Context> {
126129
}
127130
}
128131

132+
pub enum FileIsStr {}
133+
134+
impl ToProblem for FileIsStr {
135+
type ToContext = Problem;
136+
137+
fn to_problem(_name: &str, _optional_from: Option<()>, to: &Self::ToContext) -> Problem {
138+
(*to).clone()
139+
}
140+
}
141+
129142
/// The ratchet to check whether a top-level attribute has/needs a manual definition, e.g. in
130143
/// `pkgs/top-level/all-packages.nix`.
131144
///
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import <test-nixpkgs> { root = ./.; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
not = "a string";
3+
}

tests/no-strings-changed/expected

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- File lib/minver.nix is a string, which is not allowed anymore
2+
3+
This PR introduces additional instances of discouraged patterns as listed above. Merging is discouraged but would not break the base branch.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import <test-nixpkgs> { root = ./.; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import <test-nixpkgs> { root = ./.; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"1.0"

tests/no-strings-maintained/expected

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Validated successfully
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import <test-nixpkgs> { root = ./.; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"1.0"

tests/no-strings-new/base/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import <test-nixpkgs> { root = ./.; }

tests/no-strings-new/expected

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- File lib/minver.nix is a string, which is not allowed anymore
2+
3+
This PR introduces additional instances of discouraged patterns as listed above. Merging is discouraged but would not break the base branch.

tests/no-strings-new/main/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import <test-nixpkgs> { root = ./.; }
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"1.0"

0 commit comments

Comments
 (0)