//! 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] #[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() { // 'by name and description' run_search_case( vec![( "tokens".into(), PhpMixed::List(vec![PhpMixed::from("fancy")]), )], "bar/baz ! Abandoned ! 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 ! Abandoned ! 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] #[serial] fn test_invalid_format() { 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] #[serial] fn test_invalid_flags() { 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 ); }