aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/common/io_mock.rs
blob: 7b932a872e1c195169e221b3318b0f822dc64165 (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
//! ref: composer/tests/Composer/Test/Mock/IOMock.php

use shirabe::config::Config;
use shirabe::io::buffer_io::BufferIO;
use shirabe::io::io_interface;
use shirabe::io::{IOInterface, IOInterfaceImmutable, IOInterfaceMutable};
use shirabe::util::platform::Platform;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_external_packages::symfony::console::output::output_interface;
use shirabe_php_shim::{PHP_EOL, PhpMixed, preg_quote};
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;

// A single entry of the IO expectation list. PHP models these as associative
// arrays (`{text, regex?}` / `{ask, reply}` / `{auth: [repo, user, pass]}`); the
// `verbosity?` key from the original docblock is dropped because `assertComplete`
// never reads it and no test sets it.
#[derive(Debug, Clone)]
pub enum Expectation {
    Text {
        text: String,
        regex: bool,
    },
    Ask {
        ask: String,
        reply: String,
    },
    Auth {
        repository_name: String,
        username: String,
        password: Option<String>,
    },
}

impl Expectation {
    pub fn text(text: impl Into<String>) -> Self {
        Expectation::Text {
            text: text.into(),
            regex: false,
        }
    }

    pub fn text_regex(text: impl Into<String>) -> Self {
        Expectation::Text {
            text: text.into(),
            regex: true,
        }
    }

    pub fn ask(ask: impl Into<String>, reply: impl Into<String>) -> Self {
        Expectation::Ask {
            ask: ask.into(),
            reply: reply.into(),
        }
    }

    pub fn auth(
        repository_name: impl Into<String>,
        username: impl Into<String>,
        password: Option<String>,
    ) -> Self {
        Expectation::Auth {
            repository_name: repository_name.into(),
            username: username.into(),
            password,
        }
    }
}

#[derive(Debug)]
pub struct IOMock {
    inner: BufferIO,
    expectations: Option<Vec<Expectation>>,
    strict: bool,
    auth_log: Vec<(String, String, Option<String>)>,
}

impl IOMock {
    pub fn new(verbosity: i64) -> anyhow::Result<Self> {
        let sf_verbosity = match verbosity {
            io_interface::QUIET => output_interface::VERBOSITY_QUIET,
            io_interface::NORMAL => output_interface::VERBOSITY_NORMAL,
            io_interface::VERBOSE => output_interface::VERBOSITY_VERBOSE,
            io_interface::VERY_VERBOSE => output_interface::VERBOSITY_VERY_VERBOSE,
            io_interface::DEBUG => output_interface::VERBOSITY_DEBUG,
            other => panic!("unknown IOInterface verbosity: {other}"),
        };

        Ok(Self {
            inner: BufferIO::new(String::new(), sf_verbosity, None)?,
            expectations: None,
            strict: false,
            auth_log: Vec::new(),
        })
    }

    // `$strict` set to true requires *all* expected messages, not just a subset.
    pub fn expects(&mut self, expectations: Vec<Expectation>, strict: bool) -> anyhow::Result<()> {
        let inputs: Vec<String> = expectations
            .iter()
            .filter_map(|expect| match expect {
                Expectation::Ask { reply, .. } => Some(reply.clone()),
                _ => None,
            })
            .collect();

        if !inputs.is_empty() {
            self.inner.set_user_inputs(inputs)?;
        }

        self.expectations = Some(expectations);
        self.strict = strict;

        Ok(())
    }

    pub fn assert_complete(&self) {
        let output = self.inner.get_output();

        if Platform::get_env("DEBUG_OUTPUT").as_deref() == Some("1") {
            println!("{PHP_EOL}Collected output: {output}{PHP_EOL}");
        }

        // Not configured to expect anything, so there is nothing to verify.
        let Some(expectations) = &self.expectations else {
            return;
        };

        if !expectations.is_empty() {
            let mut lines: VecDeque<String> = Preg::split("{\r?\n}", &output).into();
            let mut auth_log: VecDeque<(String, String, Option<String>)> =
                self.auth_log.clone().into();

            'expects: for expect in expectations {
                if let Expectation::Auth {
                    repository_name,
                    username,
                    password,
                } = expect
                {
                    let expected = (repository_name.clone(), username.clone(), password.clone());
                    while let Some(auth) = auth_log.pop_front() {
                        if auth == expected {
                            continue 'expects;
                        }

                        if self.strict {
                            panic!(
                                "IO authentication mismatch. Expected:{PHP_EOL}{expected:?}{PHP_EOL}Got:{PHP_EOL}{auth:?}"
                            );
                        }
                    }

                    panic!(
                        "Expected \"{expected:?}\" auth to be set but there are no setAuthentication calls left to consume."
                    );
                }

                let (pattern, label) = match expect {
                    Expectation::Ask { ask, .. } => {
                        (format!("{{^{}$}}", preg_quote(ask, None)), ask.clone())
                    }
                    Expectation::Text { text, regex: true } => (text.clone(), text.clone()),
                    Expectation::Text { text, regex: false } => {
                        (format!("{{^{}$}}", preg_quote(text, None)), text.clone())
                    }
                    Expectation::Auth { .. } => unreachable!("handled above"),
                };

                while let Some(line) = lines.pop_front() {
                    if Preg::is_match(&pattern, &line) {
                        continue 'expects;
                    }

                    if self.strict {
                        panic!(
                            "IO output mismatch. Expected:{PHP_EOL}{label}{PHP_EOL}Got:{PHP_EOL}{line}"
                        );
                    }
                }

                panic!(
                    "Expected \"{label}\" to be output still but there is no output left to consume. Complete output:{PHP_EOL}{output}"
                );
            }
        } else if !output.is_empty() && self.strict {
            panic!("There was strictly no output expected but some output occurred: {output}");
        }
    }
}

