Skip to content

Commit 4516ed3

Browse files
authored
Merge pull request #10933 from q0w/script-fixture
Disallow use of script fixture in unit tests
2 parents 1a46eed + c8c88ce commit 4516ed3

13 files changed

+136
-114
lines changed

news/10721.trivial.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disallow use of script fixture in unit tests.

tests/conftest.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def pytest_addoption(parser: Parser) -> None:
8686
)
8787

8888

89-
def pytest_collection_modifyitems(config: Config, items: List[pytest.Item]) -> None:
89+
def pytest_collection_modifyitems(config: Config, items: List[pytest.Function]) -> None:
9090
for item in items:
9191
if not hasattr(item, "module"): # e.g.: DoctestTextfile
9292
continue
@@ -112,8 +112,7 @@ def pytest_collection_modifyitems(config: Config, items: List[pytest.Item]) -> N
112112
if item.get_closest_marker("incompatible_with_sysconfig") and _USE_SYSCONFIG:
113113
item.add_marker(pytest.mark.skip("Incompatible with sysconfig"))
114114

115-
# "Item" has no attribute "module"
116-
module_file = item.module.__file__ # type: ignore[attr-defined]
115+
module_file = item.module.__file__
117116
module_path = os.path.relpath(
118117
module_file, os.path.commonprefix([__file__, module_file])
119118
)
@@ -127,6 +126,14 @@ def pytest_collection_modifyitems(config: Config, items: List[pytest.Item]) -> N
127126
item.add_marker(pytest.mark.integration)
128127
elif module_root_dir.startswith("unit"):
129128
item.add_marker(pytest.mark.unit)
129+
130+
# We don't want to allow using the script resource if this is a
131+
# unit test, as unit tests should not need all that heavy lifting
132+
if "script" in item.fixturenames:
133+
raise RuntimeError(
134+
"Cannot use the ``script`` funcarg in a unit test: "
135+
"(filename = {}, item = {})".format(module_path, item)
136+
)
130137
else:
131138
raise RuntimeError(f"Unknown test type (filename = {module_path})")
132139

