Skip to content

Commit 65cab34

Browse files
committed
Auto merge of #11650 - hi-rustin:rustin-patch-tests, r=epage
Make some blocking tests non-blocking Signed-off-by: hi-rustin <rustin.liu@gmail.com>
2 parents 0625b29 + fbe7ac2 commit 65cab34

File tree

9 files changed

+228
-252
lines changed

9 files changed

+228
-252
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ impl RegistryBuilder {
261261
let server = HttpServer::new(
262262
registry_path.clone(),
263263
dl_path,
264+
api_path.clone(),
264265
token.clone(),
265266
self.auth_required,
266267
self.custom_responders,
@@ -585,6 +586,7 @@ pub struct HttpServer {
585586
listener: TcpListener,
586587
registry_path: PathBuf,
587588
dl_path: PathBuf,
589+
api_path: PathBuf,
588590
addr: SocketAddr,
589591
token: Token,
590592
auth_required: bool,
@@ -604,6 +606,7 @@ impl HttpServer {
604606
pub fn new(
605607
registry_path: PathBuf,
606608
dl_path: PathBuf,
609+
api_path: PathBuf,
607610
token: Token,
608611
auth_required: bool,
609612
api_responders: HashMap<
@@ -617,6 +620,7 @@ impl HttpServer {
617620
listener,
618621
registry_path,
619622
dl_path,
623+
api_path,
620624
addr,
621625
token,
622626
auth_required,
@@ -1007,6 +1011,12 @@ impl HttpServer {
10071011

10081012
pub fn check_authorized_publish(&self, req: &Request) -> Response {
10091013
if let Some(body) = &req.body {
1014+
// Mimic the publish behavior for local registries by writing out the request
1015+
// so tests can verify publishes made to either registry type.
1016+
let path = self.api_path.join("api/v1/crates/new");
1017+
t!(fs::create_dir_all(path.parent().unwrap()));
1018+
t!(fs::write(&path, body));
1019+
10101020
// Get the metadata of the package
10111021
let (len, remaining) = body.split_at(4);
10121022
let json_len = u32::from_le_bytes(len.try_into().unwrap());

tests/testsuite/alt_registry.rs

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,12 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {
287287

288288
#[cargo_test]
289289
fn publish_with_registry_dependency() {
290-
registry::alt_init();
290+
let _reg = RegistryBuilder::new()
291+
.http_api()
292+
.http_index()
293+
.alternative()
294+
.build();
295+
291296
let p = project()
292297
.file(
293298
"Cargo.toml",
@@ -307,10 +312,26 @@ fn publish_with_registry_dependency() {
307312

308313
Package::new("bar", "0.0.1").alternative(true).publish();
309314

310-
// Login so that we have the token available
311-
p.cargo("login --registry alternative TOKEN").run();
312-
313-
p.cargo("publish --registry alternative").run();
315+
p.cargo("publish --registry alternative")
316+
.with_stderr(
317+
"\
318+
[UPDATING] `alternative` index
319+
[WARNING] [..]
320+
[..]
321+
[PACKAGING] foo v0.0.1 [..]
322+
[UPDATING] `alternative` index
323+
[VERIFYING] foo v0.0.1 [..]
324+
[DOWNLOADING] [..]
325+
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
326+
[COMPILING] bar v0.0.1 (registry `alternative`)
327+
[COMPILING] foo v0.0.1 [..]
328+
[FINISHED] [..]
329+
[PACKAGED] [..]
330+
[UPLOADING] foo v0.0.1 [..]
331+
[UPDATING] `alternative` index
332+
",
333+
)
334+
.run();
314335

315336
validate_alt_upload(
316337
r#"{
@@ -415,48 +436,43 @@ or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN",
415436

416437
#[cargo_test]
417438
fn publish_to_alt_registry() {
418-
registry::alt_init();
419-
let p = project().file("src/main.rs", "fn main() {}").build();
420-
421-
// Setup the registry by publishing a package
422-
Package::new("bar", "0.0.1").alternative(true).publish();
439+
let _reg = RegistryBuilder::new()
440+
.http_api()
441+
.http_index()
442+
.alternative()
443+
.build();
423444

424-
// Login so that we have the token available
425-
p.cargo("login --registry alternative TOKEN").run();
445+
let p = project().file("src/main.rs", "fn main() {}").build();
426446

427447
// Now perform the actual publish
428-
p.cargo("publish --registry alternative").run();
429-
430-
validate_alt_upload(
431-
r#"{
432-
"authors": [],
433-
"badges": {},
434-
"categories": [],
435-
"deps": [],
436-
"description": null,
437-
"documentation": null,
438-
"features": {},
439-
"homepage": null,
440-
"keywords": [],
441-
"license": null,
442-
"license_file": null,
443-
"links": null,
444-
"name": "foo",
445-
"readme": null,
446-
"readme_file": null,
447-
"repository": null,
448-
"homepage": null,
449-
"documentation": null,
450-
"vers": "0.0.1"
451-
}"#,
452-
"foo-0.0.1.crate",
453-
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
454-
);
448+
p.cargo("publish --registry alternative")
449+
.with_stderr(
450+
"\
451+
[UPDATING] `alternative` index
452+
[WARNING] [..]
453+
[..]
454+
[PACKAGING] foo v0.0.1 [..]
455+
[VERIFYING] foo v0.0.1 [..]
456+
[COMPILING] foo v0.0.1 [..]
457+
[FINISHED] [..]
458+
[PACKAGED] [..]
459+
[UPLOADING] foo v0.0.1 [..]
460+
[UPDATING] `alternative` index
461+
",
462+
)
463+
.run();
455464
}
456465

457466
#[cargo_test]
458467
fn publish_with_crates_io_dep() {
459-
registry::alt_init();
468+
// crates.io registry.
469+
let _dummy_reg = registry::init();
470+
// Alternative registry.
471+
let _alt_reg = RegistryBuilder::new()
472+
.http_api()
473+
.http_index()
474+
.alternative()
475+
.build();
460476
let p = project()
461477
.file(
462478
"Cargo.toml",
@@ -477,10 +493,26 @@ fn publish_with_crates_io_dep() {
477493

478494
Package::new("bar", "0.0.1").publish();
479495

480-
// Login so that we have the token available
481-
p.cargo("login --registry alternative TOKEN").run();
482-
483-
p.cargo("publish --registry alternative").run();
496+
p.cargo("publish --registry alternative")
497+
.with_stderr(
498+
"\
499+
[UPDATING] `alternative` index
500+
[WARNING] [..]
501+
[..]
502+
[PACKAGING] foo v0.0.1 [..]
503+
[UPDATING] `dummy-registry` index
504+
[VERIFYING] foo v0.0.1 [..]
505+
[DOWNLOADING] [..]
506+
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
507+
[COMPILING] bar v0.0.1
508+
[COMPILING] foo v0.0.1 [..]
509+
[FINISHED] [..]
510+
[PACKAGED] [..]
511+
[UPLOADING] foo v0.0.1 [..]
512+
[UPDATING] `alternative` index
513+
",
514+
)
515+
.run();
484516

485517
validate_alt_upload(
486518
r#"{

tests/testsuite/artifact_dep.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! the new `dep = { artifact = "bin", … }` syntax in manifests.
33
44
use cargo_test_support::compare::match_exact;
5-
use cargo_test_support::registry::Package;
5+
use cargo_test_support::registry::{Package, RegistryBuilder};
66
use cargo_test_support::{
77
basic_bin_manifest, basic_manifest, cross_compile, project, publish, registry, rustc_host,
88
Project,
@@ -1872,8 +1872,7 @@ fn env_vars_and_build_products_for_various_build_targets() {
18721872

18731873
#[cargo_test]
18741874
fn publish_artifact_dep() {
1875-
// HACK below allows us to use a local registry
1876-
let registry = registry::init();
1875+
let registry = RegistryBuilder::new().http_api().http_index().build();
18771876

18781877
Package::new("bar", "1.0.0").publish();
18791878
Package::new("baz", "1.0.0").publish();
@@ -1903,15 +1902,6 @@ fn publish_artifact_dep() {
19031902
.file("src/lib.rs", "")
19041903
.build();
19051904

1906-
// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
1907-
// the index.
1908-
//
1909-
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
1910-
// the registry from processing the publish.
1911-
Package::new("foo", "0.1.0")
1912-
.file("src/lib.rs", "")
1913-
.publish();
1914-
19151905
p.cargo("publish -Z bindeps --no-verify")
19161906
.replace_crates_io(registry.index_url())
19171907
.masquerade_as_nightly_cargo(&["bindeps"])

tests/testsuite/cargo_features.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,10 @@ fn z_flags_rejected() {
610610

611611
#[cargo_test]
612612
fn publish_allowed() {
613-
let registry = registry::init();
613+
let registry = registry::RegistryBuilder::new()
614+
.http_api()
615+
.http_index()
616+
.build();
614617

615618
let p = project()
616619
.file(
@@ -627,16 +630,23 @@ fn publish_allowed() {
627630
.file("src/lib.rs", "")
628631
.build();
629632

630-
// HACK: Inject `a` directly into the index so `publish` won't block for it to be in
631-
// the index.
632-
//
633-
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
634-
// the registry from processing the publish.
635-
Package::new("a", "0.0.1").file("src/lib.rs", "").publish();
636-
637633
p.cargo("publish")
638634
.replace_crates_io(registry.index_url())
639635
.masquerade_as_nightly_cargo(&["test-dummy-unstable"])
636+
.with_stderr(
637+
"\
638+
[UPDATING] [..]
639+
[WARNING] [..]
640+
[..]
641+
[PACKAGING] a v0.0.1 [..]
642+
[VERIFYING] a v0.0.1 [..]
643+
[COMPILING] a v0.0.1 [..]
644+
[FINISHED] [..]
645+
[PACKAGED] [..]
646+
[UPLOADING] a v0.0.1 [..]
647+
[UPDATING] [..]
648+
",
649+
)
640650
.run();
641651
}
642652

tests/testsuite/credential_process.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for credential-process.
22
3-
use cargo_test_support::registry::{Package, TestRegistry};
3+
use cargo_test_support::registry::TestRegistry;
44
use cargo_test_support::{basic_manifest, cargo_process, paths, project, registry, Project};
55
use std::fs::{self, read_to_string};
66

@@ -69,6 +69,8 @@ or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
6969
fn warn_both_token_and_process() {
7070
// Specifying both credential-process and a token in config should issue a warning.
7171
let _server = registry::RegistryBuilder::new()
72+
.http_api()
73+
.http_index()
7274
.alternative()
7375
.no_configure_token()
7476
.build();
@@ -77,7 +79,7 @@ fn warn_both_token_and_process() {
7779
".cargo/config",
7880
r#"
7981
[registries.alternative]
80-
token = "sekrit"
82+
token = "alternative-sekrit"
8183
credential-process = "false"
8284
"#,
8385
)
@@ -96,16 +98,6 @@ fn warn_both_token_and_process() {
9698
.file("src/lib.rs", "")
9799
.build();
98100

99-
// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
100-
// the index.
101-
//
102-
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
103-
// the registry from processing the publish.
104-
Package::new("foo", "0.1.0")
105-
.file("src/lib.rs", "")
106-
.alternative(true)
107-
.publish();
108-
109101
p.cargo("publish --no-verify --registry alternative -Z credential-process")
110102
.masquerade_as_nightly_cargo(&["credential-process"])
111103
.with_status(101)
@@ -127,7 +119,7 @@ Only one of these values may be set, remove one or the other to proceed.
127119
credential-process = "false"
128120
129121
[registries.alternative]
130-
token = "sekrit"
122+
token = "alternative-sekrit"
131123
"#,
132124
);
133125
p.cargo("publish --no-verify --registry alternative -Z credential-process")

tests/testsuite/features_namespaced.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Tests for namespaced features.
22
33
use super::features2::switch_to_resolver_2;
4-
use cargo_test_support::registry::{self, Dependency, Package};
4+
use cargo_test_support::registry::{Dependency, Package, RegistryBuilder};
55
use cargo_test_support::{project, publish};
66

77
#[cargo_test]
@@ -858,8 +858,7 @@ bar v1.0.0
858858

859859
#[cargo_test]
860860
fn publish_no_implicit() {
861-
// HACK below allows us to use a local registry
862-
let registry = registry::init();
861+
let registry = RegistryBuilder::new().http_api().http_index().build();
863862

864863
// Does not include implicit features or dep: syntax on publish.
865864
Package::new("opt-dep1", "1.0.0").publish();
@@ -887,15 +886,6 @@ fn publish_no_implicit() {
887886
.file("src/lib.rs", "")
888887
.build();
889888

890-
// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
891-
// the index.
892-
//
893-
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
894-
// the registry from processing the publish.
895-
Package::new("foo", "0.1.0")
896-
.file("src/lib.rs", "")
897-
.publish();
898-
899889
p.cargo("publish --no-verify")
900890
.replace_crates_io(registry.index_url())
901891
.with_stderr(
@@ -984,8 +974,7 @@ feat = ["opt-dep1"]
984974

985975
#[cargo_test]
986976
fn publish() {
987-
// HACK below allows us to use a local registry
988-
let registry = registry::init();
977+
let registry = RegistryBuilder::new().http_api().http_index().build();
989978

990979
// Publish behavior with explicit dep: syntax.
991980
Package::new("bar", "1.0.0").publish();
@@ -1012,22 +1001,14 @@ fn publish() {
10121001
.file("src/lib.rs", "")
10131002
.build();
10141003

1015-
// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
1016-
// the index.
1017-
//
1018-
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
1019-
// the registry from processing the publish.
1020-
Package::new("foo", "0.1.0")
1021-
.file("src/lib.rs", "")
1022-
.publish();
1023-
10241004
p.cargo("publish")
10251005
.replace_crates_io(registry.index_url())
10261006
.with_stderr(
10271007
"\
10281008
[UPDATING] [..]
10291009
[PACKAGING] foo v0.1.0 [..]
10301010
[VERIFYING] foo v0.1.0 [..]
1011+
[UPDATING] [..]
10311012
[COMPILING] foo v0.1.0 [..]
10321013
[FINISHED] [..]
10331014
[PACKAGED] [..]

0 commit comments

Comments
 (0)