impl IOInterfaceImmutable for IOMock {
    fn is_interactive(&self) -> bool {
        self.inner.is_interactive()
    }
    fn is_verbose(&self) -> bool {
        self.inner.is_verbose()
    }
    fn is_very_verbose(&self) -> bool {
        self.inner.is_very_verbose()
    }
    fn is_debug(&self) -> bool {
        self.inner.is_debug()
    }
    fn is_decorated(&self) -> bool {
        self.inner.is_decorated()
    }
    fn write3(&self, message: &str, newline: bool, verbosity: i64) {
        self.inner.write3(message, newline, verbosity)
    }
    fn write_error3(&self, message: &str, newline: bool, verbosity: i64) {
        self.inner.write_error3(message, newline, verbosity)
    }
    fn write_raw3(&self, message: &str, newline: bool, verbosity: i64) {
        self.inner.write_raw3(message, newline, verbosity)
    }
    fn write_error_raw3(&self, message: &str, newline: bool, verbosity: i64) {
        self.inner.write_error_raw3(message, newline, verbosity)
    }
    fn overwrite4(&self, message: &str, newline: bool, size: Option<i64>, verbosity: i64) {
        self.inner.overwrite4(message, newline, size, verbosity)
    }
    fn overwrite_error4(&self, message: &str, newline: bool, size: Option<i64>, verbosity: i64) {
        self.inner
            .overwrite_error4(message, newline, size, verbosity)
    }

