aboutsummaryrefslogtreecommitdiff
path: root/src/tests/mod.rs
blob: ca25c1f04cb0704406f4f83df52b3c772010f8db (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::path::{Path, PathBuf};

use crate::repo::*;

use anyhow::Result;
use tracing::debug;

const REPOS: [&str; 5] = [
    "repos/site/group/repo1",
    "repos/site/group/repo2/subrepo1",
    "repos/site/group/repo2",
    "repos/site/group/subgroup/repo3",
    "repos/site/group/subgroup/subsubgroup/repo4",
];

fn prepare_repos(path: &Path) -> Result<()> {
    REPOS.iter().try_for_each(|repo| {
        let path = build_path(path, repo);
        std::fs::create_dir_all(&path)?;
        let _repo = gix::init(&path)?;

        Ok::<(), anyhow::Error>(())
    })
}

fn build_path(path: &Path, repo: &str) -> PathBuf {
    let mut path = path.to_owned();
    path.push(repo);
    return path;
}

fn build_path_string(path: &Path, repo: &str) -> String {
    let mut path = path.to_owned();
    path.push(repo);
    return path.into_os_string().into_string().unwrap();
}

#[tokio::test]
async fn search_repos() -> Result<()> {
    tracing_subscriber::fmt::init();

    let test_dir = tempfile::tempdir()?;
    let test_path = test_dir.path();
    debug!("test files placed in: {:?}", test_dir.path());

    prepare_repos(test_path)?;

    let mut left: Vec<String> = vec![
        build_path_string(test_path, "repos/site/group/repo1"),
        build_path_string(test_path, "repos/site/group/repo2"),
        build_path_string(test_path, "repos/site/group/subgroup/repo3"),
        build_path_string(test_path, "repos/site/group/subgroup/subsubgroup/repo4"),
    ];
    let right = Repos::from_local(&build_path_string(test_path, "repos"), "");

    let mut right: Vec<&str> = right.iter().map(|x| x.0.as_str()).collect();

    assert_eq!(left.sort(), right.sort_unstable());

    Ok(())
}