Skip to content

Commit 2b8de5d

Browse files
committed
Auto merge of #4267 - natboehm:add-index-flag-search, r=matklad
Replace `cargo search --host` with `cargo search --index` This change makes the command consistent with other versions of the same flag. `cargo search --host` is still supported, as well as `cargo search --index <index> --host <host>` in case of confusion. The three commands currently supported should produce the same behavior. All instances of `--host` are currently marked as deprecated, or for reuse as mentioned in issue #4208. Fixes #4219
2 parents 04d8a13 + 4d30d52 commit 2b8de5d

File tree

2 files changed

+165
-5
lines changed

2 files changed

+165
-5
lines changed

src/bin/search.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::cmp;
55

66
#[derive(Deserialize)]
77
pub struct Options {
8-
flag_host: Option<String>,
8+
flag_index: Option<String>,
9+
flag_host: Option<String>, // TODO: Depricated, remove
910
flag_verbose: u32,
1011
flag_quiet: Option<bool>,
1112
flag_color: Option<String>,
@@ -24,7 +25,8 @@ Usage:
2425
2526
Options:
2627
-h, --help Print this message
27-
--host HOST Host of a registry to search in
28+
--index INDEX Registry index to search in
29+
--host HOST DEPRICATED, renamed to '--index'
2830
-v, --verbose ... Use verbose output (-vv very verbose/build.rs output)
2931
-q, --quiet No output printed to stdout
3032
--color WHEN Coloring: auto, always, never
@@ -40,12 +42,37 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
4042
options.flag_frozen,
4143
options.flag_locked)?;
4244
let Options {
43-
flag_host: host,
45+
flag_index: index,
46+
flag_host: host, // TODO: Depricated, remove
4447
flag_limit: limit,
4548
arg_query: query,
4649
..
4750
} = options;
4851

49-
ops::search(&query.join("+"), config, host, cmp::min(100, limit.unwrap_or(10)) as u8)?;
52+
// TODO: Depricated
53+
// remove once it has been decided --host can be safely removed
54+
// We may instead want to repurpose the host flag, as
55+
// mentioned in this issue
56+
// https://github.com/rust-lang/cargo/issues/4208
57+
58+
let msg = "The flag '--host' is no longer valid.
59+
60+
Previous versions of Cargo accepted this flag, but it is being
61+
depricated. The flag is being renamed to 'index', as the flag
62+
wants the location of the index in which to search. Please
63+
use '--index' instead.
64+
65+
This will soon become a hard error, so it's either recommended
66+
to update to a fixed version or contact the upstream maintainer
67+
about this warning.";
68+
69+
let index = if host.clone().is_none() || host.clone().unwrap().is_empty() {
70+
index
71+
} else {
72+
config.shell().warn(&msg)?;
73+
host
74+
};
75+
76+
ops::search(&query.join("+"), config, index, cmp::min(100, limit.unwrap_or(10)) as u8)?;
5077
Ok(())
5178
}

tests/search.rs

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,141 @@ fn simple() {
8282
}
8383

