summaryrefslogtreecommitdiff
path: root/server/common/src/tests.rs
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-02-04 23:57:03 +0100
committerJoel Klinghed <the_jk@spawned.biz>2025-02-04 23:57:03 +0100
commit3bee55ab1dd486c7218e2810d0b3b36edb272dc5 (patch)
tree97e54f7b8faef9b26bd0dcbfd57491b951858158 /server/common/src/tests.rs
parente68e405ed902a10f771fcd8bc4837cb4e2a8e878 (diff)
common: Add tests for git
Wanted to include a non-bare repo as well but git doesn't like one adding a non-bare git repo NOT as a submodule. And bare repos are the main code path anyway.
Diffstat (limited to 'server/common/src/tests.rs')
-rw-r--r--server/common/src/tests.rs225
1 files changed, 224 insertions, 1 deletions
diff --git a/server/common/src/tests.rs b/server/common/src/tests.rs
index 540ee2d..f08ca44 100644
--- a/server/common/src/tests.rs
+++ b/server/common/src/tests.rs
@@ -1,7 +1,10 @@
-use tokio::fs;
+use std::path::PathBuf;
use testdir::testdir;
+use tokio::fs;
+use tokio::sync::OnceCell;
use crate::fs_utils;
+use crate::git;
#[tokio::test]
async fn test_fs_utils_create_dir_allow_existing() {
@@ -43,3 +46,223 @@ async fn test_fs_utils_symlink_update_existing() {
assert!(fs_utils::symlink_update_existing(&fum, &bar).await.is_ok());
assert_eq!(fs::read_link(&bar).await.unwrap(), fum);
}
+
+static BARE: OnceCell<git::Repository> = OnceCell::const_new();
+
+async fn git_setup(bare: bool) -> git::Repository {
+ let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("src/testdata")
+ .join(if bare { "bare" } else { "work" });
+ let repo = git::Repository::new(
+ path.clone(),
+ bare,
+ None::<String>,
+ None::<String>,
+ None::<PathBuf>,
+ );
+ assert_eq!(repo.remote(), None);
+ assert_eq!(repo.project_id(), None);
+ assert_eq!(repo.path(), path);
+ assert_eq!(repo.socket(), None);
+ assert_eq!(repo.is_bare(), bare);
+ repo.setup().await.unwrap();
+ repo
+}
+
+async fn git_config_get(repo: &git::Repository) {
+ assert_eq!(repo.config_get("user.name").await.unwrap(), "John Smith");
+ assert_eq!(
+ repo.config_get("user.email").await.unwrap(),
+ "jsmith@example.org"
+ );
+ assert_eq!(repo.config_get("eyeballs.foo").await.unwrap(), "bar");
+ assert_eq!(repo.config_get("eyeballs.bar").await.unwrap(), "");
+}
+
+async fn git_is_ancestor(repo: &git::Repository) {
+ assert_eq!(
+ repo.is_ancestor(
+ "807fffaad7b35683162669a485ce3e995d56170d",
+ "d7c502b9c6b833060576a0c4da0287933d603011",
+ )
+ .await
+ .unwrap(),
+ true,
+ );
+ assert_eq!(
+ repo.is_ancestor(
+ "d7c502b9c6b833060576a0c4da0287933d603011",
+ "807fffaad7b35683162669a485ce3e995d56170d",
+ )
+ .await
+ .unwrap(),
+ false,
+ );
+ assert_eq!(
+ repo.is_ancestor(
+ "807fffaad7b35683162669a485ce3e995d56170d",
+ "807fffaad7b35683162669a485ce3e995d56170d",
+ )
+ .await
+ .unwrap(),
+ true,
+ );
+ assert_eq!(
+ repo.is_ancestor(
+ "2d05d489d42d4f36dd0ebf52502f243991e010eb",
+ "2cecdec660a30bf3964cee645d9cee03640ef8dc",
+ )
+ .await
+ .unwrap(),
+ false,
+ );
+ assert!(repo
+ .is_ancestor(
+ "1234567890123456789012345678901234567890",
+ "2cecdec660a30bf3964cee645d9cee03640ef8dc",
+ )
+ .await
+ .is_err(),);
+ assert!(repo.is_ancestor("<invalid>", "'\"",).await.is_err(),);
+}
+
+async fn git_is_equal_content(repo: &git::Repository) {
+ assert_eq!(
+ repo.is_equal_content(
+ "d7c502b9c6b833060576a0c4da0287933d603011",
+ "d7c502b9c6b833060576a0c4da0287933d603011",
+ )
+ .await
+ .unwrap(),
+ true,
+ );
+ assert_eq!(
+ repo.is_equal_content(
+ "d7c502b9c6b833060576a0c4da0287933d603011",
+ "2d05d489d42d4f36dd0ebf52502f243991e010eb",
+ )
+ .await
+ .unwrap(),
+ true,
+ );
+ assert_eq!(
+ repo.is_equal_content(
+ "d7c502b9c6b833060576a0c4da0287933d603011",
+ "2cecdec660a30bf3964cee645d9cee03640ef8dc",
+ )
+ .await
+ .unwrap(),
+ false,
+ );
+ assert!(repo
+ .is_equal_content(
+ "1234567890123456789012345678901234567890",
+ "2cecdec660a30bf3964cee645d9cee03640ef8dc",
+ )
+ .await
+ .is_err(),);
+ assert!(repo.is_equal_content("<invalid>", "'\"",).await.is_err(),);
+}
+
+async fn git_get_author_commiter(repo: &git::Repository) {
+ let a1 = repo
+ .get_author("d7c502b9c6b833060576a0c4da0287933d603011")
+ .await
+ .unwrap();
+ assert_eq!(a1.name, "John Smith");
+ assert_eq!(a1.email, "jsmith@example.org");
+ assert_eq!(a1.username, "jsmith");
+ let c1 = repo
+ .get_commiter("d7c502b9c6b833060576a0c4da0287933d603011")
+ .await
+ .unwrap();
+ assert_eq!(c1.name, a1.name);
+ assert_eq!(c1.email, a1.email);
+ assert_eq!(c1.username, a1.username);
+
+ let a2 = repo
+ .get_author("2cecdec660a30bf3964cee645d9cee03640ef8dc")
+ .await
+ .unwrap();
+ assert_eq!(a2.name, "Test");
+ assert_eq!(a2.email, "testing@example.org");
+ assert_eq!(a2.username, "testing");
+ let c2 = repo
+ .get_commiter("d7c502b9c6b833060576a0c4da0287933d603011")
+ .await
+ .unwrap();
+ assert_eq!(c2.name, c1.name);
+ assert_eq!(c2.email, c1.email);
+ assert_eq!(c2.username, c1.username);
+
+ assert!(repo
+ .get_author("1234567890123456789012345678901234567890")
+ .await
+ .is_err());
+ assert!(repo.get_author("<invalid>").await.is_err());
+}
+
+async fn git_fetch(bare: bool) {
+ let path = testdir!().join("repo");
+ let remote_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/testdata/bare");
+ let remote = remote_path.to_string_lossy().into_owned();
+ let repo = git::Repository::new(
+ path,
+ bare,
+ Some(remote.as_str()),
+ None::<String>,
+ None::<PathBuf>,
+ );
+ assert_eq!(repo.remote(), Some(remote.as_str()));
+ repo.setup().await.unwrap();
+ if bare {
+ let (main, branch, other) = tokio::join!(
+ repo.fetch("main"),
+ repo.fetch("branch"),
+ repo.fetch("other"),
+ );
+ main.unwrap();
+ branch.unwrap();
+ other.unwrap();
+ } else {
+ // Not bare repo will complain when you try to fetch into the currently checked
+ // out branch. So just try fetching other branches.
+ let (branch, other) = tokio::join!(repo.fetch("branch"), repo.fetch("other"));
+ branch.unwrap();
+ other.unwrap();
+ }
+}
+
+#[tokio::test]
+async fn test_git_bare_config_get() {
+ let repo = BARE.get_or_init(|| git_setup(true)).await;
+ git_config_get(repo).await;
+}
+
+#[tokio::test]
+async fn test_git_bare_is_ancestor() {
+ let repo = BARE.get_or_init(|| git_setup(true)).await;
+ git_is_ancestor(repo).await;
+}
+
+#[tokio::test]
+async fn test_git_bare_is_equal_content() {
+ let repo = BARE.get_or_init(|| git_setup(true)).await;
+ git_is_equal_content(repo).await;
+}
+
+#[tokio::test]
+async fn test_git_bare_get_author_commiter() {
+ let repo = BARE.get_or_init(|| git_setup(true)).await;
+ git_get_author_commiter(repo).await;
+}
+
+#[tokio::test]
+async fn test_git_fetch() {
+ git_fetch(false).await;
+}
+
+#[tokio::test]
+async fn test_git_bare_fetch() {
+ git_fetch(true).await;
+}