-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add fixtures_path
in sqlx::test args
#2545
Conversation
@abonander have you got time to check this one? Let me know if any change is needed. Thanks! |
It would be beneficial to be able to provide a string relative to the root of the project so that the relative position doesn't need to be changed depending on the folder of the test. Alternatively being able to provide a path via an ENV var would be helpful in order to avoid rewriting the fixture names each time. |
Umh, this could be nice, through the ENV var only probably, because if done through Furthermore, an ENV variable could be applied to the standard @abonander, what do you think? |
Sorry for the delay. When I wrote this comment, I didn't fully grasp the existing design of this PR nor did I properly explain what I had in mind. I think there should be a few different operating modes that should cover most use cases:
// Executes `./fixtures/users.sql` then `./fixtures/posts.sql`
#[sqlx::test(fixtures("users", "posts"))]
// Executes `../fixtures/users.sql` then `../fixtures/posts.sql`
#[sqlx::test(fixtures("../fixtures/users.sql", "../fixtures.posts.sql"))] Note that the
// Executes `../fixtures/users.sql` then `../fixtures/posts.sql`
#[sqlx::test(
fixtures_path = "../fixtures",
fixtures("users", "posts")
)] For both the second and third style, we can define a placeholder variable that fills in the path of the workspace root, so multiple crates in the same workspace can share fixtures: // Executes `$WORKSPACE/fixtures/users.sql` then `$WORKSPACE/fixtures/posts.sql`
#[sqlx::test(
fixtures("$WORKSPACE/fixtures/users.sql", "$WORKSPACE/fixtures/posts.sql")
)]
// Executes `$WORKSPACE/fixtures/users.sql` then `$WORKSPACE/fixtures/posts.sql`
#[sqlx::test(
fixtures_path = "$WORKSPACE/fixtures",
fixtures("users", "posts")
)] I suppose this could be environment variable expansion, but Some alternative syntaxes for the third style (open to opinions here): // All of these execute `../fixtures/users.sql` then `../fixtures/posts.sql`
#[sqlx::test(
fixtures(
// This feels... naked.
path = "../fixtures",
"users",
"posts"
)
)]
#[sqlx::test(
fixtures(
// This seems to imply "include all fixtures in this directory"
path("../fixtures"),
"users",
"posts"
)
)]
#[sqlx::test(
fixtures(
path = "../fixtures",
// Better encapsulated, but not sure about naming this `files`
files("users", "posts")
)
)] Having the path inside // Executes, in order:
// `$WORKSPACE/fixtures/users.sql`
// `$WORKSPACE/fixtures/posts.sql`
// `./fixtures/comments.sql`
#[sqlx::test(
fixtures(
path("$WORKSPACE/fixtures"),
"users",
"posts"
),
fixtures("comments")
)] Although I feel like that's starting to make the feature a little too complicated for its own good. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See: #2545 (comment)
(wish I could mark an existing comment for this)
…low combining multiple fixtures parameters
I've implemented the first two types of operating modes, which can be combined with multiple Regarding the third operating mode, I would go with the following:
regarding the naming, maybe For now, I would avoid the use of a placeholder variable. |
I strongly disagree, but perhaps I was a bit ambiguous with my wording. We should require the path to actually match what it is on the filesystem to make it more distinct and limit confusion. We shouldn't be adding anything to it, or removing anything from it. If the file exists as In short, it would violate the principle of least astonishment. I don't care if the path actually ends with
That sounds good to me. |
Ok now I got it, thanks for explaining more in details. To recap, now there are 3 styles that can be used inside a
The above can be combined using multiple
If that works for you, I'll update the documentation as final step. |
In this case I don't want to require that the extension be Instead, I would detect this case by seeing if it looks like a relative (or absolute) path: let path = Path::new(path_str);
// This will be `true` if there's at least one path separator (`/` or `\`)
// It's also true for all absolute paths, even e.g. `/foo.sql` as the root directory is counted as a component.
let is_explicit_path = path.components().count() > 1; Also, before adding let has_extension = Path::new(path_str).extension().is_some(); |
…tension requirement for explicit path, they still need an extension. Add .sql extension to implicit fixtures style only if missing.
Done. I've also updated the docs accordingly. Let me know if you have any other comment, and thanks for your time! |
Looks good, thanks! |
Motivation
In big projects, with a lot of integration tests, it happens that the same fixture needs to be used in multiple tests which are structured in sub folders, leading to duplicated
./fixtures/
files.Changes
This PR adds
fixtures_path
, allowing to use fixtures outside the static path used byfixtures
(i.e.,fixtures/{file_name}.sql
).E.g.,
#[sqlx::test(fixtures_with_path("../other_dir/fixtures/users"))]
.Currently it is accepted only one between
fixtures
andfixtures_path
, like formigrations
andmigrator
. This mostly to avoid issues with the execution order of the fixtures themselves.Related Issues
Closes #2539