Skip to content

Commit 0983055

Browse files
authored
Merge pull request #27 from codcod/17-add-tests
17 add tests
2 parents bd7caba + e837309 commit 0983055

File tree

5 files changed

+1341
-35
lines changed

5 files changed

+1341
-35
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,30 @@ jobs:
7070
- name: Run security audit
7171
run: cargo audit
7272

73-
# coverage:
74-
# name: Code Coverage
75-
# runs-on: ubuntu-latest
76-
# steps:
77-
# - name: Checkout code
78-
# uses: actions/checkout@v5
79-
80-
# - name: Install Rust
81-
# uses: dtolnay/rust-toolchain@stable
82-
# with:
83-
# components: llvm-tools-preview
84-
85-
# - name: Cache dependencies
86-
# uses: actions/cache@v4
87-
# with:
88-
# path: |
89-
# ~/.cargo/bin/
90-
# ~/.cargo/registry/index/
91-
# ~/.cargo/registry/cache/
92-
# ~/.cargo/git/db/
93-
# target/
94-
# key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
95-
96-
# - name: Install cargo-llvm-cov
97-
# uses: taiki-e/install-action@cargo-llvm-cov
98-
99-
# - name: Generate code coverage
100-
# run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
101-
102-
# - name: Upload coverage to Codecov
103-
# uses: codecov/codecov-action@v4
104-
# with:
105-
# token: ${{ secrets.CODECOV_TOKEN }}
106-
# files: lcov.info
107-
# fail_ci_if_error: true
73+
coverage:
74+
name: Code Coverage
75+
runs-on: ubuntu-latest
76+
steps:
77+
- name: Checkout code
78+
uses: actions/checkout@v5
79+
80+
- name: Install Rust
81+
uses: dtolnay/rust-toolchain@stable
82+
with:
83+
components: llvm-tools-preview
84+
85+
- name: Install cargo-llvm-cov
86+
uses: taiki-e/install-action@cargo-llvm-cov
87+
88+
- name: Generate code coverage
89+
run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
90+
91+
- name: Upload coverage to Codecov
92+
uses: codecov/codecov-action@v4
93+
with:
94+
token: ${{ secrets.CODECOV_TOKEN }}
95+
files: lcov.info
96+
fail_ci_if_error: true
10897

10998
cross_platform:
11099
name: Cross Platform Build

tests/github_api_tests.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)