aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/installer/installation_manager_test.rs
blob: 01a751310e482c4b4813c6ccd98e273b1eab7c4e (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! ref: composer/tests/Composer/Test/Installer/InstallationManagerTest.php

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

use shirabe::dependency_resolver::operation::{
    InstallOperation, UninstallOperation, UpdateOperation,
};
use shirabe::installer::{BinaryPresenceInterface, InstallerInterface};
use shirabe::io::IOInterface;
use shirabe::io::null_io::NullIO;
use shirabe::package::PackageInterfaceHandle;
use shirabe::package::handle::CompletePackageHandle;
use shirabe::repository::{InstalledArrayRepository, InstalledRepositoryInterface};
use shirabe::util::http_downloader::HttpDownloader;
use shirabe::util::r#loop::Loop;
use shirabe_php_shim::PhpMixed;
use shirabe_semver::VersionParser;

use crate::test_case::get_package;

fn run<F: std::future::Future>(future: F) -> F::Output {
    tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap()
        .block_on(future)
}

/// ref: setUp(): the PHP loop/io mocks are never exercised by these tests (the loop has its
/// constructor disabled), so a real Loop over a real HttpDownloader and a NullIO stand in.
struct SetUp {
    loop_: Rc<RefCell<Loop>>,
    io: Rc<RefCell<dyn IOInterface>>,
}

fn set_up() -> SetUp {
    let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
    let config = Rc::new(RefCell::new(shirabe::config::Config::new(false, None)));
    // The PHP loop mock has its constructor disabled and is never exercised by these tests, so a
    // mock HttpDownloader (no real curl backend) stands in.
    let http_downloader = Rc::new(RefCell::new(HttpDownloader::__new_mock(io.clone(), config)));
    let loop_ = Rc::new(RefCell::new(Loop::new(http_downloader, None)));
    SetUp { loop_, io }
}

/// Records of the calls a `CountingInstaller` received, shared so the test can inspect them
/// after the installer has been moved into the InstallationManager. This reproduces PHPUnit's
/// `expects($this->exactly(n))->method(...)->with(...)` assertions with explicit counters.
#[derive(Debug, Default)]
struct InstallerCalls {
    supports_args: Vec<String>,
    install: Vec<PackageInterfaceHandle>,
    uninstall: Vec<PackageInterfaceHandle>,
    update: Vec<(PackageInterfaceHandle, PackageInterfaceHandle)>,
}

/// Configurable `InstallerInterface` stub, equivalent to
/// `getMockBuilder(InstallerInterface::class)->getMock()`. `supports` returns true only when the
/// requested type equals `supported_type` (PHP uses a returnCallback comparing `$arg === 'vendor'`),
/// and every method records its arguments into the shared `calls`.
#[derive(Debug)]
struct CountingInstaller {
    supported_type: String,
    calls: Rc<RefCell<InstallerCalls>>,
}

impl CountingInstaller {
    fn new(supported_type: &str) -> (Self, Rc<RefCell<InstallerCalls>>) {
        let calls = Rc::new(RefCell::new(InstallerCalls::default()));
        (
            Self {
                supported_type: supported_type.to_string(),
                calls: calls.clone(),
            },
            calls,
        )
    }
}

#[async_trait::async_trait(?Send)]
impl InstallerInterface for CountingInstaller {
    fn supports(&self, package_type: &str) -> bool {
        self.calls
            .borrow_mut()
            .supports_args
            .push(package_type.to_string());
        package_type == self.supported_type
    }

    fn is_installed(
        &mut self,
        _repo: &dyn InstalledRepositoryInterface,
        _package: PackageInterfaceHandle,
    ) -> bool {
        false
    }

    async fn download(
        &mut self,
        _package: PackageInterfaceHandle,
        _prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn prepare(
        &mut self,
        _type: &str,
        _package: PackageInterfaceHandle,
        _prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn install(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        package: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.calls.borrow_mut().install.push(package);
        Ok(None)
    }

    async fn update(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        initial: PackageInterfaceHandle,
        target: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.calls.borrow_mut().update.push((initial, target));
        Ok(None)
    }

    async fn uninstall(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        package: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.calls.borrow_mut().uninstall.push(package);
        Ok(None)
    }

    async fn cleanup(
        &mut self,
        _type: &str,
        _package: PackageInterfaceHandle,
        _prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    fn get_install_path(&mut self, _package: PackageInterfaceHandle) -> Option<String> {
        None
    }
}

/// Records the calls a `BinaryInstaller` stub received. Standing in for the partial mock of
/// `LibraryInstaller` that PHP's `testInstallBinary` builds (only `supports`/`ensureBinariesPresence`
/// are exercised).
#[derive(Debug, Default)]
struct BinaryInstallerCalls {
    supports_args: Vec<String>,
    ensure_binaries_presence: Vec<PackageInterfaceHandle>,
}

#[derive(Debug)]
struct BinaryInstaller {
    calls: Rc<RefCell<BinaryInstallerCalls>>,
}

impl BinaryInstaller {
    fn new() -> (Self, Rc<RefCell<BinaryInstallerCalls>>) {
        let calls = Rc::new(RefCell::new(BinaryInstallerCalls::default()));
        (
            Self {
                calls: calls.clone(),
            },
            calls,
        )
    }
}

#[async_trait::async_trait(?Send)]
impl InstallerInterface for BinaryInstaller {
    fn supports(&self, package_type: &str) -> bool {
        self.calls
            .borrow_mut()
            .supports_args
            .push(package_type.to_string());
        package_type == "library"
    }

    fn is_installed(
        &mut self,
        _repo: &dyn InstalledRepositoryInterface,
        _package: PackageInterfaceHandle,
    ) -> bool {
        false
    }

    async fn download(
        &mut self,
        _package: PackageInterfaceHandle,
        _prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn prepare(
        &mut self,
        _type: &str,
        _package: PackageInterfaceHandle,
        _prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn install(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        _package: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn update(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        _initial: PackageInterfaceHandle,
        _target: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn uninstall(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        _package: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    async fn cleanup(
        &mut self,
        _type: &str,
        _package: PackageInterfaceHandle,
        _prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Ok(None)
    }

    fn get_install_path(&mut self, _package: PackageInterfaceHandle) -> Option<String> {
        None
    }

    fn as_binary_presence_interface(&mut self) -> Option<&mut dyn BinaryPresenceInterface> {
        Some(self)
    }
}

impl BinaryPresenceInterface for BinaryInstaller {
    fn ensure_binaries_presence(&mut self, package: PackageInterfaceHandle) {
        self.calls
            .borrow_mut()
            .ensure_binaries_presence
            .push(package);
    }
}

/// Builds a `CompletePackage` of the given type, mirroring TestCase::getPackage but with a custom
/// type set via `__set_type` (PackageInterfaceHandle does not expose the setter).
fn typed_package(name: &str, version: &str, r#type: &str) -> PackageInterfaceHandle {
    let norm_version = VersionParser.normalize(version, None).unwrap();
    let handle = CompletePackageHandle::new(name.to_string(), norm_version, version.to_string());
    handle.__set_type(r#type.to_string());
    handle.into()
}

fn same_handle(a: &PackageInterfaceHandle, b: &PackageInterfaceHandle) -> bool {
    Rc::ptr_eq(a.as_rc(), b.as_rc())
}

#[test]
fn test_add_get_installer() {
    let set_up = set_up();
    let (installer, calls) = CountingInstaller::new("vendor");

    let mut manager =
        shirabe::installer::InstallationManager::new(set_up.loop_.clone(), set_up.io.clone(), None);

    manager.add_installer(Box::new(installer));
    assert!(manager.get_installer("vendor").is_ok());

    assert!(manager.get_installer("unregistered").is_err());

    // PHP expects supports() to be called exactly twice (once for the cached 'vendor' lookup,
    // once for the failing 'unregistered' lookup).
    assert_eq!(calls.borrow().supports_args.len(), 2);
}

#[ignore = "removeInstaller compares installers by object identity, but add_installer moves the Box<dyn InstallerInterface> into the manager, leaving no &dyn reference to pass back to remove_installer; faithful reproduction needs a shared-ownership installer registry"]
#[test]
fn test_add_remove_installer() {
    todo!()
}

#[ignore = "partial mock of InstallationManager (onlyMethods install/update/uninstall) with expects(once)->with(...) is not reproducible without method-overriding mocks; execute() also takes the batched download path"]
#[test]
fn test_execute() {
    todo!()
}

#[test]
fn test_install() {
    let set_up = set_up();
    let (installer, calls) = CountingInstaller::new("library");
    let mut manager =
        shirabe::installer::InstallationManager::new(set_up.loop_.clone(), set_up.io.clone(), None);
    manager.add_installer(Box::new(installer));

    let package = get_package("test/pkg", "1.0.0");
    let operation = InstallOperation::new(package.clone());

    let mut repository = InstalledArrayRepository::new().unwrap();
    // install() returns the (empty) installer promise; the call is observed via the recorded args.
    run(manager.install(&mut repository, &operation));

    assert_eq!(calls.borrow().supports_args, vec!["library".to_string()]);
    assert_eq!(calls.borrow().install.len(), 1);
    assert!(same_handle(&calls.borrow().install[0], &package));
}

#[test]
fn test_update_with_equal_types() {
    let set_up = set_up();
    let (installer, calls) = CountingInstaller::new("library");
    let mut manager =
        shirabe::installer::InstallationManager::new(set_up.loop_.clone(), set_up.io.clone(), None);
    manager.add_installer(Box::new(installer));

    let initial = get_package("test/initial", "1.0.0");
    let target = get_package("test/target", "1.0.1");
    let operation = UpdateOperation::new(initial.clone(), target.clone());

    let mut repository = InstalledArrayRepository::new().unwrap();
    run(manager.update(&mut repository, &operation));

    assert_eq!(calls.borrow().supports_args, vec!["library".to_string()]);
    assert_eq!(calls.borrow().update.len(), 1);
    assert!(same_handle(&calls.borrow().update[0].0, &initial));
    assert!(same_handle(&calls.borrow().update[0].1, &target));
}

#[test]
fn test_update_with_not_equal_types() {
    let set_up = set_up();
    let (lib_installer, lib_calls) = CountingInstaller::new("library");
    let (bundle_installer, bundle_calls) = CountingInstaller::new("bundles");
    let mut manager =
        shirabe::installer::InstallationManager::new(set_up.loop_.clone(), set_up.io.clone(), None);
    manager.add_installer(Box::new(lib_installer));
    manager.add_installer(Box::new(bundle_installer));

    let initial = typed_package("test/initial", "1.0.0", "library");
    let target = typed_package("test/target", "1.0.1", "bundles");
    let operation = UpdateOperation::new(initial.clone(), target.clone());

    let mut repository = InstalledArrayRepository::new().unwrap();
    run(manager.update(&mut repository, &operation));

    // The lib installer uninstalls the initial package once.
    assert_eq!(lib_calls.borrow().uninstall.len(), 1);
    assert!(same_handle(&lib_calls.borrow().uninstall[0], &initial));

    // The bundle installer installs the target package once.
    assert_eq!(bundle_calls.borrow().install.len(), 1);
    assert!(same_handle(&bundle_calls.borrow().install[0], &target));
}

#[test]
fn test_uninstall() {
    let set_up = set_up();
    let (installer, calls) = CountingInstaller::new("library");
    let mut manager =
        shirabe::installer::InstallationManager::new(set_up.loop_.clone(), set_up.io.clone(), None);
    manager.add_installer(Box::new(installer));

    let package = get_package("test/pkg", "1.0.0");
    let operation = UninstallOperation::new(package.clone());

    let mut repository = InstalledArrayRepository::new().unwrap();
    run(manager.uninstall(&mut repository, &operation));

    assert_eq!(calls.borrow().supports_args, vec!["library".to_string()]);
    assert_eq!(calls.borrow().uninstall.len(), 1);
    assert!(same_handle(&calls.borrow().uninstall[0], &package));
}

#[test]
fn test_install_binary() {
    let set_up = set_up();
    let (installer, calls) = BinaryInstaller::new();
    let mut manager =
        shirabe::installer::InstallationManager::new(set_up.loop_.clone(), set_up.io.clone(), None);
    manager.add_installer(Box::new(installer));

    let package = get_package("test/pkg", "1.0.0");
    manager.ensure_binaries_presence(package.clone());

    assert_eq!(calls.borrow().supports_args, vec!["library".to_string()]);
    assert_eq!(calls.borrow().ensure_binaries_presence.len(), 1);
    assert!(same_handle(
        &calls.borrow().ensure_binaries_presence[0],
        &package
    ));
}