Skip to content

Commit

Permalink
Auto merge of #11650 - hi-rustin:rustin-patch-tests, r=epage
Browse files Browse the repository at this point in the history
Make some blocking tests non-blocking

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
bors committed Feb 24, 2023
2 parents 0625b29 + fbe7ac2 commit 65cab34
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 252 deletions.
10 changes: 10 additions & 0 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl RegistryBuilder {
let server = HttpServer::new(
registry_path.clone(),
dl_path,
api_path.clone(),
token.clone(),
self.auth_required,
self.custom_responders,
Expand Down Expand Up @@ -585,6 +586,7 @@ pub struct HttpServer {
listener: TcpListener,
registry_path: PathBuf,
dl_path: PathBuf,
api_path: PathBuf,
addr: SocketAddr,
token: Token,
auth_required: bool,
Expand All @@ -604,6 +606,7 @@ impl HttpServer {
pub fn new(
registry_path: PathBuf,
dl_path: PathBuf,
api_path: PathBuf,
token: Token,
auth_required: bool,
api_responders: HashMap<
Expand All @@ -617,6 +620,7 @@ impl HttpServer {
listener,
registry_path,
dl_path,
api_path,
addr,
token,
auth_required,
Expand Down Expand Up @@ -1007,6 +1011,12 @@ impl HttpServer {

pub fn check_authorized_publish(&self, req: &Request) -> Response {
if let Some(body) = &req.body {
// Mimic the publish behavior for local registries by writing out the request
// so tests can verify publishes made to either registry type.
let path = self.api_path.join("api/v1/crates/new");
t!(fs::create_dir_all(path.parent().unwrap()));
t!(fs::write(&path, body));

// Get the metadata of the package
let (len, remaining) = body.split_at(4);
let json_len = u32::from_le_bytes(len.try_into().unwrap());
Expand Down
120 changes: 76 additions & 44 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {

#[cargo_test]
fn publish_with_registry_dependency() {
registry::alt_init();
let _reg = RegistryBuilder::new()
.http_api()
.http_index()
.alternative()
.build();

let p = project()
.file(
"Cargo.toml",
Expand All @@ -307,10 +312,26 @@ fn publish_with_registry_dependency() {

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

// Login so that we have the token available
p.cargo("login --registry alternative TOKEN").run();

p.cargo("publish --registry alternative").run();
p.cargo("publish --registry alternative")
.with_stderr(
"\
[UPDATING] `alternative` index
[WARNING] [..]
[..]
[PACKAGING] foo v0.0.1 [..]
[UPDATING] `alternative` index
[VERIFYING] foo v0.0.1 [..]
[DOWNLOADING] [..]
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
[COMPILING] bar v0.0.1 (registry `alternative`)
[COMPILING] foo v0.0.1 [..]
[FINISHED] [..]
[PACKAGED] [..]
[UPLOADING] foo v0.0.1 [..]
[UPDATING] `alternative` index
",
)
.run();

validate_alt_upload(
r#"{
Expand Down Expand Up @@ -415,48 +436,43 @@ or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN",

#[cargo_test]
fn publish_to_alt_registry() {
registry::alt_init();
let p = project().file("src/main.rs", "fn main() {}").build();

// Setup the registry by publishing a package
Package::new("bar", "0.0.1").alternative(true).publish();
let _reg = RegistryBuilder::new()
.http_api()
.http_index()
.alternative()
.build();

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

// Now perform the actual publish
p.cargo("publish --registry alternative").run();

validate_alt_upload(
r#"{
"authors": [],
"badges": {},
"categories": [],
"deps": [],
"description": null,
"documentation": null,
"features": {},
"homepage": null,
"keywords": [],
"license": null,
"license_file": null,
"links": null,
"name": "foo",
"readme": null,
"readme_file": null,
"repository": null,
"homepage": null,
"documentation": null,
"vers": "0.0.1"
}"#,
"foo-0.0.1.crate",
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
);
p.cargo("publish --registry alternative")
.with_stderr(
"\
[UPDATING] `alternative` index
[WARNING] [..]
[..]
[PACKAGING] foo v0.0.1 [..]
[VERIFYING] foo v0.0.1 [..]
[COMPILING] foo v0.0.1 [..]
[FINISHED] [..]
[PACKAGED] [..]
[UPLOADING] foo v0.0.1 [..]
[UPDATING] `alternative` index
",
)
.run();
}

#[cargo_test]
fn publish_with_crates_io_dep() {
registry::alt_init();
// crates.io registry.
let _dummy_reg = registry::init();
// Alternative registry.
let _alt_reg = RegistryBuilder::new()
.http_api()
.http_index()
.alternative()
.build();
let p = project()
.file(
"Cargo.toml",
Expand All @@ -477,10 +493,26 @@ fn publish_with_crates_io_dep() {

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

// Login so that we have the token available
p.cargo("login --registry alternative TOKEN").run();

p.cargo("publish --registry alternative").run();
p.cargo("publish --registry alternative")
.with_stderr(
"\
[UPDATING] `alternative` index
[WARNING] [..]
[..]
[PACKAGING] foo v0.0.1 [..]
[UPDATING] `dummy-registry` index
[VERIFYING] foo v0.0.1 [..]
[DOWNLOADING] [..]
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
[COMPILING] bar v0.0.1
[COMPILING] foo v0.0.1 [..]
[FINISHED] [..]
[PACKAGED] [..]
[UPLOADING] foo v0.0.1 [..]
[UPDATING] `alternative` index
",
)
.run();

validate_alt_upload(
r#"{
Expand Down
14 changes: 2 additions & 12 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! the new `dep = { artifact = "bin", … }` syntax in manifests.

use cargo_test_support::compare::match_exact;
use cargo_test_support::registry::Package;
use cargo_test_support::registry::{Package, RegistryBuilder};
use cargo_test_support::{
basic_bin_manifest, basic_manifest, cross_compile, project, publish, registry, rustc_host,
Project,
Expand Down Expand Up @@ -1872,8 +1872,7 @@ fn env_vars_and_build_products_for_various_build_targets() {

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

Package::new("bar", "1.0.0").publish();
Package::new("baz", "1.0.0").publish();
Expand Down Expand Up @@ -1903,15 +1902,6 @@ fn publish_artifact_dep() {
.file("src/lib.rs", "")
.build();

// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
// the index.
//
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
// the registry from processing the publish.
Package::new("foo", "0.1.0")
.file("src/lib.rs", "")
.publish();

p.cargo("publish -Z bindeps --no-verify")
.replace_crates_io(registry.index_url())
.masquerade_as_nightly_cargo(&["bindeps"])
Expand Down
26 changes: 18 additions & 8 deletions tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,10 @@ fn z_flags_rejected() {

#[cargo_test]
fn publish_allowed() {
let registry = registry::init();
let registry = registry::RegistryBuilder::new()
.http_api()
.http_index()
.build();

let p = project()
.file(
Expand All @@ -627,16 +630,23 @@ fn publish_allowed() {
.file("src/lib.rs", "")
.build();

// HACK: Inject `a` directly into the index so `publish` won't block for it to be in
// the index.
//
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
// the registry from processing the publish.
Package::new("a", "0.0.1").file("src/lib.rs", "").publish();

p.cargo("publish")
.replace_crates_io(registry.index_url())
.masquerade_as_nightly_cargo(&["test-dummy-unstable"])
.with_stderr(
"\
[UPDATING] [..]
[WARNING] [..]
[..]
[PACKAGING] a v0.0.1 [..]
[VERIFYING] a v0.0.1 [..]
[COMPILING] a v0.0.1 [..]
[FINISHED] [..]
[PACKAGED] [..]
[UPLOADING] a v0.0.1 [..]
[UPDATING] [..]
",
)
.run();
}

Expand Down
18 changes: 5 additions & 13 deletions tests/testsuite/credential_process.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Tests for credential-process.

use cargo_test_support::registry::{Package, TestRegistry};
use cargo_test_support::registry::TestRegistry;
use cargo_test_support::{basic_manifest, cargo_process, paths, project, registry, Project};
use std::fs::{self, read_to_string};

Expand Down Expand Up @@ -69,6 +69,8 @@ or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
fn warn_both_token_and_process() {
// Specifying both credential-process and a token in config should issue a warning.
let _server = registry::RegistryBuilder::new()
.http_api()
.http_index()
.alternative()
.no_configure_token()
.build();
Expand All @@ -77,7 +79,7 @@ fn warn_both_token_and_process() {
".cargo/config",
r#"
[registries.alternative]
token = "sekrit"
token = "alternative-sekrit"
credential-process = "false"
"#,
)
Expand All @@ -96,16 +98,6 @@ fn warn_both_token_and_process() {
.file("src/lib.rs", "")
.build();

// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
// the index.
//
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
// the registry from processing the publish.
Package::new("foo", "0.1.0")
.file("src/lib.rs", "")
.alternative(true)
.publish();

p.cargo("publish --no-verify --registry alternative -Z credential-process")
.masquerade_as_nightly_cargo(&["credential-process"])
.with_status(101)
Expand All @@ -127,7 +119,7 @@ Only one of these values may be set, remove one or the other to proceed.
credential-process = "false"
[registries.alternative]
token = "sekrit"
token = "alternative-sekrit"
"#,
);
p.cargo("publish --no-verify --registry alternative -Z credential-process")
Expand Down
27 changes: 4 additions & 23 deletions tests/testsuite/features_namespaced.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for namespaced features.

use super::features2::switch_to_resolver_2;
use cargo_test_support::registry::{self, Dependency, Package};
use cargo_test_support::registry::{Dependency, Package, RegistryBuilder};
use cargo_test_support::{project, publish};

#[cargo_test]
Expand Down Expand Up @@ -858,8 +858,7 @@ bar v1.0.0

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

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

// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
// the index.
//
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
// the registry from processing the publish.
Package::new("foo", "0.1.0")
.file("src/lib.rs", "")
.publish();

p.cargo("publish --no-verify")
.replace_crates_io(registry.index_url())
.with_stderr(
Expand Down Expand Up @@ -984,8 +974,7 @@ feat = ["opt-dep1"]

#[cargo_test]
fn publish() {
// HACK below allows us to use a local registry
let registry = registry::init();
let registry = RegistryBuilder::new().http_api().http_index().build();

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

// HACK: Inject `foo` directly into the index so `publish` won't block for it to be in
// the index.
//
// This is to ensure we can verify the Summary we post to the registry as doing so precludes
// the registry from processing the publish.
Package::new("foo", "0.1.0")
.file("src/lib.rs", "")
.publish();

p.cargo("publish")
.replace_crates_io(registry.index_url())
.with_stderr(
"\
[UPDATING] [..]
[PACKAGING] foo v0.1.0 [..]
[VERIFYING] foo v0.1.0 [..]
[UPDATING] [..]
[COMPILING] foo v0.1.0 [..]
[FINISHED] [..]
[PACKAGED] [..]
Expand Down

0 comments on commit 65cab34

Please sign in to comment.