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
19 changes: 19 additions & 0 deletions crates/crates_io_smoke_test/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::exit_status_ext::ExitStatusExt;
use secrecy::{ExposeSecret, SecretString};
use std::path::Path;
use std::process::Output;
use tokio::process::Command;

#[allow(unstable_name_collisions)]
Expand Down Expand Up @@ -43,3 +44,21 @@
.exit_ok()
.map_err(Into::into)
}

pub async fn publish_with_output(
project_path: &Path,
token: &SecretString,
) -> anyhow::Result<Output> {
Command::new("cargo")
.args(["publish", "--registry", "staging"])
.current_dir(project_path)
.env("CARGO_TERM_COLOR", "always")
.env(
"CARGO_REGISTRIES_STAGING_INDEX",
"https://github.com/rust-lang/staging.crates.io-index",
)
.env("CARGO_REGISTRIES_STAGING_TOKEN", token.expose_secret())
.output()
.await
.map_err(Into::into)
}

Check warning on line 64 in crates/crates_io_smoke_test/src/cargo.rs

View check run for this annotation

Codecov / codecov/patch

crates/crates_io_smoke_test/src/cargo.rs#L48-L64

Added lines #L48 - L64 were not covered by tests
17 changes: 16 additions & 1 deletion crates/crates_io_smoke_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
extern crate tracing;

use crate::api::ApiClient;
use anyhow::{anyhow, Context};
use anyhow::{anyhow, bail, Context};
use clap::Parser;
use secrecy::SecretString;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -65,13 +65,28 @@
.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}");
}
}

Check warning on line 81 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#L68-L81

Added lines #L68 - L81 were not covered by tests
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;

Check warning on line 89 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#L89

Added line #L89 was not covered by tests
} else {
info!("Publishing to staging.crates.io…");
cargo::publish(&project_path, &options.token)
Expand Down