Skip to content
Open
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
20 changes: 18 additions & 2 deletions sqlx-cli/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub async fn run(
connect_opts: ConnectOpts,
cargo_args: Vec<String>,
) -> anyhow::Result<()> {
let cargo = env::var_os("CARGO")
.context("failed to get value of `CARGO`; `prepare` subcommand may only be invoked as `cargo sqlx prepare`")?;
let cargo = cargo_command(env::var_os("CARGO"))?;

anyhow::ensure!(
Path::new("Cargo.toml").exists(),
Expand All @@ -68,6 +67,13 @@ hint: This command only works in the manifest directory of a Cargo package or wo
}
}

fn cargo_command(cargo: Option<OsString>) -> anyhow::Result<OsString> {
cargo.context(
"the `prepare` subcommand must be invoked as `cargo sqlx prepare`; \
running `sqlx prepare` directly is not supported",
)
}

async fn prepare(ctx: &PrepareCtx<'_>) -> anyhow::Result<()> {
if ctx.connect_opts.database_url.is_some() {
check_backend(ctx.config, &ctx.connect_opts).await?;
Expand Down Expand Up @@ -372,6 +378,16 @@ mod tests {
use super::*;
use std::assert_eq;

#[test]
fn missing_cargo_environment_explains_prepare_invocation() {
let error = cargo_command(None).expect_err("missing CARGO should fail");

assert_eq!(
error.to_string(),
"the `prepare` subcommand must be invoked as `cargo sqlx prepare`; running `sqlx prepare` directly is not supported"
);
}

#[test]
fn minimal_project_recompile_action_works() -> anyhow::Result<()> {
let sample_metadata_path = Path::new("tests")
Expand Down
Loading