summaryrefslogtreecommitdiff
path: root/src/test.rs
blob: 68212c3007097b74f86038bfa7e0899f5819ad50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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");
}