@@ -4,7 +4,6 @@ use std::path::Path;
44/// A simple wrapper around the `bundle` command.
55pub struct Bundler {
66 pub working_dir : String ,
7- envs : Vec < ( String , String ) > ,
87 command_executor : Box < dyn CommandExecutor > ,
98}
109
@@ -14,14 +13,9 @@ impl Bundler {
1413 /// # Arguments
1514 /// * `working_dir` - The working directory where `bundle` commands should be executed.
1615 /// * `command_executor` - An executor for `bundle` commands.
17- pub fn new (
18- working_dir : String ,
19- envs : Vec < ( String , String ) > ,
20- command_executor : Box < dyn CommandExecutor > ,
21- ) -> Self {
16+ pub fn new ( working_dir : String , command_executor : Box < dyn CommandExecutor > ) -> Self {
2217 Bundler {
2318 working_dir,
24- envs,
2519 command_executor,
2620 }
2721 }
@@ -33,31 +27,36 @@ impl Bundler {
3327 ///
3428 /// # Returns
3529 /// A `Result` containing the version string if successful, or an error message.
36- pub fn installed_gem_version ( & self , name : & str ) -> Result < String , String > {
37- let args = vec ! [ "--version" . into( ) , name. into( ) ] ;
38-
39- self . execute_bundle_command ( "info" . into ( ) , args)
30+ pub fn installed_gem_version (
31+ & self ,
32+ name : & str ,
33+ envs : & [ ( & str , & str ) ] ,
34+ ) -> Result < String , String > {
35+ let args = & [ "--version" , name] ;
36+
37+ self . execute_bundle_command ( "info" , args, envs)
4038 }
4139
42- fn execute_bundle_command ( & self , cmd : String , args : Vec < String > ) -> Result < String , String > {
40+ fn execute_bundle_command (
41+ & self ,
42+ cmd : & str ,
43+ args : & [ & str ] ,
44+ envs : & [ ( & str , & str ) ] ,
45+ ) -> Result < String , String > {
4346 let bundle_gemfile_path = Path :: new ( & self . working_dir ) . join ( "Gemfile" ) ;
4447 let bundle_gemfile = bundle_gemfile_path
4548 . to_str ( )
4649 . ok_or_else ( || "Invalid path to Gemfile" . to_string ( ) ) ?;
4750
48- let full_args: Vec < String > = std:: iter:: once ( cmd) . chain ( args) . collect ( ) ;
49- let command_envs: Vec < ( String , String ) > = self
50- . envs
51+ let full_args: Vec < & str > = std:: iter:: once ( cmd) . chain ( args. iter ( ) . copied ( ) ) . collect ( ) ;
52+ let command_envs: Vec < ( & str , & str ) > = envs
5153 . iter ( )
5254 . cloned ( )
53- . chain ( std:: iter:: once ( (
54- "BUNDLE_GEMFILE" . to_string ( ) ,
55- bundle_gemfile. to_string ( ) ,
56- ) ) )
55+ . chain ( std:: iter:: once ( ( "BUNDLE_GEMFILE" , bundle_gemfile) ) )
5756 . collect ( ) ;
5857
5958 self . command_executor
60- . execute ( "bundle" , full_args, command_envs)
59+ . execute ( "bundle" , & full_args, & command_envs)
6160 . and_then ( |output| match output. status {
6261 Some ( 0 ) => Ok ( String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ) ,
6362 Some ( status) => {
@@ -128,8 +127,8 @@ mod tests {
128127 fn execute (
129128 & self ,
130129 command_name : & str ,
131- args : Vec < String > ,
132- envs : Vec < ( String , String ) > ,
130+ args : & [ & str ] ,
131+ envs : & [ ( & str , & str ) ] ,
133132 ) -> Result < Output , String > {
134133 let mut config = self . config . borrow_mut ( ) ;
135134
@@ -140,6 +139,10 @@ mod tests {
140139 assert_eq ! ( & args, expected_args, "Mock: Args mismatch" ) ;
141140 }
142141 if let Some ( expected_envs) = & config. expected_envs {
142+ let envs: Vec < ( String , String ) > = envs
143+ . iter ( )
144+ . map ( |( k, v) | ( k. to_string ( ) , v. to_string ( ) ) )
145+ . collect ( ) ;
143146 assert_eq ! ( & envs, expected_envs, "Mock: Env mismatch" ) ;
144147 }
145148
@@ -172,9 +175,9 @@ mod tests {
172175 #[ test]
173176 fn test_installed_gem_version_success ( ) {
174177 let mock_executor = create_mock_executor_for_success ( "8.0.0" , "test_dir" , "rails" ) ;
175- let bundler = Bundler :: new ( "test_dir" . into ( ) , vec ! [ ] , Box :: new ( mock_executor) ) ;
178+ let bundler = Bundler :: new ( "test_dir" . into ( ) , Box :: new ( mock_executor) ) ;
176179 let version = bundler
177- . installed_gem_version ( "rails" )
180+ . installed_gem_version ( "rails" , & [ ] )
178181 . expect ( "Expected successful version" ) ;
179182 assert_eq ! ( version, "8.0.0" , "Installed gem version should match" ) ;
180183 }
@@ -197,8 +200,8 @@ mod tests {
197200 } ) ,
198201 ) ;
199202
200- let bundler = Bundler :: new ( "test_dir" . into ( ) , vec ! [ ] , Box :: new ( mock_executor) ) ;
201- let result = bundler. installed_gem_version ( gem_name) ;
203+ let bundler = Bundler :: new ( "test_dir" . into ( ) , Box :: new ( mock_executor) ) ;
204+ let result = bundler. installed_gem_version ( gem_name, & [ ] ) ;
202205
203206 assert ! (
204207 result. is_err( ) ,
@@ -229,8 +232,8 @@ mod tests {
229232 Err ( specific_error_msg. to_string ( ) ) ,
230233 ) ;
231234
232- let bundler = Bundler :: new ( "test_dir" . into ( ) , vec ! [ ] , Box :: new ( mock_executor) ) ;
233- let result = bundler. installed_gem_version ( gem_name) ;
235+ let bundler = Bundler :: new ( "test_dir" . into ( ) , Box :: new ( mock_executor) ) ;
236+ let result = bundler. installed_gem_version ( gem_name, & [ ] ) ;
234237
235238 assert ! ( result. is_err( ) , "Expected error from executor failure" ) ;
236239 assert_eq ! (
0 commit comments