tests/functional/test_freeze.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_freeze_editable_not_vcs(script: PipTestEnvironment) -> None:
174174
"""
175175
Test an editable install that is not version controlled.
176176
"""
177-
pkg_path = _create_test_package(script)
177+
pkg_path = _create_test_package(script.scratch_path)
178178
# Rename the .git directory so the directory is no longer recognized
179179
# as a VCS directory.
180180
os.rename(os.path.join(pkg_path, ".git"), os.path.join(pkg_path, ".bak"))
@@ -201,7 +201,7 @@ def test_freeze_editable_git_with_no_remote(
201201
"""
202202
Test an editable Git install with no remote url.
203203
"""
204-
pkg_path = _create_test_package(script)
204+
pkg_path = _create_test_package(script.scratch_path)
205205
script.pip("install", "-e", pkg_path)
206206
result = script.pip("freeze")
207207

@@ -225,7 +225,7 @@ def test_freeze_editable_git_with_no_remote(
225225
def test_freeze_svn(script: PipTestEnvironment) -> None:
226226
"""Test freezing a svn checkout"""
227227

228-
checkout_path = _create_test_package(script, vcs="svn")
228+
checkout_path = _create_test_package(script.scratch_path, vcs="svn")
229229

230230
# Install with develop
231231
script.run("python", "setup.py", "develop", cwd=checkout_path, expect_stderr=True)
@@ -250,7 +250,7 @@ def test_freeze_exclude_editable(script: PipTestEnvironment) -> None:
250250
Test excluding editable from freezing list.
251251
"""
252252
# Returns path to a generated package called "version_pkg"
253-
pkg_version = _create_test_package(script)
253+
pkg_version = _create_test_package(script.scratch_path)
254254

255255
result = script.run(
256256
"git",
@@ -283,7 +283,7 @@ def test_freeze_git_clone(script: PipTestEnvironment) -> None:
283283
Test freezing a Git clone.
284284
"""
285285
# Returns path to a generated package called "version_pkg"
286-
pkg_version = _create_test_package(script)
286+
pkg_version = _create_test_package(script.scratch_path)
287287

288288
result = script.run(
289289
"git",
@@ -343,7 +343,7 @@ def test_freeze_git_clone_srcdir(script: PipTestEnvironment) -> None:
343343
relative to setup.py.
344344
"""
345345
# Returns path to a generated package called "version_pkg"
346-
pkg_version = _create_test_package_with_srcdir(script)
346+
pkg_version = _create_test_package_with_srcdir(script.scratch_path)
347347

348348
result = script.run(
349349
"git",
@@ -378,7 +378,7 @@ def test_freeze_mercurial_clone_srcdir(script: PipTestEnvironment) -> None:
378378
relative to setup.py.
379379
"""
380380
# Returns path to a generated package called "version_pkg"
381-
pkg_version = _create_test_package_with_srcdir(script, vcs="hg")
381+
pkg_version = _create_test_package_with_srcdir(script.scratch_path, vcs="hg")
382382

383383
result = script.run("hg", "clone", os.fspath(pkg_version), "pip-test-package")
384384
repo_dir = script.scratch_path / "pip-test-package"
@@ -399,7 +399,7 @@ def test_freeze_git_remote(script: PipTestEnvironment) -> None:
399399
Test freezing a Git clone.
400400
"""
401401
# Returns path to a generated package called "version_pkg"
402-
pkg_version = _create_test_package(script)
402+
pkg_version = _create_test_package(script.scratch_path)
403403

404404
result = script.run(
405405
"git",
@@ -483,7 +483,7 @@ def test_freeze_mercurial_clone(script: PipTestEnvironment) -> None:
483483
484484
"""
485485
# Returns path to a generated package called "version_pkg"
486-
pkg_version = _create_test_package(script, vcs="hg")
486+
pkg_version = _create_test_package(script.scratch_path, vcs="hg")
487487

488488
result = script.run(
489489
"hg",
@@ -517,7 +517,7 @@ def test_freeze_bazaar_clone(script: PipTestEnvironment) -> None:
517517
518518
"""
519519
try:
520-
checkout_path = _create_test_package(script, vcs="bazaar")
520+
checkout_path = _create_test_package(script.scratch_path, vcs="bazaar")
521521
except OSError as e:
522522
pytest.fail(f"Invoking `bzr` failed: {e}")
523523

@@ -549,7 +549,7 @@ def test_freeze_nested_vcs(
549549
) -> None:
550550
"""Test VCS can be correctly freezed when resides inside another VCS repo."""
551551
# Create Python package.
552-
pkg_path = _create_test_package(script, vcs=inner_vcs)
552+
pkg_path = _create_test_package(script.scratch_path, vcs=inner_vcs)
553553

554554
# Create outer repo to clone into.
555555
root_path = script.scratch_path.joinpath("test_freeze_nested_vcs")
@@ -1011,7 +1011,7 @@ def test_freeze_pep610_editable(script: PipTestEnvironment) -> None:
10111011
Test that a package installed with a direct_url.json with editable=true
10121012
is correctly frozeon as editable.
10131013
"""
1014-
pkg_path = _create_test_package(script, name="testpkg")
1014+
pkg_path = _create_test_package(script.scratch_path, name="testpkg")
10151015
result = script.pip("install", pkg_path)
10161016
direct_url_path = get_created_direct_url_path(result, "testpkg")
10171017
assert direct_url_path

tests/functional/test_install.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ def test_basic_install_editable_from_svn(script: PipTestEnvironment) -> None:
345345
"""
346346
Test checking out from svn.
347347
"""
348-
checkout_path = _create_test_package(script)
349-
repo_url = _create_svn_repo(script, checkout_path)
348+
checkout_path = _create_test_package(script.scratch_path)
349+
repo_url = _create_svn_repo(script.scratch_path, checkout_path)
350350
result = script.pip("install", "-e", "svn+" + repo_url + "#egg=version-pkg")
351351
result.assert_installed("version-pkg", with_files=[".svn"])
352352

353353

354354
def _test_install_editable_from_git(script: PipTestEnvironment) -> None:
355355
"""Test cloning from Git."""
356-
pkg_path = _create_test_package(script, name="testpackage", vcs="git")
356+
pkg_path = _create_test_package(script.scratch_path, name="testpackage", vcs="git")
357357
args = [
358358
"install",
359359
"-e",
@@ -433,7 +433,7 @@ def test_install_editable_uninstalls_existing_from_path(
433433
@need_mercurial
434434
def test_basic_install_editable_from_hg(script: PipTestEnvironment) -> None:
435435
"""Test cloning and hg+file install from Mercurial."""
436-
pkg_path = _create_test_package(script, name="testpackage", vcs="hg")
436+
pkg_path = _create_test_package(script.scratch_path, name="testpackage", vcs="hg")
437437
url = f"hg+{pkg_path.as_uri()}#egg=testpackage"
438438
assert url.startswith("hg+file")
439439
args = ["install", "-e", url]
@@ -446,7 +446,7 @@ def test_vcs_url_final_slash_normalization(script: PipTestEnvironment) -> None:
446446
"""
447447
Test that presence or absence of final slash in VCS URL is normalized.
448448
"""
449-
pkg_path = _create_test_package(script, name="testpackage", vcs="hg")
449+
pkg_path = _create_test_package(script.scratch_path, name="testpackage", vcs="hg")
450450
args = [
451451
"install",
452452
"-e",
@@ -459,7 +459,9 @@ def test_vcs_url_final_slash_normalization(script: PipTestEnvironment) -> None:
459459
@need_bzr
460460
def test_install_editable_from_bazaar(script: PipTestEnvironment) -> None:
461461
"""Test checking out from Bazaar."""
462-
pkg_path = _create_test_package(script, name="testpackage", vcs="bazaar")
462+
pkg_path = _create_test_package(
463+
script.scratch_path, name="testpackage", vcs="bazaar"
464+
)
463465
args = [
464466
"install",
465467
"-e",

tests/functional/test_install_direct_url.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_install_find_links_no_direct_url(script: PipTestEnvironment) -> None:
1313

1414
@pytest.mark.usefixtures("with_wheel")
1515
def test_install_vcs_editable_no_direct_url(script: PipTestEnvironment) -> None:
16-
pkg_path = _create_test_package(script, name="testpkg")
16+
pkg_path = _create_test_package(script.scratch_path, name="testpkg")
1717
args = ["install", "-e", f"git+{pkg_path.as_uri()}#egg=testpkg"]
1818
result = script.pip(*args)
1919
# legacy editable installs do not generate .dist-info,
@@ -23,7 +23,7 @@ def test_install_vcs_editable_no_direct_url(script: PipTestEnvironment) -> None:
2323

2424
@pytest.mark.usefixtures("with_wheel")
2525
def test_install_vcs_non_editable_direct_url(script: PipTestEnvironment) -> None:
26-
pkg_path = _create_test_package(script, name="testpkg")
26+
pkg_path = _create_test_package(script.scratch_path, name="testpkg")
2727
url = pkg_path.as_uri()
2828
args = ["install", f"git+{url}#egg=testpkg"]
2929
result = script.pip(*args)
@@ -57,7 +57,7 @@ def test_install_vcs_constraint_direct_url(script: PipTestEnvironment) -> None:
5757

5858
@pytest.mark.usefixtures("with_wheel")
5959
def test_install_vcs_constraint_direct_file_url(script: PipTestEnvironment) -> None:
60-
pkg_path = _create_test_package(script, name="testpkg")
60+
pkg_path = _create_test_package(script.scratch_path, name="testpkg")
6161
url = pkg_path.as_uri()
6262
constraints_file = script.scratch_path / "constraints.txt"
6363
constraints_file.write_text(f"git+{url}#egg=testpkg")

tests/functional/test_install_vcs_git.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_git_install_again_after_changes(script: PipTestEnvironment) -> None:
145145
logged on the update: "Did not find branch or tag ..., assuming ref or
146146
revision."
147147
"""
148-
version_pkg_path = _create_test_package(script)
148+
version_pkg_path = _create_test_package(script.scratch_path)
149149
version = _install_version_pkg(script, version_pkg_path)
150150
assert version == "0.1"
151151

@@ -161,7 +161,7 @@ def test_git_install_branch_again_after_branch_changes(
161161
Test installing a branch again after the branch is updated in the remote
162162
repository.
163163
"""
164-
version_pkg_path = _create_test_package(script)
164+
version_pkg_path = _create_test_package(script.scratch_path)
165165
version = _install_version_pkg(script, version_pkg_path, rev="master")
166166
assert version == "0.1"
167167

@@ -203,7 +203,7 @@ def test_git_with_sha1_revisions(script: PipTestEnvironment) -> None:
203203
"""
204204
Git backend should be able to install from SHA1 revisions
205205
"""
206-
version_pkg_path = _create_test_package(script)
206+
version_pkg_path = _create_test_package(script.scratch_path)
207207
_change_test_package_version(script, version_pkg_path)
208208
sha1 = script.run(
209209
"git",
@@ -219,7 +219,7 @@ def test_git_with_short_sha1_revisions(script: PipTestEnvironment) -> None:
219219
"""
220220
Git backend should be able to install from SHA1 revisions
221221
"""
222-
version_pkg_path = _create_test_package(script)
222+
version_pkg_path = _create_test_package(script.scratch_path)
223223
_change_test_package_version(script, version_pkg_path)
224224
sha1 = script.run(
225225
"git",
@@ -235,7 +235,7 @@ def test_git_with_branch_name_as_revision(script: PipTestEnvironment) -> None:
235235
"""
236236
Git backend should be able to install from branch names
237237
"""
238-
version_pkg_path = _create_test_package(script)
238+
version_pkg_path = _create_test_package(script.scratch_path)
239239
branch = "test_branch"
240240
script.run("git", "checkout", "-b", branch, cwd=version_pkg_path)
241241
_change_test_package_version(script, version_pkg_path)
@@ -247,7 +247,7 @@ def test_git_with_tag_name_as_revision(script: PipTestEnvironment) -> None:
247247
"""
248248
Git backend should be able to install from tag names
249249
"""
250-
version_pkg_path = _create_test_package(script)
250+
version_pkg_path = _create_test_package(script.scratch_path)
251251
script.run("git", "tag", "test_tag", cwd=version_pkg_path)
252252
_change_test_package_version(script, version_pkg_path)
253253
version = _install_version_pkg(script, version_pkg_path, rev="test_tag")
@@ -265,7 +265,7 @@ def test_git_install_ref(script: PipTestEnvironment) -> None:
265265
"""
266266
The Git backend should be able to install a ref with the first install.
267267
"""
268-
version_pkg_path = _create_test_package(script)
268+
version_pkg_path = _create_test_package(script.scratch_path)
269269
_add_ref(script, version_pkg_path, "refs/foo/bar")
270270
_change_test_package_version(script, version_pkg_path)
271271

@@ -282,7 +282,7 @@ def test_git_install_then_install_ref(script: PipTestEnvironment) -> None:
282282
The Git backend should be able to install a ref after a package has
283283
already been installed.
284284
"""
285-
version_pkg_path = _create_test_package(script)
285+
version_pkg_path = _create_test_package(script.scratch_path)
286286
_add_ref(script, version_pkg_path, "refs/foo/bar")
287287
_change_test_package_version(script, version_pkg_path)
288288

@@ -431,7 +431,7 @@ def test_git_with_ambiguous_revs(script: PipTestEnvironment) -> None:
431431
"""
432432
Test git with two "names" (tag/branch) pointing to the same commit
433433
"""
434-
version_pkg_path = _create_test_package(script)
434+
version_pkg_path = _create_test_package(script.scratch_path)
435435
version_pkg_url = _make_version_pkg_url(version_pkg_path, rev="0.1")
436436
script.run("git", "tag", "0.1", cwd=version_pkg_path)
437437
result = script.pip("install", "-e", version_pkg_url)
@@ -445,7 +445,7 @@ def test_editable__no_revision(script: PipTestEnvironment) -> None:
445445
"""
446446
Test a basic install in editable mode specifying no revision.
447447
"""
448-
version_pkg_path = _create_test_package(script)
448+
version_pkg_path = _create_test_package(script.scratch_path)
449449
_install_version_pkg_only(script, version_pkg_path)
450450

451451
branch = _get_editable_branch(script, "version-pkg")
@@ -460,7 +460,7 @@ def test_editable__branch_with_sha_same_as_default(script: PipTestEnvironment) -
460460
Test installing in editable mode a branch whose sha matches the sha
461461
of the default branch, but is different from the default branch.
462462
"""
463-
version_pkg_path = _create_test_package(script)
463+
version_pkg_path = _create_test_package(script.scratch_path)
464464
# Create a second branch with the same SHA.
465465
script.run("git", "branch", "develop", cwd=version_pkg_path)
466466
_install_version_pkg_only(script, version_pkg_path, rev="develop")
@@ -479,7 +479,7 @@ def test_editable__branch_with_sha_different_from_default(
479479
Test installing in editable mode a branch whose sha is different from
480480
the sha of the default branch.
481481
"""
482-
version_pkg_path = _create_test_package(script)
482+
version_pkg_path = _create_test_package(script.scratch_path)
483483
# Create a second branch.
484484
script.run("git", "branch", "develop", cwd=version_pkg_path)
485485
# Add another commit to the master branch to give it a different sha.
@@ -500,7 +500,7 @@ def test_editable__non_master_default_branch(script: PipTestEnvironment) -> None
500500
Test the branch you get after an editable install from a remote repo
501501
with a non-master default branch.
502502
"""
503-
version_pkg_path = _create_test_package(script)
503+
version_pkg_path = _create_test_package(script.scratch_path)
504504
# Change the default branch of the remote repo to a name that is
505505
# alphabetically after "master".
506506
script.run("git", "checkout", "-b", "release", cwd=version_pkg_path)
@@ -517,7 +517,7 @@ def test_reinstalling_works_with_editable_non_master_branch(
517517
Reinstalling an editable installation should not assume that the "master"
518518
branch exists. See https://github.com/pypa/pip/issues/4448.
519519
"""
520-
version_pkg_path = _create_test_package(script)
520+
version_pkg_path = _create_test_package(script.scratch_path)
521521

522522
# Switch the default branch to something other than 'master'
523523
script.run("git", "branch", "-m", "foobar", cwd=version_pkg_path)
@@ -569,7 +569,7 @@ def test_install_git_branch_not_cached(script: PipTestEnvironment) -> None:
569569
Installing git urls with a branch revision does not cause wheel caching.
570570
"""
571571
PKG = "gitbranchnotcached"
572-
repo_dir = _create_test_package(script, name=PKG)
572+
repo_dir = _create_test_package(script.scratch_path, name=PKG)
573573
url = _make_version_pkg_url(repo_dir, rev="master", name=PKG)
574574
result = script.pip("install", url, "--only-binary=:all:")
575575
assert f"Successfully built {PKG}" in result.stdout, result.stdout
@@ -585,7 +585,7 @@ def test_install_git_sha_cached(script: PipTestEnvironment) -> None:
585585
Installing git urls with a sha revision does cause wheel caching.
586586
"""
587587
PKG = "gitshacached"
588-
repo_dir = _create_test_package(script, name=PKG)
588+
repo_dir = _create_test_package(script.scratch_path, name=PKG)
589589
commit = script.run("git", "rev-parse", "HEAD", cwd=repo_dir).stdout.strip()
590590
url = _make_version_pkg_url(repo_dir, rev=commit, name=PKG)
591591
result = script.pip("install", url)

tests/functional/test_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def test_list_pep610_editable(script: PipTestEnvironment) -> None:
736736
Test that a package installed with a direct_url.json with editable=true
737737
is correctly listed as editable.
738738
"""
739-
pkg_path = _create_test_package(script, name="testpkg")
739+
pkg_path = _create_test_package(script.scratch_path, name="testpkg")
740740
result = script.pip("install", pkg_path)
741741
direct_url_path = get_created_direct_url_path(result, "testpkg")
742742
assert direct_url_path

tests/functional/test_vcs_git.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_is_commit_id_equal(script: PipTestEnvironment) -> None:
221221
"""
222222
Test Git.is_commit_id_equal().
223223
"""
224-
version_pkg_path = os.fspath(_create_test_package(script))
224+
version_pkg_path = os.fspath(_create_test_package(script.scratch_path))
225225
script.run("git", "branch", "branch0.1", cwd=version_pkg_path)
226226
commit = script.run("git", "rev-parse", "HEAD", cwd=version_pkg_path).stdout.strip()
227227

@@ -234,7 +234,7 @@ def test_is_commit_id_equal(script: PipTestEnvironment) -> None:
234234

235235

236236
def test_is_immutable_rev_checkout(script: PipTestEnvironment) -> None:
237-
version_pkg_path = os.fspath(_create_test_package(script))
237+
version_pkg_path = os.fspath(_create_test_package(script.scratch_path))
238238
commit = script.run("git", "rev-parse", "HEAD", cwd=version_pkg_path).stdout.strip()
239239
assert Git().is_immutable_rev_checkout(
240240
"git+https://g.c/o/r@" + commit, version_pkg_path
@@ -246,7 +246,7 @@ def test_is_immutable_rev_checkout(script: PipTestEnvironment) -> None:
246246

247247

248248
def test_get_repository_root(script: PipTestEnvironment) -> None:
249-
version_pkg_path = _create_test_package(script)
249+
version_pkg_path = _create_test_package(script.scratch_path)
250250
tests_path = version_pkg_path.joinpath("tests")
251251
tests_path.mkdir()
252252

0 commit comments

Comments
 (0)