summaryrefslogtreecommitdiffstats
path: root/vendor/gix-submodule/tests/file/baseline.rs
blob: dc71ed3abe12f3800eebc9905c0520e4208d3caa (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::{ffi::OsStr, path::PathBuf};

use bstr::ByteSlice;
use gix_features::fs::walkdir::Parallelism;

#[test]
fn common_values_and_names_by_path() -> crate::Result {
    let modules = module_files()
        .map(|(path, stripped)| {
            gix_submodule::File::from_bytes(&std::fs::read(path).unwrap(), stripped, &Default::default())
        })
        .collect::<Result<Vec<_>, _>>()?;

    assert_eq!(
        modules
            .iter()
            .map(|m| { m.config_path().expect("present").to_owned() })
            .collect::<Vec<_>>(),
        [
            "empty-clone/.gitmodules",
            "multiple/.gitmodules",
            "not-a-submodule/.gitmodules",
            "recursive-clone/.gitmodules",
            "recursive-clone/submodule/.gitmodules",
            "relative-clone/.gitmodules",
            "relative-clone/submodule/.gitmodules",
            "super/.gitmodules",
            "super/submodule/.gitmodules",
            "super-clone/.gitmodules",
            "super-clone/submodule/.gitmodules",
            "top-only-clone/.gitmodules"
        ]
        .into_iter()
        .map(PathBuf::from)
        .collect::<Vec<_>>(),
        "config_path() yields the path provided when instantiating (for .gitmodules), and not the path of a submodule."
    );

    assert_eq!(
        {
            let mut v = modules.iter().flat_map(gix_submodule::File::names).collect::<Vec<_>>();
            v.sort();
            v.dedup();
            v
        },
        [".a/..c", "a/b", "a/d\\", "a\\e", "submodule"]
            .into_iter()
            .map(|n| n.as_bytes().as_bstr())
            .collect::<Vec<_>>(),
        "names can be iterated"
    );

    for module in &modules {
        for name in module.names() {
            let path = module.path(name)?;
            assert_eq!(module.name_by_path(path.as_ref()).expect("found"), name);
        }
    }
    Ok(())
}

fn module_files() -> impl Iterator<Item = (PathBuf, PathBuf)> {
    let dir = gix_testtools::scripted_fixture_read_only("basic.sh").expect("valid fixture");
    gix_features::fs::walkdir_sorted_new(&dir, Parallelism::Serial)
        .follow_links(false)
        .into_iter()
        .filter_map(move |entry| {
            let entry = entry.unwrap();
            (entry.file_name() == OsStr::new(".gitmodules")).then(|| {
                (
                    entry.path().to_owned(),
                    entry
                        .path()
                        .strip_prefix(&dir)
                        .expect("can only provide sub-dirs")
                        .to_owned(),
                )
            })
        })
}