launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
12.36k stars 1.18k forks source link

sqlx::test error message incorrectly implies `migrate` feature is disabled #3275

Open nk9 opened 3 weeks ago

nk9 commented 3 weeks ago

Bug Description

Summary

Error message implies that the "migrate" feature isn't enabled when it actually is. When the feature isn't enabled, the error doesn't mention that it's necessary.

Detail

I am receiving the following error message when I use cargo test:

error: control attributes are not allowed unless the `migrate` feature is enabled and automatic test DB management is used; see docs

This error is generated on test_attr.rs:36:

    if input.sig.inputs.is_empty() {
        if !args.is_empty() {
            if cfg!(feature = "migrate") {
                return Err(syn::Error::new_spanned( // <--- line 36
                    args.first().unwrap(),
                    "control attributes are not allowed unless \
                        the `migrate` feature is enabled and \
                        automatic test DB management is used; see docs",
                )
                .into());
            }

            return Err(syn::Error::new_spanned(
                args.first().unwrap(),
                "control attributes are not allowed unless \
                    automatic test DB management is used; see docs",
            )
            .into());
        }

        return Ok(expand_simple(input));
    }

However, my setup DOES include the default migrate feature.

Cargo.toml

[package]
name = "test-rust2"
version = "0.1.0"
edition = "2021"

[dependencies]
sqlx = { version = "0.7.4", features = ["runtime-tokio-rustls"] }
🕙 00:54:42 ❯ cargo add sqlx --dry-run
    Updating crates.io index
      Adding sqlx v0.7.4 to dependencies
             Features:
             + _rt-tokio
             + any
             + json
             + macros
             + migrate
             + runtime-tokio
             + runtime-tokio-rustls
             + sqlx-macros
             + tls-rustls
             24 deactivated features
warning: aborting add due to dry run

The code above appears to give the extra "unless the migrate feature is enabled" wording only when… the migrate feature is enabled. (if cfg!(feature = "migrate")) However, the time when that caveat is relevant is when the migrate feature is not enabled. Otherwise, the error should just point to "automatic test DB management" as the issue.

Minimal Reproduction

Cargo.toml as above, with main.rs:

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {
    #[sqlx::test(migrations = "../sibling-dir/migrations")]
    fn name() {
        assert_eq!(1, 1);
    }
}

Info

wadefletch commented 2 weeks ago

Also experiencing this issue, though only when the fixtures argument is passed. It does not occur with a plain sqlx::test macro.

Does not cause the error message: #[sqlx::test] (Migrations are applied successfully, in this case.) Causes the error message: #[sqlx::test(fixtures("graphs")]

Edit: This was resolved by adding a pool: PgPool arg to the test function signature.

nk9 commented 2 weeks ago

Yep, the code path that leads to the incorrect error is only triggered when the test macro has arguments. And adding one of the various arguments to the function enables Automatic Test DB Management, which means the error is no longer necessary.