diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-11 02:36:42 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-11 02:36:42 +0900 |
| commit | 4e99773a3d203e73b8bf6464490d05649a269fa7 (patch) | |
| tree | 7a6f1a7f773a14ea72dc2f9ff4124badd345833d /crates/mozart/src/commands/repository.rs | |
| parent | 4df5f8491320e5795718cf0222e80fa27e57c8ad (diff) | |
| download | php-mozart-4e99773a3d203e73b8bf6464490d05649a269fa7.tar.gz php-mozart-4e99773a3d203e73b8bf6464490d05649a269fa7.tar.zst php-mozart-4e99773a3d203e73b8bf6464490d05649a269fa7.zip | |
test(commands): remove per-command tests
Diffstat (limited to 'crates/mozart/src/commands/repository.rs')
| -rw-r--r-- | crates/mozart/src/commands/repository.rs | 886 |
1 files changed, 0 insertions, 886 deletions
diff --git a/crates/mozart/src/commands/repository.rs b/crates/mozart/src/commands/repository.rs index adf34b5..6616352 100644 --- a/crates/mozart/src/commands/repository.rs +++ b/crates/mozart/src/commands/repository.rs @@ -279,889 +279,3 @@ fn execute_enable(ctx: &BaseConfigContext, args: &RepositoryArgs) -> anyhow::Res anyhow::bail!("Only packagist.org can be enabled/disabled using this command."); } - -#[cfg(test)] -mod tests { - use super::*; - - fn make_io() -> std::sync::Arc<std::sync::Mutex<Box<dyn IoInterface>>> { - let console = mozart_core::console::Console::new(0, false, false, false, false); - std::sync::Arc::new(std::sync::Mutex::new(Box::new(console))) - } - - fn make_args( - action: Option<&str>, - name: Option<&str>, - arg1: Option<&str>, - arg2: Option<&str>, - ) -> RepositoryArgs { - RepositoryArgs { - action: action.map(|s| s.to_string()), - name: name.map(|s| s.to_string()), - arg1: arg1.map(|s| s.to_string()), - arg2: arg2.map(|s| s.to_string()), - global: false, - file: None, - append: false, - before: None, - after: None, - } - } - - fn make_cli() -> super::super::Cli { - use clap::Parser; - super::super::Cli::parse_from(["mozart", "repository", "list"]) - } - - #[tokio::test] - async fn test_list_empty() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("list"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - // Empty repos → synthesises [packagist.org] disabled - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_list_with_repos() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "vcs", "url": "https://example.com"}]}"#, - ).unwrap(); - - let mut args = make_args(Some("list"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_list_with_disabled_packagist() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": [{"packagist.org": false}]}"#).unwrap(); - - let mut args = make_args(Some("list"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_list_no_packagist_synth_when_composer_type_present() { - // When a composer-type repo pointing at packagist.org is present, - // no synthesised [packagist.org] disabled line should appear. - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "packagist.org", "type": "composer", "url": "https://repo.packagist.org"}]}"#, - ) - .unwrap(); - - let mut args = make_args(Some("list"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_add_type_url() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args( - Some("add"), - Some("my-repo"), - Some("vcs"), - Some("https://example.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos.len(), 1); - assert_eq!(repos[0]["name"], "my-repo"); - assert_eq!(repos[0]["type"], "vcs"); - assert_eq!(repos[0]["url"], "https://example.com"); - } - - #[tokio::test] - async fn test_add_json() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args( - Some("add"), - Some("my-repo"), - Some(r#"{"type":"path","url":"../local-pkg"}"#), - None, - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["type"], "path"); - assert_eq!(repos[0]["name"], "my-repo"); - } - - #[tokio::test] - async fn test_add_prepend_default() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "existing", "type": "vcs", "url": "https://existing.com"}]}"#, - ).unwrap(); - - let mut args = make_args( - Some("add"), - Some("new-repo"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["name"], "new-repo"); - assert_eq!(repos[1]["name"], "existing"); - } - - #[tokio::test] - async fn test_add_append() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "existing", "type": "vcs", "url": "https://existing.com"}]}"#, - ).unwrap(); - - let mut args = make_args( - Some("add"), - Some("new-repo"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.append = true; - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["name"], "existing"); - assert_eq!(repos[1]["name"], "new-repo"); - } - - #[tokio::test] - async fn test_add_before() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "a", "type": "vcs", "url": "https://a.com"}, {"name": "b", "type": "vcs", "url": "https://b.com"}]}"#, - ).unwrap(); - - let mut args = make_args( - Some("add"), - Some("new"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.before = Some("b".to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["name"], "a"); - assert_eq!(repos[1]["name"], "new"); - assert_eq!(repos[2]["name"], "b"); - } - - #[tokio::test] - async fn test_add_after() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "a", "type": "vcs", "url": "https://a.com"}, {"name": "b", "type": "vcs", "url": "https://b.com"}]}"#, - ).unwrap(); - - let mut args = make_args( - Some("add"), - Some("new"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.after = Some("a".to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["name"], "a"); - assert_eq!(repos[1]["name"], "new"); - assert_eq!(repos[2]["name"], "b"); - } - - #[tokio::test] - async fn test_add_before_and_after_error() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args( - Some("add"), - Some("new"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.before = Some("a".to_string()); - args.after = Some("b".to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_add_missing_args() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("add"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_add_missing_name() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("add"), None, Some("vcs"), Some("https://url.com")); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_remove() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "vcs", "url": "https://example.com"}]}"#, - ).unwrap(); - - let mut args = make_args(Some("remove"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - assert!(json.get("repositories").is_none()); - } - - #[tokio::test] - async fn test_remove_packagist_disables() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("remove"), Some("packagist.org"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["packagist.org"], false); - } - - #[tokio::test] - async fn test_remove_alias_rm() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "vcs", "url": "https://example.com"}]}"#, - ).unwrap(); - - let mut args = make_args(Some("rm"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - assert!(json.get("repositories").is_none()); - } - - #[tokio::test] - async fn test_remove_missing_name() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("remove"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_set_url() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "vcs", "url": "https://old.com"}]}"#, - ) - .unwrap(); - - let mut args = make_args( - Some("set-url"), - Some("my-repo"), - Some("https://new.com"), - None, - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - assert_eq!(json["repositories"][0]["url"], "https://new.com"); - } - - #[tokio::test] - async fn test_set_url_not_found() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": []}"#).unwrap(); - - let mut args = make_args( - Some("set-url"), - Some("missing"), - Some("https://new.com"), - None, - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_set_url_alias() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "vcs", "url": "https://old.com"}]}"#, - ) - .unwrap(); - - let mut args = make_args( - Some("seturl"), - Some("my-repo"), - Some("https://new.com"), - None, - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - assert_eq!(json["repositories"][0]["url"], "https://new.com"); - } - - #[tokio::test] - async fn test_get_url() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "vcs", "url": "https://example.com"}]}"#, - ).unwrap(); - - let mut args = make_args(Some("get-url"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_get_url_not_found() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": []}"#).unwrap(); - - let mut args = make_args(Some("get-url"), Some("missing"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_disable_packagist() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("disable"), Some("packagist.org"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["packagist.org"], false); - } - - #[tokio::test] - async fn test_disable_packagist_idempotent() { - // Calling disable twice should not create a duplicate entry. - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": [{"packagist.org": false}]}"#).unwrap(); - - let mut args = make_args(Some("disable"), Some("packagist.org"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos.len(), 1, "should still be just one disable entry"); - assert_eq!(repos[0]["packagist.org"], false); - } - - #[tokio::test] - async fn test_disable_non_packagist_error() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("disable"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_disable_without_name_error() { - // Composer requires a name for disable; Mozart mirrors that. - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("disable"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_enable_packagist() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": [{"packagist.org": false}]}"#).unwrap(); - - let mut args = make_args(Some("enable"), Some("packagist.org"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - assert!(json.get("repositories").is_none()); - } - - #[tokio::test] - async fn test_enable_non_packagist_error() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("enable"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_list_composer_format() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": {"my-repo": {"type": "vcs", "url": "https://example.com"}}}"#, - ) - .unwrap(); - - let mut args = make_args(Some("list"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_get_url_composer_format() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": {"my-repo": {"type": "vcs", "url": "https://example.com"}}}"#, - ) - .unwrap(); - - let mut args = make_args(Some("get-url"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_ok()); - } - - #[tokio::test] - async fn test_get_url_no_url_error() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": [{"name": "my-repo", "type": "artifact"}]}"#, - ) - .unwrap(); - - let mut args = make_args(Some("get-url"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - let msg = result.unwrap_err().to_string(); - assert!( - msg.contains("does not have a URL"), - "unexpected message: {msg}" - ); - } - - #[tokio::test] - async fn test_get_url_not_found_message() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": []}"#).unwrap(); - - let mut args = make_args(Some("get-url"), Some("missing"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - let msg = result.unwrap_err().to_string(); - assert!(msg.contains("There is no"), "unexpected message: {msg}"); - } - - #[tokio::test] - async fn test_set_url_composer_format_keeps_assoc_shape() { - // Composer's setRepositoryUrl mutates in place without converting assoc → list. - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": {"my-repo": {"type": "vcs", "url": "https://old.com"}}}"#, - ) - .unwrap(); - - let mut args = make_args( - Some("set-url"), - Some("my-repo"), - Some("https://new.com"), - None, - ); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - // Format is preserved: still an assoc object. - let repos = json["repositories"].as_object().unwrap(); - assert_eq!(repos["my-repo"]["url"], "https://new.com"); - } - - #[tokio::test] - async fn test_remove_composer_format_converts_and_removes() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories": {"my-repo": {"type": "vcs", "url": "https://example.com"}}}"#, - ) - .unwrap(); - - let mut args = make_args(Some("remove"), Some("my-repo"), None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - assert!(json.get("repositories").is_none()); - } - - #[test] - fn test_normalize_repositories_array_passthrough() { - use super::super::config_helpers::normalize_repositories; - let val = serde_json::json!([ - {"name": "foo", "type": "vcs", "url": "https://foo.com"} - ]); - let result = normalize_repositories(&val); - assert_eq!(result.len(), 1); - assert_eq!(result[0]["name"], "foo"); - } - - #[test] - fn test_normalize_repositories_object_injects_name() { - use super::super::config_helpers::normalize_repositories; - let val = serde_json::json!({ - "foo": {"type": "vcs", "url": "https://foo.com"}, - "bar": {"type": "composer", "url": "https://bar.com"} - }); - let result = normalize_repositories(&val); - assert_eq!(result.len(), 2); - let names: indexmap::IndexSet<&str> = result - .iter() - .filter_map(|v| v.get("name").and_then(|n| n.as_str())) - .collect(); - assert!(names.contains("foo")); - assert!(names.contains("bar")); - } - - #[test] - fn test_normalize_repositories_object_boolean_entry() { - use super::super::config_helpers::normalize_repositories; - let val = serde_json::json!({"packagist.org": false}); - let result = normalize_repositories(&val); - assert_eq!(result.len(), 1); - assert_eq!(result[0]["packagist.org"], false); - } - - #[test] - fn test_normalize_repositories_empty() { - use super::super::config_helpers::normalize_repositories; - let val = serde_json::json!(null); - let result = normalize_repositories(&val); - assert!(result.is_empty()); - } - - #[tokio::test] - async fn test_unknown_action() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, "{}").unwrap(); - - let mut args = make_args(Some("invalid"), None, None, None); - args.file = Some(file.to_str().unwrap().to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } - - #[tokio::test] - async fn test_insert_before() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories":[{"name":"a","type":"vcs","url":"https://a.com"},{"name":"b","type":"vcs","url":"https://b.com"}]}"#, - ) - .unwrap(); - - let mut args = make_args( - Some("add"), - Some("new"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.before = Some("b".to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["name"], "a"); - assert_eq!(repos[1]["name"], "new"); - assert_eq!(repos[2]["name"], "b"); - } - - #[tokio::test] - async fn test_insert_after() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write( - &file, - r#"{"repositories":[{"name":"a","type":"vcs","url":"https://a.com"},{"name":"b","type":"vcs","url":"https://b.com"}]}"#, - ) - .unwrap(); - - let mut args = make_args( - Some("add"), - Some("new"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.after = Some("a".to_string()); - - let cli = make_cli(); - let io = make_io(); - execute(&args, &cli, io).await.unwrap(); - - let json: serde_json::Value = - serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); - let repos = json["repositories"].as_array().unwrap(); - assert_eq!(repos[0]["name"], "a"); - assert_eq!(repos[1]["name"], "new"); - assert_eq!(repos[2]["name"], "b"); - } - - #[tokio::test] - async fn test_insert_target_not_found() { - let dir = tempfile::TempDir::new().unwrap(); - let file = dir.path().join("composer.json"); - std::fs::write(&file, r#"{"repositories": []}"#).unwrap(); - - let mut args = make_args( - Some("add"), - Some("new"), - Some("vcs"), - Some("https://new.com"), - ); - args.file = Some(file.to_str().unwrap().to_string()); - args.before = Some("nonexistent".to_string()); - - let cli = make_cli(); - let io = make_io(); - let result = execute(&args, &cli, io).await; - assert!(result.is_err()); - } -} |
