pgrx icon indicating copy to clipboard operation
pgrx copied to clipboard

Cannot Put `#[pg_schema]` Tests in Separate File

Open theory opened this issue 1 year ago • 3 comments

I created a new extension:

cargo pgrx new separate_tests

Then followed the first example in this Discord post to try to move tests into a separate file, replacing this:

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
// ...
}

With this:

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests;

Alas, the compiler doesn't like it:

error[E0658]: non-inline modules in proc macro input are unstable
  --> src/lib.rs:12:1
   |
12 | mod tests;
   | ^^^^^^^^^^
   |
   = note: see issue #54727 <https://github.com/rust-lang/rust/issues/54727> for more information

error: custom attribute panicked
  --> src/lib.rs:11:1
   |
11 | #[pg_schema]
   | ^^^^^^^^^^^^
   |
   = help: message: Can only support `mod {}` right now.

For more information about this error, try `rustc --explain E0658`.
error: could not compile `separate_tests` (lib test) due to 2 previous errors

As a secondary issue, having moved tests into tests.rs, the rust language server tells me that the file isn't part of the crate. I'd have to use this directive above mod tests; as well:

#[path = "./tests.rs"]

Though it might work if I instead created the file ./tests/mod.rs.

theory avatar Apr 03 '24 14:04 theory

Unfortunate.

As a secondary issue, having moved tests into tests.rs, the rust language server tells me that the file isn't part of the crate. I'd have to use this directive above mod tests; as well:

Which Rust language server are you using? Are you using RLS, or rust-analyzer?

workingjubilee avatar Apr 03 '24 22:04 workingjubilee

Which Rust language server are you using? Are you using RLS, or rust-analyzer?

rust-analyzer I think. I didn't realize VSCode was't using Rust's own RLS.

theory avatar Apr 03 '24 23:04 theory

It should be using rust-analyzer, which is our Rust language server, I just have to ask now and then because sometimes people wind up finding and enabling the legacy server.

I am very surprised rust-analyzer fails to detect the module. Odd.

workingjubilee avatar Apr 04 '24 00:04 workingjubilee