aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/command/fund_command_test.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/tests/command/fund_command_test.rs')
-rw-r--r--crates/shirabe/tests/command/fund_command_test.rs198
1 files changed, 196 insertions, 2 deletions
diff --git a/crates/shirabe/tests/command/fund_command_test.rs b/crates/shirabe/tests/command/fund_command_test.rs
index f75090c..bbcc589 100644
--- a/crates/shirabe/tests/command/fund_command_test.rs
+++ b/crates/shirabe/tests/command/fund_command_test.rs
@@ -1,7 +1,201 @@
//! ref: composer/tests/Composer/Test/Command/FundCommandTest.php
-#[ignore = "missing TestCase::init_temp_composer, create_installed_json, and get_application_tester (ApplicationTester) infrastructure"]
+use crate::test_case::{
+ RunOptions, create_installed_json, get_application_tester, get_complete_package,
+ init_temp_composer,
+};
+use indexmap::IndexMap;
+use serial_test::serial;
+use shirabe::package::handle::{CompletePackageHandle, PackageInterfaceHandle};
+use shirabe_php_shim::PhpMixed;
+
+fn funding_entry(r#type: &str, url: &str) -> IndexMap<String, PhpMixed> {
+ let mut m = IndexMap::new();
+ m.insert("type".to_string(), PhpMixed::from(r#type));
+ m.insert("url".to_string(), PhpMixed::from(url));
+ m
+}
+
+/// Runs one `useCaseProvider` case.
+fn run_fund_case(
+ composer_json: serde_json::Value,
+ command: &[(&str, PhpMixed)],
+ funding: &[(&str, IndexMap<String, PhpMixed>)],
+ expected: &str,
+) {
+ let tear_down = init_temp_composer(Some(&composer_json), None, None, true);
+
+ let first = get_complete_package("first/pkg", "2.3.4");
+ let stable = get_complete_package("stable/pkg", "1.0.0");
+ let dev = get_complete_package("dev/pkg", "2.3.4.5");
+
+ let by_name: IndexMap<&str, &CompletePackageHandle> = [
+ ("first/pkg", &first),
+ ("stable/pkg", &stable),
+ ("dev/pkg", &dev),
+ ]
+ .into_iter()
+ .collect();
+
+ for (pkg_name, info) in funding {
+ if let Some(pkg) = by_name.get(pkg_name) {
+ pkg.set_funding(vec![info.clone()]);
+ }
+ }
+
+ let packages: Vec<PackageInterfaceHandle> = vec![first.into(), stable.into()];
+ let dev_packages: Vec<PackageInterfaceHandle> = vec![dev.into()];
+
+ create_installed_json(&packages, &dev_packages, true);
+
+ let mut input: Vec<(PhpMixed, PhpMixed)> =
+ vec![(PhpMixed::from("command"), PhpMixed::from("fund"))];
+ for (k, v) in command {
+ input.push((PhpMixed::from(*k), v.clone()));
+ }
+
+ let mut app_tester = get_application_tester();
+ let status_code = app_tester
+ .run(
+ input,
+ RunOptions {
+ capture_stderr_separately: true,
+ ..RunOptions::default()
+ },
+ )
+ .unwrap();
+ assert_eq!(0, status_code);
+ assert_eq!(expected.trim(), app_tester.get_display().trim());
+
+ drop(tear_down);
+}
+
#[test]
+#[serial]
+#[ignore = "FundCommand queries every repository (incl. the default packagist ComposerRepository) over \
+ HTTP for funding metadata before falling back to locally installed data, reaching \
+ shirabe-php-shim curl.rs curl_version (todo!()); this path needs real network access"]
fn test_fund_command() {
- todo!()
+ // 'no funding links present, locally or remotely'
+ run_fund_case(
+ serde_json::json!({
+ "repositories": [],
+ "require": { "first/pkg": "^2.0" },
+ "require-dev": { "dev/pkg": "~4.0" },
+ }),
+ &[],
+ &[],
+ "No funding links were found in your package dependencies. This doesn't mean they don't need your support!",
+ );
+
+ // 'funding links set locally are used as fallback if not found remotely'
+ run_fund_case(
+ serde_json::json!({
+ "repositories": [],
+ "require": { "first/pkg": "^2.0" },
+ "require-dev": { "dev/pkg": "~4.0" },
+ }),
+ &[],
+ &[
+ (
+ "first/pkg",
+ funding_entry("github", "https://github.com/composer-test-data"),
+ ),
+ (
+ "dev/pkg",
+ funding_entry("github", "https://github.com/composer-test-data-dev"),
+ ),
+ ],
+ "The following packages were found in your dependencies which publish funding information:
+
+dev
+ pkg
+ https://github.com/sponsors/composer-test-data-dev
+
+first
+ https://github.com/sponsors/composer-test-data
+
+Please consider following these links and sponsoring the work of package authors!
+Thank you!",
+ );
+
+ // 'funding links set remotely are used as primary if found'
+ run_fund_case(
+ serde_json::json!({
+ "repositories": [
+ {
+ "type": "package",
+ "package": [
+ { "name": "first/pkg", "version": "dev-foo", "funding": [{ "type": "github", "url": "https://github.com/test-should-not-be-used" }] },
+ { "name": "first/pkg", "version": "dev-main", "default-branch": true, "funding": [{ "type": "custom", "url": "https://example.org" }] },
+ { "name": "dev/pkg", "version": "dev-foo", "default-branch": true, "funding": [{ "type": "github", "url": "https://github.com/org" }] },
+ { "name": "stable/pkg", "version": "1.0.0", "funding": [{ "type": "github", "url": "org2" }] },
+ ],
+ },
+ ],
+ "require": { "first/pkg": "^2.0", "stable/pkg": "^1.0" },
+ "require-dev": { "dev/pkg": "~4.0" },
+ }),
+ &[],
+ &[
+ (
+ "first/pkg",
+ funding_entry("github", "https://github.com/composer-test-data"),
+ ),
+ (
+ "dev/pkg",
+ funding_entry("github", "https://github.com/composer-test-data-dev"),
+ ),
+ (
+ "stable/pkg",
+ funding_entry("github", "https://github.com/composer-test-data-stable"),
+ ),
+ ],
+ "The following packages were found in your dependencies which publish funding information:
+
+dev
+ pkg
+ https://github.com/sponsors/org
+
+first
+ https://example.org
+
+stable
+ https://github.com/sponsors/composer-test-data-stable
+
+Please consider following these links and sponsoring the work of package authors!
+Thank you!",
+ );
+
+ // 'format funding links as JSON'
+ run_fund_case(
+ serde_json::json!({
+ "repositories": [],
+ "require": { "first/pkg": "^2.0" },
+ "require-dev": { "dev/pkg": "~4.0" },
+ }),
+ &[("--format", PhpMixed::from("json"))],
+ &[
+ (
+ "first/pkg",
+ funding_entry("github", "https://github.com/composer-test-data"),
+ ),
+ (
+ "dev/pkg",
+ funding_entry("github", "https://github.com/composer-test-data-dev"),
+ ),
+ ],
+ r#"{
+ "dev": {
+ "https://github.com/sponsors/composer-test-data-dev": [
+ "pkg"
+ ]
+ },
+ "first": {
+ "https://github.com/sponsors/composer-test-data": [
+ "pkg"
+ ]
+ }
+}"#,
+ );
}