Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the name of "inner" function generated by #[sqlx::test] #3072

Merged
merged 1 commit into from
Feb 25, 2024

Conversation

ciffelia
Copy link
Contributor

@ciffelia ciffelia commented Feb 22, 2024

Summary

This patch addresses an issue with the #[sqlx::test] attribute macro that causes problems when used in conjunction with the insta snapshot testing library.

#[sqlx::test(migrator = "mymodule::migrator")]
async fn select_foo(pool: PgPool) {
    let res = my_function_to_test(pool);
    insta::assert_debug_snapshot!(res); // we expect insta to generate a snapshot file named `mymodule__tests__select_foo.snap`
} 

When using sqlx::test, the test code is wrapped inside an inner function, which can lead to snapshot files being generated with the name of the inner function (inner), causing filename conflicts when multiple functions exist within the same module.

// expanded code
#[test]
fn select_foo() {
    async fn inner(pool: PgPool) {
        let res = my_function_to_test(pool);
        insta::assert_debug_snapshot!(res); // insta generates a snapshot file named `mymodule__tests__inner.snap`
    }
    // ...
    let f: fn(_) -> _ = inner;
    ::sqlx::testing::TestFn::run_test(f, args)
} 

mitsuhiko/insta#434

This PR modifies the sqlx::test macro to ensure that insta uses the correct function name (select_foo in this case) when generating snapshot files, thereby avoiding filename conflicts and ensuring that snapshot tests work as expected.

Changes

Modified the sqlx::test macro to generate the inner function with the original function name (select_foo in this case) when generating the test function, ensuring that insta uses the correct function name when generating snapshot files.

// expanded code
#[test]
fn select_foo() {
    async fn select_foo(pool: PgPool) {
        let res = my_function_to_test(pool);
        insta::assert_debug_snapshot!(res); // insta generates a snapshot file named `mymodule__tests__select_foo.snap`
    }
    // ...
    let f: fn(_) -> _ = select_foo;
    ::sqlx::testing::TestFn::run_test(f, args)
} 

@ciffelia ciffelia marked this pull request as ready for review February 22, 2024 15:07
@abonander
Copy link
Collaborator

I'm honestly surprised this doesn't clash. I figured the compiler would forbid this.

@abonander abonander merged commit 8c43b8c into launchbadge:main Feb 25, 2024
64 checks passed
@ciffelia ciffelia deleted the patch-1 branch February 25, 2024 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants