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

Use pytest tmp_dir fixture for safe test paths #4287

Merged
merged 1 commit into from
Dec 8, 2022
Merged

Conversation

mr-c
Copy link
Contributor

@mr-c mr-c commented Nov 28, 2022

As running the tests post-installation can result in errors otherwise. We need this for the Debian package of toil

Changelog Entry

To be copied to the draft changelog by merger:

  • Tests are now more runnable post-installation. Temporary paths are not selected based upon the location of the tests themselves.

Reviewer Checklist

  • Make sure it is coming from issues/XXXX-fix-the-thing in the Toil repo, or from an external repo.
    • If it is coming from an external repo, make sure to pull it in for CI with:
      contrib/admin/test-pr otheruser theirbranchname issues/XXXX-fix-the-thing
      
    • If there is no associated issue, create one.
  • Read through the code changes. Make sure that it doesn't have:
    • Addition of trailing whitespace.
    • New variable or member names in camelCase that want to be in snake_case.
    • New functions without type hints.
    • New functions or classes without informative docstrings.
    • Changes to semantics not reflected in the relevant docstrings.
    • New or changed command line options for Toil workflows that are not reflected in docs/running/{cliOptions,cwl,wdl}.rst
    • New features without tests.
  • Comment on the lines of code where problems exist with a review comment. You can shift-click the line numbers in the diff to select multiple lines.
  • Finish the review with an overall description of your opinion.

Merger Checklist

  • Make sure the PR passes tests.
  • Make sure the PR has been reviewed since its last modification. If not, review it.
  • Merge with the Github "Squash and merge" feature.
    • If there are multiple authors' commits, add Co-authored-by to give credit to all contributing authors.
  • Copy its recommended changelog entry to the Draft Changelog.
  • Append the issue number in parentheses to the changelog entry.

Copy link
Member

@adamnovak adamnovak left a comment

Choose a reason for hiding this comment

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

This looks OK to me.

@adamnovak
Copy link
Member

Good thing we have computers; the CI has informed me that actually this can never work, because it's adding parameters without default values to functions in the tests that start with test_. When pytest goes to run those tests, it doesn't have any values to pass for those arguments, and they fail thusly:

=================================== FAILURES ===================================
_______________ CWLSmallLogDir.test_filename_conflict_resolution _______________
Traceback (most recent call last):
  File "/usr/lib/python3.9/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.9/unittest/case.py", line 592, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.9/unittest/case.py", line 550, in _callTestMethod
    method()
TypeError: test_filename_conflict_resolution() missing 1 required positional argument: 'tmp_path'
---------------------------- Captured stdout setup -----------------------------


[TEST] toil.test.cwl.cwlTest.CWLSmallLogDir:test_filename_conflict_resolution (Dec 06 2022 02:27:46:396579 PST)


------------------------------ Captured log call -------------------------------
INFO     toil.test:__init__.py:113 Setting up toil.test.cwl.cwlTest.CWLSmallLogDir.test_filename_conflict_resolution ...
INFO     toil.test:__init__.py:118 Tore down toil.test.cwl.cwlTest.CWLSmallLogDir.test_filename_conflict_resolution

@mr-c If the path to use isn't going to come from os.path.dirname(__file__), where is it supposed to come from? It doesn't look like you included any calls to these test functions that pass the new argument you are adding.

I think either the new arguments need default values, or the functions need to be renamed to not start with test_, and the old names need to be applied to no-argument functions that compute and supply tmp_dir to the functions that do the actual testing.

@mr-c
Copy link
Contributor Author

mr-c commented Dec 6, 2022

tmp_dir is a built-in pytest fixture https://docs.pytest.org/en/6.2.x/fixture.html

@mr-c
Copy link
Contributor Author

mr-c commented Dec 6, 2022

So maybe someplace in the CI we are running the tests using unittest instead of pytest, or there is some other issue with how the Toil tests are structured.

@mr-c mr-c force-pushed the tests_safe_tmp_paths branch 2 times, most recently from 6cc7a53 to 4e819a2 Compare December 7, 2022 14:37
@mr-c
Copy link
Contributor Author

mr-c commented Dec 7, 2022

@adamnovak I converted the tmp_path using tests to an idiomatic pytest style; should work now

Copy link
Member

@adamnovak adamnovak left a comment

Choose a reason for hiding this comment

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

This can probably work? I'm worried that if we do everything like this we'll end up with a lot of marks, though, and we won't get to use the stuff we put in ToilTest.

@@ -35,6 +35,8 @@ markers =
tes
torque
wes_server
cwl_small_log_dir
Copy link
Member

Choose a reason for hiding this comment

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

Does this mark mean anything about the tests (like they need something) or is it just a category of tests so we can run them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, I didn't write the original test classes (CWLSmallLogDir , CWLSmallTests). I added the marks so that the cwl_misc gitlab job could still select them. We would split the test categories by files instead, then marks wouldn't needed.

As running the tests post-installation can result in errors otherwise
@mr-c
Copy link
Contributor Author

mr-c commented Dec 8, 2022

@adamnovak If you want, I can turn the ToilTest class attributes/methods into pytest fixtures in a separate PR. That will be more flexible and explicit.

@mr-c mr-c merged commit 4c7ee3c into master Dec 8, 2022
@mr-c mr-c deleted the tests_safe_tmp_paths branch December 8, 2022 10:03
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