8484
assert_that(cargo_process("search").arg("postgres")
85+
.arg("--index").arg(registry().to_string()),
86+
execs().with_status(0)
87+
.with_stdout_contains("\
88+
hoare = \"0.1.1\" # Design by contract style assertions for Rust"));
89+
}
90+
91+
// TODO: Depricated
92+
// remove once it has been decided '--host' can be safely removed
93+
#[test]
94+
fn simple_with_host() {
95+
setup();
96+
97+
let contents = r#"{
98+
"crates": [{
99+
"created_at": "2014-11-16T20:17:35Z",
100+
"description": "Design by contract style assertions for Rust",
101+
"documentation": null,
102+
"downloads": 2,
103+
"homepage": null,
104+
"id": "hoare",
105+
"keywords": [],
106+
"license": null,
107+
"links": {
108+
"owners": "/api/v1/crates/hoare/owners",
109+
"reverse_dependencies": "/api/v1/crates/hoare/reverse_dependencies",
110+
"version_downloads": "/api/v1/crates/hoare/downloads",
111+
"versions": "/api/v1/crates/hoare/versions"
112+
},
113+
"max_version": "0.1.1",
114+
"name": "hoare",
115+
"repository": "https://github.com/nick29581/libhoare",
116+
"updated_at": "2014-11-20T21:49:21Z",
117+
"versions": null
118+
}],
119+
"meta": {
120+
"total": 1
121+
}
122+
}"#;
123+
let base = api_path().join("api/v1/crates");
124+
125+
// Older versions of curl don't peel off query parameters when looking for
126+
// filenames, so just make both files.
127+
//
128+
// On windows, though, `?` is an invalid character, but we always build curl
129+
// from source there anyway!
130+
File::create(&base).unwrap().write_all(contents.as_bytes()).unwrap();
131+
if !cfg!(windows) {
132+
File::create(&base.with_file_name("crates?q=postgres&per_page=10")).unwrap()
133+
.write_all(contents.as_bytes()).unwrap();
134+
}
135+
136+
assert_that(cargo_process("search").arg("postgres")
137+
.arg("--host").arg(registry().to_string()),
138+
execs().with_status(0)
139+
.with_stderr(&format!("\
140+
[WARNING] The flag '--host' is no longer valid.
141+
142+
Previous versions of Cargo accepted this flag, but it is being
143+
depricated. The flag is being renamed to 'index', as the flag
144+
wants the location of the index in which to search. Please
145+
use '--index' instead.
146+
147+
This will soon become a hard error, so it's either recommended
148+
to update to a fixed version or contact the upstream maintainer
149+
about this warning.
150+
[UPDATING] registry `{reg}`
151+
",
152+
reg = registry()))
153+
.with_stdout_contains("\
154+
hoare = \"0.1.1\" # Design by contract style assertions for Rust"));
155+
}
156+
157+
// TODO: Depricated
158+
// remove once it has been decided '--host' can be safely removed
159+
#[test]
160+
fn simple_with_index_and_host() {
161+
setup();
162+
163+
let contents = r#"{
164+
"crates": [{
165+
"created_at": "2014-11-16T20:17:35Z",
166+
"description": "Design by contract style assertions for Rust",
167+
"documentation": null,
168+
"downloads": 2,
169+
"homepage": null,
170+
"id": "hoare",
171+
"keywords": [],
172+
"license": null,
173+
"links": {
174+
"owners": "/api/v1/crates/hoare/owners",
175+
"reverse_dependencies": "/api/v1/crates/hoare/reverse_dependencies",
176+
"version_downloads": "/api/v1/crates/hoare/downloads",
177+
"versions": "/api/v1/crates/hoare/versions"
178+
},
179+
"max_version": "0.1.1",
180+
"name": "hoare",
181+
"repository": "https://github.com/nick29581/libhoare",
182+
"updated_at": "2014-11-20T21:49:21Z",
183+
"versions": null
184+
}],
185+
"meta": {
186+
"total": 1
187+
}
188+
}"#;
189+
let base = api_path().join("api/v1/crates");
190+
191+
// Older versions of curl don't peel off query parameters when looking for
192+
// filenames, so just make both files.
193+
//
194+
// On windows, though, `?` is an invalid character, but we always build curl
195+
// from source there anyway!
196+
File::create(&base).unwrap().write_all(contents.as_bytes()).unwrap();
197+
if !cfg!(windows) {
198+
File::create(&base.with_file_name("crates?q=postgres&per_page=10")).unwrap()
199+
.write_all(contents.as_bytes()).unwrap();
200+
}
201+
202+
assert_that(cargo_process("search").arg("postgres")
203+
.arg("--index").arg(registry().to_string())
85204
.arg("--host").arg(registry().to_string()),
86205
execs().with_status(0)
206+
.with_stderr(&format!("\
207+
[WARNING] The flag '--host' is no longer valid.
208+
209+
Previous versions of Cargo accepted this flag, but it is being
210+
depricated. The flag is being renamed to 'index', as the flag
211+
wants the location of the index in which to search. Please
212+
use '--index' instead.
213+
214+
This will soon become a hard error, so it's either recommended
215+
to update to a fixed version or contact the upstream maintainer
216+
about this warning.
217+
[UPDATING] registry `{reg}`
218+
",
219+
reg = registry()))
87220
.with_stdout_contains("\
88221
hoare = \"0.1.1\" # Design by contract style assertions for Rust"));
89222
}
@@ -132,7 +265,7 @@ fn multiple_query_params() {
132265
}
133266

134267
assert_that(cargo_process("search").arg("postgres").arg("sql")
135-
.arg("--host").arg(registry().to_string()),
268+
.arg("--index").arg(registry().to_string()),
136269
execs().with_status(0)
137270
.with_stdout_contains("\
138271
hoare = \"0.1.1\" # Design by contract style assertions for Rust"));

0 commit comments

Comments
 (0)