diff options
Diffstat (limited to 'crates/shirabe/tests')
| -rw-r--r-- | crates/shirabe/tests/common/process_executor_mock.rs | 33 | ||||
| -rw-r--r-- | crates/shirabe/tests/util/git_test.rs | 52 | ||||
| -rw-r--r-- | crates/shirabe/tests/util/perforce_test.rs | 22 |
3 files changed, 46 insertions, 61 deletions
diff --git a/crates/shirabe/tests/common/process_executor_mock.rs b/crates/shirabe/tests/common/process_executor_mock.rs index 33d9dfba..eb39b9c6 100644 --- a/crates/shirabe/tests/common/process_executor_mock.rs +++ b/crates/shirabe/tests/common/process_executor_mock.rs @@ -1,7 +1,6 @@ //! ref: composer/tests/Composer/Test/Mock/ProcessExecutorMock.php -use shirabe::util::process_executor::{MockExpectation, MockHandler, ProcessExecutor}; -use shirabe_php_shim::PhpMixed; +use shirabe::util::process_executor::{CommandLine, MockExpectation, MockHandler, ProcessExecutor}; // A command expectation as written in the PHP tests: either a bare command // (`'git command'` / `['git', '--version']`) or the full @@ -29,44 +28,36 @@ pub fn cmd_full( // string args. Comparison against the executed command is exact (PHP `===`), so // the form here must match the form the code under test passes to `execute`. pub trait IntoMockCmd { - fn into_mock_cmd(self) -> PhpMixed; + fn into_mock_cmd(self) -> CommandLine; } impl IntoMockCmd for &str { - fn into_mock_cmd(self) -> PhpMixed { - PhpMixed::String(self.to_string()) + fn into_mock_cmd(self) -> CommandLine { + CommandLine::Shell(self.to_string()) } } impl IntoMockCmd for String { - fn into_mock_cmd(self) -> PhpMixed { - PhpMixed::String(self) + fn into_mock_cmd(self) -> CommandLine { + CommandLine::Shell(self) } } impl IntoMockCmd for Vec<&str> { - fn into_mock_cmd(self) -> PhpMixed { - PhpMixed::List( - self.into_iter() - .map(|s| PhpMixed::String(s.to_string())) - .collect(), - ) + fn into_mock_cmd(self) -> CommandLine { + CommandLine::Args(self.into_iter().map(|s| s.to_string()).collect()) } } impl IntoMockCmd for Vec<String> { - fn into_mock_cmd(self) -> PhpMixed { - PhpMixed::List(self.into_iter().map(PhpMixed::String).collect()) + fn into_mock_cmd(self) -> CommandLine { + CommandLine::Args(self) } } impl<const N: usize> IntoMockCmd for [&str; N] { - fn into_mock_cmd(self) -> PhpMixed { - PhpMixed::List( - self.iter() - .map(|s| PhpMixed::String(s.to_string())) - .collect(), - ) + fn into_mock_cmd(self) -> CommandLine { + CommandLine::Args(self.iter().map(|s| s.to_string()).collect()) } } diff --git a/crates/shirabe/tests/util/git_test.rs b/crates/shirabe/tests/util/git_test.rs index dbc06137..3d447553 100644 --- a/crates/shirabe/tests/util/git_test.rs +++ b/crates/shirabe/tests/util/git_test.rs @@ -10,7 +10,7 @@ use shirabe::io::IOInterface; use shirabe::util::filesystem::Filesystem; use shirabe::util::git::Git; use shirabe::util::http_downloader::HttpDownloaderMockHandler; -use shirabe::util::process_executor::{MockExpectation, MockHandler, ProcessExecutor}; +use shirabe::util::process_executor::{CommandLine, MockExpectation, MockHandler, ProcessExecutor}; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{PhpMixed, RuntimeException}; @@ -118,15 +118,15 @@ fn mock_sync_mirror_config() -> Config { #[test] fn test_run_command_public_git_hub_repository_not_initial_clone_ssh() { let expected_url = "git@github.com:acme/repo"; - let command_callable: Box<dyn Fn(&str) -> Vec<String>> = Box::new(move |url: &str| { + let command_callable: Box<dyn Fn(&str) -> CommandLine> = Box::new(move |url: &str| { assert_eq!(expected_url, url); - vec!["git command".to_string()] + CommandLine::Shell("git command".to_string()) }); let config = mock_config("ssh"); let (process, _guard) = - get_process_executor_mock(vec![cmd(vec!["git command"])], true, MockHandler::default()); + get_process_executor_mock(vec![cmd("git command")], true, MockHandler::default()); let mut git = build_git(IOStub::new(), config, process); @@ -144,15 +144,15 @@ fn test_run_command_public_git_hub_repository_not_initial_clone_ssh() { #[test] fn test_run_command_public_git_hub_repository_not_initial_clone_https() { let expected_url = "https://github.com/acme/repo"; - let command_callable: Box<dyn Fn(&str) -> Vec<String>> = Box::new(move |url: &str| { + let command_callable: Box<dyn Fn(&str) -> CommandLine> = Box::new(move |url: &str| { assert_eq!(expected_url, url); - vec!["git command".to_string()] + CommandLine::Shell("git command".to_string()) }); let config = mock_config("https"); let (process, _guard) = - get_process_executor_mock(vec![cmd(vec!["git command"])], true, MockHandler::default()); + get_process_executor_mock(vec![cmd("git command")], true, MockHandler::default()); let mut git = build_git(IOStub::new(), config, process); @@ -169,16 +169,16 @@ fn test_run_command_public_git_hub_repository_not_initial_clone_https() { #[test] fn test_run_command_private_git_hub_repository_not_initial_clone_not_interactive_without_authentication() { - let command_callable: Box<dyn Fn(&str) -> Vec<String>> = Box::new(|url: &str| { + let command_callable: Box<dyn Fn(&str) -> CommandLine> = Box::new(|url: &str| { assert_eq!("https://github.com/acme/repo", url); - vec!["git command".to_string()] + CommandLine::Shell("git command".to_string()) }); let config = mock_config("https"); let (process, _guard) = get_process_executor_mock( vec![ - cmd_full(vec!["git command"], 1, "", ""), + cmd_full("git command", 1, "", ""), cmd_full(vec!["git", "--version"], 0, "", ""), ], true, @@ -208,20 +208,20 @@ fn run_command_private_github_with_authentication( expected_failures_before_success: usize, ) { let expected_url_owned = expected_url.to_string(); - let command_callable: Box<dyn Fn(&str) -> Vec<String>> = Box::new(move |url: &str| { + let command_callable: Box<dyn Fn(&str) -> CommandLine> = Box::new(move |url: &str| { if url != expected_url_owned { - return vec!["git command failing".to_string()]; + return CommandLine::Shell("git command failing".to_string()); } - vec!["git command ok".to_string()] + CommandLine::Shell("git command ok".to_string()) }); let config = mock_config(protocol); let mut expected_calls: Vec<MockExpectation> = Vec::new(); for _ in 0..expected_failures_before_success { - expected_calls.push(cmd_full(vec!["git command failing"], 1, "", "")); + expected_calls.push(cmd_full("git command failing", 1, "", "")); } - expected_calls.push(cmd_full(vec!["git command ok"], 0, "", "")); + expected_calls.push(cmd_full("git command ok", 0, "", "")); let (process, _guard) = get_process_executor_mock(expected_calls, true, MockHandler::default()); @@ -273,11 +273,11 @@ fn run_command_private_bitbucket_with_authentication( bitbucket_git_auth_calls: usize, ) { let expected_url_owned = expected_url.to_string(); - let command_callable: Box<dyn Fn(&str) -> Vec<String>> = Box::new(move |url: &str| { + let command_callable: Box<dyn Fn(&str) -> CommandLine> = Box::new(move |url: &str| { if url != expected_url_owned { - return vec!["git command failing".to_string()]; + return CommandLine::Shell("git command failing".to_string()); } - vec!["git command ok".to_string()] + CommandLine::Shell("git command ok".to_string()) }); let config = ConfigStubBuilder::new() @@ -293,7 +293,7 @@ fn run_command_private_bitbucket_with_authentication( let mut expected_calls: Vec<MockExpectation> = Vec::new(); for _ in 0..expected_failures_before_success { - expected_calls.push(cmd_full(vec!["git command failing"], 1, "", "")); + expected_calls.push(cmd_full("git command failing", 1, "", "")); } if bitbucket_git_auth_calls > 0 { for _ in 0..bitbucket_git_auth_calls { @@ -305,7 +305,7 @@ fn run_command_private_bitbucket_with_authentication( )); } } - expected_calls.push(cmd_full(vec!["git command ok"], 0, "", "")); + expected_calls.push(cmd_full("git command ok", 0, "", "")); let (process, _guard) = get_process_executor_mock(expected_calls, true, MockHandler::default()); @@ -416,11 +416,11 @@ fn run_command_private_bitbucket_interactive_with_oauth( initial_config: Option<(&str, &str)>, ) { let expected_url_owned = expected_url.to_string(); - let command_callable: Box<dyn Fn(&str) -> Vec<String>> = Box::new(move |url: &str| { + let command_callable: Box<dyn Fn(&str) -> CommandLine> = Box::new(move |url: &str| { if url != expected_url_owned { - return vec!["git command failing".to_string()]; + return CommandLine::Shell("git command failing".to_string()); } - vec!["git command ok".to_string()] + CommandLine::Shell("git command ok".to_string()) }); let mut config = ConfigStubBuilder::new() @@ -437,9 +437,9 @@ fn run_command_private_bitbucket_interactive_with_oauth( config.set_auth_config_source(Box::new(NullConfigSource)); let mut expected_calls: Vec<MockExpectation> = Vec::new(); - expected_calls.push(cmd_full(vec!["git command failing"], 1, "", "")); + expected_calls.push(cmd_full("git command failing", 1, "", "")); if initial_config.is_some() { - expected_calls.push(cmd_full(vec!["git command failing"], 1, "", "")); + expected_calls.push(cmd_full("git command failing", 1, "", "")); } else { expected_calls.push(cmd_full( vec!["git", "config", "bitbucket.accesstoken"], @@ -448,7 +448,7 @@ fn run_command_private_bitbucket_interactive_with_oauth( "", )); } - expected_calls.push(cmd_full(vec!["git command ok"], 0, "", "")); + expected_calls.push(cmd_full("git command ok", 0, "", "")); let (process, _guard) = get_process_executor_mock(expected_calls, true, MockHandler::default()); diff --git a/crates/shirabe/tests/util/perforce_test.rs b/crates/shirabe/tests/util/perforce_test.rs index 293e56d5..dfeae457 100644 --- a/crates/shirabe/tests/util/perforce_test.rs +++ b/crates/shirabe/tests/util/perforce_test.rs @@ -192,7 +192,7 @@ fn test_query_p4_user_with_user_already_set() { fn test_query_p4_user_with_user_set_in_p4_variables_with_windows_os() { let (process, _guard) = get_process_executor_mock( vec![cmd_full( - vec!["p4 set"], + "p4 set", 0, format!("P4USER=TEST_P4VARIABLE_USER{}", shirabe_php_shim::PHP_EOL), "", @@ -216,7 +216,7 @@ fn test_query_p4_user_with_user_set_in_p4_variables_with_windows_os() { fn test_query_p4_user_with_user_set_in_p4_variables_not_windows_os() { let (process, _guard) = get_process_executor_mock( vec![cmd_full( - vec!["echo $P4USER"], + "echo $P4USER", 0, format!("TEST_P4VARIABLE_USER{}", shirabe_php_shim::PHP_EOL), "", @@ -259,7 +259,7 @@ fn test_query_p4_user_stores_response_to_query_for_user_with_windows() { ProcessExecutor::escape("TEST_QUERY_USER") ); let (process, _guard) = get_process_executor_mock( - vec![cmd(vec!["p4 set"]), cmd(vec![expected_command.as_str()])], + vec![cmd("p4 set"), cmd(expected_command.as_str())], true, MockHandler::default(), ); @@ -280,10 +280,7 @@ fn test_query_p4_user_stores_response_to_query_for_user_without_windows() { ProcessExecutor::escape("TEST_QUERY_USER") ); let (process, _guard) = get_process_executor_mock( - vec![ - cmd(vec!["echo $P4USER"]), - cmd(vec![expected_command.as_str()]), - ], + vec![cmd("echo $P4USER"), cmd(expected_command.as_str())], true, MockHandler::default(), ); @@ -304,7 +301,7 @@ fn test_query_p4_user_escapes_injection_on_windows() { ProcessExecutor::escape("foo && calc.exe") ); let (process, _guard) = get_process_executor_mock( - vec![cmd(vec!["p4 set"]), cmd(vec![expected_command.as_str()])], + vec![cmd("p4 set"), cmd(expected_command.as_str())], true, MockHandler::default(), ); @@ -322,10 +319,7 @@ fn test_query_p4_user_escapes_injection_on_windows() { fn test_query_p4_user_escapes_injection_on_unix() { let expected_command = format!("export P4USER={}", ProcessExecutor::escape("foo; id")); let (process, _guard) = get_process_executor_mock( - vec![ - cmd(vec!["echo $P4USER"]), - cmd(vec![expected_command.as_str()]), - ], + vec![cmd("echo $P4USER"), cmd(expected_command.as_str())], true, MockHandler::default(), ); @@ -368,7 +362,7 @@ fn test_query_p4_password_with_password_already_set() { fn test_query_p4_password_with_password_set_in_p4_variables_with_windows_os() { let (process, _guard) = get_process_executor_mock( vec![cmd_full( - vec!["p4 set"], + "p4 set", 0, format!( "P4PASSWD=TEST_P4VARIABLE_PASSWORD{}", @@ -391,7 +385,7 @@ fn test_query_p4_password_with_password_set_in_p4_variables_with_windows_os() { fn test_query_p4_password_with_password_set_in_p4_variables_not_windows_os() { let (process, _guard) = get_process_executor_mock( vec![cmd_full( - vec!["echo $P4PASSWD"], + "echo $P4PASSWD", 0, format!("TEST_P4VARIABLE_PASSWORD{}", shirabe_php_shim::PHP_EOL), "", |
