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
|
//! ref: composer/tests/Composer/Test/Command/SearchCommandTest.php
use crate::test_case::{RunOptions, get_application_tester, init_temp_composer};
use serial_test::serial;
use shirabe_php_shim::PhpMixed;
fn repositories_json() -> serde_json::Value {
serde_json::json!({
"repositories": [
{ "packagist.org": false },
{
"type": "package",
"package": [
{ "name": "vendor-1/package-1", "description": "generic description", "version": "1.0.0" },
{ "name": "foo/bar", "description": "generic description", "version": "1.0.0" },
{ "name": "bar/baz", "description": "fancy baz", "version": "1.0.0", "abandoned": true },
{ "name": "vendor-2/fancy-package", "fancy description": null, "version": "1.0.0", "type": "foo" },
],
},
],
})
}
/// ref: SearchCommandTest::testSearch (data provider rolled into one body).
fn run_search_case(command: Vec<(PhpMixed, PhpMixed)>, expected: &str) {
let _tear_down = init_temp_composer(Some(&repositories_json()), None, None, true);
let mut input: Vec<(PhpMixed, PhpMixed)> =
vec![(PhpMixed::from("command"), PhpMixed::from("search"))];
input.extend(command);
let mut app_tester = get_application_tester();
app_tester.run(input, RunOptions::default()).unwrap();
assert_eq!(expected.trim(), app_tester.get_display().trim());
}
#[test]
#[serial]
#[ignore = "searching a `package`-type repo returns incomplete results (some matching packages are dropped); the search/repository path is not yet fully ported"]
fn test_search() {
// 'by name and description'
run_search_case(
vec![(
"tokens".into(),
PhpMixed::List(vec![PhpMixed::from("fancy")]),
)],
"bar/baz <warning>! Abandoned !</warning> fancy baz\nvendor-2/fancy-package",
);
// 'by name and description with multiple tokens'
run_search_case(
vec![(
"tokens".into(),
PhpMixed::List(vec![PhpMixed::from("fancy"), PhpMixed::from("vendor")]),
)],
"vendor-1/package-1 generic description\nbar/baz <warning>! Abandoned !</warning> fancy baz\nvendor-2/fancy-package",
);
// 'by name only'
run_search_case(
vec![
(
"tokens".into(),
PhpMixed::List(vec![PhpMixed::from("fancy")]),
),
("--only-name".into(), PhpMixed::from(true)),
],
"vendor-2/fancy-package",
);
// 'by vendor only'
run_search_case(
vec![
("tokens".into(), PhpMixed::List(vec![PhpMixed::from("bar")])),
("--only-vendor".into(), PhpMixed::from(true)),
],
"bar",
);
// 'by type'
run_search_case(
vec![
(
"tokens".into(),
PhpMixed::List(vec![PhpMixed::from("vendor")]),
),
("--type".into(), PhpMixed::from("foo")),
],
"vendor-2/fancy-package",
);
// 'json format'
run_search_case(
vec![
(
"tokens".into(),
PhpMixed::List(vec![PhpMixed::from("vendor-2/fancy")]),
),
("--format".into(), PhpMixed::from("json")),
],
"[\n {\n \"name\": \"vendor-2/fancy-package\",\n \"description\": null\n }\n]",
);
// 'no results'
run_search_case(
vec![(
"tokens".into(),
PhpMixed::List(vec![PhpMixed::from("invalid-package-name")]),
)],
"",
);
}
#[test]
#[serial]
fn test_invalid_format() {
let _tear_down = init_temp_composer(
Some(&serde_json::json!({ "repositories": { "packagist.org": false } })),
None,
None,
true,
);
let mut app_tester = get_application_tester();
let result = app_tester
.run(
vec![
(PhpMixed::from("command"), PhpMixed::from("search")),
(PhpMixed::from("--format"), PhpMixed::from("test-format")),
(
PhpMixed::from("tokens"),
PhpMixed::List(vec![PhpMixed::from("test")]),
),
],
RunOptions::default(),
)
.unwrap();
assert_eq!(1, result);
assert_eq!(
"Unsupported format \"test-format\". See help for supported formats.",
app_tester.get_display().trim()
);
}
#[test]
#[serial]
fn test_invalid_flags() {
let _tear_down = init_temp_composer(
Some(&serde_json::json!({ "repositories": { "packagist.org": false } })),
None,
None,
true,
);
let mut app_tester = get_application_tester();
let err = app_tester
.run(
vec![
(PhpMixed::from("command"), PhpMixed::from("search")),
(PhpMixed::from("--only-vendor"), PhpMixed::from(true)),
(PhpMixed::from("--only-name"), PhpMixed::from(true)),
(
PhpMixed::from("tokens"),
PhpMixed::List(vec![PhpMixed::from("test")]),
),
],
RunOptions::default(),
)
.expect_err("expected InvalidArgumentException");
assert!(
err.to_string()
.contains("--only-name and --only-vendor cannot be used together"),
"got: {:?}",
err
);
}
|