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

test: Use tmp_path pytest fixture over tmpdir #2384

Merged
merged 17 commits into from
Nov 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ def datadir(tmp_path, request):
dir_util.copy_tree(test_dir, str(tmp_path))
# shutil is nicer, but doesn't work: https://bugs.python.org/issue20849
# Once pyhf is Python 3.8+ only then the below can be used.
# shutil.copytree(test_dir, tmpdir)
# shutil.copytree(test_dir, tmp_path)

return tmp_path
66 changes: 32 additions & 34 deletions tests/contrib/test_contrib_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tarfile
import zipfile
from pathlib import Path
from shutil import rmtree

import pytest
Expand All @@ -10,70 +9,69 @@


@pytest.fixture(scope="function")
def tarfile_path(tmpdir):
with open(
tmpdir.join("test_file.txt").strpath, "w", encoding="utf-8"
) as write_file:
def tarfile_path(tmp_path):
with open(tmp_path.joinpath("test_file.txt"), "w", encoding="utf-8") as write_file:
write_file.write("test file")
with tarfile.open(
tmpdir.join("test_tar.tar.gz").strpath, mode="w:gz", encoding="utf-8"
tmp_path.joinpath("test_tar.tar.gz"), mode="w:gz", encoding="utf-8"
) as archive:
archive.add(tmpdir.join("test_file.txt").strpath)
return Path(tmpdir.join("test_tar.tar.gz").strpath)
archive.add(tmp_path.joinpath("test_file.txt"))
return tmp_path.joinpath("test_tar.tar.gz")


@pytest.fixture(scope="function")
def tarfile_uncompressed_path(tmpdir):
with open(
tmpdir.join("test_file.txt").strpath, "w", encoding="utf-8"
) as write_file:
def tarfile_uncompressed_path(tmp_path):
with open(tmp_path.joinpath("test_file.txt"), "w", encoding="utf-8") as write_file:
write_file.write("test file")
with tarfile.open(
tmpdir.join("test_tar.tar").strpath, mode="w", encoding="utf-8"
tmp_path.joinpath("test_tar.tar"), mode="w", encoding="utf-8"
) as archive:
archive.add(tmpdir.join("test_file.txt").strpath)
return Path(tmpdir.join("test_tar.tar").strpath)
archive.add(tmp_path.joinpath("test_file.txt"))
return tmp_path.joinpath("test_tar.tar")


@pytest.fixture(scope="function")
def zipfile_path(tmpdir):
with open(
tmpdir.join("test_file.txt").strpath, "w", encoding="utf-8"
) as write_file:
def zipfile_path(tmp_path):
with open(tmp_path.joinpath("test_file.txt"), "w", encoding="utf-8") as write_file:
write_file.write("test file")
with zipfile.ZipFile(tmpdir.join("test_zip.zip").strpath, "w") as archive:
archive.write(tmpdir.join("test_file.txt").strpath)
return Path(tmpdir.join("test_zip.zip").strpath)
with zipfile.ZipFile(tmp_path.joinpath("test_zip.zip"), "w") as archive:
archive.write(tmp_path.joinpath("test_file.txt"))
return tmp_path.joinpath("test_zip.zip")


def test_download_untrusted_archive_host(tmpdir, requests_mock):
def test_download_untrusted_archive_host(tmp_path, requests_mock):
archive_url = "https://www.pyhfthisdoesnotexist.org"
requests_mock.get(archive_url)

with pytest.raises(InvalidArchiveHost):
download(archive_url, tmpdir.join("likelihoods").strpath)
download(archive_url, tmp_path.joinpath("likelihoods"))


def test_download_invalid_archive(tmpdir, requests_mock):
def test_download_invalid_archive(tmp_path, requests_mock):
archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
requests_mock.get(archive_url, status_code=404)

with pytest.raises(InvalidArchive):
download(archive_url, tmpdir.join("likelihoods").strpath)
download(archive_url, tmp_path.joinpath("likelihoods"))


def test_download_compress(tmpdir, requests_mock):
def test_download_compress(tmp_path, requests_mock):
archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
requests_mock.get(archive_url)

download(archive_url, tmpdir.join("likelihoods").strpath, compress=True)
download(archive_url, tmp_path.joinpath("likelihoods"), compress=True)


