Skip to content

Commit 3c34723

Browse files
authored
Merge pull request #1107 from cgwalters/lint-more5
lints: More tests && add --skip
2 parents 0fc7830 + 18cc57a commit 3c34723

File tree

2 files changed

+92
-19
lines changed

2 files changed

+92
-19
lines changed

lib/src/cli.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ pub(crate) enum ContainerOpts {
257257
/// maintaining this exact format; do not parse it via code or scripts.
258258
#[clap(long)]
259259
list: bool,
260+
261+
/// Skip checking the targeted lints, by name. Use `--list` to discover the set
262+
/// of available lints.
263+
///
264+
/// Example: --skip nonempty-boot --skip baseimage-root
265+
#[clap(long)]
266+
skip: Vec<String>,
260267
},
261268
}
262269

@@ -1044,6 +1051,7 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
10441051
rootfs,
10451052
fatal_warnings,
10461053
list,
1054+
skip,
10471055
} => {
10481056
if list {
10491057
return lints::lint_list(std::io::stdout().lock());
@@ -1060,7 +1068,8 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
10601068
};
10611069

10621070
let root = &Dir::open_ambient_dir(rootfs, cap_std::ambient_authority())?;
1063-
lints::lint(root, warnings, root_type, std::io::stdout().lock())?;
1071+
let skip = skip.iter().map(|s| s.as_str());
1072+
lints::lint(root, warnings, root_type, skip, std::io::stdout().lock())?;
10641073
Ok(())
10651074
}
10661075
},

lib/src/lints.rs

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,37 @@ pub(crate) fn lint_list(output: impl std::io::Write) -> Result<()> {
132132
Ok(())
133133
}
134134

135-
/// check for the existence of the /var/run directory
136-
/// if it exists we need to check that it links to /run if not error
137-
/// if it does not exist error.
138-
#[context("Linting")]
139-
pub(crate) fn lint(
135+
#[derive(Debug)]
136+
struct LintExecutionResult {
137+
warnings: usize,
138+
passed: usize,
139+
skipped: usize,
140+
fatal: usize,
141+
}
142+
143+
fn lint_inner<'skip>(
140144
root: &Dir,
141-
warning_disposition: WarningDisposition,
142145
root_type: RootType,
146+
skip: impl IntoIterator<Item = &'skip str>,
143147
mut output: impl std::io::Write,
144-
) -> Result<()> {
148+
) -> Result<LintExecutionResult> {
145149
let mut fatal = 0usize;
146150
let mut warnings = 0usize;
147151
let mut passed = 0usize;
148152
let mut skipped = 0usize;
153+
let skip: std::collections::HashSet<_> = skip.into_iter().collect();
149154
for lint in LINTS {
150155
let name = lint.name;
151156

157+
if skip.contains(name) {
158+
skipped += 1;
159+
continue;
160+
}
161+
152162
if let Some(lint_root_type) = lint.root_type {
153163
if lint_root_type != root_type {
154164
skipped += 1;
165+
continue;
155166
}
156167
}
157168

@@ -177,20 +188,41 @@ pub(crate) fn lint(
177188
passed += 1;
178189
}
179190
}
180-
writeln!(output, "Checks passed: {passed}")?;
181-
if skipped > 0 {
182-
writeln!(output, "Checks skipped: {skipped}")?;
191+
192+
Ok(LintExecutionResult {
193+
passed,
194+
skipped,
195+
warnings,
196+
fatal,
197+
})
198+
}
199+
200+
/// check for the existence of the /var/run directory
201+
/// if it exists we need to check that it links to /run if not error
202+
/// if it does not exist error.
203+
#[context("Linting")]
204+
pub(crate) fn lint<'skip>(
205+
root: &Dir,
206+
warning_disposition: WarningDisposition,
207+
root_type: RootType,
208+
skip: impl IntoIterator<Item = &'skip str>,
209+
mut output: impl std::io::Write,
210+
) -> Result<()> {
211+
let r = lint_inner(root, root_type, skip, &mut output)?;
212+
writeln!(output, "Checks passed: {}", r.passed)?;
213+
if r.skipped > 0 {
214+
writeln!(output, "Checks skipped: {}", r.skipped)?;
183215
}
184216
let fatal = if matches!(warning_disposition, WarningDisposition::FatalWarnings) {
185-
fatal + warnings
217+
r.fatal + r.warnings
186218
} else {
187-
fatal
219+
r.fatal
188220
};
189-
if warnings > 0 {
190-
writeln!(output, "Warnings: {warnings}")?;
221+
if r.warnings > 0 {
222+
writeln!(output, "Warnings: {}", r.warnings)?;
191223
}
192224
if fatal > 0 {
193-
anyhow::bail!("Checks failed: {fatal}")
225+
anyhow::bail!("Checks failed: {}", fatal)
194226
}
195227
Ok(())
196228
}
@@ -509,11 +541,43 @@ mod tests {
509541
let root = &passing_fixture()?;
510542
let mut out = Vec::new();
511543
let warnings = WarningDisposition::FatalWarnings;
512-
let root_type = RootType::Running;
513-
lint(root, warnings, root_type, &mut out).unwrap();
544+
let root_type = RootType::Alternative;
545+
lint(root, warnings, root_type, [], &mut out).unwrap();
514546
root.create_dir_all("var/run/foo")?;
515547
let mut out = Vec::new();
516-
assert!(lint(root, warnings, root_type, &mut out).is_err());
548+
assert!(lint(root, warnings, root_type, [], &mut out).is_err());
549+
Ok(())
550+
}
551+
552+
#[test]
553+
fn test_lint_inner() -> Result<()> {
554+
let root = &passing_fixture()?;
555+
556+
// Verify that all lints run
557+
let mut out = Vec::new();
558+
let root_type = RootType::Alternative;
559+
let r = lint_inner(root, root_type, [], &mut out).unwrap();
560+
assert_eq!(r.passed, LINTS.len());
561+
assert_eq!(r.fatal, 0);
562+
assert_eq!(r.skipped, 0);
563+
assert_eq!(r.warnings, 0);
564+
565+
let r = lint_inner(root, root_type, ["var-log"], &mut out).unwrap();
566+
// Trigger a failure in var-log
567+
root.create_dir_all("var/log/dnf")?;
568+
root.write("var/log/dnf/dnf.log", b"dummy dnf log")?;
569+
assert_eq!(r.passed, LINTS.len().checked_sub(1).unwrap());
570+
assert_eq!(r.fatal, 0);
571+
assert_eq!(r.skipped, 1);
572+
assert_eq!(r.warnings, 0);
573+
574+
// But verify that not skipping it results in a warning
575+
let mut out = Vec::new();
576+
let r = lint_inner(root, root_type, [], &mut out).unwrap();
577+
assert_eq!(r.passed, LINTS.len().checked_sub(1).unwrap());
578+
assert_eq!(r.fatal, 0);
579+
assert_eq!(r.skipped, 0);
580+
assert_eq!(r.warnings, 1);
517581
Ok(())
518582
}
519583

0 commit comments

Comments
 (0)