11use crate :: utils:: {
22 install_package, list_files, release, remove_package, replace_file_with_symlink, restore_file,
3+ Worker ,
34} ;
45use anyhow:: Result ;
5- use std:: path:: PathBuf ;
6- use tracing:: { debug, info, warn} ;
6+ use std:: { path:: PathBuf , sync:: Arc } ;
7+ use tracing:: { info, warn} ;
8+
9+ use super :: Experiment ;
710
811const PACKAGE : & str = "rust-coreutils" ;
912const FIRST_SUPPORTED_RELEASE : & str = "24.04" ;
1013
11- pub struct RustCoreutils { }
14+ pub struct RustCoreutils {
15+ system : Arc < dyn Worker > ,
16+ }
1217
1318impl RustCoreutils {
14- pub fn install ( ) -> Result < ( ) > {
19+ fn installed ( ) -> bool {
20+ list_files ( "/usr/lib/cargo/bin/coreutils" ) . is_ok ( )
21+ }
22+
23+ fn compatible ( ) -> bool {
24+ match release ( ) {
25+ Ok ( codename) => codename. as_str ( ) >= FIRST_SUPPORTED_RELEASE ,
26+ Err ( _) => false ,
27+ }
28+ }
29+ }
30+
31+ impl Experiment for RustCoreutils {
32+ fn new ( system : Arc < dyn Worker > ) -> Self {
33+ Self { system }
34+ }
35+
36+ fn install ( & self ) -> Result < ( ) > {
1537 if !Self :: compatible ( ) {
1638 warn ! ( "Skipping '{PACKAGE}'. Minimum supported release is {FIRST_SUPPORTED_RELEASE}." ) ;
1739 return Ok ( ( ) ) ;
1840 }
1941
2042 info ! ( "Installing and configuring {}" , PACKAGE ) ;
2143
22- install_package ( PACKAGE ) ?;
44+ install_package ( self . system . clone ( ) , PACKAGE ) ?;
2345
24- let files = list_files ( "/usr/lib/cargo/bin/coreutils" ) ?;
46+ let files = self . system . list_files ( "/usr/lib/cargo/bin/coreutils" ) ?;
2547
2648 for f in files {
2749 let filename = f. file_name ( ) . unwrap ( ) ;
2850 let existing = PathBuf :: from ( "/usr/bin" ) . join ( filename) ;
2951 replace_file_with_symlink ( PathBuf :: from ( "/usr/bin/coreutils" ) , existing. clone ( ) ) ?;
30-
31- debug ! (
32- "Replaced {} with symlink to {}" ,
33- existing. display( ) ,
34- f. display( )
35- ) ;
3652 }
3753 Ok ( ( ) )
3854 }
3955
40- pub fn restore ( ) -> Result < ( ) > {
56+ fn restore ( & self ) -> Result < ( ) > {
4157 if !Self :: installed ( ) || !Self :: compatible ( ) {
4258 warn ! ( "{PACKAGE} not found, skipping restore" ) ;
4359 return Ok ( ( ) ) ;
@@ -53,19 +69,42 @@ impl RustCoreutils {
5369 restore_file ( existing) ?;
5470 }
5571
56- remove_package ( PACKAGE ) ?;
72+ remove_package ( self . system . clone ( ) , PACKAGE ) ?;
5773
5874 Ok ( ( ) )
5975 }
76+ }
6077
61- fn installed ( ) -> bool {
62- list_files ( "/usr/lib/cargo/bin/coreutils" ) . is_ok ( )
63- }
64-
65- fn compatible ( ) -> bool {
66- match release ( ) {
67- Ok ( codename) => codename. as_str ( ) >= FIRST_SUPPORTED_RELEASE ,
68- Err ( _) => false ,
78+ #[ cfg( test) ]
79+ mod tests {
80+ use super :: * ;
81+ use crate :: utils:: { tests:: MockSystem , Command } ;
82+
83+ #[ test]
84+ fn test_coreutils_install ( ) -> Result < ( ) > {
85+ let runner = Arc :: new ( MockSystem :: new ( ) ) ;
86+ let coreutils = RustCoreutils :: new ( runner. clone ( ) ) ;
87+
88+ let mock_files = & [
89+ "/usr/lib/cargo/bin/coreutils/date" ,
90+ "/usr/lib/cargo/bin/coreutils/sort" ,
91+ "/usr/lib/cargo/bin/diffutils/diff" ,
92+ "/usr/lib/share/foo" ,
93+ ] ;
94+
95+ for file in mock_files {
96+ runner. mock_file ( PathBuf :: from ( file) , "" ) ;
6997 }
98+
99+ // assert!(coreutils.install().is_ok());
100+ let cmd = Command :: build ( "lsb_release" , & [ "-cs" ] ) ;
101+ runner. run ( & cmd) ?;
102+
103+ assert_eq ! (
104+ runner. commands. clone( ) . into_inner( ) ,
105+ vec!( "lsb_release -cs" )
106+ ) ;
107+
108+ Ok ( ( ) )
70109 }
71110}
0 commit comments