def test_download_archive_type(
tmpdir, mocker, requests_mock, tarfile_path, tarfile_uncompressed_path, zipfile_path
tmp_path,
mocker,
requests_mock,
tarfile_path,
tarfile_uncompressed_path,
zipfile_path,
):
archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
output_directory = tmpdir.join("likelihoods").strpath
output_directory = tmp_path.joinpath("likelihoods")
# Give BytesIO a tarfile
requests_mock.get(archive_url, content=open(tarfile_path, "rb").read())
download(archive_url, output_directory)
Expand All @@ -86,7 +84,7 @@ def test_download_archive_type(
requests_mock.get(archive_url, content=open(zipfile_path, "rb").read())
# Run without and with existing output_directory to cover both
# cases of the shutil.rmtree logic
rmtree(Path(output_directory))
rmtree(output_directory)
download(archive_url, output_directory) # without
download(archive_url, output_directory) # with

Expand All @@ -97,13 +95,13 @@ def test_download_archive_type(
download(archive_url, output_directory)


def test_download_archive_force(tmpdir, requests_mock, tarfile_path):
def test_download_archive_force(tmp_path, requests_mock, tarfile_path):
archive_url = "https://www.cern.ch/record/resource/123456789"
requests_mock.get(
archive_url, content=open(tarfile_path, "rb").read(), status_code=200
)

with pytest.raises(InvalidArchiveHost):
download(archive_url, tmpdir.join("likelihoods").strpath, force=False)
download(archive_url, tmp_path.joinpath("likelihoods"), force=False)

download(archive_url, tmpdir.join("likelihoods").strpath, force=True)
download(archive_url, tmp_path.joinpath("likelihoods"), force=True)
2 changes: 1 addition & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import shlex


def test_2bin_1channel(tmpdir, script_runner):
def test_2bin_1channel(tmp_path, script_runner):
command = f"pyhf inspect {'docs/examples/json/2-bin_1-channel.json':s}"
ret = script_runner.run(shlex.split(command))
assert ret.success
22 changes: 11 additions & 11 deletions tests/test_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)


def test_toms748_scan(tmpdir, hypotest_args):
def test_toms748_scan(tmp_path, hypotest_args):
"""
Test the upper limit toms748 scan returns the correct structure and values
"""
Expand Down Expand Up @@ -166,7 +166,7 @@
)


def test_mle_fit_default(tmpdir, hypotest_args):
def test_mle_fit_default(tmp_path, hypotest_args):
"""
Check that the default return structure of pyhf.infer.mle.fit is as expected
"""
Expand All @@ -180,7 +180,7 @@
assert pyhf.tensorlib.shape(result) == (model.config.npars,)


def test_mle_fit_return_fitted_val(tmpdir, hypotest_args):
def test_mle_fit_return_fitted_val(tmp_path, hypotest_args):
"""
Check that the return structure of pyhf.infer.mle.fit with the
return_fitted_val keyword arg is as expected
Expand All @@ -196,7 +196,7 @@
assert pyhf.tensorlib.shape(result[1]) == ()


def test_hypotest_default(tmpdir, hypotest_args):
def test_hypotest_default(tmp_path, hypotest_args):
"""
Check that the default return structure of pyhf.infer.hypotest is as expected
"""
Expand All @@ -209,7 +209,7 @@
assert isinstance(result, type(tb.astensor(result)))


def test_hypotest_poi_outofbounds(tmpdir, hypotest_args):
def test_hypotest_poi_outofbounds(tmp_path, hypotest_args):
"""
Check that the fit errors for POI outside of parameter bounds
"""
Expand All @@ -226,7 +226,7 @@


@pytest.mark.parametrize('test_stat', ['q0', 'q', 'qtilde'])
def test_hypotest_return_tail_probs(tmpdir, hypotest_args, test_stat):
def test_hypotest_return_tail_probs(tmp_path, hypotest_args, test_stat):
"""
Check that the return structure of pyhf.infer.hypotest with the
return_tail_probs keyword arg is as expected
Expand All @@ -243,7 +243,7 @@


@pytest.mark.parametrize('test_stat', ['q0', 'q', 'qtilde'])
def test_hypotest_return_expected(tmpdir, hypotest_args, test_stat):
def test_hypotest_return_expected(tmp_path, hypotest_args, test_stat):
"""
Check that the return structure of pyhf.infer.hypotest with the
addition of the return_expected keyword arg is as expected
Expand All @@ -265,7 +265,7 @@


@pytest.mark.parametrize('test_stat', ['q0', 'q', 'qtilde'])
def test_hypotest_return_expected_set(tmpdir, hypotest_args, test_stat):
def test_hypotest_return_expected_set(tmp_path, hypotest_args, test_stat):
"""
Check that the return structure of pyhf.infer.hypotest with the
addition of the return_expected_set keyword arg is as expected
Expand Down Expand Up @@ -300,7 +300,7 @@
@pytest.mark.parametrize('return_expected', [True, False])
@pytest.mark.parametrize('return_expected_set', [True, False])
def test_hypotest_return_calculator(
tmpdir,
tmp_path,
hypotest_args,
calctype,
kwargs,
Expand Down Expand Up @@ -491,7 +491,7 @@
assert np.allclose(sigma, back_to_sigma, atol=0, rtol=rtol)


def test_emperical_distribution(tmpdir, hypotest_args):
def test_emperical_distribution(tmp_path, hypotest_args):
"""
Check that the empirical distribution of the test statistic gives
expected results
Expand Down Expand Up @@ -537,7 +537,7 @@
)


def test_toy_calculator(tmpdir, hypotest_args):
def test_toy_calculator(tmp_path, hypotest_args):
"""
Check that the toy calculator is performing as expected
"""
Expand Down Expand Up @@ -628,7 +628,7 @@
assert all(~np.isnan(result) for result in test_results)


# TODO: Remove after pyhf v0.9.0 is released

Check notice on line 631 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L631

unresolved comment '# TODO: Remove after pyhf v0.9.0 is released' (C100)
def test_deprecated_upperlimit(hypotest_args):
with warnings.catch_warnings(record=True) as _warning:
# Cause all warnings to always be triggered
Expand Down
4 changes: 2 additions & 2 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


@pytest.fixture()
def common_kwargs(tmpdir):
outputnb = tmpdir.join('output.ipynb')
def common_kwargs(tmp_path):
outputnb = tmp_path.joinpath('output.ipynb')
return {
'output_path': str(outputnb),
'kernel_name': f'python{sys.version_info.major}',
Expand Down
Loading