diff --git a/src/docker_client.rs b/src/docker_client.rs index 5f61945..c7ebd11 100644 --- a/src/docker_client.rs +++ b/src/docker_client.rs @@ -65,3 +65,131 @@ impl DockerClient { command } } + +#[cfg(test)] +mod tests { + use super::*; + use std::{env::current_dir, ffi::OsStr}; + + #[test] + fn build_image() { + let command = DockerClient::build_image("3.2.3", "7.1.3", None, None); + + assert_eq!(command.get_program(), "docker"); + + let args: Vec<&OsStr> = command.get_args().collect(); + + assert_eq!( + args, + &[ + "build", + "--build-arg", + "RUBY_VERSION=3.2.3", + "--build-arg", + "RAILS_VERSION=7.1.3", + "-t", + "rails-new-3.2.3-7.1.3", + "-", + ] + ); + } + + #[test] + fn build_image_with_user_id() { + let command = DockerClient::build_image("3.2.3", "7.1.3", Some(1000), None); + + assert_eq!(command.get_program(), "docker"); + + let args: Vec<&OsStr> = command.get_args().collect(); + + assert_eq!( + args, + &[ + "build", + "--build-arg", + "RUBY_VERSION=3.2.3", + "--build-arg", + "RAILS_VERSION=7.1.3", + "--build-arg", + "USER_ID=1000", + "-t", + "rails-new-3.2.3-7.1.3", + "-", + ] + ); + } + + #[test] + fn build_image_with_group_id() { + let command = DockerClient::build_image("3.2.3", "7.1.3", None, Some(1000)); + + assert_eq!(command.get_program(), "docker"); + + let args: Vec<&OsStr> = command.get_args().collect(); + + assert_eq!( + args, + &[ + "build", + "--build-arg", + "RUBY_VERSION=3.2.3", + "--build-arg", + "RAILS_VERSION=7.1.3", + "--build-arg", + "GROUP_ID=1000", + "-t", + "rails-new-3.2.3-7.1.3", + "-", + ] + ); + } + + #[test] + fn run_image() { + let command = DockerClient::run_image("3.2.3", "7.1.3", vec!["my_app".to_string()]); + + assert_eq!(command.get_program(), "docker"); + + let binding = current_dir().unwrap(); + let current_dir = binding.to_str().unwrap(); + + let args: Vec<&OsStr> = command.get_args().collect(); + + assert_eq!( + args, + &[ + "run", + "--rm", + "-v", + &format!("{}:{}", current_dir, current_dir), + "-w", + current_dir, + "rails-new-3.2.3-7.1.3", + "rails", + "new", + "my_app", + ] + ); + } + + #[test] + fn get_help() { + let command = DockerClient::get_help("3.2.3", "7.1.3"); + + assert_eq!(command.get_program(), "docker"); + + let args: Vec<&OsStr> = command.get_args().collect(); + + assert_eq!( + args, + &[ + "run", + "--rm", + "rails-new-3.2.3-7.1.3", + "rails", + "new", + "--help", + ] + ); + } +} diff --git a/src/unix.rs b/src/unix.rs index e24e232..ffa173a 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,11 +1,11 @@ pub fn dockerfile_content() -> &'static [u8] { - include_bytes!("../Dockerfile") + include_bytes!("../Dockerfile") } pub fn get_user_id() -> Option { - Some(users::get_current_uid()) + Some(users::get_current_uid()) } pub fn get_group_id() -> Option { - Some(users::get_current_gid()) + Some(users::get_current_gid()) } diff --git a/src/windows.rs b/src/windows.rs index a9a3c49..a06c201 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,11 +1,11 @@ pub fn dockerfile_content() -> &'static [u8] { - include_bytes!("../Dockerfile.windows") + include_bytes!("../Dockerfile.windows") } pub fn get_user_id() -> Option { - None + None } pub fn get_group_id() -> Option { - None + None }