    fn ask(&self, question: String, default: PhpMixed) -> PhpMixed {
        self.inner
            .ask(format!("{}{}", trim_eol(&question), PHP_EOL), default)
    }
    fn ask_confirmation(&self, question: String, default: bool) -> bool {
        self.inner
            .ask_confirmation(format!("{}{}", trim_eol(&question), PHP_EOL), default)
    }
    fn ask_and_validate(
        &self,
        question: String,
        validator: Box<dyn Fn(PhpMixed) -> anyhow::Result<PhpMixed>>,
        attempts: Option<i64>,
        default: PhpMixed,
    ) -> anyhow::Result<PhpMixed> {
        self.inner.ask_and_validate(
            format!("{}{}", trim_eol(&question), PHP_EOL),
            validator,
            attempts,
            default,
        )
    }
    fn ask_and_hide_answer(&self, question: String) -> Option<String> {
        // Do not hide the answer in tests because that blocks on Windows with
        // hiddeninput.exe, so PHP delegates to `ask` rather than the hidden variant.
        let result = self.inner.ask(
            format!("{}{}", trim_eol(&question), PHP_EOL),
            PhpMixed::Null,
        );
        result.as_string().map(|s| s.to_string())
    }
    fn select(
        &self,
        question: String,
        choices: Vec<String>,
        default: PhpMixed,
        attempts: PhpMixed,
        error_message: String,
        multiselect: bool,
    ) -> PhpMixed {
        self.inner.select(
            format!("{}{}", trim_eol(&question), PHP_EOL),
            choices,
            default,
            attempts,
            error_message,
            multiselect,
        )
    }

    fn get_authentications(
        &self,
    ) -> indexmap::IndexMap<String, indexmap::IndexMap<String, Option<String>>> {
        self.inner.get_authentications()
    }
    fn has_authentication(&self, repository_name: &str) -> bool {
        self.inner.has_authentication(repository_name)
    }
    fn get_authentication(
        &self,
        repository_name: &str,
    ) -> indexmap::IndexMap<String, Option<String>> {
        self.inner.get_authentication(repository_name)
    }
    fn error(&self, message: &str, context: &[(&str, &str)]) {
        self.inner.error(message, context)
    }
    fn warning(&self, message: &str, context: &[(&str, &str)]) {
        self.inner.warning(message, context)
    }
    fn debug(&self, message: &str, context: &[(&str, &str)]) {
        self.inner.debug(message, context)
    }
}

impl IOInterfaceMutable for IOMock {
    fn set_authentication(
        &mut self,
        repository_name: String,
        username: String,
        password: Option<String>,
    ) {
        self.auth_log
            .push((repository_name.clone(), username.clone(), password.clone()));
        self.inner
            .set_authentication(repository_name, username, password)
    }
    fn load_configuration(&mut self, config: &mut Config) -> anyhow::Result<()> {
        self.inner.load_configuration(config)
    }
}

impl IOInterface for IOMock {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn as_base_io_mut(&mut self) -> Option<&mut dyn shirabe::io::BaseIO> {
        Some(self)
    }
}

impl shirabe::io::BaseIO for IOMock {
    fn authentications(
        &self,
    ) -> &indexmap::IndexMap<String, indexmap::IndexMap<String, Option<String>>> {
        self.inner.authentications()
    }
    fn authentications_mut(
        &mut self,
    ) -> &mut indexmap::IndexMap<String, indexmap::IndexMap<String, Option<String>>> {
        self.inner.authentications_mut()
    }
}

fn trim_eol(question: &str) -> &str {
    question.trim_end_matches(['\r', '\n'])
}

pub struct IOMockGuard(Rc<RefCell<IOMock>>);

impl Drop for IOMockGuard {
    fn drop(&mut self) {
        // Avoid aborting on a double panic when a test assertion is already unwinding.
        if std::thread::panicking() {
            return;
        }
        self.0.borrow().assert_complete();
    }
}

// For testing only. Mirrors TestCase::getIOMock: returns a shared IOMock handle
// plus a guard that runs assert_complete when it drops at the end of the test scope.
pub fn get_io_mock(verbosity: i64) -> anyhow::Result<(Rc<RefCell<IOMock>>, IOMockGuard)> {
    let mock = Rc::new(RefCell::new(IOMock::new(verbosity)?));
    Ok((mock.clone(), IOMockGuard(mock)))
}