@@ -109,7 +109,11 @@ const LINTS: &[Lint] = &[
109
109
/// if it exists we need to check that it links to /run if not error
110
110
/// if it does not exist error.
111
111
#[ context( "Linting" ) ]
112
- pub ( crate ) fn lint ( root : & Dir , fatal_warnings : bool ) -> Result < ( ) > {
112
+ pub ( crate ) fn lint (
113
+ root : & Dir ,
114
+ fatal_warnings : bool ,
115
+ mut output : impl std:: io:: Write ,
116
+ ) -> Result < ( ) > {
113
117
let mut fatal = 0usize ;
114
118
let mut warnings = 0usize ;
115
119
let mut passed = 0usize ;
@@ -123,11 +127,11 @@ pub(crate) fn lint(root: &Dir, fatal_warnings: bool) -> Result<()> {
123
127
if let Err ( e) = r {
124
128
match lint. ty {
125
129
LintType :: Fatal => {
126
- eprintln ! ( "Failed lint: {name}: {e}" ) ;
130
+ writeln ! ( output , "Failed lint: {name}: {e}" ) ? ;
127
131
fatal += 1 ;
128
132
}
129
133
LintType :: Warning => {
130
- eprintln ! ( "Lint warning: {name}: {e}" ) ;
134
+ writeln ! ( output , "Lint warning: {name}: {e}" ) ? ;
131
135
warnings += 1 ;
132
136
}
133
137
}
@@ -137,14 +141,14 @@ pub(crate) fn lint(root: &Dir, fatal_warnings: bool) -> Result<()> {
137
141
passed += 1 ;
138
142
}
139
143
}
140
- println ! ( "Checks passed: {passed}" ) ;
144
+ writeln ! ( output , "Checks passed: {passed}" ) ? ;
141
145
let fatal = if fatal_warnings {
142
146
fatal + warnings
143
147
} else {
144
148
fatal
145
149
} ;
146
150
if warnings > 0 {
147
- println ! ( "Warnings: {warnings}" ) ;
151
+ writeln ! ( output , "Warnings: {warnings}" ) ? ;
148
152
}
149
153
if fatal > 0 {
150
154
anyhow:: bail!( "Checks failed: {fatal}" )
@@ -258,11 +262,15 @@ fn check_baseimage_root_norecurse(dir: &Dir) -> LintResult {
258
262
259
263
/// Check ostree-related base image content.
260
264
fn check_baseimage_root ( dir : & Dir ) -> LintResult {
261
- check_baseimage_root_norecurse ( dir) ??;
265
+ if let Err ( e) = check_baseimage_root_norecurse ( dir) ? {
266
+ return Ok ( Err ( e) ) ;
267
+ }
262
268
// If we have our own documentation with the expected root contents
263
269
// embedded, then check that too! Mostly just because recursion is fun.
264
270
if let Some ( dir) = dir. open_dir_optional ( BASEIMAGE_REF ) ? {
265
- check_baseimage_root_norecurse ( & dir) ??;
271
+ if let Err ( e) = check_baseimage_root_norecurse ( & dir) ? {
272
+ return Ok ( Err ( e) ) ;
273
+ }
266
274
}
267
275
lint_ok ( )
268
276
}
@@ -317,6 +325,23 @@ mod tests {
317
325
Ok ( tempdir)
318
326
}
319
327
328
+ fn passing_fixture ( ) -> Result < cap_std_ext:: cap_tempfile:: TempDir > {
329
+ let root = cap_std_ext:: cap_tempfile:: tempdir ( cap_std:: ambient_authority ( ) ) ?;
330
+ root. create_dir_all ( "usr/lib/modules/5.7.2" ) ?;
331
+ root. write ( "usr/lib/modules/5.7.2/vmlinuz" , "vmlinuz" ) ?;
332
+
333
+ root. create_dir ( "sysroot" ) ?;
334
+ root. symlink_contents ( "sysroot/ostree" , "ostree" ) ?;
335
+
336
+ const PREPAREROOT_PATH : & str = "usr/lib/ostree/prepare-root.conf" ;
337
+ const PREPAREROOT : & str =
338
+ include_str ! ( "../../baseimage/base/usr/lib/ostree/prepare-root.conf" ) ;
339
+ root. create_dir_all ( Utf8Path :: new ( PREPAREROOT_PATH ) . parent ( ) . unwrap ( ) ) ?;
340
+ root. atomic_write ( PREPAREROOT_PATH , PREPAREROOT ) ?;
341
+
342
+ Ok ( root)
343
+ }
344
+
320
345
#[ test]
321
346
fn test_var_run ( ) -> Result < ( ) > {
322
347
let root = & fixture ( ) ?;
@@ -330,6 +355,17 @@ mod tests {
330
355
Ok ( ( ) )
331
356
}
332
357
358
+ #[ test]
359
+ fn test_lint_main ( ) -> Result < ( ) > {
360
+ let root = & passing_fixture ( ) ?;
361
+ let mut out = Vec :: new ( ) ;
362
+ lint ( root, true , & mut out) . unwrap ( ) ;
363
+ root. create_dir_all ( "var/run/foo" ) ?;
364
+ let mut out = Vec :: new ( ) ;
365
+ assert ! ( lint( root, true , & mut out) . is_err( ) ) ;
366
+ Ok ( ( ) )
367
+ }
368
+
333
369
#[ test]
334
370
fn test_kernel_lint ( ) -> Result < ( ) > {
335
371
let root = & fixture ( ) ?;
@@ -476,33 +512,13 @@ mod tests {
476
512
477
513
#[ test]
478
514
fn test_baseimage_root ( ) -> Result < ( ) > {
479
- use bootc_utils:: CommandRunExt ;
480
- use cap_std_ext:: cmdext:: CapStdExtCommandExt ;
481
- use std:: path:: Path ;
482
-
483
515
let td = fixture ( ) ?;
484
516
485
517
// An empty root should fail our test
486
- assert ! ( check_baseimage_root( & td) . is_err( ) ) ;
518
+ assert ! ( check_baseimage_root( & td) . unwrap ( ) . is_err( ) ) ;
487
519
488
- // Copy our reference base image content from the source dir
489
- let Some ( manifest) = std:: env:: var_os ( "CARGO_MANIFEST_PATH" ) else {
490
- // This was only added in relatively recent cargo
491
- return Ok ( ( ) ) ;
492
- } ;
493
- let srcdir = Path :: new ( & manifest)
494
- . parent ( )
495
- . unwrap ( )
496
- . join ( "../baseimage/base" ) ;
497
- for ent in std:: fs:: read_dir ( srcdir) ? {
498
- let ent = ent?;
499
- std:: process:: Command :: new ( "cp" )
500
- . cwd_dir ( td. try_clone ( ) ?)
501
- . arg ( "-pr" )
502
- . arg ( ent. path ( ) )
503
- . arg ( "." )
504
- . run ( ) ?;
505
- }
520
+ drop ( td) ;
521
+ let td = passing_fixture ( ) ?;
506
522
check_baseimage_root ( & td) . unwrap ( ) . unwrap ( ) ;
507
523
Ok ( ( ) )
508
524
}
0 commit comments