pgrx icon indicating copy to clipboard operation
pgrx copied to clipboard

v0.12.0-beta.2 Causes Warnings while running Tests

Open theory opened this issue 1 year ago • 9 comments

See, for example, this build, which outputs a couple of these:

warning: unused import: `std::error::Error`
   --> src/lib.rs:303:9
    |
303 |     use std::error::Error;
    |         ^^^^^^^^^^^^^^^^^
    |
help: if this is a test module, consider adding a `#[cfg(test)]` to the containing module
   --> src/lib.rs:297:1
    |
297 | #[pg_schema]
    | ^^^^^^^^^^^^
    = note: `#[warn(unused_imports)]` on by default
    = note: this warning originates in the attribute macro `pg_schema` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `jsonschema` (lib) generated 1 warning (run `cargo fix --lib -p jsonschema` to apply 1 suggestion)

Note that std::error::Error is used in the module and cargo fix --lib -p jsonschema makes no changes. I'm not sure why it thinks pg_schema is unused.

Do I need to put pgrx tests in a module separate from regular Rust tests, perhaps?

Potentially related: #1631

theory avatar Jul 10 '24 18:07 theory

Sad to see this still in v0.12.1:

warning: unused import: `std::error::Error`
   --> src/lib.rs:303:9
    |
303 |     use std::error::Error;
    |         ^^^^^^^^^^^^^^^^^
    |
help: if this is a test module, consider adding a `#[cfg(test)]` to the containing module
   --> src/lib.rs:297:1
    |
297 | #[pg_schema]
    | ^^^^^^^^^^^^
    = note: `#[warn(unused_imports)]` on by default
    = note: this warning originates in the attribute macro `pg_schema` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `jsonschema` (lib) generated 1 warning (run `cargo fix --lib -p jsonschema` to apply 1 suggestion)

You can replicate it thusly:

git clone https://github.com/tembo-io/pg-jsonschema-boon.git
cd pg-jsonschema-boon
make test

theory avatar Aug 26 '24 18:08 theory

This is going to be a little confusing, so bear with me.

Your mod tests {} (https://github.com/tembo-io/pg-jsonschema-boon/blob/280578f3832d309ea9e95b4778648e5497ad849a/src/lib.rs#L298) uses both rust #[test] functions and pgrx #[pg_test] functions.

It is only the #[test] functions that use std::err::Error. So when cargo-pgrx compiles your extension for its test mode (with the --features pg_test feature turned on), the #[pg_test] functions don't use std::err::Error so you get this warning.

You could split the #[test] functions out into a separate #[cfg(test)] mod {}, and I believe that would take care of this warning. AFAICS, this isn't a pgrx-specific issue.

EDIT: You could also annotate the use statement like so, and leave everything in the same module:

#[cfg(test)]
use std::err::Error;

eeeebbbbrrrr avatar Aug 26 '24 18:08 eeeebbbbrrrr

Oooh! So weird it didn't happen in v0.11.x… I'll take look, thanks.

theory avatar Aug 26 '24 19:08 theory

hmm. I can't answer that. Lots of little bits and bobs have changed between then and now.

eeeebbbbrrrr avatar Aug 26 '24 19:08 eeeebbbbrrrr

Okay, that worked, but it took some fiddling. Looks like #[pg_schema] only works for a module named tests, is that right?

theory avatar Aug 26 '24 19:08 theory

Looks like #[pg_schema] only works for a module named tests, is that right?

Hmm. <thinking...>

Yes, I think that's true as the test runner issues sql like SELECT tests.test_name();, so it expects all the tests to be in that schema. Been years since we implemented that, but I think it's because it's basically really hard/impossible to figure out the mod {} name through our test runner framework.

eeeebbbbrrrr avatar Aug 26 '24 19:08 eeeebbbbrrrr

Fixed in tembo-io/pg-jsonschema-boon#5.

theory avatar Aug 26 '24 19:08 theory

but I think it's because it's basically really hard/impossible to figure out the mod {} name through our test runner framework.

Would it make sense to add a feature parameter with the name? Better to parse it, but perhaps a useable workaround?

theory avatar Aug 26 '24 19:08 theory

I don’t even know. I’ve never put any thought into it beyond the hardcoded value.

eeeebbbbrrrr avatar Aug 26 '24 20:08 eeeebbbbrrrr