From 0116a8d4913ecee26802d094496946123ac8f3f2 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Fri, 21 Jan 2022 18:21:55 +0000 Subject: [PATCH] Bug 1748737: Update deprecated pytest code r=webdriver-reviewers,ahal,whimboo `pytest` is about to be upgraded, so this patch resolves a bunch of the issues currently blocking the upgrade. Most changes done in this patch are described in the "deprecations" page on the pytest docs: https://docs.pytest.org/en/latest/deprecations.html#using-class-in-custom-collectors One undocumented change is that defining a pytest argument as the same name as an in-scope, imported fixture function, will no longer use that imported function. Differential Revision: https://phabricator.services.mozilla.com/D136537 --- config/mozunit/mozunit/mozunit.py | 2 +- layout/tools/reftest/selftest/conftest.py | 4 +- .../test/test_optimize_strategies.py | 5 +- .../tests/harness_unit/conftest.py | 2 +- .../harness_unit/test_marionette_harness.py | 4 +- testing/mochitest/tests/python/conftest.py | 4 +- testing/mozbase/mozcrash/tests/conftest.py | 6 ++- .../moztest/moztest/selftest/fixtures.py | 6 ++- .../mozbase/mozversion/tests/test_binary.py | 46 +++++++++---------- 9 files changed, 45 insertions(+), 34 deletions(-) diff --git a/config/mozunit/mozunit/mozunit.py b/config/mozunit/mozunit/mozunit.py index 2d34484f6185..270ef97260cc 100644 --- a/config/mozunit/mozunit/mozunit.py +++ b/config/mozunit/mozunit/mozunit.py @@ -313,7 +313,7 @@ def main(*args, **kwargs): args.extend( [ "--rootdir", - topsrcdir, + str(topsrcdir), "-c", os.path.join(here, "pytest.ini"), "-vv", diff --git a/layout/tools/reftest/selftest/conftest.py b/layout/tools/reftest/selftest/conftest.py index 74067e9787f5..0beeb096f1da 100644 --- a/layout/tools/reftest/selftest/conftest.py +++ b/layout/tools/reftest/selftest/conftest.py @@ -18,7 +18,7 @@ except ImportError: import mozinfo import pytest from manifestparser import expression -from moztest.selftest.fixtures import binary, setup_test_harness # noqa +from moztest.selftest.fixtures import binary_fixture, setup_test_harness # noqa here = os.path.abspath(os.path.dirname(__file__)) setup_args = [False, "reftest", "reftest"] @@ -142,7 +142,7 @@ def skip_using_mozinfo(request, setup_test_harness): runreftest = pytest.importorskip("runreftest") runreftest.update_mozinfo() - skip_mozinfo = request.node.get_marker("skip_mozinfo") + skip_mozinfo = request.node.get_closest_marker("skip_mozinfo") if skip_mozinfo: value = skip_mozinfo.args[0] if expression.parse(value, **mozinfo.info): diff --git a/taskcluster/gecko_taskgraph/test/test_optimize_strategies.py b/taskcluster/gecko_taskgraph/test/test_optimize_strategies.py index a87fb0e16a38..76bfe35ae961 100644 --- a/taskcluster/gecko_taskgraph/test/test_optimize_strategies.py +++ b/taskcluster/gecko_taskgraph/test/test_optimize_strategies.py @@ -117,7 +117,10 @@ disperse_tasks = list( def idfn(param): if isinstance(param, tuple): - return param[0].__name__ + try: + return param[0].__name__ + except AttributeError: + return None return None diff --git a/testing/marionette/harness/marionette_harness/tests/harness_unit/conftest.py b/testing/marionette/harness/marionette_harness/tests/harness_unit/conftest.py index c86133d61e96..067f396bd2b3 100644 --- a/testing/marionette/harness/marionette_harness/tests/harness_unit/conftest.py +++ b/testing/marionette/harness/marionette_harness/tests/harness_unit/conftest.py @@ -97,5 +97,5 @@ def mock_marionette(request): """Mock marionette instance""" marionette = MagicMock(spec=dir(Marionette())) if "has_crashed" in request.funcargnames: - marionette.check_for_crash.return_value = request.getfuncargvalue("has_crashed") + marionette.check_for_crash.return_value = request.getfixturevalue("has_crashed") return marionette diff --git a/testing/marionette/harness/marionette_harness/tests/harness_unit/test_marionette_harness.py b/testing/marionette/harness/marionette_harness/tests/harness_unit/test_marionette_harness.py index a9243bb9bab3..0252272c6fc8 100644 --- a/testing/marionette/harness/marionette_harness/tests/harness_unit/test_marionette_harness.py +++ b/testing/marionette/harness/marionette_harness/tests/harness_unit/test_marionette_harness.py @@ -22,7 +22,7 @@ def harness_class(request): failures according to the supplied test parameter """ if "num_fails_crashed" in request.funcargnames: - num_fails_crashed = request.getfuncargvalue("num_fails_crashed") + num_fails_crashed = request.getfixturevalue("num_fails_crashed") else: num_fails_crashed = (0, 0) harness_cls = Mock(spec=MarionetteHarness) @@ -41,7 +41,7 @@ def runner_class(request): runner.crashed attributes are provided by a test parameter """ if "num_fails_crashed" in request.funcargnames: - failures, crashed = request.getfuncargvalue("num_fails_crashed") + failures, crashed = request.getfixturevalue("num_fails_crashed") else: failures = 0 crashed = 0 diff --git a/testing/mochitest/tests/python/conftest.py b/testing/mochitest/tests/python/conftest.py index bc7869f00d11..4663135c54b1 100644 --- a/testing/mochitest/tests/python/conftest.py +++ b/testing/mochitest/tests/python/conftest.py @@ -12,7 +12,7 @@ import pytest import mozinfo from manifestparser import TestManifest, expression -from moztest.selftest.fixtures import binary, setup_test_harness # noqa +from moztest.selftest.fixtures import binary_fixture, setup_test_harness # noqa here = os.path.abspath(os.path.dirname(__file__)) setup_args = [os.path.join(here, "files"), "mochitest", "testing/mochitest"] @@ -140,7 +140,7 @@ def skip_using_mozinfo(request, setup_test_harness): runtests = pytest.importorskip("runtests") runtests.update_mozinfo() - skip_mozinfo = request.node.get_marker("skip_mozinfo") + skip_mozinfo = request.node.get_closest_marker("skip_mozinfo") if skip_mozinfo: value = skip_mozinfo.args[0] if expression.parse(value, **mozinfo.info): diff --git a/testing/mozbase/mozcrash/tests/conftest.py b/testing/mozbase/mozcrash/tests/conftest.py index a6c28920ea78..b3a292e3ea91 100644 --- a/testing/mozbase/mozcrash/tests/conftest.py +++ b/testing/mozbase/mozcrash/tests/conftest.py @@ -50,7 +50,6 @@ def check_for_java_exception(): return wrapper -@pytest.fixture def minidump_files(request, tmpdir): files = [] @@ -104,6 +103,11 @@ def minidump_files(request, tmpdir): return files +@pytest.fixture(name="minidump_files") +def minidump_files_fixture(request, tmpdir): + return minidump_files(request, tmpdir) + + @pytest.fixture(autouse=True) def mock_popen(monkeypatch): """Generate a class that can mock subprocess.Popen. diff --git a/testing/mozbase/moztest/moztest/selftest/fixtures.py b/testing/mozbase/moztest/moztest/selftest/fixtures.py index 1c575cea8891..62318bd4b606 100644 --- a/testing/mozbase/moztest/moztest/selftest/fixtures.py +++ b/testing/mozbase/moztest/moztest/selftest/fixtures.py @@ -90,7 +90,6 @@ def setup_test_harness(request, flavor="plain"): return inner -@pytest.fixture(scope="session") def binary(): """Return a Firefox binary""" try: @@ -108,3 +107,8 @@ def binary(): if "GECKO_BINARY_PATH" in os.environ: return os.environ["GECKO_BINARY_PATH"] + + +@pytest.fixture(name="binary", scope="session") +def binary_fixture(): + return binary() diff --git a/testing/mozbase/mozversion/tests/test_binary.py b/testing/mozbase/mozversion/tests/test_binary.py index f82972ac8915..33aa6246f8bb 100644 --- a/testing/mozbase/mozversion/tests/test_binary.py +++ b/testing/mozbase/mozversion/tests/test_binary.py @@ -12,7 +12,7 @@ import shutil import mozunit import pytest -from moztest.selftest.fixtures import binary as real_binary # noqa: F401 +from moztest.selftest.fixtures import binary_fixture # noqa: F401 from mozversion import errors, get_version @@ -20,8 +20,8 @@ from mozversion import errors, get_version """test getting application version information from a binary path""" -@pytest.fixture(name="binary") -def fixure_binary(tmpdir): +@pytest.fixture() +def fake_binary(tmpdir): binary = tmpdir.join("binary") binary.write("foobar") return str(binary) @@ -57,46 +57,46 @@ SourceRepository = PlatformSourceRepo""" return str(ini) -def test_real_binary(real_binary): # noqa: F811 - if not real_binary: +def test_real_binary(binary): # noqa: F811 + if not binary: pytest.skip("No binary found") - v = get_version(real_binary) + v = get_version(binary) assert isinstance(v, dict) -def test_binary(binary, application_ini, platform_ini): - _check_version(get_version(binary)) +def test_binary(fake_binary, application_ini, platform_ini): + _check_version(get_version(fake_binary)) @pytest.mark.skipif( not hasattr(os, "symlink"), reason="os.symlink not supported on this platform" ) -def test_symlinked_binary(binary, application_ini, platform_ini, tmpdir): +def test_symlinked_binary(fake_binary, application_ini, platform_ini, tmpdir): # create a symlink of the binary in another directory and check # version against this symlink symlink = str(tmpdir.join("symlink")) - os.symlink(binary, symlink) + os.symlink(fake_binary, symlink) _check_version(get_version(symlink)) -def test_binary_in_current_path(binary, application_ini, platform_ini, tmpdir): +def test_binary_in_current_path(fake_binary, application_ini, platform_ini, tmpdir): os.chdir(str(tmpdir)) _check_version(get_version()) def test_with_ini_files_on_osx( - binary, application_ini, platform_ini, monkeypatch, tmpdir + fake_binary, application_ini, platform_ini, monkeypatch, tmpdir ): monkeypatch.setattr(sys, "platform", "darwin") # get_version is working with ini files next to the binary - _check_version(get_version(binary=binary)) + _check_version(get_version(binary=fake_binary)) # or if they are in the Resources dir # in this case the binary must be in a Contents dir, next # to the Resources dir contents_dir = tmpdir.mkdir("Contents") - moved_binary = str(contents_dir.join(os.path.basename(binary))) - shutil.move(binary, moved_binary) + moved_binary = str(contents_dir.join(os.path.basename(fake_binary))) + shutil.move(fake_binary, moved_binary) resources_dir = str(tmpdir.mkdir("Resources")) shutil.move(application_ini, resources_dir) @@ -110,22 +110,22 @@ def test_invalid_binary_path(tmpdir): get_version(str(tmpdir.join("invalid"))) -def test_without_ini_files(binary): +def test_without_ini_files(fake_binary): """With missing ini files an exception should be thrown""" with pytest.raises(errors.AppNotFoundError): - get_version(binary) + get_version(fake_binary) -def test_without_platform_ini_file(binary, application_ini): +def test_without_platform_ini_file(fake_binary, application_ini): """With a missing platform.ini file an exception should be thrown""" with pytest.raises(errors.AppNotFoundError): - get_version(binary) + get_version(fake_binary) -def test_without_application_ini_file(binary, platform_ini): +def test_without_application_ini_file(fake_binary, platform_ini): """With a missing application.ini file an exception should be thrown""" with pytest.raises(errors.AppNotFoundError): - get_version(binary) + get_version(fake_binary) def test_with_exe(application_ini, platform_ini, tmpdir): @@ -135,9 +135,9 @@ def test_with_exe(application_ini, platform_ini, tmpdir): _check_version(get_version(os.path.splitext(str(binary))[0])) -def test_not_found_with_binary_specified(binary): +def test_not_found_with_binary_specified(fake_binary): with pytest.raises(errors.LocalAppNotFoundError): - get_version(binary) + get_version(fake_binary) def _check_version(version):