diff options
Diffstat (limited to 'tests/repo.rs')
-rw-r--r-- | tests/repo.rs | 167 |
1 files changed, 97 insertions, 70 deletions
diff --git a/tests/repo.rs b/tests/repo.rs index bce5d77..a4e3bf5 100644 --- a/tests/repo.rs +++ b/tests/repo.rs @@ -2,37 +2,34 @@ use forgejo_api::structs::*; mod common; -#[tokio::test] -async fn repo() { - let api = common::get_api(); +struct Git { + dir: &'static std::path::Path, +} + +impl Git { + fn new<T: AsRef<std::path::Path> + ?Sized>(path: &'static T) -> Self { + let dir = path.as_ref(); + std::fs::create_dir_all(dir).unwrap(); + Self { dir } + } - tokio::fs::create_dir("./test_repo").await.unwrap(); - let git = || { + fn run(&self, args: &[impl AsRef<std::ffi::OsStr>]) { let mut cmd = std::process::Command::new("git"); - cmd.current_dir("./test_repo"); - cmd - }; - let _ = git() - .args(["config", "--global", "init.defaultBranch", "main"]) - .status() - .unwrap(); - let _ = git().args(["init"]).status().unwrap(); - let _ = git() - .args(["config", "user.name", "TestingAdmin"]) - .status() - .unwrap(); - let _ = git() - .args(["config", "user.email", "admin@noreply.example.org"]) - .status() - .unwrap(); - tokio::fs::write("./test_repo/README.md", "# Test\nThis is a test repo") + cmd.current_dir(self.dir); + let _ = cmd.args(args).status().unwrap(); + } +} + +async fn basic_repo(api: &forgejo_api::Forgejo, git: &Git, name: &str) -> Repository { + git.run(&["config", "--global", "init.defaultBranch", "main"]); + git.run(&["init"]); + git.run(&["config", "user.name", "TestingAdmin"]); + git.run(&["config", "user.email", "admin@noreply.example.org"]); + tokio::fs::write(&git.dir.join("README.md"), "# Test\nThis is a test repo") .await .unwrap(); - let _ = git().args(["add", "."]).status().unwrap(); - let _ = git() - .args(["commit", "-m", "initial commit"]) - .status() - .unwrap(); + git.run(&["add", "."]); + git.run(&["commit", "-m", "initial commit"]); let repo_opt = CreateRepoOption { auto_init: Some(false), @@ -41,7 +38,7 @@ async fn repo() { gitignores: Some("".into()), issue_labels: Some("".into()), license: Some("".into()), - name: "test".into(), + name: name.into(), object_format_name: None, private: Some(false), readme: None, @@ -58,36 +55,36 @@ async fn repo() { "repo owner is not \"TestingAdmin\"" ); assert!( - remote_repo.name.as_ref().unwrap() == "test", - "repo owner is not \"test\"" + remote_repo.name.as_ref().unwrap() == name, + "repo name is not \"{name}\"" ); - tokio::time::sleep(std::time::Duration::from_secs(3)).await; let mut remote_url = remote_repo.clone_url.clone().unwrap(); remote_url.set_username("TestingAdmin").unwrap(); remote_url.set_password(Some("password")).unwrap(); - let _ = git() - .args(["remote", "add", "origin", remote_url.as_str()]) - .status() - .unwrap(); - let _ = git() - .args(["push", "-u", "origin", "main"]) - .status() - .unwrap(); + git.run(&["remote", "add", "origin", remote_url.as_str()]); + git.run(&["push", "-u", "origin", "main"]); + + remote_repo +} + +#[tokio::test] +async fn pull_request() { + let api = common::login(); + + let git = Git::new("./test_repos/pr"); + let _ = basic_repo(&api, &git, "pr-test").await; + git.run(&["switch", "-c", "test"]); - let _ = git().args(["switch", "-c", "test"]).status().unwrap(); tokio::fs::write( - "./test_repo/example.rs", + "./test_repos/pr/example.rs", "fn add_one(x: u32) -> u32 { x + 1 }", ) .await .unwrap(); - let _ = git().args(["add", "."]).status().unwrap(); - let _ = git().args(["commit", "-m", "egg"]).status().unwrap(); - let _ = git() - .args(["push", "-u", "origin", "test"]) - .status() - .unwrap(); + git.run(&["add", "."]); + git.run(&["commit", "-m", "egg"]); + git.run(&["push", "-u", "origin", "test"]); let pr_opt = CreatePullRequestOption { assignee: None, @@ -101,20 +98,27 @@ async fn repo() { title: Some("test pr".into()), }; let pr = api - .repo_create_pull_request("TestingAdmin", "test", pr_opt) + .repo_create_pull_request("TestingAdmin", "pr-test", pr_opt) .await .expect("couldn't create pr"); - tokio::time::sleep(std::time::Duration::from_secs(3)).await; + let is_merged = api - .repo_pull_request_is_merged("TestingAdmin", "test", pr.number.unwrap()) + .repo_pull_request_is_merged("TestingAdmin", "pr-test", pr.number.unwrap()) .await .is_ok(); assert!(!is_merged, "pr should not yet be merged"); + let pr_files_query = RepoGetPullRequestFilesQuery::default(); let (_, _) = api - .repo_get_pull_request_files("TestingAdmin", "test", pr.number.unwrap(), pr_files_query) + .repo_get_pull_request_files( + "TestingAdmin", + "pr-test", + pr.number.unwrap(), + pr_files_query, + ) .await .unwrap(); + let merge_opt = MergePullRequestOption { r#do: MergePullRequestOptionDo::Merge, merge_commit_id: None, @@ -125,20 +129,27 @@ async fn repo() { head_commit_id: None, merge_when_checks_succeed: None, }; - api.repo_merge_pull_request("TestingAdmin", "test", pr.number.unwrap(), merge_opt) + + api.repo_merge_pull_request("TestingAdmin", "pr-test", pr.number.unwrap(), merge_opt) .await .expect("couldn't merge pr"); let is_merged = api - .repo_pull_request_is_merged("TestingAdmin", "test", pr.number.unwrap()) + .repo_pull_request_is_merged("TestingAdmin", "pr-test", pr.number.unwrap()) .await .is_ok(); assert!(is_merged, "pr should be merged"); - let _ = git().args(["fetch"]).status().unwrap(); - let _ = git().args(["pull"]).status().unwrap(); +} + +#[tokio::test] +async fn release() { + let api = common::login(); + + let git = Git::new("./test_repos/release"); + let _ = basic_repo(&api, &git, "release-test").await; let query = RepoListReleasesQuery::default(); assert!( - api.repo_list_releases("TestingAdmin", "test", query) + api.repo_list_releases("TestingAdmin", "release-test", query) .await .unwrap() .is_empty(), @@ -150,7 +161,7 @@ async fn repo() { tag_name: "v1.0".into(), target: None, }; - api.repo_create_tag("TestingAdmin", "test", tag_opt) + api.repo_create_tag("TestingAdmin", "release-test", tag_opt) .await .expect("failed to create tag"); @@ -163,7 +174,7 @@ async fn repo() { target_commitish: None, }; let release = api - .repo_create_release("TestingAdmin", "test", release_opt) + .repo_create_release("TestingAdmin", "release-test", release_opt) .await .expect("failed to create release"); let edit_release = EditReleaseOption { @@ -174,16 +185,21 @@ async fn repo() { tag_name: None, target_commitish: None, }; - api.repo_edit_release("TestingAdmin", "test", release.id.unwrap(), edit_release) - .await - .expect("failed to edit release"); + api.repo_edit_release( + "TestingAdmin", + "release-test", + release.id.unwrap(), + edit_release, + ) + .await + .expect("failed to edit release"); let release_by_tag = api - .repo_get_release_by_tag("TestingAdmin", "test", "v1.0") + .repo_get_release_by_tag("TestingAdmin", "release-test", "v1.0") .await .expect("failed to find release"); let release_latest = api - .repo_get_latest_release("TestingAdmin", "test") + .repo_get_latest_release("TestingAdmin", "release-test") .await .expect("failed to find latest release"); assert!(release_by_tag == release_latest, "releases not equal"); @@ -191,7 +207,7 @@ async fn repo() { let attachment = api .repo_create_release_attachment( "TestingAdmin", - "test", + "release-test", release.id.unwrap(), b"This is a file!".to_vec(), RepoCreateReleaseAttachmentQuery { @@ -204,7 +220,7 @@ async fn repo() { &*api .download_release_attachment( "TestingAdmin", - "test", + "release-test", release.id.unwrap(), attachment.id.unwrap() ) @@ -214,29 +230,40 @@ async fn repo() { "couldn't download attachment" ); let _zip_archive = api - .repo_get_archive("TestingAdmin", "test", "v1.0.zip") + .repo_get_archive("TestingAdmin", "release-test", "v1.0.zip") .await .unwrap(); let _tar_archive = api - .repo_get_archive("TestingAdmin", "test", "v1.0.tar.gz") + .repo_get_archive("TestingAdmin", "release-test", "v1.0.tar.gz") .await .unwrap(); // check these contents when their return value is fixed api.repo_delete_release_attachment( "TestingAdmin", - "test", + "release-test", release.id.unwrap(), attachment.id.unwrap(), ) .await .expect("failed to deleted attachment"); - api.repo_delete_release("TestingAdmin", "test", release.id.unwrap()) + api.repo_delete_release("TestingAdmin", "release-test", release.id.unwrap()) .await .expect("failed to delete release"); - api.repo_delete_tag("TestingAdmin", "test", "v1.0") + api.repo_delete_tag("TestingAdmin", "release-test", "v1.0") .await .expect("failed to delete release"); } + +#[tokio::test] +async fn delete_repo() { + let api = common::login(); + let git = Git::new("./test_repos/delete"); + let _ = basic_repo(&api, &git, "delete-test").await; + + api.repo_delete("TestingAdmin", "delete-test") + .await + .expect("failed to delete repo"); +} |