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
|
//! ref: composer/tests/Composer/Test/ApplicationTest.php
#[path = "common/bootstrap.rs"]
mod bootstrap;
#[path = "common/test_case.rs"]
mod test_case;
use serial_test::serial;
use test_case::init_temp_composer;
use shirabe::command::about_command::AboutCommand;
use shirabe::command::self_update_command::SelfUpdateCommand;
use shirabe::console::application::ApplicationHandle;
use shirabe::util::platform::Platform;
use shirabe_php_shim::{PHP_EOL, PHP_SERVER, PhpMixed, time};
use shirabe_symfony_console::command::Command;
use shirabe_symfony_console::input::ArrayInput;
use shirabe_symfony_console::input::InputInterface;
use shirabe_symfony_console::output::BufferedOutput;
use shirabe_symfony_console::output::OutputInterface;
fn set_up() {
Platform::put_env("COMPOSER_DISABLE_XDEBUG_WARN", "1");
}
fn tear_down() {
Platform::clear_env("COMPOSER_DISABLE_XDEBUG_WARN");
}
struct TearDown;
impl Drop for TearDown {
fn drop(&mut self) {
tear_down();
}
}
#[test]
fn test_dev_warning() {
let _tear_down = TearDown;
set_up();
let application = ApplicationHandle::new("Composer".to_string(), "".to_string()).unwrap();
application.__set_dev_warning_time(Some(time() - 1));
let output = std::rc::Rc::new(std::cell::RefCell::new(BufferedOutput::new(
None, false, None,
)));
let input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(
ArrayInput::new(
vec![(PhpMixed::from("command"), PhpMixed::from("about"))],
None,
)
.unwrap(),
));
let output_trait: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> = output.clone();
application.do_run(input, output_trait).unwrap();
let expected_output = format!(
"<warning>Warning: This development build of Composer is over 60 days old. It is recommended to update it by running \"{} self-update\" to get the latest version.</warning>{}",
PHP_SERVER
.lock()
.unwrap()
.get("PHP_SELF")
.unwrap_or_default()
.to_string_lossy(),
PHP_EOL
);
assert!(output.borrow().fetch().contains(&expected_output));
}
#[ignore = "SelfUpdateCommand::execute is intentionally stubbed with a Shirabe-specific \"not available\" message instead of the original Composer wording this test expects"]
#[test]
fn test_dev_warning_suppressed_for_self_update() {
let _tear_down = TearDown;
set_up();
if Platform::is_windows() {
// markTestSkipped('Does not run on windows')
return;
}
let application = ApplicationHandle::new("Composer".to_string(), "".to_string()).unwrap();
let command: std::rc::Rc<std::cell::RefCell<dyn Command>> =
std::rc::Rc::new(std::cell::RefCell::new(SelfUpdateCommand::new()));
application.add(command).unwrap();
let output = std::rc::Rc::new(std::cell::RefCell::new(BufferedOutput::new(
None, false, None,
)));
let input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(
ArrayInput::new(
vec![(PhpMixed::from("command"), PhpMixed::from("self-update"))],
None,
)
.unwrap(),
));
let output_trait: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> = output.clone();
application.do_run(input, output_trait).unwrap();
assert_eq!(
"This instance of Composer does not have the self-update command.\n\
This could be due to a number of reasons, such as Composer being installed as a system package on your OS, or Composer being installed as a package in the current project.\n",
output.borrow().fetch().as_str()
);
}
#[test]
fn test_process_isolation_works_multiple_times() {
let _tear_down = TearDown;
set_up();
let application = ApplicationHandle::new("Composer".to_string(), "".to_string()).unwrap();
let command: std::rc::Rc<std::cell::RefCell<dyn Command>> =
std::rc::Rc::new(std::cell::RefCell::new(AboutCommand::new()));
application.add(command).unwrap();
let input1: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(
ArrayInput::new(
vec![(PhpMixed::from("command"), PhpMixed::from("about"))],
None,
)
.unwrap(),
));
let output1: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> = std::rc::Rc::new(
std::cell::RefCell::new(BufferedOutput::new(None, false, None)),
);
assert_eq!(0, application.do_run(input1, output1).unwrap());
let input2: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(
ArrayInput::new(
vec![(PhpMixed::from("command"), PhpMixed::from("about"))],
None,
)
.unwrap(),
));
let output2: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> = std::rc::Rc::new(
std::cell::RefCell::new(BufferedOutput::new(None, false, None)),
);
assert_eq!(0, application.do_run(input2, output2).unwrap());
}
#[test]
#[serial]
fn test_no_plugins_disables_plugins_when_script_commands_exist() {
let _tear_down = TearDown;
set_up();
// PHP also calls setAutoExit(false)/setCatchErrors(false); both are Symfony base-Application
// methods the external-package stub does not model, and neither affects the do_run path
// exercised here (they only matter for run()/error catching), so only set_catch_exceptions
// is mirrored.
let _init = init_temp_composer(
Some(&serde_json::json!({
"scripts": {
"my-script": "echo hello",
},
})),
None,
None,
true,
);
let application = ApplicationHandle::new("Composer".to_string(), "".to_string()).unwrap();
application.set_catch_exceptions(false);
// Run list command with --no-plugins, this triggers script command registration which previously
// created a Composer instance with plugins enabled regardless of the --no-plugins flag
let input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(
ArrayInput::new(
vec![
(PhpMixed::from("command"), PhpMixed::from("list")),
(PhpMixed::from("--no-plugins"), PhpMixed::from(true)),
],
None,
)
.unwrap(),
));
let output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> = std::rc::Rc::new(
std::cell::RefCell::new(BufferedOutput::new(None, false, None)),
);
application.do_run(input, output).unwrap();
let composer = application.__get_composer(false, None, None).unwrap();
assert!(
composer.is_some(),
"Composer instance should have been created during script command registration"
);
let composer = composer.unwrap();
let composer = shirabe::composer::composer_full(&composer);
assert!(
composer
.get_plugin_manager()
.borrow()
.are_plugins_disabled("local"),
"Plugins should be disabled when --no-plugins is used"
);
assert!(
composer
.get_plugin_manager()
.borrow()
.are_plugins_disabled("global"),
"Global plugins should be disabled when --no-plugins is used"
);
}
#[test]
#[serial]
fn test_script_command_takes_priority_over_abbreviated_builtin_command() {
let _tear_down = TearDown;
set_up();
// PHP also calls setAutoExit(false)/setCatchErrors(false); both are Symfony base-Application
// methods the external-package stub does not model, and neither affects the do_run path
// exercised here (they only matter for run()/error catching), so only set_catch_exceptions
// is mirrored.
let _init = init_temp_composer(
Some(&serde_json::json!({
"scripts": {
"check": "echo hello",
},
})),
None,
None,
true,
);
let application = ApplicationHandle::new("Composer".to_string(), "".to_string()).unwrap();
application.set_catch_exceptions(false);
let app_output = std::rc::Rc::new(std::cell::RefCell::new(BufferedOutput::new(
None, false, None,
)));
let input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(
ArrayInput::new(
vec![(PhpMixed::from("command"), PhpMixed::from("check"))],
None,
)
.unwrap(),
));
let output_trait: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> = app_output.clone();
let exit_code = application.do_run(input, output_trait).unwrap();
assert_eq!(0, exit_code, "Script command should have run successfully");
assert!(
app_output.borrow().fetch().contains("hello"),
"The \"check\" script should have been executed instead of the check-platform-reqs command"
);
}
|