-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Move unit tests to separate directories, and document #8886
Conversation
I should add that the way things are today was quite concerning to @p01arst0rm when working on the Meson fork. |
good PR 👍 |
f5d40b0
to
83d0e79
Compare
aa1c15a
to
4852a5d
Compare
4852a5d
to
5b4c844
Compare
ad98a09
to
5705c8d
Compare
5705c8d
to
8d554a9
Compare
Discussed in Nix team meeting:
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2023-11-13-nix-team-meeting-minutes-103/35400/1 |
8d554a9
to
d096395
Compare
Somewhat surprisingly, this has become relevant for the Windows work (I need to separate the test exes and tests libraries better, Let me know if this looks good to you, @thufschmitt. (The conflicts are usually extremely superficial and easy to fix.) |
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.
A couple of small things in the testing manual, but looks good otherwise.
Feel free to merge once it's addressed (or discarded, I don't feel too strongly about these)
### Rationale | ||
|
||
The use of a separate directory for the unit tests might seem inconvenient, as the tests are not "right next to" the part of the code they are testing. | ||
But organizing the tests this way has one big benefit: | ||
there is no risk of any build-system wildcards for the library accidentally picking up test code that shoud not built and installed as part of the library. | ||
|
||
Likewise, the use of the `test/` subdir might seem superfluous: | ||
Isn't the point of the `*-test` subdir to indicate that these files are tests? | ||
Why do we need another `test` subdirectory? | ||
The answer is that we need to be able to tell apart the two headers, and make sure we include the right one. | ||
For `.cc` files, this is matter of doing `#include "foo.hh"` vs `#include "tests/foo.hh` | ||
For `.hh` files, this is a bit more subtle, because a `#include "foo.hh` instead `test/foo.hh` will end up including itself because `#include "..."` | ||
[always prioritizes files in the same directory](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html#tag_20_11_04), rather than the original header from the library. | ||
Instead, use `#include <foo.hh>` to get the original library header, and `#include "tests/foo.hh"` to get the test header. | ||
|
||
Why do we have test headers at all? | ||
These are usually for [rapidcheck]'s `Arbitrary` machinery, which is used to describe how to generate values of a given type for the sake of running property tests. | ||
Because types contain other types, `Arbitrary` "instances" for some type are not just useful for testing that type, but also any other type that contains it. | ||
Indeed, if we don't reuse the upstream type's `Arbitrary` instance, the downstream type's `Arbitrary` instance would become much more complex and hard to understand. | ||
In order to reuse these instances, we therefore declare them in these testing headers. |
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.
Is don't think we care a bout that at all here. This is nice for a commit message, but users/contributors generally shouldn't be really concerned about it, and wouldn't read it any way.
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.
I think it's the generally he right place for this kind of contextual information, but we may consider moving that out of the manual and into the tree. (At some point in the future, that is.)
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.
I want contributors to also write property tests and "Arbitrary instances", so I do want that pattern to be documented.
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.
Amid many other changes this text is now reorganized out of the file layout section. Hope that is better.
c10b7c7
to
ee03d39
Compare
@thufschmitt I am embarrassed to say that my "I'll just fix conflicts after" was wrong, so I think this needs to be re-reviewed. Here is what happened:
So one problem is no longer solved in the name of concensus, but another problem is solved (the windows problem), so this PR is hopefully still just as net useful. |
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2023-11-27-nix-team-meeting-minutes-107/36112/1 |
10f8db7
to
0f54ece
Compare
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.
Thoughts:
- This moves from functional grouping to technical group, which is a small regression in DX, but that's something because meson requires it anyway, and meson will fix more important issues
- Significant churn. @Ericson2314 commits to backporting and helping out with PR rebasing.
- Fixes issues as described
Conclusion: net positive
Today, with the tests inside a `tests` intermingled with the corresponding library's source code, we have a few problems: - We have to be careful that wildcards don't end up with tests being built as part of Nix proper, or test headers being installed as part of Nix proper. - Tests in libraries but not executables is not right: - It means each executable runs the previous unit tests again, because it needs the libraries. - It doesn't work right on Windows, which doesn't want you to load a DLL just for the side global variable . It could be made to work with the dlopen equivalent, but that's gross! This reorg solves these problems. There is a remaining problem which is that sibbling headers (like `hash.hh` the test header vs `hash.hh` the main `libnixutil` header) end up shadowing each other. This PR doesn't solve that. That is left as future work for a future PR. Co-authored-by: Valentin Gagarin <[email protected]>
0f54ece
to
91b6833
Compare
Backport failed for Please cherry-pick the changes locally and resolve any conflicts. git fetch origin 2.18-maintenance
git worktree add -d .worktree/backport-8886-to-2.18-maintenance origin/2.18-maintenance
cd .worktree/backport-8886-to-2.18-maintenance
git switch --create backport-8886-to-2.18-maintenance
git cherry-pick -x 91b6833686a6a6d9eac7f3f66393ec89ef1d3b57 |
Backport failed for Please cherry-pick the changes locally and resolve any conflicts. git fetch origin 2.19-maintenance
git worktree add -d .worktree/backport-8886-to-2.19-maintenance origin/2.19-maintenance
cd .worktree/backport-8886-to-2.19-maintenance
git switch --create backport-8886-to-2.19-maintenance
git cherry-pick -x 91b6833686a6a6d9eac7f3f66393ec89ef1d3b57 |
Backport failed for Please cherry-pick the changes locally and resolve any conflicts. git fetch origin 2.18-maintenance
git worktree add -d .worktree/backport-8886-to-2.18-maintenance origin/2.18-maintenance
cd .worktree/backport-8886-to-2.18-maintenance
git switch --create backport-8886-to-2.18-maintenance
git cherry-pick -x 91b6833686a6a6d9eac7f3f66393ec89ef1d3b57 |
Backport failed for Please cherry-pick the changes locally and resolve any conflicts. git fetch origin 2.19-maintenance
git worktree add -d .worktree/backport-8886-to-2.19-maintenance origin/2.19-maintenance
cd .worktree/backport-8886-to-2.19-maintenance
git switch --create backport-8886-to-2.19-maintenance
git cherry-pick -x 91b6833686a6a6d9eac7f3f66393ec89ef1d3b57 |
Motivation
Today, with the tests inside a
tests
intermingled with the corresponding library's source code, we have a few problems:We have to be careful that wildcards don't end up with tests being built as part of Nix proper, or test headers being installed as part of Nix proper.
Tests in libraries but not executables is not right:
It means each executable runs the previous unit tests again, because
it needs the libraries.
It doesn't work right on Windows, which doesn't want you to load a DLL just for the side global variable . It could be made to work with the dlopen equivalent, but that's gross!
This reorg solves these problems.
There is a remaining problem which is that sibbling headers (like
hash.hh
the test header vshash.hh
the mainlibnixutil
header) end up shadowing each other. This PR doesn't solve that. That is left as future work for a future PR.Context
This does something more minimal than what #7876 asks for, but it is a step in the right direction.
Checklist for maintainers
Maintainers: tick if completed or explain if not relevant
tests/**.sh
src/*/tests
tests/nixos/*
Priorities
Add 👍 to pull requests you find important.