summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2024-12-25 01:04:07 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2024-12-25 01:04:07 +0200
commit2c38482061c5f9f4485c421f3faae92e49afcd2a (patch)
tree8f76ab334af3e630d4be0df955e56f830ceee831
parent59319c4a0127766028aaf6f8404e0d3e239beba2 (diff)
downloadgit_service-2c38482061c5f9f4485c421f3faae92e49afcd2a.tar.gz
git_service-2c38482061c5f9f4485c421f3faae92e49afcd2a.tar.bz2
git_service-2c38482061c5f9f4485c421f3faae92e49afcd2a.zip
test: add some unit testsHEADmaster
test_config_read: tests if config can be read normally test_make_repo: checks if repo has been created and if cgitrepos updated
-rw-r--r--resources/test/config.test.0.json11
-rw-r--r--resources/test/config.test.1.json11
-rw-r--r--resources/test/testcgitrepos12
-rw-r--r--resources/test/testcgitrepos.1.test15
-rw-r--r--src/test.rs61
5 files changed, 110 insertions, 0 deletions
diff --git a/resources/test/config.test.0.json b/resources/test/config.test.0.json
new file mode 100644
index 0000000..f7c3851
--- /dev/null
+++ b/resources/test/config.test.0.json
@@ -0,0 +1,11 @@
+{
+ "authorized_owners": [
+ {
+ "name": "johndoe",
+ "email": "eg@example.com",
+ "gpg_pubkey_id": "XXXXXXXXXXXXXXXX"
+ }
+ ],
+ "cgitrepos_file_path": "/path/to/cgitrepos",
+ "git_host_dir_path": "/path/to/git/host/dir"
+}
diff --git a/resources/test/config.test.1.json b/resources/test/config.test.1.json
new file mode 100644
index 0000000..798491a
--- /dev/null
+++ b/resources/test/config.test.1.json
@@ -0,0 +1,11 @@
+{
+ "authorized_owners": [
+ {
+ "name": "johndoe",
+ "email": "eg@example.com",
+ "gpg_pubkey_id": "XXXXXXXXXXXXXXXX"
+ }
+ ],
+ "cgitrepos_file_path": "$TEST_CGITREPOS_PATH$",
+ "git_host_dir_path": "$TEST_GIT_REPOS_DIR_PATH$"
+}
diff --git a/resources/test/testcgitrepos b/resources/test/testcgitrepos
new file mode 100644
index 0000000..755ff35
--- /dev/null
+++ b/resources/test/testcgitrepos
@@ -0,0 +1,12 @@
+section=my_code_section
+repo.url=mock_repo_1
+repo.path=/path/to/mock_repo_1
+repo.desc=foo
+repo.owner=bar
+
+section=put_test_repo_here
+repo.url=mock_repo_1
+repo.path=/path/to/mock_repo_1
+repo.desc=foo
+repo.owner=bar
+
diff --git a/resources/test/testcgitrepos.1.test b/resources/test/testcgitrepos.1.test
new file mode 100644
index 0000000..9d7ee8a
--- /dev/null
+++ b/resources/test/testcgitrepos.1.test
@@ -0,0 +1,15 @@
+section=my_code_section
+repo.url=mock_repo_1
+repo.path=/path/to/mock_repo_1
+repo.desc=foo
+repo.owner=bar
+section=put_test_repo_here
+repo.url=mock_repo_1
+repo.path=/path/to/mock_repo_1
+repo.desc=foo
+repo.owner=bar
+section=custom
+repo.url=test
+repo.path=/home/stvnliu/Development/git_service/resources/test/put_git_repo_here/test.git
+repo.desc=test description
+repo.owner=testuser \ No newline at end of file
diff --git a/src/test.rs b/src/test.rs
new file mode 100644
index 0000000..68212c3
--- /dev/null
+++ b/src/test.rs
@@ -0,0 +1,61 @@
+use std::{
+ fs::{self, read_to_string},
+};
+
+use super::*;
+
+macro_rules! test_case {
+ ($fname:expr) => {
+ concat!(env!("CARGO_MANIFEST_DIR"), "/resources/test", $fname) // assumes Linux ('/')!
+ };
+}
+#[test]
+pub fn test_config_read() {
+ let config = read_config::get_config(test_case!("/config.test.0.json").to_string());
+ assert_eq!(config.git_host_dir_path, "/path/to/git/host/dir");
+ assert_eq!(config.cgitrepos_file_path, "/path/to/cgitrepos");
+ assert_eq!(config.authorized_owners.len(), 1);
+ let test_owner = &config.authorized_owners[0];
+ assert_eq!(test_owner.name, "johndoe");
+
+ assert_eq!(test_owner.email, "eg@example.com");
+
+ assert_eq!(test_owner.gpg_pubkey_id, "XXXXXXXXXXXXXXXX");
+}
+
+#[test]
+pub fn test_make_repository() {
+ let mut config = read_config::get_config(test_case!("/config.test.1.json").to_string());
+
+ fs::create_dir(test_case!("/put_git_repo_here")).expect("cannot create test directory");
+ fs::copy(test_case!("/testcgitrepos"), test_case!("/testcgitrepos.1"))
+ .expect("cannot duplicate test cgitrepos file");
+ let test_git_path = test_case!("/put_git_repo_here").to_string();
+
+ config.cgitrepos_file_path = test_case!("/testcgitrepos.1").to_string();
+ config.git_host_dir_path = test_git_path;
+ make_repo_request_handler(
+ config,
+ MakeRepoRequest {
+ repo_name: "test".to_string(),
+ author: "testuser".to_string(),
+ section: "custom".to_string(),
+ description: "test description".to_string(),
+ },
+ );
+ let new_file = read_to_string(test_case!("/testcgitrepos.1")).expect("read new file error");
+ let test_file =
+ read_to_string(test_case!("/testcgitrepos.1.test")).expect("read old file error");
+ assert_ne!(
+ fs::read_dir(test_case!("/put_git_repo_here/test.git"))
+ .into_iter()
+ .len(),
+ 0
+ );
+ assert_eq!(new_file, test_file);
+ // cleanup
+ fs::remove_dir_all(test_case!("/put_git_repo_here"))
+ .expect("expecting cleanup to run correctly");
+ fs::remove_file(test_case!("/testcgitrepos.1"))
+ .expect("expecting temp file to be cleaned up correctly");
+}