]> git.proxmox.com Git - cargo.git/blob - tests/testsuite/search.rs
Add RegistryBuilder to help initializing test registries.
[cargo.git] / tests / testsuite / search.rs
1 //! Tests for the `cargo search` command.
2
3 use cargo_test_support::cargo_process;
4 use cargo_test_support::git::repo;
5 use cargo_test_support::paths;
6 use cargo_test_support::registry::{api_path, registry_path, registry_url};
7 use std::collections::HashSet;
8 use std::fs;
9 use std::path::Path;
10 use url::Url;
11
12 fn api() -> Url {
13 Url::from_file_path(&*api_path()).ok().unwrap()
14 }
15
16 fn write_crates(dest: &Path) {
17 let content = r#"{
18 "crates": [{
19 "created_at": "2014-11-16T20:17:35Z",
20 "description": "Design by contract style assertions for Rust",
21 "documentation": null,
22 "downloads": 2,
23 "homepage": null,
24 "id": "hoare",
25 "keywords": [],
26 "license": null,
27 "links": {
28 "owners": "/api/v1/crates/hoare/owners",
29 "reverse_dependencies": "/api/v1/crates/hoare/reverse_dependencies",
30 "version_downloads": "/api/v1/crates/hoare/downloads",
31 "versions": "/api/v1/crates/hoare/versions"
32 },
33 "max_version": "0.1.1",
34 "name": "hoare",
35 "repository": "https://github.com/nick29581/libhoare",
36 "updated_at": "2014-11-20T21:49:21Z",
37 "versions": null
38 },
39 {
40 "id": "postgres",
41 "name": "postgres",
42 "updated_at": "2020-05-01T23:17:54.335921+00:00",
43 "versions": null,
44 "keywords": null,
45 "categories": null,
46 "badges": [
47 {
48 "badge_type": "circle-ci",
49 "attributes": {
50 "repository": "sfackler/rust-postgres",
51 "branch": null
52 }
53 }
54 ],
55 "created_at": "2014-11-24T02:34:44.756689+00:00",
56 "downloads": 535491,
57 "recent_downloads": 88321,
58 "max_version": "0.17.3",
59 "newest_version": "0.17.3",
60 "description": "A native, synchronous PostgreSQL client",
61 "homepage": null,
62 "documentation": null,
63 "repository": "https://github.com/sfackler/rust-postgres",
64 "links": {
65 "version_downloads": "/api/v1/crates/postgres/downloads",
66 "versions": "/api/v1/crates/postgres/versions",
67 "owners": "/api/v1/crates/postgres/owners",
68 "owner_team": "/api/v1/crates/postgres/owner_team",
69 "owner_user": "/api/v1/crates/postgres/owner_user",
70 "reverse_dependencies": "/api/v1/crates/postgres/reverse_dependencies"
71 },
72 "exact_match": true
73 }
74 ],
75 "meta": {
76 "total": 2
77 }
78 }"#;
79
80 // Older versions of curl don't peel off query parameters when looking for
81 // filenames, so just make both files.
82 //
83 // On windows, though, `?` is an invalid character, but we always build curl
84 // from source there anyway!
85 fs::write(&dest, content).unwrap();
86 if !cfg!(windows) {
87 fs::write(
88 &dest.with_file_name("crates?q=postgres&per_page=10"),
89 content,
90 )
91 .unwrap();
92 }
93 }
94
95 const SEARCH_RESULTS: &str = "\
96 hoare = \"0.1.1\" # Design by contract style assertions for Rust
97 postgres = \"0.17.3\" # A native, synchronous PostgreSQL client
98 ";
99
100 fn setup() {
101 let cargo_home = paths::root().join(".cargo");
102 fs::create_dir_all(cargo_home).unwrap();
103 fs::create_dir_all(&api_path().join("api/v1")).unwrap();
104
105 // Init a new registry
106 let _ = repo(&registry_path())
107 .file(
108 "config.json",
109 &format!(r#"{{"dl":"{0}","api":"{0}"}}"#, api()),
110 )
111 .build();
112
113 let base = api_path().join("api/v1/crates");
114 write_crates(&base);
115 }
116
117 fn set_cargo_config() {
118 let config = paths::root().join(".cargo/config");
119
120 fs::write(
121 &config,
122 format!(
123 r#"
124 [source.crates-io]
125 registry = 'https://wut'
126 replace-with = 'dummy-registry'
127
128 [source.dummy-registry]
129 registry = '{reg}'
130 "#,
131 reg = registry_url(),
132 ),
133 )
134 .unwrap();
135 }
136
137 #[cargo_test]
138 fn not_update() {
139 setup();
140 set_cargo_config();
141
142 use cargo::core::{Shell, Source, SourceId};
143 use cargo::sources::RegistrySource;
144 use cargo::util::Config;
145
146 let sid = SourceId::for_registry(&registry_url()).unwrap();
147 let cfg = Config::new(
148 Shell::from_write(Box::new(Vec::new())),
149 paths::root(),
150 paths::home().join(".cargo"),
151 );
152 let lock = cfg.acquire_package_cache_lock().unwrap();
153 let mut regsrc = RegistrySource::remote(sid, &HashSet::new(), &cfg);
154 regsrc.update().unwrap();
155 drop(lock);
156
157 cargo_process("search postgres")
158 .with_stdout_contains(SEARCH_RESULTS)
159 .with_stderr("") // without "Updating ... index"
160 .run();
161 }
162
163 #[cargo_test]
164 fn replace_default() {
165 setup();
166 set_cargo_config();
167
168 cargo_process("search postgres")
169 .with_stdout_contains(SEARCH_RESULTS)
170 .with_stderr_contains("[..]Updating [..] index")
171 .run();
172 }
173
174 #[cargo_test]
175 fn simple() {
176 setup();
177
178 cargo_process("search postgres --index")
179 .arg(registry_url().to_string())
180 .with_stdout_contains(SEARCH_RESULTS)
181 .run();
182 }
183
184 // TODO: Deprecated
185 // remove once it has been decided '--host' can be safely removed
186 #[cargo_test]
187 fn simple_with_host() {
188 setup();
189
190 cargo_process("search postgres --host")
191 .arg(registry_url().to_string())
192 .with_stderr(
193 "\
194 [WARNING] The flag '--host' is no longer valid.
195
196 Previous versions of Cargo accepted this flag, but it is being
197 deprecated. The flag is being renamed to 'index', as the flag
198 wants the location of the index. Please use '--index' instead.
199
200 This will soon become a hard error, so it's either recommended
201 to update to a fixed version or contact the upstream maintainer
202 about this warning.
203 [UPDATING] `[CWD]/registry` index
204 ",
205 )
206 .with_stdout_contains(SEARCH_RESULTS)
207 .run();
208 }
209
210 // TODO: Deprecated
211 // remove once it has been decided '--host' can be safely removed
212 #[cargo_test]
213 fn simple_with_index_and_host() {
214 setup();
215
216 cargo_process("search postgres --index")
217 .arg(registry_url().to_string())
218 .arg("--host")
219 .arg(registry_url().to_string())
220 .with_stderr(
221 "\
222 [WARNING] The flag '--host' is no longer valid.
223
224 Previous versions of Cargo accepted this flag, but it is being
225 deprecated. The flag is being renamed to 'index', as the flag
226 wants the location of the index. Please use '--index' instead.
227
228 This will soon become a hard error, so it's either recommended
229 to update to a fixed version or contact the upstream maintainer
230 about this warning.
231 [UPDATING] `[CWD]/registry` index
232 ",
233 )
234 .with_stdout_contains(SEARCH_RESULTS)
235 .run();
236 }
237
238 #[cargo_test]
239 fn multiple_query_params() {
240 setup();
241
242 cargo_process("search postgres sql --index")
243 .arg(registry_url().to_string())
244 .with_stdout_contains(SEARCH_RESULTS)
245 .run();
246 }