Skip to content

Commit

Permalink
test: Use tmp_path pytest fixture over tmpdir (#2384)
Browse files Browse the repository at this point in the history
* Use `tmp_path` over `tmpdir`.
* Use pathlib.Path functions now that paths and not files are being used.
   - Remove use of `.strpath`.
   - Update from `.join` to `.joinpath`.
   - For `json.loads` update from `.read` to `.read_text`.
   - For `json.load` update to use `.open`.
   - Update from `.write` to `.write_text` for patches.
   - Update from `.mkdir` to creating the path and then calling `.mkdir`.
  • Loading branch information
matthewfeickert authored Nov 21, 2023
1 parent f78442a commit a1a31f1
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 216 deletions.
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 check_uniform_type(in_list):
)


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_upper_limit_with_kwargs(hypotest_args):
)


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 @@ def test_mle_fit_default(tmpdir, hypotest_args):
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 @@ def test_mle_fit_return_fitted_val(tmpdir, hypotest_args):
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 @@ def test_hypotest_default(tmpdir, hypotest_args):
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 @@ def test_hypotest_poi_outofbounds(tmpdir, hypotest_args):


@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 @@ def test_hypotest_return_tail_probs(tmpdir, hypotest_args, test_stat):


@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 @@ def test_hypotest_return_expected(tmpdir, hypotest_args, test_stat):


@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 @@ def test_hypotest_return_expected_set(tmpdir, hypotest_args, test_stat):
@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 @@ def test_significance_to_pvalue_roundtrip(backend):
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_emperical_distribution(tmpdir, hypotest_args):
)


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
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

0 comments on commit a1a31f1

Please sign in to comment.