|
| 1 | +//! GitHub API tests focusing on PR creation functionality |
| 2 | +
|
| 3 | +use repos::config::Repository; |
| 4 | +use repos::github::api::create_pull_request; |
| 5 | +use repos::github::types::PrOptions; |
| 6 | +use std::fs; |
| 7 | +use std::path::PathBuf; |
| 8 | + |
| 9 | +/// Helper function to create a test repository |
| 10 | +fn create_test_repo(name: &str, url: &str) -> (Repository, PathBuf) { |
| 11 | + let temp_base = std::env::temp_dir(); |
| 12 | + let unique_id = format!("{}-{}", name, std::process::id()); |
| 13 | + let temp_dir = temp_base.join(format!("repos_test_{}", unique_id)); |
| 14 | + fs::create_dir_all(&temp_dir).expect("Failed to create temp directory"); |
| 15 | + |
| 16 | + // Create the actual repository directory |
| 17 | + let repo_dir = temp_dir.join(name); |
| 18 | + fs::create_dir_all(&repo_dir).expect("Failed to create repo directory"); |
| 19 | + |
| 20 | + // Initialize git repository |
| 21 | + std::process::Command::new("git") |
| 22 | + .args(["init"]) |
| 23 | + .current_dir(&repo_dir) |
| 24 | + .output() |
| 25 | + .expect("Failed to initialize git repository"); |
| 26 | + |
| 27 | + // Configure git user for testing |
| 28 | + std::process::Command::new("git") |
| 29 | + .args(["config", "user.name", "Test User"]) |
| 30 | + .current_dir(&repo_dir) |
| 31 | + .output() |
| 32 | + .expect("Failed to configure git user"); |
| 33 | + |
| 34 | + std::process::Command::new("git") |
| 35 | + .args(["config", "user.email", "test@example.com"]) |
| 36 | + .current_dir(&repo_dir) |
| 37 | + .output() |
| 38 | + .expect("Failed to configure git email"); |
| 39 | + |
| 40 | + let mut repo = Repository::new(name.to_string(), url.to_string()); |
| 41 | + repo.set_config_dir(Some(temp_dir.clone())); |
| 42 | + |
| 43 | + (repo, temp_dir) |
| 44 | +} |
| 45 | + |
| 46 | +#[tokio::test] |
| 47 | +async fn test_create_pull_request_no_changes() { |
| 48 | + let (repo, _temp_dir) = create_test_repo("test-repo", "git@github.com:owner/test-repo.git"); |
| 49 | + |
| 50 | + let options = PrOptions::new( |
| 51 | + "Test PR".to_string(), |
| 52 | + "Test body".to_string(), |
| 53 | + "fake-token".to_string(), |
| 54 | + ); |
| 55 | + |
| 56 | + // Should succeed with no changes (just prints a message and returns Ok) |
| 57 | + let result = create_pull_request(&repo, &options).await; |
| 58 | + assert!(result.is_ok()); |
| 59 | +} |
| 60 | + |
| 61 | +#[tokio::test] |
| 62 | +async fn test_pr_options_builder() { |
| 63 | + let options = PrOptions::new( |
| 64 | + "Test Title".to_string(), |
| 65 | + "Test Body".to_string(), |
| 66 | + "test-token".to_string(), |
| 67 | + ) |
| 68 | + .with_branch_name("feature-branch".to_string()) |
| 69 | + .with_base_branch("develop".to_string()) |
| 70 | + .with_commit_message("Custom commit".to_string()) |
| 71 | + .as_draft() |
| 72 | + .create_only(); |
| 73 | + |
| 74 | + assert_eq!(options.title, "Test Title"); |
| 75 | + assert_eq!(options.body, "Test Body"); |
| 76 | + assert_eq!(options.token, "test-token"); |
| 77 | + assert_eq!(options.branch_name, Some("feature-branch".to_string())); |
| 78 | + assert_eq!(options.base_branch, Some("develop".to_string())); |
| 79 | + assert_eq!(options.commit_msg, Some("Custom commit".to_string())); |
| 80 | + assert!(options.draft); |
| 81 | + assert!(options.create_only); |
| 82 | +} |
| 83 | + |
| 84 | +#[tokio::test] |
| 85 | +async fn test_pr_options_defaults() { |
| 86 | + let options = PrOptions::new( |
| 87 | + "Test Title".to_string(), |
| 88 | + "Test Body".to_string(), |
| 89 | + "test-token".to_string(), |
| 90 | + ); |
| 91 | + |
| 92 | + assert_eq!(options.title, "Test Title"); |
| 93 | + assert_eq!(options.body, "Test Body"); |
| 94 | + assert_eq!(options.token, "test-token"); |
| 95 | + assert_eq!(options.branch_name, None); |
| 96 | + assert_eq!(options.base_branch, None); |
| 97 | + assert_eq!(options.commit_msg, None); |
| 98 | + assert!(!options.draft); |
| 99 | + assert!(!options.create_only); |
| 100 | +} |
0 commit comments