diff options
Diffstat (limited to 'crates/shirabe/tests/repository/vcs/git_driver_test.rs')
| -rw-r--r-- | crates/shirabe/tests/repository/vcs/git_driver_test.rs | 234 |
1 files changed, 212 insertions, 22 deletions
diff --git a/crates/shirabe/tests/repository/vcs/git_driver_test.rs b/crates/shirabe/tests/repository/vcs/git_driver_test.rs index 56c2755..7c30b21 100644 --- a/crates/shirabe/tests/repository/vcs/git_driver_test.rs +++ b/crates/shirabe/tests/repository/vcs/git_driver_test.rs @@ -1,16 +1,24 @@ //! ref: composer/tests/Composer/Test/Repository/Vcs/GitDriverTest.php -// Every case constructs a GitDriver with a mocked ProcessExecutor (and an HttpDownloader -// that reaches curl_multi_init, todo!()) to feed git command output; mocking is not -// available here. +use std::cell::RefCell; +use std::rc::Rc; use indexmap::IndexMap; +use serial_test::serial; use shirabe::config::Config; +use shirabe::io::IOInterface; +use shirabe::repository::vcs::GitDriver; use shirabe::util::filesystem::Filesystem; +use shirabe::util::http_downloader::HttpDownloaderMockHandler; use shirabe::util::platform::Platform; -use shirabe_php_shim::PhpMixed; +use shirabe::util::process_executor::MockHandler; +use shirabe_php_shim::{PhpMixed, RuntimeException}; use tempfile::TempDir; +use crate::http_downloader_mock::{HttpDownloaderMockGuard, get_http_downloader_mock}; +use crate::io_stub::IOStub; +use crate::process_executor_mock::{ProcessExecutorMockGuard, cmd_full, get_process_executor_mock}; + struct SetUp { home: TempDir, config: Config, @@ -64,73 +72,255 @@ impl Drop for TearDown { } #[test] -#[ignore = "requires ProcessExecutor mock (getProcessExecutorMock/expects) and Reflection setRepoDir, neither available"] +#[serial] fn test_get_root_identifier_from_remote_local_repository() { let SetUp { home, - config: _, + config, network_env, } = set_up(); + let home_path = home.path().to_string_lossy().into_owned(); let _tear_down = TearDown::new(home.path().to_path_buf(), network_env); - todo!() + + let config = Rc::new(RefCell::new(config)); + let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(IOStub::new())); + + let (http_downloader, _http_guard): (_, HttpDownloaderMockGuard) = + get_http_downloader_mock(vec![], false, HttpDownloaderMockHandler::default()); + + let stdout = "* main\n 2.2\n 1.10"; + + let (process, _process_guard): (_, ProcessExecutorMockGuard) = get_process_executor_mock( + vec![cmd_full(["git", "branch", "--no-color"], 0, stdout, "")], + true, + MockHandler::default(), + ); + + let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new(); + repo_config.insert("url".to_string(), PhpMixed::String(home_path.clone())); + + let mut driver = GitDriver::new(repo_config, io, config, http_downloader, process); + driver.__set_repo_dir(home_path); + + assert_eq!("main", driver.get_root_identifier().unwrap()); } #[test] -#[ignore = "requires ProcessExecutor mock (getProcessExecutorMock/expects), IO mock and Reflection setRepoDir, none available"] +#[serial] fn test_get_root_identifier_from_remote() { let SetUp { home, - config: _, + config, network_env, } = set_up(); + let home_path = home.path().to_string_lossy().into_owned(); let _tear_down = TearDown::new(home.path().to_path_buf(), network_env); - todo!() + + let config = Rc::new(RefCell::new(config)); + let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(IOStub::new())); + + let (http_downloader, _http_guard): (_, HttpDownloaderMockGuard) = + get_http_downloader_mock(vec![], false, HttpDownloaderMockHandler::default()); + + let stdout = "* remote origin\n Fetch URL: https://example.org/acme.git\n Push URL: https://example.org/acme.git\n HEAD branch: main\n Remote branches:\n 1.10 tracked\n 2.2 tracked\n main tracked"; + + let (process, _process_guard): (_, ProcessExecutorMockGuard) = get_process_executor_mock( + vec![ + cmd_full(["git", "remote", "-v"], 0, "", ""), + cmd_full( + [ + "git", + "remote", + "set-url", + "origin", + "--", + "https://example.org/acme.git", + ], + 0, + "", + "", + ), + cmd_full(["git", "remote", "show", "origin"], 0, stdout, ""), + cmd_full( + [ + "git", + "remote", + "set-url", + "origin", + "--", + "https://example.org/acme.git", + ], + 0, + "", + "", + ), + ], + false, + MockHandler::default(), + ); + + let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new(); + repo_config.insert( + "url".to_string(), + PhpMixed::String("https://example.org/acme.git".to_string()), + ); + + let mut driver = GitDriver::new(repo_config, io, config, http_downloader, process); + driver.__set_repo_dir(home_path); + + assert_eq!("main", driver.get_root_identifier().unwrap()); } #[test] -#[ignore = "requires ProcessExecutor mock (getProcessExecutorMock/expects) and Reflection setRepoDir, neither available"] +#[serial] fn test_get_root_identifier_from_local_with_network_disabled() { let SetUp { home, - config: _, + config, network_env, } = set_up(); + let home_path = home.path().to_string_lossy().into_owned(); let _tear_down = TearDown::new(home.path().to_path_buf(), network_env); - todo!() + + Platform::put_env("COMPOSER_DISABLE_NETWORK", "1"); + + let config = Rc::new(RefCell::new(config)); + let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(IOStub::new())); + + let (http_downloader, _http_guard): (_, HttpDownloaderMockGuard) = + get_http_downloader_mock(vec![], false, HttpDownloaderMockHandler::default()); + + let stdout = "* main\n 2.2\n 1.10"; + + let (process, _process_guard): (_, ProcessExecutorMockGuard) = get_process_executor_mock( + vec![cmd_full(["git", "branch", "--no-color"], 0, stdout, "")], + false, + MockHandler::default(), + ); + + let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new(); + repo_config.insert( + "url".to_string(), + PhpMixed::String("https://example.org/acme.git".to_string()), + ); + + let mut driver = GitDriver::new(repo_config, io, config, http_downloader, process); + driver.__set_repo_dir(home_path); + + assert_eq!("main", driver.get_root_identifier().unwrap()); } #[test] -#[ignore = "requires ProcessExecutor mock (getProcessExecutorMock/expects), IOInterface mock and Reflection setRepoDir, none available"] fn test_get_branches_filter_invalid_branch_names() { let SetUp { home, - config: _, + config, network_env, } = set_up(); + let home_path = home.path().to_string_lossy().into_owned(); let _tear_down = TearDown::new(home.path().to_path_buf(), network_env); - todo!() + + let config = Rc::new(RefCell::new(config)); + let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(IOStub::new())); + + let (http_downloader, _http_guard): (_, HttpDownloaderMockGuard) = + get_http_downloader_mock(vec![], false, HttpDownloaderMockHandler::default()); + + // Branches starting with a - character are not valid git branch names. + // Still assert that they get filtered to prevent issues later on. + let stdout = "* main 089681446ba44d6d9004350192486f2ceb4eaa06 commit\n 2.2 12681446ba44d6d9004350192486f2ceb4eaa06 commit\n -h 089681446ba44d6d9004350192486f2ceb4eaa06 commit"; + + let (process, _process_guard): (_, ProcessExecutorMockGuard) = get_process_executor_mock( + vec![cmd_full( + ["git", "branch", "--no-color", "--no-abbrev", "-v"], + 0, + stdout, + "", + )], + false, + MockHandler::default(), + ); + + let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new(); + repo_config.insert( + "url".to_string(), + PhpMixed::String("https://example.org/acme.git".to_string()), + ); + + let mut driver = GitDriver::new(repo_config, io, config, http_downloader, process); + driver.__set_repo_dir(home_path); + + let branches = driver.get_branches().unwrap(); + let mut expected: IndexMap<String, String> = IndexMap::new(); + expected.insert( + "main".to_string(), + "089681446ba44d6d9004350192486f2ceb4eaa06".to_string(), + ); + expected.insert( + "2.2".to_string(), + "12681446ba44d6d9004350192486f2ceb4eaa06".to_string(), + ); + assert_eq!(expected, branches); } #[test] -#[ignore = "requires ProcessExecutor mock (getProcessExecutorMock) and IOInterface/HttpDownloader mocks, none available"] fn test_file_get_content_invalid_identifier() { let SetUp { home, - config: _, + config, network_env, } = set_up(); let _tear_down = TearDown::new(home.path().to_path_buf(), network_env); - todo!() + + let config = Rc::new(RefCell::new(config)); + let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(IOStub::new())); + + let (http_downloader, _http_guard): (_, HttpDownloaderMockGuard) = + get_http_downloader_mock(vec![], false, HttpDownloaderMockHandler::default()); + + let (process, _process_guard): (_, ProcessExecutorMockGuard) = + get_process_executor_mock(vec![], false, MockHandler::default()); + + let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new(); + repo_config.insert( + "url".to_string(), + PhpMixed::String("https://example.org/acme.git".to_string()), + ); + + let mut driver = GitDriver::new(repo_config, io, config, http_downloader, process); + + assert_eq!(None, driver.get_file_content("file.txt", "h").unwrap()); + + let err = driver.get_file_content("file.txt", "-h").unwrap_err(); + assert!(err.downcast_ref::<RuntimeException>().is_some()); } #[test] -#[ignore = "requires ProcessExecutor mock (getProcessExecutorMock) and IOInterface/HttpDownloader mocks, none available"] fn test_get_change_date_invalid_identifier() { let SetUp { home, - config: _, + config, network_env, } = set_up(); let _tear_down = TearDown::new(home.path().to_path_buf(), network_env); - todo!() + + let config = Rc::new(RefCell::new(config)); + let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(IOStub::new())); + + let (http_downloader, _http_guard): (_, HttpDownloaderMockGuard) = + get_http_downloader_mock(vec![], false, HttpDownloaderMockHandler::default()); + + let (process, _process_guard): (_, ProcessExecutorMockGuard) = + get_process_executor_mock(vec![], false, MockHandler::default()); + + let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new(); + repo_config.insert( + "url".to_string(), + PhpMixed::String("https://example.org/acme.git".to_string()), + ); + + let mut driver = GitDriver::new(repo_config, io, config, http_downloader, process); + + let err = driver.get_change_date("-n1 --format=%at HEAD").unwrap_err(); + assert!(err.downcast_ref::<RuntimeException>().is_some()); } |
