diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe/tests/command/search_command_test.rs | 160 |
1 files changed, 154 insertions, 6 deletions
diff --git a/crates/shirabe/tests/command/search_command_test.rs b/crates/shirabe/tests/command/search_command_test.rs index 5cad1eb..187d395 100644 --- a/crates/shirabe/tests/command/search_command_test.rs +++ b/crates/shirabe/tests/command/search_command_test.rs @@ -1,19 +1,167 @@ //! ref: composer/tests/Composer/Test/Command/SearchCommandTest.php +use crate::test_case::{RunOptions, get_application_tester, init_temp_composer}; +use serial_test::serial; +use shirabe_php_shim::PhpMixed; + +fn repositories_json() -> serde_json::Value { + serde_json::json!({ + "repositories": [ + { "packagist.org": false }, + { + "type": "package", + "package": [ + { "name": "vendor-1/package-1", "description": "generic description", "version": "1.0.0" }, + { "name": "foo/bar", "description": "generic description", "version": "1.0.0" }, + { "name": "bar/baz", "description": "fancy baz", "version": "1.0.0", "abandoned": true }, + { "name": "vendor-2/fancy-package", "fancy description": null, "version": "1.0.0", "type": "foo" }, + ], + }, + ], + }) +} + +/// ref: SearchCommandTest::testSearch (data provider rolled into one body). +fn run_search_case(command: Vec<(PhpMixed, PhpMixed)>, expected: &str) { + let _tear_down = init_temp_composer(Some(&repositories_json()), None, None, true); + + let mut input: Vec<(PhpMixed, PhpMixed)> = + vec![(PhpMixed::from("command"), PhpMixed::from("search"))]; + input.extend(command); + + let mut app_tester = get_application_tester(); + app_tester.run(input, RunOptions::default()).unwrap(); + assert_eq!(expected.trim(), app_tester.get_display().trim()); +} + #[test] -#[ignore = "missing TestCase::init_temp_composer and get_application_tester (ApplicationTester) infrastructure"] +#[serial] +#[ignore = "searching a `package`-type repo constructs an HTTP downloader whose User-Agent build calls curl_version() (todo!() in shirabe-php-shim::curl); the HTTP layer is not yet ported"] fn test_search() { - todo!() + // 'by name and description' + run_search_case( + vec![("tokens".into(), PhpMixed::List(vec![PhpMixed::from("fancy")]))], + "bar/baz <warning>! Abandoned !</warning> fancy baz\nvendor-2/fancy-package", + ); + + // 'by name and description with multiple tokens' + run_search_case( + vec![( + "tokens".into(), + PhpMixed::List(vec![PhpMixed::from("fancy"), PhpMixed::from("vendor")]), + )], + "vendor-1/package-1 generic description\nbar/baz <warning>! Abandoned !</warning> fancy baz\nvendor-2/fancy-package", + ); + + // 'by name only' + run_search_case( + vec![ + ("tokens".into(), PhpMixed::List(vec![PhpMixed::from("fancy")])), + ("--only-name".into(), PhpMixed::from(true)), + ], + "vendor-2/fancy-package", + ); + + // 'by vendor only' + run_search_case( + vec![ + ("tokens".into(), PhpMixed::List(vec![PhpMixed::from("bar")])), + ("--only-vendor".into(), PhpMixed::from(true)), + ], + "bar", + ); + + // 'by type' + run_search_case( + vec![ + ("tokens".into(), PhpMixed::List(vec![PhpMixed::from("vendor")])), + ("--type".into(), PhpMixed::from("foo")), + ], + "vendor-2/fancy-package", + ); + + // 'json format' + run_search_case( + vec![ + ( + "tokens".into(), + PhpMixed::List(vec![PhpMixed::from("vendor-2/fancy")]), + ), + ("--format".into(), PhpMixed::from("json")), + ], + "[\n {\n \"name\": \"vendor-2/fancy-package\",\n \"description\": null\n }\n]", + ); + + // 'no results' + run_search_case( + vec![( + "tokens".into(), + PhpMixed::List(vec![PhpMixed::from("invalid-package-name")]), + )], + "", + ); } #[test] -#[ignore = "missing TestCase::init_temp_composer and get_application_tester (ApplicationTester) infrastructure"] +#[serial] fn test_invalid_format() { - todo!() + let _tear_down = init_temp_composer( + Some(&serde_json::json!({ "repositories": { "packagist.org": false } })), + None, + None, + true, + ); + + let mut app_tester = get_application_tester(); + let result = app_tester + .run( + vec![ + (PhpMixed::from("command"), PhpMixed::from("search")), + (PhpMixed::from("--format"), PhpMixed::from("test-format")), + ( + PhpMixed::from("tokens"), + PhpMixed::List(vec![PhpMixed::from("test")]), + ), + ], + RunOptions::default(), + ) + .unwrap(); + assert_eq!(1, result); + assert_eq!( + "Unsupported format \"test-format\". See help for supported formats.", + app_tester.get_display().trim() + ); } #[test] -#[ignore = "missing TestCase::init_temp_composer and get_application_tester (ApplicationTester) infrastructure"] +#[serial] fn test_invalid_flags() { - todo!() + let _tear_down = init_temp_composer( + Some(&serde_json::json!({ "repositories": { "packagist.org": false } })), + None, + None, + true, + ); + + let mut app_tester = get_application_tester(); + let err = app_tester + .run( + vec![ + (PhpMixed::from("command"), PhpMixed::from("search")), + (PhpMixed::from("--only-vendor"), PhpMixed::from(true)), + (PhpMixed::from("--only-name"), PhpMixed::from(true)), + ( + PhpMixed::from("tokens"), + PhpMixed::List(vec![PhpMixed::from("test")]), + ), + ], + RunOptions::default(), + ) + .expect_err("expected InvalidArgumentException"); + assert!( + err.to_string() + .contains("--only-name and --only-vendor cannot be used together"), + "got: {:?}", + err + ); } |
