aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/repository/repository_manager_test.rs
blob: bb85d6a70a27afaa525034eca8993aa3001b9a50 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! ref: composer/tests/Composer/Test/Repository/RepositoryManagerTest.php

// These construct a RepositoryManager (which builds an HttpDownloader reaching
// curl_multi_init, todo!()) with a mocked IO/Config/EventDispatcher and exercise repo
// creation/prepending/wrapping.

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::repository::{ArrayRepository, RepositoryInterfaceHandle, RepositoryManager};
use shirabe::util::filesystem::Filesystem;
use shirabe::util::http_downloader::HttpDownloader;
use shirabe_php_shim::PhpMixed;
use tempfile::TempDir;

struct SetUp {
    tmpdir: TempDir,
}

fn set_up() -> SetUp {
    let tmpdir = TempDir::new().unwrap();
    SetUp { tmpdir }
}

fn tear_down(tmpdir: &std::path::Path) {
    if tmpdir.is_dir() {
        let mut fs = Filesystem::new(None);
        fs.remove_directory(tmpdir).unwrap();
    }
}

struct TearDown {
    tmpdir: std::path::PathBuf,
}

impl TearDown {
    fn new(tmpdir: std::path::PathBuf) -> Self {
        TearDown { tmpdir }
    }
}

impl Drop for TearDown {
    fn drop(&mut self) {
        tear_down(&self.tmpdir);
    }
}

fn null_io() -> Rc<RefCell<dyn IOInterface>> {
    Rc::new(RefCell::new(NullIO::new()))
}

fn http_downloader(io: &Rc<RefCell<dyn IOInterface>>) -> Rc<RefCell<HttpDownloader>> {
    let config = Rc::new(RefCell::new(Config::new(false, None)));
    Rc::new(RefCell::new(HttpDownloader::new(
        io.clone(),
        config,
        IndexMap::new(),
        true,
    )))
}

fn str_config(pairs: &[(&str, PhpMixed)]) -> IndexMap<String, PhpMixed> {
    let mut c: IndexMap<String, PhpMixed> = IndexMap::new();
    for (k, v) in pairs {
        c.insert(k.to_string(), v.clone());
    }
    c
}

#[test]
#[ignore]
fn test_prepend() {
    let SetUp { tmpdir } = set_up();
    let _tear_down = TearDown::new(tmpdir.path().to_path_buf());

    let io = null_io();
    let config = Rc::new(RefCell::new(Config::new(false, None)));
    let mut rm = RepositoryManager::new(io.clone(), config, http_downloader(&io), None, None);

    let repository1 = RepositoryInterfaceHandle::new(ArrayRepository::new(vec![]).unwrap());
    let repository2 = RepositoryInterfaceHandle::new(ArrayRepository::new(vec![]).unwrap());
    rm.add_repository(repository1.clone());
    rm.prepend_repository(repository2.clone());

    assert_eq!(&vec![repository2, repository1], rm.get_repositories());
}

#[test]
#[ignore]
fn test_repo_creation() {
    let SetUp { tmpdir } = set_up();
    let _tear_down = TearDown::new(tmpdir.path().to_path_buf());

    let io = null_io();
    let config = Rc::new(RefCell::new(Config::new(false, None)));
    let mut rm =
        RepositoryManager::new(io.clone(), config.clone(), http_downloader(&io), None, None);

    config.borrow_mut().merge(
        &str_config(&[(
            "config",
            PhpMixed::Array(str_config(&[(
                "cache-repo-dir",
                PhpMixed::String(tmpdir.path().to_string_lossy().to_string()),
            )])),
        )]),
        "unknown",
    );

    rm.set_repository_class("composer", "Composer\\Repository\\ComposerRepository");
    rm.set_repository_class("vcs", "Composer\\Repository\\VcsRepository");
    rm.set_repository_class("package", "Composer\\Repository\\PackageRepository");
    rm.set_repository_class("pear", "Composer\\Repository\\PearRepository");
    rm.set_repository_class("git", "Composer\\Repository\\VcsRepository");
    rm.set_repository_class("svn", "Composer\\Repository\\VcsRepository");
    rm.set_repository_class("perforce", "Composer\\Repository\\VcsRepository");
    rm.set_repository_class("hg", "Composer\\Repository\\VcsRepository");
    rm.set_repository_class("artifact", "Composer\\Repository\\ArtifactRepository");

    let cases: Vec<(&str, IndexMap<String, PhpMixed>)> = vec![
        (
            "composer",
            str_config(&[("url", PhpMixed::String("http://example.org".to_string()))]),
        ),
        (
            "vcs",
            str_config(&[(
                "url",
                PhpMixed::String("http://github.com/foo/bar".to_string()),
            )]),
        ),
        (
            "git",
            str_config(&[(
                "url",
                PhpMixed::String("http://github.com/foo/bar".to_string()),
            )]),
        ),
        (
            "git",
            str_config(&[(
                "url",
                PhpMixed::String("git@example.org:foo/bar.git".to_string()),
            )]),
        ),
        (
            "svn",
            str_config(&[(
                "url",
                PhpMixed::String("svn://example.org/foo/bar".to_string()),
            )]),
        ),
        (
            "package",
            str_config(&[("package", PhpMixed::Array(IndexMap::new()))]),
        ),
        (
            "artifact",
            str_config(&[("url", PhpMixed::String("/path/to/zips".to_string()))]),
        ),
    ];

    for (r#type, options) in cases {
        rm.create_repository(
            "composer",
            str_config(&[("url", PhpMixed::String("http://example.org".to_string()))]),
            None,
        )
        .unwrap();
        rm.create_repository(r#type, options, None).unwrap();
    }
}

#[test]
#[ignore]
fn test_invalid_repo_creation_throws() {
    let SetUp { tmpdir } = set_up();
    let _tear_down = TearDown::new(tmpdir.path().to_path_buf());

    let io = null_io();
    let config = Rc::new(RefCell::new(Config::new(false, None)));
    let rm = RepositoryManager::new(io.clone(), config.clone(), http_downloader(&io), None, None);

    config.borrow_mut().merge(
        &str_config(&[(
            "config",
            PhpMixed::Array(str_config(&[(
                "cache-repo-dir",
                PhpMixed::String(tmpdir.path().to_string_lossy().to_string()),
            )])),
        )]),
        "unknown",
    );

    let cases: Vec<(&str, IndexMap<String, PhpMixed>)> = vec![
        (
            "pear",
            str_config(&[(
                "url",
                PhpMixed::String("http://pear.example.org/foo".to_string()),
            )]),
        ),
        ("invalid", IndexMap::new()),
    ];

    for (r#type, options) in cases {
        assert!(rm.create_repository(r#type, options, None).is_err());
    }
}

#[test]
#[ignore = "PathRepository does not implement RepositoryInterface (only ConfigurableRepositoryInterface), so it cannot live inside a RepositoryInterfaceHandle nor be recovered via as_any().downcast_ref::<PathRepository>(); the assertInstanceOf(PathRepository) check is unportable"]
fn test_filter_repo_wrapping() {
    let SetUp { tmpdir } = set_up();
    let _tear_down = TearDown::new(tmpdir.path().to_path_buf());
    todo!()
}