aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/util/svn_test.rs
blob: 7a30d8b3797b5cb7ccad72d27ac983c657dcaeb1 (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
//! ref: composer/tests/Composer/Test/Util/SvnTest.php

use std::cell::RefCell;
use std::rc::Rc;

use indexmap::IndexMap;
use shirabe::config::Config;
use shirabe::io::IOInterface;
use shirabe::io::null_io::NullIO;
use shirabe::util::svn::Svn;
use shirabe_php_shim::PhpMixed;

fn map(pairs: Vec<(&str, PhpMixed)>) -> IndexMap<String, PhpMixed> {
    pairs.into_iter().map(|(k, v)| (k.to_string(), v)).collect()
}

/// Builds a `['config' => ['http-basic' => [host => ['username' => .., 'password' => ..]]]]`
/// map for `Config::merge`.
fn http_basic_config(host: &str, username: &str, password: &str) -> IndexMap<String, PhpMixed> {
    let creds = map(vec![
        ("username", PhpMixed::String(username.to_string())),
        ("password", PhpMixed::String(password.to_string())),
    ]);
    let http_basic = map(vec![(host, PhpMixed::Array(creds))]);
    let config = map(vec![("http-basic", PhpMixed::Array(http_basic))]);
    map(vec![("config", PhpMixed::Array(config))])
}

/// ref: SvnTest::urlProvider
fn url_provider() -> Vec<(&'static str, Vec<&'static str>)> {
    vec![
        (
            "http://till:test@svn.example.org/",
            vec!["--username", "till", "--password", "test"],
        ),
        ("http://svn.apache.org/", vec![]),
        (
            "svn://johndoe@example.org",
            vec!["--username", "johndoe", "--password", ""],
        ),
    ]
}

#[test]
fn test_credentials() {
    for (url, expect) in url_provider() {
        let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
        let config = Rc::new(RefCell::new(Config::new(true, None)));
        let mut svn = Svn::new(url.to_string(), io, config, None);

        let expect: Vec<String> = expect.iter().map(|s| s.to_string()).collect();
        assert_eq!(expect, svn.__get_credential_args());
    }
}

#[test]
fn test_interactive_string() {
    let url = "http://svn.example.org";

    let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
    let config = Rc::new(RefCell::new(Config::new(true, None)));
    let mut svn = Svn::new(url.to_string(), io, config, None);

    assert_eq!(
        vec![
            "svn".to_string(),
            "ls".to_string(),
            "--non-interactive".to_string(),
            "--".to_string(),
            "http://svn.example.org".to_string(),
        ],
        svn.__get_command(vec!["svn".to_string(), "ls".to_string()], url, None)
    );
}

#[test]
fn test_credentials_from_config() {
    let url = "http://svn.apache.org";

    let mut config = Config::new(true, None);
    config.merge(&http_basic_config("svn.apache.org", "foo", "bar"), "test");

    let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
    let mut svn = Svn::new(url.to_string(), io, Rc::new(RefCell::new(config)), None);

    assert_eq!(
        vec![
            "--username".to_string(),
            "foo".to_string(),
            "--password".to_string(),
            "bar".to_string(),
        ],
        svn.__get_credential_args()
    );
}

#[test]
fn test_credentials_from_config_with_cache_credentials_true() {
    let url = "http://svn.apache.org";

    let mut config = Config::new(true, None);
    config.merge(&http_basic_config("svn.apache.org", "foo", "bar"), "test");

    let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
    let mut svn = Svn::new(url.to_string(), io, Rc::new(RefCell::new(config)), None);
    svn.set_cache_credentials(true);

    assert_eq!(
        vec![
            "--username".to_string(),
            "foo".to_string(),
            "--password".to_string(),
            "bar".to_string(),
        ],
        svn.__get_credential_args()
    );
}

#[test]
fn test_credentials_from_config_with_cache_credentials_false() {
    let url = "http://svn.apache.org";

    let mut config = Config::new(true, None);
    config.merge(&http_basic_config("svn.apache.org", "foo", "bar"), "test");

    let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
    let mut svn = Svn::new(url.to_string(), io, Rc::new(RefCell::new(config)), None);
    svn.set_cache_credentials(false);

    assert_eq!(
        vec![
            "--no-auth-cache".to_string(),
            "--username".to_string(),
            "foo".to_string(),
            "--password".to_string(),
            "bar".to_string(),
        ],
        svn.__get_credential_args()
    );
}