Skip to content

Commit

Permalink
Fix test issue where warning was being raised but not captured (#151)
Browse files Browse the repository at this point in the history
* Fix test issue where warning was being raised but not captured

* Switch to recwarn instead of pytest.warn

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update parfive/tests/test_downloader.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update parfive/tests/test_downloader.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Stuart Mumford <[email protected]>
  • Loading branch information
3 people authored Apr 8, 2024
1 parent 7c70d4c commit dcbfe9f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions parfive/tests/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,14 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def run(self):
self.result = self._target(*self._args, **self._kwargs)
try:
self.result = self._target(*self._args, **self._kwargs)
finally:
del self._target, self._args, self._kwargs


@skip_windows
def test_download_out_of_main_thread(httpserver, tmpdir):
def test_download_out_of_main_thread(httpserver, tmpdir, recwarn):
tmpdir = str(tmpdir)
httpserver.serve_content(
"SIMPLE = T", headers={"Content-Disposition": "attachment; filename=testfile.fits"}
Expand All @@ -593,11 +596,16 @@ def test_download_out_of_main_thread(httpserver, tmpdir):

thread = CustomThread(target=dl.download)
thread.start()

with pytest.warns(
UserWarning,
match="This download has been started in a thread which is not the main thread. You will not be able to interrupt the download.",
):
thread.join()
thread.join()

validate_test_file(thread.result)

# We use recwarn here as for some reason pytest.warns did not reliably pickup this warning.
assert len(recwarn) > 0
assert any(
[
"This download has been started in a thread which is not the main thread. You will not be able to interrupt the download."
== w.message.args[0]
for w in recwarn
]
)

0 comments on commit dcbfe9f

Please sign in to comment.