aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/common/downloader_stub.rs
blob: 0c4432b2347ba730395dae9c18c4ccce15343881 (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
//! No-op DownloaderInterface stub for installer tests.
//!
//! PHP mocks `Composer\Downloader\DownloadManager` directly and asserts its
//! `install`/`update`/`remove` calls. The Rust DownloadManager is a concrete type
//! that dispatches to a registered DownloaderInterface, so the equivalent stub
//! lives one level down: a downloader registered under some dist type that records
//! the calls it receives and resolves to null, the same way the PHP mock returns
//! `\React\Promise\resolve(null)`.
#![allow(dead_code)]

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

use shirabe::downloader::DownloaderInterface;
use shirabe::package::PackageInterfaceHandle;
use shirabe_php_shim::PhpMixed;

/// One recorded downloader operation, capturing the package pretty-names and the
/// target path so tests can assert what the LibraryInstaller forwarded.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DownloaderCall {
    Install {
        package: String,
        path: String,
    },
    Update {
        initial: String,
        target: String,
        path: String,
    },
    Remove {
        package: String,
        path: String,
    },
}

#[derive(Debug, Default)]
pub struct DownloaderStub {
    calls: Rc<RefCell<Vec<DownloaderCall>>>,
}

impl DownloaderStub {
    pub fn new() -> Self {
        Self::default()
    }

    /// Shared handle to the recorded call log, so tests can inspect it after the
    /// stub has been moved into the DownloadManager.
    pub fn calls(&self) -> Rc<RefCell<Vec<DownloaderCall>>> {
        self.calls.clone()
    }
}

#[async_trait::async_trait(?Send)]
impl DownloaderInterface for DownloaderStub {
    fn get_installation_source(&self) -> String {
        "dist".to_string()
    }

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

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

    async fn install(
        &mut self,
        package: PackageInterfaceHandle,
        path: &str,
        _output: bool,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.calls.borrow_mut().push(DownloaderCall::Install {
            package: package.get_pretty_name(),
            path: path.to_string(),
        });
        Ok(None)
    }

    async fn update(
        &mut self,
        initial: PackageInterfaceHandle,
        target: PackageInterfaceHandle,
        path: &str,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.calls.borrow_mut().push(DownloaderCall::Update {
            initial: initial.get_pretty_name(),
            target: target.get_pretty_name(),
            path: path.to_string(),
        });
        Ok(None)
    }

    async fn remove(
        &mut self,
        package: PackageInterfaceHandle,
        path: &str,
        _output: bool,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.calls.borrow_mut().push(DownloaderCall::Remove {
            package: package.get_pretty_name(),
            path: path.to_string(),
        });
        Ok(None)
    }

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