Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/crates_io_smoke_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ anyhow = "=1.0.93"
bytes = "=1.9.0"
clap = { version = "=4.5.21", features = ["derive", "env", "unicode", "wrap_help"] }
crates_io_index = { path = "../crates_io_index" }
rand = "=0.8.5"
reqwest = { version = "=0.12.9", features = ["gzip", "json"] }
secrecy = "=0.10.3"
semver = { version = "=1.0.23", features = ["serde"] }
Expand Down
109 changes: 72 additions & 37 deletions crates/crates_io_smoke_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use std::path::{Path, PathBuf};
use tempfile::tempdir;
use tokio::fs;
use tokio::io::AsyncWriteExt;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
Expand Down Expand Up @@ -53,8 +54,12 @@
let old_version = krate.max_version;
let mut new_version = old_version.clone();

new_version.patch += 1;
info!(%old_version, %new_version, "Calculated new version number");
if !options.skip_publish {
new_version.patch += 1;
info!(%old_version, %new_version, "Calculated new version number");
} else {
info!(%old_version, %new_version, "Using old version number since `--skip-publish` is set");
}

Check warning on line 62 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L57-L62

Added lines #L57 - L62 were not covered by tests

info!("Creating temporary working folder…");
let tempdir = tempdir().context("Failed to create temporary working folder")?;
Expand All @@ -65,37 +70,20 @@
.await
.context("Failed to create project")?;

info!("Checking publish with invalid authentication…");
let invalid_token = "invalid-token".into();
let output = cargo::publish_with_output(&project_path, &invalid_token).await?;
if output.status.success() {
bail!("Expected `cargo publish` to fail with invalid token");
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
if !stderr.contains("401 Unauthorized")
|| !stderr.contains("The given API token does not match the format used by crates.io")
{
bail!("Expected `cargo publish` to fail with an `401 Unauthorized` error, but got: {stderr}");
}
}

if options.skip_publish {
info!("Packaging crate file…");
cargo::package(&project_path)
.await
.context("Failed to run `cargo package`")?;

info!("Skipping publish step");
new_version = old_version;
} else {
info!("Publishing to staging.crates.io…");
cargo::publish(&project_path, &options.token)
.await
.context("Failed to run `cargo publish`")?;
}

drop(tempdir);

let version = new_version;
info!(%version, "Checking staging.crates.io API for the new version…");

Expand Down Expand Up @@ -176,6 +164,22 @@
return Err(anyhow!("Failed to find published version on the git index"));
}

if !options.skip_publish {
info!("Checking failed publish with large payload…");
create_dummy_content(&project_path).await?;

info!("Sending publish request…");
let output = cargo::publish_with_output(&project_path, &options.token).await?;
if output.status.success() {
bail!("Expected `cargo publish` to fail with invalid token");
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
if !stderr.contains("413 Payload Too Large") {
bail!("Expected `cargo publish` to fail with an `413 Payload Too Large` error, but got:\n{stderr}");
}
}
}

Check warning on line 182 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L167-L182

Added lines #L167 - L182 were not covered by tests
info!(
"All automated smoke tests have passed.\n\nPlease visit https://staging.crates.io/crates/{}/{} for further manual testing.",
&options.crate_name, &version
Expand All @@ -201,31 +205,16 @@
name: &str,
version: &semver::Version,
) -> anyhow::Result<PathBuf> {
let version = version.to_string();

Check warning on line 209 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L208-L209

Added lines #L208 - L209 were not covered by tests
cargo::new_lib(parent_path, name)
.await
.context("Failed to run `cargo new`")?;

let project_path = parent_path.join(name);
debug!(project_path = %project_path.display());

{
let manifest_path = project_path.join("Cargo.toml");
info!(manifest_path = %manifest_path.display(), "Overriding `Cargo.toml` file…");

let new_content = format!(
r#"[package]
name = "{name}"
version = "{version}"
edition = "2018"
license = "MIT"
description = "test crate"
"#,
);

fs::write(&manifest_path, new_content)
.await
.context("Failed to write `Cargo.toml` file content")?;
}
write_manifest(&project_path, name, &version).await?;

Check warning on line 217 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L217

Added line #L217 was not covered by tests

{
let readme_path = project_path.join("README.md");
Expand Down Expand Up @@ -259,3 +248,49 @@

Ok(project_path)
}

async fn write_manifest(project_path: &Path, name: &str, version: &str) -> anyhow::Result<()> {
let manifest_path = project_path.join("Cargo.toml");
info!(manifest_path = %manifest_path.display(), "Overriding `Cargo.toml` file…");

Check warning on line 254 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L252-L254

Added lines #L252 - L254 were not covered by tests

let new_content = format!(
r#"[package]
name = "{name}"
version = "{version}"
edition = "2018"
license = "MIT"
description = "test crate"
"#,
);

fs::write(&manifest_path, new_content)
.await
.context("Failed to write `Cargo.toml` file content")?;

Check warning on line 268 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L256-L268

Added lines #L256 - L268 were not covered by tests

Ok(())
}

Check warning on line 271 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L270-L271

Added lines #L270 - L271 were not covered by tests

async fn create_dummy_content(project_path: &Path) -> anyhow::Result<()> {

Check warning on line 273 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L273

Added line #L273 was not covered by tests
const FILE_SIZE: u32 = 15 * 1024 * 1024;

debug!("Creating `dummy.txt` file…");
let f = fs::File::create(project_path.join("dummy.txt")).await?;
let mut writer = tokio::io::BufWriter::new(f);
for _ in 0..(FILE_SIZE / 16) {
writer.write_u128(rand::random()).await?;

Check warning on line 280 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L276-L280

Added lines #L276 - L280 were not covered by tests
}
drop(writer);

write_manifest(project_path, "dummy", "0.0.0-dummy").await?;

Check warning on line 284 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L282-L284

Added lines #L282 - L284 were not covered by tests

debug!("Creating additional git commit…");
git::add_all(project_path)
.await
.context("Failed to add changes to git")?;

Check warning on line 289 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L286-L289

Added lines #L286 - L289 were not covered by tests

git::commit(project_path, "add dummy content")
.await
.context("Failed to commit changes")?;

Check warning on line 293 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L291-L293

Added lines #L291 - L293 were not covered by tests

Ok(())
}

Check warning on line 296 in crates/crates_io_smoke_test/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/main.rs#L295-L296

Added lines #L295 - L296 were not covered by tests