Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ repos:
- id: trailing-whitespace
args: [ --markdown-linebreak-ext=md ]
exclude: '\.svg'
- repo: local
hooks:
- id: check-test-file-name
name: check test file name convention
entry: python tools/check-test-file-name.py
language: python
files: '(tests/.*\.py)$'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I understand correctly that it will flag any file that does not end with _test?
Sometimes we may have some code in a module that isn't a test but contains common code for setting up tests? And I think these are not always in the conftest.py?

That's probably the case for the mcstas example thing. Is it always possible to move to a conftest file, or are there cases where we cannot?

Also, do we ever have non-python files in the test/ folder, which could end in something else? Like some data files for testing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably the case for the mcstas example thing. Is it always possible to move to a conftest file, or are there cases where we cannot?

I guess we can always use the conftest file ...

And then we can accept all non-python files under the tests folder.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably the case for the mcstas example thing. Is it always possible to move to a conftest file, or are there cases where we cannot?

This file is imported by only one other file. So as long as it stays that way, we can move the contents to that importing file.

Also, do we ever have non-python files in the test/ folder, which could end in something else? Like some data files for testing?

The regex will only look for .py files. So anything else won't be checked and therefore will be accepted.

Did I understand correctly that it will flag any file that does not end with _test?
Sometimes we may have some code in a module that isn't a test but contains common code for setting up tests? And I think these are not always in the conftest.py?

Correct. I think in principle, we can always move common code into fixtures. But that does not always make sense.
Looking through our ess* packages, I found one case in loki: common.py. This defines a single function to create a workflow which could be provided as a fixture. We do that in other packages.
And esslivedata has a number of modules in its tests. But they seem to define fixtures. So they could be moved into a conftest.py file. But that file would be fairly large.

- repo: https://github.com/kynan/nbstripout
rev: 0.7.1
hooks:
Expand Down
21 changes: 21 additions & 0 deletions tools/check-test-file-name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path
import sys

ALLOWED_NAMES = ("conftest.py",)


def main():
errors = False
paths = map(Path, sys.argv[1:])
for path in paths:
if path.name in ALLOWED_NAMES:
continue
if not path.stem.endswith("_test"):
sys.stderr.write(f"Bad test file name: {path}\n")
errors = True

sys.exit(int(errors))


if __name__ == "__main__":
main()
Loading