summaryrefslogtreecommitdiffstats
path: root/vendor/gix-protocol/src/command/tests.rs
blob: 12c0eb6df0e27875a4a17afc1b98d8d1626b59ce (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
mod v1 {
    fn capabilities(input: &str) -> gix_transport::client::Capabilities {
        gix_transport::client::Capabilities::from_bytes(format!("\0{input}").as_bytes())
            .expect("valid input capabilities")
            .0
    }

    const GITHUB_CAPABILITIES: &str = "multi_ack thin-pack side-band ofs-delta shallow deepen-since deepen-not deepen-relative no-progress include-tag allow-tip-sha1-in-want allow-reachable-sha1-in-want no-done symref=HEAD:refs/heads/main filter agent=git/github-gdf51a71f0236";
    mod fetch {
        mod default_features {
            use crate::{
                command::tests::v1::{capabilities, GITHUB_CAPABILITIES},
                Command,
            };

            #[test]
            fn it_chooses_the_best_multi_ack_and_sideband() {
                assert_eq!(
                    Command::Fetch.default_features(
                        gix_transport::Protocol::V1,
                        &capabilities("multi_ack side-band side-band-64k multi_ack_detailed")
                    ),
                    &[("side-band-64k", None), ("multi_ack_detailed", None),]
                );
            }

            #[test]
            fn it_chooses_all_supported_non_stacking_capabilities_and_leaves_no_progress() {
                assert_eq!(
                    Command::Fetch.default_features(gix_transport::Protocol::V1, &capabilities(GITHUB_CAPABILITIES)),
                    &[
                        ("multi_ack", None),
                        ("thin-pack", None),
                        ("side-band", None),
                        ("ofs-delta", None),
                        ("shallow", None),
                        ("deepen-since", None),
                        ("deepen-not", None),
                        ("deepen-relative", None),
                        ("include-tag", None),
                        ("allow-tip-sha1-in-want", None),
                        ("allow-reachable-sha1-in-want", None),
                        ("no-done", None),
                        ("filter", None),
                    ],
                    "we don't enforce no-progress"
                );
            }
        }
    }
}

mod v2 {
    use gix_transport::client::Capabilities;

    fn capabilities(command: &str, input: &str) -> Capabilities {
        Capabilities::from_lines(format!("version 2\n{command}={input}").into())
            .expect("valid input for V2 capabilities")
    }

    mod fetch {
        mod default_features {
            use crate::{command::tests::v2::capabilities, Command};

            #[test]
            fn all_features() {
                assert_eq!(
                    Command::Fetch.default_features(
                        gix_transport::Protocol::V2,
                        &capabilities("fetch", "shallow filter ref-in-want sideband-all packfile-uris")
                    ),
                    ["shallow", "filter", "ref-in-want", "sideband-all", "packfile-uris"]
                        .iter()
                        .map(|s| (*s, None))
                        .collect::<Vec<_>>()
                )
            }
        }

        mod initial_arguments {
            use bstr::ByteSlice;

            use crate::{command::tests::v2::capabilities, Command};

            #[test]
            fn for_all_features() {
                assert_eq!(
                    Command::Fetch.initial_arguments(&Command::Fetch.default_features(
                        gix_transport::Protocol::V2,
                        &capabilities("fetch", "shallow filter sideband-all packfile-uris")
                    )),
                    ["thin-pack", "ofs-delta", "sideband-all"]
                        .iter()
                        .map(|s| s.as_bytes().as_bstr().to_owned())
                        .collect::<Vec<_>>(),
                    "packfile-uris isn't really supported that well and we don't support it either yet"
                )
            }
        }
    }

    mod ls_refs {
        mod default_features {
            use crate::{command::tests::v2::capabilities, Command};

            #[test]
            fn default_as_there_are_no_features() {
                assert_eq!(
                    Command::LsRefs.default_features(
                        gix_transport::Protocol::V2,
                        &capabilities("something-else", "does not matter as there are none")
                    ),
                    &[]
                );
            }
        }

        mod validate {
            use bstr::ByteSlice;

            use crate::{command::tests::v2::capabilities, Command};

            #[test]
            fn ref_prefixes_can_always_be_used() {
                Command::LsRefs.validate_argument_prefixes_or_panic(
                    gix_transport::Protocol::V2,
                    &capabilities("something else", "do-not-matter"),
                    &[b"ref-prefix hello/".as_bstr().into()],
                    &[],
                );
            }

            #[test]
            #[should_panic]
            fn unknown_argument() {
                Command::LsRefs.validate_argument_prefixes_or_panic(
                    gix_transport::Protocol::V2,
                    &capabilities("other", "do-not-matter"),
                    &[b"definitely-nothing-we-know".as_bstr().into()],
                    &[],
                );
            }

            #[test]
            #[should_panic]
            fn unknown_feature() {
                Command::LsRefs.validate_argument_prefixes_or_panic(
                    gix_transport::Protocol::V2,
                    &capabilities("other", "do-not-matter"),
                    &[],
                    &[("some-feature-that-does-not-exist", None)],
                );
            }
        }
    }
}