From a631fc714d409f6557f4a02980db3ae3fd62369a Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Fri, 29 Jun 2018 15:37:31 +0100 Subject: [PATCH] Bug 1472201 - Vendor pytest 3.6.2 and dependencies; r=ahal MozReview-Commit-ID: 5qfK6OygVMH --HG-- rename : third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/LICENSE.txt => third_party/python/pluggy/LICENSE rename : third_party/python/pytest/doc/en/example/costlysetup/sub1/__init__.py => third_party/python/pytest/doc/en/example/costlysetup/sub_a/__init__.py rename : third_party/python/pytest/doc/en/example/costlysetup/sub1/__init__.py => third_party/python/pytest/doc/en/example/costlysetup/sub_b/__init__.py rename : third_party/python/pytest/_pytest/_code/__init__.py => third_party/python/pytest/src/_pytest/_code/__init__.py extra : rebase_source : d80873f2b1899decefbddddfc2f69ae045925b81 --- Pipfile | 2 +- Pipfile.lock | 45 +- build/virtualenv_packages.txt | 6 +- third_party/python/atomicwrites/LICENSE | 19 + third_party/python/atomicwrites/MANIFEST.in | 6 + third_party/python/atomicwrites/PKG-INFO | 112 + third_party/python/atomicwrites/README.rst | 102 + .../atomicwrites/atomicwrites/__init__.py | 201 ++ third_party/python/atomicwrites/docs/Makefile | 177 ++ third_party/python/atomicwrites/docs/conf.py | 107 + .../python/atomicwrites/docs/index.rst | 35 + third_party/python/atomicwrites/docs/make.bat | 242 ++ third_party/python/atomicwrites/setup.cfg | 8 + third_party/python/atomicwrites/setup.py | 27 + .../atomicwrites/tests/test_atomicwrites.py | 89 + third_party/python/funcsigs/CHANGELOG | 24 + third_party/python/funcsigs/LICENSE | 13 + third_party/python/funcsigs/MANIFEST.in | 7 + third_party/python/funcsigs/PKG-INFO | 378 +++ third_party/python/funcsigs/README.rst | 353 +++ third_party/python/funcsigs/docs/Makefile | 153 ++ .../python/funcsigs/docs/_templates/page.html | 9 + third_party/python/funcsigs/docs/conf.py | 251 ++ third_party/python/funcsigs/docs/index.rst | 353 +++ .../python/funcsigs/funcsigs/__init__.py | 829 +++++++ .../python/funcsigs/funcsigs/version.py | 1 + third_party/python/funcsigs/setup.cfg | 8 + third_party/python/funcsigs/setup.py | 52 + .../tests}/__init__.py | 0 .../funcsigs/tests/test_formatannotation.py | 17 + .../python/funcsigs/tests/test_funcsigs.py | 91 + .../python/funcsigs/tests/test_inspect.py | 1002 ++++++++ third_party/python/more-itertools/LICENSE | 19 + third_party/python/more-itertools/MANIFEST.in | 8 + third_party/python/more-itertools/PKG-INFO | 321 +++ third_party/python/more-itertools/README.rst | 59 + .../python/more-itertools/docs/Makefile | 153 ++ .../python/more-itertools/docs/api.rst | 234 ++ .../python/more-itertools/docs/conf.py | 244 ++ .../python/more-itertools/docs/index.rst | 16 + .../python/more-itertools/docs/license.rst | 16 + .../python/more-itertools/docs/make.bat | 190 ++ .../python/more-itertools/docs/testing.rst | 19 + .../python/more-itertools/docs/versions.rst | 237 ++ .../more-itertools/more_itertools/__init__.py | 2 + .../more-itertools/more_itertools/more.py | 2068 +++++++++++++++++ .../more-itertools/more_itertools/recipes.py | 565 +++++ .../more_itertools/tests/__init__.py | 0 .../more_itertools/tests/test_more.py | 1848 +++++++++++++++ .../more_itertools/tests/test_recipes.py | 607 +++++ third_party/python/more-itertools/setup.cfg | 8 + third_party/python/more-itertools/setup.py | 59 + third_party/python/more-itertools/tox.ini | 5 + .../LICENSE.txt => pluggy/LICENSE} | 0 third_party/python/pluggy/MANIFEST.in | 7 + third_party/python/pluggy/PKG-INFO | 112 + third_party/python/pluggy/README.rst | 80 + .../pluggy.py => pluggy/pluggy/__init__.py} | 360 +-- third_party/python/pluggy/pluggy/callers.py | 201 ++ third_party/python/pluggy/setup.cfg | 13 + third_party/python/pluggy/setup.py | 51 + .../python/pluggy/testing/benchmark.py | 59 + third_party/python/pluggy/testing/conftest.py | 30 + .../python/pluggy/testing/test_details.py | 103 + .../python/pluggy/testing/test_helpers.py | 68 + .../python/pluggy/testing/test_hookrelay.py | 210 ++ .../pluggy/testing/test_method_ordering.py | 322 +++ .../python/pluggy/testing/test_multicall.py | 194 ++ .../pluggy/testing/test_pluginmanager.py | 374 +++ .../python/pluggy/testing/test_tracer.py | 89 + third_party/python/pluggy/tox.ini | 44 + third_party/python/py/.gitignore | 2 + third_party/python/py/.hgignore | 29 - third_party/python/py/.hgtags | 68 - third_party/python/py/.travis.yml | 17 + third_party/python/py/CHANGELOG | 8 +- third_party/python/py/HOWTORELEASE.rst | 17 + third_party/python/py/PKG-INFO | 10 +- third_party/python/py/README.rst | 8 +- third_party/python/py/appveyor.yml | 26 + third_party/python/py/doc/path.txt | 6 + third_party/python/py/py/__init__.py | 7 +- third_party/python/py/py/_path/local.py | 10 +- .../{pytest/_pytest => py/py}/_version.py | 2 +- third_party/python/py/setup.py | 16 +- .../python/py/testing/path/test_local.py | 33 +- third_party/python/pytest/.coveragerc | 5 +- .../pytest/.github/PULL_REQUEST_TEMPLATE.md | 19 +- third_party/python/pytest/.gitignore | 3 +- .../python/pytest/.pre-commit-config.yaml | 36 + third_party/python/pytest/.travis.yml | 47 +- third_party/python/pytest/AUTHORS | 33 + third_party/python/pytest/CHANGELOG.rst | 886 ++++++- third_party/python/pytest/CONTRIBUTING.rst | 60 +- third_party/python/pytest/HOWTORELEASE.rst | 42 +- third_party/python/pytest/PKG-INFO | 26 +- third_party/python/pytest/README.rst | 15 +- third_party/python/pytest/_pytest/_pluggy.py | 11 - third_party/python/pytest/_pytest/config.py | 1414 ----------- .../python/pytest/_pytest/deprecated.py | 42 - third_party/python/pytest/_pytest/doctest.py | 362 --- third_party/python/pytest/_pytest/main.py | 835 ------- third_party/python/pytest/_pytest/mark.py | 465 ---- third_party/python/pytest/_pytest/nodes.py | 37 - third_party/python/pytest/_pytest/skipping.py | 372 --- .../_pytest/vendored_packages/README.md | 13 - .../pluggy-0.4.0.dist-info/DESCRIPTION.rst | 11 - .../pluggy-0.4.0.dist-info/INSTALLER | 1 - .../pluggy-0.4.0.dist-info/METADATA | 40 - .../pluggy-0.4.0.dist-info/RECORD | 9 - .../pluggy-0.4.0.dist-info/WHEEL | 6 - .../pluggy-0.4.0.dist-info/metadata.json | 1 - .../pluggy-0.4.0.dist-info/top_level.txt | 1 - third_party/python/pytest/appveyor.yml | 8 +- third_party/python/pytest/bench/bench.py | 9 +- .../python/pytest/bench/bench_argcomplete.py | 25 +- third_party/python/pytest/bench/empty.py | 1 + third_party/python/pytest/bench/manyparam.py | 7 +- third_party/python/pytest/bench/skip.py | 5 +- .../python/pytest/changelog/README.rst | 32 + third_party/python/pytest/doc/en/Makefile | 2 - .../pytest/doc/en/_templates/globaltoc.html | 7 +- .../pytest/doc/en/_templates/links.html | 5 +- .../pytest/doc/en/_themes/flask/theme.conf | 2 +- .../doc/en/_themes/flask_theme_support.py | 147 +- .../python/pytest/doc/en/announce/index.rst | 13 +- .../pytest/doc/en/announce/release-2.0.3.rst | 3 +- .../pytest/doc/en/announce/release-2.1.1.rst | 1 - .../pytest/doc/en/announce/release-2.1.2.rst | 1 - .../pytest/doc/en/announce/release-2.1.3.rst | 2 +- .../pytest/doc/en/announce/release-2.2.1.rst | 4 +- .../pytest/doc/en/announce/release-2.2.2.rst | 6 +- .../pytest/doc/en/announce/release-2.2.4.rst | 1 - .../pytest/doc/en/announce/release-2.3.0.rst | 13 +- .../pytest/doc/en/announce/release-2.3.1.rst | 8 +- .../pytest/doc/en/announce/release-2.3.2.rst | 6 +- .../pytest/doc/en/announce/release-2.3.3.rst | 5 +- .../pytest/doc/en/announce/release-2.3.4.rst | 8 +- .../pytest/doc/en/announce/release-2.3.5.rst | 7 +- .../pytest/doc/en/announce/release-2.4.0.rst | 41 +- .../pytest/doc/en/announce/release-2.4.1.rst | 2 +- .../pytest/doc/en/announce/release-2.4.2.rst | 4 +- .../pytest/doc/en/announce/release-2.5.0.rst | 51 +- .../pytest/doc/en/announce/release-2.5.1.rst | 11 +- .../pytest/doc/en/announce/release-2.5.2.rst | 31 +- .../pytest/doc/en/announce/release-2.6.1.rst | 3 +- .../pytest/doc/en/announce/release-2.6.2.rst | 1 - .../pytest/doc/en/announce/release-2.6.3.rst | 1 - .../pytest/doc/en/announce/release-2.7.0.rst | 3 +- .../pytest/doc/en/announce/release-2.7.2.rst | 1 - .../pytest/doc/en/announce/release-2.8.3.rst | 5 +- .../pytest/doc/en/announce/release-2.8.7.rst | 2 +- .../pytest/doc/en/announce/release-2.9.0.rst | 56 +- .../pytest/doc/en/announce/release-2.9.1.rst | 22 +- .../pytest/doc/en/announce/release-3.0.0.rst | 6 +- .../pytest/doc/en/announce/release-3.0.1.rst | 2 +- .../pytest/doc/en/announce/release-3.0.2.rst | 2 +- .../pytest/doc/en/announce/release-3.0.3.rst | 4 +- .../pytest/doc/en/announce/release-3.0.4.rst | 4 +- .../pytest/doc/en/announce/release-3.0.5.rst | 2 +- .../pytest/doc/en/announce/release-3.0.6.rst | 2 +- .../pytest/doc/en/announce/release-3.0.7.rst | 2 +- .../pytest/doc/en/announce/release-3.1.1.rst | 2 +- .../pytest/doc/en/announce/release-3.1.2.rst | 2 +- .../pytest/doc/en/announce/release-3.1.3.rst | 2 +- .../pytest/doc/en/announce/release-3.2.1.rst | 2 +- .../pytest/doc/en/announce/release-3.2.2.rst | 2 +- .../pytest/doc/en/announce/release-3.2.3.rst | 2 +- .../pytest/doc/en/announce/release-3.2.4.rst | 2 +- .../pytest/doc/en/announce/release-3.2.5.rst | 2 +- .../pytest/doc/en/announce/release-3.3.0.rst | 50 + .../pytest/doc/en/announce/release-3.3.1.rst | 25 + .../pytest/doc/en/announce/release-3.3.2.rst | 28 + .../pytest/doc/en/announce/release-3.4.0.rst | 52 + .../pytest/doc/en/announce/release-3.4.1.rst | 27 + .../pytest/doc/en/announce/release-3.4.2.rst | 28 + .../pytest/doc/en/announce/release-3.5.0.rst | 51 + .../pytest/doc/en/announce/release-3.5.1.rst | 30 + .../pytest/doc/en/announce/release-3.6.0.rst | 41 + .../pytest/doc/en/announce/release-3.6.1.rst | 24 + .../pytest/doc/en/announce/release-3.6.2.rst | 29 + third_party/python/pytest/doc/en/assert.rst | 50 +- .../pytest/doc/en/backwards-compatibility.rst | 8 + .../python/pytest/doc/en/bash-completion.rst | 5 +- third_party/python/pytest/doc/en/builtin.rst | 153 +- third_party/python/pytest/doc/en/cache.rst | 154 +- third_party/python/pytest/doc/en/capture.rst | 70 +- .../python/pytest/doc/en/check_sphinx.py | 17 - third_party/python/pytest/doc/en/conf.py | 226 +- third_party/python/pytest/doc/en/contact.rst | 7 +- third_party/python/pytest/doc/en/contents.rst | 6 +- .../python/pytest/doc/en/customize.rst | 165 +- .../pytest/doc/en/development_guide.rst | 73 +- third_party/python/pytest/doc/en/doctest.rst | 25 +- .../doc/en/example/assertion/failure_demo.py | 144 +- .../global_testmodule_config/conftest.py | 9 +- .../{test_hello.py => test_hello_world.py} | 1 + .../doc/en/example/assertion/test_failures.py | 10 +- .../assertion/test_setup_flow_example.py | 5 +- .../python/pytest/doc/en/example/attic.rst | 10 +- .../doc/en/example/costlysetup/conftest.py | 6 +- .../costlysetup/{sub1 => sub_a}/__init__.py | 0 .../costlysetup/{sub1 => sub_a}/test_quick.py | 1 - .../costlysetup/{sub2 => sub_b}/__init__.py | 0 .../costlysetup/{sub2 => sub_b}/test_two.py | 2 +- .../python/pytest/doc/en/example/markers.rst | 284 ++- .../pytest/doc/en/example/multipython.py | 37 +- .../pytest/doc/en/example/nonpython.rst | 40 +- .../doc/en/example/nonpython/conftest.py | 21 +- .../pytest/doc/en/example/parametrize.rst | 100 +- .../pytest/doc/en/example/py2py3/conftest.py | 6 +- .../pytest/doc/en/example/py2py3/test_py2.py | 1 - .../pytest/doc/en/example/py2py3/test_py3.py | 4 +- .../pytest/doc/en/example/pythoncollection.py | 6 +- .../doc/en/example/pythoncollection.rst | 32 +- .../pytest/doc/en/example/reportingdemo.rst | 617 ++--- .../python/pytest/doc/en/example/simple.rst | 359 +-- .../pytest/doc/en/example/xfail_demo.py | 8 + third_party/python/pytest/doc/en/faq.rst | 4 +- third_party/python/pytest/doc/en/fixture.rst | 313 ++- .../python/pytest/doc/en/funcarg_compare.rst | 62 +- third_party/python/pytest/doc/en/funcargs.rst | 1 - third_party/python/pytest/doc/en/genapi.py | 41 - .../python/pytest/doc/en/getting-started.rst | 143 +- .../python/pytest/doc/en/goodpractices.rst | 36 +- third_party/python/pytest/doc/en/index.rst | 23 +- third_party/python/pytest/doc/en/links.inc | 10 +- third_party/python/pytest/doc/en/logging.rst | 245 ++ third_party/python/pytest/doc/en/mark.rst | 136 +- .../python/pytest/doc/en/monkeypatch.rst | 38 +- third_party/python/pytest/doc/en/nose.rst | 5 +- .../python/pytest/doc/en/parametrize.rst | 73 +- third_party/python/pytest/doc/en/plugins.rst | 68 +- third_party/python/pytest/doc/en/projects.rst | 22 +- .../proposals/parametrize_with_fixtures.rst | 46 +- .../python/pytest/doc/en/reference.rst | 1296 +++++++++++ .../python/pytest/doc/en/requirements.txt | 1 + third_party/python/pytest/doc/en/skipping.rst | 77 +- third_party/python/pytest/doc/en/talks.rst | 5 - .../python/pytest/doc/en/test/attic.rst | 117 - .../python/pytest/doc/en/test/config.html | 18 - .../python/pytest/doc/en/test/dist.html | 18 - .../python/pytest/doc/en/test/extend.html | 18 - .../python/pytest/doc/en/test/index.rst | 35 - .../python/pytest/doc/en/test/mission.rst | 13 - .../python/pytest/doc/en/test/plugin/cov.rst | 230 -- .../pytest/doc/en/test/plugin/coverage.rst | 51 - .../pytest/doc/en/test/plugin/django.rst | 7 - .../pytest/doc/en/test/plugin/figleaf.rst | 44 - .../pytest/doc/en/test/plugin/helpconfig.rst | 36 - .../pytest/doc/en/test/plugin/index.rst | 68 - .../pytest/doc/en/test/plugin/links.rst | 45 - .../python/pytest/doc/en/test/plugin/nose.rst | 56 - .../pytest/doc/en/test/plugin/oejskit.rst | 12 - .../pytest/doc/en/test/plugin/terminal.rst | 38 - .../pytest/doc/en/test/plugin/xdist.rst | 172 -- .../python/pytest/doc/en/test/test.html | 18 - third_party/python/pytest/doc/en/tmpdir.rst | 38 +- third_party/python/pytest/doc/en/unittest.rst | 52 +- third_party/python/pytest/doc/en/usage.rst | 172 +- third_party/python/pytest/doc/en/warnings.rst | 80 +- .../python/pytest/doc/en/writing_plugins.rst | 286 +-- .../python/pytest/doc/en/xunit_setup.rst | 2 +- third_party/python/pytest/extra/get_issues.py | 40 +- third_party/python/pytest/pyproject.toml | 8 + .../python/pytest/scripts/check-rst.py | 11 - third_party/python/pytest/setup.py | 138 +- .../pytest/{ => src}/_pytest/__init__.py | 4 +- .../pytest/{ => src}/_pytest/_argcomplete.py | 21 +- .../{ => src}/_pytest/_code/__init__.py | 0 .../{ => src}/_pytest/_code/_py2traceback.py | 24 +- .../pytest/{ => src}/_pytest/_code/code.py | 208 +- .../pytest/{ => src}/_pytest/_code/source.py | 149 +- .../python/pytest/src/_pytest/_version.py | 4 + .../{ => src}/_pytest/assertion/__init__.py | 47 +- .../{ => src}/_pytest/assertion/rewrite.py | 224 +- .../{ => src}/_pytest/assertion/truncate.py | 17 +- .../{ => src}/_pytest/assertion/util.py | 173 +- .../pytest/{ => src}/_pytest/cacheprovider.py | 181 +- .../pytest/{ => src}/_pytest/capture.py | 373 ++- .../python/pytest/{ => src}/_pytest/compat.py | 271 ++- .../pytest/src/_pytest/config/__init__.py | 979 ++++++++ .../pytest/src/_pytest/config/argparsing.py | 394 ++++ .../pytest/src/_pytest/config/exceptions.py | 8 + .../pytest/src/_pytest/config/findpaths.py | 140 ++ .../pytest/{ => src}/_pytest/debugging.py | 63 +- .../python/pytest/src/_pytest/deprecated.py | 65 + .../python/pytest/src/_pytest/doctest.py | 520 +++++ .../pytest/{ => src}/_pytest/fixtures.py | 453 ++-- .../{ => src}/_pytest/freeze_support.py | 10 +- .../pytest/{ => src}/_pytest/helpconfig.py | 126 +- .../pytest/{ => src}/_pytest/hookspec.py | 216 +- .../pytest/{ => src}/_pytest/junitxml.py | 304 ++- .../python/pytest/src/_pytest/logging.py | 575 +++++ third_party/python/pytest/src/_pytest/main.py | 611 +++++ .../pytest/src/_pytest/mark/__init__.py | 174 ++ .../pytest/src/_pytest/mark/evaluate.py | 121 + .../python/pytest/src/_pytest/mark/legacy.py | 97 + .../pytest/src/_pytest/mark/structures.py | 428 ++++ .../pytest/{ => src}/_pytest/monkeypatch.py | 72 +- .../python/pytest/src/_pytest/nodes.py | 419 ++++ .../python/pytest/{ => src}/_pytest/nose.py | 31 +- .../pytest/{ => src}/_pytest/outcomes.py | 45 +- .../pytest/{ => src}/_pytest/pastebin.py | 55 +- .../pytest/{ => src}/_pytest/pytester.py | 741 +++--- .../python/pytest/{ => src}/_pytest/python.py | 584 +++-- .../pytest/{ => src}/_pytest/python_api.py | 176 +- .../pytest/{ => src}/_pytest/recwarn.py | 88 +- .../pytest/{ => src}/_pytest/resultlog.py | 43 +- .../python/pytest/{ => src}/_pytest/runner.py | 180 +- .../pytest/{ => src}/_pytest/setuponly.py | 54 +- .../pytest/{ => src}/_pytest/setupplan.py | 10 +- .../python/pytest/src/_pytest/skipping.py | 293 +++ .../pytest/{ => src}/_pytest/terminal.py | 461 ++-- .../python/pytest/{ => src}/_pytest/tmpdir.py | 23 +- .../pytest/{ => src}/_pytest/unittest.py | 76 +- .../pytest/{ => src}/_pytest/warnings.py | 56 +- third_party/python/pytest/{ => src}/pytest.py | 84 +- third_party/python/pytest/tasks/__init__.py | 7 +- third_party/python/pytest/tasks/generate.py | 150 +- .../python/pytest/tasks/release.minor.rst | 2 +- .../python/pytest/tasks/release.patch.rst | 2 +- .../python/pytest/tasks/requirements.txt | 5 +- third_party/python/pytest/tasks/vendoring.py | 23 - .../python/pytest/testing/acceptance_test.py | 744 +++--- .../python/pytest/testing/code/test_code.py | 76 +- .../pytest/testing/code/test_excinfo.py | 481 ++-- .../python/pytest/testing/code/test_source.py | 270 ++- .../code/test_source_multiline_block.py | 5 +- .../python/pytest/testing/deprecated_test.py | 249 +- .../python/pytest/testing/freeze/.gitignore | 2 +- .../testing/freeze/create_executable.py | 8 +- .../pytest/testing/freeze/runtests_script.py | 3 +- .../testing/freeze/tests/test_trivial.py | 5 +- .../python/pytest/testing/freeze/tox_run.py | 10 +- .../pytest/testing/logging/test_fixture.py | 119 + .../pytest/testing/logging/test_formatter.py | 37 + .../pytest/testing/logging/test_reporting.py | 874 +++++++ .../python/pytest/testing/python/approx.py | 194 +- .../python/pytest/testing/python/collect.py | 768 +++--- .../python/pytest/testing/python/fixture.py | 2020 +++++++++++----- .../pytest/testing/python/integration.py | 171 +- .../python/pytest/testing/python/metafunc.py | 937 +++++--- .../python/pytest/testing/python/raises.py | 77 +- .../pytest/testing/python/setup_only.py | 188 +- .../pytest/testing/python/setup_plan.py | 14 +- .../testing/python/show_fixtures_per_test.py | 153 +- .../testing/python/test_deprecations.py | 22 + .../python/pytest/testing/test_argcomplete.py | 68 +- .../python/pytest/testing/test_assertion.py | 870 ++++--- .../pytest/testing/test_assertrewrite.py | 530 +++-- .../python/pytest/testing/test_cache.py | 605 ----- .../pytest/testing/test_cacheprovider.py | 820 +++++++ .../python/pytest/testing/test_capture.py | 806 ++++--- .../python/pytest/testing/test_collection.py | 504 ++-- .../python/pytest/testing/test_compat.py | 39 +- .../python/pytest/testing/test_config.py | 725 ++++-- .../python/pytest/testing/test_conftest.py | 301 ++- .../python/pytest/testing/test_doctest.py | 887 ++++--- .../pytest/testing/test_entry_points.py | 8 +- .../python/pytest/testing/test_helpconfig.py | 43 +- .../python/pytest/testing/test_junitxml.py | 565 +++-- .../python/pytest/testing/test_mark.py | 655 ++++-- .../python/pytest/testing/test_modimport.py | 20 +- .../python/pytest/testing/test_monkeypatch.py | 183 +- .../python/pytest/testing/test_nodes.py | 23 +- .../python/pytest/testing/test_nose.py | 144 +- .../python/pytest/testing/test_parseopt.py | 231 +- .../python/pytest/testing/test_pastebin.py | 79 +- third_party/python/pytest/testing/test_pdb.py | 460 +++- .../pytest/testing/test_pluginmanager.py | 175 +- .../python/pytest/testing/test_pytester.py | 291 ++- .../python/pytest/testing/test_recwarn.py | 181 +- .../python/pytest/testing/test_resultlog.py | 137 +- .../python/pytest/testing/test_runner.py | 458 ++-- .../pytest/testing/test_runner_xunit.py | 137 +- .../python/pytest/testing/test_session.py | 150 +- .../python/pytest/testing/test_skipping.py | 878 ++++--- .../python/pytest/testing/test_terminal.py | 1152 +++++---- .../python/pytest/testing/test_tmpdir.py | 92 +- .../python/pytest/testing/test_unittest.py | 517 +++-- .../python/pytest/testing/test_warnings.py | 241 +- third_party/python/pytest/tox.ini | 101 +- 383 files changed, 41246 insertions(+), 16799 deletions(-) create mode 100644 third_party/python/atomicwrites/LICENSE create mode 100644 third_party/python/atomicwrites/MANIFEST.in create mode 100644 third_party/python/atomicwrites/PKG-INFO create mode 100644 third_party/python/atomicwrites/README.rst create mode 100644 third_party/python/atomicwrites/atomicwrites/__init__.py create mode 100644 third_party/python/atomicwrites/docs/Makefile create mode 100644 third_party/python/atomicwrites/docs/conf.py create mode 100644 third_party/python/atomicwrites/docs/index.rst create mode 100644 third_party/python/atomicwrites/docs/make.bat create mode 100644 third_party/python/atomicwrites/setup.cfg create mode 100644 third_party/python/atomicwrites/setup.py create mode 100644 third_party/python/atomicwrites/tests/test_atomicwrites.py create mode 100644 third_party/python/funcsigs/CHANGELOG create mode 100644 third_party/python/funcsigs/LICENSE create mode 100644 third_party/python/funcsigs/MANIFEST.in create mode 100644 third_party/python/funcsigs/PKG-INFO create mode 100644 third_party/python/funcsigs/README.rst create mode 100644 third_party/python/funcsigs/docs/Makefile create mode 100644 third_party/python/funcsigs/docs/_templates/page.html create mode 100644 third_party/python/funcsigs/docs/conf.py create mode 100644 third_party/python/funcsigs/docs/index.rst create mode 100644 third_party/python/funcsigs/funcsigs/__init__.py create mode 100644 third_party/python/funcsigs/funcsigs/version.py create mode 100644 third_party/python/funcsigs/setup.cfg create mode 100644 third_party/python/funcsigs/setup.py rename third_party/python/{pytest/_pytest/vendored_packages => funcsigs/tests}/__init__.py (100%) create mode 100644 third_party/python/funcsigs/tests/test_formatannotation.py create mode 100644 third_party/python/funcsigs/tests/test_funcsigs.py create mode 100644 third_party/python/funcsigs/tests/test_inspect.py create mode 100644 third_party/python/more-itertools/LICENSE create mode 100644 third_party/python/more-itertools/MANIFEST.in create mode 100644 third_party/python/more-itertools/PKG-INFO create mode 100644 third_party/python/more-itertools/README.rst create mode 100644 third_party/python/more-itertools/docs/Makefile create mode 100644 third_party/python/more-itertools/docs/api.rst create mode 100644 third_party/python/more-itertools/docs/conf.py create mode 100644 third_party/python/more-itertools/docs/index.rst create mode 100644 third_party/python/more-itertools/docs/license.rst create mode 100644 third_party/python/more-itertools/docs/make.bat create mode 100644 third_party/python/more-itertools/docs/testing.rst create mode 100644 third_party/python/more-itertools/docs/versions.rst create mode 100644 third_party/python/more-itertools/more_itertools/__init__.py create mode 100644 third_party/python/more-itertools/more_itertools/more.py create mode 100644 third_party/python/more-itertools/more_itertools/recipes.py create mode 100644 third_party/python/more-itertools/more_itertools/tests/__init__.py create mode 100644 third_party/python/more-itertools/more_itertools/tests/test_more.py create mode 100644 third_party/python/more-itertools/more_itertools/tests/test_recipes.py create mode 100644 third_party/python/more-itertools/setup.cfg create mode 100644 third_party/python/more-itertools/setup.py create mode 100644 third_party/python/more-itertools/tox.ini rename third_party/python/{pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/LICENSE.txt => pluggy/LICENSE} (100%) create mode 100644 third_party/python/pluggy/MANIFEST.in create mode 100644 third_party/python/pluggy/PKG-INFO create mode 100644 third_party/python/pluggy/README.rst rename third_party/python/{pytest/_pytest/vendored_packages/pluggy.py => pluggy/pluggy/__init__.py} (69%) create mode 100644 third_party/python/pluggy/pluggy/callers.py create mode 100644 third_party/python/pluggy/setup.cfg create mode 100644 third_party/python/pluggy/setup.py create mode 100644 third_party/python/pluggy/testing/benchmark.py create mode 100644 third_party/python/pluggy/testing/conftest.py create mode 100644 third_party/python/pluggy/testing/test_details.py create mode 100644 third_party/python/pluggy/testing/test_helpers.py create mode 100644 third_party/python/pluggy/testing/test_hookrelay.py create mode 100644 third_party/python/pluggy/testing/test_method_ordering.py create mode 100644 third_party/python/pluggy/testing/test_multicall.py create mode 100644 third_party/python/pluggy/testing/test_pluginmanager.py create mode 100644 third_party/python/pluggy/testing/test_tracer.py create mode 100644 third_party/python/pluggy/tox.ini delete mode 100644 third_party/python/py/.hgignore delete mode 100644 third_party/python/py/.hgtags create mode 100644 third_party/python/py/HOWTORELEASE.rst create mode 100644 third_party/python/py/appveyor.yml rename third_party/python/{pytest/_pytest => py/py}/_version.py (84%) create mode 100644 third_party/python/pytest/.pre-commit-config.yaml delete mode 100644 third_party/python/pytest/_pytest/_pluggy.py delete mode 100644 third_party/python/pytest/_pytest/config.py delete mode 100644 third_party/python/pytest/_pytest/deprecated.py delete mode 100644 third_party/python/pytest/_pytest/doctest.py delete mode 100644 third_party/python/pytest/_pytest/main.py delete mode 100644 third_party/python/pytest/_pytest/mark.py delete mode 100644 third_party/python/pytest/_pytest/nodes.py delete mode 100644 third_party/python/pytest/_pytest/skipping.py delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/README.md delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/DESCRIPTION.rst delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/INSTALLER delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/METADATA delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/RECORD delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/WHEEL delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/metadata.json delete mode 100644 third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/top_level.txt create mode 100644 third_party/python/pytest/changelog/README.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.3.0.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.3.1.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.3.2.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.4.0.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.4.1.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.4.2.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.5.0.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.5.1.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.6.0.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.6.1.rst create mode 100644 third_party/python/pytest/doc/en/announce/release-3.6.2.rst delete mode 100644 third_party/python/pytest/doc/en/check_sphinx.py rename third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/{test_hello.py => test_hello_world.py} (97%) rename third_party/python/pytest/doc/en/example/costlysetup/{sub1 => sub_a}/__init__.py (100%) rename third_party/python/pytest/doc/en/example/costlysetup/{sub1 => sub_a}/test_quick.py (96%) rename third_party/python/pytest/doc/en/example/costlysetup/{sub2 => sub_b}/__init__.py (100%) rename third_party/python/pytest/doc/en/example/costlysetup/{sub2 => sub_b}/test_two.py (100%) delete mode 100644 third_party/python/pytest/doc/en/genapi.py create mode 100644 third_party/python/pytest/doc/en/logging.rst create mode 100644 third_party/python/pytest/doc/en/reference.rst delete mode 100644 third_party/python/pytest/doc/en/test/attic.rst delete mode 100644 third_party/python/pytest/doc/en/test/config.html delete mode 100644 third_party/python/pytest/doc/en/test/dist.html delete mode 100644 third_party/python/pytest/doc/en/test/extend.html delete mode 100644 third_party/python/pytest/doc/en/test/index.rst delete mode 100644 third_party/python/pytest/doc/en/test/mission.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/cov.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/coverage.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/django.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/figleaf.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/helpconfig.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/index.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/links.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/nose.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/oejskit.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/terminal.rst delete mode 100644 third_party/python/pytest/doc/en/test/plugin/xdist.rst delete mode 100644 third_party/python/pytest/doc/en/test/test.html delete mode 100644 third_party/python/pytest/scripts/check-rst.py rename third_party/python/pytest/{ => src}/_pytest/__init__.py (77%) rename third_party/python/pytest/{ => src}/_pytest/_argcomplete.py (91%) rename third_party/python/pytest/{ => src}/_pytest/_code/__init__.py (100%) rename third_party/python/pytest/{ => src}/_pytest/_code/_py2traceback.py (80%) rename third_party/python/pytest/{ => src}/_pytest/_code/code.py (85%) rename third_party/python/pytest/{ => src}/_pytest/_code/source.py (72%) create mode 100644 third_party/python/pytest/src/_pytest/_version.py rename third_party/python/pytest/{ => src}/_pytest/assertion/__init__.py (79%) rename third_party/python/pytest/{ => src}/_pytest/assertion/rewrite.py (86%) rename third_party/python/pytest/{ => src}/_pytest/assertion/truncate.py (89%) rename third_party/python/pytest/{ => src}/_pytest/assertion/util.py (62%) rename third_party/python/pytest/{ => src}/_pytest/cacheprovider.py (58%) rename third_party/python/pytest/{ => src}/_pytest/capture.py (56%) rename third_party/python/pytest/{ => src}/_pytest/compat.py (53%) create mode 100644 third_party/python/pytest/src/_pytest/config/__init__.py create mode 100644 third_party/python/pytest/src/_pytest/config/argparsing.py create mode 100644 third_party/python/pytest/src/_pytest/config/exceptions.py create mode 100644 third_party/python/pytest/src/_pytest/config/findpaths.py rename third_party/python/pytest/{ => src}/_pytest/debugging.py (67%) create mode 100644 third_party/python/pytest/src/_pytest/deprecated.py create mode 100644 third_party/python/pytest/src/_pytest/doctest.py rename third_party/python/pytest/{ => src}/_pytest/fixtures.py (78%) rename third_party/python/pytest/{ => src}/_pytest/freeze_support.py (83%) rename third_party/python/pytest/{ => src}/_pytest/helpconfig.py (61%) rename third_party/python/pytest/{ => src}/_pytest/hookspec.py (66%) rename third_party/python/pytest/{ => src}/_pytest/junitxml.py (59%) create mode 100644 third_party/python/pytest/src/_pytest/logging.py create mode 100644 third_party/python/pytest/src/_pytest/main.py create mode 100644 third_party/python/pytest/src/_pytest/mark/__init__.py create mode 100644 third_party/python/pytest/src/_pytest/mark/evaluate.py create mode 100644 third_party/python/pytest/src/_pytest/mark/legacy.py create mode 100644 third_party/python/pytest/src/_pytest/mark/structures.py rename third_party/python/pytest/{ => src}/_pytest/monkeypatch.py (81%) create mode 100644 third_party/python/pytest/src/_pytest/nodes.py rename third_party/python/pytest/{ => src}/_pytest/nose.py (69%) rename third_party/python/pytest/{ => src}/_pytest/outcomes.py (74%) rename third_party/python/pytest/{ => src}/_pytest/pastebin.py (66%) rename third_party/python/pytest/{ => src}/_pytest/pytester.py (61%) rename third_party/python/pytest/{ => src}/_pytest/python.py (70%) rename third_party/python/pytest/{ => src}/_pytest/python_api.py (82%) rename third_party/python/pytest/{ => src}/_pytest/recwarn.py (65%) rename third_party/python/pytest/{ => src}/_pytest/resultlog.py (78%) rename third_party/python/pytest/{ => src}/_pytest/runner.py (76%) rename third_party/python/pytest/{ => src}/_pytest/setuponly.py (55%) rename third_party/python/pytest/{ => src}/_pytest/setupplan.py (75%) create mode 100644 third_party/python/pytest/src/_pytest/skipping.py rename third_party/python/pytest/{ => src}/_pytest/terminal.py (60%) rename third_party/python/pytest/{ => src}/_pytest/tmpdir.py (86%) rename third_party/python/pytest/{ => src}/_pytest/unittest.py (77%) rename third_party/python/pytest/{ => src}/_pytest/warnings.py (62%) rename third_party/python/pytest/{ => src}/pytest.py (52%) delete mode 100644 third_party/python/pytest/tasks/vendoring.py create mode 100644 third_party/python/pytest/testing/logging/test_fixture.py create mode 100644 third_party/python/pytest/testing/logging/test_formatter.py create mode 100644 third_party/python/pytest/testing/logging/test_reporting.py create mode 100644 third_party/python/pytest/testing/python/test_deprecations.py delete mode 100755 third_party/python/pytest/testing/test_cache.py create mode 100644 third_party/python/pytest/testing/test_cacheprovider.py diff --git a/Pipfile b/Pipfile index 71612f0cba10..a8da44535bad 100644 --- a/Pipfile +++ b/Pipfile @@ -11,7 +11,7 @@ blessings = "==1.7" jsmin = "==2.1.0" json-e = "==2.5.0" pipenv = "==2018.5.18" -pytest = "==3.2.5" +pytest = "==3.6.2" python-hglib = "==2.4" requests = "==2.9.1" six = "==1.10.0" diff --git a/Pipfile.lock b/Pipfile.lock index 97eea51ff43a..51ba7ce30dd4 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7e168601e5f93e71900ebc68d8c18ff17edb6d5e224bcc83286b9bafaac41fe8" + "sha256": "609a35f65e9a4c07e0e1473ec982c6b5028622e9a795b6cfb8555ad8574804f3" }, "pipfile-spec": 6, "requires": {}, @@ -14,6 +14,13 @@ ] }, "default": { + "atomicwrites": { + "hashes": [ + "sha256:240831ea22da9ab882b551b31d4225591e5e447a68c5e188db5b89ca1d487585", + "sha256:a24da68318b08ac9c9c45029f4a10371ab5b20e4226738e150e6e7c571630ae6" + ], + "version": "==1.1.5" + }, "attrs": { "hashes": [ "sha256:4b90b09eeeb9b88c35bc642cbac057e45a5fd85367b985bd2809c62b7b939265", @@ -38,6 +45,14 @@ ], "version": "==2018.4.16" }, + "funcsigs": { + "hashes": [ + "sha256:330cc27ccbf7f1e992e69fef78261dc7c6569012cf397db8d3de0234e6c937ca", + "sha256:a7bb0f2cf3a3fd1ab2732cb49eba4252c2af4240442415b4abce3b87022a8f50" + ], + "markers": "python_version < '3.0'", + "version": "==1.0.2" + }, "jsmin": { "hashes": [ "sha256:5d07bf0251a4128e5e8e8eef603849b6b5741c337bff087731a248f9cc774f56" @@ -52,6 +67,14 @@ "index": "pypi", "version": "==2.5.0" }, + "more-itertools": { + "hashes": [ + "sha256:2b6b9893337bfd9166bee6a62c2b0c9fe7735dcf85948b387ec8cba30e85d8e8", + "sha256:6703844a52d3588f951883005efcf555e49566a48afd4db4e965d69b883980d3", + "sha256:a18d870ef2ffca2b8463c0070ad17b5978056f403fb64e3f15fe62a52db21cc0" + ], + "version": "==4.2.0" + }, "pipenv": { "hashes": [ "sha256:04b9a8b02a3ff12a5502b335850cfdb192adcfd1d6bbdb7a7c47cae9ab9ddece", @@ -60,20 +83,28 @@ "index": "pypi", "version": "==2018.5.18" }, + "pluggy": { + "hashes": [ + "sha256:7f8ae7f5bdf75671a718d2daf0a64b7885f74510bcd98b1a0bb420eb9a9d0cff", + "sha256:d345c8fe681115900d6da8d048ba67c25df42973bda370783cd58826442dcd7c", + "sha256:e160a7fcf25762bb60efc7e171d4497ff1d8d2d75a3d0df7a21b76821ecbf5c5" + ], + "version": "==0.6.0" + }, "py": { "hashes": [ - "sha256:29c9fab495d7528e80ba1e343b958684f4ace687327e6f789a94bf3d1915f881", - "sha256:983f77f3331356039fdd792e9220b7b8ee1aa6bd2b25f567a963ff1de5a64f6a" + "sha256:3fd59af7435864e1a243790d322d763925431213b6b8529c6ca71081ace3bbf7", + "sha256:e31fb2767eb657cbde86c454f02e99cb846d3cd9d61b318525140214fdc0e98e" ], - "version": "==1.5.3" + "version": "==1.5.4" }, "pytest": { "hashes": [ - "sha256:241d7e7798d79192a123ceaf64c602b4d233eacf6d6e42ae27caa97f498b7dc6", - "sha256:6d5bd4f7113b444c55a3bbb5c738a3dd80d43563d063fc42dcb0aaefbdd78b81" + "sha256:8ea01fc4fcc8e1b1e305252b4bc80a1528019ab99fd3b88666c9dc38d754406c", + "sha256:90898786b3d0b880b47645bae7b51aa9bbf1e9d1e4510c2cfd15dd65c70ea0cd" ], "index": "pypi", - "version": "==3.2.5" + "version": "==3.6.2" }, "python-hglib": { "hashes": [ diff --git a/build/virtualenv_packages.txt b/build/virtualenv_packages.txt index c05aa90a9130..6876f2126e9c 100644 --- a/build/virtualenv_packages.txt +++ b/build/virtualenv_packages.txt @@ -6,6 +6,7 @@ mozilla.pth:python/mozrelease mozilla.pth:python/mozterm mozilla.pth:python/mozversioncontrol mozilla.pth:python/l10n +mozilla.pth:third_party/python/atomicwrites mozilla.pth:third_party/python/attrs/src mozilla.pth:third_party/python/blessings mozilla.pth:third_party/python/compare-locales @@ -13,8 +14,11 @@ mozilla.pth:third_party/python/configobj mozilla.pth:third_party/python/cram mozilla.pth:third_party/python/dlmanager mozilla.pth:third_party/python/fluent +mozilla.pth:third_party/python/funcsigs mozilla.pth:third_party/python/futures +mozilla.pth:third_party/python/more-itertools mozilla.pth:third_party/python/python-hglib +mozilla.pth:third_party/python/pluggy mozilla.pth:third_party/python/jsmin optional:setup.py:third_party/python/psutil:build_ext:--inplace mozilla.pth:third_party/python/psutil @@ -26,7 +30,7 @@ mozilla.pth:third_party/python/requests mozilla.pth:third_party/python/requests-unixsocket mozilla.pth:third_party/python/slugid mozilla.pth:third_party/python/py -mozilla.pth:third_party/python/pytest +mozilla.pth:third_party/python/pytest/src mozilla.pth:third_party/python/pytoml mozilla.pth:third_party/python/redo mozilla.pth:third_party/python/six diff --git a/third_party/python/atomicwrites/LICENSE b/third_party/python/atomicwrites/LICENSE new file mode 100644 index 000000000000..3bbadc3af2cc --- /dev/null +++ b/third_party/python/atomicwrites/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015-2016 Markus Unterwaditzer + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/python/atomicwrites/MANIFEST.in b/third_party/python/atomicwrites/MANIFEST.in new file mode 100644 index 000000000000..1b28469174f3 --- /dev/null +++ b/third_party/python/atomicwrites/MANIFEST.in @@ -0,0 +1,6 @@ +include LICENSE +include README.rst + +recursive-include docs * +recursive-include tests * +prune docs/_build diff --git a/third_party/python/atomicwrites/PKG-INFO b/third_party/python/atomicwrites/PKG-INFO new file mode 100644 index 000000000000..eec6b7c305f2 --- /dev/null +++ b/third_party/python/atomicwrites/PKG-INFO @@ -0,0 +1,112 @@ +Metadata-Version: 1.0 +Name: atomicwrites +Version: 1.1.5 +Summary: Atomic file writes. +Home-page: https://github.com/untitaker/python-atomicwrites +Author: Markus Unterwaditzer +Author-email: markus@unterwaditzer.net +License: MIT +Description: =================== + python-atomicwrites + =================== + + .. image:: https://travis-ci.org/untitaker/python-atomicwrites.svg?branch=master + :target: https://travis-ci.org/untitaker/python-atomicwrites + + .. image:: https://ci.appveyor.com/api/projects/status/vadc4le3c27to59x/branch/master?svg=true + :target: https://ci.appveyor.com/project/untitaker/python-atomicwrites/branch/master + + Atomic file writes. + + .. code-block:: python + + from atomicwrites import atomic_write + + with atomic_write('foo.txt', overwrite=True) as f: + f.write('Hello world.') + # "foo.txt" doesn't exist yet. + + # Now it does. + + + Features that distinguish it from other similar libraries (see `Alternatives and Credit`_): + + - Race-free assertion that the target file doesn't yet exist. This can be + controlled with the ``overwrite`` parameter. + + - Windows support, although not well-tested. The MSDN resources are not very + explicit about which operations are atomic. + + - Simple high-level API that wraps a very flexible class-based API. + + - Consistent error handling across platforms. + + + How it works + ============ + + It uses a temporary file in the same directory as the given path. This ensures + that the temporary file resides on the same filesystem. + + The temporary file will then be atomically moved to the target location: On + POSIX, it will use ``rename`` if files should be overwritten, otherwise a + combination of ``link`` and ``unlink``. On Windows, it uses MoveFileEx_ through + stdlib's ``ctypes`` with the appropriate flags. + + Note that with ``link`` and ``unlink``, there's a timewindow where the file + might be available under two entries in the filesystem: The name of the + temporary file, and the name of the target file. + + Also note that the permissions of the target file may change this way. In some + situations a ``chmod`` can be issued without any concurrency problems, but + since that is not always the case, this library doesn't do it by itself. + + .. _MoveFileEx: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx + + fsync + ----- + + On POSIX, ``fsync`` is invoked on the temporary file after it is written (to + flush file content and metadata), and on the parent directory after the file is + moved (to flush filename). + + ``fsync`` does not take care of disks' internal buffers, but there don't seem + to be any standard POSIX APIs for that. On OS X, ``fcntl`` is used with + ``F_FULLFSYNC`` instead of ``fsync`` for that reason. + + On Windows, `_commit `_ + is used, but there are no guarantees about disk internal buffers. + + Alternatives and Credit + ======================= + + Atomicwrites is directly inspired by the following libraries (and shares a + minimal amount of code): + + - The Trac project's `utility functions + `_, + also used in `Werkzeug `_ and + `mitsuhiko/python-atomicfile + `_. The idea to use + ``ctypes`` instead of ``PyWin32`` originated there. + + - `abarnert/fatomic `_. Windows support + (based on ``PyWin32``) was originally taken from there. + + Other alternatives to atomicwrites include: + + - `sashka/atomicfile `_. Originally I + considered using that, but at the time it was lacking a lot of features I + needed (Windows support, overwrite-parameter, overriding behavior through + subclassing). + + - The `Boltons library collection `_ + features a class for atomic file writes, which seems to have a very similar + ``overwrite`` parameter. It is lacking Windows support though. + + License + ======= + + Licensed under the MIT, see ``LICENSE``. + +Platform: UNKNOWN diff --git a/third_party/python/atomicwrites/README.rst b/third_party/python/atomicwrites/README.rst new file mode 100644 index 000000000000..3a5658cbd8f3 --- /dev/null +++ b/third_party/python/atomicwrites/README.rst @@ -0,0 +1,102 @@ +=================== +python-atomicwrites +=================== + +.. image:: https://travis-ci.org/untitaker/python-atomicwrites.svg?branch=master + :target: https://travis-ci.org/untitaker/python-atomicwrites + +.. image:: https://ci.appveyor.com/api/projects/status/vadc4le3c27to59x/branch/master?svg=true + :target: https://ci.appveyor.com/project/untitaker/python-atomicwrites/branch/master + +Atomic file writes. + +.. code-block:: python + + from atomicwrites import atomic_write + + with atomic_write('foo.txt', overwrite=True) as f: + f.write('Hello world.') + # "foo.txt" doesn't exist yet. + + # Now it does. + + +Features that distinguish it from other similar libraries (see `Alternatives and Credit`_): + +- Race-free assertion that the target file doesn't yet exist. This can be + controlled with the ``overwrite`` parameter. + +- Windows support, although not well-tested. The MSDN resources are not very + explicit about which operations are atomic. + +- Simple high-level API that wraps a very flexible class-based API. + +- Consistent error handling across platforms. + + +How it works +============ + +It uses a temporary file in the same directory as the given path. This ensures +that the temporary file resides on the same filesystem. + +The temporary file will then be atomically moved to the target location: On +POSIX, it will use ``rename`` if files should be overwritten, otherwise a +combination of ``link`` and ``unlink``. On Windows, it uses MoveFileEx_ through +stdlib's ``ctypes`` with the appropriate flags. + +Note that with ``link`` and ``unlink``, there's a timewindow where the file +might be available under two entries in the filesystem: The name of the +temporary file, and the name of the target file. + +Also note that the permissions of the target file may change this way. In some +situations a ``chmod`` can be issued without any concurrency problems, but +since that is not always the case, this library doesn't do it by itself. + +.. _MoveFileEx: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx + +fsync +----- + +On POSIX, ``fsync`` is invoked on the temporary file after it is written (to +flush file content and metadata), and on the parent directory after the file is +moved (to flush filename). + +``fsync`` does not take care of disks' internal buffers, but there don't seem +to be any standard POSIX APIs for that. On OS X, ``fcntl`` is used with +``F_FULLFSYNC`` instead of ``fsync`` for that reason. + +On Windows, `_commit `_ +is used, but there are no guarantees about disk internal buffers. + +Alternatives and Credit +======================= + +Atomicwrites is directly inspired by the following libraries (and shares a +minimal amount of code): + +- The Trac project's `utility functions + `_, + also used in `Werkzeug `_ and + `mitsuhiko/python-atomicfile + `_. The idea to use + ``ctypes`` instead of ``PyWin32`` originated there. + +- `abarnert/fatomic `_. Windows support + (based on ``PyWin32``) was originally taken from there. + +Other alternatives to atomicwrites include: + +- `sashka/atomicfile `_. Originally I + considered using that, but at the time it was lacking a lot of features I + needed (Windows support, overwrite-parameter, overriding behavior through + subclassing). + +- The `Boltons library collection `_ + features a class for atomic file writes, which seems to have a very similar + ``overwrite`` parameter. It is lacking Windows support though. + +License +======= + +Licensed under the MIT, see ``LICENSE``. diff --git a/third_party/python/atomicwrites/atomicwrites/__init__.py b/third_party/python/atomicwrites/atomicwrites/__init__.py new file mode 100644 index 000000000000..a182c07afd99 --- /dev/null +++ b/third_party/python/atomicwrites/atomicwrites/__init__.py @@ -0,0 +1,201 @@ +import contextlib +import os +import sys +import tempfile + +try: + import fcntl +except ImportError: + fcntl = None + +__version__ = '1.1.5' + + +PY2 = sys.version_info[0] == 2 + +text_type = unicode if PY2 else str # noqa + + +def _path_to_unicode(x): + if not isinstance(x, text_type): + return x.decode(sys.getfilesystemencoding()) + return x + + +_proper_fsync = os.fsync + + +if sys.platform != 'win32': + if hasattr(fcntl, 'F_FULLFSYNC'): + def _proper_fsync(fd): + # https://lists.apple.com/archives/darwin-dev/2005/Feb/msg00072.html + # https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/fsync.2.html + # https://github.com/untitaker/python-atomicwrites/issues/6 + fcntl.fcntl(fd, fcntl.F_FULLFSYNC) + + def _sync_directory(directory): + # Ensure that filenames are written to disk + fd = os.open(directory, 0) + try: + _proper_fsync(fd) + finally: + os.close(fd) + + def _replace_atomic(src, dst): + os.rename(src, dst) + _sync_directory(os.path.normpath(os.path.dirname(dst))) + + def _move_atomic(src, dst): + os.link(src, dst) + os.unlink(src) + + src_dir = os.path.normpath(os.path.dirname(src)) + dst_dir = os.path.normpath(os.path.dirname(dst)) + _sync_directory(dst_dir) + if src_dir != dst_dir: + _sync_directory(src_dir) +else: + from ctypes import windll, WinError + + _MOVEFILE_REPLACE_EXISTING = 0x1 + _MOVEFILE_WRITE_THROUGH = 0x8 + _windows_default_flags = _MOVEFILE_WRITE_THROUGH + + def _handle_errors(rv): + if not rv: + raise WinError() + + def _replace_atomic(src, dst): + _handle_errors(windll.kernel32.MoveFileExW( + _path_to_unicode(src), _path_to_unicode(dst), + _windows_default_flags | _MOVEFILE_REPLACE_EXISTING + )) + + def _move_atomic(src, dst): + _handle_errors(windll.kernel32.MoveFileExW( + _path_to_unicode(src), _path_to_unicode(dst), + _windows_default_flags + )) + + +def replace_atomic(src, dst): + ''' + Move ``src`` to ``dst``. If ``dst`` exists, it will be silently + overwritten. + + Both paths must reside on the same filesystem for the operation to be + atomic. + ''' + return _replace_atomic(src, dst) + + +def move_atomic(src, dst): + ''' + Move ``src`` to ``dst``. There might a timewindow where both filesystem + entries exist. If ``dst`` already exists, :py:exc:`FileExistsError` will be + raised. + + Both paths must reside on the same filesystem for the operation to be + atomic. + ''' + return _move_atomic(src, dst) + + +class AtomicWriter(object): + ''' + A helper class for performing atomic writes. Usage:: + + with AtomicWriter(path).open() as f: + f.write(...) + + :param path: The destination filepath. May or may not exist. + :param mode: The filemode for the temporary file. + :param overwrite: If set to false, an error is raised if ``path`` exists. + Errors are only raised after the file has been written to. Either way, + the operation is atomic. + + If you need further control over the exact behavior, you are encouraged to + subclass. + ''' + + def __init__(self, path, mode='w', overwrite=False): + if 'a' in mode: + raise ValueError( + 'Appending to an existing file is not supported, because that ' + 'would involve an expensive `copy`-operation to a temporary ' + 'file. Open the file in normal `w`-mode and copy explicitly ' + 'if that\'s what you\'re after.' + ) + if 'x' in mode: + raise ValueError('Use the `overwrite`-parameter instead.') + if 'w' not in mode: + raise ValueError('AtomicWriters can only be written to.') + + self._path = path + self._mode = mode + self._overwrite = overwrite + + def open(self): + ''' + Open the temporary file. + ''' + return self._open(self.get_fileobject) + + @contextlib.contextmanager + def _open(self, get_fileobject): + f = None # make sure f exists even if get_fileobject() fails + try: + success = False + with get_fileobject() as f: + yield f + self.sync(f) + self.commit(f) + success = True + finally: + if not success: + try: + self.rollback(f) + except Exception: + pass + + def get_fileobject(self, dir=None, **kwargs): + '''Return the temporary file to use.''' + if dir is None: + dir = os.path.normpath(os.path.dirname(self._path)) + return tempfile.NamedTemporaryFile(mode=self._mode, dir=dir, + delete=False, **kwargs) + + def sync(self, f): + '''responsible for clearing as many file caches as possible before + commit''' + f.flush() + _proper_fsync(f.fileno()) + + def commit(self, f): + '''Move the temporary file to the target location.''' + if self._overwrite: + replace_atomic(f.name, self._path) + else: + move_atomic(f.name, self._path) + + def rollback(self, f): + '''Clean up all temporary resources.''' + os.unlink(f.name) + + +def atomic_write(path, writer_cls=AtomicWriter, **cls_kwargs): + ''' + Simple atomic writes. This wraps :py:class:`AtomicWriter`:: + + with atomic_write(path) as f: + f.write(...) + + :param path: The target path to write to. + :param writer_cls: The writer class to use. This parameter is useful if you + subclassed :py:class:`AtomicWriter` to change some behavior and want to + use that new subclass. + + Additional keyword arguments are passed to the writer class. See + :py:class:`AtomicWriter`. + ''' + return writer_cls(path, **cls_kwargs).open() diff --git a/third_party/python/atomicwrites/docs/Makefile b/third_party/python/atomicwrites/docs/Makefile new file mode 100644 index 000000000000..af5f9d9aa53a --- /dev/null +++ b/third_party/python/atomicwrites/docs/Makefile @@ -0,0 +1,177 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/atomicwrites.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/atomicwrites.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/atomicwrites" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/atomicwrites" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/third_party/python/atomicwrites/docs/conf.py b/third_party/python/atomicwrites/docs/conf.py new file mode 100644 index 000000000000..b7c7b59c4dd4 --- /dev/null +++ b/third_party/python/atomicwrites/docs/conf.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python + +import os +import sys +import pkg_resources + +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.intersphinx', + 'sphinx.ext.viewcode', +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = 'atomicwrites' +copyright = '2015, Markus Unterwaditzer' + +try: + # The full version, including alpha/beta/rc tags. + release = pkg_resources.require('atomicwrites')[0].version +except pkg_resources.DistributionNotFound: + print('To build the documentation, the distribution information of ' + 'atomicwrites has to be available. Run "setup.py develop" to do ' + 'this.') + sys.exit(1) + +version = '.'.join(release.split('.')[:2]) # The short X.Y version. + +on_rtd = os.environ.get('READTHEDOCS', None) == 'True' + +try: + import sphinx_rtd_theme + html_theme = 'sphinx_rtd_theme' + html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] +except ImportError: + html_theme = 'default' + if not on_rtd: + print('-' * 74) + print('Warning: sphinx-rtd-theme not installed, building with default ' + 'theme.') + print('-' * 74) + + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + + +# Output file base name for HTML help builder. +htmlhelp_basename = 'atomicwritesdoc' + + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = {} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + ('index', 'atomicwrites.tex', 'atomicwrites Documentation', + 'Markus Unterwaditzer', 'manual'), +] + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'atomicwrites', 'atomicwrites Documentation', + ['Markus Unterwaditzer'], 1) +] + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'atomicwrites', 'atomicwrites Documentation', + 'Markus Unterwaditzer', 'atomicwrites', 'One line description of project.', + 'Miscellaneous'), +] + +# Bibliographic Dublin Core info. +epub_title = 'atomicwrites' +epub_author = 'Markus Unterwaditzer' +epub_publisher = 'Markus Unterwaditzer' +epub_copyright = '2015, Markus Unterwaditzer' + +# A list of files that should not be packed into the epub file. +epub_exclude_files = ['search.html'] + +# Example configuration for intersphinx: refer to the Python standard library. +intersphinx_mapping = {'http://docs.python.org/': None} diff --git a/third_party/python/atomicwrites/docs/index.rst b/third_party/python/atomicwrites/docs/index.rst new file mode 100644 index 000000000000..0391c0447712 --- /dev/null +++ b/third_party/python/atomicwrites/docs/index.rst @@ -0,0 +1,35 @@ +.. include:: ../README.rst + +.. module:: atomicwrites + +API +=== + +.. autofunction:: atomic_write + + +Errorhandling +------------- + +All filesystem errors are subclasses of :py:exc:`OSError`. + +- On UNIX systems, errors from the Python stdlib calls are thrown. +- On Windows systems, errors from Python's ``ctypes`` are thrown. + +In either case, the ``errno`` attribute on the thrown exception maps to an +errorcode in the ``errno`` module. + +Low-level API +------------- + +.. autofunction:: replace_atomic + +.. autofunction:: move_atomic + +.. autoclass:: AtomicWriter + :members: + +License +======= + +.. include:: ../LICENSE diff --git a/third_party/python/atomicwrites/docs/make.bat b/third_party/python/atomicwrites/docs/make.bat new file mode 100644 index 000000000000..36fd3f6baf5a --- /dev/null +++ b/third_party/python/atomicwrites/docs/make.bat @@ -0,0 +1,242 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=_build +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . +set I18NSPHINXOPTS=%SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% + set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. singlehtml to make a single large HTML file + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. devhelp to make HTML files and a Devhelp project + echo. epub to make an epub + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. text to make text files + echo. man to make manual pages + echo. texinfo to make Texinfo files + echo. gettext to make PO message catalogs + echo. changes to make an overview over all changed/added/deprecated items + echo. xml to make Docutils-native XML files + echo. pseudoxml to make pseudoxml-XML files for display purposes + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + + +%SPHINXBUILD% 2> nul +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\atomicwrites.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\atomicwrites.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdf" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf + cd %BUILDDIR%/.. + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdfja" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf-ja + cd %BUILDDIR%/.. + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "texinfo" ( + %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. + goto end +) + +if "%1" == "gettext" ( + %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The message catalogs are in %BUILDDIR%/locale. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +if "%1" == "xml" ( + %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The XML files are in %BUILDDIR%/xml. + goto end +) + +if "%1" == "pseudoxml" ( + %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. + goto end +) + +:end diff --git a/third_party/python/atomicwrites/setup.cfg b/third_party/python/atomicwrites/setup.cfg new file mode 100644 index 000000000000..3e89eac2c0dd --- /dev/null +++ b/third_party/python/atomicwrites/setup.cfg @@ -0,0 +1,8 @@ +[wheel] +universal = 1 + +[egg_info] +tag_date = 0 +tag_svn_revision = 0 +tag_build = + diff --git a/third_party/python/atomicwrites/setup.py b/third_party/python/atomicwrites/setup.py new file mode 100644 index 000000000000..98488e9b9830 --- /dev/null +++ b/third_party/python/atomicwrites/setup.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +import ast +import re + +from setuptools import find_packages, setup + + +_version_re = re.compile(r'__version__\s+=\s+(.*)') + + +with open('atomicwrites/__init__.py', 'rb') as f: + version = str(ast.literal_eval(_version_re.search( + f.read().decode('utf-8')).group(1))) + +setup( + name='atomicwrites', + version=version, + author='Markus Unterwaditzer', + author_email='markus@unterwaditzer.net', + url='https://github.com/untitaker/python-atomicwrites', + description='Atomic file writes.', + license='MIT', + long_description=open('README.rst').read(), + packages=find_packages(exclude=['tests.*', 'tests']), + include_package_data=True, +) diff --git a/third_party/python/atomicwrites/tests/test_atomicwrites.py b/third_party/python/atomicwrites/tests/test_atomicwrites.py new file mode 100644 index 000000000000..957719961800 --- /dev/null +++ b/third_party/python/atomicwrites/tests/test_atomicwrites.py @@ -0,0 +1,89 @@ +import errno +import os + +from atomicwrites import atomic_write + +import pytest + + +def test_atomic_write(tmpdir): + fname = tmpdir.join('ha') + for i in range(2): + with atomic_write(str(fname), overwrite=True) as f: + f.write('hoho') + + with pytest.raises(OSError) as excinfo: + with atomic_write(str(fname), overwrite=False) as f: + f.write('haha') + + assert excinfo.value.errno == errno.EEXIST + + assert fname.read() == 'hoho' + assert len(tmpdir.listdir()) == 1 + + +def test_teardown(tmpdir): + fname = tmpdir.join('ha') + with pytest.raises(AssertionError): + with atomic_write(str(fname), overwrite=True): + assert False + + assert not tmpdir.listdir() + + +def test_replace_simultaneously_created_file(tmpdir): + fname = tmpdir.join('ha') + with atomic_write(str(fname), overwrite=True) as f: + f.write('hoho') + fname.write('harhar') + assert fname.read() == 'harhar' + assert fname.read() == 'hoho' + assert len(tmpdir.listdir()) == 1 + + +def test_dont_remove_simultaneously_created_file(tmpdir): + fname = tmpdir.join('ha') + with pytest.raises(OSError) as excinfo: + with atomic_write(str(fname), overwrite=False) as f: + f.write('hoho') + fname.write('harhar') + assert fname.read() == 'harhar' + + assert excinfo.value.errno == errno.EEXIST + assert fname.read() == 'harhar' + assert len(tmpdir.listdir()) == 1 + + +# Verify that nested exceptions during rollback do not overwrite the initial +# exception that triggered a rollback. +def test_open_reraise(tmpdir): + fname = tmpdir.join('ha') + with pytest.raises(AssertionError): + with atomic_write(str(fname), overwrite=False) as f: + # Mess with f, so rollback will trigger an OSError. We're testing + # that the initial AssertionError triggered below is propagated up + # the stack, not the second exception triggered during rollback. + f.name = "asdf" + # Now trigger our own exception. + assert False, "Intentional failure for testing purposes" + + +def test_atomic_write_in_pwd(tmpdir): + orig_curdir = os.getcwd() + try: + os.chdir(str(tmpdir)) + fname = 'ha' + for i in range(2): + with atomic_write(str(fname), overwrite=True) as f: + f.write('hoho') + + with pytest.raises(OSError) as excinfo: + with atomic_write(str(fname), overwrite=False) as f: + f.write('haha') + + assert excinfo.value.errno == errno.EEXIST + + assert open(fname).read() == 'hoho' + assert len(tmpdir.listdir()) == 1 + finally: + os.chdir(orig_curdir) diff --git a/third_party/python/funcsigs/CHANGELOG b/third_party/python/funcsigs/CHANGELOG new file mode 100644 index 000000000000..e1366d2668d0 --- /dev/null +++ b/third_party/python/funcsigs/CHANGELOG @@ -0,0 +1,24 @@ +Changelog +--------- + +0.5 +``` + +* Fix binding with self as a kwarg. (Robert Collins #14) + +0.4 (2013-12-20) +```````````````` +* Fix unbound methods getting their first parameter curried +* Publish Python wheel packages + +0.3 (2013-05-29) +```````````````` +* Fix annotation formatting of builtin types on Python 2.x + +0.2 (2012-01-07) +```````````````` +* PyPy compatability + +0.1 (2012-01-06) +```````````````` +* Initial release diff --git a/third_party/python/funcsigs/LICENSE b/third_party/python/funcsigs/LICENSE new file mode 100644 index 000000000000..3e563d6fbd4d --- /dev/null +++ b/third_party/python/funcsigs/LICENSE @@ -0,0 +1,13 @@ +Copyright 2013 Aaron Iles + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/python/funcsigs/MANIFEST.in b/third_party/python/funcsigs/MANIFEST.in new file mode 100644 index 000000000000..f0abb42f04a1 --- /dev/null +++ b/third_party/python/funcsigs/MANIFEST.in @@ -0,0 +1,7 @@ +recursive-include docs * +recursive-include tests *.py +include *.py +include CHANGELOG +include LICENSE +include MANIFEST.in +include README.rst diff --git a/third_party/python/funcsigs/PKG-INFO b/third_party/python/funcsigs/PKG-INFO new file mode 100644 index 000000000000..e262a8d1b955 --- /dev/null +++ b/third_party/python/funcsigs/PKG-INFO @@ -0,0 +1,378 @@ +Metadata-Version: 1.1 +Name: funcsigs +Version: 1.0.2 +Summary: Python function signatures from PEP362 for Python 2.6, 2.7 and 3.2+ +Home-page: http://funcsigs.readthedocs.org +Author: Testing Cabal +Author-email: testing-in-python@lists.idyll.org +License: ASL +Description: .. funcsigs documentation master file, created by + sphinx-quickstart on Fri Apr 20 20:27:52 2012. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + + Introducing funcsigs + ==================== + + The Funcsigs Package + -------------------- + + ``funcsigs`` is a backport of the `PEP 362`_ function signature features from + Python 3.3's `inspect`_ module. The backport is compatible with Python 2.6, 2.7 + as well as 3.3 and up. 3.2 was supported by version 0.4, but with setuptools and + pip no longer supporting 3.2, we cannot make any statement about 3.2 + compatibility. + + Compatibility + ````````````` + + The ``funcsigs`` backport has been tested against: + + * CPython 2.6 + * CPython 2.7 + * CPython 3.3 + * CPython 3.4 + * CPython 3.5 + * CPython nightlies + * PyPy and PyPy3(currently failing CI) + + Continuous integration testing is provided by `Travis CI`_. + + Under Python 2.x there is a compatibility issue when a function is assigned to + the ``__wrapped__`` property of a class after it has been constructed. + Similiarily there under PyPy directly passing the ``__call__`` method of a + builtin is also a compatibility issues. Otherwise the functionality is + believed to be uniform between both Python2 and Python3. + + Issues + `````` + + Source code for ``funcsigs`` is hosted on `GitHub`_. Any bug reports or feature + requests can be made using GitHub's `issues system`_. |build_status| |coverage| + + Example + ------- + + To obtain a `Signature` object, pass the target function to the + ``funcsigs.signature`` function. + + .. code-block:: python + + >>> from funcsigs import signature + >>> def foo(a, b=None, *args, **kwargs): + ... pass + ... + >>> sig = signature(foo) + >>> sig + + >>> sig.parameters + OrderedDict([('a', ), ('b', ), ('args', ), ('kwargs', )]) + >>> sig.return_annotation + + + Introspecting callables with the Signature object + ------------------------------------------------- + + .. note:: + + This section of documentation is a direct reproduction of the Python + standard library documentation for the inspect module. + + The Signature object represents the call signature of a callable object and its + return annotation. To retrieve a Signature object, use the :func:`signature` + function. + + .. function:: signature(callable) + + Return a :class:`Signature` object for the given ``callable``:: + + >>> from funcsigs import signature + >>> def foo(a, *, b:int, **kwargs): + ... pass + + >>> sig = signature(foo) + + >>> str(sig) + '(a, *, b:int, **kwargs)' + + >>> str(sig.parameters['b']) + 'b:int' + + >>> sig.parameters['b'].annotation + + + Accepts a wide range of python callables, from plain functions and classes to + :func:`functools.partial` objects. + + .. note:: + + Some callables may not be introspectable in certain implementations of + Python. For example, in CPython, built-in functions defined in C provide + no metadata about their arguments. + + + .. class:: Signature + + A Signature object represents the call signature of a function and its return + annotation. For each parameter accepted by the function it stores a + :class:`Parameter` object in its :attr:`parameters` collection. + + Signature objects are *immutable*. Use :meth:`Signature.replace` to make a + modified copy. + + .. attribute:: Signature.empty + + A special class-level marker to specify absence of a return annotation. + + .. attribute:: Signature.parameters + + An ordered mapping of parameters' names to the corresponding + :class:`Parameter` objects. + + .. attribute:: Signature.return_annotation + + The "return" annotation for the callable. If the callable has no "return" + annotation, this attribute is set to :attr:`Signature.empty`. + + .. method:: Signature.bind(*args, **kwargs) + + Create a mapping from positional and keyword arguments to parameters. + Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the + signature, or raises a :exc:`TypeError`. + + .. method:: Signature.bind_partial(*args, **kwargs) + + Works the same way as :meth:`Signature.bind`, but allows the omission of + some required arguments (mimics :func:`functools.partial` behavior.) + Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the + passed arguments do not match the signature. + + .. method:: Signature.replace(*[, parameters][, return_annotation]) + + Create a new Signature instance based on the instance replace was invoked + on. It is possible to pass different ``parameters`` and/or + ``return_annotation`` to override the corresponding properties of the base + signature. To remove return_annotation from the copied Signature, pass in + :attr:`Signature.empty`. + + :: + + >>> def test(a, b): + ... pass + >>> sig = signature(test) + >>> new_sig = sig.replace(return_annotation="new return anno") + >>> str(new_sig) + "(a, b) -> 'new return anno'" + + + .. class:: Parameter + + Parameter objects are *immutable*. Instead of modifying a Parameter object, + you can use :meth:`Parameter.replace` to create a modified copy. + + .. attribute:: Parameter.empty + + A special class-level marker to specify absence of default values and + annotations. + + .. attribute:: Parameter.name + + The name of the parameter as a string. Must be a valid python identifier + name (with the exception of ``POSITIONAL_ONLY`` parameters, which can have + it set to ``None``). + + .. attribute:: Parameter.default + + The default value for the parameter. If the parameter has no default + value, this attribute is set to :attr:`Parameter.empty`. + + .. attribute:: Parameter.annotation + + The annotation for the parameter. If the parameter has no annotation, + this attribute is set to :attr:`Parameter.empty`. + + .. attribute:: Parameter.kind + + Describes how argument values are bound to the parameter. Possible values + (accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``): + + +------------------------+----------------------------------------------+ + | Name | Meaning | + +========================+==============================================+ + | *POSITIONAL_ONLY* | Value must be supplied as a positional | + | | argument. | + | | | + | | Python has no explicit syntax for defining | + | | positional-only parameters, but many built-in| + | | and extension module functions (especially | + | | those that accept only one or two parameters)| + | | accept them. | + +------------------------+----------------------------------------------+ + | *POSITIONAL_OR_KEYWORD*| Value may be supplied as either a keyword or | + | | positional argument (this is the standard | + | | binding behaviour for functions implemented | + | | in Python.) | + +------------------------+----------------------------------------------+ + | *VAR_POSITIONAL* | A tuple of positional arguments that aren't | + | | bound to any other parameter. This | + | | corresponds to a ``*args`` parameter in a | + | | Python function definition. | + +------------------------+----------------------------------------------+ + | *KEYWORD_ONLY* | Value must be supplied as a keyword argument.| + | | Keyword only parameters are those which | + | | appear after a ``*`` or ``*args`` entry in a | + | | Python function definition. | + +------------------------+----------------------------------------------+ + | *VAR_KEYWORD* | A dict of keyword arguments that aren't bound| + | | to any other parameter. This corresponds to a| + | | ``**kwargs`` parameter in a Python function | + | | definition. | + +------------------------+----------------------------------------------+ + + Example: print all keyword-only arguments without default values:: + + >>> def foo(a, b, *, c, d=10): + ... pass + + >>> sig = signature(foo) + >>> for param in sig.parameters.values(): + ... if (param.kind == param.KEYWORD_ONLY and + ... param.default is param.empty): + ... print('Parameter:', param) + Parameter: c + + .. method:: Parameter.replace(*[, name][, kind][, default][, annotation]) + + Create a new Parameter instance based on the instance replaced was invoked + on. To override a :class:`Parameter` attribute, pass the corresponding + argument. To remove a default value or/and an annotation from a + Parameter, pass :attr:`Parameter.empty`. + + :: + + >>> from funcsigs import Parameter + >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) + >>> str(param) + 'foo=42' + + >>> str(param.replace()) # Will create a shallow copy of 'param' + 'foo=42' + + >>> str(param.replace(default=Parameter.empty, annotation='spam')) + "foo:'spam'" + + + .. class:: BoundArguments + + Result of a :meth:`Signature.bind` or :meth:`Signature.bind_partial` call. + Holds the mapping of arguments to the function's parameters. + + .. attribute:: BoundArguments.arguments + + An ordered, mutable mapping (:class:`collections.OrderedDict`) of + parameters' names to arguments' values. Contains only explicitly bound + arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and + :attr:`kwargs`. + + Should be used in conjunction with :attr:`Signature.parameters` for any + argument processing purposes. + + .. note:: + + Arguments for which :meth:`Signature.bind` or + :meth:`Signature.bind_partial` relied on a default value are skipped. + However, if needed, it is easy to include them. + + :: + + >>> def foo(a, b=10): + ... pass + + >>> sig = signature(foo) + >>> ba = sig.bind(5) + + >>> ba.args, ba.kwargs + ((5,), {}) + + >>> for param in sig.parameters.values(): + ... if param.name not in ba.arguments: + ... ba.arguments[param.name] = param.default + + >>> ba.args, ba.kwargs + ((5, 10), {}) + + + .. attribute:: BoundArguments.args + + A tuple of positional arguments values. Dynamically computed from the + :attr:`arguments` attribute. + + .. attribute:: BoundArguments.kwargs + + A dict of keyword arguments values. Dynamically computed from the + :attr:`arguments` attribute. + + The :attr:`args` and :attr:`kwargs` properties can be used to invoke + functions:: + + def test(a, *, b): + ... + + sig = signature(test) + ba = sig.bind(10, b=20) + test(*ba.args, **ba.kwargs) + + + .. seealso:: + + :pep:`362` - Function Signature Object. + The detailed specification, implementation details and examples. + + Copyright + --------- + + *funcsigs* is a derived work of CPython under the terms of the `PSF License + Agreement`_. The original CPython inspect module, its unit tests and + documentation are the copyright of the Python Software Foundation. The derived + work is distributed under the `Apache License Version 2.0`_. + + .. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python + .. _Apache License Version 2.0: http://opensource.org/licenses/Apache-2.0 + .. _GitHub: https://github.com/testing-cabal/funcsigs + .. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python + .. _Travis CI: http://travis-ci.org/ + .. _Read The Docs: http://funcsigs.readthedocs.org/ + .. _PEP 362: http://www.python.org/dev/peps/pep-0362/ + .. _inspect: http://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object + .. _issues system: https://github.com/testing-cabal/funcsigs/issues + + .. |build_status| image:: https://secure.travis-ci.org/aliles/funcsigs.png?branch=master + :target: http://travis-ci.org/#!/aliles/funcsigs + :alt: Current build status + + .. |coverage| image:: https://coveralls.io/repos/aliles/funcsigs/badge.png?branch=master + :target: https://coveralls.io/r/aliles/funcsigs?branch=master + :alt: Coverage status + + .. |pypi_version| image:: https://pypip.in/v/funcsigs/badge.png + :target: https://crate.io/packages/funcsigs/ + :alt: Latest PyPI version + + + +Platform: UNKNOWN +Classifier: Development Status :: 4 - Beta +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: Apache Software License +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python +Classifier: Programming Language :: Python :: 2 +Classifier: Programming Language :: Python :: 2.6 +Classifier: Programming Language :: Python :: 2.7 +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.3 +Classifier: Programming Language :: Python :: 3.4 +Classifier: Programming Language :: Python :: 3.5 +Classifier: Programming Language :: Python :: Implementation :: CPython +Classifier: Programming Language :: Python :: Implementation :: PyPy +Classifier: Topic :: Software Development :: Libraries :: Python Modules diff --git a/third_party/python/funcsigs/README.rst b/third_party/python/funcsigs/README.rst new file mode 100644 index 000000000000..5fbca27e6e6b --- /dev/null +++ b/third_party/python/funcsigs/README.rst @@ -0,0 +1,353 @@ +.. funcsigs documentation master file, created by + sphinx-quickstart on Fri Apr 20 20:27:52 2012. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Introducing funcsigs +==================== + +The Funcsigs Package +-------------------- + +``funcsigs`` is a backport of the `PEP 362`_ function signature features from +Python 3.3's `inspect`_ module. The backport is compatible with Python 2.6, 2.7 +as well as 3.3 and up. 3.2 was supported by version 0.4, but with setuptools and +pip no longer supporting 3.2, we cannot make any statement about 3.2 +compatibility. + +Compatibility +````````````` + +The ``funcsigs`` backport has been tested against: + +* CPython 2.6 +* CPython 2.7 +* CPython 3.3 +* CPython 3.4 +* CPython 3.5 +* CPython nightlies +* PyPy and PyPy3(currently failing CI) + +Continuous integration testing is provided by `Travis CI`_. + +Under Python 2.x there is a compatibility issue when a function is assigned to +the ``__wrapped__`` property of a class after it has been constructed. +Similiarily there under PyPy directly passing the ``__call__`` method of a +builtin is also a compatibility issues. Otherwise the functionality is +believed to be uniform between both Python2 and Python3. + +Issues +`````` + +Source code for ``funcsigs`` is hosted on `GitHub`_. Any bug reports or feature +requests can be made using GitHub's `issues system`_. |build_status| |coverage| + +Example +------- + +To obtain a `Signature` object, pass the target function to the +``funcsigs.signature`` function. + +.. code-block:: python + + >>> from funcsigs import signature + >>> def foo(a, b=None, *args, **kwargs): + ... pass + ... + >>> sig = signature(foo) + >>> sig + + >>> sig.parameters + OrderedDict([('a', ), ('b', ), ('args', ), ('kwargs', )]) + >>> sig.return_annotation + + +Introspecting callables with the Signature object +------------------------------------------------- + +.. note:: + + This section of documentation is a direct reproduction of the Python + standard library documentation for the inspect module. + +The Signature object represents the call signature of a callable object and its +return annotation. To retrieve a Signature object, use the :func:`signature` +function. + +.. function:: signature(callable) + + Return a :class:`Signature` object for the given ``callable``:: + + >>> from funcsigs import signature + >>> def foo(a, *, b:int, **kwargs): + ... pass + + >>> sig = signature(foo) + + >>> str(sig) + '(a, *, b:int, **kwargs)' + + >>> str(sig.parameters['b']) + 'b:int' + + >>> sig.parameters['b'].annotation + + + Accepts a wide range of python callables, from plain functions and classes to + :func:`functools.partial` objects. + + .. note:: + + Some callables may not be introspectable in certain implementations of + Python. For example, in CPython, built-in functions defined in C provide + no metadata about their arguments. + + +.. class:: Signature + + A Signature object represents the call signature of a function and its return + annotation. For each parameter accepted by the function it stores a + :class:`Parameter` object in its :attr:`parameters` collection. + + Signature objects are *immutable*. Use :meth:`Signature.replace` to make a + modified copy. + + .. attribute:: Signature.empty + + A special class-level marker to specify absence of a return annotation. + + .. attribute:: Signature.parameters + + An ordered mapping of parameters' names to the corresponding + :class:`Parameter` objects. + + .. attribute:: Signature.return_annotation + + The "return" annotation for the callable. If the callable has no "return" + annotation, this attribute is set to :attr:`Signature.empty`. + + .. method:: Signature.bind(*args, **kwargs) + + Create a mapping from positional and keyword arguments to parameters. + Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the + signature, or raises a :exc:`TypeError`. + + .. method:: Signature.bind_partial(*args, **kwargs) + + Works the same way as :meth:`Signature.bind`, but allows the omission of + some required arguments (mimics :func:`functools.partial` behavior.) + Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the + passed arguments do not match the signature. + + .. method:: Signature.replace(*[, parameters][, return_annotation]) + + Create a new Signature instance based on the instance replace was invoked + on. It is possible to pass different ``parameters`` and/or + ``return_annotation`` to override the corresponding properties of the base + signature. To remove return_annotation from the copied Signature, pass in + :attr:`Signature.empty`. + + :: + + >>> def test(a, b): + ... pass + >>> sig = signature(test) + >>> new_sig = sig.replace(return_annotation="new return anno") + >>> str(new_sig) + "(a, b) -> 'new return anno'" + + +.. class:: Parameter + + Parameter objects are *immutable*. Instead of modifying a Parameter object, + you can use :meth:`Parameter.replace` to create a modified copy. + + .. attribute:: Parameter.empty + + A special class-level marker to specify absence of default values and + annotations. + + .. attribute:: Parameter.name + + The name of the parameter as a string. Must be a valid python identifier + name (with the exception of ``POSITIONAL_ONLY`` parameters, which can have + it set to ``None``). + + .. attribute:: Parameter.default + + The default value for the parameter. If the parameter has no default + value, this attribute is set to :attr:`Parameter.empty`. + + .. attribute:: Parameter.annotation + + The annotation for the parameter. If the parameter has no annotation, + this attribute is set to :attr:`Parameter.empty`. + + .. attribute:: Parameter.kind + + Describes how argument values are bound to the parameter. Possible values + (accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``): + + +------------------------+----------------------------------------------+ + | Name | Meaning | + +========================+==============================================+ + | *POSITIONAL_ONLY* | Value must be supplied as a positional | + | | argument. | + | | | + | | Python has no explicit syntax for defining | + | | positional-only parameters, but many built-in| + | | and extension module functions (especially | + | | those that accept only one or two parameters)| + | | accept them. | + +------------------------+----------------------------------------------+ + | *POSITIONAL_OR_KEYWORD*| Value may be supplied as either a keyword or | + | | positional argument (this is the standard | + | | binding behaviour for functions implemented | + | | in Python.) | + +------------------------+----------------------------------------------+ + | *VAR_POSITIONAL* | A tuple of positional arguments that aren't | + | | bound to any other parameter. This | + | | corresponds to a ``*args`` parameter in a | + | | Python function definition. | + +------------------------+----------------------------------------------+ + | *KEYWORD_ONLY* | Value must be supplied as a keyword argument.| + | | Keyword only parameters are those which | + | | appear after a ``*`` or ``*args`` entry in a | + | | Python function definition. | + +------------------------+----------------------------------------------+ + | *VAR_KEYWORD* | A dict of keyword arguments that aren't bound| + | | to any other parameter. This corresponds to a| + | | ``**kwargs`` parameter in a Python function | + | | definition. | + +------------------------+----------------------------------------------+ + + Example: print all keyword-only arguments without default values:: + + >>> def foo(a, b, *, c, d=10): + ... pass + + >>> sig = signature(foo) + >>> for param in sig.parameters.values(): + ... if (param.kind == param.KEYWORD_ONLY and + ... param.default is param.empty): + ... print('Parameter:', param) + Parameter: c + + .. method:: Parameter.replace(*[, name][, kind][, default][, annotation]) + + Create a new Parameter instance based on the instance replaced was invoked + on. To override a :class:`Parameter` attribute, pass the corresponding + argument. To remove a default value or/and an annotation from a + Parameter, pass :attr:`Parameter.empty`. + + :: + + >>> from funcsigs import Parameter + >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) + >>> str(param) + 'foo=42' + + >>> str(param.replace()) # Will create a shallow copy of 'param' + 'foo=42' + + >>> str(param.replace(default=Parameter.empty, annotation='spam')) + "foo:'spam'" + + +.. class:: BoundArguments + + Result of a :meth:`Signature.bind` or :meth:`Signature.bind_partial` call. + Holds the mapping of arguments to the function's parameters. + + .. attribute:: BoundArguments.arguments + + An ordered, mutable mapping (:class:`collections.OrderedDict`) of + parameters' names to arguments' values. Contains only explicitly bound + arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and + :attr:`kwargs`. + + Should be used in conjunction with :attr:`Signature.parameters` for any + argument processing purposes. + + .. note:: + + Arguments for which :meth:`Signature.bind` or + :meth:`Signature.bind_partial` relied on a default value are skipped. + However, if needed, it is easy to include them. + + :: + + >>> def foo(a, b=10): + ... pass + + >>> sig = signature(foo) + >>> ba = sig.bind(5) + + >>> ba.args, ba.kwargs + ((5,), {}) + + >>> for param in sig.parameters.values(): + ... if param.name not in ba.arguments: + ... ba.arguments[param.name] = param.default + + >>> ba.args, ba.kwargs + ((5, 10), {}) + + + .. attribute:: BoundArguments.args + + A tuple of positional arguments values. Dynamically computed from the + :attr:`arguments` attribute. + + .. attribute:: BoundArguments.kwargs + + A dict of keyword arguments values. Dynamically computed from the + :attr:`arguments` attribute. + + The :attr:`args` and :attr:`kwargs` properties can be used to invoke + functions:: + + def test(a, *, b): + ... + + sig = signature(test) + ba = sig.bind(10, b=20) + test(*ba.args, **ba.kwargs) + + +.. seealso:: + + :pep:`362` - Function Signature Object. + The detailed specification, implementation details and examples. + +Copyright +--------- + +*funcsigs* is a derived work of CPython under the terms of the `PSF License +Agreement`_. The original CPython inspect module, its unit tests and +documentation are the copyright of the Python Software Foundation. The derived +work is distributed under the `Apache License Version 2.0`_. + +.. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python +.. _Apache License Version 2.0: http://opensource.org/licenses/Apache-2.0 +.. _GitHub: https://github.com/testing-cabal/funcsigs +.. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python +.. _Travis CI: http://travis-ci.org/ +.. _Read The Docs: http://funcsigs.readthedocs.org/ +.. _PEP 362: http://www.python.org/dev/peps/pep-0362/ +.. _inspect: http://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object +.. _issues system: https://github.com/testing-cabal/funcsigs/issues + +.. |build_status| image:: https://secure.travis-ci.org/aliles/funcsigs.png?branch=master + :target: http://travis-ci.org/#!/aliles/funcsigs + :alt: Current build status + +.. |coverage| image:: https://coveralls.io/repos/aliles/funcsigs/badge.png?branch=master + :target: https://coveralls.io/r/aliles/funcsigs?branch=master + :alt: Coverage status + +.. |pypi_version| image:: https://pypip.in/v/funcsigs/badge.png + :target: https://crate.io/packages/funcsigs/ + :alt: Latest PyPI version + + diff --git a/third_party/python/funcsigs/docs/Makefile b/third_party/python/funcsigs/docs/Makefile new file mode 100644 index 000000000000..f7ab3d16b405 --- /dev/null +++ b/third_party/python/funcsigs/docs/Makefile @@ -0,0 +1,153 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + -rm -rf $(BUILDDIR) + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/funcsigs.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/funcsigs.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/funcsigs" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/funcsigs" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." diff --git a/third_party/python/funcsigs/docs/_templates/page.html b/third_party/python/funcsigs/docs/_templates/page.html new file mode 100644 index 000000000000..5e1e00bcafaf --- /dev/null +++ b/third_party/python/funcsigs/docs/_templates/page.html @@ -0,0 +1,9 @@ +{% extends "!page.html" %} +{% block extrahead %} + + Fork me on GitHub + + {{ super() }} +{% endblock %} diff --git a/third_party/python/funcsigs/docs/conf.py b/third_party/python/funcsigs/docs/conf.py new file mode 100644 index 000000000000..c6e4194cc05c --- /dev/null +++ b/third_party/python/funcsigs/docs/conf.py @@ -0,0 +1,251 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# funcsigs documentation build configuration file, created by +# sphinx-quickstart on Fri Apr 20 20:27:52 2012. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +sys.path.insert(0, os.path.abspath('..')) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.viewcode'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = 'funcsigs' +copyright = '2013, Aaron Iles' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +from funcsigs import __version__ +version = '.'.join(__version__.split('.')[:2]) +# The full version, including alpha/beta/rc tags. +release = __version__ + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'agogo' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'funcsigsdoc' + + +# -- Options for LaTeX output -------------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'funcsigs.tex', 'funcsigs Documentation', + 'Aaron Iles', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output -------------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'funcsigs', 'funcsigs Documentation', + ['Aaron Iles'], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------------ + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'funcsigs', 'funcsigs Documentation', + 'Aaron Iles', 'funcsigs', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + + +# Example configuration for intersphinx: refer to the Python standard library. +intersphinx_mapping = { + 'python3': ('http://docs.python.org/py3k', None), + 'python': ('http://docs.python.org/', None) +} diff --git a/third_party/python/funcsigs/docs/index.rst b/third_party/python/funcsigs/docs/index.rst new file mode 100644 index 000000000000..5fbca27e6e6b --- /dev/null +++ b/third_party/python/funcsigs/docs/index.rst @@ -0,0 +1,353 @@ +.. funcsigs documentation master file, created by + sphinx-quickstart on Fri Apr 20 20:27:52 2012. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Introducing funcsigs +==================== + +The Funcsigs Package +-------------------- + +``funcsigs`` is a backport of the `PEP 362`_ function signature features from +Python 3.3's `inspect`_ module. The backport is compatible with Python 2.6, 2.7 +as well as 3.3 and up. 3.2 was supported by version 0.4, but with setuptools and +pip no longer supporting 3.2, we cannot make any statement about 3.2 +compatibility. + +Compatibility +````````````` + +The ``funcsigs`` backport has been tested against: + +* CPython 2.6 +* CPython 2.7 +* CPython 3.3 +* CPython 3.4 +* CPython 3.5 +* CPython nightlies +* PyPy and PyPy3(currently failing CI) + +Continuous integration testing is provided by `Travis CI`_. + +Under Python 2.x there is a compatibility issue when a function is assigned to +the ``__wrapped__`` property of a class after it has been constructed. +Similiarily there under PyPy directly passing the ``__call__`` method of a +builtin is also a compatibility issues. Otherwise the functionality is +believed to be uniform between both Python2 and Python3. + +Issues +`````` + +Source code for ``funcsigs`` is hosted on `GitHub`_. Any bug reports or feature +requests can be made using GitHub's `issues system`_. |build_status| |coverage| + +Example +------- + +To obtain a `Signature` object, pass the target function to the +``funcsigs.signature`` function. + +.. code-block:: python + + >>> from funcsigs import signature + >>> def foo(a, b=None, *args, **kwargs): + ... pass + ... + >>> sig = signature(foo) + >>> sig + + >>> sig.parameters + OrderedDict([('a', ), ('b', ), ('args', ), ('kwargs', )]) + >>> sig.return_annotation + + +Introspecting callables with the Signature object +------------------------------------------------- + +.. note:: + + This section of documentation is a direct reproduction of the Python + standard library documentation for the inspect module. + +The Signature object represents the call signature of a callable object and its +return annotation. To retrieve a Signature object, use the :func:`signature` +function. + +.. function:: signature(callable) + + Return a :class:`Signature` object for the given ``callable``:: + + >>> from funcsigs import signature + >>> def foo(a, *, b:int, **kwargs): + ... pass + + >>> sig = signature(foo) + + >>> str(sig) + '(a, *, b:int, **kwargs)' + + >>> str(sig.parameters['b']) + 'b:int' + + >>> sig.parameters['b'].annotation + + + Accepts a wide range of python callables, from plain functions and classes to + :func:`functools.partial` objects. + + .. note:: + + Some callables may not be introspectable in certain implementations of + Python. For example, in CPython, built-in functions defined in C provide + no metadata about their arguments. + + +.. class:: Signature + + A Signature object represents the call signature of a function and its return + annotation. For each parameter accepted by the function it stores a + :class:`Parameter` object in its :attr:`parameters` collection. + + Signature objects are *immutable*. Use :meth:`Signature.replace` to make a + modified copy. + + .. attribute:: Signature.empty + + A special class-level marker to specify absence of a return annotation. + + .. attribute:: Signature.parameters + + An ordered mapping of parameters' names to the corresponding + :class:`Parameter` objects. + + .. attribute:: Signature.return_annotation + + The "return" annotation for the callable. If the callable has no "return" + annotation, this attribute is set to :attr:`Signature.empty`. + + .. method:: Signature.bind(*args, **kwargs) + + Create a mapping from positional and keyword arguments to parameters. + Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the + signature, or raises a :exc:`TypeError`. + + .. method:: Signature.bind_partial(*args, **kwargs) + + Works the same way as :meth:`Signature.bind`, but allows the omission of + some required arguments (mimics :func:`functools.partial` behavior.) + Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the + passed arguments do not match the signature. + + .. method:: Signature.replace(*[, parameters][, return_annotation]) + + Create a new Signature instance based on the instance replace was invoked + on. It is possible to pass different ``parameters`` and/or + ``return_annotation`` to override the corresponding properties of the base + signature. To remove return_annotation from the copied Signature, pass in + :attr:`Signature.empty`. + + :: + + >>> def test(a, b): + ... pass + >>> sig = signature(test) + >>> new_sig = sig.replace(return_annotation="new return anno") + >>> str(new_sig) + "(a, b) -> 'new return anno'" + + +.. class:: Parameter + + Parameter objects are *immutable*. Instead of modifying a Parameter object, + you can use :meth:`Parameter.replace` to create a modified copy. + + .. attribute:: Parameter.empty + + A special class-level marker to specify absence of default values and + annotations. + + .. attribute:: Parameter.name + + The name of the parameter as a string. Must be a valid python identifier + name (with the exception of ``POSITIONAL_ONLY`` parameters, which can have + it set to ``None``). + + .. attribute:: Parameter.default + + The default value for the parameter. If the parameter has no default + value, this attribute is set to :attr:`Parameter.empty`. + + .. attribute:: Parameter.annotation + + The annotation for the parameter. If the parameter has no annotation, + this attribute is set to :attr:`Parameter.empty`. + + .. attribute:: Parameter.kind + + Describes how argument values are bound to the parameter. Possible values + (accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``): + + +------------------------+----------------------------------------------+ + | Name | Meaning | + +========================+==============================================+ + | *POSITIONAL_ONLY* | Value must be supplied as a positional | + | | argument. | + | | | + | | Python has no explicit syntax for defining | + | | positional-only parameters, but many built-in| + | | and extension module functions (especially | + | | those that accept only one or two parameters)| + | | accept them. | + +------------------------+----------------------------------------------+ + | *POSITIONAL_OR_KEYWORD*| Value may be supplied as either a keyword or | + | | positional argument (this is the standard | + | | binding behaviour for functions implemented | + | | in Python.) | + +------------------------+----------------------------------------------+ + | *VAR_POSITIONAL* | A tuple of positional arguments that aren't | + | | bound to any other parameter. This | + | | corresponds to a ``*args`` parameter in a | + | | Python function definition. | + +------------------------+----------------------------------------------+ + | *KEYWORD_ONLY* | Value must be supplied as a keyword argument.| + | | Keyword only parameters are those which | + | | appear after a ``*`` or ``*args`` entry in a | + | | Python function definition. | + +------------------------+----------------------------------------------+ + | *VAR_KEYWORD* | A dict of keyword arguments that aren't bound| + | | to any other parameter. This corresponds to a| + | | ``**kwargs`` parameter in a Python function | + | | definition. | + +------------------------+----------------------------------------------+ + + Example: print all keyword-only arguments without default values:: + + >>> def foo(a, b, *, c, d=10): + ... pass + + >>> sig = signature(foo) + >>> for param in sig.parameters.values(): + ... if (param.kind == param.KEYWORD_ONLY and + ... param.default is param.empty): + ... print('Parameter:', param) + Parameter: c + + .. method:: Parameter.replace(*[, name][, kind][, default][, annotation]) + + Create a new Parameter instance based on the instance replaced was invoked + on. To override a :class:`Parameter` attribute, pass the corresponding + argument. To remove a default value or/and an annotation from a + Parameter, pass :attr:`Parameter.empty`. + + :: + + >>> from funcsigs import Parameter + >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) + >>> str(param) + 'foo=42' + + >>> str(param.replace()) # Will create a shallow copy of 'param' + 'foo=42' + + >>> str(param.replace(default=Parameter.empty, annotation='spam')) + "foo:'spam'" + + +.. class:: BoundArguments + + Result of a :meth:`Signature.bind` or :meth:`Signature.bind_partial` call. + Holds the mapping of arguments to the function's parameters. + + .. attribute:: BoundArguments.arguments + + An ordered, mutable mapping (:class:`collections.OrderedDict`) of + parameters' names to arguments' values. Contains only explicitly bound + arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and + :attr:`kwargs`. + + Should be used in conjunction with :attr:`Signature.parameters` for any + argument processing purposes. + + .. note:: + + Arguments for which :meth:`Signature.bind` or + :meth:`Signature.bind_partial` relied on a default value are skipped. + However, if needed, it is easy to include them. + + :: + + >>> def foo(a, b=10): + ... pass + + >>> sig = signature(foo) + >>> ba = sig.bind(5) + + >>> ba.args, ba.kwargs + ((5,), {}) + + >>> for param in sig.parameters.values(): + ... if param.name not in ba.arguments: + ... ba.arguments[param.name] = param.default + + >>> ba.args, ba.kwargs + ((5, 10), {}) + + + .. attribute:: BoundArguments.args + + A tuple of positional arguments values. Dynamically computed from the + :attr:`arguments` attribute. + + .. attribute:: BoundArguments.kwargs + + A dict of keyword arguments values. Dynamically computed from the + :attr:`arguments` attribute. + + The :attr:`args` and :attr:`kwargs` properties can be used to invoke + functions:: + + def test(a, *, b): + ... + + sig = signature(test) + ba = sig.bind(10, b=20) + test(*ba.args, **ba.kwargs) + + +.. seealso:: + + :pep:`362` - Function Signature Object. + The detailed specification, implementation details and examples. + +Copyright +--------- + +*funcsigs* is a derived work of CPython under the terms of the `PSF License +Agreement`_. The original CPython inspect module, its unit tests and +documentation are the copyright of the Python Software Foundation. The derived +work is distributed under the `Apache License Version 2.0`_. + +.. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python +.. _Apache License Version 2.0: http://opensource.org/licenses/Apache-2.0 +.. _GitHub: https://github.com/testing-cabal/funcsigs +.. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python +.. _Travis CI: http://travis-ci.org/ +.. _Read The Docs: http://funcsigs.readthedocs.org/ +.. _PEP 362: http://www.python.org/dev/peps/pep-0362/ +.. _inspect: http://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object +.. _issues system: https://github.com/testing-cabal/funcsigs/issues + +.. |build_status| image:: https://secure.travis-ci.org/aliles/funcsigs.png?branch=master + :target: http://travis-ci.org/#!/aliles/funcsigs + :alt: Current build status + +.. |coverage| image:: https://coveralls.io/repos/aliles/funcsigs/badge.png?branch=master + :target: https://coveralls.io/r/aliles/funcsigs?branch=master + :alt: Coverage status + +.. |pypi_version| image:: https://pypip.in/v/funcsigs/badge.png + :target: https://crate.io/packages/funcsigs/ + :alt: Latest PyPI version + + diff --git a/third_party/python/funcsigs/funcsigs/__init__.py b/third_party/python/funcsigs/funcsigs/__init__.py new file mode 100644 index 000000000000..5f5378b42a6d --- /dev/null +++ b/third_party/python/funcsigs/funcsigs/__init__.py @@ -0,0 +1,829 @@ +# Copyright 2001-2013 Python Software Foundation; All Rights Reserved +"""Function signature objects for callables + +Back port of Python 3.3's function signature tools from the inspect module, +modified to be compatible with Python 2.6, 2.7 and 3.3+. +""" +from __future__ import absolute_import, division, print_function +import itertools +import functools +import re +import types + +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict + +from funcsigs.version import __version__ + +__all__ = ['BoundArguments', 'Parameter', 'Signature', 'signature'] + + +_WrapperDescriptor = type(type.__call__) +_MethodWrapper = type(all.__call__) + +_NonUserDefinedCallables = (_WrapperDescriptor, + _MethodWrapper, + types.BuiltinFunctionType) + + +def formatannotation(annotation, base_module=None): + if isinstance(annotation, type): + if annotation.__module__ in ('builtins', '__builtin__', base_module): + return annotation.__name__ + return annotation.__module__+'.'+annotation.__name__ + return repr(annotation) + + +def _get_user_defined_method(cls, method_name, *nested): + try: + if cls is type: + return + meth = getattr(cls, method_name) + for name in nested: + meth = getattr(meth, name, meth) + except AttributeError: + return + else: + if not isinstance(meth, _NonUserDefinedCallables): + # Once '__signature__' will be added to 'C'-level + # callables, this check won't be necessary + return meth + + +def signature(obj): + '''Get a signature object for the passed callable.''' + + if not callable(obj): + raise TypeError('{0!r} is not a callable object'.format(obj)) + + if isinstance(obj, types.MethodType): + sig = signature(obj.__func__) + if obj.__self__ is None: + # Unbound method - preserve as-is. + return sig + else: + # Bound method. Eat self - if we can. + params = tuple(sig.parameters.values()) + + if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY): + raise ValueError('invalid method signature') + + kind = params[0].kind + if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY): + # Drop first parameter: + # '(p1, p2[, ...])' -> '(p2[, ...])' + params = params[1:] + else: + if kind is not _VAR_POSITIONAL: + # Unless we add a new parameter type we never + # get here + raise ValueError('invalid argument type') + # It's a var-positional parameter. + # Do nothing. '(*args[, ...])' -> '(*args[, ...])' + + return sig.replace(parameters=params) + + try: + sig = obj.__signature__ + except AttributeError: + pass + else: + if sig is not None: + return sig + + try: + # Was this function wrapped by a decorator? + wrapped = obj.__wrapped__ + except AttributeError: + pass + else: + return signature(wrapped) + + if isinstance(obj, types.FunctionType): + return Signature.from_function(obj) + + if isinstance(obj, functools.partial): + sig = signature(obj.func) + + new_params = OrderedDict(sig.parameters.items()) + + partial_args = obj.args or () + partial_keywords = obj.keywords or {} + try: + ba = sig.bind_partial(*partial_args, **partial_keywords) + except TypeError as ex: + msg = 'partial object {0!r} has incorrect arguments'.format(obj) + raise ValueError(msg) + + for arg_name, arg_value in ba.arguments.items(): + param = new_params[arg_name] + if arg_name in partial_keywords: + # We set a new default value, because the following code + # is correct: + # + # >>> def foo(a): print(a) + # >>> print(partial(partial(foo, a=10), a=20)()) + # 20 + # >>> print(partial(partial(foo, a=10), a=20)(a=30)) + # 30 + # + # So, with 'partial' objects, passing a keyword argument is + # like setting a new default value for the corresponding + # parameter + # + # We also mark this parameter with '_partial_kwarg' + # flag. Later, in '_bind', the 'default' value of this + # parameter will be added to 'kwargs', to simulate + # the 'functools.partial' real call. + new_params[arg_name] = param.replace(default=arg_value, + _partial_kwarg=True) + + elif (param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL) and + not param._partial_kwarg): + new_params.pop(arg_name) + + return sig.replace(parameters=new_params.values()) + + sig = None + if isinstance(obj, type): + # obj is a class or a metaclass + + # First, let's see if it has an overloaded __call__ defined + # in its metaclass + call = _get_user_defined_method(type(obj), '__call__') + if call is not None: + sig = signature(call) + else: + # Now we check if the 'obj' class has a '__new__' method + new = _get_user_defined_method(obj, '__new__') + if new is not None: + sig = signature(new) + else: + # Finally, we should have at least __init__ implemented + init = _get_user_defined_method(obj, '__init__') + if init is not None: + sig = signature(init) + elif not isinstance(obj, _NonUserDefinedCallables): + # An object with __call__ + # We also check that the 'obj' is not an instance of + # _WrapperDescriptor or _MethodWrapper to avoid + # infinite recursion (and even potential segfault) + call = _get_user_defined_method(type(obj), '__call__', 'im_func') + if call is not None: + sig = signature(call) + + if sig is not None: + # For classes and objects we skip the first parameter of their + # __call__, __new__, or __init__ methods + return sig.replace(parameters=tuple(sig.parameters.values())[1:]) + + if isinstance(obj, types.BuiltinFunctionType): + # Raise a nicer error message for builtins + msg = 'no signature found for builtin function {0!r}'.format(obj) + raise ValueError(msg) + + raise ValueError('callable {0!r} is not supported by signature'.format(obj)) + + +class _void(object): + '''A private marker - used in Parameter & Signature''' + + +class _empty(object): + pass + + +class _ParameterKind(int): + def __new__(self, *args, **kwargs): + obj = int.__new__(self, *args) + obj._name = kwargs['name'] + return obj + + def __str__(self): + return self._name + + def __repr__(self): + return '<_ParameterKind: {0!r}>'.format(self._name) + + +_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY') +_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD') +_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL') +_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY') +_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD') + + +class Parameter(object): + '''Represents a parameter in a function signature. + + Has the following public attributes: + + * name : str + The name of the parameter as a string. + * default : object + The default value for the parameter if specified. If the + parameter has no default value, this attribute is not set. + * annotation + The annotation for the parameter if specified. If the + parameter has no annotation, this attribute is not set. + * kind : str + Describes how argument values are bound to the parameter. + Possible values: `Parameter.POSITIONAL_ONLY`, + `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`, + `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`. + ''' + + __slots__ = ('_name', '_kind', '_default', '_annotation', '_partial_kwarg') + + POSITIONAL_ONLY = _POSITIONAL_ONLY + POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD + VAR_POSITIONAL = _VAR_POSITIONAL + KEYWORD_ONLY = _KEYWORD_ONLY + VAR_KEYWORD = _VAR_KEYWORD + + empty = _empty + + def __init__(self, name, kind, default=_empty, annotation=_empty, + _partial_kwarg=False): + + if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD, + _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD): + raise ValueError("invalid value for 'Parameter.kind' attribute") + self._kind = kind + + if default is not _empty: + if kind in (_VAR_POSITIONAL, _VAR_KEYWORD): + msg = '{0} parameters cannot have default values'.format(kind) + raise ValueError(msg) + self._default = default + self._annotation = annotation + + if name is None: + if kind != _POSITIONAL_ONLY: + raise ValueError("None is not a valid name for a " + "non-positional-only parameter") + self._name = name + else: + name = str(name) + if kind != _POSITIONAL_ONLY and not re.match(r'[a-z_]\w*$', name, re.I): + msg = '{0!r} is not a valid parameter name'.format(name) + raise ValueError(msg) + self._name = name + + self._partial_kwarg = _partial_kwarg + + @property + def name(self): + return self._name + + @property + def default(self): + return self._default + + @property + def annotation(self): + return self._annotation + + @property + def kind(self): + return self._kind + + def replace(self, name=_void, kind=_void, annotation=_void, + default=_void, _partial_kwarg=_void): + '''Creates a customized copy of the Parameter.''' + + if name is _void: + name = self._name + + if kind is _void: + kind = self._kind + + if annotation is _void: + annotation = self._annotation + + if default is _void: + default = self._default + + if _partial_kwarg is _void: + _partial_kwarg = self._partial_kwarg + + return type(self)(name, kind, default=default, annotation=annotation, + _partial_kwarg=_partial_kwarg) + + def __str__(self): + kind = self.kind + + formatted = self._name + if kind == _POSITIONAL_ONLY: + if formatted is None: + formatted = '' + formatted = '<{0}>'.format(formatted) + + # Add annotation and default value + if self._annotation is not _empty: + formatted = '{0}:{1}'.format(formatted, + formatannotation(self._annotation)) + + if self._default is not _empty: + formatted = '{0}={1}'.format(formatted, repr(self._default)) + + if kind == _VAR_POSITIONAL: + formatted = '*' + formatted + elif kind == _VAR_KEYWORD: + formatted = '**' + formatted + + return formatted + + def __repr__(self): + return '<{0} at {1:#x} {2!r}>'.format(self.__class__.__name__, + id(self), self.name) + + def __hash__(self): + msg = "unhashable type: '{0}'".format(self.__class__.__name__) + raise TypeError(msg) + + def __eq__(self, other): + return (issubclass(other.__class__, Parameter) and + self._name == other._name and + self._kind == other._kind and + self._default == other._default and + self._annotation == other._annotation) + + def __ne__(self, other): + return not self.__eq__(other) + + +class BoundArguments(object): + '''Result of `Signature.bind` call. Holds the mapping of arguments + to the function's parameters. + + Has the following public attributes: + + * arguments : OrderedDict + An ordered mutable mapping of parameters' names to arguments' values. + Does not contain arguments' default values. + * signature : Signature + The Signature object that created this instance. + * args : tuple + Tuple of positional arguments values. + * kwargs : dict + Dict of keyword arguments values. + ''' + + def __init__(self, signature, arguments): + self.arguments = arguments + self._signature = signature + + @property + def signature(self): + return self._signature + + @property + def args(self): + args = [] + for param_name, param in self._signature.parameters.items(): + if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or + param._partial_kwarg): + # Keyword arguments mapped by 'functools.partial' + # (Parameter._partial_kwarg is True) are mapped + # in 'BoundArguments.kwargs', along with VAR_KEYWORD & + # KEYWORD_ONLY + break + + try: + arg = self.arguments[param_name] + except KeyError: + # We're done here. Other arguments + # will be mapped in 'BoundArguments.kwargs' + break + else: + if param.kind == _VAR_POSITIONAL: + # *args + args.extend(arg) + else: + # plain argument + args.append(arg) + + return tuple(args) + + @property + def kwargs(self): + kwargs = {} + kwargs_started = False + for param_name, param in self._signature.parameters.items(): + if not kwargs_started: + if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or + param._partial_kwarg): + kwargs_started = True + else: + if param_name not in self.arguments: + kwargs_started = True + continue + + if not kwargs_started: + continue + + try: + arg = self.arguments[param_name] + except KeyError: + pass + else: + if param.kind == _VAR_KEYWORD: + # **kwargs + kwargs.update(arg) + else: + # plain keyword argument + kwargs[param_name] = arg + + return kwargs + + def __hash__(self): + msg = "unhashable type: '{0}'".format(self.__class__.__name__) + raise TypeError(msg) + + def __eq__(self, other): + return (issubclass(other.__class__, BoundArguments) and + self.signature == other.signature and + self.arguments == other.arguments) + + def __ne__(self, other): + return not self.__eq__(other) + + +class Signature(object): + '''A Signature object represents the overall signature of a function. + It stores a Parameter object for each parameter accepted by the + function, as well as information specific to the function itself. + + A Signature object has the following public attributes and methods: + + * parameters : OrderedDict + An ordered mapping of parameters' names to the corresponding + Parameter objects (keyword-only arguments are in the same order + as listed in `code.co_varnames`). + * return_annotation : object + The annotation for the return type of the function if specified. + If the function has no annotation for its return type, this + attribute is not set. + * bind(*args, **kwargs) -> BoundArguments + Creates a mapping from positional and keyword arguments to + parameters. + * bind_partial(*args, **kwargs) -> BoundArguments + Creates a partial mapping from positional and keyword arguments + to parameters (simulating 'functools.partial' behavior.) + ''' + + __slots__ = ('_return_annotation', '_parameters') + + _parameter_cls = Parameter + _bound_arguments_cls = BoundArguments + + empty = _empty + + def __init__(self, parameters=None, return_annotation=_empty, + __validate_parameters__=True): + '''Constructs Signature from the given list of Parameter + objects and 'return_annotation'. All arguments are optional. + ''' + + if parameters is None: + params = OrderedDict() + else: + if __validate_parameters__: + params = OrderedDict() + top_kind = _POSITIONAL_ONLY + + for idx, param in enumerate(parameters): + kind = param.kind + if kind < top_kind: + msg = 'wrong parameter order: {0} before {1}' + msg = msg.format(top_kind, param.kind) + raise ValueError(msg) + else: + top_kind = kind + + name = param.name + if name is None: + name = str(idx) + param = param.replace(name=name) + + if name in params: + msg = 'duplicate parameter name: {0!r}'.format(name) + raise ValueError(msg) + params[name] = param + else: + params = OrderedDict(((param.name, param) + for param in parameters)) + + self._parameters = params + self._return_annotation = return_annotation + + @classmethod + def from_function(cls, func): + '''Constructs Signature for the given python function''' + + if not isinstance(func, types.FunctionType): + raise TypeError('{0!r} is not a Python function'.format(func)) + + Parameter = cls._parameter_cls + + # Parameter information. + func_code = func.__code__ + pos_count = func_code.co_argcount + arg_names = func_code.co_varnames + positional = tuple(arg_names[:pos_count]) + keyword_only_count = getattr(func_code, 'co_kwonlyargcount', 0) + keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)] + annotations = getattr(func, '__annotations__', {}) + defaults = func.__defaults__ + kwdefaults = getattr(func, '__kwdefaults__', None) + + if defaults: + pos_default_count = len(defaults) + else: + pos_default_count = 0 + + parameters = [] + + # Non-keyword-only parameters w/o defaults. + non_default_count = pos_count - pos_default_count + for name in positional[:non_default_count]: + annotation = annotations.get(name, _empty) + parameters.append(Parameter(name, annotation=annotation, + kind=_POSITIONAL_OR_KEYWORD)) + + # ... w/ defaults. + for offset, name in enumerate(positional[non_default_count:]): + annotation = annotations.get(name, _empty) + parameters.append(Parameter(name, annotation=annotation, + kind=_POSITIONAL_OR_KEYWORD, + default=defaults[offset])) + + # *args + if func_code.co_flags & 0x04: + name = arg_names[pos_count + keyword_only_count] + annotation = annotations.get(name, _empty) + parameters.append(Parameter(name, annotation=annotation, + kind=_VAR_POSITIONAL)) + + # Keyword-only parameters. + for name in keyword_only: + default = _empty + if kwdefaults is not None: + default = kwdefaults.get(name, _empty) + + annotation = annotations.get(name, _empty) + parameters.append(Parameter(name, annotation=annotation, + kind=_KEYWORD_ONLY, + default=default)) + # **kwargs + if func_code.co_flags & 0x08: + index = pos_count + keyword_only_count + if func_code.co_flags & 0x04: + index += 1 + + name = arg_names[index] + annotation = annotations.get(name, _empty) + parameters.append(Parameter(name, annotation=annotation, + kind=_VAR_KEYWORD)) + + return cls(parameters, + return_annotation=annotations.get('return', _empty), + __validate_parameters__=False) + + @property + def parameters(self): + try: + return types.MappingProxyType(self._parameters) + except AttributeError: + return OrderedDict(self._parameters.items()) + + @property + def return_annotation(self): + return self._return_annotation + + def replace(self, parameters=_void, return_annotation=_void): + '''Creates a customized copy of the Signature. + Pass 'parameters' and/or 'return_annotation' arguments + to override them in the new copy. + ''' + + if parameters is _void: + parameters = self.parameters.values() + + if return_annotation is _void: + return_annotation = self._return_annotation + + return type(self)(parameters, + return_annotation=return_annotation) + + def __hash__(self): + msg = "unhashable type: '{0}'".format(self.__class__.__name__) + raise TypeError(msg) + + def __eq__(self, other): + if (not issubclass(type(other), Signature) or + self.return_annotation != other.return_annotation or + len(self.parameters) != len(other.parameters)): + return False + + other_positions = dict((param, idx) + for idx, param in enumerate(other.parameters.keys())) + + for idx, (param_name, param) in enumerate(self.parameters.items()): + if param.kind == _KEYWORD_ONLY: + try: + other_param = other.parameters[param_name] + except KeyError: + return False + else: + if param != other_param: + return False + else: + try: + other_idx = other_positions[param_name] + except KeyError: + return False + else: + if (idx != other_idx or + param != other.parameters[param_name]): + return False + + return True + + def __ne__(self, other): + return not self.__eq__(other) + + def _bind(self, args, kwargs, partial=False): + '''Private method. Don't use directly.''' + + arguments = OrderedDict() + + parameters = iter(self.parameters.values()) + parameters_ex = () + arg_vals = iter(args) + + if partial: + # Support for binding arguments to 'functools.partial' objects. + # See 'functools.partial' case in 'signature()' implementation + # for details. + for param_name, param in self.parameters.items(): + if (param._partial_kwarg and param_name not in kwargs): + # Simulating 'functools.partial' behavior + kwargs[param_name] = param.default + + while True: + # Let's iterate through the positional arguments and corresponding + # parameters + try: + arg_val = next(arg_vals) + except StopIteration: + # No more positional arguments + try: + param = next(parameters) + except StopIteration: + # No more parameters. That's it. Just need to check that + # we have no `kwargs` after this while loop + break + else: + if param.kind == _VAR_POSITIONAL: + # That's OK, just empty *args. Let's start parsing + # kwargs + break + elif param.name in kwargs: + if param.kind == _POSITIONAL_ONLY: + msg = '{arg!r} parameter is positional only, ' \ + 'but was passed as a keyword' + msg = msg.format(arg=param.name) + raise TypeError(msg) + parameters_ex = (param,) + break + elif (param.kind == _VAR_KEYWORD or + param.default is not _empty): + # That's fine too - we have a default value for this + # parameter. So, lets start parsing `kwargs`, starting + # with the current parameter + parameters_ex = (param,) + break + else: + if partial: + parameters_ex = (param,) + break + else: + msg = '{arg!r} parameter lacking default value' + msg = msg.format(arg=param.name) + raise TypeError(msg) + else: + # We have a positional argument to process + try: + param = next(parameters) + except StopIteration: + raise TypeError('too many positional arguments') + else: + if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY): + # Looks like we have no parameter for this positional + # argument + raise TypeError('too many positional arguments') + + if param.kind == _VAR_POSITIONAL: + # We have an '*args'-like argument, let's fill it with + # all positional arguments we have left and move on to + # the next phase + values = [arg_val] + values.extend(arg_vals) + arguments[param.name] = tuple(values) + break + + if param.name in kwargs: + raise TypeError('multiple values for argument ' + '{arg!r}'.format(arg=param.name)) + + arguments[param.name] = arg_val + + # Now, we iterate through the remaining parameters to process + # keyword arguments + kwargs_param = None + for param in itertools.chain(parameters_ex, parameters): + if param.kind == _POSITIONAL_ONLY: + # This should never happen in case of a properly built + # Signature object (but let's have this check here + # to ensure correct behaviour just in case) + raise TypeError('{arg!r} parameter is positional only, ' + 'but was passed as a keyword'. \ + format(arg=param.name)) + + if param.kind == _VAR_KEYWORD: + # Memorize that we have a '**kwargs'-like parameter + kwargs_param = param + continue + + param_name = param.name + try: + arg_val = kwargs.pop(param_name) + except KeyError: + # We have no value for this parameter. It's fine though, + # if it has a default value, or it is an '*args'-like + # parameter, left alone by the processing of positional + # arguments. + if (not partial and param.kind != _VAR_POSITIONAL and + param.default is _empty): + raise TypeError('{arg!r} parameter lacking default value'. \ + format(arg=param_name)) + + else: + arguments[param_name] = arg_val + + if kwargs: + if kwargs_param is not None: + # Process our '**kwargs'-like parameter + arguments[kwargs_param.name] = kwargs + else: + raise TypeError('too many keyword arguments %r' % kwargs) + + return self._bound_arguments_cls(self, arguments) + + def bind(*args, **kwargs): + '''Get a BoundArguments object, that maps the passed `args` + and `kwargs` to the function's signature. Raises `TypeError` + if the passed arguments can not be bound. + ''' + return args[0]._bind(args[1:], kwargs) + + def bind_partial(self, *args, **kwargs): + '''Get a BoundArguments object, that partially maps the + passed `args` and `kwargs` to the function's signature. + Raises `TypeError` if the passed arguments can not be bound. + ''' + return self._bind(args, kwargs, partial=True) + + def __str__(self): + result = [] + render_kw_only_separator = True + for idx, param in enumerate(self.parameters.values()): + formatted = str(param) + + kind = param.kind + if kind == _VAR_POSITIONAL: + # OK, we have an '*args'-like parameter, so we won't need + # a '*' to separate keyword-only arguments + render_kw_only_separator = False + elif kind == _KEYWORD_ONLY and render_kw_only_separator: + # We have a keyword-only parameter to render and we haven't + # rendered an '*args'-like parameter before, so add a '*' + # separator to the parameters list ("foo(arg1, *, arg2)" case) + result.append('*') + # This condition should be only triggered once, so + # reset the flag + render_kw_only_separator = False + + result.append(formatted) + + rendered = '({0})'.format(', '.join(result)) + + if self.return_annotation is not _empty: + anno = formatannotation(self.return_annotation) + rendered += ' -> {0}'.format(anno) + + return rendered diff --git a/third_party/python/funcsigs/funcsigs/version.py b/third_party/python/funcsigs/funcsigs/version.py new file mode 100644 index 000000000000..7863915fa5f8 --- /dev/null +++ b/third_party/python/funcsigs/funcsigs/version.py @@ -0,0 +1 @@ +__version__ = "1.0.2" diff --git a/third_party/python/funcsigs/setup.cfg b/third_party/python/funcsigs/setup.cfg new file mode 100644 index 000000000000..6c71b612d8d5 --- /dev/null +++ b/third_party/python/funcsigs/setup.cfg @@ -0,0 +1,8 @@ +[wheel] +universal = 1 + +[egg_info] +tag_build = +tag_date = 0 +tag_svn_revision = 0 + diff --git a/third_party/python/funcsigs/setup.py b/third_party/python/funcsigs/setup.py new file mode 100644 index 000000000000..f3696888f9eb --- /dev/null +++ b/third_party/python/funcsigs/setup.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +from setuptools import setup +import re +import sys + +def load_version(filename='funcsigs/version.py'): + "Parse a __version__ number from a source file" + with open(filename) as source: + text = source.read() + match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", text) + if not match: + msg = "Unable to find version number in {}".format(filename) + raise RuntimeError(msg) + version = match.group(1) + return version + + +setup( + name="funcsigs", + version=load_version(), + packages=['funcsigs'], + zip_safe=False, + author="Testing Cabal", + author_email="testing-in-python@lists.idyll.org", + url="http://funcsigs.readthedocs.org", + description="Python function signatures from PEP362 for Python 2.6, 2.7 and 3.2+", + long_description=open('README.rst').read(), + license="ASL", + extras_require = { + ':python_version<"2.7"': ['ordereddict'], + }, + setup_requires = ["setuptools>=17.1"], + classifiers = [ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache Software License', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', + 'Topic :: Software Development :: Libraries :: Python Modules' + ], + tests_require = ['unittest2'], + test_suite = 'unittest2.collector', +) diff --git a/third_party/python/pytest/_pytest/vendored_packages/__init__.py b/third_party/python/funcsigs/tests/__init__.py similarity index 100% rename from third_party/python/pytest/_pytest/vendored_packages/__init__.py rename to third_party/python/funcsigs/tests/__init__.py diff --git a/third_party/python/funcsigs/tests/test_formatannotation.py b/third_party/python/funcsigs/tests/test_formatannotation.py new file mode 100644 index 000000000000..4b98e6037d83 --- /dev/null +++ b/third_party/python/funcsigs/tests/test_formatannotation.py @@ -0,0 +1,17 @@ +import funcsigs + +import unittest2 as unittest + +class TestFormatAnnotation(unittest.TestCase): + def test_string (self): + self.assertEqual(funcsigs.formatannotation("annotation"), + "'annotation'") + + def test_builtin_type (self): + self.assertEqual(funcsigs.formatannotation(int), + "int") + + def test_user_type (self): + class dummy (object): pass + self.assertEqual(funcsigs.formatannotation(dummy), + "tests.test_formatannotation.dummy") diff --git a/third_party/python/funcsigs/tests/test_funcsigs.py b/third_party/python/funcsigs/tests/test_funcsigs.py new file mode 100644 index 000000000000..a7b9cca7679f --- /dev/null +++ b/third_party/python/funcsigs/tests/test_funcsigs.py @@ -0,0 +1,91 @@ +import unittest2 as unittest + +import doctest +import sys + +import funcsigs as inspect + + +class TestFunctionSignatures(unittest.TestCase): + + @staticmethod + def signature(func): + sig = inspect.signature(func) + return (tuple((param.name, + (Ellipsis if param.default is param.empty else param.default), + (Ellipsis if param.annotation is param.empty + else param.annotation), + str(param.kind).lower()) + for param in sig.parameters.values()), + (Ellipsis if sig.return_annotation is sig.empty + else sig.return_annotation)) + + def test_zero_arguments(self): + def test(): + pass + self.assertEqual(self.signature(test), + ((), Ellipsis)) + + def test_single_positional_argument(self): + def test(a): + pass + self.assertEqual(self.signature(test), + (((('a', Ellipsis, Ellipsis, "positional_or_keyword")),), Ellipsis)) + + def test_single_keyword_argument(self): + def test(a=None): + pass + self.assertEqual(self.signature(test), + (((('a', None, Ellipsis, "positional_or_keyword")),), Ellipsis)) + + def test_var_args(self): + def test(*args): + pass + self.assertEqual(self.signature(test), + (((('args', Ellipsis, Ellipsis, "var_positional")),), Ellipsis)) + + def test_keywords_args(self): + def test(**kwargs): + pass + self.assertEqual(self.signature(test), + (((('kwargs', Ellipsis, Ellipsis, "var_keyword")),), Ellipsis)) + + def test_multiple_arguments(self): + def test(a, b=None, *args, **kwargs): + pass + self.assertEqual(self.signature(test), (( + ('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', None, Ellipsis, "positional_or_keyword"), + ('args', Ellipsis, Ellipsis, "var_positional"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword"), + ), Ellipsis)) + + def test_has_version(self): + self.assertTrue(inspect.__version__) + + def test_readme(self): + # XXX: This fails but doesn't fail the build. + # (and the syntax isn't valid on all pythons so that seems a little + # hard to get right. + doctest.testfile('../README.rst') + + def test_unbound_method(self): + self_kind = "positional_or_keyword" + class Test(object): + def method(self): + pass + def method_with_args(self, a): + pass + def method_with_varargs(*args): + pass + self.assertEqual( + self.signature(Test.method), + (((('self', Ellipsis, Ellipsis, self_kind)),), Ellipsis)) + self.assertEqual( + self.signature(Test.method_with_args), + ((('self', Ellipsis, Ellipsis, self_kind), + ('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ), Ellipsis)) + self.assertEqual( + self.signature(Test.method_with_varargs), + ((('args', Ellipsis, Ellipsis, "var_positional"),), Ellipsis)) diff --git a/third_party/python/funcsigs/tests/test_inspect.py b/third_party/python/funcsigs/tests/test_inspect.py new file mode 100644 index 000000000000..98d6592fcc72 --- /dev/null +++ b/third_party/python/funcsigs/tests/test_inspect.py @@ -0,0 +1,1002 @@ +# Copyright 2001-2013 Python Software Foundation; All Rights Reserved +from __future__ import absolute_import, division, print_function +import collections +import functools +import sys + +import unittest2 as unittest + +import funcsigs as inspect + + +class TestSignatureObject(unittest.TestCase): + @staticmethod + def signature(func): + sig = inspect.signature(func) + return (tuple((param.name, + (Ellipsis if param.default is param.empty else param.default), + (Ellipsis if param.annotation is param.empty + else param.annotation), + str(param.kind).lower()) + for param in sig.parameters.values()), + (Ellipsis if sig.return_annotation is sig.empty + else sig.return_annotation)) + + if sys.version_info[0] > 2: + exec(""" +def test_signature_object(self): + S = inspect.Signature + P = inspect.Parameter + + self.assertEqual(str(S()), '()') + + def test(po, pk, *args, ko, **kwargs): + pass + sig = inspect.signature(test) + po = sig.parameters['po'].replace(kind=P.POSITIONAL_ONLY) + pk = sig.parameters['pk'] + args = sig.parameters['args'] + ko = sig.parameters['ko'] + kwargs = sig.parameters['kwargs'] + + S((po, pk, args, ko, kwargs)) + + with self.assertRaisesRegex(ValueError, 'wrong parameter order'): + S((pk, po, args, ko, kwargs)) + + with self.assertRaisesRegex(ValueError, 'wrong parameter order'): + S((po, args, pk, ko, kwargs)) + + with self.assertRaisesRegex(ValueError, 'wrong parameter order'): + S((args, po, pk, ko, kwargs)) + + with self.assertRaisesRegex(ValueError, 'wrong parameter order'): + S((po, pk, args, kwargs, ko)) + + kwargs2 = kwargs.replace(name='args') + with self.assertRaisesRegex(ValueError, 'duplicate parameter name'): + S((po, pk, args, kwargs2, ko)) +""") + + def test_signature_immutability(self): + def test(a): + pass + sig = inspect.signature(test) + + with self.assertRaises(AttributeError): + sig.foo = 'bar' + + # Python2 does not have MappingProxyType class + if sys.version_info[:2] < (3, 3): + return + + with self.assertRaises(TypeError): + sig.parameters['a'] = None + + def test_signature_on_noarg(self): + def test(): + pass + self.assertEqual(self.signature(test), ((), Ellipsis)) + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_wargs(self): + def test(a, b:'foo') -> 123: + pass + self.assertEqual(self.signature(test), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', Ellipsis, 'foo', "positional_or_keyword")), + 123)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_wkwonly(self): + def test(*, a:float, b:str) -> int: + pass + self.assertEqual(self.signature(test), + ((('a', Ellipsis, float, "keyword_only"), + ('b', Ellipsis, str, "keyword_only")), + int)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_complex_args(self): + def test(a, b:'foo'=10, *args:'bar', spam:'baz', ham=123, **kwargs:int): + pass + self.assertEqual(self.signature(test), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', 10, 'foo', "positional_or_keyword"), + ('args', Ellipsis, 'bar', "var_positional"), + ('spam', Ellipsis, 'baz', "keyword_only"), + ('ham', 123, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, int, "var_keyword")), + Ellipsis)) +""") + + def test_signature_on_builtin_function(self): + with self.assertRaisesRegex(ValueError, 'not supported by signature'): + inspect.signature(type) + with self.assertRaisesRegex(ValueError, 'not supported by signature'): + # support for 'wrapper_descriptor' + inspect.signature(type.__call__) + if hasattr(sys, 'pypy_version_info'): + raise ValueError('not supported by signature') + with self.assertRaisesRegex(ValueError, 'not supported by signature'): + # support for 'method-wrapper' + inspect.signature(min.__call__) + if hasattr(sys, 'pypy_version_info'): + raise ValueError('not supported by signature') + with self.assertRaisesRegex(ValueError, + 'no signature found for builtin function'): + # support for 'method-wrapper' + inspect.signature(min) + + def test_signature_on_non_function(self): + with self.assertRaisesRegex(TypeError, 'is not a callable object'): + inspect.signature(42) + + with self.assertRaisesRegex(TypeError, 'is not a Python function'): + inspect.Signature.from_function(42) + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_method(self): + class Test: + def foo(self, arg1, arg2=1) -> int: + pass + + meth = Test().foo + + self.assertEqual(self.signature(meth), + ((('arg1', Ellipsis, Ellipsis, "positional_or_keyword"), + ('arg2', 1, Ellipsis, "positional_or_keyword")), + int)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_classmethod(self): + class Test: + @classmethod + def foo(cls, arg1, *, arg2=1): + pass + + meth = Test().foo + self.assertEqual(self.signature(meth), + ((('arg1', Ellipsis, Ellipsis, "positional_or_keyword"), + ('arg2', 1, Ellipsis, "keyword_only")), + Ellipsis)) + + meth = Test.foo + self.assertEqual(self.signature(meth), + ((('arg1', Ellipsis, Ellipsis, "positional_or_keyword"), + ('arg2', 1, Ellipsis, "keyword_only")), + Ellipsis)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_staticmethod(self): + class Test: + @staticmethod + def foo(cls, *, arg): + pass + + meth = Test().foo + self.assertEqual(self.signature(meth), + ((('cls', Ellipsis, Ellipsis, "positional_or_keyword"), + ('arg', Ellipsis, Ellipsis, "keyword_only")), + Ellipsis)) + + meth = Test.foo + self.assertEqual(self.signature(meth), + ((('cls', Ellipsis, Ellipsis, "positional_or_keyword"), + ('arg', Ellipsis, Ellipsis, "keyword_only")), + Ellipsis)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_partial(self): + from functools import partial + + def test(): + pass + + self.assertEqual(self.signature(partial(test)), ((), Ellipsis)) + + with self.assertRaisesRegex(ValueError, "has incorrect arguments"): + inspect.signature(partial(test, 1)) + + with self.assertRaisesRegex(ValueError, "has incorrect arguments"): + inspect.signature(partial(test, a=1)) + + def test(a, b, *, c, d): + pass + + self.assertEqual(self.signature(partial(test)), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', Ellipsis, Ellipsis, "positional_or_keyword"), + ('c', Ellipsis, Ellipsis, "keyword_only"), + ('d', Ellipsis, Ellipsis, "keyword_only")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, 1)), + ((('b', Ellipsis, Ellipsis, "positional_or_keyword"), + ('c', Ellipsis, Ellipsis, "keyword_only"), + ('d', Ellipsis, Ellipsis, "keyword_only")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, 1, c=2)), + ((('b', Ellipsis, Ellipsis, "positional_or_keyword"), + ('c', 2, Ellipsis, "keyword_only"), + ('d', Ellipsis, Ellipsis, "keyword_only")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, b=1, c=2)), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', 1, Ellipsis, "positional_or_keyword"), + ('c', 2, Ellipsis, "keyword_only"), + ('d', Ellipsis, Ellipsis, "keyword_only")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, 0, b=1, c=2)), + ((('b', 1, Ellipsis, "positional_or_keyword"), + ('c', 2, Ellipsis, "keyword_only"), + ('d', Ellipsis, Ellipsis, "keyword_only"),), + Ellipsis)) + + def test(a, *args, b, **kwargs): + pass + + self.assertEqual(self.signature(partial(test, 1)), + ((('args', Ellipsis, Ellipsis, "var_positional"), + ('b', Ellipsis, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, 1, 2, 3)), + ((('args', Ellipsis, Ellipsis, "var_positional"), + ('b', Ellipsis, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword")), + Ellipsis)) + + + self.assertEqual(self.signature(partial(test, 1, 2, 3, test=True)), + ((('args', Ellipsis, Ellipsis, "var_positional"), + ('b', Ellipsis, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, 1, 2, 3, test=1, b=0)), + ((('args', Ellipsis, Ellipsis, "var_positional"), + ('b', 0, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, b=0)), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('args', Ellipsis, Ellipsis, "var_positional"), + ('b', 0, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(partial(test, b=0, test=1)), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('args', Ellipsis, Ellipsis, "var_positional"), + ('b', 0, Ellipsis, "keyword_only"), + ('kwargs', Ellipsis, Ellipsis, "var_keyword")), + Ellipsis)) + + def test(a, b, c:int) -> 42: + pass + + sig = test.__signature__ = inspect.signature(test) + + self.assertEqual(self.signature(partial(partial(test, 1))), + ((('b', Ellipsis, Ellipsis, "positional_or_keyword"), + ('c', Ellipsis, int, "positional_or_keyword")), + 42)) + + self.assertEqual(self.signature(partial(partial(test, 1), 2)), + ((('c', Ellipsis, int, "positional_or_keyword"),), + 42)) + + psig = inspect.signature(partial(partial(test, 1), 2)) + + def foo(a): + return a + _foo = partial(partial(foo, a=10), a=20) + self.assertEqual(self.signature(_foo), + ((('a', 20, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + # check that we don't have any side-effects in signature(), + # and the partial object is still functioning + self.assertEqual(_foo(), 20) + + def foo(a, b, c): + return a, b, c + _foo = partial(partial(foo, 1, b=20), b=30) + self.assertEqual(self.signature(_foo), + ((('b', 30, Ellipsis, "positional_or_keyword"), + ('c', Ellipsis, Ellipsis, "positional_or_keyword")), + Ellipsis)) + self.assertEqual(_foo(c=10), (1, 30, 10)) + _foo = partial(_foo, 2) # now 'b' has two values - + # positional and keyword + with self.assertRaisesRegex(ValueError, "has incorrect arguments"): + inspect.signature(_foo) + + def foo(a, b, c, *, d): + return a, b, c, d + _foo = partial(partial(foo, d=20, c=20), b=10, d=30) + self.assertEqual(self.signature(_foo), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', 10, Ellipsis, "positional_or_keyword"), + ('c', 20, Ellipsis, "positional_or_keyword"), + ('d', 30, Ellipsis, "keyword_only")), + Ellipsis)) + ba = inspect.signature(_foo).bind(a=200, b=11) + self.assertEqual(_foo(*ba.args, **ba.kwargs), (200, 11, 20, 30)) + + def foo(a=1, b=2, c=3): + return a, b, c + _foo = partial(foo, a=10, c=13) + ba = inspect.signature(_foo).bind(11) + self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 2, 13)) + ba = inspect.signature(_foo).bind(11, 12) + self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 12, 13)) + ba = inspect.signature(_foo).bind(11, b=12) + self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 12, 13)) + ba = inspect.signature(_foo).bind(b=12) + self.assertEqual(_foo(*ba.args, **ba.kwargs), (10, 12, 13)) + _foo = partial(_foo, b=10) + ba = inspect.signature(_foo).bind(12, 14) + self.assertEqual(_foo(*ba.args, **ba.kwargs), (12, 14, 13)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_decorated(self): + import functools + + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs) -> int: + return func(*args, **kwargs) + return wrapper + + class Foo: + @decorator + def bar(self, a, b): + pass + + self.assertEqual(self.signature(Foo.bar), + ((('self', Ellipsis, Ellipsis, "positional_or_keyword"), + ('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', Ellipsis, Ellipsis, "positional_or_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(Foo().bar), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', Ellipsis, Ellipsis, "positional_or_keyword")), + Ellipsis)) + + # Test that we handle method wrappers correctly + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs) -> int: + return func(42, *args, **kwargs) + sig = inspect.signature(func) + new_params = tuple(sig.parameters.values())[1:] + wrapper.__signature__ = sig.replace(parameters=new_params) + return wrapper + + class Foo: + @decorator + def __call__(self, a, b): + pass + + self.assertEqual(self.signature(Foo.__call__), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"), + ('b', Ellipsis, Ellipsis, "positional_or_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(Foo().__call__), + ((('b', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_on_class(self): + class C: + def __init__(self, a): + pass + + self.assertEqual(self.signature(C), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + class CM(type): + def __call__(cls, a): + pass + class C(metaclass=CM): + def __init__(self, b): + pass + + self.assertEqual(self.signature(C), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + class CM(type): + def __new__(mcls, name, bases, dct, *, foo=1): + return super().__new__(mcls, name, bases, dct) + class C(metaclass=CM): + def __init__(self, b): + pass + + self.assertEqual(self.signature(C), + ((('b', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + self.assertEqual(self.signature(CM), + ((('name', Ellipsis, Ellipsis, "positional_or_keyword"), + ('bases', Ellipsis, Ellipsis, "positional_or_keyword"), + ('dct', Ellipsis, Ellipsis, "positional_or_keyword"), + ('foo', 1, Ellipsis, "keyword_only")), + Ellipsis)) + + class CMM(type): + def __new__(mcls, name, bases, dct, *, foo=1): + return super().__new__(mcls, name, bases, dct) + def __call__(cls, nm, bs, dt): + return type(nm, bs, dt) + class CM(type, metaclass=CMM): + def __new__(mcls, name, bases, dct, *, bar=2): + return super().__new__(mcls, name, bases, dct) + class C(metaclass=CM): + def __init__(self, b): + pass + + self.assertEqual(self.signature(CMM), + ((('name', Ellipsis, Ellipsis, "positional_or_keyword"), + ('bases', Ellipsis, Ellipsis, "positional_or_keyword"), + ('dct', Ellipsis, Ellipsis, "positional_or_keyword"), + ('foo', 1, Ellipsis, "keyword_only")), + Ellipsis)) + + self.assertEqual(self.signature(CM), + ((('nm', Ellipsis, Ellipsis, "positional_or_keyword"), + ('bs', Ellipsis, Ellipsis, "positional_or_keyword"), + ('dt', Ellipsis, Ellipsis, "positional_or_keyword")), + Ellipsis)) + + self.assertEqual(self.signature(C), + ((('b', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + class CM(type): + def __init__(cls, name, bases, dct, *, bar=2): + return super().__init__(name, bases, dct) + class C(metaclass=CM): + def __init__(self, b): + pass + + self.assertEqual(self.signature(CM), + ((('name', Ellipsis, Ellipsis, "positional_or_keyword"), + ('bases', Ellipsis, Ellipsis, "positional_or_keyword"), + ('dct', Ellipsis, Ellipsis, "positional_or_keyword"), + ('bar', 2, Ellipsis, "keyword_only")), + Ellipsis)) +""") + + def test_signature_on_callable_objects(self): + class Foo(object): + def __call__(self, a): + pass + + self.assertEqual(self.signature(Foo()), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + class Spam(object): + pass + with self.assertRaisesRegex(TypeError, "is not a callable object"): + inspect.signature(Spam()) + + class Bar(Spam, Foo): + pass + + self.assertEqual(self.signature(Bar()), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + class ToFail(object): + __call__ = type + with self.assertRaisesRegex(ValueError, "not supported by signature"): + inspect.signature(ToFail()) + + if sys.version_info[0] < 3: + return + + class Wrapped(object): + pass + Wrapped.__wrapped__ = lambda a: None + self.assertEqual(self.signature(Wrapped), + ((('a', Ellipsis, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + def test_signature_on_lambdas(self): + self.assertEqual(self.signature((lambda a=10: a)), + ((('a', 10, Ellipsis, "positional_or_keyword"),), + Ellipsis)) + + if sys.version_info[0] > 2: + exec(""" +def test_signature_equality(self): + def foo(a, *, b:int) -> float: pass + self.assertNotEqual(inspect.signature(foo), 42) + + def bar(a, *, b:int) -> float: pass + self.assertEqual(inspect.signature(foo), inspect.signature(bar)) + + def bar(a, *, b:int) -> int: pass + self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) + + def bar(a, *, b:int): pass + self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) + + def bar(a, *, b:int=42) -> float: pass + self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) + + def bar(a, *, c) -> float: pass + self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) + + def bar(a, b:int) -> float: pass + self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) + def spam(b:int, a) -> float: pass + self.assertNotEqual(inspect.signature(spam), inspect.signature(bar)) + + def foo(*, a, b, c): pass + def bar(*, c, b, a): pass + self.assertEqual(inspect.signature(foo), inspect.signature(bar)) + + def foo(*, a=1, b, c): pass + def bar(*, c, b, a=1): pass + self.assertEqual(inspect.signature(foo), inspect.signature(bar)) + + def foo(pos, *, a=1, b, c): pass + def bar(pos, *, c, b, a=1): pass + self.assertEqual(inspect.signature(foo), inspect.signature(bar)) + + def foo(pos, *, a, b, c): pass + def bar(pos, *, c, b, a=1): pass + self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) + + def foo(pos, *args, a=42, b, c, **kwargs:int): pass + def bar(pos, *args, c, b, a=42, **kwargs:int): pass + self.assertEqual(inspect.signature(foo), inspect.signature(bar)) +""") + + def test_signature_unhashable(self): + def foo(a): pass + sig = inspect.signature(foo) + with self.assertRaisesRegex(TypeError, 'unhashable type'): + hash(sig) + + + if sys.version_info[0] > 2: + exec(""" +def test_signature_str(self): + def foo(a:int=1, *, b, c=None, **kwargs) -> 42: + pass + self.assertEqual(str(inspect.signature(foo)), + '(a:int=1, *, b, c=None, **kwargs) -> 42') + + def foo(a:int=1, *args, b, c=None, **kwargs) -> 42: + pass + self.assertEqual(str(inspect.signature(foo)), + '(a:int=1, *args, b, c=None, **kwargs) -> 42') + + def foo(): + pass + self.assertEqual(str(inspect.signature(foo)), '()') +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_str_positional_only(self): + P = inspect.Parameter + + def test(a_po, *, b, **kwargs): + return a_po, kwargs + + sig = inspect.signature(test) + new_params = list(sig.parameters.values()) + new_params[0] = new_params[0].replace(kind=P.POSITIONAL_ONLY) + test.__signature__ = sig.replace(parameters=new_params) + + self.assertEqual(str(inspect.signature(test)), + '(, *, b, **kwargs)') + + sig = inspect.signature(test) + new_params = list(sig.parameters.values()) + new_params[0] = new_params[0].replace(name=None) + test.__signature__ = sig.replace(parameters=new_params) + self.assertEqual(str(inspect.signature(test)), + '(<0>, *, b, **kwargs)') +""") + + if sys.version_info[0] > 2: + exec(""" +def test_signature_replace_anno(self): + def test() -> 42: + pass + + sig = inspect.signature(test) + sig = sig.replace(return_annotation=None) + self.assertIs(sig.return_annotation, None) + sig = sig.replace(return_annotation=sig.empty) + self.assertIs(sig.return_annotation, sig.empty) + sig = sig.replace(return_annotation=42) + self.assertEqual(sig.return_annotation, 42) + self.assertEqual(sig, inspect.signature(test)) +""") + + +class TestParameterObject(unittest.TestCase): + + def test_signature_parameter_kinds(self): + P = inspect.Parameter + self.assertTrue(P.POSITIONAL_ONLY < P.POSITIONAL_OR_KEYWORD < \ + P.VAR_POSITIONAL < P.KEYWORD_ONLY < P.VAR_KEYWORD) + + self.assertEqual(str(P.POSITIONAL_ONLY), 'POSITIONAL_ONLY') + self.assertTrue('POSITIONAL_ONLY' in repr(P.POSITIONAL_ONLY)) + + def test_signature_parameter_object(self): + p = inspect.Parameter('foo', default=10, + kind=inspect.Parameter.POSITIONAL_ONLY) + self.assertEqual(p.name, 'foo') + self.assertEqual(p.default, 10) + self.assertIs(p.annotation, p.empty) + self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) + + with self.assertRaisesRegex(ValueError, 'invalid value'): + inspect.Parameter('foo', default=10, kind='123') + + with self.assertRaisesRegex(ValueError, 'not a valid parameter name'): + inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD) + + with self.assertRaisesRegex(ValueError, + 'non-positional-only parameter'): + inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD) + + with self.assertRaisesRegex(ValueError, 'cannot have default values'): + inspect.Parameter('a', default=42, + kind=inspect.Parameter.VAR_KEYWORD) + + with self.assertRaisesRegex(ValueError, 'cannot have default values'): + inspect.Parameter('a', default=42, + kind=inspect.Parameter.VAR_POSITIONAL) + + p = inspect.Parameter('a', default=42, + kind=inspect.Parameter.POSITIONAL_OR_KEYWORD) + with self.assertRaisesRegex(ValueError, 'cannot have default values'): + p.replace(kind=inspect.Parameter.VAR_POSITIONAL) + + self.assertTrue(repr(p).startswith('') + + p = p.replace(name='1') + self.assertEqual(str(p), '<1>') + + def test_signature_parameter_immutability(self): + p = inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY) + + with self.assertRaises(AttributeError): + p.foo = 'bar' + + with self.assertRaises(AttributeError): + p.kind = 123 + + +class TestSignatureBind(unittest.TestCase): + @staticmethod + def call(func, *args, **kwargs): + sig = inspect.signature(func) + ba = sig.bind(*args, **kwargs) + return func(*ba.args, **ba.kwargs) + + def test_signature_bind_empty(self): + def test(): + return 42 + + self.assertEqual(self.call(test), 42) + with self.assertRaisesRegex(TypeError, 'too many positional arguments'): + self.call(test, 1) + with self.assertRaisesRegex(TypeError, 'too many positional arguments'): + self.call(test, 1, spam=10) + with self.assertRaisesRegex(TypeError, 'too many keyword arguments'): + self.call(test, spam=1) + + def test_signature_bind_var(self): + def test(*args, **kwargs): + return args, kwargs + + self.assertEqual(self.call(test), ((), {})) + self.assertEqual(self.call(test, 1), ((1,), {})) + self.assertEqual(self.call(test, 1, 2), ((1, 2), {})) + self.assertEqual(self.call(test, foo='bar'), ((), {'foo': 'bar'})) + self.assertEqual(self.call(test, 1, foo='bar'), ((1,), {'foo': 'bar'})) + self.assertEqual(self.call(test, args=10), ((), {'args': 10})) + self.assertEqual(self.call(test, 1, 2, foo='bar'), + ((1, 2), {'foo': 'bar'})) + + def test_signature_bind_just_args(self): + def test(a, b, c): + return a, b, c + + self.assertEqual(self.call(test, 1, 2, 3), (1, 2, 3)) + + with self.assertRaisesRegex(TypeError, 'too many positional arguments'): + self.call(test, 1, 2, 3, 4) + + with self.assertRaisesRegex(TypeError, "'b' parameter lacking default"): + self.call(test, 1) + + with self.assertRaisesRegex(TypeError, "'a' parameter lacking default"): + self.call(test) + + def test(a, b, c=10): + return a, b, c + self.assertEqual(self.call(test, 1, 2, 3), (1, 2, 3)) + self.assertEqual(self.call(test, 1, 2), (1, 2, 10)) + + def test(a=1, b=2, c=3): + return a, b, c + self.assertEqual(self.call(test, a=10, c=13), (10, 2, 13)) + self.assertEqual(self.call(test, a=10), (10, 2, 3)) + self.assertEqual(self.call(test, b=10), (1, 10, 3)) + + def test_signature_bind_varargs_order(self): + def test(*args): + return args + + self.assertEqual(self.call(test), ()) + self.assertEqual(self.call(test, 1, 2, 3), (1, 2, 3)) + + def test_signature_bind_args_and_varargs(self): + def test(a, b, c=3, *args): + return a, b, c, args + + self.assertEqual(self.call(test, 1, 2, 3, 4, 5), (1, 2, 3, (4, 5))) + self.assertEqual(self.call(test, 1, 2), (1, 2, 3, ())) + self.assertEqual(self.call(test, b=1, a=2), (2, 1, 3, ())) + self.assertEqual(self.call(test, 1, b=2), (1, 2, 3, ())) + + with self.assertRaisesRegex(TypeError, + "multiple values for argument 'c'"): + self.call(test, 1, 2, 3, c=4) + + def test_signature_bind_just_kwargs(self): + def test(**kwargs): + return kwargs + + self.assertEqual(self.call(test), {}) + self.assertEqual(self.call(test, foo='bar', spam='ham'), + {'foo': 'bar', 'spam': 'ham'}) + + def test_signature_bind_args_and_kwargs(self): + def test(a, b, c=3, **kwargs): + return a, b, c, kwargs + + self.assertEqual(self.call(test, 1, 2), (1, 2, 3, {})) + self.assertEqual(self.call(test, 1, 2, foo='bar', spam='ham'), + (1, 2, 3, {'foo': 'bar', 'spam': 'ham'})) + self.assertEqual(self.call(test, b=2, a=1, foo='bar', spam='ham'), + (1, 2, 3, {'foo': 'bar', 'spam': 'ham'})) + self.assertEqual(self.call(test, a=1, b=2, foo='bar', spam='ham'), + (1, 2, 3, {'foo': 'bar', 'spam': 'ham'})) + self.assertEqual(self.call(test, 1, b=2, foo='bar', spam='ham'), + (1, 2, 3, {'foo': 'bar', 'spam': 'ham'})) + self.assertEqual(self.call(test, 1, b=2, c=4, foo='bar', spam='ham'), + (1, 2, 4, {'foo': 'bar', 'spam': 'ham'})) + self.assertEqual(self.call(test, 1, 2, 4, foo='bar'), + (1, 2, 4, {'foo': 'bar'})) + self.assertEqual(self.call(test, c=5, a=4, b=3), + (4, 3, 5, {})) + + if sys.version_info[0] > 2: + exec(""" +def test_signature_bind_kwonly(self): + def test(*, foo): + return foo + with self.assertRaisesRegex(TypeError, + 'too many positional arguments'): + self.call(test, 1) + self.assertEqual(self.call(test, foo=1), 1) + + def test(a, *, foo=1, bar): + return foo + with self.assertRaisesRegex(TypeError, + "'bar' parameter lacking default value"): + self.call(test, 1) + + def test(foo, *, bar): + return foo, bar + self.assertEqual(self.call(test, 1, bar=2), (1, 2)) + self.assertEqual(self.call(test, bar=2, foo=1), (1, 2)) + + with self.assertRaisesRegex(TypeError, + 'too many keyword arguments'): + self.call(test, bar=2, foo=1, spam=10) + + with self.assertRaisesRegex(TypeError, + 'too many positional arguments'): + self.call(test, 1, 2) + + with self.assertRaisesRegex(TypeError, + 'too many positional arguments'): + self.call(test, 1, 2, bar=2) + + with self.assertRaisesRegex(TypeError, + 'too many keyword arguments'): + self.call(test, 1, bar=2, spam='ham') + + with self.assertRaisesRegex(TypeError, + "'bar' parameter lacking default value"): + self.call(test, 1) + + def test(foo, *, bar, **bin): + return foo, bar, bin + self.assertEqual(self.call(test, 1, bar=2), (1, 2, {})) + self.assertEqual(self.call(test, foo=1, bar=2), (1, 2, {})) + self.assertEqual(self.call(test, 1, bar=2, spam='ham'), + (1, 2, {'spam': 'ham'})) + self.assertEqual(self.call(test, spam='ham', foo=1, bar=2), + (1, 2, {'spam': 'ham'})) + with self.assertRaisesRegex(TypeError, + "'foo' parameter lacking default value"): + self.call(test, spam='ham', bar=2) + self.assertEqual(self.call(test, 1, bar=2, bin=1, spam=10), + (1, 2, {'bin': 1, 'spam': 10})) +""") +# + if sys.version_info[0] > 2: + exec(""" +def test_signature_bind_arguments(self): + def test(a, *args, b, z=100, **kwargs): + pass + sig = inspect.signature(test) + ba = sig.bind(10, 20, b=30, c=40, args=50, kwargs=60) + # we won't have 'z' argument in the bound arguments object, as we didn't + # pass it to the 'bind' + self.assertEqual(tuple(ba.arguments.items()), + (('a', 10), ('args', (20,)), ('b', 30), + ('kwargs', {'c': 40, 'args': 50, 'kwargs': 60}))) + self.assertEqual(ba.kwargs, + {'b': 30, 'c': 40, 'args': 50, 'kwargs': 60}) + self.assertEqual(ba.args, (10, 20)) +""") +# + if sys.version_info[0] > 2: + exec(""" +def test_signature_bind_positional_only(self): + P = inspect.Parameter + + def test(a_po, b_po, c_po=3, foo=42, *, bar=50, **kwargs): + return a_po, b_po, c_po, foo, bar, kwargs + + sig = inspect.signature(test) + new_params = collections.OrderedDict(tuple(sig.parameters.items())) + for name in ('a_po', 'b_po', 'c_po'): + new_params[name] = new_params[name].replace(kind=P.POSITIONAL_ONLY) + new_sig = sig.replace(parameters=new_params.values()) + test.__signature__ = new_sig + + self.assertEqual(self.call(test, 1, 2, 4, 5, bar=6), + (1, 2, 4, 5, 6, {})) + + with self.assertRaisesRegex(TypeError, "parameter is positional only"): + self.call(test, 1, 2, c_po=4) + + with self.assertRaisesRegex(TypeError, "parameter is positional only"): + self.call(test, a_po=1, b_po=2) +""") + + def test_bind_self(self): + class F: + def f(a, self): + return a, self + an_f = F() + partial_f = functools.partial(F.f, an_f) + ba = inspect.signature(partial_f).bind(self=10) + self.assertEqual((an_f, 10), partial_f(*ba.args, **ba.kwargs)) + + +class TestBoundArguments(unittest.TestCase): + + def test_signature_bound_arguments_unhashable(self): + def foo(a): pass + ba = inspect.signature(foo).bind(1) + + with self.assertRaisesRegex(TypeError, 'unhashable type'): + hash(ba) + + def test_signature_bound_arguments_equality(self): + def foo(a): pass + ba = inspect.signature(foo).bind(1) + self.assertEqual(ba, ba) + + ba2 = inspect.signature(foo).bind(1) + self.assertEqual(ba, ba2) + + ba3 = inspect.signature(foo).bind(2) + self.assertNotEqual(ba, ba3) + ba3.arguments['a'] = 1 + self.assertEqual(ba, ba3) + + def bar(b): pass + ba4 = inspect.signature(bar).bind(1) + self.assertNotEqual(ba, ba4) diff --git a/third_party/python/more-itertools/LICENSE b/third_party/python/more-itertools/LICENSE new file mode 100644 index 000000000000..0a523bece3e5 --- /dev/null +++ b/third_party/python/more-itertools/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2012 Erik Rose + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/python/more-itertools/MANIFEST.in b/third_party/python/more-itertools/MANIFEST.in new file mode 100644 index 000000000000..ec800e3e02a6 --- /dev/null +++ b/third_party/python/more-itertools/MANIFEST.in @@ -0,0 +1,8 @@ +include README.rst +include LICENSE +include docs/*.rst +include docs/Makefile +include docs/make.bat +include docs/conf.py +include fabfile.py +include tox.ini diff --git a/third_party/python/more-itertools/PKG-INFO b/third_party/python/more-itertools/PKG-INFO new file mode 100644 index 000000000000..8c802bc8af65 --- /dev/null +++ b/third_party/python/more-itertools/PKG-INFO @@ -0,0 +1,321 @@ +Metadata-Version: 1.1 +Name: more-itertools +Version: 4.2.0 +Summary: More routines for operating on iterables, beyond itertools +Home-page: https://github.com/erikrose/more-itertools +Author: Erik Rose +Author-email: erikrose@grinchcentral.com +License: MIT +Description: ============== + More Itertools + ============== + + .. image:: https://coveralls.io/repos/github/erikrose/more-itertools/badge.svg?branch=master + :target: https://coveralls.io/github/erikrose/more-itertools?branch=master + + Python's ``itertools`` library is a gem - you can compose elegant solutions + for a variety of problems with the functions it provides. In ``more-itertools`` + we collect additional building blocks, recipes, and routines for working with + Python iterables. + + Getting started + =============== + + To get started, install the library with `pip `_: + + .. code-block:: shell + + pip install more-itertools + + The recipes from the `itertools docs `_ + are included in the top-level package: + + .. code-block:: python + + >>> from more_itertools import flatten + >>> iterable = [(0, 1), (2, 3)] + >>> list(flatten(iterable)) + [0, 1, 2, 3] + + Several new recipes are available as well: + + .. code-block:: python + + >>> from more_itertools import chunked + >>> iterable = [0, 1, 2, 3, 4, 5, 6, 7, 8] + >>> list(chunked(iterable, 3)) + [[0, 1, 2], [3, 4, 5], [6, 7, 8]] + + >>> from more_itertools import spy + >>> iterable = (x * x for x in range(1, 6)) + >>> head, iterable = spy(iterable, n=3) + >>> list(head) + [1, 4, 9] + >>> list(iterable) + [1, 4, 9, 16, 25] + + + + For the full listing of functions, see the `API documentation `_. + + Development + =========== + + ``more-itertools`` is maintained by `@erikrose `_ + and `@bbayles `_, with help from `many others `_. + If you have a problem or suggestion, please file a bug or pull request in this + repository. Thanks for contributing! + + + Version History + =============== + + + + 4.2.0 + ----- + + * New itertools: + * map_reduce (thanks to pylang) + * prepend (from the `Python 3.7 docs `_) + + * Improvements to existing itertools: + * bucket now complies with PEP 479 (thanks to irmen) + + * Other changes: + * Python 3.7 is now supported (thanks to irmen) + * Python 3.3 is no longer supported + * The test suite no longer requires third-party modules to run + * The API docs now include links to source code + + 4.1.0 + ----- + + * New itertools: + * split_at (thanks to michael-celani) + * circular_shifts (thanks to hiqua) + * make_decorator - see the blog post `Yo, I heard you like decorators `_ + for a tour (thanks to pylang) + * always_reversible (thanks to michael-celani) + * nth_combination (from the `Python 3.7 docs `_) + + * Improvements to existing itertools: + * seekable now has an ``elements`` method to return cached items. + * The performance tradeoffs between roundrobin and + interleave_longest are now documented (thanks michael-celani, + pylang, and MSeifert04) + + 4.0.1 + ----- + + * No code changes - this release fixes how the docs display on PyPI. + + 4.0.0 + ----- + + * New itertools: + * consecutive_groups (Based on the example in the `Python 2.4 docs `_) + * seekable (If you're looking for how to "reset" an iterator, + you're in luck!) + * exactly_n (thanks to michael-celani) + * run_length.encode and run_length.decode + * difference + + * Improvements to existing itertools: + * The number of items between filler elements in intersperse can + now be specified (thanks to pylang) + * distinct_permutations and peekable got some minor + adjustments (thanks to MSeifert04) + * always_iterable now returns an iterator object. It also now + allows different types to be considered iterable (thanks to jaraco) + * bucket can now limit the keys it stores in memory + * one now allows for custom exceptions (thanks to kalekundert) + + * Other changes: + * A few typos were fixed (thanks to EdwardBetts) + * All tests can now be run with ``python setup.py test`` + + The major version update is due to the change in the return value of always_iterable. + It now always returns iterator objects: + + .. code-block:: python + + >>> from more_itertools import always_iterable + # Non-iterable objects are wrapped with iter(tuple(obj)) + >>> always_iterable(12345) + + >>> list(always_iterable(12345)) + [12345] + # Iterable objects are wrapped with iter() + >>> always_iterable([1, 2, 3, 4, 5]) + + + 3.2.0 + ----- + + * New itertools: + * lstrip, rstrip, and strip + (thanks to MSeifert04 and pylang) + * islice_extended + * Improvements to existing itertools: + * Some bugs with slicing peekable-wrapped iterables were fixed + + 3.1.0 + ----- + + * New itertools: + * numeric_range (Thanks to BebeSparkelSparkel and MSeifert04) + * count_cycle (Thanks to BebeSparkelSparkel) + * locate (Thanks to pylang and MSeifert04) + * Improvements to existing itertools: + * A few itertools are now slightly faster due to some function + optimizations. (Thanks to MSeifert04) + * The docs have been substantially revised with installation notes, + categories for library functions, links, and more. (Thanks to pylang) + + + 3.0.0 + ----- + + * Removed itertools: + * ``context`` has been removed due to a design flaw - see below for + replacement options. (thanks to NeilGirdhar) + * Improvements to existing itertools: + * ``side_effect`` now supports ``before`` and ``after`` keyword + arguments. (Thanks to yardsale8) + * PyPy and PyPy3 are now supported. + + The major version change is due to the removal of the ``context`` function. + Replace it with standard ``with`` statement context management: + + .. code-block:: python + + # Don't use context() anymore + file_obj = StringIO() + consume(print(x, file=f) for f in context(file_obj) for x in u'123') + + # Use a with statement instead + file_obj = StringIO() + with file_obj as f: + consume(print(x, file=f) for x in u'123') + + 2.6.0 + ----- + + * New itertools: + * ``adjacent`` and ``groupby_transform`` (Thanks to diazona) + * ``always_iterable`` (Thanks to jaraco) + * (Removed in 3.0.0) ``context`` (Thanks to yardsale8) + * ``divide`` (Thanks to mozbhearsum) + * Improvements to existing itertools: + * ``ilen`` is now slightly faster. (Thanks to wbolster) + * ``peekable`` can now prepend items to an iterable. (Thanks to diazona) + + 2.5.0 + ----- + + * New itertools: + * ``distribute`` (Thanks to mozbhearsum and coady) + * ``sort_together`` (Thanks to clintval) + * ``stagger`` and ``zip_offset`` (Thanks to joshbode) + * ``padded`` + * Improvements to existing itertools: + * ``peekable`` now handles negative indexes and slices with negative + components properly. + * ``intersperse`` is now slightly faster. (Thanks to pylang) + * ``windowed`` now accepts a ``step`` keyword argument. + (Thanks to pylang) + * Python 3.6 is now supported. + + 2.4.1 + ----- + + * Move docs 100% to readthedocs.io. + + 2.4 + ----- + + * New itertools: + * ``accumulate``, ``all_equal``, ``first_true``, ``partition``, and + ``tail`` from the itertools documentation. + * ``bucket`` (Thanks to Rosuav and cvrebert) + * ``collapse`` (Thanks to abarnet) + * ``interleave`` and ``interleave_longest`` (Thanks to abarnet) + * ``side_effect`` (Thanks to nvie) + * ``sliced`` (Thanks to j4mie and coady) + * ``split_before`` and ``split_after`` (Thanks to astronouth7303) + * ``spy`` (Thanks to themiurgo and mathieulongtin) + * Improvements to existing itertools: + * ``chunked`` is now simpler and more friendly to garbage collection. + (Contributed by coady, with thanks to piskvorky) + * ``collate`` now delegates to ``heapq.merge`` when possible. + (Thanks to kmike and julianpistorius) + * ``peekable``-wrapped iterables are now indexable and sliceable. + Iterating through ``peekable``-wrapped iterables is also faster. + * ``one`` and ``unique_to_each`` have been simplified. + (Thanks to coady) + + + 2.3 + ----- + + * Added ``one`` from ``jaraco.util.itertools``. (Thanks, jaraco!) + * Added ``distinct_permutations`` and ``unique_to_each``. (Contributed by + bbayles) + * Added ``windowed``. (Contributed by bbayles, with thanks to buchanae, + jaraco, and abarnert) + * Simplified the implementation of ``chunked``. (Thanks, nvie!) + * Python 3.5 is now supported. Python 2.6 is no longer supported. + * Python 3 is now supported directly; there is no 2to3 step. + + 2.2 + ----- + + * Added ``iterate`` and ``with_iter``. (Thanks, abarnert!) + + 2.1 + ----- + + * Added (tested!) implementations of the recipes from the itertools + documentation. (Thanks, Chris Lonnen!) + * Added ``ilen``. (Thanks for the inspiration, Matt Basta!) + + 2.0 + ----- + + * ``chunked`` now returns lists rather than tuples. After all, they're + homogeneous. This slightly backward-incompatible change is the reason for + the major version bump. + * Added ``@consumer``. + * Improved test machinery. + + 1.1 + ----- + + * Added ``first`` function. + * Added Python 3 support. + * Added a default arg to ``peekable.peek()``. + * Noted how to easily test whether a peekable iterator is exhausted. + * Rewrote documentation. + + 1.0 + ----- + + * Initial release, with ``collate``, ``peekable``, and ``chunked``. Could + really use better docs. +Keywords: itertools,iterator,iteration,filter,peek,peekable,collate,chunk,chunked +Platform: UNKNOWN +Classifier: Development Status :: 5 - Production/Stable +Classifier: Intended Audience :: Developers +Classifier: Natural Language :: English +Classifier: License :: OSI Approved :: MIT License +Classifier: Programming Language :: Python :: 2 +Classifier: Programming Language :: Python :: 2.7 +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.2 +Classifier: Programming Language :: Python :: 3.3 +Classifier: Programming Language :: Python :: 3.4 +Classifier: Programming Language :: Python :: 3.5 +Classifier: Programming Language :: Python :: 3.6 +Classifier: Programming Language :: Python :: 3.7 +Classifier: Topic :: Software Development :: Libraries diff --git a/third_party/python/more-itertools/README.rst b/third_party/python/more-itertools/README.rst new file mode 100644 index 000000000000..252b394737ac --- /dev/null +++ b/third_party/python/more-itertools/README.rst @@ -0,0 +1,59 @@ +============== +More Itertools +============== + +.. image:: https://coveralls.io/repos/github/erikrose/more-itertools/badge.svg?branch=master + :target: https://coveralls.io/github/erikrose/more-itertools?branch=master + +Python's ``itertools`` library is a gem - you can compose elegant solutions +for a variety of problems with the functions it provides. In ``more-itertools`` +we collect additional building blocks, recipes, and routines for working with +Python iterables. + +Getting started +=============== + +To get started, install the library with `pip `_: + +.. code-block:: shell + + pip install more-itertools + +The recipes from the `itertools docs `_ +are included in the top-level package: + +.. code-block:: python + + >>> from more_itertools import flatten + >>> iterable = [(0, 1), (2, 3)] + >>> list(flatten(iterable)) + [0, 1, 2, 3] + +Several new recipes are available as well: + +.. code-block:: python + + >>> from more_itertools import chunked + >>> iterable = [0, 1, 2, 3, 4, 5, 6, 7, 8] + >>> list(chunked(iterable, 3)) + [[0, 1, 2], [3, 4, 5], [6, 7, 8]] + + >>> from more_itertools import spy + >>> iterable = (x * x for x in range(1, 6)) + >>> head, iterable = spy(iterable, n=3) + >>> list(head) + [1, 4, 9] + >>> list(iterable) + [1, 4, 9, 16, 25] + + + +For the full listing of functions, see the `API documentation `_. + +Development +=========== + +``more-itertools`` is maintained by `@erikrose `_ +and `@bbayles `_, with help from `many others `_. +If you have a problem or suggestion, please file a bug or pull request in this +repository. Thanks for contributing! diff --git a/third_party/python/more-itertools/docs/Makefile b/third_party/python/more-itertools/docs/Makefile new file mode 100644 index 000000000000..47888da7b72b --- /dev/null +++ b/third_party/python/more-itertools/docs/Makefile @@ -0,0 +1,153 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + -rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/more-itertools.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/more-itertools.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/more-itertools" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/more-itertools" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." diff --git a/third_party/python/more-itertools/docs/api.rst b/third_party/python/more-itertools/docs/api.rst new file mode 100644 index 000000000000..63e5d7f45001 --- /dev/null +++ b/third_party/python/more-itertools/docs/api.rst @@ -0,0 +1,234 @@ +============= +API Reference +============= + +.. automodule:: more_itertools + +Grouping +======== + +These tools yield groups of items from a source iterable. + +---- + +**New itertools** + +.. autofunction:: chunked +.. autofunction:: sliced +.. autofunction:: distribute +.. autofunction:: divide +.. autofunction:: split_at +.. autofunction:: split_before +.. autofunction:: split_after +.. autofunction:: bucket + +---- + +**Itertools recipes** + +.. autofunction:: grouper +.. autofunction:: partition + + +Lookahead and lookback +====================== + +These tools peek at an iterable's values without advancing it. + +---- + +**New itertools** + + +.. autofunction:: spy +.. autoclass:: peekable +.. autoclass:: seekable + + +Windowing +========= + +These tools yield windows of items from an iterable. + +---- + +**New itertools** + +.. autofunction:: windowed +.. autofunction:: stagger + +---- + +**Itertools recipes** + +.. autofunction:: pairwise + + +Augmenting +========== + +These tools yield items from an iterable, plus additional data. + +---- + +**New itertools** + +.. autofunction:: count_cycle +.. autofunction:: intersperse +.. autofunction:: padded +.. autofunction:: adjacent +.. autofunction:: groupby_transform + +---- + +**Itertools recipes** + +.. autofunction:: padnone +.. autofunction:: ncycles + + +Combining +========= + +These tools combine multiple iterables. + +---- + +**New itertools** + +.. autofunction:: collapse +.. autofunction:: sort_together +.. autofunction:: interleave +.. autofunction:: interleave_longest +.. autofunction:: collate(*iterables, key=lambda a: a, reverse=False) +.. autofunction:: zip_offset(*iterables, offsets, longest=False, fillvalue=None) + +---- + +**Itertools recipes** + +.. autofunction:: dotproduct +.. autofunction:: flatten +.. autofunction:: roundrobin +.. autofunction:: prepend + + +Summarizing +=========== + +These tools return summarized or aggregated data from an iterable. + +---- + +**New itertools** + +.. autofunction:: ilen +.. autofunction:: first(iterable[, default]) +.. autofunction:: one +.. autofunction:: unique_to_each +.. autofunction:: locate(iterable, pred=bool) +.. autofunction:: consecutive_groups(iterable, ordering=lambda x: x) +.. autofunction:: exactly_n(iterable, n, predicate=bool) +.. autoclass:: run_length +.. autofunction:: map_reduce + +---- + +**Itertools recipes** + +.. autofunction:: all_equal +.. autofunction:: first_true +.. autofunction:: nth +.. autofunction:: quantify(iterable, pred=bool) + + +Selecting +========= + +These tools yield certain items from an iterable. + +---- + +**New itertools** + +.. autofunction:: islice_extended(start, stop, step) +.. autofunction:: strip +.. autofunction:: lstrip +.. autofunction:: rstrip + +---- + +**Itertools recipes** + +.. autofunction:: take +.. autofunction:: tail +.. autofunction:: unique_everseen +.. autofunction:: unique_justseen + + +Combinatorics +============= + +These tools yield combinatorial arrangements of items from iterables. + +---- + +**New itertools** + +.. autofunction:: distinct_permutations +.. autofunction:: circular_shifts + +---- + +**Itertools recipes** + +.. autofunction:: powerset +.. autofunction:: random_product +.. autofunction:: random_permutation +.. autofunction:: random_combination +.. autofunction:: random_combination_with_replacement +.. autofunction:: nth_combination + + +Wrapping +======== + +These tools provide wrappers to smooth working with objects that produce or +consume iterables. + +---- + +**New itertools** + +.. autofunction:: always_iterable +.. autofunction:: consumer +.. autofunction:: with_iter + +---- + +**Itertools recipes** + +.. autofunction:: iter_except + + +Others +====== + +**New itertools** + +.. autofunction:: numeric_range(start, stop, step) +.. autofunction:: always_reversible +.. autofunction:: side_effect +.. autofunction:: iterate +.. autofunction:: difference(iterable, func=operator.sub) +.. autofunction:: make_decorator +.. autoclass:: SequenceView + +---- + +**Itertools recipes** + +.. autofunction:: consume +.. autofunction:: accumulate(iterable, func=operator.add) +.. autofunction:: tabulate +.. autofunction:: repeatfunc diff --git a/third_party/python/more-itertools/docs/conf.py b/third_party/python/more-itertools/docs/conf.py new file mode 100644 index 000000000000..e38c71aeaacd --- /dev/null +++ b/third_party/python/more-itertools/docs/conf.py @@ -0,0 +1,244 @@ +# -*- coding: utf-8 -*- +# +# more-itertools documentation build configuration file, created by +# sphinx-quickstart on Mon Jun 25 20:42:39 2012. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +import sphinx_rtd_theme + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +sys.path.insert(0, os.path.abspath('..')) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'more-itertools' +copyright = u'2012, Erik Rose' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '4.2.0' +# The full version, including alpha/beta/rc tags. +release = version + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'sphinx_rtd_theme' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'more-itertoolsdoc' + + +# -- Options for LaTeX output -------------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'more-itertools.tex', u'more-itertools Documentation', + u'Erik Rose', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output -------------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'more-itertools', u'more-itertools Documentation', + [u'Erik Rose'], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------------ + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'more-itertools', u'more-itertools Documentation', + u'Erik Rose', 'more-itertools', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' diff --git a/third_party/python/more-itertools/docs/index.rst b/third_party/python/more-itertools/docs/index.rst new file mode 100644 index 000000000000..091461ff7d66 --- /dev/null +++ b/third_party/python/more-itertools/docs/index.rst @@ -0,0 +1,16 @@ +.. include:: ../README.rst + +Contents +======== + +.. toctree:: + :maxdepth: 2 + + api + +.. toctree:: + :maxdepth: 1 + + license + testing + versions diff --git a/third_party/python/more-itertools/docs/license.rst b/third_party/python/more-itertools/docs/license.rst new file mode 100644 index 000000000000..123c0f54dcae --- /dev/null +++ b/third_party/python/more-itertools/docs/license.rst @@ -0,0 +1,16 @@ +======= +License +======= + +more-itertools is under the MIT License. See the LICENSE file. + +Conditions for Contributors +=========================== + +By contributing to this software project, you are agreeing to the following +terms and conditions for your contributions: First, you agree your +contributions are submitted under the MIT license. Second, you represent you +are authorized to make the contributions and grant the license. If your +employer has rights to intellectual property that includes your contributions, +you represent that you have received permission to make contributions and grant +the required license on behalf of that employer. diff --git a/third_party/python/more-itertools/docs/make.bat b/third_party/python/more-itertools/docs/make.bat new file mode 100644 index 000000000000..8023c0aa6740 --- /dev/null +++ b/third_party/python/more-itertools/docs/make.bat @@ -0,0 +1,190 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=_build +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . +set I18NSPHINXOPTS=%SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% + set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. singlehtml to make a single large HTML file + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. devhelp to make HTML files and a Devhelp project + echo. epub to make an epub + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. text to make text files + echo. man to make manual pages + echo. texinfo to make Texinfo files + echo. gettext to make PO message catalogs + echo. changes to make an overview over all changed/added/deprecated items + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\more-itertools.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\more-itertools.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "texinfo" ( + %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. + goto end +) + +if "%1" == "gettext" ( + %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The message catalogs are in %BUILDDIR%/locale. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +:end diff --git a/third_party/python/more-itertools/docs/testing.rst b/third_party/python/more-itertools/docs/testing.rst new file mode 100644 index 000000000000..bdd421995178 --- /dev/null +++ b/third_party/python/more-itertools/docs/testing.rst @@ -0,0 +1,19 @@ +======= +Testing +======= + +To run install dependencies and run tests, use this command:: + + python setup.py test + +Multiple Python Versions +======================== + +To run the tests on all the versions of Python more-itertools supports, install +tox:: + + pip install tox + +Then, run the tests:: + + tox diff --git a/third_party/python/more-itertools/docs/versions.rst b/third_party/python/more-itertools/docs/versions.rst new file mode 100644 index 000000000000..e50ac4393dd8 --- /dev/null +++ b/third_party/python/more-itertools/docs/versions.rst @@ -0,0 +1,237 @@ +=============== +Version History +=============== + +.. automodule:: more_itertools + +4.2.0 +----- + +* New itertools: + * :func:`map_reduce` (thanks to pylang) + * :func:`prepend` (from the `Python 3.7 docs `_) + +* Improvements to existing itertools: + * :func:`bucket` now complies with PEP 479 (thanks to irmen) + +* Other changes: + * Python 3.7 is now supported (thanks to irmen) + * Python 3.3 is no longer supported + * The test suite no longer requires third-party modules to run + * The API docs now include links to source code + +4.1.0 +----- + +* New itertools: + * :func:`split_at` (thanks to michael-celani) + * :func:`circular_shifts` (thanks to hiqua) + * :func:`make_decorator` - see the blog post `Yo, I heard you like decorators `_ + for a tour (thanks to pylang) + * :func:`always_reversible` (thanks to michael-celani) + * :func:`nth_combination` (from the `Python 3.7 docs `_) + +* Improvements to existing itertools: + * :func:`seekable` now has an ``elements`` method to return cached items. + * The performance tradeoffs between :func:`roundrobin` and + :func:`interleave_longest` are now documented (thanks michael-celani, + pylang, and MSeifert04) + +4.0.1 +----- + +* No code changes - this release fixes how the docs display on PyPI. + +4.0.0 +----- + +* New itertools: + * :func:`consecutive_groups` (Based on the example in the `Python 2.4 docs `_) + * :func:`seekable` (If you're looking for how to "reset" an iterator, + you're in luck!) + * :func:`exactly_n` (thanks to michael-celani) + * :func:`run_length.encode` and :func:`run_length.decode` + * :func:`difference` + +* Improvements to existing itertools: + * The number of items between filler elements in :func:`intersperse` can + now be specified (thanks to pylang) + * :func:`distinct_permutations` and :func:`peekable` got some minor + adjustments (thanks to MSeifert04) + * :func:`always_iterable` now returns an iterator object. It also now + allows different types to be considered iterable (thanks to jaraco) + * :func:`bucket` can now limit the keys it stores in memory + * :func:`one` now allows for custom exceptions (thanks to kalekundert) + +* Other changes: + * A few typos were fixed (thanks to EdwardBetts) + * All tests can now be run with ``python setup.py test`` + +The major version update is due to the change in the return value of :func:`always_iterable`. +It now always returns iterator objects: + +.. code-block:: python + + >>> from more_itertools import always_iterable + # Non-iterable objects are wrapped with iter(tuple(obj)) + >>> always_iterable(12345) + + >>> list(always_iterable(12345)) + [12345] + # Iterable objects are wrapped with iter() + >>> always_iterable([1, 2, 3, 4, 5]) + + +3.2.0 +----- + +* New itertools: + * :func:`lstrip`, :func:`rstrip`, and :func:`strip` + (thanks to MSeifert04 and pylang) + * :func:`islice_extended` +* Improvements to existing itertools: + * Some bugs with slicing :func:`peekable`-wrapped iterables were fixed + +3.1.0 +----- + +* New itertools: + * :func:`numeric_range` (Thanks to BebeSparkelSparkel and MSeifert04) + * :func:`count_cycle` (Thanks to BebeSparkelSparkel) + * :func:`locate` (Thanks to pylang and MSeifert04) +* Improvements to existing itertools: + * A few itertools are now slightly faster due to some function + optimizations. (Thanks to MSeifert04) +* The docs have been substantially revised with installation notes, + categories for library functions, links, and more. (Thanks to pylang) + + +3.0.0 +----- + +* Removed itertools: + * ``context`` has been removed due to a design flaw - see below for + replacement options. (thanks to NeilGirdhar) +* Improvements to existing itertools: + * ``side_effect`` now supports ``before`` and ``after`` keyword + arguments. (Thanks to yardsale8) +* PyPy and PyPy3 are now supported. + +The major version change is due to the removal of the ``context`` function. +Replace it with standard ``with`` statement context management: + +.. code-block:: python + + # Don't use context() anymore + file_obj = StringIO() + consume(print(x, file=f) for f in context(file_obj) for x in u'123') + + # Use a with statement instead + file_obj = StringIO() + with file_obj as f: + consume(print(x, file=f) for x in u'123') + +2.6.0 +----- + +* New itertools: + * ``adjacent`` and ``groupby_transform`` (Thanks to diazona) + * ``always_iterable`` (Thanks to jaraco) + * (Removed in 3.0.0) ``context`` (Thanks to yardsale8) + * ``divide`` (Thanks to mozbhearsum) +* Improvements to existing itertools: + * ``ilen`` is now slightly faster. (Thanks to wbolster) + * ``peekable`` can now prepend items to an iterable. (Thanks to diazona) + +2.5.0 +----- + +* New itertools: + * ``distribute`` (Thanks to mozbhearsum and coady) + * ``sort_together`` (Thanks to clintval) + * ``stagger`` and ``zip_offset`` (Thanks to joshbode) + * ``padded`` +* Improvements to existing itertools: + * ``peekable`` now handles negative indexes and slices with negative + components properly. + * ``intersperse`` is now slightly faster. (Thanks to pylang) + * ``windowed`` now accepts a ``step`` keyword argument. + (Thanks to pylang) +* Python 3.6 is now supported. + +2.4.1 +----- + +* Move docs 100% to readthedocs.io. + +2.4 +----- + +* New itertools: + * ``accumulate``, ``all_equal``, ``first_true``, ``partition``, and + ``tail`` from the itertools documentation. + * ``bucket`` (Thanks to Rosuav and cvrebert) + * ``collapse`` (Thanks to abarnet) + * ``interleave`` and ``interleave_longest`` (Thanks to abarnet) + * ``side_effect`` (Thanks to nvie) + * ``sliced`` (Thanks to j4mie and coady) + * ``split_before`` and ``split_after`` (Thanks to astronouth7303) + * ``spy`` (Thanks to themiurgo and mathieulongtin) +* Improvements to existing itertools: + * ``chunked`` is now simpler and more friendly to garbage collection. + (Contributed by coady, with thanks to piskvorky) + * ``collate`` now delegates to ``heapq.merge`` when possible. + (Thanks to kmike and julianpistorius) + * ``peekable``-wrapped iterables are now indexable and sliceable. + Iterating through ``peekable``-wrapped iterables is also faster. + * ``one`` and ``unique_to_each`` have been simplified. + (Thanks to coady) + + +2.3 +----- + +* Added ``one`` from ``jaraco.util.itertools``. (Thanks, jaraco!) +* Added ``distinct_permutations`` and ``unique_to_each``. (Contributed by + bbayles) +* Added ``windowed``. (Contributed by bbayles, with thanks to buchanae, + jaraco, and abarnert) +* Simplified the implementation of ``chunked``. (Thanks, nvie!) +* Python 3.5 is now supported. Python 2.6 is no longer supported. +* Python 3 is now supported directly; there is no 2to3 step. + +2.2 +----- + +* Added ``iterate`` and ``with_iter``. (Thanks, abarnert!) + +2.1 +----- + +* Added (tested!) implementations of the recipes from the itertools + documentation. (Thanks, Chris Lonnen!) +* Added ``ilen``. (Thanks for the inspiration, Matt Basta!) + +2.0 +----- + +* ``chunked`` now returns lists rather than tuples. After all, they're + homogeneous. This slightly backward-incompatible change is the reason for + the major version bump. +* Added ``@consumer``. +* Improved test machinery. + +1.1 +----- + +* Added ``first`` function. +* Added Python 3 support. +* Added a default arg to ``peekable.peek()``. +* Noted how to easily test whether a peekable iterator is exhausted. +* Rewrote documentation. + +1.0 +----- + +* Initial release, with ``collate``, ``peekable``, and ``chunked``. Could + really use better docs. diff --git a/third_party/python/more-itertools/more_itertools/__init__.py b/third_party/python/more-itertools/more_itertools/__init__.py new file mode 100644 index 000000000000..bba462c3dbfc --- /dev/null +++ b/third_party/python/more-itertools/more_itertools/__init__.py @@ -0,0 +1,2 @@ +from more_itertools.more import * # noqa +from more_itertools.recipes import * # noqa diff --git a/third_party/python/more-itertools/more_itertools/more.py b/third_party/python/more-itertools/more_itertools/more.py new file mode 100644 index 000000000000..d517250242f3 --- /dev/null +++ b/third_party/python/more-itertools/more_itertools/more.py @@ -0,0 +1,2068 @@ +from __future__ import print_function + +from collections import Counter, defaultdict, deque +from functools import partial, wraps +from heapq import merge +from itertools import ( + chain, + compress, + count, + cycle, + dropwhile, + groupby, + islice, + repeat, + takewhile, + tee +) +from operator import itemgetter, lt, gt, sub +from sys import maxsize, version_info +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence + +from six import binary_type, string_types, text_type +from six.moves import filter, map, range, zip, zip_longest + +from .recipes import consume, flatten, take + +__all__ = [ + 'adjacent', + 'always_iterable', + 'always_reversible', + 'bucket', + 'chunked', + 'circular_shifts', + 'collapse', + 'collate', + 'consecutive_groups', + 'consumer', + 'count_cycle', + 'difference', + 'distinct_permutations', + 'distribute', + 'divide', + 'exactly_n', + 'first', + 'groupby_transform', + 'ilen', + 'interleave_longest', + 'interleave', + 'intersperse', + 'islice_extended', + 'iterate', + 'locate', + 'lstrip', + 'make_decorator', + 'map_reduce', + 'numeric_range', + 'one', + 'padded', + 'peekable', + 'rstrip', + 'run_length', + 'seekable', + 'SequenceView', + 'side_effect', + 'sliced', + 'sort_together', + 'split_at', + 'split_after', + 'split_before', + 'spy', + 'stagger', + 'strip', + 'unique_to_each', + 'windowed', + 'with_iter', + 'zip_offset', +] + +_marker = object() + + +def chunked(iterable, n): + """Break *iterable* into lists of length *n*: + + >>> list(chunked([1, 2, 3, 4, 5, 6], 3)) + [[1, 2, 3], [4, 5, 6]] + + If the length of *iterable* is not evenly divisible by *n*, the last + returned list will be shorter: + + >>> list(chunked([1, 2, 3, 4, 5, 6, 7, 8], 3)) + [[1, 2, 3], [4, 5, 6], [7, 8]] + + To use a fill-in value instead, see the :func:`grouper` recipe. + + :func:`chunked` is useful for splitting up a computation on a large number + of keys into batches, to be pickled and sent off to worker processes. One + example is operations on rows in MySQL, which does not implement + server-side cursors properly and would otherwise load the entire dataset + into RAM on the client. + + """ + return iter(partial(take, n, iter(iterable)), []) + + +def first(iterable, default=_marker): + """Return the first item of *iterable*, or *default* if *iterable* is + empty. + + >>> first([0, 1, 2, 3]) + 0 + >>> first([], 'some default') + 'some default' + + If *default* is not provided and there are no items in the iterable, + raise ``ValueError``. + + :func:`first` is useful when you have a generator of expensive-to-retrieve + values and want any arbitrary one. It is marginally shorter than + ``next(iter(iterable), default)``. + + """ + try: + return next(iter(iterable)) + except StopIteration: + # I'm on the edge about raising ValueError instead of StopIteration. At + # the moment, ValueError wins, because the caller could conceivably + # want to do something different with flow control when I raise the + # exception, and it's weird to explicitly catch StopIteration. + if default is _marker: + raise ValueError('first() was called on an empty iterable, and no ' + 'default value was provided.') + return default + + +class peekable(object): + """Wrap an iterator to allow lookahead and prepending elements. + + Call :meth:`peek` on the result to get the value that will be returned + by :func:`next`. This won't advance the iterator: + + >>> p = peekable(['a', 'b']) + >>> p.peek() + 'a' + >>> next(p) + 'a' + + Pass :meth:`peek` a default value to return that instead of raising + ``StopIteration`` when the iterator is exhausted. + + >>> p = peekable([]) + >>> p.peek('hi') + 'hi' + + peekables also offer a :meth:`prepend` method, which "inserts" items + at the head of the iterable: + + >>> p = peekable([1, 2, 3]) + >>> p.prepend(10, 11, 12) + >>> next(p) + 10 + >>> p.peek() + 11 + >>> list(p) + [11, 12, 1, 2, 3] + + peekables can be indexed. Index 0 is the item that will be returned by + :func:`next`, index 1 is the item after that, and so on: + The values up to the given index will be cached. + + >>> p = peekable(['a', 'b', 'c', 'd']) + >>> p[0] + 'a' + >>> p[1] + 'b' + >>> next(p) + 'a' + + Negative indexes are supported, but be aware that they will cache the + remaining items in the source iterator, which may require significant + storage. + + To check whether a peekable is exhausted, check its truth value: + + >>> p = peekable(['a', 'b']) + >>> if p: # peekable has items + ... list(p) + ['a', 'b'] + >>> if not p: # peekable is exhaused + ... list(p) + [] + + """ + def __init__(self, iterable): + self._it = iter(iterable) + self._cache = deque() + + def __iter__(self): + return self + + def __bool__(self): + try: + self.peek() + except StopIteration: + return False + return True + + def __nonzero__(self): + # For Python 2 compatibility + return self.__bool__() + + def peek(self, default=_marker): + """Return the item that will be next returned from ``next()``. + + Return ``default`` if there are no items left. If ``default`` is not + provided, raise ``StopIteration``. + + """ + if not self._cache: + try: + self._cache.append(next(self._it)) + except StopIteration: + if default is _marker: + raise + return default + return self._cache[0] + + def prepend(self, *items): + """Stack up items to be the next ones returned from ``next()`` or + ``self.peek()``. The items will be returned in + first in, first out order:: + + >>> p = peekable([1, 2, 3]) + >>> p.prepend(10, 11, 12) + >>> next(p) + 10 + >>> list(p) + [11, 12, 1, 2, 3] + + It is possible, by prepending items, to "resurrect" a peekable that + previously raised ``StopIteration``. + + >>> p = peekable([]) + >>> next(p) + Traceback (most recent call last): + ... + StopIteration + >>> p.prepend(1) + >>> next(p) + 1 + >>> next(p) + Traceback (most recent call last): + ... + StopIteration + + """ + self._cache.extendleft(reversed(items)) + + def __next__(self): + if self._cache: + return self._cache.popleft() + + return next(self._it) + + next = __next__ # For Python 2 compatibility + + def _get_slice(self, index): + # Normalize the slice's arguments + step = 1 if (index.step is None) else index.step + if step > 0: + start = 0 if (index.start is None) else index.start + stop = maxsize if (index.stop is None) else index.stop + elif step < 0: + start = -1 if (index.start is None) else index.start + stop = (-maxsize - 1) if (index.stop is None) else index.stop + else: + raise ValueError('slice step cannot be zero') + + # If either the start or stop index is negative, we'll need to cache + # the rest of the iterable in order to slice from the right side. + if (start < 0) or (stop < 0): + self._cache.extend(self._it) + # Otherwise we'll need to find the rightmost index and cache to that + # point. + else: + n = min(max(start, stop) + 1, maxsize) + cache_len = len(self._cache) + if n >= cache_len: + self._cache.extend(islice(self._it, n - cache_len)) + + return list(self._cache)[index] + + def __getitem__(self, index): + if isinstance(index, slice): + return self._get_slice(index) + + cache_len = len(self._cache) + if index < 0: + self._cache.extend(self._it) + elif index >= cache_len: + self._cache.extend(islice(self._it, index + 1 - cache_len)) + + return self._cache[index] + + +def _collate(*iterables, **kwargs): + """Helper for ``collate()``, called when the user is using the ``reverse`` + or ``key`` keyword arguments on Python versions below 3.5. + + """ + key = kwargs.pop('key', lambda a: a) + reverse = kwargs.pop('reverse', False) + + min_or_max = partial(max if reverse else min, key=itemgetter(0)) + peekables = [peekable(it) for it in iterables] + peekables = [p for p in peekables if p] # Kill empties. + while peekables: + _, p = min_or_max((key(p.peek()), p) for p in peekables) + yield next(p) + peekables = [x for x in peekables if x] + + +def collate(*iterables, **kwargs): + """Return a sorted merge of the items from each of several already-sorted + *iterables*. + + >>> list(collate('ACDZ', 'AZ', 'JKL')) + ['A', 'A', 'C', 'D', 'J', 'K', 'L', 'Z', 'Z'] + + Works lazily, keeping only the next value from each iterable in memory. Use + :func:`collate` to, for example, perform a n-way mergesort of items that + don't fit in memory. + + If a *key* function is specified, the iterables will be sorted according + to its result: + + >>> key = lambda s: int(s) # Sort by numeric value, not by string + >>> list(collate(['1', '10'], ['2', '11'], key=key)) + ['1', '2', '10', '11'] + + + If the *iterables* are sorted in descending order, set *reverse* to + ``True``: + + >>> list(collate([5, 3, 1], [4, 2, 0], reverse=True)) + [5, 4, 3, 2, 1, 0] + + If the elements of the passed-in iterables are out of order, you might get + unexpected results. + + On Python 2.7, this function delegates to :func:`heapq.merge` if neither + of the keyword arguments are specified. On Python 3.5+, this function + is an alias for :func:`heapq.merge`. + + """ + if not kwargs: + return merge(*iterables) + + return _collate(*iterables, **kwargs) + + +# If using Python version 3.5 or greater, heapq.merge() will be faster than +# collate - use that instead. +if version_info >= (3, 5, 0): + _collate_docstring = collate.__doc__ + collate = partial(merge) + collate.__doc__ = _collate_docstring + + +def consumer(func): + """Decorator that automatically advances a PEP-342-style "reverse iterator" + to its first yield point so you don't have to call ``next()`` on it + manually. + + >>> @consumer + ... def tally(): + ... i = 0 + ... while True: + ... print('Thing number %s is %s.' % (i, (yield))) + ... i += 1 + ... + >>> t = tally() + >>> t.send('red') + Thing number 0 is red. + >>> t.send('fish') + Thing number 1 is fish. + + Without the decorator, you would have to call ``next(t)`` before + ``t.send()`` could be used. + + """ + @wraps(func) + def wrapper(*args, **kwargs): + gen = func(*args, **kwargs) + next(gen) + return gen + return wrapper + + +def ilen(iterable): + """Return the number of items in *iterable*. + + >>> ilen(x for x in range(1000000) if x % 3 == 0) + 333334 + + This consumes the iterable, so handle with care. + + """ + # maxlen=1 only stores the last item in the deque + d = deque(enumerate(iterable, 1), maxlen=1) + # since we started enumerate at 1, + # the first item of the last pair will be the length of the iterable + # (assuming there were items) + return d[0][0] if d else 0 + + +def iterate(func, start): + """Return ``start``, ``func(start)``, ``func(func(start))``, ... + + >>> from itertools import islice + >>> list(islice(iterate(lambda x: 2*x, 1), 10)) + [1, 2, 4, 8, 16, 32, 64, 128, 256, 512] + + """ + while True: + yield start + start = func(start) + + +def with_iter(context_manager): + """Wrap an iterable in a ``with`` statement, so it closes once exhausted. + + For example, this will close the file when the iterator is exhausted:: + + upper_lines = (line.upper() for line in with_iter(open('foo'))) + + Any context manager which returns an iterable is a candidate for + ``with_iter``. + + """ + with context_manager as iterable: + for item in iterable: + yield item + + +def one(iterable, too_short=None, too_long=None): + """Return the first item from *iterable*, which is expected to contain only + that item. Raise an exception if *iterable* is empty or has more than one + item. + + :func:`one` is useful for ensuring that an iterable contains only one item. + For example, it can be used to retrieve the result of a database query + that is expected to return a single row. + + If *iterable* is empty, ``ValueError`` will be raised. You may specify a + different exception with the *too_short* keyword: + + >>> it = [] + >>> one(it) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + ValueError: too many items in iterable (expected 1)' + >>> too_short = IndexError('too few items') + >>> one(it, too_short=too_short) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + IndexError: too few items + + Similarly, if *iterable* contains more than one item, ``ValueError`` will + be raised. You may specify a different exception with the *too_long* + keyword: + + >>> it = ['too', 'many'] + >>> one(it) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + ValueError: too many items in iterable (expected 1)' + >>> too_long = RuntimeError + >>> one(it, too_long=too_long) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + RuntimeError + + Note that :func:`one` attempts to advance *iterable* twice to ensure there + is only one item. If there is more than one, both items will be discarded. + See :func:`spy` or :func:`peekable` to check iterable contents less + destructively. + + """ + it = iter(iterable) + + try: + value = next(it) + except StopIteration: + raise too_short or ValueError('too few items in iterable (expected 1)') + + try: + next(it) + except StopIteration: + pass + else: + raise too_long or ValueError('too many items in iterable (expected 1)') + + return value + + +def distinct_permutations(iterable): + """Yield successive distinct permutations of the elements in *iterable*. + + >>> sorted(distinct_permutations([1, 0, 1])) + [(0, 1, 1), (1, 0, 1), (1, 1, 0)] + + Equivalent to ``set(permutations(iterable))``, except duplicates are not + generated and thrown away. For larger input sequences this is much more + efficient. + + Duplicate permutations arise when there are duplicated elements in the + input iterable. The number of items returned is + `n! / (x_1! * x_2! * ... * x_n!)`, where `n` is the total number of + items input, and each `x_i` is the count of a distinct item in the input + sequence. + + """ + def perm_unique_helper(item_counts, perm, i): + """Internal helper function + + :arg item_counts: Stores the unique items in ``iterable`` and how many + times they are repeated + :arg perm: The permutation that is being built for output + :arg i: The index of the permutation being modified + + The output permutations are built up recursively; the distinct items + are placed until their repetitions are exhausted. + """ + if i < 0: + yield tuple(perm) + else: + for item in item_counts: + if item_counts[item] <= 0: + continue + perm[i] = item + item_counts[item] -= 1 + for x in perm_unique_helper(item_counts, perm, i - 1): + yield x + item_counts[item] += 1 + + item_counts = Counter(iterable) + length = sum(item_counts.values()) + + return perm_unique_helper(item_counts, [None] * length, length - 1) + + +def intersperse(e, iterable, n=1): + """Intersperse filler element *e* among the items in *iterable*, leaving + *n* items between each filler element. + + >>> list(intersperse('!', [1, 2, 3, 4, 5])) + [1, '!', 2, '!', 3, '!', 4, '!', 5] + + >>> list(intersperse(None, [1, 2, 3, 4, 5], n=2)) + [1, 2, None, 3, 4, None, 5] + + """ + if n == 0: + raise ValueError('n must be > 0') + elif n == 1: + # interleave(repeat(e), iterable) -> e, x_0, e, e, x_1, e, x_2... + # islice(..., 1, None) -> x_0, e, e, x_1, e, x_2... + return islice(interleave(repeat(e), iterable), 1, None) + else: + # interleave(filler, chunks) -> [e], [x_0, x_1], [e], [x_2, x_3]... + # islice(..., 1, None) -> [x_0, x_1], [e], [x_2, x_3]... + # flatten(...) -> x_0, x_1, e, x_2, x_3... + filler = repeat([e]) + chunks = chunked(iterable, n) + return flatten(islice(interleave(filler, chunks), 1, None)) + + +def unique_to_each(*iterables): + """Return the elements from each of the input iterables that aren't in the + other input iterables. + + For example, suppose you have a set of packages, each with a set of + dependencies:: + + {'pkg_1': {'A', 'B'}, 'pkg_2': {'B', 'C'}, 'pkg_3': {'B', 'D'}} + + If you remove one package, which dependencies can also be removed? + + If ``pkg_1`` is removed, then ``A`` is no longer necessary - it is not + associated with ``pkg_2`` or ``pkg_3``. Similarly, ``C`` is only needed for + ``pkg_2``, and ``D`` is only needed for ``pkg_3``:: + + >>> unique_to_each({'A', 'B'}, {'B', 'C'}, {'B', 'D'}) + [['A'], ['C'], ['D']] + + If there are duplicates in one input iterable that aren't in the others + they will be duplicated in the output. Input order is preserved:: + + >>> unique_to_each("mississippi", "missouri") + [['p', 'p'], ['o', 'u', 'r']] + + It is assumed that the elements of each iterable are hashable. + + """ + pool = [list(it) for it in iterables] + counts = Counter(chain.from_iterable(map(set, pool))) + uniques = {element for element in counts if counts[element] == 1} + return [list(filter(uniques.__contains__, it)) for it in pool] + + +def windowed(seq, n, fillvalue=None, step=1): + """Return a sliding window of width *n* over the given iterable. + + >>> all_windows = windowed([1, 2, 3, 4, 5], 3) + >>> list(all_windows) + [(1, 2, 3), (2, 3, 4), (3, 4, 5)] + + When the window is larger than the iterable, *fillvalue* is used in place + of missing values:: + + >>> list(windowed([1, 2, 3], 4)) + [(1, 2, 3, None)] + + Each window will advance in increments of *step*: + + >>> list(windowed([1, 2, 3, 4, 5, 6], 3, fillvalue='!', step=2)) + [(1, 2, 3), (3, 4, 5), (5, 6, '!')] + + """ + if n < 0: + raise ValueError('n must be >= 0') + if n == 0: + yield tuple() + return + if step < 1: + raise ValueError('step must be >= 1') + + it = iter(seq) + window = deque([], n) + append = window.append + + # Initial deque fill + for _ in range(n): + append(next(it, fillvalue)) + yield tuple(window) + + # Appending new items to the right causes old items to fall off the left + i = 0 + for item in it: + append(item) + i = (i + 1) % step + if i % step == 0: + yield tuple(window) + + # If there are items from the iterable in the window, pad with the given + # value and emit them. + if (i % step) and (step - i < n): + for _ in range(step - i): + append(fillvalue) + yield tuple(window) + + +class bucket(object): + """Wrap *iterable* and return an object that buckets it iterable into + child iterables based on a *key* function. + + >>> iterable = ['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'b3'] + >>> s = bucket(iterable, key=lambda x: x[0]) + >>> a_iterable = s['a'] + >>> next(a_iterable) + 'a1' + >>> next(a_iterable) + 'a2' + >>> list(s['b']) + ['b1', 'b2', 'b3'] + + The original iterable will be advanced and its items will be cached until + they are used by the child iterables. This may require significant storage. + + By default, attempting to select a bucket to which no items belong will + exhaust the iterable and cache all values. + If you specify a *validator* function, selected buckets will instead be + checked against it. + + >>> from itertools import count + >>> it = count(1, 2) # Infinite sequence of odd numbers + >>> key = lambda x: x % 10 # Bucket by last digit + >>> validator = lambda x: x in {1, 3, 5, 7, 9} # Odd digits only + >>> s = bucket(it, key=key, validator=validator) + >>> 2 in s + False + >>> list(s[2]) + [] + + """ + def __init__(self, iterable, key, validator=None): + self._it = iter(iterable) + self._key = key + self._cache = defaultdict(deque) + self._validator = validator or (lambda x: True) + + def __contains__(self, value): + if not self._validator(value): + return False + + try: + item = next(self[value]) + except StopIteration: + return False + else: + self._cache[value].appendleft(item) + + return True + + def _get_values(self, value): + """ + Helper to yield items from the parent iterator that match *value*. + Items that don't match are stored in the local cache as they + are encountered. + """ + while True: + # If we've cached some items that match the target value, emit + # the first one and evict it from the cache. + if self._cache[value]: + yield self._cache[value].popleft() + # Otherwise we need to advance the parent iterator to search for + # a matching item, caching the rest. + else: + while True: + try: + item = next(self._it) + except StopIteration: + return + item_value = self._key(item) + if item_value == value: + yield item + break + elif self._validator(item_value): + self._cache[item_value].append(item) + + def __getitem__(self, value): + if not self._validator(value): + return iter(()) + + return self._get_values(value) + + +def spy(iterable, n=1): + """Return a 2-tuple with a list containing the first *n* elements of + *iterable*, and an iterator with the same items as *iterable*. + This allows you to "look ahead" at the items in the iterable without + advancing it. + + There is one item in the list by default: + + >>> iterable = 'abcdefg' + >>> head, iterable = spy(iterable) + >>> head + ['a'] + >>> list(iterable) + ['a', 'b', 'c', 'd', 'e', 'f', 'g'] + + You may use unpacking to retrieve items instead of lists: + + >>> (head,), iterable = spy('abcdefg') + >>> head + 'a' + >>> (first, second), iterable = spy('abcdefg', 2) + >>> first + 'a' + >>> second + 'b' + + The number of items requested can be larger than the number of items in + the iterable: + + >>> iterable = [1, 2, 3, 4, 5] + >>> head, iterable = spy(iterable, 10) + >>> head + [1, 2, 3, 4, 5] + >>> list(iterable) + [1, 2, 3, 4, 5] + + """ + it = iter(iterable) + head = take(n, it) + + return head, chain(head, it) + + +def interleave(*iterables): + """Return a new iterable yielding from each iterable in turn, + until the shortest is exhausted. + + >>> list(interleave([1, 2, 3], [4, 5], [6, 7, 8])) + [1, 4, 6, 2, 5, 7] + + For a version that doesn't terminate after the shortest iterable is + exhausted, see :func:`interleave_longest`. + + """ + return chain.from_iterable(zip(*iterables)) + + +def interleave_longest(*iterables): + """Return a new iterable yielding from each iterable in turn, + skipping any that are exhausted. + + >>> list(interleave_longest([1, 2, 3], [4, 5], [6, 7, 8])) + [1, 4, 6, 2, 5, 7, 3, 8] + + This function produces the same output as :func:`roundrobin`, but may + perform better for some inputs (in particular when the number of iterables + is large). + + """ + i = chain.from_iterable(zip_longest(*iterables, fillvalue=_marker)) + return (x for x in i if x is not _marker) + + +def collapse(iterable, base_type=None, levels=None): + """Flatten an iterable with multiple levels of nesting (e.g., a list of + lists of tuples) into non-iterable types. + + >>> iterable = [(1, 2), ([3, 4], [[5], [6]])] + >>> list(collapse(iterable)) + [1, 2, 3, 4, 5, 6] + + String types are not considered iterable and will not be collapsed. + To avoid collapsing other types, specify *base_type*: + + >>> iterable = ['ab', ('cd', 'ef'), ['gh', 'ij']] + >>> list(collapse(iterable, base_type=tuple)) + ['ab', ('cd', 'ef'), 'gh', 'ij'] + + Specify *levels* to stop flattening after a certain level: + + >>> iterable = [('a', ['b']), ('c', ['d'])] + >>> list(collapse(iterable)) # Fully flattened + ['a', 'b', 'c', 'd'] + >>> list(collapse(iterable, levels=1)) # Only one level flattened + ['a', ['b'], 'c', ['d']] + + """ + def walk(node, level): + if ( + ((levels is not None) and (level > levels)) or + isinstance(node, string_types) or + ((base_type is not None) and isinstance(node, base_type)) + ): + yield node + return + + try: + tree = iter(node) + except TypeError: + yield node + return + else: + for child in tree: + for x in walk(child, level + 1): + yield x + + for x in walk(iterable, 0): + yield x + + +def side_effect(func, iterable, chunk_size=None, before=None, after=None): + """Invoke *func* on each item in *iterable* (or on each *chunk_size* group + of items) before yielding the item. + + `func` must be a function that takes a single argument. Its return value + will be discarded. + + *before* and *after* are optional functions that take no arguments. They + will be executed before iteration starts and after it ends, respectively. + + `side_effect` can be used for logging, updating progress bars, or anything + that is not functionally "pure." + + Emitting a status message: + + >>> from more_itertools import consume + >>> func = lambda item: print('Received {}'.format(item)) + >>> consume(side_effect(func, range(2))) + Received 0 + Received 1 + + Operating on chunks of items: + + >>> pair_sums = [] + >>> func = lambda chunk: pair_sums.append(sum(chunk)) + >>> list(side_effect(func, [0, 1, 2, 3, 4, 5], 2)) + [0, 1, 2, 3, 4, 5] + >>> list(pair_sums) + [1, 5, 9] + + Writing to a file-like object: + + >>> from io import StringIO + >>> from more_itertools import consume + >>> f = StringIO() + >>> func = lambda x: print(x, file=f) + >>> before = lambda: print(u'HEADER', file=f) + >>> after = f.close + >>> it = [u'a', u'b', u'c'] + >>> consume(side_effect(func, it, before=before, after=after)) + >>> f.closed + True + + """ + try: + if before is not None: + before() + + if chunk_size is None: + for item in iterable: + func(item) + yield item + else: + for chunk in chunked(iterable, chunk_size): + func(chunk) + for item in chunk: + yield item + finally: + if after is not None: + after() + + +def sliced(seq, n): + """Yield slices of length *n* from the sequence *seq*. + + >>> list(sliced((1, 2, 3, 4, 5, 6), 3)) + [(1, 2, 3), (4, 5, 6)] + + If the length of the sequence is not divisible by the requested slice + length, the last slice will be shorter. + + >>> list(sliced((1, 2, 3, 4, 5, 6, 7, 8), 3)) + [(1, 2, 3), (4, 5, 6), (7, 8)] + + This function will only work for iterables that support slicing. + For non-sliceable iterables, see :func:`chunked`. + + """ + return takewhile(bool, (seq[i: i + n] for i in count(0, n))) + + +def split_at(iterable, pred): + """Yield lists of items from *iterable*, where each list is delimited by + an item where callable *pred* returns ``True``. The lists do not include + the delimiting items. + + >>> list(split_at('abcdcba', lambda x: x == 'b')) + [['a'], ['c', 'd', 'c'], ['a']] + + >>> list(split_at(range(10), lambda n: n % 2 == 1)) + [[0], [2], [4], [6], [8], []] + """ + buf = [] + for item in iterable: + if pred(item): + yield buf + buf = [] + else: + buf.append(item) + yield buf + + +def split_before(iterable, pred): + """Yield lists of items from *iterable*, where each list starts with an + item where callable *pred* returns ``True``: + + >>> list(split_before('OneTwo', lambda s: s.isupper())) + [['O', 'n', 'e'], ['T', 'w', 'o']] + + >>> list(split_before(range(10), lambda n: n % 3 == 0)) + [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] + + """ + buf = [] + for item in iterable: + if pred(item) and buf: + yield buf + buf = [] + buf.append(item) + yield buf + + +def split_after(iterable, pred): + """Yield lists of items from *iterable*, where each list ends with an + item where callable *pred* returns ``True``: + + >>> list(split_after('one1two2', lambda s: s.isdigit())) + [['o', 'n', 'e', '1'], ['t', 'w', 'o', '2']] + + >>> list(split_after(range(10), lambda n: n % 3 == 0)) + [[0], [1, 2, 3], [4, 5, 6], [7, 8, 9]] + + """ + buf = [] + for item in iterable: + buf.append(item) + if pred(item) and buf: + yield buf + buf = [] + if buf: + yield buf + + +def padded(iterable, fillvalue=None, n=None, next_multiple=False): + """Yield the elements from *iterable*, followed by *fillvalue*, such that + at least *n* items are emitted. + + >>> list(padded([1, 2, 3], '?', 5)) + [1, 2, 3, '?', '?'] + + If *next_multiple* is ``True``, *fillvalue* will be emitted until the + number of items emitted is a multiple of *n*:: + + >>> list(padded([1, 2, 3, 4], n=3, next_multiple=True)) + [1, 2, 3, 4, None, None] + + If *n* is ``None``, *fillvalue* will be emitted indefinitely. + + """ + it = iter(iterable) + if n is None: + for item in chain(it, repeat(fillvalue)): + yield item + elif n < 1: + raise ValueError('n must be at least 1') + else: + item_count = 0 + for item in it: + yield item + item_count += 1 + + remaining = (n - item_count) % n if next_multiple else n - item_count + for _ in range(remaining): + yield fillvalue + + +def distribute(n, iterable): + """Distribute the items from *iterable* among *n* smaller iterables. + + >>> group_1, group_2 = distribute(2, [1, 2, 3, 4, 5, 6]) + >>> list(group_1) + [1, 3, 5] + >>> list(group_2) + [2, 4, 6] + + If the length of *iterable* is not evenly divisible by *n*, then the + length of the returned iterables will not be identical: + + >>> children = distribute(3, [1, 2, 3, 4, 5, 6, 7]) + >>> [list(c) for c in children] + [[1, 4, 7], [2, 5], [3, 6]] + + If the length of *iterable* is smaller than *n*, then the last returned + iterables will be empty: + + >>> children = distribute(5, [1, 2, 3]) + >>> [list(c) for c in children] + [[1], [2], [3], [], []] + + This function uses :func:`itertools.tee` and may require significant + storage. If you need the order items in the smaller iterables to match the + original iterable, see :func:`divide`. + + """ + if n < 1: + raise ValueError('n must be at least 1') + + children = tee(iterable, n) + return [islice(it, index, None, n) for index, it in enumerate(children)] + + +def stagger(iterable, offsets=(-1, 0, 1), longest=False, fillvalue=None): + """Yield tuples whose elements are offset from *iterable*. + The amount by which the `i`-th item in each tuple is offset is given by + the `i`-th item in *offsets*. + + >>> list(stagger([0, 1, 2, 3])) + [(None, 0, 1), (0, 1, 2), (1, 2, 3)] + >>> list(stagger(range(8), offsets=(0, 2, 4))) + [(0, 2, 4), (1, 3, 5), (2, 4, 6), (3, 5, 7)] + + By default, the sequence will end when the final element of a tuple is the + last item in the iterable. To continue until the first element of a tuple + is the last item in the iterable, set *longest* to ``True``:: + + >>> list(stagger([0, 1, 2, 3], longest=True)) + [(None, 0, 1), (0, 1, 2), (1, 2, 3), (2, 3, None), (3, None, None)] + + By default, ``None`` will be used to replace offsets beyond the end of the + sequence. Specify *fillvalue* to use some other value. + + """ + children = tee(iterable, len(offsets)) + + return zip_offset( + *children, offsets=offsets, longest=longest, fillvalue=fillvalue + ) + + +def zip_offset(*iterables, **kwargs): + """``zip`` the input *iterables* together, but offset the `i`-th iterable + by the `i`-th item in *offsets*. + + >>> list(zip_offset('0123', 'abcdef', offsets=(0, 1))) + [('0', 'b'), ('1', 'c'), ('2', 'd'), ('3', 'e')] + + This can be used as a lightweight alternative to SciPy or pandas to analyze + data sets in which somes series have a lead or lag relationship. + + By default, the sequence will end when the shortest iterable is exhausted. + To continue until the longest iterable is exhausted, set *longest* to + ``True``. + + >>> list(zip_offset('0123', 'abcdef', offsets=(0, 1), longest=True)) + [('0', 'b'), ('1', 'c'), ('2', 'd'), ('3', 'e'), (None, 'f')] + + By default, ``None`` will be used to replace offsets beyond the end of the + sequence. Specify *fillvalue* to use some other value. + + """ + offsets = kwargs['offsets'] + longest = kwargs.get('longest', False) + fillvalue = kwargs.get('fillvalue', None) + + if len(iterables) != len(offsets): + raise ValueError("Number of iterables and offsets didn't match") + + staggered = [] + for it, n in zip(iterables, offsets): + if n < 0: + staggered.append(chain(repeat(fillvalue, -n), it)) + elif n > 0: + staggered.append(islice(it, n, None)) + else: + staggered.append(it) + + if longest: + return zip_longest(*staggered, fillvalue=fillvalue) + + return zip(*staggered) + + +def sort_together(iterables, key_list=(0,), reverse=False): + """Return the input iterables sorted together, with *key_list* as the + priority for sorting. All iterables are trimmed to the length of the + shortest one. + + This can be used like the sorting function in a spreadsheet. If each + iterable represents a column of data, the key list determines which + columns are used for sorting. + + By default, all iterables are sorted using the ``0``-th iterable:: + + >>> iterables = [(4, 3, 2, 1), ('a', 'b', 'c', 'd')] + >>> sort_together(iterables) + [(1, 2, 3, 4), ('d', 'c', 'b', 'a')] + + Set a different key list to sort according to another iterable. + Specifying mutliple keys dictates how ties are broken:: + + >>> iterables = [(3, 1, 2), (0, 1, 0), ('c', 'b', 'a')] + >>> sort_together(iterables, key_list=(1, 2)) + [(2, 3, 1), (0, 0, 1), ('a', 'c', 'b')] + + Set *reverse* to ``True`` to sort in descending order. + + >>> sort_together([(1, 2, 3), ('c', 'b', 'a')], reverse=True) + [(3, 2, 1), ('a', 'b', 'c')] + + """ + return list(zip(*sorted(zip(*iterables), + key=itemgetter(*key_list), + reverse=reverse))) + + +def divide(n, iterable): + """Divide the elements from *iterable* into *n* parts, maintaining + order. + + >>> group_1, group_2 = divide(2, [1, 2, 3, 4, 5, 6]) + >>> list(group_1) + [1, 2, 3] + >>> list(group_2) + [4, 5, 6] + + If the length of *iterable* is not evenly divisible by *n*, then the + length of the returned iterables will not be identical: + + >>> children = divide(3, [1, 2, 3, 4, 5, 6, 7]) + >>> [list(c) for c in children] + [[1, 2, 3], [4, 5], [6, 7]] + + If the length of the iterable is smaller than n, then the last returned + iterables will be empty: + + >>> children = divide(5, [1, 2, 3]) + >>> [list(c) for c in children] + [[1], [2], [3], [], []] + + This function will exhaust the iterable before returning and may require + significant storage. If order is not important, see :func:`distribute`, + which does not first pull the iterable into memory. + + """ + if n < 1: + raise ValueError('n must be at least 1') + + seq = tuple(iterable) + q, r = divmod(len(seq), n) + + ret = [] + for i in range(n): + start = (i * q) + (i if i < r else r) + stop = ((i + 1) * q) + (i + 1 if i + 1 < r else r) + ret.append(iter(seq[start:stop])) + + return ret + + +def always_iterable(obj, base_type=(text_type, binary_type)): + """If *obj* is iterable, return an iterator over its items:: + + >>> obj = (1, 2, 3) + >>> list(always_iterable(obj)) + [1, 2, 3] + + If *obj* is not iterable, return a one-item iterable containing *obj*:: + + >>> obj = 1 + >>> list(always_iterable(obj)) + [1] + + If *obj* is ``None``, return an empty iterable: + + >>> obj = None + >>> list(always_iterable(None)) + [] + + By default, binary and text strings are not considered iterable:: + + >>> obj = 'foo' + >>> list(always_iterable(obj)) + ['foo'] + + If *base_type* is set, objects for which ``isinstance(obj, base_type)`` + returns ``True`` won't be considered iterable. + + >>> obj = {'a': 1} + >>> list(always_iterable(obj)) # Iterate over the dict's keys + ['a'] + >>> list(always_iterable(obj, base_type=dict)) # Treat dicts as a unit + [{'a': 1}] + + Set *base_type* to ``None`` to avoid any special handling and treat objects + Python considers iterable as iterable: + + >>> obj = 'foo' + >>> list(always_iterable(obj, base_type=None)) + ['f', 'o', 'o'] + """ + if obj is None: + return iter(()) + + if (base_type is not None) and isinstance(obj, base_type): + return iter((obj,)) + + try: + return iter(obj) + except TypeError: + return iter((obj,)) + + +def adjacent(predicate, iterable, distance=1): + """Return an iterable over `(bool, item)` tuples where the `item` is + drawn from *iterable* and the `bool` indicates whether + that item satisfies the *predicate* or is adjacent to an item that does. + + For example, to find whether items are adjacent to a ``3``:: + + >>> list(adjacent(lambda x: x == 3, range(6))) + [(False, 0), (False, 1), (True, 2), (True, 3), (True, 4), (False, 5)] + + Set *distance* to change what counts as adjacent. For example, to find + whether items are two places away from a ``3``: + + >>> list(adjacent(lambda x: x == 3, range(6), distance=2)) + [(False, 0), (True, 1), (True, 2), (True, 3), (True, 4), (True, 5)] + + This is useful for contextualizing the results of a search function. + For example, a code comparison tool might want to identify lines that + have changed, but also surrounding lines to give the viewer of the diff + context. + + The predicate function will only be called once for each item in the + iterable. + + See also :func:`groupby_transform`, which can be used with this function + to group ranges of items with the same `bool` value. + + """ + # Allow distance=0 mainly for testing that it reproduces results with map() + if distance < 0: + raise ValueError('distance must be at least 0') + + i1, i2 = tee(iterable) + padding = [False] * distance + selected = chain(padding, map(predicate, i1), padding) + adjacent_to_selected = map(any, windowed(selected, 2 * distance + 1)) + return zip(adjacent_to_selected, i2) + + +def groupby_transform(iterable, keyfunc=None, valuefunc=None): + """An extension of :func:`itertools.groupby` that transforms the values of + *iterable* after grouping them. + *keyfunc* is a function used to compute a grouping key for each item. + *valuefunc* is a function for transforming the items after grouping. + + >>> iterable = 'AaaABbBCcA' + >>> keyfunc = lambda x: x.upper() + >>> valuefunc = lambda x: x.lower() + >>> grouper = groupby_transform(iterable, keyfunc, valuefunc) + >>> [(k, ''.join(g)) for k, g in grouper] + [('A', 'aaaa'), ('B', 'bbb'), ('C', 'cc'), ('A', 'a')] + + *keyfunc* and *valuefunc* default to identity functions if they are not + specified. + + :func:`groupby_transform` is useful when grouping elements of an iterable + using a separate iterable as the key. To do this, :func:`zip` the iterables + and pass a *keyfunc* that extracts the first element and a *valuefunc* + that extracts the second element:: + + >>> from operator import itemgetter + >>> keys = [0, 0, 1, 1, 1, 2, 2, 2, 3] + >>> values = 'abcdefghi' + >>> iterable = zip(keys, values) + >>> grouper = groupby_transform(iterable, itemgetter(0), itemgetter(1)) + >>> [(k, ''.join(g)) for k, g in grouper] + [(0, 'ab'), (1, 'cde'), (2, 'fgh'), (3, 'i')] + + Note that the order of items in the iterable is significant. + Only adjacent items are grouped together, so if you don't want any + duplicate groups, you should sort the iterable by the key function. + + """ + valuefunc = (lambda x: x) if valuefunc is None else valuefunc + return ((k, map(valuefunc, g)) for k, g in groupby(iterable, keyfunc)) + + +def numeric_range(*args): + """An extension of the built-in ``range()`` function whose arguments can + be any orderable numeric type. + + With only *stop* specified, *start* defaults to ``0`` and *step* + defaults to ``1``. The output items will match the type of *stop*: + + >>> list(numeric_range(3.5)) + [0.0, 1.0, 2.0, 3.0] + + With only *start* and *stop* specified, *step* defaults to ``1``. The + output items will match the type of *start*: + + >>> from decimal import Decimal + >>> start = Decimal('2.1') + >>> stop = Decimal('5.1') + >>> list(numeric_range(start, stop)) + [Decimal('2.1'), Decimal('3.1'), Decimal('4.1')] + + With *start*, *stop*, and *step* specified the output items will match + the type of ``start + step``: + + >>> from fractions import Fraction + >>> start = Fraction(1, 2) # Start at 1/2 + >>> stop = Fraction(5, 2) # End at 5/2 + >>> step = Fraction(1, 2) # Count by 1/2 + >>> list(numeric_range(start, stop, step)) + [Fraction(1, 2), Fraction(1, 1), Fraction(3, 2), Fraction(2, 1)] + + If *step* is zero, ``ValueError`` is raised. Negative steps are supported: + + >>> list(numeric_range(3, -1, -1.0)) + [3.0, 2.0, 1.0, 0.0] + + Be aware of the limitations of floating point numbers; the representation + of the yielded numbers may be surprising. + + """ + argc = len(args) + if argc == 1: + stop, = args + start = type(stop)(0) + step = 1 + elif argc == 2: + start, stop = args + step = 1 + elif argc == 3: + start, stop, step = args + else: + err_msg = 'numeric_range takes at most 3 arguments, got {}' + raise TypeError(err_msg.format(argc)) + + values = (start + (step * n) for n in count()) + if step > 0: + return takewhile(partial(gt, stop), values) + elif step < 0: + return takewhile(partial(lt, stop), values) + else: + raise ValueError('numeric_range arg 3 must not be zero') + + +def count_cycle(iterable, n=None): + """Cycle through the items from *iterable* up to *n* times, yielding + the number of completed cycles along with each item. If *n* is omitted the + process repeats indefinitely. + + >>> list(count_cycle('AB', 3)) + [(0, 'A'), (0, 'B'), (1, 'A'), (1, 'B'), (2, 'A'), (2, 'B')] + + """ + iterable = tuple(iterable) + if not iterable: + return iter(()) + counter = count() if n is None else range(n) + return ((i, item) for i in counter for item in iterable) + + +def locate(iterable, pred=bool): + """Yield the index of each item in *iterable* for which *pred* returns + ``True``. + + *pred* defaults to :func:`bool`, which will select truthy items: + + >>> list(locate([0, 1, 1, 0, 1, 0, 0])) + [1, 2, 4] + + Set *pred* to a custom function to, e.g., find the indexes for a particular + item: + + >>> list(locate(['a', 'b', 'c', 'b'], lambda x: x == 'b')) + [1, 3] + + Use with :func:`windowed` to find the indexes of a sub-sequence: + + >>> from more_itertools import windowed + >>> iterable = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] + >>> sub = [1, 2, 3] + >>> pred = lambda w: w == tuple(sub) # windowed() returns tuples + >>> list(locate(windowed(iterable, len(sub)), pred=pred)) + [1, 5, 9] + + Use with :func:`seekable` to find indexes and then retrieve the associated + items: + + >>> from itertools import count + >>> from more_itertools import seekable + >>> source = (3 * n + 1 if (n % 2) else n // 2 for n in count()) + >>> it = seekable(source) + >>> pred = lambda x: x > 100 + >>> indexes = locate(it, pred=pred) + >>> i = next(indexes) + >>> it.seek(i) + >>> next(it) + 106 + + """ + return compress(count(), map(pred, iterable)) + + +def lstrip(iterable, pred): + """Yield the items from *iterable*, but strip any from the beginning + for which *pred* returns ``True``. + + For example, to remove a set of items from the start of an iterable: + + >>> iterable = (None, False, None, 1, 2, None, 3, False, None) + >>> pred = lambda x: x in {None, False, ''} + >>> list(lstrip(iterable, pred)) + [1, 2, None, 3, False, None] + + This function is analogous to to :func:`str.lstrip`, and is essentially + an wrapper for :func:`itertools.dropwhile`. + + """ + return dropwhile(pred, iterable) + + +def rstrip(iterable, pred): + """Yield the items from *iterable*, but strip any from the end + for which *pred* returns ``True``. + + For example, to remove a set of items from the end of an iterable: + + >>> iterable = (None, False, None, 1, 2, None, 3, False, None) + >>> pred = lambda x: x in {None, False, ''} + >>> list(rstrip(iterable, pred)) + [None, False, None, 1, 2, None, 3] + + This function is analogous to :func:`str.rstrip`. + + """ + cache = [] + cache_append = cache.append + for x in iterable: + if pred(x): + cache_append(x) + else: + for y in cache: + yield y + del cache[:] + yield x + + +def strip(iterable, pred): + """Yield the items from *iterable*, but strip any from the + beginning and end for which *pred* returns ``True``. + + For example, to remove a set of items from both ends of an iterable: + + >>> iterable = (None, False, None, 1, 2, None, 3, False, None) + >>> pred = lambda x: x in {None, False, ''} + >>> list(strip(iterable, pred)) + [1, 2, None, 3] + + This function is analogous to :func:`str.strip`. + + """ + return rstrip(lstrip(iterable, pred), pred) + + +def islice_extended(iterable, *args): + """An extension of :func:`itertools.islice` that supports negative values + for *stop*, *start*, and *step*. + + >>> iterable = iter('abcdefgh') + >>> list(islice_extended(iterable, -4, -1)) + ['e', 'f', 'g'] + + Slices with negative values require some caching of *iterable*, but this + function takes care to minimize the amount of memory required. + + For example, you can use a negative step with an infinite iterator: + + >>> from itertools import count + >>> list(islice_extended(count(), 110, 99, -2)) + [110, 108, 106, 104, 102, 100] + + """ + s = slice(*args) + start = s.start + stop = s.stop + if s.step == 0: + raise ValueError('step argument must be a non-zero integer or None.') + step = s.step or 1 + + it = iter(iterable) + + if step > 0: + start = 0 if (start is None) else start + + if (start < 0): + # Consume all but the last -start items + cache = deque(enumerate(it, 1), maxlen=-start) + len_iter = cache[-1][0] if cache else 0 + + # Adjust start to be positive + i = max(len_iter + start, 0) + + # Adjust stop to be positive + if stop is None: + j = len_iter + elif stop >= 0: + j = min(stop, len_iter) + else: + j = max(len_iter + stop, 0) + + # Slice the cache + n = j - i + if n <= 0: + return + + for index, item in islice(cache, 0, n, step): + yield item + elif (stop is not None) and (stop < 0): + # Advance to the start position + next(islice(it, start, start), None) + + # When stop is negative, we have to carry -stop items while + # iterating + cache = deque(islice(it, -stop), maxlen=-stop) + + for index, item in enumerate(it): + cached_item = cache.popleft() + if index % step == 0: + yield cached_item + cache.append(item) + else: + # When both start and stop are positive we have the normal case + for item in islice(it, start, stop, step): + yield item + else: + start = -1 if (start is None) else start + + if (stop is not None) and (stop < 0): + # Consume all but the last items + n = -stop - 1 + cache = deque(enumerate(it, 1), maxlen=n) + len_iter = cache[-1][0] if cache else 0 + + # If start and stop are both negative they are comparable and + # we can just slice. Otherwise we can adjust start to be negative + # and then slice. + if start < 0: + i, j = start, stop + else: + i, j = min(start - len_iter, -1), None + + for index, item in list(cache)[i:j:step]: + yield item + else: + # Advance to the stop position + if stop is not None: + m = stop + 1 + next(islice(it, m, m), None) + + # stop is positive, so if start is negative they are not comparable + # and we need the rest of the items. + if start < 0: + i = start + n = None + # stop is None and start is positive, so we just need items up to + # the start index. + elif stop is None: + i = None + n = start + 1 + # Both stop and start are positive, so they are comparable. + else: + i = None + n = start - stop + if n <= 0: + return + + cache = list(islice(it, n)) + + for item in cache[i::step]: + yield item + + +def always_reversible(iterable): + """An extension of :func:`reversed` that supports all iterables, not + just those which implement the ``Reversible`` or ``Sequence`` protocols. + + >>> print(*always_reversible(x for x in range(3))) + 2 1 0 + + If the iterable is already reversible, this function returns the + result of :func:`reversed()`. If the iterable is not reversible, + this function will cache the remaining items in the iterable and + yield them in reverse order, which may require significant storage. + """ + try: + return reversed(iterable) + except TypeError: + return reversed(list(iterable)) + + +def consecutive_groups(iterable, ordering=lambda x: x): + """Yield groups of consecutive items using :func:`itertools.groupby`. + The *ordering* function determines whether two items are adjacent by + returning their position. + + By default, the ordering function is the identity function. This is + suitable for finding runs of numbers: + + >>> iterable = [1, 10, 11, 12, 20, 30, 31, 32, 33, 40] + >>> for group in consecutive_groups(iterable): + ... print(list(group)) + [1] + [10, 11, 12] + [20] + [30, 31, 32, 33] + [40] + + For finding runs of adjacent letters, try using the :meth:`index` method + of a string of letters: + + >>> from string import ascii_lowercase + >>> iterable = 'abcdfgilmnop' + >>> ordering = ascii_lowercase.index + >>> for group in consecutive_groups(iterable, ordering): + ... print(list(group)) + ['a', 'b', 'c', 'd'] + ['f', 'g'] + ['i'] + ['l', 'm', 'n', 'o', 'p'] + + """ + for k, g in groupby( + enumerate(iterable), key=lambda x: x[0] - ordering(x[1]) + ): + yield map(itemgetter(1), g) + + +def difference(iterable, func=sub): + """By default, compute the first difference of *iterable* using + :func:`operator.sub`. + + >>> iterable = [0, 1, 3, 6, 10] + >>> list(difference(iterable)) + [0, 1, 2, 3, 4] + + This is the opposite of :func:`accumulate`'s default behavior: + + >>> from more_itertools import accumulate + >>> iterable = [0, 1, 2, 3, 4] + >>> list(accumulate(iterable)) + [0, 1, 3, 6, 10] + >>> list(difference(accumulate(iterable))) + [0, 1, 2, 3, 4] + + By default *func* is :func:`operator.sub`, but other functions can be + specified. They will be applied as follows:: + + A, B, C, D, ... --> A, func(B, A), func(C, B), func(D, C), ... + + For example, to do progressive division: + + >>> iterable = [1, 2, 6, 24, 120] # Factorial sequence + >>> func = lambda x, y: x // y + >>> list(difference(iterable, func)) + [1, 2, 3, 4, 5] + + """ + a, b = tee(iterable) + try: + item = next(b) + except StopIteration: + return iter([]) + return chain([item], map(lambda x: func(x[1], x[0]), zip(a, b))) + + +class SequenceView(Sequence): + """Return a read-only view of the sequence object *target*. + + :class:`SequenceView` objects are analagous to Python's built-in + "dictionary view" types. They provide a dynamic view of a sequence's items, + meaning that when the sequence updates, so does the view. + + >>> seq = ['0', '1', '2'] + >>> view = SequenceView(seq) + >>> view + SequenceView(['0', '1', '2']) + >>> seq.append('3') + >>> view + SequenceView(['0', '1', '2', '3']) + + Sequence views support indexing, slicing, and length queries. They act + like the underlying sequence, except they don't allow assignment: + + >>> view[1] + '1' + >>> view[1:-1] + ['1', '2'] + >>> len(view) + 4 + + Sequence views are useful as an alternative to copying, as they don't + require (much) extra storage. + + """ + def __init__(self, target): + if not isinstance(target, Sequence): + raise TypeError + self._target = target + + def __getitem__(self, index): + return self._target[index] + + def __len__(self): + return len(self._target) + + def __repr__(self): + return '{}({})'.format(self.__class__.__name__, repr(self._target)) + + +class seekable(object): + """Wrap an iterator to allow for seeking backward and forward. This + progressively caches the items in the source iterable so they can be + re-visited. + + Call :meth:`seek` with an index to seek to that position in the source + iterable. + + To "reset" an iterator, seek to ``0``: + + >>> from itertools import count + >>> it = seekable((str(n) for n in count())) + >>> next(it), next(it), next(it) + ('0', '1', '2') + >>> it.seek(0) + >>> next(it), next(it), next(it) + ('0', '1', '2') + >>> next(it) + '3' + + You can also seek forward: + + >>> it = seekable((str(n) for n in range(20))) + >>> it.seek(10) + >>> next(it) + '10' + >>> it.seek(20) # Seeking past the end of the source isn't a problem + >>> list(it) + [] + >>> it.seek(0) # Resetting works even after hitting the end + >>> next(it), next(it), next(it) + ('0', '1', '2') + + The cache grows as the source iterable progresses, so beware of wrapping + very large or infinite iterables. + + You may view the contents of the cache with the :meth:`elements` method. + That returns a :class:`SequenceView`, a view that updates automatically: + + >>> it = seekable((str(n) for n in range(10))) + >>> next(it), next(it), next(it) + ('0', '1', '2') + >>> elements = it.elements() + >>> elements + SequenceView(['0', '1', '2']) + >>> next(it) + '3' + >>> elements + SequenceView(['0', '1', '2', '3']) + + """ + + def __init__(self, iterable): + self._source = iter(iterable) + self._cache = [] + self._index = None + + def __iter__(self): + return self + + def __next__(self): + if self._index is not None: + try: + item = self._cache[self._index] + except IndexError: + self._index = None + else: + self._index += 1 + return item + + item = next(self._source) + self._cache.append(item) + return item + + next = __next__ + + def elements(self): + return SequenceView(self._cache) + + def seek(self, index): + self._index = index + remainder = index - len(self._cache) + if remainder > 0: + consume(self, remainder) + + +class run_length(object): + """ + :func:`run_length.encode` compresses an iterable with run-length encoding. + It yields groups of repeated items with the count of how many times they + were repeated: + + >>> uncompressed = 'abbcccdddd' + >>> list(run_length.encode(uncompressed)) + [('a', 1), ('b', 2), ('c', 3), ('d', 4)] + + :func:`run_length.decode` decompresses an iterable that was previously + compressed with run-length encoding. It yields the items of the + decompressed iterable: + + >>> compressed = [('a', 1), ('b', 2), ('c', 3), ('d', 4)] + >>> list(run_length.decode(compressed)) + ['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd'] + + """ + + @staticmethod + def encode(iterable): + return ((k, ilen(g)) for k, g in groupby(iterable)) + + @staticmethod + def decode(iterable): + return chain.from_iterable(repeat(k, n) for k, n in iterable) + + +def exactly_n(iterable, n, predicate=bool): + """Return ``True`` if exactly ``n`` items in the iterable are ``True`` + according to the *predicate* function. + + >>> exactly_n([True, True, False], 2) + True + >>> exactly_n([True, True, False], 1) + False + >>> exactly_n([0, 1, 2, 3, 4, 5], 3, lambda x: x < 3) + True + + The iterable will be advanced until ``n + 1`` truthy items are encountered, + so avoid calling it on infinite iterables. + + """ + return len(take(n + 1, filter(predicate, iterable))) == n + + +def circular_shifts(iterable): + """Return a list of circular shifts of *iterable*. + + >>> circular_shifts(range(4)) + [(0, 1, 2, 3), (1, 2, 3, 0), (2, 3, 0, 1), (3, 0, 1, 2)] + """ + lst = list(iterable) + return take(len(lst), windowed(cycle(lst), len(lst))) + + +def make_decorator(wrapping_func, result_index=0): + """Return a decorator version of *wrapping_func*, which is a function that + modifies an iterable. *result_index* is the position in that function's + signature where the iterable goes. + + This lets you use itertools on the "production end," i.e. at function + definition. This can augment what the function returns without changing the + function's code. + + For example, to produce a decorator version of :func:`chunked`: + + >>> from more_itertools import chunked + >>> chunker = make_decorator(chunked, result_index=0) + >>> @chunker(3) + ... def iter_range(n): + ... return iter(range(n)) + ... + >>> list(iter_range(9)) + [[0, 1, 2], [3, 4, 5], [6, 7, 8]] + + To only allow truthy items to be returned: + + >>> truth_serum = make_decorator(filter, result_index=1) + >>> @truth_serum(bool) + ... def boolean_test(): + ... return [0, 1, '', ' ', False, True] + ... + >>> list(boolean_test()) + [1, ' ', True] + + The :func:`peekable` and :func:`seekable` wrappers make for practical + decorators: + + >>> from more_itertools import peekable + >>> peekable_function = make_decorator(peekable) + >>> @peekable_function() + ... def str_range(*args): + ... return (str(x) for x in range(*args)) + ... + >>> it = str_range(1, 20, 2) + >>> next(it), next(it), next(it) + ('1', '3', '5') + >>> it.peek() + '7' + >>> next(it) + '7' + + """ + # See https://sites.google.com/site/bbayles/index/decorator_factory for + # notes on how this works. + def decorator(*wrapping_args, **wrapping_kwargs): + def outer_wrapper(f): + def inner_wrapper(*args, **kwargs): + result = f(*args, **kwargs) + wrapping_args_ = list(wrapping_args) + wrapping_args_.insert(result_index, result) + return wrapping_func(*wrapping_args_, **wrapping_kwargs) + + return inner_wrapper + + return outer_wrapper + + return decorator + + +def map_reduce(iterable, keyfunc, valuefunc=None, reducefunc=None): + """Return a dictionary that maps the items in *iterable* to categories + defined by *keyfunc*, transforms them with *valuefunc*, and + then summarizes them by category with *reducefunc*. + + *valuefunc* defaults to the identity function if it is unspecified. + If *reducefunc* is unspecified, no summarization takes place: + + >>> keyfunc = lambda x: x.upper() + >>> result = map_reduce('abbccc', keyfunc) + >>> sorted(result.items()) + [('A', ['a']), ('B', ['b', 'b']), ('C', ['c', 'c', 'c'])] + + Specifying *valuefunc* transforms the categorized items: + + >>> keyfunc = lambda x: x.upper() + >>> valuefunc = lambda x: 1 + >>> result = map_reduce('abbccc', keyfunc, valuefunc) + >>> sorted(result.items()) + [('A', [1]), ('B', [1, 1]), ('C', [1, 1, 1])] + + Specifying *reducefunc* summarizes the categorized items: + + >>> keyfunc = lambda x: x.upper() + >>> valuefunc = lambda x: 1 + >>> reducefunc = sum + >>> result = map_reduce('abbccc', keyfunc, valuefunc, reducefunc) + >>> sorted(result.items()) + [('A', 1), ('B', 2), ('C', 3)] + + You may want to filter the input iterable before applying the map/reduce + proecdure: + + >>> all_items = range(30) + >>> items = [x for x in all_items if 10 <= x <= 20] # Filter + >>> keyfunc = lambda x: x % 2 # Evens map to 0; odds to 1 + >>> categories = map_reduce(items, keyfunc=keyfunc) + >>> sorted(categories.items()) + [(0, [10, 12, 14, 16, 18, 20]), (1, [11, 13, 15, 17, 19])] + >>> summaries = map_reduce(items, keyfunc=keyfunc, reducefunc=sum) + >>> sorted(summaries.items()) + [(0, 90), (1, 75)] + + Note that all items in the iterable are gathered into a list before the + summarization step, which may require significant storage. + + The returned object is a :obj:`collections.defaultdict` with the + ``default_factory`` set to ``None``, such that it behaves like a normal + dictionary. + + """ + valuefunc = (lambda x: x) if (valuefunc is None) else valuefunc + + ret = defaultdict(list) + for item in iterable: + key = keyfunc(item) + value = valuefunc(item) + ret[key].append(value) + + if reducefunc is not None: + for key, value_list in ret.items(): + ret[key] = reducefunc(value_list) + + ret.default_factory = None + return ret diff --git a/third_party/python/more-itertools/more_itertools/recipes.py b/third_party/python/more-itertools/more_itertools/recipes.py new file mode 100644 index 000000000000..3a7706cb91f0 --- /dev/null +++ b/third_party/python/more-itertools/more_itertools/recipes.py @@ -0,0 +1,565 @@ +"""Imported from the recipes section of the itertools documentation. + +All functions taken from the recipes section of the itertools library docs +[1]_. +Some backward-compatible usability improvements have been made. + +.. [1] http://docs.python.org/library/itertools.html#recipes + +""" +from collections import deque +from itertools import ( + chain, combinations, count, cycle, groupby, islice, repeat, starmap, tee +) +import operator +from random import randrange, sample, choice + +from six import PY2 +from six.moves import filter, filterfalse, map, range, zip, zip_longest + +__all__ = [ + 'accumulate', + 'all_equal', + 'consume', + 'dotproduct', + 'first_true', + 'flatten', + 'grouper', + 'iter_except', + 'ncycles', + 'nth', + 'nth_combination', + 'padnone', + 'pairwise', + 'partition', + 'powerset', + 'prepend', + 'quantify', + 'random_combination_with_replacement', + 'random_combination', + 'random_permutation', + 'random_product', + 'repeatfunc', + 'roundrobin', + 'tabulate', + 'tail', + 'take', + 'unique_everseen', + 'unique_justseen', +] + + +def accumulate(iterable, func=operator.add): + """ + Return an iterator whose items are the accumulated results of a function + (specified by the optional *func* argument) that takes two arguments. + By default, returns accumulated sums with :func:`operator.add`. + + >>> list(accumulate([1, 2, 3, 4, 5])) # Running sum + [1, 3, 6, 10, 15] + >>> list(accumulate([1, 2, 3], func=operator.mul)) # Running product + [1, 2, 6] + >>> list(accumulate([0, 1, -1, 2, 3, 2], func=max)) # Running maximum + [0, 1, 1, 2, 3, 3] + + This function is available in the ``itertools`` module for Python 3.2 and + greater. + + """ + it = iter(iterable) + try: + total = next(it) + except StopIteration: + return + else: + yield total + + for element in it: + total = func(total, element) + yield total + + +def take(n, iterable): + """Return first *n* items of the iterable as a list. + + >>> take(3, range(10)) + [0, 1, 2] + >>> take(5, range(3)) + [0, 1, 2] + + Effectively a short replacement for ``next`` based iterator consumption + when you want more than one item, but less than the whole iterator. + + """ + return list(islice(iterable, n)) + + +def tabulate(function, start=0): + """Return an iterator over the results of ``func(start)``, + ``func(start + 1)``, ``func(start + 2)``... + + *func* should be a function that accepts one integer argument. + + If *start* is not specified it defaults to 0. It will be incremented each + time the iterator is advanced. + + >>> square = lambda x: x ** 2 + >>> iterator = tabulate(square, -3) + >>> take(4, iterator) + [9, 4, 1, 0] + + """ + return map(function, count(start)) + + +def tail(n, iterable): + """Return an iterator over the last *n* items of *iterable*. + + >>> t = tail(3, 'ABCDEFG') + >>> list(t) + ['E', 'F', 'G'] + + """ + return iter(deque(iterable, maxlen=n)) + + +def consume(iterator, n=None): + """Advance *iterable* by *n* steps. If *n* is ``None``, consume it + entirely. + + Efficiently exhausts an iterator without returning values. Defaults to + consuming the whole iterator, but an optional second argument may be + provided to limit consumption. + + >>> i = (x for x in range(10)) + >>> next(i) + 0 + >>> consume(i, 3) + >>> next(i) + 4 + >>> consume(i) + >>> next(i) + Traceback (most recent call last): + File "", line 1, in + StopIteration + + If the iterator has fewer items remaining than the provided limit, the + whole iterator will be consumed. + + >>> i = (x for x in range(3)) + >>> consume(i, 5) + >>> next(i) + Traceback (most recent call last): + File "", line 1, in + StopIteration + + """ + # Use functions that consume iterators at C speed. + if n is None: + # feed the entire iterator into a zero-length deque + deque(iterator, maxlen=0) + else: + # advance to the empty slice starting at position n + next(islice(iterator, n, n), None) + + +def nth(iterable, n, default=None): + """Returns the nth item or a default value. + + >>> l = range(10) + >>> nth(l, 3) + 3 + >>> nth(l, 20, "zebra") + 'zebra' + + """ + return next(islice(iterable, n, None), default) + + +def all_equal(iterable): + """ + Returns ``True`` if all the elements are equal to each other. + + >>> all_equal('aaaa') + True + >>> all_equal('aaab') + False + + """ + g = groupby(iterable) + return next(g, True) and not next(g, False) + + +def quantify(iterable, pred=bool): + """Return the how many times the predicate is true. + + >>> quantify([True, False, True]) + 2 + + """ + return sum(map(pred, iterable)) + + +def padnone(iterable): + """Returns the sequence of elements and then returns ``None`` indefinitely. + + >>> take(5, padnone(range(3))) + [0, 1, 2, None, None] + + Useful for emulating the behavior of the built-in :func:`map` function. + + See also :func:`padded`. + + """ + return chain(iterable, repeat(None)) + + +def ncycles(iterable, n): + """Returns the sequence elements *n* times + + >>> list(ncycles(["a", "b"], 3)) + ['a', 'b', 'a', 'b', 'a', 'b'] + + """ + return chain.from_iterable(repeat(tuple(iterable), n)) + + +def dotproduct(vec1, vec2): + """Returns the dot product of the two iterables. + + >>> dotproduct([10, 10], [20, 20]) + 400 + + """ + return sum(map(operator.mul, vec1, vec2)) + + +def flatten(listOfLists): + """Return an iterator flattening one level of nesting in a list of lists. + + >>> list(flatten([[0, 1], [2, 3]])) + [0, 1, 2, 3] + + See also :func:`collapse`, which can flatten multiple levels of nesting. + + """ + return chain.from_iterable(listOfLists) + + +def repeatfunc(func, times=None, *args): + """Call *func* with *args* repeatedly, returning an iterable over the + results. + + If *times* is specified, the iterable will terminate after that many + repetitions: + + >>> from operator import add + >>> times = 4 + >>> args = 3, 5 + >>> list(repeatfunc(add, times, *args)) + [8, 8, 8, 8] + + If *times* is ``None`` the iterable will not terminate: + + >>> from random import randrange + >>> times = None + >>> args = 1, 11 + >>> take(6, repeatfunc(randrange, times, *args)) # doctest:+SKIP + [2, 4, 8, 1, 8, 4] + + """ + if times is None: + return starmap(func, repeat(args)) + return starmap(func, repeat(args, times)) + + +def pairwise(iterable): + """Returns an iterator of paired items, overlapping, from the original + + >>> take(4, pairwise(count())) + [(0, 1), (1, 2), (2, 3), (3, 4)] + + """ + a, b = tee(iterable) + next(b, None) + return zip(a, b) + + +def grouper(n, iterable, fillvalue=None): + """Collect data into fixed-length chunks or blocks. + + >>> list(grouper(3, 'ABCDEFG', 'x')) + [('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')] + + """ + args = [iter(iterable)] * n + return zip_longest(fillvalue=fillvalue, *args) + + +def roundrobin(*iterables): + """Yields an item from each iterable, alternating between them. + + >>> list(roundrobin('ABC', 'D', 'EF')) + ['A', 'D', 'E', 'B', 'F', 'C'] + + This function produces the same output as :func:`interleave_longest`, but + may perform better for some inputs (in particular when the number of + iterables is small). + + """ + # Recipe credited to George Sakkis + pending = len(iterables) + if PY2: + nexts = cycle(iter(it).next for it in iterables) + else: + nexts = cycle(iter(it).__next__ for it in iterables) + while pending: + try: + for next in nexts: + yield next() + except StopIteration: + pending -= 1 + nexts = cycle(islice(nexts, pending)) + + +def partition(pred, iterable): + """ + Returns a 2-tuple of iterables derived from the input iterable. + The first yields the items that have ``pred(item) == False``. + The second yields the items that have ``pred(item) == True``. + + >>> is_odd = lambda x: x % 2 != 0 + >>> iterable = range(10) + >>> even_items, odd_items = partition(is_odd, iterable) + >>> list(even_items), list(odd_items) + ([0, 2, 4, 6, 8], [1, 3, 5, 7, 9]) + + """ + # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9 + t1, t2 = tee(iterable) + return filterfalse(pred, t1), filter(pred, t2) + + +def powerset(iterable): + """Yields all possible subsets of the iterable. + + >>> list(powerset([1,2,3])) + [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] + + """ + s = list(iterable) + return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1)) + + +def unique_everseen(iterable, key=None): + """ + Yield unique elements, preserving order. + + >>> list(unique_everseen('AAAABBBCCDAABBB')) + ['A', 'B', 'C', 'D'] + >>> list(unique_everseen('ABBCcAD', str.lower)) + ['A', 'B', 'C', 'D'] + + Sequences with a mix of hashable and unhashable items can be used. + The function will be slower (i.e., `O(n^2)`) for unhashable items. + + """ + seenset = set() + seenset_add = seenset.add + seenlist = [] + seenlist_add = seenlist.append + if key is None: + for element in iterable: + try: + if element not in seenset: + seenset_add(element) + yield element + except TypeError: + if element not in seenlist: + seenlist_add(element) + yield element + else: + for element in iterable: + k = key(element) + try: + if k not in seenset: + seenset_add(k) + yield element + except TypeError: + if k not in seenlist: + seenlist_add(k) + yield element + + +def unique_justseen(iterable, key=None): + """Yields elements in order, ignoring serial duplicates + + >>> list(unique_justseen('AAAABBBCCDAABBB')) + ['A', 'B', 'C', 'D', 'A', 'B'] + >>> list(unique_justseen('ABBCcAD', str.lower)) + ['A', 'B', 'C', 'A', 'D'] + + """ + return map(next, map(operator.itemgetter(1), groupby(iterable, key))) + + +def iter_except(func, exception, first=None): + """Yields results from a function repeatedly until an exception is raised. + + Converts a call-until-exception interface to an iterator interface. + Like ``iter(func, sentinel)``, but uses an exception instead of a sentinel + to end the loop. + + >>> l = [0, 1, 2] + >>> list(iter_except(l.pop, IndexError)) + [2, 1, 0] + + """ + try: + if first is not None: + yield first() + while 1: + yield func() + except exception: + pass + + +def first_true(iterable, default=False, pred=None): + """ + Returns the first true value in the iterable. + + If no true value is found, returns *default* + + If *pred* is not None, returns the first item for which + ``pred(item) == True`` . + + >>> first_true(range(10)) + 1 + >>> first_true(range(10), pred=lambda x: x > 5) + 6 + >>> first_true(range(10), default='missing', pred=lambda x: x > 9) + 'missing' + + """ + return next(filter(pred, iterable), default) + + +def random_product(*args, **kwds): + """Draw an item at random from each of the input iterables. + + >>> random_product('abc', range(4), 'XYZ') # doctest:+SKIP + ('c', 3, 'Z') + + If *repeat* is provided as a keyword argument, that many items will be + drawn from each iterable. + + >>> random_product('abcd', range(4), repeat=2) # doctest:+SKIP + ('a', 2, 'd', 3) + + This equivalent to taking a random selection from + ``itertools.product(*args, **kwarg)``. + + """ + pools = [tuple(pool) for pool in args] * kwds.get('repeat', 1) + return tuple(choice(pool) for pool in pools) + + +def random_permutation(iterable, r=None): + """Return a random *r* length permutation of the elements in *iterable*. + + If *r* is not specified or is ``None``, then *r* defaults to the length of + *iterable*. + + >>> random_permutation(range(5)) # doctest:+SKIP + (3, 4, 0, 1, 2) + + This equivalent to taking a random selection from + ``itertools.permutations(iterable, r)``. + + """ + pool = tuple(iterable) + r = len(pool) if r is None else r + return tuple(sample(pool, r)) + + +def random_combination(iterable, r): + """Return a random *r* length subsequence of the elements in *iterable*. + + >>> random_combination(range(5), 3) # doctest:+SKIP + (2, 3, 4) + + This equivalent to taking a random selection from + ``itertools.combinations(iterable, r)``. + + """ + pool = tuple(iterable) + n = len(pool) + indices = sorted(sample(range(n), r)) + return tuple(pool[i] for i in indices) + + +def random_combination_with_replacement(iterable, r): + """Return a random *r* length subsequence of elements in *iterable*, + allowing individual elements to be repeated. + + >>> random_combination_with_replacement(range(3), 5) # doctest:+SKIP + (0, 0, 1, 2, 2) + + This equivalent to taking a random selection from + ``itertools.combinations_with_replacement(iterable, r)``. + + """ + pool = tuple(iterable) + n = len(pool) + indices = sorted(randrange(n) for i in range(r)) + return tuple(pool[i] for i in indices) + + +def nth_combination(iterable, r, index): + """Equivalent to ``list(combinations(iterable, r))[index]``. + + The subsequences of *iterable* that are of length *r* can be ordered + lexicographically. :func:`nth_combination` computes the subsequence at + sort position *index* directly, without computing the previous + subsequences. + + """ + pool = tuple(iterable) + n = len(pool) + if (r < 0) or (r > n): + raise ValueError + + c = 1 + k = min(r, n - r) + for i in range(1, k + 1): + c = c * (n - k + i) // i + + if index < 0: + index += c + + if (index < 0) or (index >= c): + raise IndexError + + result = [] + while r: + c, n, r = c * r // n, n - 1, r - 1 + while index >= c: + index -= c + c, n = c * (n - r) // n, n - 1 + result.append(pool[-1 - n]) + + return tuple(result) + + +def prepend(value, iterator): + """Yield *value*, followed by the elements in *iterator*. + + >>> value = '0' + >>> iterator = ['1', '2', '3'] + >>> list(prepend(value, iterator)) + ['0', '1', '2', '3'] + + To prepend multiple values, see :func:`itertools.chain`. + + """ + return chain([value], iterator) diff --git a/third_party/python/more-itertools/more_itertools/tests/__init__.py b/third_party/python/more-itertools/more_itertools/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/third_party/python/more-itertools/more_itertools/tests/test_more.py b/third_party/python/more-itertools/more_itertools/tests/test_more.py new file mode 100644 index 000000000000..2023ba6a4cbd --- /dev/null +++ b/third_party/python/more-itertools/more_itertools/tests/test_more.py @@ -0,0 +1,1848 @@ +from __future__ import division, print_function, unicode_literals + +from decimal import Decimal +from doctest import DocTestSuite +from fractions import Fraction +from functools import partial, reduce +from heapq import merge +from io import StringIO +from itertools import ( + chain, + count, + groupby, + islice, + permutations, + product, + repeat, +) +from operator import add, mul, itemgetter +from unittest import TestCase + +from six.moves import filter, map, range, zip + +import more_itertools as mi + + +def load_tests(loader, tests, ignore): + # Add the doctests + tests.addTests(DocTestSuite('more_itertools.more')) + return tests + + +class CollateTests(TestCase): + """Unit tests for ``collate()``""" + # Also accidentally tests peekable, though that could use its own tests + + def test_default(self): + """Test with the default `key` function.""" + iterables = [range(4), range(7), range(3, 6)] + self.assertEqual( + sorted(reduce(list.__add__, [list(it) for it in iterables])), + list(mi.collate(*iterables)) + ) + + def test_key(self): + """Test using a custom `key` function.""" + iterables = [range(5, 0, -1), range(4, 0, -1)] + actual = sorted( + reduce(list.__add__, [list(it) for it in iterables]), reverse=True + ) + expected = list(mi.collate(*iterables, key=lambda x: -x)) + self.assertEqual(actual, expected) + + def test_empty(self): + """Be nice if passed an empty list of iterables.""" + self.assertEqual([], list(mi.collate())) + + def test_one(self): + """Work when only 1 iterable is passed.""" + self.assertEqual([0, 1], list(mi.collate(range(2)))) + + def test_reverse(self): + """Test the `reverse` kwarg.""" + iterables = [range(4, 0, -1), range(7, 0, -1), range(3, 6, -1)] + + actual = sorted( + reduce(list.__add__, [list(it) for it in iterables]), reverse=True + ) + expected = list(mi.collate(*iterables, reverse=True)) + self.assertEqual(actual, expected) + + def test_alias(self): + self.assertNotEqual(merge.__doc__, mi.collate.__doc__) + self.assertNotEqual(partial.__doc__, mi.collate.__doc__) + + +class ChunkedTests(TestCase): + """Tests for ``chunked()``""" + + def test_even(self): + """Test when ``n`` divides evenly into the length of the iterable.""" + self.assertEqual( + list(mi.chunked('ABCDEF', 3)), [['A', 'B', 'C'], ['D', 'E', 'F']] + ) + + def test_odd(self): + """Test when ``n`` does not divide evenly into the length of the + iterable. + + """ + self.assertEqual( + list(mi.chunked('ABCDE', 3)), [['A', 'B', 'C'], ['D', 'E']] + ) + + +class FirstTests(TestCase): + """Tests for ``first()``""" + + def test_many(self): + """Test that it works on many-item iterables.""" + # Also try it on a generator expression to make sure it works on + # whatever those return, across Python versions. + self.assertEqual(mi.first(x for x in range(4)), 0) + + def test_one(self): + """Test that it doesn't raise StopIteration prematurely.""" + self.assertEqual(mi.first([3]), 3) + + def test_empty_stop_iteration(self): + """It should raise StopIteration for empty iterables.""" + self.assertRaises(ValueError, lambda: mi.first([])) + + def test_default(self): + """It should return the provided default arg for empty iterables.""" + self.assertEqual(mi.first([], 'boo'), 'boo') + + +class PeekableTests(TestCase): + """Tests for ``peekable()`` behavor not incidentally covered by testing + ``collate()`` + + """ + def test_peek_default(self): + """Make sure passing a default into ``peek()`` works.""" + p = mi.peekable([]) + self.assertEqual(p.peek(7), 7) + + def test_truthiness(self): + """Make sure a ``peekable`` tests true iff there are items remaining in + the iterable. + + """ + p = mi.peekable([]) + self.assertFalse(p) + + p = mi.peekable(range(3)) + self.assertTrue(p) + + def test_simple_peeking(self): + """Make sure ``next`` and ``peek`` advance and don't advance the + iterator, respectively. + + """ + p = mi.peekable(range(10)) + self.assertEqual(next(p), 0) + self.assertEqual(p.peek(), 1) + self.assertEqual(next(p), 1) + + def test_indexing(self): + """ + Indexing into the peekable shouldn't advance the iterator. + """ + p = mi.peekable('abcdefghijkl') + + # The 0th index is what ``next()`` will return + self.assertEqual(p[0], 'a') + self.assertEqual(next(p), 'a') + + # Indexing further into the peekable shouldn't advance the itertor + self.assertEqual(p[2], 'd') + self.assertEqual(next(p), 'b') + + # The 0th index moves up with the iterator; the last index follows + self.assertEqual(p[0], 'c') + self.assertEqual(p[9], 'l') + + self.assertEqual(next(p), 'c') + self.assertEqual(p[8], 'l') + + # Negative indexing should work too + self.assertEqual(p[-2], 'k') + self.assertEqual(p[-9], 'd') + self.assertRaises(IndexError, lambda: p[-10]) + + def test_slicing(self): + """Slicing the peekable shouldn't advance the iterator.""" + seq = list('abcdefghijkl') + p = mi.peekable(seq) + + # Slicing the peekable should just be like slicing a re-iterable + self.assertEqual(p[1:4], seq[1:4]) + + # Advancing the iterator moves the slices up also + self.assertEqual(next(p), 'a') + self.assertEqual(p[1:4], seq[1:][1:4]) + + # Implicit starts and stop should work + self.assertEqual(p[:5], seq[1:][:5]) + self.assertEqual(p[:], seq[1:][:]) + + # Indexing past the end should work + self.assertEqual(p[:100], seq[1:][:100]) + + # Steps should work, including negative + self.assertEqual(p[::2], seq[1:][::2]) + self.assertEqual(p[::-1], seq[1:][::-1]) + + def test_slicing_reset(self): + """Test slicing on a fresh iterable each time""" + iterable = ['0', '1', '2', '3', '4', '5'] + indexes = list(range(-4, len(iterable) + 4)) + [None] + steps = [1, 2, 3, 4, -1, -2, -3, 4] + for slice_args in product(indexes, indexes, steps): + it = iter(iterable) + p = mi.peekable(it) + next(p) + index = slice(*slice_args) + actual = p[index] + expected = iterable[1:][index] + self.assertEqual(actual, expected, slice_args) + + def test_slicing_error(self): + iterable = '01234567' + p = mi.peekable(iter(iterable)) + + # Prime the cache + p.peek() + old_cache = list(p._cache) + + # Illegal slice + with self.assertRaises(ValueError): + p[1:-1:0] + + # Neither the cache nor the iteration should be affected + self.assertEqual(old_cache, list(p._cache)) + self.assertEqual(list(p), list(iterable)) + + def test_passthrough(self): + """Iterating a peekable without using ``peek()`` or ``prepend()`` + should just give the underlying iterable's elements (a trivial test but + useful to set a baseline in case something goes wrong)""" + expected = [1, 2, 3, 4, 5] + actual = list(mi.peekable(expected)) + self.assertEqual(actual, expected) + + # prepend() behavior tests + + def test_prepend(self): + """Tests intersperesed ``prepend()`` and ``next()`` calls""" + it = mi.peekable(range(2)) + actual = [] + + # Test prepend() before next() + it.prepend(10) + actual += [next(it), next(it)] + + # Test prepend() between next()s + it.prepend(11) + actual += [next(it), next(it)] + + # Test prepend() after source iterable is consumed + it.prepend(12) + actual += [next(it)] + + expected = [10, 0, 11, 1, 12] + self.assertEqual(actual, expected) + + def test_multi_prepend(self): + """Tests prepending multiple items and getting them in proper order""" + it = mi.peekable(range(5)) + actual = [next(it), next(it)] + it.prepend(10, 11, 12) + it.prepend(20, 21) + actual += list(it) + expected = [0, 1, 20, 21, 10, 11, 12, 2, 3, 4] + self.assertEqual(actual, expected) + + def test_empty(self): + """Tests prepending in front of an empty iterable""" + it = mi.peekable([]) + it.prepend(10) + actual = list(it) + expected = [10] + self.assertEqual(actual, expected) + + def test_prepend_truthiness(self): + """Tests that ``__bool__()`` or ``__nonzero__()`` works properly + with ``prepend()``""" + it = mi.peekable(range(5)) + self.assertTrue(it) + actual = list(it) + self.assertFalse(it) + it.prepend(10) + self.assertTrue(it) + actual += [next(it)] + self.assertFalse(it) + expected = [0, 1, 2, 3, 4, 10] + self.assertEqual(actual, expected) + + def test_multi_prepend_peek(self): + """Tests prepending multiple elements and getting them in reverse order + while peeking""" + it = mi.peekable(range(5)) + actual = [next(it), next(it)] + self.assertEqual(it.peek(), 2) + it.prepend(10, 11, 12) + self.assertEqual(it.peek(), 10) + it.prepend(20, 21) + self.assertEqual(it.peek(), 20) + actual += list(it) + self.assertFalse(it) + expected = [0, 1, 20, 21, 10, 11, 12, 2, 3, 4] + self.assertEqual(actual, expected) + + def test_prepend_after_stop(self): + """Test resuming iteration after a previous exhaustion""" + it = mi.peekable(range(3)) + self.assertEqual(list(it), [0, 1, 2]) + self.assertRaises(StopIteration, lambda: next(it)) + it.prepend(10) + self.assertEqual(next(it), 10) + self.assertRaises(StopIteration, lambda: next(it)) + + def test_prepend_slicing(self): + """Tests interaction between prepending and slicing""" + seq = list(range(20)) + p = mi.peekable(seq) + + p.prepend(30, 40, 50) + pseq = [30, 40, 50] + seq # pseq for prepended_seq + + # adapt the specific tests from test_slicing + self.assertEqual(p[0], 30) + self.assertEqual(p[1:8], pseq[1:8]) + self.assertEqual(p[1:], pseq[1:]) + self.assertEqual(p[:5], pseq[:5]) + self.assertEqual(p[:], pseq[:]) + self.assertEqual(p[:100], pseq[:100]) + self.assertEqual(p[::2], pseq[::2]) + self.assertEqual(p[::-1], pseq[::-1]) + + def test_prepend_indexing(self): + """Tests interaction between prepending and indexing""" + seq = list(range(20)) + p = mi.peekable(seq) + + p.prepend(30, 40, 50) + + self.assertEqual(p[0], 30) + self.assertEqual(next(p), 30) + self.assertEqual(p[2], 0) + self.assertEqual(next(p), 40) + self.assertEqual(p[0], 50) + self.assertEqual(p[9], 8) + self.assertEqual(next(p), 50) + self.assertEqual(p[8], 8) + self.assertEqual(p[-2], 18) + self.assertEqual(p[-9], 11) + self.assertRaises(IndexError, lambda: p[-21]) + + def test_prepend_iterable(self): + """Tests prepending from an iterable""" + it = mi.peekable(range(5)) + # Don't directly use the range() object to avoid any range-specific + # optimizations + it.prepend(*(x for x in range(5))) + actual = list(it) + expected = list(chain(range(5), range(5))) + self.assertEqual(actual, expected) + + def test_prepend_many(self): + """Tests that prepending a huge number of elements works""" + it = mi.peekable(range(5)) + # Don't directly use the range() object to avoid any range-specific + # optimizations + it.prepend(*(x for x in range(20000))) + actual = list(it) + expected = list(chain(range(20000), range(5))) + self.assertEqual(actual, expected) + + def test_prepend_reversed(self): + """Tests prepending from a reversed iterable""" + it = mi.peekable(range(3)) + it.prepend(*reversed((10, 11, 12))) + actual = list(it) + expected = [12, 11, 10, 0, 1, 2] + self.assertEqual(actual, expected) + + +class ConsumerTests(TestCase): + """Tests for ``consumer()``""" + + def test_consumer(self): + @mi.consumer + def eater(): + while True: + x = yield # noqa + + e = eater() + e.send('hi') # without @consumer, would raise TypeError + + +class DistinctPermutationsTests(TestCase): + def test_distinct_permutations(self): + """Make sure the output for ``distinct_permutations()`` is the same as + set(permutations(it)). + + """ + iterable = ['z', 'a', 'a', 'q', 'q', 'q', 'y'] + test_output = sorted(mi.distinct_permutations(iterable)) + ref_output = sorted(set(permutations(iterable))) + self.assertEqual(test_output, ref_output) + + def test_other_iterables(self): + """Make sure ``distinct_permutations()`` accepts a different type of + iterables. + + """ + # a generator + iterable = (c for c in ['z', 'a', 'a', 'q', 'q', 'q', 'y']) + test_output = sorted(mi.distinct_permutations(iterable)) + # "reload" it + iterable = (c for c in ['z', 'a', 'a', 'q', 'q', 'q', 'y']) + ref_output = sorted(set(permutations(iterable))) + self.assertEqual(test_output, ref_output) + + # an iterator + iterable = iter(['z', 'a', 'a', 'q', 'q', 'q', 'y']) + test_output = sorted(mi.distinct_permutations(iterable)) + # "reload" it + iterable = iter(['z', 'a', 'a', 'q', 'q', 'q', 'y']) + ref_output = sorted(set(permutations(iterable))) + self.assertEqual(test_output, ref_output) + + +class IlenTests(TestCase): + def test_ilen(self): + """Sanity-checks for ``ilen()``.""" + # Non-empty + self.assertEqual( + mi.ilen(filter(lambda x: x % 10 == 0, range(101))), 11 + ) + + # Empty + self.assertEqual(mi.ilen((x for x in range(0))), 0) + + # Iterable with __len__ + self.assertEqual(mi.ilen(list(range(6))), 6) + + +class WithIterTests(TestCase): + def test_with_iter(self): + s = StringIO('One fish\nTwo fish') + initial_words = [line.split()[0] for line in mi.with_iter(s)] + + # Iterable's items should be faithfully represented + self.assertEqual(initial_words, ['One', 'Two']) + # The file object should be closed + self.assertEqual(s.closed, True) + + +class OneTests(TestCase): + def test_basic(self): + it = iter(['item']) + self.assertEqual(mi.one(it), 'item') + + def test_too_short(self): + it = iter([]) + self.assertRaises(ValueError, lambda: mi.one(it)) + self.assertRaises(IndexError, lambda: mi.one(it, too_short=IndexError)) + + def test_too_long(self): + it = count() + self.assertRaises(ValueError, lambda: mi.one(it)) # burn 0 and 1 + self.assertEqual(next(it), 2) + self.assertRaises( + OverflowError, lambda: mi.one(it, too_long=OverflowError) + ) + + +class IntersperseTest(TestCase): + """ Tests for intersperse() """ + + def test_even(self): + iterable = (x for x in '01') + self.assertEqual( + list(mi.intersperse(None, iterable)), ['0', None, '1'] + ) + + def test_odd(self): + iterable = (x for x in '012') + self.assertEqual( + list(mi.intersperse(None, iterable)), ['0', None, '1', None, '2'] + ) + + def test_nested(self): + element = ('a', 'b') + iterable = (x for x in '012') + actual = list(mi.intersperse(element, iterable)) + expected = ['0', ('a', 'b'), '1', ('a', 'b'), '2'] + self.assertEqual(actual, expected) + + def test_not_iterable(self): + self.assertRaises(TypeError, lambda: mi.intersperse('x', 1)) + + def test_n(self): + for n, element, expected in [ + (1, '_', ['0', '_', '1', '_', '2', '_', '3', '_', '4', '_', '5']), + (2, '_', ['0', '1', '_', '2', '3', '_', '4', '5']), + (3, '_', ['0', '1', '2', '_', '3', '4', '5']), + (4, '_', ['0', '1', '2', '3', '_', '4', '5']), + (5, '_', ['0', '1', '2', '3', '4', '_', '5']), + (6, '_', ['0', '1', '2', '3', '4', '5']), + (7, '_', ['0', '1', '2', '3', '4', '5']), + (3, ['a', 'b'], ['0', '1', '2', ['a', 'b'], '3', '4', '5']), + ]: + iterable = (x for x in '012345') + actual = list(mi.intersperse(element, iterable, n=n)) + self.assertEqual(actual, expected) + + def test_n_zero(self): + self.assertRaises( + ValueError, lambda: list(mi.intersperse('x', '012', n=0)) + ) + + +class UniqueToEachTests(TestCase): + """Tests for ``unique_to_each()``""" + + def test_all_unique(self): + """When all the input iterables are unique the output should match + the input.""" + iterables = [[1, 2], [3, 4, 5], [6, 7, 8]] + self.assertEqual(mi.unique_to_each(*iterables), iterables) + + def test_duplicates(self): + """When there are duplicates in any of the input iterables that aren't + in the rest, those duplicates should be emitted.""" + iterables = ["mississippi", "missouri"] + self.assertEqual( + mi.unique_to_each(*iterables), [['p', 'p'], ['o', 'u', 'r']] + ) + + def test_mixed(self): + """When the input iterables contain different types the function should + still behave properly""" + iterables = ['x', (i for i in range(3)), [1, 2, 3], tuple()] + self.assertEqual(mi.unique_to_each(*iterables), [['x'], [0], [3], []]) + + +class WindowedTests(TestCase): + """Tests for ``windowed()``""" + + def test_basic(self): + actual = list(mi.windowed([1, 2, 3, 4, 5], 3)) + expected = [(1, 2, 3), (2, 3, 4), (3, 4, 5)] + self.assertEqual(actual, expected) + + def test_large_size(self): + """ + When the window size is larger than the iterable, and no fill value is + given,``None`` should be filled in. + """ + actual = list(mi.windowed([1, 2, 3, 4, 5], 6)) + expected = [(1, 2, 3, 4, 5, None)] + self.assertEqual(actual, expected) + + def test_fillvalue(self): + """ + When sizes don't match evenly, the given fill value should be used. + """ + iterable = [1, 2, 3, 4, 5] + + for n, kwargs, expected in [ + (6, {}, [(1, 2, 3, 4, 5, '!')]), # n > len(iterable) + (3, {'step': 3}, [(1, 2, 3), (4, 5, '!')]), # using ``step`` + ]: + actual = list(mi.windowed(iterable, n, fillvalue='!', **kwargs)) + self.assertEqual(actual, expected) + + def test_zero(self): + """When the window size is zero, an empty tuple should be emitted.""" + actual = list(mi.windowed([1, 2, 3, 4, 5], 0)) + expected = [tuple()] + self.assertEqual(actual, expected) + + def test_negative(self): + """When the window size is negative, ValueError should be raised.""" + with self.assertRaises(ValueError): + list(mi.windowed([1, 2, 3, 4, 5], -1)) + + def test_step(self): + """The window should advance by the number of steps provided""" + iterable = [1, 2, 3, 4, 5, 6, 7] + for n, step, expected in [ + (3, 2, [(1, 2, 3), (3, 4, 5), (5, 6, 7)]), # n > step + (3, 3, [(1, 2, 3), (4, 5, 6), (7, None, None)]), # n == step + (3, 4, [(1, 2, 3), (5, 6, 7)]), # line up nicely + (3, 5, [(1, 2, 3), (6, 7, None)]), # off by one + (3, 6, [(1, 2, 3), (7, None, None)]), # off by two + (3, 7, [(1, 2, 3)]), # step past the end + (7, 8, [(1, 2, 3, 4, 5, 6, 7)]), # step > len(iterable) + ]: + actual = list(mi.windowed(iterable, n, step=step)) + self.assertEqual(actual, expected) + + # Step must be greater than or equal to 1 + with self.assertRaises(ValueError): + list(mi.windowed(iterable, 3, step=0)) + + +class BucketTests(TestCase): + """Tests for ``bucket()``""" + + def test_basic(self): + iterable = [10, 20, 30, 11, 21, 31, 12, 22, 23, 33] + D = mi.bucket(iterable, key=lambda x: 10 * (x // 10)) + + # In-order access + self.assertEqual(list(D[10]), [10, 11, 12]) + + # Out of order access + self.assertEqual(list(D[30]), [30, 31, 33]) + self.assertEqual(list(D[20]), [20, 21, 22, 23]) + + self.assertEqual(list(D[40]), []) # Nothing in here! + + def test_in(self): + iterable = [10, 20, 30, 11, 21, 31, 12, 22, 23, 33] + D = mi.bucket(iterable, key=lambda x: 10 * (x // 10)) + + self.assertTrue(10 in D) + self.assertFalse(40 in D) + self.assertTrue(20 in D) + self.assertFalse(21 in D) + + # Checking in-ness shouldn't advance the iterator + self.assertEqual(next(D[10]), 10) + + def test_validator(self): + iterable = count(0) + key = lambda x: int(str(x)[0]) # First digit of each number + validator = lambda x: 0 < x < 10 # No leading zeros + D = mi.bucket(iterable, key, validator=validator) + self.assertEqual(mi.take(3, D[1]), [1, 10, 11]) + self.assertNotIn(0, D) # Non-valid entries don't return True + self.assertNotIn(0, D._cache) # Don't store non-valid entries + self.assertEqual(list(D[0]), []) + + +class SpyTests(TestCase): + """Tests for ``spy()``""" + + def test_basic(self): + original_iterable = iter('abcdefg') + head, new_iterable = mi.spy(original_iterable) + self.assertEqual(head, ['a']) + self.assertEqual( + list(new_iterable), ['a', 'b', 'c', 'd', 'e', 'f', 'g'] + ) + + def test_unpacking(self): + original_iterable = iter('abcdefg') + (first, second, third), new_iterable = mi.spy(original_iterable, 3) + self.assertEqual(first, 'a') + self.assertEqual(second, 'b') + self.assertEqual(third, 'c') + self.assertEqual( + list(new_iterable), ['a', 'b', 'c', 'd', 'e', 'f', 'g'] + ) + + def test_too_many(self): + original_iterable = iter('abc') + head, new_iterable = mi.spy(original_iterable, 4) + self.assertEqual(head, ['a', 'b', 'c']) + self.assertEqual(list(new_iterable), ['a', 'b', 'c']) + + def test_zero(self): + original_iterable = iter('abc') + head, new_iterable = mi.spy(original_iterable, 0) + self.assertEqual(head, []) + self.assertEqual(list(new_iterable), ['a', 'b', 'c']) + + +class InterleaveTests(TestCase): + def test_even(self): + actual = list(mi.interleave([1, 4, 7], [2, 5, 8], [3, 6, 9])) + expected = [1, 2, 3, 4, 5, 6, 7, 8, 9] + self.assertEqual(actual, expected) + + def test_short(self): + actual = list(mi.interleave([1, 4], [2, 5, 7], [3, 6, 8])) + expected = [1, 2, 3, 4, 5, 6] + self.assertEqual(actual, expected) + + def test_mixed_types(self): + it_list = ['a', 'b', 'c', 'd'] + it_str = '12345' + it_inf = count() + actual = list(mi.interleave(it_list, it_str, it_inf)) + expected = ['a', '1', 0, 'b', '2', 1, 'c', '3', 2, 'd', '4', 3] + self.assertEqual(actual, expected) + + +class InterleaveLongestTests(TestCase): + def test_even(self): + actual = list(mi.interleave_longest([1, 4, 7], [2, 5, 8], [3, 6, 9])) + expected = [1, 2, 3, 4, 5, 6, 7, 8, 9] + self.assertEqual(actual, expected) + + def test_short(self): + actual = list(mi.interleave_longest([1, 4], [2, 5, 7], [3, 6, 8])) + expected = [1, 2, 3, 4, 5, 6, 7, 8] + self.assertEqual(actual, expected) + + def test_mixed_types(self): + it_list = ['a', 'b', 'c', 'd'] + it_str = '12345' + it_gen = (x for x in range(3)) + actual = list(mi.interleave_longest(it_list, it_str, it_gen)) + expected = ['a', '1', 0, 'b', '2', 1, 'c', '3', 2, 'd', '4', '5'] + self.assertEqual(actual, expected) + + +class TestCollapse(TestCase): + """Tests for ``collapse()``""" + + def test_collapse(self): + l = [[1], 2, [[3], 4], [[[5]]]] + self.assertEqual(list(mi.collapse(l)), [1, 2, 3, 4, 5]) + + def test_collapse_to_string(self): + l = [["s1"], "s2", [["s3"], "s4"], [[["s5"]]]] + self.assertEqual(list(mi.collapse(l)), ["s1", "s2", "s3", "s4", "s5"]) + + def test_collapse_flatten(self): + l = [[1], [2], [[3], 4], [[[5]]]] + self.assertEqual(list(mi.collapse(l, levels=1)), list(mi.flatten(l))) + + def test_collapse_to_level(self): + l = [[1], 2, [[3], 4], [[[5]]]] + self.assertEqual(list(mi.collapse(l, levels=2)), [1, 2, 3, 4, [5]]) + self.assertEqual( + list(mi.collapse(mi.collapse(l, levels=1), levels=1)), + list(mi.collapse(l, levels=2)) + ) + + def test_collapse_to_list(self): + l = (1, [2], (3, [4, (5,)], 'ab')) + actual = list(mi.collapse(l, base_type=list)) + expected = [1, [2], 3, [4, (5,)], 'ab'] + self.assertEqual(actual, expected) + + +class SideEffectTests(TestCase): + """Tests for ``side_effect()``""" + + def test_individual(self): + # The function increments the counter for each call + counter = [0] + + def func(arg): + counter[0] += 1 + + result = list(mi.side_effect(func, range(10))) + self.assertEqual(result, list(range(10))) + self.assertEqual(counter[0], 10) + + def test_chunked(self): + # The function increments the counter for each call + counter = [0] + + def func(arg): + counter[0] += 1 + + result = list(mi.side_effect(func, range(10), 2)) + self.assertEqual(result, list(range(10))) + self.assertEqual(counter[0], 5) + + def test_before_after(self): + f = StringIO() + collector = [] + + def func(item): + print(item, file=f) + collector.append(f.getvalue()) + + def it(): + yield u'a' + yield u'b' + raise RuntimeError('kaboom') + + before = lambda: print('HEADER', file=f) + after = f.close + + try: + mi.consume(mi.side_effect(func, it(), before=before, after=after)) + except RuntimeError: + pass + + # The iterable should have been written to the file + self.assertEqual(collector, [u'HEADER\na\n', u'HEADER\na\nb\n']) + + # The file should be closed even though something bad happened + self.assertTrue(f.closed) + + def test_before_fails(self): + f = StringIO() + func = lambda x: print(x, file=f) + + def before(): + raise RuntimeError('ouch') + + try: + mi.consume( + mi.side_effect(func, u'abc', before=before, after=f.close) + ) + except RuntimeError: + pass + + # The file should be closed even though something bad happened in the + # before function + self.assertTrue(f.closed) + + +class SlicedTests(TestCase): + """Tests for ``sliced()``""" + + def test_even(self): + """Test when the length of the sequence is divisible by *n*""" + seq = 'ABCDEFGHI' + self.assertEqual(list(mi.sliced(seq, 3)), ['ABC', 'DEF', 'GHI']) + + def test_odd(self): + """Test when the length of the sequence is not divisible by *n*""" + seq = 'ABCDEFGHI' + self.assertEqual(list(mi.sliced(seq, 4)), ['ABCD', 'EFGH', 'I']) + + def test_not_sliceable(self): + seq = (x for x in 'ABCDEFGHI') + + with self.assertRaises(TypeError): + list(mi.sliced(seq, 3)) + + +class SplitAtTests(TestCase): + """Tests for ``split()``""" + + def comp_with_str_split(self, str_to_split, delim): + pred = lambda c: c == delim + actual = list(map(''.join, mi.split_at(str_to_split, pred))) + expected = str_to_split.split(delim) + self.assertEqual(actual, expected) + + def test_seperators(self): + test_strs = ['', 'abcba', 'aaabbbcccddd', 'e'] + for s, delim in product(test_strs, 'abcd'): + self.comp_with_str_split(s, delim) + + +class SplitBeforeTest(TestCase): + """Tests for ``split_before()``""" + + def test_starts_with_sep(self): + actual = list(mi.split_before('xooxoo', lambda c: c == 'x')) + expected = [['x', 'o', 'o'], ['x', 'o', 'o']] + self.assertEqual(actual, expected) + + def test_ends_with_sep(self): + actual = list(mi.split_before('ooxoox', lambda c: c == 'x')) + expected = [['o', 'o'], ['x', 'o', 'o'], ['x']] + self.assertEqual(actual, expected) + + def test_no_sep(self): + actual = list(mi.split_before('ooo', lambda c: c == 'x')) + expected = [['o', 'o', 'o']] + self.assertEqual(actual, expected) + + +class SplitAfterTest(TestCase): + """Tests for ``split_after()``""" + + def test_starts_with_sep(self): + actual = list(mi.split_after('xooxoo', lambda c: c == 'x')) + expected = [['x'], ['o', 'o', 'x'], ['o', 'o']] + self.assertEqual(actual, expected) + + def test_ends_with_sep(self): + actual = list(mi.split_after('ooxoox', lambda c: c == 'x')) + expected = [['o', 'o', 'x'], ['o', 'o', 'x']] + self.assertEqual(actual, expected) + + def test_no_sep(self): + actual = list(mi.split_after('ooo', lambda c: c == 'x')) + expected = [['o', 'o', 'o']] + self.assertEqual(actual, expected) + + +class PaddedTest(TestCase): + """Tests for ``padded()``""" + + def test_no_n(self): + seq = [1, 2, 3] + + # No fillvalue + self.assertEqual(mi.take(5, mi.padded(seq)), [1, 2, 3, None, None]) + + # With fillvalue + self.assertEqual( + mi.take(5, mi.padded(seq, fillvalue='')), [1, 2, 3, '', ''] + ) + + def test_invalid_n(self): + self.assertRaises(ValueError, lambda: list(mi.padded([1, 2, 3], n=-1))) + self.assertRaises(ValueError, lambda: list(mi.padded([1, 2, 3], n=0))) + + def test_valid_n(self): + seq = [1, 2, 3, 4, 5] + + # No need for padding: len(seq) <= n + self.assertEqual(list(mi.padded(seq, n=4)), [1, 2, 3, 4, 5]) + self.assertEqual(list(mi.padded(seq, n=5)), [1, 2, 3, 4, 5]) + + # No fillvalue + self.assertEqual( + list(mi.padded(seq, n=7)), [1, 2, 3, 4, 5, None, None] + ) + + # With fillvalue + self.assertEqual( + list(mi.padded(seq, fillvalue='', n=7)), [1, 2, 3, 4, 5, '', ''] + ) + + def test_next_multiple(self): + seq = [1, 2, 3, 4, 5, 6] + + # No need for padding: len(seq) % n == 0 + self.assertEqual( + list(mi.padded(seq, n=3, next_multiple=True)), [1, 2, 3, 4, 5, 6] + ) + + # Padding needed: len(seq) < n + self.assertEqual( + list(mi.padded(seq, n=8, next_multiple=True)), + [1, 2, 3, 4, 5, 6, None, None] + ) + + # No padding needed: len(seq) == n + self.assertEqual( + list(mi.padded(seq, n=6, next_multiple=True)), [1, 2, 3, 4, 5, 6] + ) + + # Padding needed: len(seq) > n + self.assertEqual( + list(mi.padded(seq, n=4, next_multiple=True)), + [1, 2, 3, 4, 5, 6, None, None] + ) + + # With fillvalue + self.assertEqual( + list(mi.padded(seq, fillvalue='', n=4, next_multiple=True)), + [1, 2, 3, 4, 5, 6, '', ''] + ) + + +class DistributeTest(TestCase): + """Tests for distribute()""" + + def test_invalid_n(self): + self.assertRaises(ValueError, lambda: mi.distribute(-1, [1, 2, 3])) + self.assertRaises(ValueError, lambda: mi.distribute(0, [1, 2, 3])) + + def test_basic(self): + iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + + for n, expected in [ + (1, [iterable]), + (2, [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]), + (3, [[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]), + (10, [[n] for n in range(1, 10 + 1)]), + ]: + self.assertEqual( + [list(x) for x in mi.distribute(n, iterable)], expected + ) + + def test_large_n(self): + iterable = [1, 2, 3, 4] + self.assertEqual( + [list(x) for x in mi.distribute(6, iterable)], + [[1], [2], [3], [4], [], []] + ) + + +class StaggerTest(TestCase): + """Tests for ``stagger()``""" + + def test_default(self): + iterable = [0, 1, 2, 3] + actual = list(mi.stagger(iterable)) + expected = [(None, 0, 1), (0, 1, 2), (1, 2, 3)] + self.assertEqual(actual, expected) + + def test_offsets(self): + iterable = [0, 1, 2, 3] + for offsets, expected in [ + ((-2, 0, 2), [('', 0, 2), ('', 1, 3)]), + ((-2, -1), [('', ''), ('', 0), (0, 1), (1, 2), (2, 3)]), + ((1, 2), [(1, 2), (2, 3)]), + ]: + all_groups = mi.stagger(iterable, offsets=offsets, fillvalue='') + self.assertEqual(list(all_groups), expected) + + def test_longest(self): + iterable = [0, 1, 2, 3] + for offsets, expected in [ + ( + (-1, 0, 1), + [('', 0, 1), (0, 1, 2), (1, 2, 3), (2, 3, ''), (3, '', '')] + ), + ((-2, -1), [('', ''), ('', 0), (0, 1), (1, 2), (2, 3), (3, '')]), + ((1, 2), [(1, 2), (2, 3), (3, '')]), + ]: + all_groups = mi.stagger( + iterable, offsets=offsets, fillvalue='', longest=True + ) + self.assertEqual(list(all_groups), expected) + + +class ZipOffsetTest(TestCase): + """Tests for ``zip_offset()``""" + + def test_shortest(self): + a_1 = [0, 1, 2, 3] + a_2 = [0, 1, 2, 3, 4, 5] + a_3 = [0, 1, 2, 3, 4, 5, 6, 7] + actual = list( + mi.zip_offset(a_1, a_2, a_3, offsets=(-1, 0, 1), fillvalue='') + ) + expected = [('', 0, 1), (0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)] + self.assertEqual(actual, expected) + + def test_longest(self): + a_1 = [0, 1, 2, 3] + a_2 = [0, 1, 2, 3, 4, 5] + a_3 = [0, 1, 2, 3, 4, 5, 6, 7] + actual = list( + mi.zip_offset(a_1, a_2, a_3, offsets=(-1, 0, 1), longest=True) + ) + expected = [ + (None, 0, 1), + (0, 1, 2), + (1, 2, 3), + (2, 3, 4), + (3, 4, 5), + (None, 5, 6), + (None, None, 7), + ] + self.assertEqual(actual, expected) + + def test_mismatch(self): + iterables = [0, 1, 2], [2, 3, 4] + offsets = (-1, 0, 1) + self.assertRaises( + ValueError, + lambda: list(mi.zip_offset(*iterables, offsets=offsets)) + ) + + +class SortTogetherTest(TestCase): + """Tests for sort_together()""" + + def test_key_list(self): + """tests `key_list` including default, iterables include duplicates""" + iterables = [ + ['GA', 'GA', 'GA', 'CT', 'CT', 'CT'], + ['May', 'Aug.', 'May', 'June', 'July', 'July'], + [97, 20, 100, 70, 100, 20] + ] + + self.assertEqual( + mi.sort_together(iterables), + [ + ('CT', 'CT', 'CT', 'GA', 'GA', 'GA'), + ('June', 'July', 'July', 'May', 'Aug.', 'May'), + (70, 100, 20, 97, 20, 100) + ] + ) + + self.assertEqual( + mi.sort_together(iterables, key_list=(0, 1)), + [ + ('CT', 'CT', 'CT', 'GA', 'GA', 'GA'), + ('July', 'July', 'June', 'Aug.', 'May', 'May'), + (100, 20, 70, 20, 97, 100) + ] + ) + + self.assertEqual( + mi.sort_together(iterables, key_list=(0, 1, 2)), + [ + ('CT', 'CT', 'CT', 'GA', 'GA', 'GA'), + ('July', 'July', 'June', 'Aug.', 'May', 'May'), + (20, 100, 70, 20, 97, 100) + ] + ) + + self.assertEqual( + mi.sort_together(iterables, key_list=(2,)), + [ + ('GA', 'CT', 'CT', 'GA', 'GA', 'CT'), + ('Aug.', 'July', 'June', 'May', 'May', 'July'), + (20, 20, 70, 97, 100, 100) + ] + ) + + def test_invalid_key_list(self): + """tests `key_list` for indexes not available in `iterables`""" + iterables = [ + ['GA', 'GA', 'GA', 'CT', 'CT', 'CT'], + ['May', 'Aug.', 'May', 'June', 'July', 'July'], + [97, 20, 100, 70, 100, 20] + ] + + self.assertRaises( + IndexError, lambda: mi.sort_together(iterables, key_list=(5,)) + ) + + def test_reverse(self): + """tests `reverse` to ensure a reverse sort for `key_list` iterables""" + iterables = [ + ['GA', 'GA', 'GA', 'CT', 'CT', 'CT'], + ['May', 'Aug.', 'May', 'June', 'July', 'July'], + [97, 20, 100, 70, 100, 20] + ] + + self.assertEqual( + mi.sort_together(iterables, key_list=(0, 1, 2), reverse=True), + [('GA', 'GA', 'GA', 'CT', 'CT', 'CT'), + ('May', 'May', 'Aug.', 'June', 'July', 'July'), + (100, 97, 20, 70, 100, 20)] + ) + + def test_uneven_iterables(self): + """tests trimming of iterables to the shortest length before sorting""" + iterables = [['GA', 'GA', 'GA', 'CT', 'CT', 'CT', 'MA'], + ['May', 'Aug.', 'May', 'June', 'July', 'July'], + [97, 20, 100, 70, 100, 20, 0]] + + self.assertEqual( + mi.sort_together(iterables), + [ + ('CT', 'CT', 'CT', 'GA', 'GA', 'GA'), + ('June', 'July', 'July', 'May', 'Aug.', 'May'), + (70, 100, 20, 97, 20, 100) + ] + ) + + +class DivideTest(TestCase): + """Tests for divide()""" + + def test_invalid_n(self): + self.assertRaises(ValueError, lambda: mi.divide(-1, [1, 2, 3])) + self.assertRaises(ValueError, lambda: mi.divide(0, [1, 2, 3])) + + def test_basic(self): + iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + + for n, expected in [ + (1, [iterable]), + (2, [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]), + (3, [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]), + (10, [[n] for n in range(1, 10 + 1)]), + ]: + self.assertEqual( + [list(x) for x in mi.divide(n, iterable)], expected + ) + + def test_large_n(self): + iterable = [1, 2, 3, 4] + self.assertEqual( + [list(x) for x in mi.divide(6, iterable)], + [[1], [2], [3], [4], [], []] + ) + + +class TestAlwaysIterable(TestCase): + """Tests for always_iterable()""" + def test_single(self): + self.assertEqual(list(mi.always_iterable(1)), [1]) + + def test_strings(self): + for obj in ['foo', b'bar', u'baz']: + actual = list(mi.always_iterable(obj)) + expected = [obj] + self.assertEqual(actual, expected) + + def test_base_type(self): + dict_obj = {'a': 1, 'b': 2} + str_obj = '123' + + # Default: dicts are iterable like they normally are + default_actual = list(mi.always_iterable(dict_obj)) + default_expected = list(dict_obj) + self.assertEqual(default_actual, default_expected) + + # Unitary types set: dicts are not iterable + custom_actual = list(mi.always_iterable(dict_obj, base_type=dict)) + custom_expected = [dict_obj] + self.assertEqual(custom_actual, custom_expected) + + # With unitary types set, strings are iterable + str_actual = list(mi.always_iterable(str_obj, base_type=None)) + str_expected = list(str_obj) + self.assertEqual(str_actual, str_expected) + + def test_iterables(self): + self.assertEqual(list(mi.always_iterable([0, 1])), [0, 1]) + self.assertEqual( + list(mi.always_iterable([0, 1], base_type=list)), [[0, 1]] + ) + self.assertEqual( + list(mi.always_iterable(iter('foo'))), ['f', 'o', 'o'] + ) + self.assertEqual(list(mi.always_iterable([])), []) + + def test_none(self): + self.assertEqual(list(mi.always_iterable(None)), []) + + def test_generator(self): + def _gen(): + yield 0 + yield 1 + + self.assertEqual(list(mi.always_iterable(_gen())), [0, 1]) + + +class AdjacentTests(TestCase): + def test_typical(self): + actual = list(mi.adjacent(lambda x: x % 5 == 0, range(10))) + expected = [(True, 0), (True, 1), (False, 2), (False, 3), (True, 4), + (True, 5), (True, 6), (False, 7), (False, 8), (False, 9)] + self.assertEqual(actual, expected) + + def test_empty_iterable(self): + actual = list(mi.adjacent(lambda x: x % 5 == 0, [])) + expected = [] + self.assertEqual(actual, expected) + + def test_length_one(self): + actual = list(mi.adjacent(lambda x: x % 5 == 0, [0])) + expected = [(True, 0)] + self.assertEqual(actual, expected) + + actual = list(mi.adjacent(lambda x: x % 5 == 0, [1])) + expected = [(False, 1)] + self.assertEqual(actual, expected) + + def test_consecutive_true(self): + """Test that when the predicate matches multiple consecutive elements + it doesn't repeat elements in the output""" + actual = list(mi.adjacent(lambda x: x % 5 < 2, range(10))) + expected = [(True, 0), (True, 1), (True, 2), (False, 3), (True, 4), + (True, 5), (True, 6), (True, 7), (False, 8), (False, 9)] + self.assertEqual(actual, expected) + + def test_distance(self): + actual = list(mi.adjacent(lambda x: x % 5 == 0, range(10), distance=2)) + expected = [(True, 0), (True, 1), (True, 2), (True, 3), (True, 4), + (True, 5), (True, 6), (True, 7), (False, 8), (False, 9)] + self.assertEqual(actual, expected) + + actual = list(mi.adjacent(lambda x: x % 5 == 0, range(10), distance=3)) + expected = [(True, 0), (True, 1), (True, 2), (True, 3), (True, 4), + (True, 5), (True, 6), (True, 7), (True, 8), (False, 9)] + self.assertEqual(actual, expected) + + def test_large_distance(self): + """Test distance larger than the length of the iterable""" + iterable = range(10) + actual = list(mi.adjacent(lambda x: x % 5 == 4, iterable, distance=20)) + expected = list(zip(repeat(True), iterable)) + self.assertEqual(actual, expected) + + actual = list(mi.adjacent(lambda x: False, iterable, distance=20)) + expected = list(zip(repeat(False), iterable)) + self.assertEqual(actual, expected) + + def test_zero_distance(self): + """Test that adjacent() reduces to zip+map when distance is 0""" + iterable = range(1000) + predicate = lambda x: x % 4 == 2 + actual = mi.adjacent(predicate, iterable, 0) + expected = zip(map(predicate, iterable), iterable) + self.assertTrue(all(a == e for a, e in zip(actual, expected))) + + def test_negative_distance(self): + """Test that adjacent() raises an error with negative distance""" + pred = lambda x: x + self.assertRaises( + ValueError, lambda: mi.adjacent(pred, range(1000), -1) + ) + self.assertRaises( + ValueError, lambda: mi.adjacent(pred, range(10), -10) + ) + + def test_grouping(self): + """Test interaction of adjacent() with groupby_transform()""" + iterable = mi.adjacent(lambda x: x % 5 == 0, range(10)) + grouper = mi.groupby_transform(iterable, itemgetter(0), itemgetter(1)) + actual = [(k, list(g)) for k, g in grouper] + expected = [ + (True, [0, 1]), + (False, [2, 3]), + (True, [4, 5, 6]), + (False, [7, 8, 9]), + ] + self.assertEqual(actual, expected) + + def test_call_once(self): + """Test that the predicate is only called once per item.""" + already_seen = set() + iterable = range(10) + + def predicate(item): + self.assertNotIn(item, already_seen) + already_seen.add(item) + return True + + actual = list(mi.adjacent(predicate, iterable)) + expected = [(True, x) for x in iterable] + self.assertEqual(actual, expected) + + +class GroupByTransformTests(TestCase): + def assertAllGroupsEqual(self, groupby1, groupby2): + """Compare two groupby objects for equality, both keys and groups.""" + for a, b in zip(groupby1, groupby2): + key1, group1 = a + key2, group2 = b + self.assertEqual(key1, key2) + self.assertListEqual(list(group1), list(group2)) + self.assertRaises(StopIteration, lambda: next(groupby1)) + self.assertRaises(StopIteration, lambda: next(groupby2)) + + def test_default_funcs(self): + """Test that groupby_transform() with default args mimics groupby()""" + iterable = [(x // 5, x) for x in range(1000)] + actual = mi.groupby_transform(iterable) + expected = groupby(iterable) + self.assertAllGroupsEqual(actual, expected) + + def test_valuefunc(self): + iterable = [(int(x / 5), int(x / 3), x) for x in range(10)] + + # Test the standard usage of grouping one iterable using another's keys + grouper = mi.groupby_transform( + iterable, keyfunc=itemgetter(0), valuefunc=itemgetter(-1) + ) + actual = [(k, list(g)) for k, g in grouper] + expected = [(0, [0, 1, 2, 3, 4]), (1, [5, 6, 7, 8, 9])] + self.assertEqual(actual, expected) + + grouper = mi.groupby_transform( + iterable, keyfunc=itemgetter(1), valuefunc=itemgetter(-1) + ) + actual = [(k, list(g)) for k, g in grouper] + expected = [(0, [0, 1, 2]), (1, [3, 4, 5]), (2, [6, 7, 8]), (3, [9])] + self.assertEqual(actual, expected) + + # and now for something a little different + d = dict(zip(range(10), 'abcdefghij')) + grouper = mi.groupby_transform( + range(10), keyfunc=lambda x: x // 5, valuefunc=d.get + ) + actual = [(k, ''.join(g)) for k, g in grouper] + expected = [(0, 'abcde'), (1, 'fghij')] + self.assertEqual(actual, expected) + + def test_no_valuefunc(self): + iterable = range(1000) + + def key(x): + return x // 5 + + actual = mi.groupby_transform(iterable, key, valuefunc=None) + expected = groupby(iterable, key) + self.assertAllGroupsEqual(actual, expected) + + actual = mi.groupby_transform(iterable, key) # default valuefunc + expected = groupby(iterable, key) + self.assertAllGroupsEqual(actual, expected) + + +class NumericRangeTests(TestCase): + def test_basic(self): + for args, expected in [ + ((4,), [0, 1, 2, 3]), + ((4.0,), [0.0, 1.0, 2.0, 3.0]), + ((1.0, 4), [1.0, 2.0, 3.0]), + ((1, 4.0), [1, 2, 3]), + ((1.0, 5), [1.0, 2.0, 3.0, 4.0]), + ((0, 20, 5), [0, 5, 10, 15]), + ((0, 20, 5.0), [0.0, 5.0, 10.0, 15.0]), + ((0, 10, 3), [0, 3, 6, 9]), + ((0, 10, 3.0), [0.0, 3.0, 6.0, 9.0]), + ((0, -5, -1), [0, -1, -2, -3, -4]), + ((0.0, -5, -1), [0.0, -1.0, -2.0, -3.0, -4.0]), + ((1, 2, Fraction(1, 2)), [Fraction(1, 1), Fraction(3, 2)]), + ((0,), []), + ((0.0,), []), + ((1, 0), []), + ((1.0, 0.0), []), + ((Fraction(2, 1),), [Fraction(0, 1), Fraction(1, 1)]), + ((Decimal('2.0'),), [Decimal('0.0'), Decimal('1.0')]), + ]: + actual = list(mi.numeric_range(*args)) + self.assertEqual(actual, expected) + self.assertTrue( + all(type(a) == type(e) for a, e in zip(actual, expected)) + ) + + def test_arg_count(self): + self.assertRaises(TypeError, lambda: list(mi.numeric_range())) + self.assertRaises( + TypeError, lambda: list(mi.numeric_range(0, 1, 2, 3)) + ) + + def test_zero_step(self): + self.assertRaises( + ValueError, lambda: list(mi.numeric_range(1, 2, 0)) + ) + + +class CountCycleTests(TestCase): + def test_basic(self): + expected = [ + (0, 'a'), (0, 'b'), (0, 'c'), + (1, 'a'), (1, 'b'), (1, 'c'), + (2, 'a'), (2, 'b'), (2, 'c'), + ] + for actual in [ + mi.take(9, mi.count_cycle('abc')), # n=None + list(mi.count_cycle('abc', 3)), # n=3 + ]: + self.assertEqual(actual, expected) + + def test_empty(self): + self.assertEqual(list(mi.count_cycle('')), []) + self.assertEqual(list(mi.count_cycle('', 2)), []) + + def test_negative(self): + self.assertEqual(list(mi.count_cycle('abc', -3)), []) + + +class LocateTests(TestCase): + def test_default_pred(self): + iterable = [0, 1, 1, 0, 1, 0, 0] + actual = list(mi.locate(iterable)) + expected = [1, 2, 4] + self.assertEqual(actual, expected) + + def test_no_matches(self): + iterable = [0, 0, 0] + actual = list(mi.locate(iterable)) + expected = [] + self.assertEqual(actual, expected) + + def test_custom_pred(self): + iterable = ['0', 1, 1, '0', 1, '0', '0'] + pred = lambda x: x == '0' + actual = list(mi.locate(iterable, pred)) + expected = [0, 3, 5, 6] + self.assertEqual(actual, expected) + + +class StripFunctionTests(TestCase): + def test_hashable(self): + iterable = list('www.example.com') + pred = lambda x: x in set('cmowz.') + + self.assertEqual(list(mi.lstrip(iterable, pred)), list('example.com')) + self.assertEqual(list(mi.rstrip(iterable, pred)), list('www.example')) + self.assertEqual(list(mi.strip(iterable, pred)), list('example')) + + def test_not_hashable(self): + iterable = [ + list('http://'), list('www'), list('.example'), list('.com') + ] + pred = lambda x: x in [list('http://'), list('www'), list('.com')] + + self.assertEqual(list(mi.lstrip(iterable, pred)), iterable[2:]) + self.assertEqual(list(mi.rstrip(iterable, pred)), iterable[:3]) + self.assertEqual(list(mi.strip(iterable, pred)), iterable[2: 3]) + + def test_math(self): + iterable = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2] + pred = lambda x: x <= 2 + + self.assertEqual(list(mi.lstrip(iterable, pred)), iterable[3:]) + self.assertEqual(list(mi.rstrip(iterable, pred)), iterable[:-3]) + self.assertEqual(list(mi.strip(iterable, pred)), iterable[3:-3]) + + +class IsliceExtendedTests(TestCase): + def test_all(self): + iterable = ['0', '1', '2', '3', '4', '5'] + indexes = list(range(-4, len(iterable) + 4)) + [None] + steps = [1, 2, 3, 4, -1, -2, -3, 4] + for slice_args in product(indexes, indexes, steps): + try: + actual = list(mi.islice_extended(iterable, *slice_args)) + except Exception as e: + self.fail((slice_args, e)) + + expected = iterable[slice(*slice_args)] + self.assertEqual(actual, expected, slice_args) + + def test_zero_step(self): + with self.assertRaises(ValueError): + list(mi.islice_extended([1, 2, 3], 0, 1, 0)) + + +class ConsecutiveGroupsTest(TestCase): + def test_numbers(self): + iterable = [-10, -8, -7, -6, 1, 2, 4, 5, -1, 7] + actual = [list(g) for g in mi.consecutive_groups(iterable)] + expected = [[-10], [-8, -7, -6], [1, 2], [4, 5], [-1], [7]] + self.assertEqual(actual, expected) + + def test_custom_ordering(self): + iterable = ['1', '10', '11', '20', '21', '22', '30', '31'] + ordering = lambda x: int(x) + actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)] + expected = [['1'], ['10', '11'], ['20', '21', '22'], ['30', '31']] + self.assertEqual(actual, expected) + + def test_exotic_ordering(self): + iterable = [ + ('a', 'b', 'c', 'd'), + ('a', 'c', 'b', 'd'), + ('a', 'c', 'd', 'b'), + ('a', 'd', 'b', 'c'), + ('d', 'b', 'c', 'a'), + ('d', 'c', 'a', 'b'), + ] + ordering = list(permutations('abcd')).index + actual = [list(g) for g in mi.consecutive_groups(iterable, ordering)] + expected = [ + [('a', 'b', 'c', 'd')], + [('a', 'c', 'b', 'd'), ('a', 'c', 'd', 'b'), ('a', 'd', 'b', 'c')], + [('d', 'b', 'c', 'a'), ('d', 'c', 'a', 'b')], + ] + self.assertEqual(actual, expected) + + +class DifferenceTest(TestCase): + def test_normal(self): + iterable = [10, 20, 30, 40, 50] + actual = list(mi.difference(iterable)) + expected = [10, 10, 10, 10, 10] + self.assertEqual(actual, expected) + + def test_custom(self): + iterable = [10, 20, 30, 40, 50] + actual = list(mi.difference(iterable, add)) + expected = [10, 30, 50, 70, 90] + self.assertEqual(actual, expected) + + def test_roundtrip(self): + original = list(range(100)) + accumulated = mi.accumulate(original) + actual = list(mi.difference(accumulated)) + self.assertEqual(actual, original) + + def test_one(self): + self.assertEqual(list(mi.difference([0])), [0]) + + def test_empty(self): + self.assertEqual(list(mi.difference([])), []) + + +class SeekableTest(TestCase): + def test_exhaustion_reset(self): + iterable = [str(n) for n in range(10)] + + s = mi.seekable(iterable) + self.assertEqual(list(s), iterable) # Normal iteration + self.assertEqual(list(s), []) # Iterable is exhausted + + s.seek(0) + self.assertEqual(list(s), iterable) # Back in action + + def test_partial_reset(self): + iterable = [str(n) for n in range(10)] + + s = mi.seekable(iterable) + self.assertEqual(mi.take(5, s), iterable[:5]) # Normal iteration + + s.seek(1) + self.assertEqual(list(s), iterable[1:]) # Get the rest of the iterable + + def test_forward(self): + iterable = [str(n) for n in range(10)] + + s = mi.seekable(iterable) + self.assertEqual(mi.take(1, s), iterable[:1]) # Normal iteration + + s.seek(3) # Skip over index 2 + self.assertEqual(list(s), iterable[3:]) # Result is similar to slicing + + s.seek(0) # Back to 0 + self.assertEqual(list(s), iterable) # No difference in result + + def test_past_end(self): + iterable = [str(n) for n in range(10)] + + s = mi.seekable(iterable) + self.assertEqual(mi.take(1, s), iterable[:1]) # Normal iteration + + s.seek(20) + self.assertEqual(list(s), []) # Iterable is exhausted + + s.seek(0) # Back to 0 + self.assertEqual(list(s), iterable) # No difference in result + + def test_elements(self): + iterable = map(str, count()) + + s = mi.seekable(iterable) + mi.take(10, s) + + elements = s.elements() + self.assertEqual( + [elements[i] for i in range(10)], [str(n) for n in range(10)] + ) + self.assertEqual(len(elements), 10) + + mi.take(10, s) + self.assertEqual(list(elements), [str(n) for n in range(20)]) + + +class SequenceViewTests(TestCase): + def test_init(self): + view = mi.SequenceView((1, 2, 3)) + self.assertEqual(repr(view), "SequenceView((1, 2, 3))") + self.assertRaises(TypeError, lambda: mi.SequenceView({})) + + def test_update(self): + seq = [1, 2, 3] + view = mi.SequenceView(seq) + self.assertEqual(len(view), 3) + self.assertEqual(repr(view), "SequenceView([1, 2, 3])") + + seq.pop() + self.assertEqual(len(view), 2) + self.assertEqual(repr(view), "SequenceView([1, 2])") + + def test_indexing(self): + seq = ('a', 'b', 'c', 'd', 'e', 'f') + view = mi.SequenceView(seq) + for i in range(-len(seq), len(seq)): + self.assertEqual(view[i], seq[i]) + + def test_slicing(self): + seq = ('a', 'b', 'c', 'd', 'e', 'f') + view = mi.SequenceView(seq) + n = len(seq) + indexes = list(range(-n - 1, n + 1)) + [None] + steps = list(range(-n, n + 1)) + steps.remove(0) + for slice_args in product(indexes, indexes, steps): + i = slice(*slice_args) + self.assertEqual(view[i], seq[i]) + + def test_abc_methods(self): + # collections.Sequence should provide all of this functionality + seq = ('a', 'b', 'c', 'd', 'e', 'f', 'f') + view = mi.SequenceView(seq) + + # __contains__ + self.assertIn('b', view) + self.assertNotIn('g', view) + + # __iter__ + self.assertEqual(list(iter(view)), list(seq)) + + # __reversed__ + self.assertEqual(list(reversed(view)), list(reversed(seq))) + + # index + self.assertEqual(view.index('b'), 1) + + # count + self.assertEqual(seq.count('f'), 2) + + +class RunLengthTest(TestCase): + def test_encode(self): + iterable = (int(str(n)[0]) for n in count(800)) + actual = mi.take(4, mi.run_length.encode(iterable)) + expected = [(8, 100), (9, 100), (1, 1000), (2, 1000)] + self.assertEqual(actual, expected) + + def test_decode(self): + iterable = [('d', 4), ('c', 3), ('b', 2), ('a', 1)] + actual = ''.join(mi.run_length.decode(iterable)) + expected = 'ddddcccbba' + self.assertEqual(actual, expected) + + +class ExactlyNTests(TestCase): + """Tests for ``exactly_n()``""" + + def test_true(self): + """Iterable has ``n`` ``True`` elements""" + self.assertTrue(mi.exactly_n([True, False, True], 2)) + self.assertTrue(mi.exactly_n([1, 1, 1, 0], 3)) + self.assertTrue(mi.exactly_n([False, False], 0)) + self.assertTrue(mi.exactly_n(range(100), 10, lambda x: x < 10)) + + def test_false(self): + """Iterable does not have ``n`` ``True`` elements""" + self.assertFalse(mi.exactly_n([True, False, False], 2)) + self.assertFalse(mi.exactly_n([True, True, False], 1)) + self.assertFalse(mi.exactly_n([False], 1)) + self.assertFalse(mi.exactly_n([True], -1)) + self.assertFalse(mi.exactly_n(repeat(True), 100)) + + def test_empty(self): + """Return ``True`` if the iterable is empty and ``n`` is 0""" + self.assertTrue(mi.exactly_n([], 0)) + self.assertFalse(mi.exactly_n([], 1)) + + +class AlwaysReversibleTests(TestCase): + """Tests for ``always_reversible()``""" + + def test_regular_reversed(self): + self.assertEqual(list(reversed(range(10))), + list(mi.always_reversible(range(10)))) + self.assertEqual(list(reversed([1, 2, 3])), + list(mi.always_reversible([1, 2, 3]))) + self.assertEqual(reversed([1, 2, 3]).__class__, + mi.always_reversible([1, 2, 3]).__class__) + + def test_nonseq_reversed(self): + # Create a non-reversible generator from a sequence + with self.assertRaises(TypeError): + reversed(x for x in range(10)) + + self.assertEqual(list(reversed(range(10))), + list(mi.always_reversible(x for x in range(10)))) + self.assertEqual(list(reversed([1, 2, 3])), + list(mi.always_reversible(x for x in [1, 2, 3]))) + self.assertNotEqual(reversed((1, 2)).__class__, + mi.always_reversible(x for x in (1, 2)).__class__) + + +class CircularShiftsTests(TestCase): + def test_empty(self): + # empty iterable -> empty list + self.assertEqual(list(mi.circular_shifts([])), []) + + def test_simple_circular_shifts(self): + # test the a simple iterator case + self.assertEqual( + mi.circular_shifts(range(4)), + [(0, 1, 2, 3), (1, 2, 3, 0), (2, 3, 0, 1), (3, 0, 1, 2)] + ) + + def test_duplicates(self): + # test non-distinct entries + self.assertEqual( + mi.circular_shifts([0, 1, 0, 1]), + [(0, 1, 0, 1), (1, 0, 1, 0), (0, 1, 0, 1), (1, 0, 1, 0)] + ) + + +class MakeDecoratorTests(TestCase): + def test_basic(self): + slicer = mi.make_decorator(islice) + + @slicer(1, 10, 2) + def user_function(arg_1, arg_2, kwarg_1=None): + self.assertEqual(arg_1, 'arg_1') + self.assertEqual(arg_2, 'arg_2') + self.assertEqual(kwarg_1, 'kwarg_1') + return map(str, count()) + + it = user_function('arg_1', 'arg_2', kwarg_1='kwarg_1') + actual = list(it) + expected = ['1', '3', '5', '7', '9'] + self.assertEqual(actual, expected) + + def test_result_index(self): + def stringify(*args, **kwargs): + self.assertEqual(args[0], 'arg_0') + iterable = args[1] + self.assertEqual(args[2], 'arg_2') + self.assertEqual(kwargs['kwarg_1'], 'kwarg_1') + return map(str, iterable) + + stringifier = mi.make_decorator(stringify, result_index=1) + + @stringifier('arg_0', 'arg_2', kwarg_1='kwarg_1') + def user_function(n): + return count(n) + + it = user_function(1) + actual = mi.take(5, it) + expected = ['1', '2', '3', '4', '5'] + self.assertEqual(actual, expected) + + def test_wrap_class(self): + seeker = mi.make_decorator(mi.seekable) + + @seeker() + def user_function(n): + return map(str, range(n)) + + it = user_function(5) + self.assertEqual(list(it), ['0', '1', '2', '3', '4']) + + it.seek(0) + self.assertEqual(list(it), ['0', '1', '2', '3', '4']) + + +class MapReduceTests(TestCase): + def test_default(self): + iterable = (str(x) for x in range(5)) + keyfunc = lambda x: int(x) // 2 + actual = sorted(mi.map_reduce(iterable, keyfunc).items()) + expected = [(0, ['0', '1']), (1, ['2', '3']), (2, ['4'])] + self.assertEqual(actual, expected) + + def test_valuefunc(self): + iterable = (str(x) for x in range(5)) + keyfunc = lambda x: int(x) // 2 + valuefunc = int + actual = sorted(mi.map_reduce(iterable, keyfunc, valuefunc).items()) + expected = [(0, [0, 1]), (1, [2, 3]), (2, [4])] + self.assertEqual(actual, expected) + + def test_reducefunc(self): + iterable = (str(x) for x in range(5)) + keyfunc = lambda x: int(x) // 2 + valuefunc = int + reducefunc = lambda value_list: reduce(mul, value_list, 1) + actual = sorted( + mi.map_reduce(iterable, keyfunc, valuefunc, reducefunc).items() + ) + expected = [(0, 0), (1, 6), (2, 4)] + self.assertEqual(actual, expected) + + def test_ret(self): + d = mi.map_reduce([1, 0, 2, 0, 1, 0], bool) + self.assertEqual(d, {False: [0, 0, 0], True: [1, 2, 1]}) + self.assertRaises(KeyError, lambda: d[None].append(1)) diff --git a/third_party/python/more-itertools/more_itertools/tests/test_recipes.py b/third_party/python/more-itertools/more_itertools/tests/test_recipes.py new file mode 100644 index 000000000000..81721fdf9fe9 --- /dev/null +++ b/third_party/python/more-itertools/more_itertools/tests/test_recipes.py @@ -0,0 +1,607 @@ +from doctest import DocTestSuite +from unittest import TestCase + +from itertools import combinations +from six.moves import range + +import more_itertools as mi + + +def load_tests(loader, tests, ignore): + # Add the doctests + tests.addTests(DocTestSuite('more_itertools.recipes')) + return tests + + +class AccumulateTests(TestCase): + """Tests for ``accumulate()``""" + + def test_empty(self): + """Test that an empty input returns an empty output""" + self.assertEqual(list(mi.accumulate([])), []) + + def test_default(self): + """Test accumulate with the default function (addition)""" + self.assertEqual(list(mi.accumulate([1, 2, 3])), [1, 3, 6]) + + def test_bogus_function(self): + """Test accumulate with an invalid function""" + with self.assertRaises(TypeError): + list(mi.accumulate([1, 2, 3], func=lambda x: x)) + + def test_custom_function(self): + """Test accumulate with a custom function""" + self.assertEqual( + list(mi.accumulate((1, 2, 3, 2, 1), func=max)), [1, 2, 3, 3, 3] + ) + + +class TakeTests(TestCase): + """Tests for ``take()``""" + + def test_simple_take(self): + """Test basic usage""" + t = mi.take(5, range(10)) + self.assertEqual(t, [0, 1, 2, 3, 4]) + + def test_null_take(self): + """Check the null case""" + t = mi.take(0, range(10)) + self.assertEqual(t, []) + + def test_negative_take(self): + """Make sure taking negative items results in a ValueError""" + self.assertRaises(ValueError, lambda: mi.take(-3, range(10))) + + def test_take_too_much(self): + """Taking more than an iterator has remaining should return what the + iterator has remaining. + + """ + t = mi.take(10, range(5)) + self.assertEqual(t, [0, 1, 2, 3, 4]) + + +class TabulateTests(TestCase): + """Tests for ``tabulate()``""" + + def test_simple_tabulate(self): + """Test the happy path""" + t = mi.tabulate(lambda x: x) + f = tuple([next(t) for _ in range(3)]) + self.assertEqual(f, (0, 1, 2)) + + def test_count(self): + """Ensure tabulate accepts specific count""" + t = mi.tabulate(lambda x: 2 * x, -1) + f = (next(t), next(t), next(t)) + self.assertEqual(f, (-2, 0, 2)) + + +class TailTests(TestCase): + """Tests for ``tail()``""" + + def test_greater(self): + """Length of iterable is greather than requested tail""" + self.assertEqual(list(mi.tail(3, 'ABCDEFG')), ['E', 'F', 'G']) + + def test_equal(self): + """Length of iterable is equal to the requested tail""" + self.assertEqual( + list(mi.tail(7, 'ABCDEFG')), ['A', 'B', 'C', 'D', 'E', 'F', 'G'] + ) + + def test_less(self): + """Length of iterable is less than requested tail""" + self.assertEqual( + list(mi.tail(8, 'ABCDEFG')), ['A', 'B', 'C', 'D', 'E', 'F', 'G'] + ) + + +class ConsumeTests(TestCase): + """Tests for ``consume()``""" + + def test_sanity(self): + """Test basic functionality""" + r = (x for x in range(10)) + mi.consume(r, 3) + self.assertEqual(3, next(r)) + + def test_null_consume(self): + """Check the null case""" + r = (x for x in range(10)) + mi.consume(r, 0) + self.assertEqual(0, next(r)) + + def test_negative_consume(self): + """Check that negative consumsion throws an error""" + r = (x for x in range(10)) + self.assertRaises(ValueError, lambda: mi.consume(r, -1)) + + def test_total_consume(self): + """Check that iterator is totally consumed by default""" + r = (x for x in range(10)) + mi.consume(r) + self.assertRaises(StopIteration, lambda: next(r)) + + +class NthTests(TestCase): + """Tests for ``nth()``""" + + def test_basic(self): + """Make sure the nth item is returned""" + l = range(10) + for i, v in enumerate(l): + self.assertEqual(mi.nth(l, i), v) + + def test_default(self): + """Ensure a default value is returned when nth item not found""" + l = range(3) + self.assertEqual(mi.nth(l, 100, "zebra"), "zebra") + + def test_negative_item_raises(self): + """Ensure asking for a negative item raises an exception""" + self.assertRaises(ValueError, lambda: mi.nth(range(10), -3)) + + +class AllEqualTests(TestCase): + """Tests for ``all_equal()``""" + + def test_true(self): + """Everything is equal""" + self.assertTrue(mi.all_equal('aaaaaa')) + self.assertTrue(mi.all_equal([0, 0, 0, 0])) + + def test_false(self): + """Not everything is equal""" + self.assertFalse(mi.all_equal('aaaaab')) + self.assertFalse(mi.all_equal([0, 0, 0, 1])) + + def test_tricky(self): + """Not everything is identical, but everything is equal""" + items = [1, complex(1, 0), 1.0] + self.assertTrue(mi.all_equal(items)) + + def test_empty(self): + """Return True if the iterable is empty""" + self.assertTrue(mi.all_equal('')) + self.assertTrue(mi.all_equal([])) + + def test_one(self): + """Return True if the iterable is singular""" + self.assertTrue(mi.all_equal('0')) + self.assertTrue(mi.all_equal([0])) + + +class QuantifyTests(TestCase): + """Tests for ``quantify()``""" + + def test_happy_path(self): + """Make sure True count is returned""" + q = [True, False, True] + self.assertEqual(mi.quantify(q), 2) + + def test_custom_predicate(self): + """Ensure non-default predicates return as expected""" + q = range(10) + self.assertEqual(mi.quantify(q, lambda x: x % 2 == 0), 5) + + +class PadnoneTests(TestCase): + """Tests for ``padnone()``""" + + def test_happy_path(self): + """wrapper iterator should return None indefinitely""" + r = range(2) + p = mi.padnone(r) + self.assertEqual([0, 1, None, None], [next(p) for _ in range(4)]) + + +class NcyclesTests(TestCase): + """Tests for ``nyclces()``""" + + def test_happy_path(self): + """cycle a sequence three times""" + r = ["a", "b", "c"] + n = mi.ncycles(r, 3) + self.assertEqual( + ["a", "b", "c", "a", "b", "c", "a", "b", "c"], + list(n) + ) + + def test_null_case(self): + """asking for 0 cycles should return an empty iterator""" + n = mi.ncycles(range(100), 0) + self.assertRaises(StopIteration, lambda: next(n)) + + def test_pathalogical_case(self): + """asking for negative cycles should return an empty iterator""" + n = mi.ncycles(range(100), -10) + self.assertRaises(StopIteration, lambda: next(n)) + + +class DotproductTests(TestCase): + """Tests for ``dotproduct()``'""" + + def test_happy_path(self): + """simple dotproduct example""" + self.assertEqual(400, mi.dotproduct([10, 10], [20, 20])) + + +class FlattenTests(TestCase): + """Tests for ``flatten()``""" + + def test_basic_usage(self): + """ensure list of lists is flattened one level""" + f = [[0, 1, 2], [3, 4, 5]] + self.assertEqual(list(range(6)), list(mi.flatten(f))) + + def test_single_level(self): + """ensure list of lists is flattened only one level""" + f = [[0, [1, 2]], [[3, 4], 5]] + self.assertEqual([0, [1, 2], [3, 4], 5], list(mi.flatten(f))) + + +class RepeatfuncTests(TestCase): + """Tests for ``repeatfunc()``""" + + def test_simple_repeat(self): + """test simple repeated functions""" + r = mi.repeatfunc(lambda: 5) + self.assertEqual([5, 5, 5, 5, 5], [next(r) for _ in range(5)]) + + def test_finite_repeat(self): + """ensure limited repeat when times is provided""" + r = mi.repeatfunc(lambda: 5, times=5) + self.assertEqual([5, 5, 5, 5, 5], list(r)) + + def test_added_arguments(self): + """ensure arguments are applied to the function""" + r = mi.repeatfunc(lambda x: x, 2, 3) + self.assertEqual([3, 3], list(r)) + + def test_null_times(self): + """repeat 0 should return an empty iterator""" + r = mi.repeatfunc(range, 0, 3) + self.assertRaises(StopIteration, lambda: next(r)) + + +class PairwiseTests(TestCase): + """Tests for ``pairwise()``""" + + def test_base_case(self): + """ensure an iterable will return pairwise""" + p = mi.pairwise([1, 2, 3]) + self.assertEqual([(1, 2), (2, 3)], list(p)) + + def test_short_case(self): + """ensure an empty iterator if there's not enough values to pair""" + p = mi.pairwise("a") + self.assertRaises(StopIteration, lambda: next(p)) + + +class GrouperTests(TestCase): + """Tests for ``grouper()``""" + + def test_even(self): + """Test when group size divides evenly into the length of + the iterable. + + """ + self.assertEqual( + list(mi.grouper(3, 'ABCDEF')), [('A', 'B', 'C'), ('D', 'E', 'F')] + ) + + def test_odd(self): + """Test when group size does not divide evenly into the length of the + iterable. + + """ + self.assertEqual( + list(mi.grouper(3, 'ABCDE')), [('A', 'B', 'C'), ('D', 'E', None)] + ) + + def test_fill_value(self): + """Test that the fill value is used to pad the final group""" + self.assertEqual( + list(mi.grouper(3, 'ABCDE', 'x')), + [('A', 'B', 'C'), ('D', 'E', 'x')] + ) + + +class RoundrobinTests(TestCase): + """Tests for ``roundrobin()``""" + + def test_even_groups(self): + """Ensure ordered output from evenly populated iterables""" + self.assertEqual( + list(mi.roundrobin('ABC', [1, 2, 3], range(3))), + ['A', 1, 0, 'B', 2, 1, 'C', 3, 2] + ) + + def test_uneven_groups(self): + """Ensure ordered output from unevenly populated iterables""" + self.assertEqual( + list(mi.roundrobin('ABCD', [1, 2], range(0))), + ['A', 1, 'B', 2, 'C', 'D'] + ) + + +class PartitionTests(TestCase): + """Tests for ``partition()``""" + + def test_bool(self): + """Test when pred() returns a boolean""" + lesser, greater = mi.partition(lambda x: x > 5, range(10)) + self.assertEqual(list(lesser), [0, 1, 2, 3, 4, 5]) + self.assertEqual(list(greater), [6, 7, 8, 9]) + + def test_arbitrary(self): + """Test when pred() returns an integer""" + divisibles, remainders = mi.partition(lambda x: x % 3, range(10)) + self.assertEqual(list(divisibles), [0, 3, 6, 9]) + self.assertEqual(list(remainders), [1, 2, 4, 5, 7, 8]) + + +class PowersetTests(TestCase): + """Tests for ``powerset()``""" + + def test_combinatorics(self): + """Ensure a proper enumeration""" + p = mi.powerset([1, 2, 3]) + self.assertEqual( + list(p), + [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] + ) + + +class UniqueEverseenTests(TestCase): + """Tests for ``unique_everseen()``""" + + def test_everseen(self): + """ensure duplicate elements are ignored""" + u = mi.unique_everseen('AAAABBBBCCDAABBB') + self.assertEqual( + ['A', 'B', 'C', 'D'], + list(u) + ) + + def test_custom_key(self): + """ensure the custom key comparison works""" + u = mi.unique_everseen('aAbACCc', key=str.lower) + self.assertEqual(list('abC'), list(u)) + + def test_unhashable(self): + """ensure things work for unhashable items""" + iterable = ['a', [1, 2, 3], [1, 2, 3], 'a'] + u = mi.unique_everseen(iterable) + self.assertEqual(list(u), ['a', [1, 2, 3]]) + + def test_unhashable_key(self): + """ensure things work for unhashable items with a custom key""" + iterable = ['a', [1, 2, 3], [1, 2, 3], 'a'] + u = mi.unique_everseen(iterable, key=lambda x: x) + self.assertEqual(list(u), ['a', [1, 2, 3]]) + + +class UniqueJustseenTests(TestCase): + """Tests for ``unique_justseen()``""" + + def test_justseen(self): + """ensure only last item is remembered""" + u = mi.unique_justseen('AAAABBBCCDABB') + self.assertEqual(list('ABCDAB'), list(u)) + + def test_custom_key(self): + """ensure the custom key comparison works""" + u = mi.unique_justseen('AABCcAD', str.lower) + self.assertEqual(list('ABCAD'), list(u)) + + +class IterExceptTests(TestCase): + """Tests for ``iter_except()``""" + + def test_exact_exception(self): + """ensure the exact specified exception is caught""" + l = [1, 2, 3] + i = mi.iter_except(l.pop, IndexError) + self.assertEqual(list(i), [3, 2, 1]) + + def test_generic_exception(self): + """ensure the generic exception can be caught""" + l = [1, 2] + i = mi.iter_except(l.pop, Exception) + self.assertEqual(list(i), [2, 1]) + + def test_uncaught_exception_is_raised(self): + """ensure a non-specified exception is raised""" + l = [1, 2, 3] + i = mi.iter_except(l.pop, KeyError) + self.assertRaises(IndexError, lambda: list(i)) + + def test_first(self): + """ensure first is run before the function""" + l = [1, 2, 3] + f = lambda: 25 + i = mi.iter_except(l.pop, IndexError, f) + self.assertEqual(list(i), [25, 3, 2, 1]) + + +class FirstTrueTests(TestCase): + """Tests for ``first_true()``""" + + def test_something_true(self): + """Test with no keywords""" + self.assertEqual(mi.first_true(range(10)), 1) + + def test_nothing_true(self): + """Test default return value.""" + self.assertEqual(mi.first_true([0, 0, 0]), False) + + def test_default(self): + """Test with a default keyword""" + self.assertEqual(mi.first_true([0, 0, 0], default='!'), '!') + + def test_pred(self): + """Test with a custom predicate""" + self.assertEqual( + mi.first_true([2, 4, 6], pred=lambda x: x % 3 == 0), 6 + ) + + +class RandomProductTests(TestCase): + """Tests for ``random_product()`` + + Since random.choice() has different results with the same seed across + python versions 2.x and 3.x, these tests use highly probably events to + create predictable outcomes across platforms. + """ + + def test_simple_lists(self): + """Ensure that one item is chosen from each list in each pair. + Also ensure that each item from each list eventually appears in + the chosen combinations. + + Odds are roughly 1 in 7.1 * 10e16 that one item from either list will + not be chosen after 100 samplings of one item from each list. Just to + be safe, better use a known random seed, too. + + """ + nums = [1, 2, 3] + lets = ['a', 'b', 'c'] + n, m = zip(*[mi.random_product(nums, lets) for _ in range(100)]) + n, m = set(n), set(m) + self.assertEqual(n, set(nums)) + self.assertEqual(m, set(lets)) + self.assertEqual(len(n), len(nums)) + self.assertEqual(len(m), len(lets)) + + def test_list_with_repeat(self): + """ensure multiple items are chosen, and that they appear to be chosen + from one list then the next, in proper order. + + """ + nums = [1, 2, 3] + lets = ['a', 'b', 'c'] + r = list(mi.random_product(nums, lets, repeat=100)) + self.assertEqual(2 * 100, len(r)) + n, m = set(r[::2]), set(r[1::2]) + self.assertEqual(n, set(nums)) + self.assertEqual(m, set(lets)) + self.assertEqual(len(n), len(nums)) + self.assertEqual(len(m), len(lets)) + + +class RandomPermutationTests(TestCase): + """Tests for ``random_permutation()``""" + + def test_full_permutation(self): + """ensure every item from the iterable is returned in a new ordering + + 15 elements have a 1 in 1.3 * 10e12 of appearing in sorted order, so + we fix a seed value just to be sure. + + """ + i = range(15) + r = mi.random_permutation(i) + self.assertEqual(set(i), set(r)) + if i == r: + raise AssertionError("Values were not permuted") + + def test_partial_permutation(self): + """ensure all returned items are from the iterable, that the returned + permutation is of the desired length, and that all items eventually + get returned. + + Sampling 100 permutations of length 5 from a set of 15 leaves a + (2/3)^100 chance that an item will not be chosen. Multiplied by 15 + items, there is a 1 in 2.6e16 chance that at least 1 item will not + show up in the resulting output. Using a random seed will fix that. + + """ + items = range(15) + item_set = set(items) + all_items = set() + for _ in range(100): + permutation = mi.random_permutation(items, 5) + self.assertEqual(len(permutation), 5) + permutation_set = set(permutation) + self.assertLessEqual(permutation_set, item_set) + all_items |= permutation_set + self.assertEqual(all_items, item_set) + + +class RandomCombinationTests(TestCase): + """Tests for ``random_combination()``""" + + def test_psuedorandomness(self): + """ensure different subsets of the iterable get returned over many + samplings of random combinations""" + items = range(15) + all_items = set() + for _ in range(50): + combination = mi.random_combination(items, 5) + all_items |= set(combination) + self.assertEqual(all_items, set(items)) + + def test_no_replacement(self): + """ensure that elements are sampled without replacement""" + items = range(15) + for _ in range(50): + combination = mi.random_combination(items, len(items)) + self.assertEqual(len(combination), len(set(combination))) + self.assertRaises( + ValueError, lambda: mi.random_combination(items, len(items) + 1) + ) + + +class RandomCombinationWithReplacementTests(TestCase): + """Tests for ``random_combination_with_replacement()``""" + + def test_replacement(self): + """ensure that elements are sampled with replacement""" + items = range(5) + combo = mi.random_combination_with_replacement(items, len(items) * 2) + self.assertEqual(2 * len(items), len(combo)) + if len(set(combo)) == len(combo): + raise AssertionError("Combination contained no duplicates") + + def test_pseudorandomness(self): + """ensure different subsets of the iterable get returned over many + samplings of random combinations""" + items = range(15) + all_items = set() + for _ in range(50): + combination = mi.random_combination_with_replacement(items, 5) + all_items |= set(combination) + self.assertEqual(all_items, set(items)) + + +class NthCombinationTests(TestCase): + def test_basic(self): + iterable = 'abcdefg' + r = 4 + for index, expected in enumerate(combinations(iterable, r)): + actual = mi.nth_combination(iterable, r, index) + self.assertEqual(actual, expected) + + def test_long(self): + actual = mi.nth_combination(range(180), 4, 2000000) + expected = (2, 12, 35, 126) + self.assertEqual(actual, expected) + + +class PrependTests(TestCase): + def test_basic(self): + value = 'a' + iterator = iter('bcdefg') + actual = list(mi.prepend(value, iterator)) + expected = list('abcdefg') + self.assertEqual(actual, expected) + + def test_multiple(self): + value = 'ab' + iterator = iter('cdefg') + actual = tuple(mi.prepend(value, iterator)) + expected = ('ab',) + tuple('cdefg') + self.assertEqual(actual, expected) diff --git a/third_party/python/more-itertools/setup.cfg b/third_party/python/more-itertools/setup.cfg new file mode 100644 index 000000000000..86c9d8ce2f5f --- /dev/null +++ b/third_party/python/more-itertools/setup.cfg @@ -0,0 +1,8 @@ +[flake8] +exclude = ./docs/conf.py, .eggs/ +ignore = E731, E741, F999 + +[egg_info] +tag_build = +tag_date = 0 + diff --git a/third_party/python/more-itertools/setup.py b/third_party/python/more-itertools/setup.py new file mode 100644 index 000000000000..484e4d06f799 --- /dev/null +++ b/third_party/python/more-itertools/setup.py @@ -0,0 +1,59 @@ +# Hack to prevent stupid error on exit of `python setup.py test`. (See +# http://www.eby-sarna.com/pipermail/peak/2010-May/003357.html.) +try: + import multiprocessing # noqa +except ImportError: + pass +from re import sub + +from setuptools import setup, find_packages + + +def get_long_description(): + # Fix display issues on PyPI caused by RST markup + readme = open('README.rst').read() + + version_lines = [] + with open('docs/versions.rst') as infile: + next(infile) + for line in infile: + line = line.rstrip().replace('.. automodule:: more_itertools', '') + version_lines.append(line) + version_history = '\n'.join(version_lines) + version_history = sub(r':func:`([a-zA-Z0-9._]+)`', r'\1', version_history) + + ret = readme + '\n\n' + version_history + return ret + + +setup( + name='more-itertools', + version='4.2.0', + description='More routines for operating on iterables, beyond itertools', + long_description=get_long_description(), + author='Erik Rose', + author_email='erikrose@grinchcentral.com', + license='MIT', + packages=find_packages(exclude=['ez_setup']), + install_requires=['six>=1.0.0,<2.0.0'], + test_suite='more_itertools.tests', + url='https://github.com/erikrose/more-itertools', + include_package_data=True, + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Natural Language :: English', + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Topic :: Software Development :: Libraries'], + keywords=['itertools', 'iterator', 'iteration', 'filter', 'peek', + 'peekable', 'collate', 'chunk', 'chunked'], +) diff --git a/third_party/python/more-itertools/tox.ini b/third_party/python/more-itertools/tox.ini new file mode 100644 index 000000000000..70c68c058d12 --- /dev/null +++ b/third_party/python/more-itertools/tox.ini @@ -0,0 +1,5 @@ +[tox] +envlist = py27, py34, py35, py36, py37 + +[testenv] +commands = {envbindir}/python -m unittest discover -v diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/LICENSE.txt b/third_party/python/pluggy/LICENSE similarity index 100% rename from third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/LICENSE.txt rename to third_party/python/pluggy/LICENSE diff --git a/third_party/python/pluggy/MANIFEST.in b/third_party/python/pluggy/MANIFEST.in new file mode 100644 index 000000000000..0cf8f3e0887c --- /dev/null +++ b/third_party/python/pluggy/MANIFEST.in @@ -0,0 +1,7 @@ +include CHANGELOG +include README.rst +include setup.py +include tox.ini +include LICENSE +graft testing +recursive-exclude * *.pyc *.pyo diff --git a/third_party/python/pluggy/PKG-INFO b/third_party/python/pluggy/PKG-INFO new file mode 100644 index 000000000000..6e2f59f1d37d --- /dev/null +++ b/third_party/python/pluggy/PKG-INFO @@ -0,0 +1,112 @@ +Metadata-Version: 1.2 +Name: pluggy +Version: 0.6.0 +Summary: plugin and hook calling mechanisms for python +Home-page: https://github.com/pytest-dev/pluggy +Author: Holger Krekel +Author-email: holger@merlinux.eu +License: MIT license +Description-Content-Type: UNKNOWN +Description: pluggy - A minimalist production ready plugin system + ==================================================== + |pypi| |anaconda| |versions| |travis| |appveyor| + + + This is the core framework used by the `pytest`_, `tox`_, and `devpi`_ projects. + + Please `read the docs`_ to learn more! + + A definitive example + ******************** + .. code-block:: python + + import pluggy + + hookspec = pluggy.HookspecMarker("myproject") + hookimpl = pluggy.HookimplMarker("myproject") + + + class MySpec(object): + """A hook specification namespace. + """ + @hookspec + def myhook(self, arg1, arg2): + """My special little hook that you can customize. + """ + + + class Plugin_1(object): + """A hook implementation namespace. + """ + @hookimpl + def myhook(self, arg1, arg2): + print("inside Plugin_1.myhook()") + return arg1 + arg2 + + + class Plugin_2(object): + """A 2nd hook implementation namespace. + """ + @hookimpl + def myhook(self, arg1, arg2): + print("inside Plugin_2.myhook()") + return arg1 - arg2 + + + # create a manager and add the spec + pm = pluggy.PluginManager("myproject") + pm.add_hookspecs(MySpec) + + # register plugins + pm.register(Plugin_1()) + pm.register(Plugin_2()) + + # call our `myhook` hook + results = pm.hook.myhook(arg1=1, arg2=2) + print(results) + + + .. badges + .. |pypi| image:: https://img.shields.io/pypi/v/pluggy.svg + :target: https://pypi.python.org/pypi/pluggy + .. |versions| image:: https://img.shields.io/pypi/pyversions/pluggy.svg + :target: https://pypi.python.org/pypi/pluggy + .. |travis| image:: https://img.shields.io/travis/pytest-dev/pluggy/master.svg + :target: https://travis-ci.org/pytest-dev/pluggy + .. |appveyor| image:: https://img.shields.io/appveyor/ci/pytestbot/pluggy/master.svg + :target: https://ci.appveyor.com/project/pytestbot/pluggy + .. |anaconda| image:: https://anaconda.org/conda-forge/pluggy/badges/version.svg + :target: https://anaconda.org/conda-forge/pluggy + + .. links + .. _pytest: + http://pytest.org + .. _tox: + https://tox.readthedocs.org + .. _devpi: + http://doc.devpi.net + .. _read the docs: + https://pluggy.readthedocs.io/en/latest/ + +Platform: unix +Platform: linux +Platform: osx +Platform: win32 +Classifier: Development Status :: 4 - Beta +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: MIT License +Classifier: Operating System :: POSIX +Classifier: Operating System :: Microsoft :: Windows +Classifier: Operating System :: MacOS :: MacOS X +Classifier: Topic :: Software Development :: Testing +Classifier: Topic :: Software Development :: Libraries +Classifier: Topic :: Utilities +Classifier: Programming Language :: Python :: Implementation :: CPython +Classifier: Programming Language :: Python :: Implementation :: PyPy +Classifier: Programming Language :: Python :: 2 +Classifier: Programming Language :: Python :: 2.7 +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.4 +Classifier: Programming Language :: Python :: 3.5 +Classifier: Programming Language :: Python :: 3.6 +Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.* diff --git a/third_party/python/pluggy/README.rst b/third_party/python/pluggy/README.rst new file mode 100644 index 000000000000..3636b6ec6425 --- /dev/null +++ b/third_party/python/pluggy/README.rst @@ -0,0 +1,80 @@ +pluggy - A minimalist production ready plugin system +==================================================== +|pypi| |anaconda| |versions| |travis| |appveyor| + + +This is the core framework used by the `pytest`_, `tox`_, and `devpi`_ projects. + +Please `read the docs`_ to learn more! + +A definitive example +******************** +.. code-block:: python + + import pluggy + + hookspec = pluggy.HookspecMarker("myproject") + hookimpl = pluggy.HookimplMarker("myproject") + + + class MySpec(object): + """A hook specification namespace. + """ + @hookspec + def myhook(self, arg1, arg2): + """My special little hook that you can customize. + """ + + + class Plugin_1(object): + """A hook implementation namespace. + """ + @hookimpl + def myhook(self, arg1, arg2): + print("inside Plugin_1.myhook()") + return arg1 + arg2 + + + class Plugin_2(object): + """A 2nd hook implementation namespace. + """ + @hookimpl + def myhook(self, arg1, arg2): + print("inside Plugin_2.myhook()") + return arg1 - arg2 + + + # create a manager and add the spec + pm = pluggy.PluginManager("myproject") + pm.add_hookspecs(MySpec) + + # register plugins + pm.register(Plugin_1()) + pm.register(Plugin_2()) + + # call our `myhook` hook + results = pm.hook.myhook(arg1=1, arg2=2) + print(results) + + +.. badges +.. |pypi| image:: https://img.shields.io/pypi/v/pluggy.svg + :target: https://pypi.python.org/pypi/pluggy +.. |versions| image:: https://img.shields.io/pypi/pyversions/pluggy.svg + :target: https://pypi.python.org/pypi/pluggy +.. |travis| image:: https://img.shields.io/travis/pytest-dev/pluggy/master.svg + :target: https://travis-ci.org/pytest-dev/pluggy +.. |appveyor| image:: https://img.shields.io/appveyor/ci/pytestbot/pluggy/master.svg + :target: https://ci.appveyor.com/project/pytestbot/pluggy +.. |anaconda| image:: https://anaconda.org/conda-forge/pluggy/badges/version.svg + :target: https://anaconda.org/conda-forge/pluggy + +.. links +.. _pytest: + http://pytest.org +.. _tox: + https://tox.readthedocs.org +.. _devpi: + http://doc.devpi.net +.. _read the docs: + https://pluggy.readthedocs.io/en/latest/ diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy.py b/third_party/python/pluggy/pluggy/__init__.py similarity index 69% rename from third_party/python/pytest/_pytest/vendored_packages/pluggy.py rename to third_party/python/pluggy/pluggy/__init__.py index aebddad01dae..42d2220be490 100644 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy.py +++ b/third_party/python/pluggy/pluggy/__init__.py @@ -1,81 +1,18 @@ -""" -PluginManager, basic initialization and tracing. - -pluggy is the cristallized core of plugin management as used -by some 150 plugins for pytest. - -Pluggy uses semantic versioning. Breaking changes are only foreseen for -Major releases (incremented X in "X.Y.Z"). If you want to use pluggy in -your project you should thus use a dependency restriction like -"pluggy>=0.1.0,<1.0" to avoid surprises. - -pluggy is concerned with hook specification, hook implementations and hook -calling. For any given hook specification a hook call invokes up to N implementations. -A hook implementation can influence its position and type of execution: -if attributed "tryfirst" or "trylast" it will be tried to execute -first or last. However, if attributed "hookwrapper" an implementation -can wrap all calls to non-hookwrapper implementations. A hookwrapper -can thus execute some code ahead and after the execution of other hooks. - -Hook specification is done by way of a regular python function where -both the function name and the names of all its arguments are significant. -Each hook implementation function is verified against the original specification -function, including the names of all its arguments. To allow for hook specifications -to evolve over the livetime of a project, hook implementations can -accept less arguments. One can thus add new arguments and semantics to -a hook specification by adding another argument typically without breaking -existing hook implementations. - -The chosen approach is meant to let a hook designer think carefuly about -which objects are needed by an extension writer. By contrast, subclass-based -extension mechanisms often expose a lot more state and behaviour than needed, -thus restricting future developments. - -Pluggy currently consists of functionality for: - -- a way to register new hook specifications. Without a hook - specification no hook calling can be performed. - -- a registry of plugins which contain hook implementation functions. It - is possible to register plugins for which a hook specification is not yet - known and validate all hooks when the system is in a more referentially - consistent state. Setting an "optionalhook" attribution to a hook - implementation will avoid PluginValidationError's if a specification - is missing. This allows to have optional integration between plugins. - -- a "hook" relay object from which you can launch 1:N calls to - registered hook implementation functions - -- a mechanism for ordering hook implementation functions - -- mechanisms for two different type of 1:N calls: "firstresult" for when - the call should stop when the first implementation returns a non-None result. - And the other (default) way of guaranteeing that all hook implementations - will be called and their non-None result collected. - -- mechanisms for "historic" extension points such that all newly - registered functions will receive all hook calls that happened - before their registration. - -- a mechanism for discovering plugin objects which are based on - setuptools based entry points. - -- a simple tracing mechanism, including tracing of plugin calls and - their arguments. - -""" -import sys import inspect +import warnings +from .callers import _multicall, HookCallError, _Result, _legacymulticall -__version__ = '0.4.0' +__version__ = '0.6.0' __all__ = ["PluginManager", "PluginValidationError", "HookCallError", "HookspecMarker", "HookimplMarker"] -_py3 = sys.version_info > (3, 0) + +class PluginValidationError(Exception): + """ plugin failed validation. """ -class HookspecMarker: +class HookspecMarker(object): """ Decorator helper class for marking functions as hook specifications. You can instantiate it with a project_name to get a decorator. @@ -104,7 +41,7 @@ class HookspecMarker: if historic and firstresult: raise ValueError("cannot have a historic firstresult hook") setattr(func, self.project_name + "_spec", - dict(firstresult=firstresult, historic=historic)) + dict(firstresult=firstresult, historic=historic)) return func if function is not None: @@ -113,7 +50,7 @@ class HookspecMarker: return setattr_hookspec_opts -class HookimplMarker: +class HookimplMarker(object): """ Decorator helper class for marking functions as hook implementations. You can instantiate with a project_name to get a decorator. @@ -143,15 +80,15 @@ class HookimplMarker: If hookwrapper is True the hook implementations needs to execute exactly one "yield". The code before the yield is run early before any non-hookwrapper function is run. The code after the yield is run after all non-hookwrapper - function have run. The yield receives an ``_CallOutcome`` object representing + function have run. The yield receives a ``_Result`` object representing the exception or result outcome of the inner calls (including other hookwrapper calls). """ def setattr_hookimpl_opts(func): setattr(func, self.project_name + "_impl", - dict(hookwrapper=hookwrapper, optionalhook=optionalhook, - tryfirst=tryfirst, trylast=trylast)) + dict(hookwrapper=hookwrapper, optionalhook=optionalhook, + tryfirst=tryfirst, trylast=trylast)) return func if function is None: @@ -167,7 +104,7 @@ def normalize_hookimpl_opts(opts): opts.setdefault("optionalhook", False) -class _TagTracer: +class _TagTracer(object): def __init__(self): self._tag2proc = {} self.writer = None @@ -214,7 +151,7 @@ class _TagTracer: self._tag2proc[tags] = processor -class _TagTracerSub: +class _TagTracerSub(object): def __init__(self, root, tags): self.root = root self.tags = tags @@ -229,64 +166,7 @@ class _TagTracerSub: return self.__class__(self.root, self.tags + (name,)) -def _raise_wrapfail(wrap_controller, msg): - co = wrap_controller.gi_code - raise RuntimeError("wrap_controller at %r %s:%d %s" % - (co.co_name, co.co_filename, co.co_firstlineno, msg)) - - -def _wrapped_call(wrap_controller, func): - """ Wrap calling to a function with a generator which needs to yield - exactly once. The yield point will trigger calling the wrapped function - and return its _CallOutcome to the yield point. The generator then needs - to finish (raise StopIteration) in order for the wrapped call to complete. - """ - try: - next(wrap_controller) # first yield - except StopIteration: - _raise_wrapfail(wrap_controller, "did not yield") - call_outcome = _CallOutcome(func) - try: - wrap_controller.send(call_outcome) - _raise_wrapfail(wrap_controller, "has second yield") - except StopIteration: - pass - return call_outcome.get_result() - - -class _CallOutcome: - """ Outcome of a function call, either an exception or a proper result. - Calling the ``get_result`` method will return the result or reraise - the exception raised when the function was called. """ - excinfo = None - - def __init__(self, func): - try: - self.result = func() - except BaseException: - self.excinfo = sys.exc_info() - - def force_result(self, result): - self.result = result - self.excinfo = None - - def get_result(self): - if self.excinfo is None: - return self.result - else: - ex = self.excinfo - if _py3: - raise ex[1].with_traceback(ex[2]) - _reraise(*ex) # noqa - -if not _py3: - exec(""" -def _reraise(cls, val, tb): - raise cls, val, tb -""") - - -class _TracedHookExecution: +class _TracedHookExecution(object): def __init__(self, pluginmanager, before, after): self.pluginmanager = pluginmanager self.before = before @@ -297,7 +177,7 @@ class _TracedHookExecution: def __call__(self, hook, hook_impls, kwargs): self.before(hook.name, hook_impls, kwargs) - outcome = _CallOutcome(lambda: self.oldcall(hook, hook_impls, kwargs)) + outcome = _Result.from_call(lambda: self.oldcall(hook, hook_impls, kwargs)) self.after(outcome, hook.name, hook_impls, kwargs) return outcome.get_result() @@ -331,7 +211,10 @@ class PluginManager(object): self.hook = _HookRelay(self.trace.root.get("hook")) self._implprefix = implprefix self._inner_hookexec = lambda hook, methods, kwargs: \ - _MultiCall(methods, kwargs, hook.spec_opts).execute() + hook.multicall( + methods, kwargs, + firstresult=hook.spec_opts.get('firstresult'), + ) def _hookexec(self, hook, methods, kwargs): # called from all hookcaller instances. @@ -348,7 +231,7 @@ class PluginManager(object): if self._name2plugin.get(plugin_name, -1) is None: return # blocked plugin, return None to indicate no registration raise ValueError("Plugin already registered: %s=%s\n%s" % - (plugin_name, plugin, self._name2plugin)) + (plugin_name, plugin, self._name2plugin)) # XXX if an error happens we should make sure no state has been # changed at point of return @@ -375,6 +258,8 @@ class PluginManager(object): def parse_hookimpl_opts(self, plugin, name): method = getattr(plugin, name) + if not inspect.isroutine(method): + return try: res = getattr(method, self.project_name + "_impl", None) except Exception: @@ -475,14 +360,16 @@ class PluginManager(object): "Plugin %r\nhook %r\nhistoric incompatible to hookwrapper" % (hookimpl.plugin_name, hook.name)) - for arg in hookimpl.argnames: - if arg not in hook.argnames: - raise PluginValidationError( - "Plugin %r\nhook %r\nargument %r not available\n" - "plugin definition: %s\n" - "available hookargs: %s" % - (hookimpl.plugin_name, hook.name, arg, - _formatdef(hookimpl.function), ", ".join(hook.argnames))) + # positional arg checking + notinspec = set(hookimpl.argnames) - set(hook.argnames) + if notinspec: + raise PluginValidationError( + "Plugin %r for hook %r\nhookimpl definition: %s\n" + "Argument(s) %s are declared in the hookimpl but " + "can not be found in the hookspec" % + (hookimpl.plugin_name, hook.name, + _formatdef(hookimpl.function), notinspec) + ) def check_pending(self): """ Verify that all hooks which have not been verified against @@ -540,7 +427,7 @@ class PluginManager(object): of HookImpl instances and the keyword arguments for the hook call. ``after(outcome, hook_name, hook_impls, kwargs)`` receives the - same arguments as ``before`` but also a :py:class:`_CallOutcome <_pytest.vendored_packages.pluggy._CallOutcome>` object + same arguments as ``before`` but also a :py:class:`_Result`` object which represents the result of the overall hook call. """ return _TracedHookExecution(self, before, after).undo @@ -555,7 +442,7 @@ class PluginManager(object): def after(outcome, hook_name, methods, kwargs): if outcome.excinfo is None: - hooktrace("finish", hook_name, "-->", outcome.result) + hooktrace("finish", hook_name, "-->", outcome.get_result()) hooktrace.root.indent -= 1 return self.add_hookcall_monitoring(before, after) @@ -580,100 +467,58 @@ class PluginManager(object): return orig -class _MultiCall: - """ execute a call into multiple python functions/methods. """ +def varnames(func): + """Return tuple of positional and keywrord argument names for a function, + method, class or callable. - # XXX note that the __multicall__ argument is supported only - # for pytest compatibility reasons. It was never officially - # supported there and is explicitely deprecated since 2.8 - # so we can remove it soon, allowing to avoid the below recursion - # in execute() and simplify/speed up the execute loop. - - def __init__(self, hook_impls, kwargs, specopts={}): - self.hook_impls = hook_impls - self.kwargs = kwargs - self.kwargs["__multicall__"] = self - self.specopts = specopts - - def execute(self): - all_kwargs = self.kwargs - self.results = results = [] - firstresult = self.specopts.get("firstresult") - - while self.hook_impls: - hook_impl = self.hook_impls.pop() - try: - args = [all_kwargs[argname] for argname in hook_impl.argnames] - except KeyError: - for argname in hook_impl.argnames: - if argname not in all_kwargs: - raise HookCallError( - "hook call must provide argument %r" % (argname,)) - if hook_impl.hookwrapper: - return _wrapped_call(hook_impl.function(*args), self.execute) - res = hook_impl.function(*args) - if res is not None: - if firstresult: - return res - results.append(res) - - if not firstresult: - return results - - def __repr__(self): - status = "%d meths" % (len(self.hook_impls),) - if hasattr(self, "results"): - status = ("%d results, " % len(self.results)) + status - return "<_MultiCall %s, kwargs=%r>" % (status, self.kwargs) - - -def varnames(func, startindex=None): - """ return argument name tuple for a function, method, class or callable. - - In case of a class, its "__init__" method is considered. - For methods the "self" parameter is not included unless you are passing - an unbound method with Python3 (which has no supports for unbound methods) + In case of a class, its ``__init__`` method is considered. + For methods the ``self`` parameter is not included. """ cache = getattr(func, "__dict__", {}) try: return cache["_varnames"] except KeyError: pass + if inspect.isclass(func): try: func = func.__init__ except AttributeError: + return (), () + elif not inspect.isroutine(func): # callable object? + try: + func = getattr(func, '__call__', func) + except Exception: return () - startindex = 1 - else: - if not inspect.isfunction(func) and not inspect.ismethod(func): - try: - func = getattr(func, '__call__', func) - except Exception: - return () - if startindex is None: - startindex = int(inspect.ismethod(func)) - try: - rawcode = func.__code__ - except AttributeError: - return () - try: - x = rawcode.co_varnames[startindex:rawcode.co_argcount] - except AttributeError: - x = () + try: # func MUST be a function or method here or we won't parse any args + spec = _getargspec(func) + except TypeError: + return (), () + + args, defaults = tuple(spec.args), spec.defaults + if defaults: + index = -len(defaults) + args, defaults = args[:index], tuple(args[index:]) else: - defaults = func.__defaults__ - if defaults: - x = x[:-len(defaults)] + defaults = () + + # strip any implicit instance arg + if args: + if inspect.ismethod(func) or ( + '.' in getattr(func, '__qualname__', ()) and args[0] == 'self' + ): + args = args[1:] + + assert "self" not in args # best naming practises check? try: - cache["_varnames"] = x + cache["_varnames"] = args, defaults except TypeError: pass - return x + return args, defaults -class _HookRelay: +class _HookRelay(object): """ hook holder object for performing 1:N hook calls where N is the number of registered plugins. @@ -684,26 +529,31 @@ class _HookRelay: class _HookCaller(object): - def __init__(self, name, hook_execute, specmodule_or_class=None, spec_opts=None): + def __init__(self, name, hook_execute, specmodule_or_class=None, + spec_opts=None): self.name = name self._wrappers = [] self._nonwrappers = [] self._hookexec = hook_execute + self._specmodule_or_class = None + self.argnames = None + self.kwargnames = None + self.multicall = _multicall + self.spec_opts = spec_opts or {} if specmodule_or_class is not None: - assert spec_opts is not None self.set_specification(specmodule_or_class, spec_opts) def has_spec(self): - return hasattr(self, "_specmodule_or_class") + return self._specmodule_or_class is not None def set_specification(self, specmodule_or_class, spec_opts): assert not self.has_spec() self._specmodule_or_class = specmodule_or_class specfunc = getattr(specmodule_or_class, self.name) - argnames = varnames(specfunc, startindex=inspect.isclass(specmodule_or_class)) - assert "self" not in argnames # sanity check + # get spec arg signature + argnames, self.kwargnames = varnames(specfunc) self.argnames = ["__multicall__"] + list(argnames) - self.spec_opts = spec_opts + self.spec_opts.update(spec_opts) if spec_opts.get("historic"): self._call_history = [] @@ -721,6 +571,8 @@ class _HookCaller(object): raise ValueError("plugin %r not found" % (plugin,)) def _add_hookimpl(self, hookimpl): + """A an implementation to the callback chain. + """ if hookimpl.hookwrapper: methods = self._wrappers else: @@ -737,17 +589,45 @@ class _HookCaller(object): i -= 1 methods.insert(i + 1, hookimpl) + if '__multicall__' in hookimpl.argnames: + warnings.warn( + "Support for __multicall__ is now deprecated and will be" + "removed in an upcoming release.", + DeprecationWarning + ) + self.multicall = _legacymulticall + def __repr__(self): return "<_HookCaller %r>" % (self.name,) - def __call__(self, **kwargs): + def __call__(self, *args, **kwargs): + if args: + raise TypeError("hook calling supports only keyword arguments") assert not self.is_historic() + if self.argnames: + notincall = set(self.argnames) - set(['__multicall__']) - set( + kwargs.keys()) + if notincall: + warnings.warn( + "Argument(s) {} which are declared in the hookspec " + "can not be found in this hook call" + .format(tuple(notincall)), + stacklevel=2, + ) return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs) def call_historic(self, proc=None, kwargs=None): + """ call the hook with given ``kwargs`` for all registered plugins and + for all plugins which will be registered afterwards. + + If ``proc`` is not None it will be called for for each non-None result + obtained from a hook implementation. + """ self._call_history.append((kwargs or {}, proc)) # historizing hooks don't return results - self._hookexec(self, self._nonwrappers + self._wrappers, kwargs) + res = self._hookexec(self, self._nonwrappers + self._wrappers, kwargs) + for x in res or []: + proc(x) def call_extra(self, methods, kwargs): """ Call the hook with some additional temporarily participating @@ -763,6 +643,8 @@ class _HookCaller(object): self._nonwrappers, self._wrappers = old def _maybe_apply_history(self, method): + """Apply call history to a new hookimpl if it is marked as historic. + """ if self.is_historic(): for kwargs, proc in self._call_history: res = self._hookexec(self, [method], kwargs) @@ -770,22 +652,22 @@ class _HookCaller(object): proc(res[0]) -class HookImpl: +class HookImpl(object): def __init__(self, plugin, plugin_name, function, hook_impl_opts): self.function = function - self.argnames = varnames(self.function) + self.argnames, self.kwargnames = varnames(self.function) self.plugin = plugin self.opts = hook_impl_opts self.plugin_name = plugin_name self.__dict__.update(hook_impl_opts) -class PluginValidationError(Exception): - """ plugin failed validation. """ - - -class HookCallError(Exception): - """ Hook was called wrongly. """ +if hasattr(inspect, 'getfullargspec'): + def _getargspec(func): + return inspect.getfullargspec(func) +else: + def _getargspec(func): + return inspect.getargspec(func) if hasattr(inspect, 'signature'): diff --git a/third_party/python/pluggy/pluggy/callers.py b/third_party/python/pluggy/pluggy/callers.py new file mode 100644 index 000000000000..3ff67becffbc --- /dev/null +++ b/third_party/python/pluggy/pluggy/callers.py @@ -0,0 +1,201 @@ +''' +Call loop machinery +''' +import sys +import warnings + +_py3 = sys.version_info > (3, 0) + + +if not _py3: + exec(""" +def _reraise(cls, val, tb): + raise cls, val, tb +""") + + +def _raise_wrapfail(wrap_controller, msg): + co = wrap_controller.gi_code + raise RuntimeError("wrap_controller at %r %s:%d %s" % + (co.co_name, co.co_filename, co.co_firstlineno, msg)) + + +class HookCallError(Exception): + """ Hook was called wrongly. """ + + +class _Result(object): + def __init__(self, result, excinfo): + self._result = result + self._excinfo = excinfo + + @property + def excinfo(self): + return self._excinfo + + @property + def result(self): + """Get the result(s) for this hook call (DEPRECATED in favor of ``get_result()``).""" + msg = 'Use get_result() which forces correct exception handling' + warnings.warn(DeprecationWarning(msg), stacklevel=2) + return self._result + + @classmethod + def from_call(cls, func): + __tracebackhide__ = True + result = excinfo = None + try: + result = func() + except BaseException: + excinfo = sys.exc_info() + + return cls(result, excinfo) + + def force_result(self, result): + """Force the result(s) to ``result``. + + If the hook was marked as a ``firstresult`` a single value should + be set otherwise set a (modified) list of results. Any exceptions + found during invocation will be deleted. + """ + self._result = result + self._excinfo = None + + def get_result(self): + """Get the result(s) for this hook call. + + If the hook was marked as a ``firstresult`` only a single value + will be returned otherwise a list of results. + """ + __tracebackhide__ = True + if self._excinfo is None: + return self._result + else: + ex = self._excinfo + if _py3: + raise ex[1].with_traceback(ex[2]) + _reraise(*ex) # noqa + + +def _wrapped_call(wrap_controller, func): + """ Wrap calling to a function with a generator which needs to yield + exactly once. The yield point will trigger calling the wrapped function + and return its ``_Result`` to the yield point. The generator then needs + to finish (raise StopIteration) in order for the wrapped call to complete. + """ + try: + next(wrap_controller) # first yield + except StopIteration: + _raise_wrapfail(wrap_controller, "did not yield") + call_outcome = _Result.from_call(func) + try: + wrap_controller.send(call_outcome) + _raise_wrapfail(wrap_controller, "has second yield") + except StopIteration: + pass + return call_outcome.get_result() + + +class _LegacyMultiCall(object): + """ execute a call into multiple python functions/methods. """ + + # XXX note that the __multicall__ argument is supported only + # for pytest compatibility reasons. It was never officially + # supported there and is explicitely deprecated since 2.8 + # so we can remove it soon, allowing to avoid the below recursion + # in execute() and simplify/speed up the execute loop. + + def __init__(self, hook_impls, kwargs, firstresult=False): + self.hook_impls = hook_impls + self.caller_kwargs = kwargs # come from _HookCaller.__call__() + self.caller_kwargs["__multicall__"] = self + self.firstresult = firstresult + + def execute(self): + caller_kwargs = self.caller_kwargs + self.results = results = [] + firstresult = self.firstresult + + while self.hook_impls: + hook_impl = self.hook_impls.pop() + try: + args = [caller_kwargs[argname] for argname in hook_impl.argnames] + except KeyError: + for argname in hook_impl.argnames: + if argname not in caller_kwargs: + raise HookCallError( + "hook call must provide argument %r" % (argname,)) + if hook_impl.hookwrapper: + return _wrapped_call(hook_impl.function(*args), self.execute) + res = hook_impl.function(*args) + if res is not None: + if firstresult: + return res + results.append(res) + + if not firstresult: + return results + + def __repr__(self): + status = "%d meths" % (len(self.hook_impls),) + if hasattr(self, "results"): + status = ("%d results, " % len(self.results)) + status + return "<_MultiCall %s, kwargs=%r>" % (status, self.caller_kwargs) + + +def _legacymulticall(hook_impls, caller_kwargs, firstresult=False): + return _LegacyMultiCall( + hook_impls, caller_kwargs, firstresult=firstresult).execute() + + +def _multicall(hook_impls, caller_kwargs, firstresult=False): + """Execute a call into multiple python functions/methods and return the + result(s). + + ``caller_kwargs`` comes from _HookCaller.__call__(). + """ + __tracebackhide__ = True + results = [] + excinfo = None + try: # run impl and wrapper setup functions in a loop + teardowns = [] + try: + for hook_impl in reversed(hook_impls): + try: + args = [caller_kwargs[argname] for argname in hook_impl.argnames] + except KeyError: + for argname in hook_impl.argnames: + if argname not in caller_kwargs: + raise HookCallError( + "hook call must provide argument %r" % (argname,)) + + if hook_impl.hookwrapper: + try: + gen = hook_impl.function(*args) + next(gen) # first yield + teardowns.append(gen) + except StopIteration: + _raise_wrapfail(gen, "did not yield") + else: + res = hook_impl.function(*args) + if res is not None: + results.append(res) + if firstresult: # halt further impl calls + break + except BaseException: + excinfo = sys.exc_info() + finally: + if firstresult: # first result hooks return a single value + outcome = _Result(results[0] if results else None, excinfo) + else: + outcome = _Result(results, excinfo) + + # run all wrapper post-yield blocks + for gen in reversed(teardowns): + try: + gen.send(outcome) + _raise_wrapfail(gen, "has second yield") + except StopIteration: + pass + + return outcome.get_result() diff --git a/third_party/python/pluggy/setup.cfg b/third_party/python/pluggy/setup.cfg new file mode 100644 index 000000000000..ead73f887a17 --- /dev/null +++ b/third_party/python/pluggy/setup.cfg @@ -0,0 +1,13 @@ +[bdist_wheel] +universal = 1 + +[metadata] +license_file = LICENSE + +[devpi:upload] +formats = sdist.tgz,bdist_wheel + +[egg_info] +tag_build = +tag_date = 0 + diff --git a/third_party/python/pluggy/setup.py b/third_party/python/pluggy/setup.py new file mode 100644 index 000000000000..b7c0f697120a --- /dev/null +++ b/third_party/python/pluggy/setup.py @@ -0,0 +1,51 @@ +import os +from setuptools import setup + +classifiers = [ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Operating System :: POSIX', + 'Operating System :: Microsoft :: Windows', + 'Operating System :: MacOS :: MacOS X', + 'Topic :: Software Development :: Testing', + 'Topic :: Software Development :: Libraries', + 'Topic :: Utilities', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy'] + [ + ('Programming Language :: Python :: %s' % x) for x in + '2 2.7 3 3.4 3.5 3.6'.split()] + +with open('README.rst') as fd: + long_description = fd.read() + + +def get_version(): + p = os.path.join(os.path.dirname( + os.path.abspath(__file__)), "pluggy/__init__.py") + with open(p) as f: + for line in f.readlines(): + if "__version__" in line: + return line.strip().split("=")[-1].strip(" '") + raise ValueError("could not read version") + + +def main(): + setup( + name='pluggy', + description='plugin and hook calling mechanisms for python', + long_description=long_description, + version=get_version(), + license='MIT license', + platforms=['unix', 'linux', 'osx', 'win32'], + author='Holger Krekel', + author_email='holger@merlinux.eu', + url='https://github.com/pytest-dev/pluggy', + python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', + classifiers=classifiers, + packages=['pluggy'], + ) + + +if __name__ == '__main__': + main() diff --git a/third_party/python/pluggy/testing/benchmark.py b/third_party/python/pluggy/testing/benchmark.py new file mode 100644 index 000000000000..5a913e9d418d --- /dev/null +++ b/third_party/python/pluggy/testing/benchmark.py @@ -0,0 +1,59 @@ +""" +Benchmarking and performance tests. +""" +import pytest +from pluggy import (_multicall, _legacymulticall, HookImpl, HookspecMarker, + HookimplMarker) + +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + + +def MC(methods, kwargs, callertype, firstresult=False): + hookfuncs = [] + for method in methods: + f = HookImpl(None, "", method, method.example_impl) + hookfuncs.append(f) + return callertype(hookfuncs, kwargs, {"firstresult": firstresult}) + + +@hookimpl +def hook(arg1, arg2, arg3): + return arg1, arg2, arg3 + + +@hookimpl(hookwrapper=True) +def wrapper(arg1, arg2, arg3): + yield + + +@pytest.fixture( + params=[10, 100], + ids="hooks={}".format, +) +def hooks(request): + return [hook for i in range(request.param)] + + +@pytest.fixture( + params=[10, 100], + ids="wrappers={}".format, +) +def wrappers(request): + return [wrapper for i in range(request.param)] + + +@pytest.fixture( + params=[_multicall, _legacymulticall], + ids=lambda item: item.__name__ +) +def callertype(request): + return request.param + + +def inner_exec(methods, callertype): + return MC(methods, {'arg1': 1, 'arg2': 2, 'arg3': 3}, callertype) + + +def test_hook_and_wrappers_speed(benchmark, hooks, wrappers, callertype): + benchmark(inner_exec, hooks + wrappers, callertype) diff --git a/third_party/python/pluggy/testing/conftest.py b/third_party/python/pluggy/testing/conftest.py new file mode 100644 index 000000000000..3d61a349c88c --- /dev/null +++ b/third_party/python/pluggy/testing/conftest.py @@ -0,0 +1,30 @@ +import pytest + + +@pytest.fixture( + params=[ + lambda spec: spec, + lambda spec: spec() + ], + ids=[ + "spec-is-class", + "spec-is-instance" + ], +) +def he_pm(request, pm): + from pluggy import HookspecMarker + hookspec = HookspecMarker("example") + + class Hooks(object): + @hookspec + def he_method1(self, arg): + return arg + 1 + + pm.add_hookspecs(request.param(Hooks)) + return pm + + +@pytest.fixture +def pm(): + from pluggy import PluginManager + return PluginManager("example") diff --git a/third_party/python/pluggy/testing/test_details.py b/third_party/python/pluggy/testing/test_details.py new file mode 100644 index 000000000000..2fad198d9585 --- /dev/null +++ b/third_party/python/pluggy/testing/test_details.py @@ -0,0 +1,103 @@ +import warnings + +import pytest + +from pluggy import PluginManager, HookimplMarker, HookspecMarker, _Result + +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + + +def test_parse_hookimpl_override(): + class MyPluginManager(PluginManager): + def parse_hookimpl_opts(self, module_or_class, name): + opts = PluginManager.parse_hookimpl_opts( + self, module_or_class, name) + if opts is None: + if name.startswith("x1"): + opts = {} + return opts + + class Plugin(object): + def x1meth(self): + pass + + @hookimpl(hookwrapper=True, tryfirst=True) + def x1meth2(self): + pass + + class Spec(object): + @hookspec + def x1meth(self): + pass + + @hookspec + def x1meth2(self): + pass + + pm = MyPluginManager(hookspec.project_name) + pm.register(Plugin()) + pm.add_hookspecs(Spec) + assert not pm.hook.x1meth._nonwrappers[0].hookwrapper + assert not pm.hook.x1meth._nonwrappers[0].tryfirst + assert not pm.hook.x1meth._nonwrappers[0].trylast + assert not pm.hook.x1meth._nonwrappers[0].optionalhook + + assert pm.hook.x1meth2._wrappers[0].tryfirst + assert pm.hook.x1meth2._wrappers[0].hookwrapper + + +def test_plugin_getattr_raises_errors(): + """Pluggy must be able to handle plugins which raise weird exceptions + when getattr() gets called (#11). + """ + class DontTouchMe(object): + def __getattr__(self, x): + raise Exception('cant touch me') + + class Module(object): + pass + + module = Module() + module.x = DontTouchMe() + + pm = PluginManager(hookspec.project_name) + # register() would raise an error + pm.register(module, 'donttouch') + assert pm.get_plugin('donttouch') is module + + +def test_warning_on_call_vs_hookspec_arg_mismatch(): + """Verify that is a hook is called with less arguments then defined in the + spec that a warning is emitted. + """ + class Spec: + @hookspec + def myhook(self, arg1, arg2): + pass + + class Plugin: + @hookimpl + def myhook(self, arg1): + pass + + pm = PluginManager(hookspec.project_name) + pm.register(Plugin()) + pm.add_hookspecs(Spec()) + + with warnings.catch_warnings(record=True) as warns: + warnings.simplefilter('always') + + # calling should trigger a warning + pm.hook.myhook(arg1=1) + + assert len(warns) == 1 + warning = warns[-1] + assert issubclass(warning.category, Warning) + assert "Argument(s) ('arg2',)" in str(warning.message) + + +def test_result_deprecated(): + r = _Result(10, None) + with pytest.deprecated_call(): + assert r.result == 10 diff --git a/third_party/python/pluggy/testing/test_helpers.py b/third_party/python/pluggy/testing/test_helpers.py new file mode 100644 index 000000000000..b17809684465 --- /dev/null +++ b/third_party/python/pluggy/testing/test_helpers.py @@ -0,0 +1,68 @@ +from pluggy import _formatdef, varnames + + +def test_varnames(): + def f(x): + i = 3 # noqa + + class A(object): + def f(self, y): + pass + + class B(object): + def __call__(self, z): + pass + + assert varnames(f) == (("x",), ()) + assert varnames(A().f) == (('y',), ()) + assert varnames(B()) == (('z',), ()) + + +def test_varnames_default(): + def f(x, y=3): + pass + + assert varnames(f) == (("x",), ("y",)) + + +def test_varnames_class(): + class C(object): + def __init__(self, x): + pass + + class D(object): + pass + + class E(object): + def __init__(self, x): + pass + + class F(object): + pass + + assert varnames(C) == (("x",), ()) + assert varnames(D) == ((), ()) + assert varnames(E) == (("x",), ()) + assert varnames(F) == ((), ()) + + +def test_formatdef(): + def function1(): + pass + + assert _formatdef(function1) == 'function1()' + + def function2(arg1): + pass + + assert _formatdef(function2) == "function2(arg1)" + + def function3(arg1, arg2="qwe"): + pass + + assert _formatdef(function3) == "function3(arg1, arg2='qwe')" + + def function4(arg1, *args, **kwargs): + pass + + assert _formatdef(function4) == "function4(arg1, *args, **kwargs)" diff --git a/third_party/python/pluggy/testing/test_hookrelay.py b/third_party/python/pluggy/testing/test_hookrelay.py new file mode 100644 index 000000000000..5e7821bed8f9 --- /dev/null +++ b/third_party/python/pluggy/testing/test_hookrelay.py @@ -0,0 +1,210 @@ +import pytest +from pluggy import PluginValidationError, HookimplMarker, HookspecMarker + + +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + + +def test_happypath(pm): + class Api(object): + @hookspec + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + hook = pm.hook + assert hasattr(hook, 'hello') + assert repr(hook.hello).find("hello") != -1 + + class Plugin(object): + @hookimpl + def hello(self, arg): + return arg + 1 + + plugin = Plugin() + pm.register(plugin) + out = hook.hello(arg=3) + assert out == [4] + assert not hasattr(hook, 'world') + pm.unregister(plugin) + assert hook.hello(arg=3) == [] + + +def test_argmismatch(pm): + class Api(object): + @hookspec + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + + class Plugin(object): + @hookimpl + def hello(self, argwrong): + pass + + with pytest.raises(PluginValidationError) as exc: + pm.register(Plugin()) + + assert "argwrong" in str(exc.value) + + +def test_only_kwargs(pm): + class Api(object): + @hookspec + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + with pytest.raises(TypeError) as exc: + pm.hook.hello(3) + + comprehensible = "hook calling supports only keyword arguments" + assert comprehensible in str(exc.value) + + +def test_call_order(pm): + class Api(object): + @hookspec + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + + class Plugin1(object): + @hookimpl + def hello(self, arg): + return 1 + + class Plugin2(object): + @hookimpl + def hello(self, arg): + return 2 + + class Plugin3(object): + @hookimpl + def hello(self, arg): + return 3 + + class Plugin4(object): + @hookimpl(hookwrapper=True) + def hello(self, arg): + assert arg == 0 + outcome = yield + assert outcome.get_result() == [3, 2, 1] + + pm.register(Plugin1()) + pm.register(Plugin2()) + pm.register(Plugin3()) + pm.register(Plugin4()) # hookwrapper should get same list result + res = pm.hook.hello(arg=0) + assert res == [3, 2, 1] + + +def test_firstresult_definition(pm): + class Api(object): + @hookspec(firstresult=True) + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + + class Plugin1(object): + @hookimpl + def hello(self, arg): + return arg + 1 + + class Plugin2(object): + @hookimpl + def hello(self, arg): + return arg - 1 + + class Plugin3(object): + @hookimpl + def hello(self, arg): + return None + + class Plugin4(object): + @hookimpl(hookwrapper=True) + def hello(self, arg): + assert arg == 3 + outcome = yield + assert outcome.get_result() == 2 + + pm.register(Plugin1()) # discarded - not the last registered plugin + pm.register(Plugin2()) # used as result + pm.register(Plugin3()) # None result is ignored + pm.register(Plugin4()) # hookwrapper should get same non-list result + res = pm.hook.hello(arg=3) + assert res == 2 + + +def test_firstresult_force_result(pm): + """Verify forcing a result in a wrapper. + """ + class Api(object): + @hookspec(firstresult=True) + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + + class Plugin1(object): + @hookimpl + def hello(self, arg): + return arg + 1 + + class Plugin2(object): + @hookimpl(hookwrapper=True) + def hello(self, arg): + assert arg == 3 + outcome = yield + assert outcome.get_result() == 4 + outcome.force_result(0) + + class Plugin3(object): + @hookimpl + def hello(self, arg): + return None + + pm.register(Plugin1()) + pm.register(Plugin2()) # wrapper + pm.register(Plugin3()) # ignored since returns None + res = pm.hook.hello(arg=3) + assert res == 0 # this result is forced and not a list + + +def test_firstresult_returns_none(pm): + """If None results are returned by underlying implementations ensure + the multi-call loop returns a None value. + """ + class Api(object): + @hookspec(firstresult=True) + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + + class Plugin1(object): + @hookimpl + def hello(self, arg): + return None + + pm.register(Plugin1()) + res = pm.hook.hello(arg=3) + assert res is None + + +def test_firstresult_no_plugin(pm): + """If no implementations/plugins have been registered for a firstresult + hook the multi-call loop should return a None value. + """ + class Api(object): + @hookspec(firstresult=True) + def hello(self, arg): + "api hook 1" + + pm.add_hookspecs(Api) + res = pm.hook.hello(arg=3) + assert res is None diff --git a/third_party/python/pluggy/testing/test_method_ordering.py b/third_party/python/pluggy/testing/test_method_ordering.py new file mode 100644 index 000000000000..9584a0ae5a82 --- /dev/null +++ b/third_party/python/pluggy/testing/test_method_ordering.py @@ -0,0 +1,322 @@ +import pytest + + +import sys +import types + +from pluggy import PluginManager, HookImpl, HookimplMarker, HookspecMarker + +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + + +@pytest.fixture +def hc(pm): + class Hooks(object): + @hookspec + def he_method1(self, arg): + pass + pm.add_hookspecs(Hooks) + return pm.hook.he_method1 + + +@pytest.fixture +def addmeth(hc): + def addmeth(tryfirst=False, trylast=False, hookwrapper=False): + def wrap(func): + hookimpl(tryfirst=tryfirst, trylast=trylast, + hookwrapper=hookwrapper)(func) + hc._add_hookimpl(HookImpl(None, "", func, func.example_impl)) + return func + return wrap + return addmeth + + +def funcs(hookmethods): + return [hookmethod.function for hookmethod in hookmethods] + + +def test_adding_nonwrappers(hc, addmeth): + @addmeth() + def he_method1(): + pass + + @addmeth() + def he_method2(): + pass + + @addmeth() + def he_method3(): + pass + assert funcs(hc._nonwrappers) == [he_method1, he_method2, he_method3] + + +def test_adding_nonwrappers_trylast(hc, addmeth): + @addmeth() + def he_method1_middle(): + pass + + @addmeth(trylast=True) + def he_method1(): + pass + + @addmeth() + def he_method1_b(): + pass + assert funcs(hc._nonwrappers) == [he_method1, he_method1_middle, he_method1_b] + + +def test_adding_nonwrappers_trylast3(hc, addmeth): + @addmeth() + def he_method1_a(): + pass + + @addmeth(trylast=True) + def he_method1_b(): + pass + + @addmeth() + def he_method1_c(): + pass + + @addmeth(trylast=True) + def he_method1_d(): + pass + assert funcs(hc._nonwrappers) == \ + [he_method1_d, he_method1_b, he_method1_a, he_method1_c] + + +def test_adding_nonwrappers_trylast2(hc, addmeth): + @addmeth() + def he_method1_middle(): + pass + + @addmeth() + def he_method1_b(): + pass + + @addmeth(trylast=True) + def he_method1(): + pass + assert funcs(hc._nonwrappers) == \ + [he_method1, he_method1_middle, he_method1_b] + + +def test_adding_nonwrappers_tryfirst(hc, addmeth): + @addmeth(tryfirst=True) + def he_method1(): + pass + + @addmeth() + def he_method1_middle(): + pass + + @addmeth() + def he_method1_b(): + pass + assert funcs(hc._nonwrappers) == [ + he_method1_middle, he_method1_b, he_method1] + + +def test_adding_wrappers_ordering(hc, addmeth): + @addmeth(hookwrapper=True) + def he_method1(): + pass + + @addmeth() + def he_method1_middle(): + pass + + @addmeth(hookwrapper=True) + def he_method3(): + pass + + assert funcs(hc._nonwrappers) == [he_method1_middle] + assert funcs(hc._wrappers) == [he_method1, he_method3] + + +def test_adding_wrappers_ordering_tryfirst(hc, addmeth): + @addmeth(hookwrapper=True, tryfirst=True) + def he_method1(): + pass + + @addmeth(hookwrapper=True) + def he_method2(): + pass + + assert hc._nonwrappers == [] + assert funcs(hc._wrappers) == [he_method2, he_method1] + + +def test_hookspec(pm): + class HookSpec(object): + @hookspec() + def he_myhook1(arg1): + pass + + @hookspec(firstresult=True) + def he_myhook2(arg1): + pass + + @hookspec(firstresult=False) + def he_myhook3(arg1): + pass + + pm.add_hookspecs(HookSpec) + assert not pm.hook.he_myhook1.spec_opts["firstresult"] + assert pm.hook.he_myhook2.spec_opts["firstresult"] + assert not pm.hook.he_myhook3.spec_opts["firstresult"] + + +@pytest.mark.parametrize('name', ["hookwrapper", "optionalhook", "tryfirst", "trylast"]) +@pytest.mark.parametrize('val', [True, False]) +def test_hookimpl(name, val): + @hookimpl(**{name: val}) + def he_myhook1(arg1): + pass + if val: + assert he_myhook1.example_impl.get(name) + else: + assert not hasattr(he_myhook1, name) + + +def test_load_setuptools_instantiation(monkeypatch, pm): + pkg_resources = pytest.importorskip("pkg_resources") + + def my_iter(name): + assert name == "hello" + + class EntryPoint(object): + name = "myname" + dist = None + + def load(self): + class PseudoPlugin(object): + x = 42 + return PseudoPlugin() + + return iter([EntryPoint()]) + + monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter) + num = pm.load_setuptools_entrypoints("hello") + assert num == 1 + plugin = pm.get_plugin("myname") + assert plugin.x == 42 + assert pm.list_plugin_distinfo() == [(plugin, None)] + + +def test_load_setuptools_not_installed(monkeypatch, pm): + monkeypatch.setitem( + sys.modules, 'pkg_resources', + types.ModuleType("pkg_resources")) + + with pytest.raises(ImportError): + pm.load_setuptools_entrypoints("qwe") + + +def test_add_tracefuncs(he_pm): + out = [] + + class api1(object): + @hookimpl + def he_method1(self): + out.append("he_method1-api1") + + class api2(object): + @hookimpl + def he_method1(self): + out.append("he_method1-api2") + + he_pm.register(api1()) + he_pm.register(api2()) + + def before(hook_name, hook_impls, kwargs): + out.append((hook_name, list(hook_impls), kwargs)) + + def after(outcome, hook_name, hook_impls, kwargs): + out.append((outcome, hook_name, list(hook_impls), kwargs)) + + undo = he_pm.add_hookcall_monitoring(before, after) + + he_pm.hook.he_method1(arg=1) + assert len(out) == 4 + assert out[0][0] == "he_method1" + assert len(out[0][1]) == 2 + assert isinstance(out[0][2], dict) + assert out[1] == "he_method1-api2" + assert out[2] == "he_method1-api1" + assert len(out[3]) == 4 + assert out[3][1] == out[0][0] + + undo() + he_pm.hook.he_method1(arg=1) + assert len(out) == 4 + 2 + + +def test_hook_tracing(he_pm): + saveindent = [] + + class api1(object): + @hookimpl + def he_method1(self): + saveindent.append(he_pm.trace.root.indent) + + class api2(object): + @hookimpl + def he_method1(self): + saveindent.append(he_pm.trace.root.indent) + raise ValueError() + + he_pm.register(api1()) + out = [] + he_pm.trace.root.setwriter(out.append) + undo = he_pm.enable_tracing() + try: + indent = he_pm.trace.root.indent + he_pm.hook.he_method1(arg=1) + assert indent == he_pm.trace.root.indent + assert len(out) == 2 + assert 'he_method1' in out[0] + assert 'finish' in out[1] + + out[:] = [] + he_pm.register(api2()) + + with pytest.raises(ValueError): + he_pm.hook.he_method1(arg=1) + assert he_pm.trace.root.indent == indent + assert saveindent[0] > indent + finally: + undo() + + +@pytest.mark.parametrize('include_hookspec', [True, False]) +def test_prefix_hookimpl(include_hookspec): + pm = PluginManager(hookspec.project_name, "hello_") + + if include_hookspec: + class HookSpec(object): + @hookspec + def hello_myhook(self, arg1): + """ add to arg1 """ + + pm.add_hookspecs(HookSpec) + + class Plugin(object): + def hello_myhook(self, arg1): + return arg1 + 1 + + pm.register(Plugin()) + pm.register(Plugin()) + results = pm.hook.hello_myhook(arg1=17) + assert results == [18, 18] + + +def test_prefix_hookimpl_dontmatch_module(): + pm = PluginManager(hookspec.project_name, "hello_") + + class BadPlugin(object): + hello_module = __import__('email') + + pm.register(BadPlugin()) + pm.check_pending() diff --git a/third_party/python/pluggy/testing/test_multicall.py b/third_party/python/pluggy/testing/test_multicall.py new file mode 100644 index 000000000000..860a209b6690 --- /dev/null +++ b/third_party/python/pluggy/testing/test_multicall.py @@ -0,0 +1,194 @@ +import pytest + +from pluggy import _multicall, _legacymulticall, HookImpl, HookCallError +from pluggy.callers import _LegacyMultiCall +from pluggy import HookspecMarker, HookimplMarker + + +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + + +def test_uses_copy_of_methods(): + out = [lambda: 42] + mc = _LegacyMultiCall(out, {}) + repr(mc) + out[:] = [] + res = mc.execute() + return res == 42 + + +def MC(methods, kwargs, firstresult=False): + caller = _multicall + hookfuncs = [] + for method in methods: + f = HookImpl(None, "", method, method.example_impl) + hookfuncs.append(f) + if '__multicall__' in f.argnames: + caller = _legacymulticall + return caller(hookfuncs, kwargs, firstresult=firstresult) + + +def test_call_passing(): + class P1(object): + @hookimpl + def m(self, __multicall__, x): + assert len(__multicall__.results) == 1 + assert not __multicall__.hook_impls + return 17 + + class P2(object): + @hookimpl + def m(self, __multicall__, x): + assert __multicall__.results == [] + assert __multicall__.hook_impls + return 23 + + p1 = P1() + p2 = P2() + reslist = MC([p1.m, p2.m], {"x": 23}) + assert len(reslist) == 2 + # ensure reversed order + assert reslist == [23, 17] + + +def test_keyword_args(): + @hookimpl + def f(x): + return x + 1 + + class A(object): + @hookimpl + def f(self, x, y): + return x + y + + reslist = MC([f, A().f], dict(x=23, y=24)) + assert reslist == [24 + 23, 24] + + +def test_keyword_args_with_defaultargs(): + @hookimpl + def f(x, z=1): + return x + z + reslist = MC([f], dict(x=23, y=24)) + assert reslist == [24] + + +def test_tags_call_error(): + @hookimpl + def f(x): + return x + with pytest.raises(HookCallError): + MC([f], {}) + + +def test_call_subexecute(): + @hookimpl + def m(__multicall__): + subresult = __multicall__.execute() + return subresult + 1 + + @hookimpl + def n(): + return 1 + + res = MC([n, m], {}, firstresult=True) + assert res == 2 + + +def test_call_none_is_no_result(): + @hookimpl + def m1(): + return 1 + + @hookimpl + def m2(): + return None + + res = MC([m1, m2], {}, firstresult=True) + assert res == 1 + res = MC([m1, m2], {}, {}) + assert res == [1] + + +def test_hookwrapper(): + out = [] + + @hookimpl(hookwrapper=True) + def m1(): + out.append("m1 init") + yield None + out.append("m1 finish") + + @hookimpl + def m2(): + out.append("m2") + return 2 + + res = MC([m2, m1], {}) + assert res == [2] + assert out == ["m1 init", "m2", "m1 finish"] + out[:] = [] + res = MC([m2, m1], {}, firstresult=True) + assert res == 2 + assert out == ["m1 init", "m2", "m1 finish"] + + +def test_hookwrapper_order(): + out = [] + + @hookimpl(hookwrapper=True) + def m1(): + out.append("m1 init") + yield 1 + out.append("m1 finish") + + @hookimpl(hookwrapper=True) + def m2(): + out.append("m2 init") + yield 2 + out.append("m2 finish") + + res = MC([m2, m1], {}) + assert res == [] + assert out == ["m1 init", "m2 init", "m2 finish", "m1 finish"] + + +def test_hookwrapper_not_yield(): + @hookimpl(hookwrapper=True) + def m1(): + pass + + with pytest.raises(TypeError): + MC([m1], {}) + + +def test_hookwrapper_too_many_yield(): + @hookimpl(hookwrapper=True) + def m1(): + yield 1 + yield 2 + + with pytest.raises(RuntimeError) as ex: + MC([m1], {}) + assert "m1" in str(ex.value) + assert (__file__ + ':') in str(ex.value) + + +@pytest.mark.parametrize("exc", [ValueError, SystemExit]) +def test_hookwrapper_exception(exc): + out = [] + + @hookimpl(hookwrapper=True) + def m1(): + out.append("m1 init") + yield None + out.append("m1 finish") + + @hookimpl + def m2(): + raise exc + + with pytest.raises(exc): + MC([m2, m1], {}) + assert out == ["m1 init", "m1 finish"] diff --git a/third_party/python/pluggy/testing/test_pluginmanager.py b/third_party/python/pluggy/testing/test_pluginmanager.py new file mode 100644 index 000000000000..e2c86cc64426 --- /dev/null +++ b/third_party/python/pluggy/testing/test_pluginmanager.py @@ -0,0 +1,374 @@ +import pytest +import types + +from pluggy import (PluginValidationError, + HookCallError, HookimplMarker, HookspecMarker) + + +hookspec = HookspecMarker("example") +hookimpl = HookimplMarker("example") + + +def test_plugin_double_register(pm): + pm.register(42, name="abc") + with pytest.raises(ValueError): + pm.register(42, name="abc") + with pytest.raises(ValueError): + pm.register(42, name="def") + + +def test_pm(pm): + class A(object): + pass + + a1, a2 = A(), A() + pm.register(a1) + assert pm.is_registered(a1) + pm.register(a2, "hello") + assert pm.is_registered(a2) + out = pm.get_plugins() + assert a1 in out + assert a2 in out + assert pm.get_plugin('hello') == a2 + assert pm.unregister(a1) == a1 + assert not pm.is_registered(a1) + + out = pm.list_name_plugin() + assert len(out) == 1 + assert out == [("hello", a2)] + + +def test_has_plugin(pm): + class A(object): + pass + + a1 = A() + pm.register(a1, 'hello') + assert pm.is_registered(a1) + assert pm.has_plugin('hello') + + +def test_register_dynamic_attr(he_pm): + class A(object): + def __getattr__(self, name): + if name[0] != "_": + return 42 + raise AttributeError() + + a = A() + he_pm.register(a) + assert not he_pm.get_hookcallers(a) + + +def test_pm_name(pm): + class A(object): + pass + + a1 = A() + name = pm.register(a1, name="hello") + assert name == "hello" + pm.unregister(a1) + assert pm.get_plugin(a1) is None + assert not pm.is_registered(a1) + assert not pm.get_plugins() + name2 = pm.register(a1, name="hello") + assert name2 == name + pm.unregister(name="hello") + assert pm.get_plugin(a1) is None + assert not pm.is_registered(a1) + assert not pm.get_plugins() + + +def test_set_blocked(pm): + class A(object): + pass + + a1 = A() + name = pm.register(a1) + assert pm.is_registered(a1) + assert not pm.is_blocked(name) + pm.set_blocked(name) + assert pm.is_blocked(name) + assert not pm.is_registered(a1) + + pm.set_blocked("somename") + assert pm.is_blocked("somename") + assert not pm.register(A(), "somename") + pm.unregister(name="somename") + assert pm.is_blocked("somename") + + +def test_register_mismatch_method(he_pm): + class hello(object): + @hookimpl + def he_method_notexists(self): + pass + + he_pm.register(hello()) + with pytest.raises(PluginValidationError): + he_pm.check_pending() + + +def test_register_mismatch_arg(he_pm): + class hello(object): + @hookimpl + def he_method1(self, qlwkje): + pass + + with pytest.raises(PluginValidationError): + he_pm.register(hello()) + + +def test_register(pm): + class MyPlugin(object): + pass + my = MyPlugin() + pm.register(my) + assert my in pm.get_plugins() + my2 = MyPlugin() + pm.register(my2) + assert set([my, my2]).issubset(pm.get_plugins()) + + assert pm.is_registered(my) + assert pm.is_registered(my2) + pm.unregister(my) + assert not pm.is_registered(my) + assert my not in pm.get_plugins() + + +def test_register_unknown_hooks(pm): + class Plugin1(object): + @hookimpl + def he_method1(self, arg): + return arg + 1 + + pname = pm.register(Plugin1()) + + class Hooks(object): + @hookspec + def he_method1(self, arg): + pass + + pm.add_hookspecs(Hooks) + # assert not pm._unverified_hooks + assert pm.hook.he_method1(arg=1) == [2] + assert len(pm.get_hookcallers(pm.get_plugin(pname))) == 1 + + +def test_register_historic(pm): + class Hooks(object): + @hookspec(historic=True) + def he_method1(self, arg): + pass + pm.add_hookspecs(Hooks) + + pm.hook.he_method1.call_historic(kwargs=dict(arg=1)) + out = [] + + class Plugin(object): + @hookimpl + def he_method1(self, arg): + out.append(arg) + + pm.register(Plugin()) + assert out == [1] + + class Plugin2(object): + @hookimpl + def he_method1(self, arg): + out.append(arg * 10) + + pm.register(Plugin2()) + assert out == [1, 10] + pm.hook.he_method1.call_historic(kwargs=dict(arg=12)) + assert out == [1, 10, 120, 12] + + +def test_with_result_memorized(pm): + class Hooks(object): + @hookspec(historic=True) + def he_method1(self, arg): + pass + pm.add_hookspecs(Hooks) + + he_method1 = pm.hook.he_method1 + he_method1.call_historic(lambda res: out.append(res), dict(arg=1)) + out = [] + + class Plugin(object): + @hookimpl + def he_method1(self, arg): + return arg * 10 + + pm.register(Plugin()) + assert out == [10] + + +def test_with_callbacks_immediately_executed(pm): + class Hooks(object): + @hookspec(historic=True) + def he_method1(self, arg): + pass + pm.add_hookspecs(Hooks) + + class Plugin1(object): + @hookimpl + def he_method1(self, arg): + return arg * 10 + + class Plugin2(object): + @hookimpl + def he_method1(self, arg): + return arg * 20 + + class Plugin3(object): + @hookimpl + def he_method1(self, arg): + return arg * 30 + + out = [] + pm.register(Plugin1()) + pm.register(Plugin2()) + + he_method1 = pm.hook.he_method1 + he_method1.call_historic(lambda res: out.append(res), dict(arg=1)) + assert out == [20, 10] + pm.register(Plugin3()) + assert out == [20, 10, 30] + + +def test_register_historic_incompat_hookwrapper(pm): + class Hooks(object): + @hookspec(historic=True) + def he_method1(self, arg): + pass + + pm.add_hookspecs(Hooks) + + out = [] + + class Plugin(object): + @hookimpl(hookwrapper=True) + def he_method1(self, arg): + out.append(arg) + + with pytest.raises(PluginValidationError): + pm.register(Plugin()) + + +def test_call_extra(pm): + class Hooks(object): + @hookspec + def he_method1(self, arg): + pass + + pm.add_hookspecs(Hooks) + + def he_method1(arg): + return arg * 10 + + out = pm.hook.he_method1.call_extra([he_method1], dict(arg=1)) + assert out == [10] + + +def test_call_with_too_few_args(pm): + class Hooks(object): + @hookspec + def he_method1(self, arg): + pass + + pm.add_hookspecs(Hooks) + + class Plugin1(object): + @hookimpl + def he_method1(self, arg): + 0 / 0 + pm.register(Plugin1()) + with pytest.raises(HookCallError): + with pytest.warns(UserWarning): + pm.hook.he_method1() + + +def test_subset_hook_caller(pm): + class Hooks(object): + @hookspec + def he_method1(self, arg): + pass + + pm.add_hookspecs(Hooks) + + out = [] + + class Plugin1(object): + @hookimpl + def he_method1(self, arg): + out.append(arg) + + class Plugin2(object): + @hookimpl + def he_method1(self, arg): + out.append(arg * 10) + + class PluginNo(object): + pass + + plugin1, plugin2, plugin3 = Plugin1(), Plugin2(), PluginNo() + pm.register(plugin1) + pm.register(plugin2) + pm.register(plugin3) + pm.hook.he_method1(arg=1) + assert out == [10, 1] + out[:] = [] + + hc = pm.subset_hook_caller("he_method1", [plugin1]) + hc(arg=2) + assert out == [20] + out[:] = [] + + hc = pm.subset_hook_caller("he_method1", [plugin2]) + hc(arg=2) + assert out == [2] + out[:] = [] + + pm.unregister(plugin1) + hc(arg=2) + assert out == [] + out[:] = [] + + pm.hook.he_method1(arg=1) + assert out == [10] + + +def test_multicall_deprecated(pm): + class P1(object): + @hookimpl + def m(self, __multicall__, x): + pass + + pytest.deprecated_call(pm.register, P1()) + + +def test_add_hookspecs_nohooks(pm): + with pytest.raises(ValueError): + pm.add_hookspecs(10) + + +def test_reject_prefixed_module(pm): + """Verify that a module type attribute that contains the project + prefix in its name (in this case `'example_*'` isn't collected + when registering a module which imports it. + """ + pm._implprefix = 'example' + conftest = types.ModuleType("conftest") + src = (""" +def example_hook(): + pass +""") + exec(src, conftest.__dict__) + conftest.example_blah = types.ModuleType("example_blah") + name = pm.register(conftest) + assert name == 'conftest' + assert getattr(pm.hook, 'example_blah', None) is None + assert getattr(pm.hook, 'example_hook', None) # conftest.example_hook should be collected + assert pm.parse_hookimpl_opts(conftest, 'example_blah') is None + assert pm.parse_hookimpl_opts(conftest, 'example_hook') == {} diff --git a/third_party/python/pluggy/testing/test_tracer.py b/third_party/python/pluggy/testing/test_tracer.py new file mode 100644 index 000000000000..4a3e16cec436 --- /dev/null +++ b/third_party/python/pluggy/testing/test_tracer.py @@ -0,0 +1,89 @@ + +from pluggy import _TagTracer + + +def test_simple(): + rootlogger = _TagTracer() + log = rootlogger.get("pytest") + log("hello") + out = [] + rootlogger.setwriter(out.append) + log("world") + assert len(out) == 1 + assert out[0] == "world [pytest]\n" + sublog = log.get("collection") + sublog("hello") + assert out[1] == "hello [pytest:collection]\n" + + +def test_indent(): + rootlogger = _TagTracer() + log = rootlogger.get("1") + out = [] + log.root.setwriter(lambda arg: out.append(arg)) + log("hello") + log.root.indent += 1 + log("line1") + log("line2") + log.root.indent += 1 + log("line3") + log("line4") + log.root.indent -= 1 + log("line5") + log.root.indent -= 1 + log("last") + assert len(out) == 7 + names = [x[:x.rfind(' [')] for x in out] + assert names == [ + 'hello', ' line1', ' line2', + ' line3', ' line4', ' line5', 'last'] + + +def test_readable_output_dictargs(): + rootlogger = _TagTracer() + + out = rootlogger.format_message(['test'], [1]) + assert out == ['1 [test]\n'] + + out2 = rootlogger.format_message(['test'], ['test', {'a': 1}]) + assert out2 == [ + 'test [test]\n', + ' a: 1\n' + ] + + +def test_setprocessor(): + rootlogger = _TagTracer() + log = rootlogger.get("1") + log2 = log.get("2") + assert log2.tags == tuple("12") + out = [] + rootlogger.setprocessor(tuple("12"), lambda *args: out.append(args)) + log("not seen") + log2("seen") + assert len(out) == 1 + tags, args = out[0] + assert "1" in tags + assert "2" in tags + assert args == ("seen",) + l2 = [] + rootlogger.setprocessor("1:2", lambda *args: l2.append(args)) + log2("seen") + tags, args = l2[0] + assert args == ("seen",) + + +def test_setmyprocessor(): + rootlogger = _TagTracer() + log = rootlogger.get("1") + log2 = log.get("2") + out = [] + log2.setmyprocessor(lambda *args: out.append(args)) + log("not seen") + assert not out + log2(42) + assert len(out) == 1 + tags, args = out[0] + assert "1" in tags + assert "2" in tags + assert args == (42,) diff --git a/third_party/python/pluggy/tox.ini b/third_party/python/pluggy/tox.ini new file mode 100644 index 000000000000..89d44e352dba --- /dev/null +++ b/third_party/python/pluggy/tox.ini @@ -0,0 +1,44 @@ +[tox] +envlist=check,docs,py{27,34,35,36,py}-pytestrelease,py{27,36}-pytest{master,features} + +[testenv] +commands=py.test {posargs:testing/} +setenv= + _PYTEST_SETUP_SKIP_PLUGGY_DEP=1 +deps= + pytestrelease: pytest + pytestmaster: git+https://github.com/pytest-dev/pytest.git@master + pytestfeatures: git+https://github.com/pytest-dev/pytest.git@features + +[testenv:benchmark] +commands=py.test {posargs:testing/benchmark.py} +deps= + pytest + pytest-benchmark + +[testenv:check] +deps = + flake8 + restructuredtext_lint + pygments +commands = + flake8 pluggy.py setup.py testing + rst-lint CHANGELOG.rst README.rst + +[testenv:docs] +deps = + sphinx + pygments +commands = + sphinx-build -b html {toxinidir}/docs {toxinidir}/build/html-docs + +[pytest] +minversion=2.0 +#--pyargs --doctest-modules --ignore=.tox +addopts=-rxsX +norecursedirs=.tox ja .hg .env* +filterwarnings = + error + +[flake8] +max-line-length=99 diff --git a/third_party/python/py/.gitignore b/third_party/python/py/.gitignore index 5bb0d457d498..375476fd3cde 100644 --- a/third_party/python/py/.gitignore +++ b/third_party/python/py/.gitignore @@ -10,3 +10,5 @@ __pycache__/ .eggs/ dist/* +/py/_version.py +.pytest_cache/ diff --git a/third_party/python/py/.hgignore b/third_party/python/py/.hgignore deleted file mode 100644 index 34976da5d153..000000000000 --- a/third_party/python/py/.hgignore +++ /dev/null @@ -1,29 +0,0 @@ - -# Automatically generated by `hgimportsvn` -syntax:glob -.svn -.hgsvn - -# These lines are suggested according to the svn:ignore property -# Feel free to enable them by uncommenting them -syntax:glob -*.pyc -*.pyo -*.swp -*.html -*.class -*.orig -*~ - -doc/_build -build/ -dist/ -*.egg-info -issue/ -env/ -3rdparty/ -.tox -lib/ -bin/ -include/ -src/ diff --git a/third_party/python/py/.hgtags b/third_party/python/py/.hgtags deleted file mode 100644 index 9d48095bf647..000000000000 --- a/third_party/python/py/.hgtags +++ /dev/null @@ -1,68 +0,0 @@ -52c6d9e78777a5a34e813123997dfc614a1a4767 1.0.0b3 -1c7aaa8c61f3b0945921a9acc7beb184201aed4b 1.0.0b4 -1c7aaa8c61f3b0945921a9acc7beb184201aed4b 1.0.0b4 -0000000000000000000000000000000000000000 1.0.0b4 -0000000000000000000000000000000000000000 1.0.0b4 -8cd6eb91eba313b012d6e568f37d844dc0751f2e 1.0.0b4 -8cd6eb91eba313b012d6e568f37d844dc0751f2e 1.0.0b4 -0000000000000000000000000000000000000000 1.0.0b4 -2cc0507f117ffe721dff7ee026648cfce00ec92f 1.0.0b6 -86f1e1b6e49bf5882a809f11edd1dbb08162cdad 1.0.0b8 -86f1e1b6e49bf5882a809f11edd1dbb08162cdad 1.0.0b8 -c63f35c266cbb26dad6b87b5e115d65685adf448 1.0.0b8 -c63f35c266cbb26dad6b87b5e115d65685adf448 1.0.0b8 -0eaa0fdf2ba0163cf534dc2eff4ba2e5fc66c261 1.0.0b8 -e2a60653cb490aeed81bbbd83c070b99401c211c 1.0.0b9 -5ea0cdf7854c3d4278d36eda94a2b68483a0e211 1.0.0 -5ea0cdf7854c3d4278d36eda94a2b68483a0e211 1.0.0 -7acde360d94b6a2690ce3d03ff39301da84c0a2b 1.0.0 -6bd221981ac99103002c1cb94fede400d23a96a1 1.0.1 -4816e8b80602a3fd3a0a120333ad85fbe7d8bab4 1.0.2 -60c44bdbf093285dc69d5462d4dbb4acad325ca6 1.1.0 -319187fcda66714c5eb1353492babeec3d3c826f 1.1.1 -4fc5212f7626a56b9eb6437b5c673f56dd7eb942 1.2.0 -c143a8c8840a1c68570890c8ac6165bbf92fd3c6 1.2.1 -eafd3c256e8732dfb0a4d49d051b5b4339858926 1.3.0 -d5eacf390af74553227122b85e20345d47b2f9e6 1.3.1 -d5eacf390af74553227122b85e20345d47b2f9e6 1.3.1 -8b8e7c25a13cf863f01b2dd955978285ae9daf6a 1.3.1 -3bff44b188a7ec1af328d977b9d39b6757bb38df 1.3.2 -c59d3fa8681a5b5966b8375b16fccd64a3a8dbeb 1.3.3 -79ef6377705184c55633d456832eea318fedcf61 1.3.4 -79ef6377705184c55633d456832eea318fedcf61 1.3.4 -90fffd35373e9f125af233f78b19416f0938d841 1.3.4 -5346ab41b059c95a48cbe1e8a7bae96ce6e0da27 1.4.0 -1f3125cba7976538952be268f107c1d0c36c5ce8 1.4.1 -04ab22db4ff737cf31e91d75a0f5d7077f324167 1.4.2 -9950bf9d684a984d511795013421c89c5cf88bef 1.4.3 -d9951e3bdbc765e73835ae13012f6a074d13d8bf 1.4.4 -b827dd156a36753e32c7f3f15ce82d6fe9e356c8 1.4.6 -f15726f9e5a67cc6221c499affa4840e9d591763 1.4.7 -abfabd07a1d328f13c730e8a50d80d2e470afd3b 1.4.9 -7f37ee0aff9be4b839d6759cfee336f60e8393a4 1.4.10 -fe4593263efa10ea7ba014db6e3379e0b82368a2 1.4.11 -f07af25a26786e4825b5170e17ad693245cb3426 1.4.12 -d3730d84ba7eda92fd3469a3f63fd6d8cb22c975 1.4.13 -12c1ae8e7c5345721e9ec9f8e27b1e36c07f74dc 1.4.14 -12c1ae8e7c5345721e9ec9f8e27b1e36c07f74dc 1.4.14 -0000000000000000000000000000000000000000 1.4.14 -0000000000000000000000000000000000000000 1.4.14 -1497e2efd0f8c73a0e3d529debf0c489e4cd6cab 1.4.14 -e065014c1ce8ad110a381e9baaaa5d647ba7ac6b 1.4.15 -e9e5b38f53dc35b35aa1f9ee9a9be9bbd2d2c3b1 1.4.16 -c603503945f52b78522d96a423605cbc953236d3 1.4.17 -c59201105a29801cc858eb9160b7a19791b91a35 1.4.18 -284cc172e294d48edc840012e1451c32c3963d92 1.4.19 -a3e0626aa0c5aecf271367dc77e476ab216ea3c8 1.4.20 -5e48016c4a3af8e7358a1267d33d021e71765bed 1.4.21 -01ae2cfcc61c4fcb3aa5031349adb5b467c31018 1.4.23 -5ffd982f4dff60b588f309cd9bdc61036547282a 1.4.24 -dc9ffbcaf1f7d72e96be3f68c11deebb7e7193c5 1.4.25 -6de1a44bf75de7af4fcae947c235e9072bbdbb9a 1.4.26 -7d650ba2657890a2253c8c4a83f170febebd90fa 1.4.27 -7d650ba2657890a2253c8c4a83f170febebd90fa 1.4.27 -1810003dec63dd1b506a23849861fffa5bc3ba13 1.4.27 -ba08706f08ddea1b77a426f00dfe2bdc244345e8 1.4.28 -4e8054ada63f3327bcf759ae7cd36c7c8652bc9b 1.4.29 -366ab346610c6de8aaa7617e24011794b40236c6 1.4.30 -657380e439f9b7e04918cb162cb2e46388244b42 1.4.31 diff --git a/third_party/python/py/.travis.yml b/third_party/python/py/.travis.yml index 917c59d14b70..ea75028de52e 100644 --- a/third_party/python/py/.travis.yml +++ b/third_party/python/py/.travis.yml @@ -17,6 +17,23 @@ matrix: - python: '2.7' # using a different option due to pytest-addopts pytester issues env: PYTEST_XADDOPTS="-n 3 --runslowtests" DEPS="pytest~=3.0.0 pytest-xdist" + + - stage: deploy + python: '3.6' + env: + install: pip install -U setuptools setuptools_scm + script: skip + deploy: + provider: pypi + user: nicoddemus + distributions: sdist bdist_wheel + skip_upload_docs: true + password: + secure: VNYW/sZoD+9DzKCe6vANNXXJR7jP7rwySafQ33N1jAnCrdylQjEN/p6tSfUe8jDi3wDpLPL9h8pwfxuUT7CRxglHov3Qe7zSeywixvHan5aFahQiQ8+gucYIM7wITHH3oQs7jN35pnhdnF+QlW2+eDCL6qOLU5XwuRhsDKXjQ/hUWR5hlX5EniD1gzyKEf6j1YCpST87tKpeLwVEYEmsucdkUZuXhxDtyaWQHWiPsLWwh/slQtUJEHeLF26r8UxFy0RiGne9jR+CzRfH5ktcA9/pArvp4VuwOii+1TDxVSYP7+I8Z+eUKN9JBg12QLaHwoIN/8J+MvHCkuf+OGSLM3sEyNRJGDev372xg3K7ylIkeeK4WXirKEp2ojgN8tniloDjnwdu/gPWBnrXuooA60tNoByHFa8KbMZAr2B2sQeMxD4VZGr1N8l0rX4gRTrwvdk3i3ulLKVSwkXaGn+GrfZTTboa7dEnpuma8tv1niNCSpStYIy7atS8129+5ijV3OC8DzOMh/rVbO9WsDb/RPG3yjFiDvEJPIPeE0l/m5u42QBqtdZSS2ia7UWTJBiEY09uFMTRmH5hhE/1aiYBbvAztf5CReUbeKdSQz3L8TTSZqewtFZmXTkX97/xQnrEpsnGezIM2DNuMEuQG3MxGkNCxwbQKpx/bkHdrD75yMk= + on: + tags: true + repo: pytest-dev/py + allow_failures: - python: 'pypy-5.4' install: diff --git a/third_party/python/py/CHANGELOG b/third_party/python/py/CHANGELOG index d033f0ee3107..a17cdb5992f2 100644 --- a/third_party/python/py/CHANGELOG +++ b/third_party/python/py/CHANGELOG @@ -1,6 +1,12 @@ -1.5.3 (unreleased) +1.5.4 (2018-06-27) ================== +- fix pytest-dev/pytest#3451: don't make assumptions about fs case sensitivity + in ``make_numbered_dir``. + +1.5.3 +===== + - fix #179: ensure we can support 'from py.error import ...' 1.5.2 diff --git a/third_party/python/py/HOWTORELEASE.rst b/third_party/python/py/HOWTORELEASE.rst new file mode 100644 index 000000000000..8d0231639db8 --- /dev/null +++ b/third_party/python/py/HOWTORELEASE.rst @@ -0,0 +1,17 @@ +Release Procedure +----------------- + +#. Create a branch ``release-X.Y.Z`` from the latest ``master``. + +#. Manually update the ``CHANGELOG`` and commit. + +#. Open a PR for this branch targeting ``master``. + +#. After all tests pass and the PR has been approved by at least another maintainer, publish to PyPI by creating and pushing a tag:: + + git tag X.Y.Z + git push git@github.com:pytest-dev/py X.Y.Z + + Wait for the deploy to complete, then make sure it is `available on PyPI `_. + +#. Merge your PR to ``master``. diff --git a/third_party/python/py/PKG-INFO b/third_party/python/py/PKG-INFO index 0e5eb0360a6f..359dcef42126 100644 --- a/third_party/python/py/PKG-INFO +++ b/third_party/python/py/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 1.2 Name: py -Version: 1.5.3 +Version: 1.5.4 Summary: library with cross-python path, ini-parsing, io, code, log facilities Home-page: http://py.readthedocs.io/ Author: holger krekel, Ronny Pfannschmidt, Benjamin Peterson and others @@ -27,10 +27,10 @@ Description: .. image:: https://img.shields.io/pypi/v/py.svg The py lib is a Python development support library featuring the following tools and modules: - * ``py.path``: uniform local and svn path objects - * ``py.apipkg``: explicit API control and lazy-importing - * ``py.iniconfig``: easy parsing of .ini files - * ``py.code``: dynamic code generation and introspection (deprecated, moved to ``pytest``). + * ``py.path``: uniform local and svn path objects -> please use pathlib/pathlib2 instead + * ``py.apipkg``: explicit API control and lazy-importing -> please use the standalone package instead + * ``py.iniconfig``: easy parsing of .ini files -> please use the standalone package instead + * ``py.code``: dynamic code generation and introspection (deprecated, moved to ``pytest`` as a implementation detail). **NOTE**: prior to the 1.4 release this distribution used to contain py.test which is now its own package, see http://pytest.org diff --git a/third_party/python/py/README.rst b/third_party/python/py/README.rst index 7092ae4c460c..3d9ec0faeb1e 100644 --- a/third_party/python/py/README.rst +++ b/third_party/python/py/README.rst @@ -19,10 +19,10 @@ The py lib is a Python development support library featuring the following tools and modules: -* ``py.path``: uniform local and svn path objects -* ``py.apipkg``: explicit API control and lazy-importing -* ``py.iniconfig``: easy parsing of .ini files -* ``py.code``: dynamic code generation and introspection (deprecated, moved to ``pytest``). +* ``py.path``: uniform local and svn path objects -> please use pathlib/pathlib2 instead +* ``py.apipkg``: explicit API control and lazy-importing -> please use the standalone package instead +* ``py.iniconfig``: easy parsing of .ini files -> please use the standalone package instead +* ``py.code``: dynamic code generation and introspection (deprecated, moved to ``pytest`` as a implementation detail). **NOTE**: prior to the 1.4 release this distribution used to contain py.test which is now its own package, see http://pytest.org diff --git a/third_party/python/py/appveyor.yml b/third_party/python/py/appveyor.yml new file mode 100644 index 000000000000..5fbeca9ab659 --- /dev/null +++ b/third_party/python/py/appveyor.yml @@ -0,0 +1,26 @@ +environment: + matrix: + # note: please use "tox --listenvs" to populate the build matrix below + - TOXENV: "py27-pytest29" + - TOXENV: "py27-pytest30" + - TOXENV: "py27-pytest31" + - TOXENV: "py34-pytest29" + - TOXENV: "py34-pytest30" + - TOXENV: "py34-pytest31" + - TOXENV: "py35-pytest29" + - TOXENV: "py35-pytest30" + - TOXENV: "py35-pytest31" + - TOXENV: "py36-pytest29" + - TOXENV: "py36-pytest30" + - TOXENV: "py36-pytest31" + +install: + - echo Installed Pythons + - dir c:\Python* + + - C:\Python36\python -m pip install --upgrade --pre tox + +build: false # Not a C# project, build stuff at the test step instead. + +test_script: + - C:\Python36\python -m tox diff --git a/third_party/python/py/doc/path.txt b/third_party/python/py/doc/path.txt index c906179099af..3c1d321a3341 100644 --- a/third_party/python/py/doc/path.txt +++ b/third_party/python/py/doc/path.txt @@ -2,6 +2,12 @@ py.path ======= + **Note**: The 'py' library is in "maintenance mode" and so is not + recommended for new projects. Please check out + `pathlib `_ or + `pathlib2 `_ for path + operations. + The 'py' lib provides a uniform high-level api to deal with filesystems and filesystem-like interfaces: ``py.path``. It aims to offer a central object to fs-like object trees (reading from and writing to files, adding diff --git a/third_party/python/py/py/__init__.py b/third_party/python/py/py/__init__.py index be27e920046e..b892ce1a2a6b 100644 --- a/third_party/python/py/py/__init__.py +++ b/third_party/python/py/py/__init__.py @@ -18,7 +18,12 @@ except ImportError: import apipkg lib_not_mangled_by_packagers = False vendor_prefix = '' -__version__ = '1.5.3' + +try: + from ._version import version as __version__ +except ImportError: + # broken installation, we don't even try + __version__ = "unknown" apipkg.initpkg(__name__, attr={'_apipkg': apipkg, 'error': error}, exportdefs={ diff --git a/third_party/python/py/py/_path/local.py b/third_party/python/py/py/_path/local.py index 5a785b0fb281..79dc6284c3da 100644 --- a/third_party/python/py/py/_path/local.py +++ b/third_party/python/py/py/_path/local.py @@ -4,13 +4,13 @@ local path implementation. from __future__ import with_statement from contextlib import contextmanager -import sys, os, re, atexit, io, uuid +import sys, os, atexit, io, uuid import py from py._path import common from py._path.common import iswin32, fspath from stat import S_ISLNK, S_ISDIR, S_ISREG -from os.path import abspath, normcase, normpath, isabs, exists, isdir, isfile, islink, dirname +from os.path import abspath, normpath, isabs, exists, isdir, isfile, islink, dirname if sys.version_info > (3,0): def map_as_list(func, iter): @@ -800,7 +800,7 @@ class LocalPath(FSBase): return cls(py.error.checked_call(tempfile.mkdtemp, dir=str(rootdir))) def make_numbered_dir(cls, prefix='session-', rootdir=None, keep=3, - lock_timeout = 172800): # two days + lock_timeout=172800): # two days """ return unique directory with a number greater than the current maximum one. The number is assumed to start directly after prefix. if keep is true directories with a number less than (maxnum-keep) @@ -810,10 +810,10 @@ class LocalPath(FSBase): if rootdir is None: rootdir = cls.get_temproot() - nprefix = normcase(prefix) + nprefix = prefix.lower() def parse_num(path): """ parse the number out of a path (if it matches the prefix) """ - nbasename = normcase(path.basename) + nbasename = path.basename.lower() if nbasename.startswith(nprefix): try: return int(nbasename[len(nprefix):]) diff --git a/third_party/python/pytest/_pytest/_version.py b/third_party/python/py/py/_version.py similarity index 84% rename from third_party/python/pytest/_pytest/_version.py rename to third_party/python/py/py/_version.py index 3edb7da9ad9f..e7188f937f1e 100644 --- a/third_party/python/pytest/_pytest/_version.py +++ b/third_party/python/py/py/_version.py @@ -1,4 +1,4 @@ # coding: utf-8 # file generated by setuptools_scm # don't change, don't track in version control -version = '3.2.5' +version = '1.5.4' diff --git a/third_party/python/py/setup.py b/third_party/python/py/setup.py index 959323b0c75b..a0d723c307eb 100644 --- a/third_party/python/py/setup.py +++ b/third_party/python/py/setup.py @@ -1,25 +1,13 @@ -import os -import sys - from setuptools import setup, find_packages -def get_version(): - p = os.path.join(os.path.dirname( - os.path.abspath(__file__)), "py", "__init__.py") - with open(p) as f: - for line in f.readlines(): - if "__version__" in line: - return line.strip().split("=")[-1].strip(" '") - raise ValueError("could not read version") - - def main(): setup( name='py', description='library with cross-python path, ini-parsing, io, code, log facilities', long_description=open('README.rst').read(), - version=get_version(), + use_scm_version={"write_to": "py/_version.py"}, + setup_requires=["setuptools-scm"], url='http://py.readthedocs.io/', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff --git a/third_party/python/py/testing/path/test_local.py b/third_party/python/py/testing/path/test_local.py index c9075d6fbb42..ee4b9bde9c31 100644 --- a/third_party/python/py/testing/path/test_local.py +++ b/third_party/python/py/testing/path/test_local.py @@ -425,24 +425,23 @@ class TestExecution: if i >= 3: assert not numdir.new(ext=str(i-3)).check() - def test_make_numbered_dir_case_insensitive(self, tmpdir, monkeypatch): - # https://github.com/pytest-dev/pytest/issues/708 - monkeypatch.setattr(py._path.local, 'normcase', - lambda path: path.lower()) - monkeypatch.setattr(tmpdir, 'listdir', - lambda: [tmpdir._fastjoin('case.0')]) - numdir = local.make_numbered_dir(prefix='CAse.', rootdir=tmpdir, - keep=2, lock_timeout=0) - assert numdir.basename.endswith('.1') + def test_make_numbered_dir_case(self, tmpdir): + """make_numbered_dir does not make assumptions on the underlying + filesystem based on the platform and will assume it _could_ be case + insensitive. - def test_make_numbered_dir_case_sensitive(self, tmpdir, monkeypatch): - # https://github.com/pytest-dev/pytest/issues/708 - monkeypatch.setattr(py._path.local, 'normcase', lambda path: path) - monkeypatch.setattr(tmpdir, 'listdir', - lambda: [tmpdir._fastjoin('case.0')]) - numdir = local.make_numbered_dir(prefix='CAse.', rootdir=tmpdir, - keep=2, lock_timeout=0) - assert numdir.basename.endswith('.0') + See issues: + - https://github.com/pytest-dev/pytest/issues/708 + - https://github.com/pytest-dev/pytest/issues/3451 + """ + d1 = local.make_numbered_dir( + prefix='CAse.', rootdir=tmpdir, keep=2, lock_timeout=0, + ) + d2 = local.make_numbered_dir( + prefix='caSE.', rootdir=tmpdir, keep=2, lock_timeout=0, + ) + assert str(d1).lower() != str(d2).lower() + assert str(d2).endswith('.1') def test_make_numbered_dir_NotImplemented_Error(self, tmpdir, monkeypatch): def notimpl(x, y): diff --git a/third_party/python/pytest/.coveragerc b/third_party/python/pytest/.coveragerc index 27db64e09c12..61ff66749dc8 100644 --- a/third_party/python/pytest/.coveragerc +++ b/third_party/python/pytest/.coveragerc @@ -1,7 +1,4 @@ [run] -omit = +omit = # standlonetemplate is read dynamically and tested by test_genscript *standalonetemplate.py - # oldinterpret could be removed, as it is no longer used in py26+ - *oldinterpret.py - vendored_packages diff --git a/third_party/python/pytest/.github/PULL_REQUEST_TEMPLATE.md b/third_party/python/pytest/.github/PULL_REQUEST_TEMPLATE.md index bf9fc199f59f..23a9f8c56832 100644 --- a/third_party/python/pytest/.github/PULL_REQUEST_TEMPLATE.md +++ b/third_party/python/pytest/.github/PULL_REQUEST_TEMPLATE.md @@ -1,15 +1,14 @@ Thanks for submitting a PR, your contribution is really appreciated! -Here's a quick checklist that should be present in PRs: +Here's a quick checklist that should be present in PRs (you can delete this text from the final description, this is +just a guideline): -- [ ] Add a new news fragment into the changelog folder - * name it `$issue_id.$type` for example (588.bug) - * if you don't have an issue_id change it to the pr id after creating the pr - * ensure type is one of `removal`, `feature`, `bugfix`, `vendor`, `doc` or `trivial` - * Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files." -- [ ] Target: for `bugfix`, `vendor`, `doc` or `trivial` fixes, target `master`; for removals or features target `features`; -- [ ] Make sure to include reasonable tests for your change if necessary +- [ ] Create a new changelog file in the `changelog` folder, with a name like `..rst`. See [changelog/README.rst](/changelog/README.rst) for details. +- [ ] Target the `master` branch for bug fixes, documentation updates and trivial changes. +- [ ] Target the `features` branch for new features and removals/deprecations. +- [ ] Include documentation when adding new features. +- [ ] Include new tests or update existing tests when applicable. -Unless your change is a trivial or a documentation fix (e.g., a typo or reword of a small section) please: +Unless your change is trivial or a small documentation fix (e.g., a typo or reword of a small section) please: -- [ ] Add yourself to `AUTHORS`, in alphabetical order; +- [ ] Add yourself to `AUTHORS` in alphabetical order; diff --git a/third_party/python/pytest/.gitignore b/third_party/python/pytest/.gitignore index 3b7ec9facf23..afb6bf9fd3f9 100644 --- a/third_party/python/pytest/.gitignore +++ b/third_party/python/pytest/.gitignore @@ -19,7 +19,7 @@ include/ .hypothesis/ # autogenerated -_pytest/_version.py +src/_pytest/_version.py # setuptools .eggs/ @@ -33,6 +33,7 @@ env/ 3rdparty/ .tox .cache +.pytest_cache .coverage .ropeproject .idea diff --git a/third_party/python/pytest/.pre-commit-config.yaml b/third_party/python/pytest/.pre-commit-config.yaml new file mode 100644 index 000000000000..e50891bbc1cb --- /dev/null +++ b/third_party/python/pytest/.pre-commit-config.yaml @@ -0,0 +1,36 @@ +exclude: doc/en/example/py2py3/test_py2.py +repos: +- repo: https://github.com/ambv/black + rev: 18.4a4 + hooks: + - id: black + args: [--safe, --quiet] + language_version: python3.6 +- repo: https://github.com/asottile/blacken-docs + rev: v0.1.1 + hooks: + - id: blacken-docs + additional_dependencies: [black==18.5b1] + language_version: python3.6 +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v1.2.3 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: debug-statements + exclude: _pytest/debugging.py + - id: flake8 +- repo: https://github.com/asottile/pyupgrade + rev: v1.2.0 + hooks: + - id: pyupgrade +- repo: local + hooks: + - id: rst + name: rst + entry: rst-lint --encoding utf-8 + files: ^(CHANGELOG.rst|HOWTORELEASE.rst|README.rst|changelog/.*)$ + language: python + additional_dependencies: [pygments, restructuredtext_lint] + python_version: python3.6 diff --git a/third_party/python/pytest/.travis.yml b/third_party/python/pytest/.travis.yml index b9ced86461c3..1d092149b5d5 100644 --- a/third_party/python/pytest/.travis.yml +++ b/third_party/python/pytest/.travis.yml @@ -1,17 +1,19 @@ sudo: false language: python +stages: +- linting +- test +- deploy python: - '3.6' -# command to install dependencies install: - pip install --upgrade --pre tox -# # command to run tests env: matrix: # coveralls is not listed in tox's envlist, but should run in travis - TOXENV=coveralls # note: please use "tox --listenvs" to populate the build matrix below - - TOXENV=linting + # please remove the linting env in all cases - TOXENV=py27 - TOXENV=py34 - TOXENV=py36 @@ -19,20 +21,18 @@ env: - TOXENV=py27-xdist - TOXENV=py27-trial - TOXENV=py27-numpy + - TOXENV=py27-pluggymaster - TOXENV=py36-pexpect - TOXENV=py36-xdist - TOXENV=py36-trial - TOXENV=py36-numpy + - TOXENV=py36-pluggymaster - TOXENV=py27-nobyte - TOXENV=doctesting - TOXENV=docs -matrix: +jobs: include: - - env: TOXENV=py26 - python: '2.6' - - env: TOXENV=py33 - python: '3.3' - env: TOXENV=pypy python: 'pypy-5.4' - env: TOXENV=py35 @@ -41,9 +41,30 @@ matrix: python: '3.5' - env: TOXENV=py37 python: 'nightly' - allow_failures: - - env: TOXENV=py37 - python: 'nightly' + + - stage: deploy + python: '3.6' + env: + install: pip install -U setuptools setuptools_scm + script: skip + deploy: + provider: pypi + user: nicoddemus + distributions: sdist bdist_wheel + skip_upload_docs: true + password: + secure: xanTgTUu6XDQVqB/0bwJQXoDMnU5tkwZc5koz6mBkkqZhKdNOi2CLoC1XhiSZ+ah24l4V1E0GAqY5kBBcy9d7NVe4WNg4tD095LsHw+CRU6/HCVIFfyk2IZ+FPAlguesCcUiJSXOrlBF+Wj68wEvLoK7EoRFbJeiZ/f91Ww1sbtDlqXABWGHrmhPJL5Wva7o7+wG7JwJowqdZg1pbQExsCc7b53w4v2RBu3D6TJaTAzHiVsW+nUSI67vKI/uf+cR/OixsTfy37wlHgSwihYmrYLFls3V0bSpahCim3bCgMaFZx8S8xrdgJ++PzBCof2HeflFKvW+VCkoYzGEG4NrTWJoNz6ni4red9GdvfjGH3YCjAKS56h9x58zp2E5rpsb/kVq5/45xzV+dq6JRuhQ1nJWjBC6fSKAc/bfwnuFK3EBxNLkvBssLHvsNjj5XG++cB8DdS9wVGUqjpoK4puaXUWFqy4q3S9F86HEsKNgExtieA9qNx+pCIZVs6JCXZNjr0I5eVNzqJIyggNgJG6RyravsU35t9Zd9doL5g4Y7UKmAGTn1Sz24HQ4sMQgXdm2SyD8gEK5je4tlhUvfGtDvMSlstq71kIn9nRpFnqB6MFlbYSEAZmo8dGbCquoUc++6Rum208wcVbrzzVtGlXB/Ow9AbFMYeAGA0+N/K1e59c= + on: + tags: true + repo: pytest-dev/pytest + - stage: linting + python: '3.6' + env: + install: + - pip install pre-commit + - pre-commit install-hooks + script: + - pre-commit run --all-files script: tox --recreate @@ -56,3 +77,7 @@ notifications: skip_join: true email: - pytest-commit@python.org +cache: + directories: + - $HOME/.cache/pip + - $HOME/.cache/pre-commit diff --git a/third_party/python/pytest/AUTHORS b/third_party/python/pytest/AUTHORS index cc789b3b664b..3edfdcf85ea6 100644 --- a/third_party/python/pytest/AUTHORS +++ b/third_party/python/pytest/AUTHORS @@ -3,21 +3,27 @@ merlinux GmbH, Germany, office at merlinux eu Contributors include:: +Aaron Coleman Abdeali JK Abhijeet Kasurde Ahn Ki-Wook +Alan Velasco Alexander Johnson Alexei Kozlenok Anatoly Bubenkoff +Anders Hovmöller Andras Tim Andreas Zeidler Andrzej Ostrowski Andy Freeland Anthon van der Neut +Anthony Shaw Anthony Sottile Antony Lee Armin Rigo +Aron Coyle Aron Curzon +Aviral Verma Aviv Palivoda Barney Gale Ben Webb @@ -25,11 +31,14 @@ Benjamin Peterson Bernard Pratz Bob Ippolito Brian Dorsey +Brian Maissy Brian Okken Brianna Laugher Bruno Oliveira Cal Leeming Carl Friedrich Bolz +Carlos Jenkins +Ceridwen Charles Cloud Charnjit SiNGH (CCSJ) Chris Lamb @@ -37,6 +46,7 @@ Christian Boelsen Christian Theunert Christian Tismer Christopher Gilling +Cyrus Maden Daniel Grana Daniel Hahler Daniel Nuri @@ -65,34 +75,44 @@ Feng Ma Florian Bruhin Floris Bruynooghe Gabriel Reis +George Kussumoto Georgy Dyuldin Graham Horler Greg Price Grig Gheorghiu Grigorii Eremeev (budulianin) Guido Wesdorp +Guoqiang Zhang Harald Armin Massa +Henk-Jaap Wagenaar +Hugo van Kemenade Hui Wang (coldnight) Ian Bicking +Ian Lesperance Jaap Broekhuizen Jan Balster Janne Vanhala Jason R. Coombs Javier Domingo Cansino Javier Romero +Jeff Rackauckas Jeff Widman +John Eddie Ayson John Towler Jon Sonesen Jonas Obrist Jordan Guymon Jordan Moldow +Jordan Speicher Joshua Bronson Jurko Gospodnetić Justyna Janczyszyn Kale Kundert Katarzyna Jachim +Katerina Koukiou Kevin Cox Kodi B. Arfer +Kostis Anagnostopoulos Lawrence Mitchell Lee Kamentsky Lev Maximov @@ -118,6 +138,7 @@ Matt Bachmann Matt Duck Matt Williams Matthias Hafner +Maxim Filipenko mbyt Michael Aquilina Michael Birtwell @@ -126,22 +147,26 @@ Michael Seifert Michal Wajszczuk Mihai Capotă Mike Lundy +Miro Hrončok Nathaniel Waisbrot Ned Batchelder Neven Mundar Nicolas Delaby Oleg Pidsadnyi +Oleg Sushchenko Oliver Bestwalter Omar Kohl Omer Hadari Patrick Hayes Paweł Adamczak +Pedro Algarvio Pieter Mulder Piotr Banaszkiewicz Punyashloka Biswal Quentin Pradet Ralf Schmitt Ran Benita +Raphael Castaneda Raphael Pierzina Raquel Alegre Ravi Chandra @@ -152,6 +177,7 @@ Ronny Pfannschmidt Ross Lawley Russel Winder Ryan Wooden +Samuel Dion-Girardeau Samuele Pedroni Segev Finer Simon Gomizelj @@ -162,19 +188,26 @@ Stefan Zimmermann Stefano Taschini Steffen Allner Stephan Obermann +Tarcisio Fischer Tareq Alayan Ted Xiao Thomas Grainger +Thomas Hisch +Tim Strazny Tom Dalton Tom Viner Trevor Bekolay Tyler Goodlet +Tzu-ping Chung Vasily Kuznetsov Victor Uriarte Vidar T. Fauske Vitaly Lashmanov Vlad Dragos +William Lee Wouter van Ackooy Xuan Luong Xuecong Liao Zoltán Máté +Roland Puntaier +Allan Feldman diff --git a/third_party/python/pytest/CHANGELOG.rst b/third_party/python/pytest/CHANGELOG.rst index 4146670f2f1e..21a090414a0d 100644 --- a/third_party/python/pytest/CHANGELOG.rst +++ b/third_party/python/pytest/CHANGELOG.rst @@ -1,4 +1,4 @@ -.. +.. You should *NOT* be adding new change log entries to this file, this file is managed by towncrier. You *may* edit previous change logs to fix problems like typo corrections or such. @@ -8,6 +8,872 @@ .. towncrier release notes start +Pytest 3.6.2 (2018-06-20) +========================= + +Bug Fixes +--------- + +- Fix regression in ``Node.add_marker`` by extracting the mark object of a + ``MarkDecorator``. (`#3555 + `_) + +- Warnings without ``location`` were reported as ``None``. This is corrected to + now report ````. (`#3563 + `_) + +- Continue to call finalizers in the stack when a finalizer in a former scope + raises an exception. (`#3569 + `_) + +- Fix encoding error with `print` statements in doctests (`#3583 + `_) + + +Improved Documentation +---------------------- + +- Add documentation for the ``--strict`` flag. (`#3549 + `_) + + +Trivial/Internal Changes +------------------------ + +- Update old quotation style to parens in fixture.rst documentation. (`#3525 + `_) + +- Improve display of hint about ``--fulltrace`` with ``KeyboardInterrupt``. + (`#3545 `_) + +- pytest's testsuite is no longer runnable through ``python setup.py test`` -- + instead invoke ``pytest`` or ``tox`` directly. (`#3552 + `_) + +- Fix typo in documentation (`#3567 + `_) + + +Pytest 3.6.1 (2018-06-05) +========================= + +Bug Fixes +--------- + +- Fixed a bug where stdout and stderr were logged twice by junitxml when a test + was marked xfail. (`#3491 + `_) + +- Fix ``usefixtures`` mark applyed to unittest tests by correctly instantiating + ``FixtureInfo``. (`#3498 + `_) + +- Fix assertion rewriter compatibility with libraries that monkey patch + ``file`` objects. (`#3503 + `_) + + +Improved Documentation +---------------------- + +- Added a section on how to use fixtures as factories to the fixture + documentation. (`#3461 `_) + + +Trivial/Internal Changes +------------------------ + +- Enable caching for pip/pre-commit in order to reduce build time on + travis/appveyor. (`#3502 + `_) + +- Switch pytest to the src/ layout as we already suggested it for good practice + - now we implement it as well. (`#3513 + `_) + +- Fix if in tests to support 3.7.0b5, where a docstring handling in AST got + reverted. (`#3530 `_) + +- Remove some python2.5 compatibility code. (`#3529 + `_) + + +Pytest 3.6.0 (2018-05-23) +========================= + +Features +-------- + +- Revamp the internals of the ``pytest.mark`` implementation with correct per + node handling which fixes a number of long standing bugs caused by the old + design. This introduces new ``Node.iter_markers(name)`` and + ``Node.get_closest_mark(name)`` APIs. Users are **strongly encouraged** to + read the `reasons for the revamp in the docs + `_, + or jump over to details about `updating existing code to use the new APIs + `_. (`#3317 + `_) + +- Now when ``@pytest.fixture`` is applied more than once to the same function a + ``ValueError`` is raised. This buggy behavior would cause surprising problems + and if was working for a test suite it was mostly by accident. (`#2334 + `_) + +- Support for Python 3.7's builtin ``breakpoint()`` method, see `Using the + builtin breakpoint function + `_ for + details. (`#3180 `_) + +- ``monkeypatch`` now supports a ``context()`` function which acts as a context + manager which undoes all patching done within the ``with`` block. (`#3290 + `_) + +- The ``--pdb`` option now causes KeyboardInterrupt to enter the debugger, + instead of stopping the test session. On python 2.7, hitting CTRL+C again + exits the debugger. On python 3.2 and higher, use CTRL+D. (`#3299 + `_) + +- pytest not longer changes the log level of the root logger when the + ``log-level`` parameter has greater numeric value than that of the level of + the root logger, which makes it play better with custom logging configuration + in user code. (`#3307 `_) + + +Bug Fixes +--------- + +- A rare race-condition which might result in corrupted ``.pyc`` files on + Windows has been hopefully solved. (`#3008 + `_) + +- Also use iter_marker for discovering the marks applying for marker + expressions from the cli to avoid the bad data from the legacy mark storage. + (`#3441 `_) + +- When showing diffs of failed assertions where the contents contain only + whitespace, escape them using ``repr()`` first to make it easy to spot the + differences. (`#3443 `_) + + +Improved Documentation +---------------------- + +- Change documentation copyright year to a range which auto-updates itself each + time it is published. (`#3303 + `_) + + +Trivial/Internal Changes +------------------------ + +- ``pytest`` now depends on the `python-atomicwrites + `_ library. (`#3008 + `_) + +- Update all pypi.python.org URLs to pypi.org. (`#3431 + `_) + +- Detect `pytest_` prefixed hooks using the internal plugin manager since + ``pluggy`` is deprecating the ``implprefix`` argument to ``PluginManager``. + (`#3487 `_) + +- Import ``Mapping`` and ``Sequence`` from ``_pytest.compat`` instead of + directly from ``collections`` in ``python_api.py::approx``. Add ``Mapping`` + to ``_pytest.compat``, import it from ``collections`` on python 2, but from + ``collections.abc`` on Python 3 to avoid a ``DeprecationWarning`` on Python + 3.7 or newer. (`#3497 `_) + + +Pytest 3.5.1 (2018-04-23) +========================= + + +Bug Fixes +--------- + +- Reset ``sys.last_type``, ``sys.last_value`` and ``sys.last_traceback`` before + each test executes. Those attributes are added by pytest during the test run + to aid debugging, but were never reset so they would create a leaking + reference to the last failing test's frame which in turn could never be + reclaimed by the garbage collector. (`#2798 + `_) + +- ``pytest.raises`` now raises ``TypeError`` when receiving an unknown keyword + argument. (`#3348 `_) + +- ``pytest.raises`` now works with exception classes that look like iterables. + (`#3372 `_) + + +Improved Documentation +---------------------- + +- Fix typo in ``caplog`` fixture documentation, which incorrectly identified + certain attributes as methods. (`#3406 + `_) + + +Trivial/Internal Changes +------------------------ + +- Added a more indicative error message when parametrizing a function whose + argument takes a default value. (`#3221 + `_) + +- Remove internal ``_pytest.terminal.flatten`` function in favor of + ``more_itertools.collapse``. (`#3330 + `_) + +- Import some modules from ``collections.abc`` instead of ``collections`` as + the former modules trigger ``DeprecationWarning`` in Python 3.7. (`#3339 + `_) + +- record_property is no longer experimental, removing the warnings was + forgotten. (`#3360 `_) + +- Mention in documentation and CLI help that fixtures with leading ``_`` are + printed by ``pytest --fixtures`` only if the ``-v`` option is added. (`#3398 + `_) + + +Pytest 3.5.0 (2018-03-21) +========================= + +Deprecations and Removals +------------------------- + +- ``record_xml_property`` fixture is now deprecated in favor of the more + generic ``record_property``. (`#2770 + `_) + +- Defining ``pytest_plugins`` is now deprecated in non-top-level conftest.py + files, because they "leak" to the entire directory tree. (`#3084 + `_) + + +Features +-------- + +- New ``--show-capture`` command-line option that allows to specify how to + display captured output when tests fail: ``no``, ``stdout``, ``stderr``, + ``log`` or ``all`` (the default). (`#1478 + `_) + +- New ``--rootdir`` command-line option to override the rules for discovering + the root directory. See `customize + `_ in the documentation for + details. (`#1642 `_) + +- Fixtures are now instantiated based on their scopes, with higher-scoped + fixtures (such as ``session``) being instantiated first than lower-scoped + fixtures (such as ``function``). The relative order of fixtures of the same + scope is kept unchanged, based in their declaration order and their + dependencies. (`#2405 `_) + +- ``record_xml_property`` renamed to ``record_property`` and is now compatible + with xdist, markers and any reporter. ``record_xml_property`` name is now + deprecated. (`#2770 `_) + +- New ``--nf``, ``--new-first`` options: run new tests first followed by the + rest of the tests, in both cases tests are also sorted by the file modified + time, with more recent files coming first. (`#3034 + `_) + +- New ``--last-failed-no-failures`` command-line option that allows to specify + the behavior of the cache plugin's ```--last-failed`` feature when no tests + failed in the last run (or no cache was found): ``none`` or ``all`` (the + default). (`#3139 `_) + +- New ``--doctest-continue-on-failure`` command-line option to enable doctests + to show multiple failures for each snippet, instead of stopping at the first + failure. (`#3149 `_) + +- Captured log messages are added to the ```` tag in the generated + junit xml file if the ``junit_logging`` ini option is set to ``system-out``. + If the value of this ini option is ``system-err``, the logs are written to + ````. The default value for ``junit_logging`` is ``no``, meaning + captured logs are not written to the output file. (`#3156 + `_) + +- Allow the logging plugin to handle ``pytest_runtest_logstart`` and + ``pytest_runtest_logfinish`` hooks when live logs are enabled. (`#3189 + `_) + +- Passing `--log-cli-level` in the command-line now automatically activates + live logging. (`#3190 `_) + +- Add command line option ``--deselect`` to allow deselection of individual + tests at collection time. (`#3198 + `_) + +- Captured logs are printed before entering pdb. (`#3204 + `_) + +- Deselected item count is now shown before tests are run, e.g. ``collected X + items / Y deselected``. (`#3213 + `_) + +- The builtin module ``platform`` is now available for use in expressions in + ``pytest.mark``. (`#3236 + `_) + +- The *short test summary info* section now is displayed after tracebacks and + warnings in the terminal. (`#3255 + `_) + +- New ``--verbosity`` flag to set verbosity level explicitly. (`#3296 + `_) + +- ``pytest.approx`` now accepts comparing a numpy array with a scalar. (`#3312 + `_) + + +Bug Fixes +--------- + +- Suppress ``IOError`` when closing the temporary file used for capturing + streams in Python 2.7. (`#2370 + `_) + +- Fixed ``clear()`` method on ``caplog`` fixture which cleared ``records``, but + not the ``text`` property. (`#3297 + `_) + +- During test collection, when stdin is not allowed to be read, the + ``DontReadFromStdin`` object still allow itself to be iterable and resolved + to an iterator without crashing. (`#3314 + `_) + + +Improved Documentation +---------------------- + +- Added a `reference `_ page + to the docs. (`#1713 `_) + + +Trivial/Internal Changes +------------------------ + +- Change minimum requirement of ``attrs`` to ``17.4.0``. (`#3228 + `_) + +- Renamed example directories so all tests pass when ran from the base + directory. (`#3245 `_) + +- Internal ``mark.py`` module has been turned into a package. (`#3250 + `_) + +- ``pytest`` now depends on the `more-itertools + `_ package. (`#3265 + `_) + +- Added warning when ``[pytest]`` section is used in a ``.cfg`` file passed + with ``-c`` (`#3268 `_) + +- ``nodeids`` can now be passed explicitly to ``FSCollector`` and ``Node`` + constructors. (`#3291 `_) + +- Internal refactoring of ``FormattedExcinfo`` to use ``attrs`` facilities and + remove old support code for legacy Python versions. (`#3292 + `_) + +- Refactoring to unify how verbosity is handled internally. (`#3296 + `_) + +- Internal refactoring to better integrate with argparse. (`#3304 + `_) + +- Fix a python example when calling a fixture in doc/en/usage.rst (`#3308 + `_) + + +Pytest 3.4.2 (2018-03-04) +========================= + +Bug Fixes +--------- + +- Removed progress information when capture option is ``no``. (`#3203 + `_) + +- Refactor check of bindir from ``exists`` to ``isdir``. (`#3241 + `_) + +- Fix ``TypeError`` issue when using ``approx`` with a ``Decimal`` value. + (`#3247 `_) + +- Fix reference cycle generated when using the ``request`` fixture. (`#3249 + `_) + +- ``[tool:pytest]`` sections in ``*.cfg`` files passed by the ``-c`` option are + now properly recognized. (`#3260 + `_) + + +Improved Documentation +---------------------- + +- Add logging plugin to plugins list. (`#3209 + `_) + + +Trivial/Internal Changes +------------------------ + +- Fix minor typo in fixture.rst (`#3259 + `_) + + +Pytest 3.4.1 (2018-02-20) +========================= + +Bug Fixes +--------- + +- Move import of ``doctest.UnexpectedException`` to top-level to avoid possible + errors when using ``--pdb``. (`#1810 + `_) + +- Added printing of captured stdout/stderr before entering pdb, and improved a + test which was giving false negatives about output capturing. (`#3052 + `_) + +- Fix ordering of tests using parametrized fixtures which can lead to fixtures + being created more than necessary. (`#3161 + `_) + +- Fix bug where logging happening at hooks outside of "test run" hooks would + cause an internal error. (`#3184 + `_) + +- Detect arguments injected by ``unittest.mock.patch`` decorator correctly when + pypi ``mock.patch`` is installed and imported. (`#3206 + `_) + +- Errors shown when a ``pytest.raises()`` with ``match=`` fails are now cleaner + on what happened: When no exception was raised, the "matching '...'" part got + removed as it falsely implies that an exception was raised but it didn't + match. When a wrong exception was raised, it's now thrown (like + ``pytest.raised()`` without ``match=`` would) instead of complaining about + the unmatched text. (`#3222 + `_) + +- Fixed output capture handling in doctests on macOS. (`#985 + `_) + + +Improved Documentation +---------------------- + +- Add Sphinx parameter docs for ``match`` and ``message`` args to + ``pytest.raises``. (`#3202 + `_) + + +Trivial/Internal Changes +------------------------ + +- pytest has changed the publication procedure and is now being published to + PyPI directly from Travis. (`#3060 + `_) + +- Rename ``ParameterSet._for_parameterize()`` to ``_for_parametrize()`` in + order to comply with the naming convention. (`#3166 + `_) + +- Skip failing pdb/doctest test on mac. (`#985 + `_) + + +Pytest 3.4.0 (2018-01-30) +========================= + +Deprecations and Removals +------------------------- + +- All pytest classes now subclass ``object`` for better Python 2/3 compatibility. + This should not affect user code except in very rare edge cases. (`#2147 + `_) + + +Features +-------- + +- Introduce ``empty_parameter_set_mark`` ini option to select which mark to + apply when ``@pytest.mark.parametrize`` is given an empty set of parameters. + Valid options are ``skip`` (default) and ``xfail``. Note that it is planned + to change the default to ``xfail`` in future releases as this is considered + less error prone. (`#2527 + `_) + +- **Incompatible change**: after community feedback the `logging + `_ functionality has + undergone some changes. Please consult the `logging documentation + `_ + for details. (`#3013 `_) + +- Console output falls back to "classic" mode when capturing is disabled (``-s``), + otherwise the output gets garbled to the point of being useless. (`#3038 + `_) + +- New `pytest_runtest_logfinish + `_ + hook which is called when a test item has finished executing, analogous to + `pytest_runtest_logstart + `_. + (`#3101 `_) + +- Improve performance when collecting tests using many fixtures. (`#3107 + `_) + +- New ``caplog.get_records(when)`` method which provides access to the captured + records for the ``"setup"``, ``"call"`` and ``"teardown"`` + testing stages. (`#3117 `_) + +- New fixture ``record_xml_attribute`` that allows modifying and inserting + attributes on the ```` xml node in JUnit reports. (`#3130 + `_) + +- The default cache directory has been renamed from ``.cache`` to + ``.pytest_cache`` after community feedback that the name ``.cache`` did not + make it clear that it was used by pytest. (`#3138 + `_) + +- Colorize the levelname column in the live-log output. (`#3142 + `_) + + +Bug Fixes +--------- + +- Fix hanging pexpect test on MacOS by using flush() instead of wait(). + (`#2022 `_) + +- Fix restoring Python state after in-process pytest runs with the + ``pytester`` plugin; this may break tests using multiple inprocess + pytest runs if later ones depend on earlier ones leaking global interpreter + changes. (`#3016 `_) + +- Fix skipping plugin reporting hook when test aborted before plugin setup + hook. (`#3074 `_) + +- Fix progress percentage reported when tests fail during teardown. (`#3088 + `_) + +- **Incompatible change**: ``-o/--override`` option no longer eats all the + remaining options, which can lead to surprising behavior: for example, + ``pytest -o foo=1 /path/to/test.py`` would fail because ``/path/to/test.py`` + would be considered as part of the ``-o`` command-line argument. One + consequence of this is that now multiple configuration overrides need + multiple ``-o`` flags: ``pytest -o foo=1 -o bar=2``. (`#3103 + `_) + + +Improved Documentation +---------------------- + +- Document hooks (defined with ``historic=True``) which cannot be used with + ``hookwrapper=True``. (`#2423 + `_) + +- Clarify that warning capturing doesn't change the warning filter by default. + (`#2457 `_) + +- Clarify a possible confusion when using pytest_fixture_setup with fixture + functions that return None. (`#2698 + `_) + +- Fix the wording of a sentence on doctest flags used in pytest. (`#3076 + `_) + +- Prefer ``https://*.readthedocs.io`` over ``http://*.rtfd.org`` for links in + the documentation. (`#3092 + `_) + +- Improve readability (wording, grammar) of Getting Started guide (`#3131 + `_) + +- Added note that calling pytest.main multiple times from the same process is + not recommended because of import caching. (`#3143 + `_) + + +Trivial/Internal Changes +------------------------ + +- Show a simple and easy error when keyword expressions trigger a syntax error + (for example, ``"-k foo and import"`` will show an error that you can not use + the ``import`` keyword in expressions). (`#2953 + `_) + +- Change parametrized automatic test id generation to use the ``__name__`` + attribute of functions instead of the fallback argument name plus counter. + (`#2976 `_) + +- Replace py.std with stdlib imports. (`#3067 + `_) + +- Corrected 'you' to 'your' in logging docs. (`#3129 + `_) + + +Pytest 3.3.2 (2017-12-25) +========================= + +Bug Fixes +--------- + +- pytester: ignore files used to obtain current user metadata in the fd leak + detector. (`#2784 `_) + +- Fix **memory leak** where objects returned by fixtures were never destructed + by the garbage collector. (`#2981 + `_) + +- Fix conversion of pyargs to filename to not convert symlinks on Python 2. (`#2985 + `_) + +- ``PYTEST_DONT_REWRITE`` is now checked for plugins too rather than only for + test modules. (`#2995 `_) + + +Improved Documentation +---------------------- + +- Add clarifying note about behavior of multiple parametrized arguments (`#3001 + `_) + + +Trivial/Internal Changes +------------------------ + +- Code cleanup. (`#3015 `_, + `#3021 `_) + +- Clean up code by replacing imports and references of `_ast` to `ast`. (`#3018 + `_) + + +Pytest 3.3.1 (2017-12-05) +========================= + +Bug Fixes +--------- + +- Fix issue about ``-p no:`` having no effect. (`#2920 + `_) + +- Fix regression with warnings that contained non-strings in their arguments in + Python 2. (`#2956 `_) + +- Always escape null bytes when setting ``PYTEST_CURRENT_TEST``. (`#2957 + `_) + +- Fix ``ZeroDivisionError`` when using the ``testmon`` plugin when no tests + were actually collected. (`#2971 + `_) + +- Bring back ``TerminalReporter.writer`` as an alias to + ``TerminalReporter._tw``. This alias was removed by accident in the ``3.3.0`` + release. (`#2984 `_) + +- The ``pytest-capturelog`` plugin is now also blacklisted, avoiding errors when + running pytest with it still installed. (`#3004 + `_) + + +Improved Documentation +---------------------- + +- Fix broken link to plugin ``pytest-localserver``. (`#2963 + `_) + + +Trivial/Internal Changes +------------------------ + +- Update github "bugs" link in ``CONTRIBUTING.rst`` (`#2949 + `_) + + +Pytest 3.3.0 (2017-11-23) +========================= + +Deprecations and Removals +------------------------- + +- Pytest no longer supports Python **2.6** and **3.3**. Those Python versions + are EOL for some time now and incur maintenance and compatibility costs on + the pytest core team, and following up with the rest of the community we + decided that they will no longer be supported starting on this version. Users + which still require those versions should pin pytest to ``<3.3``. (`#2812 + `_) + +- Remove internal ``_preloadplugins()`` function. This removal is part of the + ``pytest_namespace()`` hook deprecation. (`#2636 + `_) + +- Internally change ``CallSpec2`` to have a list of marks instead of a broken + mapping of keywords. This removes the keywords attribute of the internal + ``CallSpec2`` class. (`#2672 + `_) + +- Remove ParameterSet.deprecated_arg_dict - its not a public api and the lack + of the underscore was a naming error. (`#2675 + `_) + +- Remove the internal multi-typed attribute ``Node._evalskip`` and replace it + with the boolean ``Node._skipped_by_mark``. (`#2767 + `_) + +- The ``params`` list passed to ``pytest.fixture`` is now for + all effects considered immutable and frozen at the moment of the ``pytest.fixture`` + call. Previously the list could be changed before the first invocation of the fixture + allowing for a form of dynamic parametrization (for example, updated from command-line options), + but this was an unwanted implementation detail which complicated the internals and prevented + some internal cleanup. See issue `#2959 `_ + for details and a recommended workaround. + +Features +-------- + +- ``pytest_fixture_post_finalizer`` hook can now receive a ``request`` + argument. (`#2124 `_) + +- Replace the old introspection code in compat.py that determines the available + arguments of fixtures with inspect.signature on Python 3 and + funcsigs.signature on Python 2. This should respect ``__signature__`` + declarations on functions. (`#2267 + `_) + +- Report tests with global ``pytestmark`` variable only once. (`#2549 + `_) + +- Now pytest displays the total progress percentage while running tests. The + previous output style can be set by configuring the ``console_output_style`` + setting to ``classic``. (`#2657 `_) + +- Match ``warns`` signature to ``raises`` by adding ``match`` keyword. (`#2708 + `_) + +- Pytest now captures and displays output from the standard ``logging`` module. + The user can control the logging level to be captured by specifying options + in ``pytest.ini``, the command line and also during individual tests using + markers. Also, a ``caplog`` fixture is available that enables users to test + the captured log during specific tests (similar to ``capsys`` for example). + For more information, please see the `logging docs + `_. This feature was + introduced by merging the popular `pytest-catchlog + `_ plugin, thanks to `Thomas Hisch + `_. Be advised that during the merging the + backward compatibility interface with the defunct ``pytest-capturelog`` has + been dropped. (`#2794 `_) + +- Add ``allow_module_level`` kwarg to ``pytest.skip()``, enabling to skip the + whole module. (`#2808 `_) + +- Allow setting ``file_or_dir``, ``-c``, and ``-o`` in PYTEST_ADDOPTS. (`#2824 + `_) + +- Return stdout/stderr capture results as a ``namedtuple``, so ``out`` and + ``err`` can be accessed by attribute. (`#2879 + `_) + +- Add ``capfdbinary``, a version of ``capfd`` which returns bytes from + ``readouterr()``. (`#2923 + `_) + +- Add ``capsysbinary`` a version of ``capsys`` which returns bytes from + ``readouterr()``. (`#2934 + `_) + +- Implement feature to skip ``setup.py`` files when run with + ``--doctest-modules``. (`#502 + `_) + + +Bug Fixes +--------- + +- Resume output capturing after ``capsys/capfd.disabled()`` context manager. + (`#1993 `_) + +- ``pytest_fixture_setup`` and ``pytest_fixture_post_finalizer`` hooks are now + called for all ``conftest.py`` files. (`#2124 + `_) + +- If an exception happens while loading a plugin, pytest no longer hides the + original traceback. In Python 2 it will show the original traceback with a new + message that explains in which plugin. In Python 3 it will show 2 canonized + exceptions, the original exception while loading the plugin in addition to an + exception that pytest throws about loading a plugin. (`#2491 + `_) + +- ``capsys`` and ``capfd`` can now be used by other fixtures. (`#2709 + `_) + +- Internal ``pytester`` plugin properly encodes ``bytes`` arguments to + ``utf-8``. (`#2738 `_) + +- ``testdir`` now uses use the same method used by ``tmpdir`` to create its + temporary directory. This changes the final structure of the ``testdir`` + directory slightly, but should not affect usage in normal scenarios and + avoids a number of potential problems. (`#2751 + `_) + +- Pytest no longer complains about warnings with unicode messages being + non-ascii compatible even for ascii-compatible messages. As a result of this, + warnings with unicode messages are converted first to an ascii representation + for safety. (`#2809 `_) + +- Change return value of pytest command when ``--maxfail`` is reached from + ``2`` (interrupted) to ``1`` (failed). (`#2845 + `_) + +- Fix issue in assertion rewriting which could lead it to rewrite modules which + should not be rewritten. (`#2939 + `_) + +- Handle marks without description in ``pytest.ini``. (`#2942 + `_) + + +Trivial/Internal Changes +------------------------ + +- pytest now depends on `attrs `_ for internal + structures to ease code maintainability. (`#2641 + `_) + +- Refactored internal Python 2/3 compatibility code to use ``six``. (`#2642 + `_) + +- Stop vendoring ``pluggy`` - we're missing out on its latest changes for not + much benefit (`#2719 `_) + +- Internal refactor: simplify ascii string escaping by using the + backslashreplace error handler in newer Python 3 versions. (`#2734 + `_) + +- Remove unnecessary mark evaluator in unittest plugin (`#2767 + `_) + +- Calls to ``Metafunc.addcall`` now emit a deprecation warning. This function + is scheduled to be removed in ``pytest-4.0``. (`#2876 + `_) + +- Internal move of the parameterset extraction to a more maintainable place. + (`#2877 `_) + +- Internal refactoring to simplify scope node lookup. (`#2910 + `_) + +- Configure ``pytest`` to prevent pip from installing pytest in unsupported + Python versions. (`#2922 + `_) + + Pytest 3.2.5 (2017-11-15) ========================= @@ -192,7 +1058,7 @@ Deprecations and Removals ------------------------- - ``pytest.approx`` no longer supports ``>``, ``>=``, ``<`` and ``<=`` - operators to avoid surprising/inconsistent behavior. See `the docs + operators to avoid surprising/inconsistent behavior. See `the approx docs `_ for more information. (`#2003 `_) @@ -516,7 +1382,7 @@ Changes * Testcase reports with a ``url`` attribute will now properly write this to junitxml. Thanks `@fushi`_ for the PR (`#1874`_). -* Remove common items from dict comparision output when verbosity=1. Also update +* Remove common items from dict comparison output when verbosity=1. Also update the truncation message to make it clearer that pytest truncates all assertion messages if verbosity < 2 (`#1512`_). Thanks `@mattduck`_ for the PR @@ -528,7 +1394,7 @@ Changes * fix `#2013`_: turn RecordedWarning into ``namedtuple``, to give it a comprehensible repr while preventing unwarranted modification. -* fix `#2208`_: ensure a iteration limit for _pytest.compat.get_real_func. +* fix `#2208`_: ensure an iteration limit for _pytest.compat.get_real_func. Thanks `@RonnyPfannschmidt`_ for the report and PR. * Hooks are now verified after collection is complete, rather than right after loading installed plugins. This @@ -632,7 +1498,7 @@ Bug Fixes Notably, importing the ``anydbm`` module is fixed. (`#2248`_). Thanks `@pfhayes`_ for the PR. -* junitxml: Fix problematic case where system-out tag occured twice per testcase +* junitxml: Fix problematic case where system-out tag occurred twice per testcase element in the XML report. Thanks `@kkoukiou`_ for the PR. * Fix regression, pytest now skips unittest correctly if run with ``--pdb`` @@ -2079,7 +2945,7 @@ time or change existing behaviors in order to make them less surprising/more use - fix issue655: work around different ways that cause python2/3 to leak sys.exc_info into fixtures/tests causing failures in 3rd party code -- fix issue615: assertion re-writing did not correctly escape % signs +- fix issue615: assertion rewriting did not correctly escape % signs when formatting boolean operations, which tripped over mixing booleans with modulo operators. Thanks to Tom Viner for the report, triaging and fix. @@ -2228,7 +3094,7 @@ time or change existing behaviors in order to make them less surprising/more use "::" node id specifications (copy pasted from "-v" output) - fix issue544 by only removing "@NUM" at the end of "::" separated parts - and if the part has an ".py" extension + and if the part has a ".py" extension - don't use py.std import helper, rather import things directly. Thanks Bruno Oliveira. @@ -2499,7 +3365,7 @@ time or change existing behaviors in order to make them less surprising/more use would not work correctly because pytest assumes @pytest.mark.some gets a function to be decorated already. We now at least detect if this - arg is an lambda and thus the example will work. Thanks Alex Gaynor + arg is a lambda and thus the example will work. Thanks Alex Gaynor for bringing it up. - xfail a test on pypy that checks wrong encoding/ascii (pypy does @@ -2812,7 +3678,7 @@ Bug fixes: rather use the post-2.0 parametrize features instead of yield, see: http://pytest.org/latest/example/parametrize.html - fix autouse-issue where autouse-fixtures would not be discovered - if defined in a a/conftest.py file and tests in a/tests/test_some.py + if defined in an a/conftest.py file and tests in a/tests/test_some.py - fix issue226 - LIFO ordering for fixture teardowns - fix issue224 - invocations with >256 char arguments now work - fix issue91 - add/discuss package/directory level setups in example @@ -3382,7 +4248,7 @@ Bug fixes: - make path.bestrelpath(path) return ".", note that when calling X.bestrelpath the assumption is that X is a directory. - make initial conftest discovery ignore "--" prefixed arguments -- fix resultlog plugin when used in an multicpu/multihost xdist situation +- fix resultlog plugin when used in a multicpu/multihost xdist situation (thanks Jakub Gustak) - perform distributed testing related reporting in the xdist-plugin rather than having dist-related code in the generic py.test diff --git a/third_party/python/pytest/CONTRIBUTING.rst b/third_party/python/pytest/CONTRIBUTING.rst index 68db81398ddb..c005c2fb2545 100644 --- a/third_party/python/pytest/CONTRIBUTING.rst +++ b/third_party/python/pytest/CONTRIBUTING.rst @@ -48,8 +48,7 @@ fix the bug itself. Fix bugs -------- -Look through the GitHub issues for bugs. Here is a filter you can use: -https://github.com/pytest-dev/pytest/labels/bug +Look through the `GitHub issues for bugs `_. :ref:`Talk ` to developers to find out how you can fix specific bugs. @@ -60,8 +59,7 @@ Don't forget to check the issue trackers of your favourite plugins, too! Implement features ------------------ -Look through the GitHub issues for enhancements. Here is a filter you can use: -https://github.com/pytest-dev/pytest/labels/enhancement +Look through the `GitHub issues for enhancements `_. :ref:`Talk ` to developers to find out how you can implement specific features. @@ -141,7 +139,7 @@ Here's a rundown of how a repository transfer usually proceeds * ``joedoe`` transfers repository ownership to ``pytest-dev`` administrator ``calvin``. * ``calvin`` creates ``pytest-xyz-admin`` and ``pytest-xyz-developers`` teams, inviting ``joedoe`` to both as **maintainer**. * ``calvin`` transfers repository to ``pytest-dev`` and configures team access: - + - ``pytest-xyz-admin`` **admin** access; - ``pytest-xyz-developers`` **write** access; @@ -164,10 +162,11 @@ Preparing Pull Requests Short version ~~~~~~~~~~~~~ -#. Fork the repository; -#. Target ``master`` for bugfixes and doc changes; +#. Fork the repository. +#. Enable and install `pre-commit `_ to ensure style-guides and code checks are followed. +#. Target ``master`` for bugfixes and doc changes. #. Target ``features`` for new features or functionality changes. -#. Follow **PEP-8**. There's a ``tox`` command to help fixing it: ``tox -e fix-lint``. +#. Follow **PEP-8** for naming and `black `_ for formatting. #. Tests are run using ``tox``:: tox -e linting,py27,py36 @@ -178,7 +177,7 @@ Short version and one of ``bugfix``, ``removal``, ``feature``, ``vendor``, ``doc`` or ``trivial`` for the issue type. #. Unless your change is a trivial or a documentation fix (e.g., a typo or reword of a small section) please - add yourself to the ``AUTHORS`` file, in alphabetical order; + add yourself to the ``AUTHORS`` file, in alphabetical order. Long version @@ -204,20 +203,30 @@ Here is a simple overview, with pytest-specific bits: $ git clone git@github.com:YOUR_GITHUB_USERNAME/pytest.git $ cd pytest # now, to fix a bug create your own branch off "master": - + $ git checkout -b your-bugfix-branch-name master # or to instead add a feature create your own branch off "features": - + $ git checkout -b your-feature-branch-name features - Given we have "major.minor.micro" version numbers, bugfixes will usually - be released in micro releases whereas features will be released in + Given we have "major.minor.micro" version numbers, bugfixes will usually + be released in micro releases whereas features will be released in minor releases and incompatible changes in major releases. If you need some help with Git, follow this quick start guide: https://git.wiki.kernel.org/index.php/QuickStart +#. Install `pre-commit `_ and its hook on the pytest repo:: + + $ pip install --user pre-commit + $ pre-commit install + + Afterwards ``pre-commit`` will run whenever you commit. + + https://pre-commit.com/ is a framework for managing and maintaining multi-language pre-commit hooks + to ensure code-style and code formatting is consistent. + #. Install tox Tox is used to run all the tests and will automatically setup virtualenvs @@ -236,15 +245,7 @@ Here is a simple overview, with pytest-specific bits: This command will run tests via the "tox" tool against Python 2.7 and 3.6 and also perform "lint" coding-style checks. -#. You can now edit your local working copy. Please follow PEP-8. - - You can now make the changes you want and run the tests again as necessary. - - If you have too much linting errors, try running:: - - $ tox -e fix-lint - - To fix pep8 related errors. +#. You can now edit your local working copy and run the tests again as necessary. Please follow PEP-8 for naming. You can pass different options to ``tox``. For example, to run tests on Python 2.7 and pass options to pytest (e.g. enter pdb on failure) to pytest you can do:: @@ -255,6 +256,9 @@ Here is a simple overview, with pytest-specific bits: $ tox -e py36 -- testing/test_config.py + + When committing, ``pre-commit`` will re-format the files if necessary. + #. Commit and push once your tests pass and you are happy with your change(s):: $ git commit -a -m "" @@ -276,3 +280,15 @@ Here is a simple overview, with pytest-specific bits: base: features # if it's a feature +Joining the Development Team +---------------------------- + +Anyone who has successfully seen through a pull request which did not +require any extra work from the development team to merge will +themselves gain commit access if they so wish (if we forget to ask please send a friendly +reminder). This does not mean your workflow to contribute changes, +everyone goes through the same pull-request-and-review process and +no-one merges their own pull requests unless already approved. It does however mean you can +participate in the development process more fully since you can merge +pull requests from other contributors yourself after having reviewed +them. diff --git a/third_party/python/pytest/HOWTORELEASE.rst b/third_party/python/pytest/HOWTORELEASE.rst index 48a3461d4bc9..97bddf7202b1 100644 --- a/third_party/python/pytest/HOWTORELEASE.rst +++ b/third_party/python/pytest/HOWTORELEASE.rst @@ -12,7 +12,7 @@ taking a lot of time to make a new one. #. Install development dependencies in a virtual environment with:: - pip3 install -r tasks/requirements.txt + pip3 install -U -r tasks/requirements.txt #. Create a branch ``release-X.Y.Z`` with the version for the release. @@ -22,44 +22,28 @@ taking a lot of time to make a new one. Ensure your are in a clean work tree. -#. Generate docs, changelog, announcements and upload a package to - your ``devpi`` staging server:: +#. Generate docs, changelog, announcements and a **local** tag:: - invoke generate.pre-release --password - - If ``--password`` is not given, it is assumed the user is already logged in ``devpi``. - If you don't have an account, please ask for one. + invoke generate.pre-release #. Open a PR for this branch targeting ``master``. -#. Test the package +#. After all tests pass and the PR has been approved, publish to PyPI by pushing the tag:: - * **Manual method** + git push git@github.com:pytest-dev/pytest.git - Run from multiple machines:: + Wait for the deploy to complete, then make sure it is `available on PyPI `_. - devpi use https://devpi.net/USER/dev - devpi test pytest==VERSION +#. Send an email announcement with the contents from:: - Check that tests pass for relevant combinations with:: + doc/en/announce/release-.rst - devpi list pytest + To the following mailing lists: - * **CI servers** + * pytest-dev@python.org (all releases) + * python-announce-list@python.org (all releases) + * testing-in-python@lists.idyll.org (only major/minor releases) - Configure a repository as per-instructions on - devpi-cloud-test_ to test the package on Travis_ and AppVeyor_. - All test environments should pass. - -#. Publish to PyPI:: - - invoke generate.publish-release - - where PYPI_NAME is the name of pypi.python.org as configured in your ``~/.pypirc`` - file `for devpi `_. + And announce it on `Twitter `_ with the ``#pytest`` hashtag. #. After a minor/major release, merge ``release-X.Y.Z`` into ``master`` and push (or open a PR). - -.. _devpi-cloud-test: https://github.com/obestwalter/devpi-cloud-test -.. _AppVeyor: https://www.appveyor.com/ -.. _Travis: https://travis-ci.org diff --git a/third_party/python/pytest/PKG-INFO b/third_party/python/pytest/PKG-INFO index 632118174c7b..1c50ee34dfa3 100644 --- a/third_party/python/pytest/PKG-INFO +++ b/third_party/python/pytest/PKG-INFO @@ -1,11 +1,12 @@ -Metadata-Version: 1.1 +Metadata-Version: 1.2 Name: pytest -Version: 3.2.5 +Version: 3.6.2 Summary: pytest: simple powerful testing with Python Home-page: http://pytest.org Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others -Author-email: UNKNOWN License: MIT license +Project-URL: Source, https://github.com/pytest-dev/pytest +Project-URL: Tracker, https://github.com/pytest-dev/pytest/issues Description: .. image:: http://docs.pytest.org/en/latest/_static/pytest1.png :target: http://docs.pytest.org :align: center @@ -14,13 +15,13 @@ Description: .. image:: http://docs.pytest.org/en/latest/_static/pytest1.png ------ .. image:: https://img.shields.io/pypi/v/pytest.svg - :target: https://pypi.python.org/pypi/pytest + :target: https://pypi.org/project/pytest/ - .. image:: https://anaconda.org/conda-forge/pytest/badges/version.svg + .. image:: https://img.shields.io/conda/vn/conda-forge/pytest.svg :target: https://anaconda.org/conda-forge/pytest .. image:: https://img.shields.io/pypi/pyversions/pytest.svg - :target: https://pypi.python.org/pypi/pytest + :target: https://pypi.org/project/pytest/ .. image:: https://img.shields.io/coveralls/pytest-dev/pytest/master.svg :target: https://coveralls.io/r/pytest-dev/pytest @@ -31,6 +32,12 @@ Description: .. image:: http://docs.pytest.org/en/latest/_static/pytest1.png .. image:: https://ci.appveyor.com/api/projects/status/mrgbjaua7t33pg6b?svg=true :target: https://ci.appveyor.com/project/pytestbot/pytest + .. image:: https://img.shields.io/badge/code%20style-black-000000.svg + :target: https://github.com/ambv/black + + .. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg + :target: https://www.codetriage.com/pytest-dev/pytest + The ``pytest`` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. @@ -42,6 +49,7 @@ Description: .. image:: http://docs.pytest.org/en/latest/_static/pytest1.png def inc(x): return x + 1 + def test_answer(): assert inc(3) == 5 @@ -84,7 +92,7 @@ Description: .. image:: http://docs.pytest.org/en/latest/_static/pytest1.png - Can run `unittest `_ (or trial), `nose `_ test suites out of the box; - - Python2.6+, Python3.3+, PyPy-2.3, Jython-2.5 (untested); + - Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; @@ -132,10 +140,10 @@ Classifier: Topic :: Software Development :: Testing Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: Utilities Classifier: Programming Language :: Python :: 2 -Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 -Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 +Classifier: Programming Language :: Python :: 3.7 +Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.* diff --git a/third_party/python/pytest/README.rst b/third_party/python/pytest/README.rst index 15ad6ea18246..564ffff6c91e 100644 --- a/third_party/python/pytest/README.rst +++ b/third_party/python/pytest/README.rst @@ -6,13 +6,13 @@ ------ .. image:: https://img.shields.io/pypi/v/pytest.svg - :target: https://pypi.python.org/pypi/pytest + :target: https://pypi.org/project/pytest/ -.. image:: https://anaconda.org/conda-forge/pytest/badges/version.svg +.. image:: https://img.shields.io/conda/vn/conda-forge/pytest.svg :target: https://anaconda.org/conda-forge/pytest .. image:: https://img.shields.io/pypi/pyversions/pytest.svg - :target: https://pypi.python.org/pypi/pytest + :target: https://pypi.org/project/pytest/ .. image:: https://img.shields.io/coveralls/pytest-dev/pytest/master.svg :target: https://coveralls.io/r/pytest-dev/pytest @@ -23,6 +23,12 @@ .. image:: https://ci.appveyor.com/api/projects/status/mrgbjaua7t33pg6b?svg=true :target: https://ci.appveyor.com/project/pytestbot/pytest +.. image:: https://img.shields.io/badge/code%20style-black-000000.svg + :target: https://github.com/ambv/black + +.. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg + :target: https://www.codetriage.com/pytest-dev/pytest + The ``pytest`` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. @@ -34,6 +40,7 @@ An example of a simple test: def inc(x): return x + 1 + def test_answer(): assert inc(3) == 5 @@ -76,7 +83,7 @@ Features - Can run `unittest `_ (or trial), `nose `_ test suites out of the box; -- Python2.6+, Python3.3+, PyPy-2.3, Jython-2.5 (untested); +- Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; diff --git a/third_party/python/pytest/_pytest/_pluggy.py b/third_party/python/pytest/_pytest/_pluggy.py deleted file mode 100644 index 6cc1d3d54a65..000000000000 --- a/third_party/python/pytest/_pytest/_pluggy.py +++ /dev/null @@ -1,11 +0,0 @@ -""" -imports symbols from vendored "pluggy" if available, otherwise -falls back to importing "pluggy" from the default namespace. -""" -from __future__ import absolute_import, division, print_function -try: - from _pytest.vendored_packages.pluggy import * # noqa - from _pytest.vendored_packages.pluggy import __version__ # noqa -except ImportError: - from pluggy import * # noqa - from pluggy import __version__ # noqa diff --git a/third_party/python/pytest/_pytest/config.py b/third_party/python/pytest/_pytest/config.py deleted file mode 100644 index 19835d2c39f9..000000000000 --- a/third_party/python/pytest/_pytest/config.py +++ /dev/null @@ -1,1414 +0,0 @@ -""" command line options, ini-file and conftest.py processing. """ -from __future__ import absolute_import, division, print_function -import argparse -import shlex -import traceback -import types -import warnings - -import py -# DON't import pytest here because it causes import cycle troubles -import sys -import os -import _pytest._code -import _pytest.hookspec # the extension point definitions -import _pytest.assertion -from _pytest._pluggy import PluginManager, HookimplMarker, HookspecMarker -from _pytest.compat import safe_str - -hookimpl = HookimplMarker("pytest") -hookspec = HookspecMarker("pytest") - -# pytest startup -# - - -class ConftestImportFailure(Exception): - def __init__(self, path, excinfo): - Exception.__init__(self, path, excinfo) - self.path = path - self.excinfo = excinfo - - def __str__(self): - etype, evalue, etb = self.excinfo - formatted = traceback.format_tb(etb) - # The level of the tracebacks we want to print is hand crafted :( - return repr(evalue) + '\n' + ''.join(formatted[2:]) - - -def main(args=None, plugins=None): - """ return exit code, after performing an in-process test run. - - :arg args: list of command line arguments. - - :arg plugins: list of plugin objects to be auto-registered during - initialization. - """ - try: - try: - config = _prepareconfig(args, plugins) - except ConftestImportFailure as e: - tw = py.io.TerminalWriter(sys.stderr) - for line in traceback.format_exception(*e.excinfo): - tw.line(line.rstrip(), red=True) - tw.line("ERROR: could not load %s\n" % (e.path), red=True) - return 4 - else: - try: - return config.hook.pytest_cmdline_main(config=config) - finally: - config._ensure_unconfigure() - except UsageError as e: - for msg in e.args: - sys.stderr.write("ERROR: %s\n" % (msg,)) - return 4 - - -class cmdline: # compatibility namespace - main = staticmethod(main) - - -class UsageError(Exception): - """ error in pytest usage or invocation""" - - -class PrintHelp(Exception): - """Raised when pytest should print it's help to skip the rest of the - argument parsing and validation.""" - pass - - -def filename_arg(path, optname): - """ Argparse type validator for filename arguments. - - :path: path of filename - :optname: name of the option - """ - if os.path.isdir(path): - raise UsageError("{0} must be a filename, given: {1}".format(optname, path)) - return path - - -def directory_arg(path, optname): - """Argparse type validator for directory arguments. - - :path: path of directory - :optname: name of the option - """ - if not os.path.isdir(path): - raise UsageError("{0} must be a directory, given: {1}".format(optname, path)) - return path - - -_preinit = [] - -default_plugins = ( - "mark main terminal runner python fixtures debugging unittest capture skipping " - "tmpdir monkeypatch recwarn pastebin helpconfig nose assertion " - "junitxml resultlog doctest cacheprovider freeze_support " - "setuponly setupplan warnings").split() - - -builtin_plugins = set(default_plugins) -builtin_plugins.add("pytester") - - -def _preloadplugins(): - assert not _preinit - _preinit.append(get_config()) - - -def get_config(): - if _preinit: - return _preinit.pop(0) - # subsequent calls to main will create a fresh instance - pluginmanager = PytestPluginManager() - config = Config(pluginmanager) - for spec in default_plugins: - pluginmanager.import_plugin(spec) - return config - - -def get_plugin_manager(): - """ - Obtain a new instance of the - :py:class:`_pytest.config.PytestPluginManager`, with default plugins - already loaded. - - This function can be used by integration with other tools, like hooking - into pytest to run tests into an IDE. - """ - return get_config().pluginmanager - - -def _prepareconfig(args=None, plugins=None): - warning = None - if args is None: - args = sys.argv[1:] - elif isinstance(args, py.path.local): - args = [str(args)] - elif not isinstance(args, (tuple, list)): - if not isinstance(args, str): - raise ValueError("not a string or argument list: %r" % (args,)) - args = shlex.split(args, posix=sys.platform != "win32") - from _pytest import deprecated - warning = deprecated.MAIN_STR_ARGS - config = get_config() - pluginmanager = config.pluginmanager - try: - if plugins: - for plugin in plugins: - if isinstance(plugin, py.builtin._basestring): - pluginmanager.consider_pluginarg(plugin) - else: - pluginmanager.register(plugin) - if warning: - config.warn('C1', warning) - return pluginmanager.hook.pytest_cmdline_parse( - pluginmanager=pluginmanager, args=args) - except BaseException: - config._ensure_unconfigure() - raise - - -class PytestPluginManager(PluginManager): - """ - Overwrites :py:class:`pluggy.PluginManager <_pytest.vendored_packages.pluggy.PluginManager>` to add pytest-specific - functionality: - - * loading plugins from the command line, ``PYTEST_PLUGIN`` env variable and - ``pytest_plugins`` global variables found in plugins being loaded; - * ``conftest.py`` loading during start-up; - """ - - def __init__(self): - super(PytestPluginManager, self).__init__("pytest", implprefix="pytest_") - self._conftest_plugins = set() - - # state related to local conftest plugins - self._path2confmods = {} - self._conftestpath2mod = {} - self._confcutdir = None - self._noconftest = False - self._duplicatepaths = set() - - self.add_hookspecs(_pytest.hookspec) - self.register(self) - if os.environ.get('PYTEST_DEBUG'): - err = sys.stderr - encoding = getattr(err, 'encoding', 'utf8') - try: - err = py.io.dupfile(err, encoding=encoding) - except Exception: - pass - self.trace.root.setwriter(err.write) - self.enable_tracing() - - # Config._consider_importhook will set a real object if required. - self.rewrite_hook = _pytest.assertion.DummyRewriteHook() - - def addhooks(self, module_or_class): - """ - .. deprecated:: 2.8 - - Use :py:meth:`pluggy.PluginManager.add_hookspecs <_pytest.vendored_packages.pluggy.PluginManager.add_hookspecs>` - instead. - """ - warning = dict(code="I2", - fslocation=_pytest._code.getfslineno(sys._getframe(1)), - nodeid=None, - message="use pluginmanager.add_hookspecs instead of " - "deprecated addhooks() method.") - self._warn(warning) - return self.add_hookspecs(module_or_class) - - def parse_hookimpl_opts(self, plugin, name): - # pytest hooks are always prefixed with pytest_ - # so we avoid accessing possibly non-readable attributes - # (see issue #1073) - if not name.startswith("pytest_"): - return - # ignore some historic special names which can not be hooks anyway - if name == "pytest_plugins" or name.startswith("pytest_funcarg__"): - return - - method = getattr(plugin, name) - opts = super(PytestPluginManager, self).parse_hookimpl_opts(plugin, name) - if opts is not None: - for name in ("tryfirst", "trylast", "optionalhook", "hookwrapper"): - opts.setdefault(name, hasattr(method, name)) - return opts - - def parse_hookspec_opts(self, module_or_class, name): - opts = super(PytestPluginManager, self).parse_hookspec_opts( - module_or_class, name) - if opts is None: - method = getattr(module_or_class, name) - if name.startswith("pytest_"): - opts = {"firstresult": hasattr(method, "firstresult"), - "historic": hasattr(method, "historic")} - return opts - - def _verify_hook(self, hook, hookmethod): - super(PytestPluginManager, self)._verify_hook(hook, hookmethod) - if "__multicall__" in hookmethod.argnames: - fslineno = _pytest._code.getfslineno(hookmethod.function) - warning = dict(code="I1", - fslocation=fslineno, - nodeid=None, - message="%r hook uses deprecated __multicall__ " - "argument" % (hook.name)) - self._warn(warning) - - def register(self, plugin, name=None): - ret = super(PytestPluginManager, self).register(plugin, name) - if ret: - self.hook.pytest_plugin_registered.call_historic( - kwargs=dict(plugin=plugin, manager=self)) - - if isinstance(plugin, types.ModuleType): - self.consider_module(plugin) - return ret - - def getplugin(self, name): - # support deprecated naming because plugins (xdist e.g.) use it - return self.get_plugin(name) - - def hasplugin(self, name): - """Return True if the plugin with the given name is registered.""" - return bool(self.get_plugin(name)) - - def pytest_configure(self, config): - # XXX now that the pluginmanager exposes hookimpl(tryfirst...) - # we should remove tryfirst/trylast as markers - config.addinivalue_line("markers", - "tryfirst: mark a hook implementation function such that the " - "plugin machinery will try to call it first/as early as possible.") - config.addinivalue_line("markers", - "trylast: mark a hook implementation function such that the " - "plugin machinery will try to call it last/as late as possible.") - - def _warn(self, message): - kwargs = message if isinstance(message, dict) else { - 'code': 'I1', - 'message': message, - 'fslocation': None, - 'nodeid': None, - } - self.hook.pytest_logwarning.call_historic(kwargs=kwargs) - - # - # internal API for local conftest plugin handling - # - def _set_initial_conftests(self, namespace): - """ load initial conftest files given a preparsed "namespace". - As conftest files may add their own command line options - which have arguments ('--my-opt somepath') we might get some - false positives. All builtin and 3rd party plugins will have - been loaded, however, so common options will not confuse our logic - here. - """ - current = py.path.local() - self._confcutdir = current.join(namespace.confcutdir, abs=True) \ - if namespace.confcutdir else None - self._noconftest = namespace.noconftest - testpaths = namespace.file_or_dir - foundanchor = False - for path in testpaths: - path = str(path) - # remove node-id syntax - i = path.find("::") - if i != -1: - path = path[:i] - anchor = current.join(path, abs=1) - if exists(anchor): # we found some file object - self._try_load_conftest(anchor) - foundanchor = True - if not foundanchor: - self._try_load_conftest(current) - - def _try_load_conftest(self, anchor): - self._getconftestmodules(anchor) - # let's also consider test* subdirs - if anchor.check(dir=1): - for x in anchor.listdir("test*"): - if x.check(dir=1): - self._getconftestmodules(x) - - def _getconftestmodules(self, path): - if self._noconftest: - return [] - try: - return self._path2confmods[path] - except KeyError: - if path.isfile(): - clist = self._getconftestmodules(path.dirpath()) - else: - # XXX these days we may rather want to use config.rootdir - # and allow users to opt into looking into the rootdir parent - # directories instead of requiring to specify confcutdir - clist = [] - for parent in path.parts(): - if self._confcutdir and self._confcutdir.relto(parent): - continue - conftestpath = parent.join("conftest.py") - if conftestpath.isfile(): - mod = self._importconftest(conftestpath) - clist.append(mod) - - self._path2confmods[path] = clist - return clist - - def _rget_with_confmod(self, name, path): - modules = self._getconftestmodules(path) - for mod in reversed(modules): - try: - return mod, getattr(mod, name) - except AttributeError: - continue - raise KeyError(name) - - def _importconftest(self, conftestpath): - try: - return self._conftestpath2mod[conftestpath] - except KeyError: - pkgpath = conftestpath.pypkgpath() - if pkgpath is None: - _ensure_removed_sysmodule(conftestpath.purebasename) - try: - mod = conftestpath.pyimport() - except Exception: - raise ConftestImportFailure(conftestpath, sys.exc_info()) - - self._conftest_plugins.add(mod) - self._conftestpath2mod[conftestpath] = mod - dirpath = conftestpath.dirpath() - if dirpath in self._path2confmods: - for path, mods in self._path2confmods.items(): - if path and path.relto(dirpath) or path == dirpath: - assert mod not in mods - mods.append(mod) - self.trace("loaded conftestmodule %r" % (mod)) - self.consider_conftest(mod) - return mod - - # - # API for bootstrapping plugin loading - # - # - - def consider_preparse(self, args): - for opt1, opt2 in zip(args, args[1:]): - if opt1 == "-p": - self.consider_pluginarg(opt2) - - def consider_pluginarg(self, arg): - if arg.startswith("no:"): - name = arg[3:] - self.set_blocked(name) - if not name.startswith("pytest_"): - self.set_blocked("pytest_" + name) - else: - self.import_plugin(arg) - - def consider_conftest(self, conftestmodule): - self.register(conftestmodule, name=conftestmodule.__file__) - - def consider_env(self): - self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS")) - - def consider_module(self, mod): - self._import_plugin_specs(getattr(mod, 'pytest_plugins', [])) - - def _import_plugin_specs(self, spec): - plugins = _get_plugin_specs_as_list(spec) - for import_spec in plugins: - self.import_plugin(import_spec) - - def import_plugin(self, modname): - # most often modname refers to builtin modules, e.g. "pytester", - # "terminal" or "capture". Those plugins are registered under their - # basename for historic purposes but must be imported with the - # _pytest prefix. - assert isinstance(modname, (py.builtin.text, str)), "module name as text required, got %r" % modname - modname = str(modname) - if self.get_plugin(modname) is not None: - return - if modname in builtin_plugins: - importspec = "_pytest." + modname - else: - importspec = modname - self.rewrite_hook.mark_rewrite(importspec) - try: - __import__(importspec) - except ImportError as e: - new_exc = ImportError('Error importing plugin "%s": %s' % (modname, safe_str(e.args[0]))) - # copy over name and path attributes - for attr in ('name', 'path'): - if hasattr(e, attr): - setattr(new_exc, attr, getattr(e, attr)) - raise new_exc - except Exception as e: - import pytest - if not hasattr(pytest, 'skip') or not isinstance(e, pytest.skip.Exception): - raise - self._warn("skipped plugin %r: %s" % ((modname, e.msg))) - else: - mod = sys.modules[importspec] - self.register(mod, modname) - - -def _get_plugin_specs_as_list(specs): - """ - Parses a list of "plugin specs" and returns a list of plugin names. - - Plugin specs can be given as a list of strings separated by "," or already as a list/tuple in - which case it is returned as a list. Specs can also be `None` in which case an - empty list is returned. - """ - if specs is not None: - if isinstance(specs, str): - specs = specs.split(',') if specs else [] - if not isinstance(specs, (list, tuple)): - raise UsageError("Plugin specs must be a ','-separated string or a " - "list/tuple of strings for plugin names. Given: %r" % specs) - return list(specs) - return [] - - -class Parser: - """ Parser for command line arguments and ini-file values. - - :ivar extra_info: dict of generic param -> value to display in case - there's an error processing the command line arguments. - """ - - def __init__(self, usage=None, processopt=None): - self._anonymous = OptionGroup("custom options", parser=self) - self._groups = [] - self._processopt = processopt - self._usage = usage - self._inidict = {} - self._ininames = [] - self.extra_info = {} - - def processoption(self, option): - if self._processopt: - if option.dest: - self._processopt(option) - - def getgroup(self, name, description="", after=None): - """ get (or create) a named option Group. - - :name: name of the option group. - :description: long description for --help output. - :after: name of other group, used for ordering --help output. - - The returned group object has an ``addoption`` method with the same - signature as :py:func:`parser.addoption - <_pytest.config.Parser.addoption>` but will be shown in the - respective group in the output of ``pytest. --help``. - """ - for group in self._groups: - if group.name == name: - return group - group = OptionGroup(name, description, parser=self) - i = 0 - for i, grp in enumerate(self._groups): - if grp.name == after: - break - self._groups.insert(i + 1, group) - return group - - def addoption(self, *opts, **attrs): - """ register a command line option. - - :opts: option names, can be short or long options. - :attrs: same attributes which the ``add_option()`` function of the - `argparse library - `_ - accepts. - - After command line parsing options are available on the pytest config - object via ``config.option.NAME`` where ``NAME`` is usually set - by passing a ``dest`` attribute, for example - ``addoption("--long", dest="NAME", ...)``. - """ - self._anonymous.addoption(*opts, **attrs) - - def parse(self, args, namespace=None): - from _pytest._argcomplete import try_argcomplete - self.optparser = self._getparser() - try_argcomplete(self.optparser) - return self.optparser.parse_args([str(x) for x in args], namespace=namespace) - - def _getparser(self): - from _pytest._argcomplete import filescompleter - optparser = MyOptionParser(self, self.extra_info) - groups = self._groups + [self._anonymous] - for group in groups: - if group.options: - desc = group.description or group.name - arggroup = optparser.add_argument_group(desc) - for option in group.options: - n = option.names() - a = option.attrs() - arggroup.add_argument(*n, **a) - # bash like autocompletion for dirs (appending '/') - optparser.add_argument(FILE_OR_DIR, nargs='*').completer = filescompleter - return optparser - - def parse_setoption(self, args, option, namespace=None): - parsedoption = self.parse(args, namespace=namespace) - for name, value in parsedoption.__dict__.items(): - setattr(option, name, value) - return getattr(parsedoption, FILE_OR_DIR) - - def parse_known_args(self, args, namespace=None): - """parses and returns a namespace object with known arguments at this - point. - """ - return self.parse_known_and_unknown_args(args, namespace=namespace)[0] - - def parse_known_and_unknown_args(self, args, namespace=None): - """parses and returns a namespace object with known arguments, and - the remaining arguments unknown at this point. - """ - optparser = self._getparser() - args = [str(x) for x in args] - return optparser.parse_known_args(args, namespace=namespace) - - def addini(self, name, help, type=None, default=None): - """ register an ini-file option. - - :name: name of the ini-variable - :type: type of the variable, can be ``pathlist``, ``args``, ``linelist`` - or ``bool``. - :default: default value if no ini-file option exists but is queried. - - The value of ini-variables can be retrieved via a call to - :py:func:`config.getini(name) <_pytest.config.Config.getini>`. - """ - assert type in (None, "pathlist", "args", "linelist", "bool") - self._inidict[name] = (help, type, default) - self._ininames.append(name) - - -class ArgumentError(Exception): - """ - Raised if an Argument instance is created with invalid or - inconsistent arguments. - """ - - def __init__(self, msg, option): - self.msg = msg - self.option_id = str(option) - - def __str__(self): - if self.option_id: - return "option %s: %s" % (self.option_id, self.msg) - else: - return self.msg - - -class Argument: - """class that mimics the necessary behaviour of optparse.Option - - its currently a least effort implementation - and ignoring choices and integer prefixes - https://docs.python.org/3/library/optparse.html#optparse-standard-option-types - """ - _typ_map = { - 'int': int, - 'string': str, - 'float': float, - 'complex': complex, - } - - def __init__(self, *names, **attrs): - """store parms in private vars for use in add_argument""" - self._attrs = attrs - self._short_opts = [] - self._long_opts = [] - self.dest = attrs.get('dest') - if '%default' in (attrs.get('help') or ''): - warnings.warn( - 'pytest now uses argparse. "%default" should be' - ' changed to "%(default)s" ', - DeprecationWarning, - stacklevel=3) - try: - typ = attrs['type'] - except KeyError: - pass - else: - # this might raise a keyerror as well, don't want to catch that - if isinstance(typ, py.builtin._basestring): - if typ == 'choice': - warnings.warn( - 'type argument to addoption() is a string %r.' - ' For parsearg this is optional and when supplied' - ' should be a type.' - ' (options: %s)' % (typ, names), - DeprecationWarning, - stacklevel=3) - # argparse expects a type here take it from - # the type of the first element - attrs['type'] = type(attrs['choices'][0]) - else: - warnings.warn( - 'type argument to addoption() is a string %r.' - ' For parsearg this should be a type.' - ' (options: %s)' % (typ, names), - DeprecationWarning, - stacklevel=3) - attrs['type'] = Argument._typ_map[typ] - # used in test_parseopt -> test_parse_defaultgetter - self.type = attrs['type'] - else: - self.type = typ - try: - # attribute existence is tested in Config._processopt - self.default = attrs['default'] - except KeyError: - pass - self._set_opt_strings(names) - if not self.dest: - if self._long_opts: - self.dest = self._long_opts[0][2:].replace('-', '_') - else: - try: - self.dest = self._short_opts[0][1:] - except IndexError: - raise ArgumentError( - 'need a long or short option', self) - - def names(self): - return self._short_opts + self._long_opts - - def attrs(self): - # update any attributes set by processopt - attrs = 'default dest help'.split() - if self.dest: - attrs.append(self.dest) - for attr in attrs: - try: - self._attrs[attr] = getattr(self, attr) - except AttributeError: - pass - if self._attrs.get('help'): - a = self._attrs['help'] - a = a.replace('%default', '%(default)s') - # a = a.replace('%prog', '%(prog)s') - self._attrs['help'] = a - return self._attrs - - def _set_opt_strings(self, opts): - """directly from optparse - - might not be necessary as this is passed to argparse later on""" - for opt in opts: - if len(opt) < 2: - raise ArgumentError( - "invalid option string %r: " - "must be at least two characters long" % opt, self) - elif len(opt) == 2: - if not (opt[0] == "-" and opt[1] != "-"): - raise ArgumentError( - "invalid short option string %r: " - "must be of the form -x, (x any non-dash char)" % opt, - self) - self._short_opts.append(opt) - else: - if not (opt[0:2] == "--" and opt[2] != "-"): - raise ArgumentError( - "invalid long option string %r: " - "must start with --, followed by non-dash" % opt, - self) - self._long_opts.append(opt) - - def __repr__(self): - args = [] - if self._short_opts: - args += ['_short_opts: ' + repr(self._short_opts)] - if self._long_opts: - args += ['_long_opts: ' + repr(self._long_opts)] - args += ['dest: ' + repr(self.dest)] - if hasattr(self, 'type'): - args += ['type: ' + repr(self.type)] - if hasattr(self, 'default'): - args += ['default: ' + repr(self.default)] - return 'Argument({0})'.format(', '.join(args)) - - -class OptionGroup: - def __init__(self, name, description="", parser=None): - self.name = name - self.description = description - self.options = [] - self.parser = parser - - def addoption(self, *optnames, **attrs): - """ add an option to this group. - - if a shortened version of a long option is specified it will - be suppressed in the help. addoption('--twowords', '--two-words') - results in help showing '--two-words' only, but --twowords gets - accepted **and** the automatic destination is in args.twowords - """ - conflict = set(optnames).intersection( - name for opt in self.options for name in opt.names()) - if conflict: - raise ValueError("option names %s already added" % conflict) - option = Argument(*optnames, **attrs) - self._addoption_instance(option, shortupper=False) - - def _addoption(self, *optnames, **attrs): - option = Argument(*optnames, **attrs) - self._addoption_instance(option, shortupper=True) - - def _addoption_instance(self, option, shortupper=False): - if not shortupper: - for opt in option._short_opts: - if opt[0] == '-' and opt[1].islower(): - raise ValueError("lowercase shortoptions reserved") - if self.parser: - self.parser.processoption(option) - self.options.append(option) - - -class MyOptionParser(argparse.ArgumentParser): - def __init__(self, parser, extra_info=None): - if not extra_info: - extra_info = {} - self._parser = parser - argparse.ArgumentParser.__init__(self, usage=parser._usage, - add_help=False, formatter_class=DropShorterLongHelpFormatter) - # extra_info is a dict of (param -> value) to display if there's - # an usage error to provide more contextual information to the user - self.extra_info = extra_info - - def parse_args(self, args=None, namespace=None): - """allow splitting of positional arguments""" - args, argv = self.parse_known_args(args, namespace) - if argv: - for arg in argv: - if arg and arg[0] == '-': - lines = ['unrecognized arguments: %s' % (' '.join(argv))] - for k, v in sorted(self.extra_info.items()): - lines.append(' %s: %s' % (k, v)) - self.error('\n'.join(lines)) - getattr(args, FILE_OR_DIR).extend(argv) - return args - - -class DropShorterLongHelpFormatter(argparse.HelpFormatter): - """shorten help for long options that differ only in extra hyphens - - - collapse **long** options that are the same except for extra hyphens - - special action attribute map_long_option allows surpressing additional - long options - - shortcut if there are only two options and one of them is a short one - - cache result on action object as this is called at least 2 times - """ - - def _format_action_invocation(self, action): - orgstr = argparse.HelpFormatter._format_action_invocation(self, action) - if orgstr and orgstr[0] != '-': # only optional arguments - return orgstr - res = getattr(action, '_formatted_action_invocation', None) - if res: - return res - options = orgstr.split(', ') - if len(options) == 2 and (len(options[0]) == 2 or len(options[1]) == 2): - # a shortcut for '-h, --help' or '--abc', '-a' - action._formatted_action_invocation = orgstr - return orgstr - return_list = [] - option_map = getattr(action, 'map_long_option', {}) - if option_map is None: - option_map = {} - short_long = {} - for option in options: - if len(option) == 2 or option[2] == ' ': - continue - if not option.startswith('--'): - raise ArgumentError('long optional argument without "--": [%s]' - % (option), self) - xxoption = option[2:] - if xxoption.split()[0] not in option_map: - shortened = xxoption.replace('-', '') - if shortened not in short_long or \ - len(short_long[shortened]) < len(xxoption): - short_long[shortened] = xxoption - # now short_long has been filled out to the longest with dashes - # **and** we keep the right option ordering from add_argument - for option in options: - if len(option) == 2 or option[2] == ' ': - return_list.append(option) - if option[2:] == short_long.get(option.replace('-', '')): - return_list.append(option.replace(' ', '=', 1)) - action._formatted_action_invocation = ', '.join(return_list) - return action._formatted_action_invocation - - -def _ensure_removed_sysmodule(modname): - try: - del sys.modules[modname] - except KeyError: - pass - - -class CmdOptions(object): - """ holds cmdline options as attributes.""" - - def __init__(self, values=()): - self.__dict__.update(values) - - def __repr__(self): - return "" % (self.__dict__,) - - def copy(self): - return CmdOptions(self.__dict__) - - -class Notset: - def __repr__(self): - return "" - - -notset = Notset() -FILE_OR_DIR = 'file_or_dir' - - -def _iter_rewritable_modules(package_files): - for fn in package_files: - is_simple_module = '/' not in fn and fn.endswith('.py') - is_package = fn.count('/') == 1 and fn.endswith('__init__.py') - if is_simple_module: - module_name, _ = os.path.splitext(fn) - yield module_name - elif is_package: - package_name = os.path.dirname(fn) - yield package_name - - -class Config(object): - """ access to configuration values, pluginmanager and plugin hooks. """ - - def __init__(self, pluginmanager): - #: access to command line option as attributes. - #: (deprecated), use :py:func:`getoption() <_pytest.config.Config.getoption>` instead - self.option = CmdOptions() - _a = FILE_OR_DIR - self._parser = Parser( - usage="%%(prog)s [options] [%s] [%s] [...]" % (_a, _a), - processopt=self._processopt, - ) - #: a pluginmanager instance - self.pluginmanager = pluginmanager - self.trace = self.pluginmanager.trace.root.get("config") - self.hook = self.pluginmanager.hook - self._inicache = {} - self._override_ini = () - self._opt2dest = {} - self._cleanup = [] - self._warn = self.pluginmanager._warn - self.pluginmanager.register(self, "pytestconfig") - self._configured = False - - def do_setns(dic): - import pytest - setns(pytest, dic) - - self.hook.pytest_namespace.call_historic(do_setns, {}) - self.hook.pytest_addoption.call_historic(kwargs=dict(parser=self._parser)) - - def add_cleanup(self, func): - """ Add a function to be called when the config object gets out of - use (usually coninciding with pytest_unconfigure).""" - self._cleanup.append(func) - - def _do_configure(self): - assert not self._configured - self._configured = True - self.hook.pytest_configure.call_historic(kwargs=dict(config=self)) - - def _ensure_unconfigure(self): - if self._configured: - self._configured = False - self.hook.pytest_unconfigure(config=self) - self.hook.pytest_configure._call_history = [] - while self._cleanup: - fin = self._cleanup.pop() - fin() - - def warn(self, code, message, fslocation=None, nodeid=None): - """ generate a warning for this test session. """ - self.hook.pytest_logwarning.call_historic(kwargs=dict( - code=code, message=message, - fslocation=fslocation, nodeid=nodeid)) - - def get_terminal_writer(self): - return self.pluginmanager.get_plugin("terminalreporter")._tw - - def pytest_cmdline_parse(self, pluginmanager, args): - # REF1 assert self == pluginmanager.config, (self, pluginmanager.config) - self.parse(args) - return self - - def notify_exception(self, excinfo, option=None): - if option and option.fulltrace: - style = "long" - else: - style = "native" - excrepr = excinfo.getrepr(funcargs=True, - showlocals=getattr(option, 'showlocals', False), - style=style, - ) - res = self.hook.pytest_internalerror(excrepr=excrepr, - excinfo=excinfo) - if not py.builtin.any(res): - for line in str(excrepr).split("\n"): - sys.stderr.write("INTERNALERROR> %s\n" % line) - sys.stderr.flush() - - def cwd_relative_nodeid(self, nodeid): - # nodeid's are relative to the rootpath, compute relative to cwd - if self.invocation_dir != self.rootdir: - fullpath = self.rootdir.join(nodeid) - nodeid = self.invocation_dir.bestrelpath(fullpath) - return nodeid - - @classmethod - def fromdictargs(cls, option_dict, args): - """ constructor useable for subprocesses. """ - config = get_config() - config.option.__dict__.update(option_dict) - config.parse(args, addopts=False) - for x in config.option.plugins: - config.pluginmanager.consider_pluginarg(x) - return config - - def _processopt(self, opt): - for name in opt._short_opts + opt._long_opts: - self._opt2dest[name] = opt.dest - - if hasattr(opt, 'default') and opt.dest: - if not hasattr(self.option, opt.dest): - setattr(self.option, opt.dest, opt.default) - - @hookimpl(trylast=True) - def pytest_load_initial_conftests(self, early_config): - self.pluginmanager._set_initial_conftests(early_config.known_args_namespace) - - def _initini(self, args): - ns, unknown_args = self._parser.parse_known_and_unknown_args(args, namespace=self.option.copy()) - r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args, warnfunc=self.warn) - self.rootdir, self.inifile, self.inicfg = r - self._parser.extra_info['rootdir'] = self.rootdir - self._parser.extra_info['inifile'] = self.inifile - self.invocation_dir = py.path.local() - self._parser.addini('addopts', 'extra command line options', 'args') - self._parser.addini('minversion', 'minimally required pytest version') - self._override_ini = ns.override_ini or () - - def _consider_importhook(self, args): - """Install the PEP 302 import hook if using assertion re-writing. - - Needs to parse the --assert= option from the commandline - and find all the installed plugins to mark them for re-writing - by the importhook. - """ - ns, unknown_args = self._parser.parse_known_and_unknown_args(args) - mode = ns.assertmode - if mode == 'rewrite': - try: - hook = _pytest.assertion.install_importhook(self) - except SystemError: - mode = 'plain' - else: - self._mark_plugins_for_rewrite(hook) - self._warn_about_missing_assertion(mode) - - def _mark_plugins_for_rewrite(self, hook): - """ - Given an importhook, mark for rewrite any top-level - modules or packages in the distribution package for - all pytest plugins. - """ - import pkg_resources - self.pluginmanager.rewrite_hook = hook - - # 'RECORD' available for plugins installed normally (pip install) - # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e) - # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa - # so it shouldn't be an issue - metadata_files = 'RECORD', 'SOURCES.txt' - - package_files = ( - entry.split(',')[0] - for entrypoint in pkg_resources.iter_entry_points('pytest11') - for metadata in metadata_files - for entry in entrypoint.dist._get_metadata(metadata) - ) - - for name in _iter_rewritable_modules(package_files): - hook.mark_rewrite(name) - - def _warn_about_missing_assertion(self, mode): - try: - assert False - except AssertionError: - pass - else: - if mode == 'plain': - sys.stderr.write("WARNING: ASSERTIONS ARE NOT EXECUTED" - " and FAILING TESTS WILL PASS. Are you" - " using python -O?") - else: - sys.stderr.write("WARNING: assertions not in test modules or" - " plugins will be ignored" - " because assert statements are not executed " - "by the underlying Python interpreter " - "(are you using python -O?)\n") - - def _preparse(self, args, addopts=True): - self._initini(args) - if addopts: - args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args - args[:] = self.getini("addopts") + args - self._checkversion() - self._consider_importhook(args) - self.pluginmanager.consider_preparse(args) - self.pluginmanager.load_setuptools_entrypoints('pytest11') - self.pluginmanager.consider_env() - self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy()) - if self.known_args_namespace.confcutdir is None and self.inifile: - confcutdir = py.path.local(self.inifile).dirname - self.known_args_namespace.confcutdir = confcutdir - try: - self.hook.pytest_load_initial_conftests(early_config=self, - args=args, parser=self._parser) - except ConftestImportFailure: - e = sys.exc_info()[1] - if ns.help or ns.version: - # we don't want to prevent --help/--version to work - # so just let is pass and print a warning at the end - self._warn("could not load initial conftests (%s)\n" % e.path) - else: - raise - - def _checkversion(self): - import pytest - minver = self.inicfg.get('minversion', None) - if minver: - ver = minver.split(".") - myver = pytest.__version__.split(".") - if myver < ver: - raise pytest.UsageError( - "%s:%d: requires pytest-%s, actual pytest-%s'" % ( - self.inicfg.config.path, self.inicfg.lineof('minversion'), - minver, pytest.__version__)) - - def parse(self, args, addopts=True): - # parse given cmdline arguments into this config object. - assert not hasattr(self, 'args'), ( - "can only parse cmdline args at most once per Config object") - self._origargs = args - self.hook.pytest_addhooks.call_historic( - kwargs=dict(pluginmanager=self.pluginmanager)) - self._preparse(args, addopts=addopts) - # XXX deprecated hook: - self.hook.pytest_cmdline_preparse(config=self, args=args) - self._parser.after_preparse = True - try: - args = self._parser.parse_setoption(args, self.option, namespace=self.option) - if not args: - cwd = os.getcwd() - if cwd == self.rootdir: - args = self.getini('testpaths') - if not args: - args = [cwd] - self.args = args - except PrintHelp: - pass - - def addinivalue_line(self, name, line): - """ add a line to an ini-file option. The option must have been - declared but might not yet be set in which case the line becomes the - the first line in its value. """ - x = self.getini(name) - assert isinstance(x, list) - x.append(line) # modifies the cached list inline - - def getini(self, name): - """ return configuration value from an :ref:`ini file `. If the - specified name hasn't been registered through a prior - :py:func:`parser.addini <_pytest.config.Parser.addini>` - call (usually from a plugin), a ValueError is raised. """ - try: - return self._inicache[name] - except KeyError: - self._inicache[name] = val = self._getini(name) - return val - - def _getini(self, name): - try: - description, type, default = self._parser._inidict[name] - except KeyError: - raise ValueError("unknown configuration value: %r" % (name,)) - value = self._get_override_ini_value(name) - if value is None: - try: - value = self.inicfg[name] - except KeyError: - if default is not None: - return default - if type is None: - return '' - return [] - if type == "pathlist": - dp = py.path.local(self.inicfg.config.path).dirpath() - values = [] - for relpath in shlex.split(value): - values.append(dp.join(relpath, abs=True)) - return values - elif type == "args": - return shlex.split(value) - elif type == "linelist": - return [t for t in map(lambda x: x.strip(), value.split("\n")) if t] - elif type == "bool": - return bool(_strtobool(value.strip())) - else: - assert type is None - return value - - def _getconftest_pathlist(self, name, path): - try: - mod, relroots = self.pluginmanager._rget_with_confmod(name, path) - except KeyError: - return None - modpath = py.path.local(mod.__file__).dirpath() - values = [] - for relroot in relroots: - if not isinstance(relroot, py.path.local): - relroot = relroot.replace("/", py.path.local.sep) - relroot = modpath.join(relroot, abs=True) - values.append(relroot) - return values - - def _get_override_ini_value(self, name): - value = None - # override_ini is a list of list, to support both -o foo1=bar1 foo2=bar2 and - # and -o foo1=bar1 -o foo2=bar2 options - # always use the last item if multiple value set for same ini-name, - # e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2 - for ini_config_list in self._override_ini: - for ini_config in ini_config_list: - try: - (key, user_ini_value) = ini_config.split("=", 1) - except ValueError: - raise UsageError("-o/--override-ini expects option=value style.") - if key == name: - value = user_ini_value - return value - - def getoption(self, name, default=notset, skip=False): - """ return command line option value. - - :arg name: name of the option. You may also specify - the literal ``--OPT`` option instead of the "dest" option name. - :arg default: default value if no option of that name exists. - :arg skip: if True raise pytest.skip if option does not exists - or has a None value. - """ - name = self._opt2dest.get(name, name) - try: - val = getattr(self.option, name) - if val is None and skip: - raise AttributeError(name) - return val - except AttributeError: - if default is not notset: - return default - if skip: - import pytest - pytest.skip("no %r option found" % (name,)) - raise ValueError("no option named %r" % (name,)) - - def getvalue(self, name, path=None): - """ (deprecated, use getoption()) """ - return self.getoption(name) - - def getvalueorskip(self, name, path=None): - """ (deprecated, use getoption(skip=True)) """ - return self.getoption(name, skip=True) - - -def exists(path, ignore=EnvironmentError): - try: - return path.check() - except ignore: - return False - - -def getcfg(args, warnfunc=None): - """ - Search the list of arguments for a valid ini-file for pytest, - and return a tuple of (rootdir, inifile, cfg-dict). - - note: warnfunc is an optional function used to warn - about ini-files that use deprecated features. - This parameter should be removed when pytest - adopts standard deprecation warnings (#1804). - """ - from _pytest.deprecated import SETUP_CFG_PYTEST - inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"] - args = [x for x in args if not str(x).startswith("-")] - if not args: - args = [py.path.local()] - for arg in args: - arg = py.path.local(arg) - for base in arg.parts(reverse=True): - for inibasename in inibasenames: - p = base.join(inibasename) - if exists(p): - iniconfig = py.iniconfig.IniConfig(p) - if 'pytest' in iniconfig.sections: - if inibasename == 'setup.cfg' and warnfunc: - warnfunc('C1', SETUP_CFG_PYTEST) - return base, p, iniconfig['pytest'] - if inibasename == 'setup.cfg' and 'tool:pytest' in iniconfig.sections: - return base, p, iniconfig['tool:pytest'] - elif inibasename == "pytest.ini": - # allowed to be empty - return base, p, {} - return None, None, None - - -def get_common_ancestor(paths): - common_ancestor = None - for path in paths: - if not path.exists(): - continue - if common_ancestor is None: - common_ancestor = path - else: - if path.relto(common_ancestor) or path == common_ancestor: - continue - elif common_ancestor.relto(path): - common_ancestor = path - else: - shared = path.common(common_ancestor) - if shared is not None: - common_ancestor = shared - if common_ancestor is None: - common_ancestor = py.path.local() - elif common_ancestor.isfile(): - common_ancestor = common_ancestor.dirpath() - return common_ancestor - - -def get_dirs_from_args(args): - def is_option(x): - return str(x).startswith('-') - - def get_file_part_from_node_id(x): - return str(x).split('::')[0] - - def get_dir_from_path(path): - if path.isdir(): - return path - return py.path.local(path.dirname) - - # These look like paths but may not exist - possible_paths = ( - py.path.local(get_file_part_from_node_id(arg)) - for arg in args - if not is_option(arg) - ) - - return [ - get_dir_from_path(path) - for path in possible_paths - if path.exists() - ] - - -def determine_setup(inifile, args, warnfunc=None): - dirs = get_dirs_from_args(args) - if inifile: - iniconfig = py.iniconfig.IniConfig(inifile) - try: - inicfg = iniconfig["pytest"] - except KeyError: - inicfg = None - rootdir = get_common_ancestor(dirs) - else: - ancestor = get_common_ancestor(dirs) - rootdir, inifile, inicfg = getcfg([ancestor], warnfunc=warnfunc) - if rootdir is None: - for rootdir in ancestor.parts(reverse=True): - if rootdir.join("setup.py").exists(): - break - else: - rootdir, inifile, inicfg = getcfg(dirs, warnfunc=warnfunc) - if rootdir is None: - rootdir = get_common_ancestor([py.path.local(), ancestor]) - is_fs_root = os.path.splitdrive(str(rootdir))[1] == '/' - if is_fs_root: - rootdir = ancestor - return rootdir, inifile, inicfg or {} - - -def setns(obj, dic): - import pytest - for name, value in dic.items(): - if isinstance(value, dict): - mod = getattr(obj, name, None) - if mod is None: - modname = "pytest.%s" % name - mod = types.ModuleType(modname) - sys.modules[modname] = mod - mod.__all__ = [] - setattr(obj, name, mod) - obj.__all__.append(name) - setns(mod, value) - else: - setattr(obj, name, value) - obj.__all__.append(name) - # if obj != pytest: - # pytest.__all__.append(name) - setattr(pytest, name, value) - - -def create_terminal_writer(config, *args, **kwargs): - """Create a TerminalWriter instance configured according to the options - in the config object. Every code which requires a TerminalWriter object - and has access to a config object should use this function. - """ - tw = py.io.TerminalWriter(*args, **kwargs) - if config.option.color == 'yes': - tw.hasmarkup = True - if config.option.color == 'no': - tw.hasmarkup = False - return tw - - -def _strtobool(val): - """Convert a string representation of truth to true (1) or false (0). - - True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values - are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if - 'val' is anything else. - - .. note:: copied from distutils.util - """ - val = val.lower() - if val in ('y', 'yes', 't', 'true', 'on', '1'): - return 1 - elif val in ('n', 'no', 'f', 'false', 'off', '0'): - return 0 - else: - raise ValueError("invalid truth value %r" % (val,)) diff --git a/third_party/python/pytest/_pytest/deprecated.py b/third_party/python/pytest/_pytest/deprecated.py deleted file mode 100644 index 38e949677827..000000000000 --- a/third_party/python/pytest/_pytest/deprecated.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -This module contains deprecation messages and bits of code used elsewhere in the codebase -that is planned to be removed in the next pytest release. - -Keeping it in a central location makes it easy to track what is deprecated and should -be removed when the time comes. -""" -from __future__ import absolute_import, division, print_function - - -class RemovedInPytest4Warning(DeprecationWarning): - """warning class for features removed in pytest 4.0""" - - -MAIN_STR_ARGS = 'passing a string to pytest.main() is deprecated, ' \ - 'pass a list of arguments instead.' - -YIELD_TESTS = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0' - -FUNCARG_PREFIX = ( - '{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated ' - 'and scheduled to be removed in pytest 4.0. ' - 'Please remove the prefix and use the @pytest.fixture decorator instead.') - -SETUP_CFG_PYTEST = '[pytest] section in setup.cfg files is deprecated, use [tool:pytest] instead.' - -GETFUNCARGVALUE = "use of getfuncargvalue is deprecated, use getfixturevalue" - -RESULT_LOG = ( - '--result-log is deprecated and scheduled for removal in pytest 4.0.\n' - 'See https://docs.pytest.org/en/latest/usage.html#creating-resultlog-format-files for more information.' -) - -MARK_INFO_ATTRIBUTE = RemovedInPytest4Warning( - "MarkInfo objects are deprecated as they contain the merged marks" -) - -MARK_PARAMETERSET_UNPACKING = RemovedInPytest4Warning( - "Applying marks directly to parameters is deprecated," - " please use pytest.param(..., marks=...) instead.\n" - "For more details, see: https://docs.pytest.org/en/latest/parametrize.html" -) diff --git a/third_party/python/pytest/_pytest/doctest.py b/third_party/python/pytest/_pytest/doctest.py deleted file mode 100644 index 4c05acddf75b..000000000000 --- a/third_party/python/pytest/_pytest/doctest.py +++ /dev/null @@ -1,362 +0,0 @@ -""" discover and run doctests in modules and test files.""" -from __future__ import absolute_import, division, print_function - -import traceback - -import pytest -from _pytest._code.code import ExceptionInfo, ReprFileLocation, TerminalRepr -from _pytest.fixtures import FixtureRequest - - -DOCTEST_REPORT_CHOICE_NONE = 'none' -DOCTEST_REPORT_CHOICE_CDIFF = 'cdiff' -DOCTEST_REPORT_CHOICE_NDIFF = 'ndiff' -DOCTEST_REPORT_CHOICE_UDIFF = 'udiff' -DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE = 'only_first_failure' - -DOCTEST_REPORT_CHOICES = ( - DOCTEST_REPORT_CHOICE_NONE, - DOCTEST_REPORT_CHOICE_CDIFF, - DOCTEST_REPORT_CHOICE_NDIFF, - DOCTEST_REPORT_CHOICE_UDIFF, - DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE, -) - - -def pytest_addoption(parser): - parser.addini('doctest_optionflags', 'option flags for doctests', - type="args", default=["ELLIPSIS"]) - parser.addini("doctest_encoding", 'encoding used for doctest files', default="utf-8") - group = parser.getgroup("collect") - group.addoption("--doctest-modules", - action="store_true", default=False, - help="run doctests in all .py modules", - dest="doctestmodules") - group.addoption("--doctest-report", - type=str.lower, default="udiff", - help="choose another output format for diffs on doctest failure", - choices=DOCTEST_REPORT_CHOICES, - dest="doctestreport") - group.addoption("--doctest-glob", - action="append", default=[], metavar="pat", - help="doctests file matching pattern, default: test*.txt", - dest="doctestglob") - group.addoption("--doctest-ignore-import-errors", - action="store_true", default=False, - help="ignore doctest ImportErrors", - dest="doctest_ignore_import_errors") - - -def pytest_collect_file(path, parent): - config = parent.config - if path.ext == ".py": - if config.option.doctestmodules: - return DoctestModule(path, parent) - elif _is_doctest(config, path, parent): - return DoctestTextfile(path, parent) - - -def _is_doctest(config, path, parent): - if path.ext in ('.txt', '.rst') and parent.session.isinitpath(path): - return True - globs = config.getoption("doctestglob") or ['test*.txt'] - for glob in globs: - if path.check(fnmatch=glob): - return True - return False - - -class ReprFailDoctest(TerminalRepr): - - def __init__(self, reprlocation, lines): - self.reprlocation = reprlocation - self.lines = lines - - def toterminal(self, tw): - for line in self.lines: - tw.line(line) - self.reprlocation.toterminal(tw) - - -class DoctestItem(pytest.Item): - def __init__(self, name, parent, runner=None, dtest=None): - super(DoctestItem, self).__init__(name, parent) - self.runner = runner - self.dtest = dtest - self.obj = None - self.fixture_request = None - - def setup(self): - if self.dtest is not None: - self.fixture_request = _setup_fixtures(self) - globs = dict(getfixture=self.fixture_request.getfixturevalue) - for name, value in self.fixture_request.getfixturevalue('doctest_namespace').items(): - globs[name] = value - self.dtest.globs.update(globs) - - def runtest(self): - _check_all_skipped(self.dtest) - self.runner.run(self.dtest) - - def repr_failure(self, excinfo): - import doctest - if excinfo.errisinstance((doctest.DocTestFailure, - doctest.UnexpectedException)): - doctestfailure = excinfo.value - example = doctestfailure.example - test = doctestfailure.test - filename = test.filename - if test.lineno is None: - lineno = None - else: - lineno = test.lineno + example.lineno + 1 - message = excinfo.type.__name__ - reprlocation = ReprFileLocation(filename, lineno, message) - checker = _get_checker() - report_choice = _get_report_choice(self.config.getoption("doctestreport")) - if lineno is not None: - lines = doctestfailure.test.docstring.splitlines(False) - # add line numbers to the left of the error message - lines = ["%03d %s" % (i + test.lineno + 1, x) - for (i, x) in enumerate(lines)] - # trim docstring error lines to 10 - lines = lines[max(example.lineno - 9, 0):example.lineno + 1] - else: - lines = ['EXAMPLE LOCATION UNKNOWN, not showing all tests of that example'] - indent = '>>>' - for line in example.source.splitlines(): - lines.append('??? %s %s' % (indent, line)) - indent = '...' - if excinfo.errisinstance(doctest.DocTestFailure): - lines += checker.output_difference(example, - doctestfailure.got, report_choice).split("\n") - else: - inner_excinfo = ExceptionInfo(excinfo.value.exc_info) - lines += ["UNEXPECTED EXCEPTION: %s" % - repr(inner_excinfo.value)] - lines += traceback.format_exception(*excinfo.value.exc_info) - return ReprFailDoctest(reprlocation, lines) - else: - return super(DoctestItem, self).repr_failure(excinfo) - - def reportinfo(self): - return self.fspath, self.dtest.lineno, "[doctest] %s" % self.name - - -def _get_flag_lookup(): - import doctest - return dict(DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1, - DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE, - NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE, - ELLIPSIS=doctest.ELLIPSIS, - IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL, - COMPARISON_FLAGS=doctest.COMPARISON_FLAGS, - ALLOW_UNICODE=_get_allow_unicode_flag(), - ALLOW_BYTES=_get_allow_bytes_flag(), - ) - - -def get_optionflags(parent): - optionflags_str = parent.config.getini("doctest_optionflags") - flag_lookup_table = _get_flag_lookup() - flag_acc = 0 - for flag in optionflags_str: - flag_acc |= flag_lookup_table[flag] - return flag_acc - - -class DoctestTextfile(pytest.Module): - obj = None - - def collect(self): - import doctest - - # inspired by doctest.testfile; ideally we would use it directly, - # but it doesn't support passing a custom checker - encoding = self.config.getini("doctest_encoding") - text = self.fspath.read_text(encoding) - filename = str(self.fspath) - name = self.fspath.basename - globs = {'__name__': '__main__'} - - optionflags = get_optionflags(self) - runner = doctest.DebugRunner(verbose=0, optionflags=optionflags, - checker=_get_checker()) - _fix_spoof_python2(runner, encoding) - - parser = doctest.DocTestParser() - test = parser.get_doctest(text, globs, name, filename, 0) - if test.examples: - yield DoctestItem(test.name, self, runner, test) - - -def _check_all_skipped(test): - """raises pytest.skip() if all examples in the given DocTest have the SKIP - option set. - """ - import doctest - all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples) - if all_skipped: - pytest.skip('all tests skipped by +SKIP option') - - -class DoctestModule(pytest.Module): - def collect(self): - import doctest - if self.fspath.basename == "conftest.py": - module = self.config.pluginmanager._importconftest(self.fspath) - else: - try: - module = self.fspath.pyimport() - except ImportError: - if self.config.getvalue('doctest_ignore_import_errors'): - pytest.skip('unable to import module %r' % self.fspath) - else: - raise - # uses internal doctest module parsing mechanism - finder = doctest.DocTestFinder() - optionflags = get_optionflags(self) - runner = doctest.DebugRunner(verbose=0, optionflags=optionflags, - checker=_get_checker()) - - for test in finder.find(module, module.__name__): - if test.examples: # skip empty doctests - yield DoctestItem(test.name, self, runner, test) - - -def _setup_fixtures(doctest_item): - """ - Used by DoctestTextfile and DoctestItem to setup fixture information. - """ - def func(): - pass - - doctest_item.funcargs = {} - fm = doctest_item.session._fixturemanager - doctest_item._fixtureinfo = fm.getfixtureinfo(node=doctest_item, func=func, - cls=None, funcargs=False) - fixture_request = FixtureRequest(doctest_item) - fixture_request._fillfixtures() - return fixture_request - - -def _get_checker(): - """ - Returns a doctest.OutputChecker subclass that takes in account the - ALLOW_UNICODE option to ignore u'' prefixes in strings and ALLOW_BYTES - to strip b'' prefixes. - Useful when the same doctest should run in Python 2 and Python 3. - - An inner class is used to avoid importing "doctest" at the module - level. - """ - if hasattr(_get_checker, 'LiteralsOutputChecker'): - return _get_checker.LiteralsOutputChecker() - - import doctest - import re - - class LiteralsOutputChecker(doctest.OutputChecker): - """ - Copied from doctest_nose_plugin.py from the nltk project: - https://github.com/nltk/nltk - - Further extended to also support byte literals. - """ - - _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE) - _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE) - - def check_output(self, want, got, optionflags): - res = doctest.OutputChecker.check_output(self, want, got, - optionflags) - if res: - return True - - allow_unicode = optionflags & _get_allow_unicode_flag() - allow_bytes = optionflags & _get_allow_bytes_flag() - if not allow_unicode and not allow_bytes: - return False - - else: # pragma: no cover - def remove_prefixes(regex, txt): - return re.sub(regex, r'\1\2', txt) - - if allow_unicode: - want = remove_prefixes(self._unicode_literal_re, want) - got = remove_prefixes(self._unicode_literal_re, got) - if allow_bytes: - want = remove_prefixes(self._bytes_literal_re, want) - got = remove_prefixes(self._bytes_literal_re, got) - res = doctest.OutputChecker.check_output(self, want, got, - optionflags) - return res - - _get_checker.LiteralsOutputChecker = LiteralsOutputChecker - return _get_checker.LiteralsOutputChecker() - - -def _get_allow_unicode_flag(): - """ - Registers and returns the ALLOW_UNICODE flag. - """ - import doctest - return doctest.register_optionflag('ALLOW_UNICODE') - - -def _get_allow_bytes_flag(): - """ - Registers and returns the ALLOW_BYTES flag. - """ - import doctest - return doctest.register_optionflag('ALLOW_BYTES') - - -def _get_report_choice(key): - """ - This function returns the actual `doctest` module flag value, we want to do it as late as possible to avoid - importing `doctest` and all its dependencies when parsing options, as it adds overhead and breaks tests. - """ - import doctest - - return { - DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF, - DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF, - DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF, - DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE, - DOCTEST_REPORT_CHOICE_NONE: 0, - }[key] - - -def _fix_spoof_python2(runner, encoding): - """ - Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output. This - should patch only doctests for text files because they don't have a way to declare their - encoding. Doctests in docstrings from Python modules don't have the same problem given that - Python already decoded the strings. - - This fixes the problem related in issue #2434. - """ - from _pytest.compat import _PY2 - if not _PY2: - return - - from doctest import _SpoofOut - - class UnicodeSpoof(_SpoofOut): - - def getvalue(self): - result = _SpoofOut.getvalue(self) - if encoding: - result = result.decode(encoding) - return result - - runner._fakeout = UnicodeSpoof() - - -@pytest.fixture(scope='session') -def doctest_namespace(): - """ - Inject names into the doctest namespace. - """ - return dict() diff --git a/third_party/python/pytest/_pytest/main.py b/third_party/python/pytest/_pytest/main.py deleted file mode 100644 index eacae8dab1eb..000000000000 --- a/third_party/python/pytest/_pytest/main.py +++ /dev/null @@ -1,835 +0,0 @@ -""" core implementation of testing process: init, session, runtest loop. """ -from __future__ import absolute_import, division, print_function - -import functools -import os -import sys - -import _pytest -from _pytest import nodes -import _pytest._code -import py -try: - from collections import MutableMapping as MappingMixin -except ImportError: - from UserDict import DictMixin as MappingMixin - -from _pytest.config import directory_arg, UsageError, hookimpl -from _pytest.outcomes import exit -from _pytest.runner import collect_one_node - -tracebackcutdir = py.path.local(_pytest.__file__).dirpath() - -# exitcodes for the command line -EXIT_OK = 0 -EXIT_TESTSFAILED = 1 -EXIT_INTERRUPTED = 2 -EXIT_INTERNALERROR = 3 -EXIT_USAGEERROR = 4 -EXIT_NOTESTSCOLLECTED = 5 - - -def pytest_addoption(parser): - parser.addini("norecursedirs", "directory patterns to avoid for recursion", - type="args", default=['.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', 'venv']) - parser.addini("testpaths", "directories to search for tests when no files or directories are given in the " - "command line.", - type="args", default=[]) - # parser.addini("dirpatterns", - # "patterns specifying possible locations of test files", - # type="linelist", default=["**/test_*.txt", - # "**/test_*.py", "**/*_test.py"] - # ) - group = parser.getgroup("general", "running and selection options") - group._addoption('-x', '--exitfirst', action="store_const", - dest="maxfail", const=1, - help="exit instantly on first error or failed test."), - group._addoption('--maxfail', metavar="num", - action="store", type=int, dest="maxfail", default=0, - help="exit after first num failures or errors.") - group._addoption('--strict', action="store_true", - help="marks not registered in configuration file raise errors.") - group._addoption("-c", metavar="file", type=str, dest="inifilename", - help="load configuration from `file` instead of trying to locate one of the implicit " - "configuration files.") - group._addoption("--continue-on-collection-errors", action="store_true", - default=False, dest="continue_on_collection_errors", - help="Force test execution even if collection errors occur.") - - group = parser.getgroup("collect", "collection") - group.addoption('--collectonly', '--collect-only', action="store_true", - help="only collect tests, don't execute them."), - group.addoption('--pyargs', action="store_true", - help="try to interpret all arguments as python packages.") - group.addoption("--ignore", action="append", metavar="path", - help="ignore path during collection (multi-allowed).") - # when changing this to --conf-cut-dir, config.py Conftest.setinitial - # needs upgrading as well - group.addoption('--confcutdir', dest="confcutdir", default=None, - metavar="dir", type=functools.partial(directory_arg, optname="--confcutdir"), - help="only load conftest.py's relative to specified dir.") - group.addoption('--noconftest', action="store_true", - dest="noconftest", default=False, - help="Don't load any conftest.py files.") - group.addoption('--keepduplicates', '--keep-duplicates', action="store_true", - dest="keepduplicates", default=False, - help="Keep duplicate tests.") - group.addoption('--collect-in-virtualenv', action='store_true', - dest='collect_in_virtualenv', default=False, - help="Don't ignore tests in a local virtualenv directory") - - group = parser.getgroup("debugconfig", - "test session debugging and configuration") - group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir", - help="base temporary directory for this test run.") - - -def pytest_namespace(): - """keeping this one works around a deeper startup issue in pytest - - i tried to find it for a while but the amount of time turned unsustainable, - so i put a hack in to revisit later - """ - return {} - - -def pytest_configure(config): - __import__('pytest').config = config # compatibiltiy - - -def wrap_session(config, doit): - """Skeleton command line program""" - session = Session(config) - session.exitstatus = EXIT_OK - initstate = 0 - try: - try: - config._do_configure() - initstate = 1 - config.hook.pytest_sessionstart(session=session) - initstate = 2 - session.exitstatus = doit(config, session) or 0 - except UsageError: - raise - except KeyboardInterrupt: - excinfo = _pytest._code.ExceptionInfo() - if initstate < 2 and isinstance(excinfo.value, exit.Exception): - sys.stderr.write('{0}: {1}\n'.format( - excinfo.typename, excinfo.value.msg)) - config.hook.pytest_keyboard_interrupt(excinfo=excinfo) - session.exitstatus = EXIT_INTERRUPTED - except: # noqa - excinfo = _pytest._code.ExceptionInfo() - config.notify_exception(excinfo, config.option) - session.exitstatus = EXIT_INTERNALERROR - if excinfo.errisinstance(SystemExit): - sys.stderr.write("mainloop: caught Spurious SystemExit!\n") - - finally: - excinfo = None # Explicitly break reference cycle. - session.startdir.chdir() - if initstate >= 2: - config.hook.pytest_sessionfinish( - session=session, - exitstatus=session.exitstatus) - config._ensure_unconfigure() - return session.exitstatus - - -def pytest_cmdline_main(config): - return wrap_session(config, _main) - - -def _main(config, session): - """ default command line protocol for initialization, session, - running tests and reporting. """ - config.hook.pytest_collection(session=session) - config.hook.pytest_runtestloop(session=session) - - if session.testsfailed: - return EXIT_TESTSFAILED - elif session.testscollected == 0: - return EXIT_NOTESTSCOLLECTED - - -def pytest_collection(session): - return session.perform_collect() - - -def pytest_runtestloop(session): - if (session.testsfailed and - not session.config.option.continue_on_collection_errors): - raise session.Interrupted( - "%d errors during collection" % session.testsfailed) - - if session.config.option.collectonly: - return True - - for i, item in enumerate(session.items): - nextitem = session.items[i + 1] if i + 1 < len(session.items) else None - item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) - if session.shouldstop: - raise session.Interrupted(session.shouldstop) - return True - - -def _in_venv(path): - """Attempts to detect if ``path`` is the root of a Virtual Environment by - checking for the existence of the appropriate activate script""" - bindir = path.join('Scripts' if sys.platform.startswith('win') else 'bin') - if not bindir.exists(): - return False - activates = ('activate', 'activate.csh', 'activate.fish', - 'Activate', 'Activate.bat', 'Activate.ps1') - return any([fname.basename in activates for fname in bindir.listdir()]) - - -def pytest_ignore_collect(path, config): - ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath()) - ignore_paths = ignore_paths or [] - excludeopt = config.getoption("ignore") - if excludeopt: - ignore_paths.extend([py.path.local(x) for x in excludeopt]) - - if py.path.local(path) in ignore_paths: - return True - - allow_in_venv = config.getoption("collect_in_virtualenv") - if _in_venv(path) and not allow_in_venv: - return True - - # Skip duplicate paths. - keepduplicates = config.getoption("keepduplicates") - duplicate_paths = config.pluginmanager._duplicatepaths - if not keepduplicates: - if path in duplicate_paths: - return True - else: - duplicate_paths.add(path) - - return False - - -class FSHookProxy: - def __init__(self, fspath, pm, remove_mods): - self.fspath = fspath - self.pm = pm - self.remove_mods = remove_mods - - def __getattr__(self, name): - x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods) - self.__dict__[name] = x - return x - - -class _CompatProperty(object): - def __init__(self, name): - self.name = name - - def __get__(self, obj, owner): - if obj is None: - return self - - # TODO: reenable in the features branch - # warnings.warn( - # "usage of {owner!r}.{name} is deprecated, please use pytest.{name} instead".format( - # name=self.name, owner=type(owner).__name__), - # PendingDeprecationWarning, stacklevel=2) - return getattr(__import__('pytest'), self.name) - - -class NodeKeywords(MappingMixin): - def __init__(self, node): - self.node = node - self.parent = node.parent - self._markers = {node.name: True} - - def __getitem__(self, key): - try: - return self._markers[key] - except KeyError: - if self.parent is None: - raise - return self.parent.keywords[key] - - def __setitem__(self, key, value): - self._markers[key] = value - - def __delitem__(self, key): - raise ValueError("cannot delete key in keywords dict") - - def __iter__(self): - seen = set(self._markers) - if self.parent is not None: - seen.update(self.parent.keywords) - return iter(seen) - - def __len__(self): - return len(self.__iter__()) - - def keys(self): - return list(self) - - def __repr__(self): - return "" % (self.node, ) - - -class Node(object): - """ base class for Collector and Item the test collection tree. - Collector subclasses have children, Items are terminal nodes.""" - - def __init__(self, name, parent=None, config=None, session=None): - #: a unique name within the scope of the parent node - self.name = name - - #: the parent collector node. - self.parent = parent - - #: the pytest config object - self.config = config or parent.config - - #: the session this node is part of - self.session = session or parent.session - - #: filesystem path where this node was collected from (can be None) - self.fspath = getattr(parent, 'fspath', None) - - #: keywords/markers collected from all scopes - self.keywords = NodeKeywords(self) - - #: allow adding of extra keywords to use for matching - self.extra_keyword_matches = set() - - # used for storing artificial fixturedefs for direct parametrization - self._name2pseudofixturedef = {} - - @property - def ihook(self): - """ fspath sensitive hook proxy used to call pytest hooks""" - return self.session.gethookproxy(self.fspath) - - Module = _CompatProperty("Module") - Class = _CompatProperty("Class") - Instance = _CompatProperty("Instance") - Function = _CompatProperty("Function") - File = _CompatProperty("File") - Item = _CompatProperty("Item") - - def _getcustomclass(self, name): - maybe_compatprop = getattr(type(self), name) - if isinstance(maybe_compatprop, _CompatProperty): - return getattr(__import__('pytest'), name) - else: - cls = getattr(self, name) - # TODO: reenable in the features branch - # warnings.warn("use of node.%s is deprecated, " - # "use pytest_pycollect_makeitem(...) to create custom " - # "collection nodes" % name, category=DeprecationWarning) - return cls - - def __repr__(self): - return "<%s %r>" % (self.__class__.__name__, - getattr(self, 'name', None)) - - def warn(self, code, message): - """ generate a warning with the given code and message for this - item. """ - assert isinstance(code, str) - fslocation = getattr(self, "location", None) - if fslocation is None: - fslocation = getattr(self, "fspath", None) - self.ihook.pytest_logwarning.call_historic(kwargs=dict( - code=code, message=message, - nodeid=self.nodeid, fslocation=fslocation)) - - # methods for ordering nodes - @property - def nodeid(self): - """ a ::-separated string denoting its collection tree address. """ - try: - return self._nodeid - except AttributeError: - self._nodeid = x = self._makeid() - return x - - def _makeid(self): - return self.parent.nodeid + "::" + self.name - - def __hash__(self): - return hash(self.nodeid) - - def setup(self): - pass - - def teardown(self): - pass - - def _memoizedcall(self, attrname, function): - exattrname = "_ex_" + attrname - failure = getattr(self, exattrname, None) - if failure is not None: - py.builtin._reraise(failure[0], failure[1], failure[2]) - if hasattr(self, attrname): - return getattr(self, attrname) - try: - res = function() - except py.builtin._sysex: - raise - except: # noqa - failure = sys.exc_info() - setattr(self, exattrname, failure) - raise - setattr(self, attrname, res) - return res - - def listchain(self): - """ return list of all parent collectors up to self, - starting from root of collection tree. """ - chain = [] - item = self - while item is not None: - chain.append(item) - item = item.parent - chain.reverse() - return chain - - def add_marker(self, marker): - """ dynamically add a marker object to the node. - - ``marker`` can be a string or pytest.mark.* instance. - """ - from _pytest.mark import MarkDecorator, MARK_GEN - if isinstance(marker, py.builtin._basestring): - marker = getattr(MARK_GEN, marker) - elif not isinstance(marker, MarkDecorator): - raise ValueError("is not a string or pytest.mark.* Marker") - self.keywords[marker.name] = marker - - def get_marker(self, name): - """ get a marker object from this node or None if - the node doesn't have a marker with that name. """ - val = self.keywords.get(name, None) - if val is not None: - from _pytest.mark import MarkInfo, MarkDecorator - if isinstance(val, (MarkDecorator, MarkInfo)): - return val - - def listextrakeywords(self): - """ Return a set of all extra keywords in self and any parents.""" - extra_keywords = set() - item = self - for item in self.listchain(): - extra_keywords.update(item.extra_keyword_matches) - return extra_keywords - - def listnames(self): - return [x.name for x in self.listchain()] - - def addfinalizer(self, fin): - """ register a function to be called when this node is finalized. - - This method can only be called when this node is active - in a setup chain, for example during self.setup(). - """ - self.session._setupstate.addfinalizer(fin, self) - - def getparent(self, cls): - """ get the next parent node (including ourself) - which is an instance of the given class""" - current = self - while current and not isinstance(current, cls): - current = current.parent - return current - - def _prunetraceback(self, excinfo): - pass - - def _repr_failure_py(self, excinfo, style=None): - fm = self.session._fixturemanager - if excinfo.errisinstance(fm.FixtureLookupError): - return excinfo.value.formatrepr() - tbfilter = True - if self.config.option.fulltrace: - style = "long" - else: - tb = _pytest._code.Traceback([excinfo.traceback[-1]]) - self._prunetraceback(excinfo) - if len(excinfo.traceback) == 0: - excinfo.traceback = tb - tbfilter = False # prunetraceback already does it - if style == "auto": - style = "long" - # XXX should excinfo.getrepr record all data and toterminal() process it? - if style is None: - if self.config.option.tbstyle == "short": - style = "short" - else: - style = "long" - - try: - os.getcwd() - abspath = False - except OSError: - abspath = True - - return excinfo.getrepr(funcargs=True, abspath=abspath, - showlocals=self.config.option.showlocals, - style=style, tbfilter=tbfilter) - - repr_failure = _repr_failure_py - - -class Collector(Node): - """ Collector instances create children through collect() - and thus iteratively build a tree. - """ - - class CollectError(Exception): - """ an error during collection, contains a custom message. """ - - def collect(self): - """ returns a list of children (items and collectors) - for this collection node. - """ - raise NotImplementedError("abstract") - - def repr_failure(self, excinfo): - """ represent a collection failure. """ - if excinfo.errisinstance(self.CollectError): - exc = excinfo.value - return str(exc.args[0]) - return self._repr_failure_py(excinfo, style="short") - - def _prunetraceback(self, excinfo): - if hasattr(self, 'fspath'): - traceback = excinfo.traceback - ntraceback = traceback.cut(path=self.fspath) - if ntraceback == traceback: - ntraceback = ntraceback.cut(excludepath=tracebackcutdir) - excinfo.traceback = ntraceback.filter() - - -class FSCollector(Collector): - def __init__(self, fspath, parent=None, config=None, session=None): - fspath = py.path.local(fspath) # xxx only for test_resultlog.py? - name = fspath.basename - if parent is not None: - rel = fspath.relto(parent.fspath) - if rel: - name = rel - name = name.replace(os.sep, nodes.SEP) - super(FSCollector, self).__init__(name, parent, config, session) - self.fspath = fspath - - def _check_initialpaths_for_relpath(self): - for initialpath in self.session._initialpaths: - if self.fspath.common(initialpath) == initialpath: - return self.fspath.relto(initialpath.dirname) - - def _makeid(self): - relpath = self.fspath.relto(self.config.rootdir) - - if not relpath: - relpath = self._check_initialpaths_for_relpath() - if os.sep != nodes.SEP: - relpath = relpath.replace(os.sep, nodes.SEP) - return relpath - - -class File(FSCollector): - """ base class for collecting tests from a file. """ - - -class Item(Node): - """ a basic test invocation item. Note that for a single function - there might be multiple test invocation items. - """ - nextitem = None - - def __init__(self, name, parent=None, config=None, session=None): - super(Item, self).__init__(name, parent, config, session) - self._report_sections = [] - - def add_report_section(self, when, key, content): - """ - Adds a new report section, similar to what's done internally to add stdout and - stderr captured output:: - - item.add_report_section("call", "stdout", "report section contents") - - :param str when: - One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``. - :param str key: - Name of the section, can be customized at will. Pytest uses ``"stdout"`` and - ``"stderr"`` internally. - - :param str content: - The full contents as a string. - """ - if content: - self._report_sections.append((when, key, content)) - - def reportinfo(self): - return self.fspath, None, "" - - @property - def location(self): - try: - return self._location - except AttributeError: - location = self.reportinfo() - # bestrelpath is a quite slow function - cache = self.config.__dict__.setdefault("_bestrelpathcache", {}) - try: - fspath = cache[location[0]] - except KeyError: - fspath = self.session.fspath.bestrelpath(location[0]) - cache[location[0]] = fspath - location = (fspath, location[1], str(location[2])) - self._location = location - return location - - -class NoMatch(Exception): - """ raised if matching cannot locate a matching names. """ - - -class Interrupted(KeyboardInterrupt): - """ signals an interrupted test run. """ - __module__ = 'builtins' # for py3 - - -class Session(FSCollector): - Interrupted = Interrupted - - def __init__(self, config): - FSCollector.__init__(self, config.rootdir, parent=None, - config=config, session=self) - self.testsfailed = 0 - self.testscollected = 0 - self.shouldstop = False - self.trace = config.trace.root.get("collection") - self._norecursepatterns = config.getini("norecursedirs") - self.startdir = py.path.local() - self.config.pluginmanager.register(self, name="session") - - def _makeid(self): - return "" - - @hookimpl(tryfirst=True) - def pytest_collectstart(self): - if self.shouldstop: - raise self.Interrupted(self.shouldstop) - - @hookimpl(tryfirst=True) - def pytest_runtest_logreport(self, report): - if report.failed and not hasattr(report, 'wasxfail'): - self.testsfailed += 1 - maxfail = self.config.getvalue("maxfail") - if maxfail and self.testsfailed >= maxfail: - self.shouldstop = "stopping after %d failures" % ( - self.testsfailed) - pytest_collectreport = pytest_runtest_logreport - - def isinitpath(self, path): - return path in self._initialpaths - - def gethookproxy(self, fspath): - # check if we have the common case of running - # hooks with all conftest.py filesall conftest.py - pm = self.config.pluginmanager - my_conftestmodules = pm._getconftestmodules(fspath) - remove_mods = pm._conftest_plugins.difference(my_conftestmodules) - if remove_mods: - # one or more conftests are not in use at this fspath - proxy = FSHookProxy(fspath, pm, remove_mods) - else: - # all plugis are active for this fspath - proxy = self.config.hook - return proxy - - def perform_collect(self, args=None, genitems=True): - hook = self.config.hook - try: - items = self._perform_collect(args, genitems) - self.config.pluginmanager.check_pending() - hook.pytest_collection_modifyitems(session=self, - config=self.config, items=items) - finally: - hook.pytest_collection_finish(session=self) - self.testscollected = len(items) - return items - - def _perform_collect(self, args, genitems): - if args is None: - args = self.config.args - self.trace("perform_collect", self, args) - self.trace.root.indent += 1 - self._notfound = [] - self._initialpaths = set() - self._initialparts = [] - self.items = items = [] - for arg in args: - parts = self._parsearg(arg) - self._initialparts.append(parts) - self._initialpaths.add(parts[0]) - rep = collect_one_node(self) - self.ihook.pytest_collectreport(report=rep) - self.trace.root.indent -= 1 - if self._notfound: - errors = [] - for arg, exc in self._notfound: - line = "(no name %r in any of %r)" % (arg, exc.args[0]) - errors.append("not found: %s\n%s" % (arg, line)) - # XXX: test this - raise UsageError(*errors) - if not genitems: - return rep.result - else: - if rep.passed: - for node in rep.result: - self.items.extend(self.genitems(node)) - return items - - def collect(self): - for parts in self._initialparts: - arg = "::".join(map(str, parts)) - self.trace("processing argument", arg) - self.trace.root.indent += 1 - try: - for x in self._collect(arg): - yield x - except NoMatch: - # we are inside a make_report hook so - # we cannot directly pass through the exception - self._notfound.append((arg, sys.exc_info()[1])) - - self.trace.root.indent -= 1 - - def _collect(self, arg): - names = self._parsearg(arg) - path = names.pop(0) - if path.check(dir=1): - assert not names, "invalid arg %r" % (arg,) - for path in path.visit(fil=lambda x: x.check(file=1), - rec=self._recurse, bf=True, sort=True): - for x in self._collectfile(path): - yield x - else: - assert path.check(file=1) - for x in self.matchnodes(self._collectfile(path), names): - yield x - - def _collectfile(self, path): - ihook = self.gethookproxy(path) - if not self.isinitpath(path): - if ihook.pytest_ignore_collect(path=path, config=self.config): - return () - return ihook.pytest_collect_file(path=path, parent=self) - - def _recurse(self, path): - ihook = self.gethookproxy(path.dirpath()) - if ihook.pytest_ignore_collect(path=path, config=self.config): - return - for pat in self._norecursepatterns: - if path.check(fnmatch=pat): - return False - ihook = self.gethookproxy(path) - ihook.pytest_collect_directory(path=path, parent=self) - return True - - def _tryconvertpyarg(self, x): - """Convert a dotted module name to path. - - """ - import pkgutil - try: - loader = pkgutil.find_loader(x) - except ImportError: - return x - if loader is None: - return x - # This method is sometimes invoked when AssertionRewritingHook, which - # does not define a get_filename method, is already in place: - try: - path = loader.get_filename(x) - except AttributeError: - # Retrieve path from AssertionRewritingHook: - path = loader.modules[x][0].co_filename - if loader.is_package(x): - path = os.path.dirname(path) - return path - - def _parsearg(self, arg): - """ return (fspath, names) tuple after checking the file exists. """ - parts = str(arg).split("::") - if self.config.option.pyargs: - parts[0] = self._tryconvertpyarg(parts[0]) - relpath = parts[0].replace("/", os.sep) - path = self.config.invocation_dir.join(relpath, abs=True) - if not path.check(): - if self.config.option.pyargs: - raise UsageError( - "file or package not found: " + arg + - " (missing __init__.py?)") - else: - raise UsageError("file not found: " + arg) - parts[0] = path - return parts - - def matchnodes(self, matching, names): - self.trace("matchnodes", matching, names) - self.trace.root.indent += 1 - nodes = self._matchnodes(matching, names) - num = len(nodes) - self.trace("matchnodes finished -> ", num, "nodes") - self.trace.root.indent -= 1 - if num == 0: - raise NoMatch(matching, names[:1]) - return nodes - - def _matchnodes(self, matching, names): - if not matching or not names: - return matching - name = names[0] - assert name - nextnames = names[1:] - resultnodes = [] - for node in matching: - if isinstance(node, Item): - if not names: - resultnodes.append(node) - continue - assert isinstance(node, Collector) - rep = collect_one_node(node) - if rep.passed: - has_matched = False - for x in rep.result: - # TODO: remove parametrized workaround once collection structure contains parametrization - if x.name == name or x.name.split("[")[0] == name: - resultnodes.extend(self.matchnodes([x], nextnames)) - has_matched = True - # XXX accept IDs that don't have "()" for class instances - if not has_matched and len(rep.result) == 1 and x.name == "()": - nextnames.insert(0, name) - resultnodes.extend(self.matchnodes([x], nextnames)) - else: - # report collection failures here to avoid failing to run some test - # specified in the command line because the module could not be - # imported (#134) - node.ihook.pytest_collectreport(report=rep) - return resultnodes - - def genitems(self, node): - self.trace("genitems", node) - if isinstance(node, Item): - node.ihook.pytest_itemcollected(item=node) - yield node - else: - assert isinstance(node, Collector) - rep = collect_one_node(node) - if rep.passed: - for subnode in rep.result: - for x in self.genitems(subnode): - yield x - node.ihook.pytest_collectreport(report=rep) diff --git a/third_party/python/pytest/_pytest/mark.py b/third_party/python/pytest/_pytest/mark.py deleted file mode 100644 index 454722ca2c8a..000000000000 --- a/third_party/python/pytest/_pytest/mark.py +++ /dev/null @@ -1,465 +0,0 @@ -""" generic mechanism for marking and selecting python functions. """ -from __future__ import absolute_import, division, print_function - -import inspect -import warnings -from collections import namedtuple -from operator import attrgetter -from .compat import imap -from .deprecated import MARK_PARAMETERSET_UNPACKING - - -def alias(name, warning=None): - getter = attrgetter(name) - - def warned(self): - warnings.warn(warning, stacklevel=2) - return getter(self) - - return property(getter if warning is None else warned, doc='alias for ' + name) - - -class ParameterSet(namedtuple('ParameterSet', 'values, marks, id')): - @classmethod - def param(cls, *values, **kw): - marks = kw.pop('marks', ()) - if isinstance(marks, MarkDecorator): - marks = marks, - else: - assert isinstance(marks, (tuple, list, set)) - - def param_extract_id(id=None): - return id - - id = param_extract_id(**kw) - return cls(values, marks, id) - - @classmethod - def extract_from(cls, parameterset, legacy_force_tuple=False): - """ - :param parameterset: - a legacy style parameterset that may or may not be a tuple, - and may or may not be wrapped into a mess of mark objects - - :param legacy_force_tuple: - enforce tuple wrapping so single argument tuple values - don't get decomposed and break tests - - """ - - if isinstance(parameterset, cls): - return parameterset - if not isinstance(parameterset, MarkDecorator) and legacy_force_tuple: - return cls.param(parameterset) - - newmarks = [] - argval = parameterset - while isinstance(argval, MarkDecorator): - newmarks.append(MarkDecorator(Mark( - argval.markname, argval.args[:-1], argval.kwargs))) - argval = argval.args[-1] - assert not isinstance(argval, ParameterSet) - if legacy_force_tuple: - argval = argval, - - if newmarks: - warnings.warn(MARK_PARAMETERSET_UNPACKING) - - return cls(argval, marks=newmarks, id=None) - - @property - def deprecated_arg_dict(self): - return dict((mark.name, mark) for mark in self.marks) - - -class MarkerError(Exception): - - """Error in use of a pytest marker/attribute.""" - - -def param(*values, **kw): - return ParameterSet.param(*values, **kw) - - -def pytest_addoption(parser): - group = parser.getgroup("general") - group._addoption( - '-k', - action="store", dest="keyword", default='', metavar="EXPRESSION", - help="only run tests which match the given substring expression. " - "An expression is a python evaluatable expression " - "where all names are substring-matched against test names " - "and their parent classes. Example: -k 'test_method or test_" - "other' matches all test functions and classes whose name " - "contains 'test_method' or 'test_other', while -k 'not test_method' " - "matches those that don't contain 'test_method' in their names. " - "Additionally keywords are matched to classes and functions " - "containing extra names in their 'extra_keyword_matches' set, " - "as well as functions which have names assigned directly to them." - ) - - group._addoption( - "-m", - action="store", dest="markexpr", default="", metavar="MARKEXPR", - help="only run tests matching given mark expression. " - "example: -m 'mark1 and not mark2'." - ) - - group.addoption( - "--markers", action="store_true", - help="show markers (builtin, plugin and per-project ones)." - ) - - parser.addini("markers", "markers for test functions", 'linelist') - - -def pytest_cmdline_main(config): - import _pytest.config - if config.option.markers: - config._do_configure() - tw = _pytest.config.create_terminal_writer(config) - for line in config.getini("markers"): - name, rest = line.split(":", 1) - tw.write("@pytest.mark.%s:" % name, bold=True) - tw.line(rest) - tw.line() - config._ensure_unconfigure() - return 0 - - -pytest_cmdline_main.tryfirst = True - - -def pytest_collection_modifyitems(items, config): - keywordexpr = config.option.keyword.lstrip() - matchexpr = config.option.markexpr - if not keywordexpr and not matchexpr: - return - # pytest used to allow "-" for negating - # but today we just allow "-" at the beginning, use "not" instead - # we probably remove "-" altogether soon - if keywordexpr.startswith("-"): - keywordexpr = "not " + keywordexpr[1:] - selectuntil = False - if keywordexpr[-1:] == ":": - selectuntil = True - keywordexpr = keywordexpr[:-1] - - remaining = [] - deselected = [] - for colitem in items: - if keywordexpr and not matchkeyword(colitem, keywordexpr): - deselected.append(colitem) - else: - if selectuntil: - keywordexpr = None - if matchexpr: - if not matchmark(colitem, matchexpr): - deselected.append(colitem) - continue - remaining.append(colitem) - - if deselected: - config.hook.pytest_deselected(items=deselected) - items[:] = remaining - - -class MarkMapping: - """Provides a local mapping for markers where item access - resolves to True if the marker is present. """ - - def __init__(self, keywords): - mymarks = set() - for key, value in keywords.items(): - if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator): - mymarks.add(key) - self._mymarks = mymarks - - def __getitem__(self, name): - return name in self._mymarks - - -class KeywordMapping: - """Provides a local mapping for keywords. - Given a list of names, map any substring of one of these names to True. - """ - - def __init__(self, names): - self._names = names - - def __getitem__(self, subname): - for name in self._names: - if subname in name: - return True - return False - - -def matchmark(colitem, markexpr): - """Tries to match on any marker names, attached to the given colitem.""" - return eval(markexpr, {}, MarkMapping(colitem.keywords)) - - -def matchkeyword(colitem, keywordexpr): - """Tries to match given keyword expression to given collector item. - - Will match on the name of colitem, including the names of its parents. - Only matches names of items which are either a :class:`Class` or a - :class:`Function`. - Additionally, matches on names in the 'extra_keyword_matches' set of - any item, as well as names directly assigned to test functions. - """ - mapped_names = set() - - # Add the names of the current item and any parent items - import pytest - for item in colitem.listchain(): - if not isinstance(item, pytest.Instance): - mapped_names.add(item.name) - - # Add the names added as extra keywords to current or parent items - for name in colitem.listextrakeywords(): - mapped_names.add(name) - - # Add the names attached to the current function through direct assignment - if hasattr(colitem, 'function'): - for name in colitem.function.__dict__: - mapped_names.add(name) - - mapping = KeywordMapping(mapped_names) - if " " not in keywordexpr: - # special case to allow for simple "-k pass" and "-k 1.3" - return mapping[keywordexpr] - elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]: - return not mapping[keywordexpr[4:]] - return eval(keywordexpr, {}, mapping) - - -def pytest_configure(config): - config._old_mark_config = MARK_GEN._config - if config.option.strict: - MARK_GEN._config = config - - -def pytest_unconfigure(config): - MARK_GEN._config = getattr(config, '_old_mark_config', None) - - -class MarkGenerator: - """ Factory for :class:`MarkDecorator` objects - exposed as - a ``pytest.mark`` singleton instance. Example:: - - import pytest - @pytest.mark.slowtest - def test_function(): - pass - - will set a 'slowtest' :class:`MarkInfo` object - on the ``test_function`` object. """ - _config = None - - def __getattr__(self, name): - if name[0] == "_": - raise AttributeError("Marker name must NOT start with underscore") - if self._config is not None: - self._check(name) - return MarkDecorator(Mark(name, (), {})) - - def _check(self, name): - try: - if name in self._markers: - return - except AttributeError: - pass - self._markers = values = set() - for line in self._config.getini("markers"): - marker, _ = line.split(":", 1) - marker = marker.rstrip() - x = marker.split("(", 1)[0] - values.add(x) - if name not in self._markers: - raise AttributeError("%r not a registered marker" % (name,)) - - -def istestfunc(func): - return hasattr(func, "__call__") and \ - getattr(func, "__name__", "") != "" - - -class MarkDecorator: - """ A decorator for test functions and test classes. When applied - it will create :class:`MarkInfo` objects which may be - :ref:`retrieved by hooks as item keywords `. - MarkDecorator instances are often created like this:: - - mark1 = pytest.mark.NAME # simple MarkDecorator - mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator - - and can then be applied as decorators to test functions:: - - @mark2 - def test_function(): - pass - - When a MarkDecorator instance is called it does the following: - 1. If called with a single class as its only positional argument and no - additional keyword arguments, it attaches itself to the class so it - gets applied automatically to all test cases found in that class. - 2. If called with a single function as its only positional argument and - no additional keyword arguments, it attaches a MarkInfo object to the - function, containing all the arguments already stored internally in - the MarkDecorator. - 3. When called in any other case, it performs a 'fake construction' call, - i.e. it returns a new MarkDecorator instance with the original - MarkDecorator's content updated with the arguments passed to this - call. - - Note: The rules above prevent MarkDecorator objects from storing only a - single function or class reference as their positional argument with no - additional keyword or positional arguments. - - """ - - def __init__(self, mark): - assert isinstance(mark, Mark), repr(mark) - self.mark = mark - - name = alias('mark.name') - args = alias('mark.args') - kwargs = alias('mark.kwargs') - - @property - def markname(self): - return self.name # for backward-compat (2.4.1 had this attr) - - def __eq__(self, other): - return self.mark == other.mark if isinstance(other, MarkDecorator) else False - - def __repr__(self): - return "" % (self.mark,) - - def with_args(self, *args, **kwargs): - """ return a MarkDecorator with extra arguments added - - unlike call this can be used even if the sole argument is a callable/class - - :return: MarkDecorator - """ - - mark = Mark(self.name, args, kwargs) - return self.__class__(self.mark.combined_with(mark)) - - def __call__(self, *args, **kwargs): - """ if passed a single callable argument: decorate it with mark info. - otherwise add *args/**kwargs in-place to mark information. """ - if args and not kwargs: - func = args[0] - is_class = inspect.isclass(func) - if len(args) == 1 and (istestfunc(func) or is_class): - if is_class: - store_mark(func, self.mark) - else: - store_legacy_markinfo(func, self.mark) - store_mark(func, self.mark) - return func - return self.with_args(*args, **kwargs) - - -def get_unpacked_marks(obj): - """ - obtain the unpacked marks that are stored on a object - """ - mark_list = getattr(obj, 'pytestmark', []) - - if not isinstance(mark_list, list): - mark_list = [mark_list] - return [ - getattr(mark, 'mark', mark) # unpack MarkDecorator - for mark in mark_list - ] - - -def store_mark(obj, mark): - """store a Mark on a object - this is used to implement the Mark declarations/decorators correctly - """ - assert isinstance(mark, Mark), mark - # always reassign name to avoid updating pytestmark - # in a reference that was only borrowed - obj.pytestmark = get_unpacked_marks(obj) + [mark] - - -def store_legacy_markinfo(func, mark): - """create the legacy MarkInfo objects and put them onto the function - """ - if not isinstance(mark, Mark): - raise TypeError("got {mark!r} instead of a Mark".format(mark=mark)) - holder = getattr(func, mark.name, None) - if holder is None: - holder = MarkInfo(mark) - setattr(func, mark.name, holder) - else: - holder.add_mark(mark) - - -class Mark(namedtuple('Mark', 'name, args, kwargs')): - - def combined_with(self, other): - assert self.name == other.name - return Mark( - self.name, self.args + other.args, - dict(self.kwargs, **other.kwargs)) - - -class MarkInfo(object): - """ Marking object created by :class:`MarkDecorator` instances. """ - - def __init__(self, mark): - assert isinstance(mark, Mark), repr(mark) - self.combined = mark - self._marks = [mark] - - name = alias('combined.name') - args = alias('combined.args') - kwargs = alias('combined.kwargs') - - def __repr__(self): - return "".format(self.combined) - - def add_mark(self, mark): - """ add a MarkInfo with the given args and kwargs. """ - self._marks.append(mark) - self.combined = self.combined.combined_with(mark) - - def __iter__(self): - """ yield MarkInfo objects each relating to a marking-call. """ - return imap(MarkInfo, self._marks) - - -MARK_GEN = MarkGenerator() - - -def _marked(func, mark): - """ Returns True if :func: is already marked with :mark:, False otherwise. - This can happen if marker is applied to class and the test file is - invoked more than once. - """ - try: - func_mark = getattr(func, mark.name) - except AttributeError: - return False - return mark.args == func_mark.args and mark.kwargs == func_mark.kwargs - - -def transfer_markers(funcobj, cls, mod): - """ - this function transfers class level markers and module level markers - into function level markinfo objects - - this is the main reason why marks are so broken - the resolution will involve phasing out function level MarkInfo objects - - """ - for obj in (cls, mod): - for mark in get_unpacked_marks(obj): - if not _marked(funcobj, mark): - store_legacy_markinfo(funcobj, mark) diff --git a/third_party/python/pytest/_pytest/nodes.py b/third_party/python/pytest/_pytest/nodes.py deleted file mode 100644 index ad3af2ce67c8..000000000000 --- a/third_party/python/pytest/_pytest/nodes.py +++ /dev/null @@ -1,37 +0,0 @@ -SEP = "/" - - -def _splitnode(nodeid): - """Split a nodeid into constituent 'parts'. - - Node IDs are strings, and can be things like: - '' - 'testing/code' - 'testing/code/test_excinfo.py' - 'testing/code/test_excinfo.py::TestFormattedExcinfo::()' - - Return values are lists e.g. - [] - ['testing', 'code'] - ['testing', 'code', 'test_excinfo.py'] - ['testing', 'code', 'test_excinfo.py', 'TestFormattedExcinfo', '()'] - """ - if nodeid == '': - # If there is no root node at all, return an empty list so the caller's logic can remain sane - return [] - parts = nodeid.split(SEP) - # Replace single last element 'test_foo.py::Bar::()' with multiple elements 'test_foo.py', 'Bar', '()' - parts[-1:] = parts[-1].split("::") - return parts - - -def ischildnode(baseid, nodeid): - """Return True if the nodeid is a child node of the baseid. - - E.g. 'foo/bar::Baz::()' is a child of 'foo', 'foo/bar' and 'foo/bar::Baz', but not of 'foo/blorp' - """ - base_parts = _splitnode(baseid) - node_parts = _splitnode(nodeid) - if len(node_parts) < len(base_parts): - return False - return node_parts[:len(base_parts)] == base_parts diff --git a/third_party/python/pytest/_pytest/skipping.py b/third_party/python/pytest/_pytest/skipping.py deleted file mode 100644 index b92800d10b45..000000000000 --- a/third_party/python/pytest/_pytest/skipping.py +++ /dev/null @@ -1,372 +0,0 @@ -""" support for skip/xfail functions and markers. """ -from __future__ import absolute_import, division, print_function - -import os -import sys -import traceback - -import py -from _pytest.config import hookimpl -from _pytest.mark import MarkInfo, MarkDecorator -from _pytest.outcomes import fail, skip, xfail, TEST_OUTCOME - - -def pytest_addoption(parser): - group = parser.getgroup("general") - group.addoption('--runxfail', - action="store_true", dest="runxfail", default=False, - help="run tests even if they are marked xfail") - - parser.addini("xfail_strict", "default for the strict parameter of xfail " - "markers when not given explicitly (default: " - "False)", - default=False, - type="bool") - - -def pytest_configure(config): - if config.option.runxfail: - # yay a hack - import pytest - old = pytest.xfail - config._cleanup.append(lambda: setattr(pytest, "xfail", old)) - - def nop(*args, **kwargs): - pass - - nop.Exception = xfail.Exception - setattr(pytest, "xfail", nop) - - config.addinivalue_line("markers", - "skip(reason=None): skip the given test function with an optional reason. " - "Example: skip(reason=\"no way of currently testing this\") skips the " - "test." - ) - config.addinivalue_line("markers", - "skipif(condition): skip the given test function if eval(condition) " - "results in a True value. Evaluation happens within the " - "module global context. Example: skipif('sys.platform == \"win32\"') " - "skips the test if we are on the win32 platform. see " - "http://pytest.org/latest/skipping.html" - ) - config.addinivalue_line("markers", - "xfail(condition, reason=None, run=True, raises=None, strict=False): " - "mark the test function as an expected failure if eval(condition) " - "has a True value. Optionally specify a reason for better reporting " - "and run=False if you don't even want to execute the test function. " - "If only specific exception(s) are expected, you can list them in " - "raises, and if the test fails in other ways, it will be reported as " - "a true failure. See http://pytest.org/latest/skipping.html" - ) - - -class MarkEvaluator: - def __init__(self, item, name): - self.item = item - self.name = name - - @property - def holder(self): - return self.item.keywords.get(self.name) - - def __bool__(self): - return bool(self.holder) - __nonzero__ = __bool__ - - def wasvalid(self): - return not hasattr(self, 'exc') - - def invalidraise(self, exc): - raises = self.get('raises') - if not raises: - return - return not isinstance(exc, raises) - - def istrue(self): - try: - return self._istrue() - except TEST_OUTCOME: - self.exc = sys.exc_info() - if isinstance(self.exc[1], SyntaxError): - msg = [" " * (self.exc[1].offset + 4) + "^", ] - msg.append("SyntaxError: invalid syntax") - else: - msg = traceback.format_exception_only(*self.exc[:2]) - fail("Error evaluating %r expression\n" - " %s\n" - "%s" - % (self.name, self.expr, "\n".join(msg)), - pytrace=False) - - def _getglobals(self): - d = {'os': os, 'sys': sys, 'config': self.item.config} - if hasattr(self.item, 'obj'): - d.update(self.item.obj.__globals__) - return d - - def _istrue(self): - if hasattr(self, 'result'): - return self.result - if self.holder: - if self.holder.args or 'condition' in self.holder.kwargs: - self.result = False - # "holder" might be a MarkInfo or a MarkDecorator; only - # MarkInfo keeps track of all parameters it received in an - # _arglist attribute - marks = getattr(self.holder, '_marks', None) \ - or [self.holder.mark] - for _, args, kwargs in marks: - if 'condition' in kwargs: - args = (kwargs['condition'],) - for expr in args: - self.expr = expr - if isinstance(expr, py.builtin._basestring): - d = self._getglobals() - result = cached_eval(self.item.config, expr, d) - else: - if "reason" not in kwargs: - # XXX better be checked at collection time - msg = "you need to specify reason=STRING " \ - "when using booleans as conditions." - fail(msg) - result = bool(expr) - if result: - self.result = True - self.reason = kwargs.get('reason', None) - self.expr = expr - return self.result - else: - self.result = True - return getattr(self, 'result', False) - - def get(self, attr, default=None): - return self.holder.kwargs.get(attr, default) - - def getexplanation(self): - expl = getattr(self, 'reason', None) or self.get('reason', None) - if not expl: - if not hasattr(self, 'expr'): - return "" - else: - return "condition: " + str(self.expr) - return expl - - -@hookimpl(tryfirst=True) -def pytest_runtest_setup(item): - # Check if skip or skipif are specified as pytest marks - - skipif_info = item.keywords.get('skipif') - if isinstance(skipif_info, (MarkInfo, MarkDecorator)): - eval_skipif = MarkEvaluator(item, 'skipif') - if eval_skipif.istrue(): - item._evalskip = eval_skipif - skip(eval_skipif.getexplanation()) - - skip_info = item.keywords.get('skip') - if isinstance(skip_info, (MarkInfo, MarkDecorator)): - item._evalskip = True - if 'reason' in skip_info.kwargs: - skip(skip_info.kwargs['reason']) - elif skip_info.args: - skip(skip_info.args[0]) - else: - skip("unconditional skip") - - item._evalxfail = MarkEvaluator(item, 'xfail') - check_xfail_no_run(item) - - -@hookimpl(hookwrapper=True) -def pytest_pyfunc_call(pyfuncitem): - check_xfail_no_run(pyfuncitem) - outcome = yield - passed = outcome.excinfo is None - if passed: - check_strict_xfail(pyfuncitem) - - -def check_xfail_no_run(item): - """check xfail(run=False)""" - if not item.config.option.runxfail: - evalxfail = item._evalxfail - if evalxfail.istrue(): - if not evalxfail.get('run', True): - xfail("[NOTRUN] " + evalxfail.getexplanation()) - - -def check_strict_xfail(pyfuncitem): - """check xfail(strict=True) for the given PASSING test""" - evalxfail = pyfuncitem._evalxfail - if evalxfail.istrue(): - strict_default = pyfuncitem.config.getini('xfail_strict') - is_strict_xfail = evalxfail.get('strict', strict_default) - if is_strict_xfail: - del pyfuncitem._evalxfail - explanation = evalxfail.getexplanation() - fail('[XPASS(strict)] ' + explanation, pytrace=False) - - -@hookimpl(hookwrapper=True) -def pytest_runtest_makereport(item, call): - outcome = yield - rep = outcome.get_result() - evalxfail = getattr(item, '_evalxfail', None) - evalskip = getattr(item, '_evalskip', None) - # unitttest special case, see setting of _unexpectedsuccess - if hasattr(item, '_unexpectedsuccess') and rep.when == "call": - from _pytest.compat import _is_unittest_unexpected_success_a_failure - if item._unexpectedsuccess: - rep.longrepr = "Unexpected success: {0}".format(item._unexpectedsuccess) - else: - rep.longrepr = "Unexpected success" - if _is_unittest_unexpected_success_a_failure(): - rep.outcome = "failed" - else: - rep.outcome = "passed" - rep.wasxfail = rep.longrepr - elif item.config.option.runxfail: - pass # don't interefere - elif call.excinfo and call.excinfo.errisinstance(xfail.Exception): - rep.wasxfail = "reason: " + call.excinfo.value.msg - rep.outcome = "skipped" - elif evalxfail and not rep.skipped and evalxfail.wasvalid() and \ - evalxfail.istrue(): - if call.excinfo: - if evalxfail.invalidraise(call.excinfo.value): - rep.outcome = "failed" - else: - rep.outcome = "skipped" - rep.wasxfail = evalxfail.getexplanation() - elif call.when == "call": - strict_default = item.config.getini('xfail_strict') - is_strict_xfail = evalxfail.get('strict', strict_default) - explanation = evalxfail.getexplanation() - if is_strict_xfail: - rep.outcome = "failed" - rep.longrepr = "[XPASS(strict)] {0}".format(explanation) - else: - rep.outcome = "passed" - rep.wasxfail = explanation - elif evalskip is not None and rep.skipped and type(rep.longrepr) is tuple: - # skipped by mark.skipif; change the location of the failure - # to point to the item definition, otherwise it will display - # the location of where the skip exception was raised within pytest - filename, line, reason = rep.longrepr - filename, line = item.location[:2] - rep.longrepr = filename, line, reason - -# called by terminalreporter progress reporting - - -def pytest_report_teststatus(report): - if hasattr(report, "wasxfail"): - if report.skipped: - return "xfailed", "x", "xfail" - elif report.passed: - return "xpassed", "X", ("XPASS", {'yellow': True}) - -# called by the terminalreporter instance/plugin - - -def pytest_terminal_summary(terminalreporter): - tr = terminalreporter - if not tr.reportchars: - # for name in "xfailed skipped failed xpassed": - # if not tr.stats.get(name, 0): - # tr.write_line("HINT: use '-r' option to see extra " - # "summary info about tests") - # break - return - - lines = [] - for char in tr.reportchars: - if char == "x": - show_xfailed(terminalreporter, lines) - elif char == "X": - show_xpassed(terminalreporter, lines) - elif char in "fF": - show_simple(terminalreporter, lines, 'failed', "FAIL %s") - elif char in "sS": - show_skipped(terminalreporter, lines) - elif char == "E": - show_simple(terminalreporter, lines, 'error', "ERROR %s") - elif char == 'p': - show_simple(terminalreporter, lines, 'passed', "PASSED %s") - - if lines: - tr._tw.sep("=", "short test summary info") - for line in lines: - tr._tw.line(line) - - -def show_simple(terminalreporter, lines, stat, format): - failed = terminalreporter.stats.get(stat) - if failed: - for rep in failed: - pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid) - lines.append(format % (pos,)) - - -def show_xfailed(terminalreporter, lines): - xfailed = terminalreporter.stats.get("xfailed") - if xfailed: - for rep in xfailed: - pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid) - reason = rep.wasxfail - lines.append("XFAIL %s" % (pos,)) - if reason: - lines.append(" " + str(reason)) - - -def show_xpassed(terminalreporter, lines): - xpassed = terminalreporter.stats.get("xpassed") - if xpassed: - for rep in xpassed: - pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid) - reason = rep.wasxfail - lines.append("XPASS %s %s" % (pos, reason)) - - -def cached_eval(config, expr, d): - if not hasattr(config, '_evalcache'): - config._evalcache = {} - try: - return config._evalcache[expr] - except KeyError: - import _pytest._code - exprcode = _pytest._code.compile(expr, mode="eval") - config._evalcache[expr] = x = eval(exprcode, d) - return x - - -def folded_skips(skipped): - d = {} - for event in skipped: - key = event.longrepr - assert len(key) == 3, (event, key) - d.setdefault(key, []).append(event) - values = [] - for key, events in d.items(): - values.append((len(events),) + key) - return values - - -def show_skipped(terminalreporter, lines): - tr = terminalreporter - skipped = tr.stats.get('skipped', []) - if skipped: - # if not tr.hasopt('skipped'): - # tr.write_line( - # "%d skipped tests, specify -rs for more info" % - # len(skipped)) - # return - fskips = folded_skips(skipped) - if fskips: - # tr.write_sep("_", "skipped test summary") - for num, fspath, lineno, reason in fskips: - if reason.startswith("Skipped: "): - reason = reason[9:] - lines.append( - "SKIP [%d] %s:%d: %s" % - (num, fspath, lineno + 1, reason)) diff --git a/third_party/python/pytest/_pytest/vendored_packages/README.md b/third_party/python/pytest/_pytest/vendored_packages/README.md deleted file mode 100644 index b5fe6febb089..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/README.md +++ /dev/null @@ -1,13 +0,0 @@ -This directory vendors the `pluggy` module. - -For a more detailed discussion for the reasons to vendoring this -package, please see [this issue](https://github.com/pytest-dev/pytest/issues/944). - -To update the current version, execute: - -``` -$ pip install -U pluggy== --no-compile --target=_pytest/vendored_packages -``` - -And commit the modified files. The `pluggy-.dist-info` directory -created by `pip` should be added as well. diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/DESCRIPTION.rst b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/DESCRIPTION.rst deleted file mode 100644 index da0e7a6ed727..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/DESCRIPTION.rst +++ /dev/null @@ -1,11 +0,0 @@ - -Plugin registration and hook calling for Python -=============================================== - -This is the plugin manager as used by pytest but stripped -of pytest specific details. - -During the 0.x series this plugin does not have much documentation -except extensive docstrings in the pluggy.py module. - - diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/INSTALLER b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/INSTALLER deleted file mode 100644 index a1b589e38a32..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/INSTALLER +++ /dev/null @@ -1 +0,0 @@ -pip diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/METADATA b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/METADATA deleted file mode 100644 index bd88517c94df..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/METADATA +++ /dev/null @@ -1,40 +0,0 @@ -Metadata-Version: 2.0 -Name: pluggy -Version: 0.4.0 -Summary: plugin and hook calling mechanisms for python -Home-page: https://github.com/pytest-dev/pluggy -Author: Holger Krekel -Author-email: holger at merlinux.eu -License: MIT license -Platform: unix -Platform: linux -Platform: osx -Platform: win32 -Classifier: Development Status :: 4 - Beta -Classifier: Intended Audience :: Developers -Classifier: License :: OSI Approved :: MIT License -Classifier: Operating System :: POSIX -Classifier: Operating System :: Microsoft :: Windows -Classifier: Operating System :: MacOS :: MacOS X -Classifier: Topic :: Software Development :: Testing -Classifier: Topic :: Software Development :: Libraries -Classifier: Topic :: Utilities -Classifier: Programming Language :: Python :: 2 -Classifier: Programming Language :: Python :: 2.6 -Classifier: Programming Language :: Python :: 2.7 -Classifier: Programming Language :: Python :: 3 -Classifier: Programming Language :: Python :: 3.3 -Classifier: Programming Language :: Python :: 3.4 -Classifier: Programming Language :: Python :: 3.5 - - -Plugin registration and hook calling for Python -=============================================== - -This is the plugin manager as used by pytest but stripped -of pytest specific details. - -During the 0.x series this plugin does not have much documentation -except extensive docstrings in the pluggy.py module. - - diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/RECORD b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/RECORD deleted file mode 100644 index 3003a3bf2b02..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/RECORD +++ /dev/null @@ -1,9 +0,0 @@ -pluggy.py,sha256=u0oG9cv-oLOkNvEBlwnnu8pp1AyxpoERgUO00S3rvpQ,31543 -pluggy-0.4.0.dist-info/DESCRIPTION.rst,sha256=ltvjkFd40LW_xShthp6RRVM6OB_uACYDFR3kTpKw7o4,307 -pluggy-0.4.0.dist-info/LICENSE.txt,sha256=ruwhUOyV1HgE9F35JVL9BCZ9vMSALx369I4xq9rhpkM,1134 -pluggy-0.4.0.dist-info/METADATA,sha256=pe2hbsqKFaLHC6wAQPpFPn0KlpcPfLBe_BnS4O70bfk,1364 -pluggy-0.4.0.dist-info/RECORD,, -pluggy-0.4.0.dist-info/WHEEL,sha256=9Z5Xm-eel1bTS7e6ogYiKz0zmPEqDwIypurdHN1hR40,116 -pluggy-0.4.0.dist-info/metadata.json,sha256=T3go5L2qOa_-H-HpCZi3EoVKb8sZ3R-fOssbkWo2nvM,1119 -pluggy-0.4.0.dist-info/top_level.txt,sha256=xKSCRhai-v9MckvMuWqNz16c1tbsmOggoMSwTgcpYHE,7 -pluggy-0.4.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/WHEEL b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/WHEEL deleted file mode 100644 index 8b6dd1b5a884..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/WHEEL +++ /dev/null @@ -1,6 +0,0 @@ -Wheel-Version: 1.0 -Generator: bdist_wheel (0.29.0) -Root-Is-Purelib: true -Tag: py2-none-any -Tag: py3-none-any - diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/metadata.json b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/metadata.json deleted file mode 100644 index cde22aff02de..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/metadata.json +++ /dev/null @@ -1 +0,0 @@ -{"classifiers": ["Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: POSIX", "Operating System :: Microsoft :: Windows", "Operating System :: MacOS :: MacOS X", "Topic :: Software Development :: Testing", "Topic :: Software Development :: Libraries", "Topic :: Utilities", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5"], "extensions": {"python.details": {"contacts": [{"email": "holger at merlinux.eu", "name": "Holger Krekel", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst", "license": "LICENSE.txt"}, "project_urls": {"Home": "https://github.com/pytest-dev/pluggy"}}}, "generator": "bdist_wheel (0.29.0)", "license": "MIT license", "metadata_version": "2.0", "name": "pluggy", "platform": "unix", "summary": "plugin and hook calling mechanisms for python", "version": "0.4.0"} \ No newline at end of file diff --git a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/top_level.txt b/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/top_level.txt deleted file mode 100644 index 11bdb5c1f5fc..000000000000 --- a/third_party/python/pytest/_pytest/vendored_packages/pluggy-0.4.0.dist-info/top_level.txt +++ /dev/null @@ -1 +0,0 @@ -pluggy diff --git a/third_party/python/pytest/appveyor.yml b/third_party/python/pytest/appveyor.yml index 3a11700e3fe0..b808fa6d91a9 100644 --- a/third_party/python/pytest/appveyor.yml +++ b/third_party/python/pytest/appveyor.yml @@ -10,9 +10,7 @@ environment: - TOXENV: "coveralls" # note: please use "tox --listenvs" to populate the build matrix below - TOXENV: "linting" - - TOXENV: "py26" - TOXENV: "py27" - - TOXENV: "py33" - TOXENV: "py34" - TOXENV: "py35" - TOXENV: "py36" @@ -21,10 +19,12 @@ environment: - TOXENV: "py27-xdist" - TOXENV: "py27-trial" - TOXENV: "py27-numpy" + - TOXENV: "py27-pluggymaster" - TOXENV: "py36-pexpect" - TOXENV: "py36-xdist" - TOXENV: "py36-trial" - TOXENV: "py36-numpy" + - TOXENV: "py36-pluggymaster" - TOXENV: "py27-nobyte" - TOXENV: "doctesting" - TOXENV: "py35-freeze" @@ -42,3 +42,7 @@ build: false # Not a C# project, build stuff at the test step instead. test_script: - call scripts\call-tox.bat + +cache: + - '%LOCALAPPDATA%\pip\cache' + - '%USERPROFILE%\.cache\pre-commit' diff --git a/third_party/python/pytest/bench/bench.py b/third_party/python/pytest/bench/bench.py index ce94964172f0..4e72444e7d7a 100644 --- a/third_party/python/pytest/bench/bench.py +++ b/third_party/python/pytest/bench/bench.py @@ -1,12 +1,13 @@ import sys -if __name__ == '__main__': +if __name__ == "__main__": import cProfile - import pytest + import pytest # NOQA import pstats + script = sys.argv[1:] if len(sys.argv) > 1 else "empty.py" - stats = cProfile.run('pytest.cmdline.main(%r)' % script, 'prof') + stats = cProfile.run("pytest.cmdline.main(%r)" % script, "prof") p = pstats.Stats("prof") p.strip_dirs() - p.sort_stats('cumulative') + p.sort_stats("cumulative") print(p.print_stats(500)) diff --git a/third_party/python/pytest/bench/bench_argcomplete.py b/third_party/python/pytest/bench/bench_argcomplete.py index d66c664f346e..495e2c4ed3b1 100644 --- a/third_party/python/pytest/bench/bench_argcomplete.py +++ b/third_party/python/pytest/bench/bench_argcomplete.py @@ -5,15 +5,18 @@ # FilesCompleter 75.1109 69.2116 # FastFilesCompleter 0.7383 1.0760 +import timeit -if __name__ == '__main__': - import sys - import timeit - from argcomplete.completers import FilesCompleter - from _pytest._argcomplete import FastFilesCompleter - count = 1000 # only a few seconds - setup = 'from __main__ import FastFilesCompleter\nfc = FastFilesCompleter()' - run = 'fc("/d")' - sys.stdout.write('%s\n' % (timeit.timeit(run, - setup=setup.replace('Fast', ''), number=count))) - sys.stdout.write('%s\n' % (timeit.timeit(run, setup=setup, number=count))) +imports = [ + "from argcomplete.completers import FilesCompleter as completer", + "from _pytest._argcomplete import FastFilesCompleter as completer", +] + +count = 1000 # only a few seconds +setup = "%s\nfc = completer()" +run = 'fc("/d")' + + +if __name__ == "__main__": + print(timeit.timeit(run, setup=setup % imports[0], number=count)) + print((timeit.timeit(run, setup=setup % imports[1], number=count))) diff --git a/third_party/python/pytest/bench/empty.py b/third_party/python/pytest/bench/empty.py index ac5e25701da9..b90319936b3e 100644 --- a/third_party/python/pytest/bench/empty.py +++ b/third_party/python/pytest/bench/empty.py @@ -1,3 +1,4 @@ import py + for i in range(1000): py.builtin.exec_("def test_func_%d(): pass" % i) diff --git a/third_party/python/pytest/bench/manyparam.py b/third_party/python/pytest/bench/manyparam.py index d2bca0e8acd1..a25b098de868 100644 --- a/third_party/python/pytest/bench/manyparam.py +++ b/third_party/python/pytest/bench/manyparam.py @@ -1,12 +1,15 @@ import pytest -@pytest.fixture(scope='module', params=range(966)) + +@pytest.fixture(scope="module", params=range(966)) def foo(request): return request.param + def test_it(foo): pass + + def test_it2(foo): pass - diff --git a/third_party/python/pytest/bench/skip.py b/third_party/python/pytest/bench/skip.py index 960b308645a2..b105e79f82eb 100644 --- a/third_party/python/pytest/bench/skip.py +++ b/third_party/python/pytest/bench/skip.py @@ -1,10 +1,11 @@ - +from six.moves import range import pytest SKIP = True -@pytest.mark.parametrize("x", xrange(5000)) + +@pytest.mark.parametrize("x", range(5000)) def test_foo(x): if SKIP: pytest.skip("heh") diff --git a/third_party/python/pytest/changelog/README.rst b/third_party/python/pytest/changelog/README.rst new file mode 100644 index 000000000000..e34bd4da267c --- /dev/null +++ b/third_party/python/pytest/changelog/README.rst @@ -0,0 +1,32 @@ +This directory contains "newsfragments" which are short files that contain a small **ReST**-formatted +text that will be added to the next ``CHANGELOG``. + +The ``CHANGELOG`` will be read by users, so this description should be aimed to pytest users +instead of describing internal changes which are only relevant to the developers. + +Make sure to use full sentences with correct case and punctuation, for example:: + + Fix issue with non-ascii messages from the ``warnings`` module. + +Each file should be named like ``..rst``, where +```` is an issue number, and ```` is one of: + +* ``feature``: new user facing features, like new command-line options and new behavior. +* ``bugfix``: fixes a reported bug. +* ``doc``: documentation improvement, like rewording an entire session or adding missing docs. +* ``removal``: feature deprecation or removal. +* ``vendor``: changes in packages vendored in pytest. +* ``trivial``: fixing a small typo or internal change that might be noteworthy. + +So for example: ``123.feature.rst``, ``456.bugfix.rst``. + +If your PR fixes an issue, use that number here. If there is no issue, +then after you submit the PR and get the PR number you can add a +changelog using that instead. + +If you are not sure what issue type to use, don't hesitate to ask in your PR. + +Note that the ``towncrier`` tool will automatically +reflow your text, so it will work best if you stick to a single paragraph, but multiple sentences and links are OK +and encouraged. You can install ``towncrier`` and then run ``towncrier --draft`` +if you want to get a preview of how your change will look in the final release notes. diff --git a/third_party/python/pytest/doc/en/Makefile b/third_party/python/pytest/doc/en/Makefile index 286bbd8e7603..fa8e8266a29e 100644 --- a/third_party/python/pytest/doc/en/Makefile +++ b/third_party/python/pytest/doc/en/Makefile @@ -13,8 +13,6 @@ PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . REGENDOC_ARGS := \ - --normalize "/={8,} (.*) ={8,}/======= \1 ========/" \ - --normalize "/_{8,} (.*) _{8,}/_______ \1 ________/" \ --normalize "/in \d+.\d+ seconds/in 0.12 seconds/" \ --normalize "@/tmp/pytest-of-.*/pytest-\d+@PYTEST_TMPDIR@" \ --normalize "@pytest-(\d+)\\.[^ ,]+@pytest-\1.x.y@" \ diff --git a/third_party/python/pytest/doc/en/_templates/globaltoc.html b/third_party/python/pytest/doc/en/_templates/globaltoc.html index fdd4dd59b324..0e088d67ef36 100644 --- a/third_party/python/pytest/doc/en/_templates/globaltoc.html +++ b/third_party/python/pytest/doc/en/_templates/globaltoc.html @@ -2,15 +2,16 @@ {%- if display_toc %} diff --git a/third_party/python/pytest/doc/en/_templates/links.html b/third_party/python/pytest/doc/en/_templates/links.html index d855a013f340..6f27757a3483 100644 --- a/third_party/python/pytest/doc/en/_templates/links.html +++ b/third_party/python/pytest/doc/en/_templates/links.html @@ -1,11 +1,8 @@

Useful Links

- diff --git a/third_party/python/pytest/doc/en/_themes/flask/theme.conf b/third_party/python/pytest/doc/en/_themes/flask/theme.conf index 18c720f804c6..372b00283930 100644 --- a/third_party/python/pytest/doc/en/_themes/flask/theme.conf +++ b/third_party/python/pytest/doc/en/_themes/flask/theme.conf @@ -6,4 +6,4 @@ pygments_style = flask_theme_support.FlaskyStyle [options] index_logo = '' index_logo_height = 120px -touch_icon = +touch_icon = diff --git a/third_party/python/pytest/doc/en/_themes/flask_theme_support.py b/third_party/python/pytest/doc/en/_themes/flask_theme_support.py index 33f47449c11d..0dcf53b7542b 100644 --- a/third_party/python/pytest/doc/en/_themes/flask_theme_support.py +++ b/third_party/python/pytest/doc/en/_themes/flask_theme_support.py @@ -1,7 +1,19 @@ # flasky extensions. flasky pygments style based on tango style from pygments.style import Style -from pygments.token import Keyword, Name, Comment, String, Error, \ - Number, Operator, Generic, Whitespace, Punctuation, Other, Literal +from pygments.token import ( + Keyword, + Name, + Comment, + String, + Error, + Number, + Operator, + Generic, + Whitespace, + Punctuation, + Other, + Literal, +) class FlaskyStyle(Style): @@ -10,77 +22,68 @@ class FlaskyStyle(Style): styles = { # No corresponding class for the following: - #Text: "", # class: '' - Whitespace: "underline #f8f8f8", # class: 'w' - Error: "#a40000 border:#ef2929", # class: 'err' - Other: "#000000", # class 'x' - - Comment: "italic #8f5902", # class: 'c' - Comment.Preproc: "noitalic", # class: 'cp' - - Keyword: "bold #004461", # class: 'k' - Keyword.Constant: "bold #004461", # class: 'kc' - Keyword.Declaration: "bold #004461", # class: 'kd' - Keyword.Namespace: "bold #004461", # class: 'kn' - Keyword.Pseudo: "bold #004461", # class: 'kp' - Keyword.Reserved: "bold #004461", # class: 'kr' - Keyword.Type: "bold #004461", # class: 'kt' - - Operator: "#582800", # class: 'o' - Operator.Word: "bold #004461", # class: 'ow' - like keywords - - Punctuation: "bold #000000", # class: 'p' - + # Text: "", # class: '' + Whitespace: "underline #f8f8f8", # class: 'w' + Error: "#a40000 border:#ef2929", # class: 'err' + Other: "#000000", # class 'x' + Comment: "italic #8f5902", # class: 'c' + Comment.Preproc: "noitalic", # class: 'cp' + Keyword: "bold #004461", # class: 'k' + Keyword.Constant: "bold #004461", # class: 'kc' + Keyword.Declaration: "bold #004461", # class: 'kd' + Keyword.Namespace: "bold #004461", # class: 'kn' + Keyword.Pseudo: "bold #004461", # class: 'kp' + Keyword.Reserved: "bold #004461", # class: 'kr' + Keyword.Type: "bold #004461", # class: 'kt' + Operator: "#582800", # class: 'o' + Operator.Word: "bold #004461", # class: 'ow' - like keywords + Punctuation: "bold #000000", # class: 'p' # because special names such as Name.Class, Name.Function, etc. # are not recognized as such later in the parsing, we choose them # to look the same as ordinary variables. - Name: "#000000", # class: 'n' - Name.Attribute: "#c4a000", # class: 'na' - to be revised - Name.Builtin: "#004461", # class: 'nb' - Name.Builtin.Pseudo: "#3465a4", # class: 'bp' - Name.Class: "#000000", # class: 'nc' - to be revised - Name.Constant: "#000000", # class: 'no' - to be revised - Name.Decorator: "#888", # class: 'nd' - to be revised - Name.Entity: "#ce5c00", # class: 'ni' - Name.Exception: "bold #cc0000", # class: 'ne' - Name.Function: "#000000", # class: 'nf' - Name.Property: "#000000", # class: 'py' - Name.Label: "#f57900", # class: 'nl' - Name.Namespace: "#000000", # class: 'nn' - to be revised - Name.Other: "#000000", # class: 'nx' - Name.Tag: "bold #004461", # class: 'nt' - like a keyword - Name.Variable: "#000000", # class: 'nv' - to be revised - Name.Variable.Class: "#000000", # class: 'vc' - to be revised - Name.Variable.Global: "#000000", # class: 'vg' - to be revised - Name.Variable.Instance: "#000000", # class: 'vi' - to be revised - - Number: "#990000", # class: 'm' - - Literal: "#000000", # class: 'l' - Literal.Date: "#000000", # class: 'ld' - - String: "#4e9a06", # class: 's' - String.Backtick: "#4e9a06", # class: 'sb' - String.Char: "#4e9a06", # class: 'sc' - String.Doc: "italic #8f5902", # class: 'sd' - like a comment - String.Double: "#4e9a06", # class: 's2' - String.Escape: "#4e9a06", # class: 'se' - String.Heredoc: "#4e9a06", # class: 'sh' - String.Interpol: "#4e9a06", # class: 'si' - String.Other: "#4e9a06", # class: 'sx' - String.Regex: "#4e9a06", # class: 'sr' - String.Single: "#4e9a06", # class: 's1' - String.Symbol: "#4e9a06", # class: 'ss' - - Generic: "#000000", # class: 'g' - Generic.Deleted: "#a40000", # class: 'gd' - Generic.Emph: "italic #000000", # class: 'ge' - Generic.Error: "#ef2929", # class: 'gr' - Generic.Heading: "bold #000080", # class: 'gh' - Generic.Inserted: "#00A000", # class: 'gi' - Generic.Output: "#888", # class: 'go' - Generic.Prompt: "#745334", # class: 'gp' - Generic.Strong: "bold #000000", # class: 'gs' - Generic.Subheading: "bold #800080", # class: 'gu' - Generic.Traceback: "bold #a40000", # class: 'gt' + Name: "#000000", # class: 'n' + Name.Attribute: "#c4a000", # class: 'na' - to be revised + Name.Builtin: "#004461", # class: 'nb' + Name.Builtin.Pseudo: "#3465a4", # class: 'bp' + Name.Class: "#000000", # class: 'nc' - to be revised + Name.Constant: "#000000", # class: 'no' - to be revised + Name.Decorator: "#888", # class: 'nd' - to be revised + Name.Entity: "#ce5c00", # class: 'ni' + Name.Exception: "bold #cc0000", # class: 'ne' + Name.Function: "#000000", # class: 'nf' + Name.Property: "#000000", # class: 'py' + Name.Label: "#f57900", # class: 'nl' + Name.Namespace: "#000000", # class: 'nn' - to be revised + Name.Other: "#000000", # class: 'nx' + Name.Tag: "bold #004461", # class: 'nt' - like a keyword + Name.Variable: "#000000", # class: 'nv' - to be revised + Name.Variable.Class: "#000000", # class: 'vc' - to be revised + Name.Variable.Global: "#000000", # class: 'vg' - to be revised + Name.Variable.Instance: "#000000", # class: 'vi' - to be revised + Number: "#990000", # class: 'm' + Literal: "#000000", # class: 'l' + Literal.Date: "#000000", # class: 'ld' + String: "#4e9a06", # class: 's' + String.Backtick: "#4e9a06", # class: 'sb' + String.Char: "#4e9a06", # class: 'sc' + String.Doc: "italic #8f5902", # class: 'sd' - like a comment + String.Double: "#4e9a06", # class: 's2' + String.Escape: "#4e9a06", # class: 'se' + String.Heredoc: "#4e9a06", # class: 'sh' + String.Interpol: "#4e9a06", # class: 'si' + String.Other: "#4e9a06", # class: 'sx' + String.Regex: "#4e9a06", # class: 'sr' + String.Single: "#4e9a06", # class: 's1' + String.Symbol: "#4e9a06", # class: 'ss' + Generic: "#000000", # class: 'g' + Generic.Deleted: "#a40000", # class: 'gd' + Generic.Emph: "italic #000000", # class: 'ge' + Generic.Error: "#ef2929", # class: 'gr' + Generic.Heading: "bold #000080", # class: 'gh' + Generic.Inserted: "#00A000", # class: 'gi' + Generic.Output: "#888", # class: 'go' + Generic.Prompt: "#745334", # class: 'gp' + Generic.Strong: "bold #000000", # class: 'gs' + Generic.Subheading: "bold #800080", # class: 'gu' + Generic.Traceback: "bold #a40000", # class: 'gt' } diff --git a/third_party/python/pytest/doc/en/announce/index.rst b/third_party/python/pytest/doc/en/announce/index.rst index d340f5d46b95..107fcd2ad33f 100644 --- a/third_party/python/pytest/doc/en/announce/index.rst +++ b/third_party/python/pytest/doc/en/announce/index.rst @@ -5,7 +5,18 @@ Release announcements .. toctree:: :maxdepth: 2 - + + release-3.6.2 + release-3.6.1 + release-3.6.0 + release-3.5.1 + release-3.5.0 + release-3.4.2 + release-3.4.1 + release-3.4.0 + release-3.3.2 + release-3.3.1 + release-3.3.0 release-3.2.5 release-3.2.4 release-3.2.3 diff --git a/third_party/python/pytest/doc/en/announce/release-2.0.3.rst b/third_party/python/pytest/doc/en/announce/release-2.0.3.rst index 9bbfdaab361a..81d01eb99f9b 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.0.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.0.3.rst @@ -1,4 +1,4 @@ -py.test 2.0.3: bug fixes and speed ups +py.test 2.0.3: bug fixes and speed ups =========================================================================== Welcome to pytest-2.0.3, a maintenance and bug fix release of pytest, @@ -37,4 +37,3 @@ Changes between 2.0.2 and 2.0.3 internally) - fix issue37: avoid invalid characters in junitxml's output - diff --git a/third_party/python/pytest/doc/en/announce/release-2.1.1.rst b/third_party/python/pytest/doc/en/announce/release-2.1.1.rst index ecdd69f4dc93..c2285eba9fa2 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.1.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.1.1.rst @@ -34,4 +34,3 @@ Changes between 2.1.0 and 2.1.1 - fix issue59: provide system-out/err tags for junitxml output - fix issue61: assertion rewriting on boolean operations with 3 or more operands - you can now build a man page with "cd doc ; make man" - diff --git a/third_party/python/pytest/doc/en/announce/release-2.1.2.rst b/third_party/python/pytest/doc/en/announce/release-2.1.2.rst index 51b7591d3667..1975f368a3fc 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.1.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.1.2.rst @@ -30,4 +30,3 @@ Changes between 2.1.1 and 2.1.2 - fix issue68 / packages now work with assertion rewriting - fix issue66: use different assertion rewriting caches when the -O option is passed - don't try assertion rewriting on Jython, use reinterp - diff --git a/third_party/python/pytest/doc/en/announce/release-2.1.3.rst b/third_party/python/pytest/doc/en/announce/release-2.1.3.rst index f4da60b8ba4f..a43bc058c178 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.1.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.1.3.rst @@ -9,7 +9,7 @@ and integration testing. See extensive docs with examples here: The release contains another fix to the perfected assertions introduced with the 2.1 series as well as the new possibility to customize reporting -for assertion expressions on a per-directory level. +for assertion expressions on a per-directory level. If you want to install or upgrade pytest, just type one of:: diff --git a/third_party/python/pytest/doc/en/announce/release-2.2.1.rst b/third_party/python/pytest/doc/en/announce/release-2.2.1.rst index 5d28bcb01f4a..44281597eab0 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.2.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.2.1.rst @@ -27,7 +27,7 @@ Changes between 2.2.0 and 2.2.1 ---------------------------------------- - fix issue99 (in pytest and py) internallerrors with resultlog now - produce better output - fixed by normalizing pytest_internalerror + produce better output - fixed by normalizing pytest_internalerror input arguments. - fix issue97 / traceback issues (in pytest and py) improve traceback output in conjunction with jinja2 and cython which hack tracebacks @@ -35,7 +35,7 @@ Changes between 2.2.0 and 2.2.1 the final test in a test node will now run its teardown directly instead of waiting for the end of the session. Thanks Dave Hunt for the good reporting and feedback. The pytest_runtest_protocol as well - as the pytest_runtest_teardown hooks now have "nextitem" available + as the pytest_runtest_teardown hooks now have "nextitem" available which will be None indicating the end of the test run. - fix collection crash due to unknown-source collected items, thanks to Ralf Schmitt (fixed by depending on a more recent pylib) diff --git a/third_party/python/pytest/doc/en/announce/release-2.2.2.rst b/third_party/python/pytest/doc/en/announce/release-2.2.2.rst index 733aedec4135..22ef0bc7a166 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.2.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.2.2.rst @@ -4,7 +4,7 @@ pytest-2.2.2: bug fixes pytest-2.2.2 (updated to 2.2.3 to fix packaging issues) is a minor backward-compatible release of the versatile py.test testing tool. It contains bug fixes and a few refinements particularly to reporting with -"--collectonly", see below for betails. +"--collectonly", see below for betails. For general information see here: @@ -27,7 +27,7 @@ Changes between 2.2.1 and 2.2.2 - fix issue101: wrong args to unittest.TestCase test function now produce better output -- fix issue102: report more useful errors and hints for when a +- fix issue102: report more useful errors and hints for when a test directory was renamed and some pyc/__pycache__ remain - fix issue106: allow parametrize to be applied multiple times e.g. from module, class and at function level. @@ -38,6 +38,6 @@ Changes between 2.2.1 and 2.2.2 - fix issue115: make --collectonly robust against early failure (missing files/directories) - "-qq --collectonly" now shows only files and the number of tests in them -- "-q --collectonly" now shows test ids +- "-q --collectonly" now shows test ids - allow adding of attributes to test reports such that it also works with distributed testing (no upgrade of pytest-xdist needed) diff --git a/third_party/python/pytest/doc/en/announce/release-2.2.4.rst b/third_party/python/pytest/doc/en/announce/release-2.2.4.rst index 67f0feb27c7f..a8fb9b93c5b0 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.2.4.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.2.4.rst @@ -36,4 +36,3 @@ Changes between 2.2.3 and 2.2.4 configure/sessionstart where called - fix issue #144: better mangle test ids to junitxml classnames - upgrade distribute_setup.py to 0.6.27 - diff --git a/third_party/python/pytest/doc/en/announce/release-2.3.0.rst b/third_party/python/pytest/doc/en/announce/release-2.3.0.rst index f863aad0ace9..061aa025c1e4 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.3.0.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.3.0.rst @@ -1,7 +1,7 @@ pytest-2.3: improved fixtures / better unittest integration ============================================================================= -pytest-2.3 comes with many major improvements for fixture/funcarg management +pytest-2.3 comes with many major improvements for fixture/funcarg management and parametrized testing in Python. It is now easier, more efficient and more predicatable to re-run the same tests with different fixture instances. Also, you can directly declare the caching "scope" of @@ -9,7 +9,7 @@ fixtures so that dependent tests throughout your whole test suite can re-use database or other expensive fixture objects with ease. Lastly, it's possible for fixture functions (formerly known as funcarg factories) to use other fixtures, allowing for a completely modular and -re-useable fixture design. +re-useable fixture design. For detailed info and tutorial-style examples, see: @@ -27,7 +27,7 @@ All changes are backward compatible and you should be able to continue to run your test suites and 3rd party plugins that worked with pytest-2.2.4. -If you are interested in the precise reasoning (including examples) of the +If you are interested in the precise reasoning (including examples) of the pytest-2.3 fixture evolution, please consult http://pytest.org/latest/funcarg_compare.html @@ -43,7 +43,7 @@ and more details for those already in the knowing of pytest can be found in the CHANGELOG below. Particular thanks for this release go to Floris Bruynooghe, Alex Okrushko -Carl Meyer, Ronny Pfannschmidt, Benjamin Peterson and Alex Gaynor for helping +Carl Meyer, Ronny Pfannschmidt, Benjamin Peterson and Alex Gaynor for helping to get the new features right and well integrated. Ronny and Floris also helped to fix a number of bugs and yet more people helped by providing bug reports. @@ -94,7 +94,7 @@ Changes between 2.2.4 and 2.3.0 - pluginmanager.register(...) now raises ValueError if the plugin has been already registered or the name is taken -- fix issue159: improve http://pytest.org/latest/faq.html +- fix issue159: improve http://pytest.org/latest/faq.html especially with respect to the "magic" history, also mention pytest-django, trial and unittest integration. @@ -125,10 +125,9 @@ Changes between 2.2.4 and 2.3.0 you can use startdir.bestrelpath(yourpath) to show nice relative path - - allow plugins to implement both pytest_report_header and + - allow plugins to implement both pytest_report_header and pytest_sessionstart (sessionstart is invoked first). - don't show deselected reason line if there is none - py.test -vv will show all of assert comparisons instead of truncating - diff --git a/third_party/python/pytest/doc/en/announce/release-2.3.1.rst b/third_party/python/pytest/doc/en/announce/release-2.3.1.rst index b787dc203f44..6f8770b34515 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.3.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.3.1.rst @@ -3,16 +3,16 @@ pytest-2.3.1: fix regression with factory functions pytest-2.3.1 is a quick follow-up release: -- fix issue202 - regression with fixture functions/funcarg factories: - using "self" is now safe again and works as in 2.2.4. Thanks +- fix issue202 - regression with fixture functions/funcarg factories: + using "self" is now safe again and works as in 2.2.4. Thanks to Eduard Schettino for the quick bug report. -- disable pexpect pytest self tests on Freebsd - thanks Koob for the +- disable pexpect pytest self tests on Freebsd - thanks Koob for the quick reporting - fix/improve interactive docs with --markers -See +See http://pytest.org/ diff --git a/third_party/python/pytest/doc/en/announce/release-2.3.2.rst b/third_party/python/pytest/doc/en/announce/release-2.3.2.rst index 75312b429cdd..484feaaa5a26 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.3.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.3.2.rst @@ -8,9 +8,9 @@ pytest-2.3.2 is another stabilization release: - fix teardown-ordering for parametrized setups - fix unittest and trial compat behaviour with respect to runTest() methods - issue 206 and others: some improvements to packaging -- fix issue127 and others: improve some docs +- fix issue127 and others: improve some docs -See +See http://pytest.org/ @@ -26,7 +26,7 @@ holger krekel Changes between 2.3.1 and 2.3.2 ----------------------------------- -- fix issue208 and fix issue29 use new py version to avoid long pauses +- fix issue208 and fix issue29 use new py version to avoid long pauses when printing tracebacks in long modules - fix issue205 - conftests in subdirs customizing diff --git a/third_party/python/pytest/doc/en/announce/release-2.3.3.rst b/third_party/python/pytest/doc/en/announce/release-2.3.3.rst index 3a48b6ac4baf..0cb598a426a1 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.3.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.3.3.rst @@ -6,7 +6,7 @@ which offers uebersimple assertions, scalable fixture mechanisms and deep customization for testing with Python. Particularly, this release provides: -- integration fixes and improvements related to flask, numpy, nose, +- integration fixes and improvements related to flask, numpy, nose, unittest, mock - makes pytest work on py24 again (yes, people sometimes still need to use it) @@ -16,7 +16,7 @@ this release provides: Thanks to Manuel Jacob, Thomas Waldmann, Ronny Pfannschmidt, Pavel Repin and Andreas Taumoefolau for providing patches and all for the issues. -See +See http://pytest.org/ @@ -59,4 +59,3 @@ Changes between 2.3.2 and 2.3.3 - fix issue127 - improve documentation for pytest_addoption() and add a ``config.getoption(name)`` helper function for consistency. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.3.4.rst b/third_party/python/pytest/doc/en/announce/release-2.3.4.rst index d6c597b54890..e2e8cb143a33 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.3.4.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.3.4.rst @@ -10,15 +10,15 @@ comes with the following fixes and features: can write: -k "name1 or name2" etc. This is a slight usage incompatibility if you used special syntax like "TestClass.test_method" which you now need to write as -k "TestClass and test_method" to match a certain - method in a certain test class. + method in a certain test class. - allow to dynamically define markers via item.keywords[...]=assignment integrating with "-m" option -- yielded test functions will now have autouse-fixtures active but +- yielded test functions will now have autouse-fixtures active but cannot accept fixtures as funcargs - it's anyway recommended to rather use the post-2.0 parametrize features instead of yield, see: http://pytest.org/latest/example/parametrize.html - fix autouse-issue where autouse-fixtures would not be discovered - if defined in a a/conftest.py file and tests in a/tests/test_some.py + if defined in an a/conftest.py file and tests in a/tests/test_some.py - fix issue226 - LIFO ordering for fixture teardowns - fix issue224 - invocations with >256 char arguments now work - fix issue91 - add/discuss package/directory level setups in example @@ -26,7 +26,7 @@ comes with the following fixes and features: Thanks in particular to Thomas Waldmann for spotting and reporting issues. -See +See http://pytest.org/ diff --git a/third_party/python/pytest/doc/en/announce/release-2.3.5.rst b/third_party/python/pytest/doc/en/announce/release-2.3.5.rst index 112399ef3ca7..465dd826ed47 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.3.5.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.3.5.rst @@ -13,8 +13,8 @@ few interesting new plugins saw the light last month: - pytest-random: randomize test ordering And several others like pytest-django saw maintenance releases. -For a more complete list, check out -https://pypi.python.org/pypi?%3Aaction=search&term=pytest&submit=search. +For a more complete list, check out +https://pypi.org/search/?q=pytest For general information see: @@ -81,7 +81,7 @@ Changes between 2.3.4 and 2.3.5 - fix bug where using capsys with pytest.set_trace() in a test function would break when looking at capsys.readouterr() -- allow to specify prefixes starting with "_" when +- allow to specify prefixes starting with "_" when customizing python_functions test discovery. (thanks Graham Horler) - improve PYTEST_DEBUG tracing output by putting @@ -94,4 +94,3 @@ Changes between 2.3.4 and 2.3.5 - fix issue134 - print the collect errors that prevent running specified test items - fix issue266 - accept unicode in MarkEvaluator expressions - diff --git a/third_party/python/pytest/doc/en/announce/release-2.4.0.rst b/third_party/python/pytest/doc/en/announce/release-2.4.0.rst index be3aaedb09ff..25f6254c5299 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.4.0.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.4.0.rst @@ -1,9 +1,9 @@ pytest-2.4.0: new fixture features/hooks and bug fixes =========================================================================== -The just released pytest-2.4.0 brings many improvements and numerous +The just released pytest-2.4.0 brings many improvements and numerous bug fixes while remaining plugin- and test-suite compatible apart -from a few supposedly very minor incompatibilities. See below for +from a few supposedly very minor incompatibilities. See below for a full list of details. A few feature highlights: - new yield-style fixtures `pytest.yield_fixture @@ -13,7 +13,7 @@ a full list of details. A few feature highlights: - improved pdb support: ``import pdb ; pdb.set_trace()`` now works without requiring prior disabling of stdout/stderr capturing. Also the ``--pdb`` options works now on collection and internal errors - and we introduced a new experimental hook for IDEs/plugins to + and we introduced a new experimental hook for IDEs/plugins to intercept debugging: ``pytest_exception_interact(node, call, report)``. - shorter monkeypatch variant to allow specifying an import path as @@ -23,21 +23,21 @@ a full list of details. A few feature highlights: called if the corresponding setup method succeeded. - integrate tab-completion on command line options if you - have `argcomplete `_ + have `argcomplete `_ configured. - allow boolean expression directly with skipif/xfail if a "reason" is also specified. - a new hook ``pytest_load_initial_conftests`` allows plugins like - `pytest-django `_ to + `pytest-django `_ to influence the environment before conftest files import ``django``. - reporting: color the last line red or green depending if failures/errors occurred or everything passed. -The documentation has been updated to accommodate the changes, -see `http://pytest.org `_ +The documentation has been updated to accommodate the changes, +see `http://pytest.org `_ To install or upgrade pytest:: @@ -45,8 +45,8 @@ To install or upgrade pytest:: easy_install -U pytest -**Many thanks to all who helped, including Floris Bruynooghe, -Brianna Laugher, Andreas Pelme, Anthon van der Neut, Anatoly Bubenkoff, +**Many thanks to all who helped, including Floris Bruynooghe, +Brianna Laugher, Andreas Pelme, Anthon van der Neut, Anatoly Bubenkoff, Vladimir Keleshev, Mathieu Agopian, Ronny Pfannschmidt, Christian Theunert and many others.** @@ -101,12 +101,12 @@ new features: - make "import pdb ; pdb.set_trace()" work natively wrt capturing (no "-s" needed anymore), making ``pytest.set_trace()`` a mere shortcut. -- fix issue181: --pdb now also works on collect errors (and - on internal errors) . This was implemented by a slight internal - refactoring and the introduction of a new hook +- fix issue181: --pdb now also works on collect errors (and + on internal errors) . This was implemented by a slight internal + refactoring and the introduction of a new hook ``pytest_exception_interact`` hook (see next item). -- fix issue341: introduce new experimental hook for IDEs/terminals to +- fix issue341: introduce new experimental hook for IDEs/terminals to intercept debugging: ``pytest_exception_interact(node, call, report)``. - new monkeypatch.setattr() variant to provide a shorter @@ -124,7 +124,7 @@ new features: phase of a node. - simplify pytest.mark.parametrize() signature: allow to pass a - CSV-separated string to specify argnames. For example: + CSV-separated string to specify argnames. For example: ``pytest.mark.parametrize("input,expected", [(1,2), (2,3)])`` works as well as the previous: ``pytest.mark.parametrize(("input", "expected"), ...)``. @@ -149,10 +149,10 @@ new features: Bug fixes: -- fix issue358 - capturing options are now parsed more properly +- fix issue358 - capturing options are now parsed more properly by using a new parser.parse_known_args method. -- pytest now uses argparse instead of optparse (thanks Anthon) which +- pytest now uses argparse instead of optparse (thanks Anthon) which means that "argparse" is added as a dependency if installing into python2.6 environments or below. @@ -193,7 +193,7 @@ Bug fixes: - fix issue323 - sorting of many module-scoped arg parametrizations - make sessionfinish hooks execute with the same cwd-context as at - session start (helps fix plugin behaviour which write output files + session start (helps fix plugin behaviour which write output files with relative path such as pytest-cov) - fix issue316 - properly reference collection hooks in docs @@ -201,7 +201,7 @@ Bug fixes: - fix issue 306 - cleanup of -k/-m options to only match markers/test names/keywords respectively. Thanks Wouter van Ackooy. -- improved doctest counting for doctests in python modules -- +- improved doctest counting for doctests in python modules -- files without any doctest items will not show up anymore and doctest examples are counted as separate test items. thanks Danilo Bellini. @@ -211,7 +211,7 @@ Bug fixes: mode. Thanks Jason R. Coombs. - fix junitxml generation when test output contains control characters, - addressing issue267, thanks Jaap Broekhuizen + addressing issue267, thanks Jaap Broekhuizen - fix issue338: honor --tb style for setup/teardown errors as well. Thanks Maho. @@ -220,6 +220,5 @@ Bug fixes: - better parametrize error messages, thanks Brianna Laugher - pytest_terminal_summary(terminalreporter) hooks can now use - ".section(title)" and ".line(msg)" methods to print extra + ".section(title)" and ".line(msg)" methods to print extra information at the end of a test run. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.4.1.rst b/third_party/python/pytest/doc/en/announce/release-2.4.1.rst index 64ba170f8977..308df6bdc40a 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.4.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.4.1.rst @@ -8,7 +8,7 @@ compared to 2.3.5 before they hit more people: "type" keyword should also be converted to the respective types. thanks Floris Bruynooghe, @dnozay. (fixes issue360 and issue362) -- fix dotted filename completion when using argcomplete +- fix dotted filename completion when using argcomplete thanks Anthon van der Neuth. (fixes issue361) - fix regression when a 1-tuple ("arg",) is used for specifying diff --git a/third_party/python/pytest/doc/en/announce/release-2.4.2.rst b/third_party/python/pytest/doc/en/announce/release-2.4.2.rst index 3b4aa95abbcc..ab08b72aaf71 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.4.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.4.2.rst @@ -26,9 +26,9 @@ pytest-2.4.2 is another bug-fixing release: - remove attempt to "dup" stdout at startup as it's icky. the normal capturing should catch enough possibilities - of tests messing up standard FDs. + of tests messing up standard FDs. -- add pluginmanager.do_configure(config) as a link to +- add pluginmanager.do_configure(config) as a link to config.do_configure() for plugin-compatibility as usual, docs at http://pytest.org and upgrades via:: diff --git a/third_party/python/pytest/doc/en/announce/release-2.5.0.rst b/third_party/python/pytest/doc/en/announce/release-2.5.0.rst index b04a825cd8e4..29064e05e6db 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.5.0.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.5.0.rst @@ -4,7 +4,7 @@ pytest-2.5.0: now down to ZERO reported bugs! pytest-2.5.0 is a big fixing release, the result of two community bug fixing days plus numerous additional works from many people and reporters. The release should be fully compatible to 2.4.2, existing -plugins and test suites. We aim at maintaining this level of ZERO reported +plugins and test suites. We aim at maintaining this level of ZERO reported bugs because it's no fun if your testing tool has bugs, is it? Under a condition, though: when submitting a bug report please provide clear information about the circumstances and a simple example which @@ -17,12 +17,12 @@ help. For those who use older Python versions, please note that pytest is not automatically tested on python2.5 due to virtualenv, setuptools and tox not supporting it anymore. Manual verification shows that it mostly -works fine but it's not going to be part of the automated release +works fine but it's not going to be part of the automated release process and thus likely to break in the future. -As usual, current docs are at +As usual, current docs are at - http://pytest.org + http://pytest.org and you can upgrade from pypi via:: @@ -40,28 +40,28 @@ holger krekel 2.5.0 ----------------------------------- -- dropped python2.5 from automated release testing of pytest itself - which means it's probably going to break soon (but still works +- dropped python2.5 from automated release testing of pytest itself + which means it's probably going to break soon (but still works with this release we believe). - simplified and fixed implementation for calling finalizers when - parametrized fixtures or function arguments are involved. finalization + parametrized fixtures or function arguments are involved. finalization is now performed lazily at setup time instead of in the "teardown phase". - While this might sound odd at first, it helps to ensure that we are + While this might sound odd at first, it helps to ensure that we are correctly handling setup/teardown even in complex code. User-level code should not be affected unless it's implementing the pytest_runtest_teardown hook and expecting certain fixture instances are torn down within (very unlikely and would have been unreliable anyway). -- PR90: add --color=yes|no|auto option to force terminal coloring +- PR90: add --color=yes|no|auto option to force terminal coloring mode ("auto" is default). Thanks Marc Abramowitz. - fix issue319 - correctly show unicode in assertion errors. Many thanks to Floris Bruynooghe for the complete PR. Also means we depend on py>=1.4.19 now. -- fix issue396 - correctly sort and finalize class-scoped parametrized - tests independently from number of methods on the class. +- fix issue396 - correctly sort and finalize class-scoped parametrized + tests independently from number of methods on the class. - refix issue323 in a better way -- parametrization should now never cause Runtime Recursion errors because the underlying algorithm @@ -70,18 +70,18 @@ holger krekel to problems for more than >966 non-function scoped parameters). - fix issue290 - there is preliminary support now for parametrizing - with repeated same values (sometimes useful to test if calling + with repeated same values (sometimes useful to test if calling a second time works as with the first time). - close issue240 - document precisely how pytest module importing - works, discuss the two common test directory layouts, and how it + works, discuss the two common test directory layouts, and how it interacts with PEP420-namespace packages. - fix issue246 fix finalizer order to be LIFO on independent fixtures - depending on a parametrized higher-than-function scoped fixture. + depending on a parametrized higher-than-function scoped fixture. (was quite some effort so please bear with the complexity of this sentence :) Thanks Ralph Schmitt for the precise failure example. - + - fix issue244 by implementing special index for parameters to only use indices for paramentrized test ids @@ -99,9 +99,9 @@ holger krekel filtering with simple strings that are not valid python expressions. Examples: "-k 1.3" matches all tests parametrized with 1.3. "-k None" filters all tests that have "None" in their name - and conversely "-k 'not None'". + and conversely "-k 'not None'". Previously these examples would raise syntax errors. - + - fix issue384 by removing the trial support code since the unittest compat enhancements allow trial to handle it on its own @@ -109,7 +109,7 @@ holger krekel - don't hide an ImportError when importing a plugin produces one. fixes issue375. -- fix issue275 - allow usefixtures and autouse fixtures +- fix issue275 - allow usefixtures and autouse fixtures for running doctest text files. - fix issue380 by making --resultlog only rely on longrepr instead @@ -135,20 +135,20 @@ holger krekel (it already did neutralize pytest.mark.xfail markers) - refine pytest / pkg_resources interactions: The AssertionRewritingHook - PEP302 compliant loader now registers itself with setuptools/pkg_resources + PEP302 compliant loader now registers itself with setuptools/pkg_resources properly so that the pkg_resources.resource_stream method works properly. Fixes issue366. Thanks for the investigations and full PR to Jason R. Coombs. - pytestconfig fixture is now session-scoped as it is the same object during the - whole test run. Fixes issue370. + whole test run. Fixes issue370. - avoid one surprising case of marker malfunction/confusion:: - + @pytest.mark.some(lambda arg: ...) def test_function(): - would not work correctly because pytest assumes @pytest.mark.some - gets a function to be decorated already. We now at least detect if this + would not work correctly because pytest assumes @pytest.mark.some + gets a function to be decorated already. We now at least detect if this arg is a lambda and thus the example will work. Thanks Alex Gaynor for bringing it up. @@ -159,11 +159,11 @@ holger krekel although it's not needed by pytest itself atm. Also fix caching. Fixes issue376. -- fix issue221 - handle importing of namespace-package with no +- fix issue221 - handle importing of namespace-package with no __init__.py properly. - refactor internal FixtureRequest handling to avoid monkeypatching. - One of the positive user-facing effects is that the "request" object + One of the positive user-facing effects is that the "request" object can now be used in closures. - fixed version comparison in pytest.importskip(modname, minverstring) @@ -172,4 +172,3 @@ holger krekel does not duplicate the unittest-API into the "plain" namespace. - fix verbose reporting for @mock'd test functions - diff --git a/third_party/python/pytest/doc/en/announce/release-2.5.1.rst b/third_party/python/pytest/doc/en/announce/release-2.5.1.rst index a3a74cec6265..22e69a836b94 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.5.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.5.1.rst @@ -1,8 +1,8 @@ pytest-2.5.1: fixes and new home page styling =========================================================================== -pytest is a mature Python testing tool with more than a 1000 tests -against itself, passing on many different interpreters and platforms. +pytest is a mature Python testing tool with more than a 1000 tests +against itself, passing on many different interpreters and platforms. The 2.5.1 release maintains the "zero-reported-bugs" promise by fixing the three bugs reported since the last release a few days ago. It also @@ -11,12 +11,12 @@ the flask theme from Armin Ronacher: http://pytest.org -If you have anything more to improve styling and docs, +If you have anything more to improve styling and docs, we'd be very happy to merge further pull requests. On the coding side, the release also contains a little enhancement to fixture decorators allowing to directly influence generation of test -ids, thanks to Floris Bruynooghe. Other thanks for helping with +ids, thanks to Floris Bruynooghe. Other thanks for helping with this release go to Anatoly Bubenkoff and Ronny Pfannschmidt. As usual, you can upgrade from pypi via:: @@ -37,11 +37,10 @@ holger krekel - Allow parameterized fixtures to specify the ID of the parameters by adding an ids argument to pytest.fixture() and pytest.yield_fixture(). - Thanks Floris Bruynooghe. + Thanks Floris Bruynooghe. - fix issue404 by always using the binary xml escape in the junitxml plugin. Thanks Ronny Pfannschmidt. - fix issue407: fix addoption docstring to point to argparse instead of optparse. Thanks Daniel D. Wright. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.5.2.rst b/third_party/python/pytest/doc/en/announce/release-2.5.2.rst index d5cfca2dbdad..c389f5f54038 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.5.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.5.2.rst @@ -1,8 +1,8 @@ -pytest-2.5.2: fixes +pytest-2.5.2: fixes =========================================================================== -pytest is a mature Python testing tool with more than a 1000 tests -against itself, passing on many different interpreters and platforms. +pytest is a mature Python testing tool with more than a 1000 tests +against itself, passing on many different interpreters and platforms. The 2.5.2 release fixes a few bugs with two maybe-bugs remaining and actively being worked on (and waiting for the bug reporter's input). @@ -19,18 +19,18 @@ As usual, you can upgrade from pypi via:: Thanks to the following people who contributed to this release: - Anatoly Bubenkov + Anatoly Bubenkov Ronny Pfannschmidt Floris Bruynooghe - Bruno Oliveira - Andreas Pelme + Bruno Oliveira + Andreas Pelme Jurko Gospodnetić - Piotr Banaszkiewicz - Simon Liedtke - lakka - Lukasz Balcerzak - Philippe Muller - Daniel Hahler + Piotr Banaszkiewicz + Simon Liedtke + lakka + Lukasz Balcerzak + Philippe Muller + Daniel Hahler have fun, holger krekel @@ -39,11 +39,11 @@ holger krekel ----------------------------------- - fix issue409 -- better interoperate with cx_freeze by not - trying to import from collections.abc which causes problems + trying to import from collections.abc which causes problems for py27/cx_freeze. Thanks Wolfgang L. for reporting and tracking it down. - fixed docs and code to use "pytest" instead of "py.test" almost everywhere. - Thanks Jurko Gospodnetic for the complete PR. + Thanks Jurko Gospodnetic for the complete PR. - fix issue425: mention at end of "py.test -h" that --markers and --fixtures work according to specified test path (or current dir) @@ -54,11 +54,10 @@ holger krekel - copy, cleanup and integrate py.io capture from pylib 1.4.20.dev2 (rev 13d9af95547e) - + - address issue416: clarify docs as to conftest.py loading semantics - fix issue429: comparing byte strings with non-ascii chars in assert expressions now work better. Thanks Floris Bruynooghe. - make capfd/capsys.capture private, its unused and shouldn't be exposed - diff --git a/third_party/python/pytest/doc/en/announce/release-2.6.1.rst b/third_party/python/pytest/doc/en/announce/release-2.6.1.rst index 6f27c5861cae..fba6f2993a5c 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.6.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.6.1.rst @@ -52,8 +52,7 @@ Changes 2.6.1 "::" node id specifications (copy pasted from "-v" output) - fix issue544 by only removing "@NUM" at the end of "::" separated parts - and if the part has an ".py" extension + and if the part has a ".py" extension - don't use py.std import helper, rather import things directly. Thanks Bruno Oliveira. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.6.2.rst b/third_party/python/pytest/doc/en/announce/release-2.6.2.rst index 4efc73a4eaad..f6ce178a1070 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.6.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.6.2.rst @@ -49,4 +49,3 @@ holger krekel - Do not mark as universal wheel because Python 2.6 is different from other builds due to the extra argparse dependency. Fixes issue566. Thanks sontek. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.6.3.rst b/third_party/python/pytest/doc/en/announce/release-2.6.3.rst index ee0d2692c474..7353dfee71cd 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.6.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.6.3.rst @@ -49,4 +49,3 @@ Changes 2.6.3 - check xfail/skip also with non-python function test items. Thanks Floris Bruynooghe. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.7.0.rst b/third_party/python/pytest/doc/en/announce/release-2.7.0.rst index 07ae44ca1acc..d63081edb1b4 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.7.0.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.7.0.rst @@ -62,7 +62,7 @@ holger krekel - fix issue655: work around different ways that cause python2/3 to leak sys.exc_info into fixtures/tests causing failures in 3rd party code -- fix issue615: assertion re-writing did not correctly escape % signs +- fix issue615: assertion rewriting did not correctly escape % signs when formatting boolean operations, which tripped over mixing booleans with modulo operators. Thanks to Tom Viner for the report, triaging and fix. @@ -98,4 +98,3 @@ holger krekel - On failure, the ``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback`` are set, so that a user can inspect the error via postmortem debugging (almarklein). - diff --git a/third_party/python/pytest/doc/en/announce/release-2.7.2.rst b/third_party/python/pytest/doc/en/announce/release-2.7.2.rst index 69130ad623f5..1e3950de4d0c 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.7.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.7.2.rst @@ -55,4 +55,3 @@ The py.test Development Team - fix issue756, fix issue752 (and similar issues): depend on py-1.4.29 which has a refined algorithm for traceback generation. - diff --git a/third_party/python/pytest/doc/en/announce/release-2.8.3.rst b/third_party/python/pytest/doc/en/announce/release-2.8.3.rst index d080ac724c1d..b131a7e1f14d 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.8.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.8.3.rst @@ -53,7 +53,6 @@ The py.test Development Team Thanks Gabriel Reis for the PR. - add more talks to the documentation -- extend documentation on the --ignore cli option -- use pytest-runner for setuptools integration +- extend documentation on the --ignore cli option +- use pytest-runner for setuptools integration - minor fixes for interaction with OS X El Capitan system integrity protection (thanks Florian) - diff --git a/third_party/python/pytest/doc/en/announce/release-2.8.7.rst b/third_party/python/pytest/doc/en/announce/release-2.8.7.rst index d98d731064b3..9005f56363a3 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.8.7.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.8.7.rst @@ -28,4 +28,4 @@ The py.test Development Team 2.8.7 (compared to 2.8.6) ------------------------- -- fix #1338: use predictable object resolution for monkeypatch \ No newline at end of file +- fix #1338: use predictable object resolution for monkeypatch diff --git a/third_party/python/pytest/doc/en/announce/release-2.9.0.rst b/third_party/python/pytest/doc/en/announce/release-2.9.0.rst index 011b1ffb9d29..8d829996d83c 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.9.0.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.9.0.rst @@ -14,25 +14,25 @@ As usual, you can upgrade from pypi via:: Thanks to all who contributed to this release, among them: - Anatoly Bubenkov - Bruno Oliveira - Buck Golemon - David Vierra - Florian Bruhin - Galaczi Endre - Georgy Dyuldin - Lukas Bednar - Luke Murphy - Marcin Biernat - Matt Williams - Michael Aquilina - Raphael Pierzina - Ronny Pfannschmidt - Ryan Wooden - Tiemo Kieft - TomV - holger krekel - jab + Anatoly Bubenkov + Bruno Oliveira + Buck Golemon + David Vierra + Florian Bruhin + Galaczi Endre + Georgy Dyuldin + Lukas Bednar + Luke Murphy + Marcin Biernat + Matt Williams + Michael Aquilina + Raphael Pierzina + Ronny Pfannschmidt + Ryan Wooden + Tiemo Kieft + TomV + holger krekel + jab Happy testing, @@ -76,18 +76,18 @@ The py.test Development Team **Changes** * **Important**: `py.code `_ has been - merged into the ``pytest`` repository as ``pytest._code``. This decision - was made because ``py.code`` had very few uses outside ``pytest`` and the - fact that it was in a different repository made it difficult to fix bugs on + merged into the ``pytest`` repository as ``pytest._code``. This decision + was made because ``py.code`` had very few uses outside ``pytest`` and the + fact that it was in a different repository made it difficult to fix bugs on its code in a timely manner. The team hopes with this to be able to better refactor out and improve that code. This change shouldn't affect users, but it is useful to let users aware if they encounter any strange behavior. - - Keep in mind that the code for ``pytest._code`` is **private** and + + Keep in mind that the code for ``pytest._code`` is **private** and **experimental**, so you definitely should not import it explicitly! - Please note that the original ``py.code`` is still available in + Please note that the original ``py.code`` is still available in `pylib `_. * ``pytest_enter_pdb`` now optionally receives the pytest config object. @@ -129,8 +129,8 @@ The py.test Development Team * Fix (`#1422`_): junit record_xml_property doesn't allow multiple records with same name. - - + + .. _`traceback style docs`: https://pytest.org/latest/usage.html#modifying-python-traceback-printing .. _#1422: https://github.com/pytest-dev/pytest/issues/1422 @@ -156,4 +156,4 @@ The py.test Development Team .. _@tomviner: https://github.com/tomviner .. _@RonnyPfannschmidt: https://github.com/RonnyPfannschmidt .. _@rabbbit: https://github.com/rabbbit -.. _@hackebrot: https://github.com/hackebrot \ No newline at end of file +.. _@hackebrot: https://github.com/hackebrot diff --git a/third_party/python/pytest/doc/en/announce/release-2.9.1.rst b/third_party/python/pytest/doc/en/announce/release-2.9.1.rst index 3277da1e9b0f..c71f38516382 100644 --- a/third_party/python/pytest/doc/en/announce/release-2.9.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-2.9.1.rst @@ -14,17 +14,17 @@ As usual, you can upgrade from pypi via:: Thanks to all who contributed to this release, among them: - Bruno Oliveira - Daniel Hahler - Dmitry Malinovsky - Florian Bruhin - Floris Bruynooghe - Matt Bachmann - Ronny Pfannschmidt - TomV - Vladimir Bolshakov - Zearin - palaviv + Bruno Oliveira + Daniel Hahler + Dmitry Malinovsky + Florian Bruhin + Floris Bruynooghe + Matt Bachmann + Ronny Pfannschmidt + TomV + Vladimir Bolshakov + Zearin + palaviv Happy testing, diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.0.rst b/third_party/python/pytest/doc/en/announce/release-3.0.0.rst index 4bf1e8534ec3..ca3e9e327632 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.0.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.0.rst @@ -8,10 +8,10 @@ against itself, passing on many different interpreters and platforms. This release contains a lot of bugs fixes and improvements, and much of the work done on it was possible because of the 2016 Sprint[1], which -was funded by an indiegogo campaign which raised over US$12,000 with -nearly 100 backers. +was funded by an indiegogo campaign which raised over US$12,000 with +nearly 100 backers. -There's a "What's new in pytest 3.0" [2] blog post highlighting the +There's a "What's new in pytest 3.0" [2] blog post highlighting the major features in this release. To see the complete changelog and documentation, please visit: diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.1.rst b/third_party/python/pytest/doc/en/announce/release-3.0.1.rst index 9fb38047b9ca..eb6f6a50ef7f 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.1.rst @@ -7,7 +7,7 @@ This release fixes some regressions reported in version 3.0.0, being a drop-in replacement. To upgrade: pip install --upgrade pytest - + The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.2.rst b/third_party/python/pytest/doc/en/announce/release-3.0.2.rst index 9d1c05f2d453..4af412fc5ee5 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.2.rst @@ -7,7 +7,7 @@ This release fixes some regressions and bugs reported in version 3.0.1, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.3.rst b/third_party/python/pytest/doc/en/announce/release-3.0.3.rst index f00172195db8..896d4787304c 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.3.rst @@ -3,11 +3,11 @@ pytest-3.0.3 pytest 3.0.3 has just been released to PyPI. -This release fixes some regressions and bugs reported in the last version, +This release fixes some regressions and bugs reported in the last version, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.4.rst b/third_party/python/pytest/doc/en/announce/release-3.0.4.rst index 852057037dd4..855bc56d5b87 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.4.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.4.rst @@ -3,11 +3,11 @@ pytest-3.0.4 pytest 3.0.4 has just been released to PyPI. -This release fixes some regressions and bugs reported in the last version, +This release fixes some regressions and bugs reported in the last version, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.5.rst b/third_party/python/pytest/doc/en/announce/release-3.0.5.rst index 3e2419d7e5dc..2f3698275889 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.5.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.5.rst @@ -6,7 +6,7 @@ pytest 3.0.5 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.6.rst b/third_party/python/pytest/doc/en/announce/release-3.0.6.rst index 2988b9cb3b8f..149c2d65e1a9 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.6.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.6.rst @@ -6,7 +6,7 @@ pytest 3.0.6 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. diff --git a/third_party/python/pytest/doc/en/announce/release-3.0.7.rst b/third_party/python/pytest/doc/en/announce/release-3.0.7.rst index 591557aa7873..b37e4f61dee3 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.0.7.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.0.7.rst @@ -6,7 +6,7 @@ pytest 3.0.7 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.1.1.rst b/third_party/python/pytest/doc/en/announce/release-3.1.1.rst index 370b8fd73550..4ce7531977c3 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.1.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.1.1.rst @@ -6,7 +6,7 @@ pytest 3.1.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.1.2.rst b/third_party/python/pytest/doc/en/announce/release-3.1.2.rst index 60168a857bab..8ed0c93e9ad2 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.1.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.1.2.rst @@ -6,7 +6,7 @@ pytest 3.1.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.1.3.rst b/third_party/python/pytest/doc/en/announce/release-3.1.3.rst index a55280626ba9..d7771f922326 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.1.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.1.3.rst @@ -6,7 +6,7 @@ pytest 3.1.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.2.1.rst b/third_party/python/pytest/doc/en/announce/release-3.2.1.rst index 899ffcd4b4aa..afe2c5bfe2c1 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.2.1.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.2.1.rst @@ -6,7 +6,7 @@ pytest 3.2.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.2.2.rst b/third_party/python/pytest/doc/en/announce/release-3.2.2.rst index 599bf872775c..88e32873a1b4 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.2.2.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.2.2.rst @@ -6,7 +6,7 @@ pytest 3.2.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.2.3.rst b/third_party/python/pytest/doc/en/announce/release-3.2.3.rst index 589374974d91..ddfda4d132fb 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.2.3.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.2.3.rst @@ -6,7 +6,7 @@ pytest 3.2.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.2.4.rst b/third_party/python/pytest/doc/en/announce/release-3.2.4.rst index 44bfcc27e29b..65e486b7aa22 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.2.4.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.2.4.rst @@ -6,7 +6,7 @@ pytest 3.2.4 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.2.5.rst b/third_party/python/pytest/doc/en/announce/release-3.2.5.rst index a520ce2b333d..2e5304c6f270 100644 --- a/third_party/python/pytest/doc/en/announce/release-3.2.5.rst +++ b/third_party/python/pytest/doc/en/announce/release-3.2.5.rst @@ -6,7 +6,7 @@ pytest 3.2.5 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/doc/en/announce/release-3.3.0.rst b/third_party/python/pytest/doc/en/announce/release-3.3.0.rst new file mode 100644 index 000000000000..e0740e7d5929 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.3.0.rst @@ -0,0 +1,50 @@ +pytest-3.3.0 +======================================= + +The pytest team is proud to announce the 3.3.0 release! + +pytest is a mature Python testing tool with more than a 1600 tests +against itself, passing on many different interpreters and platforms. + +This release contains a number of bugs fixes and improvements, so users are encouraged +to take a look at the CHANGELOG: + + http://doc.pytest.org/en/latest/changelog.html + +For complete documentation, please visit: + + http://docs.pytest.org + +As usual, you can upgrade from pypi via: + + pip install -U pytest + +Thanks to all who contributed to this release, among them: + +* Anthony Sottile +* Bruno Oliveira +* Ceridwen +* Daniel Hahler +* Dirk Thomas +* Dmitry Malinovsky +* Florian Bruhin +* George Y. Kussumoto +* Hugo +* Jesús Espino +* Joan Massich +* Ofir +* OfirOshir +* Ronny Pfannschmidt +* Samuel Dion-Girardeau +* Srinivas Reddy Thatiparthy +* Sviatoslav Abakumov +* Tarcisio Fischer +* Thomas Hisch +* Tyler Goodlet +* hugovk +* je +* prokaktus + + +Happy testing, +The Pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.3.1.rst b/third_party/python/pytest/doc/en/announce/release-3.3.1.rst new file mode 100644 index 000000000000..7eed836ae6d5 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.3.1.rst @@ -0,0 +1,25 @@ +pytest-3.3.1 +======================================= + +pytest 3.3.1 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Bruno Oliveira +* Daniel Hahler +* Eugene Prikazchikov +* Florian Bruhin +* Roland Puntaier +* Ronny Pfannschmidt +* Sebastian Rahlf +* Tom Viner + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.3.2.rst b/third_party/python/pytest/doc/en/announce/release-3.3.2.rst new file mode 100644 index 000000000000..d9acef947ddd --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.3.2.rst @@ -0,0 +1,28 @@ +pytest-3.3.2 +======================================= + +pytest 3.3.2 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Anthony Sottile +* Antony Lee +* Austin +* Bruno Oliveira +* Florian Bruhin +* Floris Bruynooghe +* Henk-Jaap Wagenaar +* Jurko Gospodnetić +* Ronny Pfannschmidt +* Srinivas Reddy Thatiparthy +* Thomas Hisch + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.4.0.rst b/third_party/python/pytest/doc/en/announce/release-3.4.0.rst new file mode 100644 index 000000000000..df1e004f1cc1 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.4.0.rst @@ -0,0 +1,52 @@ +pytest-3.4.0 +======================================= + +The pytest team is proud to announce the 3.4.0 release! + +pytest is a mature Python testing tool with more than a 1600 tests +against itself, passing on many different interpreters and platforms. + +This release contains a number of bugs fixes and improvements, so users are encouraged +to take a look at the CHANGELOG: + + http://doc.pytest.org/en/latest/changelog.html + +For complete documentation, please visit: + + http://docs.pytest.org + +As usual, you can upgrade from pypi via: + + pip install -U pytest + +Thanks to all who contributed to this release, among them: + +* Aaron +* Alan Velasco +* Anders Hovmöller +* Andrew Toolan +* Anthony Sottile +* Aron Coyle +* Brian Maissy +* Bruno Oliveira +* Cyrus Maden +* Florian Bruhin +* Henk-Jaap Wagenaar +* Ian Lesperance +* Jon Dufresne +* Jurko Gospodnetić +* Kate +* Kimberly +* Per A. Brodtkorb +* Pierre-Alexandre Fonta +* Raphael Castaneda +* Ronny Pfannschmidt +* ST John +* Segev Finer +* Thomas Hisch +* Tzu-ping Chung +* feuillemorte + + +Happy testing, +The Pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.4.1.rst b/third_party/python/pytest/doc/en/announce/release-3.4.1.rst new file mode 100644 index 000000000000..e37f5d7e2405 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.4.1.rst @@ -0,0 +1,27 @@ +pytest-3.4.1 +======================================= + +pytest 3.4.1 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Aaron +* Alan Velasco +* Andy Freeland +* Brian Maissy +* Bruno Oliveira +* Florian Bruhin +* Jason R. Coombs +* Marcin Bachry +* Pedro Algarvio +* Ronny Pfannschmidt + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.4.2.rst b/third_party/python/pytest/doc/en/announce/release-3.4.2.rst new file mode 100644 index 000000000000..8e9988228fac --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.4.2.rst @@ -0,0 +1,28 @@ +pytest-3.4.2 +======================================= + +pytest 3.4.2 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Allan Feldman +* Bruno Oliveira +* Florian Bruhin +* Jason R. Coombs +* Kyle Altendorf +* Maik Figura +* Ronny Pfannschmidt +* codetriage-readme-bot +* feuillemorte +* joshm91 +* mike + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.5.0.rst b/third_party/python/pytest/doc/en/announce/release-3.5.0.rst new file mode 100644 index 000000000000..54a05cea24d9 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.5.0.rst @@ -0,0 +1,51 @@ +pytest-3.5.0 +======================================= + +The pytest team is proud to announce the 3.5.0 release! + +pytest is a mature Python testing tool with more than a 1600 tests +against itself, passing on many different interpreters and platforms. + +This release contains a number of bugs fixes and improvements, so users are encouraged +to take a look at the CHANGELOG: + + http://doc.pytest.org/en/latest/changelog.html + +For complete documentation, please visit: + + http://docs.pytest.org + +As usual, you can upgrade from pypi via: + + pip install -U pytest + +Thanks to all who contributed to this release, among them: + +* Allan Feldman +* Brian Maissy +* Bruno Oliveira +* Carlos Jenkins +* Daniel Hahler +* Florian Bruhin +* Jason R. Coombs +* Jeffrey Rackauckas +* Jordan Speicher +* Julien Palard +* Kale Kundert +* Kostis Anagnostopoulos +* Kyle Altendorf +* Maik Figura +* Pedro Algarvio +* Ronny Pfannschmidt +* Tadeu Manoel +* Tareq Alayan +* Thomas Hisch +* William Lee +* codetriage-readme-bot +* feuillemorte +* joshm91 +* mike + + +Happy testing, +The Pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.5.1.rst b/third_party/python/pytest/doc/en/announce/release-3.5.1.rst new file mode 100644 index 000000000000..91f14390eeb5 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.5.1.rst @@ -0,0 +1,30 @@ +pytest-3.5.1 +======================================= + +pytest 3.5.1 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Brian Maissy +* Bruno Oliveira +* Darren Burns +* David Chudzicki +* Floris Bruynooghe +* Holger Kohr +* Irmen de Jong +* Jeffrey Rackauckas +* Rachel Kogan +* Ronny Pfannschmidt +* Stefan Scherfke +* Tim Strazny +* Семён Марьясин + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.6.0.rst b/third_party/python/pytest/doc/en/announce/release-3.6.0.rst new file mode 100644 index 000000000000..37361cf4add8 --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.6.0.rst @@ -0,0 +1,41 @@ +pytest-3.6.0 +======================================= + +The pytest team is proud to announce the 3.6.0 release! + +pytest is a mature Python testing tool with more than a 1600 tests +against itself, passing on many different interpreters and platforms. + +This release contains a number of bugs fixes and improvements, so users are encouraged +to take a look at the CHANGELOG: + + http://doc.pytest.org/en/latest/changelog.html + +For complete documentation, please visit: + + http://docs.pytest.org + +As usual, you can upgrade from pypi via: + + pip install -U pytest + +Thanks to all who contributed to this release, among them: + +* Anthony Shaw +* ApaDoctor +* Brian Maissy +* Bruno Oliveira +* Jon Dufresne +* Katerina Koukiou +* Miro Hrončok +* Rachel Kogan +* Ronny Pfannschmidt +* Tim Hughes +* Tyler Goodlet +* Ville Skyttä +* aviral1701 +* feuillemorte + + +Happy testing, +The Pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.6.1.rst b/third_party/python/pytest/doc/en/announce/release-3.6.1.rst new file mode 100644 index 000000000000..3bedcf46a85e --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.6.1.rst @@ -0,0 +1,24 @@ +pytest-3.6.1 +======================================= + +pytest 3.6.1 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Anthony Sottile +* Bruno Oliveira +* Jeffrey Rackauckas +* Miro Hrončok +* Niklas Meinzer +* Oliver Bestwalter +* Ronny Pfannschmidt + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/announce/release-3.6.2.rst b/third_party/python/pytest/doc/en/announce/release-3.6.2.rst new file mode 100644 index 000000000000..a1215f57689c --- /dev/null +++ b/third_party/python/pytest/doc/en/announce/release-3.6.2.rst @@ -0,0 +1,29 @@ +pytest-3.6.2 +======================================= + +pytest 3.6.2 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. + +Thanks to all who contributed to this release, among them: + +* Alan Velasco +* Alex Barbato +* Anthony Sottile +* Bartosz Cierocki +* Bruno Oliveira +* Daniel Hahler +* Guoqiang Zhang +* Hynek Schlawack +* John T. Wodder II +* Michael Käufl +* Ronny Pfannschmidt +* Samuel Dion-Girardeau + + +Happy testing, +The pytest Development Team diff --git a/third_party/python/pytest/doc/en/assert.rst b/third_party/python/pytest/doc/en/assert.rst index d9e044356b31..e0e9b9305875 100644 --- a/third_party/python/pytest/doc/en/assert.rst +++ b/third_party/python/pytest/doc/en/assert.rst @@ -25,23 +25,23 @@ to assert that your function returns a certain value. If this assertion fails you will see the return value of the function call:: $ pytest test_assert1.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_assert1.py F - - ======= FAILURES ======== - _______ test_function ________ - + + test_assert1.py F [100%] + + ================================= FAILURES ================================= + ______________________________ test_function _______________________________ + def test_function(): > assert f() == 4 E assert 3 == 4 E + where 3 = f() - + test_assert1.py:5: AssertionError - ======= 1 failed in 0.12 seconds ======== + ========================= 1 failed in 0.12 seconds ========================= ``pytest`` has support for showing the values of the most common subexpressions including calls, attributes, comparisons, and binary and unary @@ -91,7 +91,7 @@ In the context manager form you may use the keyword argument ``message`` to specify a custom failure message:: >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"): - ... pass + ... pass ... Failed: Expecting ZeroDivisionError If you want to write test code that works on Python 2.4 as well, @@ -168,16 +168,16 @@ when it encounters comparisons. For example:: if you run this module:: $ pytest test_assert2.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_assert2.py F - - ======= FAILURES ======== - _______ test_set_comparison ________ - + + test_assert2.py F [100%] + + ================================= FAILURES ================================= + ___________________________ test_set_comparison ____________________________ + def test_set_comparison(): set1 = set("1308") set2 = set("8035") @@ -188,9 +188,9 @@ if you run this module:: E Extra items in the right set: E '5' E Use -v to get the full diff - + test_assert2.py:5: AssertionError - ======= 1 failed in 0.12 seconds ======== + ========================= 1 failed in 0.12 seconds ========================= Special comparisons are done for a number of cases: @@ -209,7 +209,7 @@ the ``pytest_assertrepr_compare`` hook. .. autofunction:: _pytest.hookspec.pytest_assertrepr_compare :noindex: -As an example consider adding the following hook in a :ref:`conftest.py ` +As an example consider adding the following hook in a :ref:`conftest.py ` file which provides an alternative explanation for ``Foo`` objects:: # content of conftest.py @@ -238,17 +238,17 @@ you can run the test module and get the custom output defined in the conftest file:: $ pytest -q test_foocompare.py - F - ======= FAILURES ======== - _______ test_compare ________ - + F [100%] + ================================= FAILURES ================================= + _______________________________ test_compare _______________________________ + def test_compare(): f1 = Foo(1) f2 = Foo(2) > assert f1 == f2 E assert Comparing Foo instances: E vals: 1 != 2 - + test_foocompare.py:11: AssertionError 1 failed in 0.12 seconds diff --git a/third_party/python/pytest/doc/en/backwards-compatibility.rst b/third_party/python/pytest/doc/en/backwards-compatibility.rst index 8ceada52d66c..55506e7c338e 100644 --- a/third_party/python/pytest/doc/en/backwards-compatibility.rst +++ b/third_party/python/pytest/doc/en/backwards-compatibility.rst @@ -10,3 +10,11 @@ With the pytest 3.0 release we introduced a clear communication scheme for when To communicate changes we are already issuing deprecation warnings, but they are not displayed by default. In pytest 3.0 we changed the default setting so that pytest deprecation warnings are displayed if not explicitly silenced (with ``--disable-pytest-warnings``). We will only remove deprecated functionality in major releases (e.g. if we deprecate something in 3.0 we will remove it in 4.0), and keep it around for at least two minor releases (e.g. if we deprecate something in 3.9 and 4.0 is the next release, we will not remove it in 4.0 but in 5.0). + + +Deprecation Roadmap +------------------- + +We track deprecation and removal of features using milestones and the `deprecation `_ and `removal `_ labels on GitHub. + +Following our deprecation policy, after starting issuing deprecation warnings we keep features for *at least* two minor versions before considering removal. diff --git a/third_party/python/pytest/doc/en/bash-completion.rst b/third_party/python/pytest/doc/en/bash-completion.rst index 81fe62183fb0..08d9782090ca 100644 --- a/third_party/python/pytest/doc/en/bash-completion.rst +++ b/third_party/python/pytest/doc/en/bash-completion.rst @@ -14,7 +14,7 @@ Install argcomplete using:: For global activation of all argcomplete enabled python applications run:: - sudo activate-global-python-argcomplete + sudo activate-global-python-argcomplete For permanent (but not global) ``pytest`` activation, use:: @@ -23,6 +23,3 @@ For permanent (but not global) ``pytest`` activation, use:: For one-time activation of argcomplete for ``pytest`` only, use:: eval "$(register-python-argcomplete pytest)" - - - diff --git a/third_party/python/pytest/doc/en/builtin.rst b/third_party/python/pytest/doc/en/builtin.rst index b59399a79239..c2d23469bc84 100644 --- a/third_party/python/pytest/doc/en/builtin.rst +++ b/third_party/python/pytest/doc/en/builtin.rst @@ -1,113 +1,90 @@ +:orphan: .. _`pytest helpers`: Pytest API and builtin fixtures ================================================ -This is a list of ``pytest.*`` API functions and fixtures. + +Most of the information of this page has been moved over to :ref:`reference`. For information on plugin hooks and objects, see :ref:`plugins`. For information on the ``pytest.mark`` mechanism, see :ref:`mark`. -For the below objects, you can also interactively ask for help, e.g. by -typing on the Python interactive prompt something like:: - - import pytest - help(pytest) - -.. currentmodule:: pytest - -Invoking pytest interactively ---------------------------------------------------- - -.. autofunction:: main - -More examples at :ref:`pytest.main-usage` - - -Helpers for assertions about Exceptions/Warnings --------------------------------------------------------- - -.. autofunction:: raises - -Examples at :ref:`assertraises`. - -.. autofunction:: deprecated_call - -Comparing floating point numbers --------------------------------- - -.. autofunction:: approx - -Raising a specific test outcome --------------------------------------- - -You can use the following functions in your test, fixture or setup -functions to force a certain test outcome. Note that most often -you can rather use declarative marks, see :ref:`skipping`. - -.. autofunction:: _pytest.outcomes.fail -.. autofunction:: _pytest.outcomes.skip -.. autofunction:: _pytest.outcomes.importorskip -.. autofunction:: _pytest.outcomes.xfail -.. autofunction:: _pytest.outcomes.exit - -Fixtures and requests ------------------------------------------------------ - -To mark a fixture function: - -.. autofunction:: _pytest.fixtures.fixture - -Tutorial at :ref:`fixtures`. - -The ``request`` object that can be used from fixture functions. - -.. autoclass:: _pytest.fixtures.FixtureRequest() - :members: - - -.. _builtinfixtures: -.. _builtinfuncargs: - -Builtin fixtures/function arguments ------------------------------------------ - -You can ask for available builtin or project-custom -:ref:`fixtures ` by typing:: +For information about fixtures, see :ref:`fixtures`. To see a complete list of available fixtures (add ``-v`` to also see fixtures with leading ``_``), type :: $ pytest -q --fixtures cache Return a cache object that can persist state between testing sessions. - + cache.get(key, default) cache.set(key, value) - + Keys must be a ``/`` separated value, where the first part is usually the name of your plugin or application to avoid clashes with other cache users. - + Values can be any object handled by the json stdlib module. capsys - Enable capturing of writes to sys.stdout/sys.stderr and make + Enable capturing of writes to ``sys.stdout`` and ``sys.stderr`` and make captured output available via ``capsys.readouterr()`` method calls - which return a ``(out, err)`` tuple. + which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` + objects. + capsysbinary + Enable capturing of writes to ``sys.stdout`` and ``sys.stderr`` and make + captured output available via ``capsys.readouterr()`` method calls + which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``bytes`` + objects. capfd - Enable capturing of writes to file descriptors 1 and 2 and make + Enable capturing of writes to file descriptors ``1`` and ``2`` and make captured output available via ``capfd.readouterr()`` method calls - which return a ``(out, err)`` tuple. + which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``text`` + objects. + capfdbinary + Enable capturing of write to file descriptors 1 and 2 and make + captured output available via ``capfdbinary.readouterr`` method calls + which return a ``(out, err)`` tuple. ``out`` and ``err`` will be + ``bytes`` objects. doctest_namespace - Inject names into the doctest namespace. + Fixture that returns a :py:class:`dict` that will be injected into the namespace of doctests. pytestconfig - the pytest config object with access to command line opts. - record_xml_property - Add extra xml properties to the tag for the calling test. + Session-scoped fixture that returns the :class:`_pytest.config.Config` object. + + Example:: + + def test_foo(pytestconfig): + if pytestconfig.getoption("verbose"): + ... + record_property + Add an extra properties the calling test. + User properties become part of the test report and are available to the + configured reporters, like JUnit XML. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded. + + Example:: + + def test_function(record_property): + record_property("example_key", 1) + record_xml_property + (Deprecated) use record_property. + record_xml_attribute + Add extra xml attributes to the tag for the calling test. + The fixture is callable with ``(name, value)``, with value being + automatically xml-encoded + caplog + Access and control log capturing. + + Captured logs are available through the following methods:: + + * caplog.text -> string containing formatted log output + * caplog.records -> list of logging.LogRecord instances + * caplog.record_tuples -> list of (logger_name, level, message) tuples + * caplog.clear() -> clear captured records and formatted log output string monkeypatch The returned ``monkeypatch`` fixture provides these helper methods to modify objects, dictionaries or os.environ:: - + monkeypatch.setattr(obj, name, value, raising=True) monkeypatch.delattr(obj, name, raising=True) monkeypatch.setitem(mapping, name, value) @@ -116,17 +93,14 @@ You can ask for available builtin or project-custom monkeypatch.delenv(name, value, raising=True) monkeypatch.syspath_prepend(path) monkeypatch.chdir(path) - + All modifications will be undone after the requesting test function or fixture has finished. The ``raising`` parameter determines if a KeyError or AttributeError will be raised if the set/deletion operation has no target. recwarn - Return a WarningsRecorder instance that provides these methods: - - * ``pop(category=None)``: return last warning matching the category. - * ``clear()``: clear list of warnings - + Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. + See http://docs.python.org/library/warnings.html for information on warning categories. tmpdir_factory @@ -137,5 +111,12 @@ You can ask for available builtin or project-custom created as a sub directory of the base temporary directory. The returned object is a `py.path.local`_ path object. - + + .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html + no tests ran in 0.12 seconds + +You can also interactively ask for help, e.g. by typing on the Python interactive prompt something like:: + + import pytest + help(pytest) diff --git a/third_party/python/pytest/doc/en/cache.rst b/third_party/python/pytest/doc/en/cache.rst index d5d6b653bea7..37bcf7070d42 100644 --- a/third_party/python/pytest/doc/en/cache.rst +++ b/third_party/python/pytest/doc/en/cache.rst @@ -20,7 +20,7 @@ last ``pytest`` invocation: For cleanup (usually not needed), a ``--cache-clear`` option allows to remove all cross-session cache contents ahead of a test run. -Other plugins may access the `config.cache`_ object to set/get +Other plugins may access the `config.cache`_ object to set/get **json encodable** values between ``pytest`` invocations. .. note:: @@ -46,68 +46,67 @@ First, let's create 50 test invocation of which only 2 fail:: If you run this for the first time you will see two failures:: $ pytest -q - .................F.......F........................ - ======= FAILURES ======== - _______ test_num[17] ________ - + .................F.......F........................ [100%] + ================================= FAILURES ================================= + _______________________________ test_num[17] _______________________________ + i = 17 - + @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck - + test_50.py:6: Failed - _______ test_num[25] ________ - + _______________________________ test_num[25] _______________________________ + i = 25 - + @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck - + test_50.py:6: Failed 2 failed, 48 passed in 0.12 seconds If you then run it with ``--lf``:: $ pytest --lf - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: - collected 50 items + collected 50 items / 48 deselected run-last-failure: rerun previous 2 failures - - test_50.py FF - - ======= FAILURES ======== - _______ test_num[17] ________ - + + test_50.py FF [100%] + + ================================= FAILURES ================================= + _______________________________ test_num[17] _______________________________ + i = 17 - + @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck - + test_50.py:6: Failed - _______ test_num[25] ________ - + _______________________________ test_num[25] _______________________________ + i = 25 - + @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck - + test_50.py:6: Failed - ======= 48 tests deselected ======== - ======= 2 failed, 48 deselected in 0.12 seconds ======== + ================= 2 failed, 48 deselected in 0.12 seconds ================== You have run only the two failing test from the last run, while 48 tests have not been run ("deselected"). @@ -117,41 +116,55 @@ previous failures will be executed first (as can be seen from the series of ``FF`` and dots):: $ pytest --ff - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 50 items run-last-failure: rerun previous 2 failures first - - test_50.py FF................................................ - - ======= FAILURES ======== - _______ test_num[17] ________ - + + test_50.py FF................................................ [100%] + + ================================= FAILURES ================================= + _______________________________ test_num[17] _______________________________ + i = 17 - + @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck - + test_50.py:6: Failed - _______ test_num[25] ________ - + _______________________________ test_num[25] _______________________________ + i = 25 - + @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck - + test_50.py:6: Failed - ======= 2 failed, 48 passed in 0.12 seconds ======== + =================== 2 failed, 48 passed in 0.12 seconds ==================== .. _`config.cache`: +New ``--nf``, ``--new-first`` options: run new tests first followed by the rest +of the tests, in both cases tests are also sorted by the file modified time, +with more recent files coming first. + +Behavior when no tests failed in the last run +--------------------------------------------- + +When no tests failed in the last run, or when no cached ``lastfailed`` data was +found, ``pytest`` can be configured either to run all of the tests or no tests, +using the ``--last-failed-no-failures`` option, which takes one of the following values:: + + pytest --last-failed-no-failures all # run all tests (default behavior) + pytest --last-failed-no-failures none # run no tests and exit + The new config.cache object -------------------------------- @@ -182,16 +195,16 @@ If you run this command once, it will take a while because of the sleep:: $ pytest -q - F - ======= FAILURES ======== - _______ test_function ________ - + F [100%] + ================================= FAILURES ================================= + ______________________________ test_function _______________________________ + mydata = 42 - + def test_function(mydata): > assert mydata == 23 E assert 42 == 23 - + test_caching.py:14: AssertionError 1 failed in 0.12 seconds @@ -199,20 +212,20 @@ If you run it a second time the value will be retrieved from the cache and this will be quick:: $ pytest -q - F - ======= FAILURES ======== - _______ test_function ________ - + F [100%] + ================================= FAILURES ================================= + ______________________________ test_function _______________________________ + mydata = 42 - + def test_function(mydata): > assert mydata == 23 E assert 42 == 23 - + test_caching.py:14: AssertionError 1 failed in 0.12 seconds -See the `cache-api`_ for more details. +See the :ref:`cache-api` for more details. Inspecting Cache content @@ -221,18 +234,20 @@ Inspecting Cache content You can always peek at the content of the cache using the ``--cache-show`` command line option:: - $ py.test --cache-show - ======= test session starts ======== + $ pytest --cache-show + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: - cachedir: $REGENDOC_TMPDIR/.cache + cachedir: $REGENDOC_TMPDIR/.pytest_cache ------------------------------- cache values ------------------------------- cache/lastfailed contains: {'test_caching.py::test_function': True} + cache/nodeids contains: + ['test_caching.py::test_function'] example/value contains: 42 - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= Clearing Cache content ------------------------------- @@ -245,24 +260,3 @@ by adding the ``--cache-clear`` option like this:: This is recommended for invocations from Continuous Integration servers where isolation and correctness is more important than speed. - - -.. _`cache-api`: - -config.cache API ------------------- - -The ``config.cache`` object allows other plugins, -including ``conftest.py`` files, -to safely and flexibly store and retrieve values across -test runs because the ``config`` object is available -in many places. - -Under the hood, the cache plugin uses the simple -dumps/loads API of the json stdlib module - -.. currentmodule:: _pytest.cacheprovider - -.. automethod:: Cache.get -.. automethod:: Cache.set -.. automethod:: Cache.makedir diff --git a/third_party/python/pytest/doc/en/capture.rst b/third_party/python/pytest/doc/en/capture.rst index 58ebdf8408a2..ab86fb55f3d8 100644 --- a/third_party/python/pytest/doc/en/capture.rst +++ b/third_party/python/pytest/doc/en/capture.rst @@ -9,7 +9,8 @@ Default stdout/stderr/stdin capturing behaviour During test execution any output sent to ``stdout`` and ``stderr`` is captured. If a test or a setup method fails its according captured -output will usually be shown along with the failure traceback. +output will usually be shown along with the failure traceback. (this +behavior can be configured by the ``--show-capture`` command-line option). In addition, ``stdin`` is set to a "null" object which will fail on attempts to read from it because it is rarely desired @@ -63,43 +64,43 @@ and running this module will show you precisely the output of the failing function and hide the other one:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_module.py .F - - ======= FAILURES ======== - _______ test_func2 ________ - + + test_module.py .F [100%] + + ================================= FAILURES ================================= + ________________________________ test_func2 ________________________________ + def test_func2(): > assert False E assert False - + test_module.py:9: AssertionError -------------------------- Captured stdout setup --------------------------- setting up - ======= 1 failed, 1 passed in 0.12 seconds ======== + ==================== 1 failed, 1 passed in 0.12 seconds ==================== Accessing captured output from a test function --------------------------------------------------- -The ``capsys`` and ``capfd`` fixtures allow to access stdout/stderr -output created during test execution. Here is an example test function -that performs some output related checks: +The ``capsys``, ``capsysbinary``, ``capfd``, and ``capfdbinary`` fixtures +allow access to stdout/stderr output created during test execution. Here is +an example test function that performs some output related checks: .. code-block:: python - def test_myoutput(capsys): # or use "capfd" for fd-level - print ("hello") + def test_myoutput(capsys): # or use "capfd" for fd-level + print("hello") sys.stderr.write("world\n") - out, err = capsys.readouterr() - assert out == "hello\n" - assert err == "world\n" - print ("next") - out, err = capsys.readouterr() - assert out == "next\n" + captured = capsys.readouterr() + assert captured.out == "hello\n" + assert captured.err == "world\n" + print("next") + captured = capsys.readouterr() + assert captured.out == "next\n" The ``readouterr()`` call snapshots the output so far - and capturing will be continued. After the test @@ -110,11 +111,30 @@ output streams and also interacts well with pytest's own per-test capturing. If you want to capture on filedescriptor level you can use -the ``capfd`` function argument which offers the exact +the ``capfd`` fixture which offers the exact same interface but allows to also capture output from libraries or subprocesses that directly write to operating system level output streams (FD1 and FD2). +.. versionadded:: 3.3 + +The return value from ``readouterr`` changed to a ``namedtuple`` with two attributes, ``out`` and ``err``. + +.. versionadded:: 3.3 + +If the code under test writes non-textual data, you can capture this using +the ``capsysbinary`` fixture which instead returns ``bytes`` from +the ``readouterr`` method. The ``capfsysbinary`` fixture is currently only +available in python 3. + + +.. versionadded:: 3.3 + +If the code under test writes non-textual data, you can capture this using +the ``capfdbinary`` fixture which instead returns ``bytes`` from +the ``readouterr`` method. The ``capfdbinary`` fixture operates on the +filedescriptor level. + .. versionadded:: 3.0 @@ -125,9 +145,9 @@ as a context manager, disabling capture inside the ``with`` block: .. code-block:: python def test_disabling_capturing(capsys): - print('this output is captured') + print("this output is captured") with capsys.disabled(): - print('output not captured, going directly to sys.stdout') - print('this output is also captured') + print("output not captured, going directly to sys.stdout") + print("this output is also captured") .. include:: links.inc diff --git a/third_party/python/pytest/doc/en/check_sphinx.py b/third_party/python/pytest/doc/en/check_sphinx.py deleted file mode 100644 index 0f536ffa6ac0..000000000000 --- a/third_party/python/pytest/doc/en/check_sphinx.py +++ /dev/null @@ -1,17 +0,0 @@ -import py -import subprocess -def test_build_docs(tmpdir): - doctrees = tmpdir.join("doctrees") - htmldir = tmpdir.join("html") - subprocess.check_call([ - "sphinx-build", "-W", "-bhtml", - "-d", str(doctrees), ".", str(htmldir)]) - -def test_linkcheck(tmpdir): - doctrees = tmpdir.join("doctrees") - htmldir = tmpdir.join("html") - subprocess.check_call( - ["sphinx-build", "-blinkcheck", - "-d", str(doctrees), ".", str(htmldir)]) - - diff --git a/third_party/python/pytest/doc/en/conf.py b/third_party/python/pytest/doc/en/conf.py index 40f1e4165e44..ebff6ff3bfa4 100644 --- a/third_party/python/pytest/doc/en/conf.py +++ b/third_party/python/pytest/doc/en/conf.py @@ -18,14 +18,18 @@ # The full version, including alpha/beta/rc tags. # The short X.Y version. -import os, sys +import os +import sys +import datetime + from _pytest import __version__ as version + release = ".".join(version.split(".")[:2]) # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -#sys.path.insert(0, os.path.abspath('.')) +# sys.path.insert(0, os.path.abspath('.')) autodoc_member_order = "bysource" todo_include_todos = 1 @@ -33,58 +37,68 @@ todo_include_todos = 1 # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = '1.0' +# needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.autosummary', - 'sphinx.ext.intersphinx', 'sphinx.ext.viewcode'] +extensions = [ + "sphinx.ext.autodoc", + "sphinx.ext.todo", + "sphinx.ext.autosummary", + "sphinx.ext.intersphinx", + "sphinx.ext.viewcode", + "sphinxcontrib_trio", +] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix of source filenames. -source_suffix = '.rst' +source_suffix = ".rst" # The encoding of source files. -#source_encoding = 'utf-8-sig' +# source_encoding = 'utf-8-sig' # The master toctree document. -master_doc = 'contents' +master_doc = "contents" # General information about the project. -project = u'pytest' -copyright = u'2015, holger krekel and pytest-dev team' - +project = u"pytest" +year = datetime.datetime.utcnow().year +copyright = u"2015–{} , holger krekel and pytest-dev team".format(year) # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. -#language = None +# language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = '' +# today = '' # Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' +# today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ['links.inc', '_build', 'naming20.rst', 'test/*', +exclude_patterns = [ + "links.inc", + "_build", + "naming20.rst", + "test/*", "old_*", - '*attic*', - '*/attic*', - 'funcargs.rst', - 'setup.rst', - 'example/remoteinterp.rst', - ] + "*attic*", + "*/attic*", + "funcargs.rst", + "setup.rst", + "example/remoteinterp.rst", +] # The reST default role (used for this markup: `text`) to use for all documents. -#default_role = None +# default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True +# add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). @@ -92,39 +106,36 @@ add_module_names = False # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. -#show_authors = False +# show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - +pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] +# modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- -sys.path.append(os.path.abspath('_themes')) -html_theme_path = ['_themes'] +sys.path.append(os.path.abspath("_themes")) +html_theme_path = ["_themes"] # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'flask' +html_theme = "flask" # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. -html_theme_options = { - 'index_logo': None -} +html_theme_options = {"index_logo": None} # Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] +# html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -html_title = 'pytest documentation' +html_title = "pytest documentation" # A shorter title for the navigation bar. Default is the same as html_title. html_short_title = "pytest-%s" % release @@ -145,37 +156,37 @@ html_favicon = "img/pytest1favi.ico" # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' +# html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. -#html_use_smartypants = True +# html_use_smartypants = True # Custom sidebar templates, maps document names to template names. -#html_sidebars = {} -#html_sidebars = {'index': 'indexsidebar.html'} +# html_sidebars = {} +# html_sidebars = {'index': 'indexsidebar.html'} html_sidebars = { - 'index': [ - 'sidebarintro.html', - 'globaltoc.html', - 'links.html', - 'sourcelink.html', - 'searchbox.html' + "index": [ + "sidebarintro.html", + "globaltoc.html", + "links.html", + "sourcelink.html", + "searchbox.html", + ], + "**": [ + "globaltoc.html", + "relations.html", + "links.html", + "sourcelink.html", + "searchbox.html", ], - '**': [ - 'globaltoc.html', - 'relations.html', - 'links.html', - 'sourcelink.html', - 'searchbox.html' - ] } # Additional templates that should be rendered to pages, maps page names to # template names. -#html_additional_pages = {} -#html_additional_pages = {'index': 'index.html'} +# html_additional_pages = {} +# html_additional_pages = {'index': 'index.html'} # If false, no module index is generated. @@ -185,63 +196,68 @@ html_domain_indices = True html_use_index = False # If true, the index is split into individual pages for each letter. -#html_split_index = False +# html_split_index = False # If true, links to the reST sources are added to the pages. html_show_sourcelink = False # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True +# html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True +# html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = '' +# html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None +# html_file_suffix = None # Output file base name for HTML help builder. -htmlhelp_basename = 'pytestdoc' +htmlhelp_basename = "pytestdoc" # -- Options for LaTeX output -------------------------------------------------- # The paper size ('letter' or 'a4'). -#latex_paper_size = 'letter' +# latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). -#latex_font_size = '10pt' +# latex_font_size = '10pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('contents', 'pytest.tex', u'pytest Documentation', - u'holger krekel, trainer and consultant, http://merlinux.eu', 'manual'), + ( + "contents", + "pytest.tex", + u"pytest Documentation", + u"holger krekel, trainer and consultant, http://merlinux.eu", + "manual", + ) ] # The name of an image file (relative to this directory) to place at the top of # the title page. -latex_logo = 'img/pytest1.png' +latex_logo = "img/pytest1.png" # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. -#latex_use_parts = False +# latex_use_parts = False # If true, show page references after internal links. -#latex_show_pagerefs = False +# latex_show_pagerefs = False # If true, show URL addresses after external links. -#latex_show_urls = False +# latex_show_urls = False # Additional stuff for the LaTeX preamble. -#latex_preamble = '' +# latex_preamble = '' # Documents to append as an appendix to all manuals. -#latex_appendices = [] +# latex_appendices = [] # If false, no module index is generated. latex_domain_indices = False @@ -250,74 +266,78 @@ latex_domain_indices = False # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - ('usage', 'pytest', u'pytest usage', - [u'holger krekel at merlinux eu'], 1) -] +man_pages = [("usage", "pytest", u"pytest usage", [u"holger krekel at merlinux eu"], 1)] # -- Options for Epub output --------------------------------------------------- # Bibliographic Dublin Core info. -epub_title = u'pytest' -epub_author = u'holger krekel at merlinux eu' -epub_publisher = u'holger krekel at merlinux eu' -epub_copyright = u'2013, holger krekel et alii' +epub_title = u"pytest" +epub_author = u"holger krekel at merlinux eu" +epub_publisher = u"holger krekel at merlinux eu" +epub_copyright = u"2013, holger krekel et alii" # The language of the text. It defaults to the language option # or en if the language is not set. -#epub_language = '' +# epub_language = '' # The scheme of the identifier. Typical schemes are ISBN or URL. -#epub_scheme = '' +# epub_scheme = '' # The unique identifier of the text. This can be a ISBN number # or the project homepage. -#epub_identifier = '' +# epub_identifier = '' # A unique identification for the text. -#epub_uid = '' +# epub_uid = '' # HTML files that should be inserted before the pages created by sphinx. # The format is a list of tuples containing the path and title. -#epub_pre_files = [] +# epub_pre_files = [] # HTML files shat should be inserted after the pages created by sphinx. # The format is a list of tuples containing the path and title. -#epub_post_files = [] +# epub_post_files = [] # A list of files that should not be packed into the epub file. -#epub_exclude_files = [] +# epub_exclude_files = [] # The depth of the table of contents in toc.ncx. -#epub_tocdepth = 3 +# epub_tocdepth = 3 # Allow duplicate toc entries. -#epub_tocdup = True +# epub_tocdup = True # -- Options for texinfo output ------------------------------------------------ texinfo_documents = [ - (master_doc, 'pytest', 'pytest Documentation', - ('Holger Krekel@*Benjamin Peterson@*Ronny Pfannschmidt@*' - 'Floris Bruynooghe@*others'), - 'pytest', - 'simple powerful testing with Python', - 'Programming', - 1), + ( + master_doc, + "pytest", + "pytest Documentation", + ( + "Holger Krekel@*Benjamin Peterson@*Ronny Pfannschmidt@*" + "Floris Bruynooghe@*others" + ), + "pytest", + "simple powerful testing with Python", + "Programming", + 1, + ) ] # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {'python': ('http://docs.python.org/', None), -# 'lib': ("http://docs.python.org/2.7library/", None), - } +intersphinx_mapping = {"python": ("http://docs.python.org/3", None)} def setup(app): - #from sphinx.ext.autodoc import cut_lines - #app.connect('autodoc-process-docstring', cut_lines(4, what=['module'])) - app.add_description_unit('confval', 'confval', - objname='configuration value', - indextemplate='pair: %s; configuration value') + # from sphinx.ext.autodoc import cut_lines + # app.connect('autodoc-process-docstring', cut_lines(4, what=['module'])) + app.add_description_unit( + "confval", + "confval", + objname="configuration value", + indextemplate="pair: %s; configuration value", + ) diff --git a/third_party/python/pytest/doc/en/contact.rst b/third_party/python/pytest/doc/en/contact.rst index 83d496640d5e..efc6a8f57d30 100644 --- a/third_party/python/pytest/doc/en/contact.rst +++ b/third_party/python/pytest/doc/en/contact.rst @@ -8,9 +8,9 @@ Contact channels - `pytest issue tracker`_ to report bugs or suggest features (for version 2.0 and above). -- `pytest on stackoverflow.com `_ - to post questions with the tag ``pytest``. New Questions will usually - be seen by pytest users or developers and answered quickly. +- `pytest on stackoverflow.com `_ + to post questions with the tag ``pytest``. New Questions will usually + be seen by pytest users or developers and answered quickly. - `Testing In Python`_: a mailing list for Python testing tools and discussion. @@ -47,4 +47,3 @@ Contact channels .. _`development mailing list`: .. _`pytest-dev at python.org (mailing list)`: http://mail.python.org/mailman/listinfo/pytest-dev .. _`pytest-commit at python.org (mailing list)`: http://mail.python.org/mailman/listinfo/pytest-commit - diff --git a/third_party/python/pytest/doc/en/contents.rst b/third_party/python/pytest/doc/en/contents.rst index 6b9eed010480..9f1d8d85ac25 100644 --- a/third_party/python/pytest/doc/en/contents.rst +++ b/third_party/python/pytest/doc/en/contents.rst @@ -14,14 +14,13 @@ Full pytest documentation usage existingtestsuite assert - builtin fixture + mark monkeypatch tmpdir capture warnings doctest - mark skipping parametrize cache @@ -30,6 +29,8 @@ Full pytest documentation xunit_setup plugins writing_plugins + logging + reference goodpractices pythonpath @@ -61,4 +62,3 @@ Full pytest documentation :maxdepth: 1 changelog - diff --git a/third_party/python/pytest/doc/en/customize.rst b/third_party/python/pytest/doc/en/customize.rst index 21deb582e51d..c1a3498c7b67 100644 --- a/third_party/python/pytest/doc/en/customize.rst +++ b/third_party/python/pytest/doc/en/customize.rst @@ -38,6 +38,10 @@ Here's a summary what ``pytest`` uses ``rootdir`` for: Important to emphasize that ``rootdir`` is **NOT** used to modify ``sys.path``/``PYTHONPATH`` or influence how modules are imported. See :ref:`pythonpath` for more details. +``--rootdir=path`` command-line option can be used to force a specific directory. +The directory passed may contain environment variables when it is used in conjunction +with ``addopts`` in a ``pytest.ini`` file. + Finding the ``rootdir`` ~~~~~~~~~~~~~~~~~~~~~~~ @@ -152,163 +156,4 @@ above will show verbose output because ``-v`` overwrites ``-q``. Builtin configuration file options ---------------------------------------------- -.. confval:: minversion - - Specifies a minimal pytest version required for running tests. - - minversion = 2.1 # will fail if we run with pytest-2.0 - -.. confval:: addopts - - Add the specified ``OPTS`` to the set of command line arguments as if they - had been specified by the user. Example: if you have this ini file content: - - .. code-block:: ini - - [pytest] - addopts = --maxfail=2 -rf # exit after 2 failures, report fail info - - issuing ``pytest test_hello.py`` actually means:: - - pytest --maxfail=2 -rf test_hello.py - - Default is to add no options. - -.. confval:: norecursedirs - - Set the directory basename patterns to avoid when recursing - for test discovery. The individual (fnmatch-style) patterns are - applied to the basename of a directory to decide if to recurse into it. - Pattern matching characters:: - - * matches everything - ? matches any single character - [seq] matches any character in seq - [!seq] matches any char not in seq - - Default patterns are ``'.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', 'venv'``. - Setting a ``norecursedirs`` replaces the default. Here is an example of - how to avoid certain directories: - - .. code-block:: ini - - # content of pytest.ini - [pytest] - norecursedirs = .svn _build tmp* - - This would tell ``pytest`` to not look into typical subversion or - sphinx-build directories or into any ``tmp`` prefixed directory. - - Additionally, ``pytest`` will attempt to intelligently identify and ignore a - virtualenv by the presence of an activation script. Any directory deemed to - be the root of a virtual environment will not be considered during test - collection unless ``‑‑collect‑in‑virtualenv`` is given. Note also that - ``norecursedirs`` takes precedence over ``‑‑collect‑in‑virtualenv``; e.g. if - you intend to run tests in a virtualenv with a base directory that matches - ``'.*'`` you *must* override ``norecursedirs`` in addition to using the - ``‑‑collect‑in‑virtualenv`` flag. - -.. confval:: testpaths - - .. versionadded:: 2.8 - - Sets list of directories that should be searched for tests when - no specific directories, files or test ids are given in the command line when - executing pytest from the :ref:`rootdir ` directory. - Useful when all project tests are in a known location to speed up - test collection and to avoid picking up undesired tests by accident. - - .. code-block:: ini - - # content of pytest.ini - [pytest] - testpaths = testing doc - - This tells pytest to only look for tests in ``testing`` and ``doc`` - directories when executing from the root directory. - -.. confval:: python_files - - One or more Glob-style file patterns determining which python files - are considered as test modules. By default, pytest will consider - any file matching with ``test_*.py`` and ``*_test.py`` globs as a test - module. - -.. confval:: python_classes - - One or more name prefixes or glob-style patterns determining which classes - are considered for test collection. By default, pytest will consider any - class prefixed with ``Test`` as a test collection. Here is an example of how - to collect tests from classes that end in ``Suite``: - - .. code-block:: ini - - # content of pytest.ini - [pytest] - python_classes = *Suite - - Note that ``unittest.TestCase`` derived classes are always collected - regardless of this option, as ``unittest``'s own collection framework is used - to collect those tests. - -.. confval:: python_functions - - One or more name prefixes or glob-patterns determining which test functions - and methods are considered tests. By default, pytest will consider any - function prefixed with ``test`` as a test. Here is an example of how - to collect test functions and methods that end in ``_test``: - - .. code-block:: ini - - # content of pytest.ini - [pytest] - python_functions = *_test - - Note that this has no effect on methods that live on a ``unittest - .TestCase`` derived class, as ``unittest``'s own collection framework is used - to collect those tests. - - See :ref:`change naming conventions` for more detailed examples. - -.. confval:: doctest_optionflags - - One or more doctest flag names from the standard ``doctest`` module. - :doc:`See how pytest handles doctests `. - -.. confval:: confcutdir - - Sets a directory where search upwards for ``conftest.py`` files stops. - By default, pytest will stop searching for ``conftest.py`` files upwards - from ``pytest.ini``/``tox.ini``/``setup.cfg`` of the project if any, - or up to the file-system root. - - -.. confval:: filterwarnings - - .. versionadded:: 3.1 - - Sets a list of filters and actions that should be taken for matched - warnings. By default all warnings emitted during the test session - will be displayed in a summary at the end of the test session. - - .. code-block:: ini - - # content of pytest.ini - [pytest] - filterwarnings = - error - ignore::DeprecationWarning - - This tells pytest to ignore deprecation warnings and turn all other warnings - into errors. For more information please refer to :ref:`warnings`. - -.. confval:: cache_dir - - .. versionadded:: 3.2 - - Sets a directory where stores content of cache plugin. Default directory is - ``.cache`` which is created in :ref:`rootdir `. Directory may be - relative or absolute path. If setting relative path, then directory is created - relative to :ref:`rootdir `. Additionally path may contain environment - variables, that will be expanded. For more information about cache plugin - please refer to :ref:`cache_provider`. +For the full list of options consult the :ref:`reference documentation `. diff --git a/third_party/python/pytest/doc/en/development_guide.rst b/third_party/python/pytest/doc/en/development_guide.rst index 465e97de0f9c..69e866943e57 100644 --- a/third_party/python/pytest/doc/en/development_guide.rst +++ b/third_party/python/pytest/doc/en/development_guide.rst @@ -2,7 +2,7 @@ Development Guide ================= -Some general guidelines regarding development in pytest for core maintainers and general contributors. Nothing here +Some general guidelines regarding development in pytest for maintainers and contributors. Nothing here is set in stone and can't be changed, feel free to suggest improvements or changes in the workflow. @@ -10,7 +10,7 @@ Code Style ---------- * `PEP-8 `_ -* `flake8 `_ for quality checks +* `flake8 `_ for quality checks * `invoke `_ to automate development tasks @@ -37,72 +37,19 @@ Any question, feature, bug or proposal is welcome as an issue. Users are encoura GitHub issues should use labels to categorize them. Labels should be created sporadically, to fill a niche; we should avoid creating labels just for the sake of creating them. -Here is a list of labels and a brief description mentioning their intent. +Each label should include a description in the GitHub's interface stating its purpose. +Temporary labels +~~~~~~~~~~~~~~~~ -**Type** +To classify issues for a special event it is encouraged to create a temporary label. This helps those involved to find +the relevant issues to work on. Examples of that are sprints in Python events or global hacking events. -* ``type: backward compatibility``: issue that will cause problems with old pytest versions. -* ``type: bug``: problem that needs to be addressed. -* ``type: deprecation``: feature that will be deprecated in the future. -* ``type: docs``: documentation missing or needing clarification. -* ``type: enhancement``: new feature or API change, should be merged into ``features``. -* ``type: feature-branch``: new feature or API change, should be merged into ``features``. -* ``type: infrastructure``: improvement to development/releases/CI structure. -* ``type: performance``: performance or memory problem/improvement. -* ``type: proposal``: proposal for a new feature, often to gather opinions or design the API around the new feature. -* ``type: question``: question regarding usage, installation, internals or how to test something. -* ``type: refactoring``: internal improvements to the code. -* ``type: regression``: indicates a problem that was introduced in a release which was working previously. +* ``temporary: EP2017 sprint``: candidate issues or PRs tackled during the EuroPython 2017 -**Status** +Issues created at those events should have other relevant labels added as well. -* ``status: critical``: grave problem or usability issue that affects lots of users. -* ``status: easy``: easy issue that is friendly to new contributors. -* ``status: help wanted``: core developers need help from experts on this topic. -* ``status: needs information``: reporter needs to provide more information; can be closed after 2 or more weeks of inactivity. - -**Topic** - -* ``topic: collection`` -* ``topic: fixtures`` -* ``topic: parametrize`` -* ``topic: reporting`` -* ``topic: selection`` -* ``topic: tracebacks`` - -**Plugin (internal or external)** - -* ``plugin: cache`` -* ``plugin: capture`` -* ``plugin: doctests`` -* ``plugin: junitxml`` -* ``plugin: monkeypatch`` -* ``plugin: nose`` -* ``plugin: pastebin`` -* ``plugin: pytester`` -* ``plugin: tmpdir`` -* ``plugin: unittest`` -* ``plugin: warnings`` -* ``plugin: xdist`` - - -**OS** - -Issues specific to a single operating system. Do not use as a means to indicate where an issue originated from, only -for problems that happen **only** in that system. - -* ``os: linux`` -* ``os: mac`` -* ``os: windows`` - -**Temporary** - -Used to classify issues for limited time, to help find issues related in events for example. -They should be removed after they are no longer relevant. - -* ``temporary: EP2017 sprint``: -* ``temporary: sprint-candidate``: +Those labels should be removed after they are no longer relevant. .. include:: ../../HOWTORELEASE.rst diff --git a/third_party/python/pytest/doc/en/doctest.rst b/third_party/python/pytest/doc/en/doctest.rst index f5800fec2f24..9488ee826572 100644 --- a/third_party/python/pytest/doc/en/doctest.rst +++ b/third_party/python/pytest/doc/en/doctest.rst @@ -61,14 +61,14 @@ and another like this:: then you can just invoke ``pytest`` without command line options:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 1 item - - mymodule.py . - - ======= 1 passed in 0.12 seconds ======== + + mymodule.py . [100%] + + ========================= 1 passed in 0.12 seconds ========================= It is possible to use fixtures using the ``getfixture`` helper:: @@ -81,9 +81,9 @@ Also, :ref:`usefixtures` and :ref:`autouse` fixtures are supported when executing text doctest files. The standard ``doctest`` module provides some setting flags to configure the -strictness of doctest tests. In pytest You can enable those flags those flags -using the configuration file. To make pytest ignore trailing whitespaces and -ignore lengthy exception stack traces you can just write: +strictness of doctest tests. In pytest, you can enable those flags using the +configuration file. To make pytest ignore trailing whitespaces and ignore +lengthy exception stack traces you can just write: .. code-block:: ini @@ -115,6 +115,13 @@ itself:: >>> get_unicode_greeting() # doctest: +ALLOW_UNICODE 'Hello' +By default, pytest would report only the first failure for a given doctest. If +you want to continue the test even when you have failures, do:: + + pytest --doctest-modules --doctest-continue-on-failure + + +.. _`doctest_namespace`: The 'doctest_namespace' fixture ------------------------------- @@ -161,5 +168,3 @@ by using one of standard doctest modules format in options pytest --doctest-modules --doctest-report cdiff pytest --doctest-modules --doctest-report ndiff pytest --doctest-modules --doctest-report only_first_failure - - diff --git a/third_party/python/pytest/doc/en/example/assertion/failure_demo.py b/third_party/python/pytest/doc/en/example/assertion/failure_demo.py index d31fba2adaa2..dc27f5dfbc12 100644 --- a/third_party/python/pytest/doc/en/example/assertion/failure_demo.py +++ b/third_party/python/pytest/doc/en/example/assertion/failure_demo.py @@ -2,135 +2,158 @@ from pytest import raises import _pytest._code import py -def otherfunc(a,b): - assert a==b -def somefunc(x,y): - otherfunc(x,y) +def otherfunc(a, b): + assert a == b + + +def somefunc(x, y): + otherfunc(x, y) + + +def otherfunc_multi(a, b): + assert a == b -def otherfunc_multi(a,b): - assert (a == - b) def test_generative(param1, param2): assert param1 * 2 < param2 + def pytest_generate_tests(metafunc): - if 'param1' in metafunc.fixturenames: + if "param1" in metafunc.fixturenames: metafunc.addcall(funcargs=dict(param1=3, param2=6)) + class TestFailing(object): + def test_simple(self): + def f(): return 42 + def g(): return 43 assert f() == g() def test_simple_multiline(self): - otherfunc_multi( - 42, - 6*9) + otherfunc_multi(42, 6 * 9) def test_not(self): + def f(): return 42 + assert not f() + class TestSpecialisedExplanations(object): + def test_eq_text(self): - assert 'spam' == 'eggs' + assert "spam" == "eggs" def test_eq_similar_text(self): - assert 'foo 1 bar' == 'foo 2 bar' + assert "foo 1 bar" == "foo 2 bar" def test_eq_multiline_text(self): - assert 'foo\nspam\nbar' == 'foo\neggs\nbar' + assert "foo\nspam\nbar" == "foo\neggs\nbar" def test_eq_long_text(self): - a = '1'*100 + 'a' + '2'*100 - b = '1'*100 + 'b' + '2'*100 + a = "1" * 100 + "a" + "2" * 100 + b = "1" * 100 + "b" + "2" * 100 assert a == b def test_eq_long_text_multiline(self): - a = '1\n'*100 + 'a' + '2\n'*100 - b = '1\n'*100 + 'b' + '2\n'*100 + a = "1\n" * 100 + "a" + "2\n" * 100 + b = "1\n" * 100 + "b" + "2\n" * 100 assert a == b def test_eq_list(self): assert [0, 1, 2] == [0, 1, 3] def test_eq_list_long(self): - a = [0]*100 + [1] + [3]*100 - b = [0]*100 + [2] + [3]*100 + a = [0] * 100 + [1] + [3] * 100 + b = [0] * 100 + [2] + [3] * 100 assert a == b def test_eq_dict(self): - assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0} + assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0} def test_eq_set(self): - assert set([0, 10, 11, 12]) == set([0, 20, 21]) + assert {0, 10, 11, 12} == {0, 20, 21} def test_eq_longer_list(self): - assert [1,2] == [1,2,3] + assert [1, 2] == [1, 2, 3] def test_in_list(self): assert 1 in [0, 2, 3, 4, 5] def test_not_in_text_multiline(self): - text = 'some multiline\ntext\nwhich\nincludes foo\nand a\ntail' - assert 'foo' not in text + text = "some multiline\ntext\nwhich\nincludes foo\nand a\ntail" + assert "foo" not in text def test_not_in_text_single(self): - text = 'single foo line' - assert 'foo' not in text + text = "single foo line" + assert "foo" not in text def test_not_in_text_single_long(self): - text = 'head ' * 50 + 'foo ' + 'tail ' * 20 - assert 'foo' not in text + text = "head " * 50 + "foo " + "tail " * 20 + assert "foo" not in text def test_not_in_text_single_long_term(self): - text = 'head ' * 50 + 'f'*70 + 'tail ' * 20 - assert 'f'*70 not in text + text = "head " * 50 + "f" * 70 + "tail " * 20 + assert "f" * 70 not in text def test_attribute(): + class Foo(object): b = 1 + i = Foo() assert i.b == 2 def test_attribute_instance(): + class Foo(object): b = 1 + assert Foo().b == 2 def test_attribute_failure(): + class Foo(object): + def _get_b(self): - raise Exception('Failed to get attrib') + raise Exception("Failed to get attrib") + b = property(_get_b) + i = Foo() assert i.b == 2 def test_attribute_multiple(): + class Foo(object): b = 1 + class Bar(object): b = 2 + assert Foo().b == Bar().b def globf(x): - return x+1 + return x + 1 + class TestRaises(object): + def test_raises(self): - s = 'qwe' + s = "qwe" # NOQA raises(TypeError, "int(s)") def test_raises_doesnt(self): @@ -140,15 +163,15 @@ class TestRaises(object): raise ValueError("demo error") def test_tupleerror(self): - a,b = [1] + a, b = [1] # NOQA def test_reinterpret_fails_with_print_for_the_fun_of_it(self): - l = [1,2,3] - print ("l is %r" % l) - a,b = l.pop() + items = [1, 2, 3] + print("items is %r" % items) + a, b = items.pop() def test_some_error(self): - if namenotexi: + if namenotexi: # NOQA pass def func1(self): @@ -157,31 +180,37 @@ class TestRaises(object): # thanks to Matthew Scott for this test def test_dynamic_compile_shows_nicely(): - src = 'def foo():\n assert 1 == 0\n' - name = 'abc-123' - module = py.std.imp.new_module(name) - code = _pytest._code.compile(src, name, 'exec') + import imp + import sys + + src = "def foo():\n assert 1 == 0\n" + name = "abc-123" + module = imp.new_module(name) + code = _pytest._code.compile(src, name, "exec") py.builtin.exec_(code, module.__dict__) - py.std.sys.modules[name] = module + sys.modules[name] = module module.foo() - class TestMoreErrors(object): + def test_complex_error(self): + def f(): return 44 + def g(): return 43 + somefunc(f(), g()) def test_z1_unpack_error(self): - l = [] - a,b = l + items = [] + a, b = items def test_z2_type_error(self): - l = 3 - a,b = l + items = 3 + a, b = items def test_startswith(self): s = "123" @@ -189,17 +218,20 @@ class TestMoreErrors(object): assert s.startswith(g) def test_startswith_nested(self): + def f(): return "123" + def g(): return "456" + assert f().startswith(g()) def test_global_func(self): assert isinstance(globf(42), float) def test_instance(self): - self.x = 6*7 + self.x = 6 * 7 assert self.x != 42 def test_compare(self): @@ -216,23 +248,31 @@ class TestMoreErrors(object): class TestCustomAssertMsg(object): def test_single_line(self): + class A(object): a = 1 + b = 2 assert A.a == b, "A.a appears not to be b" def test_multiline(self): + class A(object): a = 1 + b = 2 - assert A.a == b, "A.a appears not to be b\n" \ - "or does not appear to be b\none of those" + assert ( + A.a == b + ), "A.a appears not to be b\n" "or does not appear to be b\none of those" def test_custom_repr(self): + class JSON(object): a = 1 + def __repr__(self): return "This is JSON\n{\n 'foo': 'bar'\n}" + a = JSON() b = 2 assert a.a == b, a diff --git a/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/conftest.py b/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/conftest.py index 71e8c54be538..4859bea78e68 100644 --- a/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/conftest.py +++ b/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/conftest.py @@ -1,10 +1,13 @@ -import pytest, py +import pytest +import py + mydir = py.path.local(__file__).dirpath() + def pytest_runtest_setup(item): if isinstance(item, pytest.Function): if not item.fspath.relto(mydir): return mod = item.getparent(pytest.Module).obj - if hasattr(mod, 'hello'): - print ("mod.hello %r" % (mod.hello,)) + if hasattr(mod, "hello"): + print("mod.hello %r" % (mod.hello,)) diff --git a/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/test_hello.py b/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/test_hello_world.py similarity index 97% rename from third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/test_hello.py rename to third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/test_hello_world.py index 828e6b9fd7d7..b945afa67da3 100644 --- a/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/test_hello.py +++ b/third_party/python/pytest/doc/en/example/assertion/global_testmodule_config/test_hello_world.py @@ -1,5 +1,6 @@ hello = "world" + def test_func(): pass diff --git a/third_party/python/pytest/doc/en/example/assertion/test_failures.py b/third_party/python/pytest/doc/en/example/assertion/test_failures.py index 2e5cd20b1945..1150ec12f66d 100644 --- a/third_party/python/pytest/doc/en/example/assertion/test_failures.py +++ b/third_party/python/pytest/doc/en/example/assertion/test_failures.py @@ -1,14 +1,14 @@ import py -failure_demo = py.path.local(__file__).dirpath('failure_demo.py') -pytest_plugins = 'pytester', + +failure_demo = py.path.local(__file__).dirpath("failure_demo.py") +pytest_plugins = "pytester", + def test_failure_demo_fails_properly(testdir): target = testdir.tmpdir.join(failure_demo.basename) failure_demo.copy(target) failure_demo.copy(testdir.tmpdir.join(failure_demo.basename)) result = testdir.runpytest(target, syspathinsert=True) - result.stdout.fnmatch_lines([ - "*42 failed*" - ]) + result.stdout.fnmatch_lines(["*42 failed*"]) assert result.ret != 0 diff --git a/third_party/python/pytest/doc/en/example/assertion/test_setup_flow_example.py b/third_party/python/pytest/doc/en/example/assertion/test_setup_flow_example.py index 100effa499f3..c00711dc2381 100644 --- a/third_party/python/pytest/doc/en/example/assertion/test_setup_flow_example.py +++ b/third_party/python/pytest/doc/en/example/assertion/test_setup_flow_example.py @@ -1,7 +1,9 @@ def setup_module(module): module.TestStateFullThing.classcount = 0 + class TestStateFullThing(object): + def setup_class(cls): cls.classcount += 1 @@ -19,9 +21,11 @@ class TestStateFullThing(object): assert self.classcount == 1 assert self.id == 23 + def teardown_module(module): assert module.TestStateFullThing.classcount == 0 + """ For this example the control flow happens as follows:: import test_setup_flow_example setup_module(test_setup_flow_example) @@ -39,4 +43,3 @@ Note that ``setup_class(TestStateFullThing)`` is called and not to insert ``setup_class = classmethod(setup_class)`` to make your setup function callable. """ - diff --git a/third_party/python/pytest/doc/en/example/attic.rst b/third_party/python/pytest/doc/en/example/attic.rst index 9e124a5d09d3..d6fecf340b62 100644 --- a/third_party/python/pytest/doc/en/example/attic.rst +++ b/third_party/python/pytest/doc/en/example/attic.rst @@ -9,15 +9,18 @@ example: specifying and selecting acceptance tests # ./conftest.py def pytest_option(parser): group = parser.getgroup("myproject") - group.addoption("-A", dest="acceptance", action="store_true", - help="run (slow) acceptance tests") + group.addoption( + "-A", dest="acceptance", action="store_true", help="run (slow) acceptance tests" + ) + def pytest_funcarg__accept(request): return AcceptFixture(request) + class AcceptFixture(object): def __init__(self, request): - if not request.config.getoption('acceptance'): + if not request.config.getoption("acceptance"): pytest.skip("specify -A to run acceptance tests") self.tmpdir = request.config.mktemp(request.function.__name__, numbered=True) @@ -61,6 +64,7 @@ extend the `accept example`_ by putting this in our test module: arg.tmpdir.mkdir("special") return arg + class TestSpecialAcceptance(object): def test_sometest(self, accept): assert accept.tmpdir.join("special").check() diff --git a/third_party/python/pytest/doc/en/example/costlysetup/conftest.py b/third_party/python/pytest/doc/en/example/costlysetup/conftest.py index ea3c1cffb72f..466c62c06ab8 100644 --- a/third_party/python/pytest/doc/en/example/costlysetup/conftest.py +++ b/third_party/python/pytest/doc/en/example/costlysetup/conftest.py @@ -1,16 +1,20 @@ import pytest + @pytest.fixture("session") def setup(request): setup = CostlySetup() yield setup setup.finalize() + class CostlySetup(object): + def __init__(self): import time - print ("performing costly setup") + + print("performing costly setup") time.sleep(5) self.timecostly = 1 diff --git a/third_party/python/pytest/doc/en/example/costlysetup/sub1/__init__.py b/third_party/python/pytest/doc/en/example/costlysetup/sub_a/__init__.py similarity index 100% rename from third_party/python/pytest/doc/en/example/costlysetup/sub1/__init__.py rename to third_party/python/pytest/doc/en/example/costlysetup/sub_a/__init__.py diff --git a/third_party/python/pytest/doc/en/example/costlysetup/sub1/test_quick.py b/third_party/python/pytest/doc/en/example/costlysetup/sub_a/test_quick.py similarity index 96% rename from third_party/python/pytest/doc/en/example/costlysetup/sub1/test_quick.py rename to third_party/python/pytest/doc/en/example/costlysetup/sub_a/test_quick.py index d97657867e02..38dda2660d24 100644 --- a/third_party/python/pytest/doc/en/example/costlysetup/sub1/test_quick.py +++ b/third_party/python/pytest/doc/en/example/costlysetup/sub_a/test_quick.py @@ -1,3 +1,2 @@ - def test_quick(setup): pass diff --git a/third_party/python/pytest/doc/en/example/costlysetup/sub2/__init__.py b/third_party/python/pytest/doc/en/example/costlysetup/sub_b/__init__.py similarity index 100% rename from third_party/python/pytest/doc/en/example/costlysetup/sub2/__init__.py rename to third_party/python/pytest/doc/en/example/costlysetup/sub_b/__init__.py diff --git a/third_party/python/pytest/doc/en/example/costlysetup/sub2/test_two.py b/third_party/python/pytest/doc/en/example/costlysetup/sub_b/test_two.py similarity index 100% rename from third_party/python/pytest/doc/en/example/costlysetup/sub2/test_two.py rename to third_party/python/pytest/doc/en/example/costlysetup/sub_b/test_two.py index 6ed6ee4d898f..b1653aaab88a 100644 --- a/third_party/python/pytest/doc/en/example/costlysetup/sub2/test_two.py +++ b/third_party/python/pytest/doc/en/example/costlysetup/sub_b/test_two.py @@ -1,6 +1,6 @@ def test_something(setup): assert setup.timecostly == 1 + def test_something_more(setup): assert setup.timecostly == 1 - diff --git a/third_party/python/pytest/doc/en/example/markers.rst b/third_party/python/pytest/doc/en/example/markers.rst index e3082f279eeb..bf352bc81298 100644 --- a/third_party/python/pytest/doc/en/example/markers.rst +++ b/third_party/python/pytest/doc/en/example/markers.rst @@ -30,32 +30,30 @@ You can "mark" a test function with custom metadata like this:: You can then restrict a test run to only run tests marked with ``webtest``:: $ pytest -v -m webtest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: - collecting ... collected 4 items - - test_server.py::test_send_http PASSED - - ======= 3 tests deselected ======== - ======= 1 passed, 3 deselected in 0.12 seconds ======== + collecting ... collected 4 items / 3 deselected + + test_server.py::test_send_http PASSED [100%] + + ================== 1 passed, 3 deselected in 0.12 seconds ================== Or the inverse, running all tests except the webtest ones:: $ pytest -v -m "not webtest" - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: - collecting ... collected 4 items - - test_server.py::test_something_quick PASSED - test_server.py::test_another PASSED - test_server.py::TestClass::test_method PASSED - - ======= 1 tests deselected ======== - ======= 3 passed, 1 deselected in 0.12 seconds ======== + collecting ... collected 4 items / 1 deselected + + test_server.py::test_something_quick PASSED [ 33%] + test_server.py::test_another PASSED [ 66%] + test_server.py::TestClass::test_method PASSED [100%] + + ================== 3 passed, 1 deselected in 0.12 seconds ================== Selecting tests based on their node ID -------------------------------------- @@ -65,42 +63,42 @@ arguments to select only specified tests. This makes it easy to select tests based on their module, class, method, or function name:: $ pytest -v test_server.py::TestClass::test_method - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 1 item - - test_server.py::TestClass::test_method PASSED - - ======= 1 passed in 0.12 seconds ======== + + test_server.py::TestClass::test_method PASSED [100%] + + ========================= 1 passed in 0.12 seconds ========================= You can also select on the class:: $ pytest -v test_server.py::TestClass - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 1 item - - test_server.py::TestClass::test_method PASSED - - ======= 1 passed in 0.12 seconds ======== + + test_server.py::TestClass::test_method PASSED [100%] + + ========================= 1 passed in 0.12 seconds ========================= Or select multiple nodes:: $ pytest -v test_server.py::TestClass test_server.py::test_send_http - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 2 items - - test_server.py::TestClass::test_method PASSED - test_server.py::test_send_http PASSED - - ======= 2 passed in 0.12 seconds ======== + + test_server.py::TestClass::test_method PASSED [ 50%] + test_server.py::test_send_http PASSED [100%] + + ========================= 2 passed in 0.12 seconds ========================= .. _node-id: @@ -129,47 +127,44 @@ exact match on markers that ``-m`` provides. This makes it easy to select tests based on their names:: $ pytest -v -k http # running with the above defined example module - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: - collecting ... collected 4 items - - test_server.py::test_send_http PASSED - - ======= 3 tests deselected ======== - ======= 1 passed, 3 deselected in 0.12 seconds ======== + collecting ... collected 4 items / 3 deselected + + test_server.py::test_send_http PASSED [100%] + + ================== 1 passed, 3 deselected in 0.12 seconds ================== And you can also run all tests except the ones that match the keyword:: $ pytest -k "not send_http" -v - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: - collecting ... collected 4 items - - test_server.py::test_something_quick PASSED - test_server.py::test_another PASSED - test_server.py::TestClass::test_method PASSED - - ======= 1 tests deselected ======== - ======= 3 passed, 1 deselected in 0.12 seconds ======== + collecting ... collected 4 items / 1 deselected + + test_server.py::test_something_quick PASSED [ 33%] + test_server.py::test_another PASSED [ 66%] + test_server.py::TestClass::test_method PASSED [100%] + + ================== 3 passed, 1 deselected in 0.12 seconds ================== Or to select "http" and "quick" tests:: $ pytest -k "http or quick" -v - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: - collecting ... collected 4 items - - test_server.py::test_send_http PASSED - test_server.py::test_something_quick PASSED - - ======= 2 tests deselected ======== - ======= 2 passed, 2 deselected in 0.12 seconds ======== + collecting ... collected 4 items / 2 deselected + + test_server.py::test_send_http PASSED [ 50%] + test_server.py::test_something_quick PASSED [100%] + + ================== 2 passed, 2 deselected in 0.12 seconds ================== .. note:: @@ -204,21 +199,21 @@ You can ask which markers exist for your test suite - the list includes our just $ pytest --markers @pytest.mark.webtest: mark a test as a webtest. - + @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test. - + @pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see http://pytest.org/latest/skipping.html - + @pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See http://pytest.org/latest/skipping.html - + @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see http://pytest.org/latest/parametrize.html for more info and examples. - - @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures - + + @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures + @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. - + @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible. - + For an example on how to add and work with markers from a plugin, see :ref:`adding a custom marker from a plugin`. @@ -232,7 +227,7 @@ For an example on how to add and work with markers from a plugin, see * Asking for existing markers via ``pytest --markers`` gives good output * Typos in function markers are treated as an error if you use - the ``--strict`` option. + the ``--strict`` option. .. _`scoped-marking`: @@ -335,11 +330,10 @@ specifies via named environments:: "env(name): mark test to run only on named environment") def pytest_runtest_setup(item): - envmarker = item.get_marker("env") - if envmarker is not None: - envname = envmarker.args[0] - if envname != item.config.getoption("-E"): - pytest.skip("test requires env %r" % envname) + envnames = [mark.args[0] for mark in item.iter_markers(name='env')] + if envnames: + if item.config.getoption("-E") not in envnames: + pytest.skip("test requires env in %r" % envnames) A test file using this local plugin:: @@ -354,46 +348,46 @@ and an example invocations specifying a different environment than what the test needs:: $ pytest -E stage2 - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_someenv.py s - - ======= 1 skipped in 0.12 seconds ======== + + test_someenv.py s [100%] + + ======================== 1 skipped in 0.12 seconds ========================= and here is one that specifies exactly the environment needed:: $ pytest -E stage1 - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_someenv.py . - - ======= 1 passed in 0.12 seconds ======== + + test_someenv.py . [100%] + + ========================= 1 passed in 0.12 seconds ========================= The ``--markers`` option always gives you a list of available markers:: $ pytest --markers @pytest.mark.env(name): mark test to run only on named environment - + @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test. - + @pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see http://pytest.org/latest/skipping.html - + @pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See http://pytest.org/latest/skipping.html - + @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see http://pytest.org/latest/parametrize.html for more info and examples. - - @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures - + + @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures + @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. - + @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible. - + .. _`passing callables to custom markers`: @@ -408,11 +402,9 @@ Below is the config file that will be used in the next examples:: import sys def pytest_runtest_setup(item): - marker = item.get_marker('my_marker') - if marker is not None: - for info in marker: - print('Marker info name={} args={} kwars={}'.format(info.name, info.args, info.kwargs)) - sys.stdout.flush() + for marker in item.iter_markers(name='my_marker'): + print(marker) + sys.stdout.flush() A custom marker can have its argument set, i.e. ``args`` and ``kwargs`` properties, defined by either invoking it as a callable or using ``pytest.mark.MARKER_NAME.with_args``. These two methods achieve the same effect most of the time. @@ -431,7 +423,7 @@ However, if there is a callable as the single positional argument with no keywor The output is as follows:: $ pytest -q -s - Marker info name=my_marker args=(,) kwars={} + Mark(name='my_marker', args=(,), kwargs={}) . 1 passed in 0.12 seconds @@ -465,11 +457,9 @@ test function. From a conftest file we can read it like this:: import sys def pytest_runtest_setup(item): - g = item.get_marker("glob") - if g is not None: - for info in g: - print ("glob args=%s kwargs=%s" %(info.args, info.kwargs)) - sys.stdout.flush() + for mark in item.iter_markers(name='glob'): + print ("glob args=%s kwargs=%s" %(mark.args, mark.kwargs)) + sys.stdout.flush() Let's run this without capturing output and see what we get:: @@ -499,11 +489,10 @@ for your particular platform, you could use the following plugin:: ALL = set("darwin linux win32".split()) def pytest_runtest_setup(item): - if isinstance(item, item.Function): - plat = sys.platform - if not item.get_marker(plat): - if ALL.intersection(item.keywords): - pytest.skip("cannot run on platform %s" %(plat)) + supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers()) + plat = sys.platform + if supported_platforms and plat not in supported_platforms: + pytest.skip("cannot run on platform %s" % (plat)) then tests will be skipped if they were specified for a different platform. Let's do a little test file to show how this looks like:: @@ -530,29 +519,28 @@ Let's do a little test file to show how this looks like:: then you will see two tests skipped and two executed tests as expected:: $ pytest -rs # this option reports skip reasons - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 4 items - - test_plat.py s.s. - ======= short test summary info ======== - SKIP [2] $REGENDOC_TMPDIR/conftest.py:13: cannot run on platform linux - - ======= 2 passed, 2 skipped in 0.12 seconds ======== + + test_plat.py s.s. [100%] + ========================= short test summary info ========================== + SKIP [2] $REGENDOC_TMPDIR/conftest.py:12: cannot run on platform linux + + =================== 2 passed, 2 skipped in 0.12 seconds ==================== Note that if you specify a platform via the marker-command line option like this:: $ pytest -m linux - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: - collected 4 items - - test_plat.py . - - ======= 3 tests deselected ======== - ======= 1 passed, 3 deselected in 0.12 seconds ======== + collected 4 items / 3 deselected + + test_plat.py . [100%] + + ================== 1 passed, 3 deselected in 0.12 seconds ================== then the unmarked-tests will not be run. It is thus a way to restrict the run to the specific tests. @@ -596,47 +584,45 @@ We want to dynamically define two markers and can do it in a We can now use the ``-m option`` to select one set:: $ pytest -m interface --tb=short - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: - collected 4 items - - test_module.py FF - - ======= FAILURES ======== - _______ test_interface_simple ________ + collected 4 items / 2 deselected + + test_module.py FF [100%] + + ================================= FAILURES ================================= + __________________________ test_interface_simple ___________________________ test_module.py:3: in test_interface_simple assert 0 E assert 0 - _______ test_interface_complex ________ + __________________________ test_interface_complex __________________________ test_module.py:6: in test_interface_complex assert 0 E assert 0 - ======= 2 tests deselected ======== - ======= 2 failed, 2 deselected in 0.12 seconds ======== + ================== 2 failed, 2 deselected in 0.12 seconds ================== or to select both "event" and "interface" tests:: $ pytest -m "interface or event" --tb=short - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: - collected 4 items - - test_module.py FFF - - ======= FAILURES ======== - _______ test_interface_simple ________ + collected 4 items / 1 deselected + + test_module.py FFF [100%] + + ================================= FAILURES ================================= + __________________________ test_interface_simple ___________________________ test_module.py:3: in test_interface_simple assert 0 E assert 0 - _______ test_interface_complex ________ + __________________________ test_interface_complex __________________________ test_module.py:6: in test_interface_complex assert 0 E assert 0 - _______ test_event_simple ________ + ____________________________ test_event_simple _____________________________ test_module.py:9: in test_event_simple assert 0 E assert 0 - ======= 1 tests deselected ======== - ======= 3 failed, 1 deselected in 0.12 seconds ======== + ================== 3 failed, 1 deselected in 0.12 seconds ================== diff --git a/third_party/python/pytest/doc/en/example/multipython.py b/third_party/python/pytest/doc/en/example/multipython.py index 586f441848f4..970800c7e614 100644 --- a/third_party/python/pytest/doc/en/example/multipython.py +++ b/third_party/python/pytest/doc/en/example/multipython.py @@ -6,35 +6,48 @@ import py import pytest import _pytest._code -pythonlist = ['python2.6', 'python2.7', 'python3.4', 'python3.5'] +pythonlist = ["python2.7", "python3.4", "python3.5"] + + @pytest.fixture(params=pythonlist) def python1(request, tmpdir): picklefile = tmpdir.join("data.pickle") return Python(request.param, picklefile) + @pytest.fixture(params=pythonlist) def python2(request, python1): return Python(request.param, python1.picklefile) + class Python(object): + def __init__(self, version, picklefile): self.pythonpath = py.path.local.sysfind(version) if not self.pythonpath: - pytest.skip("%r not found" %(version,)) + pytest.skip("%r not found" % (version,)) self.picklefile = picklefile + def dumps(self, obj): dumpfile = self.picklefile.dirpath("dump.py") - dumpfile.write(_pytest._code.Source(""" + dumpfile.write( + _pytest._code.Source( + """ import pickle f = open(%r, 'wb') s = pickle.dump(%r, f, protocol=2) f.close() - """ % (str(self.picklefile), obj))) - py.process.cmdexec("%s %s" %(self.pythonpath, dumpfile)) + """ + % (str(self.picklefile), obj) + ) + ) + py.process.cmdexec("%s %s" % (self.pythonpath, dumpfile)) def load_and_is_true(self, expression): loadfile = self.picklefile.dirpath("load.py") - loadfile.write(_pytest._code.Source(""" + loadfile.write( + _pytest._code.Source( + """ import pickle f = open(%r, 'rb') obj = pickle.load(f) @@ -42,11 +55,15 @@ class Python(object): res = eval(%r) if not res: raise SystemExit(1) - """ % (str(self.picklefile), expression))) - print (loadfile) - py.process.cmdexec("%s %s" %(self.pythonpath, loadfile)) + """ + % (str(self.picklefile), expression) + ) + ) + print(loadfile) + py.process.cmdexec("%s %s" % (self.pythonpath, loadfile)) -@pytest.mark.parametrize("obj", [42, {}, {1:3},]) + +@pytest.mark.parametrize("obj", [42, {}, {1: 3}]) def test_basic_objects(python1, python2, obj): python1.dumps(obj) python2.load_and_is_true("obj == %s" % obj) diff --git a/third_party/python/pytest/doc/en/example/nonpython.rst b/third_party/python/pytest/doc/en/example/nonpython.rst index 5784f6ed6469..4f5adf63fd76 100644 --- a/third_party/python/pytest/doc/en/example/nonpython.rst +++ b/third_party/python/pytest/doc/en/example/nonpython.rst @@ -10,7 +10,7 @@ A basic example for specifying tests in Yaml files -------------------------------------------------------------- .. _`pytest-yamlwsgi`: http://bitbucket.org/aafshar/pytest-yamlwsgi/src/tip/pytest_yamlwsgi.py -.. _`PyYAML`: http://pypi.python.org/pypi/PyYAML/ +.. _`PyYAML`: https://pypi.org/project/PyYAML/ Here is an example ``conftest.py`` (extracted from Ali Afshnars special purpose `pytest-yamlwsgi`_ plugin). This ``conftest.py`` will collect ``test*.yml`` files and will execute the yaml-formatted content as custom tests: @@ -26,19 +26,19 @@ and if you installed `PyYAML`_ or a compatible YAML-parser you can now execute the test specification:: nonpython $ pytest test_simple.yml - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR/nonpython, inifile: collected 2 items - - test_simple.yml F. - - ======= FAILURES ======== - _______ usecase: hello ________ + + test_simple.yml F. [100%] + + ================================= FAILURES ================================= + ______________________________ usecase: hello ______________________________ usecase execution failed spec failed: 'some': 'other' no further details known at this point. - ======= 1 failed, 1 passed in 0.12 seconds ======== + ==================== 1 failed, 1 passed in 0.12 seconds ==================== .. regendoc:wipe @@ -58,21 +58,21 @@ your own domain specific testing language this way. consulted when reporting in ``verbose`` mode:: nonpython $ pytest -v - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR/nonpython, inifile: collecting ... collected 2 items - - test_simple.yml::hello FAILED - test_simple.yml::ok PASSED - - ======= FAILURES ======== - _______ usecase: hello ________ + + test_simple.yml::hello FAILED [ 50%] + test_simple.yml::ok PASSED [100%] + + ================================= FAILURES ================================= + ______________________________ usecase: hello ______________________________ usecase execution failed spec failed: 'some': 'other' no further details known at this point. - ======= 1 failed, 1 passed in 0.12 seconds ======== + ==================== 1 failed, 1 passed in 0.12 seconds ==================== .. regendoc:wipe @@ -80,12 +80,12 @@ While developing your custom test collection and execution it's also interesting to just look at the collection tree:: nonpython $ pytest --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR/nonpython, inifile: collected 2 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= diff --git a/third_party/python/pytest/doc/en/example/nonpython/conftest.py b/third_party/python/pytest/doc/en/example/nonpython/conftest.py index baff30015509..8429dd114b18 100644 --- a/third_party/python/pytest/doc/en/example/nonpython/conftest.py +++ b/third_party/python/pytest/doc/en/example/nonpython/conftest.py @@ -2,18 +2,24 @@ import pytest + def pytest_collect_file(parent, path): if path.ext == ".yml" and path.basename.startswith("test"): return YamlFile(path, parent) + class YamlFile(pytest.File): + def collect(self): - import yaml # we need a yaml parser, e.g. PyYAML + import yaml # we need a yaml parser, e.g. PyYAML + raw = yaml.safe_load(self.fspath.open()) for name, spec in sorted(raw.items()): yield YamlItem(name, self, spec) + class YamlItem(pytest.Item): + def __init__(self, name, parent, spec): super(YamlItem, self).__init__(name, parent) self.spec = spec @@ -27,14 +33,17 @@ class YamlItem(pytest.Item): def repr_failure(self, excinfo): """ called when self.runtest() raises an exception. """ if isinstance(excinfo.value, YamlException): - return "\n".join([ - "usecase execution failed", - " spec failed: %r: %r" % excinfo.value.args[1:3], - " no further details known at this point." - ]) + return "\n".join( + [ + "usecase execution failed", + " spec failed: %r: %r" % excinfo.value.args[1:3], + " no further details known at this point.", + ] + ) def reportinfo(self): return self.fspath, 0, "usecase: %s" % self.name + class YamlException(Exception): """ custom exception for error reporting. """ diff --git a/third_party/python/pytest/doc/en/example/parametrize.rst b/third_party/python/pytest/doc/en/example/parametrize.rst index 1a8de235a1de..882700fec11e 100644 --- a/third_party/python/pytest/doc/en/example/parametrize.rst +++ b/third_party/python/pytest/doc/en/example/parametrize.rst @@ -45,23 +45,23 @@ Now we add a test configuration like this:: This means that we only run 2 tests if we do not pass ``--all``:: $ pytest -q test_compute.py - .. + .. [100%] 2 passed in 0.12 seconds We run only two computations, so we see two dots. let's run the full monty:: $ pytest -q --all - ....F - ======= FAILURES ======== - _______ test_compute[4] ________ - + ....F [100%] + ================================= FAILURES ================================= + _____________________________ test_compute[4] ______________________________ + param1 = 4 - + def test_compute(param1): > assert param1 < 4 E assert 4 < 4 - + test_compute.py:3: AssertionError 1 failed, 4 passed in 0.12 seconds @@ -138,7 +138,7 @@ objects, they are still using the default pytest representation:: $ pytest test_time.py --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 8 items @@ -151,8 +151,8 @@ objects, they are still using the default pytest representation:: - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= In ``test_timedistance_v3``, we used ``pytest.param`` to specify the test IDs together with the actual data, instead of listing them separately. @@ -160,7 +160,7 @@ together with the actual data, instead of listing them separately. A quick port of "testscenarios" ------------------------------------ -.. _`test scenarios`: http://pypi.python.org/pypi/testscenarios/ +.. _`test scenarios`: https://pypi.org/project/testscenarios/ Here is a quick port to run tests configured with `test scenarios`_, an add-on from Robert Collins for the standard unittest framework. We @@ -194,20 +194,20 @@ only have to work a bit to construct the correct arguments for pytest's this is a fully self-contained example which you can run with:: $ pytest test_scenarios.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 4 items - - test_scenarios.py .... - - ======= 4 passed in 0.12 seconds ======== + + test_scenarios.py .... [100%] + + ========================= 4 passed in 0.12 seconds ========================= If you just collect tests you'll also nicely see 'advanced' and 'basic' as variants for the test function:: $ pytest --collect-only test_scenarios.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 4 items @@ -218,8 +218,8 @@ If you just collect tests you'll also nicely see 'advanced' and 'basic' as varia - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= Note that we told ``metafunc.parametrize()`` that your scenario values should be considered class-scoped. With pytest-2.3 this leads to a @@ -272,31 +272,31 @@ creates a database object for the actual test invocations:: Let's first see how it looks like at collection time:: $ pytest test_backends.py --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= And then when we run the test:: $ pytest -q test_backends.py - .F - ======= FAILURES ======== - _______ test_db_initialized[d2] ________ - + .F [100%] + ================================= FAILURES ================================= + _________________________ test_db_initialized[d2] __________________________ + db = - + def test_db_initialized(db): # a dummy test if db.__class__.__name__ == "DB2": > pytest.fail("deliberately failing for demo purposes") E Failed: deliberately failing for demo purposes - + test_backends.py:6: Failed 1 failed, 1 passed in 0.12 seconds @@ -333,14 +333,14 @@ will be passed to respective fixture function:: The result of this test will be successful:: $ pytest test_indirect_list.py --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= .. regendoc:wipe @@ -381,16 +381,16 @@ Our test generator looks up a class-level definition which specifies which argument sets to use for each test function. Let's run it:: $ pytest -q - F.. - ======= FAILURES ======== - _______ TestClass.test_equals[1-2] ________ - + F.. [100%] + ================================= FAILURES ================================= + ________________________ TestClass.test_equals[1-2] ________________________ + self = , a = 1, b = 2 - + def test_equals(self, a, b): > assert a == b E assert 1 == 2 - + test_parametrize.py:18: AssertionError 1 failed, 2 passed in 0.12 seconds @@ -411,10 +411,8 @@ is to be run with different sets of arguments for its three arguments: Running it results in some skips if we don't have all the python interpreters installed and otherwise runs all combinations (5 interpreters times 5 interpreters times 3 objects to serialize/deserialize):: . $ pytest -rs -q multipython.py - sssssssssssssss.........sss.........sss......... - ======= short test summary info ======== - SKIP [21] $REGENDOC_TMPDIR/CWD/multipython.py:24: 'python2.6' not found - 27 passed, 21 skipped in 0.12 seconds + ........................... [100%] + 27 passed in 0.12 seconds Indirect parametrization of optional implementations/imports -------------------------------------------------------------------- @@ -460,18 +458,18 @@ And finally a little test module:: If you run this with reporting for skips enabled:: $ pytest -rs test_module.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_module.py .s - ======= short test summary info ======== - SKIP [1] $REGENDOC_TMPDIR/conftest.py:11: could not import 'opt2' - - ======= 1 passed, 1 skipped in 0.12 seconds ======== -You'll see that we don't have a ``opt2`` module and thus the second test run + test_module.py .s [100%] + ========================= short test summary info ========================== + SKIP [1] $REGENDOC_TMPDIR/conftest.py:11: could not import 'opt2' + + =================== 1 passed, 1 skipped in 0.12 seconds ==================== + +You'll see that we don't have an ``opt2`` module and thus the second test run of our ``test_func1`` was skipped. A few notes: - the fixture functions in the ``conftest.py`` file are "session-scoped" because we @@ -506,10 +504,10 @@ For example:: ]) def test_eval(test_input, expected): assert eval(test_input) == expected - + In this example, we have 4 parametrized tests. Except for the first test, we mark the rest three parametrized tests with the custom marker ``basic``, -and for the fourth test we also use the built-in mark ``xfail`` to indicate this +and for the fourth test we also use the built-in mark ``xfail`` to indicate this test is expected to fail. For explicitness, we set test ids for some tests. Then run ``pytest`` with verbose mode and with only the ``basic`` marker:: diff --git a/third_party/python/pytest/doc/en/example/py2py3/conftest.py b/third_party/python/pytest/doc/en/example/py2py3/conftest.py index 81cd1fb11ea7..5d9a07e3e32a 100644 --- a/third_party/python/pytest/doc/en/example/py2py3/conftest.py +++ b/third_party/python/pytest/doc/en/example/py2py3/conftest.py @@ -3,14 +3,14 @@ import pytest py3 = sys.version_info[0] >= 3 + class DummyCollector(pytest.collect.File): + def collect(self): return [] + def pytest_pycollect_makemodule(path, parent): bn = path.basename if "py3" in bn and not py3 or ("py2" in bn and py3): return DummyCollector(path, parent=parent) - - - diff --git a/third_party/python/pytest/doc/en/example/py2py3/test_py2.py b/third_party/python/pytest/doc/en/example/py2py3/test_py2.py index e09ed9466275..664acf178a00 100644 --- a/third_party/python/pytest/doc/en/example/py2py3/test_py2.py +++ b/third_party/python/pytest/doc/en/example/py2py3/test_py2.py @@ -4,4 +4,3 @@ def test_exception_syntax(): 0/0 except ZeroDivisionError, e: pass - diff --git a/third_party/python/pytest/doc/en/example/py2py3/test_py3.py b/third_party/python/pytest/doc/en/example/py2py3/test_py3.py index a811f2bbc55f..baf0ffbd8220 100644 --- a/third_party/python/pytest/doc/en/example/py2py3/test_py3.py +++ b/third_party/python/pytest/doc/en/example/py2py3/test_py3.py @@ -1,7 +1,5 @@ - def test_exception_syntax(): try: - 0/0 + 0 / 0 except ZeroDivisionError as e: pass - diff --git a/third_party/python/pytest/doc/en/example/pythoncollection.py b/third_party/python/pytest/doc/en/example/pythoncollection.py index 9c4bd31cea02..3603361c3dcf 100644 --- a/third_party/python/pytest/doc/en/example/pythoncollection.py +++ b/third_party/python/pytest/doc/en/example/pythoncollection.py @@ -1,11 +1,15 @@ - # run this with $ pytest --collect-only test_collectonly.py # + + def test_function(): pass + class TestClass(object): + def test_method(self): pass + def test_anothermethod(self): pass diff --git a/third_party/python/pytest/doc/en/example/pythoncollection.rst b/third_party/python/pytest/doc/en/example/pythoncollection.rst index 18ccae66eadf..8e9d3ae62a7d 100644 --- a/third_party/python/pytest/doc/en/example/pythoncollection.rst +++ b/third_party/python/pytest/doc/en/example/pythoncollection.rst @@ -39,6 +39,14 @@ you will see that ``pytest`` only collects test-modules, which do not match the ======= 5 passed in 0.02 seconds ======= +Deselect tests during test collection +------------------------------------- + +Tests can individually be deselected during collection by passing the ``--deselect=item`` option. +For example, say ``tests/foobar/test_foobar_01.py`` contains ``test_a`` and ``test_b``. +You can run all of the tests within ``tests/`` *except* for ``tests/foobar/test_foobar_01.py::test_a`` +by invoking ``pytest`` with ``--deselect tests/foobar/test_foobar_01.py::test_a``. +``pytest`` allows multiple ``--deselect`` options. Keeping duplicate paths specified from command line ---------------------------------------------------- @@ -46,7 +54,7 @@ Keeping duplicate paths specified from command line Default behavior of ``pytest`` is to ignore duplicate paths specified from the command line. Example:: - py.test path_a path_a + pytest path_a path_a ... collected 1 item @@ -57,7 +65,7 @@ Just collect tests once. To collect duplicate tests, use the ``--keep-duplicates`` option on the cli. Example:: - py.test --keep-duplicates path_a path_a + pytest --keep-duplicates path_a path_a ... collected 2 items @@ -67,7 +75,7 @@ As the collector just works on directories, if you specify twice a single test f still collect it twice, no matter if the ``--keep-duplicates`` is not specified. Example:: - py.test test_a.py test_a.py + pytest test_a.py test_a.py ... collected 2 items @@ -116,7 +124,7 @@ that match ``*_check``. For example, if we have:: then the test collection looks like this:: $ pytest --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 2 items @@ -125,8 +133,8 @@ then the test collection looks like this:: - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= .. note:: @@ -162,7 +170,7 @@ Finding out what is collected You can always peek at the collection tree without running tests like this:: . $ pytest --collect-only pythoncollection.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 3 items @@ -172,8 +180,8 @@ You can always peek at the collection tree without running tests like this:: - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= .. _customizing-test-collection: @@ -231,9 +239,9 @@ If you run with a Python 3 interpreter both the one test and the ``setup.py`` file will be left out:: $ pytest --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 0 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= diff --git a/third_party/python/pytest/doc/en/example/reportingdemo.rst b/third_party/python/pytest/doc/en/example/reportingdemo.rst index 2543e840eb6b..4691b128bb83 100644 --- a/third_party/python/pytest/doc/en/example/reportingdemo.rst +++ b/third_party/python/pytest/doc/en/example/reportingdemo.rst @@ -10,115 +10,116 @@ not showing the nice colors here in the HTML that you get on the terminal - we are working on that):: assertion $ pytest failure_demo.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR/assertion, inifile: collected 42 items - - failure_demo.py FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - - ======= FAILURES ======== - _______ test_generative[0] ________ - + + failure_demo.py FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF [100%] + + ================================= FAILURES ================================= + ____________________________ test_generative[0] ____________________________ + param1 = 3, param2 = 6 - + def test_generative(param1, param2): > assert param1 * 2 < param2 E assert (3 * 2) < 6 - - failure_demo.py:16: AssertionError - _______ TestFailing.test_simple ________ - + + failure_demo.py:19: AssertionError + _________________________ TestFailing.test_simple __________________________ + self = - + def test_simple(self): + def f(): return 42 + def g(): return 43 - + > assert f() == g() E assert 42 == 43 E + where 42 = .f at 0xdeadbeef>() E + and 43 = .g at 0xdeadbeef>() - - failure_demo.py:29: AssertionError - _______ TestFailing.test_simple_multiline ________ - + + failure_demo.py:37: AssertionError + ____________________ TestFailing.test_simple_multiline _____________________ + self = - + def test_simple_multiline(self): - otherfunc_multi( - 42, - > 6*9) - - failure_demo.py:34: - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - + > otherfunc_multi(42, 6 * 9) + + failure_demo.py:40: + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + a = 42, b = 54 - - def otherfunc_multi(a,b): - > assert (a == - b) + + def otherfunc_multi(a, b): + > assert a == b E assert 42 == 54 - - failure_demo.py:12: AssertionError - _______ TestFailing.test_not ________ - + + failure_demo.py:15: AssertionError + ___________________________ TestFailing.test_not ___________________________ + self = - + def test_not(self): + def f(): return 42 + > assert not f() E assert not 42 E + where 42 = .f at 0xdeadbeef>() - - failure_demo.py:39: AssertionError - _______ TestSpecialisedExplanations.test_eq_text ________ - + + failure_demo.py:47: AssertionError + _________________ TestSpecialisedExplanations.test_eq_text _________________ + self = - + def test_eq_text(self): - > assert 'spam' == 'eggs' + > assert "spam" == "eggs" E AssertionError: assert 'spam' == 'eggs' E - spam E + eggs - - failure_demo.py:43: AssertionError - _______ TestSpecialisedExplanations.test_eq_similar_text ________ - + + failure_demo.py:53: AssertionError + _____________ TestSpecialisedExplanations.test_eq_similar_text _____________ + self = - + def test_eq_similar_text(self): - > assert 'foo 1 bar' == 'foo 2 bar' + > assert "foo 1 bar" == "foo 2 bar" E AssertionError: assert 'foo 1 bar' == 'foo 2 bar' E - foo 1 bar E ? ^ E + foo 2 bar E ? ^ - - failure_demo.py:46: AssertionError - _______ TestSpecialisedExplanations.test_eq_multiline_text ________ - + + failure_demo.py:56: AssertionError + ____________ TestSpecialisedExplanations.test_eq_multiline_text ____________ + self = - + def test_eq_multiline_text(self): - > assert 'foo\nspam\nbar' == 'foo\neggs\nbar' + > assert "foo\nspam\nbar" == "foo\neggs\nbar" E AssertionError: assert 'foo\nspam\nbar' == 'foo\neggs\nbar' E foo E - spam E + eggs E bar - - failure_demo.py:49: AssertionError - _______ TestSpecialisedExplanations.test_eq_long_text ________ - + + failure_demo.py:59: AssertionError + ______________ TestSpecialisedExplanations.test_eq_long_text _______________ + self = - + def test_eq_long_text(self): - a = '1'*100 + 'a' + '2'*100 - b = '1'*100 + 'b' + '2'*100 + a = "1" * 100 + "a" + "2" * 100 + b = "1" * 100 + "b" + "2" * 100 > assert a == b E AssertionError: assert '111111111111...2222222222222' == '1111111111111...2222222222222' E Skipping 90 identical leading characters in diff, use -v to show @@ -127,15 +128,15 @@ get on the terminal - we are working on that):: E ? ^ E + 1111111111b222222222 E ? ^ - - failure_demo.py:54: AssertionError - _______ TestSpecialisedExplanations.test_eq_long_text_multiline ________ - + + failure_demo.py:64: AssertionError + _________ TestSpecialisedExplanations.test_eq_long_text_multiline __________ + self = - + def test_eq_long_text_multiline(self): - a = '1\n'*100 + 'a' + '2\n'*100 - b = '1\n'*100 + 'b' + '2\n'*100 + a = "1\n" * 100 + "a" + "2\n" * 100 + b = "1\n" * 100 + "b" + "2\n" * 100 > assert a == b E AssertionError: assert '1\n1\n1\n1\n...n2\n2\n2\n2\n' == '1\n1\n1\n1\n1...n2\n2\n2\n2\n' E Skipping 190 identical leading characters in diff, use -v to show @@ -145,40 +146,40 @@ get on the terminal - we are working on that):: E 1 E 1 E 1... - E + E E ...Full output truncated (7 lines hidden), use '-vv' to show - - failure_demo.py:59: AssertionError - _______ TestSpecialisedExplanations.test_eq_list ________ - + + failure_demo.py:69: AssertionError + _________________ TestSpecialisedExplanations.test_eq_list _________________ + self = - + def test_eq_list(self): > assert [0, 1, 2] == [0, 1, 3] E assert [0, 1, 2] == [0, 1, 3] E At index 2 diff: 2 != 3 E Use -v to get the full diff - - failure_demo.py:62: AssertionError - _______ TestSpecialisedExplanations.test_eq_list_long ________ - + + failure_demo.py:72: AssertionError + ______________ TestSpecialisedExplanations.test_eq_list_long _______________ + self = - + def test_eq_list_long(self): - a = [0]*100 + [1] + [3]*100 - b = [0]*100 + [2] + [3]*100 + a = [0] * 100 + [1] + [3] * 100 + b = [0] * 100 + [2] + [3] * 100 > assert a == b E assert [0, 0, 0, 0, 0, 0, ...] == [0, 0, 0, 0, 0, 0, ...] E At index 100 diff: 1 != 2 E Use -v to get the full diff - - failure_demo.py:67: AssertionError - _______ TestSpecialisedExplanations.test_eq_dict ________ - + + failure_demo.py:77: AssertionError + _________________ TestSpecialisedExplanations.test_eq_dict _________________ + self = - + def test_eq_dict(self): - > assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0} + > assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0} E AssertionError: assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0} E Omitting 1 identical items, use -vv to show E Differing items: @@ -187,16 +188,16 @@ get on the terminal - we are working on that):: E {'c': 0} E Right contains more items: E {'d': 0}... - E + E E ...Full output truncated (2 lines hidden), use '-vv' to show - - failure_demo.py:70: AssertionError - _______ TestSpecialisedExplanations.test_eq_set ________ - + + failure_demo.py:80: AssertionError + _________________ TestSpecialisedExplanations.test_eq_set __________________ + self = - + def test_eq_set(self): - > assert set([0, 10, 11, 12]) == set([0, 20, 21]) + > assert {0, 10, 11, 12} == {0, 20, 21} E AssertionError: assert {0, 10, 11, 12} == {0, 20, 21} E Extra items in the left set: E 10 @@ -205,37 +206,37 @@ get on the terminal - we are working on that):: E Extra items in the right set: E 20 E 21... - E + E E ...Full output truncated (2 lines hidden), use '-vv' to show - - failure_demo.py:73: AssertionError - _______ TestSpecialisedExplanations.test_eq_longer_list ________ - + + failure_demo.py:83: AssertionError + _____________ TestSpecialisedExplanations.test_eq_longer_list ______________ + self = - + def test_eq_longer_list(self): - > assert [1,2] == [1,2,3] + > assert [1, 2] == [1, 2, 3] E assert [1, 2] == [1, 2, 3] E Right contains more items, first extra item: 3 E Use -v to get the full diff - - failure_demo.py:76: AssertionError - _______ TestSpecialisedExplanations.test_in_list ________ - + + failure_demo.py:86: AssertionError + _________________ TestSpecialisedExplanations.test_in_list _________________ + self = - + def test_in_list(self): > assert 1 in [0, 2, 3, 4, 5] E assert 1 in [0, 2, 3, 4, 5] - - failure_demo.py:79: AssertionError - _______ TestSpecialisedExplanations.test_not_in_text_multiline ________ - + + failure_demo.py:89: AssertionError + __________ TestSpecialisedExplanations.test_not_in_text_multiline __________ + self = - + def test_not_in_text_multiline(self): - text = 'some multiline\ntext\nwhich\nincludes foo\nand a\ntail' - > assert 'foo' not in text + text = "some multiline\ntext\nwhich\nincludes foo\nand a\ntail" + > assert "foo" not in text E AssertionError: assert 'foo' not in 'some multiline\ntext\nw...ncludes foo\nand a\ntail' E 'foo' is contained here: E some multiline @@ -244,237 +245,254 @@ get on the terminal - we are working on that):: E includes foo E ? +++ E and a... - E + E E ...Full output truncated (2 lines hidden), use '-vv' to show - - failure_demo.py:83: AssertionError - _______ TestSpecialisedExplanations.test_not_in_text_single ________ - + + failure_demo.py:93: AssertionError + ___________ TestSpecialisedExplanations.test_not_in_text_single ____________ + self = - + def test_not_in_text_single(self): - text = 'single foo line' - > assert 'foo' not in text + text = "single foo line" + > assert "foo" not in text E AssertionError: assert 'foo' not in 'single foo line' E 'foo' is contained here: E single foo line E ? +++ - - failure_demo.py:87: AssertionError - _______ TestSpecialisedExplanations.test_not_in_text_single_long ________ - + + failure_demo.py:97: AssertionError + _________ TestSpecialisedExplanations.test_not_in_text_single_long _________ + self = - + def test_not_in_text_single_long(self): - text = 'head ' * 50 + 'foo ' + 'tail ' * 20 - > assert 'foo' not in text + text = "head " * 50 + "foo " + "tail " * 20 + > assert "foo" not in text E AssertionError: assert 'foo' not in 'head head head head hea...ail tail tail tail tail ' E 'foo' is contained here: - E head head foo tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail + E head head foo tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail E ? +++ - - failure_demo.py:91: AssertionError + + failure_demo.py:101: AssertionError ______ TestSpecialisedExplanations.test_not_in_text_single_long_term _______ - + self = - + def test_not_in_text_single_long_term(self): - text = 'head ' * 50 + 'f'*70 + 'tail ' * 20 - > assert 'f'*70 not in text + text = "head " * 50 + "f" * 70 + "tail " * 20 + > assert "f" * 70 not in text E AssertionError: assert 'fffffffffff...ffffffffffff' not in 'head head he...l tail tail ' E 'ffffffffffffffffff...fffffffffffffffffff' is contained here: - E head head fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffftail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail + E head head fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffftail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail E ? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - failure_demo.py:95: AssertionError - _______ test_attribute ________ - + + failure_demo.py:105: AssertionError + ______________________________ test_attribute ______________________________ + def test_attribute(): + class Foo(object): b = 1 + i = Foo() > assert i.b == 2 E assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b - - failure_demo.py:102: AssertionError - _______ test_attribute_instance ________ - + + failure_demo.py:114: AssertionError + _________________________ test_attribute_instance __________________________ + def test_attribute_instance(): + class Foo(object): b = 1 + > assert Foo().b == 2 E AssertionError: assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b E + where .Foo object at 0xdeadbeef> = .Foo'>() - - failure_demo.py:108: AssertionError - _______ test_attribute_failure ________ - + + failure_demo.py:122: AssertionError + __________________________ test_attribute_failure __________________________ + def test_attribute_failure(): + class Foo(object): + def _get_b(self): - raise Exception('Failed to get attrib') + raise Exception("Failed to get attrib") + b = property(_get_b) + i = Foo() > assert i.b == 2 - - failure_demo.py:117: - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - + + failure_demo.py:135: + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + self = .Foo object at 0xdeadbeef> - + def _get_b(self): - > raise Exception('Failed to get attrib') + > raise Exception("Failed to get attrib") E Exception: Failed to get attrib - - failure_demo.py:114: Exception - _______ test_attribute_multiple ________ - + + failure_demo.py:130: Exception + _________________________ test_attribute_multiple __________________________ + def test_attribute_multiple(): + class Foo(object): b = 1 + class Bar(object): b = 2 + > assert Foo().b == Bar().b E AssertionError: assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b E + where .Foo object at 0xdeadbeef> = .Foo'>() E + and 2 = .Bar object at 0xdeadbeef>.b E + where .Bar object at 0xdeadbeef> = .Bar'>() - - failure_demo.py:125: AssertionError - _______ TestRaises.test_raises ________ - + + failure_demo.py:146: AssertionError + __________________________ TestRaises.test_raises __________________________ + self = - + def test_raises(self): - s = 'qwe' + s = "qwe" # NOQA > raises(TypeError, "int(s)") - - failure_demo.py:134: - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - + + failure_demo.py:157: + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + > int(s) E ValueError: invalid literal for int() with base 10: 'qwe' - - <0-codegen $PYTHON_PREFIX/lib/python3.5/site-packages/_pytest/python_api.py:580>:1: ValueError - _______ TestRaises.test_raises_doesnt ________ - + + <0-codegen $PYTHON_PREFIX/lib/python3.5/site-packages/_pytest/python_api.py:634>:1: ValueError + ______________________ TestRaises.test_raises_doesnt _______________________ + self = - + def test_raises_doesnt(self): > raises(IOError, "int('3')") E Failed: DID NOT RAISE - - failure_demo.py:137: Failed - _______ TestRaises.test_raise ________ - + + failure_demo.py:160: Failed + __________________________ TestRaises.test_raise ___________________________ + self = - + def test_raise(self): > raise ValueError("demo error") E ValueError: demo error - - failure_demo.py:140: ValueError - _______ TestRaises.test_tupleerror ________ - + + failure_demo.py:163: ValueError + ________________________ TestRaises.test_tupleerror ________________________ + self = - + def test_tupleerror(self): - > a,b = [1] + > a, b = [1] # NOQA E ValueError: not enough values to unpack (expected 2, got 1) - - failure_demo.py:143: ValueError + + failure_demo.py:166: ValueError ______ TestRaises.test_reinterpret_fails_with_print_for_the_fun_of_it ______ - + self = - + def test_reinterpret_fails_with_print_for_the_fun_of_it(self): - l = [1,2,3] - print ("l is %r" % l) - > a,b = l.pop() + items = [1, 2, 3] + print("items is %r" % items) + > a, b = items.pop() E TypeError: 'int' object is not iterable - - failure_demo.py:148: TypeError + + failure_demo.py:171: TypeError --------------------------- Captured stdout call --------------------------- - l is [1, 2, 3] - _______ TestRaises.test_some_error ________ - + items is [1, 2, 3] + ________________________ TestRaises.test_some_error ________________________ + self = - + def test_some_error(self): - > if namenotexi: + > if namenotexi: # NOQA E NameError: name 'namenotexi' is not defined - - failure_demo.py:151: NameError - _______ test_dynamic_compile_shows_nicely ________ - + + failure_demo.py:174: NameError + ____________________ test_dynamic_compile_shows_nicely _____________________ + def test_dynamic_compile_shows_nicely(): - src = 'def foo():\n assert 1 == 0\n' - name = 'abc-123' - module = py.std.imp.new_module(name) - code = _pytest._code.compile(src, name, 'exec') + import imp + import sys + + src = "def foo():\n assert 1 == 0\n" + name = "abc-123" + module = imp.new_module(name) + code = _pytest._code.compile(src, name, "exec") py.builtin.exec_(code, module.__dict__) - py.std.sys.modules[name] = module + sys.modules[name] = module > module.foo() - - failure_demo.py:166: - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - + + failure_demo.py:192: + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + def foo(): > assert 1 == 0 E AssertionError - - <2-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:163>:2: AssertionError - _______ TestMoreErrors.test_complex_error ________ - + + <2-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:189>:2: AssertionError + ____________________ TestMoreErrors.test_complex_error _____________________ + self = - + def test_complex_error(self): + def f(): return 44 + def g(): return 43 + > somefunc(f(), g()) - - failure_demo.py:176: - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - failure_demo.py:9: in somefunc - otherfunc(x,y) - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - + + failure_demo.py:205: + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + failure_demo.py:11: in somefunc + otherfunc(x, y) + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + a = 44, b = 43 - - def otherfunc(a,b): - > assert a==b + + def otherfunc(a, b): + > assert a == b E assert 44 == 43 - - failure_demo.py:6: AssertionError - _______ TestMoreErrors.test_z1_unpack_error ________ - + + failure_demo.py:7: AssertionError + ___________________ TestMoreErrors.test_z1_unpack_error ____________________ + self = - + def test_z1_unpack_error(self): - l = [] - > a,b = l + items = [] + > a, b = items E ValueError: not enough values to unpack (expected 2, got 0) - - failure_demo.py:180: ValueError - _______ TestMoreErrors.test_z2_type_error ________ - + + failure_demo.py:209: ValueError + ____________________ TestMoreErrors.test_z2_type_error _____________________ + self = - + def test_z2_type_error(self): - l = 3 - > a,b = l + items = 3 + > a, b = items E TypeError: 'int' object is not iterable - - failure_demo.py:184: TypeError - _______ TestMoreErrors.test_startswith ________ - + + failure_demo.py:213: TypeError + ______________________ TestMoreErrors.test_startswith ______________________ + self = - + def test_startswith(self): s = "123" g = "456" @@ -482,108 +500,119 @@ get on the terminal - we are working on that):: E AssertionError: assert False E + where False = ('456') E + where = '123'.startswith - - failure_demo.py:189: AssertionError - _______ TestMoreErrors.test_startswith_nested ________ - + + failure_demo.py:218: AssertionError + __________________ TestMoreErrors.test_startswith_nested ___________________ + self = - + def test_startswith_nested(self): + def f(): return "123" + def g(): return "456" + > assert f().startswith(g()) E AssertionError: assert False E + where False = ('456') E + where = '123'.startswith E + where '123' = .f at 0xdeadbeef>() E + and '456' = .g at 0xdeadbeef>() - - failure_demo.py:196: AssertionError - _______ TestMoreErrors.test_global_func ________ - + + failure_demo.py:228: AssertionError + _____________________ TestMoreErrors.test_global_func ______________________ + self = - + def test_global_func(self): > assert isinstance(globf(42), float) E assert False E + where False = isinstance(43, float) E + where 43 = globf(42) - - failure_demo.py:199: AssertionError - _______ TestMoreErrors.test_instance ________ - + + failure_demo.py:231: AssertionError + _______________________ TestMoreErrors.test_instance _______________________ + self = - + def test_instance(self): - self.x = 6*7 + self.x = 6 * 7 > assert self.x != 42 E assert 42 != 42 E + where 42 = .x - - failure_demo.py:203: AssertionError - _______ TestMoreErrors.test_compare ________ - + + failure_demo.py:235: AssertionError + _______________________ TestMoreErrors.test_compare ________________________ + self = - + def test_compare(self): > assert globf(10) < 5 E assert 11 < 5 E + where 11 = globf(10) - - failure_demo.py:206: AssertionError - _______ TestMoreErrors.test_try_finally ________ - + + failure_demo.py:238: AssertionError + _____________________ TestMoreErrors.test_try_finally ______________________ + self = - + def test_try_finally(self): x = 1 try: > assert x == 0 E assert 1 == 0 - - failure_demo.py:211: AssertionError - _______ TestCustomAssertMsg.test_single_line ________ - + + failure_demo.py:243: AssertionError + ___________________ TestCustomAssertMsg.test_single_line ___________________ + self = - + def test_single_line(self): + class A(object): a = 1 + b = 2 > assert A.a == b, "A.a appears not to be b" E AssertionError: A.a appears not to be b E assert 1 == 2 E + where 1 = .A'>.a - - failure_demo.py:222: AssertionError - _______ TestCustomAssertMsg.test_multiline ________ - + + failure_demo.py:256: AssertionError + ____________________ TestCustomAssertMsg.test_multiline ____________________ + self = - + def test_multiline(self): + class A(object): a = 1 + b = 2 - > assert A.a == b, "A.a appears not to be b\n" \ - "or does not appear to be b\none of those" + > assert ( + A.a == b + ), "A.a appears not to be b\n" "or does not appear to be b\none of those" E AssertionError: A.a appears not to be b E or does not appear to be b E one of those E assert 1 == 2 E + where 1 = .A'>.a - - failure_demo.py:228: AssertionError - _______ TestCustomAssertMsg.test_custom_repr ________ - + + failure_demo.py:264: AssertionError + ___________________ TestCustomAssertMsg.test_custom_repr ___________________ + self = - + def test_custom_repr(self): + class JSON(object): a = 1 + def __repr__(self): return "This is JSON\n{\n 'foo': 'bar'\n}" + a = JSON() b = 2 > assert a.a == b, a @@ -593,6 +622,12 @@ get on the terminal - we are working on that):: E } E assert 1 == 2 E + where 1 = This is JSON\n{\n 'foo': 'bar'\n}.a - - failure_demo.py:238: AssertionError - ======= 42 failed in 0.12 seconds ======== + + failure_demo.py:278: AssertionError + ============================= warnings summary ============================= + + Metafunc.addcall is deprecated and scheduled to be removed in pytest 4.0. + Please use Metafunc.parametrize instead. + + -- Docs: http://doc.pytest.org/en/latest/warnings.html + ================== 42 failed, 1 warnings in 0.12 seconds =================== diff --git a/third_party/python/pytest/doc/en/example/simple.rst b/third_party/python/pytest/doc/en/example/simple.rst index 82347409531e..180637ae994b 100644 --- a/third_party/python/pytest/doc/en/example/simple.rst +++ b/third_party/python/pytest/doc/en/example/simple.rst @@ -3,6 +3,8 @@ Basic patterns and examples ========================================================== +.. _request example: + Pass different values to a test function, depending on command line options ---------------------------------------------------------------------------- @@ -16,10 +18,10 @@ Here is a basic pattern to achieve this: # content of test_sample.py def test_answer(cmdopt): if cmdopt == "type1": - print ("first") + print("first") elif cmdopt == "type2": - print ("second") - assert 0 # to see what was printed + print("second") + assert 0 # to see what was printed For this to work we need to add a command line option and @@ -30,9 +32,12 @@ provide the ``cmdopt`` through a :ref:`fixture function `: # content of conftest.py import pytest + def pytest_addoption(parser): - parser.addoption("--cmdopt", action="store", default="type1", - help="my option: type1 or type2") + parser.addoption( + "--cmdopt", action="store", default="type1", help="my option: type1 or type2" + ) + @pytest.fixture def cmdopt(request): @@ -41,20 +46,20 @@ provide the ``cmdopt`` through a :ref:`fixture function `: Let's run this without supplying our new option:: $ pytest -q test_sample.py - F - ======= FAILURES ======== - _______ test_answer ________ - + F [100%] + ================================= FAILURES ================================= + _______________________________ test_answer ________________________________ + cmdopt = 'type1' - + def test_answer(cmdopt): if cmdopt == "type1": - print ("first") + print("first") elif cmdopt == "type2": - print ("second") - > assert 0 # to see what was printed + print("second") + > assert 0 # to see what was printed E assert 0 - + test_sample.py:6: AssertionError --------------------------- Captured stdout call --------------------------- first @@ -63,20 +68,20 @@ Let's run this without supplying our new option:: And now with supplying a command line option:: $ pytest -q --cmdopt=type2 - F - ======= FAILURES ======== - _______ test_answer ________ - + F [100%] + ================================= FAILURES ================================= + _______________________________ test_answer ________________________________ + cmdopt = 'type2' - + def test_answer(cmdopt): if cmdopt == "type1": - print ("first") + print("first") elif cmdopt == "type2": - print ("second") - > assert 0 # to see what was printed + print("second") + > assert 0 # to see what was printed E assert 0 - + test_sample.py:6: AssertionError --------------------------- Captured stdout call --------------------------- second @@ -100,24 +105,27 @@ the command line arguments before they get processed: # content of conftest.py import sys - def pytest_cmdline_preparse(args): - if 'xdist' in sys.modules: # pytest-xdist plugin + + + def pytest_load_initial_conftests(args): + if "xdist" in sys.modules: # pytest-xdist plugin import multiprocessing + num = max(multiprocessing.cpu_count() / 2, 1) args[:] = ["-n", str(num)] + args -If you have the `xdist plugin `_ installed +If you have the `xdist plugin `_ installed you will now always perform test runs using a number of subprocesses close to your CPU. Running in an empty directory with the above conftest.py:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 0 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= .. _`excontrolskip`: @@ -134,9 +142,13 @@ line option to control skipping of ``pytest.mark.slow`` marked tests: # content of conftest.py import pytest + + def pytest_addoption(parser): - parser.addoption("--runslow", action="store_true", - default=False, help="run slow tests") + parser.addoption( + "--runslow", action="store_true", default=False, help="run slow tests" + ) + def pytest_collection_modifyitems(config, items): if config.getoption("--runslow"): @@ -166,28 +178,28 @@ We can now write a test module like this: and when running it will see a skipped "slow" test:: $ pytest -rs # "-rs" means report details on the little 's' - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_module.py .s - ======= short test summary info ======== + + test_module.py .s [100%] + ========================= short test summary info ========================== SKIP [1] test_module.py:8: need --runslow option to run - - ======= 1 passed, 1 skipped in 0.12 seconds ======== + + =================== 1 passed, 1 skipped in 0.12 seconds ==================== Or run it including the ``slow`` marked test:: $ pytest --runslow - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_module.py .. - - ======= 2 passed in 0.12 seconds ======== + + test_module.py .. [100%] + + ========================= 2 passed in 0.12 seconds ========================= Writing well integrated assertion helpers -------------------------------------------------- @@ -204,10 +216,13 @@ Example: # content of test_checkconfig.py import pytest + + def checkconfig(x): __tracebackhide__ = True if not hasattr(x, "config"): - pytest.fail("not configured: %s" %(x,)) + pytest.fail("not configured: %s" % (x,)) + def test_something(): checkconfig(42) @@ -218,15 +233,15 @@ unless the ``--full-trace`` command line option is specified. Let's run our little function:: $ pytest -q test_checkconfig.py - F - ======= FAILURES ======== - _______ test_something ________ - + F [100%] + ================================= FAILURES ================================= + ______________________________ test_something ______________________________ + def test_something(): > checkconfig(42) E Failed: not configured: 42 - - test_checkconfig.py:8: Failed + + test_checkconfig.py:11: Failed 1 failed in 0.12 seconds If you only want to hide certain exceptions, you can set ``__tracebackhide__`` @@ -238,13 +253,16 @@ this to make sure unexpected exception types aren't hidden: import operator import pytest + class ConfigException(Exception): pass + def checkconfig(x): - __tracebackhide__ = operator.methodcaller('errisinstance', ConfigException) + __tracebackhide__ = operator.methodcaller("errisinstance", ConfigException) if not hasattr(x, "config"): - raise ConfigException("not configured: %s" %(x,)) + raise ConfigException("not configured: %s" % (x,)) + def test_something(): checkconfig(42) @@ -267,22 +285,28 @@ running from a test you can do something like this: # content of conftest.py + def pytest_configure(config): import sys + sys._called_from_test = True + def pytest_unconfigure(config): import sys + del sys._called_from_test and then check for the ``sys._called_from_test`` flag: .. code-block:: python - if hasattr(sys, '_called_from_test'): + if hasattr(sys, "_called_from_test"): # called from within a test run + ... else: # called "normally" + ... accordingly in your application. It's also a good idea to use your own application module rather than ``sys`` @@ -299,19 +323,20 @@ It's easy to present extra information in a ``pytest`` run: # content of conftest.py + def pytest_report_header(config): return "project deps: mylib-1.1" which will add the string to the test header accordingly:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y project deps: mylib-1.1 rootdir: $REGENDOC_TMPDIR, inifile: collected 0 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= .. regendoc:wipe @@ -323,32 +348,33 @@ display more information if applicable: # content of conftest.py + def pytest_report_header(config): - if config.getoption('verbose') > 0: + if config.getoption("verbose") > 0: return ["info1: did you know that ...", "did you?"] which will add info only when run with "--v":: $ pytest -v - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache info1: did you know that ... did you? rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 0 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= and nothing when run plainly:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 0 items - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= profiling test duration -------------------------- @@ -365,30 +391,33 @@ out which tests are the slowest. Let's make an artificial test suite: # content of test_some_are_slow.py import time + def test_funcfast(): time.sleep(0.1) + def test_funcslow1(): time.sleep(0.2) + def test_funcslow2(): time.sleep(0.3) Now we can profile which test functions execute the slowest:: $ pytest --durations=3 - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 3 items - - test_some_are_slow.py ... - - ======= slowest 3 test durations ======== + + test_some_are_slow.py ... [100%] + + ========================= slowest 3 test durations ========================= 0.30s call test_some_are_slow.py::test_funcslow2 0.20s call test_some_are_slow.py::test_funcslow1 0.10s call test_some_are_slow.py::test_funcfast - ======= 3 passed in 0.12 seconds ======== + ========================= 3 passed in 0.12 seconds ========================= incremental testing - test steps --------------------------------------------------- @@ -407,17 +436,19 @@ an ``incremental`` marker which is to be used on classes: import pytest + def pytest_runtest_makereport(item, call): if "incremental" in item.keywords: if call.excinfo is not None: parent = item.parent parent._previousfailed = item + def pytest_runtest_setup(item): if "incremental" in item.keywords: previousfailed = getattr(item.parent, "_previousfailed", None) if previousfailed is not None: - pytest.xfail("previous test failed (%s)" %previousfailed.name) + pytest.xfail("previous test failed (%s)" % previousfailed.name) These two hook implementations work together to abort incremental-marked tests in a class. Here is a test module example: @@ -428,42 +459,46 @@ tests in a class. Here is a test module example: import pytest + @pytest.mark.incremental class TestUserHandling(object): def test_login(self): pass + def test_modification(self): assert 0 + def test_deletion(self): pass + def test_normal(): pass If we run this:: $ pytest -rx - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 4 items - - test_step.py .Fx. - ======= short test summary info ======== - XFAIL test_step.py::TestUserHandling::()::test_deletion - reason: previous test failed (test_modification) - - ======= FAILURES ======== - _______ TestUserHandling.test_modification ________ - + + test_step.py .Fx. [100%] + + ================================= FAILURES ================================= + ____________________ TestUserHandling.test_modification ____________________ + self = - + def test_modification(self): > assert 0 E assert 0 - - test_step.py:9: AssertionError - ======= 1 failed, 2 passed, 1 xfailed in 0.12 seconds ======== + + test_step.py:11: AssertionError + ========================= short test summary info ========================== + XFAIL test_step.py::TestUserHandling::()::test_deletion + reason: previous test failed (test_modification) + ============== 1 failed, 2 passed, 1 xfailed in 0.12 seconds =============== We'll see that ``test_deletion`` was not executed because ``test_modification`` failed. It is reported as an "expected failure". @@ -487,9 +522,11 @@ Here is an example for making a ``db`` fixture available in a directory: # content of a/conftest.py import pytest + class DB(object): pass + @pytest.fixture(scope="session") def db(): return DB() @@ -522,56 +559,56 @@ the ``db`` fixture: We can run this:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 7 items - - test_step.py .Fx. - a/test_db.py F - a/test_db2.py F - b/test_error.py E - - ======= ERRORS ======== - _______ ERROR at setup of test_root ________ + + test_step.py .Fx. [ 57%] + a/test_db.py F [ 71%] + a/test_db2.py F [ 85%] + b/test_error.py E [100%] + + ================================== ERRORS ================================== + _______________________ ERROR at setup of test_root ________________________ file $REGENDOC_TMPDIR/b/test_error.py, line 1 def test_root(db): # no db here, will error out E fixture 'db' not found - > available fixtures: cache, capfd, capsys, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory + > available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. - + $REGENDOC_TMPDIR/b/test_error.py:1 - ======= FAILURES ======== - _______ TestUserHandling.test_modification ________ - + ================================= FAILURES ================================= + ____________________ TestUserHandling.test_modification ____________________ + self = - + def test_modification(self): > assert 0 E assert 0 - - test_step.py:9: AssertionError - _______ test_a1 ________ - + + test_step.py:11: AssertionError + _________________________________ test_a1 __________________________________ + db = - + def test_a1(db): > assert 0, db # to show value E AssertionError: E assert 0 - + a/test_db.py:2: AssertionError - _______ test_a2 ________ - + _________________________________ test_a2 __________________________________ + db = - + def test_a2(db): > assert 0, db # to show value E AssertionError: E assert 0 - + a/test_db2.py:2: AssertionError - ======= 3 failed, 2 passed, 1 xfailed, 1 error in 0.12 seconds ======== + ========== 3 failed, 2 passed, 1 xfailed, 1 error in 0.12 seconds ========== The two test modules in the ``a`` directory see the same ``db`` fixture instance while the one test in the sister-directory ``b`` doesn't see it. We could of course @@ -598,6 +635,7 @@ case we just write some information out to a ``failures`` file: import pytest import os.path + @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object @@ -624,37 +662,39 @@ if you then have failing tests: # content of test_module.py def test_fail1(tmpdir): assert 0 + + def test_fail2(): assert 0 and run them:: $ pytest test_module.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_module.py FF - - ======= FAILURES ======== - _______ test_fail1 ________ - + + test_module.py FF [100%] + + ================================= FAILURES ================================= + ________________________________ test_fail1 ________________________________ + tmpdir = local('PYTEST_TMPDIR/test_fail10') - + def test_fail1(tmpdir): > assert 0 E assert 0 - + test_module.py:2: AssertionError - _______ test_fail2 ________ - + ________________________________ test_fail2 ________________________________ + def test_fail2(): > assert 0 E assert 0 - - test_module.py:4: AssertionError - ======= 2 failed in 0.12 seconds ======== + + test_module.py:6: AssertionError + ========================= 2 failed in 0.12 seconds ========================= you will have a "failures" file which contains the failing test ids:: @@ -676,6 +716,7 @@ here is a little example implemented via a local plugin: import pytest + @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object @@ -694,10 +735,10 @@ here is a little example implemented via a local plugin: # request.node is an "item" because we use the default # "function" scope if request.node.rep_setup.failed: - print ("setting up a test failed!", request.node.nodeid) + print("setting up a test failed!", request.node.nodeid) elif request.node.rep_setup.passed: if request.node.rep_call.failed: - print ("executing test failed", request.node.nodeid) + print("executing test failed", request.node.nodeid) if you then have failing tests: @@ -708,62 +749,68 @@ if you then have failing tests: import pytest + @pytest.fixture def other(): assert 0 + def test_setup_fails(something, other): pass + def test_call_fails(something): assert 0 + def test_fail2(): assert 0 and run it:: $ pytest -s test_module.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 3 items - + test_module.py Esetting up a test failed! test_module.py::test_setup_fails Fexecuting test failed test_module.py::test_call_fails F - - ======= ERRORS ======== - _______ ERROR at setup of test_setup_fails ________ - + + ================================== ERRORS ================================== + ____________________ ERROR at setup of test_setup_fails ____________________ + @pytest.fixture def other(): > assert 0 E assert 0 - - test_module.py:6: AssertionError - ======= FAILURES ======== - _______ test_call_fails ________ - + + test_module.py:7: AssertionError + ================================= FAILURES ================================= + _____________________________ test_call_fails ______________________________ + something = None - + def test_call_fails(something): > assert 0 E assert 0 - - test_module.py:12: AssertionError - _______ test_fail2 ________ - + + test_module.py:15: AssertionError + ________________________________ test_fail2 ________________________________ + def test_fail2(): > assert 0 E assert 0 - - test_module.py:15: AssertionError - ======= 2 failed, 1 error in 0.12 seconds ======== + + test_module.py:19: AssertionError + ==================== 2 failed, 1 error in 0.12 seconds ===================== You'll see that the fixture finalizers could use the precise reporting information. +.. _pytest current test env: + ``PYTEST_CURRENT_TEST`` environment variable -------------------------------------------- @@ -774,7 +821,7 @@ which test got stuck, for example if pytest was run in quiet mode (``-q``) or yo output. This is particularly a problem if the problem helps only sporadically, the famous "flaky" kind of tests. ``pytest`` sets a ``PYTEST_CURRENT_TEST`` environment variable when running tests, which can be inspected -by process monitoring utilities or libraries like `psutil `_ to discover which +by process monitoring utilities or libraries like `psutil `_ to discover which test got stuck if necessary: .. code-block:: python @@ -783,7 +830,7 @@ test got stuck if necessary: for pid in psutil.pids(): environ = psutil.Process(pid).environ() - if 'PYTEST_CURRENT_TEST' in environ: + if "PYTEST_CURRENT_TEST" in environ: print(f'pytest process {pid} running: {environ["PYTEST_CURRENT_TEST"]}') During the test session pytest will set ``PYTEST_CURRENT_TEST`` to the current test @@ -805,7 +852,7 @@ In that order. can be changed between releases (even bug fixes) so it shouldn't be relied on for scripting or automation. -Freezing pytest +Freezing pytest --------------- If you freeze your application using a tool like @@ -817,24 +864,30 @@ while also allowing you to send test files to users so they can run them in thei machines, which can be useful to obtain more information about a hard to reproduce bug. Fortunately recent ``PyInstaller`` releases already have a custom hook -for pytest, but if you are using another tool to freeze executables +for pytest, but if you are using another tool to freeze executables such as ``cx_freeze`` or ``py2exe``, you can use ``pytest.freeze_includes()`` to obtain the full list of internal pytest modules. How to configure the tools to find the internal modules varies from tool to tool, however. -Instead of freezing the pytest runner as a separate executable, you can make +Instead of freezing the pytest runner as a separate executable, you can make your frozen program work as the pytest runner by some clever -argument handling during program startup. This allows you to +argument handling during program startup. This allows you to have a single executable, which is usually more convenient. +Please note that the mechanism for plugin discovery used by pytest +(setupttools entry points) doesn't work with frozen executables so pytest +can't find any third party plugins automatically. To include third party plugins +like ``pytest-timeout`` they must be imported explicitly and passed on to pytest.main. .. code-block:: python # contents of app_main.py import sys + import pytest_timeout # Third party plugin - if len(sys.argv) > 1 and sys.argv[1] == '--pytest': + if len(sys.argv) > 1 and sys.argv[1] == "--pytest": import pytest - sys.exit(pytest.main(sys.argv[2:])) + + sys.exit(pytest.main(sys.argv[2:], plugins=[pytest_timeout])) else: # normal application execution: at this point argv can be parsed # by your argument-parsing library of choice as usual diff --git a/third_party/python/pytest/doc/en/example/xfail_demo.py b/third_party/python/pytest/doc/en/example/xfail_demo.py index 5648575e8787..01e6da1ad2ed 100644 --- a/third_party/python/pytest/doc/en/example/xfail_demo.py +++ b/third_party/python/pytest/doc/en/example/xfail_demo.py @@ -1,29 +1,37 @@ import pytest + xfail = pytest.mark.xfail + @xfail def test_hello(): assert 0 + @xfail(run=False) def test_hello2(): assert 0 + @xfail("hasattr(os, 'sep')") def test_hello3(): assert 0 + @xfail(reason="bug 110") def test_hello4(): assert 0 + @xfail('pytest.__version__[0] != "17"') def test_hello5(): assert 0 + def test_hello6(): pytest.xfail("reason") + @xfail(raises=IndexError) def test_hello7(): x = [] diff --git a/third_party/python/pytest/doc/en/faq.rst b/third_party/python/pytest/doc/en/faq.rst index 27d74e1148c5..5b13818ea5ed 100644 --- a/third_party/python/pytest/doc/en/faq.rst +++ b/third_party/python/pytest/doc/en/faq.rst @@ -30,14 +30,14 @@ and does not handle Deferreds returned from a test in pytest style. If you are using trial's unittest.TestCase chances are that you can just run your tests even if you return Deferreds. In addition, there also is a dedicated `pytest-twisted -`_ plugin which allows you to +`_ plugin which allows you to return deferreds from pytest-style tests, allowing the use of :ref:`fixtures` and other features. how does pytest work with Django? ++++++++++++++++++++++++++++++++++++++++++++++ -In 2012, some work is going into the `pytest-django plugin `_. It substitutes the usage of Django's +In 2012, some work is going into the `pytest-django plugin `_. It substitutes the usage of Django's ``manage.py test`` and allows the use of all pytest features_ most of which are not available from Django directly. diff --git a/third_party/python/pytest/doc/en/fixture.rst b/third_party/python/pytest/doc/en/fixture.rst index 1d7ba8640e17..d4d386792e08 100644 --- a/third_party/python/pytest/doc/en/fixture.rst +++ b/third_party/python/pytest/doc/en/fixture.rst @@ -69,26 +69,26 @@ will discover and call the :py:func:`@pytest.fixture <_pytest.python.fixture>` marked ``smtp`` fixture function. Running the test looks like this:: $ pytest test_smtpsimple.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_smtpsimple.py F - - ======= FAILURES ======== - _______ test_ehlo ________ - + + test_smtpsimple.py F [100%] + + ================================= FAILURES ================================= + ________________________________ test_ehlo _________________________________ + smtp = - + def test_ehlo(smtp): response, msg = smtp.ehlo() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_smtpsimple.py:11: AssertionError - ======= 1 failed in 0.12 seconds ======== + ========================= 1 failed in 0.12 seconds ========================= In the failure traceback we see that the test function was called with a ``smtp`` argument, the ``smtplib.SMTP()`` instance created by the fixture @@ -111,11 +111,11 @@ with a list of available function arguments. .. note:: - You can always issue:: + You can always issue :: pytest --fixtures test_simplefactory.py - to see available fixtures. + to see available fixtures (fixtures with leading ``_`` are only shown if you add the ``-v`` option). Fixtures: a prime example of dependency injection --------------------------------------------------- @@ -141,7 +141,7 @@ automatically gets discovered by pytest. The discovery of fixture functions starts at test classes, then test modules, then ``conftest.py`` files and finally builtin and third party plugins. -You can also use the ``conftest.py`` file to implement +You can also use the ``conftest.py`` file to implement :ref:`local per-directory plugins `. Sharing test data @@ -152,9 +152,9 @@ to do this is by loading these data in a fixture for use by your tests. This makes use of the automatic caching mechanisms of pytest. Another good approach is by adding the data files in the ``tests`` folder. -There are also community plugins available to help managing this aspect of -testing, e.g. `pytest-datadir `__ -and `pytest-datafiles `__. +There are also community plugins available to help managing this aspect of +testing, e.g. `pytest-datadir `__ +and `pytest-datafiles `__. .. _smtpshared: @@ -165,14 +165,14 @@ Scope: sharing a fixture instance across tests in a class, module or session Fixtures requiring network access depend on connectivity and are usually time-expensive to create. Extending the previous example, we -can add a ``scope='module'`` parameter to the +can add a ``scope="module"`` parameter to the :py:func:`@pytest.fixture <_pytest.python.fixture>` invocation to cause the decorated ``smtp`` fixture function to only be invoked once per test *module* (the default is to invoke once per test *function*). Multiple test functions in a test module will thus each receive the same ``smtp`` fixture instance, thus saving time. -The next example puts the fixture function into a separate ``conftest.py`` file +The next example puts the fixture function into a separate ``conftest.py`` file so that tests from multiple test modules in the directory can access the fixture function:: @@ -205,38 +205,38 @@ We deliberately insert failing ``assert 0`` statements in order to inspect what is going on and can now run the tests:: $ pytest test_module.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_module.py FF - - ======= FAILURES ======== - _______ test_ehlo ________ - + + test_module.py FF [100%] + + ================================= FAILURES ================================= + ________________________________ test_ehlo _________________________________ + smtp = - + def test_ehlo(smtp): response, msg = smtp.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes E assert 0 - + test_module.py:6: AssertionError - _______ test_noop ________ - + ________________________________ test_noop _________________________________ + smtp = - + def test_noop(smtp): response, msg = smtp.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_module.py:11: AssertionError - ======= 2 failed in 0.12 seconds ======== + ========================= 2 failed in 0.12 seconds ========================= You see the two ``assert 0`` failing and more importantly you can also see that the same (module-scoped) ``smtp`` object was passed into the two @@ -250,12 +250,61 @@ instance, you can simply declare it: .. code-block:: python @pytest.fixture(scope="session") - def smtp(...): + def smtp(): # the returned fixture value will be shared for # all tests needing it + ... Finally, the ``class`` scope will invoke the fixture once per test *class*. + +Higher-scoped fixtures are instantiated first +--------------------------------------------- + +.. versionadded:: 3.5 + +Within a function request for features, fixture of higher-scopes (such as ``session``) are instantiated first than +lower-scoped fixtures (such as ``function`` or ``class``). The relative order of fixtures of same scope follows +the declared order in the test function and honours dependencies between fixtures. + +Consider the code below: + +.. code-block:: python + + @pytest.fixture(scope="session") + def s1(): + pass + + + @pytest.fixture(scope="module") + def m1(): + pass + + + @pytest.fixture + def f1(tmpdir): + pass + + + @pytest.fixture + def f2(): + pass + + + def test_foo(f1, m1, f2, s1): + ... + + +The fixtures requested by ``test_foo`` will be instantiated in the following order: + +1. ``s1``: is the highest-scoped fixture (``session``). +2. ``m1``: is the second highest-scoped fixture (``module``). +3. ``tmpdir``: is a ``function``-scoped fixture, required by ``f1``: it needs to be instantiated at this point + because it is a dependency of ``f1``. +4. ``f1``: is the first ``function``-scoped fixture in ``test_foo`` parameter list. +5. ``f2``: is the last ``function``-scoped fixture in ``test_foo`` parameter list. + + .. _`finalization`: Fixture finalization / executing teardown code @@ -272,6 +321,7 @@ the code after the *yield* statement serves as the teardown code: import smtplib import pytest + @pytest.fixture(scope="module") def smtp(): smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) @@ -287,7 +337,7 @@ Let's execute it:: $ pytest -s -q --tb=no FFteardown smtp - + 2 failed in 0.12 seconds We see that the ``smtp`` instance is finalized after the two @@ -306,6 +356,7 @@ Note that we can also seamlessly use the ``yield`` syntax with ``with`` statemen import smtplib import pytest + @pytest.fixture(scope="module") def smtp(): with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp: @@ -331,12 +382,15 @@ Here's the ``smtp`` fixture changed to use ``addfinalizer`` for cleanup: import smtplib import pytest + @pytest.fixture(scope="module") def smtp(request): smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) + def fin(): - print ("teardown smtp") + print("teardown smtp") smtp.close() + request.addfinalizer(fin) return smtp # provide the fixture value @@ -369,7 +423,7 @@ ends, but ``addfinalizer`` has two key differences over ``yield``: Fixtures can introspect the requesting test context ------------------------------------------------------------- -Fixture function can accept the :py:class:`request ` object +Fixture functions can accept the :py:class:`request ` object to introspect the "requesting" test function, class or module context. Further extending the previous ``smtp`` fixture example, let's read an optional server URL from the test module which uses our fixture:: @@ -392,7 +446,7 @@ again, nothing much has changed:: $ pytest -s -q --tb=no FFfinalizing (smtp.gmail.com) - + 2 failed in 0.12 seconds Let's quickly create another test module that actually sets the @@ -408,9 +462,9 @@ server URL in its module namespace:: Running it:: $ pytest -qq --tb=short test_anothersmtp.py - F - ======= FAILURES ======== - _______ test_showhelo ________ + F [100%] + ================================= FAILURES ================================= + ______________________________ test_showhelo _______________________________ test_anothersmtp.py:5: in test_showhelo assert 0, smtp.helo() E AssertionError: (250, b'mail.python.org') @@ -421,6 +475,59 @@ Running it:: voila! The ``smtp`` fixture function picked up our mail server name from the module namespace. +.. _`fixture-factory`: + +Factories as fixtures +------------------------------------------------------------- + +The "factory as fixture" pattern can help in situations where the result +of a fixture is needed multiple times in a single test. Instead of returning +data directly, the fixture instead returns a function which generates the data. +This function can then be called multiple times in the test. + +Factories can have have parameters as needed:: + + @pytest.fixture + def make_customer_record(): + + def _make_customer_record(name): + return { + "name": name, + "orders": [] + } + + return _make_customer_record + + + def test_customer_records(make_customer_record): + customer_1 = make_customer_record("Lisa") + customer_2 = make_customer_record("Mike") + customer_3 = make_customer_record("Meredith") + +If the data created by the factory requires managing, the fixture can take care of that:: + + @pytest.fixture + def make_customer_record(): + + created_records = [] + + def _make_customer_record(name): + record = models.Customer(name=name, orders=[]) + created_records.append(record) + return record + + yield _make_customer_record + + for record in created_records: + record.destroy() + + + def test_customer_records(make_customer_record): + customer_1 = make_customer_record("Lisa") + customer_2 = make_customer_record("Mike") + customer_3 = make_customer_record("Meredith") + + .. _`fixture-parametrize`: Parametrizing fixtures @@ -457,54 +564,54 @@ a value via ``request.param``. No test function code needs to change. So let's just do another run:: $ pytest -q test_module.py - FFFF - ======= FAILURES ======== - _______ test_ehlo[smtp.gmail.com] ________ - + FFFF [100%] + ================================= FAILURES ================================= + ________________________ test_ehlo[smtp.gmail.com] _________________________ + smtp = - + def test_ehlo(smtp): response, msg = smtp.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes E assert 0 - + test_module.py:6: AssertionError - _______ test_noop[smtp.gmail.com] ________ - + ________________________ test_noop[smtp.gmail.com] _________________________ + smtp = - + def test_noop(smtp): response, msg = smtp.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_module.py:11: AssertionError - _______ test_ehlo[mail.python.org] ________ - + ________________________ test_ehlo[mail.python.org] ________________________ + smtp = - + def test_ehlo(smtp): response, msg = smtp.ehlo() assert response == 250 > assert b"smtp.gmail.com" in msg E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nPIPELINING\nSIZE 51200000\nETRN\nSTARTTLS\nAUTH DIGEST-MD5 NTLM CRAM-MD5\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8' - + test_module.py:5: AssertionError -------------------------- Captured stdout setup --------------------------- finalizing - _______ test_noop[mail.python.org] ________ - + ________________________ test_noop[mail.python.org] ________________________ + smtp = - + def test_noop(smtp): response, msg = smtp.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_module.py:11: AssertionError ------------------------- Captured stdout teardown ------------------------- finalizing @@ -559,7 +666,7 @@ return ``None`` then pytest's auto-generated ID will be used. Running the above tests results in the following test IDs being used:: $ pytest --collect-only - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 10 items @@ -576,8 +683,42 @@ Running the above tests results in the following test IDs being used:: - - ======= no tests ran in 0.12 seconds ======== + + ======================= no tests ran in 0.12 seconds ======================= + +.. _`fixture-parametrize-marks`: + +Using marks with parametrized fixtures +-------------------------------------- + +:func:`pytest.param` can be used to apply marks in values sets of parametrized fixtures in the same way +that they can be used with :ref:`@pytest.mark.parametrize <@pytest.mark.parametrize>`. + +Example:: + + # content of test_fixture_marks.py + import pytest + @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)]) + def data_set(request): + return request.param + + def test_data(data_set): + pass + +Running this test will *skip* the invocation of ``data_set`` with value ``2``:: + + $ pytest test_fixture_marks.py -v + =========================== test session starts ============================ + platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 + cachedir: .pytest_cache + rootdir: $REGENDOC_TMPDIR, inifile: + collecting ... collected 3 items + + test_fixture_marks.py::test_data[0] PASSED [ 33%] + test_fixture_marks.py::test_data[1] PASSED [ 66%] + test_fixture_marks.py::test_data[2] SKIPPED [100%] + + =================== 2 passed, 1 skipped in 0.12 seconds ==================== .. _`interdependent fixtures`: @@ -610,16 +751,16 @@ Here we declare an ``app`` fixture which receives the previously defined ``smtp`` fixture and instantiates an ``App`` object with it. Let's run it:: $ pytest -v test_appsetup.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 2 items - - test_appsetup.py::test_smtp_exists[smtp.gmail.com] PASSED - test_appsetup.py::test_smtp_exists[mail.python.org] PASSED - - ======= 2 passed in 0.12 seconds ======== + + test_appsetup.py::test_smtp_exists[smtp.gmail.com] PASSED [ 50%] + test_appsetup.py::test_smtp_exists[mail.python.org] PASSED [100%] + + ========================= 2 passed in 0.12 seconds ========================= Due to the parametrization of ``smtp`` the test will run twice with two different ``App`` instances and respective smtp servers. There is no @@ -679,46 +820,46 @@ to show the setup/teardown flow:: Let's run the tests in verbose mode and with looking at the print-output:: $ pytest -v -s test_module.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 8 items - + test_module.py::test_0[1] SETUP otherarg 1 RUN test0 with otherarg 1 PASSED TEARDOWN otherarg 1 - + test_module.py::test_0[2] SETUP otherarg 2 RUN test0 with otherarg 2 PASSED TEARDOWN otherarg 2 - + test_module.py::test_1[mod1] SETUP modarg mod1 RUN test1 with modarg mod1 PASSED - test_module.py::test_2[1-mod1] SETUP otherarg 1 + test_module.py::test_2[mod1-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod1 PASSED TEARDOWN otherarg 1 - - test_module.py::test_2[2-mod1] SETUP otherarg 2 + + test_module.py::test_2[mod1-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod1 PASSED TEARDOWN otherarg 2 - + test_module.py::test_1[mod2] TEARDOWN modarg mod1 SETUP modarg mod2 RUN test1 with modarg mod2 PASSED - test_module.py::test_2[1-mod2] SETUP otherarg 1 + test_module.py::test_2[mod2-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod2 PASSED TEARDOWN otherarg 1 - - test_module.py::test_2[2-mod2] SETUP otherarg 2 + + test_module.py::test_2[mod2-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod2 PASSED TEARDOWN otherarg 2 TEARDOWN modarg mod2 - - - ======= 8 passed in 0.12 seconds ======== + + + ========================= 8 passed in 0.12 seconds ========================= You can see that the parametrized module-scoped ``modarg`` resource caused an ordering of test execution that lead to the fewest possible "active" resources. @@ -781,7 +922,7 @@ you specified a "cleandir" function argument to each of them. Let's run it to verify our fixture is activated and the tests pass:: $ pytest -q - .. + .. [100%] 2 passed in 0.12 seconds You can specify multiple fixtures like this: @@ -789,6 +930,8 @@ You can specify multiple fixtures like this: .. code-block:: python @pytest.mark.usefixtures("cleandir", "anotherfixture") + def test(): + ... and you may specify fixture usage at the test module level, using a generic feature of the mark mechanism: @@ -862,7 +1005,7 @@ class-level ``usefixtures`` decorator. If we run it, we get two passing tests:: $ pytest -q - .. + .. [100%] 2 passed in 0.12 seconds Here is how autouse fixtures work in other scopes: diff --git a/third_party/python/pytest/doc/en/funcarg_compare.rst b/third_party/python/pytest/doc/en/funcarg_compare.rst index b857a014d318..c29ba1f3c357 100644 --- a/third_party/python/pytest/doc/en/funcarg_compare.rst +++ b/third_party/python/pytest/doc/en/funcarg_compare.rst @@ -5,9 +5,9 @@ pytest-2.3: reasoning for fixture/funcarg evolution ============================================================= -**Target audience**: Reading this document requires basic knowledge of -python testing, xUnit setup methods and the (previous) basic pytest -funcarg mechanism, see http://pytest.org/2.2.4/funcargs.html +**Target audience**: Reading this document requires basic knowledge of +python testing, xUnit setup methods and the (previous) basic pytest +funcarg mechanism, see http://pytest.org/2.2.4/funcargs.html If you are new to pytest, then you can simply ignore this section and read the other sections. @@ -18,12 +18,12 @@ Shortcomings of the previous ``pytest_funcarg__`` mechanism The pre pytest-2.3 funcarg mechanism calls a factory each time a funcarg for a test function is required. If a factory wants to -re-use a resource across different scopes, it often used -the ``request.cached_setup()`` helper to manage caching of -resources. Here is a basic example how we could implement +re-use a resource across different scopes, it often used +the ``request.cached_setup()`` helper to manage caching of +resources. Here is a basic example how we could implement a per-session Database object:: - # content of conftest.py + # content of conftest.py class Database(object): def __init__(self): print ("database instance created") @@ -31,7 +31,7 @@ a per-session Database object:: print ("database instance destroyed") def pytest_funcarg__db(request): - return request.cached_setup(setup=DataBase, + return request.cached_setup(setup=DataBase, teardown=lambda db: db.destroy, scope="session") @@ -40,13 +40,13 @@ There are several limitations and difficulties with this approach: 1. Scoping funcarg resource creation is not straight forward, instead one must understand the intricate cached_setup() method mechanics. -2. parametrizing the "db" resource is not straight forward: +2. parametrizing the "db" resource is not straight forward: you need to apply a "parametrize" decorator or implement a - :py:func:`~hookspec.pytest_generate_tests` hook + :py:func:`~hookspec.pytest_generate_tests` hook calling :py:func:`~python.Metafunc.parametrize` which - performs parametrization at the places where the resource - is used. Moreover, you need to modify the factory to use an - ``extrakey`` parameter containing ``request.param`` to the + performs parametrization at the places where the resource + is used. Moreover, you need to modify the factory to use an + ``extrakey`` parameter containing ``request.param`` to the :py:func:`~python.Request.cached_setup` call. 3. Multiple parametrized session-scoped resources will be active @@ -56,7 +56,7 @@ There are several limitations and difficulties with this approach: 4. there is no way how you can make use of funcarg factories in xUnit setup methods. -5. A non-parametrized fixture function cannot use a parametrized +5. A non-parametrized fixture function cannot use a parametrized funcarg resource if it isn't stated in the test function signature. All of these limitations are addressed with pytest-2.3 and its @@ -72,18 +72,18 @@ the scope:: @pytest.fixture(scope="session") def db(request): - # factory will only be invoked once per session - + # factory will only be invoked once per session - db = DataBase() request.addfinalizer(db.destroy) # destroy when session is finished return db This factory implementation does not need to call ``cached_setup()`` anymore -because it will only be invoked once per session. Moreover, the +because it will only be invoked once per session. Moreover, the ``request.addfinalizer()`` registers a finalizer according to the specified resource scope on which the factory function is operating. -Direct parametrization of funcarg resource factories +Direct parametrization of funcarg resource factories ---------------------------------------------------------- Previously, funcarg factories could not directly cause parametrization. @@ -96,9 +96,9 @@ sets. pytest-2.3 introduces a decorator for use on the factory itself:: def db(request): ... # use request.param -Here the factory will be invoked twice (with the respective "mysql" -and "pg" values set as ``request.param`` attributes) and all of -the tests requiring "db" will run twice as well. The "mysql" and +Here the factory will be invoked twice (with the respective "mysql" +and "pg" values set as ``request.param`` attributes) and all of +the tests requiring "db" will run twice as well. The "mysql" and "pg" values will also be used for reporting the test-invocation variants. This new way of parametrizing funcarg factories should in many cases @@ -136,7 +136,7 @@ argument:: The name under which the funcarg resource can be requested is ``db``. -You can still use the "old" non-decorator way of specifying funcarg factories +You can still use the "old" non-decorator way of specifying funcarg factories aka:: def pytest_funcarg__db(request): @@ -156,10 +156,10 @@ several problems: 1. in distributed testing the master process would setup test resources that are never needed because it only co-ordinates the test run - activities of the slave processes. + activities of the slave processes. -2. if you only perform a collection (with "--collect-only") - resource-setup will still be executed. +2. if you only perform a collection (with "--collect-only") + resource-setup will still be executed. 3. If a pytest_sessionstart is contained in some subdirectories conftest.py file, it will not be called. This stems from the @@ -194,17 +194,17 @@ overview of fixture management in your project. Conclusion and compatibility notes --------------------------------------------------------- -**funcargs** were originally introduced to pytest-2.0. In pytest-2.3 +**funcargs** were originally introduced to pytest-2.0. In pytest-2.3 the mechanism was extended and refined and is now described as fixtures: -* previously funcarg factories were specified with a special - ``pytest_funcarg__NAME`` prefix instead of using the +* previously funcarg factories were specified with a special + ``pytest_funcarg__NAME`` prefix instead of using the ``@pytest.fixture`` decorator. * Factories received a ``request`` object which managed caching through - ``request.cached_setup()`` calls and allowed using other funcargs via - ``request.getfuncargvalue()`` calls. These intricate APIs made it hard + ``request.cached_setup()`` calls and allowed using other funcargs via + ``request.getfuncargvalue()`` calls. These intricate APIs made it hard to do proper parametrization and implement resource caching. The new :py:func:`pytest.fixture` decorator allows to declare the scope and let pytest figure things out for you. @@ -212,7 +212,5 @@ fixtures: * if you used parametrization and funcarg factories which made use of ``request.cached_setup()`` it is recommended to invest a few minutes and simplify your fixture function code to use the :ref:`@pytest.fixture` - decorator instead. This will also allow to take advantage of + decorator instead. This will also allow to take advantage of the automatic per-resource grouping of tests. - - diff --git a/third_party/python/pytest/doc/en/funcargs.rst b/third_party/python/pytest/doc/en/funcargs.rst index bc2c04302390..4173675cddfb 100644 --- a/third_party/python/pytest/doc/en/funcargs.rst +++ b/third_party/python/pytest/doc/en/funcargs.rst @@ -11,4 +11,3 @@ and you can read on here: - :ref:`fixtures` - :ref:`parametrize` - :ref:`funcargcompare` - diff --git a/third_party/python/pytest/doc/en/genapi.py b/third_party/python/pytest/doc/en/genapi.py deleted file mode 100644 index 0ede44fa2dee..000000000000 --- a/third_party/python/pytest/doc/en/genapi.py +++ /dev/null @@ -1,41 +0,0 @@ -import textwrap -import inspect - -class Writer(object): - def __init__(self, clsname): - self.clsname = clsname - - def __enter__(self): - self.file = open("%s.api" % self.clsname, "w") - return self - - def __exit__(self, *args): - self.file.close() - print "wrote", self.file.name - - def line(self, line): - self.file.write(line+"\n") - - def docmethod(self, method): - doc = " ".join(method.__doc__.split()) - indent = " " - w = textwrap.TextWrapper(initial_indent=indent, - subsequent_indent=indent) - - spec = inspect.getargspec(method) - del spec.args[0] - self.line(".. py:method:: " + method.__name__ + - inspect.formatargspec(*spec)) - self.line("") - self.line(w.fill(doc)) - self.line("") - -def pytest_funcarg__a(request): - with Writer("request") as writer: - writer.docmethod(request.getfixturevalue) - writer.docmethod(request.cached_setup) - writer.docmethod(request.addfinalizer) - writer.docmethod(request.applymarker) - -def test_hello(a): - pass diff --git a/third_party/python/pytest/doc/en/getting-started.rst b/third_party/python/pytest/doc/en/getting-started.rst index 0b336a41ff9d..f2dbec5e9328 100644 --- a/third_party/python/pytest/doc/en/getting-started.rst +++ b/third_party/python/pytest/doc/en/getting-started.rst @@ -1,40 +1,40 @@ Installation and Getting Started =================================== -**Pythons**: Python 2.6,2.7,3.3,3.4,3.5,3.6 Jython, PyPy-2.3 +**Pythons**: Python 2.7, 3.4, 3.5, 3.6, Jython, PyPy-2.3 **Platforms**: Unix/Posix and Windows -**PyPI package name**: `pytest `_ +**PyPI package name**: `pytest `_ -**dependencies**: `py `_, -`colorama (Windows) `_, -`argparse (py26) `_, -`ordereddict (py26) `_. +**Dependencies**: `py `_, +`colorama (Windows) `_, -**documentation as PDF**: `download latest `_ +**Documentation as PDF**: `download latest `_ + +``pytest`` is a framework that makes building simple and scalable tests easy. Tests are expressive and readable—no boilerplate code required. Get started in minutes with a small unit test or complex functional test for your application or library. .. _`getstarted`: -.. _installation: +.. _`installation`: -Installation +Install ``pytest`` ---------------------------------------- -Installation:: +1. Run the following command in your command line:: pip install -U pytest -To check your installation has installed the correct version:: +2. Check that you installed the correct version:: $ pytest --version This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py .. _`simpletest`: -Our first test run +Create your first test ---------------------------------------------------------- -Let's create a first test file with a simple test function:: +Create a simple test function with just four lines of code:: # content of test_sample.py def func(x): @@ -43,51 +43,43 @@ Let's create a first test file with a simple test function:: def test_answer(): assert func(3) == 5 -That's it. You can execute the test function now:: +That’s it. You can now execute the test function:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_sample.py F - - ======= FAILURES ======== - _______ test_answer ________ - + + test_sample.py F [100%] + + ================================= FAILURES ================================= + _______________________________ test_answer ________________________________ + def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) - - test_sample.py:5: AssertionError - ======= 1 failed in 0.12 seconds ======== -We got a failure report because our little ``func(3)`` call did not return ``5``. + test_sample.py:5: AssertionError + ========================= 1 failed in 0.12 seconds ========================= + +This test returns a failure report because ``func(3)`` does not return ``5``. .. note:: - You can simply use the ``assert`` statement for asserting test - expectations. pytest's :ref:`assert introspection` will intelligently - report intermediate values of the assert expression freeing - you from the need to learn the many names of `JUnit legacy methods`_. + You can use the ``assert`` statement to verify test expectations. pytest’s `Advanced assertion introspection `_ will intelligently report intermediate values of the assert expression so you can avoid the many names `of JUnit legacy methods `_. -.. _`JUnit legacy methods`: http://docs.python.org/library/unittest.html#test-cases - -.. _`assert statement`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement - -Running multiple tests +Run multiple tests ---------------------------------------------------------- -``pytest`` will run all files in the current directory and its subdirectories of the form test_*.py or \*_test.py. More generally, it follows :ref:`standard test discovery rules `. +``pytest`` will run all files of the form test_*.py or \*_test.py in the current directory and its subdirectories. More generally, it follows :ref:`standard test discovery rules `. -Asserting that a certain exception is raised +Assert that a certain exception is raised -------------------------------------------------------------- -If you want to assert that some code raises an exception you can -use the ``raises`` helper:: +Use the ``raises`` helper to assert that some code raises an exception:: # content of test_sysexit.py import pytest @@ -98,18 +90,16 @@ use the ``raises`` helper:: with pytest.raises(SystemExit): f() -Running it with, this time in "quiet" reporting mode:: +Execute the test function with “quiet” reporting mode:: $ pytest -q test_sysexit.py - . + . [100%] 1 passed in 0.12 seconds -Grouping multiple tests in a class +Group multiple tests in a class -------------------------------------------------------------- -Once you start to have more than a few tests it often makes sense -to group tests logically, in classes and modules. Let's write a class -containing two tests:: +Once you develop multiple tests, you may want to group them into a class. pytest makes it easy to create a class containing more than one test:: # content of test_class.py class TestClass(object): @@ -121,80 +111,73 @@ containing two tests:: x = "hello" assert hasattr(x, 'check') -The two tests are found because of the standard :ref:`test discovery`. -There is no need to subclass anything. We can simply -run the module by passing its filename:: +``pytest`` discovers all tests following its :ref:`Conventions for Python test discovery `, so it finds both ``test_`` prefixed functions. There is no need to subclass anything. We can simply run the module by passing its filename:: $ pytest -q test_class.py - .F - ======= FAILURES ======== - _______ TestClass.test_two ________ - + .F [100%] + ================================= FAILURES ================================= + ____________________________ TestClass.test_two ____________________________ + self = - + def test_two(self): x = "hello" > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check') - + test_class.py:8: AssertionError 1 failed, 1 passed in 0.12 seconds -The first test passed, the second failed. Again we can easily see -the intermediate values used in the assertion, helping us to -understand the reason for the failure. +The first test passed and the second failed. You can easily see the intermediate values in the assertion to help you understand the reason for the failure. -Going functional: requesting a unique temporary directory +Request a unique temporary directory for functional tests -------------------------------------------------------------- -For functional tests one often needs to create some files -and pass them to application objects. pytest provides -:ref:`builtinfixtures` which allow to request arbitrary -resources, for example a unique temporary directory:: +``pytest`` provides `Builtin fixtures/function arguments `_ to request arbitrary resources, like a unique temporary directory:: # content of test_tmpdir.py def test_needsfiles(tmpdir): print (tmpdir) assert 0 -We list the name ``tmpdir`` in the test function signature and -``pytest`` will lookup and call a fixture factory to create the resource -before performing the test function call. Let's just run it:: +List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:: $ pytest -q test_tmpdir.py - F - ======= FAILURES ======== - _______ test_needsfiles ________ - + F [100%] + ================================= FAILURES ================================= + _____________________________ test_needsfiles ______________________________ + tmpdir = local('PYTEST_TMPDIR/test_needsfiles0') - + def test_needsfiles(tmpdir): print (tmpdir) > assert 0 E assert 0 - + test_tmpdir.py:3: AssertionError --------------------------- Captured stdout call --------------------------- PYTEST_TMPDIR/test_needsfiles0 1 failed in 0.12 seconds -Before the test runs, a unique-per-test-invocation temporary directory -was created. More info at :ref:`tmpdir handling`. +More info on tmpdir handling is available at :ref:`Temporary directories and files `. -You can find out what kind of builtin :ref:`fixtures` exist by typing:: +Find out what kind of builtin :ref:`pytest fixtures ` exist with the command:: pytest --fixtures # shows builtin and custom fixtures -Where to go next +Note that this command omits fixtures with leading ``_`` unless the ``-v`` option is added. + +Continue reading ------------------------------------- -Here are a few suggestions where to go next: +Check out additional pytest resources to help you customize tests for your unique workflow: -* :ref:`cmdline` for command line invocation examples -* :ref:`good practices ` for virtualenv, test layout -* :ref:`existingtestsuite` for working with pre-existing tests -* :ref:`fixtures` for providing a functional baseline to your tests -* :ref:`plugins` managing and writing plugins +* ":ref:`cmdline`" for command line invocation examples +* ":ref:`existingtestsuite`" for working with pre-existing tests +* ":ref:`mark`" for information on the ``pytest.mark`` mechanism +* ":ref:`fixtures`" for providing a functional baseline to your tests +* ":ref:`plugins`" for managing and writing plugins +* ":ref:`goodpractices`" for virtualenv and test layouts .. include:: links.inc diff --git a/third_party/python/pytest/doc/en/goodpractices.rst b/third_party/python/pytest/doc/en/goodpractices.rst index 16fdd24c3927..2bbd9d0ae584 100644 --- a/third_party/python/pytest/doc/en/goodpractices.rst +++ b/third_party/python/pytest/doc/en/goodpractices.rst @@ -145,7 +145,7 @@ Note that this layout also works in conjunction with the ``src`` layout mentione .. note:: - If ``pytest`` finds a "a/b/test_module.py" test file while + If ``pytest`` finds an "a/b/test_module.py" test file while recursing into the filesystem it determines the import name as follows: @@ -168,13 +168,13 @@ Note that this layout also works in conjunction with the ``src`` layout mentione to avoid surprises such as a test module getting imported twice. -.. _`virtualenv`: http://pypi.python.org/pypi/virtualenv +.. _`virtualenv`: https://pypi.org/project/virtualenv/ .. _`buildout`: http://www.buildout.org/ -.. _pip: http://pypi.python.org/pypi/pip +.. _pip: https://pypi.org/project/pip/ .. _`use tox`: -Tox +tox ------ For development, we recommend to use virtualenv_ environments and pip_ @@ -194,7 +194,7 @@ Once you are done with your work and want to make sure that your actual package passes all tests you may want to look into `tox`_, the virtualenv test automation tool and its `pytest support `_. -Tox helps you to setup virtualenv environments with pre-defined +tox helps you to setup virtualenv environments with pre-defined dependencies and then executing a pre-configured test command with options. It will run tests against the installed package and not against your source code checkout, helping to detect packaging @@ -205,7 +205,7 @@ Integrating with setuptools / ``python setup.py test`` / ``pytest-runner`` -------------------------------------------------------------------------- You can integrate test runs into your setuptools based project -with the `pytest-runner `_ plugin. +with the `pytest-runner `_ plugin. Add this to ``setup.py`` file: @@ -214,10 +214,10 @@ Add this to ``setup.py`` file: from setuptools import setup setup( - #..., - setup_requires=['pytest-runner', ...], - tests_require=['pytest', ...], - #..., + # ..., + setup_requires=["pytest-runner", ...], + tests_require=["pytest", ...], + # ..., ) @@ -263,25 +263,27 @@ your own setuptools Test command for invoking pytest. class PyTest(TestCommand): - user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")] + user_options = [("pytest-args=", "a", "Arguments to pass to pytest")] def initialize_options(self): TestCommand.initialize_options(self) - self.pytest_args = '' + self.pytest_args = "" def run_tests(self): import shlex - #import here, cause outside the eggs aren't loaded + + # import here, cause outside the eggs aren't loaded import pytest + errno = pytest.main(shlex.split(self.pytest_args)) sys.exit(errno) setup( - #..., - tests_require=['pytest'], - cmdclass = {'test': PyTest}, - ) + # ..., + tests_require=["pytest"], + cmdclass={"test": PyTest}, + ) Now if you run:: diff --git a/third_party/python/pytest/doc/en/index.rst b/third_party/python/pytest/doc/en/index.rst index 1d2ca57efbb7..6a382e57173a 100644 --- a/third_party/python/pytest/doc/en/index.rst +++ b/third_party/python/pytest/doc/en/index.rst @@ -17,6 +17,7 @@ An example of a simple test: def inc(x): return x + 1 + def test_answer(): assert inc(3) == 5 @@ -24,23 +25,23 @@ An example of a simple test: To execute it:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_sample.py F - - ======= FAILURES ======== - _______ test_answer ________ - + + test_sample.py F [100%] + + ================================= FAILURES ================================= + _______________________________ test_answer ________________________________ + def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) - - test_sample.py:5: AssertionError - ======= 1 failed in 0.12 seconds ======== + + test_sample.py:6: AssertionError + ========================= 1 failed in 0.12 seconds ========================= Due to ``pytest``'s detailed assertion introspection, only plain ``assert`` statements are used. See :ref:`Getting Started ` for more examples. @@ -57,7 +58,7 @@ Features - Can run :ref:`unittest ` (including trial) and :ref:`nose ` test suites out of the box; -- Python2.6+, Python3.3+, PyPy-2.3, Jython-2.5 (untested); +- Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; diff --git a/third_party/python/pytest/doc/en/links.inc b/third_party/python/pytest/doc/en/links.inc index b69390baa84b..4d5a4b1eebf2 100644 --- a/third_party/python/pytest/doc/en/links.inc +++ b/third_party/python/pytest/doc/en/links.inc @@ -7,14 +7,14 @@ .. _`reStructured Text`: http://docutils.sourceforge.net .. _`Python debugger`: http://docs.python.org/lib/module-pdb.html .. _nose: https://nose.readthedocs.io/en/latest/ -.. _pytest: http://pypi.python.org/pypi/pytest +.. _pytest: https://pypi.org/project/pytest/ .. _mercurial: http://mercurial.selenic.com/wiki/ -.. _`setuptools`: http://pypi.python.org/pypi/setuptools +.. _`setuptools`: https://pypi.org/project/setuptools/ .. _`easy_install`: .. _`distribute docs`: -.. _`distribute`: http://pypi.python.org/pypi/distribute -.. _`pip`: http://pypi.python.org/pypi/pip -.. _`virtualenv`: http://pypi.python.org/pypi/virtualenv +.. _`distribute`: https://pypi.org/project/distribute/ +.. _`pip`: https://pypi.org/project/pip/ +.. _`virtualenv`: https://pypi.org/project/virtualenv/ .. _hudson: http://hudson-ci.org/ .. _jenkins: http://jenkins-ci.org/ .. _tox: http://testrun.org/tox diff --git a/third_party/python/pytest/doc/en/logging.rst b/third_party/python/pytest/doc/en/logging.rst new file mode 100644 index 000000000000..00829c15eef7 --- /dev/null +++ b/third_party/python/pytest/doc/en/logging.rst @@ -0,0 +1,245 @@ +.. _logging: + +Logging +------- + +.. versionadded:: 3.3 +.. versionchanged:: 3.4 + +pytest captures log messages of level ``WARNING`` or above automatically and displays them in their own section +for each failed test in the same manner as captured stdout and stderr. + +Running without options:: + + pytest + +Shows failed tests like so:: + + ----------------------- Captured stdlog call ---------------------- + test_reporting.py 26 WARNING text going to logger + ----------------------- Captured stdout call ---------------------- + text going to stdout + ----------------------- Captured stderr call ---------------------- + text going to stderr + ==================== 2 failed in 0.02 seconds ===================== + +By default each captured log message shows the module, line number, log level +and message. + +If desired the log and date format can be specified to +anything that the logging module supports by passing specific formatting options:: + + pytest --log-format="%(asctime)s %(levelname)s %(message)s" \ + --log-date-format="%Y-%m-%d %H:%M:%S" + +Shows failed tests like so:: + + ----------------------- Captured stdlog call ---------------------- + 2010-04-10 14:48:44 WARNING text going to logger + ----------------------- Captured stdout call ---------------------- + text going to stdout + ----------------------- Captured stderr call ---------------------- + text going to stderr + ==================== 2 failed in 0.02 seconds ===================== + +These options can also be customized through ``pytest.ini`` file: + +.. code-block:: ini + + [pytest] + log_format = %(asctime)s %(levelname)s %(message)s + log_date_format = %Y-%m-%d %H:%M:%S + +Further it is possible to disable reporting of captured content (stdout, +stderr and logs) on failed tests completely with:: + + pytest --show-capture=no + + +caplog fixture +^^^^^^^^^^^^^^ + +Inside tests it is possible to change the log level for the captured log +messages. This is supported by the ``caplog`` fixture:: + + def test_foo(caplog): + caplog.set_level(logging.INFO) + pass + +By default the level is set on the root logger, +however as a convenience it is also possible to set the log level of any +logger:: + + def test_foo(caplog): + caplog.set_level(logging.CRITICAL, logger='root.baz') + pass + +The log levels set are restored automatically at the end of the test. + +It is also possible to use a context manager to temporarily change the log +level inside a ``with`` block:: + + def test_bar(caplog): + with caplog.at_level(logging.INFO): + pass + +Again, by default the level of the root logger is affected but the level of any +logger can be changed instead with:: + + def test_bar(caplog): + with caplog.at_level(logging.CRITICAL, logger='root.baz'): + pass + +Lastly all the logs sent to the logger during the test run are made available on +the fixture in the form of both the ``logging.LogRecord`` instances and the final log text. +This is useful for when you want to assert on the contents of a message:: + + def test_baz(caplog): + func_under_test() + for record in caplog.records: + assert record.levelname != 'CRITICAL' + assert 'wally' not in caplog.text + +For all the available attributes of the log records see the +``logging.LogRecord`` class. + +You can also resort to ``record_tuples`` if all you want to do is to ensure, +that certain messages have been logged under a given logger name with a given +severity and message:: + + def test_foo(caplog): + logging.getLogger().info('boo %s', 'arg') + + assert caplog.record_tuples == [ + ('root', logging.INFO, 'boo arg'), + ] + +You can call ``caplog.clear()`` to reset the captured log records in a test:: + + def test_something_with_clearing_records(caplog): + some_method_that_creates_log_records() + caplog.clear() + your_test_method() + assert ['Foo'] == [rec.message for rec in caplog.records] + + +The ``caplog.records`` attribute contains records from the current stage only, so +inside the ``setup`` phase it contains only setup logs, same with the ``call`` and +``teardown`` phases. + +To access logs from other stages, use the ``caplog.get_records(when)`` method. As an example, +if you want to make sure that tests which use a certain fixture never log any warnings, you can inspect +the records for the ``setup`` and ``call`` stages during teardown like so: + +.. code-block:: python + + + @pytest.fixture + def window(caplog): + window = create_window() + yield window + for when in ("setup", "call"): + messages = [ + x.message for x in caplog.get_records(when) if x.level == logging.WARNING + ] + if messages: + pytest.fail( + "warning messages encountered during testing: {}".format(messages) + ) + + + +The full API is available at :class:`_pytest.logging.LogCaptureFixture`. + + +.. _live_logs: + +Live Logs +^^^^^^^^^ + +By setting the :confval:`log_cli` configuration option to ``true``, pytest will output +logging records as they are emitted directly into the console. + +You can specify the logging level for which log records with equal or higher +level are printed to the console by passing ``--log-cli-level``. This setting +accepts the logging level names as seen in python's documentation or an integer +as the logging level num. + +Additionally, you can also specify ``--log-cli-format`` and +``--log-cli-date-format`` which mirror and default to ``--log-format`` and +``--log-date-format`` if not provided, but are applied only to the console +logging handler. + +All of the CLI log options can also be set in the configuration INI file. The +option names are: + +* ``log_cli_level`` +* ``log_cli_format`` +* ``log_cli_date_format`` + +If you need to record the whole test suite logging calls to a file, you can pass +``--log-file=/path/to/log/file``. This log file is opened in write mode which +means that it will be overwritten at each run tests session. + +You can also specify the logging level for the log file by passing +``--log-file-level``. This setting accepts the logging level names as seen in +python's documentation(ie, uppercased level names) or an integer as the logging +level num. + +Additionally, you can also specify ``--log-file-format`` and +``--log-file-date-format`` which are equal to ``--log-format`` and +``--log-date-format`` but are applied to the log file logging handler. + +All of the log file options can also be set in the configuration INI file. The +option names are: + +* ``log_file`` +* ``log_file_level`` +* ``log_file_format`` +* ``log_file_date_format`` + +.. _log_release_notes: + +Release notes +^^^^^^^^^^^^^ + +This feature was introduced as a drop-in replacement for the `pytest-catchlog +`_ plugin and they conflict +with each other. The backward compatibility API with ``pytest-capturelog`` +has been dropped when this feature was introduced, so if for that reason you +still need ``pytest-catchlog`` you can disable the internal feature by +adding to your ``pytest.ini``: + +.. code-block:: ini + + [pytest] + addopts=-p no:logging + + +.. _log_changes_3_4: + +Incompatible changes in pytest 3.4 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +This feature was introduced in ``3.3`` and some **incompatible changes** have been +made in ``3.4`` after community feedback: + +* Log levels are no longer changed unless explicitly requested by the :confval:`log_level` configuration + or ``--log-level`` command-line options. This allows users to configure logger objects themselves. +* :ref:`Live Logs ` is now disabled by default and can be enabled setting the + :confval:`log_cli` configuration option to ``true``. When enabled, the verbosity is increased so logging for each + test is visible. +* :ref:`Live Logs ` are now sent to ``sys.stdout`` and no longer require the ``-s`` command-line option + to work. + +If you want to partially restore the logging behavior of version ``3.3``, you can add this options to your ``ini`` +file: + +.. code-block:: ini + + [pytest] + log_cli=true + log_level=NOTSET + +More details about the discussion that lead to this changes can be read in +issue `#3013 `_. diff --git a/third_party/python/pytest/doc/en/mark.rst b/third_party/python/pytest/doc/en/mark.rst index 0b0e072a09ba..aa1210bb6b5a 100644 --- a/third_party/python/pytest/doc/en/mark.rst +++ b/third_party/python/pytest/doc/en/mark.rst @@ -4,7 +4,6 @@ Marking test functions with attributes ================================================================= -.. currentmodule:: _pytest.mark By using the ``pytest.mark`` helper you can easily set metadata on your test functions. There are @@ -27,15 +26,134 @@ which also serve as documentation. :ref:`fixtures `. -API reference for mark related objects ------------------------------------------------- +Raising errors on unknown marks: --strict +----------------------------------------- -.. autoclass:: MarkGenerator - :members: +When the ``--strict`` command-line flag is passed, any marks not registered in the ``pytest.ini`` file will trigger an error. -.. autoclass:: MarkDecorator - :members: +Marks can be registered like this: -.. autoclass:: MarkInfo - :members: +.. code-block:: ini + [pytest] + markers = + slow + serial + +This can be used to prevent users mistyping mark names by accident. Test suites that want to enforce this +should add ``--strict`` to ``addopts``: + +.. code-block:: ini + + [pytest] + addopts = --strict + markers = + slow + serial + + +.. `marker-iteration` + +Marker revamp and iteration +--------------------------- + +.. versionadded:: 3.6 + +pytest's marker implementation traditionally worked by simply updating the ``__dict__`` attribute of functions to add markers, in a cumulative manner. As a result of the this, markers would unintendely be passed along class hierarchies in surprising ways plus the API for retriving them was inconsistent, as markers from parameterization would be stored differently than markers applied using the ``@pytest.mark`` decorator and markers added via ``node.add_marker``. + +This state of things made it technically next to impossible to use data from markers correctly without having a deep understanding of the internals, leading to subtle and hard to understand bugs in more advanced usages. + +Depending on how a marker got declared/changed one would get either a ``MarkerInfo`` which might contain markers from sibling classes, +``MarkDecorators`` when marks came from parameterization or from a ``node.add_marker`` call, discarding prior marks. Also ``MarkerInfo`` acts like a single mark, when it in fact represents a merged view on multiple marks with the same name. + +On top of that markers where not accessible the same way for modules, classes, and functions/methods, +in fact, markers where only accessible in functions, even if they where declared on classes/modules. + +A new API to access markers has been introduced in pytest 3.6 in order to solve the problems with the initial design, providing :func:`_pytest.nodes.Node.iter_markers` method to iterate over markers in a consistent manner and reworking the internals, which solved great deal of problems with the initial design. + + +.. _update marker code: + +Updating code +~~~~~~~~~~~~~ + +The old ``Node.get_marker(name)`` function is considered deprecated because it returns an internal ``MarkerInfo`` object +which contains the merged name, ``*args`` and ``**kwargs`` of all the markers which apply to that node. + +In general there are two scenarios on how markers should be handled: + +1. Marks overwrite each other. Order matters but you only want to think of your mark as a single item. E.g. +``log_level('info')`` at a module level can be overwritten by ``log_level('debug')`` for a specific test. + + In this case replace use ``Node.get_closest_marker(name)``: + + .. code-block:: python + + # replace this: + marker = item.get_marker("log_level") + if marker: + level = marker.args[0] + + # by this: + marker = item.get_closest_marker("log_level") + if marker: + level = marker.args[0] + +2. Marks compose additive. E.g. ``skipif(condition)`` marks means you just want to evaluate all of them, +order doesn't even matter. You probably want to think of your marks as a set here. + + In this case iterate over each mark and handle their ``*args`` and ``**kwargs`` individually. + + .. code-block:: python + + # replace this + skipif = item.get_marker("skipif") + if skipif: + for condition in skipif.args: + # eval condition + ... + + # by this: + for skipif in item.iter_markers("skipif"): + condition = skipif.args[0] + # eval condition + + +If you are unsure or have any questions, please consider opening +`an issue `_. + +Related issues +~~~~~~~~~~~~~~ + +Here is a non-exhaustive list of issues fixed by the new implementation: + +* Marks don't pick up nested classes (`#199 `_). + +* markers stains on all related classes (`#568 `_). + +* combining marks - args and kwargs calculation (`#2897 `_). + +* ``request.node.get_marker('name')`` returns ``None`` for markers applied in classes (`#902 `_). + +* marks applied in parametrize are stored as markdecorator (`#2400 `_). + +* fix marker interaction in a backward incompatible way (`#1670 `_). + +* Refactor marks to get rid of the current "marks transfer" mechanism (`#2363 `_). + +* Introduce FunctionDefinition node, use it in generate_tests (`#2522 `_). + +* remove named marker attributes and collect markers in items (`#891 `_). + +* skipif mark from parametrize hides module level skipif mark (`#1540 `_). + +* skipif + parametrize not skipping tests (`#1296 `_). + +* marker transfer incompatible with inheritance (`#535 `_). + +More details can be found in the `original PR `_. + +.. note:: + + in a future major relase of pytest we will introduce class based markers, + at which points markers will no longer be limited to instances of :py:class:`Mark` diff --git a/third_party/python/pytest/doc/en/monkeypatch.rst b/third_party/python/pytest/doc/en/monkeypatch.rst index 0c07b2f44fcb..1a69c3a429d2 100644 --- a/third_party/python/pytest/doc/en/monkeypatch.rst +++ b/third_party/python/pytest/doc/en/monkeypatch.rst @@ -35,7 +35,7 @@ patch this function before calling into a function which uses it:: assert x == '/abc/.ssh' Here our test function monkeypatches ``os.path.expanduser`` and -then calls into a function that calls it. After the test function +then calls into a function that calls it. After the test function finishes the ``os.path.expanduser`` modification will be undone. example: preventing "requests" from remote operations @@ -51,25 +51,39 @@ requests in all your tests, you can do:: monkeypatch.delattr("requests.sessions.Session.request") This autouse fixture will be executed for each test function and it -will delete the method ``request.session.Session.request`` +will delete the method ``request.session.Session.request`` so that any attempts within tests to create http requests will fail. .. note:: - + Be advised that it is not recommended to patch builtin functions such as ``open``, ``compile``, etc., because it might break pytest's internals. If that's - unavoidable, passing ``--tb=native``, ``--assert=plain`` and ``--capture=no`` might + unavoidable, passing ``--tb=native``, ``--assert=plain`` and ``--capture=no`` might help although there's no guarantee. - -Method reference of the monkeypatch fixture -------------------------------------------- +.. note:: -.. autoclass:: MonkeyPatch - :members: + Mind that patching ``stdlib`` functions and some third-party libraries used by pytest + might break pytest itself, therefore in those cases it is recommended to use + :meth:`MonkeyPatch.context` to limit the patching to the block you want tested: -``monkeypatch.setattr/delattr/delitem/delenv()`` all -by default raise an Exception if the target does not exist. -Pass ``raising=False`` if you want to skip this check. + .. code-block:: python + import functools + + + def test_partial(monkeypatch): + with monkeypatch.context() as m: + m.setattr(functools, "partial", 3) + assert functools.partial == 3 + + See issue `#3290 `_ for details. + + +.. currentmodule:: _pytest.monkeypatch + +API Reference +------------- + +Consult the docs for the :class:`MonkeyPatch` class. diff --git a/third_party/python/pytest/doc/en/nose.rst b/third_party/python/pytest/doc/en/nose.rst index 10a10633ab06..1f7b7b638448 100644 --- a/third_party/python/pytest/doc/en/nose.rst +++ b/third_party/python/pytest/doc/en/nose.rst @@ -58,7 +58,7 @@ Unsupported idioms / known issues You may find yourself wanting to do this if you ran ``python setup.py install`` to set up your project, as opposed to ``python setup.py develop`` or any of the package manager equivalents. Installing with develop in a - virtual environment like Tox is recommended over this pattern. + virtual environment like tox is recommended over this pattern. - nose-style doctests are not collected and executed correctly, also doctest fixtures don't work. @@ -70,6 +70,3 @@ Unsupported idioms / known issues There are no plans to fix this currently because ``yield``-tests are deprecated in pytest 3.0, with ``pytest.mark.parametrize`` being the recommended alternative. - - - diff --git a/third_party/python/pytest/doc/en/parametrize.rst b/third_party/python/pytest/doc/en/parametrize.rst index ebbae31b2d80..693cf1913bf8 100644 --- a/third_party/python/pytest/doc/en/parametrize.rst +++ b/third_party/python/pytest/doc/en/parametrize.rst @@ -11,13 +11,13 @@ Parametrizing fixtures and test functions pytest enables test parametrization at several levels: -- :py:func:`pytest.fixture` allows one to :ref:`parametrize fixture +- :py:func:`pytest.fixture` allows one to :ref:`parametrize fixture functions `. -* `@pytest.mark.parametrize`_ allows one to define multiple sets of +* `@pytest.mark.parametrize`_ allows one to define multiple sets of arguments and fixtures at the test function or class. -* `pytest_generate_tests`_ allows one to define custom parametrization +* `pytest_generate_tests`_ allows one to define custom parametrization schemes or extensions. .. _parametrizemark: @@ -33,7 +33,7 @@ pytest enables test parametrization at several levels: .. versionchanged:: 2.4 Several improvements. -The builtin ``pytest.mark.parametrize`` decorator enables +The builtin :ref:`pytest.mark.parametrize ref` decorator enables parametrization of arguments for a test function. Here is a typical example of a test function that implements checking that a certain input leads to an expected output:: @@ -53,18 +53,18 @@ tuples so that the ``test_eval`` function will run three times using them in turn:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 3 items - - test_expectation.py ..F - - ======= FAILURES ======== - _______ test_eval[6*9-42] ________ - + + test_expectation.py ..F [100%] + + ================================= FAILURES ================================= + ____________________________ test_eval[6*9-42] _____________________________ + test_input = '6*9', expected = 42 - + @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), @@ -74,9 +74,9 @@ them in turn:: > assert eval(test_input) == expected E AssertionError: assert 54 == 42 E + where 54 = eval('6*9') - + test_expectation.py:8: AssertionError - ======= 1 failed, 2 passed in 0.12 seconds ======== + ==================== 1 failed, 2 passed in 0.12 seconds ==================== As designed in this example, only one pair of input/output values fails the simple test function. And as usual with test function arguments, @@ -102,14 +102,14 @@ for example with the builtin ``mark.xfail``:: Let's run this:: $ pytest - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 3 items - - test_expectation.py ..x - - ======= 2 passed, 1 xfailed in 0.12 seconds ======== + + test_expectation.py ..x [100%] + + =================== 2 passed, 1 xfailed in 0.12 seconds ==================== The one parameter set which caused a failure previously now shows up as an "xfailed (expected to fail)" test. @@ -123,8 +123,8 @@ To get all combinations of multiple parametrized arguments you can stack def test_foo(x, y): pass -This will run the test with the arguments set to ``x=0/y=2``, ``x=0/y=3``, ``x=1/y=2`` and -``x=1/y=3``. +This will run the test with the arguments set to ``x=0/y=2``, ``x=1/y=2``, +``x=0/y=3``, and ``x=1/y=3`` exhausting parameters in the order of the decorators. .. _`pytest_generate_tests`: @@ -165,24 +165,24 @@ command line option and the parametrization of our test function:: If we now pass two stringinput values, our test will run twice:: $ pytest -q --stringinput="hello" --stringinput="world" test_strings.py - .. + .. [100%] 2 passed in 0.12 seconds Let's also run with a stringinput that will lead to a failing test:: $ pytest -q --stringinput="!" test_strings.py - F - ======= FAILURES ======== - _______ test_valid_string[!] ________ - + F [100%] + ================================= FAILURES ================================= + ___________________________ test_valid_string[!] ___________________________ + stringinput = '!' - + def test_valid_string(stringinput): > assert stringinput.isalpha() E AssertionError: assert False E + where False = () E + where = '!'.isalpha - + test_strings.py:3: AssertionError 1 failed in 0.12 seconds @@ -193,12 +193,12 @@ If you don't specify a stringinput it will be skipped because list:: $ pytest -q -rs test_strings.py - s - ======= short test summary info ======== - SKIP [1] test_strings.py:2: got empty parameter set ['stringinput'], function test_valid_string at $REGENDOC_TMPDIR/test_strings.py:1 + s [100%] + ========================= short test summary info ========================== + SKIP [1] test_strings.py: got empty parameter set ['stringinput'], function test_valid_string at $REGENDOC_TMPDIR/test_strings.py:1 1 skipped in 0.12 seconds -Note that when calling ``metafunc.parametrize`` multiple times with different parameter sets, all parameter names across +Note that when calling ``metafunc.parametrize`` multiple times with different parameter sets, all parameter names across those sets cannot be duplicated, otherwise an error will be raised. More examples @@ -206,12 +206,3 @@ More examples For further examples, you might want to look at :ref:`more parametrization examples `. - -.. _`metafunc object`: - -The **metafunc** object -------------------------------------------- - -.. currentmodule:: _pytest.python -.. autoclass:: Metafunc - :members: diff --git a/third_party/python/pytest/doc/en/plugins.rst b/third_party/python/pytest/doc/en/plugins.rst index 4a6772ca3495..9ddba5d38ca6 100644 --- a/third_party/python/pytest/doc/en/plugins.rst +++ b/third_party/python/pytest/doc/en/plugins.rst @@ -20,42 +20,39 @@ Here is a little annotated list for some popular plugins: .. _`django`: https://www.djangoproject.com/ -* `pytest-django `_: write tests +* `pytest-django `_: write tests for `django`_ apps, using pytest integration. -* `pytest-twisted `_: write tests +* `pytest-twisted `_: write tests for `twisted `_ apps, starting a reactor and processing deferreds from test functions. -* `pytest-catchlog `_: - to capture and assert about messages from the logging module - -* `pytest-cov `_: +* `pytest-cov `_: coverage reporting, compatible with distributed testing -* `pytest-xdist `_: +* `pytest-xdist `_: to distribute tests to CPUs and remote hosts, to run in boxed mode which allows to survive segmentation faults, to run in looponfailing mode, automatically re-running failing tests on file changes. -* `pytest-instafail `_: +* `pytest-instafail `_: to report failures while the test run is happening. -* `pytest-bdd `_ and - `pytest-konira `_ +* `pytest-bdd `_ and + `pytest-konira `_ to write tests using behaviour-driven testing. -* `pytest-timeout `_: +* `pytest-timeout `_: to timeout tests based on function marks or global definitions. -* `pytest-pep8 `_: +* `pytest-pep8 `_: a ``--pep8`` option to enable PEP8 compliance checking. -* `pytest-flakes `_: +* `pytest-flakes `_: check source code with pyflakes. -* `oejskit `_: +* `oejskit `_: a plugin to run javascript unittests in live browsers. To see a complete list of all plugins with their latest testing @@ -64,10 +61,11 @@ status against different pytest and Python versions, please visit You may also discover more plugins through a `pytest- pypi.python.org search`_. -.. _`available installable plugins`: -.. _`pytest- pypi.python.org search`: http://pypi.python.org/pypi?%3Aaction=search&term=pytest-&submit=search +.. _`pytest- pypi.python.org search`: https://pypi.org/search/?q=pytest- +.. _`available installable plugins`: + Requiring/Loading plugins in a test module or conftest file ----------------------------------------------------------- @@ -82,6 +80,12 @@ will be loaded as well. which will import the specified module as a ``pytest`` plugin. +.. note:: + Requiring plugins using a ``pytest_plugins`` variable in non-root + ``conftest.py`` files is deprecated. See + :ref:`full explanation ` + in the Writing plugins section. + .. _`findpluginname`: Finding out which plugins are active @@ -123,35 +127,3 @@ CI server), you can set ``PYTEST_ADDOPTS`` environment variable to See :ref:`findpluginname` for how to obtain the name of a plugin. .. _`builtin plugins`: - -Pytest default plugin reference -------------------------------- - - -You can find the source code for the following plugins -in the `pytest repository `_. - -.. autosummary:: - - _pytest.assertion - _pytest.cacheprovider - _pytest.capture - _pytest.config - _pytest.doctest - _pytest.helpconfig - _pytest.junitxml - _pytest.mark - _pytest.monkeypatch - _pytest.nose - _pytest.pastebin - _pytest.debugging - _pytest.pytester - _pytest.python - _pytest.recwarn - _pytest.resultlog - _pytest.runner - _pytest.main - _pytest.skipping - _pytest.terminal - _pytest.tmpdir - _pytest.unittest diff --git a/third_party/python/pytest/doc/en/projects.rst b/third_party/python/pytest/doc/en/projects.rst index a2edbf68fb6e..606e9d47c161 100644 --- a/third_party/python/pytest/doc/en/projects.rst +++ b/third_party/python/pytest/doc/en/projects.rst @@ -32,40 +32,40 @@ Here are some examples of projects using ``pytest`` (please send notes via :ref: * `PyPM `_ ActiveState's package manager * `Fom `_ a fluid object mapper for FluidDB * `applib `_ cross-platform utilities -* `six `_ Python 2 and 3 compatibility utilities +* `six `_ Python 2 and 3 compatibility utilities * `pediapress `_ MediaWiki articles -* `mwlib `_ mediawiki parser and utility library +* `mwlib `_ mediawiki parser and utility library * `The Translate Toolkit `_ for localization and conversion * `execnet `_ rapid multi-Python deployment -* `pylib `_ cross-platform path, IO, dynamic code library +* `pylib `_ cross-platform path, IO, dynamic code library * `Pacha `_ configuration management in five minutes -* `bbfreeze `_ create standalone executables from Python scripts +* `bbfreeze `_ create standalone executables from Python scripts * `pdb++ `_ a fancier version of PDB * `py-s3fuse `_ Amazon S3 FUSE based filesystem * `waskr `_ WSGI Stats Middleware * `guachi `_ global persistent configs for Python modules -* `Circuits `_ lightweight Event Driven Framework +* `Circuits `_ lightweight Event Driven Framework * `pygtk-helpers `_ easy interaction with PyGTK * `QuantumCore `_ statusmessage and repoze openid plugin * `pydataportability `_ libraries for managing the open web * `XIST `_ extensible HTML/XML generator -* `tiddlyweb `_ optionally headless, extensible RESTful datastore +* `tiddlyweb `_ optionally headless, extensible RESTful datastore * `fancycompleter `_ for colorful tab-completion * `Paludis `_ tools for Gentoo Paludis package manager * `Gerald `_ schema comparison tool * `abjad `_ Python API for Formalized Score control * `bu `_ a microscopic build system * `katcp `_ Telescope communication protocol over Twisted -* `kss plugin timer `_ +* `kss plugin timer `_ * `pyudev `_ a pure Python binding to the Linux library libudev -* `pytest-localserver `_ a plugin for pytest that provides an httpserver and smtpserver -* `pytest-monkeyplus `_ a plugin that extends monkeypatch +* `pytest-localserver `_ a plugin for pytest that provides an httpserver and smtpserver +* `pytest-monkeyplus `_ a plugin that extends monkeypatch These projects help integrate ``pytest`` into other Python frameworks: -* `pytest-django `_ for Django +* `pytest-django `_ for Django * `zope.pytest `_ for Zope and Grok -* `pytest_gae `_ for Google App Engine +* `pytest_gae `_ for Google App Engine * There is `some work `_ underway for Kotti, a CMS built in Pyramid/Pylons diff --git a/third_party/python/pytest/doc/en/proposals/parametrize_with_fixtures.rst b/third_party/python/pytest/doc/en/proposals/parametrize_with_fixtures.rst index 146032aa4713..92e7993f3906 100644 --- a/third_party/python/pytest/doc/en/proposals/parametrize_with_fixtures.rst +++ b/third_party/python/pytest/doc/en/proposals/parametrize_with_fixtures.rst @@ -35,26 +35,29 @@ This is how a functional test could look like: import pytest + @pytest.fixture def default_context(): - return {'extra_context': {}} + return {"extra_context": {}} - @pytest.fixture(params=[ - {'author': 'alice'}, - {'project_slug': 'helloworld'}, - {'author': 'bob', 'project_slug': 'foobar'}, - ]) + @pytest.fixture( + params=[ + {"author": "alice"}, + {"project_slug": "helloworld"}, + {"author": "bob", "project_slug": "foobar"}, + ] + ) def extra_context(request): - return {'extra_context': request.param} + return {"extra_context": request.param} - @pytest.fixture(params=['default', 'extra']) + @pytest.fixture(params=["default", "extra"]) def context(request): - if request.param == 'default': - return request.getfuncargvalue('default_context') + if request.param == "default": + return request.getfuncargvalue("default_context") else: - return request.getfuncargvalue('extra_context') + return request.getfuncargvalue("extra_context") def test_generate_project(cookies, context): @@ -95,8 +98,7 @@ fixtures from existing ones. .. code-block:: python pytest.define_combined_fixture( - name='context', - fixtures=['default_context', 'extra_context'], + name="context", fixtures=["default_context", "extra_context"] ) The new fixture ``context`` inherits the scope from the used fixtures and yield @@ -118,15 +120,17 @@ all parameters marked as a fixture. .. note:: - The `pytest-lazy-fixture `_ plugin implements a very + The `pytest-lazy-fixture `_ plugin implements a very similar solution to the proposal below, make sure to check it out. .. code-block:: python - @pytest.fixture(params=[ - pytest.fixture_request('default_context'), - pytest.fixture_request('extra_context'), - ]) + @pytest.fixture( + params=[ + pytest.fixture_request("default_context"), + pytest.fixture_request("extra_context"), + ] + ) def context(request): """Returns all values for ``default_context``, one-by-one before it does the same for ``extra_context``. @@ -145,10 +149,10 @@ The same helper can be used in combination with ``pytest.mark.parametrize``. @pytest.mark.parametrize( - 'context, expected_response_code', + "context, expected_response_code", [ - (pytest.fixture_request('default_context'), 0), - (pytest.fixture_request('extra_context'), 0), + (pytest.fixture_request("default_context"), 0), + (pytest.fixture_request("extra_context"), 0), ], ) def test_generate_project(cookies, context, exit_code): diff --git a/third_party/python/pytest/doc/en/reference.rst b/third_party/python/pytest/doc/en/reference.rst new file mode 100644 index 000000000000..fe9e87042f9a --- /dev/null +++ b/third_party/python/pytest/doc/en/reference.rst @@ -0,0 +1,1296 @@ + +Reference +========= + +This page contains the full reference to pytest's API. + +.. contents:: + :depth: 3 + :local: + +Functions +--------- + +pytest.approx +~~~~~~~~~~~~~ + +.. autofunction:: _pytest.python_api.approx + +pytest.fail +~~~~~~~~~~~ + +**Tutorial**: :ref:`skipping` + +.. autofunction:: _pytest.outcomes.fail + +pytest.skip +~~~~~~~~~~~ + +.. autofunction:: _pytest.outcomes.skip(msg, [allow_module_level=False]) + +pytest.importorskip +~~~~~~~~~~~~~~~~~~~ + +.. autofunction:: _pytest.outcomes.importorskip + +pytest.xfail +~~~~~~~~~~~~ + +.. autofunction:: _pytest.outcomes.xfail + +pytest.exit +~~~~~~~~~~~ + +.. autofunction:: _pytest.outcomes.exit + +pytest.main +~~~~~~~~~~~ + +.. autofunction:: _pytest.config.main + +pytest.param +~~~~~~~~~~~~~ + +.. autofunction:: pytest.param(*values, [id], [marks]) + +pytest.raises +~~~~~~~~~~~~~ + +**Tutorial**: :ref:`assertraises`. + +.. autofunction:: pytest.raises(expected_exception: Exception, [match], [message]) + :with: excinfo + +pytest.deprecated_call +~~~~~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`ensuring_function_triggers`. + +.. autofunction:: pytest.deprecated_call() + :with: + +pytest.register_assert_rewrite +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`assertion-rewriting`. + +.. autofunction:: pytest.register_assert_rewrite + +pytest.warns +~~~~~~~~~~~~ + +**Tutorial**: :ref:`assertwarnings` + +.. autofunction:: pytest.warns(expected_warning: Exception, [match]) + :with: + + +.. _`marks ref`: + +Marks +----- + +Marks can be used apply meta data to *test functions* (but not fixtures), which can then be accessed by +fixtures or plugins. + + + + +.. _`pytest.mark.filterwarnings ref`: + +pytest.mark.filterwarnings +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`filterwarnings`. + +Add warning filters to marked test items. + +.. py:function:: pytest.mark.filterwarnings(filter) + + :keyword str filter: + A *warning specification string*, which is composed of contents of the tuple ``(action, message, category, module, lineno)`` + as specified in `The Warnings filter `_ section of + the Python documentation, separated by ``":"``. Optional fields can be omitted. + + For example: + + .. code-block:: python + + @pytest.mark.warnings("ignore:.*usage will be deprecated.*:DeprecationWarning") + def test_foo(): + ... + + +.. _`pytest.mark.parametrize ref`: + +pytest.mark.parametrize +~~~~~~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :doc:`parametrize`. + +.. automethod:: _pytest.python.Metafunc.parametrize + + +.. _`pytest.mark.skip ref`: + +pytest.mark.skip +~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`skip`. + +Unconditionally skip a test function. + +.. py:function:: pytest.mark.skip(*, reason=None) + + :keyword str reason: Reason why the test function is being skipped. + + +.. _`pytest.mark.skipif ref`: + +pytest.mark.skipif +~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`skipif`. + +Skip a test function if a condition is ``True``. + +.. py:function:: pytest.mark.skipif(condition, *, reason=None) + + :type condition: bool or str + :param condition: ``True/False`` if the condition should be skipped or a :ref:`condition string `. + :keyword str reason: Reason why the test function is being skipped. + + +.. _`pytest.mark.xfail ref`: + +pytest.mark.xfail +~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`xfail`. + +Marks a test function as *expected to fail*. + +.. py:function:: pytest.mark.xfail(condition=None, *, reason=None, raises=None, run=True, strict=False) + + :type condition: bool or str + :param condition: ``True/False`` if the condition should be marked as xfail or a :ref:`condition string `. + :keyword str reason: Reason why the test function is marked as xfail. + :keyword Exception raises: Exception subclass expected to be raised by the test function; other exceptions will fail the test. + :keyword bool run: + If the test function should actually be executed. If ``False``, the function will always xfail and will + not be executed (useful a function is segfaulting). + :keyword bool strict: + * If ``False`` (the default) the function will be shown in the terminal output as ``xfailed`` if it fails + and as ``xpass`` if it passes. In both cases this will not cause the test suite to fail as a whole. This + is particularly useful to mark *flaky* tests (tests that random at fail) to be tackled later. + * If ``True``, the function will be shown in the terminal output as ``xfailed`` if it fails, but if it + unexpectedly passes then it will **fail** the test suite. This is particularly useful to mark functions + that are always failing and there should be a clear indication if they unexpectedly start to pass (for example + a new release of a library fixes a known bug). + + +custom marks +~~~~~~~~~~~~ + +Marks are created dynamically using the factory object ``pytest.mark`` and applied as a decorator. + +For example: + +.. code-block:: python + + @pytest.mark.timeout(10, "slow", method="thread") + def test_function(): + ... + +Will create and attach a :class:`Mark <_pytest.mark.structures.Mark>` object to the collected +:class:`Item <_pytest.nodes.Item>`, which can then be accessed by fixtures or hooks with +:meth:`Node.iter_markers <_pytest.nodes.Node.iter_markers>`. The ``mark`` object will have the following attributes: + +.. code-block:: python + + mark.args == (10, "slow") + mark.kwargs == {"method": "thread"} + + +Fixtures +-------- + +**Tutorial**: :ref:`fixture`. + +Fixtures are requested by test functions or other fixtures by declaring them as argument names. + + +Example of a test requiring a fixture: + +.. code-block:: python + + def test_output(capsys): + print("hello") + out, err = capsys.readouterr() + assert out == "hello\n" + + +Example of a fixture requiring another fixture: + +.. code-block:: python + + @pytest.fixture + def db_session(tmpdir): + fn = tmpdir / "db.file" + return connect(str(fn)) + +For more details, consult the full :ref:`fixtures docs `. + + +@pytest.fixture +~~~~~~~~~~~~~~~ + +.. autofunction:: pytest.fixture + :decorator: + + +.. _`cache-api`: + +config.cache +~~~~~~~~~~~~ + +**Tutorial**: :ref:`cache`. + +The ``config.cache`` object allows other plugins and fixtures +to store and retrieve values across test runs. To access it from fixtures +request ``pytestconfig`` into your fixture and get it with ``pytestconfig.cache``. + +Under the hood, the cache plugin uses the simple +``dumps``/``loads`` API of the :py:mod:`json` stdlib module. + +.. currentmodule:: _pytest.cacheprovider + +.. automethod:: Cache.get +.. automethod:: Cache.set +.. automethod:: Cache.makedir + + +capsys +~~~~~~ + +**Tutorial**: :doc:`capture`. + +.. currentmodule:: _pytest.capture + +.. autofunction:: capsys() + :no-auto-options: + + Returns an instance of :py:class:`CaptureFixture`. + + Example: + + .. code-block:: python + + def test_output(capsys): + print("hello") + captured = capsys.readouterr() + assert captured.out == "hello\n" + +.. autoclass:: CaptureFixture() + :members: + + +capsysbinary +~~~~~~~~~~~~ + +**Tutorial**: :doc:`capture`. + +.. autofunction:: capsysbinary() + :no-auto-options: + + Returns an instance of :py:class:`CaptureFixture`. + + Example: + + .. code-block:: python + + def test_output(capsysbinary): + print("hello") + captured = capsysbinary.readouterr() + assert captured.out == b"hello\n" + + +capfd +~~~~~~ + +**Tutorial**: :doc:`capture`. + +.. autofunction:: capfd() + :no-auto-options: + + Returns an instance of :py:class:`CaptureFixture`. + + Example: + + .. code-block:: python + + def test_system_echo(capfd): + os.system('echo "hello"') + captured = capsys.readouterr() + assert captured.out == "hello\n" + + +capfdbinary +~~~~~~~~~~~~ + +**Tutorial**: :doc:`capture`. + +.. autofunction:: capfdbinary() + :no-auto-options: + + Returns an instance of :py:class:`CaptureFixture`. + + Example: + + .. code-block:: python + + def test_system_echo(capfdbinary): + os.system('echo "hello"') + captured = capfdbinary.readouterr() + assert captured.out == b"hello\n" + + +doctest_namespace +~~~~~~~~~~~~~~~~~ + +**Tutorial**: :doc:`doctest`. + +.. autofunction:: _pytest.doctest.doctest_namespace() + + Usually this fixture is used in conjunction with another ``autouse`` fixture: + + .. code-block:: python + + @pytest.fixture(autouse=True) + def add_np(doctest_namespace): + doctest_namespace["np"] = numpy + + For more details: :ref:`doctest_namespace`. + + +request +~~~~~~~ + +**Tutorial**: :ref:`request example`. + +The ``request`` fixture is a special fixture providing information of the requesting test function. + +.. autoclass:: _pytest.fixtures.FixtureRequest() + :members: + + +pytestconfig +~~~~~~~~~~~~ + +.. autofunction:: _pytest.fixtures.pytestconfig() + + +record_property +~~~~~~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`record_property example`. + +.. autofunction:: _pytest.junitxml.record_property() + +caplog +~~~~~~ + +**Tutorial**: :doc:`logging`. + +.. autofunction:: _pytest.logging.caplog() + :no-auto-options: + + This returns a :class:`_pytest.logging.LogCaptureFixture` instance. + +.. autoclass:: _pytest.logging.LogCaptureFixture + :members: + + +monkeypatch +~~~~~~~~~~~ + +.. currentmodule:: _pytest.monkeypatch + +**Tutorial**: :doc:`monkeypatch`. + +.. autofunction:: _pytest.monkeypatch.monkeypatch() + :no-auto-options: + + This returns a :class:`MonkeyPatch` instance. + +.. autoclass:: _pytest.monkeypatch.MonkeyPatch + :members: + +testdir +~~~~~~~ + +.. currentmodule:: _pytest.pytester + +This fixture provides a :class:`Testdir` instance useful for black-box testing of test files, making it ideal to +test plugins. + +To use it, include in your top-most ``conftest.py`` file:: + + pytest_plugins = 'pytester' + + + +.. autoclass:: Testdir() + :members: runpytest,runpytest_subprocess,runpytest_inprocess,makeconftest,makepyfile + +.. autoclass:: RunResult() + :members: + +.. autoclass:: LineMatcher() + :members: + + +recwarn +~~~~~~~ + +**Tutorial**: :ref:`assertwarnings` + +.. currentmodule:: _pytest.recwarn + +.. autofunction:: recwarn() + :no-auto-options: + +.. autoclass:: _pytest.recwarn.WarningsRecorder() + :members: + +Each recorded warning is an instance of :class:`warnings.WarningMessage`. + +.. note:: + :class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1 + +.. note:: + ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated + differently; see :ref:`ensuring_function_triggers`. + + +tmpdir +~~~~~~ + +**Tutorial**: :doc:`tmpdir` + +.. currentmodule:: _pytest.tmpdir + +.. autofunction:: tmpdir() + :no-auto-options: + + +tmpdir_factory +~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`tmpdir factory example` + +.. _`tmpdir factory api`: + +``tmpdir_factory`` instances have the following methods: + +.. currentmodule:: _pytest.tmpdir + +.. automethod:: TempdirFactory.mktemp +.. automethod:: TempdirFactory.getbasetemp + + +.. _`hook-reference`: + +Hooks +----- + +**Tutorial**: :doc:`writing_plugins`. + +.. currentmodule:: _pytest.hookspec + +Reference to all hooks which can be implemented by :ref:`conftest.py files ` and :ref:`plugins `. + +Bootstrapping hooks +~~~~~~~~~~~~~~~~~~~ + +Bootstrapping hooks called for plugins registered early enough (internal and setuptools plugins). + +.. autofunction:: pytest_load_initial_conftests +.. autofunction:: pytest_cmdline_preparse +.. autofunction:: pytest_cmdline_parse +.. autofunction:: pytest_cmdline_main + +Initialization hooks +~~~~~~~~~~~~~~~~~~~~ + +Initialization hooks called for plugins and ``conftest.py`` files. + +.. autofunction:: pytest_addoption +.. autofunction:: pytest_addhooks +.. autofunction:: pytest_configure +.. autofunction:: pytest_unconfigure +.. autofunction:: pytest_sessionstart +.. autofunction:: pytest_sessionfinish + +Test running hooks +~~~~~~~~~~~~~~~~~~ + +All runtest related hooks receive a :py:class:`pytest.Item <_pytest.main.Item>` object. + +.. autofunction:: pytest_runtestloop +.. autofunction:: pytest_runtest_protocol +.. autofunction:: pytest_runtest_logstart +.. autofunction:: pytest_runtest_logfinish +.. autofunction:: pytest_runtest_setup +.. autofunction:: pytest_runtest_call +.. autofunction:: pytest_runtest_teardown +.. autofunction:: pytest_runtest_makereport + +For deeper understanding you may look at the default implementation of +these hooks in :py:mod:`_pytest.runner` and maybe also +in :py:mod:`_pytest.pdb` which interacts with :py:mod:`_pytest.capture` +and its input/output capturing in order to immediately drop +into interactive debugging when a test failure occurs. + +The :py:mod:`_pytest.terminal` reported specifically uses +the reporting hook to print information about a test run. + +Collection hooks +~~~~~~~~~~~~~~~~ + +``pytest`` calls the following hooks for collecting files and directories: + +.. autofunction:: pytest_collection +.. autofunction:: pytest_ignore_collect +.. autofunction:: pytest_collect_directory +.. autofunction:: pytest_collect_file + +For influencing the collection of objects in Python modules +you can use the following hook: + +.. autofunction:: pytest_pycollect_makeitem +.. autofunction:: pytest_generate_tests +.. autofunction:: pytest_make_parametrize_id + +After collection is complete, you can modify the order of +items, delete or otherwise amend the test items: + +.. autofunction:: pytest_collection_modifyitems + +Reporting hooks +~~~~~~~~~~~~~~~ + +Session related reporting hooks: + +.. autofunction:: pytest_collectstart +.. autofunction:: pytest_itemcollected +.. autofunction:: pytest_collectreport +.. autofunction:: pytest_deselected +.. autofunction:: pytest_report_header +.. autofunction:: pytest_report_collectionfinish +.. autofunction:: pytest_report_teststatus +.. autofunction:: pytest_terminal_summary +.. autofunction:: pytest_fixture_setup +.. autofunction:: pytest_fixture_post_finalizer + +And here is the central hook for reporting about +test execution: + +.. autofunction:: pytest_runtest_logreport + +You can also use this hook to customize assertion representation for some +types: + +.. autofunction:: pytest_assertrepr_compare + + +Debugging/Interaction hooks +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +There are few hooks which can be used for special +reporting or interaction with exceptions: + +.. autofunction:: pytest_internalerror +.. autofunction:: pytest_keyboard_interrupt +.. autofunction:: pytest_exception_interact +.. autofunction:: pytest_enter_pdb + + +Objects +------- + +Full reference to objects accessible from :ref:`fixtures ` or :ref:`hooks `. + + +CallInfo +~~~~~~~~ + +.. autoclass:: _pytest.runner.CallInfo() + :members: + + +Class +~~~~~ + +.. autoclass:: _pytest.python.Class() + :members: + :show-inheritance: + +Collector +~~~~~~~~~ + +.. autoclass:: _pytest.nodes.Collector() + :members: + :show-inheritance: + +Config +~~~~~~ + +.. autoclass:: _pytest.config.Config() + :members: + +ExceptionInfo +~~~~~~~~~~~~~ + +.. autoclass:: _pytest._code.ExceptionInfo + :members: + +FixtureDef +~~~~~~~~~~ + +.. autoclass:: _pytest.fixtures.FixtureDef() + :members: + :show-inheritance: + +FSCollector +~~~~~~~~~~~ + +.. autoclass:: _pytest.nodes.FSCollector() + :members: + :show-inheritance: + +Function +~~~~~~~~ + +.. autoclass:: _pytest.python.Function() + :members: + :show-inheritance: + +Item +~~~~ + +.. autoclass:: _pytest.nodes.Item() + :members: + :show-inheritance: + +MarkDecorator +~~~~~~~~~~~~~ + +.. autoclass:: _pytest.mark.MarkDecorator + :members: + + +MarkGenerator +~~~~~~~~~~~~~ + +.. autoclass:: _pytest.mark.MarkGenerator + :members: + + +MarkInfo +~~~~~~~~ + +.. autoclass:: _pytest.mark.MarkInfo + :members: + + +Mark +~~~~ + +.. autoclass:: _pytest.mark.structures.Mark + :members: + + +Metafunc +~~~~~~~~ + +.. autoclass:: _pytest.python.Metafunc + :members: + +Module +~~~~~~ + +.. autoclass:: _pytest.python.Module() + :members: + :show-inheritance: + +Node +~~~~ + +.. autoclass:: _pytest.nodes.Node() + :members: + +Parser +~~~~~~ + +.. autoclass:: _pytest.config.argparsing.Parser() + :members: + +PluginManager +~~~~~~~~~~~~~ + +.. autoclass:: pluggy.PluginManager() + :members: + + +PytestPluginManager +~~~~~~~~~~~~~~~~~~~ + +.. autoclass:: _pytest.config.PytestPluginManager() + :members: + :undoc-members: + :show-inheritance: + +Session +~~~~~~~ + +.. autoclass:: _pytest.main.Session() + :members: + :show-inheritance: + +TestReport +~~~~~~~~~~ + +.. autoclass:: _pytest.runner.TestReport() + :members: + :inherited-members: + +_Result +~~~~~~~ + +.. autoclass:: pluggy._Result + :members: + +Special Variables +----------------- + +pytest treats some global variables in a special manner when defined in a test module. + + +pytest_plugins +~~~~~~~~~~~~~~ + +**Tutorial**: :ref:`available installable plugins` + +Can be declared at the **global** level in *test modules* and *conftest.py files* to register additional plugins. +Can be either a ``str`` or ``Sequence[str]``. + +.. code-block:: python + + pytest_plugins = "myapp.testsupport.myplugin" + +.. code-block:: python + + pytest_plugins = ("myapp.testsupport.tools", "myapp.testsupport.regression") + + +pytest_mark +~~~~~~~~~~~ + +**Tutorial**: :ref:`scoped-marking` + +Can be declared at the **global** level in *test modules* to apply one or more :ref:`marks ` to all +test functions and methods. Can be either a single mark or a sequence of marks. + +.. code-block:: python + + import pytest + + pytestmark = pytest.mark.webtest + + +.. code-block:: python + + import pytest + + pytestmark = (pytest.mark.integration, pytest.mark.slow) + +PYTEST_DONT_REWRITE (module docstring) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The text ``PYTEST_DONT_REWRITE`` can be add to any **module docstring** to disable +:ref:`assertion rewriting ` for that module. + + +Environment Variables +--------------------- + +Environment variables that can be used to change pytest's behavior. + +PYTEST_ADDOPTS +~~~~~~~~~~~~~~ + +This contains a command-line (parsed by the py:mod:`shlex` module) that will be **prepended** to the command line given +by the user, see :ref:`adding default options` for more information. + +PYTEST_DEBUG +~~~~~~~~~~~~ + +When set, pytest will print tracing and debug information. + +PYTEST_PLUGINS +~~~~~~~~~~~~~~ + +Contains comma-separated list of modules that should be loaded as plugins: + +.. code-block:: bash + + export PYTEST_PLUGINS=mymodule.plugin,xdist + + +PYTEST_CURRENT_TEST +~~~~~~~~~~~~~~~~~~~ + +This is not meant to be set by users, but is set by pytest internally with the name of the current test so other +processes can inspect it, see :ref:`pytest current test env` for more information. + + +.. _`ini options ref`: + +Configuration Options +--------------------- + +Here is a list of builtin configuration options that may be written in a ``pytest.ini``, ``tox.ini`` or ``setup.cfg`` +file, usually located at the root of your repository. All options must be under a ``[pytest]`` section +(``[tool:pytest]`` for ``setup.cfg`` files). + +Configuration file options may be overwritten in the command-line by using ``-o/--override``, which can also be +passed multiple times. The expected format is ``name=value``. For example:: + + pytest -o console_output_style=classic -o cache_dir=/tmp/mycache + + +.. confval:: addopts + + Add the specified ``OPTS`` to the set of command line arguments as if they + had been specified by the user. Example: if you have this ini file content: + + .. code-block:: ini + + # content of pytest.ini + [pytest] + addopts = --maxfail=2 -rf # exit after 2 failures, report fail info + + issuing ``pytest test_hello.py`` actually means:: + + pytest --maxfail=2 -rf test_hello.py + + Default is to add no options. + + +.. confval:: cache_dir + + .. versionadded:: 3.2 + + Sets a directory where stores content of cache plugin. Default directory is + ``.pytest_cache`` which is created in :ref:`rootdir `. Directory may be + relative or absolute path. If setting relative path, then directory is created + relative to :ref:`rootdir `. Additionally path may contain environment + variables, that will be expanded. For more information about cache plugin + please refer to :ref:`cache_provider`. + + +.. confval:: confcutdir + + Sets a directory where search upwards for ``conftest.py`` files stops. + By default, pytest will stop searching for ``conftest.py`` files upwards + from ``pytest.ini``/``tox.ini``/``setup.cfg`` of the project if any, + or up to the file-system root. + + +.. confval:: console_output_style + + .. versionadded:: 3.3 + + Sets the console output style while running tests: + + * ``classic``: classic pytest output. + * ``progress``: like classic pytest output, but with a progress indicator. + + The default is ``progress``, but you can fallback to ``classic`` if you prefer or + the new mode is causing unexpected problems: + + .. code-block:: ini + + # content of pytest.ini + [pytest] + console_output_style = classic + + +.. confval:: doctest_encoding + + .. versionadded:: 3.1 + + Default encoding to use to decode text files with docstrings. + :doc:`See how pytest handles doctests `. + + +.. confval:: doctest_optionflags + + One or more doctest flag names from the standard ``doctest`` module. + :doc:`See how pytest handles doctests `. + + +.. confval:: empty_parameter_set_mark + + .. versionadded:: 3.4 + + Allows to pick the action for empty parametersets in parameterization + + * ``skip`` skips tests with an empty parameterset (default) + * ``xfail`` marks tests with an empty parameterset as xfail(run=False) + + .. code-block:: ini + + # content of pytest.ini + [pytest] + empty_parameter_set_mark = xfail + + .. note:: + + The default value of this option is planned to change to ``xfail`` in future releases + as this is considered less error prone, see `#3155 `_ + for more details. + + +.. confval:: filterwarnings + + .. versionadded:: 3.1 + + Sets a list of filters and actions that should be taken for matched + warnings. By default all warnings emitted during the test session + will be displayed in a summary at the end of the test session. + + .. code-block:: ini + + # content of pytest.ini + [pytest] + filterwarnings = + error + ignore::DeprecationWarning + + This tells pytest to ignore deprecation warnings and turn all other warnings + into errors. For more information please refer to :ref:`warnings`. + + +.. confval:: junit_suite_name + + .. versionadded:: 3.1 + + To set the name of the root test suite xml item, you can configure the ``junit_suite_name`` option in your config file: + + .. code-block:: ini + + [pytest] + junit_suite_name = my_suite + + +.. confval:: log_cli_date_format + + .. versionadded:: 3.3 + + Sets a :py:func:`time.strftime`-compatible string that will be used when formatting dates for live logging. + + .. code-block:: ini + + [pytest] + log_cli_date_format = %Y-%m-%d %H:%M:%S + + For more information, see :ref:`live_logs`. + +.. confval:: log_cli_format + + .. versionadded:: 3.3 + + Sets a :py:mod:`logging`-compatible string used to format live logging messages. + + .. code-block:: ini + + [pytest] + log_cli_format = %(asctime)s %(levelname)s %(message)s + + For more information, see :ref:`live_logs`. + + +.. confval:: log_cli_level + + .. versionadded:: 3.3 + + Sets the minimum log message level that should be captured for live logging. The integer value or + the names of the levels can be used. + + .. code-block:: ini + + [pytest] + log_cli_level = INFO + + For more information, see :ref:`live_logs`. + + +.. confval:: log_date_format + + .. versionadded:: 3.3 + + Sets a :py:func:`time.strftime`-compatible string that will be used when formatting dates for logging capture. + + .. code-block:: ini + + [pytest] + log_date_format = %Y-%m-%d %H:%M:%S + + For more information, see :ref:`logging`. + + +.. confval:: log_file + + .. versionadded:: 3.3 + + Sets a file name relative to the ``pytest.ini`` file where log messages should be written to, in addition + to the other logging facilities that are active. + + .. code-block:: ini + + [pytest] + log_file = logs/pytest-logs.txt + + For more information, see :ref:`logging`. + + +.. confval:: log_file_date_format + + .. versionadded:: 3.3 + + Sets a :py:func:`time.strftime`-compatible string that will be used when formatting dates for the logging file. + + .. code-block:: ini + + [pytest] + log_file_date_format = %Y-%m-%d %H:%M:%S + + For more information, see :ref:`logging`. + +.. confval:: log_file_format + + .. versionadded:: 3.3 + + Sets a :py:mod:`logging`-compatible string used to format logging messages redirected to the logging file. + + .. code-block:: ini + + [pytest] + log_file_format = %(asctime)s %(levelname)s %(message)s + + For more information, see :ref:`logging`. + +.. confval:: log_file_level + + .. versionadded:: 3.3 + + Sets the minimum log message level that should be captured for the logging file. The integer value or + the names of the levels can be used. + + .. code-block:: ini + + [pytest] + log_file_level = INFO + + For more information, see :ref:`logging`. + + +.. confval:: log_format + + .. versionadded:: 3.3 + + Sets a :py:mod:`logging`-compatible string used to format captured logging messages. + + .. code-block:: ini + + [pytest] + log_format = %(asctime)s %(levelname)s %(message)s + + For more information, see :ref:`logging`. + + +.. confval:: log_level + + .. versionadded:: 3.3 + + Sets the minimum log message level that should be captured for logging capture. The integer value or + the names of the levels can be used. + + .. code-block:: ini + + [pytest] + log_level = INFO + + For more information, see :ref:`logging`. + + +.. confval:: log_print + + .. versionadded:: 3.3 + + If set to ``False``, will disable displaying captured logging messages for failed tests. + + .. code-block:: ini + + [pytest] + log_print = False + + For more information, see :ref:`logging`. + + +.. confval:: markers + + List of markers that are allowed in test functions, enforced when ``--strict`` command-line argument is used. + You can use a marker name per line, indented from the option name. + + .. code-block:: ini + + [pytest] + markers = + slow + serial + +.. confval:: minversion + + Specifies a minimal pytest version required for running tests. + + .. code-block:: ini + + # content of pytest.ini + [pytest] + minversion = 3.0 # will fail if we run with pytest-2.8 + + +.. confval:: norecursedirs + + Set the directory basename patterns to avoid when recursing + for test discovery. The individual (fnmatch-style) patterns are + applied to the basename of a directory to decide if to recurse into it. + Pattern matching characters:: + + * matches everything + ? matches any single character + [seq] matches any character in seq + [!seq] matches any char not in seq + + Default patterns are ``'.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', 'venv'``. + Setting a ``norecursedirs`` replaces the default. Here is an example of + how to avoid certain directories: + + .. code-block:: ini + + [pytest] + norecursedirs = .svn _build tmp* + + This would tell ``pytest`` to not look into typical subversion or + sphinx-build directories or into any ``tmp`` prefixed directory. + + Additionally, ``pytest`` will attempt to intelligently identify and ignore a + virtualenv by the presence of an activation script. Any directory deemed to + be the root of a virtual environment will not be considered during test + collection unless ``‑‑collect‑in‑virtualenv`` is given. Note also that + ``norecursedirs`` takes precedence over ``‑‑collect‑in‑virtualenv``; e.g. if + you intend to run tests in a virtualenv with a base directory that matches + ``'.*'`` you *must* override ``norecursedirs`` in addition to using the + ``‑‑collect‑in‑virtualenv`` flag. + + +.. confval:: python_classes + + One or more name prefixes or glob-style patterns determining which classes + are considered for test collection. By default, pytest will consider any + class prefixed with ``Test`` as a test collection. Here is an example of how + to collect tests from classes that end in ``Suite``: + + .. code-block:: ini + + [pytest] + python_classes = *Suite + + Note that ``unittest.TestCase`` derived classes are always collected + regardless of this option, as ``unittest``'s own collection framework is used + to collect those tests. + + +.. confval:: python_files + + One or more Glob-style file patterns determining which python files + are considered as test modules. By default, pytest will consider + any file matching with ``test_*.py`` and ``*_test.py`` globs as a test + module. + + +.. confval:: python_functions + + One or more name prefixes or glob-patterns determining which test functions + and methods are considered tests. By default, pytest will consider any + function prefixed with ``test`` as a test. Here is an example of how + to collect test functions and methods that end in ``_test``: + + .. code-block:: ini + + [pytest] + python_functions = *_test + + Note that this has no effect on methods that live on a ``unittest + .TestCase`` derived class, as ``unittest``'s own collection framework is used + to collect those tests. + + See :ref:`change naming conventions` for more detailed examples. + + +.. confval:: testpaths + + .. versionadded:: 2.8 + + Sets list of directories that should be searched for tests when + no specific directories, files or test ids are given in the command line when + executing pytest from the :ref:`rootdir ` directory. + Useful when all project tests are in a known location to speed up + test collection and to avoid picking up undesired tests by accident. + + .. code-block:: ini + + [pytest] + testpaths = testing doc + + This tells pytest to only look for tests in ``testing`` and ``doc`` + directories when executing from the root directory. + + +.. confval:: usefixtures + + List of fixtures that will be applied to all test functions; this is semantically the same to apply + the ``@pytest.mark.usefixtures`` marker to all test functions. + + + .. code-block:: ini + + [pytest] + usefixtures = + clean_db + + +.. confval:: xfail_strict + + If set to ``True``, tests marked with ``@pytest.mark.xfail`` that actually succeed will by default fail the + test suite. + For more information, see :ref:`xfail strict tutorial`. + + + .. code-block:: ini + + [pytest] + xfail_strict = True diff --git a/third_party/python/pytest/doc/en/requirements.txt b/third_party/python/pytest/doc/en/requirements.txt index 72bb60a811ff..e3cc47ed5a50 100644 --- a/third_party/python/pytest/doc/en/requirements.txt +++ b/third_party/python/pytest/doc/en/requirements.txt @@ -1,3 +1,4 @@ # pinning sphinx to 1.4.* due to search issues with rtd: # https://github.com/rtfd/readthedocs-sphinx-ext/issues/25 sphinx ==1.4.* +sphinxcontrib-trio diff --git a/third_party/python/pytest/doc/en/skipping.rst b/third_party/python/pytest/doc/en/skipping.rst index f0fbaa774440..bfa6f3e75a62 100644 --- a/third_party/python/pytest/doc/en/skipping.rst +++ b/third_party/python/pytest/doc/en/skipping.rst @@ -58,9 +58,21 @@ by calling the ``pytest.skip(reason)`` function: if not valid_config(): pytest.skip("unsupported configuration") +It is also possible to skip the whole module using +``pytest.skip(reason, allow_module_level=True)`` at the module level: + +.. code-block:: python + + import pytest + + if not pytest.config.getoption("--custom-flag"): + pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True) + The imperative method is useful when it is not possible to evaluate the skip condition during import time. +**Reference**: :ref:`pytest.mark.skip ref` + ``skipif`` ~~~~~~~~~~ @@ -68,11 +80,11 @@ during import time. If you wish to skip something conditionally then you can use ``skipif`` instead. Here is an example of marking a test function to be skipped -when run on a Python3.3 interpreter:: +when run on a Python3.6 interpreter:: import sys - @pytest.mark.skipif(sys.version_info < (3,3), - reason="requires python3.3") + @pytest.mark.skipif(sys.version_info < (3,6), + reason="requires python3.6") def test_function(): ... @@ -106,6 +118,8 @@ Alternatively, you can use :ref:`condition strings ` instead of booleans, but they can't be shared between modules easily so they are supported mainly for backward compatibility reasons. +**Reference**: :ref:`pytest.mark.skipif ref` + Skip all test functions of a class or module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -178,19 +192,19 @@ Here's a quick guide on how to skip tests in a module in different situations: .. code-block:: python - pytestmark = pytest.mark.skip('all tests still WIP') + pytestmark = pytest.mark.skip("all tests still WIP") 2. Skip all tests in a module based on some condition: .. code-block:: python - pytestmark = pytest.mark.skipif(sys.platform == 'win32', 'tests for linux only') + pytestmark = pytest.mark.skipif(sys.platform == "win32", "tests for linux only") 3. Skip all tests in a module if some import is missing: .. code-block:: python - pexpect = pytest.importorskip('pexpect') + pexpect = pytest.importorskip("pexpect") .. _xfail: @@ -222,15 +236,10 @@ This will unconditionally make ``test_function`` ``XFAIL``. Note that no other c after ``pytest.xfail`` call, differently from the marker. That's because it is implemented internally by raising a known exception. -Here's the signature of the ``xfail`` **marker** (not the function), using Python 3 keyword-only -arguments syntax: - -.. code-block:: python - - def xfail(condition=None, *, reason=None, raises=None, run=True, strict=False): - +**Reference**: :ref:`pytest.mark.xfail ref` +.. _`xfail strict tutorial`: ``strict`` parameter ~~~~~~~~~~~~~~~~~~~~ @@ -264,8 +273,8 @@ You can change the default value of the ``strict`` parameter using the As with skipif_ you can also mark your expectation of a failure on a particular platform:: - @pytest.mark.xfail(sys.version_info >= (3,3), - reason="python3.3 api changes") + @pytest.mark.xfail(sys.version_info >= (3,6), + reason="python3.6 api changes") def test_function(): ... @@ -321,16 +330,16 @@ Here is a simple test file with the several usages: Running it with the report-on-xfail option gives this output:: example $ pytest -rx xfail_demo.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR/example, inifile: collected 7 items - - xfail_demo.py xxxxxxx - ======= short test summary info ======== + + xfail_demo.py xxxxxxx [100%] + ========================= short test summary info ========================== XFAIL xfail_demo.py::test_hello XFAIL xfail_demo.py::test_hello2 - reason: [NOTRUN] + reason: [NOTRUN] XFAIL xfail_demo.py::test_hello3 condition: hasattr(os, 'sep') XFAIL xfail_demo.py::test_hello4 @@ -340,8 +349,8 @@ Running it with the report-on-xfail option gives this output:: XFAIL xfail_demo.py::test_hello6 reason: reason XFAIL xfail_demo.py::test_hello7 - - ======= 7 xfailed in 0.12 seconds ======== + + ======================== 7 xfailed in 0.12 seconds ========================= .. _`skip/xfail with parametrize`: @@ -355,14 +364,20 @@ test instances when using parametrize: import pytest - @pytest.mark.parametrize(("n", "expected"), [ - (1, 2), - pytest.param(1, 0, marks=pytest.mark.xfail), - pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), - (2, 3), - (3, 4), - (4, 5), - pytest.param(10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k")), - ]) + + @pytest.mark.parametrize( + ("n", "expected"), + [ + (1, 2), + pytest.param(1, 0, marks=pytest.mark.xfail), + pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), + (2, 3), + (3, 4), + (4, 5), + pytest.param( + 10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k") + ), + ], + ) def test_increment(n, expected): assert n + 1 == expected diff --git a/third_party/python/pytest/doc/en/talks.rst b/third_party/python/pytest/doc/en/talks.rst index bf593db4b4b1..79534ea6e9a6 100644 --- a/third_party/python/pytest/doc/en/talks.rst +++ b/third_party/python/pytest/doc/en/talks.rst @@ -98,8 +98,3 @@ Plugin specific examples: .. _`parametrizing tests, generalized`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/ .. _`generating parametrized tests with fixtures`: parametrize.html#test-generators .. _`test generators and cached setup`: http://bruynooghe.blogspot.com/2010/06/pytest-test-generators-and-cached-setup.html - - - - - diff --git a/third_party/python/pytest/doc/en/test/attic.rst b/third_party/python/pytest/doc/en/test/attic.rst deleted file mode 100644 index 06944661cb5c..000000000000 --- a/third_party/python/pytest/doc/en/test/attic.rst +++ /dev/null @@ -1,117 +0,0 @@ -=============================================== -ATTIC documentation -=============================================== - -XXX REVIEW and remove the below XXX - -Customizing the testing process -=============================== - -writing conftest.py files ------------------------------------ - -You may put conftest.py files containing project-specific -configuration in your project's root directory, it's usually -best to put it just into the same directory level as your -topmost ``__init__.py``. In fact, ``pytest`` performs -an "upwards" search starting from the directory that you specify -to be tested and will lookup configuration values right-to-left. -You may have options that reside e.g. in your home directory -but note that project specific settings will be considered -first. There is a flag that helps you debugging your -conftest.py configurations:: - - pytest --trace-config - - -customizing the collecting and running process ------------------------------------------------ - -To introduce different test items you can create -one or more ``conftest.py`` files in your project. -When the collection process traverses directories -and modules the default collectors will produce -custom Collectors and Items if they are found -in a local ``conftest.py`` file. - - -Customizing the collection process in a module ----------------------------------------------- - -If you have a module where you want to take responsibility for -collecting your own test Items and possibly even for executing -a test then you can provide `generative tests`_ that yield -callables and possibly arguments as a tuple. This is especially -useful for calling application test machinery with different -parameter sets but counting each of the calls as a separate -tests. - -.. _`generative tests`: features.html#generative-tests - -The other extension possibility is about -specifying a custom test ``Item`` class which -is responsible for setting up and executing an underlying -test. Or you can extend the collection process for a whole -directory tree by putting Items in a ``conftest.py`` configuration file. -The collection process dynamically consults the *chain of conftest.py* -modules to determine collectors and items at ``Directory``, ``Module``, -``Class``, ``Function`` or ``Generator`` level respectively. - -Customizing execution of Items and Functions ----------------------------------------------------- - -- ``pytest.Function`` test items control execution - of a test function through its ``function.runtest()`` method. - This method is responsible for performing setup and teardown - ("Test Fixtures") for a test Function. - -- ``Function.execute(target, *args)`` methods are invoked by - the default ``Function.run()`` to actually execute a python - function with the given (usually empty set of) arguments. - -.. _`py-dev mailing list`: http://codespeak.net/mailman/listinfo/py-dev - - -.. _`test generators`: funcargs.html#test-generators - -.. _`generative tests`: - -generative tests: yielding parametrized tests -==================================================== - -Deprecated since 1.0 in favour of `test generators`_. - -*Generative tests* are test methods that are *generator functions* which -``yield`` callables and their arguments. This is useful for running a -test function multiple times against different parameters. Example:: - - def test_generative(): - for x in (42,17,49): - yield check, x - - def check(arg): - assert arg % 7 == 0 # second generated tests fails! - -Note that ``test_generative()`` will cause three tests -to get run, notably ``check(42)``, ``check(17)`` and ``check(49)`` -of which the middle one will obviously fail. - -To make it easier to distinguish the generated tests it is possible to specify an explicit name for them, like for example:: - - def test_generative(): - for x in (42,17,49): - yield "case %d" % x, check, x - - -disabling a test class ----------------------- - -If you want to disable a complete test class you -can set the class-level attribute ``disabled``. -For example, in order to avoid running some tests on Win32:: - - class TestPosixOnly(object): - disabled = sys.platform == 'win32' - - def test_xxx(self): - ... diff --git a/third_party/python/pytest/doc/en/test/config.html b/third_party/python/pytest/doc/en/test/config.html deleted file mode 100644 index cba5a46f982e..000000000000 --- a/third_party/python/pytest/doc/en/test/config.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - diff --git a/third_party/python/pytest/doc/en/test/dist.html b/third_party/python/pytest/doc/en/test/dist.html deleted file mode 100644 index e328550a85cb..000000000000 --- a/third_party/python/pytest/doc/en/test/dist.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - diff --git a/third_party/python/pytest/doc/en/test/extend.html b/third_party/python/pytest/doc/en/test/extend.html deleted file mode 100644 index cba5a46f982e..000000000000 --- a/third_party/python/pytest/doc/en/test/extend.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - diff --git a/third_party/python/pytest/doc/en/test/index.rst b/third_party/python/pytest/doc/en/test/index.rst deleted file mode 100644 index 1a3b5a54da08..000000000000 --- a/third_party/python/pytest/doc/en/test/index.rst +++ /dev/null @@ -1,35 +0,0 @@ -======================================= -pytest documentation index -======================================= - - -features_: overview and discussion of features. - -quickstart_: getting started with writing a simple test. - -`talks, tutorials, examples`_: tutorial examples, slides - -funcargs_: powerful parametrized test function setup - -`plugins`_: list of available plugins with usage examples and feature details. - -customize_: configuration, customization, extensions - -changelog_: history of changes covering last releases - -**Continuous Integration of pytest's own tests and plugins with Hudson**: - - `http://hudson.testrun.org/view/pytest`_ - -.. _`http://hudson.testrun.org/view/pytest`: http://hudson.testrun.org/view/pytest/ - - -.. _changelog: ../changelog.html -.. _`plugins`: plugin/index.html -.. _`talks, tutorials, examples`: talks.html -.. _quickstart: quickstart.html -.. _features: features.html -.. _funcargs: funcargs.html -.. _customize: customize.html - - diff --git a/third_party/python/pytest/doc/en/test/mission.rst b/third_party/python/pytest/doc/en/test/mission.rst deleted file mode 100644 index 51c252dc0d86..000000000000 --- a/third_party/python/pytest/doc/en/test/mission.rst +++ /dev/null @@ -1,13 +0,0 @@ - -Mission -==================================== - -``pytest`` strives to make testing a fun and no-boilerplate effort. - -The tool is distributed as a `pytest` package. Its project independent -``pytest`` command line tool helps you to: - -* rapidly collect and run tests -* run unit- or doctests, functional or integration tests -* distribute tests to multiple environments -* use local or global plugins for custom test types and setup diff --git a/third_party/python/pytest/doc/en/test/plugin/cov.rst b/third_party/python/pytest/doc/en/test/plugin/cov.rst deleted file mode 100644 index 541c7ef94793..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/cov.rst +++ /dev/null @@ -1,230 +0,0 @@ - -produce code coverage reports using the 'coverage' package, including support for distributed testing. -====================================================================================================== - - -.. contents:: - :local: - -This plugin produces coverage reports. It supports centralised testing and distributed testing in -both load and each modes. It also supports coverage of subprocesses. - -All features offered by the coverage package should be available, either through pytest-cov or -through coverage's config file. - - -Installation ------------- - -The `pytest-cov`_ package may be installed with pip or easy_install:: - - pip install pytest-cov - easy_install pytest-cov - -.. _`pytest-cov`: http://pypi.python.org/pypi/pytest-cov/ - - -Uninstallation --------------- - -Uninstalling packages is supported by pip:: - - pip uninstall pytest-cov - -However easy_install does not provide an uninstall facility. - -.. IMPORTANT:: - - Ensure that you manually delete the init_covmain.pth file in your - site-packages directory. - - This file starts coverage collection of subprocesses if appropriate during - site initialization at python startup. - - -Usage ------ - -Centralised Testing -~~~~~~~~~~~~~~~~~~~ - -Centralised testing will report on the combined coverage of the main process and all of it's -subprocesses. - -Running centralised testing:: - - pytest --cov myproj tests/ - -Shows a terminal report:: - - -------------------- coverage: platform linux2, python 2.6.4-final-0 --------------------- - Name Stmts Miss Cover - ---------------------------------------- - myproj/__init__ 2 0 100% - myproj/myproj 257 13 94% - myproj/feature4286 94 7 92% - ---------------------------------------- - TOTAL 353 20 94% - - -Distributed Testing: Load -~~~~~~~~~~~~~~~~~~~~~~~~~ - -Distributed testing with dist mode set to load will report on the combined coverage of all slaves. -The slaves may be spread out over any number of hosts and each slave may be located anywhere on the -file system. Each slave will have it's subprocesses measured. - -Running distributed testing with dist mode set to load:: - - pytest --cov myproj -n 2 tests/ - -Shows a terminal report:: - - -------------------- coverage: platform linux2, python 2.6.4-final-0 --------------------- - Name Stmts Miss Cover - ---------------------------------------- - myproj/__init__ 2 0 100% - myproj/myproj 257 13 94% - myproj/feature4286 94 7 92% - ---------------------------------------- - TOTAL 353 20 94% - - -Again but spread over different hosts and different directories:: - - pytest --cov myproj --dist load - --tx ssh=memedough@host1//chdir=testenv1 - --tx ssh=memedough@host2//chdir=/tmp/testenv2//python=/tmp/env1/bin/python - --rsyncdir myproj --rsyncdir tests --rsync examples - tests/ - -Shows a terminal report:: - - -------------------- coverage: platform linux2, python 2.6.4-final-0 --------------------- - Name Stmts Miss Cover - ---------------------------------------- - myproj/__init__ 2 0 100% - myproj/myproj 257 13 94% - myproj/feature4286 94 7 92% - ---------------------------------------- - TOTAL 353 20 94% - - -Distributed Testing: Each -~~~~~~~~~~~~~~~~~~~~~~~~~ - -Distributed testing with dist mode set to each will report on the combined coverage of all slaves. -Since each slave is running all tests this allows generating a combined coverage report for multiple -environments. - -Running distributed testing with dist mode set to each:: - - pytest --cov myproj --dist each - --tx popen//chdir=/tmp/testenv3//python=/usr/local/python27/bin/python - --tx ssh=memedough@host2//chdir=/tmp/testenv4//python=/tmp/env2/bin/python - --rsyncdir myproj --rsyncdir tests --rsync examples - tests/ - -Shows a terminal report:: - - ---------------------------------------- coverage ---------------------------------------- - platform linux2, python 2.6.5-final-0 - platform linux2, python 2.7.0-final-0 - Name Stmts Miss Cover - ---------------------------------------- - myproj/__init__ 2 0 100% - myproj/myproj 257 13 94% - myproj/feature4286 94 7 92% - ---------------------------------------- - TOTAL 353 20 94% - - -Reporting ---------- - -It is possible to generate any combination of the reports for a single test run. - -The available reports are terminal (with or without missing line numbers shown), HTML, XML and -annotated source code. - -The terminal report without line numbers (default):: - - pytest --cov-report term --cov myproj tests/ - - -------------------- coverage: platform linux2, python 2.6.4-final-0 --------------------- - Name Stmts Miss Cover - ---------------------------------------- - myproj/__init__ 2 0 100% - myproj/myproj 257 13 94% - myproj/feature4286 94 7 92% - ---------------------------------------- - TOTAL 353 20 94% - - -The terminal report with line numbers:: - - pytest --cov-report term-missing --cov myproj tests/ - - -------------------- coverage: platform linux2, python 2.6.4-final-0 --------------------- - Name Stmts Miss Cover Missing - -------------------------------------------------- - myproj/__init__ 2 0 100% - myproj/myproj 257 13 94% 24-26, 99, 149, 233-236, 297-298, 369-370 - myproj/feature4286 94 7 92% 183-188, 197 - -------------------------------------------------- - TOTAL 353 20 94% - - -The remaining three reports output to files without showing anything on the terminal (useful for -when the output is going to a continuous integration server):: - - pytest --cov-report html --cov-report xml --cov-report annotate --cov myproj tests/ - - -Coverage Data File ------------------- - -The data file is erased at the beginning of testing to ensure clean data for each test run. - -The data file is left at the end of testing so that it is possible to use normal coverage tools to -examine it. - - -Limitations ------------ - -For distributed testing the slaves must have the pytest-cov package installed. This is needed since -the plugin must be registered through setuptools / distribute for pytest to start the plugin on the -slave. - -For subprocess measurement environment variables must make it from the main process to the -subprocess. The python used by the subprocess must have pytest-cov installed. The subprocess must -do normal site initialization so that the environment variables can be detected and coverage -started. - - -Acknowledgments ----------------- - -Holger Krekel for pytest with its distributed testing support. - -Ned Batchelder for coverage and its ability to combine the coverage results of parallel runs. - -Whilst this plugin has been built fresh from the ground up to support distributed testing it has -been influenced by the work done on pytest-coverage (Ross Lawley, James Mills, Holger Krekel) and -nose-cover (Jason Pellerin) which are other coverage plugins for pytest and nose respectively. - -No doubt others have contributed to these tools as well. - -command line options --------------------- - - -``--cov=path`` - measure coverage for filesystem path (multi-allowed) -``--cov-report=type`` - type of report to generate: term, term-missing, annotate, html, xml (multi-allowed) -``--cov-config=path`` - config file for coverage, default: .coveragerc - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/coverage.rst b/third_party/python/pytest/doc/en/test/plugin/coverage.rst deleted file mode 100644 index 71139d008ba0..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/coverage.rst +++ /dev/null @@ -1,51 +0,0 @@ - -Write and report coverage data with the 'coverage' package. -=========================================================== - - -.. contents:: - :local: - -Note: Original code by Ross Lawley. - -Install --------------- - -Use pip to (un)install:: - - pip install pytest-coverage - pip uninstall pytest-coverage - -or alternatively use easy_install to install:: - - easy_install pytest-coverage - - -Usage -------------- - -To get full test coverage reports for a particular package type:: - - pytest --cover-report=report - -command line options --------------------- - - -``--cover=COVERPACKAGES`` - (multi allowed) only include info from specified package. -``--cover-report=REPORT_TYPE`` - html: Directory for html output. - report: Output a text report. - annotate: Annotate your source code for which lines were executed and which were not. - xml: Output an xml report compatible with the cobertura plugin for hudson. -``--cover-directory=DIRECTORY`` - Directory for the reports (html / annotate results) defaults to ./coverage -``--cover-xml-file=XML_FILE`` - File for the xml report defaults to ./coverage.xml -``--cover-show-missing`` - Show missing files -``--cover-ignore-errors=IGNORE_ERRORS`` - Ignore errors of finding source files for code. - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/django.rst b/third_party/python/pytest/doc/en/test/plugin/django.rst deleted file mode 100644 index 061497b385ed..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/django.rst +++ /dev/null @@ -1,7 +0,0 @@ -pytest_django plugin (EXTERNAL) -========================================== - -pytest_django is a plugin for ``pytest`` that provides a set of useful tools for testing Django applications, checkout Ben Firshman's `pytest_django github page`_. - -.. _`pytest_django github page`: http://github.com/bfirsh/pytest_django/tree/master - diff --git a/third_party/python/pytest/doc/en/test/plugin/figleaf.rst b/third_party/python/pytest/doc/en/test/plugin/figleaf.rst deleted file mode 100644 index 0c1603ade9fc..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/figleaf.rst +++ /dev/null @@ -1,44 +0,0 @@ - -report test coverage using the 'figleaf' package. -================================================= - - -.. contents:: - :local: - -Install ---------------- - -To install the plugin issue:: - - easy_install pytest-figleaf # or - pip install pytest-figleaf - -and if you are using pip you can also uninstall:: - - pip uninstall pytest-figleaf - - -Usage ---------------- - -After installation you can simply type:: - - pytest --figleaf [...] - -to enable figleaf coverage in your test run. A default ".figleaf" data file -and "html" directory will be created. You can use command line options -to control where data and html files are created. - -command line options --------------------- - - -``--figleaf`` - trace python coverage with figleaf and write HTML for files below the current working dir -``--fig-data=dir`` - set tracing file, default: ".figleaf". -``--fig-html=dir`` - set html reporting dir, default "html". - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/helpconfig.rst b/third_party/python/pytest/doc/en/test/plugin/helpconfig.rst deleted file mode 100644 index 326b75c4552e..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/helpconfig.rst +++ /dev/null @@ -1,36 +0,0 @@ - -provide version info, conftest/environment config names. -======================================================== - - -.. contents:: - :local: - - - -command line options --------------------- - - -``--version`` - display py lib version and import information. -``-p name`` - early-load given plugin (multi-allowed). -``--trace-config`` - trace considerations of conftest.py files. -``--debug`` - generate and show internal debugging information. -``--help-config`` - show available conftest.py and ENV-variable names. - -Start improving this plugin in 30 seconds -========================================= - - -1. Download `pytest_helpconfig.py`_ plugin source code -2. put it somewhere as ``pytest_helpconfig.py`` into your import path -3. a subsequent ``pytest`` run will use your local version - -Checkout customize_, other plugins_ or `get in contact`_. - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/index.rst b/third_party/python/pytest/doc/en/test/plugin/index.rst deleted file mode 100644 index 853a4dce681c..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/index.rst +++ /dev/null @@ -1,68 +0,0 @@ - -advanced python testing -======================= - -skipping_ advanced skipping for python test functions, classes or modules. - -mark_ generic mechanism for marking python functions. - -pdb_ interactive debugging with the Python Debugger. - -figleaf_ (external) report test coverage using the 'figleaf' package. - -monkeypatch_ safely patch object attributes, dicts and environment variables. - -coverage_ (external) Write and report coverage data with the 'coverage' package. - -cov_ (external) produce code coverage reports using the 'coverage' package, including support for distributed testing. - -capture_ configurable per-test stdout/stderr capturing mechanisms. - -capturelog_ (external) capture output of logging module. - -recwarn_ helpers for asserting deprecation and other warnings. - -tmpdir_ provide temporary directories to test functions. - - -distributed testing, CI and deployment -====================================== - -xdist_ (external) loop on failing tests, distribute test runs to CPUs and hosts. - -pastebin_ submit failure or test session information to a pastebin service. - -junitxml_ logging of test results in JUnit-XML format, for use with Hudson - -resultlog_ non-xml machine-readable logging of test results. - -genscript_ generate standalone test script to be distributed along with an application. - - -testing domains and conventions codecheckers -============================================ - -oejskit_ (external) run javascript tests in real life browsers - -django_ (external) for testing django applications - -unittest_ automatically discover and run traditional "unittest.py" style tests. - -nose_ nose-compatibility plugin: allow to run nose test suites natively. - -doctest_ collect and execute doctests from modules and test files. - -restdoc_ perform ReST syntax, local and remote reference tests on .rst/.txt files. - - -internal, debugging, help functionality -======================================= - -helpconfig_ provide version info, conftest/environment config names. - -terminal_ Implements terminal reporting of the full testing process. - -hooklog_ log invocations of extension hooks to a file. - - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/links.rst b/third_party/python/pytest/doc/en/test/plugin/links.rst deleted file mode 100644 index 6dec2b4848a6..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/links.rst +++ /dev/null @@ -1,45 +0,0 @@ -.. _`helpconfig`: helpconfig.html -.. _`pytest_recwarn.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_recwarn.py -.. _`unittest`: unittest.html -.. _`pytest_monkeypatch.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_monkeypatch.py -.. _`pastebin`: pastebin.html -.. _`skipping`: skipping.html -.. _`plugins`: index.html -.. _`mark`: mark.html -.. _`tmpdir`: tmpdir.html -.. _`pytest_doctest.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_doctest.py -.. _`capture`: capture.html -.. _`pytest_nose.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_nose.py -.. _`pytest_restdoc.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_restdoc.py -.. _`restdoc`: restdoc.html -.. _`xdist`: xdist.html -.. _`pytest_pastebin.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_pastebin.py -.. _`pytest_tmpdir.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_tmpdir.py -.. _`terminal`: terminal.html -.. _`pytest_hooklog.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_hooklog.py -.. _`capturelog`: capturelog.html -.. _`junitxml`: junitxml.html -.. _`pytest_skipping.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_skipping.py -.. _`checkout the pytest development version`: ../../install.html#checkout -.. _`pytest_helpconfig.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_helpconfig.py -.. _`oejskit`: oejskit.html -.. _`doctest`: doctest.html -.. _`pytest_mark.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_mark.py -.. _`get in contact`: ../../contact.html -.. _`pytest_capture.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_capture.py -.. _`figleaf`: figleaf.html -.. _`customize`: ../customize.html -.. _`hooklog`: hooklog.html -.. _`pytest_terminal.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_terminal.py -.. _`recwarn`: recwarn.html -.. _`pytest_pdb.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_pdb.py -.. _`monkeypatch`: monkeypatch.html -.. _`coverage`: coverage.html -.. _`resultlog`: resultlog.html -.. _`cov`: cov.html -.. _`pytest_junitxml.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_junitxml.py -.. _`django`: django.html -.. _`pytest_unittest.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_unittest.py -.. _`nose`: nose.html -.. _`pytest_resultlog.py`: http://bitbucket.org/hpk42/py-trunk/raw/1.3.4/py/_plugin/pytest_resultlog.py -.. _`pdb`: pdb.html diff --git a/third_party/python/pytest/doc/en/test/plugin/nose.rst b/third_party/python/pytest/doc/en/test/plugin/nose.rst deleted file mode 100644 index 9eeae5ff697f..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/nose.rst +++ /dev/null @@ -1,56 +0,0 @@ - -nose-compatibility plugin: allow to run nose test suites natively. -================================================================== - - -.. contents:: - :local: - -This is an experimental plugin for allowing to run tests written -in 'nosetests' style with ``pytest``. - -Usage -------------- - -type:: - - pytest # instead of 'nosetests' - -and you should be able to run nose style tests and at the same -time can make full use of pytest's capabilities. - -Supported nose Idioms ----------------------- - -* setup and teardown at module/class/method level -* SkipTest exceptions and markers -* setup/teardown decorators -* yield-based tests and their setup -* general usage of nose utilities - -Unsupported idioms / issues ----------------------------------- - -- nose-style doctests are not collected and executed correctly, - also fixtures don't work. - -- no nose-configuration is recognized - -If you find other issues or have suggestions please run:: - - pytest --pastebin=all - -and send the resulting URL to a ``pytest`` contact channel, -at best to the mailing list. - -Start improving this plugin in 30 seconds -========================================= - - -1. Download `pytest_nose.py`_ plugin source code -2. put it somewhere as ``pytest_nose.py`` into your import path -3. a subsequent ``pytest`` run will use your local version - -Checkout customize_, other plugins_ or `get in contact`_. - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/oejskit.rst b/third_party/python/pytest/doc/en/test/plugin/oejskit.rst deleted file mode 100644 index 4995aa17c72d..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/oejskit.rst +++ /dev/null @@ -1,12 +0,0 @@ -pytest_oejskit plugin (EXTERNAL) -========================================== - -The `oejskit`_ offers a ``pytest`` plugin for running Javascript tests in live browsers. Running inside the browsers comes with some speed cost, on the other hand it means for example the code is tested against the real-word DOM implementations. -The approach enables to write integration tests such that the JavaScript code is tested against server-side Python code mocked as necessary. Any server-side framework that can already be exposed through WSGI (or for which a subset of WSGI can be written to accommodate the jskit own needs) can play along. - -For more info and download please visit the `oejskit PyPI`_ page. - -.. _`oejskit`: -.. _`oejskit PyPI`: http://pypi.python.org/pypi/oejskit - -.. source link 'http://bitbucket.org/pedronis/js-infrastructure/src/tip/pytest_jstests.py', diff --git a/third_party/python/pytest/doc/en/test/plugin/terminal.rst b/third_party/python/pytest/doc/en/test/plugin/terminal.rst deleted file mode 100644 index e07d4f72183a..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/terminal.rst +++ /dev/null @@ -1,38 +0,0 @@ - -Implements terminal reporting of the full testing process. -========================================================== - - -.. contents:: - :local: - -This is a good source for looking at the various reporting hooks. - -command line options --------------------- - - -``-v, --verbose`` - increase verbosity. -``-r chars`` - show extra test summary info as specified by chars (f)ailed, (s)skipped, (x)failed, (X)passed. -``-l, --showlocals`` - show locals in tracebacks (disabled by default). -``--tb=style`` - traceback print mode (long/short/line/no). -``--full-trace`` - don't cut any tracebacks (default is to cut). -``--fixtures`` - show available function arguments, sorted by plugin - -Start improving this plugin in 30 seconds -========================================= - - -1. Download `pytest_terminal.py`_ plugin source code -2. put it somewhere as ``pytest_terminal.py`` into your import path -3. a subsequent ``pytest`` run will use your local version - -Checkout customize_, other plugins_ or `get in contact`_. - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/plugin/xdist.rst b/third_party/python/pytest/doc/en/test/plugin/xdist.rst deleted file mode 100644 index 506d240aee13..000000000000 --- a/third_party/python/pytest/doc/en/test/plugin/xdist.rst +++ /dev/null @@ -1,172 +0,0 @@ - -loop on failing tests, distribute test runs to CPUs and hosts. -============================================================== - - -.. contents:: - :local: - -The `pytest-xdist`_ plugin extends ``pytest`` with some unique -test execution modes: - -* Looponfail: run your tests repeatedly in a subprocess. After each run - ``pytest`` waits until a file in your project changes and then re-runs the - previously failing tests. This is repeated until all tests pass after which - again a full run is performed. - -* Load-balancing: if you have multiple CPUs or hosts you can use - those for a combined test run. This allows to speed up - development or to use special resources of remote machines. - -* Multi-Platform coverage: you can specify different Python interpreters - or different platforms and run tests in parallel on all of them. - -Before running tests remotely, ``pytest`` efficiently synchronizes your -program source code to the remote place. All test results -are reported back and displayed to your local test session. -You may specify different Python versions and interpreters. - -.. _`pytest-xdist`: http://pypi.python.org/pypi/pytest-xdist - -Usage examples ---------------------- - -Speed up test runs by sending tests to multiple CPUs -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -To send tests to multiple CPUs, type:: - - pytest -n NUM - -Especially for longer running tests or tests requiring -a lot of IO this can lead to considerable speed ups. - - -Running tests in a Python subprocess -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -To instantiate a python2.4 sub process and send tests to it, you may type:: - - pytest -d --tx popen//python=python2.4 - -This will start a subprocess which is run with the "python2.4" -Python interpreter, found in your system binary lookup path. - -If you prefix the --tx option value like this:: - - --tx 3*popen//python=python2.4 - -then three subprocesses would be created and tests -will be load-balanced across these three processes. - - -Sending tests to remote SSH accounts -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -Suppose you have a package ``mypkg`` which contains some -tests that you can successfully run locally. And you -have a ssh-reachable machine ``myhost``. Then -you can ad-hoc distribute your tests by typing:: - - pytest -d --tx ssh=myhostpopen --rsyncdir mypkg mypkg - -This will synchronize your ``mypkg`` package directory -to a remote ssh account and then locally collect tests -and send them to remote places for execution. - -You can specify multiple ``--rsyncdir`` directories -to be sent to the remote side. - -**NOTE:** For ``pytest`` to collect and send tests correctly -you not only need to make sure all code and tests -directories are rsynced, but that any test (sub) directory -also has an ``__init__.py`` file because internally -``pytest`` references tests using their fully qualified python -module path. **You will otherwise get strange errors** -during setup of the remote side. - -Sending tests to remote Socket Servers -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -Download the single-module `socketserver.py`_ Python program -and run it like this:: - - python socketserver.py - -It will tell you that it starts listening on the default -port. You can now on your home machine specify this -new socket host with something like this:: - - pytest -d --tx socket=192.168.1.102:8888 --rsyncdir mypkg mypkg - - -.. _`atonce`: - -Running tests on many platforms at once -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -The basic command to run tests on multiple platforms is:: - - pytest --dist=each --tx=spec1 --tx=spec2 - -If you specify a windows host, an OSX host and a Linux -environment this command will send each tests to all -platforms - and report back failures from all platforms -at once. The specifications strings use the `xspec syntax`_. - -.. _`xspec syntax`: http://codespeak.net/execnet/trunk/basics.html#xspec - -.. _`socketserver.py`: http://codespeak.net/svn/py/dist/py/execnet/script/socketserver.py - -.. _`execnet`: http://codespeak.net/execnet - -Specifying test exec environments in a conftest.py -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -Instead of specifying command line options, you can -put options values in a ``conftest.py`` file like this:: - - option_tx = ['ssh=myhost//python=python2.7', 'popen//python=python2.7'] - option_dist = True - -Any commandline ``--tx`` specifications will add to the list of -available execution environments. - -Specifying "rsync" dirs in a conftest.py -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -In your ``mypkg/conftest.py`` you may specify directories to synchronise -or to exclude:: - - rsyncdirs = ['.', '../plugins'] - rsyncignore = ['_cache'] - -These directory specifications are relative to the directory -where the ``conftest.py`` is found. - -command line options --------------------- - - -``-f, --looponfail`` - run tests in subprocess, wait for modified files and re-run failing test set until all pass. -``-n numprocesses`` - shortcut for '--dist=load --tx=NUM*popen' -``--boxed`` - box each test run in a separate process (unix) -``--dist=distmode`` - set mode for distributing tests to exec environments. - - each: send each test to each available environment. - - load: send each test to one available environment so it is run only once. - - (default) no: run tests inprocess, don't distribute. -``--tx=xspec`` - add a test execution environment. some examples: --tx popen//python=python2.7 --tx socket=192.168.1.102:8888 --tx ssh=user@codespeak.net//chdir=testcache -``-d`` - load-balance tests. shortcut for '--dist=load' -``--rsyncdir=dir1`` - add directory for rsyncing to remote tx nodes. - -.. include:: links.txt diff --git a/third_party/python/pytest/doc/en/test/test.html b/third_party/python/pytest/doc/en/test/test.html deleted file mode 100644 index 7d00f718a677..000000000000 --- a/third_party/python/pytest/doc/en/test/test.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - diff --git a/third_party/python/pytest/doc/en/tmpdir.rst b/third_party/python/pytest/doc/en/tmpdir.rst index 56a347619a99..421b4c898c5a 100644 --- a/third_party/python/pytest/doc/en/tmpdir.rst +++ b/third_party/python/pytest/doc/en/tmpdir.rst @@ -28,18 +28,18 @@ Running this would result in a passed test except for the last ``assert 0`` line which we use to look at values:: $ pytest test_tmpdir.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_tmpdir.py F - - ======= FAILURES ======== - _______ test_create_file ________ - + + test_tmpdir.py F [100%] + + ================================= FAILURES ================================= + _____________________________ test_create_file _____________________________ + tmpdir = local('PYTEST_TMPDIR/test_create_file0') - + def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") @@ -47,9 +47,11 @@ Running this would result in a passed test except for the last assert len(tmpdir.listdir()) == 1 > assert 0 E assert 0 - + test_tmpdir.py:7: AssertionError - ======= 1 failed in 0.12 seconds ======== + ========================= 1 failed in 0.12 seconds ========================= + +.. _`tmpdir factory example`: The 'tmpdir_factory' fixture ---------------------------- @@ -69,24 +71,22 @@ to save time: # contents of conftest.py import pytest - @pytest.fixture(scope='session') + + @pytest.fixture(scope="session") def image_file(tmpdir_factory): img = compute_expensive_image() - fn = tmpdir_factory.mktemp('data').join('img.png') + fn = tmpdir_factory.mktemp("data").join("img.png") img.save(str(fn)) return fn + # contents of test_image.py def test_histogram(image_file): img = load_image(image_file) # compute and test histogram -``tmpdir_factory`` instances have the following methods: +See :ref:`tmpdir_factory API ` for details. -.. currentmodule:: _pytest.tmpdir - -.. automethod:: TempdirFactory.mktemp -.. automethod:: TempdirFactory.getbasetemp .. _`base temporary directory`: @@ -106,6 +106,4 @@ When distributing tests on the local machine, ``pytest`` takes care to configure a basetemp directory for the sub processes such that all temporary data lands below a single per-test run basetemp directory. -.. _`py.path.local`: http://py.rtfd.org/en/latest/path.html - - +.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html diff --git a/third_party/python/pytest/doc/en/unittest.rst b/third_party/python/pytest/doc/en/unittest.rst index db1692029356..53192b3469d3 100644 --- a/third_party/python/pytest/doc/en/unittest.rst +++ b/third_party/python/pytest/doc/en/unittest.rst @@ -46,9 +46,9 @@ in most cases without having to modify existing code: * :ref:`maxfail`; * :ref:`--pdb ` command-line option for debugging on test failures (see :ref:`note ` below); -* Distribute tests to multiple CPUs using the `pytest-xdist `_ plugin; +* Distribute tests to multiple CPUs using the `pytest-xdist `_ plugin; * Use :ref:`plain assert-statements ` instead of ``self.assert*`` functions (`unittest2pytest - `__ is immensely helpful in this); + `__ is immensely helpful in this); pytest features in ``unittest.TestCase`` subclasses @@ -92,18 +92,18 @@ it from a unittest-style test:: def db_class(request): class DummyDB(object): pass - # set a class attribute on the invoking test context + # set a class attribute on the invoking test context request.cls.db = DummyDB() -This defines a fixture function ``db_class`` which - if used - is -called once for each test class and which sets the class-level +This defines a fixture function ``db_class`` which - if used - is +called once for each test class and which sets the class-level ``db`` attribute to a ``DummyDB`` instance. The fixture function achieves this by receiving a special ``request`` object which gives access to :ref:`the requesting test context ` such -as the ``cls`` attribute, denoting the class from which the fixture +as the ``cls`` attribute, denoting the class from which the fixture is used. This architecture de-couples fixture writing from actual test code and allows re-use of the fixture by a minimal reference, the fixture -name. So let's write an actual ``unittest.TestCase`` class using our +name. So let's write an actual ``unittest.TestCase`` class using our fixture definition:: # content of test_unittest_db.py @@ -120,42 +120,42 @@ fixture definition:: def test_method2(self): assert 0, self.db # fail for demo purposes -The ``@pytest.mark.usefixtures("db_class")`` class-decorator makes sure that +The ``@pytest.mark.usefixtures("db_class")`` class-decorator makes sure that the pytest fixture function ``db_class`` is called once per class. Due to the deliberately failing assert statements, we can take a look at the ``self.db`` values in the traceback:: $ pytest test_unittest_db.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - - test_unittest_db.py FF - - ======= FAILURES ======== - _______ MyTest.test_method1 ________ - + + test_unittest_db.py FF [100%] + + ================================= FAILURES ================================= + ___________________________ MyTest.test_method1 ____________________________ + self = - + def test_method1(self): assert hasattr(self, "db") > assert 0, self.db # fail for demo purposes E AssertionError: .DummyDB object at 0xdeadbeef> E assert 0 - + test_unittest_db.py:9: AssertionError - _______ MyTest.test_method2 ________ - + ___________________________ MyTest.test_method2 ____________________________ + self = - + def test_method2(self): > assert 0, self.db # fail for demo purposes E AssertionError: .DummyDB object at 0xdeadbeef> E assert 0 - + test_unittest_db.py:12: AssertionError - ======= 2 failed in 0.12 seconds ======== + ========================= 2 failed in 0.12 seconds ========================= This default pytest traceback shows that the two test methods share the same ``self.db`` instance which was our intention @@ -166,10 +166,10 @@ Using autouse fixtures and accessing other fixtures --------------------------------------------------- Although it's usually better to explicitly declare use of fixtures you need -for a given test, you may sometimes want to have fixtures that are -automatically used in a given context. After all, the traditional +for a given test, you may sometimes want to have fixtures that are +automatically used in a given context. After all, the traditional style of unittest-setup mandates the use of this implicit fixture writing -and chances are, you are used to it or like it. +and chances are, you are used to it or like it. You can flag fixture functions with ``@pytest.fixture(autouse=True)`` and define the fixture function in the context where you want it used. @@ -203,7 +203,7 @@ on the class like in the previous example. Running this test module ...:: $ pytest -q test_unittest_cleandir.py - . + . [100%] 1 passed in 0.12 seconds ... gives us one passed test because the ``initdir`` fixture function diff --git a/third_party/python/pytest/doc/en/usage.rst b/third_party/python/pytest/doc/en/usage.rst index c5b919fe9eb4..25be54395af7 100644 --- a/third_party/python/pytest/doc/en/usage.rst +++ b/third_party/python/pytest/doc/en/usage.rst @@ -111,9 +111,9 @@ For more information see :ref:`marks `. :: pytest --pyargs pkg.testing - + This will import ``pkg.testing`` and use its filesystem location to find and run tests from. - + Modifying Python traceback printing ---------------------------------------------- @@ -152,9 +152,9 @@ allows one to drop into the PDB_ prompt via a command line option:: pytest --pdb -This will invoke the Python debugger on every failure. Often you might -only want to do this for the first failing test to understand a certain -failure situation:: +This will invoke the Python debugger on every failure (or KeyboardInterrupt). +Often you might only want to do this for the first failing test to understand +a certain failure situation:: pytest -x --pdb # drop to PDB on first failure, then end test session pytest --pdb --maxfail=3 # drop to PDB for first three failures @@ -190,6 +190,19 @@ in your code and pytest automatically disables its output capture for that test: and continue with the regular test run. +.. _`breakpoint-builtin`: + +Using the builtin breakpoint function +------------------------------------- + +Python 3.7 introduces a builtin ``breakpoint()`` function. +Pytest supports the use of ``breakpoint()`` with the following behaviours: + + - When ``breakpoint()`` is called and ``PYTHONBREAKPOINT`` is set to the default value, pytest will use the custom internal PDB trace UI instead of the system default ``Pdb``. + - When tests are complete, the system will default back to the system ``Pdb`` trace UI. + - If ``--pdb`` is called on execution of pytest, the custom internal Pdb trace UI is used on ``bothbreakpoint()`` and failed tests/unhandled exceptions. + - If ``--pdbcls`` is used, the custom class debugger will be executed when a test fails (as expected within existing behaviour), but also when ``breakpoint()`` is called from within a test, the custom class debugger will be instantiated. + .. _durations: Profiling test execution duration @@ -221,19 +234,26 @@ To set the name of the root test suite xml item, you can configure the ``junit_s [pytest] junit_suite_name = my_suite -record_xml_property -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +.. _record_property example: + +record_property +^^^^^^^^^^^^^^^ .. versionadded:: 2.8 +.. versionchanged:: 3.5 + + Fixture renamed from ``record_xml_property`` to ``record_property`` as user + properties are now available to all reporters. + ``record_xml_property`` is now deprecated. If you want to log additional information for a test, you can use the -``record_xml_property`` fixture: +``record_property`` fixture: .. code-block:: python - def test_function(record_xml_property): - record_xml_property("example_key", 1) - assert 0 + def test_function(record_property): + record_property("example_key", 1) + assert True This will add an extra property ``example_key="1"`` to the generated ``testcase`` tag: @@ -246,17 +266,108 @@ This will add an extra property ``example_key="1"`` to the generated
+Alternatively, you can integrate this functionality with custom markers: + +.. code-block:: python + + # content of conftest.py + + + def pytest_collection_modifyitems(session, config, items): + for item in items: + for marker in item.iter_markers(name="test_id"): + test_id = marker.args[0] + item.user_properties.append(("test_id", test_id)) + +And in your tests: + +.. code-block:: python + + # content of test_function.py + import pytest + + + @pytest.mark.test_id(1501) + def test_function(): + assert True + +Will result in: + +.. code-block:: xml + + + + + + + .. warning:: - ``record_xml_property`` is an experimental feature, and its interface might be replaced - by something more powerful and general in future versions. The - functionality per-se will be kept, however. - - Currently it does not work when used with the ``pytest-xdist`` plugin. + ``record_property`` is an experimental feature and may change in the future. Also please note that using this feature will break any schema verification. This might be a problem when used with some CI servers. +record_xml_attribute +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. versionadded:: 3.4 + +To add an additional xml attribute to a testcase element, you can use +``record_xml_attribute`` fixture. This can also be used to override existing values: + +.. code-block:: python + + def test_function(record_xml_attribute): + record_xml_attribute("assertions", "REQ-1234") + record_xml_attribute("classname", "custom_classname") + print("hello world") + assert True + +Unlike ``record_property``, this will not add a new child element. +Instead, this will add an attribute ``assertions="REQ-1234"`` inside the generated +``testcase`` tag and override the default ``classname`` with ``"classname=custom_classname"``: + +.. code-block:: xml + + + + hello world + + + +.. warning:: + + ``record_xml_attribute`` is an experimental feature, and its interface might be replaced + by something more powerful and general in future versions. The + functionality per-se will be kept, however. + + Using this over ``record_xml_property`` can help when using ci tools to parse the xml report. + However, some parsers are quite strict about the elements and attributes that are allowed. + Many tools use an xsd schema (like the example below) to validate incoming xml. + Make sure you are using attribute names that are allowed by your parser. + + Below is the Scheme used by Jenkins to validate the XML report: + + .. code-block:: xml + + + + + + + + + + + + + + + + + + LogXML: add_global_property ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -269,19 +380,22 @@ to all testcases you can use ``LogXML.add_global_properties`` import pytest + @pytest.fixture(scope="session") def log_global_env_facts(f): - if pytest.config.pluginmanager.hasplugin('junitxml'): - my_junit = getattr(pytest.config, '_xml', None) + if pytest.config.pluginmanager.hasplugin("junitxml"): + my_junit = getattr(pytest.config, "_xml", None) - my_junit.add_global_property('ARCH', 'PPC') - my_junit.add_global_property('STORAGE_TYPE', 'CEPH') + my_junit.add_global_property("ARCH", "PPC") + my_junit.add_global_property("STORAGE_TYPE", "CEPH") - @pytest.mark.usefixtures(log_global_env_facts) + + @pytest.mark.usefixtures(log_global_env_facts.__name__) def start_and_prepare_env(): pass + class TestMe(object): def test_foo(self): assert True @@ -312,7 +426,7 @@ Creating resultlog format files This option is rarely used and is scheduled for removal in 4.0. An alternative for users which still need similar functionality is to use the - `pytest-tap `_ plugin which provides + `pytest-tap `_ plugin which provides a stream of test data. If you have any concerns, please don't hesitate to @@ -387,7 +501,17 @@ Running it will show that ``MyPlugin`` was added and its hook was invoked:: $ python myinvoke.py - *** test run reporting finishing - + . [100%]*** test run reporting finishing + + +.. note:: + + Calling ``pytest.main()`` will result in importing your tests and any modules + that they import. Due to the caching mechanism of python's import system, + making subsequent calls to ``pytest.main()`` from the same process will not + reflect changes to those files between the calls. For this reason, making + multiple calls to ``pytest.main()`` from the same process (in order to re-run + tests, for example) is not recommended. + .. include:: links.inc diff --git a/third_party/python/pytest/doc/en/warnings.rst b/third_party/python/pytest/doc/en/warnings.rst index de8456af0232..df93a02b540a 100644 --- a/third_party/python/pytest/doc/en/warnings.rst +++ b/third_party/python/pytest/doc/en/warnings.rst @@ -21,20 +21,20 @@ and displays them at the end of the session:: Running pytest now produces this output:: $ pytest test_show_warnings.py - ======= test session starts ======== + =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - - test_show_warnings.py . - - ======= warnings summary ======== + + test_show_warnings.py . [100%] + + ============================= warnings summary ============================= test_show_warnings.py::test_one $REGENDOC_TMPDIR/test_show_warnings.py:4: UserWarning: api v1, should use functions from v2 warnings.warn(UserWarning("api v1, should use functions from v2")) - + -- Docs: http://doc.pytest.org/en/latest/warnings.html - ======= 1 passed, 1 warnings in 0.12 seconds ======== + =================== 1 passed, 1 warnings in 0.12 seconds =================== Pytest by default catches all warnings except for ``DeprecationWarning`` and ``PendingDeprecationWarning``. @@ -42,20 +42,20 @@ The ``-W`` flag can be passed to control which warnings will be displayed or eve them into errors:: $ pytest -q test_show_warnings.py -W error::UserWarning - F - ======= FAILURES ======== - _______ test_one ________ - + F [100%] + ================================= FAILURES ================================= + _________________________________ test_one _________________________________ + def test_one(): > assert api_v1() == 1 - - test_show_warnings.py:8: - _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ - + + test_show_warnings.py:8: + _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + def api_v1(): > warnings.warn(UserWarning("api v1, should use functions from v2")) E UserWarning: api v1, should use functions from v2 - + test_show_warnings.py:4: UserWarning 1 failed in 0.12 seconds @@ -78,6 +78,9 @@ Both ``-W`` command-line option and ``filterwarnings`` ini option are based on P `-W option`_ and `warnings.simplefilter`_, so please refer to those sections in the Python documentation for other examples and advanced usage. + +.. _`filterwarnings`: + ``@pytest.mark.filterwarnings`` ------------------------------- @@ -91,11 +94,13 @@ even module level: import warnings + def api_v1(): warnings.warn(UserWarning("api v1, should use functions from v2")) return 1 - @pytest.mark.filterwarnings('ignore:api v1') + + @pytest.mark.filterwarnings("ignore:api v1") def test_one(): assert api_v1() == 1 @@ -109,9 +114,15 @@ decorator or to all tests in a module by setting the ``pytestmark`` variable: .. code-block:: python # turns all warnings into errors for this module - pytestmark = pytest.mark.filterwarnings('error') + pytestmark = pytest.mark.filterwarnings("error") +.. note:: + + Except for these features, pytest does not change the python warning filter; it only captures + and displays the warnings which are issued with respect to the currently configured filter, + including changes to the filter made by test functions or by the system under test. + .. note:: ``DeprecationWarning`` and ``PendingDeprecationWarning`` are hidden by the standard library @@ -168,7 +179,20 @@ which works in a similar manner to :ref:`raises `:: with pytest.warns(UserWarning): warnings.warn("my warning", UserWarning) -The test will fail if the warning in question is not raised. +The test will fail if the warning in question is not raised. The keyword +argument ``match`` to assert that the exception matches a text or regex:: + + >>> with warns(UserWarning, match='must be 0 or None'): + ... warnings.warn("value must be 0 or None", UserWarning) + + >>> with warns(UserWarning, match=r'must be \d+$'): + ... warnings.warn("value must be 42", UserWarning) + + >>> with warns(UserWarning, match=r'must be \d+$'): + ... warnings.warn("this is not here", UserWarning) + Traceback (most recent call last): + ... + Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted... You can also call ``pytest.warns`` on a function or code string:: @@ -231,23 +255,11 @@ The ``recwarn`` fixture will record warnings for the whole function:: Both ``recwarn`` and ``pytest.warns`` return the same interface for recorded warnings: a WarningsRecorder instance. To view the recorded warnings, you can iterate over this instance, call ``len`` on it to get the number of recorded -warnings, or index into it to get a particular recorded warning. It also -provides these methods: +warnings, or index into it to get a particular recorded warning. -.. autoclass:: _pytest.recwarn.WarningsRecorder() - :members: +.. currentmodule:: _pytest.warnings -Each recorded warning has the attributes ``message``, ``category``, -``filename``, ``lineno``, ``file``, and ``line``. The ``category`` is the -class of the warning. The ``message`` is the warning itself; calling -``str(message)`` will return the actual message of the warning. - -.. note:: - :class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1 - -.. note:: - ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated - differently; see :ref:`ensuring_function_triggers`. +Full API: :class:`WarningsRecorder`. .. _`ensuring a function triggers a deprecation warning`: diff --git a/third_party/python/pytest/doc/en/writing_plugins.rst b/third_party/python/pytest/doc/en/writing_plugins.rst index 5dccdb884fea..95b810bcb36f 100644 --- a/third_party/python/pytest/doc/en/writing_plugins.rst +++ b/third_party/python/pytest/doc/en/writing_plugins.rst @@ -12,9 +12,9 @@ only want to use but not write plugins. A plugin contains one or multiple hook functions. :ref:`Writing hooks ` explains the basics and details of how you can write a hook function yourself. ``pytest`` implements all aspects of configuration, collection, running and -reporting by calling `well specified hooks`_ of the following plugins: +reporting by calling :ref:`well specified hooks ` of the following plugins: -* :ref:`builtin plugins`: loaded from pytest's internal ``_pytest`` directory. +* builtin plugins: loaded from pytest's internal ``_pytest`` directory. * :ref:`external plugins `: modules discovered through `setuptools entry points`_ @@ -85,8 +85,8 @@ sub directory but not for other directories:: Here is how you might run it:: - pytest test_flat.py # will not show "setting up" - pytest a/test_sub.py # will show "setting up" +     pytest test_flat.py --capture=no # will not show "setting up" + pytest a/test_sub.py --capture=no # will show "setting up" .. note:: If you have ``conftest.py`` files which do not reside in a @@ -103,16 +103,16 @@ Here is how you might run it:: Writing your own plugin ----------------------- -.. _`setuptools`: http://pypi.python.org/pypi/setuptools +.. _`setuptools`: https://pypi.org/project/setuptools/ If you want to write a plugin, there are many real-life examples you can copy from: * a custom collection example plugin: :ref:`yaml plugin` -* around 20 :ref:`builtin plugins` which provide pytest's own functionality +* builtin plugins which provide pytest's own functionality * many `external plugins `_ providing additional features -All of these plugins implement the documented `well specified hooks`_ +All of these plugins implement :ref:`hooks ` and/or :ref:`fixtures ` to extend and add functionality. .. note:: @@ -150,24 +150,16 @@ it in your setuptools-invocation: setup( name="myproject", - packages = ['myproject'] - + packages=["myproject"], # the following makes a plugin available to pytest - entry_points = { - 'pytest11': [ - 'name_of_plugin = myproject.pluginmodule', - ] - }, - + entry_points={"pytest11": ["name_of_plugin = myproject.pluginmodule"]}, # custom PyPI classifier for pytest plugins - classifiers=[ - "Framework :: Pytest", - ], + classifiers=["Framework :: Pytest"], ) If a package is installed this way, ``pytest`` will load ``myproject.pluginmodule`` as a plugin which can define -`well specified hooks`_. +:ref:`hooks `. .. note:: @@ -176,6 +168,8 @@ If a package is installed this way, ``pytest`` will load to make it easy for users to find your plugin. +.. _assertion-rewriting: + Assertion Rewriting ------------------- @@ -184,18 +178,19 @@ statements and the detailed introspection of expressions upon assertion failures. This is provided by "assertion rewriting" which modifies the parsed AST before it gets compiled to bytecode. This is done via a :pep:`302` import hook which gets installed early on when -``pytest`` starts up and will perform this re-writing when modules get +``pytest`` starts up and will perform this rewriting when modules get imported. However since we do not want to test different bytecode -then you will run in production this hook only re-writes test modules +then you will run in production this hook only rewrites test modules themselves as well as any modules which are part of plugins. Any -other imported module will not be re-written and normal assertion +other imported module will not be rewritten and normal assertion behaviour will happen. If you have assertion helpers in other modules where you would need assertion rewriting to be enabled you need to ask ``pytest`` -explicitly to re-write this module before it gets imported. +explicitly to rewrite this module before it gets imported. .. autofunction:: pytest.register_assert_rewrite + :noindex: This is especially important when you write a pytest plugin which is created using a package. The import hook only treats ``conftest.py`` @@ -210,16 +205,12 @@ With the following typical ``setup.py`` extract: .. code-block:: python - setup( - ... - entry_points={'pytest11': ['foo = pytest_foo.plugin']}, - ... - ) + setup(..., entry_points={"pytest11": ["foo = pytest_foo.plugin"]}, ...) -In this case only ``pytest_foo/plugin.py`` will be re-written. If the +In this case only ``pytest_foo/plugin.py`` will be rewritten. If the helper module also contains assert statements which need to be -re-written it needs to be marked as such, before it gets imported. -This is easiest by marking it for re-writing inside the +rewritten it needs to be marked as such, before it gets imported. +This is easiest by marking it for rewriting inside the ``__init__.py`` module, which will always be imported first when a module inside a package is imported. This way ``plugin.py`` can still import ``helper.py`` normally. The contents of @@ -229,7 +220,7 @@ import ``helper.py`` normally. The contents of import pytest - pytest.register_assert_rewrite('pytest_foo.helper') + pytest.register_assert_rewrite("pytest_foo.helper") @@ -254,6 +245,18 @@ application modules: if ``myapp.testsupport.myplugin`` also declares ``pytest_plugins``, the contents of the variable will also be loaded as plugins, and so on. +.. _`requiring plugins in non-root conftests`: + +.. note:: + Requiring plugins using a ``pytest_plugins`` variable in non-root + ``conftest.py`` files is deprecated. + + This is important because ``conftest.py`` files implement per-directory + hook implementations, but once a plugin is imported, it will affect the + entire directory tree. In order to avoid confusion, defining + ``pytest_plugins`` in any ``conftest.py`` file which is not located in the + tests root directory is deprecated, and will raise a warning. + This mechanism makes it easy to share fixtures within applications or even external applications without the need to create external plugins using the ``setuptools``'s entry point technique. @@ -263,7 +266,7 @@ for assertion rewriting (see :func:`pytest.register_assert_rewrite`). However for this to have any effect the module must not be imported already; if it was already imported at the time the ``pytest_plugins`` statement is processed, a warning will result and -assertions inside the plugin will not be re-written. To fix this you +assertions inside the plugin will not be rewritten. To fix this you can either call :func:`pytest.register_assert_rewrite` yourself before the module is imported, or you can arrange the code to delay the importing until after the plugin is registered. @@ -278,7 +281,7 @@ the plugin manager like this: .. sourcecode:: python - plugin = config.pluginmanager.getplugin("name_of_plugin") + plugin = config.pluginmanager.get_plugin("name_of_plugin") If you want to look at the names of existing plugins, use the ``--trace-config`` option. @@ -317,23 +320,25 @@ string value of ``Hello World!`` if we do not supply a value or ``Hello import pytest + def pytest_addoption(parser): - group = parser.getgroup('helloworld') + group = parser.getgroup("helloworld") group.addoption( - '--name', - action='store', - dest='name', - default='World', - help='Default "name" for hello().' + "--name", + action="store", + dest="name", + default="World", + help='Default "name" for hello().', ) + @pytest.fixture def hello(request): - name = request.config.getoption('name') + name = request.config.getoption("name") def _hello(name=None): if not name: - name = request.config.getoption('name') + name = request.config.getoption("name") return "Hello {name}!".format(name=name) return _hello @@ -349,7 +354,8 @@ return a result object, with which we can assert the tests' outcomes. """Make sure that our plugin works.""" # create a temporary conftest.py file - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture(params=[ @@ -359,16 +365,19 @@ return a result object, with which we can assert the tests' outcomes. ]) def name(request): return request.param - """) + """ + ) # create a temporary pytest test file - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello_default(hello): assert hello() == "Hello World!" def test_hello_name(hello, name): assert hello(name) == "Hello {0}!".format(name) - """) + """ + ) # run all tests with pytest result = testdir.runpytest() @@ -410,6 +419,7 @@ Let's look at a possible implementation: def pytest_collection_modifyitems(config, items): # called after collection is completed # you can modify the ``items`` list + ... Here, ``pytest`` will pass in ``config`` (the pytest config object) and ``items`` (the list of collected test items) but will not pass @@ -452,7 +462,7 @@ hook wrappers and passes the same arguments as to the regular hooks. At the yield point of the hook wrapper pytest will execute the next hook implementations and return their result to the yield point in the form of -a :py:class:`CallOutcome <_pytest.vendored_packages.pluggy._CallOutcome>` instance which encapsulates a result or +a :py:class:`Result ` instance which encapsulates a result or exception info. The yield point itself will thus typically not raise exceptions (unless there are bugs). @@ -462,19 +472,24 @@ Here is an example definition of a hook wrapper:: @pytest.hookimpl(hookwrapper=True) def pytest_pyfunc_call(pyfuncitem): - # do whatever you want before the next hook executes + do_something_before_next_hook_executes() outcome = yield # outcome.excinfo may be None or a (cls, val, tb) tuple res = outcome.get_result() # will raise if outcome was exception - # postprocess result + + post_process_result(res) + + outcome.force_result(new_res) # to override the return value to the plugin system Note that hook wrappers don't return results themselves, they merely perform tracing or other side effects around the actual hook implementations. If the result of the underlying hook is a mutable object, they may modify that result but it's probably better to avoid it. +For more information, consult the `pluggy documentation `_. + Hook function ordering / call example ------------------------------------- @@ -491,11 +506,15 @@ after others, i.e. the position in the ``N``-sized list of functions: @pytest.hookimpl(tryfirst=True) def pytest_collection_modifyitems(items): # will execute as early as possible + ... + # Plugin 2 @pytest.hookimpl(trylast=True) def pytest_collection_modifyitems(items): # will execute as late as possible + ... + # Plugin 3 @pytest.hookimpl(hookwrapper=True) @@ -517,7 +536,7 @@ Here is the order of execution: Plugin1). 4. Plugin3's pytest_collection_modifyitems then executing the code after the yield - point. The yield receives a :py:class:`CallOutcome <_pytest.vendored_packages.pluggy._CallOutcome>` instance which encapsulates + point. The yield receives a :py:class:`Result ` instance which encapsulates the result from calling the non-wrappers. Wrappers shall not modify the result. It's possible to use ``tryfirst`` and ``trylast`` also in conjunction with @@ -535,6 +554,7 @@ implemented by other plugins in order to alter behaviour or interact with the new plugin: .. autofunction:: pytest_addhooks + :noindex: Hooks are usually declared as do-nothing functions that contain only documentation describing when the hook will be called and what return values @@ -571,169 +591,3 @@ declaring the hook functions directly in your plugin module, for example:: This has the added benefit of allowing you to conditionally install hooks depending on which plugins are installed. - -.. _`well specified hooks`: - -.. currentmodule:: _pytest.hookspec - -pytest hook reference -===================== - - -Initialization, command line and configuration hooks ----------------------------------------------------- - -.. autofunction:: pytest_load_initial_conftests -.. autofunction:: pytest_cmdline_preparse -.. autofunction:: pytest_cmdline_parse -.. autofunction:: pytest_addoption -.. autofunction:: pytest_cmdline_main -.. autofunction:: pytest_configure -.. autofunction:: pytest_unconfigure - -Generic "runtest" hooks ------------------------ - -All runtest related hooks receive a :py:class:`pytest.Item <_pytest.main.Item>` object. - -.. autofunction:: pytest_runtest_protocol -.. autofunction:: pytest_runtest_setup -.. autofunction:: pytest_runtest_call -.. autofunction:: pytest_runtest_teardown -.. autofunction:: pytest_runtest_makereport - -For deeper understanding you may look at the default implementation of -these hooks in :py:mod:`_pytest.runner` and maybe also -in :py:mod:`_pytest.pdb` which interacts with :py:mod:`_pytest.capture` -and its input/output capturing in order to immediately drop -into interactive debugging when a test failure occurs. - -The :py:mod:`_pytest.terminal` reported specifically uses -the reporting hook to print information about a test run. - -Collection hooks ----------------- - -``pytest`` calls the following hooks for collecting files and directories: - -.. autofunction:: pytest_ignore_collect -.. autofunction:: pytest_collect_directory -.. autofunction:: pytest_collect_file - -For influencing the collection of objects in Python modules -you can use the following hook: - -.. autofunction:: pytest_pycollect_makeitem -.. autofunction:: pytest_generate_tests -.. autofunction:: pytest_make_parametrize_id - -After collection is complete, you can modify the order of -items, delete or otherwise amend the test items: - -.. autofunction:: pytest_collection_modifyitems - -Reporting hooks ---------------- - -Session related reporting hooks: - -.. autofunction:: pytest_collectstart -.. autofunction:: pytest_itemcollected -.. autofunction:: pytest_collectreport -.. autofunction:: pytest_deselected -.. autofunction:: pytest_report_header -.. autofunction:: pytest_report_collectionfinish -.. autofunction:: pytest_report_teststatus -.. autofunction:: pytest_terminal_summary -.. autofunction:: pytest_fixture_setup -.. autofunction:: pytest_fixture_post_finalizer - -And here is the central hook for reporting about -test execution: - -.. autofunction:: pytest_runtest_logreport - -You can also use this hook to customize assertion representation for some -types: - -.. autofunction:: pytest_assertrepr_compare - - -Debugging/Interaction hooks ---------------------------- - -There are few hooks which can be used for special -reporting or interaction with exceptions: - -.. autofunction:: pytest_internalerror -.. autofunction:: pytest_keyboard_interrupt -.. autofunction:: pytest_exception_interact -.. autofunction:: pytest_enter_pdb - - -Reference of objects involved in hooks -====================================== - -.. autoclass:: _pytest.config.Config() - :members: - -.. autoclass:: _pytest.config.Parser() - :members: - -.. autoclass:: _pytest.main.Node() - :members: - -.. autoclass:: _pytest.main.Collector() - :members: - :show-inheritance: - -.. autoclass:: _pytest.main.Item() - :members: - :show-inheritance: - -.. autoclass:: _pytest.python.Module() - :members: - :show-inheritance: - -.. autoclass:: _pytest.python.Class() - :members: - :show-inheritance: - -.. autoclass:: _pytest.python.Function() - :members: - :show-inheritance: - -.. autoclass:: _pytest.fixtures.FixtureDef() - :members: - :show-inheritance: - -.. autoclass:: _pytest.runner.CallInfo() - :members: - -.. autoclass:: _pytest.runner.TestReport() - :members: - :inherited-members: - -.. autoclass:: _pytest.vendored_packages.pluggy._CallOutcome() - :members: - -.. autofunction:: _pytest.config.get_plugin_manager() - -.. autoclass:: _pytest.config.PytestPluginManager() - :members: - :undoc-members: - :show-inheritance: - -.. autoclass:: _pytest.vendored_packages.pluggy.PluginManager() - :members: - -.. currentmodule:: _pytest.pytester - -.. autoclass:: Testdir() - :members: runpytest,runpytest_subprocess,runpytest_inprocess,makeconftest,makepyfile - -.. autoclass:: RunResult() - :members: - -.. autoclass:: LineMatcher() - :members: diff --git a/third_party/python/pytest/doc/en/xunit_setup.rst b/third_party/python/pytest/doc/en/xunit_setup.rst index 148fb1209ea3..7a6c099f5590 100644 --- a/third_party/python/pytest/doc/en/xunit_setup.rst +++ b/third_party/python/pytest/doc/en/xunit_setup.rst @@ -6,7 +6,7 @@ classic xunit-style setup ======================================== This section describes a classic and popular way how you can implement -fixtures (setup and teardown test state) on a per-module/class/function basis. +fixtures (setup and teardown test state) on a per-module/class/function basis. .. note:: diff --git a/third_party/python/pytest/extra/get_issues.py b/third_party/python/pytest/extra/get_issues.py index 2a8f8c316069..c026972b145d 100644 --- a/third_party/python/pytest/extra/get_issues.py +++ b/third_party/python/pytest/extra/get_issues.py @@ -1,11 +1,9 @@ import json import py -import textwrap +import requests issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues" -import requests - def get_issues(): issues = [] @@ -16,16 +14,16 @@ def get_issues(): data = r.json() if r.status_code == 403: # API request limit exceeded - print(data['message']) + print(data["message"]) exit(1) issues.extend(data) # Look for next page - links = requests.utils.parse_header_links(r.headers['Link']) + links = requests.utils.parse_header_links(r.headers["Link"]) another_page = False for link in links: - if link['rel'] == 'next': - url = link['url'] + if link["rel"] == "next": + url = link["url"] another_page = True if not another_page: return issues @@ -46,17 +44,17 @@ def main(args): def _get_kind(issue): - labels = [l['name'] for l in issue['labels']] - for key in ('bug', 'enhancement', 'proposal'): + labels = [l["name"] for l in issue["labels"]] + for key in ("bug", "enhancement", "proposal"): if key in labels: return key - return 'issue' + return "issue" def report(issues): for issue in issues: title = issue["title"] - body = issue["body"] + # body = issue["body"] kind = _get_kind(issue) status = issue["state"] number = issue["number"] @@ -64,21 +62,23 @@ def report(issues): print("----") print(status, kind, link) print(title) - #print() - #lines = body.split("\n") - #print ("\n".join(lines[:3])) - #if len(lines) > 3 or len(body) > 240: + # print() + # lines = body.split("\n") + # print ("\n".join(lines[:3])) + # if len(lines) > 3 or len(body) > 240: # print ("...") print("\n\nFound %s open issues" % len(issues)) if __name__ == "__main__": import argparse + parser = argparse.ArgumentParser("process bitbucket issues") - parser.add_argument("--refresh", action="store_true", - help="invalidate cache, refresh issues") - parser.add_argument("--cache", action="store", default="issues.json", - help="cache file") + parser.add_argument( + "--refresh", action="store_true", help="invalidate cache, refresh issues" + ) + parser.add_argument( + "--cache", action="store", default="issues.json", help="cache file" + ) args = parser.parse_args() main(args) - diff --git a/third_party/python/pytest/pyproject.toml b/third_party/python/pytest/pyproject.toml index 88571e208b9a..65e6bf59bf1c 100644 --- a/third_party/python/pytest/pyproject.toml +++ b/third_party/python/pytest/pyproject.toml @@ -1,5 +1,13 @@ +[build-system] +requires = [ + "setuptools", + "setuptools-scm", + "wheel", +] + [tool.towncrier] package = "pytest" +package_dir = "src" filename = "CHANGELOG.rst" directory = "changelog/" template = "changelog/_template.rst" diff --git a/third_party/python/pytest/scripts/check-rst.py b/third_party/python/pytest/scripts/check-rst.py deleted file mode 100644 index 57f717501af4..000000000000 --- a/third_party/python/pytest/scripts/check-rst.py +++ /dev/null @@ -1,11 +0,0 @@ - -from __future__ import print_function - -import subprocess -import glob -import sys - -sys.exit(subprocess.call([ - 'rst-lint', '--encoding', 'utf-8', - 'CHANGELOG.rst', 'HOWTORELEASE.rst', 'README.rst', -] + glob.glob('changelog/[0-9]*.*'))) diff --git a/third_party/python/pytest/setup.py b/third_party/python/pytest/setup.py index 55607912bf44..3d60d6becf84 100644 --- a/third_party/python/pytest/setup.py +++ b/third_party/python/pytest/setup.py @@ -2,99 +2,123 @@ import os import sys import setuptools import pkg_resources -from setuptools import setup, Command +from setuptools import setup classifiers = [ - 'Development Status :: 6 - Mature', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: MIT License', - 'Operating System :: POSIX', - 'Operating System :: Microsoft :: Windows', - 'Operating System :: MacOS :: MacOS X', - 'Topic :: Software Development :: Testing', - 'Topic :: Software Development :: Libraries', - 'Topic :: Utilities', + "Development Status :: 6 - Mature", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: POSIX", + "Operating System :: Microsoft :: Windows", + "Operating System :: MacOS :: MacOS X", + "Topic :: Software Development :: Testing", + "Topic :: Software Development :: Libraries", + "Topic :: Utilities", ] + [ - ('Programming Language :: Python :: %s' % x) - for x in '2 2.6 2.7 3 3.3 3.4 3.5 3.6'.split() + ("Programming Language :: Python :: %s" % x) + for x in "2 2.7 3 3.4 3.5 3.6 3.7".split() ] -with open('README.rst') as fd: +with open("README.rst") as fd: long_description = fd.read() -def has_environment_marker_support(): +def get_environment_marker_support_level(): """ - Tests that setuptools has support for PEP-426 environment marker support. + Tests how well setuptools supports PEP-426 environment marker. The first known release to support it is 0.7 (and the earliest on PyPI seems to be 0.7.2 - so we're using that), see: http://pythonhosted.org/setuptools/history.html#id142 + so we're using that), see: https://setuptools.readthedocs.io/en/latest/history.html#id350 + + The support is later enhanced to allow direct conditional inclusions inside install_requires, + which is now recommended by setuptools. It first appeared in 36.2.0, went broken with 36.2.1, and + again worked since 36.2.2, so we're using that. See: + https://setuptools.readthedocs.io/en/latest/history.html#v36-2-2 + https://github.com/pypa/setuptools/issues/1099 References: * https://wheel.readthedocs.io/en/latest/index.html#defining-conditional-dependencies * https://www.python.org/dev/peps/pep-0426/#environment-markers + * https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies """ try: - return pkg_resources.parse_version(setuptools.__version__) >= pkg_resources.parse_version('0.7.2') + version = pkg_resources.parse_version(setuptools.__version__) + if version >= pkg_resources.parse_version("36.2.2"): + return 2 + if version >= pkg_resources.parse_version("0.7.2"): + return 1 except Exception as exc: sys.stderr.write("Could not test setuptool's version: %s\n" % exc) - return False + + # as of testing on 2018-05-26 fedora was on version 37* and debian was on version 33+ + # we should consider erroring on those + return 0 def main(): - install_requires = ['py>=1.4.33', 'setuptools'] # pluggy is vendored in _pytest.vendored_packages extras_require = {} - if has_environment_marker_support(): - extras_require[':python_version=="2.6"'] = ['argparse', 'ordereddict'] - extras_require[':sys_platform=="win32"'] = ['colorama'] + install_requires = [ + "py>=1.5.0", + "six>=1.10.0", + "setuptools", + "attrs>=17.4.0", + "more-itertools>=4.0.0", + "atomicwrites>=1.0", + ] + # if _PYTEST_SETUP_SKIP_PLUGGY_DEP is set, skip installing pluggy; + # used by tox.ini to test with pluggy master + if "_PYTEST_SETUP_SKIP_PLUGGY_DEP" not in os.environ: + install_requires.append("pluggy>=0.5,<0.7") + environment_marker_support_level = get_environment_marker_support_level() + if environment_marker_support_level >= 2: + install_requires.append('funcsigs;python_version<"3.0"') + install_requires.append('colorama;sys_platform=="win32"') + elif environment_marker_support_level == 1: + extras_require[':python_version<"3.0"'] = ["funcsigs"] + extras_require[':sys_platform=="win32"'] = ["colorama"] else: - if sys.version_info < (2, 7): - install_requires.append('argparse') - install_requires.append('ordereddict') - if sys.platform == 'win32': - install_requires.append('colorama') + if sys.platform == "win32": + install_requires.append("colorama") + if sys.version_info < (3, 0): + install_requires.append("funcsigs") setup( - name='pytest', - description='pytest: simple powerful testing with Python', + name="pytest", + description="pytest: simple powerful testing with Python", long_description=long_description, - use_scm_version={ - 'write_to': '_pytest/_version.py', + use_scm_version={"write_to": "src/_pytest/_version.py"}, + url="http://pytest.org", + project_urls={ + "Source": "https://github.com/pytest-dev/pytest", + "Tracker": "https://github.com/pytest-dev/pytest/issues", }, - url='http://pytest.org', - license='MIT license', - platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], - author='Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others', - entry_points={'console_scripts': - ['pytest=pytest:main', 'py.test=pytest:main']}, + license="MIT license", + platforms=["unix", "linux", "osx", "cygwin", "win32"], + author=( + "Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, " + "Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others" + ), + entry_points={"console_scripts": ["pytest=pytest:main", "py.test=pytest:main"]}, classifiers=classifiers, keywords="test unittest", - cmdclass={'test': PyTest}, # the following should be enabled for release - setup_requires=['setuptools-scm'], + setup_requires=["setuptools-scm"], + package_dir={"": "src"}, + python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", install_requires=install_requires, extras_require=extras_require, - packages=['_pytest', '_pytest.assertion', '_pytest._code', '_pytest.vendored_packages'], - py_modules=['pytest'], + packages=[ + "_pytest", + "_pytest.assertion", + "_pytest._code", + "_pytest.mark", + "_pytest.config", + ], + py_modules=["pytest"], zip_safe=False, ) -class PyTest(Command): - user_options = [] - def initialize_options(self): - pass - def finalize_options(self): - pass - def run(self): - import subprocess - PPATH = [x for x in os.environ.get('PYTHONPATH', '').split(':') if x] - PPATH.insert(0, os.getcwd()) - os.environ['PYTHONPATH'] = ':'.join(PPATH) - errno = subprocess.call([sys.executable, 'pytest.py', '--ignore=doc']) - raise SystemExit(errno) - - -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/third_party/python/pytest/_pytest/__init__.py b/third_party/python/pytest/src/_pytest/__init__.py similarity index 77% rename from third_party/python/pytest/_pytest/__init__.py rename to third_party/python/pytest/src/_pytest/__init__.py index 6e41f0504e47..46c7827ed5ec 100644 --- a/third_party/python/pytest/_pytest/__init__.py +++ b/third_party/python/pytest/src/_pytest/__init__.py @@ -1,8 +1,8 @@ -__all__ = ['__version__'] +__all__ = ["__version__"] try: from ._version import version as __version__ except ImportError: # broken installation, we don't even try # unknown only works because we do poor mans version compare - __version__ = 'unknown' + __version__ = "unknown" diff --git a/third_party/python/pytest/_pytest/_argcomplete.py b/third_party/python/pytest/src/_pytest/_argcomplete.py similarity index 91% rename from third_party/python/pytest/_pytest/_argcomplete.py rename to third_party/python/pytest/src/_pytest/_argcomplete.py index 965ec795136a..8f480d71d79f 100644 --- a/third_party/python/pytest/_pytest/_argcomplete.py +++ b/third_party/python/pytest/src/_pytest/_argcomplete.py @@ -4,9 +4,6 @@ needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail to find the magic string, so _ARGCOMPLETE env. var is never set, and this does not need special code. -argcomplete does not support python 2.5 (although the changes for that -are minor). - Function try_argcomplete(parser) should be called directly before the call to ArgumentParser.parse_args(). @@ -63,8 +60,8 @@ import os from glob import glob -class FastFilesCompleter: - 'Fast file completer class' +class FastFilesCompleter(object): + "Fast file completer class" def __init__(self, directories=True): self.directories = directories @@ -77,21 +74,21 @@ class FastFilesCompleter: prefix_dir = 0 completion = [] globbed = [] - if '*' not in prefix and '?' not in prefix: + if "*" not in prefix and "?" not in prefix: # we are on unix, otherwise no bash if not prefix or prefix[-1] == os.path.sep: - globbed.extend(glob(prefix + '.*')) - prefix += '*' + globbed.extend(glob(prefix + ".*")) + prefix += "*" globbed.extend(glob(prefix)) for x in sorted(globbed): if os.path.isdir(x): - x += '/' + x += "/" # append stripping the prefix (like bash, not like compgen) completion.append(x[prefix_dir:]) return completion -if os.environ.get('_ARGCOMPLETE'): +if os.environ.get("_ARGCOMPLETE"): try: import argcomplete.completers except ImportError: @@ -100,7 +97,11 @@ if os.environ.get('_ARGCOMPLETE'): def try_argcomplete(parser): argcomplete.autocomplete(parser, always_complete_options=False) + + else: + def try_argcomplete(parser): pass + filescompleter = None diff --git a/third_party/python/pytest/_pytest/_code/__init__.py b/third_party/python/pytest/src/_pytest/_code/__init__.py similarity index 100% rename from third_party/python/pytest/_pytest/_code/__init__.py rename to third_party/python/pytest/src/_pytest/_code/__init__.py diff --git a/third_party/python/pytest/_pytest/_code/_py2traceback.py b/third_party/python/pytest/src/_pytest/_code/_py2traceback.py similarity index 80% rename from third_party/python/pytest/_pytest/_code/_py2traceback.py rename to third_party/python/pytest/src/_pytest/_code/_py2traceback.py index 5aacf0a428d8..2dd100c33bdd 100644 --- a/third_party/python/pytest/_pytest/_code/_py2traceback.py +++ b/third_party/python/pytest/src/_pytest/_code/_py2traceback.py @@ -4,6 +4,7 @@ # from __future__ import absolute_import, division, print_function import types +from six import text_type def format_exception_only(etype, value): @@ -29,9 +30,12 @@ def format_exception_only(etype, value): # # Clear these out first because issubtype(string1, SyntaxError) # would throw another exception and mask the original problem. - if (isinstance(etype, BaseException) or - isinstance(etype, types.InstanceType) or - etype is None or type(etype) is str): + if ( + isinstance(etype, BaseException) + or isinstance(etype, types.InstanceType) + or etype is None + or type(etype) is str + ): return [_format_final_exc_line(etype, value)] stype = etype.__name__ @@ -50,14 +54,14 @@ def format_exception_only(etype, value): lines.append(' File "%s", line %d\n' % (filename, lineno)) if badline is not None: if isinstance(badline, bytes): # python 2 only - badline = badline.decode('utf-8', 'replace') - lines.append(u' %s\n' % badline.strip()) + badline = badline.decode("utf-8", "replace") + lines.append(u" %s\n" % badline.strip()) if offset is not None: - caretspace = badline.rstrip('\n')[:offset].lstrip() + caretspace = badline.rstrip("\n")[:offset].lstrip() # non-space whitespace (likes tabs) must be kept for alignment - caretspace = ((c.isspace() and c or ' ') for c in caretspace) + caretspace = ((c.isspace() and c or " ") for c in caretspace) # only three spaces to account for offset1 == pos 0 - lines.append(' %s^\n' % ''.join(caretspace)) + lines.append(" %s^\n" % "".join(caretspace)) value = msg lines.append(_format_final_exc_line(stype, value)) @@ -76,10 +80,10 @@ def _format_final_exc_line(etype, value): def _some_str(value): try: - return unicode(value) + return text_type(value) except Exception: try: return str(value) except Exception: pass - return '' % type(value).__name__ + return "" % type(value).__name__ diff --git a/third_party/python/pytest/_pytest/_code/code.py b/third_party/python/pytest/src/_pytest/_code/code.py similarity index 85% rename from third_party/python/pytest/_pytest/_code/code.py rename to third_party/python/pytest/src/_pytest/_code/code.py index f3b7eedfce63..cb788c17f04b 100644 --- a/third_party/python/pytest/_pytest/_code/code.py +++ b/third_party/python/pytest/src/_pytest/_code/code.py @@ -1,14 +1,17 @@ from __future__ import absolute_import, division, print_function +import inspect import sys +import traceback from inspect import CO_VARARGS, CO_VARKEYWORDS + +import attr import re from weakref import ref from _pytest.compat import _PY2, _PY3, PY35, safe_str - +from six import text_type import py -builtin_repr = repr -reprlib = py.builtin._tryimport('repr', 'reprlib') +builtin_repr = repr if _PY3: from traceback import format_exception_only @@ -59,6 +62,7 @@ class Code(object): """ return a _pytest._code.Source object for the full source file of the code """ from _pytest._code import source + full, _ = source.findsource(self.raw) return full @@ -67,6 +71,7 @@ class Code(object): """ # return source only for that part of code import _pytest._code + return _pytest._code.Source(self.raw) def getargs(self, var=False): @@ -99,6 +104,7 @@ class Frame(object): def statement(self): """ statement this frame is at """ import _pytest._code + if self.code.fullsource is None: return _pytest._code.Source("") return self.code.fullsource.getstatement(self.lineno) @@ -142,7 +148,7 @@ class Frame(object): try: retval.append((arg, self.f_locals[arg])) except KeyError: - pass # this can occur when using Psyco + pass # this can occur when using Psyco return retval @@ -164,6 +170,7 @@ class TracebackEntry(object): @property def frame(self): import _pytest._code + return _pytest._code.Frame(self._rawentry.tb_frame) @property @@ -186,6 +193,7 @@ class TracebackEntry(object): def getlocals(self): return self.frame.f_locals + locals = property(getlocals, None, None, "locals of underlaying frame") def getfirstlinesource(self): @@ -197,6 +205,7 @@ class TracebackEntry(object): # we use the passed in astcache to not reparse asttrees # within exception info printing from _pytest._code.source import getstatementrange_ast + source = self.frame.code.fullsource if source is None: return None @@ -207,8 +216,9 @@ class TracebackEntry(object): astnode = astcache.get(key, None) start = self.getfirstlinesource() try: - astnode, _, end = getstatementrange_ast(self.lineno, source, - astnode=astnode) + astnode, _, end = getstatementrange_ast( + self.lineno, source, astnode=astnode + ) except SyntaxError: end = self.lineno + 1 else: @@ -228,14 +238,14 @@ class TracebackEntry(object): mostly for internal use """ try: - tbh = self.frame.f_locals['__tracebackhide__'] + tbh = self.frame.f_locals["__tracebackhide__"] except KeyError: try: - tbh = self.frame.f_globals['__tracebackhide__'] + tbh = self.frame.f_globals["__tracebackhide__"] except KeyError: return False - if py.builtin.callable(tbh): + if callable(tbh): return tbh(None if self._excinfo is None else self._excinfo()) else: return tbh @@ -244,7 +254,7 @@ class TracebackEntry(object): try: fn = str(self.path) except py.error.Error: - fn = '???' + fn = "???" name = self.frame.code.name try: line = str(self.statement).lstrip() @@ -256,6 +266,7 @@ class TracebackEntry(object): def name(self): return self.frame.code.raw.co_name + name = property(name, None, None, "co_name of underlaying code") @@ -268,11 +279,13 @@ class Traceback(list): def __init__(self, tb, excinfo=None): """ initialize from given python traceback object and ExceptionInfo """ self._excinfo = excinfo - if hasattr(tb, 'tb_next'): + if hasattr(tb, "tb_next"): + def f(cur): while cur is not None: yield self.Entry(cur, excinfo=excinfo) cur = cur.tb_next + list.__init__(self, f(tb)) else: list.__init__(self, tb) @@ -290,11 +303,16 @@ class Traceback(list): for x in self: code = x.frame.code codepath = code.path - if ((path is None or codepath == path) and - (excludepath is None or not hasattr(codepath, 'relto') or - not codepath.relto(excludepath)) and - (lineno is None or x.lineno == lineno) and - (firstlineno is None or x.frame.code.firstlineno == firstlineno)): + if ( + (path is None or codepath == path) + and ( + excludepath is None + or not hasattr(codepath, "relto") + or not codepath.relto(excludepath) + ) + and (lineno is None or x.lineno == lineno) + and (firstlineno is None or x.frame.code.firstlineno == firstlineno) + ): return Traceback(x._rawentry, self._excinfo) return self @@ -343,35 +361,41 @@ class Traceback(list): f = entry.frame loc = f.f_locals for otherloc in values: - if f.is_true(f.eval(co_equal, - __recursioncache_locals_1=loc, - __recursioncache_locals_2=otherloc)): + if f.is_true( + f.eval( + co_equal, + __recursioncache_locals_1=loc, + __recursioncache_locals_2=otherloc, + ) + ): return i values.append(entry.frame.f_locals) return None -co_equal = compile('__recursioncache_locals_1 == __recursioncache_locals_2', - '?', 'eval') +co_equal = compile( + "__recursioncache_locals_1 == __recursioncache_locals_2", "?", "eval" +) class ExceptionInfo(object): """ wraps sys.exc_info() objects and offers help for navigating the traceback. """ - _striptext = '' - _assert_start_repr = "AssertionError(u\'assert " if _PY2 else "AssertionError(\'assert " + _striptext = "" + _assert_start_repr = "AssertionError(u'assert " if _PY2 else "AssertionError('assert " def __init__(self, tup=None, exprinfo=None): import _pytest._code + if tup is None: tup = sys.exc_info() if exprinfo is None and isinstance(tup[1], AssertionError): - exprinfo = getattr(tup[1], 'msg', None) + exprinfo = getattr(tup[1], "msg", None) if exprinfo is None: exprinfo = py.io.saferepr(tup[1]) if exprinfo and exprinfo.startswith(self._assert_start_repr): - self._striptext = 'AssertionError: ' + self._striptext = "AssertionError: " self._excinfo = tup #: the exception class self.type = tup[0] @@ -396,7 +420,7 @@ class ExceptionInfo(object): removed from the beginning) """ lines = format_exception_only(self.type, self.value) - text = ''.join(lines) + text = "".join(lines) text = text.rstrip() if tryshort: if text.startswith(self._striptext): @@ -413,8 +437,14 @@ class ExceptionInfo(object): path, lineno = entry.frame.code.raw.co_filename, entry.lineno return ReprFileLocation(path, lineno + 1, exconly) - def getrepr(self, showlocals=False, style="long", - abspath=False, tbfilter=True, funcargs=False): + def getrepr( + self, + showlocals=False, + style="long", + abspath=False, + tbfilter=True, + funcargs=False, + ): """ return str()able representation of this exception info. showlocals: show locals per traceback entry style: long|short|no|native traceback style @@ -422,16 +452,23 @@ class ExceptionInfo(object): in case of style==native, tbfilter and showlocals is ignored. """ - if style == 'native': - return ReprExceptionInfo(ReprTracebackNative( - py.std.traceback.format_exception( - self.type, - self.value, - self.traceback[0]._rawentry, - )), self._getreprcrash()) + if style == "native": + return ReprExceptionInfo( + ReprTracebackNative( + traceback.format_exception( + self.type, self.value, self.traceback[0]._rawentry + ) + ), + self._getreprcrash(), + ) - fmt = FormattedExcinfo(showlocals=showlocals, style=style, - abspath=abspath, tbfilter=tbfilter, funcargs=funcargs) + fmt = FormattedExcinfo( + showlocals=showlocals, + style=style, + abspath=abspath, + tbfilter=tbfilter, + funcargs=funcargs, + ) return fmt.repr_excinfo(self) def __str__(self): @@ -442,7 +479,7 @@ class ExceptionInfo(object): def __unicode__(self): entry = self.traceback[-1] loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly()) - return unicode(loc) + return text_type(loc) def match(self, regexp): """ @@ -453,24 +490,23 @@ class ExceptionInfo(object): """ __tracebackhide__ = True if not re.search(regexp, str(self.value)): - assert 0, "Pattern '{0!s}' not found in '{1!s}'".format( - regexp, self.value) + assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value) return True +@attr.s class FormattedExcinfo(object): """ presenting information about failing Functions and Generators. """ # for traceback entries flow_marker = ">" fail_marker = "E" - def __init__(self, showlocals=False, style="long", abspath=True, tbfilter=True, funcargs=False): - self.showlocals = showlocals - self.style = style - self.tbfilter = tbfilter - self.funcargs = funcargs - self.abspath = abspath - self.astcache = {} + showlocals = attr.ib(default=False) + style = attr.ib(default="long") + abspath = attr.ib(default=True) + tbfilter = attr.ib(default=True) + funcargs = attr.ib(default=False) + astcache = attr.ib(default=attr.Factory(dict), init=False, repr=False) def _getindent(self, source): # figure out indent for given source @@ -506,6 +542,7 @@ class FormattedExcinfo(object): def get_source(self, source, line_index=-1, excinfo=None, short=False): """ return formatted and marked up source lines. """ import _pytest._code + lines = [] if source is None or line_index >= len(source.lines): source = _pytest._code.Source("???") @@ -530,7 +567,7 @@ class FormattedExcinfo(object): lines = [] indent = " " * indent # get the real exception information out - exlines = excinfo.exconly(tryshort=True).split('\n') + exlines = excinfo.exconly(tryshort=True).split("\n") failindent = self.fail_marker + indent[1:] for line in exlines: lines.append(failindent + line) @@ -545,7 +582,7 @@ class FormattedExcinfo(object): keys.sort() for name in keys: value = locals[name] - if name == '__builtins__': + if name == "__builtins__": lines.append("__builtins__ = ") else: # This formatting could all be handled by the @@ -558,11 +595,12 @@ class FormattedExcinfo(object): # else: # self._line("%-10s =\\" % (name,)) # # XXX - # py.std.pprint.pprint(value, stream=self.excinfowriter) + # pprint.pprint(value, stream=self.excinfowriter) return ReprLocals(lines) def repr_traceback_entry(self, entry, excinfo=None): import _pytest._code + source = self._getentrysource(entry) if source is None: source = _pytest._code.Source("???") @@ -639,11 +677,16 @@ class FormattedExcinfo(object): except Exception as e: max_frames = 10 extraline = ( - '!!! Recursion error detected, but an error occurred locating the origin of recursion.\n' - ' The following exception happened when comparing locals in the stack frame:\n' - ' {exc_type}: {exc_msg}\n' - ' Displaying first and last {max_frames} stack frames out of {total}.' - ).format(exc_type=type(e).__name__, exc_msg=safe_str(e), max_frames=max_frames, total=len(traceback)) + "!!! Recursion error detected, but an error occurred locating the origin of recursion.\n" + " The following exception happened when comparing locals in the stack frame:\n" + " {exc_type}: {exc_msg}\n" + " Displaying first and last {max_frames} stack frames out of {total}." + ).format( + exc_type=type(e).__name__, + exc_msg=safe_str(e), + max_frames=max_frames, + total=len(traceback), + ) traceback = traceback[:max_frames] + traceback[-max_frames:] else: if recursionindex is not None: @@ -671,18 +714,24 @@ class FormattedExcinfo(object): else: # fallback to native repr if the exception doesn't have a traceback: # ExceptionInfo objects require a full traceback to work - reprtraceback = ReprTracebackNative(py.std.traceback.format_exception(type(e), e, None)) + reprtraceback = ReprTracebackNative( + traceback.format_exception(type(e), e, None) + ) reprcrash = None repr_chain += [(reprtraceback, reprcrash, descr)] if e.__cause__ is not None: e = e.__cause__ - excinfo = ExceptionInfo((type(e), e, e.__traceback__)) if e.__traceback__ else None - descr = 'The above exception was the direct cause of the following exception:' + excinfo = ExceptionInfo( + (type(e), e, e.__traceback__) + ) if e.__traceback__ else None + descr = "The above exception was the direct cause of the following exception:" elif (e.__context__ is not None and not e.__suppress_context__): e = e.__context__ - excinfo = ExceptionInfo((type(e), e, e.__traceback__)) if e.__traceback__ else None - descr = 'During handling of the above exception, another exception occurred:' + excinfo = ExceptionInfo( + (type(e), e, e.__traceback__) + ) if e.__traceback__ else None + descr = "During handling of the above exception, another exception occurred:" else: e = None repr_chain.reverse() @@ -690,10 +739,11 @@ class FormattedExcinfo(object): class TerminalRepr(object): + def __str__(self): s = self.__unicode__() if _PY2: - s = s.encode('utf-8') + s = s.encode("utf-8") return s def __unicode__(self): @@ -709,6 +759,7 @@ class TerminalRepr(object): class ExceptionRepr(TerminalRepr): + def __init__(self): self.sections = [] @@ -722,6 +773,7 @@ class ExceptionRepr(TerminalRepr): class ExceptionChainRepr(ExceptionRepr): + def __init__(self, chain): super(ExceptionChainRepr, self).__init__() self.chain = chain @@ -740,6 +792,7 @@ class ExceptionChainRepr(ExceptionRepr): class ReprExceptionInfo(ExceptionRepr): + def __init__(self, reprtraceback, reprcrash): super(ReprExceptionInfo, self).__init__() self.reprtraceback = reprtraceback @@ -766,8 +819,11 @@ class ReprTraceback(TerminalRepr): entry.toterminal(tw) if i < len(self.reprentries) - 1: next_entry = self.reprentries[i + 1] - if entry.style == "long" or \ - entry.style == "short" and next_entry.style == "long": + if ( + entry.style == "long" + or entry.style == "short" + and next_entry.style == "long" + ): tw.sep(self.entrysep) if self.extraline: @@ -775,6 +831,7 @@ class ReprTraceback(TerminalRepr): class ReprTracebackNative(ReprTraceback): + def __init__(self, tblines): self.style = "native" self.reprentries = [ReprEntryNative(tblines)] @@ -824,12 +881,11 @@ class ReprEntry(TerminalRepr): self.reprfileloc.toterminal(tw) def __str__(self): - return "%s\n%s\n%s" % ("\n".join(self.lines), - self.reprlocals, - self.reprfileloc) + return "%s\n%s\n%s" % ("\n".join(self.lines), self.reprlocals, self.reprfileloc) class ReprFileLocation(TerminalRepr): + def __init__(self, path, lineno, message): self.path = str(path) self.lineno = lineno @@ -847,6 +903,7 @@ class ReprFileLocation(TerminalRepr): class ReprLocals(TerminalRepr): + def __init__(self, lines): self.lines = lines @@ -856,6 +913,7 @@ class ReprLocals(TerminalRepr): class ReprFuncArgs(TerminalRepr): + def __init__(self, args): self.args = args @@ -883,22 +941,26 @@ def getrawcode(obj, trycall=True): try: return obj.__code__ except AttributeError: - obj = getattr(obj, 'im_func', obj) - obj = getattr(obj, 'func_code', obj) - obj = getattr(obj, 'f_code', obj) - obj = getattr(obj, '__code__', obj) - if trycall and not hasattr(obj, 'co_firstlineno'): - if hasattr(obj, '__call__') and not py.std.inspect.isclass(obj): + obj = getattr(obj, "im_func", obj) + obj = getattr(obj, "func_code", obj) + obj = getattr(obj, "f_code", obj) + obj = getattr(obj, "__code__", obj) + if trycall and not hasattr(obj, "co_firstlineno"): + if hasattr(obj, "__call__") and not inspect.isclass(obj): x = getrawcode(obj.__call__, trycall=False) - if hasattr(x, 'co_firstlineno'): + if hasattr(x, "co_firstlineno"): return x return obj if PY35: # RecursionError introduced in 3.5 + def is_recursion_error(excinfo): return excinfo.errisinstance(RecursionError) # noqa + + else: + def is_recursion_error(excinfo): if not excinfo.errisinstance(RuntimeError): return False diff --git a/third_party/python/pytest/_pytest/_code/source.py b/third_party/python/pytest/src/_pytest/_code/source.py similarity index 72% rename from third_party/python/pytest/_pytest/_code/source.py rename to third_party/python/pytest/src/_pytest/_code/source.py index fc41712649d4..6b982a4cac47 100644 --- a/third_party/python/pytest/_pytest/_code/source.py +++ b/third_party/python/pytest/src/_pytest/_code/source.py @@ -1,39 +1,37 @@ from __future__ import absolute_import, division, generators, print_function +import ast +from ast import PyCF_ONLY_AST as _AST_FLAG from bisect import bisect_right +import linecache import sys +import six import inspect import tokenize import py -cpy_compile = compile -try: - import _ast - from _ast import PyCF_ONLY_AST as _AST_FLAG -except ImportError: - _AST_FLAG = 0 - _ast = None +cpy_compile = compile class Source(object): - """ a immutable object holding a source code fragment, + """ an immutable object holding a source code fragment, possibly deindenting it. """ _compilecounter = 0 def __init__(self, *parts, **kwargs): self.lines = lines = [] - de = kwargs.get('deindent', True) - rstrip = kwargs.get('rstrip', True) + de = kwargs.get("deindent", True) + rstrip = kwargs.get("rstrip", True) for part in parts: if not part: partlines = [] - if isinstance(part, Source): + elif isinstance(part, Source): partlines = part.lines elif isinstance(part, (tuple, list)): partlines = [x.rstrip("\n") for x in part] - elif isinstance(part, py.builtin._basestring): - partlines = part.split('\n') + elif isinstance(part, six.string_types): + partlines = part.split("\n") if rstrip: while partlines: if partlines[-1].strip(): @@ -81,7 +79,7 @@ class Source(object): source.lines[:] = self.lines[start:end] return source - def putaround(self, before='', after='', indent=' ' * 4): + def putaround(self, before="", after="", indent=" " * 4): """ return a copy of the source object with 'before' and 'after' wrapped around it. """ @@ -92,7 +90,7 @@ class Source(object): newsource.lines = before.lines + lines + after.lines return newsource - def indent(self, indent=' ' * 4): + def indent(self, indent=" " * 4): """ return a copy of the source object with all lines indented by the given indent-string. """ @@ -100,14 +98,14 @@ class Source(object): newsource.lines = [(indent + line) for line in self.lines] return newsource - def getstatement(self, lineno, assertion=False): + def getstatement(self, lineno): """ return Source statement which contains the given linenumber (counted from 0). """ - start, end = self.getstatementrange(lineno, assertion) + start, end = self.getstatementrange(lineno) return self[start:end] - def getstatementrange(self, lineno, assertion=False): + def getstatementrange(self, lineno): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. """ @@ -133,13 +131,7 @@ class Source(object): """ return True if source is parseable, heuristically deindenting it by default. """ - try: - import parser - except ImportError: - def syntax_checker(x): - return compile(x, 'asd', 'exec') - else: - syntax_checker = parser.suite + from parser import suite as syntax_checker if deindent: source = str(self.deindent()) @@ -147,7 +139,7 @@ class Source(object): source = str(self) try: # compile(source+'\n', "x", "exec") - syntax_checker(source + '\n') + syntax_checker(source + "\n") except KeyboardInterrupt: raise except Exception: @@ -158,9 +150,14 @@ class Source(object): def __str__(self): return "\n".join(self.lines) - def compile(self, filename=None, mode='exec', - flag=generators.compiler_flag, - dont_inherit=0, _genframe=None): + def compile( + self, + filename=None, + mode="exec", + flag=generators.compiler_flag, + dont_inherit=0, + _genframe=None, + ): """ return compiled code object. if filename is None invent an artificial filename which displays the source/line position of the caller frame. @@ -172,10 +169,10 @@ class Source(object): base = "<%d-codegen " % self._compilecounter self.__class__._compilecounter += 1 if not filename: - filename = base + '%s:%d>' % (fn, lineno) + filename = base + "%s:%d>" % (fn, lineno) else: - filename = base + '%r %s:%d>' % (filename, fn, lineno) - source = "\n".join(self.lines) + '\n' + filename = base + "%r %s:%d>" % (filename, fn, lineno) + source = "\n".join(self.lines) + "\n" try: co = cpy_compile(source, filename, mode, flag) except SyntaxError: @@ -183,9 +180,9 @@ class Source(object): # re-represent syntax errors from parsing python strings msglines = self.lines[:ex.lineno] if ex.offset: - msglines.append(" " * ex.offset + '^') + msglines.append(" " * ex.offset + "^") msglines.append("(code was compiled probably from here: %s)" % filename) - newex = SyntaxError('\n'.join(msglines)) + newex = SyntaxError("\n".join(msglines)) newex.offset = ex.offset newex.lineno = ex.lineno newex.text = ex.text @@ -194,21 +191,24 @@ class Source(object): if flag & _AST_FLAG: return co lines = [(x + "\n") for x in self.lines] - py.std.linecache.cache[filename] = (1, None, lines, filename) + linecache.cache[filename] = (1, None, lines, filename) return co + # # public API shortcut functions # -def compile_(source, filename=None, mode='exec', flags=generators.compiler_flag, dont_inherit=0): +def compile_( + source, filename=None, mode="exec", flags=generators.compiler_flag, dont_inherit=0 +): """ compile the given source to a raw code object, and maintain an internal cache which allows later retrieval of the source code for the code object and any recursively created code objects. """ - if _ast is not None and isinstance(source, _ast.AST): + if isinstance(source, ast.AST): # XXX should Source support having AST? return cpy_compile(source, filename, mode, flags, dont_inherit) _genframe = sys._getframe(1) # the caller @@ -221,13 +221,13 @@ def getfslineno(obj): """ Return source location (path, lineno) for the given object. If the source cannot be determined return ("", -1) """ - import _pytest._code + from .code import Code + try: - code = _pytest._code.Code(obj) + code = Code(obj) except TypeError: try: - fn = (py.std.inspect.getsourcefile(obj) or - py.std.inspect.getfile(obj)) + fn = inspect.getsourcefile(obj) or inspect.getfile(obj) except TypeError: return "", -1 @@ -244,6 +244,7 @@ def getfslineno(obj): assert isinstance(lineno, int) return fspath, lineno + # # helper functions # @@ -251,7 +252,7 @@ def getfslineno(obj): def findsource(obj): try: - sourcelines, lineno = py.std.inspect.findsource(obj) + sourcelines, lineno = inspect.findsource(obj) except py.builtin._sysex: raise except: # noqa @@ -262,12 +263,13 @@ def findsource(obj): def getsource(obj, **kwargs): - import _pytest._code - obj = _pytest._code.getrawcode(obj) + from .code import getrawcode + + obj = getrawcode(obj) try: strsrc = inspect.getsource(obj) except IndentationError: - strsrc = "\"Buggy python version consider upgrading, cannot get source\"" + strsrc = '"Buggy python version consider upgrading, cannot get source"' assert isinstance(strsrc, str) return Source(strsrc, **kwargs) @@ -288,14 +290,14 @@ def deindent(lines, offset=None): def readline_generator(lines): for line in lines: - yield line + '\n' - while True: - yield '' + yield line + "\n" it = readline_generator(lines) try: - for _, _, (sline, _), (eline, _), _ in tokenize.generate_tokens(lambda: next(it)): + for _, _, (sline, _), (eline, _), _ in tokenize.generate_tokens( + lambda: next(it) + ): if sline > len(lines): break # End of input reached if sline > len(newlines): @@ -317,13 +319,14 @@ def deindent(lines, offset=None): def get_statement_startend2(lineno, node): import ast + # flatten all statements and except handlers into one lineno-list # AST's line numbers start indexing at 1 values = [] for x in ast.walk(node): - if isinstance(x, _ast.stmt) or isinstance(x, _ast.ExceptHandler): + if isinstance(x, (ast.stmt, ast.ExceptHandler)): values.append(x.lineno - 1) - for name in "finalbody", "orelse": + for name in ("finalbody", "orelse"): val = getattr(x, name, None) if val: # treat the finally/orelse part as its own statement @@ -341,13 +344,8 @@ def get_statement_startend2(lineno, node): def getstatementrange_ast(lineno, source, assertion=False, astnode=None): if astnode is None: content = str(source) - if sys.version_info < (2, 7): - content += "\n" - try: - astnode = compile(content, "source", "exec", 1024) # 1024 for AST - except ValueError: - start, end = getstatementrange_old(lineno, source, assertion) - return None, start, end + astnode = compile(content, "source", "exec", 1024) # 1024 for AST + start, end = get_statement_startend2(lineno, astnode) # we need to correct the end: # - ast-parsing strips comments @@ -379,38 +377,3 @@ def getstatementrange_ast(lineno, source, assertion=False, astnode=None): else: break return astnode, start, end - - -def getstatementrange_old(lineno, source, assertion=False): - """ return (start, end) tuple which spans the minimal - statement region which containing the given lineno. - raise an IndexError if no such statementrange can be found. - """ - # XXX this logic is only used on python2.4 and below - # 1. find the start of the statement - from codeop import compile_command - for start in range(lineno, -1, -1): - if assertion: - line = source.lines[start] - # the following lines are not fully tested, change with care - if 'super' in line and 'self' in line and '__init__' in line: - raise IndexError("likely a subclass") - if "assert" not in line and "raise" not in line: - continue - trylines = source.lines[start:lineno + 1] - # quick hack to prepare parsing an indented line with - # compile_command() (which errors on "return" outside defs) - trylines.insert(0, 'def xxx():') - trysource = '\n '.join(trylines) - # ^ space here - try: - compile_command(trysource) - except (SyntaxError, OverflowError, ValueError): - continue - - # 2. find the end of the statement - for end in range(lineno + 1, len(source) + 1): - trysource = source[start:end] - if trysource.isparseable(): - return start, end - raise SyntaxError("no valid source range around line %d " % (lineno,)) diff --git a/third_party/python/pytest/src/_pytest/_version.py b/third_party/python/pytest/src/_pytest/_version.py new file mode 100644 index 000000000000..f54dbb046281 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/_version.py @@ -0,0 +1,4 @@ +# coding: utf-8 +# file generated by setuptools_scm +# don't change, don't track in version control +version = '3.6.2' diff --git a/third_party/python/pytest/_pytest/assertion/__init__.py b/third_party/python/pytest/src/_pytest/assertion/__init__.py similarity index 79% rename from third_party/python/pytest/_pytest/assertion/__init__.py rename to third_party/python/pytest/src/_pytest/assertion/__init__.py index b0ef667d5652..2c9a8890c9af 100644 --- a/third_party/python/pytest/_pytest/assertion/__init__.py +++ b/third_party/python/pytest/src/_pytest/assertion/__init__.py @@ -2,8 +2,8 @@ support for presenting detailed information in failing assertions. """ from __future__ import absolute_import, division, print_function -import py import sys +import six from _pytest.assertion import util from _pytest.assertion import rewrite @@ -12,17 +12,19 @@ from _pytest.assertion import truncate def pytest_addoption(parser): group = parser.getgroup("debugconfig") - group.addoption('--assert', - action="store", - dest="assertmode", - choices=("rewrite", "plain",), - default="rewrite", - metavar="MODE", - help="""Control assertion debugging tools. 'plain' + group.addoption( + "--assert", + action="store", + dest="assertmode", + choices=("rewrite", "plain"), + default="rewrite", + metavar="MODE", + help="""Control assertion debugging tools. 'plain' performs no assertion debugging. 'rewrite' (the default) rewrites assert statements in test modules on import to provide assert - expression information.""") + expression information.""", + ) def register_assert_rewrite(*names): @@ -38,7 +40,7 @@ def register_assert_rewrite(*names): """ for name in names: if not isinstance(name, str): - msg = 'expected module names as *args, got {0} instead' + msg = "expected module names as *args, got {0} instead" raise TypeError(msg.format(repr(names))) for hook in sys.meta_path: if isinstance(hook, rewrite.AssertionRewritingHook): @@ -56,7 +58,7 @@ class DummyRewriteHook(object): pass -class AssertionState: +class AssertionState(object): """State for the assertion plugin.""" def __init__(self, config, mode): @@ -67,16 +69,14 @@ class AssertionState: def install_importhook(config): """Try to install the rewrite hook, raise SystemError if it fails.""" - # Both Jython and CPython 2.6.0 have AST bugs that make the - # assertion rewriting hook malfunction. - if (sys.platform.startswith('java') or - sys.version_info[:3] == (2, 6, 0)): - raise SystemError('rewrite not supported') + # Jython has an AST bug that make the assertion rewriting hook malfunction. + if sys.platform.startswith("java"): + raise SystemError("rewrite not supported") - config._assertstate = AssertionState(config, 'rewrite') + config._assertstate = AssertionState(config, "rewrite") config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config) sys.meta_path.insert(0, hook) - config._assertstate.trace('installed rewrite import hook') + config._assertstate.trace("installed rewrite import hook") def undo(): hook = config._assertstate.hook @@ -91,7 +91,7 @@ def pytest_collection(session): # this hook is only called when test modules are collected # so for example not in the master process of pytest-xdist # (which does not collect test modules) - assertstate = getattr(session.config, '_assertstate', None) + assertstate = getattr(session.config, "_assertstate", None) if assertstate: if assertstate.hook is not None: assertstate.hook.set_session(session) @@ -105,6 +105,7 @@ def pytest_runtest_setup(item): pytest_assertrepr_compare hook. This sets up this custom comparison for the test. """ + def callbinrepr(op, left, right): """Call the pytest_assertrepr_compare hook and prepare the result @@ -121,15 +122,17 @@ def pytest_runtest_setup(item): pretty printing. """ hook_result = item.ihook.pytest_assertrepr_compare( - config=item.config, op=op, left=left, right=right) + config=item.config, op=op, left=left, right=right + ) for new_expl in hook_result: if new_expl: new_expl = truncate.truncate_if_required(new_expl, item) new_expl = [line.replace("\n", "\\n") for line in new_expl] - res = py.builtin._totext("\n~").join(new_expl) + res = six.text_type("\n~").join(new_expl) if item.config.getvalue("assertmode") == "rewrite": res = res.replace("%", "%%") return res + util._reprcompare = callbinrepr @@ -138,7 +141,7 @@ def pytest_runtest_teardown(item): def pytest_sessionfinish(session): - assertstate = getattr(session.config, '_assertstate', None) + assertstate = getattr(session.config, "_assertstate", None) if assertstate: if assertstate.hook is not None: assertstate.hook.set_session(None) diff --git a/third_party/python/pytest/_pytest/assertion/rewrite.py b/third_party/python/pytest/src/_pytest/assertion/rewrite.py similarity index 86% rename from third_party/python/pytest/_pytest/assertion/rewrite.py rename to third_party/python/pytest/src/_pytest/assertion/rewrite.py index d48b6648fb7c..eceed611ffb1 100644 --- a/third_party/python/pytest/_pytest/assertion/rewrite.py +++ b/third_party/python/pytest/src/_pytest/assertion/rewrite.py @@ -1,18 +1,20 @@ """Rewrite assertion AST to produce nice error messages""" from __future__ import absolute_import, division, print_function import ast -import _ast import errno import itertools import imp import marshal import os import re +import six import struct import sys import types +import atomicwrites import py + from _pytest.assertion import util @@ -33,12 +35,12 @@ else: PYC_EXT = ".py" + (__debug__ and "c" or "o") PYC_TAIL = "." + PYTEST_TAG + PYC_EXT -REWRITE_NEWLINES = sys.version_info[:2] != (2, 7) and sys.version_info < (3, 2) ASCII_IS_DEFAULT_ENCODING = sys.version_info[0] < 3 if sys.version_info >= (3, 5): ast_Call = ast.Call else: + def ast_Call(a, b, c): return ast.Call(a, b, c, None, None) @@ -141,7 +143,7 @@ class AssertionRewritingHook(object): # Probably a SyntaxError in the test. return None if write: - _make_rewritten_pyc(state, source_stat, pyc, co) + _write_pyc(state, co, source_stat, pyc) else: state.trace("found cached rewritten pyc for %r" % (fn,)) self.modules[name] = co, pyc @@ -150,14 +152,13 @@ class AssertionRewritingHook(object): def _should_rewrite(self, name, fn_pypath, state): # always rewrite conftest files fn = str(fn_pypath) - if fn_pypath.basename == 'conftest.py': + if fn_pypath.basename == "conftest.py": state.trace("rewriting conftest file: %r" % (fn,)) return True if self.session is not None: if self.session.isinitpath(fn): - state.trace("matched test file (was specified on cmdline): %r" % - (fn,)) + state.trace("matched test file (was specified on cmdline): %r" % (fn,)) return True # modules not passed explicitly on the command line are only @@ -168,29 +169,32 @@ class AssertionRewritingHook(object): return True for marked in self._must_rewrite: - if name.startswith(marked): + if name == marked or name.startswith(marked + "."): state.trace("matched marked file %r (from %r)" % (name, marked)) return True return False def mark_rewrite(self, *names): - """Mark import names as needing to be re-written. + """Mark import names as needing to be rewritten. The named module or package as well as any nested modules will - be re-written on import. + be rewritten on import. """ - already_imported = set(names).intersection(set(sys.modules)) - if already_imported: - for name in already_imported: - if name not in self._rewritten_names: - self._warn_already_imported(name) + already_imported = ( + set(names).intersection(sys.modules).difference(self._rewritten_names) + ) + for name in already_imported: + if not AssertionRewriter.is_rewrite_disabled( + sys.modules[name].__doc__ or "" + ): + self._warn_already_imported(name) self._must_rewrite.update(names) def _warn_already_imported(self, name): self.config.warn( - 'P1', - 'Module already imported so can not be re-written: %s' % name) + "P1", "Module already imported so cannot be rewritten: %s" % name + ) def load_module(self, name): # If there is an existing module object named 'fullname' in @@ -234,6 +238,7 @@ class AssertionRewritingHook(object): """ try: import pkg_resources + # access an attribute in case a deferred importer is present pkg_resources.__name__ except ImportError: @@ -246,7 +251,7 @@ class AssertionRewritingHook(object): def get_data(self, pathname): """Optional PEP302 get_data API. """ - with open(pathname, 'rb') as f: + with open(pathname, "rb") as f: return f.read() @@ -257,22 +262,18 @@ def _write_pyc(state, co, source_stat, pyc): # sometime to be able to use imp.load_compiled to load them. (See # the comment in load_module above.) try: - fp = open(pyc, "wb") - except IOError: - err = sys.exc_info()[1].errno - state.trace("error writing pyc file at %s: errno=%s" % (pyc, err)) + with atomicwrites.atomic_write(pyc, mode="wb", overwrite=True) as fp: + fp.write(imp.get_magic()) + mtime = int(source_stat.mtime) + size = source_stat.size & 0xFFFFFFFF + fp.write(struct.pack(" 0 or - item.module != "__future__"): + elif ( + not isinstance(item, ast.ImportFrom) + or item.level > 0 + or item.module != "__future__" + ): lineno = item.lineno break pos += 1 else: lineno = item.lineno - imports = [ast.Import([alias], lineno=lineno, col_offset=0) - for alias in aliases] + imports = [ + ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases + ] mod.body[pos:pos] = imports # Collect asserts. nodes = [mod] @@ -634,13 +629,17 @@ class AssertionRewriter(ast.NodeVisitor): if isinstance(child, ast.AST): nodes.append(child) setattr(node, name, new) - elif (isinstance(field, ast.AST) and - # Don't recurse into expressions as they can't contain - # asserts. - not isinstance(field, ast.expr)): + elif ( + isinstance(field, ast.AST) + and + # Don't recurse into expressions as they can't contain + # asserts. + not isinstance(field, ast.expr) + ): nodes.append(field) - def is_rewrite_disabled(self, docstring): + @staticmethod + def is_rewrite_disabled(docstring): return "PYTEST_DONT_REWRITE" in docstring def variable(self): @@ -726,7 +725,7 @@ class AssertionRewriter(ast.NodeVisitor): def visit_Assert(self, assert_): """Return the AST statements to replace the ast.Assert instance. - This re-writes the test of an assertion to provide + This rewrites the test of an assertion to provide intermediate values and replace it with an if statement which raises an assertion error with a detailed explanation in case the expression is false. @@ -734,8 +733,11 @@ class AssertionRewriter(ast.NodeVisitor): """ if isinstance(assert_.test, ast.Tuple) and self.config is not None: fslocation = (self.module_path, assert_.lineno) - self.config.warn('R1', 'assertion is always true, perhaps ' - 'remove parentheses?', fslocation=fslocation) + self.config.warn( + "R1", + "assertion is always true, perhaps " "remove parentheses?", + fslocation=fslocation, + ) self.statements = [] self.variables = [] self.variable_counter = itertools.count() @@ -749,7 +751,7 @@ class AssertionRewriter(ast.NodeVisitor): negation = ast.UnaryOp(ast.Not(), top_condition) self.statements.append(ast.If(negation, body, [])) if assert_.msg: - assertmsg = self.helper('format_assertmsg', assert_.msg) + assertmsg = self.helper("format_assertmsg", assert_.msg) explanation = "\n>assert " + explanation else: assertmsg = ast.Str("") @@ -766,8 +768,7 @@ class AssertionRewriter(ast.NodeVisitor): body.append(raise_) # Clear temporary variables by setting them to None. if self.variables: - variables = [ast.Name(name, ast.Store()) - for name in self.variables] + variables = [ast.Name(name, ast.Store()) for name in self.variables] clear = ast.Assign(variables, _NameConstant(None)) self.statements.append(clear) # Fix line numbers. @@ -854,7 +855,7 @@ class AssertionRewriter(ast.NodeVisitor): else: # **args have `arg` keywords with an .arg of None arg_expls.append("**" + expl) - expl = "%s(%s)" % (func_expl, ', '.join(arg_expls)) + expl = "%s(%s)" % (func_expl, ", ".join(arg_expls)) new_call = ast.Call(new_func, new_args, new_kwargs) res = self.assign(new_call) res_expl = self.explanation_param(self.display(res)) @@ -864,7 +865,7 @@ class AssertionRewriter(ast.NodeVisitor): def visit_Starred(self, starred): # From Python 3.5, a Starred node can appear in a function call res, expl = self.visit(starred.value) - return starred, '*' + expl + return starred, "*" + expl def visit_Call_legacy(self, call): """ @@ -889,9 +890,8 @@ class AssertionRewriter(ast.NodeVisitor): if call.kwargs: new_kwarg, expl = self.visit(call.kwargs) arg_expls.append("**" + expl) - expl = "%s(%s)" % (func_expl, ', '.join(arg_expls)) - new_call = ast.Call(new_func, new_args, new_kwargs, - new_star, new_kwarg) + expl = "%s(%s)" % (func_expl, ", ".join(arg_expls)) + new_call = ast.Call(new_func, new_args, new_kwargs, new_star, new_kwarg) res = self.assign(new_call) res_expl = self.explanation_param(self.display(res)) outer_expl = "%s\n{%s = %s\n}" % (res_expl, res_expl, expl) @@ -918,8 +918,8 @@ class AssertionRewriter(ast.NodeVisitor): def visit_Compare(self, comp): self.push_format_context() left_res, left_expl = self.visit(comp.left) - if isinstance(comp.left, (_ast.Compare, _ast.BoolOp)): - left_expl = "({0})".format(left_expl) + if isinstance(comp.left, (ast.Compare, ast.BoolOp)): + left_expl = "({})".format(left_expl) res_variables = [self.variable() for i in range(len(comp.ops))] load_names = [ast.Name(v, ast.Load()) for v in res_variables] store_names = [ast.Name(v, ast.Store()) for v in res_variables] @@ -929,8 +929,8 @@ class AssertionRewriter(ast.NodeVisitor): results = [left_res] for i, op, next_operand in it: next_res, next_expl = self.visit(next_operand) - if isinstance(next_operand, (_ast.Compare, _ast.BoolOp)): - next_expl = "({0})".format(next_expl) + if isinstance(next_operand, (ast.Compare, ast.BoolOp)): + next_expl = "({})".format(next_expl) results.append(next_res) sym = binop_map[op.__class__] syms.append(ast.Str(sym)) @@ -940,11 +940,13 @@ class AssertionRewriter(ast.NodeVisitor): self.statements.append(ast.Assign([store_names[i]], res_expr)) left_res, left_expl = next_res, next_expl # Use pytest.assertion.util._reprcompare if that's available. - expl_call = self.helper("call_reprcompare", - ast.Tuple(syms, ast.Load()), - ast.Tuple(load_names, ast.Load()), - ast.Tuple(expls, ast.Load()), - ast.Tuple(results, ast.Load())) + expl_call = self.helper( + "call_reprcompare", + ast.Tuple(syms, ast.Load()), + ast.Tuple(load_names, ast.Load()), + ast.Tuple(expls, ast.Load()), + ast.Tuple(results, ast.Load()), + ) if len(comp.ops) > 1: res = ast.BoolOp(ast.And(), load_names) else: diff --git a/third_party/python/pytest/_pytest/assertion/truncate.py b/third_party/python/pytest/src/_pytest/assertion/truncate.py similarity index 89% rename from third_party/python/pytest/_pytest/assertion/truncate.py rename to third_party/python/pytest/src/_pytest/assertion/truncate.py index 1e1306356938..79adeca6a9ab 100644 --- a/third_party/python/pytest/_pytest/assertion/truncate.py +++ b/third_party/python/pytest/src/_pytest/assertion/truncate.py @@ -7,7 +7,7 @@ Current default behaviour is to truncate assertion explanations at from __future__ import absolute_import, division, print_function import os -import py +import six DEFAULT_MAX_LINES = 8 @@ -34,7 +34,7 @@ def _should_truncate_item(item): def _running_on_ci(): """Check if we're currently running on a CI system.""" - env_vars = ['CI', 'BUILD_NUMBER'] + env_vars = ["CI", "BUILD_NUMBER"] return any(var in os.environ for var in env_vars) @@ -67,16 +67,13 @@ def _truncate_explanation(input_lines, max_lines=None, max_chars=None): # Append useful message to explanation truncated_line_count = len(input_lines) - len(truncated_explanation) truncated_line_count += 1 # Account for the part-truncated final line - msg = '...Full output truncated' + msg = "...Full output truncated" if truncated_line_count == 1: - msg += ' ({0} line hidden)'.format(truncated_line_count) + msg += " ({} line hidden)".format(truncated_line_count) else: - msg += ' ({0} lines hidden)'.format(truncated_line_count) - msg += ", {0}" .format(USAGE_MSG) - truncated_explanation.extend([ - py.builtin._totext(""), - py.builtin._totext(msg), - ]) + msg += " ({} lines hidden)".format(truncated_line_count) + msg += ", {}".format(USAGE_MSG) + truncated_explanation.extend([six.text_type(""), six.text_type(msg)]) return truncated_explanation diff --git a/third_party/python/pytest/_pytest/assertion/util.py b/third_party/python/pytest/src/_pytest/assertion/util.py similarity index 62% rename from third_party/python/pytest/_pytest/assertion/util.py rename to third_party/python/pytest/src/_pytest/assertion/util.py index 9f0092907362..bcb800a4a07d 100644 --- a/third_party/python/pytest/_pytest/assertion/util.py +++ b/third_party/python/pytest/src/_pytest/assertion/util.py @@ -4,13 +4,10 @@ import pprint import _pytest._code import py -try: - from collections import Sequence -except ImportError: - Sequence = list +import six +from ..compat import Sequence - -u = py.builtin._totext +u = six.text_type # The _reprcompare attribute on the util module is used by the new assertion # interpretation code and assertion rewriter to detect this plugin was @@ -23,7 +20,7 @@ _reprcompare = None # with non-ascii characters (see issue 877 and 1379) def ecu(s): try: - return u(s, 'utf-8', 'replace') + return u(s, "utf-8", "replace") except TypeError: return s @@ -41,7 +38,7 @@ def format_explanation(explanation): explanation = ecu(explanation) lines = _split_explanation(explanation) result = _format_lines(lines) - return u('\n').join(result) + return u("\n").join(result) def _split_explanation(explanation): @@ -51,13 +48,13 @@ def _split_explanation(explanation): Any other newlines will be escaped and appear in the line as the literal '\n' characters. """ - raw_lines = (explanation or u('')).split('\n') + raw_lines = (explanation or u("")).split("\n") lines = [raw_lines[0]] for values in raw_lines[1:]: - if values and values[0] in ['{', '}', '~', '>']: + if values and values[0] in ["{", "}", "~", ">"]: lines.append(values) else: - lines[-1] += '\\n' + values + lines[-1] += "\\n" + values return lines @@ -74,24 +71,24 @@ def _format_lines(lines): stack = [0] stackcnt = [0] for line in lines[1:]: - if line.startswith('{'): + if line.startswith("{"): if stackcnt[-1]: - s = u('and ') + s = u("and ") else: - s = u('where ') + s = u("where ") stack.append(len(result)) stackcnt[-1] += 1 stackcnt.append(0) - result.append(u(' +') + u(' ') * (len(stack) - 1) + s + line[1:]) - elif line.startswith('}'): + result.append(u(" +") + u(" ") * (len(stack) - 1) + s + line[1:]) + elif line.startswith("}"): stack.pop() stackcnt.pop() result[stack[-1]] += line[1:] else: - assert line[0] in ['~', '>'] + assert line[0] in ["~", ">"] stack[-1] += 1 - indent = len(stack) if line.startswith('~') else len(stack) - 1 - result.append(u(' ') * indent + line[1:]) + indent = len(stack) if line.startswith("~") else len(stack) - 1 + result.append(u(" ") * indent + line[1:]) assert len(stack) == 1 return result @@ -109,10 +106,10 @@ def assertrepr_compare(config, op, left, right): left_repr = py.io.saferepr(left, maxsize=int(width // 2)) right_repr = py.io.saferepr(right, maxsize=width - len(left_repr)) - summary = u('%s %s %s') % (ecu(left_repr), op, ecu(right_repr)) + summary = u("%s %s %s") % (ecu(left_repr), op, ecu(right_repr)) def issequence(x): - return (isinstance(x, (list, tuple, Sequence)) and not isinstance(x, basestring)) + return isinstance(x, Sequence) and not isinstance(x, basestring) def istext(x): return isinstance(x, basestring) @@ -130,10 +127,10 @@ def assertrepr_compare(config, op, left, right): except TypeError: return False - verbose = config.getoption('verbose') + verbose = config.getoption("verbose") explanation = None try: - if op == '==': + if op == "==": if istext(left) and istext(right): explanation = _diff_text(left, right, verbose) else: @@ -149,14 +146,17 @@ def assertrepr_compare(config, op, left, right): explanation.extend(expl) else: explanation = expl - elif op == 'not in': + elif op == "not in": if istext(left) and istext(right): explanation = _notin_text(left, right, verbose) except Exception: explanation = [ - u('(pytest_assertion plugin: representation of details failed. ' - 'Probably an object has a faulty __repr__.)'), - u(_pytest._code.ExceptionInfo())] + u( + "(pytest_assertion plugin: representation of details failed. " + "Probably an object has a faulty __repr__.)" + ), + u(_pytest._code.ExceptionInfo()), + ] if not explanation: return None @@ -173,20 +173,35 @@ def _diff_text(left, right, verbose=False): If the input are bytes they will be safely converted to text. """ from difflib import ndiff + explanation = [] - if isinstance(left, py.builtin.bytes): - left = u(repr(left)[1:-1]).replace(r'\n', '\n') - if isinstance(right, py.builtin.bytes): - right = u(repr(right)[1:-1]).replace(r'\n', '\n') + + def escape_for_readable_diff(binary_text): + """ + Ensures that the internal string is always valid unicode, converting any bytes safely to valid unicode. + This is done using repr() which then needs post-processing to fix the encompassing quotes and un-escape + newlines and carriage returns (#429). + """ + r = six.text_type(repr(binary_text)[1:-1]) + r = r.replace(r"\n", "\n") + r = r.replace(r"\r", "\r") + return r + + if isinstance(left, six.binary_type): + left = escape_for_readable_diff(left) + if isinstance(right, six.binary_type): + right = escape_for_readable_diff(right) if not verbose: i = 0 # just in case left or right has zero length for i in range(min(len(left), len(right))): if left[i] != right[i]: break if i > 42: - i -= 10 # Provide some context - explanation = [u('Skipping %s identical leading ' - 'characters in diff, use -v to show') % i] + i -= 10 # Provide some context + explanation = [ + u("Skipping %s identical leading " "characters in diff, use -v to show") + % i + ] left = left[i:] right = right[i:] if len(left) == len(right): @@ -194,36 +209,48 @@ def _diff_text(left, right, verbose=False): if left[-i] != right[-i]: break if i > 42: - i -= 10 # Provide some context - explanation += [u('Skipping %s identical trailing ' - 'characters in diff, use -v to show') % i] + i -= 10 # Provide some context + explanation += [ + u( + "Skipping %s identical trailing " + "characters in diff, use -v to show" + ) + % i + ] left = left[:-i] right = right[:-i] keepends = True - explanation += [line.strip('\n') - for line in ndiff(left.splitlines(keepends), - right.splitlines(keepends))] + if left.isspace() or right.isspace(): + left = repr(str(left)) + right = repr(str(right)) + explanation += [u"Strings contain only whitespace, escaping them using repr()"] + explanation += [ + line.strip("\n") + for line in ndiff(left.splitlines(keepends), right.splitlines(keepends)) + ] return explanation def _compare_eq_iterable(left, right, verbose=False): if not verbose: - return [u('Use -v to get the full diff')] + return [u("Use -v to get the full diff")] # dynamic import to speedup pytest import difflib try: left_formatting = pprint.pformat(left).splitlines() right_formatting = pprint.pformat(right).splitlines() - explanation = [u('Full diff:')] + explanation = [u("Full diff:")] except Exception: # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling # sorted() on a list would raise. See issue #718. # As a workaround, the full diff is generated by using the repr() string of each item of each container. left_formatting = sorted(repr(x) for x in left) right_formatting = sorted(repr(x) for x in right) - explanation = [u('Full diff (fallback to calling repr on each item):')] - explanation.extend(line.strip() for line in difflib.ndiff(left_formatting, right_formatting)) + explanation = [u("Full diff (fallback to calling repr on each item):")] + explanation.extend( + line.strip() for line in difflib.ndiff(left_formatting, right_formatting) + ) return explanation @@ -231,16 +258,18 @@ def _compare_eq_sequence(left, right, verbose=False): explanation = [] for i in range(min(len(left), len(right))): if left[i] != right[i]: - explanation += [u('At index %s diff: %r != %r') - % (i, left[i], right[i])] + explanation += [u("At index %s diff: %r != %r") % (i, left[i], right[i])] break if len(left) > len(right): - explanation += [u('Left contains more items, first extra item: %s') - % py.io.saferepr(left[len(right)],)] + explanation += [ + u("Left contains more items, first extra item: %s") + % py.io.saferepr(left[len(right)]) + ] elif len(left) < len(right): explanation += [ - u('Right contains more items, first extra item: %s') % - py.io.saferepr(right[len(left)],)] + u("Right contains more items, first extra item: %s") + % py.io.saferepr(right[len(left)]) + ] return explanation @@ -249,11 +278,11 @@ def _compare_eq_set(left, right, verbose=False): diff_left = left - right diff_right = right - left if diff_left: - explanation.append(u('Extra items in the left set:')) + explanation.append(u("Extra items in the left set:")) for item in diff_left: explanation.append(py.io.saferepr(item)) if diff_right: - explanation.append(u('Extra items in the right set:')) + explanation.append(u("Extra items in the right set:")) for item in diff_right: explanation.append(py.io.saferepr(item)) return explanation @@ -262,29 +291,31 @@ def _compare_eq_set(left, right, verbose=False): def _compare_eq_dict(left, right, verbose=False): explanation = [] common = set(left).intersection(set(right)) - same = dict((k, left[k]) for k in common if left[k] == right[k]) + same = {k: left[k] for k in common if left[k] == right[k]} if same and verbose < 2: - explanation += [u('Omitting %s identical items, use -vv to show') % - len(same)] + explanation += [u("Omitting %s identical items, use -vv to show") % len(same)] elif same: - explanation += [u('Common items:')] + explanation += [u("Common items:")] explanation += pprint.pformat(same).splitlines() - diff = set(k for k in common if left[k] != right[k]) + diff = {k for k in common if left[k] != right[k]} if diff: - explanation += [u('Differing items:')] + explanation += [u("Differing items:")] for k in diff: - explanation += [py.io.saferepr({k: left[k]}) + ' != ' + - py.io.saferepr({k: right[k]})] + explanation += [ + py.io.saferepr({k: left[k]}) + " != " + py.io.saferepr({k: right[k]}) + ] extra_left = set(left) - set(right) if extra_left: - explanation.append(u('Left contains more items:')) - explanation.extend(pprint.pformat( - dict((k, left[k]) for k in extra_left)).splitlines()) + explanation.append(u("Left contains more items:")) + explanation.extend( + pprint.pformat({k: left[k] for k in extra_left}).splitlines() + ) extra_right = set(right) - set(left) if extra_right: - explanation.append(u('Right contains more items:')) - explanation.extend(pprint.pformat( - dict((k, right[k]) for k in extra_right)).splitlines()) + explanation.append(u("Right contains more items:")) + explanation.extend( + pprint.pformat({k: right[k] for k in extra_right}).splitlines() + ) return explanation @@ -294,14 +325,14 @@ def _notin_text(term, text, verbose=False): tail = text[index + len(term):] correct_text = head + tail diff = _diff_text(correct_text, text, verbose) - newdiff = [u('%s is contained here:') % py.io.saferepr(term, maxsize=42)] + newdiff = [u("%s is contained here:") % py.io.saferepr(term, maxsize=42)] for line in diff: - if line.startswith(u('Skipping')): + if line.startswith(u("Skipping")): continue - if line.startswith(u('- ')): + if line.startswith(u("- ")): continue - if line.startswith(u('+ ')): - newdiff.append(u(' ') + line[2:]) + if line.startswith(u("+ ")): + newdiff.append(u(" ") + line[2:]) else: newdiff.append(line) return newdiff diff --git a/third_party/python/pytest/_pytest/cacheprovider.py b/third_party/python/pytest/src/_pytest/cacheprovider.py similarity index 58% rename from third_party/python/pytest/_pytest/cacheprovider.py rename to third_party/python/pytest/src/_pytest/cacheprovider.py index c537c14472b1..eb0fcc06f9bd 100755 --- a/third_party/python/pytest/_pytest/cacheprovider.py +++ b/third_party/python/pytest/src/_pytest/cacheprovider.py @@ -5,7 +5,12 @@ the name cache was not chosen to ensure pluggy automatically ignores the external pytest-cache """ from __future__ import absolute_import, division, print_function + +from collections import OrderedDict + import py +import six + import pytest import json import os @@ -13,11 +18,12 @@ from os.path import sep as _sep, altsep as _altsep class Cache(object): + def __init__(self, config): self.config = config self._cachedir = Cache.cache_dir_from_config(config) self.trace = config.trace.root.get("cache") - if config.getvalue("cacheclear"): + if config.getoption("cacheclear"): self.trace("clearing cachedir") if self._cachedir.check(): self._cachedir.remove() @@ -48,7 +54,7 @@ class Cache(object): return self._cachedir.ensure_dir("d", name) def _getvaluepath(self, key): - return self._cachedir.join('v', *key.split('/')) + return self._cachedir.join("v", *key.split("/")) def get(self, key, default): """ return cached value for the given key. If no value @@ -84,82 +90,88 @@ class Cache(object): path.dirpath().ensure_dir() except (py.error.EEXIST, py.error.EACCES): self.config.warn( - code='I9', message='could not create cache path %s' % (path,) + code="I9", message="could not create cache path %s" % (path,) ) return try: - f = path.open('w') + f = path.open("w") except py.error.ENOTDIR: self.config.warn( - code='I9', message='cache could not write path %s' % (path,)) + code="I9", message="cache could not write path %s" % (path,) + ) else: with f: - self.trace("cache-write %s: %r" % (key, value,)) + self.trace("cache-write %s: %r" % (key, value)) json.dump(value, f, indent=2, sort_keys=True) -class LFPlugin: +class LFPlugin(object): """ Plugin which implements the --lf (run last-failing) option """ def __init__(self, config): self.config = config - active_keys = 'lf', 'failedfirst' - self.active = any(config.getvalue(key) for key in active_keys) + active_keys = "lf", "failedfirst" + self.active = any(config.getoption(key) for key in active_keys) self.lastfailed = config.cache.get("cache/lastfailed", {}) self._previously_failed_count = None + self._no_failures_behavior = self.config.getoption("last_failed_no_failures") def pytest_report_collectionfinish(self): if self.active: if not self._previously_failed_count: - mode = "run all (no recorded failures)" + mode = "run {} (no recorded failures)".format( + self._no_failures_behavior + ) else: - noun = 'failure' if self._previously_failed_count == 1 else 'failures' - suffix = " first" if self.config.getvalue("failedfirst") else "" + noun = "failure" if self._previously_failed_count == 1 else "failures" + suffix = " first" if self.config.getoption("failedfirst") else "" mode = "rerun previous {count} {noun}{suffix}".format( count=self._previously_failed_count, suffix=suffix, noun=noun ) return "run-last-failure: %s" % mode def pytest_runtest_logreport(self, report): - if (report.when == 'call' and report.passed) or report.skipped: + if (report.when == "call" and report.passed) or report.skipped: self.lastfailed.pop(report.nodeid, None) elif report.failed: self.lastfailed[report.nodeid] = True def pytest_collectreport(self, report): - passed = report.outcome in ('passed', 'skipped') + passed = report.outcome in ("passed", "skipped") if passed: if report.nodeid in self.lastfailed: self.lastfailed.pop(report.nodeid) - self.lastfailed.update( - (item.nodeid, True) - for item in report.result) + self.lastfailed.update((item.nodeid, True) for item in report.result) else: self.lastfailed[report.nodeid] = True def pytest_collection_modifyitems(self, session, config, items): - if self.active and self.lastfailed: - previously_failed = [] - previously_passed = [] - for item in items: - if item.nodeid in self.lastfailed: - previously_failed.append(item) + if self.active: + if self.lastfailed: + previously_failed = [] + previously_passed = [] + for item in items: + if item.nodeid in self.lastfailed: + previously_failed.append(item) + else: + previously_passed.append(item) + self._previously_failed_count = len(previously_failed) + if not previously_failed: + # running a subset of all tests with recorded failures outside + # of the set of tests currently executing + return + if self.config.getoption("lf"): + items[:] = previously_failed + config.hook.pytest_deselected(items=previously_passed) else: - previously_passed.append(item) - self._previously_failed_count = len(previously_failed) - if not previously_failed: - # running a subset of all tests with recorded failures outside - # of the set of tests currently executing - return - if self.config.getvalue("lf"): - items[:] = previously_failed - config.hook.pytest_deselected(items=previously_passed) - else: - items[:] = previously_failed + previously_passed + items[:] = previously_failed + previously_passed + elif self._no_failures_behavior == "none": + config.hook.pytest_deselected(items=items) + items[:] = [] def pytest_sessionfinish(self, session): config = self.config - if config.getvalue("cacheshow") or hasattr(config, "slaveinput"): + if config.getoption("cacheshow") or hasattr(config, "slaveinput"): return saved_lastfailed = config.cache.get("cache/lastfailed", {}) @@ -167,31 +179,98 @@ class LFPlugin: config.cache.set("cache/lastfailed", self.lastfailed) +class NFPlugin(object): + """ Plugin which implements the --nf (run new-first) option """ + + def __init__(self, config): + self.config = config + self.active = config.option.newfirst + self.cached_nodeids = config.cache.get("cache/nodeids", []) + + def pytest_collection_modifyitems(self, session, config, items): + if self.active: + new_items = OrderedDict() + other_items = OrderedDict() + for item in items: + if item.nodeid not in self.cached_nodeids: + new_items[item.nodeid] = item + else: + other_items[item.nodeid] = item + + items[:] = self._get_increasing_order( + six.itervalues(new_items) + ) + self._get_increasing_order( + six.itervalues(other_items) + ) + self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)] + + def _get_increasing_order(self, items): + return sorted(items, key=lambda item: item.fspath.mtime(), reverse=True) + + def pytest_sessionfinish(self, session): + config = self.config + if config.getoption("cacheshow") or hasattr(config, "slaveinput"): + return + + config.cache.set("cache/nodeids", self.cached_nodeids) + + def pytest_addoption(parser): group = parser.getgroup("general") group.addoption( - '--lf', '--last-failed', action='store_true', dest="lf", + "--lf", + "--last-failed", + action="store_true", + dest="lf", help="rerun only the tests that failed " - "at the last run (or all if none failed)") + "at the last run (or all if none failed)", + ) group.addoption( - '--ff', '--failed-first', action='store_true', dest="failedfirst", + "--ff", + "--failed-first", + action="store_true", + dest="failedfirst", help="run all tests but run the last failures first. " - "This may re-order tests and thus lead to " - "repeated fixture setup/teardown") + "This may re-order tests and thus lead to " + "repeated fixture setup/teardown", + ) group.addoption( - '--cache-show', action='store_true', dest="cacheshow", - help="show cache contents, don't perform collection or tests") + "--nf", + "--new-first", + action="store_true", + dest="newfirst", + help="run tests from new files first, then the rest of the tests " + "sorted by file mtime", + ) group.addoption( - '--cache-clear', action='store_true', dest="cacheclear", - help="remove all cache contents at start of test run.") - parser.addini( - "cache_dir", default='.cache', - help="cache directory path.") + "--cache-show", + action="store_true", + dest="cacheshow", + help="show cache contents, don't perform collection or tests", + ) + group.addoption( + "--cache-clear", + action="store_true", + dest="cacheclear", + help="remove all cache contents at start of test run.", + ) + parser.addini("cache_dir", default=".pytest_cache", help="cache directory path.") + group.addoption( + "--lfnf", + "--last-failed-no-failures", + action="store", + dest="last_failed_no_failures", + choices=("all", "none"), + default="all", + help="change the behavior when no test failed in the last run or no " + "information about the last failures was found in the cache", + ) def pytest_cmdline_main(config): if config.option.cacheshow: from _pytest.main import wrap_session + return wrap_session(config, cacheshow) @@ -199,6 +278,7 @@ def pytest_cmdline_main(config): def pytest_configure(config): config.cache = Cache(config) config.pluginmanager.register(LFPlugin(config), "lfplugin") + config.pluginmanager.register(NFPlugin(config), "nfplugin") @pytest.fixture @@ -225,6 +305,7 @@ def pytest_report_header(config): def cacheshow(config, session): from pprint import pprint + tw = py.io.TerminalWriter() tw.line("cachedir: " + str(config.cache._cachedir)) if not config.cache._cachedir.check(): @@ -238,8 +319,7 @@ def cacheshow(config, session): key = valpath.relto(vdir).replace(valpath.sep, "/") val = config.cache.get(key, dummy) if val is dummy: - tw.line("%s contains unreadable content, " - "will be ignored" % key) + tw.line("%s contains unreadable content, " "will be ignored" % key) else: tw.line("%s contains:" % key) stream = py.io.TextIO() @@ -255,6 +335,5 @@ def cacheshow(config, session): # print("%s/" % p.relto(basedir)) if p.isfile(): key = p.relto(basedir) - tw.line("%s is a file of length %d" % ( - key, p.size())) + tw.line("%s is a file of length %d" % (key, p.size())) return 0 diff --git a/third_party/python/pytest/_pytest/capture.py b/third_party/python/pytest/src/_pytest/capture.py similarity index 56% rename from third_party/python/pytest/_pytest/capture.py rename to third_party/python/pytest/src/_pytest/capture.py index cb5af6fcb3ce..7a57adb752a7 100644 --- a/third_party/python/pytest/_pytest/capture.py +++ b/third_party/python/pytest/src/_pytest/capture.py @@ -4,6 +4,7 @@ per-test stdout/stderr capturing mechanism. """ from __future__ import absolute_import, division, print_function +import collections import contextlib import sys import os @@ -11,25 +12,31 @@ import io from io import UnsupportedOperation from tempfile import TemporaryFile -import py +import six import pytest from _pytest.compat import CaptureIO -unicode = py.builtin.text -patchsysdict = {0: 'stdin', 1: 'stdout', 2: 'stderr'} +patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"} def pytest_addoption(parser): group = parser.getgroup("general") group._addoption( - '--capture', action="store", + "--capture", + action="store", default="fd" if hasattr(os, "dup") else "sys", - metavar="method", choices=['fd', 'sys', 'no'], - help="per-test capturing method: one of fd|sys|no.") + metavar="method", + choices=["fd", "sys", "no"], + help="per-test capturing method: one of fd|sys|no.", + ) group._addoption( - '-s', action="store_const", const="no", dest="capture", - help="shortcut for --capture=no.") + "-s", + action="store_const", + const="no", + dest="capture", + help="shortcut for --capture=no.", + ) @pytest.hookimpl(hookwrapper=True) @@ -44,26 +51,40 @@ def pytest_load_initial_conftests(early_config, parser, args): pluginmanager.register(capman, "capturemanager") # make sure that capturemanager is properly reset at final shutdown - early_config.add_cleanup(capman.reset_capturings) + early_config.add_cleanup(capman.stop_global_capturing) # make sure logging does not raise exceptions at the end def silence_logging_at_shutdown(): if "logging" in sys.modules: sys.modules["logging"].raiseExceptions = False + early_config.add_cleanup(silence_logging_at_shutdown) # finally trigger conftest loading but while capturing (issue93) - capman.init_capturings() + capman.start_global_capturing() outcome = yield - out, err = capman.suspendcapture() + out, err = capman.suspend_global_capture() if outcome.excinfo is not None: sys.stdout.write(out) sys.stderr.write(err) -class CaptureManager: +class CaptureManager(object): + """ + Capture plugin, manages that the appropriate capture method is enabled/disabled during collection and each + test phase (setup, call, teardown). After each of those points, the captured output is obtained and + attached to the collection/runtest report. + + There are two levels of capture: + * global: which is enabled by default and can be suppressed by the ``-s`` option. This is always enabled/disabled + during collection and each test phase. + * fixture: when a test function or one of its fixture depend on the ``capsys`` or ``capfd`` fixtures. In this + case special handling is needed to ensure the fixtures take precedence over the global capture. + """ + def __init__(self, method): self._method = method + self._global_capturing = None def _getcapture(self, method): if method == "fd": @@ -75,23 +96,24 @@ class CaptureManager: else: raise ValueError("unknown capturing method: %r" % method) - def init_capturings(self): - assert not hasattr(self, "_capturing") - self._capturing = self._getcapture(self._method) - self._capturing.start_capturing() + def start_global_capturing(self): + assert self._global_capturing is None + self._global_capturing = self._getcapture(self._method) + self._global_capturing.start_capturing() - def reset_capturings(self): - cap = self.__dict__.pop("_capturing", None) - if cap is not None: - cap.pop_outerr_to_orig() - cap.stop_capturing() + def stop_global_capturing(self): + if self._global_capturing is not None: + self._global_capturing.pop_outerr_to_orig() + self._global_capturing.stop_capturing() + self._global_capturing = None - def resumecapture(self): - self._capturing.resume_capturing() + def resume_global_capture(self): + self._global_capturing.resume_capturing() - def suspendcapture(self, in_=False): - self.deactivate_funcargs() - cap = getattr(self, "_capturing", None) + def suspend_global_capture(self, item=None, in_=False): + if item is not None: + self.deactivate_fixture(item) + cap = getattr(self, "_global_capturing", None) if cap is not None: try: outerr = cap.readouterr() @@ -99,23 +121,26 @@ class CaptureManager: cap.suspend_capturing(in_=in_) return outerr - def activate_funcargs(self, pyfuncitem): - capfuncarg = pyfuncitem.__dict__.pop("_capfuncarg", None) - if capfuncarg is not None: - capfuncarg._start() - self._capfuncarg = capfuncarg + def activate_fixture(self, item): + """If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over + the global capture. + """ + fixture = getattr(item, "_capture_fixture", None) + if fixture is not None: + fixture._start() - def deactivate_funcargs(self): - capfuncarg = self.__dict__.pop("_capfuncarg", None) - if capfuncarg is not None: - capfuncarg.close() + def deactivate_fixture(self, item): + """Deactivates the ``capsys`` or ``capfd`` fixture of this item, if any.""" + fixture = getattr(item, "_capture_fixture", None) + if fixture is not None: + fixture.close() @pytest.hookimpl(hookwrapper=True) def pytest_make_collect_report(self, collector): if isinstance(collector, pytest.File): - self.resumecapture() + self.resume_global_capture() outcome = yield - out, err = self.suspendcapture() + out, err = self.suspend_global_capture() rep = outcome.get_result() if out: rep.sections.append(("Captured stdout", out)) @@ -126,75 +151,150 @@ class CaptureManager: @pytest.hookimpl(hookwrapper=True) def pytest_runtest_setup(self, item): - self.resumecapture() + self.resume_global_capture() + # no need to activate a capture fixture because they activate themselves during creation; this + # only makes sense when a fixture uses a capture fixture, otherwise the capture fixture will + # be activated during pytest_runtest_call yield - self.suspendcapture_item(item, "setup") + self.suspend_capture_item(item, "setup") @pytest.hookimpl(hookwrapper=True) def pytest_runtest_call(self, item): - self.resumecapture() - self.activate_funcargs(item) + self.resume_global_capture() + # it is important to activate this fixture during the call phase so it overwrites the "global" + # capture + self.activate_fixture(item) yield - # self.deactivate_funcargs() called from suspendcapture() - self.suspendcapture_item(item, "call") + self.suspend_capture_item(item, "call") @pytest.hookimpl(hookwrapper=True) def pytest_runtest_teardown(self, item): - self.resumecapture() + self.resume_global_capture() + self.activate_fixture(item) yield - self.suspendcapture_item(item, "teardown") + self.suspend_capture_item(item, "teardown") @pytest.hookimpl(tryfirst=True) def pytest_keyboard_interrupt(self, excinfo): - self.reset_capturings() + self.stop_global_capturing() @pytest.hookimpl(tryfirst=True) def pytest_internalerror(self, excinfo): - self.reset_capturings() + self.stop_global_capturing() - def suspendcapture_item(self, item, when, in_=False): - out, err = self.suspendcapture(in_=in_) + def suspend_capture_item(self, item, when, in_=False): + out, err = self.suspend_global_capture(item, in_=in_) item.add_report_section(when, "stdout", out) item.add_report_section(when, "stderr", err) -error_capsysfderror = "cannot use capsys and capfd at the same time" +capture_fixtures = {"capfd", "capfdbinary", "capsys", "capsysbinary"} + + +def _ensure_only_one_capture_fixture(request, name): + fixtures = set(request.fixturenames) & capture_fixtures - {name} + if fixtures: + fixtures = sorted(fixtures) + fixtures = fixtures[0] if len(fixtures) == 1 else fixtures + raise request.raiseerror( + "cannot use {} and {} at the same time".format(fixtures, name) + ) @pytest.fixture def capsys(request): - """Enable capturing of writes to sys.stdout/sys.stderr and make + """Enable capturing of writes to ``sys.stdout`` and ``sys.stderr`` and make captured output available via ``capsys.readouterr()`` method calls - which return a ``(out, err)`` tuple. + which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` + objects. """ - if "capfd" in request.fixturenames: - raise request.raiseerror(error_capsysfderror) - request.node._capfuncarg = c = CaptureFixture(SysCapture, request) - return c + _ensure_only_one_capture_fixture(request, "capsys") + with _install_capture_fixture_on_item(request, SysCapture) as fixture: + yield fixture + + +@pytest.fixture +def capsysbinary(request): + """Enable capturing of writes to ``sys.stdout`` and ``sys.stderr`` and make + captured output available via ``capsys.readouterr()`` method calls + which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``bytes`` + objects. + """ + _ensure_only_one_capture_fixture(request, "capsysbinary") + # Currently, the implementation uses the python3 specific `.buffer` + # property of CaptureIO. + if sys.version_info < (3,): + raise request.raiseerror("capsysbinary is only supported on python 3") + with _install_capture_fixture_on_item(request, SysCaptureBinary) as fixture: + yield fixture @pytest.fixture def capfd(request): - """Enable capturing of writes to file descriptors 1 and 2 and make + """Enable capturing of writes to file descriptors ``1`` and ``2`` and make captured output available via ``capfd.readouterr()`` method calls - which return a ``(out, err)`` tuple. + which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``text`` + objects. """ - if "capsys" in request.fixturenames: - request.raiseerror(error_capsysfderror) - if not hasattr(os, 'dup'): - pytest.skip("capfd funcarg needs os.dup") - request.node._capfuncarg = c = CaptureFixture(FDCapture, request) - return c + _ensure_only_one_capture_fixture(request, "capfd") + if not hasattr(os, "dup"): + pytest.skip( + "capfd fixture needs os.dup function which is not available in this system" + ) + with _install_capture_fixture_on_item(request, FDCapture) as fixture: + yield fixture -class CaptureFixture: +@pytest.fixture +def capfdbinary(request): + """Enable capturing of write to file descriptors 1 and 2 and make + captured output available via ``capfdbinary.readouterr`` method calls + which return a ``(out, err)`` tuple. ``out`` and ``err`` will be + ``bytes`` objects. + """ + _ensure_only_one_capture_fixture(request, "capfdbinary") + if not hasattr(os, "dup"): + pytest.skip( + "capfdbinary fixture needs os.dup function which is not available in this system" + ) + with _install_capture_fixture_on_item(request, FDCaptureBinary) as fixture: + yield fixture + + +@contextlib.contextmanager +def _install_capture_fixture_on_item(request, capture_class): + """ + Context manager which creates a ``CaptureFixture`` instance and "installs" it on + the item/node of the given request. Used by ``capsys`` and ``capfd``. + + The CaptureFixture is added as attribute of the item because it needs to accessed + by ``CaptureManager`` during its ``pytest_runtest_*`` hooks. + """ + request.node._capture_fixture = fixture = CaptureFixture(capture_class, request) + capmanager = request.config.pluginmanager.getplugin("capturemanager") + # need to active this fixture right away in case it is being used by another fixture (setup phase) + # if this fixture is being used only by a test function (call phase), then we wouldn't need this + # activation, but it doesn't hurt + capmanager.activate_fixture(request.node) + yield fixture + fixture.close() + del request.node._capture_fixture + + +class CaptureFixture(object): + """ + Object returned by :py:func:`capsys`, :py:func:`capsysbinary`, :py:func:`capfd` and :py:func:`capfdbinary` + fixtures. + """ + def __init__(self, captureclass, request): self.captureclass = captureclass self.request = request def _start(self): - self._capture = MultiCapture(out=True, err=True, in_=False, - Capture=self.captureclass) + self._capture = MultiCapture( + out=True, err=True, in_=False, Capture=self.captureclass + ) self._capture.start_capturing() def close(self): @@ -204,6 +304,10 @@ class CaptureFixture: cap.stop_capturing() def readouterr(self): + """Read and return the captured output so far, resetting the internal buffer. + + :return: captured content as a namedtuple with ``out`` and ``err`` string attributes + """ try: return self._capture.readouterr() except AttributeError: @@ -211,16 +315,19 @@ class CaptureFixture: @contextlib.contextmanager def disabled(self): - capmanager = self.request.config.pluginmanager.getplugin('capturemanager') - capmanager.suspendcapture_item(self.request.node, "call", in_=True) + """Temporarily disables capture while inside the 'with' block.""" + self._capture.suspend_capturing() + capmanager = self.request.config.pluginmanager.getplugin("capturemanager") + capmanager.suspend_global_capture(item=None, in_=False) try: yield finally: - capmanager.resumecapture() + capmanager.resume_global_capture() + self._capture.resume_capturing() def safe_text_dupfile(f, mode, default_encoding="UTF8"): - """ return a open text file object that's a duplicate of f on the + """ return an open text file object that's a duplicate of f on the FD-level if possible. """ encoding = getattr(f, "encoding", None) @@ -246,12 +353,12 @@ class EncodedFile(object): self.encoding = encoding def write(self, obj): - if isinstance(obj, unicode): + if isinstance(obj, six.text_type): obj = obj.encode(self.encoding, "replace") self.buffer.write(obj) def writelines(self, linelist): - data = ''.join(linelist) + data = "".join(linelist) self.write(data) @property @@ -263,6 +370,9 @@ class EncodedFile(object): return getattr(object.__getattribute__(self, "buffer"), name) +CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"]) + + class MultiCapture(object): out = err = in_ = None @@ -311,7 +421,7 @@ class MultiCapture(object): def stop_capturing(self): """ stop capturing and reset capturing streams """ - if hasattr(self, '_reset'): + if hasattr(self, "_reset"): raise ValueError("was already stopped") self._reset = True if self.out: @@ -323,16 +433,21 @@ class MultiCapture(object): def readouterr(self): """ return snapshot unicode value of stdout/stderr capturings. """ - return (self.out.snap() if self.out is not None else "", - self.err.snap() if self.err is not None else "") + return CaptureResult( + self.out.snap() if self.out is not None else "", + self.err.snap() if self.err is not None else "", + ) -class NoCapture: +class NoCapture(object): __init__ = start = done = suspend = resume = lambda *args: None -class FDCapture: - """ Capture IO to/from a given os-level filedescriptor. """ +class FDCaptureBinary(object): + """Capture IO to/from a given os-level filedescriptor. + + snap() produces `bytes` + """ def __init__(self, targetfd, tmpfile=None): self.targetfd = targetfd @@ -371,17 +486,11 @@ class FDCapture: self.syscapture.start() def snap(self): - f = self.tmpfile - f.seek(0) - res = f.read() - if res: - enc = getattr(f, "encoding", None) - if enc and isinstance(res, bytes): - res = py.builtin._totext(res, enc, "replace") - f.truncate(0) - f.seek(0) - return res - return '' + self.tmpfile.seek(0) + res = self.tmpfile.read() + self.tmpfile.seek(0) + self.tmpfile.truncate() + return res def done(self): """ stop capturing, restore streams, return original capture file, @@ -390,7 +499,7 @@ class FDCapture: os.dup2(targetfd_save, self.targetfd) os.close(targetfd_save) self.syscapture.done() - self.tmpfile.close() + _attempt_to_close_capture_file(self.tmpfile) def suspend(self): self.syscapture.suspend() @@ -402,12 +511,27 @@ class FDCapture: def writeorg(self, data): """ write to original file descriptor. """ - if py.builtin._istext(data): + if isinstance(data, six.text_type): data = data.encode("utf8") # XXX use encoding of original stream os.write(self.targetfd_save, data) -class SysCapture: +class FDCapture(FDCaptureBinary): + """Capture IO to/from a given os-level filedescriptor. + + snap() produces text + """ + + def snap(self): + res = FDCaptureBinary.snap(self) + enc = getattr(self.tmpfile, "encoding", None) + if enc and isinstance(res, bytes): + res = six.text_type(res, enc, "replace") + return res + + +class SysCapture(object): + def __init__(self, fd, tmpfile=None): name = patchsysdict[fd] self._old = getattr(sys, name) @@ -423,16 +547,15 @@ class SysCapture: setattr(sys, self.name, self.tmpfile) def snap(self): - f = self.tmpfile - res = f.getvalue() - f.truncate(0) - f.seek(0) + res = self.tmpfile.getvalue() + self.tmpfile.seek(0) + self.tmpfile.truncate() return res def done(self): setattr(sys, self.name, self._old) del self._old - self.tmpfile.close() + _attempt_to_close_capture_file(self.tmpfile) def suspend(self): setattr(sys, self.name, self._old) @@ -445,7 +568,16 @@ class SysCapture: self._old.flush() -class DontReadFromInput: +class SysCaptureBinary(SysCapture): + + def snap(self): + res = self.tmpfile.buffer.getvalue() + self.tmpfile.seek(0) + self.tmpfile.truncate() + return res + + +class DontReadFromInput(six.Iterator): """Temporary stub class. Ideally when stdin is accessed, the capturing should be turned off, with possibly all data captured so far sent to the screen. This should be configurable, though, @@ -457,13 +589,16 @@ class DontReadFromInput: def read(self, *args): raise IOError("reading from stdin while output is captured") + readline = read readlines = read - __iter__ = read + __next__ = read + + def __iter__(self): + return self def fileno(self): - raise UnsupportedOperation("redirected stdin is pseudofile, " - "has no fileno()") + raise UnsupportedOperation("redirected stdin is pseudofile, " "has no fileno()") def isatty(self): return False @@ -476,7 +611,7 @@ class DontReadFromInput: if sys.version_info >= (3, 0): return self else: - raise AttributeError('redirected stdin has no attribute buffer') + raise AttributeError("redirected stdin has no attribute buffer") def _colorama_workaround(): @@ -489,7 +624,7 @@ def _colorama_workaround(): fail in various ways. """ - if not sys.platform.startswith('win32'): + if not sys.platform.startswith("win32"): return try: import colorama # noqa @@ -516,7 +651,7 @@ def _readline_workaround(): See https://github.com/pytest-dev/pytest/pull/1281 """ - if not sys.platform.startswith('win32'): + if not sys.platform.startswith("win32"): return try: import readline # noqa @@ -546,21 +681,21 @@ def _py36_windowsconsoleio_workaround(stream): See https://github.com/pytest-dev/py/issues/103 """ - if not sys.platform.startswith('win32') or sys.version_info[:2] < (3, 6): + if not sys.platform.startswith("win32") or sys.version_info[:2] < (3, 6): return # bail out if ``stream`` doesn't seem like a proper ``io`` stream (#2666) - if not hasattr(stream, 'buffer'): + if not hasattr(stream, "buffer"): return - buffered = hasattr(stream.buffer, 'raw') + buffered = hasattr(stream.buffer, "raw") raw_stdout = stream.buffer.raw if buffered else stream.buffer if not isinstance(raw_stdout, io._WindowsConsoleIO): return def _reopen_stdio(f, mode): - if not buffered and mode[0] == 'w': + if not buffered and mode[0] == "w": buffering = 0 else: buffering = -1 @@ -570,8 +705,20 @@ def _py36_windowsconsoleio_workaround(stream): f.encoding, f.errors, f.newlines, - f.line_buffering) + f.line_buffering, + ) - sys.__stdin__ = sys.stdin = _reopen_stdio(sys.stdin, 'rb') - sys.__stdout__ = sys.stdout = _reopen_stdio(sys.stdout, 'wb') - sys.__stderr__ = sys.stderr = _reopen_stdio(sys.stderr, 'wb') + sys.__stdin__ = sys.stdin = _reopen_stdio(sys.stdin, "rb") + sys.__stdout__ = sys.stdout = _reopen_stdio(sys.stdout, "wb") + sys.__stderr__ = sys.stderr = _reopen_stdio(sys.stderr, "wb") + + +def _attempt_to_close_capture_file(f): + """Suppress IOError when closing the temporary file used for capturing streams in py27 (#2370)""" + if six.PY2: + try: + f.close() + except IOError: + pass + else: + f.close() diff --git a/third_party/python/pytest/_pytest/compat.py b/third_party/python/pytest/src/_pytest/compat.py similarity index 53% rename from third_party/python/pytest/_pytest/compat.py rename to third_party/python/pytest/src/_pytest/compat.py index 255f69ce0d8b..7abd3d53ff91 100644 --- a/third_party/python/pytest/_pytest/compat.py +++ b/third_party/python/pytest/src/_pytest/compat.py @@ -2,17 +2,19 @@ python version compatibility code """ from __future__ import absolute_import, division, print_function -import sys -import inspect -import types -import re + +import codecs import functools +import inspect +import re +import sys import py import _pytest from _pytest.outcomes import TEST_OUTCOME - +from six import text_type +import six try: import enum @@ -25,26 +27,38 @@ _PY3 = sys.version_info > (3, 0) _PY2 = not _PY3 +if _PY3: + from inspect import signature, Parameter as Parameter +else: + from funcsigs import signature, Parameter as Parameter + + NoneType = type(None) NOTSET = object() PY35 = sys.version_info[:2] >= (3, 5) PY36 = sys.version_info[:2] >= (3, 6) -MODULE_NOT_FOUND_ERROR = 'ModuleNotFoundError' if PY36 else 'ImportError' +MODULE_NOT_FOUND_ERROR = "ModuleNotFoundError" if PY36 else "ImportError" -if hasattr(inspect, 'signature'): - def _format_args(func): - return str(inspect.signature(func)) +if _PY3: + from collections.abc import MutableMapping as MappingMixin # noqa + from collections.abc import Mapping, Sequence # noqa else: - def _format_args(func): - return inspect.formatargspec(*inspect.getargspec(func)) + # those raise DeprecationWarnings in Python >=3.7 + from collections import MutableMapping as MappingMixin # noqa + from collections import Mapping, Sequence # noqa + + +def _format_args(func): + return str(signature(func)) + isfunction = inspect.isfunction isclass = inspect.isclass # used to work around a python2 exception info leak -exc_clear = getattr(sys, 'exc_clear', lambda: None) +exc_clear = getattr(sys, "exc_clear", lambda: None) # The type of re.compile objects is not exposed in Python. -REGEX_TYPE = type(re.compile('')) +REGEX_TYPE = type(re.compile("")) def is_generator(func): @@ -58,14 +72,18 @@ def iscoroutinefunction(func): Note: copied and modified from Python 3.5's builtin couroutines.py to avoid import asyncio directly, which in turns also initializes the "logging" module as side-effect (see issue #8). """ - return (getattr(func, '_is_coroutine', False) or - (hasattr(inspect, 'iscoroutinefunction') and inspect.iscoroutinefunction(func))) + return ( + getattr(func, "_is_coroutine", False) + or ( + hasattr(inspect, "iscoroutinefunction") + and inspect.iscoroutinefunction(func) + ) + ) def getlocation(function, curdir): - import inspect fn = py.path.local(inspect.getfile(function)) - lineno = py.builtin._getcode(function).co_firstlineno + lineno = function.__code__.co_firstlineno if fn.relto(curdir): fn = fn.relto(curdir) return "%s:%d" % (fn, lineno + 1) @@ -76,67 +94,96 @@ def num_mock_patch_args(function): patchings = getattr(function, "patchings", None) if not patchings: return 0 - mock = sys.modules.get("mock", sys.modules.get("unittest.mock", None)) - if mock is not None: - return len([p for p in patchings - if not p.attribute_name and p.new is mock.DEFAULT]) + mock_modules = [sys.modules.get("mock"), sys.modules.get("unittest.mock")] + if any(mock_modules): + sentinels = [m.DEFAULT for m in mock_modules if m is not None] + return len( + [p for p in patchings if not p.attribute_name and p.new in sentinels] + ) return len(patchings) -def getfuncargnames(function, startindex=None, cls=None): +def getfuncargnames(function, is_method=False, cls=None): + """Returns the names of a function's mandatory arguments. + + This should return the names of all function arguments that: + * Aren't bound to an instance or type as in instance or class methods. + * Don't have default values. + * Aren't bound with functools.partial. + * Aren't replaced with mocks. + + The is_method and cls arguments indicate that the function should + be treated as a bound method even though it's not unless, only in + the case of cls, the function is a static method. + + @RonnyPfannschmidt: This function should be refactored when we + revisit fixtures. The fixture mechanism should ask the node for + the fixture names, and not try to obtain directly from the + function object well after collection has occurred. + """ - @RonnyPfannschmidt: This function should be refactored when we revisit fixtures. The - fixture mechanism should ask the node for the fixture names, and not try to obtain - directly from the function object well after collection has occurred. - """ - if startindex is None and cls is not None: - is_staticmethod = isinstance(cls.__dict__.get(function.__name__, None), staticmethod) - startindex = 0 if is_staticmethod else 1 - # XXX merge with main.py's varnames - # assert not isclass(function) - realfunction = function - while hasattr(realfunction, "__wrapped__"): - realfunction = realfunction.__wrapped__ - if startindex is None: - startindex = inspect.ismethod(function) and 1 or 0 - if realfunction != function: - startindex += num_mock_patch_args(function) - function = realfunction - if isinstance(function, functools.partial): - argnames = inspect.getargs(_pytest._code.getrawcode(function.func))[0] - partial = function - argnames = argnames[len(partial.args):] - if partial.keywords: - for kw in partial.keywords: - argnames.remove(kw) - else: - argnames = inspect.getargs(_pytest._code.getrawcode(function))[0] - defaults = getattr(function, 'func_defaults', - getattr(function, '__defaults__', None)) or () - numdefaults = len(defaults) - if numdefaults: - return tuple(argnames[startindex:-numdefaults]) - return tuple(argnames[startindex:]) + # The parameters attribute of a Signature object contains an + # ordered mapping of parameter names to Parameter instances. This + # creates a tuple of the names of the parameters that don't have + # defaults. + arg_names = tuple( + p.name + for p in signature(function).parameters.values() + if ( + p.kind is Parameter.POSITIONAL_OR_KEYWORD + or p.kind is Parameter.KEYWORD_ONLY + ) + and p.default is Parameter.empty + ) + # If this function should be treated as a bound method even though + # it's passed as an unbound method or function, remove the first + # parameter name. + if ( + is_method + or ( + cls + and not isinstance(cls.__dict__.get(function.__name__, None), staticmethod) + ) + ): + arg_names = arg_names[1:] + # Remove any names that will be replaced with mocks. + if hasattr(function, "__wrapped__"): + arg_names = arg_names[num_mock_patch_args(function):] + return arg_names -if sys.version_info[:2] == (2, 6): - def isclass(object): - """ Return true if the object is a class. Overrides inspect.isclass for - python 2.6 because it will return True for objects which always return - something on __getattr__ calls (see #1035). - Backport of https://hg.python.org/cpython/rev/35bf8f7a8edc - """ - return isinstance(object, (type, types.ClassType)) +def get_default_arg_names(function): + # Note: this code intentionally mirrors the code at the beginning of getfuncargnames, + # to get the arguments which were excluded from its result because they had default values + return tuple( + p.name + for p in signature(function).parameters.values() + if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY) + and p.default is not Parameter.empty + ) if _PY3: - import codecs - imap = map - izip = zip STRING_TYPES = bytes, str - UNICODE_TYPES = str, + UNICODE_TYPES = six.text_type - def _ascii_escaped(val): + if PY35: + + def _bytes_to_ascii(val): + return val.decode("ascii", "backslashreplace") + + else: + + def _bytes_to_ascii(val): + if val: + # source: http://goo.gl/bGsnwC + encoded_bytes, _ = codecs.escape_encode(val) + return encoded_bytes.decode("ascii") + else: + # empty bytes crashes codecs.escape_encode (#1087) + return "" + + def ascii_escaped(val): """If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes: @@ -155,22 +202,16 @@ if _PY3: """ if isinstance(val, bytes): - if val: - # source: http://goo.gl/bGsnwC - encoded_bytes, _ = codecs.escape_encode(val) - return encoded_bytes.decode('ascii') - else: - # empty bytes crashes codecs.escape_encode (#1087) - return '' + return _bytes_to_ascii(val) else: - return val.encode('unicode_escape').decode('ascii') + return val.encode("unicode_escape").decode("ascii") + + else: - STRING_TYPES = bytes, str, unicode - UNICODE_TYPES = unicode, + STRING_TYPES = six.string_types + UNICODE_TYPES = six.text_type - from itertools import imap, izip # NOQA - - def _ascii_escaped(val): + def ascii_escaped(val): """In py2 bytes and str are the same type, so return if it's a bytes object, return it unchanged if it is a full ascii string, otherwise escape it into its binary form. @@ -181,11 +222,11 @@ else: """ if isinstance(val, bytes): try: - return val.encode('ascii') + return val.encode("ascii") except UnicodeDecodeError: - return val.encode('string-escape') + return val.encode("string-escape") else: - return val.encode('unicode-escape') + return val.encode("unicode-escape") def get_real_func(obj): @@ -194,16 +235,16 @@ def get_real_func(obj): """ start_obj = obj for i in range(100): - new_obj = getattr(obj, '__wrapped__', None) + new_obj = getattr(obj, "__wrapped__", None) if new_obj is None: break obj = new_obj else: raise ValueError( - ("could not find real function of {start}" - "\nstopped at {current}").format( - start=py.io.saferepr(start_obj), - current=py.io.saferepr(obj))) + ("could not find real function of {start}" "\nstopped at {current}").format( + start=py.io.saferepr(start_obj), current=py.io.saferepr(obj) + ) + ) if isinstance(obj, functools.partial): obj = obj.func return obj @@ -212,7 +253,7 @@ def get_real_func(obj): def getfslineno(obj): # xxx let decorators etc specify a sane ordering obj = get_real_func(obj) - if hasattr(obj, 'place_as'): + if hasattr(obj, "place_as"): obj = obj.place_as fslineno = _pytest._code.getfslineno(obj) assert isinstance(fslineno[1], int), obj @@ -223,10 +264,7 @@ def getimfunc(func): try: return func.__func__ except AttributeError: - try: - return func.im_func - except AttributeError: - return func + return func def safe_getattr(object, name, default): @@ -244,7 +282,7 @@ def safe_getattr(object, name, default): def _is_unittest_unexpected_success_a_failure(): - """Return if the test suite should fail if a @expectedFailure unittest test PASSES. + """Return if the test suite should fail if an @expectedFailure unittest test PASSES. From https://docs.python.org/3/library/unittest.html?highlight=unittest#unittest.TestResult.wasSuccessful: Changed in version 3.4: Returns False if there were any @@ -254,39 +292,44 @@ def _is_unittest_unexpected_success_a_failure(): if _PY3: + def safe_str(v): """returns v as string""" return str(v) + + else: + def safe_str(v): """returns v as string, converting to ascii if necessary""" try: return str(v) except UnicodeError: - if not isinstance(v, unicode): - v = unicode(v) - errors = 'replace' - return v.encode('utf-8', errors) + if not isinstance(v, text_type): + v = text_type(v) + errors = "replace" + return v.encode("utf-8", errors) COLLECT_FAKEMODULE_ATTRIBUTES = ( - 'Collector', - 'Module', - 'Generator', - 'Function', - 'Instance', - 'Session', - 'Item', - 'Class', - 'File', - '_fillfuncargs', + "Collector", + "Module", + "Generator", + "Function", + "Instance", + "Session", + "Item", + "Class", + "File", + "_fillfuncargs", ) def _setup_collect_fakemodule(): from types import ModuleType import pytest - pytest.collect = ModuleType('pytest.collect') + + pytest.collect = ModuleType("pytest.collect") pytest.collect.__all__ = [] # used for setns for attr in COLLECT_FAKEMODULE_ATTRIBUTES: setattr(pytest.collect, attr, getattr(pytest, attr)) @@ -300,26 +343,28 @@ if _PY2: @property def encoding(self): - return getattr(self, '_encoding', 'UTF-8') + return getattr(self, "_encoding", "UTF-8") + else: import io class CaptureIO(io.TextIOWrapper): + def __init__(self): super(CaptureIO, self).__init__( - io.BytesIO(), - encoding='UTF-8', newline='', write_through=True, + io.BytesIO(), encoding="UTF-8", newline="", write_through=True ) def getvalue(self): - return self.buffer.getvalue().decode('UTF-8') + return self.buffer.getvalue().decode("UTF-8") class FuncargnamesCompatAttr(object): """ helper class so that Metafunc, Function and FixtureRequest don't need to each define the "funcargnames" compatibility attribute. """ + @property def funcargnames(self): """ alias attribute for ``fixturenames`` for pre-2.3 compatibility""" diff --git a/third_party/python/pytest/src/_pytest/config/__init__.py b/third_party/python/pytest/src/_pytest/config/__init__.py new file mode 100644 index 000000000000..11348b80d0f0 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/config/__init__.py @@ -0,0 +1,979 @@ +""" command line options, ini-file and conftest.py processing. """ +from __future__ import absolute_import, division, print_function +import argparse +import shlex +import traceback +import types +import warnings +import copy +import six +import py + +# DON't import pytest here because it causes import cycle troubles +import sys +import os +from _pytest.outcomes import Skipped + +import _pytest._code +import _pytest.hookspec # the extension point definitions +import _pytest.assertion +from pluggy import PluginManager, HookimplMarker, HookspecMarker +from _pytest.compat import safe_str +from .exceptions import UsageError, PrintHelp +from .findpaths import determine_setup, exists + +hookimpl = HookimplMarker("pytest") +hookspec = HookspecMarker("pytest") + +# pytest startup +# + + +class ConftestImportFailure(Exception): + + def __init__(self, path, excinfo): + Exception.__init__(self, path, excinfo) + self.path = path + self.excinfo = excinfo + + def __str__(self): + etype, evalue, etb = self.excinfo + formatted = traceback.format_tb(etb) + # The level of the tracebacks we want to print is hand crafted :( + return repr(evalue) + "\n" + "".join(formatted[2:]) + + +def main(args=None, plugins=None): + """ return exit code, after performing an in-process test run. + + :arg args: list of command line arguments. + + :arg plugins: list of plugin objects to be auto-registered during + initialization. + """ + try: + try: + config = _prepareconfig(args, plugins) + except ConftestImportFailure as e: + tw = py.io.TerminalWriter(sys.stderr) + for line in traceback.format_exception(*e.excinfo): + tw.line(line.rstrip(), red=True) + tw.line("ERROR: could not load %s\n" % (e.path,), red=True) + return 4 + else: + try: + return config.hook.pytest_cmdline_main(config=config) + finally: + config._ensure_unconfigure() + except UsageError as e: + tw = py.io.TerminalWriter(sys.stderr) + for msg in e.args: + tw.line("ERROR: {}\n".format(msg), red=True) + return 4 + + +class cmdline(object): # NOQA compatibility namespace + main = staticmethod(main) + + +def filename_arg(path, optname): + """ Argparse type validator for filename arguments. + + :path: path of filename + :optname: name of the option + """ + if os.path.isdir(path): + raise UsageError("{} must be a filename, given: {}".format(optname, path)) + return path + + +def directory_arg(path, optname): + """Argparse type validator for directory arguments. + + :path: path of directory + :optname: name of the option + """ + if not os.path.isdir(path): + raise UsageError("{} must be a directory, given: {}".format(optname, path)) + return path + + +default_plugins = ( + "mark", + "main", + "terminal", + "runner", + "python", + "fixtures", + "debugging", + "unittest", + "capture", + "skipping", + "tmpdir", + "monkeypatch", + "recwarn", + "pastebin", + "helpconfig", + "nose", + "assertion", + "junitxml", + "resultlog", + "doctest", + "cacheprovider", + "freeze_support", + "setuponly", + "setupplan", + "warnings", + "logging", +) + + +builtin_plugins = set(default_plugins) +builtin_plugins.add("pytester") + + +def get_config(): + # subsequent calls to main will create a fresh instance + pluginmanager = PytestPluginManager() + config = Config(pluginmanager) + for spec in default_plugins: + pluginmanager.import_plugin(spec) + return config + + +def get_plugin_manager(): + """ + Obtain a new instance of the + :py:class:`_pytest.config.PytestPluginManager`, with default plugins + already loaded. + + This function can be used by integration with other tools, like hooking + into pytest to run tests into an IDE. + """ + return get_config().pluginmanager + + +def _prepareconfig(args=None, plugins=None): + warning = None + if args is None: + args = sys.argv[1:] + elif isinstance(args, py.path.local): + args = [str(args)] + elif not isinstance(args, (tuple, list)): + if not isinstance(args, str): + raise ValueError("not a string or argument list: %r" % (args,)) + args = shlex.split(args, posix=sys.platform != "win32") + from _pytest import deprecated + + warning = deprecated.MAIN_STR_ARGS + config = get_config() + pluginmanager = config.pluginmanager + try: + if plugins: + for plugin in plugins: + if isinstance(plugin, six.string_types): + pluginmanager.consider_pluginarg(plugin) + else: + pluginmanager.register(plugin) + if warning: + config.warn("C1", warning) + return pluginmanager.hook.pytest_cmdline_parse( + pluginmanager=pluginmanager, args=args + ) + except BaseException: + config._ensure_unconfigure() + raise + + +class PytestPluginManager(PluginManager): + """ + Overwrites :py:class:`pluggy.PluginManager ` to add pytest-specific + functionality: + + * loading plugins from the command line, ``PYTEST_PLUGINS`` env variable and + ``pytest_plugins`` global variables found in plugins being loaded; + * ``conftest.py`` loading during start-up; + """ + + def __init__(self): + super(PytestPluginManager, self).__init__("pytest") + self._conftest_plugins = set() + + # state related to local conftest plugins + self._path2confmods = {} + self._conftestpath2mod = {} + self._confcutdir = None + self._noconftest = False + self._duplicatepaths = set() + + self.add_hookspecs(_pytest.hookspec) + self.register(self) + if os.environ.get("PYTEST_DEBUG"): + err = sys.stderr + encoding = getattr(err, "encoding", "utf8") + try: + err = py.io.dupfile(err, encoding=encoding) + except Exception: + pass + self.trace.root.setwriter(err.write) + self.enable_tracing() + + # Config._consider_importhook will set a real object if required. + self.rewrite_hook = _pytest.assertion.DummyRewriteHook() + # Used to know when we are importing conftests after the pytest_configure stage + self._configured = False + + def addhooks(self, module_or_class): + """ + .. deprecated:: 2.8 + + Use :py:meth:`pluggy.PluginManager.add_hookspecs ` + instead. + """ + warning = dict( + code="I2", + fslocation=_pytest._code.getfslineno(sys._getframe(1)), + nodeid=None, + message="use pluginmanager.add_hookspecs instead of " + "deprecated addhooks() method.", + ) + self._warn(warning) + return self.add_hookspecs(module_or_class) + + def parse_hookimpl_opts(self, plugin, name): + # pytest hooks are always prefixed with pytest_ + # so we avoid accessing possibly non-readable attributes + # (see issue #1073) + if not name.startswith("pytest_"): + return + # ignore some historic special names which can not be hooks anyway + if name == "pytest_plugins" or name.startswith("pytest_funcarg__"): + return + + method = getattr(plugin, name) + opts = super(PytestPluginManager, self).parse_hookimpl_opts(plugin, name) + + # collect unmarked hooks as long as they have the `pytest_' prefix + if opts is None and name.startswith("pytest_"): + opts = {} + + if opts is not None: + for name in ("tryfirst", "trylast", "optionalhook", "hookwrapper"): + opts.setdefault(name, hasattr(method, name)) + return opts + + def parse_hookspec_opts(self, module_or_class, name): + opts = super(PytestPluginManager, self).parse_hookspec_opts( + module_or_class, name + ) + if opts is None: + method = getattr(module_or_class, name) + if name.startswith("pytest_"): + opts = { + "firstresult": hasattr(method, "firstresult"), + "historic": hasattr(method, "historic"), + } + return opts + + def register(self, plugin, name=None): + if name in ["pytest_catchlog", "pytest_capturelog"]: + self._warn( + "{} plugin has been merged into the core, " + "please remove it from your requirements.".format( + name.replace("_", "-") + ) + ) + return + ret = super(PytestPluginManager, self).register(plugin, name) + if ret: + self.hook.pytest_plugin_registered.call_historic( + kwargs=dict(plugin=plugin, manager=self) + ) + + if isinstance(plugin, types.ModuleType): + self.consider_module(plugin) + return ret + + def getplugin(self, name): + # support deprecated naming because plugins (xdist e.g.) use it + return self.get_plugin(name) + + def hasplugin(self, name): + """Return True if the plugin with the given name is registered.""" + return bool(self.get_plugin(name)) + + def pytest_configure(self, config): + # XXX now that the pluginmanager exposes hookimpl(tryfirst...) + # we should remove tryfirst/trylast as markers + config.addinivalue_line( + "markers", + "tryfirst: mark a hook implementation function such that the " + "plugin machinery will try to call it first/as early as possible.", + ) + config.addinivalue_line( + "markers", + "trylast: mark a hook implementation function such that the " + "plugin machinery will try to call it last/as late as possible.", + ) + self._configured = True + + def _warn(self, message): + kwargs = message if isinstance(message, dict) else { + "code": "I1", "message": message, "fslocation": None, "nodeid": None + } + self.hook.pytest_logwarning.call_historic(kwargs=kwargs) + + # + # internal API for local conftest plugin handling + # + def _set_initial_conftests(self, namespace): + """ load initial conftest files given a preparsed "namespace". + As conftest files may add their own command line options + which have arguments ('--my-opt somepath') we might get some + false positives. All builtin and 3rd party plugins will have + been loaded, however, so common options will not confuse our logic + here. + """ + current = py.path.local() + self._confcutdir = current.join( + namespace.confcutdir, abs=True + ) if namespace.confcutdir else None + self._noconftest = namespace.noconftest + testpaths = namespace.file_or_dir + foundanchor = False + for path in testpaths: + path = str(path) + # remove node-id syntax + i = path.find("::") + if i != -1: + path = path[:i] + anchor = current.join(path, abs=1) + if exists(anchor): # we found some file object + self._try_load_conftest(anchor) + foundanchor = True + if not foundanchor: + self._try_load_conftest(current) + + def _try_load_conftest(self, anchor): + self._getconftestmodules(anchor) + # let's also consider test* subdirs + if anchor.check(dir=1): + for x in anchor.listdir("test*"): + if x.check(dir=1): + self._getconftestmodules(x) + + def _getconftestmodules(self, path): + if self._noconftest: + return [] + try: + return self._path2confmods[path] + except KeyError: + if path.isfile(): + clist = self._getconftestmodules(path.dirpath()) + else: + # XXX these days we may rather want to use config.rootdir + # and allow users to opt into looking into the rootdir parent + # directories instead of requiring to specify confcutdir + clist = [] + for parent in path.parts(): + if self._confcutdir and self._confcutdir.relto(parent): + continue + conftestpath = parent.join("conftest.py") + if conftestpath.isfile(): + mod = self._importconftest(conftestpath) + clist.append(mod) + + self._path2confmods[path] = clist + return clist + + def _rget_with_confmod(self, name, path): + modules = self._getconftestmodules(path) + for mod in reversed(modules): + try: + return mod, getattr(mod, name) + except AttributeError: + continue + raise KeyError(name) + + def _importconftest(self, conftestpath): + try: + return self._conftestpath2mod[conftestpath] + except KeyError: + pkgpath = conftestpath.pypkgpath() + if pkgpath is None: + _ensure_removed_sysmodule(conftestpath.purebasename) + try: + mod = conftestpath.pyimport() + if hasattr(mod, "pytest_plugins") and self._configured: + from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST + + warnings.warn(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST) + except Exception: + raise ConftestImportFailure(conftestpath, sys.exc_info()) + + self._conftest_plugins.add(mod) + self._conftestpath2mod[conftestpath] = mod + dirpath = conftestpath.dirpath() + if dirpath in self._path2confmods: + for path, mods in self._path2confmods.items(): + if path and path.relto(dirpath) or path == dirpath: + assert mod not in mods + mods.append(mod) + self.trace("loaded conftestmodule %r" % (mod)) + self.consider_conftest(mod) + return mod + + # + # API for bootstrapping plugin loading + # + # + + def consider_preparse(self, args): + for opt1, opt2 in zip(args, args[1:]): + if opt1 == "-p": + self.consider_pluginarg(opt2) + + def consider_pluginarg(self, arg): + if arg.startswith("no:"): + name = arg[3:] + self.set_blocked(name) + if not name.startswith("pytest_"): + self.set_blocked("pytest_" + name) + else: + self.import_plugin(arg) + + def consider_conftest(self, conftestmodule): + self.register(conftestmodule, name=conftestmodule.__file__) + + def consider_env(self): + self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS")) + + def consider_module(self, mod): + self._import_plugin_specs(getattr(mod, "pytest_plugins", [])) + + def _import_plugin_specs(self, spec): + plugins = _get_plugin_specs_as_list(spec) + for import_spec in plugins: + self.import_plugin(import_spec) + + def import_plugin(self, modname): + # most often modname refers to builtin modules, e.g. "pytester", + # "terminal" or "capture". Those plugins are registered under their + # basename for historic purposes but must be imported with the + # _pytest prefix. + assert isinstance(modname, (six.text_type, str)), ( + "module name as text required, got %r" % modname + ) + modname = str(modname) + if self.is_blocked(modname) or self.get_plugin(modname) is not None: + return + if modname in builtin_plugins: + importspec = "_pytest." + modname + else: + importspec = modname + self.rewrite_hook.mark_rewrite(importspec) + try: + __import__(importspec) + except ImportError as e: + new_exc_type = ImportError + new_exc_message = 'Error importing plugin "%s": %s' % ( + modname, safe_str(e.args[0]) + ) + new_exc = new_exc_type(new_exc_message) + + six.reraise(new_exc_type, new_exc, sys.exc_info()[2]) + + except Skipped as e: + self._warn("skipped plugin %r: %s" % ((modname, e.msg))) + else: + mod = sys.modules[importspec] + self.register(mod, modname) + + +def _get_plugin_specs_as_list(specs): + """ + Parses a list of "plugin specs" and returns a list of plugin names. + + Plugin specs can be given as a list of strings separated by "," or already as a list/tuple in + which case it is returned as a list. Specs can also be `None` in which case an + empty list is returned. + """ + if specs is not None: + if isinstance(specs, str): + specs = specs.split(",") if specs else [] + if not isinstance(specs, (list, tuple)): + raise UsageError( + "Plugin specs must be a ','-separated string or a " + "list/tuple of strings for plugin names. Given: %r" % specs + ) + return list(specs) + return [] + + +def _ensure_removed_sysmodule(modname): + try: + del sys.modules[modname] + except KeyError: + pass + + +class Notset(object): + + def __repr__(self): + return "" + + +notset = Notset() + + +def _iter_rewritable_modules(package_files): + for fn in package_files: + is_simple_module = "/" not in fn and fn.endswith(".py") + is_package = fn.count("/") == 1 and fn.endswith("__init__.py") + if is_simple_module: + module_name, _ = os.path.splitext(fn) + yield module_name + elif is_package: + package_name = os.path.dirname(fn) + yield package_name + + +class Config(object): + """ access to configuration values, pluginmanager and plugin hooks. """ + + def __init__(self, pluginmanager): + #: access to command line option as attributes. + #: (deprecated), use :py:func:`getoption() <_pytest.config.Config.getoption>` instead + self.option = argparse.Namespace() + from .argparsing import Parser, FILE_OR_DIR + + _a = FILE_OR_DIR + self._parser = Parser( + usage="%%(prog)s [options] [%s] [%s] [...]" % (_a, _a), + processopt=self._processopt, + ) + #: a pluginmanager instance + self.pluginmanager = pluginmanager + self.trace = self.pluginmanager.trace.root.get("config") + self.hook = self.pluginmanager.hook + self._inicache = {} + self._override_ini = () + self._opt2dest = {} + self._cleanup = [] + self._warn = self.pluginmanager._warn + self.pluginmanager.register(self, "pytestconfig") + self._configured = False + + def do_setns(dic): + import pytest + + setns(pytest, dic) + + self.hook.pytest_namespace.call_historic(do_setns, {}) + self.hook.pytest_addoption.call_historic(kwargs=dict(parser=self._parser)) + + def add_cleanup(self, func): + """ Add a function to be called when the config object gets out of + use (usually coninciding with pytest_unconfigure).""" + self._cleanup.append(func) + + def _do_configure(self): + assert not self._configured + self._configured = True + self.hook.pytest_configure.call_historic(kwargs=dict(config=self)) + + def _ensure_unconfigure(self): + if self._configured: + self._configured = False + self.hook.pytest_unconfigure(config=self) + self.hook.pytest_configure._call_history = [] + while self._cleanup: + fin = self._cleanup.pop() + fin() + + def warn(self, code, message, fslocation=None, nodeid=None): + """ generate a warning for this test session. """ + self.hook.pytest_logwarning.call_historic( + kwargs=dict( + code=code, message=message, fslocation=fslocation, nodeid=nodeid + ) + ) + + def get_terminal_writer(self): + return self.pluginmanager.get_plugin("terminalreporter")._tw + + def pytest_cmdline_parse(self, pluginmanager, args): + # REF1 assert self == pluginmanager.config, (self, pluginmanager.config) + self.parse(args) + return self + + def notify_exception(self, excinfo, option=None): + if option and option.fulltrace: + style = "long" + else: + style = "native" + excrepr = excinfo.getrepr( + funcargs=True, showlocals=getattr(option, "showlocals", False), style=style + ) + res = self.hook.pytest_internalerror(excrepr=excrepr, excinfo=excinfo) + if not any(res): + for line in str(excrepr).split("\n"): + sys.stderr.write("INTERNALERROR> %s\n" % line) + sys.stderr.flush() + + def cwd_relative_nodeid(self, nodeid): + # nodeid's are relative to the rootpath, compute relative to cwd + if self.invocation_dir != self.rootdir: + fullpath = self.rootdir.join(nodeid) + nodeid = self.invocation_dir.bestrelpath(fullpath) + return nodeid + + @classmethod + def fromdictargs(cls, option_dict, args): + """ constructor useable for subprocesses. """ + config = get_config() + config.option.__dict__.update(option_dict) + config.parse(args, addopts=False) + for x in config.option.plugins: + config.pluginmanager.consider_pluginarg(x) + return config + + def _processopt(self, opt): + for name in opt._short_opts + opt._long_opts: + self._opt2dest[name] = opt.dest + + if hasattr(opt, "default") and opt.dest: + if not hasattr(self.option, opt.dest): + setattr(self.option, opt.dest, opt.default) + + @hookimpl(trylast=True) + def pytest_load_initial_conftests(self, early_config): + self.pluginmanager._set_initial_conftests(early_config.known_args_namespace) + + def _initini(self, args): + ns, unknown_args = self._parser.parse_known_and_unknown_args( + args, namespace=copy.copy(self.option) + ) + r = determine_setup( + ns.inifilename, + ns.file_or_dir + unknown_args, + warnfunc=self.warn, + rootdir_cmd_arg=ns.rootdir or None, + ) + self.rootdir, self.inifile, self.inicfg = r + self._parser.extra_info["rootdir"] = self.rootdir + self._parser.extra_info["inifile"] = self.inifile + self.invocation_dir = py.path.local() + self._parser.addini("addopts", "extra command line options", "args") + self._parser.addini("minversion", "minimally required pytest version") + self._override_ini = ns.override_ini or () + + def _consider_importhook(self, args): + """Install the PEP 302 import hook if using assertion rewriting. + + Needs to parse the --assert= option from the commandline + and find all the installed plugins to mark them for rewriting + by the importhook. + """ + ns, unknown_args = self._parser.parse_known_and_unknown_args(args) + mode = ns.assertmode + if mode == "rewrite": + try: + hook = _pytest.assertion.install_importhook(self) + except SystemError: + mode = "plain" + else: + self._mark_plugins_for_rewrite(hook) + _warn_about_missing_assertion(mode) + + def _mark_plugins_for_rewrite(self, hook): + """ + Given an importhook, mark for rewrite any top-level + modules or packages in the distribution package for + all pytest plugins. + """ + import pkg_resources + + self.pluginmanager.rewrite_hook = hook + + # 'RECORD' available for plugins installed normally (pip install) + # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e) + # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa + # so it shouldn't be an issue + metadata_files = "RECORD", "SOURCES.txt" + + package_files = ( + entry.split(",")[0] + for entrypoint in pkg_resources.iter_entry_points("pytest11") + for metadata in metadata_files + for entry in entrypoint.dist._get_metadata(metadata) + ) + + for name in _iter_rewritable_modules(package_files): + hook.mark_rewrite(name) + + def _preparse(self, args, addopts=True): + if addopts: + args[:] = shlex.split(os.environ.get("PYTEST_ADDOPTS", "")) + args + self._initini(args) + if addopts: + args[:] = self.getini("addopts") + args + self._checkversion() + self._consider_importhook(args) + self.pluginmanager.consider_preparse(args) + self.pluginmanager.load_setuptools_entrypoints("pytest11") + self.pluginmanager.consider_env() + self.known_args_namespace = ns = self._parser.parse_known_args( + args, namespace=copy.copy(self.option) + ) + if self.known_args_namespace.confcutdir is None and self.inifile: + confcutdir = py.path.local(self.inifile).dirname + self.known_args_namespace.confcutdir = confcutdir + try: + self.hook.pytest_load_initial_conftests( + early_config=self, args=args, parser=self._parser + ) + except ConftestImportFailure: + e = sys.exc_info()[1] + if ns.help or ns.version: + # we don't want to prevent --help/--version to work + # so just let is pass and print a warning at the end + self._warn("could not load initial conftests (%s)\n" % e.path) + else: + raise + + def _checkversion(self): + import pytest + + minver = self.inicfg.get("minversion", None) + if minver: + ver = minver.split(".") + myver = pytest.__version__.split(".") + if myver < ver: + raise pytest.UsageError( + "%s:%d: requires pytest-%s, actual pytest-%s'" + % ( + self.inicfg.config.path, + self.inicfg.lineof("minversion"), + minver, + pytest.__version__, + ) + ) + + def parse(self, args, addopts=True): + # parse given cmdline arguments into this config object. + assert not hasattr( + self, "args" + ), "can only parse cmdline args at most once per Config object" + self._origargs = args + self.hook.pytest_addhooks.call_historic( + kwargs=dict(pluginmanager=self.pluginmanager) + ) + self._preparse(args, addopts=addopts) + # XXX deprecated hook: + self.hook.pytest_cmdline_preparse(config=self, args=args) + self._parser.after_preparse = True + try: + args = self._parser.parse_setoption( + args, self.option, namespace=self.option + ) + if not args: + cwd = os.getcwd() + if cwd == self.rootdir: + args = self.getini("testpaths") + if not args: + args = [cwd] + self.args = args + except PrintHelp: + pass + + def addinivalue_line(self, name, line): + """ add a line to an ini-file option. The option must have been + declared but might not yet be set in which case the line becomes the + the first line in its value. """ + x = self.getini(name) + assert isinstance(x, list) + x.append(line) # modifies the cached list inline + + def getini(self, name): + """ return configuration value from an :ref:`ini file `. If the + specified name hasn't been registered through a prior + :py:func:`parser.addini <_pytest.config.Parser.addini>` + call (usually from a plugin), a ValueError is raised. """ + try: + return self._inicache[name] + except KeyError: + self._inicache[name] = val = self._getini(name) + return val + + def _getini(self, name): + try: + description, type, default = self._parser._inidict[name] + except KeyError: + raise ValueError("unknown configuration value: %r" % (name,)) + value = self._get_override_ini_value(name) + if value is None: + try: + value = self.inicfg[name] + except KeyError: + if default is not None: + return default + if type is None: + return "" + return [] + if type == "pathlist": + dp = py.path.local(self.inicfg.config.path).dirpath() + values = [] + for relpath in shlex.split(value): + values.append(dp.join(relpath, abs=True)) + return values + elif type == "args": + return shlex.split(value) + elif type == "linelist": + return [t for t in map(lambda x: x.strip(), value.split("\n")) if t] + elif type == "bool": + return bool(_strtobool(value.strip())) + else: + assert type is None + return value + + def _getconftest_pathlist(self, name, path): + try: + mod, relroots = self.pluginmanager._rget_with_confmod(name, path) + except KeyError: + return None + modpath = py.path.local(mod.__file__).dirpath() + values = [] + for relroot in relroots: + if not isinstance(relroot, py.path.local): + relroot = relroot.replace("/", py.path.local.sep) + relroot = modpath.join(relroot, abs=True) + values.append(relroot) + return values + + def _get_override_ini_value(self, name): + value = None + # override_ini is a list of "ini=value" options + # always use the last item if multiple values are set for same ini-name, + # e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2 + for ini_config in self._override_ini: + try: + key, user_ini_value = ini_config.split("=", 1) + except ValueError: + raise UsageError("-o/--override-ini expects option=value style.") + else: + if key == name: + value = user_ini_value + return value + + def getoption(self, name, default=notset, skip=False): + """ return command line option value. + + :arg name: name of the option. You may also specify + the literal ``--OPT`` option instead of the "dest" option name. + :arg default: default value if no option of that name exists. + :arg skip: if True raise pytest.skip if option does not exists + or has a None value. + """ + name = self._opt2dest.get(name, name) + try: + val = getattr(self.option, name) + if val is None and skip: + raise AttributeError(name) + return val + except AttributeError: + if default is not notset: + return default + if skip: + import pytest + + pytest.skip("no %r option found" % (name,)) + raise ValueError("no option named %r" % (name,)) + + def getvalue(self, name, path=None): + """ (deprecated, use getoption()) """ + return self.getoption(name) + + def getvalueorskip(self, name, path=None): + """ (deprecated, use getoption(skip=True)) """ + return self.getoption(name, skip=True) + + +def _assertion_supported(): + try: + assert False + except AssertionError: + return True + else: + return False + + +def _warn_about_missing_assertion(mode): + if not _assertion_supported(): + if mode == "plain": + sys.stderr.write( + "WARNING: ASSERTIONS ARE NOT EXECUTED" + " and FAILING TESTS WILL PASS. Are you" + " using python -O?" + ) + else: + sys.stderr.write( + "WARNING: assertions not in test modules or" + " plugins will be ignored" + " because assert statements are not executed " + "by the underlying Python interpreter " + "(are you using python -O?)\n" + ) + + +def setns(obj, dic): + import pytest + + for name, value in dic.items(): + if isinstance(value, dict): + mod = getattr(obj, name, None) + if mod is None: + modname = "pytest.%s" % name + mod = types.ModuleType(modname) + sys.modules[modname] = mod + mod.__all__ = [] + setattr(obj, name, mod) + obj.__all__.append(name) + setns(mod, value) + else: + setattr(obj, name, value) + obj.__all__.append(name) + # if obj != pytest: + # pytest.__all__.append(name) + setattr(pytest, name, value) + + +def create_terminal_writer(config, *args, **kwargs): + """Create a TerminalWriter instance configured according to the options + in the config object. Every code which requires a TerminalWriter object + and has access to a config object should use this function. + """ + tw = py.io.TerminalWriter(*args, **kwargs) + if config.option.color == "yes": + tw.hasmarkup = True + if config.option.color == "no": + tw.hasmarkup = False + return tw + + +def _strtobool(val): + """Convert a string representation of truth to true (1) or false (0). + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + + .. note:: copied from distutils.util + """ + val = val.lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return 1 + elif val in ("n", "no", "f", "false", "off", "0"): + return 0 + else: + raise ValueError("invalid truth value %r" % (val,)) diff --git a/third_party/python/pytest/src/_pytest/config/argparsing.py b/third_party/python/pytest/src/_pytest/config/argparsing.py new file mode 100644 index 000000000000..781d8e8c4ee5 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/config/argparsing.py @@ -0,0 +1,394 @@ +import six +import warnings +import argparse + +FILE_OR_DIR = "file_or_dir" + + +class Parser(object): + """ Parser for command line arguments and ini-file values. + + :ivar extra_info: dict of generic param -> value to display in case + there's an error processing the command line arguments. + """ + + def __init__(self, usage=None, processopt=None): + self._anonymous = OptionGroup("custom options", parser=self) + self._groups = [] + self._processopt = processopt + self._usage = usage + self._inidict = {} + self._ininames = [] + self.extra_info = {} + + def processoption(self, option): + if self._processopt: + if option.dest: + self._processopt(option) + + def getgroup(self, name, description="", after=None): + """ get (or create) a named option Group. + + :name: name of the option group. + :description: long description for --help output. + :after: name of other group, used for ordering --help output. + + The returned group object has an ``addoption`` method with the same + signature as :py:func:`parser.addoption + <_pytest.config.Parser.addoption>` but will be shown in the + respective group in the output of ``pytest. --help``. + """ + for group in self._groups: + if group.name == name: + return group + group = OptionGroup(name, description, parser=self) + i = 0 + for i, grp in enumerate(self._groups): + if grp.name == after: + break + self._groups.insert(i + 1, group) + return group + + def addoption(self, *opts, **attrs): + """ register a command line option. + + :opts: option names, can be short or long options. + :attrs: same attributes which the ``add_option()`` function of the + `argparse library + `_ + accepts. + + After command line parsing options are available on the pytest config + object via ``config.option.NAME`` where ``NAME`` is usually set + by passing a ``dest`` attribute, for example + ``addoption("--long", dest="NAME", ...)``. + """ + self._anonymous.addoption(*opts, **attrs) + + def parse(self, args, namespace=None): + from _pytest._argcomplete import try_argcomplete + + self.optparser = self._getparser() + try_argcomplete(self.optparser) + return self.optparser.parse_args([str(x) for x in args], namespace=namespace) + + def _getparser(self): + from _pytest._argcomplete import filescompleter + + optparser = MyOptionParser(self, self.extra_info) + groups = self._groups + [self._anonymous] + for group in groups: + if group.options: + desc = group.description or group.name + arggroup = optparser.add_argument_group(desc) + for option in group.options: + n = option.names() + a = option.attrs() + arggroup.add_argument(*n, **a) + # bash like autocompletion for dirs (appending '/') + optparser.add_argument(FILE_OR_DIR, nargs="*").completer = filescompleter + return optparser + + def parse_setoption(self, args, option, namespace=None): + parsedoption = self.parse(args, namespace=namespace) + for name, value in parsedoption.__dict__.items(): + setattr(option, name, value) + return getattr(parsedoption, FILE_OR_DIR) + + def parse_known_args(self, args, namespace=None): + """parses and returns a namespace object with known arguments at this + point. + """ + return self.parse_known_and_unknown_args(args, namespace=namespace)[0] + + def parse_known_and_unknown_args(self, args, namespace=None): + """parses and returns a namespace object with known arguments, and + the remaining arguments unknown at this point. + """ + optparser = self._getparser() + args = [str(x) for x in args] + return optparser.parse_known_args(args, namespace=namespace) + + def addini(self, name, help, type=None, default=None): + """ register an ini-file option. + + :name: name of the ini-variable + :type: type of the variable, can be ``pathlist``, ``args``, ``linelist`` + or ``bool``. + :default: default value if no ini-file option exists but is queried. + + The value of ini-variables can be retrieved via a call to + :py:func:`config.getini(name) <_pytest.config.Config.getini>`. + """ + assert type in (None, "pathlist", "args", "linelist", "bool") + self._inidict[name] = (help, type, default) + self._ininames.append(name) + + +class ArgumentError(Exception): + """ + Raised if an Argument instance is created with invalid or + inconsistent arguments. + """ + + def __init__(self, msg, option): + self.msg = msg + self.option_id = str(option) + + def __str__(self): + if self.option_id: + return "option %s: %s" % (self.option_id, self.msg) + else: + return self.msg + + +class Argument(object): + """class that mimics the necessary behaviour of optparse.Option + + its currently a least effort implementation + and ignoring choices and integer prefixes + https://docs.python.org/3/library/optparse.html#optparse-standard-option-types + """ + _typ_map = {"int": int, "string": str, "float": float, "complex": complex} + + def __init__(self, *names, **attrs): + """store parms in private vars for use in add_argument""" + self._attrs = attrs + self._short_opts = [] + self._long_opts = [] + self.dest = attrs.get("dest") + if "%default" in (attrs.get("help") or ""): + warnings.warn( + 'pytest now uses argparse. "%default" should be' + ' changed to "%(default)s" ', + DeprecationWarning, + stacklevel=3, + ) + try: + typ = attrs["type"] + except KeyError: + pass + else: + # this might raise a keyerror as well, don't want to catch that + if isinstance(typ, six.string_types): + if typ == "choice": + warnings.warn( + "type argument to addoption() is a string %r." + " For parsearg this is optional and when supplied" + " should be a type." + " (options: %s)" % (typ, names), + DeprecationWarning, + stacklevel=3, + ) + # argparse expects a type here take it from + # the type of the first element + attrs["type"] = type(attrs["choices"][0]) + else: + warnings.warn( + "type argument to addoption() is a string %r." + " For parsearg this should be a type." + " (options: %s)" % (typ, names), + DeprecationWarning, + stacklevel=3, + ) + attrs["type"] = Argument._typ_map[typ] + # used in test_parseopt -> test_parse_defaultgetter + self.type = attrs["type"] + else: + self.type = typ + try: + # attribute existence is tested in Config._processopt + self.default = attrs["default"] + except KeyError: + pass + self._set_opt_strings(names) + if not self.dest: + if self._long_opts: + self.dest = self._long_opts[0][2:].replace("-", "_") + else: + try: + self.dest = self._short_opts[0][1:] + except IndexError: + raise ArgumentError("need a long or short option", self) + + def names(self): + return self._short_opts + self._long_opts + + def attrs(self): + # update any attributes set by processopt + attrs = "default dest help".split() + if self.dest: + attrs.append(self.dest) + for attr in attrs: + try: + self._attrs[attr] = getattr(self, attr) + except AttributeError: + pass + if self._attrs.get("help"): + a = self._attrs["help"] + a = a.replace("%default", "%(default)s") + # a = a.replace('%prog', '%(prog)s') + self._attrs["help"] = a + return self._attrs + + def _set_opt_strings(self, opts): + """directly from optparse + + might not be necessary as this is passed to argparse later on""" + for opt in opts: + if len(opt) < 2: + raise ArgumentError( + "invalid option string %r: " + "must be at least two characters long" % opt, + self, + ) + elif len(opt) == 2: + if not (opt[0] == "-" and opt[1] != "-"): + raise ArgumentError( + "invalid short option string %r: " + "must be of the form -x, (x any non-dash char)" % opt, + self, + ) + self._short_opts.append(opt) + else: + if not (opt[0:2] == "--" and opt[2] != "-"): + raise ArgumentError( + "invalid long option string %r: " + "must start with --, followed by non-dash" % opt, + self, + ) + self._long_opts.append(opt) + + def __repr__(self): + args = [] + if self._short_opts: + args += ["_short_opts: " + repr(self._short_opts)] + if self._long_opts: + args += ["_long_opts: " + repr(self._long_opts)] + args += ["dest: " + repr(self.dest)] + if hasattr(self, "type"): + args += ["type: " + repr(self.type)] + if hasattr(self, "default"): + args += ["default: " + repr(self.default)] + return "Argument({})".format(", ".join(args)) + + +class OptionGroup(object): + + def __init__(self, name, description="", parser=None): + self.name = name + self.description = description + self.options = [] + self.parser = parser + + def addoption(self, *optnames, **attrs): + """ add an option to this group. + + if a shortened version of a long option is specified it will + be suppressed in the help. addoption('--twowords', '--two-words') + results in help showing '--two-words' only, but --twowords gets + accepted **and** the automatic destination is in args.twowords + """ + conflict = set(optnames).intersection( + name for opt in self.options for name in opt.names() + ) + if conflict: + raise ValueError("option names %s already added" % conflict) + option = Argument(*optnames, **attrs) + self._addoption_instance(option, shortupper=False) + + def _addoption(self, *optnames, **attrs): + option = Argument(*optnames, **attrs) + self._addoption_instance(option, shortupper=True) + + def _addoption_instance(self, option, shortupper=False): + if not shortupper: + for opt in option._short_opts: + if opt[0] == "-" and opt[1].islower(): + raise ValueError("lowercase shortoptions reserved") + if self.parser: + self.parser.processoption(option) + self.options.append(option) + + +class MyOptionParser(argparse.ArgumentParser): + + def __init__(self, parser, extra_info=None): + if not extra_info: + extra_info = {} + self._parser = parser + argparse.ArgumentParser.__init__( + self, + usage=parser._usage, + add_help=False, + formatter_class=DropShorterLongHelpFormatter, + ) + # extra_info is a dict of (param -> value) to display if there's + # an usage error to provide more contextual information to the user + self.extra_info = extra_info + + def parse_args(self, args=None, namespace=None): + """allow splitting of positional arguments""" + args, argv = self.parse_known_args(args, namespace) + if argv: + for arg in argv: + if arg and arg[0] == "-": + lines = ["unrecognized arguments: %s" % (" ".join(argv))] + for k, v in sorted(self.extra_info.items()): + lines.append(" %s: %s" % (k, v)) + self.error("\n".join(lines)) + getattr(args, FILE_OR_DIR).extend(argv) + return args + + +class DropShorterLongHelpFormatter(argparse.HelpFormatter): + """shorten help for long options that differ only in extra hyphens + + - collapse **long** options that are the same except for extra hyphens + - special action attribute map_long_option allows surpressing additional + long options + - shortcut if there are only two options and one of them is a short one + - cache result on action object as this is called at least 2 times + """ + + def _format_action_invocation(self, action): + orgstr = argparse.HelpFormatter._format_action_invocation(self, action) + if orgstr and orgstr[0] != "-": # only optional arguments + return orgstr + res = getattr(action, "_formatted_action_invocation", None) + if res: + return res + options = orgstr.split(", ") + if len(options) == 2 and (len(options[0]) == 2 or len(options[1]) == 2): + # a shortcut for '-h, --help' or '--abc', '-a' + action._formatted_action_invocation = orgstr + return orgstr + return_list = [] + option_map = getattr(action, "map_long_option", {}) + if option_map is None: + option_map = {} + short_long = {} + for option in options: + if len(option) == 2 or option[2] == " ": + continue + if not option.startswith("--"): + raise ArgumentError( + 'long optional argument without "--": [%s]' % (option), self + ) + xxoption = option[2:] + if xxoption.split()[0] not in option_map: + shortened = xxoption.replace("-", "") + if ( + shortened not in short_long + or len(short_long[shortened]) < len(xxoption) + ): + short_long[shortened] = xxoption + # now short_long has been filled out to the longest with dashes + # **and** we keep the right option ordering from add_argument + for option in options: + if len(option) == 2 or option[2] == " ": + return_list.append(option) + if option[2:] == short_long.get(option.replace("-", "")): + return_list.append(option.replace(" ", "=", 1)) + action._formatted_action_invocation = ", ".join(return_list) + return action._formatted_action_invocation diff --git a/third_party/python/pytest/src/_pytest/config/exceptions.py b/third_party/python/pytest/src/_pytest/config/exceptions.py new file mode 100644 index 000000000000..64bae834d1ce --- /dev/null +++ b/third_party/python/pytest/src/_pytest/config/exceptions.py @@ -0,0 +1,8 @@ +class UsageError(Exception): + """ error in pytest usage or invocation""" + + +class PrintHelp(Exception): + """Raised when pytest should print it's help to skip the rest of the + argument parsing and validation.""" + pass diff --git a/third_party/python/pytest/src/_pytest/config/findpaths.py b/third_party/python/pytest/src/_pytest/config/findpaths.py new file mode 100644 index 000000000000..fde7bddb939b --- /dev/null +++ b/third_party/python/pytest/src/_pytest/config/findpaths.py @@ -0,0 +1,140 @@ +import py +import os +from .exceptions import UsageError + + +def exists(path, ignore=EnvironmentError): + try: + return path.check() + except ignore: + return False + + +def getcfg(args, warnfunc=None): + """ + Search the list of arguments for a valid ini-file for pytest, + and return a tuple of (rootdir, inifile, cfg-dict). + + note: warnfunc is an optional function used to warn + about ini-files that use deprecated features. + This parameter should be removed when pytest + adopts standard deprecation warnings (#1804). + """ + from _pytest.deprecated import CFG_PYTEST_SECTION + + inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"] + args = [x for x in args if not str(x).startswith("-")] + if not args: + args = [py.path.local()] + for arg in args: + arg = py.path.local(arg) + for base in arg.parts(reverse=True): + for inibasename in inibasenames: + p = base.join(inibasename) + if exists(p): + iniconfig = py.iniconfig.IniConfig(p) + if "pytest" in iniconfig.sections: + if inibasename == "setup.cfg" and warnfunc: + warnfunc( + "C1", CFG_PYTEST_SECTION.format(filename=inibasename) + ) + return base, p, iniconfig["pytest"] + if ( + inibasename == "setup.cfg" + and "tool:pytest" in iniconfig.sections + ): + return base, p, iniconfig["tool:pytest"] + elif inibasename == "pytest.ini": + # allowed to be empty + return base, p, {} + return None, None, None + + +def get_common_ancestor(paths): + common_ancestor = None + for path in paths: + if not path.exists(): + continue + if common_ancestor is None: + common_ancestor = path + else: + if path.relto(common_ancestor) or path == common_ancestor: + continue + elif common_ancestor.relto(path): + common_ancestor = path + else: + shared = path.common(common_ancestor) + if shared is not None: + common_ancestor = shared + if common_ancestor is None: + common_ancestor = py.path.local() + elif common_ancestor.isfile(): + common_ancestor = common_ancestor.dirpath() + return common_ancestor + + +def get_dirs_from_args(args): + + def is_option(x): + return str(x).startswith("-") + + def get_file_part_from_node_id(x): + return str(x).split("::")[0] + + def get_dir_from_path(path): + if path.isdir(): + return path + return py.path.local(path.dirname) + + # These look like paths but may not exist + possible_paths = ( + py.path.local(get_file_part_from_node_id(arg)) + for arg in args + if not is_option(arg) + ) + + return [get_dir_from_path(path) for path in possible_paths if path.exists()] + + +def determine_setup(inifile, args, warnfunc=None, rootdir_cmd_arg=None): + dirs = get_dirs_from_args(args) + if inifile: + iniconfig = py.iniconfig.IniConfig(inifile) + is_cfg_file = str(inifile).endswith(".cfg") + # TODO: [pytest] section in *.cfg files is depricated. Need refactoring. + sections = ["tool:pytest", "pytest"] if is_cfg_file else ["pytest"] + for section in sections: + try: + inicfg = iniconfig[section] + if is_cfg_file and section == "pytest" and warnfunc: + from _pytest.deprecated import CFG_PYTEST_SECTION + + warnfunc("C1", CFG_PYTEST_SECTION.format(filename=str(inifile))) + break + except KeyError: + inicfg = None + rootdir = get_common_ancestor(dirs) + else: + ancestor = get_common_ancestor(dirs) + rootdir, inifile, inicfg = getcfg([ancestor], warnfunc=warnfunc) + if rootdir is None: + for rootdir in ancestor.parts(reverse=True): + if rootdir.join("setup.py").exists(): + break + else: + rootdir, inifile, inicfg = getcfg(dirs, warnfunc=warnfunc) + if rootdir is None: + rootdir = get_common_ancestor([py.path.local(), ancestor]) + is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/" + if is_fs_root: + rootdir = ancestor + if rootdir_cmd_arg: + rootdir_abs_path = py.path.local(os.path.expandvars(rootdir_cmd_arg)) + if not os.path.isdir(str(rootdir_abs_path)): + raise UsageError( + "Directory '{}' not found. Check your '--rootdir' option.".format( + rootdir_abs_path + ) + ) + rootdir = rootdir_abs_path + return rootdir, inifile, inicfg or {} diff --git a/third_party/python/pytest/_pytest/debugging.py b/third_party/python/pytest/src/_pytest/debugging.py similarity index 67% rename from third_party/python/pytest/_pytest/debugging.py rename to third_party/python/pytest/src/_pytest/debugging.py index aa9c9a3863fe..2e253aaa2af3 100644 --- a/third_party/python/pytest/_pytest/debugging.py +++ b/third_party/python/pytest/src/_pytest/debugging.py @@ -2,17 +2,32 @@ from __future__ import absolute_import, division, print_function import pdb import sys +import os +from doctest import UnexpectedException + +try: + from builtins import breakpoint # noqa + + SUPPORTS_BREAKPOINT_BUILTIN = True +except ImportError: + SUPPORTS_BREAKPOINT_BUILTIN = False def pytest_addoption(parser): group = parser.getgroup("general") group._addoption( - '--pdb', dest="usepdb", action="store_true", - help="start the interactive Python debugger on errors.") + "--pdb", + dest="usepdb", + action="store_true", + help="start the interactive Python debugger on errors or KeyboardInterrupt.", + ) group._addoption( - '--pdbcls', dest="usepdb_cls", metavar="modulename:classname", + "--pdbcls", + dest="usepdb_cls", + metavar="modulename:classname", help="start a custom interactive Python debugger on errors. " - "For example: --pdbcls=IPython.terminal.debugger:TerminalPdb") + "For example: --pdbcls=IPython.terminal.debugger:TerminalPdb", + ) def pytest_configure(config): @@ -24,7 +39,13 @@ def pytest_configure(config): pdb_cls = pdb.Pdb if config.getvalue("usepdb"): - config.pluginmanager.register(PdbInvoke(), 'pdbinvoke') + config.pluginmanager.register(PdbInvoke(), "pdbinvoke") + + # Use custom Pdb class set_trace instead of default Pdb on breakpoint() call + if SUPPORTS_BREAKPOINT_BUILTIN: + _environ_pythonbreakpoint = os.environ.get("PYTHONBREAKPOINT", "") + if _environ_pythonbreakpoint == "": + sys.breakpointhook = pytestPDB.set_trace old = (pdb.set_trace, pytestPDB._pluginmanager) @@ -32,6 +53,8 @@ def pytest_configure(config): pdb.set_trace, pytestPDB._pluginmanager = old pytestPDB._config = None pytestPDB._pdb_cls = pdb.Pdb + if SUPPORTS_BREAKPOINT_BUILTIN: + sys.breakpointhook = sys.__breakpointhook__ pdb.set_trace = pytestPDB.set_trace pytestPDB._pluginmanager = config.pluginmanager @@ -40,7 +63,7 @@ def pytest_configure(config): config._cleanup.append(fin) -class pytestPDB: +class pytestPDB(object): """ Pseudo PDB that defers to the real pdb. """ _pluginmanager = None _config = None @@ -50,11 +73,12 @@ class pytestPDB: def set_trace(cls): """ invoke PDB set_trace debugging, dropping any IO capturing. """ import _pytest.config + frame = sys._getframe().f_back if cls._pluginmanager is not None: capman = cls._pluginmanager.getplugin("capturemanager") if capman: - capman.suspendcapture(in_=True) + capman.suspend_global_capture(in_=True) tw = _pytest.config.create_terminal_writer(cls._config) tw.line() tw.sep(">", "PDB set_trace (IO-capturing turned off)") @@ -62,11 +86,12 @@ class pytestPDB: cls._pdb_cls().set_trace(frame) -class PdbInvoke: +class PdbInvoke(object): + def pytest_exception_interact(self, node, call, report): capman = node.config.pluginmanager.getplugin("capturemanager") if capman: - out, err = capman.suspendcapture(in_=True) + out, err = capman.suspend_global_capture(in_=True) sys.stdout.write(out) sys.stdout.write(err) _enter_pdb(node, call.excinfo, report) @@ -85,6 +110,18 @@ def _enter_pdb(node, excinfo, rep): # for not completely clear reasons. tw = node.config.pluginmanager.getplugin("terminalreporter")._tw tw.line() + + showcapture = node.config.option.showcapture + + for sectionname, content in ( + ("stdout", rep.capstdout), ("stderr", rep.capstderr), ("log", rep.caplog) + ): + if showcapture in (sectionname, "all") and content: + tw.sep(">", "captured " + sectionname) + if content[-1:] == "\n": + content = content[:-1] + tw.line(content) + tw.sep(">", "traceback") rep.toterminal(tw) tw.sep(">", "entering PDB") @@ -95,10 +132,9 @@ def _enter_pdb(node, excinfo, rep): def _postmortem_traceback(excinfo): - # A doctest.UnexpectedException is not useful for post_mortem. - # Use the underlying exception instead: - from doctest import UnexpectedException if isinstance(excinfo.value, UnexpectedException): + # A doctest.UnexpectedException is not useful for post_mortem. + # Use the underlying exception instead: return excinfo.value.exc_info[2] else: return excinfo._excinfo[2] @@ -112,12 +148,15 @@ def _find_last_non_hidden_frame(stack): def post_mortem(t): + class Pdb(pytestPDB._pdb_cls): + def get_stack(self, f, t): stack, i = pdb.Pdb.get_stack(self, f, t) if f is None: i = _find_last_non_hidden_frame(stack) return stack, i + p = Pdb() p.reset() p.interaction(None, t) diff --git a/third_party/python/pytest/src/_pytest/deprecated.py b/third_party/python/pytest/src/_pytest/deprecated.py new file mode 100644 index 000000000000..7ebdcf999722 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/deprecated.py @@ -0,0 +1,65 @@ +""" +This module contains deprecation messages and bits of code used elsewhere in the codebase +that is planned to be removed in the next pytest release. + +Keeping it in a central location makes it easy to track what is deprecated and should +be removed when the time comes. +""" +from __future__ import absolute_import, division, print_function + + +class RemovedInPytest4Warning(DeprecationWarning): + """warning class for features removed in pytest 4.0""" + + +MAIN_STR_ARGS = "passing a string to pytest.main() is deprecated, " "pass a list of arguments instead." + +YIELD_TESTS = "yield tests are deprecated, and scheduled to be removed in pytest 4.0" + +FUNCARG_PREFIX = ( + '{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated ' + "and scheduled to be removed in pytest 4.0. " + "Please remove the prefix and use the @pytest.fixture decorator instead." +) + +CFG_PYTEST_SECTION = "[pytest] section in {filename} files is deprecated, use [tool:pytest] instead." + +GETFUNCARGVALUE = "use of getfuncargvalue is deprecated, use getfixturevalue" + +RESULT_LOG = ( + "--result-log is deprecated and scheduled for removal in pytest 4.0.\n" + "See https://docs.pytest.org/en/latest/usage.html#creating-resultlog-format-files for more information." +) + +MARK_INFO_ATTRIBUTE = RemovedInPytest4Warning( + "MarkInfo objects are deprecated as they contain merged marks which are hard to deal with correctly.\n" + "Please use node.get_closest_marker(name) or node.iter_markers(name).\n" + "Docs: https://docs.pytest.org/en/latest/mark.html#updating-code" +) + +MARK_PARAMETERSET_UNPACKING = RemovedInPytest4Warning( + "Applying marks directly to parameters is deprecated," + " please use pytest.param(..., marks=...) instead.\n" + "For more details, see: https://docs.pytest.org/en/latest/parametrize.html" +) + +RECORD_XML_PROPERTY = ( + 'Fixture renamed from "record_xml_property" to "record_property" as user ' + "properties are now available to all reporters.\n" + '"record_xml_property" is now deprecated.' +) + +COLLECTOR_MAKEITEM = RemovedInPytest4Warning( + "pycollector makeitem was removed " "as it is an accidentially leaked internal api" +) + +METAFUNC_ADD_CALL = ( + "Metafunc.addcall is deprecated and scheduled to be removed in pytest 4.0.\n" + "Please use Metafunc.parametrize instead." +) + +PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST = RemovedInPytest4Warning( + "Defining pytest_plugins in a non-top-level conftest is deprecated, " + "because it affects the entire directory tree in a non-explicit way.\n" + "Please move it to the top level conftest file instead." +) diff --git a/third_party/python/pytest/src/_pytest/doctest.py b/third_party/python/pytest/src/_pytest/doctest.py new file mode 100644 index 000000000000..b0a3ad08dfc4 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/doctest.py @@ -0,0 +1,520 @@ +""" discover and run doctests in modules and test files.""" +from __future__ import absolute_import, division, print_function + +import traceback +import sys +import platform + +import pytest +from _pytest._code.code import ExceptionInfo, ReprFileLocation, TerminalRepr +from _pytest.fixtures import FixtureRequest + + +DOCTEST_REPORT_CHOICE_NONE = "none" +DOCTEST_REPORT_CHOICE_CDIFF = "cdiff" +DOCTEST_REPORT_CHOICE_NDIFF = "ndiff" +DOCTEST_REPORT_CHOICE_UDIFF = "udiff" +DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE = "only_first_failure" + +DOCTEST_REPORT_CHOICES = ( + DOCTEST_REPORT_CHOICE_NONE, + DOCTEST_REPORT_CHOICE_CDIFF, + DOCTEST_REPORT_CHOICE_NDIFF, + DOCTEST_REPORT_CHOICE_UDIFF, + DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE, +) + +# Lazy definition of runner class +RUNNER_CLASS = None + + +def pytest_addoption(parser): + parser.addini( + "doctest_optionflags", + "option flags for doctests", + type="args", + default=["ELLIPSIS"], + ) + parser.addini( + "doctest_encoding", "encoding used for doctest files", default="utf-8" + ) + group = parser.getgroup("collect") + group.addoption( + "--doctest-modules", + action="store_true", + default=False, + help="run doctests in all .py modules", + dest="doctestmodules", + ) + group.addoption( + "--doctest-report", + type=str.lower, + default="udiff", + help="choose another output format for diffs on doctest failure", + choices=DOCTEST_REPORT_CHOICES, + dest="doctestreport", + ) + group.addoption( + "--doctest-glob", + action="append", + default=[], + metavar="pat", + help="doctests file matching pattern, default: test*.txt", + dest="doctestglob", + ) + group.addoption( + "--doctest-ignore-import-errors", + action="store_true", + default=False, + help="ignore doctest ImportErrors", + dest="doctest_ignore_import_errors", + ) + group.addoption( + "--doctest-continue-on-failure", + action="store_true", + default=False, + help="for a given doctest, continue to run after the first failure", + dest="doctest_continue_on_failure", + ) + + +def pytest_collect_file(path, parent): + config = parent.config + if path.ext == ".py": + if config.option.doctestmodules and not _is_setup_py(config, path, parent): + return DoctestModule(path, parent) + elif _is_doctest(config, path, parent): + return DoctestTextfile(path, parent) + + +def _is_setup_py(config, path, parent): + if path.basename != "setup.py": + return False + contents = path.read() + return "setuptools" in contents or "distutils" in contents + + +def _is_doctest(config, path, parent): + if path.ext in (".txt", ".rst") and parent.session.isinitpath(path): + return True + globs = config.getoption("doctestglob") or ["test*.txt"] + for glob in globs: + if path.check(fnmatch=glob): + return True + return False + + +class ReprFailDoctest(TerminalRepr): + + def __init__(self, reprlocation_lines): + # List of (reprlocation, lines) tuples + self.reprlocation_lines = reprlocation_lines + + def toterminal(self, tw): + for reprlocation, lines in self.reprlocation_lines: + for line in lines: + tw.line(line) + reprlocation.toterminal(tw) + + +class MultipleDoctestFailures(Exception): + + def __init__(self, failures): + super(MultipleDoctestFailures, self).__init__() + self.failures = failures + + +def _init_runner_class(): + import doctest + + class PytestDoctestRunner(doctest.DebugRunner): + """ + Runner to collect failures. Note that the out variable in this case is + a list instead of a stdout-like object + """ + + def __init__( + self, checker=None, verbose=None, optionflags=0, continue_on_failure=True + ): + doctest.DebugRunner.__init__( + self, checker=checker, verbose=verbose, optionflags=optionflags + ) + self.continue_on_failure = continue_on_failure + + def report_failure(self, out, test, example, got): + failure = doctest.DocTestFailure(test, example, got) + if self.continue_on_failure: + out.append(failure) + else: + raise failure + + def report_unexpected_exception(self, out, test, example, exc_info): + failure = doctest.UnexpectedException(test, example, exc_info) + if self.continue_on_failure: + out.append(failure) + else: + raise failure + + return PytestDoctestRunner + + +def _get_runner(checker=None, verbose=None, optionflags=0, continue_on_failure=True): + # We need this in order to do a lazy import on doctest + global RUNNER_CLASS + if RUNNER_CLASS is None: + RUNNER_CLASS = _init_runner_class() + return RUNNER_CLASS( + checker=checker, + verbose=verbose, + optionflags=optionflags, + continue_on_failure=continue_on_failure, + ) + + +class DoctestItem(pytest.Item): + + def __init__(self, name, parent, runner=None, dtest=None): + super(DoctestItem, self).__init__(name, parent) + self.runner = runner + self.dtest = dtest + self.obj = None + self.fixture_request = None + + def setup(self): + if self.dtest is not None: + self.fixture_request = _setup_fixtures(self) + globs = dict(getfixture=self.fixture_request.getfixturevalue) + for name, value in self.fixture_request.getfixturevalue( + "doctest_namespace" + ).items(): + globs[name] = value + self.dtest.globs.update(globs) + + def runtest(self): + _check_all_skipped(self.dtest) + self._disable_output_capturing_for_darwin() + failures = [] + self.runner.run(self.dtest, out=failures) + if failures: + raise MultipleDoctestFailures(failures) + + def _disable_output_capturing_for_darwin(self): + """ + Disable output capturing. Otherwise, stdout is lost to doctest (#985) + """ + if platform.system() != "Darwin": + return + capman = self.config.pluginmanager.getplugin("capturemanager") + if capman: + out, err = capman.suspend_global_capture(in_=True) + sys.stdout.write(out) + sys.stderr.write(err) + + def repr_failure(self, excinfo): + import doctest + + failures = None + if excinfo.errisinstance((doctest.DocTestFailure, doctest.UnexpectedException)): + failures = [excinfo.value] + elif excinfo.errisinstance(MultipleDoctestFailures): + failures = excinfo.value.failures + + if failures is not None: + reprlocation_lines = [] + for failure in failures: + example = failure.example + test = failure.test + filename = test.filename + if test.lineno is None: + lineno = None + else: + lineno = test.lineno + example.lineno + 1 + message = type(failure).__name__ + reprlocation = ReprFileLocation(filename, lineno, message) + checker = _get_checker() + report_choice = _get_report_choice( + self.config.getoption("doctestreport") + ) + if lineno is not None: + lines = failure.test.docstring.splitlines(False) + # add line numbers to the left of the error message + lines = [ + "%03d %s" % (i + test.lineno + 1, x) + for (i, x) in enumerate(lines) + ] + # trim docstring error lines to 10 + lines = lines[max(example.lineno - 9, 0):example.lineno + 1] + else: + lines = [ + "EXAMPLE LOCATION UNKNOWN, not showing all tests of that example" + ] + indent = ">>>" + for line in example.source.splitlines(): + lines.append("??? %s %s" % (indent, line)) + indent = "..." + if isinstance(failure, doctest.DocTestFailure): + lines += checker.output_difference( + example, failure.got, report_choice + ).split( + "\n" + ) + else: + inner_excinfo = ExceptionInfo(failure.exc_info) + lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)] + lines += traceback.format_exception(*failure.exc_info) + reprlocation_lines.append((reprlocation, lines)) + return ReprFailDoctest(reprlocation_lines) + else: + return super(DoctestItem, self).repr_failure(excinfo) + + def reportinfo(self): + return self.fspath, self.dtest.lineno, "[doctest] %s" % self.name + + +def _get_flag_lookup(): + import doctest + + return dict( + DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1, + DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE, + NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE, + ELLIPSIS=doctest.ELLIPSIS, + IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL, + COMPARISON_FLAGS=doctest.COMPARISON_FLAGS, + ALLOW_UNICODE=_get_allow_unicode_flag(), + ALLOW_BYTES=_get_allow_bytes_flag(), + ) + + +def get_optionflags(parent): + optionflags_str = parent.config.getini("doctest_optionflags") + flag_lookup_table = _get_flag_lookup() + flag_acc = 0 + for flag in optionflags_str: + flag_acc |= flag_lookup_table[flag] + return flag_acc + + +def _get_continue_on_failure(config): + continue_on_failure = config.getvalue("doctest_continue_on_failure") + if continue_on_failure: + # We need to turn off this if we use pdb since we should stop at + # the first failure + if config.getvalue("usepdb"): + continue_on_failure = False + return continue_on_failure + + +class DoctestTextfile(pytest.Module): + obj = None + + def collect(self): + import doctest + + # inspired by doctest.testfile; ideally we would use it directly, + # but it doesn't support passing a custom checker + encoding = self.config.getini("doctest_encoding") + text = self.fspath.read_text(encoding) + filename = str(self.fspath) + name = self.fspath.basename + globs = {"__name__": "__main__"} + + optionflags = get_optionflags(self) + + runner = _get_runner( + verbose=0, + optionflags=optionflags, + checker=_get_checker(), + continue_on_failure=_get_continue_on_failure(self.config), + ) + _fix_spoof_python2(runner, encoding) + + parser = doctest.DocTestParser() + test = parser.get_doctest(text, globs, name, filename, 0) + if test.examples: + yield DoctestItem(test.name, self, runner, test) + + +def _check_all_skipped(test): + """raises pytest.skip() if all examples in the given DocTest have the SKIP + option set. + """ + import doctest + + all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples) + if all_skipped: + pytest.skip("all tests skipped by +SKIP option") + + +class DoctestModule(pytest.Module): + + def collect(self): + import doctest + + if self.fspath.basename == "conftest.py": + module = self.config.pluginmanager._importconftest(self.fspath) + else: + try: + module = self.fspath.pyimport() + except ImportError: + if self.config.getvalue("doctest_ignore_import_errors"): + pytest.skip("unable to import module %r" % self.fspath) + else: + raise + # uses internal doctest module parsing mechanism + finder = doctest.DocTestFinder() + optionflags = get_optionflags(self) + runner = _get_runner( + verbose=0, + optionflags=optionflags, + checker=_get_checker(), + continue_on_failure=_get_continue_on_failure(self.config), + ) + + for test in finder.find(module, module.__name__): + if test.examples: # skip empty doctests + yield DoctestItem(test.name, self, runner, test) + + +def _setup_fixtures(doctest_item): + """ + Used by DoctestTextfile and DoctestItem to setup fixture information. + """ + + def func(): + pass + + doctest_item.funcargs = {} + fm = doctest_item.session._fixturemanager + doctest_item._fixtureinfo = fm.getfixtureinfo( + node=doctest_item, func=func, cls=None, funcargs=False + ) + fixture_request = FixtureRequest(doctest_item) + fixture_request._fillfixtures() + return fixture_request + + +def _get_checker(): + """ + Returns a doctest.OutputChecker subclass that takes in account the + ALLOW_UNICODE option to ignore u'' prefixes in strings and ALLOW_BYTES + to strip b'' prefixes. + Useful when the same doctest should run in Python 2 and Python 3. + + An inner class is used to avoid importing "doctest" at the module + level. + """ + if hasattr(_get_checker, "LiteralsOutputChecker"): + return _get_checker.LiteralsOutputChecker() + + import doctest + import re + + class LiteralsOutputChecker(doctest.OutputChecker): + """ + Copied from doctest_nose_plugin.py from the nltk project: + https://github.com/nltk/nltk + + Further extended to also support byte literals. + """ + + _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE) + _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE) + + def check_output(self, want, got, optionflags): + res = doctest.OutputChecker.check_output(self, want, got, optionflags) + if res: + return True + + allow_unicode = optionflags & _get_allow_unicode_flag() + allow_bytes = optionflags & _get_allow_bytes_flag() + if not allow_unicode and not allow_bytes: + return False + + else: # pragma: no cover + + def remove_prefixes(regex, txt): + return re.sub(regex, r"\1\2", txt) + + if allow_unicode: + want = remove_prefixes(self._unicode_literal_re, want) + got = remove_prefixes(self._unicode_literal_re, got) + if allow_bytes: + want = remove_prefixes(self._bytes_literal_re, want) + got = remove_prefixes(self._bytes_literal_re, got) + res = doctest.OutputChecker.check_output(self, want, got, optionflags) + return res + + _get_checker.LiteralsOutputChecker = LiteralsOutputChecker + return _get_checker.LiteralsOutputChecker() + + +def _get_allow_unicode_flag(): + """ + Registers and returns the ALLOW_UNICODE flag. + """ + import doctest + + return doctest.register_optionflag("ALLOW_UNICODE") + + +def _get_allow_bytes_flag(): + """ + Registers and returns the ALLOW_BYTES flag. + """ + import doctest + + return doctest.register_optionflag("ALLOW_BYTES") + + +def _get_report_choice(key): + """ + This function returns the actual `doctest` module flag value, we want to do it as late as possible to avoid + importing `doctest` and all its dependencies when parsing options, as it adds overhead and breaks tests. + """ + import doctest + + return { + DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF, + DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF, + DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF, + DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE, + DOCTEST_REPORT_CHOICE_NONE: 0, + }[ + key + ] + + +def _fix_spoof_python2(runner, encoding): + """ + Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output. This + should patch only doctests for text files because they don't have a way to declare their + encoding. Doctests in docstrings from Python modules don't have the same problem given that + Python already decoded the strings. + + This fixes the problem related in issue #2434. + """ + from _pytest.compat import _PY2 + + if not _PY2: + return + + from doctest import _SpoofOut + + class UnicodeSpoof(_SpoofOut): + + def getvalue(self): + result = _SpoofOut.getvalue(self) + if encoding and isinstance(result, bytes): + result = result.decode(encoding) + return result + + runner._fakeout = UnicodeSpoof() + + +@pytest.fixture(scope="session") +def doctest_namespace(): + """ + Fixture that returns a :py:class:`dict` that will be injected into the namespace of doctests. + """ + return dict() diff --git a/third_party/python/pytest/_pytest/fixtures.py b/third_party/python/pytest/src/_pytest/fixtures.py similarity index 78% rename from third_party/python/pytest/_pytest/fixtures.py rename to third_party/python/pytest/src/_pytest/fixtures.py index 8858a40e9111..495e6b9b332a 100644 --- a/third_party/python/pytest/_pytest/fixtures.py +++ b/third_party/python/pytest/src/_pytest/fixtures.py @@ -1,9 +1,13 @@ from __future__ import absolute_import, division, print_function +import functools import inspect import sys import warnings +from collections import OrderedDict, deque, defaultdict +from more_itertools import flatten +import attr import py from py._code.code import FormattedExcinfo @@ -11,29 +15,42 @@ import _pytest from _pytest import nodes from _pytest._code.code import TerminalRepr from _pytest.compat import ( - NOTSET, exc_clear, _format_args, - getfslineno, get_real_func, - is_generator, isclass, getimfunc, - getlocation, getfuncargnames, + NOTSET, + exc_clear, + _format_args, + getfslineno, + get_real_func, + is_generator, + isclass, + getimfunc, + getlocation, + getfuncargnames, safe_getattr, FuncargnamesCompatAttr, ) from _pytest.outcomes import fail, TEST_OUTCOME +FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}' -if sys.version_info[:2] == (2, 6): - from ordereddict import OrderedDict -else: - from collections import OrderedDict + +@attr.s(frozen=True) +class PseudoFixtureDef(object): + cached_result = attr.ib() + scope = attr.ib() def pytest_sessionstart(session): import _pytest.python - scopename2class.update({ - 'class': _pytest.python.Class, - 'module': _pytest.python.Module, - 'function': _pytest.main.Item, - }) + import _pytest.nodes + + scopename2class.update( + { + "class": _pytest.python.Class, + "module": _pytest.python.Module, + "function": _pytest.nodes.Item, + "session": _pytest.main.Session, + } + ) session._fixturemanager = FixtureManager(session) @@ -43,29 +60,30 @@ scopename2class = {} scope2props = dict(session=()) scope2props["module"] = ("fspath", "module") scope2props["class"] = scope2props["module"] + ("cls",) -scope2props["instance"] = scope2props["class"] + ("instance", ) +scope2props["instance"] = scope2props["class"] + ("instance",) scope2props["function"] = scope2props["instance"] + ("function", "keywords") def scopeproperty(name=None, doc=None): + def decoratescope(func): scopename = name or func.__name__ def provide(self): if func.__name__ in scope2props[self.scope]: return func(self) - raise AttributeError("%s not available in %s-scoped context" % ( - scopename, self.scope)) + raise AttributeError( + "%s not available in %s-scoped context" % (scopename, self.scope) + ) return property(provide, None, None, func.__doc__) + return decoratescope def get_scope_node(node, scope): cls = scopename2class.get(scope) if cls is None: - if scope == "session": - return node.session raise ValueError("unknown scope") return node.getparent(cls) @@ -90,8 +108,7 @@ def add_funcarg_pseudo_fixture_def(collector, metafunc, fixturemanager): callspec.indices[argname] = len(arg2params_list) arg2params_list.append(argvalue) if argname not in arg2scope: - scopenum = callspec._arg2scopenum.get(argname, - scopenum_function) + scopenum = callspec._arg2scopenum.get(argname, scopenum_function) arg2scope[argname] = scopes[scopenum] callspec.funcargs.clear() @@ -114,10 +131,16 @@ def add_funcarg_pseudo_fixture_def(collector, metafunc, fixturemanager): if node and argname in node._name2pseudofixturedef: arg2fixturedefs[argname] = [node._name2pseudofixturedef[argname]] else: - fixturedef = FixtureDef(fixturemanager, '', argname, - get_direct_param_fixture_func, - arg2scope[argname], - valuelist, False, False) + fixturedef = FixtureDef( + fixturemanager, + "", + argname, + get_direct_param_fixture_func, + arg2scope[argname], + valuelist, + False, + False, + ) arg2fixturedefs[argname] = [fixturedef] if node is not None: node._name2pseudofixturedef[argname] = fixturedef @@ -149,7 +172,7 @@ def get_parametrized_fixture_keys(item, scopenum): for argname, param_index in sorted(cs.indices.items()): if cs._arg2scopenum[argname] != scopenum: continue - if scopenum == 0: # session + if scopenum == 0: # session key = (argname, param_index) elif scopenum == 1: # module key = (argname, param_index, item.fspath) @@ -163,64 +186,67 @@ def get_parametrized_fixture_keys(item, scopenum): # down to the lower scopes such as to minimize number of "high scope" # setups and teardowns + def reorder_items(items): argkeys_cache = {} + items_by_argkey = {} for scopenum in range(0, scopenum_function): argkeys_cache[scopenum] = d = {} + items_by_argkey[scopenum] = item_d = defaultdict(deque) for item in items: keys = OrderedDict.fromkeys(get_parametrized_fixture_keys(item, scopenum)) if keys: d[item] = keys - return reorder_items_atscope(items, set(), argkeys_cache, 0) + for key in keys: + item_d[key].append(item) + items = OrderedDict.fromkeys(items) + return list(reorder_items_atscope(items, argkeys_cache, items_by_argkey, 0)) -def reorder_items_atscope(items, ignore, argkeys_cache, scopenum): +def fix_cache_order(item, argkeys_cache, items_by_argkey): + for scopenum in range(0, scopenum_function): + for key in argkeys_cache[scopenum].get(item, []): + items_by_argkey[scopenum][key].appendleft(item) + + +def reorder_items_atscope(items, argkeys_cache, items_by_argkey, scopenum): if scopenum >= scopenum_function or len(items) < 3: return items - items_done = [] - while 1: - items_before, items_same, items_other, newignore = \ - slice_items(items, ignore, argkeys_cache[scopenum]) - items_before = reorder_items_atscope( - items_before, ignore, argkeys_cache, scopenum + 1) - if items_same is None: - # nothing to reorder in this scope - assert items_other is None - return items_done + items_before - items_done.extend(items_before) - items = items_same + items_other - ignore = newignore - - -def slice_items(items, ignore, scoped_argkeys_cache): - # we pick the first item which uses a fixture instance in the - # requested scope and which we haven't seen yet. We slice the input - # items list into a list of items_nomatch, items_same and - # items_other - if scoped_argkeys_cache: # do we need to do work at all? - it = iter(items) - # first find a slicing key - for i, item in enumerate(it): - argkeys = scoped_argkeys_cache.get(item) - if argkeys is not None: - newargkeys = OrderedDict.fromkeys(k for k in argkeys if k not in ignore) - if newargkeys: # found a slicing key - slicing_argkey, _ = newargkeys.popitem() - items_before = items[:i] - items_same = [item] - items_other = [] - # now slice the remainder of the list - for item in it: - argkeys = scoped_argkeys_cache.get(item) - if argkeys and slicing_argkey in argkeys and \ - slicing_argkey not in ignore: - items_same.append(item) - else: - items_other.append(item) - newignore = ignore.copy() - newignore.add(slicing_argkey) - return (items_before, items_same, items_other, newignore) - return items, None, None, None + ignore = set() + items_deque = deque(items) + items_done = OrderedDict() + scoped_items_by_argkey = items_by_argkey[scopenum] + scoped_argkeys_cache = argkeys_cache[scopenum] + while items_deque: + no_argkey_group = OrderedDict() + slicing_argkey = None + while items_deque: + item = items_deque.popleft() + if item in items_done or item in no_argkey_group: + continue + argkeys = OrderedDict.fromkeys( + k for k in scoped_argkeys_cache.get(item, []) if k not in ignore + ) + if not argkeys: + no_argkey_group[item] = None + else: + slicing_argkey, _ = argkeys.popitem() + # we don't have to remove relevant items from later in the deque because they'll just be ignored + matching_items = [ + i for i in scoped_items_by_argkey[slicing_argkey] if i in items + ] + for i in reversed(matching_items): + fix_cache_order(i, argkeys_cache, items_by_argkey) + items_deque.appendleft(i) + break + if no_argkey_group: + no_argkey_group = reorder_items_atscope( + no_argkey_group, argkeys_cache, items_by_argkey, scopenum + 1 + ) + for item in no_argkey_group: + items_done[item] = None + ignore.add(slicing_argkey) + return items_done def fillfixtures(function): @@ -249,7 +275,8 @@ def get_direct_param_fixture_func(request): return request.param -class FuncFixtureInfo: +class FuncFixtureInfo(object): + def __init__(self, argnames, names_closure, name2fixturedefs): self.argnames = argnames self.names_closure = names_closure @@ -270,7 +297,6 @@ class FixtureRequest(FuncargnamesCompatAttr): self.fixturename = None #: Scope string, one of "function", "class", "module", "session" self.scope = "function" - self._fixture_values = {} # argname -> fixture value self._fixture_defs = {} # argname -> FixtureDef fixtureinfo = pyfuncitem._fixtureinfo self._arg2fixturedefs = fixtureinfo.name2fixturedefs.copy() @@ -290,7 +316,7 @@ class FixtureRequest(FuncargnamesCompatAttr): def _getnextfixturedef(self, argname): fixturedefs = self._arg2fixturedefs.get(argname, None) if fixturedefs is None: - # we arrive here because of a a dynamic call to + # we arrive here because of a dynamic call to # getfixturevalue(argname) usage which was naturally # not known at parsing/collection time parentid = self._pyfuncitem.parent.nodeid @@ -328,8 +354,7 @@ class FixtureRequest(FuncargnamesCompatAttr): return self._pyfuncitem._testcase except AttributeError: function = getattr(self, "function", None) - if function is not None: - return py.builtin._getimself(function) + return getattr(function, "__self__", None) @scopeproperty() def module(self): @@ -361,7 +386,8 @@ class FixtureRequest(FuncargnamesCompatAttr): def _addfinalizer(self, finalizer, scope): colitem = self._getscopeitem(scope) self._pyfuncitem.session._setupstate.addfinalizer( - finalizer=finalizer, colitem=colitem) + finalizer=finalizer, colitem=colitem + ) def applymarker(self, marker): """ Apply a marker to a single test function invocation. @@ -371,10 +397,7 @@ class FixtureRequest(FuncargnamesCompatAttr): :arg marker: a :py:class:`_pytest.mark.MarkDecorator` object created by a call to ``pytest.mark.NAME(...)``. """ - try: - self.node.keywords[marker.markname] = marker - except AttributeError: - raise ValueError(marker) + self.node.add_marker(marker) def raiseerror(self, msg): """ raise a FixtureLookupError with the given message. """ @@ -402,7 +425,7 @@ class FixtureRequest(FuncargnamesCompatAttr): or ``session`` indicating the caching lifecycle of the resource. :arg extrakey: added to internal caching key of (funcargname, scope). """ - if not hasattr(self.config, '_setupcache'): + if not hasattr(self.config, "_setupcache"): self.config._setupcache = {} # XXX weakref? cachekey = (self.fixturename, self._getscopeitem(scope), extrakey) cache = self.config._setupcache @@ -413,9 +436,11 @@ class FixtureRequest(FuncargnamesCompatAttr): val = setup() cache[cachekey] = val if teardown is not None: + def finalizer(): del cache[cachekey] teardown(val) + self._addfinalizer(finalizer, scope=scope) return val @@ -432,10 +457,8 @@ class FixtureRequest(FuncargnamesCompatAttr): def getfuncargvalue(self, argname): """ Deprecated, use getfixturevalue. """ from _pytest import deprecated - warnings.warn( - deprecated.GETFUNCARGVALUE, - DeprecationWarning, - stacklevel=2) + + warnings.warn(deprecated.GETFUNCARGVALUE, DeprecationWarning, stacklevel=2) return self.getfixturevalue(argname) def _get_active_fixturedef(self, argname): @@ -446,15 +469,13 @@ class FixtureRequest(FuncargnamesCompatAttr): fixturedef = self._getnextfixturedef(argname) except FixtureLookupError: if argname == "request": - class PseudoFixtureDef: - cached_result = (self, [0], None) - scope = "function" - return PseudoFixtureDef + cached_result = (self, [0], None) + scope = "function" + return PseudoFixtureDef(cached_result, scope) raise # remove indent to prevent the python3 exception # from leaking into the call - result = self._getfixturevalue(fixturedef) - self._fixture_values[argname] = result + self._compute_fixture_value(fixturedef) self._fixture_defs[argname] = fixturedef return fixturedef @@ -469,7 +490,14 @@ class FixtureRequest(FuncargnamesCompatAttr): values.append(fixturedef) current = current._parent_request - def _getfixturevalue(self, fixturedef): + def _compute_fixture_value(self, fixturedef): + """ + Creates a SubRequest based on "self" and calls the execute method of the given fixturedef object. This will + force the FixtureDef object to throw away any previous results and compute a new fixture value, which + will be stored into the FixtureDef object itself. + + :param FixtureDef fixturedef: + """ # prepare a subrequest object before calling fixture function # (latter managed by fixturedef) argname = fixturedef.argname @@ -490,8 +518,8 @@ class FixtureRequest(FuncargnamesCompatAttr): source_path = source_path.relto(funcitem.config.rootdir) msg = ( "The requested fixture has no parameter defined for the " - "current test.\n\nRequested fixture '{0}' defined in:\n{1}" - "\n\nRequested here:\n{2}:{3}".format( + "current test.\n\nRequested fixture '{}' defined in:\n{}" + "\n\nRequested here:\n{}:{}".format( fixturedef.argname, getlocation(fixturedef.func, funcitem.config.rootdir), source_path, @@ -518,12 +546,13 @@ class FixtureRequest(FuncargnamesCompatAttr): exc_clear() try: # call the fixture function - val = fixturedef.execute(request=subrequest) + fixturedef.execute(request=subrequest) finally: # if fixture function failed it might have registered finalizers - self.session._setupstate.addfinalizer(fixturedef.finish, - subrequest.node) - return val + self.session._setupstate.addfinalizer( + functools.partial(fixturedef.finish, request=subrequest), + subrequest.node, + ) def _check_scope(self, argname, invoking_scope, requested_scope): if argname == "request": @@ -531,11 +560,13 @@ class FixtureRequest(FuncargnamesCompatAttr): if scopemismatch(invoking_scope, requested_scope): # try to report something helpful lines = self._factorytraceback() - fail("ScopeMismatch: You tried to access the %r scoped " - "fixture %r with a %r scoped request object, " - "involved factories\n%s" % ( - (requested_scope, argname, invoking_scope, "\n".join(lines))), - pytrace=False) + fail( + "ScopeMismatch: You tried to access the %r scoped " + "fixture %r with a %r scoped request object, " + "involved factories\n%s" + % ((requested_scope, argname, invoking_scope, "\n".join(lines))), + pytrace=False, + ) def _factorytraceback(self): lines = [] @@ -544,8 +575,7 @@ class FixtureRequest(FuncargnamesCompatAttr): fs, lineno = getfslineno(factory) p = self._pyfuncitem.session.fspath.bestrelpath(fs) args = _format_args(factory) - lines.append("%s:%d: def %s%s" % ( - p, lineno, factory.__name__, args)) + lines.append("%s:%d: def %s%s" % (p, lineno, factory.__name__, args)) return lines def _getscopeitem(self, scope): @@ -556,7 +586,9 @@ class FixtureRequest(FuncargnamesCompatAttr): if node is None and scope == "class": # fallback to function item itself node = self._pyfuncitem - assert node + assert node, 'Could not obtain a node for scope "{}" for function {!r}'.format( + scope, self._pyfuncitem + ) return node def __repr__(self): @@ -576,7 +608,6 @@ class SubRequest(FixtureRequest): self.scope = scope self._fixturedef = fixturedef self._pyfuncitem = request._pyfuncitem - self._fixture_values = request._fixture_values self._fixture_defs = request._fixture_defs self._arg2fixturedefs = request._arg2fixturedefs self._arg2index = request._arg2index @@ -611,9 +642,9 @@ def scope2index(scope, descr, where=None): return scopes.index(scope) except ValueError: raise ValueError( - "{0} {1}has an unsupported scope value '{2}'".format( - descr, 'from {0} '.format(where) if where else '', - scope) + "{} {}has an unsupported scope value '{}'".format( + descr, "from {} ".format(where) if where else "", scope + ) ) @@ -648,7 +679,7 @@ class FixtureLookupError(LookupError): for i, line in enumerate(lines): line = line.rstrip() addline(" " + line) - if line.lstrip().startswith('def'): + if line.lstrip().startswith("def"): break if msg is None: @@ -667,6 +698,7 @@ class FixtureLookupError(LookupError): class FixtureLookupErrorRepr(TerminalRepr): + def __init__(self, filename, firstlineno, tblines, errorstring, argname): self.tblines = tblines self.errorstring = errorstring @@ -680,11 +712,15 @@ class FixtureLookupErrorRepr(TerminalRepr): tw.line(tbline.rstrip()) lines = self.errorstring.split("\n") if lines: - tw.line('{0} {1}'.format(FormattedExcinfo.fail_marker, - lines[0].strip()), red=True) + tw.line( + "{} {}".format(FormattedExcinfo.fail_marker, lines[0].strip()), + red=True, + ) for line in lines[1:]: - tw.line('{0} {1}'.format(FormattedExcinfo.flow_marker, - line.strip()), red=True) + tw.line( + "{} {}".format(FormattedExcinfo.flow_marker, line.strip()), + red=True, + ) tw.line() tw.line("%s:%d" % (self.filename, self.firstlineno + 1)) @@ -693,8 +729,7 @@ def fail_fixturefunc(fixturefunc, msg): fs, lineno = getfslineno(fixturefunc) location = "%s:%s" % (fs, lineno + 1) source = _pytest._code.Source(fixturefunc) - fail(msg + ":\n\n" + str(source.indent()) + "\n" + location, - pytrace=False) + fail(msg + ":\n\n" + str(source.indent()) + "\n" + location, pytrace=False) def call_fixture_func(fixturefunc, request, kwargs): @@ -709,8 +744,9 @@ def call_fixture_func(fixturefunc, request, kwargs): except StopIteration: pass else: - fail_fixturefunc(fixturefunc, - "yield_fixture function has more than one 'yield'") + fail_fixturefunc( + fixturefunc, "yield_fixture function has more than one 'yield'" + ) request.addfinalizer(teardown) else: @@ -718,38 +754,44 @@ def call_fixture_func(fixturefunc, request, kwargs): return res -class FixtureDef: +class FixtureDef(object): """ A container for a factory definition. """ - def __init__(self, fixturemanager, baseid, argname, func, scope, params, - unittest=False, ids=None): + def __init__( + self, + fixturemanager, + baseid, + argname, + func, + scope, + params, + unittest=False, + ids=None, + ): self._fixturemanager = fixturemanager - self.baseid = baseid or '' + self.baseid = baseid or "" self.has_location = baseid is not None self.func = func self.argname = argname self.scope = scope self.scopenum = scope2index( - scope or "function", - descr='fixture {0}'.format(func.__name__), - where=baseid + scope or "function", descr="fixture {}".format(func.__name__), where=baseid ) self.params = params - startindex = unittest and 1 or None - self.argnames = getfuncargnames(func, startindex=startindex) + self.argnames = getfuncargnames(func, is_method=unittest) self.unittest = unittest self.ids = ids - self._finalizer = [] + self._finalizers = [] def addfinalizer(self, finalizer): - self._finalizer.append(finalizer) + self._finalizers.append(finalizer) - def finish(self): + def finish(self, request): exceptions = [] try: - while self._finalizer: + while self._finalizers: try: - func = self._finalizer.pop() + func = self._finalizers.pop() func() except: # noqa exceptions.append(sys.exc_info()) @@ -759,12 +801,15 @@ class FixtureDef: py.builtin._reraise(*e) finally: - ihook = self._fixturemanager.session.ihook - ihook.pytest_fixture_post_finalizer(fixturedef=self) + hook = self._fixturemanager.session.gethookproxy(request.node.fspath) + hook.pytest_fixture_post_finalizer(fixturedef=self, request=request) # even if finalization fails, we invalidate - # the cached fixture value + # the cached fixture value and remove + # all finalizers because they may be bound methods which will + # keep instances alive if hasattr(self, "cached_result"): del self.cached_result + self._finalizers = [] def execute(self, request): # get required arguments and register our own finish() @@ -772,7 +817,7 @@ class FixtureDef: for argname in self.argnames: fixturedef = request._get_active_fixturedef(argname) if argname != "request": - fixturedef.addfinalizer(self.finish) + fixturedef.addfinalizer(functools.partial(self.finish, request=request)) my_cache_key = request.param_index cached_result = getattr(self, "cached_result", None) @@ -785,15 +830,17 @@ class FixtureDef: return result # we have a previous but differently parametrized fixture instance # so we need to tear it down before creating a new one - self.finish() + self.finish(request) assert not hasattr(self, "cached_result") - ihook = self._fixturemanager.session.ihook - return ihook.pytest_fixture_setup(fixturedef=self, request=request) + hook = self._fixturemanager.session.gethookproxy(request.node.fspath) + return hook.pytest_fixture_setup(fixturedef=self, request=request) def __repr__(self): - return ("" % - (self.argname, self.scope, self.baseid)) + return ( + "" + % (self.argname, self.scope, self.baseid) + ) def pytest_fixture_setup(fixturedef, request): @@ -828,24 +875,37 @@ def pytest_fixture_setup(fixturedef, request): return result -class FixtureFunctionMarker: - def __init__(self, scope, params, autouse=False, ids=None, name=None): - self.scope = scope - self.params = params - self.autouse = autouse - self.ids = ids - self.name = name +def _ensure_immutable_ids(ids): + if ids is None: + return + if callable(ids): + return ids + return tuple(ids) + + +@attr.s(frozen=True) +class FixtureFunctionMarker(object): + scope = attr.ib() + params = attr.ib(converter=attr.converters.optional(tuple)) + autouse = attr.ib(default=False) + ids = attr.ib(default=None, converter=_ensure_immutable_ids) + name = attr.ib(default=None) def __call__(self, function): if isclass(function): + raise ValueError("class fixtures not supported (may be in the future)") + + if getattr(function, "_pytestfixturefunction", False): raise ValueError( - "class fixtures not supported (may be in the future)") + "fixture is being applied more than once to the same function" + ) + function._pytestfixturefunction = self return function def fixture(scope="function", params=None, autouse=False, ids=None, name=None): - """ (return a) decorator to mark a fixture factory function. + """Decorator to mark a fixture factory function. This decorator can be used (with or without parameters) to define a fixture function. The name of the fixture function can later be @@ -884,8 +944,7 @@ def fixture(scope="function", params=None, autouse=False, ids=None, name=None): """ if callable(scope) and params is None and autouse is False: # direct decoration - return FixtureFunctionMarker( - "function", params, autouse, name=name)(scope) + return FixtureFunctionMarker("function", params, autouse, name=name)(scope) if params is not None and not isinstance(params, (list, tuple)): params = list(params) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) @@ -899,8 +958,9 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N """ if callable(scope) and params is None and not autouse: # direct decoration - return FixtureFunctionMarker( - "function", params, autouse, ids=ids, name=name)(scope) + return FixtureFunctionMarker("function", params, autouse, ids=ids, name=name)( + scope + ) else: return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) @@ -910,11 +970,19 @@ defaultfuncargprefixmarker = fixture() @fixture(scope="session") def pytestconfig(request): - """ the pytest config object with access to command line opts.""" + """Session-scoped fixture that returns the :class:`_pytest.config.Config` object. + + Example:: + + def test_foo(pytestconfig): + if pytestconfig.getoption("verbose"): + ... + + """ return request.config -class FixtureManager: +class FixtureManager(object): """ pytest fixtures definitions and information is stored and managed from this class. @@ -960,17 +1028,17 @@ class FixtureManager: session.config.pluginmanager.register(self, "funcmanage") def getfixtureinfo(self, node, func, cls, funcargs=True): - if funcargs and not hasattr(node, "nofuncargs"): + if funcargs and not getattr(node, "nofuncargs", False): argnames = getfuncargnames(func, cls=cls) else: argnames = () - usefixtures = getattr(func, "usefixtures", None) + usefixtures = flatten( + mark.args for mark in node.iter_markers(name="usefixtures") + ) initialnames = argnames - if usefixtures is not None: - initialnames = usefixtures.args + initialnames + initialnames = tuple(usefixtures) + initialnames fm = node.session._fixturemanager - names_closure, arg2fixturedefs = fm.getfixtureclosure(initialnames, - node) + names_closure, arg2fixturedefs = fm.getfixtureclosure(initialnames, node) return FuncFixtureInfo(argnames, names_closure, arg2fixturedefs) def pytest_plugin_registered(self, plugin): @@ -1000,15 +1068,12 @@ class FixtureManager: if nextchar and nextchar not in ":/": continue autousenames.extend(basenames) - # make sure autousenames are sorted by scope, scopenum 0 is session - autousenames.sort( - key=lambda x: self._arg2fixturedefs[x][-1].scopenum) return autousenames def getfixtureclosure(self, fixturenames, parentnode): # collect the closure of all fixtures , starting with the given # fixturenames as the initial set. As we have to visit all - # factory definitions anyway, we also return a arg2fixturedefs + # factory definitions anyway, we also return an arg2fixturedefs # mapping so that the caller can reuse it and does not have # to re-discover fixturedefs again for each fixturename # (discovering matching fixtures for a given name/node is expensive) @@ -1033,6 +1098,16 @@ class FixtureManager: if fixturedefs: arg2fixturedefs[argname] = fixturedefs merge(fixturedefs[-1].argnames) + + def sort_by_scope(arg_name): + try: + fixturedefs = arg2fixturedefs[arg_name] + except KeyError: + return scopes.index("function") + else: + return fixturedefs[-1].scopenum + + fixturenames_closure.sort(key=sort_by_scope) return fixturenames_closure, arg2fixturedefs def pytest_generate_tests(self, metafunc): @@ -1041,9 +1116,11 @@ class FixtureManager: if faclist: fixturedef = faclist[-1] if fixturedef.params is not None: - parametrize_func = getattr(metafunc.function, 'parametrize', None) - func_params = getattr(parametrize_func, 'args', [[None]]) - func_kwargs = getattr(parametrize_func, 'kwargs', {}) + parametrize_func = getattr(metafunc.function, "parametrize", None) + if parametrize_func is not None: + parametrize_func = parametrize_func.combined + func_params = getattr(parametrize_func, "args", [[None]]) + func_kwargs = getattr(parametrize_func, "kwargs", {}) # skip directly parametrized arguments if "argnames" in func_kwargs: argnames = parametrize_func.kwargs["argnames"] @@ -1052,9 +1129,13 @@ class FixtureManager: if not isinstance(argnames, (tuple, list)): argnames = [x.strip() for x in argnames.split(",") if x.strip()] if argname not in func_params and argname not in argnames: - metafunc.parametrize(argname, fixturedef.params, - indirect=True, scope=fixturedef.scope, - ids=fixturedef.ids) + metafunc.parametrize( + argname, + fixturedef.params, + indirect=True, + scope=fixturedef.scope, + ids=fixturedef.ids, + ) else: continue # will raise FixtureLookupError at setup time @@ -1086,7 +1167,10 @@ class FixtureManager: continue marker = defaultfuncargprefixmarker from _pytest import deprecated - self.config.warn('C1', deprecated.FUNCARG_PREFIX.format(name=name), nodeid=nodeid) + + self.config.warn( + "C1", deprecated.FUNCARG_PREFIX.format(name=name), nodeid=nodeid + ) name = name[len(self._argprefix):] elif not isinstance(marker, FixtureFunctionMarker): # magic globals with __getattr__ might have got us a wrong @@ -1095,13 +1179,18 @@ class FixtureManager: else: if marker.name: name = marker.name - msg = 'fixtures cannot have "pytest_funcarg__" prefix ' \ - 'and be decorated with @pytest.fixture:\n%s' % name - assert not name.startswith(self._argprefix), msg + assert not name.startswith(self._argprefix), FIXTURE_MSG.format(name) - fixture_def = FixtureDef(self, nodeid, name, obj, - marker.scope, marker.params, - unittest=unittest, ids=marker.ids) + fixture_def = FixtureDef( + self, + nodeid, + name, + obj, + marker.scope, + marker.params, + unittest=unittest, + ids=marker.ids, + ) faclist = self._arg2fixturedefs.setdefault(name, []) if fixture_def.has_location: @@ -1117,7 +1206,7 @@ class FixtureManager: autousenames.append(name) if autousenames: - self._nodeid_and_autousenames.append((nodeid or '', autousenames)) + self._nodeid_and_autousenames.append((nodeid or "", autousenames)) def getfixturedefs(self, argname, nodeid): """ diff --git a/third_party/python/pytest/_pytest/freeze_support.py b/third_party/python/pytest/src/_pytest/freeze_support.py similarity index 83% rename from third_party/python/pytest/_pytest/freeze_support.py rename to third_party/python/pytest/src/_pytest/freeze_support.py index 97147a88250a..002e077301f9 100644 --- a/third_party/python/pytest/_pytest/freeze_support.py +++ b/third_party/python/pytest/src/_pytest/freeze_support.py @@ -7,17 +7,18 @@ from __future__ import absolute_import, division, print_function def freeze_includes(): """ - Returns a list of module names used by py.test that should be + Returns a list of module names used by pytest that should be included by cx_freeze. """ import py import _pytest + result = list(_iter_all_modules(py)) result += list(_iter_all_modules(_pytest)) return result -def _iter_all_modules(package, prefix=''): +def _iter_all_modules(package, prefix=""): """ Iterates over the names of all modules that can be found in the given package, recursively. @@ -31,13 +32,14 @@ def _iter_all_modules(package, prefix=''): """ import os import pkgutil + if type(package) is not str: - path, prefix = package.__path__[0], package.__name__ + '.' + path, prefix = package.__path__[0], package.__name__ + "." else: path = package for _, name, is_package in pkgutil.iter_modules([path]): if is_package: - for m in _iter_all_modules(os.path.join(path, name), prefix=name + '.'): + for m in _iter_all_modules(os.path.join(path, name), prefix=name + "."): yield prefix + m else: yield prefix + name diff --git a/third_party/python/pytest/_pytest/helpconfig.py b/third_party/python/pytest/src/_pytest/helpconfig.py similarity index 61% rename from third_party/python/pytest/_pytest/helpconfig.py rename to third_party/python/pytest/src/_pytest/helpconfig.py index e744637f8660..5514fec404da 100644 --- a/third_party/python/pytest/_pytest/helpconfig.py +++ b/third_party/python/pytest/src/_pytest/helpconfig.py @@ -18,48 +18,69 @@ class HelpAction(Action): implemented by raising SystemExit. """ - def __init__(self, - option_strings, - dest=None, - default=False, - help=None): + def __init__(self, option_strings, dest=None, default=False, help=None): super(HelpAction, self).__init__( option_strings=option_strings, dest=dest, const=True, default=default, nargs=0, - help=help) + help=help, + ) def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, self.const) # We should only skip the rest of the parsing after preparse is done - if getattr(parser._parser, 'after_preparse', False): + if getattr(parser._parser, "after_preparse", False): raise PrintHelp def pytest_addoption(parser): - group = parser.getgroup('debugconfig') - group.addoption('--version', action="store_true", - help="display pytest lib version and import information.") - group._addoption("-h", "--help", action=HelpAction, dest="help", - help="show help message and configuration info") - group._addoption('-p', action="append", dest="plugins", default=[], - metavar="name", - help="early-load given plugin (multi-allowed). " - "To avoid loading of plugins, use the `no:` prefix, e.g. " - "`no:doctest`.") - group.addoption('--traceconfig', '--trace-config', - action="store_true", default=False, - help="trace considerations of conftest.py files."), - group.addoption('--debug', - action="store_true", dest="debug", default=False, - help="store internal tracing debug information in 'pytestdebug.log'.") + group = parser.getgroup("debugconfig") + group.addoption( + "--version", + action="store_true", + help="display pytest lib version and import information.", + ) group._addoption( - '-o', '--override-ini', nargs='*', dest="override_ini", + "-h", + "--help", + action=HelpAction, + dest="help", + help="show help message and configuration info", + ) + group._addoption( + "-p", action="append", - help="override config option with option=value style, e.g. `-o xfail_strict=True`.") + dest="plugins", + default=[], + metavar="name", + help="early-load given plugin (multi-allowed). " + "To avoid loading of plugins, use the `no:` prefix, e.g. " + "`no:doctest`.", + ) + group.addoption( + "--traceconfig", + "--trace-config", + action="store_true", + default=False, + help="trace considerations of conftest.py files.", + ), + group.addoption( + "--debug", + action="store_true", + dest="debug", + default=False, + help="store internal tracing debug information in 'pytestdebug.log'.", + ) + group._addoption( + "-o", + "--override-ini", + dest="override_ini", + action="append", + help='override ini option with "option=value" style, e.g. `-o xfail_strict=True -o cache_dir=cache`.', + ) @pytest.hookimpl(hookwrapper=True) @@ -68,20 +89,25 @@ def pytest_cmdline_parse(): config = outcome.get_result() if config.option.debug: path = os.path.abspath("pytestdebug.log") - debugfile = open(path, 'w') - debugfile.write("versions pytest-%s, py-%s, " - "python-%s\ncwd=%s\nargs=%s\n\n" % ( - pytest.__version__, py.__version__, - ".".join(map(str, sys.version_info)), - os.getcwd(), config._origargs)) + debugfile = open(path, "w") + debugfile.write( + "versions pytest-%s, py-%s, " + "python-%s\ncwd=%s\nargs=%s\n\n" + % ( + pytest.__version__, + py.__version__, + ".".join(map(str, sys.version_info)), + os.getcwd(), + config._origargs, + ) + ) config.trace.root.setwriter(debugfile.write) undo_tracing = config.pluginmanager.enable_tracing() sys.stderr.write("writing pytestdebug information to %s\n" % path) def unset_tracing(): debugfile.close() - sys.stderr.write("wrote pytestdebug information to %s\n" % - debugfile.name) + sys.stderr.write("wrote pytestdebug information to %s\n" % debugfile.name) config.trace.root.setwriter(None) undo_tracing() @@ -91,8 +117,9 @@ def pytest_cmdline_parse(): def pytest_cmdline_main(config): if config.option.version: p = py.path.local(pytest.__file__) - sys.stderr.write("This is pytest version %s, imported from %s\n" % - (pytest.__version__, p)) + sys.stderr.write( + "This is pytest version %s, imported from %s\n" % (pytest.__version__, p) + ) plugininfo = getpluginversioninfo(config) if plugininfo: for line in plugininfo: @@ -106,13 +133,14 @@ def pytest_cmdline_main(config): def showhelp(config): - reporter = config.pluginmanager.get_plugin('terminalreporter') + reporter = config.pluginmanager.get_plugin("terminalreporter") tw = reporter._tw tw.write(config._parser.optparser.format_help()) tw.line() tw.line() - tw.line("[pytest] ini-options in the first " - "pytest.ini|tox.ini|setup.cfg file found:") + tw.line( + "[pytest] ini-options in the first " "pytest.ini|tox.ini|setup.cfg file found:" + ) tw.line() for name in config._parser._ininames: @@ -128,7 +156,7 @@ def showhelp(config): vars = [ ("PYTEST_ADDOPTS", "extra command line options"), ("PYTEST_PLUGINS", "comma-separated plugins to load during startup"), - ("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals") + ("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals"), ] for name, help in vars: tw.line(" %-24s %s" % (name, help)) @@ -137,17 +165,18 @@ def showhelp(config): tw.line("to see available markers type: pytest --markers") tw.line("to see available fixtures type: pytest --fixtures") - tw.line("(shown according to specified file_or_dir or current dir " - "if not specified)") + tw.line( + "(shown according to specified file_or_dir or current dir " + "if not specified; fixtures with leading '_' are only shown " + "with the '-v' option" + ) - for warningreport in reporter.stats.get('warnings', []): + for warningreport in reporter.stats.get("warnings", []): tw.line("warning : " + warningreport.message, red=True) return -conftest_options = [ - ('pytest_plugins', 'list of plugin names to load'), -] +conftest_options = [("pytest_plugins", "list of plugin names to load")] def getpluginversioninfo(config): @@ -156,7 +185,7 @@ def getpluginversioninfo(config): if plugininfo: lines.append("setuptools registered plugins:") for plugin, dist in plugininfo: - loc = getattr(plugin, '__file__', repr(plugin)) + loc = getattr(plugin, "__file__", repr(plugin)) content = "%s-%s at %s" % (dist.project_name, dist.version, loc) lines.append(" " + content) return lines @@ -165,8 +194,7 @@ def getpluginversioninfo(config): def pytest_report_header(config): lines = [] if config.option.debug or config.option.traceconfig: - lines.append("using: pytest-%s pylib-%s" % - (pytest.__version__, py.__version__)) + lines.append("using: pytest-%s pylib-%s" % (pytest.__version__, py.__version__)) verinfo = getpluginversioninfo(config) if verinfo: @@ -176,7 +204,7 @@ def pytest_report_header(config): lines.append("active plugins:") items = config.pluginmanager.list_name_plugin() for name, plugin in items: - if hasattr(plugin, '__file__'): + if hasattr(plugin, "__file__"): r = plugin.__file__ else: r = repr(plugin) diff --git a/third_party/python/pytest/_pytest/hookspec.py b/third_party/python/pytest/src/_pytest/hookspec.py similarity index 66% rename from third_party/python/pytest/_pytest/hookspec.py rename to third_party/python/pytest/src/_pytest/hookspec.py index e5c966e58bd9..fec43a400991 100644 --- a/third_party/python/pytest/_pytest/hookspec.py +++ b/third_party/python/pytest/src/_pytest/hookspec.py @@ -1,6 +1,6 @@ """ hook specifications for pytest plugins, invoked from main.py and builtin plugins. """ -from _pytest._pluggy import HookspecMarker +from pluggy import HookspecMarker hookspec = HookspecMarker("pytest") @@ -12,22 +12,40 @@ hookspec = HookspecMarker("pytest") @hookspec(historic=True) def pytest_addhooks(pluginmanager): """called at plugin registration time to allow adding new hooks via a call to - pluginmanager.add_hookspecs(module_or_class, prefix).""" + ``pluginmanager.add_hookspecs(module_or_class, prefix)``. + + + :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager + + .. note:: + This hook is incompatible with ``hookwrapper=True``. + """ @hookspec(historic=True) def pytest_namespace(): """ - DEPRECATED: this hook causes direct monkeypatching on pytest, its use is strongly discouraged + (**Deprecated**) this hook causes direct monkeypatching on pytest, its use is strongly discouraged return dict of name->object to be made globally available in - the pytest namespace. This hook is called at plugin registration - time. + the pytest namespace. + + This hook is called at plugin registration time. + + .. note:: + This hook is incompatible with ``hookwrapper=True``. """ @hookspec(historic=True) def pytest_plugin_registered(plugin, manager): - """ a new pytest plugin got registered. """ + """ a new pytest plugin got registered. + + :param plugin: the plugin module or instance + :param _pytest.config.PytestPluginManager manager: pytest plugin manager + + .. note:: + This hook is incompatible with ``hookwrapper=True``. + """ @hookspec(historic=True) @@ -41,7 +59,7 @@ def pytest_addoption(parser): files situated at the tests root directory due to how pytest :ref:`discovers plugins during startup `. - :arg parser: To add command line options, call + :arg _pytest.config.Parser parser: To add command line options, call :py:func:`parser.addoption(...) <_pytest.config.Parser.addoption>`. To add ini-file values call :py:func:`parser.addini(...) <_pytest.config.Parser.addini>`. @@ -56,8 +74,10 @@ def pytest_addoption(parser): a value read from an ini-style file. The config object is passed around on many internal objects via the ``.config`` - attribute or can be retrieved as the ``pytestconfig`` fixture or accessed - via (deprecated) ``pytest.config``. + attribute or can be retrieved as the ``pytestconfig`` fixture. + + .. note:: + This hook is incompatible with ``hookwrapper=True``. """ @@ -72,14 +92,16 @@ def pytest_configure(config): After that, the hook is called for other conftest files as they are imported. - :arg config: pytest config object - :type config: _pytest.config.Config + .. note:: + This hook is incompatible with ``hookwrapper=True``. + + :arg _pytest.config.Config config: pytest config object """ + # ------------------------------------------------------------------------- # Bootstrapping hooks called for plugins registered early enough: -# internal and 3rd party plugins as well as directly -# discoverable conftest.py local plugins. +# internal and 3rd party plugins. # ------------------------------------------------------------------------- @@ -87,11 +109,28 @@ def pytest_configure(config): def pytest_cmdline_parse(pluginmanager, args): """return initialized config object, parsing the specified args. - Stops at first non-None result, see :ref:`firstresult` """ + Stops at first non-None result, see :ref:`firstresult` + + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + + :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager + :param list[str] args: list of arguments passed on the command line + """ def pytest_cmdline_preparse(config, args): - """(deprecated) modify command line arguments before option parsing. """ + """(**Deprecated**) modify command line arguments before option parsing. + + This hook is considered deprecated and will be removed in a future pytest version. Consider + using :func:`pytest_load_initial_conftests` instead. + + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + + :param _pytest.config.Config config: pytest config object + :param list[str] args: list of arguments passed on the command line + """ @hookspec(firstresult=True) @@ -99,32 +138,58 @@ def pytest_cmdline_main(config): """ called for performing the main command line action. The default implementation will invoke the configure hooks and runtest_mainloop. - Stops at first non-None result, see :ref:`firstresult` """ + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + + Stops at first non-None result, see :ref:`firstresult` + + :param _pytest.config.Config config: pytest config object + """ def pytest_load_initial_conftests(early_config, parser, args): """ implements the loading of initial conftest files ahead - of command line option parsing. """ + of command line option parsing. + + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + + :param _pytest.config.Config early_config: pytest config object + :param list[str] args: list of arguments passed on the command line + :param _pytest.config.Parser parser: to add command line options + """ # ------------------------------------------------------------------------- # collection hooks # ------------------------------------------------------------------------- + @hookspec(firstresult=True) def pytest_collection(session): - """ perform the collection protocol for the given session. + """Perform the collection protocol for the given session. - Stops at first non-None result, see :ref:`firstresult` """ + Stops at first non-None result, see :ref:`firstresult`. + + :param _pytest.main.Session session: the pytest session object + """ def pytest_collection_modifyitems(session, config, items): """ called after collection has been performed, may filter or re-order - the items in-place.""" + the items in-place. + + :param _pytest.main.Session session: the pytest session object + :param _pytest.config.Config config: pytest config object + :param List[_pytest.nodes.Item] items: list of item objects + """ def pytest_collection_finish(session): - """ called after collection has been performed and modified. """ + """ called after collection has been performed and modified. + + :param _pytest.main.Session session: the pytest session object + """ @hookspec(firstresult=True) @@ -134,6 +199,9 @@ def pytest_ignore_collect(path, config): more specific hooks. Stops at first non-None result, see :ref:`firstresult` + + :param str path: the path to analyze + :param _pytest.config.Config config: pytest config object """ @@ -141,12 +209,19 @@ def pytest_ignore_collect(path, config): def pytest_collect_directory(path, parent): """ called before traversing a directory for collection files. - Stops at first non-None result, see :ref:`firstresult` """ + Stops at first non-None result, see :ref:`firstresult` + + :param str path: the path to analyze + """ def pytest_collect_file(path, parent): """ return collection Node or None for the given path. Any new node - needs to have the specified ``parent`` as a parent.""" + needs to have the specified ``parent`` as a parent. + + :param str path: the path to collect + """ + # logging hooks for collection @@ -173,6 +248,7 @@ def pytest_make_collect_report(collector): Stops at first non-None result, see :ref:`firstresult` """ + # ------------------------------------------------------------------------- # Python test function related hooks # ------------------------------------------------------------------------- @@ -212,7 +288,13 @@ def pytest_make_parametrize_id(config, val, argname): by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``. The parameter name is available as ``argname``, if required. - Stops at first non-None result, see :ref:`firstresult` """ + Stops at first non-None result, see :ref:`firstresult` + + :param _pytest.config.Config config: pytest config object + :param val: the parametrized value + :param str argname: the automatic parameter name produced by pytest + """ + # ------------------------------------------------------------------------- # generic runtest related hooks @@ -224,11 +306,14 @@ def pytest_runtestloop(session): """ called for performing the main runtest loop (after collection finished). - Stops at first non-None result, see :ref:`firstresult` """ + Stops at first non-None result, see :ref:`firstresult` + + :param _pytest.main.Session session: the pytest session object + """ def pytest_itemstart(item, node): - """ (deprecated, use pytest_runtest_logstart). """ + """(**Deprecated**) use pytest_runtest_logstart. """ @hookspec(firstresult=True) @@ -250,7 +335,25 @@ def pytest_runtest_protocol(item, nextitem): def pytest_runtest_logstart(nodeid, location): - """ signal the start of running a single test item. """ + """ signal the start of running a single test item. + + This hook will be called **before** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and + :func:`pytest_runtest_teardown` hooks. + + :param str nodeid: full id of the item + :param location: a triple of ``(filename, linenum, testname)`` + """ + + +def pytest_runtest_logfinish(nodeid, location): + """ signal the complete finish of running a single test item. + + This hook will be called **after** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and + :func:`pytest_runtest_teardown` hooks. + + :param str nodeid: full id of the item + :param location: a triple of ``(filename, linenum, testname)`` + """ def pytest_runtest_setup(item): @@ -284,6 +387,7 @@ def pytest_runtest_logreport(report): """ process a test setup/call/teardown report relating to the respective phase of executing a test. """ + # ------------------------------------------------------------------------- # Fixture related hooks # ------------------------------------------------------------------------- @@ -293,35 +397,56 @@ def pytest_runtest_logreport(report): def pytest_fixture_setup(fixturedef, request): """ performs fixture setup execution. - Stops at first non-None result, see :ref:`firstresult` """ + :return: The return value of the call to the fixture function + + Stops at first non-None result, see :ref:`firstresult` + + .. note:: + If the fixture function returns None, other implementations of + this hook function will continue to be called, according to the + behavior of the :ref:`firstresult` option. + """ -def pytest_fixture_post_finalizer(fixturedef): +def pytest_fixture_post_finalizer(fixturedef, request): """ called after fixture teardown, but before the cache is cleared so the fixture result cache ``fixturedef.cached_result`` can still be accessed.""" + # ------------------------------------------------------------------------- # test session related hooks # ------------------------------------------------------------------------- def pytest_sessionstart(session): - """ before session.main() is called. """ + """ called after the ``Session`` object has been created and before performing collection + and entering the run test loop. + + :param _pytest.main.Session session: the pytest session object + """ def pytest_sessionfinish(session, exitstatus): - """ whole test run finishes. """ + """ called after whole test run finished, right before returning the exit status to the system. + + :param _pytest.main.Session session: the pytest session object + :param int exitstatus: the status which pytest will return to the system + """ def pytest_unconfigure(config): - """ called before test process is exited. """ + """ called before test process is exited. + + :param _pytest.config.Config config: pytest config object + """ # ------------------------------------------------------------------------- # hooks for customizing the assert methods # ------------------------------------------------------------------------- + def pytest_assertrepr_compare(config, op, left, right): """return explanation for comparisons in failing assert expressions. @@ -329,8 +454,11 @@ def pytest_assertrepr_compare(config, op, left, right): of strings. The strings will be joined by newlines but any newlines *in* a string will be escaped. Note that all but the first line will be indented slightly, the intention is for the first line to be a summary. + + :param _pytest.config.Config config: pytest config object """ + # ------------------------------------------------------------------------- # hooks for influencing reporting (invoked from _pytest_terminal) # ------------------------------------------------------------------------- @@ -339,7 +467,7 @@ def pytest_assertrepr_compare(config, op, left, right): def pytest_report_header(config, startdir): """ return a string or list of strings to be displayed as header info for terminal reporting. - :param config: the pytest config object. + :param _pytest.config.Config config: pytest config object :param startdir: py.path object with the starting dir .. note:: @@ -358,7 +486,7 @@ def pytest_report_collectionfinish(config, startdir, items): This strings will be displayed after the standard "collected X items" message. - :param config: the pytest config object. + :param _pytest.config.Config config: pytest config object :param startdir: py.path object with the starting dir :param items: list of pytest items that are going to be executed; this list should not be modified. """ @@ -372,14 +500,26 @@ def pytest_report_teststatus(report): def pytest_terminal_summary(terminalreporter, exitstatus): - """ add additional section in terminal summary reporting. """ + """Add a section to terminal summary reporting. + + :param _pytest.terminal.TerminalReporter terminalreporter: the internal terminal reporter object + :param int exitstatus: the exit status that will be reported back to the OS + + .. versionadded:: 3.5 + The ``config`` parameter. + """ @hookspec(historic=True) def pytest_logwarning(message, code, nodeid, fslocation): """ process a warning specified by a message, a code string, a nodeid and fslocation (both of which may be None - if the warning is not tied to a partilar node/location).""" + if the warning is not tied to a particular node/location). + + .. note:: + This hook is incompatible with ``hookwrapper=True``. + """ + # ------------------------------------------------------------------------- # doctest hooks @@ -392,6 +532,7 @@ def pytest_doctest_prepare_content(content): Stops at first non-None result, see :ref:`firstresult` """ + # ------------------------------------------------------------------------- # error handling and internal debugging hooks # ------------------------------------------------------------------------- @@ -418,6 +559,5 @@ def pytest_enter_pdb(config): """ called upon pdb.set_trace(), can be used by plugins to take special action just before the python debugger enters in interactive mode. - :arg config: pytest config object - :type config: _pytest.config.Config + :param _pytest.config.Config config: pytest config object """ diff --git a/third_party/python/pytest/_pytest/junitxml.py b/third_party/python/pytest/src/_pytest/junitxml.py similarity index 59% rename from third_party/python/pytest/_pytest/junitxml.py rename to third_party/python/pytest/src/_pytest/junitxml.py index 7fb40dc35487..29da27de7ccf 100644 --- a/third_party/python/pytest/_pytest/junitxml.py +++ b/third_party/python/pytest/src/_pytest/junitxml.py @@ -39,15 +39,14 @@ class Junit(py.xml.Namespace): # chars is: Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] # | [#x10000-#x10FFFF] _legal_chars = (0x09, 0x0A, 0x0d) -_legal_ranges = ( - (0x20, 0x7E), (0x80, 0xD7FF), (0xE000, 0xFFFD), (0x10000, 0x10FFFF), -) +_legal_ranges = ((0x20, 0x7E), (0x80, 0xD7FF), (0xE000, 0xFFFD), (0x10000, 0x10FFFF)) _legal_xml_re = [ unicode("%s-%s") % (unichr(low), unichr(high)) - for (low, high) in _legal_ranges if low < sys.maxunicode + for (low, high) in _legal_ranges + if low < sys.maxunicode ] _legal_xml_re = [unichr(x) for x in _legal_chars] + _legal_xml_re -illegal_xml_re = re.compile(unicode('[^%s]') % unicode('').join(_legal_xml_re)) +illegal_xml_re = re.compile(unicode("[^%s]") % unicode("").join(_legal_xml_re)) del _legal_chars del _legal_ranges del _legal_xml_re @@ -56,17 +55,19 @@ _py_ext_re = re.compile(r"\.py$") def bin_xml_escape(arg): + def repl(matchobj): i = ord(matchobj.group()) if i <= 0xFF: - return unicode('#x%02X') % i + return unicode("#x%02X") % i else: - return unicode('#x%04X') % i + return unicode("#x%04X") % i return py.xml.raw(illegal_xml_re.sub(repl, py.xml.escape(arg))) class _NodeReporter(object): + def __init__(self, nodeid, xml): self.id = nodeid @@ -85,19 +86,25 @@ class _NodeReporter(object): def add_property(self, name, value): self.properties.append((str(name), bin_xml_escape(value))) + def add_attribute(self, name, value): + self.attrs[str(name)] = bin_xml_escape(value) + def make_properties_node(self): """Return a Junit node containing custom properties, if any. """ if self.properties: - return Junit.properties([ - Junit.property(name=name, value=value) - for name, value in self.properties - ]) - return '' + return Junit.properties( + [ + Junit.property(name=name, value=value) + for name, value in self.properties + ] + ) + return "" def record_testreport(self, testreport): assert not self.testcase names = mangle_test_address(testreport.nodeid) + existing_attrs = self.attrs classnames = names[:-1] if self.xml.prefix: classnames.insert(0, self.xml.prefix) @@ -111,6 +118,7 @@ class _NodeReporter(object): if hasattr(testreport, "url"): attrs["url"] = testreport.url self.attrs = attrs + self.attrs.update(existing_attrs) # restore any user-defined attributes def to_xml(self): testcase = Junit.testcase(time=self.duration, **self.attrs) @@ -125,21 +133,62 @@ class _NodeReporter(object): self.append(node) def write_captured_output(self, report): - for capname in ('out', 'err'): - content = getattr(report, 'capstd' + capname) + content_out = report.capstdout + content_log = report.caplog + content_err = report.capstderr + + if content_log or content_out: + if content_log and self.xml.logging == "system-out": + if content_out: + # syncing stdout and the log-output is not done yet. It's + # probably not worth the effort. Therefore, first the captured + # stdout is shown and then the captured logs. + content = "\n".join( + [ + " Captured Stdout ".center(80, "-"), + content_out, + "", + " Captured Log ".center(80, "-"), + content_log, + ] + ) + else: + content = content_log + else: + content = content_out + if content: - tag = getattr(Junit, 'system-' + capname) + tag = getattr(Junit, "system-out") + self.append(tag(bin_xml_escape(content))) + + if content_log or content_err: + if content_log and self.xml.logging == "system-err": + if content_err: + content = "\n".join( + [ + " Captured Stderr ".center(80, "-"), + content_err, + "", + " Captured Log ".center(80, "-"), + content_log, + ] + ) + else: + content = content_log + else: + content = content_err + + if content: + tag = getattr(Junit, "system-err") self.append(tag(bin_xml_escape(content))) def append_pass(self, report): - self.add_stats('passed') + self.add_stats("passed") def append_failure(self, report): # msg = str(report.longrepr.reprtraceback.extraline) if hasattr(report, "wasxfail"): - self._add_simple( - Junit.skipped, - "xfail-marked test passes unexpectedly") + self._add_simple(Junit.skipped, "xfail-marked test passes unexpectedly") else: if hasattr(report.longrepr, "reprcrash"): message = report.longrepr.reprcrash.message @@ -154,35 +203,35 @@ class _NodeReporter(object): def append_collect_error(self, report): # msg = str(report.longrepr.reprtraceback.extraline) - self.append(Junit.error(bin_xml_escape(report.longrepr), - message="collection failure")) + self.append( + Junit.error(bin_xml_escape(report.longrepr), message="collection failure") + ) def append_collect_skipped(self, report): - self._add_simple( - Junit.skipped, "collection skipped", report.longrepr) + self._add_simple(Junit.skipped, "collection skipped", report.longrepr) def append_error(self, report): - if getattr(report, 'when', None) == 'teardown': + if getattr(report, "when", None) == "teardown": msg = "test teardown failure" else: msg = "test setup failure" - self._add_simple( - Junit.error, msg, report.longrepr) + self._add_simple(Junit.error, msg, report.longrepr) def append_skipped(self, report): if hasattr(report, "wasxfail"): - self._add_simple( - Junit.skipped, "expected test failure", report.wasxfail - ) + self._add_simple(Junit.skipped, "expected test failure", report.wasxfail) else: filename, lineno, skipreason = report.longrepr if skipreason.startswith("Skipped: "): skipreason = bin_xml_escape(skipreason[9:]) self.append( - Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason), - type="pytest.skip", - message=skipreason)) - self.write_captured_output(report) + Junit.skipped( + "%s:%s: %s" % (filename, lineno, skipreason), + type="pytest.skip", + message=skipreason, + ) + ) + self.write_captured_output(report) def finalize(self): data = self.to_xml().unicode(indent=0) @@ -191,69 +240,117 @@ class _NodeReporter(object): @pytest.fixture -def record_xml_property(request): - """Add extra xml properties to the tag for the calling test. +def record_property(request): + """Add an extra properties the calling test. + User properties become part of the test report and are available to the + configured reporters, like JUnit XML. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded. + + Example:: + + def test_function(record_property): + record_property("example_key", 1) + """ + + def append_property(name, value): + request.node.user_properties.append((name, value)) + + return append_property + + +@pytest.fixture +def record_xml_property(record_property): + """(Deprecated) use record_property.""" + import warnings + from _pytest import deprecated + + warnings.warn(deprecated.RECORD_XML_PROPERTY, DeprecationWarning, stacklevel=2) + + return record_property + + +@pytest.fixture +def record_xml_attribute(request): + """Add extra xml attributes to the tag for the calling test. + The fixture is callable with ``(name, value)``, with value being + automatically xml-encoded """ request.node.warn( - code='C3', - message='record_xml_property is an experimental feature', + code="C3", message="record_xml_attribute is an experimental feature" ) xml = getattr(request.config, "_xml", None) if xml is not None: node_reporter = xml.node_reporter(request.node.nodeid) - return node_reporter.add_property + return node_reporter.add_attribute else: - def add_property_noop(name, value): + + def add_attr_noop(name, value): pass - return add_property_noop + return add_attr_noop def pytest_addoption(parser): group = parser.getgroup("terminal reporting") group.addoption( - '--junitxml', '--junit-xml', + "--junitxml", + "--junit-xml", action="store", dest="xmlpath", metavar="path", type=functools.partial(filename_arg, optname="--junitxml"), default=None, - help="create junit-xml style report file at given path.") + help="create junit-xml style report file at given path.", + ) group.addoption( - '--junitprefix', '--junit-prefix', + "--junitprefix", + "--junit-prefix", action="store", metavar="str", default=None, - help="prepend prefix to classnames in junit-xml output") - parser.addini("junit_suite_name", "Test suite name for JUnit report", default="pytest") + help="prepend prefix to classnames in junit-xml output", + ) + parser.addini( + "junit_suite_name", "Test suite name for JUnit report", default="pytest" + ) + parser.addini( + "junit_logging", + "Write captured log messages to JUnit report: " + "one of no|system-out|system-err", + default="no", + ) # choices=['no', 'stdout', 'stderr']) def pytest_configure(config): xmlpath = config.option.xmlpath # prevent opening xmllog on slave nodes (xdist) - if xmlpath and not hasattr(config, 'slaveinput'): - config._xml = LogXML(xmlpath, config.option.junitprefix, config.getini("junit_suite_name")) + if xmlpath and not hasattr(config, "slaveinput"): + config._xml = LogXML( + xmlpath, + config.option.junitprefix, + config.getini("junit_suite_name"), + config.getini("junit_logging"), + ) config.pluginmanager.register(config._xml) def pytest_unconfigure(config): - xml = getattr(config, '_xml', None) + xml = getattr(config, "_xml", None) if xml: del config._xml config.pluginmanager.unregister(xml) def mangle_test_address(address): - path, possible_open_bracket, params = address.partition('[') + path, possible_open_bracket, params = address.partition("[") names = path.split("::") try: - names.remove('()') + names.remove("()") except ValueError: pass # convert file path to dotted path - names[0] = names[0].replace(nodes.SEP, '.') + names[0] = names[0].replace(nodes.SEP, ".") names[0] = _py_ext_re.sub("", names[0]) # put any params back names[-1] += possible_open_bracket + params @@ -261,17 +358,14 @@ def mangle_test_address(address): class LogXML(object): - def __init__(self, logfile, prefix, suite_name="pytest"): + + def __init__(self, logfile, prefix, suite_name="pytest", logging="no"): logfile = os.path.expanduser(os.path.expandvars(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix self.suite_name = suite_name - self.stats = dict.fromkeys([ - 'error', - 'passed', - 'failure', - 'skipped', - ], 0) + self.logging = logging + self.stats = dict.fromkeys(["error", "passed", "failure", "skipped"], 0) self.node_reporters = {} # nodeid -> _NodeReporter self.node_reporters_ordered = [] self.global_properties = [] @@ -280,17 +374,17 @@ class LogXML(object): self.cnt_double_fail_tests = 0 def finalize(self, report): - nodeid = getattr(report, 'nodeid', report) + nodeid = getattr(report, "nodeid", report) # local hack to handle xdist report order - slavenode = getattr(report, 'node', None) + slavenode = getattr(report, "node", None) reporter = self.node_reporters.pop((nodeid, slavenode)) if reporter is not None: reporter.finalize() def node_reporter(self, report): - nodeid = getattr(report, 'nodeid', report) + nodeid = getattr(report, "nodeid", report) # local hack to handle xdist report order - slavenode = getattr(report, 'node', None) + slavenode = getattr(report, "node", None) key = nodeid, slavenode @@ -348,12 +442,17 @@ class LogXML(object): report_wid = getattr(report, "worker_id", None) report_ii = getattr(report, "item_index", None) close_report = next( - (rep for rep in self.open_reports - if (rep.nodeid == report.nodeid and - getattr(rep, "item_index", None) == report_ii and - getattr(rep, "worker_id", None) == report_wid - ) - ), None) + ( + rep + for rep in self.open_reports + if ( + rep.nodeid == report.nodeid + and getattr(rep, "item_index", None) == report_ii + and getattr(rep, "worker_id", None) == report_wid + ) + ), + None, + ) if close_report: # We need to open new testcase in case we have failure in # call and error in teardown in order to follow junit @@ -373,16 +472,25 @@ class LogXML(object): if report.when == "teardown": reporter = self._opentestcase(report) reporter.write_captured_output(report) + + for propname, propvalue in report.user_properties: + reporter.add_property(propname, propvalue) + self.finalize(report) report_wid = getattr(report, "worker_id", None) report_ii = getattr(report, "item_index", None) close_report = next( - (rep for rep in self.open_reports - if (rep.nodeid == report.nodeid and - getattr(rep, "item_index", None) == report_ii and - getattr(rep, "worker_id", None) == report_wid - ) - ), None) + ( + rep + for rep in self.open_reports + if ( + rep.nodeid == report.nodeid + and getattr(rep, "item_index", None) == report_ii + and getattr(rep, "worker_id", None) == report_wid + ) + ), + None, + ) if close_report: self.open_reports.remove(close_report) @@ -391,7 +499,7 @@ class LogXML(object): the Junit.testcase with the new total if already created. """ reporter = self.node_reporter(report) - reporter.duration += getattr(report, 'duration', 0.0) + reporter.duration += getattr(report, "duration", 0.0) def pytest_collectreport(self, report): if not report.passed: @@ -402,9 +510,9 @@ class LogXML(object): reporter.append_collect_skipped(report) def pytest_internalerror(self, excrepr): - reporter = self.node_reporter('internal') - reporter.attrs.update(classname="pytest", name='internal') - reporter._add_simple(Junit.error, 'internal error', excrepr) + reporter = self.node_reporter("internal") + reporter.attrs.update(classname="pytest", name="internal") + reporter._add_simple(Junit.error, "internal error", excrepr) def pytest_sessionstart(self): self.suite_start_time = time.time() @@ -413,29 +521,37 @@ class LogXML(object): dirname = os.path.dirname(os.path.abspath(self.logfile)) if not os.path.isdir(dirname): os.makedirs(dirname) - logfile = open(self.logfile, 'w', encoding='utf-8') + logfile = open(self.logfile, "w", encoding="utf-8") suite_stop_time = time.time() suite_time_delta = suite_stop_time - self.suite_start_time - numtests = (self.stats['passed'] + self.stats['failure'] + - self.stats['skipped'] + self.stats['error'] - - self.cnt_double_fail_tests) + numtests = ( + self.stats["passed"] + + self.stats["failure"] + + self.stats["skipped"] + + self.stats["error"] + - self.cnt_double_fail_tests + ) logfile.write('') - logfile.write(Junit.testsuite( - self._get_global_properties_node(), - [x.to_xml() for x in self.node_reporters_ordered], - name=self.suite_name, - errors=self.stats['error'], - failures=self.stats['failure'], - skips=self.stats['skipped'], - tests=numtests, - time="%.3f" % suite_time_delta, ).unicode(indent=0)) + logfile.write( + Junit.testsuite( + self._get_global_properties_node(), + [x.to_xml() for x in self.node_reporters_ordered], + name=self.suite_name, + errors=self.stats["error"], + failures=self.stats["failure"], + skips=self.stats["skipped"], + tests=numtests, + time="%.3f" % suite_time_delta, + ).unicode( + indent=0 + ) + ) logfile.close() def pytest_terminal_summary(self, terminalreporter): - terminalreporter.write_sep("-", - "generated xml file: %s" % (self.logfile)) + terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) def add_global_property(self, name, value): self.global_properties.append((str(name), bin_xml_escape(value))) @@ -450,4 +566,4 @@ class LogXML(object): for name, value in self.global_properties ] ) - return '' + return "" diff --git a/third_party/python/pytest/src/_pytest/logging.py b/third_party/python/pytest/src/_pytest/logging.py new file mode 100644 index 000000000000..00bb9aeb54a7 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/logging.py @@ -0,0 +1,575 @@ +""" Access and control log capturing. """ +from __future__ import absolute_import, division, print_function + +import logging +from contextlib import closing, contextmanager +import re +import six + +from _pytest.config import create_terminal_writer +import pytest +import py + + +DEFAULT_LOG_FORMAT = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s" +DEFAULT_LOG_DATE_FORMAT = "%H:%M:%S" + + +class ColoredLevelFormatter(logging.Formatter): + """ + Colorize the %(levelname)..s part of the log format passed to __init__. + """ + + LOGLEVEL_COLOROPTS = { + logging.CRITICAL: {"red"}, + logging.ERROR: {"red", "bold"}, + logging.WARNING: {"yellow"}, + logging.WARN: {"yellow"}, + logging.INFO: {"green"}, + logging.DEBUG: {"purple"}, + logging.NOTSET: set(), + } + LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-]?\d*s)") + + def __init__(self, terminalwriter, *args, **kwargs): + super(ColoredLevelFormatter, self).__init__(*args, **kwargs) + if six.PY2: + self._original_fmt = self._fmt + else: + self._original_fmt = self._style._fmt + self._level_to_fmt_mapping = {} + + levelname_fmt_match = self.LEVELNAME_FMT_REGEX.search(self._fmt) + if not levelname_fmt_match: + return + levelname_fmt = levelname_fmt_match.group() + + for level, color_opts in self.LOGLEVEL_COLOROPTS.items(): + formatted_levelname = levelname_fmt % { + "levelname": logging.getLevelName(level) + } + + # add ANSI escape sequences around the formatted levelname + color_kwargs = {name: True for name in color_opts} + colorized_formatted_levelname = terminalwriter.markup( + formatted_levelname, **color_kwargs + ) + self._level_to_fmt_mapping[level] = self.LEVELNAME_FMT_REGEX.sub( + colorized_formatted_levelname, self._fmt + ) + + def format(self, record): + fmt = self._level_to_fmt_mapping.get(record.levelno, self._original_fmt) + if six.PY2: + self._fmt = fmt + else: + self._style._fmt = fmt + return super(ColoredLevelFormatter, self).format(record) + + +def get_option_ini(config, *names): + for name in names: + ret = config.getoption(name) # 'default' arg won't work as expected + if ret is None: + ret = config.getini(name) + if ret: + return ret + + +def pytest_addoption(parser): + """Add options to control log capturing.""" + group = parser.getgroup("logging") + + def add_option_ini(option, dest, default=None, type=None, **kwargs): + parser.addini( + dest, default=default, type=type, help="default value for " + option + ) + group.addoption(option, dest=dest, **kwargs) + + add_option_ini( + "--no-print-logs", + dest="log_print", + action="store_const", + const=False, + default=True, + type="bool", + help="disable printing caught logs on failed tests.", + ) + add_option_ini( + "--log-level", + dest="log_level", + default=None, + help="logging level used by the logging module", + ) + add_option_ini( + "--log-format", + dest="log_format", + default=DEFAULT_LOG_FORMAT, + help="log format as used by the logging module.", + ) + add_option_ini( + "--log-date-format", + dest="log_date_format", + default=DEFAULT_LOG_DATE_FORMAT, + help="log date format as used by the logging module.", + ) + parser.addini( + "log_cli", + default=False, + type="bool", + help='enable log display during test run (also known as "live logging").', + ) + add_option_ini( + "--log-cli-level", dest="log_cli_level", default=None, help="cli logging level." + ) + add_option_ini( + "--log-cli-format", + dest="log_cli_format", + default=None, + help="log format as used by the logging module.", + ) + add_option_ini( + "--log-cli-date-format", + dest="log_cli_date_format", + default=None, + help="log date format as used by the logging module.", + ) + add_option_ini( + "--log-file", + dest="log_file", + default=None, + help="path to a file when logging will be written to.", + ) + add_option_ini( + "--log-file-level", + dest="log_file_level", + default=None, + help="log file logging level.", + ) + add_option_ini( + "--log-file-format", + dest="log_file_format", + default=DEFAULT_LOG_FORMAT, + help="log format as used by the logging module.", + ) + add_option_ini( + "--log-file-date-format", + dest="log_file_date_format", + default=DEFAULT_LOG_DATE_FORMAT, + help="log date format as used by the logging module.", + ) + + +@contextmanager +def catching_logs(handler, formatter=None, level=None): + """Context manager that prepares the whole logging machinery properly.""" + root_logger = logging.getLogger() + + if formatter is not None: + handler.setFormatter(formatter) + if level is not None: + handler.setLevel(level) + + # Adding the same handler twice would confuse logging system. + # Just don't do that. + add_new_handler = handler not in root_logger.handlers + + if add_new_handler: + root_logger.addHandler(handler) + if level is not None: + orig_level = root_logger.level + root_logger.setLevel(min(orig_level, level)) + try: + yield handler + finally: + if level is not None: + root_logger.setLevel(orig_level) + if add_new_handler: + root_logger.removeHandler(handler) + + +class LogCaptureHandler(logging.StreamHandler): + """A logging handler that stores log records and the log text.""" + + def __init__(self): + """Creates a new log handler.""" + logging.StreamHandler.__init__(self, py.io.TextIO()) + self.records = [] + + def emit(self, record): + """Keep the log records in a list in addition to the log text.""" + self.records.append(record) + logging.StreamHandler.emit(self, record) + + def reset(self): + self.records = [] + self.stream = py.io.TextIO() + + +class LogCaptureFixture(object): + """Provides access and control of log capturing.""" + + def __init__(self, item): + """Creates a new funcarg.""" + self._item = item + self._initial_log_levels = {} # type: Dict[str, int] # dict of log name -> log level + + def _finalize(self): + """Finalizes the fixture. + + This restores the log levels changed by :meth:`set_level`. + """ + # restore log levels + for logger_name, level in self._initial_log_levels.items(): + logger = logging.getLogger(logger_name) + logger.setLevel(level) + + @property + def handler(self): + """ + :rtype: LogCaptureHandler + """ + return self._item.catch_log_handler + + def get_records(self, when): + """ + Get the logging records for one of the possible test phases. + + :param str when: + Which test phase to obtain the records from. Valid values are: "setup", "call" and "teardown". + + :rtype: List[logging.LogRecord] + :return: the list of captured records at the given stage + + .. versionadded:: 3.4 + """ + handler = self._item.catch_log_handlers.get(when) + if handler: + return handler.records + else: + return [] + + @property + def text(self): + """Returns the log text.""" + return self.handler.stream.getvalue() + + @property + def records(self): + """Returns the list of log records.""" + return self.handler.records + + @property + def record_tuples(self): + """Returns a list of a striped down version of log records intended + for use in assertion comparison. + + The format of the tuple is: + + (logger_name, log_level, message) + """ + return [(r.name, r.levelno, r.getMessage()) for r in self.records] + + def clear(self): + """Reset the list of log records and the captured log text.""" + self.handler.reset() + + def set_level(self, level, logger=None): + """Sets the level for capturing of logs. The level will be restored to its previous value at the end of + the test. + + :param int level: the logger to level. + :param str logger: the logger to update the level. If not given, the root logger level is updated. + + .. versionchanged:: 3.4 + The levels of the loggers changed by this function will be restored to their initial values at the + end of the test. + """ + logger_name = logger + logger = logging.getLogger(logger_name) + # save the original log-level to restore it during teardown + self._initial_log_levels.setdefault(logger_name, logger.level) + logger.setLevel(level) + + @contextmanager + def at_level(self, level, logger=None): + """Context manager that sets the level for capturing of logs. After the end of the 'with' statement the + level is restored to its original value. + + :param int level: the logger to level. + :param str logger: the logger to update the level. If not given, the root logger level is updated. + """ + logger = logging.getLogger(logger) + orig_level = logger.level + logger.setLevel(level) + try: + yield + finally: + logger.setLevel(orig_level) + + +@pytest.fixture +def caplog(request): + """Access and control log capturing. + + Captured logs are available through the following methods:: + + * caplog.text -> string containing formatted log output + * caplog.records -> list of logging.LogRecord instances + * caplog.record_tuples -> list of (logger_name, level, message) tuples + * caplog.clear() -> clear captured records and formatted log output string + """ + result = LogCaptureFixture(request.node) + yield result + result._finalize() + + +def get_actual_log_level(config, *setting_names): + """Return the actual logging level.""" + + for setting_name in setting_names: + log_level = config.getoption(setting_name) + if log_level is None: + log_level = config.getini(setting_name) + if log_level: + break + else: + return + + if isinstance(log_level, six.string_types): + log_level = log_level.upper() + try: + return int(getattr(logging, log_level, log_level)) + except ValueError: + # Python logging does not recognise this as a logging level + raise pytest.UsageError( + "'{}' is not recognized as a logging level name for " + "'{}'. Please consider passing the " + "logging level num instead.".format(log_level, setting_name) + ) + + +def pytest_configure(config): + config.pluginmanager.register(LoggingPlugin(config), "logging-plugin") + + +@contextmanager +def _dummy_context_manager(): + yield + + +class LoggingPlugin(object): + """Attaches to the logging module and captures log messages for each test. + """ + + def __init__(self, config): + """Creates a new plugin to capture log messages. + + The formatter can be safely shared across all handlers so + create a single one for the entire test session here. + """ + self._config = config + + # enable verbose output automatically if live logging is enabled + if self._log_cli_enabled() and not config.getoption("verbose"): + # sanity check: terminal reporter should not have been loaded at this point + assert self._config.pluginmanager.get_plugin("terminalreporter") is None + config.option.verbose = 1 + + self.print_logs = get_option_ini(config, "log_print") + self.formatter = logging.Formatter( + get_option_ini(config, "log_format"), + get_option_ini(config, "log_date_format"), + ) + self.log_level = get_actual_log_level(config, "log_level") + + log_file = get_option_ini(config, "log_file") + if log_file: + self.log_file_level = get_actual_log_level(config, "log_file_level") + + log_file_format = get_option_ini(config, "log_file_format", "log_format") + log_file_date_format = get_option_ini( + config, "log_file_date_format", "log_date_format" + ) + # Each pytest runtests session will write to a clean logfile + self.log_file_handler = logging.FileHandler(log_file, mode="w") + log_file_formatter = logging.Formatter( + log_file_format, datefmt=log_file_date_format + ) + self.log_file_handler.setFormatter(log_file_formatter) + else: + self.log_file_handler = None + + # initialized during pytest_runtestloop + self.log_cli_handler = None + + def _log_cli_enabled(self): + """Return True if log_cli should be considered enabled, either explicitly + or because --log-cli-level was given in the command-line. + """ + return self._config.getoption( + "--log-cli-level" + ) is not None or self._config.getini( + "log_cli" + ) + + @contextmanager + def _runtest_for(self, item, when): + """Implements the internals of pytest_runtest_xxx() hook.""" + with catching_logs( + LogCaptureHandler(), formatter=self.formatter, level=self.log_level + ) as log_handler: + if self.log_cli_handler: + self.log_cli_handler.set_when(when) + + if item is None: + yield # run the test + return + + if not hasattr(item, "catch_log_handlers"): + item.catch_log_handlers = {} + item.catch_log_handlers[when] = log_handler + item.catch_log_handler = log_handler + try: + yield # run test + finally: + del item.catch_log_handler + if when == "teardown": + del item.catch_log_handlers + + if self.print_logs: + # Add a captured log section to the report. + log = log_handler.stream.getvalue().strip() + item.add_report_section(when, "log", log) + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_setup(self, item): + with self._runtest_for(item, "setup"): + yield + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_call(self, item): + with self._runtest_for(item, "call"): + yield + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_teardown(self, item): + with self._runtest_for(item, "teardown"): + yield + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_logstart(self): + if self.log_cli_handler: + self.log_cli_handler.reset() + with self._runtest_for(None, "start"): + yield + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_logfinish(self): + with self._runtest_for(None, "finish"): + yield + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtestloop(self, session): + """Runs all collected test items.""" + self._setup_cli_logging() + with self.live_logs_context: + if self.log_file_handler is not None: + with closing(self.log_file_handler): + with catching_logs( + self.log_file_handler, level=self.log_file_level + ): + yield # run all the tests + else: + yield # run all the tests + + def _setup_cli_logging(self): + """Sets up the handler and logger for the Live Logs feature, if enabled. + + This must be done right before starting the loop so we can access the terminal reporter plugin. + """ + terminal_reporter = self._config.pluginmanager.get_plugin("terminalreporter") + if self._log_cli_enabled() and terminal_reporter is not None: + capture_manager = self._config.pluginmanager.get_plugin("capturemanager") + log_cli_handler = _LiveLoggingStreamHandler( + terminal_reporter, capture_manager + ) + log_cli_format = get_option_ini( + self._config, "log_cli_format", "log_format" + ) + log_cli_date_format = get_option_ini( + self._config, "log_cli_date_format", "log_date_format" + ) + if ( + self._config.option.color != "no" + and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format) + ): + log_cli_formatter = ColoredLevelFormatter( + create_terminal_writer(self._config), + log_cli_format, + datefmt=log_cli_date_format, + ) + else: + log_cli_formatter = logging.Formatter( + log_cli_format, datefmt=log_cli_date_format + ) + log_cli_level = get_actual_log_level( + self._config, "log_cli_level", "log_level" + ) + self.log_cli_handler = log_cli_handler + self.live_logs_context = catching_logs( + log_cli_handler, formatter=log_cli_formatter, level=log_cli_level + ) + else: + self.live_logs_context = _dummy_context_manager() + + +class _LiveLoggingStreamHandler(logging.StreamHandler): + """ + Custom StreamHandler used by the live logging feature: it will write a newline before the first log message + in each test. + + During live logging we must also explicitly disable stdout/stderr capturing otherwise it will get captured + and won't appear in the terminal. + """ + + def __init__(self, terminal_reporter, capture_manager): + """ + :param _pytest.terminal.TerminalReporter terminal_reporter: + :param _pytest.capture.CaptureManager capture_manager: + """ + logging.StreamHandler.__init__(self, stream=terminal_reporter) + self.capture_manager = capture_manager + self.reset() + self.set_when(None) + self._test_outcome_written = False + + def reset(self): + """Reset the handler; should be called before the start of each test""" + self._first_record_emitted = False + + def set_when(self, when): + """Prepares for the given test phase (setup/call/teardown)""" + self._when = when + self._section_name_shown = False + if when == "start": + self._test_outcome_written = False + + def emit(self, record): + if self.capture_manager is not None: + self.capture_manager.suspend_global_capture() + try: + if not self._first_record_emitted: + self.stream.write("\n") + self._first_record_emitted = True + elif self._when in ("teardown", "finish"): + if not self._test_outcome_written: + self._test_outcome_written = True + self.stream.write("\n") + if not self._section_name_shown and self._when: + self.stream.section("live log " + self._when, sep="-", bold=True) + self._section_name_shown = True + logging.StreamHandler.emit(self, record) + finally: + if self.capture_manager is not None: + self.capture_manager.resume_global_capture() diff --git a/third_party/python/pytest/src/_pytest/main.py b/third_party/python/pytest/src/_pytest/main.py new file mode 100644 index 000000000000..23562358d7c3 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/main.py @@ -0,0 +1,611 @@ +""" core implementation of testing process: init, session, runtest loop. """ +from __future__ import absolute_import, division, print_function + +import contextlib +import functools +import os +import pkgutil +import six +import sys + +import _pytest +from _pytest import nodes +import _pytest._code +import py + +from _pytest.config import directory_arg, UsageError, hookimpl +from _pytest.outcomes import exit +from _pytest.runner import collect_one_node + + +# exitcodes for the command line +EXIT_OK = 0 +EXIT_TESTSFAILED = 1 +EXIT_INTERRUPTED = 2 +EXIT_INTERNALERROR = 3 +EXIT_USAGEERROR = 4 +EXIT_NOTESTSCOLLECTED = 5 + + +def pytest_addoption(parser): + parser.addini( + "norecursedirs", + "directory patterns to avoid for recursion", + type="args", + default=[".*", "build", "dist", "CVS", "_darcs", "{arch}", "*.egg", "venv"], + ) + parser.addini( + "testpaths", + "directories to search for tests when no files or directories are given in the " + "command line.", + type="args", + default=[], + ) + # parser.addini("dirpatterns", + # "patterns specifying possible locations of test files", + # type="linelist", default=["**/test_*.txt", + # "**/test_*.py", "**/*_test.py"] + # ) + group = parser.getgroup("general", "running and selection options") + group._addoption( + "-x", + "--exitfirst", + action="store_const", + dest="maxfail", + const=1, + help="exit instantly on first error or failed test.", + ), + group._addoption( + "--maxfail", + metavar="num", + action="store", + type=int, + dest="maxfail", + default=0, + help="exit after first num failures or errors.", + ) + group._addoption( + "--strict", + action="store_true", + help="marks not registered in configuration file raise errors.", + ) + group._addoption( + "-c", + metavar="file", + type=str, + dest="inifilename", + help="load configuration from `file` instead of trying to locate one of the implicit " + "configuration files.", + ) + group._addoption( + "--continue-on-collection-errors", + action="store_true", + default=False, + dest="continue_on_collection_errors", + help="Force test execution even if collection errors occur.", + ) + group._addoption( + "--rootdir", + action="store", + dest="rootdir", + help="Define root directory for tests. Can be relative path: 'root_dir', './root_dir', " + "'root_dir/another_dir/'; absolute path: '/home/user/root_dir'; path with variables: " + "'$HOME/root_dir'.", + ) + + group = parser.getgroup("collect", "collection") + group.addoption( + "--collectonly", + "--collect-only", + action="store_true", + help="only collect tests, don't execute them.", + ), + group.addoption( + "--pyargs", + action="store_true", + help="try to interpret all arguments as python packages.", + ) + group.addoption( + "--ignore", + action="append", + metavar="path", + help="ignore path during collection (multi-allowed).", + ) + group.addoption( + "--deselect", + action="append", + metavar="nodeid_prefix", + help="deselect item during collection (multi-allowed).", + ) + # when changing this to --conf-cut-dir, config.py Conftest.setinitial + # needs upgrading as well + group.addoption( + "--confcutdir", + dest="confcutdir", + default=None, + metavar="dir", + type=functools.partial(directory_arg, optname="--confcutdir"), + help="only load conftest.py's relative to specified dir.", + ) + group.addoption( + "--noconftest", + action="store_true", + dest="noconftest", + default=False, + help="Don't load any conftest.py files.", + ) + group.addoption( + "--keepduplicates", + "--keep-duplicates", + action="store_true", + dest="keepduplicates", + default=False, + help="Keep duplicate tests.", + ) + group.addoption( + "--collect-in-virtualenv", + action="store_true", + dest="collect_in_virtualenv", + default=False, + help="Don't ignore tests in a local virtualenv directory", + ) + + group = parser.getgroup("debugconfig", "test session debugging and configuration") + group.addoption( + "--basetemp", + dest="basetemp", + default=None, + metavar="dir", + help="base temporary directory for this test run.", + ) + + +def pytest_configure(config): + __import__("pytest").config = config # compatibility + + +def wrap_session(config, doit): + """Skeleton command line program""" + session = Session(config) + session.exitstatus = EXIT_OK + initstate = 0 + try: + try: + config._do_configure() + initstate = 1 + config.hook.pytest_sessionstart(session=session) + initstate = 2 + session.exitstatus = doit(config, session) or 0 + except UsageError: + raise + except Failed: + session.exitstatus = EXIT_TESTSFAILED + except KeyboardInterrupt: + excinfo = _pytest._code.ExceptionInfo() + if initstate < 2 and isinstance(excinfo.value, exit.Exception): + sys.stderr.write("{}: {}\n".format(excinfo.typename, excinfo.value.msg)) + config.hook.pytest_keyboard_interrupt(excinfo=excinfo) + session.exitstatus = EXIT_INTERRUPTED + except: # noqa + excinfo = _pytest._code.ExceptionInfo() + config.notify_exception(excinfo, config.option) + session.exitstatus = EXIT_INTERNALERROR + if excinfo.errisinstance(SystemExit): + sys.stderr.write("mainloop: caught Spurious SystemExit!\n") + + finally: + excinfo = None # Explicitly break reference cycle. + session.startdir.chdir() + if initstate >= 2: + config.hook.pytest_sessionfinish( + session=session, exitstatus=session.exitstatus + ) + config._ensure_unconfigure() + return session.exitstatus + + +def pytest_cmdline_main(config): + return wrap_session(config, _main) + + +def _main(config, session): + """ default command line protocol for initialization, session, + running tests and reporting. """ + config.hook.pytest_collection(session=session) + config.hook.pytest_runtestloop(session=session) + + if session.testsfailed: + return EXIT_TESTSFAILED + elif session.testscollected == 0: + return EXIT_NOTESTSCOLLECTED + + +def pytest_collection(session): + return session.perform_collect() + + +def pytest_runtestloop(session): + if session.testsfailed and not session.config.option.continue_on_collection_errors: + raise session.Interrupted("%d errors during collection" % session.testsfailed) + + if session.config.option.collectonly: + return True + + for i, item in enumerate(session.items): + nextitem = session.items[i + 1] if i + 1 < len(session.items) else None + item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) + if session.shouldfail: + raise session.Failed(session.shouldfail) + if session.shouldstop: + raise session.Interrupted(session.shouldstop) + return True + + +def _in_venv(path): + """Attempts to detect if ``path`` is the root of a Virtual Environment by + checking for the existence of the appropriate activate script""" + bindir = path.join("Scripts" if sys.platform.startswith("win") else "bin") + if not bindir.isdir(): + return False + activates = ( + "activate", + "activate.csh", + "activate.fish", + "Activate", + "Activate.bat", + "Activate.ps1", + ) + return any([fname.basename in activates for fname in bindir.listdir()]) + + +def pytest_ignore_collect(path, config): + ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath()) + ignore_paths = ignore_paths or [] + excludeopt = config.getoption("ignore") + if excludeopt: + ignore_paths.extend([py.path.local(x) for x in excludeopt]) + + if py.path.local(path) in ignore_paths: + return True + + allow_in_venv = config.getoption("collect_in_virtualenv") + if _in_venv(path) and not allow_in_venv: + return True + + # Skip duplicate paths. + keepduplicates = config.getoption("keepduplicates") + duplicate_paths = config.pluginmanager._duplicatepaths + if not keepduplicates: + if path in duplicate_paths: + return True + else: + duplicate_paths.add(path) + + return False + + +def pytest_collection_modifyitems(items, config): + deselect_prefixes = tuple(config.getoption("deselect") or []) + if not deselect_prefixes: + return + + remaining = [] + deselected = [] + for colitem in items: + if colitem.nodeid.startswith(deselect_prefixes): + deselected.append(colitem) + else: + remaining.append(colitem) + + if deselected: + config.hook.pytest_deselected(items=deselected) + items[:] = remaining + + +@contextlib.contextmanager +def _patched_find_module(): + """Patch bug in pkgutil.ImpImporter.find_module + + When using pkgutil.find_loader on python<3.4 it removes symlinks + from the path due to a call to os.path.realpath. This is not consistent + with actually doing the import (in these versions, pkgutil and __import__ + did not share the same underlying code). This can break conftest + discovery for pytest where symlinks are involved. + + The only supported python<3.4 by pytest is python 2.7. + """ + if six.PY2: # python 3.4+ uses importlib instead + + def find_module_patched(self, fullname, path=None): + # Note: we ignore 'path' argument since it is only used via meta_path + subname = fullname.split(".")[-1] + if subname != fullname and self.path is None: + return None + if self.path is None: + path = None + else: + # original: path = [os.path.realpath(self.path)] + path = [self.path] + try: + file, filename, etc = pkgutil.imp.find_module(subname, path) + except ImportError: + return None + return pkgutil.ImpLoader(fullname, file, filename, etc) + + old_find_module = pkgutil.ImpImporter.find_module + pkgutil.ImpImporter.find_module = find_module_patched + try: + yield + finally: + pkgutil.ImpImporter.find_module = old_find_module + else: + yield + + +class FSHookProxy(object): + + def __init__(self, fspath, pm, remove_mods): + self.fspath = fspath + self.pm = pm + self.remove_mods = remove_mods + + def __getattr__(self, name): + x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods) + self.__dict__[name] = x + return x + + +class NoMatch(Exception): + """ raised if matching cannot locate a matching names. """ + + +class Interrupted(KeyboardInterrupt): + """ signals an interrupted test run. """ + __module__ = "builtins" # for py3 + + +class Failed(Exception): + """ signals a stop as failed test run. """ + + +class Session(nodes.FSCollector): + Interrupted = Interrupted + Failed = Failed + + def __init__(self, config): + nodes.FSCollector.__init__( + self, config.rootdir, parent=None, config=config, session=self, nodeid="" + ) + self.testsfailed = 0 + self.testscollected = 0 + self.shouldstop = False + self.shouldfail = False + self.trace = config.trace.root.get("collection") + self._norecursepatterns = config.getini("norecursedirs") + self.startdir = py.path.local() + + self.config.pluginmanager.register(self, name="session") + + @hookimpl(tryfirst=True) + def pytest_collectstart(self): + if self.shouldfail: + raise self.Failed(self.shouldfail) + if self.shouldstop: + raise self.Interrupted(self.shouldstop) + + @hookimpl(tryfirst=True) + def pytest_runtest_logreport(self, report): + if report.failed and not hasattr(report, "wasxfail"): + self.testsfailed += 1 + maxfail = self.config.getvalue("maxfail") + if maxfail and self.testsfailed >= maxfail: + self.shouldfail = "stopping after %d failures" % (self.testsfailed) + + pytest_collectreport = pytest_runtest_logreport + + def isinitpath(self, path): + return path in self._initialpaths + + def gethookproxy(self, fspath): + # check if we have the common case of running + # hooks with all conftest.py files + pm = self.config.pluginmanager + my_conftestmodules = pm._getconftestmodules(fspath) + remove_mods = pm._conftest_plugins.difference(my_conftestmodules) + if remove_mods: + # one or more conftests are not in use at this fspath + proxy = FSHookProxy(fspath, pm, remove_mods) + else: + # all plugis are active for this fspath + proxy = self.config.hook + return proxy + + def perform_collect(self, args=None, genitems=True): + hook = self.config.hook + try: + items = self._perform_collect(args, genitems) + self.config.pluginmanager.check_pending() + hook.pytest_collection_modifyitems( + session=self, config=self.config, items=items + ) + finally: + hook.pytest_collection_finish(session=self) + self.testscollected = len(items) + return items + + def _perform_collect(self, args, genitems): + if args is None: + args = self.config.args + self.trace("perform_collect", self, args) + self.trace.root.indent += 1 + self._notfound = [] + self._initialpaths = set() + self._initialparts = [] + self.items = items = [] + for arg in args: + parts = self._parsearg(arg) + self._initialparts.append(parts) + self._initialpaths.add(parts[0]) + rep = collect_one_node(self) + self.ihook.pytest_collectreport(report=rep) + self.trace.root.indent -= 1 + if self._notfound: + errors = [] + for arg, exc in self._notfound: + line = "(no name %r in any of %r)" % (arg, exc.args[0]) + errors.append("not found: %s\n%s" % (arg, line)) + # XXX: test this + raise UsageError(*errors) + if not genitems: + return rep.result + else: + if rep.passed: + for node in rep.result: + self.items.extend(self.genitems(node)) + return items + + def collect(self): + for parts in self._initialparts: + arg = "::".join(map(str, parts)) + self.trace("processing argument", arg) + self.trace.root.indent += 1 + try: + for x in self._collect(arg): + yield x + except NoMatch: + # we are inside a make_report hook so + # we cannot directly pass through the exception + self._notfound.append((arg, sys.exc_info()[1])) + + self.trace.root.indent -= 1 + + def _collect(self, arg): + names = self._parsearg(arg) + path = names.pop(0) + if path.check(dir=1): + assert not names, "invalid arg %r" % (arg,) + for path in path.visit( + fil=lambda x: x.check(file=1), rec=self._recurse, bf=True, sort=True + ): + for x in self._collectfile(path): + yield x + else: + assert path.check(file=1) + for x in self.matchnodes(self._collectfile(path), names): + yield x + + def _collectfile(self, path): + ihook = self.gethookproxy(path) + if not self.isinitpath(path): + if ihook.pytest_ignore_collect(path=path, config=self.config): + return () + return ihook.pytest_collect_file(path=path, parent=self) + + def _recurse(self, path): + ihook = self.gethookproxy(path.dirpath()) + if ihook.pytest_ignore_collect(path=path, config=self.config): + return + for pat in self._norecursepatterns: + if path.check(fnmatch=pat): + return False + ihook = self.gethookproxy(path) + ihook.pytest_collect_directory(path=path, parent=self) + return True + + def _tryconvertpyarg(self, x): + """Convert a dotted module name to path. + + """ + + try: + with _patched_find_module(): + loader = pkgutil.find_loader(x) + except ImportError: + return x + if loader is None: + return x + # This method is sometimes invoked when AssertionRewritingHook, which + # does not define a get_filename method, is already in place: + try: + with _patched_find_module(): + path = loader.get_filename(x) + except AttributeError: + # Retrieve path from AssertionRewritingHook: + path = loader.modules[x][0].co_filename + if loader.is_package(x): + path = os.path.dirname(path) + return path + + def _parsearg(self, arg): + """ return (fspath, names) tuple after checking the file exists. """ + parts = str(arg).split("::") + if self.config.option.pyargs: + parts[0] = self._tryconvertpyarg(parts[0]) + relpath = parts[0].replace("/", os.sep) + path = self.config.invocation_dir.join(relpath, abs=True) + if not path.check(): + if self.config.option.pyargs: + raise UsageError( + "file or package not found: " + arg + " (missing __init__.py?)" + ) + else: + raise UsageError("file not found: " + arg) + parts[0] = path + return parts + + def matchnodes(self, matching, names): + self.trace("matchnodes", matching, names) + self.trace.root.indent += 1 + nodes = self._matchnodes(matching, names) + num = len(nodes) + self.trace("matchnodes finished -> ", num, "nodes") + self.trace.root.indent -= 1 + if num == 0: + raise NoMatch(matching, names[:1]) + return nodes + + def _matchnodes(self, matching, names): + if not matching or not names: + return matching + name = names[0] + assert name + nextnames = names[1:] + resultnodes = [] + for node in matching: + if isinstance(node, nodes.Item): + if not names: + resultnodes.append(node) + continue + assert isinstance(node, nodes.Collector) + rep = collect_one_node(node) + if rep.passed: + has_matched = False + for x in rep.result: + # TODO: remove parametrized workaround once collection structure contains parametrization + if x.name == name or x.name.split("[")[0] == name: + resultnodes.extend(self.matchnodes([x], nextnames)) + has_matched = True + # XXX accept IDs that don't have "()" for class instances + if not has_matched and len(rep.result) == 1 and x.name == "()": + nextnames.insert(0, name) + resultnodes.extend(self.matchnodes([x], nextnames)) + else: + # report collection failures here to avoid failing to run some test + # specified in the command line because the module could not be + # imported (#134) + node.ihook.pytest_collectreport(report=rep) + return resultnodes + + def genitems(self, node): + self.trace("genitems", node) + if isinstance(node, nodes.Item): + node.ihook.pytest_itemcollected(item=node) + yield node + else: + assert isinstance(node, nodes.Collector) + rep = collect_one_node(node) + if rep.passed: + for subnode in rep.result: + for x in self.genitems(subnode): + yield x + node.ihook.pytest_collectreport(report=rep) diff --git a/third_party/python/pytest/src/_pytest/mark/__init__.py b/third_party/python/pytest/src/_pytest/mark/__init__.py new file mode 100644 index 000000000000..e3918ca6a4fb --- /dev/null +++ b/third_party/python/pytest/src/_pytest/mark/__init__.py @@ -0,0 +1,174 @@ +""" generic mechanism for marking and selecting python functions. """ +from __future__ import absolute_import, division, print_function +from _pytest.config import UsageError +from .structures import ( + ParameterSet, + EMPTY_PARAMETERSET_OPTION, + MARK_GEN, + Mark, + MarkInfo, + MarkDecorator, + MarkGenerator, + transfer_markers, + get_empty_parameterset_mark, +) +from .legacy import matchkeyword, matchmark + +__all__ = [ + "Mark", + "MarkInfo", + "MarkDecorator", + "MarkGenerator", + "transfer_markers", + "get_empty_parameterset_mark", +] + + +class MarkerError(Exception): + + """Error in use of a pytest marker/attribute.""" + + +def param(*values, **kw): + """Specify a parameter in `pytest.mark.parametrize`_ calls or + :ref:`parametrized fixtures `. + + .. code-block:: python + + @pytest.mark.parametrize("test_input,expected", [ + ("3+5", 8), + pytest.param("6*9", 42, marks=pytest.mark.xfail), + ]) + def test_eval(test_input, expected): + assert eval(test_input) == expected + + :param values: variable args of the values of the parameter set, in order. + :keyword marks: a single mark or a list of marks to be applied to this parameter set. + :keyword str id: the id to attribute to this parameter set. + """ + return ParameterSet.param(*values, **kw) + + +def pytest_addoption(parser): + group = parser.getgroup("general") + group._addoption( + "-k", + action="store", + dest="keyword", + default="", + metavar="EXPRESSION", + help="only run tests which match the given substring expression. " + "An expression is a python evaluatable expression " + "where all names are substring-matched against test names " + "and their parent classes. Example: -k 'test_method or test_" + "other' matches all test functions and classes whose name " + "contains 'test_method' or 'test_other', while -k 'not test_method' " + "matches those that don't contain 'test_method' in their names. " + "Additionally keywords are matched to classes and functions " + "containing extra names in their 'extra_keyword_matches' set, " + "as well as functions which have names assigned directly to them.", + ) + + group._addoption( + "-m", + action="store", + dest="markexpr", + default="", + metavar="MARKEXPR", + help="only run tests matching given mark expression. " + "example: -m 'mark1 and not mark2'.", + ) + + group.addoption( + "--markers", + action="store_true", + help="show markers (builtin, plugin and per-project ones).", + ) + + parser.addini("markers", "markers for test functions", "linelist") + parser.addini(EMPTY_PARAMETERSET_OPTION, "default marker for empty parametersets") + + +def pytest_cmdline_main(config): + import _pytest.config + + if config.option.markers: + config._do_configure() + tw = _pytest.config.create_terminal_writer(config) + for line in config.getini("markers"): + parts = line.split(":", 1) + name = parts[0] + rest = parts[1] if len(parts) == 2 else "" + tw.write("@pytest.mark.%s:" % name, bold=True) + tw.line(rest) + tw.line() + config._ensure_unconfigure() + return 0 + + +pytest_cmdline_main.tryfirst = True + + +def deselect_by_keyword(items, config): + keywordexpr = config.option.keyword.lstrip() + if keywordexpr.startswith("-"): + keywordexpr = "not " + keywordexpr[1:] + selectuntil = False + if keywordexpr[-1:] == ":": + selectuntil = True + keywordexpr = keywordexpr[:-1] + + remaining = [] + deselected = [] + for colitem in items: + if keywordexpr and not matchkeyword(colitem, keywordexpr): + deselected.append(colitem) + else: + if selectuntil: + keywordexpr = None + remaining.append(colitem) + + if deselected: + config.hook.pytest_deselected(items=deselected) + items[:] = remaining + + +def deselect_by_mark(items, config): + matchexpr = config.option.markexpr + if not matchexpr: + return + + remaining = [] + deselected = [] + for item in items: + if matchmark(item, matchexpr): + remaining.append(item) + else: + deselected.append(item) + + if deselected: + config.hook.pytest_deselected(items=deselected) + items[:] = remaining + + +def pytest_collection_modifyitems(items, config): + deselect_by_keyword(items, config) + deselect_by_mark(items, config) + + +def pytest_configure(config): + config._old_mark_config = MARK_GEN._config + if config.option.strict: + MARK_GEN._config = config + + empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION) + + if empty_parameterset not in ("skip", "xfail", None, ""): + raise UsageError( + "{!s} must be one of skip and xfail," + " but it is {!r}".format(EMPTY_PARAMETERSET_OPTION, empty_parameterset) + ) + + +def pytest_unconfigure(config): + MARK_GEN._config = getattr(config, "_old_mark_config", None) diff --git a/third_party/python/pytest/src/_pytest/mark/evaluate.py b/third_party/python/pytest/src/_pytest/mark/evaluate.py new file mode 100644 index 000000000000..a3d11ee0f81a --- /dev/null +++ b/third_party/python/pytest/src/_pytest/mark/evaluate.py @@ -0,0 +1,121 @@ +import os +import six +import sys +import platform +import traceback + +from ..outcomes import fail, TEST_OUTCOME + + +def cached_eval(config, expr, d): + if not hasattr(config, "_evalcache"): + config._evalcache = {} + try: + return config._evalcache[expr] + except KeyError: + import _pytest._code + + exprcode = _pytest._code.compile(expr, mode="eval") + config._evalcache[expr] = x = eval(exprcode, d) + return x + + +class MarkEvaluator(object): + + def __init__(self, item, name): + self.item = item + self._marks = None + self._mark = None + self._mark_name = name + + def __bool__(self): + # dont cache here to prevent staleness + return bool(self._get_marks()) + + __nonzero__ = __bool__ + + def wasvalid(self): + return not hasattr(self, "exc") + + def _get_marks(self): + return list(self.item.iter_markers(name=self._mark_name)) + + def invalidraise(self, exc): + raises = self.get("raises") + if not raises: + return + return not isinstance(exc, raises) + + def istrue(self): + try: + return self._istrue() + except TEST_OUTCOME: + self.exc = sys.exc_info() + if isinstance(self.exc[1], SyntaxError): + msg = [" " * (self.exc[1].offset + 4) + "^"] + msg.append("SyntaxError: invalid syntax") + else: + msg = traceback.format_exception_only(*self.exc[:2]) + fail( + "Error evaluating %r expression\n" + " %s\n" + "%s" % (self._mark_name, self.expr, "\n".join(msg)), + pytrace=False, + ) + + def _getglobals(self): + d = {"os": os, "sys": sys, "platform": platform, "config": self.item.config} + if hasattr(self.item, "obj"): + d.update(self.item.obj.__globals__) + return d + + def _istrue(self): + if hasattr(self, "result"): + return self.result + self._marks = self._get_marks() + + if self._marks: + self.result = False + for mark in self._marks: + self._mark = mark + if "condition" in mark.kwargs: + args = (mark.kwargs["condition"],) + else: + args = mark.args + + for expr in args: + self.expr = expr + if isinstance(expr, six.string_types): + d = self._getglobals() + result = cached_eval(self.item.config, expr, d) + else: + if "reason" not in mark.kwargs: + # XXX better be checked at collection time + msg = "you need to specify reason=STRING " "when using booleans as conditions." + fail(msg) + result = bool(expr) + if result: + self.result = True + self.reason = mark.kwargs.get("reason", None) + self.expr = expr + return self.result + + if not args: + self.result = True + self.reason = mark.kwargs.get("reason", None) + return self.result + return False + + def get(self, attr, default=None): + if self._mark is None: + return default + return self._mark.kwargs.get(attr, default) + + def getexplanation(self): + expl = getattr(self, "reason", None) or self.get("reason", None) + if not expl: + if not hasattr(self, "expr"): + return "" + else: + return "condition: " + str(self.expr) + return expl diff --git a/third_party/python/pytest/src/_pytest/mark/legacy.py b/third_party/python/pytest/src/_pytest/mark/legacy.py new file mode 100644 index 000000000000..ab016a0355b7 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/mark/legacy.py @@ -0,0 +1,97 @@ +""" +this is a place where we put datastructures used by legacy apis +we hope ot remove +""" +import attr +import keyword + +from _pytest.config import UsageError + + +@attr.s +class MarkMapping(object): + """Provides a local mapping for markers where item access + resolves to True if the marker is present. """ + + own_mark_names = attr.ib() + + @classmethod + def from_item(cls, item): + mark_names = {mark.name for mark in item.iter_markers()} + return cls(mark_names) + + def __getitem__(self, name): + return name in self.own_mark_names + + +class KeywordMapping(object): + """Provides a local mapping for keywords. + Given a list of names, map any substring of one of these names to True. + """ + + def __init__(self, names): + self._names = names + + @classmethod + def from_item(cls, item): + mapped_names = set() + + # Add the names of the current item and any parent items + import pytest + + for item in item.listchain(): + if not isinstance(item, pytest.Instance): + mapped_names.add(item.name) + + # Add the names added as extra keywords to current or parent items + for name in item.listextrakeywords(): + mapped_names.add(name) + + # Add the names attached to the current function through direct assignment + if hasattr(item, "function"): + for name in item.function.__dict__: + mapped_names.add(name) + + return cls(mapped_names) + + def __getitem__(self, subname): + for name in self._names: + if subname in name: + return True + return False + + +python_keywords_allowed_list = ["or", "and", "not"] + + +def matchmark(colitem, markexpr): + """Tries to match on any marker names, attached to the given colitem.""" + return eval(markexpr, {}, MarkMapping.from_item(colitem)) + + +def matchkeyword(colitem, keywordexpr): + """Tries to match given keyword expression to given collector item. + + Will match on the name of colitem, including the names of its parents. + Only matches names of items which are either a :class:`Class` or a + :class:`Function`. + Additionally, matches on names in the 'extra_keyword_matches' set of + any item, as well as names directly assigned to test functions. + """ + mapping = KeywordMapping.from_item(colitem) + if " " not in keywordexpr: + # special case to allow for simple "-k pass" and "-k 1.3" + return mapping[keywordexpr] + elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]: + return not mapping[keywordexpr[4:]] + for kwd in keywordexpr.split(): + if keyword.iskeyword(kwd) and kwd not in python_keywords_allowed_list: + raise UsageError( + "Python keyword '{}' not accepted in expressions passed to '-k'".format( + kwd + ) + ) + try: + return eval(keywordexpr, {}, mapping) + except SyntaxError: + raise UsageError("Wrong expression passed to '-k': {}".format(keywordexpr)) diff --git a/third_party/python/pytest/src/_pytest/mark/structures.py b/third_party/python/pytest/src/_pytest/mark/structures.py new file mode 100644 index 000000000000..1a2bd73de568 --- /dev/null +++ b/third_party/python/pytest/src/_pytest/mark/structures.py @@ -0,0 +1,428 @@ +import inspect +import warnings +from collections import namedtuple +from operator import attrgetter + +import attr + +from ..deprecated import MARK_PARAMETERSET_UNPACKING, MARK_INFO_ATTRIBUTE +from ..compat import NOTSET, getfslineno, MappingMixin +from six.moves import map, reduce + + +EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" + + +def alias(name, warning=None): + getter = attrgetter(name) + + def warned(self): + warnings.warn(warning, stacklevel=2) + return getter(self) + + return property(getter if warning is None else warned, doc="alias for " + name) + + +def istestfunc(func): + return hasattr(func, "__call__") and getattr( + func, "__name__", "" + ) != "" + + +def get_empty_parameterset_mark(config, argnames, func): + requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION) + if requested_mark in ("", None, "skip"): + mark = MARK_GEN.skip + elif requested_mark == "xfail": + mark = MARK_GEN.xfail(run=False) + else: + raise LookupError(requested_mark) + fs, lineno = getfslineno(func) + reason = "got empty parameter set %r, function %s at %s:%d" % ( + argnames, func.__name__, fs, lineno + ) + return mark(reason=reason) + + +class ParameterSet(namedtuple("ParameterSet", "values, marks, id")): + + @classmethod + def param(cls, *values, **kw): + marks = kw.pop("marks", ()) + if isinstance(marks, MarkDecorator): + marks = marks, + else: + assert isinstance(marks, (tuple, list, set)) + + def param_extract_id(id=None): + return id + + id_ = param_extract_id(**kw) + return cls(values, marks, id_) + + @classmethod + def extract_from(cls, parameterset, legacy_force_tuple=False): + """ + :param parameterset: + a legacy style parameterset that may or may not be a tuple, + and may or may not be wrapped into a mess of mark objects + + :param legacy_force_tuple: + enforce tuple wrapping so single argument tuple values + don't get decomposed and break tests + + """ + + if isinstance(parameterset, cls): + return parameterset + if not isinstance(parameterset, MarkDecorator) and legacy_force_tuple: + return cls.param(parameterset) + + newmarks = [] + argval = parameterset + while isinstance(argval, MarkDecorator): + newmarks.append( + MarkDecorator(Mark(argval.markname, argval.args[:-1], argval.kwargs)) + ) + argval = argval.args[-1] + assert not isinstance(argval, ParameterSet) + if legacy_force_tuple: + argval = argval, + + if newmarks: + warnings.warn(MARK_PARAMETERSET_UNPACKING) + + return cls(argval, marks=newmarks, id=None) + + @classmethod + def _for_parametrize(cls, argnames, argvalues, func, config): + if not isinstance(argnames, (tuple, list)): + argnames = [x.strip() for x in argnames.split(",") if x.strip()] + force_tuple = len(argnames) == 1 + else: + force_tuple = False + parameters = [ + ParameterSet.extract_from(x, legacy_force_tuple=force_tuple) + for x in argvalues + ] + del argvalues + + if not parameters: + mark = get_empty_parameterset_mark(config, argnames, func) + parameters.append( + ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None) + ) + return argnames, parameters + + +@attr.s(frozen=True) +class Mark(object): + #: name of the mark + name = attr.ib(type=str) + #: positional arguments of the mark decorator + args = attr.ib(type="List[object]") + #: keyword arguments of the mark decorator + kwargs = attr.ib(type="Dict[str, object]") + + def combined_with(self, other): + """ + :param other: the mark to combine with + :type other: Mark + :rtype: Mark + + combines by appending aargs and merging the mappings + """ + assert self.name == other.name + return Mark( + self.name, self.args + other.args, dict(self.kwargs, **other.kwargs) + ) + + +@attr.s +class MarkDecorator(object): + """ A decorator for test functions and test classes. When applied + it will create :class:`MarkInfo` objects which may be + :ref:`retrieved by hooks as item keywords `. + MarkDecorator instances are often created like this:: + + mark1 = pytest.mark.NAME # simple MarkDecorator + mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator + + and can then be applied as decorators to test functions:: + + @mark2 + def test_function(): + pass + + When a MarkDecorator instance is called it does the following: + 1. If called with a single class as its only positional argument and no + additional keyword arguments, it attaches itself to the class so it + gets applied automatically to all test cases found in that class. + 2. If called with a single function as its only positional argument and + no additional keyword arguments, it attaches a MarkInfo object to the + function, containing all the arguments already stored internally in + the MarkDecorator. + 3. When called in any other case, it performs a 'fake construction' call, + i.e. it returns a new MarkDecorator instance with the original + MarkDecorator's content updated with the arguments passed to this + call. + + Note: The rules above prevent MarkDecorator objects from storing only a + single function or class reference as their positional argument with no + additional keyword or positional arguments. + + """ + + mark = attr.ib(validator=attr.validators.instance_of(Mark)) + + name = alias("mark.name") + args = alias("mark.args") + kwargs = alias("mark.kwargs") + + @property + def markname(self): + return self.name # for backward-compat (2.4.1 had this attr) + + def __eq__(self, other): + return self.mark == other.mark if isinstance(other, MarkDecorator) else False + + def __repr__(self): + return "" % (self.mark,) + + def with_args(self, *args, **kwargs): + """ return a MarkDecorator with extra arguments added + + unlike call this can be used even if the sole argument is a callable/class + + :return: MarkDecorator + """ + + mark = Mark(self.name, args, kwargs) + return self.__class__(self.mark.combined_with(mark)) + + def __call__(self, *args, **kwargs): + """ if passed a single callable argument: decorate it with mark info. + otherwise add *args/**kwargs in-place to mark information. """ + if args and not kwargs: + func = args[0] + is_class = inspect.isclass(func) + if len(args) == 1 and (istestfunc(func) or is_class): + if is_class: + store_mark(func, self.mark) + else: + store_legacy_markinfo(func, self.mark) + store_mark(func, self.mark) + return func + return self.with_args(*args, **kwargs) + + +def get_unpacked_marks(obj): + """ + obtain the unpacked marks that are stored on an object + """ + mark_list = getattr(obj, "pytestmark", []) + + if not isinstance(mark_list, list): + mark_list = [mark_list] + return [getattr(mark, "mark", mark) for mark in mark_list] # unpack MarkDecorator + + +def store_mark(obj, mark): + """store a Mark on an object + this is used to implement the Mark declarations/decorators correctly + """ + assert isinstance(mark, Mark), mark + # always reassign name to avoid updating pytestmark + # in a reference that was only borrowed + obj.pytestmark = get_unpacked_marks(obj) + [mark] + + +def store_legacy_markinfo(func, mark): + """create the legacy MarkInfo objects and put them onto the function + """ + if not isinstance(mark, Mark): + raise TypeError("got {mark!r} instead of a Mark".format(mark=mark)) + holder = getattr(func, mark.name, None) + if holder is None: + holder = MarkInfo.for_mark(mark) + setattr(func, mark.name, holder) + else: + holder.add_mark(mark) + + +def transfer_markers(funcobj, cls, mod): + """ + this function transfers class level markers and module level markers + into function level markinfo objects + + this is the main reason why marks are so broken + the resolution will involve phasing out function level MarkInfo objects + + """ + for obj in (cls, mod): + for mark in get_unpacked_marks(obj): + if not _marked(funcobj, mark): + store_legacy_markinfo(funcobj, mark) + + +def _marked(func, mark): + """ Returns True if :func: is already marked with :mark:, False otherwise. + This can happen if marker is applied to class and the test file is + invoked more than once. + """ + try: + func_mark = getattr(func, getattr(mark, "combined", mark).name) + except AttributeError: + return False + return any(mark == info.combined for info in func_mark) + + +@attr.s +class MarkInfo(object): + """ Marking object created by :class:`MarkDecorator` instances. """ + + _marks = attr.ib(convert=list) + + @_marks.validator + def validate_marks(self, attribute, value): + for item in value: + if not isinstance(item, Mark): + raise ValueError( + "MarkInfo expects Mark instances, got {!r} ({!r})".format( + item, type(item) + ) + ) + + combined = attr.ib( + repr=False, + default=attr.Factory( + lambda self: reduce(Mark.combined_with, self._marks), takes_self=True + ), + ) + + name = alias("combined.name", warning=MARK_INFO_ATTRIBUTE) + args = alias("combined.args", warning=MARK_INFO_ATTRIBUTE) + kwargs = alias("combined.kwargs", warning=MARK_INFO_ATTRIBUTE) + + @classmethod + def for_mark(cls, mark): + return cls([mark]) + + def __repr__(self): + return "".format(self.combined) + + def add_mark(self, mark): + """ add a MarkInfo with the given args and kwargs. """ + self._marks.append(mark) + self.combined = self.combined.combined_with(mark) + + def __iter__(self): + """ yield MarkInfo objects each relating to a marking-call. """ + return map(MarkInfo.for_mark, self._marks) + + +class MarkGenerator(object): + """ Factory for :class:`MarkDecorator` objects - exposed as + a ``pytest.mark`` singleton instance. Example:: + + import pytest + @pytest.mark.slowtest + def test_function(): + pass + + will set a 'slowtest' :class:`MarkInfo` object + on the ``test_function`` object. """ + _config = None + + def __getattr__(self, name): + if name[0] == "_": + raise AttributeError("Marker name must NOT start with underscore") + if self._config is not None: + self._check(name) + return MarkDecorator(Mark(name, (), {})) + + def _check(self, name): + try: + if name in self._markers: + return + except AttributeError: + pass + self._markers = values = set() + for line in self._config.getini("markers"): + marker = line.split(":", 1)[0] + marker = marker.rstrip() + x = marker.split("(", 1)[0] + values.add(x) + if name not in self._markers: + raise AttributeError("%r not a registered marker" % (name,)) + + +MARK_GEN = MarkGenerator() + + +class NodeKeywords(MappingMixin): + + def __init__(self, node): + self.node = node + self.parent = node.parent + self._markers = {node.name: True} + + def __getitem__(self, key): + try: + return self._markers[key] + except KeyError: + if self.parent is None: + raise + return self.parent.keywords[key] + + def __setitem__(self, key, value): + self._markers[key] = value + + def __delitem__(self, key): + raise ValueError("cannot delete key in keywords dict") + + def __iter__(self): + seen = self._seen() + return iter(seen) + + def _seen(self): + seen = set(self._markers) + if self.parent is not None: + seen.update(self.parent.keywords) + return seen + + def __len__(self): + return len(self._seen()) + + def __repr__(self): + return "" % (self.node,) + + +@attr.s(cmp=False, hash=False) +class NodeMarkers(object): + """ + internal strucutre for storing marks belongong to a node + + ..warning:: + + unstable api + + """ + own_markers = attr.ib(default=attr.Factory(list)) + + def update(self, add_markers): + """update the own markers + """ + self.own_markers.extend(add_markers) + + def find(self, name): + """ + find markers in own nodes or parent nodes + needs a better place + """ + for mark in self.own_markers: + if mark.name == name: + yield mark + + def __iter__(self): + return iter(self.own_markers) diff --git a/third_party/python/pytest/_pytest/monkeypatch.py b/third_party/python/pytest/src/_pytest/monkeypatch.py similarity index 81% rename from third_party/python/pytest/_pytest/monkeypatch.py rename to third_party/python/pytest/src/_pytest/monkeypatch.py index 39ac77013504..16080b5d5cb2 100644 --- a/third_party/python/pytest/_pytest/monkeypatch.py +++ b/third_party/python/pytest/src/_pytest/monkeypatch.py @@ -4,8 +4,9 @@ from __future__ import absolute_import, division, print_function import os import sys import re +from contextlib import contextmanager -from py.builtin import _basestring +import six from _pytest.fixtures import fixture RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$") @@ -37,12 +38,12 @@ def monkeypatch(): def resolve(name): # simplified from zope.dottedname - parts = name.split('.') + parts = name.split(".") used = parts.pop(0) found = __import__(used) for part in parts: - used += '.' + part + used += "." + part try: found = getattr(found, part) except AttributeError: @@ -59,9 +60,7 @@ def resolve(name): if expected == used: raise else: - raise ImportError( - 'import error in %s: %s' % (used, ex) - ) + raise ImportError("import error in %s: %s" % (used, ex)) found = annotated_getattr(found, part, used) return found @@ -71,25 +70,23 @@ def annotated_getattr(obj, name, ann): obj = getattr(obj, name) except AttributeError: raise AttributeError( - '%r object at %s has no attribute %r' % ( - type(obj).__name__, ann, name - ) + "%r object at %s has no attribute %r" % (type(obj).__name__, ann, name) ) return obj def derive_importpath(import_path, raising): - if not isinstance(import_path, _basestring) or "." not in import_path: - raise TypeError("must be absolute import path string, not %r" % - (import_path,)) - module, attr = import_path.rsplit('.', 1) + if not isinstance(import_path, six.string_types) or "." not in import_path: + raise TypeError("must be absolute import path string, not %r" % (import_path,)) + module, attr = import_path.rsplit(".", 1) target = resolve(module) if raising: annotated_getattr(target, attr, ann=module) return attr, target -class Notset: +class Notset(object): + def __repr__(self): return "" @@ -97,7 +94,7 @@ class Notset: notset = Notset() -class MonkeyPatch: +class MonkeyPatch(object): """ Object returned by the ``monkeypatch`` fixture keeping a record of setattr/item/env/syspath changes. """ @@ -107,6 +104,29 @@ class MonkeyPatch: self._cwd = None self._savesyspath = None + @contextmanager + def context(self): + """ + Context manager that returns a new :class:`MonkeyPatch` object which + undoes any patching done inside the ``with`` block upon exit: + + .. code-block:: python + + import functools + def test_partial(monkeypatch): + with monkeypatch.context() as m: + m.setattr(functools, "partial", 3) + + Useful in situations where it is desired to undo some patches before the test ends, + such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples + of this see `#3290 `_. + """ + m = MonkeyPatch() + try: + yield m + finally: + m.undo() + def setattr(self, target, name, value=notset, raising=True): """ Set attribute value on target, memorizing the old value. By default raise AttributeError if the attribute did not exist. @@ -114,7 +134,7 @@ class MonkeyPatch: For convenience you can specify a string as ``target`` which will be interpreted as a dotted import path, with the last part being the attribute name. Example: - ``monkeypatch.setattr("os.getcwd", lambda x: "/")`` + ``monkeypatch.setattr("os.getcwd", lambda: "/")`` would set the ``getcwd`` function of the ``os`` module. The ``raising`` value determines if the setattr should fail @@ -125,10 +145,12 @@ class MonkeyPatch: import inspect if value is notset: - if not isinstance(target, _basestring): - raise TypeError("use setattr(target, name, value) or " - "setattr(target, value) with target being a dotted " - "import string") + if not isinstance(target, six.string_types): + raise TypeError( + "use setattr(target, name, value) or " + "setattr(target, value) with target being a dotted " + "import string" + ) value = name name, target = derive_importpath(target, raising) @@ -155,10 +177,12 @@ class MonkeyPatch: """ __tracebackhide__ = True if name is notset: - if not isinstance(target, _basestring): - raise TypeError("use delattr(target, name) or " - "delattr(target) with target being a dotted " - "import string") + if not isinstance(target, six.string_types): + raise TypeError( + "use delattr(target, name) or " + "delattr(target) with target being a dotted " + "import string" + ) name, target = derive_importpath(target, raising) if not hasattr(target, name): diff --git a/third_party/python/pytest/src/_pytest/nodes.py b/third_party/python/pytest/src/_pytest/nodes.py new file mode 100644 index 000000000000..ef74c53eba4e --- /dev/null +++ b/third_party/python/pytest/src/_pytest/nodes.py @@ -0,0 +1,419 @@ +from __future__ import absolute_import, division, print_function +import os + +import six +import py +import attr + +import _pytest +import _pytest._code + +from _pytest.mark.structures import NodeKeywords, MarkInfo + +SEP = "/" + +tracebackcutdir = py.path.local(_pytest.__file__).dirpath() + + +def _splitnode(nodeid): + """Split a nodeid into constituent 'parts'. + + Node IDs are strings, and can be things like: + '' + 'testing/code' + 'testing/code/test_excinfo.py' + 'testing/code/test_excinfo.py::TestFormattedExcinfo::()' + + Return values are lists e.g. + [] + ['testing', 'code'] + ['testing', 'code', 'test_excinfo.py'] + ['testing', 'code', 'test_excinfo.py', 'TestFormattedExcinfo', '()'] + """ + if nodeid == "": + # If there is no root node at all, return an empty list so the caller's logic can remain sane + return [] + parts = nodeid.split(SEP) + # Replace single last element 'test_foo.py::Bar::()' with multiple elements 'test_foo.py', 'Bar', '()' + parts[-1:] = parts[-1].split("::") + return parts + + +def ischildnode(baseid, nodeid): + """Return True if the nodeid is a child node of the baseid. + + E.g. 'foo/bar::Baz::()' is a child of 'foo', 'foo/bar' and 'foo/bar::Baz', but not of 'foo/blorp' + """ + base_parts = _splitnode(baseid) + node_parts = _splitnode(nodeid) + if len(node_parts) < len(base_parts): + return False + return node_parts[:len(base_parts)] == base_parts + + +@attr.s +class _CompatProperty(object): + name = attr.ib() + + def __get__(self, obj, owner): + if obj is None: + return self + + # TODO: reenable in the features branch + # warnings.warn( + # "usage of {owner!r}.{name} is deprecated, please use pytest.{name} instead".format( + # name=self.name, owner=type(owner).__name__), + # PendingDeprecationWarning, stacklevel=2) + return getattr(__import__("pytest"), self.name) + + +class Node(object): + """ base class for Collector and Item the test collection tree. + Collector subclasses have children, Items are terminal nodes.""" + + def __init__( + self, name, parent=None, config=None, session=None, fspath=None, nodeid=None + ): + #: a unique name within the scope of the parent node + self.name = name + + #: the parent collector node. + self.parent = parent + + #: the pytest config object + self.config = config or parent.config + + #: the session this node is part of + self.session = session or parent.session + + #: filesystem path where this node was collected from (can be None) + self.fspath = fspath or getattr(parent, "fspath", None) + + #: keywords/markers collected from all scopes + self.keywords = NodeKeywords(self) + + #: the marker objects belonging to this node + self.own_markers = [] + + #: allow adding of extra keywords to use for matching + self.extra_keyword_matches = set() + + # used for storing artificial fixturedefs for direct parametrization + self._name2pseudofixturedef = {} + + if nodeid is not None: + self._nodeid = nodeid + else: + assert parent is not None + self._nodeid = self.parent.nodeid + "::" + self.name + + @property + def ihook(self): + """ fspath sensitive hook proxy used to call pytest hooks""" + return self.session.gethookproxy(self.fspath) + + Module = _CompatProperty("Module") + Class = _CompatProperty("Class") + Instance = _CompatProperty("Instance") + Function = _CompatProperty("Function") + File = _CompatProperty("File") + Item = _CompatProperty("Item") + + def _getcustomclass(self, name): + maybe_compatprop = getattr(type(self), name) + if isinstance(maybe_compatprop, _CompatProperty): + return getattr(__import__("pytest"), name) + else: + cls = getattr(self, name) + # TODO: reenable in the features branch + # warnings.warn("use of node.%s is deprecated, " + # "use pytest_pycollect_makeitem(...) to create custom " + # "collection nodes" % name, category=DeprecationWarning) + return cls + + def __repr__(self): + return "<%s %r>" % (self.__class__.__name__, getattr(self, "name", None)) + + def warn(self, code, message): + """ generate a warning with the given code and message for this + item. """ + assert isinstance(code, str) + fslocation = getattr(self, "location", None) + if fslocation is None: + fslocation = getattr(self, "fspath", None) + self.ihook.pytest_logwarning.call_historic( + kwargs=dict( + code=code, message=message, nodeid=self.nodeid, fslocation=fslocation + ) + ) + + # methods for ordering nodes + @property + def nodeid(self): + """ a ::-separated string denoting its collection tree address. """ + return self._nodeid + + def __hash__(self): + return hash(self.nodeid) + + def setup(self): + pass + + def teardown(self): + pass + + def listchain(self): + """ return list of all parent collectors up to self, + starting from root of collection tree. """ + chain = [] + item = self + while item is not None: + chain.append(item) + item = item.parent + chain.reverse() + return chain + + def add_marker(self, marker): + """dynamically add a marker object to the node. + + :type marker: str or pytest.mark.* + """ + from _pytest.mark import MarkDecorator, MARK_GEN + + if isinstance(marker, six.string_types): + marker = getattr(MARK_GEN, marker) + elif not isinstance(marker, MarkDecorator): + raise ValueError("is not a string or pytest.mark.* Marker") + self.keywords[marker.name] = marker + self.own_markers.append(marker.mark) + + def iter_markers(self, name=None): + """ + :param name: if given, filter the results by the name attribute + + iterate over all markers of the node + """ + return (x[1] for x in self.iter_markers_with_node(name=name)) + + def iter_markers_with_node(self, name=None): + """ + :param name: if given, filter the results by the name attribute + + iterate over all markers of the node + returns sequence of tuples (node, mark) + """ + for node in reversed(self.listchain()): + for mark in node.own_markers: + if name is None or getattr(mark, "name", None) == name: + yield node, mark + + def get_closest_marker(self, name, default=None): + """return the first marker matching the name, from closest (for example function) to farther level (for example + module level). + + :param default: fallback return value of no marker was found + :param name: name to filter by + """ + return next(self.iter_markers(name=name), default) + + def get_marker(self, name): + """ get a marker object from this node or None if + the node doesn't have a marker with that name. + + .. deprecated:: 3.6 + This function has been deprecated in favor of + :meth:`Node.get_closest_marker <_pytest.nodes.Node.get_closest_marker>` and + :meth:`Node.iter_markers <_pytest.nodes.Node.iter_markers>`, see :ref:`update marker code` + for more details. + """ + markers = list(self.iter_markers(name=name)) + if markers: + return MarkInfo(markers) + + def listextrakeywords(self): + """ Return a set of all extra keywords in self and any parents.""" + extra_keywords = set() + for item in self.listchain(): + extra_keywords.update(item.extra_keyword_matches) + return extra_keywords + + def listnames(self): + return [x.name for x in self.listchain()] + + def addfinalizer(self, fin): + """ register a function to be called when this node is finalized. + + This method can only be called when this node is active + in a setup chain, for example during self.setup(). + """ + self.session._setupstate.addfinalizer(fin, self) + + def getparent(self, cls): + """ get the next parent node (including ourself) + which is an instance of the given class""" + current = self + while current and not isinstance(current, cls): + current = current.parent + return current + + def _prunetraceback(self, excinfo): + pass + + def _repr_failure_py(self, excinfo, style=None): + fm = self.session._fixturemanager + if excinfo.errisinstance(fm.FixtureLookupError): + return excinfo.value.formatrepr() + tbfilter = True + if self.config.option.fulltrace: + style = "long" + else: + tb = _pytest._code.Traceback([excinfo.traceback[-1]]) + self._prunetraceback(excinfo) + if len(excinfo.traceback) == 0: + excinfo.traceback = tb + tbfilter = False # prunetraceback already does it + if style == "auto": + style = "long" + # XXX should excinfo.getrepr record all data and toterminal() process it? + if style is None: + if self.config.option.tbstyle == "short": + style = "short" + else: + style = "long" + + try: + os.getcwd() + abspath = False + except OSError: + abspath = True + + return excinfo.getrepr( + funcargs=True, + abspath=abspath, + showlocals=self.config.option.showlocals, + style=style, + tbfilter=tbfilter, + ) + + repr_failure = _repr_failure_py + + +class Collector(Node): + """ Collector instances create children through collect() + and thus iteratively build a tree. + """ + + class CollectError(Exception): + """ an error during collection, contains a custom message. """ + + def collect(self): + """ returns a list of children (items and collectors) + for this collection node. + """ + raise NotImplementedError("abstract") + + def repr_failure(self, excinfo): + """ represent a collection failure. """ + if excinfo.errisinstance(self.CollectError): + exc = excinfo.value + return str(exc.args[0]) + return self._repr_failure_py(excinfo, style="short") + + def _prunetraceback(self, excinfo): + if hasattr(self, "fspath"): + traceback = excinfo.traceback + ntraceback = traceback.cut(path=self.fspath) + if ntraceback == traceback: + ntraceback = ntraceback.cut(excludepath=tracebackcutdir) + excinfo.traceback = ntraceback.filter() + + +def _check_initialpaths_for_relpath(session, fspath): + for initial_path in session._initialpaths: + if fspath.common(initial_path) == initial_path: + return fspath.relto(initial_path.dirname) + + +class FSCollector(Collector): + + def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None): + fspath = py.path.local(fspath) # xxx only for test_resultlog.py? + name = fspath.basename + if parent is not None: + rel = fspath.relto(parent.fspath) + if rel: + name = rel + name = name.replace(os.sep, SEP) + self.fspath = fspath + + session = session or parent.session + + if nodeid is None: + nodeid = self.fspath.relto(session.config.rootdir) + + if not nodeid: + nodeid = _check_initialpaths_for_relpath(session, fspath) + if os.sep != SEP: + nodeid = nodeid.replace(os.sep, SEP) + + super(FSCollector, self).__init__( + name, parent, config, session, nodeid=nodeid, fspath=fspath + ) + + +class File(FSCollector): + """ base class for collecting tests from a file. """ + + +class Item(Node): + """ a basic test invocation item. Note that for a single function + there might be multiple test invocation items. + """ + nextitem = None + + def __init__(self, name, parent=None, config=None, session=None, nodeid=None): + super(Item, self).__init__(name, parent, config, session, nodeid=nodeid) + self._report_sections = [] + + #: user properties is a list of tuples (name, value) that holds user + #: defined properties for this test. + self.user_properties = [] + + def add_report_section(self, when, key, content): + """ + Adds a new report section, similar to what's done internally to add stdout and + stderr captured output:: + + item.add_report_section("call", "stdout", "report section contents") + + :param str when: + One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``. + :param str key: + Name of the section, can be customized at will. Pytest uses ``"stdout"`` and + ``"stderr"`` internally. + + :param str content: + The full contents as a string. + """ + if content: + self._report_sections.append((when, key, content)) + + def reportinfo(self): + return self.fspath, None, "" + + @property + def location(self): + try: + return self._location + except AttributeError: + location = self.reportinfo() + # bestrelpath is a quite slow function + cache = self.config.__dict__.setdefault("_bestrelpathcache", {}) + try: + fspath = cache[location[0]] + except KeyError: + fspath = self.session.fspath.bestrelpath(location[0]) + cache[location[0]] = fspath + location = (fspath, location[1], str(location[2])) + self._location = location + return location diff --git a/third_party/python/pytest/_pytest/nose.py b/third_party/python/pytest/src/_pytest/nose.py similarity index 69% rename from third_party/python/pytest/_pytest/nose.py rename to third_party/python/pytest/src/_pytest/nose.py index d246c5603d21..bb2e4277d1c8 100644 --- a/third_party/python/pytest/_pytest/nose.py +++ b/third_party/python/pytest/src/_pytest/nose.py @@ -3,16 +3,15 @@ from __future__ import absolute_import, division, print_function import sys -import py from _pytest import unittest, runner, python from _pytest.config import hookimpl def get_skip_exceptions(): skip_classes = set() - for module_name in ('unittest', 'unittest2', 'nose'): + for module_name in ("unittest", "unittest2", "nose"): mod = sys.modules.get(module_name) - if hasattr(mod, 'SkipTest'): + if hasattr(mod, "SkipTest"): skip_classes.add(mod.SkipTest) return tuple(skip_classes) @@ -20,8 +19,7 @@ def get_skip_exceptions(): def pytest_runtest_makereport(item, call): if call.excinfo and call.excinfo.errisinstance(get_skip_exceptions()): # let's substitute the excinfo with a pytest.skip one - call2 = call.__class__( - lambda: runner.skip(str(call.excinfo.value)), call.when) + call2 = call.__class__(lambda: runner.skip(str(call.excinfo.value)), call.when) call.excinfo = call2.excinfo @@ -30,22 +28,22 @@ def pytest_runtest_setup(item): if is_potential_nosetest(item): if isinstance(item.parent, python.Generator): gen = item.parent - if not hasattr(gen, '_nosegensetup'): - call_optional(gen.obj, 'setup') + if not hasattr(gen, "_nosegensetup"): + call_optional(gen.obj, "setup") if isinstance(gen.parent, python.Instance): - call_optional(gen.parent.obj, 'setup') + call_optional(gen.parent.obj, "setup") gen._nosegensetup = True - if not call_optional(item.obj, 'setup'): + if not call_optional(item.obj, "setup"): # call module level setup if there is no object level one - call_optional(item.parent.obj, 'setup') + call_optional(item.parent.obj, "setup") # XXX this implies we only call teardown when setup worked item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item) def teardown_nose(item): if is_potential_nosetest(item): - if not call_optional(item.obj, 'teardown'): - call_optional(item.parent.obj, 'teardown') + if not call_optional(item.obj, "teardown"): + call_optional(item.parent.obj, "teardown") # if hasattr(item.parent, '_nosegensetup'): # #call_optional(item._nosegensetup, 'teardown') # del item.parent._nosegensetup @@ -53,20 +51,21 @@ def teardown_nose(item): def pytest_make_collect_report(collector): if isinstance(collector, python.Generator): - call_optional(collector.obj, 'setup') + call_optional(collector.obj, "setup") def is_potential_nosetest(item): # extra check needed since we do not do nose style setup/teardown # on direct unittest style classes - return isinstance(item, python.Function) and \ - not isinstance(item, unittest.TestCaseFunction) + return isinstance(item, python.Function) and not isinstance( + item, unittest.TestCaseFunction + ) def call_optional(obj, name): method = getattr(obj, name, None) isfixture = hasattr(method, "_pytestfixturefunction") - if method is not None and not isfixture and py.builtin.callable(method): + if method is not None and not isfixture and callable(method): # If there's any problems allow the exception to raise rather than # silently ignoring them method() diff --git a/third_party/python/pytest/_pytest/outcomes.py b/third_party/python/pytest/src/_pytest/outcomes.py similarity index 74% rename from third_party/python/pytest/_pytest/outcomes.py rename to third_party/python/pytest/src/_pytest/outcomes.py index ff5ef756d91b..8a3662e1b3e7 100644 --- a/third_party/python/pytest/_pytest/outcomes.py +++ b/third_party/python/pytest/src/_pytest/outcomes.py @@ -11,6 +11,7 @@ class OutcomeException(BaseException): """ OutcomeException and its subclass instances indicate and contain info about test and collection outcomes. """ + def __init__(self, msg=None, pytrace=True): BaseException.__init__(self, msg) self.msg = msg @@ -20,9 +21,10 @@ class OutcomeException(BaseException): if self.msg: val = self.msg if isinstance(val, bytes): - val = py._builtin._totext(val, errors='replace') + val = py._builtin._totext(val, errors="replace") return val return "<%s instance>" % (self.__class__.__name__,) + __str__ = __repr__ @@ -32,7 +34,7 @@ TEST_OUTCOME = (OutcomeException, Exception) class Skipped(OutcomeException): # XXX hackish: on 3k we fake to live in the builtins # in order to have Skipped exception printing shorter/nicer - __module__ = 'builtins' + __module__ = "builtins" def __init__(self, msg=None, pytrace=True, allow_module_level=False): OutcomeException.__init__(self, msg=msg, pytrace=pytrace) @@ -41,15 +43,17 @@ class Skipped(OutcomeException): class Failed(OutcomeException): """ raised from an explicit call to pytest.fail() """ - __module__ = 'builtins' + __module__ = "builtins" class Exit(KeyboardInterrupt): """ raised for immediate program exits (no tracebacks/summaries)""" + def __init__(self, msg="unknown reason"): self.msg = msg KeyboardInterrupt.__init__(self, msg) + # exposed helper methods @@ -62,21 +66,28 @@ def exit(msg): exit.Exception = Exit -def skip(msg=""): +def skip(msg="", **kwargs): """ skip an executing test with the given message. Note: it's usually better to use the pytest.mark.skipif marker to declare a test to be skipped under certain conditions like mismatching platforms or dependencies. See the pytest_skipping plugin for details. + + :kwarg bool allow_module_level: allows this function to be called at + module level, skipping the rest of the module. Default to False. """ __tracebackhide__ = True - raise Skipped(msg=msg) + allow_module_level = kwargs.pop("allow_module_level", False) + if kwargs: + keys = [k for k in kwargs.keys()] + raise TypeError("unexpected keyword arguments: {}".format(keys)) + raise Skipped(msg=msg, allow_module_level=allow_module_level) skip.Exception = Skipped def fail(msg="", pytrace=True): - """ explicitly fail an currently-executing test with the given Message. + """ explicitly fail a currently-executing test with the given Message. :arg pytrace: if false the msg represents the full failure information and no python traceback will be reported. @@ -107,15 +118,16 @@ def importorskip(modname, minversion=None): is only triggered if the module can not be imported. """ import warnings + __tracebackhide__ = True - compile(modname, '', 'eval') # to catch syntaxerrors + compile(modname, "", "eval") # to catch syntaxerrors should_skip = False with warnings.catch_warnings(): # make sure to ignore ImportWarnings that might happen because # of existing directories with the same name we're trying to # import but without a __init__.py file - warnings.simplefilter('ignore') + warnings.simplefilter("ignore") try: __import__(modname) except ImportError: @@ -126,15 +138,20 @@ def importorskip(modname, minversion=None): mod = sys.modules[modname] if minversion is None: return mod - verattr = getattr(mod, '__version__', None) + verattr = getattr(mod, "__version__", None) if minversion is not None: try: from pkg_resources import parse_version as pv except ImportError: - raise Skipped("we have a required version for %r but can not import " - "pkg_resources to parse version strings." % (modname,), - allow_module_level=True) + raise Skipped( + "we have a required version for %r but can not import " + "pkg_resources to parse version strings." % (modname,), + allow_module_level=True, + ) if verattr is None or pv(verattr) < pv(minversion): - raise Skipped("module %r has __version__ %r, required is: %r" % ( - modname, verattr, minversion), allow_module_level=True) + raise Skipped( + "module %r has __version__ %r, required is: %r" + % (modname, verattr, minversion), + allow_module_level=True, + ) return mod diff --git a/third_party/python/pytest/_pytest/pastebin.py b/third_party/python/pytest/src/_pytest/pastebin.py similarity index 66% rename from third_party/python/pytest/_pytest/pastebin.py rename to third_party/python/pytest/src/_pytest/pastebin.py index 9d689819f095..6af202d1f0a9 100644 --- a/third_party/python/pytest/_pytest/pastebin.py +++ b/third_party/python/pytest/src/_pytest/pastebin.py @@ -2,50 +2,55 @@ from __future__ import absolute_import, division, print_function import pytest +import six import sys import tempfile def pytest_addoption(parser): group = parser.getgroup("terminal reporting") - group._addoption('--pastebin', metavar="mode", - action='store', dest="pastebin", default=None, - choices=['failed', 'all'], - help="send failed|all info to bpaste.net pastebin service.") + group._addoption( + "--pastebin", + metavar="mode", + action="store", + dest="pastebin", + default=None, + choices=["failed", "all"], + help="send failed|all info to bpaste.net pastebin service.", + ) @pytest.hookimpl(trylast=True) def pytest_configure(config): - import py if config.option.pastebin == "all": - tr = config.pluginmanager.getplugin('terminalreporter') + tr = config.pluginmanager.getplugin("terminalreporter") # if no terminal reporter plugin is present, nothing we can do here; # this can happen when this function executes in a slave node # when using pytest-xdist, for example if tr is not None: # pastebin file will be utf-8 encoded binary file - config._pastebinfile = tempfile.TemporaryFile('w+b') + config._pastebinfile = tempfile.TemporaryFile("w+b") oldwrite = tr._tw.write def tee_write(s, **kwargs): oldwrite(s, **kwargs) - if py.builtin._istext(s): - s = s.encode('utf-8') + if isinstance(s, six.text_type): + s = s.encode("utf-8") config._pastebinfile.write(s) tr._tw.write = tee_write def pytest_unconfigure(config): - if hasattr(config, '_pastebinfile'): + if hasattr(config, "_pastebinfile"): # get terminal contents and delete file config._pastebinfile.seek(0) sessionlog = config._pastebinfile.read() config._pastebinfile.close() del config._pastebinfile # undo our patching in the terminal reporter - tr = config.pluginmanager.getplugin('terminalreporter') - del tr._tw.__dict__['write'] + tr = config.pluginmanager.getplugin("terminalreporter") + del tr._tw.__dict__["write"] # write summary tr.write_sep("=", "Sending information to Paste Service") pastebinurl = create_new_paste(sessionlog) @@ -60,6 +65,7 @@ def create_new_paste(contents): :returns: url to the pasted contents """ import re + if sys.version_info < (3, 0): from urllib import urlopen, urlencode else: @@ -67,32 +73,35 @@ def create_new_paste(contents): from urllib.parse import urlencode params = { - 'code': contents, - 'lexer': 'python3' if sys.version_info[0] == 3 else 'python', - 'expiry': '1week', + "code": contents, + "lexer": "python3" if sys.version_info[0] == 3 else "python", + "expiry": "1week", } - url = 'https://bpaste.net' - response = urlopen(url, data=urlencode(params).encode('ascii')).read() - m = re.search(r'href="/raw/(\w+)"', response.decode('utf-8')) + url = "https://bpaste.net" + response = urlopen(url, data=urlencode(params).encode("ascii")).read() + m = re.search(r'href="/raw/(\w+)"', response.decode("utf-8")) if m: - return '%s/show/%s' % (url, m.group(1)) + return "%s/show/%s" % (url, m.group(1)) else: - return 'bad response: ' + response + return "bad response: " + response def pytest_terminal_summary(terminalreporter): import _pytest.config + if terminalreporter.config.option.pastebin != "failed": return tr = terminalreporter - if 'failed' in tr.stats: + if "failed" in tr.stats: terminalreporter.write_sep("=", "Sending information to Paste Service") - for rep in terminalreporter.stats.get('failed'): + for rep in terminalreporter.stats.get("failed"): try: msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc except AttributeError: msg = tr._getfailureheadline(rep) - tw = _pytest.config.create_terminal_writer(terminalreporter.config, stringio=True) + tw = _pytest.config.create_terminal_writer( + terminalreporter.config, stringio=True + ) rep.toterminal(tw) s = tw.stringio.getvalue() assert len(s) diff --git a/third_party/python/pytest/_pytest/pytester.py b/third_party/python/pytest/src/_pytest/pytester.py similarity index 61% rename from third_party/python/pytest/_pytest/pytester.py rename to third_party/python/pytest/src/_pytest/pytester.py index 82aa00e0d2a3..c9defe03a668 100644 --- a/third_party/python/pytest/_pytest/pytester.py +++ b/third_party/python/pytest/src/_pytest/pytester.py @@ -1,4 +1,4 @@ -""" (disabled by default) support for testing pytest and pytest plugins. """ +"""(disabled by default) support for testing pytest and pytest plugins.""" from __future__ import absolute_import, division, print_function import codecs @@ -7,6 +7,7 @@ import os import platform import re import subprocess +import six import sys import time import traceback @@ -22,27 +23,38 @@ from _pytest.main import Session, EXIT_OK from _pytest.assertion.rewrite import AssertionRewritingHook -def pytest_addoption(parser): - # group = parser.getgroup("pytester", "pytester (self-tests) options") - parser.addoption('--lsof', - action="store_true", dest="lsof", default=False, - help=("run FD checks if lsof is available")) +PYTEST_FULLPATH = os.path.abspath(pytest.__file__.rstrip("oc")).replace( + "$py.class", ".py" +) - parser.addoption('--runpytest', default="inprocess", dest="runpytest", - choices=("inprocess", "subprocess", ), - help=("run pytest sub runs in tests using an 'inprocess' " - "or 'subprocess' (python -m main) method")) + +IGNORE_PAM = [ # filenames added when obtaining details about the current user + u"/var/lib/sss/mc/passwd" +] + + +def pytest_addoption(parser): + parser.addoption( + "--lsof", + action="store_true", + dest="lsof", + default=False, + help=("run FD checks if lsof is available"), + ) + + parser.addoption( + "--runpytest", + default="inprocess", + dest="runpytest", + choices=("inprocess", "subprocess"), + help=( + "run pytest sub runs in tests using an 'inprocess' " + "or 'subprocess' (python -m main) method" + ), + ) def pytest_configure(config): - # This might be called multiple times. Only take the first. - global _pytest_fullpath - try: - _pytest_fullpath - except NameError: - _pytest_fullpath = os.path.abspath(pytest.__file__.rstrip("oc")) - _pytest_fullpath = _pytest_fullpath.replace("$py.class", ".py") - if config.getvalue("lsof"): checker = LsofFdLeakChecker() if checker.matching_platform(): @@ -50,6 +62,7 @@ def pytest_configure(config): class LsofFdLeakChecker(object): + def get_open_files(self): out = self._exec_lsof() open_files = self._parse_lsof_output(out) @@ -60,18 +73,25 @@ class LsofFdLeakChecker(object): return py.process.cmdexec("lsof -Ffn0 -p %d" % pid) def _parse_lsof_output(self, out): + def isopen(line): - return line.startswith('f') and ("deleted" not in line and - 'mem' not in line and "txt" not in line and 'cwd' not in line) + return line.startswith("f") and ( + "deleted" not in line + and "mem" not in line + and "txt" not in line + and "cwd" not in line + ) open_files = [] for line in out.split("\n"): if isopen(line): - fields = line.split('\0') + fields = line.split("\0") fd = fields[0][1:] filename = fields[1][1:] - if filename.startswith('/'): + if filename in IGNORE_PAM: + continue + if filename.startswith("/"): open_files.append((fd, filename)) return open_files @@ -80,8 +100,8 @@ class LsofFdLeakChecker(object): try: py.process.cmdexec("lsof -v") except (py.process.cmdexec.Error, UnicodeDecodeError): - # cmdexec may raise UnicodeDecodeError on Windows systems - # with locale other than english: + # cmdexec may raise UnicodeDecodeError on Windows systems with + # locale other than English: # https://bitbucket.org/pytest-dev/py/issues/66 return False else: @@ -95,7 +115,7 @@ class LsofFdLeakChecker(object): gc.collect() lines2 = self.get_open_files() - new_fds = set([t[0] for t in lines2]) - set([t[0] for t in lines1]) + new_fds = {t[0] for t in lines2} - {t[0] for t in lines1} leaked_files = [t for t in lines2 if t[0] in new_fds] if leaked_files: error = [] @@ -108,18 +128,15 @@ class LsofFdLeakChecker(object): error.append(error[0]) error.append("*** function %s:%s: %s " % item.location) error.append("See issue #2366") - item.warn('', "\n".join(error)) + item.warn("", "\n".join(error)) # XXX copied from execnet's conftest.py - needs to be merged winpymap = { - 'python2.7': r'C:\Python27\python.exe', - 'python2.6': r'C:\Python26\python.exe', - 'python3.1': r'C:\Python31\python.exe', - 'python3.2': r'C:\Python32\python.exe', - 'python3.3': r'C:\Python33\python.exe', - 'python3.4': r'C:\Python34\python.exe', - 'python3.5': r'C:\Python35\python.exe', + "python2.7": r"C:\Python27\python.exe", + "python3.4": r"C:\Python34\python.exe", + "python3.5": r"C:\Python35\python.exe", + "python3.6": r"C:\Python36\python.exe", } @@ -130,8 +147,12 @@ def getexecutable(name, cache={}): executable = py.path.local.sysfind(name) if executable: import subprocess - popen = subprocess.Popen([str(executable), "--version"], - universal_newlines=True, stderr=subprocess.PIPE) + + popen = subprocess.Popen( + [str(executable), "--version"], + universal_newlines=True, + stderr=subprocess.PIPE, + ) out, err = popen.communicate() if name == "jython": if not err or "2.5" not in err: @@ -139,14 +160,13 @@ def getexecutable(name, cache={}): if "2.5.2" in err: executable = None # http://bugs.jython.org/issue1790 elif popen.returncode != 0: - # Handle pyenv's 127. + # handle pyenv's 127 executable = None cache[name] = executable return executable -@pytest.fixture(params=['python2.6', 'python2.7', 'python3.3', "python3.4", - 'pypy', 'pypy3']) +@pytest.fixture(params=["python2.7", "python3.4", "pypy", "pypy3"]) def anypython(request): name = request.param executable = getexecutable(name) @@ -160,19 +180,22 @@ def anypython(request): pytest.skip("no suitable %s found" % (name,)) return executable + # used at least by pytest-xdist plugin @pytest.fixture def _pytest(request): - """ Return a helper which offers a gethookrecorder(hook) - method which returns a HookRecorder instance which helps - to make assertions about called hooks. + """Return a helper which offers a gethookrecorder(hook) method which + returns a HookRecorder instance which helps to make assertions about called + hooks. + """ return PytestArg(request) -class PytestArg: +class PytestArg(object): + def __init__(self, request): self.request = request @@ -187,22 +210,23 @@ def get_public_names(values): return [x for x in values if x[0] != "_"] -class ParsedCall: +class ParsedCall(object): + def __init__(self, name, kwargs): self.__dict__.update(kwargs) self._name = name def __repr__(self): d = self.__dict__.copy() - del d['_name'] + del d["_name"] return "" % (self._name, d) -class HookRecorder: +class HookRecorder(object): """Record all hooks called in a plugin manager. - This wraps all the hook calls in the plugin manager, recording - each call before propagating the normal calls. + This wraps all the hook calls in the plugin manager, recording each call + before propagating the normal calls. """ @@ -264,13 +288,16 @@ class HookRecorder: # functionality for test reports - def getreports(self, - names="pytest_runtest_logreport pytest_collectreport"): + def getreports(self, names="pytest_runtest_logreport pytest_collectreport"): return [x.report for x in self.getcalls(names)] - def matchreport(self, inamepart="", - names="pytest_runtest_logreport pytest_collectreport", when=None): - """ return a testreport whose dotted import path matches """ + def matchreport( + self, + inamepart="", + names="pytest_runtest_logreport pytest_collectreport", + when=None, + ): + """return a testreport whose dotted import path matches""" values = [] for rep in self.getreports(names=names): try: @@ -279,31 +306,32 @@ class HookRecorder: continue except AttributeError: pass - if when and getattr(rep, 'when', None) != when: + if when and getattr(rep, "when", None) != when: continue if not inamepart or inamepart in rep.nodeid.split("::"): values.append(rep) if not values: - raise ValueError("could not find test report matching %r: " - "no test reports at all!" % (inamepart,)) + raise ValueError( + "could not find test report matching %r: " + "no test reports at all!" % (inamepart,) + ) if len(values) > 1: raise ValueError( - "found 2 or more testreports matching %r: %s" % (inamepart, values)) + "found 2 or more testreports matching %r: %s" % (inamepart, values) + ) return values[0] - def getfailures(self, - names='pytest_runtest_logreport pytest_collectreport'): + def getfailures(self, names="pytest_runtest_logreport pytest_collectreport"): return [rep for rep in self.getreports(names) if rep.failed] def getfailedcollections(self): - return self.getfailures('pytest_collectreport') + return self.getfailures("pytest_collectreport") def listoutcomes(self): passed = [] skipped = [] failed = [] - for rep in self.getreports( - "pytest_collectreport pytest_runtest_logreport"): + for rep in self.getreports("pytest_collectreport pytest_runtest_logreport"): if rep.passed: if getattr(rep, "when", None) == "call": passed.append(rep) @@ -331,7 +359,7 @@ def linecomp(request): return LineComp() -@pytest.fixture(name='LineMatcher') +@pytest.fixture(name="LineMatcher") def LineMatcher_fixture(request): return LineMatcher @@ -344,19 +372,19 @@ def testdir(request, tmpdir_factory): rex_outcome = re.compile(r"(\d+) ([\w-]+)") -class RunResult: +class RunResult(object): """The result of running a command. Attributes: - :ret: The return value. - :outlines: List of lines captured from stdout. - :errlines: List of lines captures from stderr. + :ret: the return value + :outlines: list of lines captured from stdout + :errlines: list of lines captures from stderr :stdout: :py:class:`LineMatcher` of stdout, use ``stdout.str()`` to - reconstruct stdout or the commonly used - ``stdout.fnmatch_lines()`` method. - :stderrr: :py:class:`LineMatcher` of stderr. - :duration: Duration in seconds. + reconstruct stdout or the commonly used ``stdout.fnmatch_lines()`` + method + :stderr: :py:class:`LineMatcher` of stderr + :duration: duration in seconds """ @@ -369,10 +397,12 @@ class RunResult: self.duration = duration def parseoutcomes(self): - """ Return a dictionary of outcomestring->num from parsing - the terminal output that the test process produced.""" + """Return a dictionary of outcomestring->num from parsing the terminal + output that the test process produced. + + """ for line in reversed(self.outlines): - if 'seconds' in line: + if "seconds" in line: outcomes = rex_outcome.findall(line) if outcomes: d = {} @@ -382,56 +412,83 @@ class RunResult: raise ValueError("Pytest terminal report not found") def assert_outcomes(self, passed=0, skipped=0, failed=0, error=0): - """ assert that the specified outcomes appear with the respective - numbers (0 means it didn't occur) in the text output from a test run.""" + """Assert that the specified outcomes appear with the respective + numbers (0 means it didn't occur) in the text output from a test run. + + """ d = self.parseoutcomes() obtained = { - 'passed': d.get('passed', 0), - 'skipped': d.get('skipped', 0), - 'failed': d.get('failed', 0), - 'error': d.get('error', 0), + "passed": d.get("passed", 0), + "skipped": d.get("skipped", 0), + "failed": d.get("failed", 0), + "error": d.get("error", 0), } - assert obtained == dict(passed=passed, skipped=skipped, failed=failed, error=error) + assert ( + obtained == dict(passed=passed, skipped=skipped, failed=failed, error=error) + ) -class Testdir: +class CwdSnapshot(object): + + def __init__(self): + self.__saved = os.getcwd() + + def restore(self): + os.chdir(self.__saved) + + +class SysModulesSnapshot(object): + + def __init__(self, preserve=None): + self.__preserve = preserve + self.__saved = dict(sys.modules) + + def restore(self): + if self.__preserve: + self.__saved.update( + (k, m) for k, m in sys.modules.items() if self.__preserve(k) + ) + sys.modules.clear() + sys.modules.update(self.__saved) + + +class SysPathsSnapshot(object): + + def __init__(self): + self.__saved = list(sys.path), list(sys.meta_path) + + def restore(self): + sys.path[:], sys.meta_path[:] = self.__saved + + +class Testdir(object): """Temporary test directory with tools to test/run pytest itself. - This is based on the ``tmpdir`` fixture but provides a number of - methods which aid with testing pytest itself. Unless - :py:meth:`chdir` is used all methods will use :py:attr:`tmpdir` as - current working directory. + This is based on the ``tmpdir`` fixture but provides a number of methods + which aid with testing pytest itself. Unless :py:meth:`chdir` is used all + methods will use :py:attr:`tmpdir` as their current working directory. Attributes: - :tmpdir: The :py:class:`py.path.local` instance of the temporary - directory. + :tmpdir: The :py:class:`py.path.local` instance of the temporary directory. :plugins: A list of plugins to use with :py:meth:`parseconfig` and - :py:meth:`runpytest`. Initially this is an empty list but - plugins can be added to the list. The type of items to add to - the list depend on the method which uses them so refer to them - for details. + :py:meth:`runpytest`. Initially this is an empty list but plugins can + be added to the list. The type of items to add to the list depends on + the method using them so refer to them for details. """ def __init__(self, request, tmpdir_factory): self.request = request self._mod_collections = WeakKeyDictionary() - # XXX remove duplication with tmpdir plugin - basetmp = tmpdir_factory.ensuretemp("testdir") name = request.function.__name__ - for i in range(100): - try: - tmpdir = basetmp.mkdir(name + str(i)) - except py.error.EEXIST: - continue - break - self.tmpdir = tmpdir + self.tmpdir = tmpdir_factory.mktemp(name, numbered=True) self.plugins = [] - self._savesyspath = (list(sys.path), list(sys.meta_path)) - self._savemodulekeys = set(sys.modules) - self.chdir() # always chdir + self._cwd_snapshot = CwdSnapshot() + self._sys_path_snapshot = SysPathsSnapshot() + self._sys_modules_snapshot = self.__take_sys_modules_snapshot() + self.chdir() self.request.addfinalizer(self.finalize) method = self.request.config.getoption("--runpytest") if method == "inprocess": @@ -445,29 +502,23 @@ class Testdir: def finalize(self): """Clean up global state artifacts. - Some methods modify the global interpreter state and this - tries to clean this up. It does not remove the temporary - directory however so it can be looked at after the test run - has finished. + Some methods modify the global interpreter state and this tries to + clean this up. It does not remove the temporary directory however so + it can be looked at after the test run has finished. """ - sys.path[:], sys.meta_path[:] = self._savesyspath - if hasattr(self, '_olddir'): - self._olddir.chdir() - self.delete_loaded_modules() + self._sys_modules_snapshot.restore() + self._sys_path_snapshot.restore() + self._cwd_snapshot.restore() - def delete_loaded_modules(self): - """Delete modules that have been loaded during a test. + def __take_sys_modules_snapshot(self): + # some zope modules used by twisted-related tests keep internal state + # and can't be deleted; we had some trouble in the past with + # `zope.interface` for example + def preserve_module(name): + return name.startswith("zope") - This allows the interpreter to catch module changes in case - the module is re-imported. - """ - for name in set(sys.modules).difference(self._savemodulekeys): - # some zope modules used by twisted-related tests keeps internal - # state and can't be deleted; we had some trouble in the past - # with zope.interface for example - if not name.startswith("zope"): - del sys.modules[name] + return SysModulesSnapshot(preserve=preserve_module) def make_hook_recorder(self, pluginmanager): """Create a new :py:class:`HookRecorder` for a PluginManager.""" @@ -482,33 +533,26 @@ class Testdir: This is done automatically upon instantiation. """ - old = self.tmpdir.chdir() - if not hasattr(self, '_olddir'): - self._olddir = old + self.tmpdir.chdir() def _makefile(self, ext, args, kwargs, encoding="utf-8"): items = list(kwargs.items()) + + def to_text(s): + return s.decode(encoding) if isinstance(s, bytes) else six.text_type(s) + if args: - source = py.builtin._totext("\n").join( - map(py.builtin._totext, args)) + py.builtin._totext("\n") + source = u"\n".join(to_text(x) for x in args) basename = self.request.function.__name__ items.insert(0, (basename, source)) + ret = None - for name, value in items: - p = self.tmpdir.join(name).new(ext=ext) + for basename, value in items: + p = self.tmpdir.join(basename).new(ext=ext) p.dirpath().ensure_dir() source = Source(value) - - def my_totext(s, encoding="utf-8"): - if py.builtin._isbytes(s): - s = py.builtin._totext(s, encoding=encoding) - return s - - source_unicode = "\n".join([my_totext(line) for line in source.lines]) - source = py.builtin._totext(source_unicode) - content = source.strip().encode(encoding) # + "\n" - # content = content.rstrip() + "\n" - p.write(content, "wb") + source = u"\n".join(to_text(line) for line in source.lines) + p.write(source.strip().encode(encoding), "wb") if ret is None: ret = p return ret @@ -516,17 +560,15 @@ class Testdir: def makefile(self, ext, *args, **kwargs): """Create a new file in the testdir. - ext: The extension the file should use, including the dot. - E.g. ".py". + ext: The extension the file should use, including the dot, e.g. `.py`. - args: All args will be treated as strings and joined using - newlines. The result will be written as contents to the - file. The name of the file will be based on the test - function requesting this fixture. + args: All args will be treated as strings and joined using newlines. + The result will be written as contents to the file. The name of the + file will be based on the test function requesting this fixture. E.g. "testdir.makefile('.txt', 'line1', 'line2')" - kwargs: Each keyword is the name of a file, while the value of - it will be written as contents of the file. + kwargs: Each keyword is the name of a file, while the value of it will + be written as contents of the file. E.g. "testdir.makefile('.ini', pytest='[pytest]\naddopts=-rs\n')" """ @@ -538,32 +580,34 @@ class Testdir: def makeini(self, source): """Write a tox.ini file with 'source' as contents.""" - return self.makefile('.ini', tox=source) + return self.makefile(".ini", tox=source) def getinicfg(self, source): """Return the pytest section from the tox.ini config file.""" p = self.makeini(source) - return py.iniconfig.IniConfig(p)['pytest'] + return py.iniconfig.IniConfig(p)["pytest"] def makepyfile(self, *args, **kwargs): """Shortcut for .makefile() with a .py extension.""" - return self._makefile('.py', args, kwargs) + return self._makefile(".py", args, kwargs) def maketxtfile(self, *args, **kwargs): """Shortcut for .makefile() with a .txt extension.""" - return self._makefile('.txt', args, kwargs) + return self._makefile(".txt", args, kwargs) def syspathinsert(self, path=None): """Prepend a directory to sys.path, defaults to :py:attr:`tmpdir`. - This is undone automatically after the test. + This is undone automatically when this object dies at the end of each + test. + """ if path is None: path = self.tmpdir sys.path.insert(0, str(path)) - # a call to syspathinsert() usually means that the caller - # wants to import some dynamically created files. - # with python3 we thus invalidate import caches. + # a call to syspathinsert() usually means that the caller wants to + # import some dynamically created files, thus with python3 we + # invalidate its import caches self._possibly_invalidate_import_caches() def _possibly_invalidate_import_caches(self): @@ -583,8 +627,8 @@ class Testdir: def mkpydir(self, name): """Create a new python package. - This creates a (sub)directory with an empty ``__init__.py`` - file so that is recognised as a python package. + This creates a (sub)directory with an empty ``__init__.py`` file so it + gets recognised as a python package. """ p = self.mkdir(name) @@ -597,14 +641,14 @@ class Testdir: """Return the collection node of a file. :param config: :py:class:`_pytest.config.Config` instance, see - :py:meth:`parseconfig` and :py:meth:`parseconfigure` to - create the configuration. + :py:meth:`parseconfig` and :py:meth:`parseconfigure` to create the + configuration - :param arg: A :py:class:`py.path.local` instance of the file. + :param arg: a :py:class:`py.path.local` instance of the file """ session = Session(config) - assert '::' not in str(arg) + assert "::" not in str(arg) p = py.path.local(arg) config.hook.pytest_sessionstart(session=session) res = session.perform_collect([str(p)], genitems=False)[0] @@ -614,11 +658,10 @@ class Testdir: def getpathnode(self, path): """Return the collection node of a file. - This is like :py:meth:`getnode` but uses - :py:meth:`parseconfigure` to create the (configured) pytest - Config instance. + This is like :py:meth:`getnode` but uses :py:meth:`parseconfigure` to + create the (configured) pytest Config instance. - :param path: A :py:class:`py.path.local` instance of the file. + :param path: a :py:class:`py.path.local` instance of the file """ config = self.parseconfigure(path) @@ -632,8 +675,8 @@ class Testdir: def genitems(self, colitems): """Generate all test items from a collection node. - This recurses into the collection node and returns a list of - all the test items contained within. + This recurses into the collection node and returns a list of all the + test items contained within. """ session = colitems[0].session @@ -645,10 +688,10 @@ class Testdir: def runitem(self, source): """Run the "test_func" Item. - The calling test instance (the class which contains the test - method) must provide a ``.getrunner()`` method which should - return a runner which can run the test protocol for a single - item, like e.g. :py:func:`_pytest.runner.runtestprotocol`. + The calling test instance (class containing the test method) must + provide a ``.getrunner()`` method which should return a runner which + can run the test protocol for a single item, e.g. + :py:func:`_pytest.runner.runtestprotocol`. """ # used from runner functional tests @@ -662,14 +705,14 @@ class Testdir: """Run a test module in process using ``pytest.main()``. This run writes "source" into a temporary file and runs - ``pytest.main()`` on it, returning a :py:class:`HookRecorder` - instance for the result. + ``pytest.main()`` on it, returning a :py:class:`HookRecorder` instance + for the result. - :param source: The source code of the test module. + :param source: the source code of the test module - :param cmdlineargs: Any extra command line arguments to use. + :param cmdlineargs: any extra command line arguments to use - :return: :py:class:`HookRecorder` instance of the result. + :return: :py:class:`HookRecorder` instance of the result """ p = self.makepyfile(source) @@ -679,13 +722,9 @@ class Testdir: def inline_genitems(self, *args): """Run ``pytest.main(['--collectonly'])`` in-process. - Returns a tuple of the collected items and a - :py:class:`HookRecorder` instance. - - This runs the :py:func:`pytest.main` function to run all of - pytest inside the test process itself like - :py:meth:`inline_run`. However the return value is a tuple of - the collection items and a :py:class:`HookRecorder` instance. + Runs the :py:func:`pytest.main` function to run all of pytest inside + the test process itself like :py:meth:`inline_run`, but returns a + tuple of the collected items and a :py:class:`HookRecorder` instance. """ rec = self.inline_run("--collect-only", *args) @@ -695,60 +734,82 @@ class Testdir: def inline_run(self, *args, **kwargs): """Run ``pytest.main()`` in-process, returning a HookRecorder. - This runs the :py:func:`pytest.main` function to run all of - pytest inside the test process itself. This means it can - return a :py:class:`HookRecorder` instance which gives more - detailed results from then run then can be done by matching - stdout/stderr from :py:meth:`runpytest`. + Runs the :py:func:`pytest.main` function to run all of pytest inside + the test process itself. This means it can return a + :py:class:`HookRecorder` instance which gives more detailed results + from that run than can be done by matching stdout/stderr from + :py:meth:`runpytest`. - :param args: Any command line arguments to pass to - :py:func:`pytest.main`. + :param args: command line arguments to pass to :py:func:`pytest.main` - :param plugin: (keyword-only) Extra plugin instances the - ``pytest.main()`` instance should use. + :param plugin: (keyword-only) extra plugin instances the + ``pytest.main()`` instance should use + + :return: a :py:class:`HookRecorder` instance - :return: A :py:class:`HookRecorder` instance. """ - # When running py.test inline any plugins active in the main - # test process are already imported. So this disables the - # warning which will trigger to say they can no longer be - # re-written, which is fine as they are already re-written. - orig_warn = AssertionRewritingHook._warn_already_imported + finalizers = [] + try: + # When running pytest inline any plugins active in the main test + # process are already imported. So this disables the warning which + # will trigger to say they can no longer be rewritten, which is + # fine as they have already been rewritten. + orig_warn = AssertionRewritingHook._warn_already_imported - def revert(): - AssertionRewritingHook._warn_already_imported = orig_warn + def revert_warn_already_imported(): + AssertionRewritingHook._warn_already_imported = orig_warn - self.request.addfinalizer(revert) - AssertionRewritingHook._warn_already_imported = lambda *a: None + finalizers.append(revert_warn_already_imported) + AssertionRewritingHook._warn_already_imported = lambda *a: None - rec = [] + # Any sys.module or sys.path changes done while running pytest + # inline should be reverted after the test run completes to avoid + # clashing with later inline tests run within the same pytest test, + # e.g. just because they use matching test module names. + finalizers.append(self.__take_sys_modules_snapshot().restore) + finalizers.append(SysPathsSnapshot().restore) - class Collect: - def pytest_configure(x, config): - rec.append(self.make_hook_recorder(config.pluginmanager)) + # Important note: + # - our tests should not leave any other references/registrations + # laying around other than possibly loaded test modules + # referenced from sys.modules, as nothing will clean those up + # automatically - plugins = kwargs.get("plugins") or [] - plugins.append(Collect()) - ret = pytest.main(list(args), plugins=plugins) - self.delete_loaded_modules() - if len(rec) == 1: - reprec = rec.pop() - else: - class reprec: - pass - reprec.ret = ret + rec = [] - # typically we reraise keyboard interrupts from the child run - # because it's our user requesting interruption of the testing - if ret == 2 and not kwargs.get("no_reraise_ctrlc"): - calls = reprec.getcalls("pytest_keyboard_interrupt") - if calls and calls[-1].excinfo.type == KeyboardInterrupt: - raise KeyboardInterrupt() - return reprec + class Collect(object): + + def pytest_configure(x, config): + rec.append(self.make_hook_recorder(config.pluginmanager)) + + plugins = kwargs.get("plugins") or [] + plugins.append(Collect()) + ret = pytest.main(list(args), plugins=plugins) + if len(rec) == 1: + reprec = rec.pop() + else: + + class reprec(object): + pass + + reprec.ret = ret + + # typically we reraise keyboard interrupts from the child run + # because it's our user requesting interruption of the testing + if ret == 2 and not kwargs.get("no_reraise_ctrlc"): + calls = reprec.getcalls("pytest_keyboard_interrupt") + if calls and calls[-1].excinfo.type == KeyboardInterrupt: + raise KeyboardInterrupt() + return reprec + finally: + for finalizer in finalizers: + finalizer() def runpytest_inprocess(self, *args, **kwargs): - """ Return result of running pytest in-process, providing a similar - interface to what self.runpytest() provides. """ + """Return result of running pytest in-process, providing a similar + interface to what self.runpytest() provides. + + """ if kwargs.get("syspathinsert"): self.syspathinsert() now = time.time() @@ -759,28 +820,27 @@ class Testdir: reprec = self.inline_run(*args, **kwargs) except SystemExit as e: - class reprec: + class reprec(object): ret = e.args[0] except Exception: traceback.print_exc() - class reprec: + class reprec(object): ret = 3 + finally: out, err = capture.readouterr() capture.stop_capturing() sys.stdout.write(out) sys.stderr.write(err) - res = RunResult(reprec.ret, - out.split("\n"), err.split("\n"), - time.time() - now) + res = RunResult(reprec.ret, out.split("\n"), err.split("\n"), time.time() - now) res.reprec = reprec return res def runpytest(self, *args, **kwargs): - """ Run pytest inline or in a subprocess, depending on the command line + """Run pytest inline or in a subprocess, depending on the command line option "--runpytest" and return a :py:class:`RunResult`. """ @@ -790,29 +850,30 @@ class Testdir: def _ensure_basetemp(self, args): args = [str(x) for x in args] for x in args: - if str(x).startswith('--basetemp'): + if str(x).startswith("--basetemp"): # print("basedtemp exists: %s" %(args,)) break else: - args.append("--basetemp=%s" % self.tmpdir.dirpath('basetemp')) + args.append("--basetemp=%s" % self.tmpdir.dirpath("basetemp")) # print("added basetemp: %s" %(args,)) return args def parseconfig(self, *args): """Return a new pytest Config instance from given commandline args. - This invokes the pytest bootstrapping code in _pytest.config - to create a new :py:class:`_pytest.core.PluginManager` and - call the pytest_cmdline_parse hook to create new + This invokes the pytest bootstrapping code in _pytest.config to create + a new :py:class:`_pytest.core.PluginManager` and call the + pytest_cmdline_parse hook to create a new :py:class:`_pytest.config.Config` instance. - If :py:attr:`plugins` has been populated they should be plugin - modules which will be registered with the PluginManager. + If :py:attr:`plugins` has been populated they should be plugin modules + to be registered with the PluginManager. """ args = self._ensure_basetemp(args) import _pytest.config + config = _pytest.config._prepareconfig(args, self.plugins) # we don't know what the test will do with this half-setup config # object and thus we make sure it gets unconfigured properly in any @@ -823,9 +884,8 @@ class Testdir: def parseconfigure(self, *args): """Return a new pytest configured Config instance. - This returns a new :py:class:`_pytest.config.Config` instance - like :py:meth:`parseconfig`, but also calls the - pytest_configure hook. + This returns a new :py:class:`_pytest.config.Config` instance like + :py:meth:`parseconfig`, but also calls the pytest_configure hook. """ config = self.parseconfig(*args) @@ -836,29 +896,29 @@ class Testdir: def getitem(self, source, funcname="test_func"): """Return the test item for a test function. - This writes the source to a python file and runs pytest's - collection on the resulting module, returning the test item - for the requested function name. + This writes the source to a python file and runs pytest's collection on + the resulting module, returning the test item for the requested + function name. - :param source: The module source. + :param source: the module source - :param funcname: The name of the test function for which the - Item must be returned. + :param funcname: the name of the test function for which to return a + test item """ items = self.getitems(source) for item in items: if item.name == funcname: return item - assert 0, "%r item not found in module:\n%s\nitems: %s" % ( - funcname, source, items) + assert 0, ( + "%r item not found in module:\n%s\nitems: %s" % (funcname, source, items) + ) def getitems(self, source): """Return all test items collected from the module. - This writes the source to a python file and runs pytest's - collection on the resulting module, returning all test items - contained within. + This writes the source to a python file and runs pytest's collection on + the resulting module, returning all test items contained within. """ modcol = self.getmodulecol(source) @@ -867,17 +927,17 @@ class Testdir: def getmodulecol(self, source, configargs=(), withinit=False): """Return the module collection node for ``source``. - This writes ``source`` to a file using :py:meth:`makepyfile` - and then runs the pytest collection on it, returning the - collection node for the test module. + This writes ``source`` to a file using :py:meth:`makepyfile` and then + runs the pytest collection on it, returning the collection node for the + test module. - :param source: The source code of the module to collect. + :param source: the source code of the module to collect - :param configargs: Any extra arguments to pass to - :py:meth:`parseconfigure`. + :param configargs: any extra arguments to pass to + :py:meth:`parseconfigure` - :param withinit: Whether to also write a ``__init__.py`` file - to the temporary directory to ensure it is a package. + :param withinit: whether to also write an ``__init__.py`` file to the + same directory to ensure it is a package """ kw = {self.request.function.__name__: Source(source).strip()} @@ -892,13 +952,12 @@ class Testdir: def collect_by_name(self, modcol, name): """Return the collection node for name from the module collection. - This will search a module collection node for a collection - node matching the given name. + This will search a module collection node for a collection node + matching the given name. - :param modcol: A module collection node, see - :py:meth:`getmodulecol`. + :param modcol: a module collection node; see :py:meth:`getmodulecol` - :param name: The name of the node to return. + :param name: the name of the node to return """ if modcol not in self._mod_collections: @@ -910,18 +969,21 @@ class Testdir: def popen(self, cmdargs, stdout, stderr, **kw): """Invoke subprocess.Popen. - This calls subprocess.Popen making sure the current working - directory is the PYTHONPATH. + This calls subprocess.Popen making sure the current working directory + is in the PYTHONPATH. You probably want to use :py:meth:`run` instead. """ env = os.environ.copy() - env['PYTHONPATH'] = os.pathsep.join(filter(None, [ - str(os.getcwd()), env.get('PYTHONPATH', '')])) - kw['env'] = env + env["PYTHONPATH"] = os.pathsep.join( + filter(None, [str(os.getcwd()), env.get("PYTHONPATH", "")]) + ) + kw["env"] = env - popen = subprocess.Popen(cmdargs, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, **kw) + popen = subprocess.Popen( + cmdargs, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, **kw + ) popen.stdin.close() return popen @@ -929,8 +991,7 @@ class Testdir: def run(self, *cmdargs): """Run a command with arguments. - Run a process using subprocess.Popen saving the stdout and - stderr. + Run a process using subprocess.Popen saving the stdout and stderr. Returns a :py:class:`RunResult`. @@ -941,14 +1002,15 @@ class Testdir: cmdargs = [str(x) for x in cmdargs] p1 = self.tmpdir.join("stdout") p2 = self.tmpdir.join("stderr") - print("running:", ' '.join(cmdargs)) + print("running:", " ".join(cmdargs)) print(" in:", str(py.path.local())) f1 = codecs.open(str(p1), "w", encoding="utf8") f2 = codecs.open(str(p2), "w", encoding="utf8") try: now = time.time() - popen = self.popen(cmdargs, stdout=f1, stderr=f2, - close_fds=(sys.platform != "win32")) + popen = self.popen( + cmdargs, stdout=f1, stderr=f2, close_fds=(sys.platform != "win32") + ) ret = popen.wait() finally: f1.close() @@ -973,14 +1035,15 @@ class Testdir: print("couldn't print to %s because of encoding" % (fp,)) def _getpytestargs(self): - # we cannot use "(sys.executable,script)" - # because on windows the script is e.g. a pytest.exe - return (sys.executable, _pytest_fullpath,) # noqa + # we cannot use `(sys.executable, script)` because on Windows the + # script is e.g. `pytest.exe` + return (sys.executable, PYTEST_FULLPATH) # noqa def runpython(self, script): """Run a python script using sys.executable as interpreter. Returns a :py:class:`RunResult`. + """ return self.run(sys.executable, script) @@ -991,36 +1054,30 @@ class Testdir: def runpytest_subprocess(self, *args, **kwargs): """Run pytest as a subprocess with given arguments. - Any plugins added to the :py:attr:`plugins` list will added - using the ``-p`` command line option. Addtionally - ``--basetemp`` is used put any temporary files and directories - in a numbered directory prefixed with "runpytest-" so they do - not conflict with the normal numberd pytest location for - temporary files and directories. + Any plugins added to the :py:attr:`plugins` list will added using the + ``-p`` command line option. Additionally ``--basetemp`` is used put + any temporary files and directories in a numbered directory prefixed + with "runpytest-" so they do not conflict with the normal numbered + pytest location for temporary files and directories. Returns a :py:class:`RunResult`. """ - p = py.path.local.make_numbered_dir(prefix="runpytest-", - keep=None, rootdir=self.tmpdir) - args = ('--basetemp=%s' % p, ) + args - # for x in args: - # if '--confcutdir' in str(x): - # break - # else: - # pass - # args = ('--confcutdir=.',) + args + p = py.path.local.make_numbered_dir( + prefix="runpytest-", keep=None, rootdir=self.tmpdir + ) + args = ("--basetemp=%s" % p,) + args plugins = [x for x in self.plugins if isinstance(x, str)] if plugins: - args = ('-p', plugins[0]) + args + args = ("-p", plugins[0]) + args args = self._getpytestargs() + args return self.run(*args) def spawn_pytest(self, string, expect_timeout=10.0): """Run pytest using pexpect. - This makes sure to use the right pytest and sets up the - temporary directory locations. + This makes sure to use the right pytest and sets up the temporary + directory locations. The pexpect child is returned. @@ -1034,9 +1091,10 @@ class Testdir: """Run a command using pexpect. The pexpect child is returned. + """ pexpect = pytest.importorskip("pexpect", "3.0") - if hasattr(sys, 'pypy_version_info') and '64' in platform.machine(): + if hasattr(sys, "pypy_version_info") and "64" in platform.machine(): pytest.skip("pypy-64 bit not supported") if sys.platform.startswith("freebsd"): pytest.xfail("pexpect does not work reliably on freebsd") @@ -1052,16 +1110,20 @@ def getdecoded(out): return out.decode("utf-8") except UnicodeDecodeError: return "INTERNAL not-utf8-decodeable, truncated string:\n%s" % ( - py.io.saferepr(out),) + py.io.saferepr(out), + ) -class LineComp: +class LineComp(object): + def __init__(self): self.stringio = py.io.TextIO() def assert_contains_lines(self, lines2): - """ assert that lines2 are contained (linearly) in lines1. - return a list of extralines found. + """Assert that lines2 are contained (linearly) in lines1. + + Return a list of extralines found. + """ __tracebackhide__ = True val = self.stringio.getvalue() @@ -1071,14 +1133,14 @@ class LineComp: return LineMatcher(lines1).fnmatch_lines(lines2) -class LineMatcher: +class LineMatcher(object): """Flexible matching of text. This is a convenience class to test large texts like the output of commands. - The constructor takes a list of lines without their trailing - newlines, i.e. ``text.splitlines()``. + The constructor takes a list of lines without their trailing newlines, i.e. + ``text.splitlines()``. """ @@ -1098,16 +1160,34 @@ class LineMatcher: return lines2 def fnmatch_lines_random(self, lines2): + """Check lines exist in the output using in any order. + + Lines are checked using ``fnmatch.fnmatch``. The argument is a list of + lines which have to occur in the output, in any order. + + """ + self._match_lines_random(lines2, fnmatch) + + def re_match_lines_random(self, lines2): + """Check lines exist in the output using ``re.match``, in any order. + + The argument is a list of lines which have to occur in the output, in + any order. + + """ + self._match_lines_random(lines2, lambda name, pat: re.match(pat, name)) + + def _match_lines_random(self, lines2, match_func): """Check lines exist in the output. - The argument is a list of lines which have to occur in the - output, in any order. Each line can contain glob whildcards. + The argument is a list of lines which have to occur in the output, in + any order. Each line can contain glob whildcards. """ lines2 = self._getlines(lines2) for line in lines2: for x in self.lines: - if line == x or fnmatch(x, line): + if line == x or match_func(x, line): self._log("matched: ", repr(line)) break else: @@ -1118,6 +1198,7 @@ class LineMatcher: """Return all lines following the given line in the text. The given line can contain glob wildcards. + """ for i, line in enumerate(self.lines): if fnline == line or fnmatch(line, fnline): @@ -1125,19 +1206,43 @@ class LineMatcher: raise ValueError("line %r not found in output" % fnline) def _log(self, *args): - self._log_output.append(' '.join((str(x) for x in args))) + self._log_output.append(" ".join((str(x) for x in args))) @property def _log_text(self): - return '\n'.join(self._log_output) + return "\n".join(self._log_output) def fnmatch_lines(self, lines2): - """Search the text for matching lines. + """Search captured text for matching lines using ``fnmatch.fnmatch``. - The argument is a list of lines which have to match and can - use glob wildcards. If they do not match an pytest.fail() is - called. The matches and non-matches are also printed on - stdout. + The argument is a list of lines which have to match and can use glob + wildcards. If they do not match a pytest.fail() is called. The + matches and non-matches are also printed on stdout. + + """ + self._match_lines(lines2, fnmatch, "fnmatch") + + def re_match_lines(self, lines2): + """Search captured text for matching lines using ``re.match``. + + The argument is a list of lines which have to match using ``re.match``. + If they do not match a pytest.fail() is called. + + The matches and non-matches are also printed on stdout. + + """ + self._match_lines(lines2, lambda name, pat: re.match(pat, name), "re.match") + + def _match_lines(self, lines2, match_func, match_nickname): + """Underlying implementation of ``fnmatch_lines`` and ``re_match_lines``. + + :param list[str] lines2: list of string patterns to match. The actual + format depends on ``match_func`` + :param match_func: a callable ``match_func(line, pattern)`` where line + is the captured line from stdout/stderr and pattern is the matching + pattern + :param str match_nickname: the nickname for the match function that + will be logged to stdout when a match occurs """ lines2 = self._getlines(lines2) @@ -1152,8 +1257,8 @@ class LineMatcher: if line == nextline: self._log("exact match:", repr(line)) break - elif fnmatch(nextline, line): - self._log("fnmatch:", repr(line)) + elif match_func(nextline, line): + self._log("%s:" % match_nickname, repr(line)) self._log(" with:", repr(nextline)) break else: diff --git a/third_party/python/pytest/_pytest/python.py b/third_party/python/pytest/src/_pytest/python.py similarity index 70% rename from third_party/python/pytest/_pytest/python.py rename to third_party/python/pytest/src/_pytest/python.py index 41fd2bdb7fe2..48516199fff7 100644 --- a/third_party/python/pytest/_pytest/python.py +++ b/third_party/python/pytest/src/_pytest/python.py @@ -6,29 +6,52 @@ import inspect import sys import os import collections +import warnings from textwrap import dedent from itertools import count + import py +import six from _pytest.mark import MarkerError from _pytest.config import hookimpl import _pytest -import _pytest._pluggy as pluggy +import pluggy from _pytest import fixtures -from _pytest import main +from _pytest import nodes +from _pytest import deprecated from _pytest.compat import ( - isclass, isfunction, is_generator, _ascii_escaped, - REGEX_TYPE, STRING_TYPES, NoneType, NOTSET, - get_real_func, getfslineno, safe_getattr, - safe_str, getlocation, enum, + isclass, + isfunction, + is_generator, + ascii_escaped, + REGEX_TYPE, + STRING_TYPES, + NoneType, + NOTSET, + get_real_func, + getfslineno, + safe_getattr, + safe_str, + getlocation, + enum, + get_default_arg_names, ) from _pytest.outcomes import fail -from _pytest.mark import transfer_markers +from _pytest.mark.structures import transfer_markers, get_unpacked_marks -cutdir1 = py.path.local(pluggy.__file__.rstrip("oc")) -cutdir2 = py.path.local(_pytest.__file__).dirpath() -cutdir3 = py.path.local(py.__file__).dirpath() + +# relative paths that we use to filter traceback entries from appearing to the user; +# see filter_traceback +# note: if we need to add more paths than what we have now we should probably use a list +# for better maintenance +_pluggy_dir = py.path.local(pluggy.__file__.rstrip("oc")) +# pluggy is either a package or a single module depending on the version +if _pluggy_dir.basename == "__init__.py": + _pluggy_dir = _pluggy_dir.dirpath() +_pytest_dir = py.path.local(_pytest.__file__).dirpath() +_py_dir = py.path.local(py.__file__).dirpath() def filter_traceback(entry): @@ -40,52 +63,81 @@ def filter_traceback(entry): # points to dynamically generated code # see https://bitbucket.org/pytest-dev/py/issues/71 raw_filename = entry.frame.code.raw.co_filename - is_generated = '<' in raw_filename and '>' in raw_filename + is_generated = "<" in raw_filename and ">" in raw_filename if is_generated: return False - # entry.path might point to an inexisting file, in which case it will - # alsso return a str object. see #1133 + # entry.path might point to a non-existing file, in which case it will + # also return a str object. see #1133 p = py.path.local(entry.path) - return p != cutdir1 and not p.relto(cutdir2) and not p.relto(cutdir3) + return not p.relto(_pluggy_dir) and not p.relto(_pytest_dir) and not p.relto( + _py_dir + ) def pyobj_property(name): + def get(self): - node = self.getparent(getattr(__import__('pytest'), name)) + node = self.getparent(getattr(__import__("pytest"), name)) if node is not None: return node.obj + doc = "python %s object this node was collected from (can be None)." % ( - name.lower(),) + name.lower(), + ) return property(get, None, None, doc) def pytest_addoption(parser): group = parser.getgroup("general") - group.addoption('--fixtures', '--funcargs', - action="store_true", dest="showfixtures", default=False, - help="show available fixtures, sorted by plugin appearance") group.addoption( - '--fixtures-per-test', + "--fixtures", + "--funcargs", + action="store_true", + dest="showfixtures", + default=False, + help="show available fixtures, sorted by plugin appearance " + "(fixtures with leading '_' are only shown with '-v')", + ) + group.addoption( + "--fixtures-per-test", action="store_true", dest="show_fixtures_per_test", default=False, help="show fixtures per test", ) - parser.addini("usefixtures", type="args", default=[], - help="list of default fixtures to be used with this project") - parser.addini("python_files", type="args", - default=['test_*.py', '*_test.py'], - help="glob-style file patterns for Python test module discovery") - parser.addini("python_classes", type="args", default=["Test", ], - help="prefixes or glob names for Python test class discovery") - parser.addini("python_functions", type="args", default=["test", ], - help="prefixes or glob names for Python test function and " - "method discovery") + parser.addini( + "usefixtures", + type="args", + default=[], + help="list of default fixtures to be used with this project", + ) + parser.addini( + "python_files", + type="args", + default=["test_*.py", "*_test.py"], + help="glob-style file patterns for Python test module discovery", + ) + parser.addini( + "python_classes", + type="args", + default=["Test"], + help="prefixes or glob names for Python test class discovery", + ) + parser.addini( + "python_functions", + type="args", + default=["test"], + help="prefixes or glob names for Python test function and " "method discovery", + ) - group.addoption("--import-mode", default="prepend", - choices=["prepend", "append"], dest="importmode", - help="prepend/append to sys.path when importing test modules, " - "default is to prepend.") + group.addoption( + "--import-mode", + default="prepend", + choices=["prepend", "append"], + dest="importmode", + help="prepend/append to sys.path when importing test modules, " + "default is to prepend.", + ) def pytest_cmdline_main(config): @@ -100,34 +152,32 @@ def pytest_cmdline_main(config): def pytest_generate_tests(metafunc): # those alternative spellings are common - raise a specific error to alert # the user - alt_spellings = ['parameterize', 'parametrise', 'parameterise'] + alt_spellings = ["parameterize", "parametrise", "parameterise"] for attr in alt_spellings: if hasattr(metafunc.function, attr): msg = "{0} has '{1}', spelling should be 'parametrize'" raise MarkerError(msg.format(metafunc.function.__name__, attr)) - try: - markers = metafunc.function.parametrize - except AttributeError: - return - for marker in markers: + for marker in metafunc.definition.iter_markers(name="parametrize"): metafunc.parametrize(*marker.args, **marker.kwargs) def pytest_configure(config): - config.addinivalue_line("markers", - "parametrize(argnames, argvalues): call a test function multiple " - "times passing in different arguments in turn. argvalues generally " - "needs to be a list of values if argnames specifies only one name " - "or a list of tuples of values if argnames specifies multiple names. " - "Example: @parametrize('arg1', [1,2]) would lead to two calls of the " - "decorated test function, one with arg1=1 and another with arg1=2." - "see http://pytest.org/latest/parametrize.html for more info and " - "examples." - ) - config.addinivalue_line("markers", - "usefixtures(fixturename1, fixturename2, ...): mark tests as needing " - "all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures " - ) + config.addinivalue_line( + "markers", + "parametrize(argnames, argvalues): call a test function multiple " + "times passing in different arguments in turn. argvalues generally " + "needs to be a list of values if argnames specifies only one name " + "or a list of tuples of values if argnames specifies multiple names. " + "Example: @parametrize('arg1', [1,2]) would lead to two calls of the " + "decorated test function, one with arg1=1 and another with arg1=2." + "see http://pytest.org/latest/parametrize.html for more info and " + "examples.", + ) + config.addinivalue_line( + "markers", + "usefixtures(fixturename1, fixturename2, ...): mark tests as needing " + "all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures ", + ) @hookimpl(trylast=True) @@ -148,7 +198,7 @@ def pytest_collect_file(path, parent): ext = path.ext if ext == ".py": if not parent.session.isinitpath(path): - for pat in parent.config.getini('python_files'): + for pat in parent.config.getini("python_files"): if path.fnmatch(pat): break else: @@ -179,8 +229,10 @@ def pytest_pycollect_makeitem(collector, name, obj): # or a funtools.wrapped. # We musn't if it's been wrapped with mock.patch (python 2 only) if not (isfunction(obj) or isfunction(get_real_func(obj))): - collector.warn(code="C2", message="cannot collect %r because it is not a function." - % name, ) + collector.warn( + code="C2", + message="cannot collect %r because it is not a function." % name, + ) elif getattr(obj, "__test__", True): if is_generator(obj): res = Generator(name, parent=collector) @@ -200,11 +252,21 @@ class PyobjContext(object): class PyobjMixin(PyobjContext): + _ALLOW_MARKERS = True + + def __init__(self, *k, **kw): + super(PyobjMixin, self).__init__(*k, **kw) + def obj(): + def fget(self): - obj = getattr(self, '_obj', None) + obj = getattr(self, "_obj", None) if obj is None: self._obj = obj = self._getobj() + # XXX evil hack + # used to avoid Instance collector marker duplication + if self._ALLOW_MARKERS: + self.own_markers.extend(get_unpacked_marks(self.obj)) return obj def fset(self, value): @@ -243,7 +305,7 @@ class PyobjMixin(PyobjContext): def reportinfo(self): # XXX caching? obj = self.obj - compat_co_firstlineno = getattr(obj, 'compat_co_firstlineno', None) + compat_co_firstlineno = getattr(obj, "compat_co_firstlineno", None) if isinstance(compat_co_firstlineno, int): # nose compatibility fspath = sys.modules[obj.__module__].__file__ @@ -257,10 +319,10 @@ class PyobjMixin(PyobjContext): return fspath, lineno, modpath -class PyCollector(PyobjMixin, main.Collector): +class PyCollector(PyobjMixin, nodes.Collector): def funcnamefilter(self, name): - return self._matches_prefix_or_glob_option('python_functions', name) + return self._matches_prefix_or_glob_option("python_functions", name) def isnosetest(self, obj): """ Look for the __test__ attribute, which is applied by the @@ -269,25 +331,24 @@ class PyCollector(PyobjMixin, main.Collector): # We explicitly check for "is True" here to not mistakenly treat # classes with a custom __getattr__ returning something truthy (like a # function) as test classes. - return safe_getattr(obj, '__test__', False) is True + return safe_getattr(obj, "__test__", False) is True def classnamefilter(self, name): - return self._matches_prefix_or_glob_option('python_classes', name) + return self._matches_prefix_or_glob_option("python_classes", name) def istestfunction(self, obj, name): if self.funcnamefilter(name) or self.isnosetest(obj): if isinstance(obj, staticmethod): # static methods need to be unwrapped - obj = safe_getattr(obj, '__func__', False) + obj = safe_getattr(obj, "__func__", False) if obj is False: # Python 2.6 wraps in a different way that we won't try to handle - msg = "cannot collect static method %r because " \ - "it is not a function (always the case in Python 2.6)" - self.warn( - code="C2", message=msg % name) + msg = "cannot collect static method %r because it is not a function" + self.warn(code="C2", message=msg % name) return False return ( - safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None + safe_getattr(obj, "__call__", False) + and fixtures.getfixturemarker(obj) is None ) else: return False @@ -306,8 +367,9 @@ class PyCollector(PyobjMixin, main.Collector): # check that name looks like a glob-string before calling fnmatch # because this is called for every name in each collected module, # and fnmatch is somewhat expensive to call - elif ('*' in option or '?' in option or '[' in option) and \ - fnmatch.fnmatch(name, option): + elif ("*" in option or "?" in option or "[" in option) and fnmatch.fnmatch( + name, option + ): return True return False @@ -317,7 +379,7 @@ class PyCollector(PyobjMixin, main.Collector): # NB. we avoid random getattrs and peek in the __dict__ instead # (XXX originally introduced from a PyPy need, still true?) - dicts = [getattr(self.obj, '__dict__', {})] + dicts = [getattr(self.obj, "__dict__", {})] for basecls in inspect.getmro(self.obj.__class__): dicts.append(basecls.__dict__) seen = {} @@ -327,7 +389,7 @@ class PyCollector(PyobjMixin, main.Collector): if name in seen: continue seen[name] = True - res = self.makeitem(name, obj) + res = self._makeitem(name, obj) if res is None: continue if not isinstance(res, list): @@ -337,9 +399,12 @@ class PyCollector(PyobjMixin, main.Collector): return values def makeitem(self, name, obj): + warnings.warn(deprecated.COLLECTOR_MAKEITEM, stacklevel=2) + self._makeitem(name, obj) + + def _makeitem(self, name, obj): # assert self.ihook.fspath == self.fspath, self - return self.ihook.pytest_pycollect_makeitem( - collector=self, name=name, obj=obj) + return self.ihook.pytest_pycollect_makeitem(collector=self, name=name, obj=obj) def _genfunctions(self, name, funcobj): module = self.getparent(Module).obj @@ -347,17 +412,22 @@ class PyCollector(PyobjMixin, main.Collector): cls = clscol and clscol.obj or None transfer_markers(funcobj, cls, module) fm = self.session._fixturemanager - fixtureinfo = fm.getfixtureinfo(self, funcobj, cls) - metafunc = Metafunc(funcobj, fixtureinfo, self.config, - cls=cls, module=module) + + definition = FunctionDefinition(name=name, parent=self, callobj=funcobj) + fixtureinfo = fm.getfixtureinfo(definition, funcobj, cls) + + metafunc = Metafunc( + definition, fixtureinfo, self.config, cls=cls, module=module + ) methods = [] if hasattr(module, "pytest_generate_tests"): methods.append(module.pytest_generate_tests) if hasattr(cls, "pytest_generate_tests"): methods.append(cls().pytest_generate_tests) if methods: - self.ihook.pytest_generate_tests.call_extra(methods, - dict(metafunc=metafunc)) + self.ihook.pytest_generate_tests.call_extra( + methods, dict(metafunc=metafunc) + ) else: self.ihook.pytest_generate_tests(metafunc=metafunc) @@ -370,15 +440,18 @@ class PyCollector(PyobjMixin, main.Collector): for callspec in metafunc._calls: subname = "%s[%s]" % (name, callspec.id) - yield Function(name=subname, parent=self, - callspec=callspec, callobj=funcobj, - fixtureinfo=fixtureinfo, - keywords={callspec.id: True}, - originalname=name, - ) + yield Function( + name=subname, + parent=self, + callspec=callspec, + callobj=funcobj, + fixtureinfo=fixtureinfo, + keywords={callspec.id: True}, + originalname=name, + ) -class Module(main.File, PyCollector): +class Module(nodes.File, PyCollector): """ Collector for test classes and functions. """ def _getobj(self): @@ -395,7 +468,8 @@ class Module(main.File, PyCollector): mod = self.fspath.pyimport(ensuresyspath=importmode) except SyntaxError: raise self.CollectError( - _pytest._code.ExceptionInfo().getrepr(style="short")) + _pytest._code.ExceptionInfo().getrepr(style="short") + ) except self.fspath.ImportMismatchError: e = sys.exc_info()[1] raise self.CollectError( @@ -405,15 +479,17 @@ class Module(main.File, PyCollector): "which is not the same as the test file we want to collect:\n" " %s\n" "HINT: remove __pycache__ / .pyc files and/or use a " - "unique basename for your test file modules" - % e.args + "unique basename for your test file modules" % e.args ) except ImportError: from _pytest._code.code import ExceptionInfo + exc_info = ExceptionInfo() - if self.config.getoption('verbose') < 2: + if self.config.getoption("verbose") < 2: exc_info.traceback = exc_info.traceback.filter(filter_traceback) - exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly() + exc_repr = exc_info.getrepr( + style="short" + ) if exc_info.traceback else exc_info.exconly() formatted_tb = safe_str(exc_repr) raise self.CollectError( "ImportError while importing test module '{fspath}'.\n" @@ -440,9 +516,9 @@ class Module(main.File, PyCollector): if setup_module is not None: setup_module() - teardown_module = _get_xunit_setup_teardown(self.obj, 'tearDownModule') + teardown_module = _get_xunit_setup_teardown(self.obj, "tearDownModule") if teardown_module is None: - teardown_module = _get_xunit_setup_teardown(self.obj, 'teardown_module') + teardown_module = _get_xunit_setup_teardown(self.obj, "teardown_module") if teardown_module is not None: self.addfinalizer(teardown_module) @@ -484,30 +560,41 @@ class Class(PyCollector): if not safe_getattr(self.obj, "__test__", True): return [] if hasinit(self.obj): - self.warn("C1", "cannot collect test class %r because it has a " - "__init__ constructor" % self.obj.__name__) + self.warn( + "C1", + "cannot collect test class %r because it has a " + "__init__ constructor" % self.obj.__name__, + ) return [] elif hasnew(self.obj): - self.warn("C1", "cannot collect test class %r because it has a " - "__new__ constructor" % self.obj.__name__) + self.warn( + "C1", + "cannot collect test class %r because it has a " + "__new__ constructor" % self.obj.__name__, + ) return [] return [self._getcustomclass("Instance")(name="()", parent=self)] def setup(self): - setup_class = _get_xunit_func(self.obj, 'setup_class') + setup_class = _get_xunit_func(self.obj, "setup_class") if setup_class is not None: - setup_class = getattr(setup_class, 'im_func', setup_class) - setup_class = getattr(setup_class, '__func__', setup_class) + setup_class = getattr(setup_class, "im_func", setup_class) + setup_class = getattr(setup_class, "__func__", setup_class) setup_class(self.obj) - fin_class = getattr(self.obj, 'teardown_class', None) + fin_class = getattr(self.obj, "teardown_class", None) if fin_class is not None: - fin_class = getattr(fin_class, 'im_func', fin_class) - fin_class = getattr(fin_class, '__func__', fin_class) + fin_class = getattr(fin_class, "im_func", fin_class) + fin_class = getattr(fin_class, "__func__", fin_class) self.addfinalizer(lambda: fin_class(self.obj)) class Instance(PyCollector): + _ALLOW_MARKERS = False # hack, destroy later + # instances share the object with their parents in a way + # that duplicates markers instances if not taken out + # can be removed at node strucutre reorganization time + def _getobj(self): return self.parent.obj() @@ -526,7 +613,7 @@ class FunctionMixin(PyobjMixin): def setup(self): """ perform setup for this test function. """ - if hasattr(self, '_preservedparent'): + if hasattr(self, "_preservedparent"): obj = self._preservedparent elif isinstance(self.parent, Instance): obj = self.parent.newinstance() @@ -534,20 +621,24 @@ class FunctionMixin(PyobjMixin): else: obj = self.parent.obj if inspect.ismethod(self.obj): - setup_name = 'setup_method' - teardown_name = 'teardown_method' + setup_name = "setup_method" + teardown_name = "teardown_method" else: - setup_name = 'setup_function' - teardown_name = 'teardown_function' - setup_func_or_method = _get_xunit_setup_teardown(obj, setup_name, param_obj=self.obj) + setup_name = "setup_function" + teardown_name = "teardown_function" + setup_func_or_method = _get_xunit_setup_teardown( + obj, setup_name, param_obj=self.obj + ) if setup_func_or_method is not None: setup_func_or_method() - teardown_func_or_method = _get_xunit_setup_teardown(obj, teardown_name, param_obj=self.obj) + teardown_func_or_method = _get_xunit_setup_teardown( + obj, teardown_name, param_obj=self.obj + ) if teardown_func_or_method is not None: self.addfinalizer(teardown_func_or_method) def _prunetraceback(self, excinfo): - if hasattr(self, '_obj') and not self.config.option.fulltrace: + if hasattr(self, "_obj") and not self.config.option.fulltrace: code = _pytest._code.Code(get_real_func(self.obj)) path, firstlineno = code.path, code.firstlineno traceback = excinfo.traceback @@ -555,7 +646,6 @@ class FunctionMixin(PyobjMixin): if ntraceback == traceback: ntraceback = ntraceback.cut(path=path) if ntraceback == traceback: - # ntraceback = ntraceback.cut(excludepath=cutdir2) ntraceback = ntraceback.filter(filter_traceback) if not ntraceback: ntraceback = traceback @@ -566,14 +656,13 @@ class FunctionMixin(PyobjMixin): if self.config.option.tbstyle == "auto": if len(excinfo.traceback) > 2: for entry in excinfo.traceback[1:-1]: - entry.set_repr_style('short') + entry.set_repr_style("short") def _repr_failure_py(self, excinfo, style="long"): if excinfo.errisinstance(fail.Exception): if not excinfo.value.pytrace: return py._builtin._totext(excinfo.value) - return super(FunctionMixin, self)._repr_failure_py(excinfo, - style=style) + return super(FunctionMixin, self)._repr_failure_py(excinfo, style=style) def repr_failure(self, excinfo, outerr=None): assert outerr is None, "XXX outerr usage is deprecated" @@ -584,11 +673,13 @@ class FunctionMixin(PyobjMixin): class Generator(FunctionMixin, PyCollector): + def collect(self): # test generators are seen as collectors but they also # invoke setup/teardown on popular request # (induced by the common "test_*" naming shared with normal tests) from _pytest import deprecated + self.session._setupstate.prepare(self) # see FunctionMixin.setup and test_setupstate_is_preserved_134 self._preservedparent = self.parent.obj @@ -597,23 +688,25 @@ class Generator(FunctionMixin, PyCollector): for i, x in enumerate(self.obj()): name, call, args = self.getcallargs(x) if not callable(call): - raise TypeError("%r yielded non callable test %r" % (self.obj, call,)) + raise TypeError("%r yielded non callable test %r" % (self.obj, call)) if name is None: name = "[%d]" % i else: name = "['%s']" % name if name in seen: - raise ValueError("%r generated tests with non-unique name %r" % (self, name)) + raise ValueError( + "%r generated tests with non-unique name %r" % (self, name) + ) seen[name] = True values.append(self.Function(name, self, args=args, callobj=call)) - self.warn('C1', deprecated.YIELD_TESTS) + self.warn("C1", deprecated.YIELD_TESTS) return values def getcallargs(self, obj): if not isinstance(obj, (tuple, list)): obj = (obj,) # explicit naming - if isinstance(obj[0], py.builtin._basestring): + if isinstance(obj[0], six.string_types): name = obj[0] obj = obj[1:] else: @@ -623,18 +716,19 @@ class Generator(FunctionMixin, PyCollector): def hasinit(obj): - init = getattr(obj, '__init__', None) + init = getattr(obj, "__init__", None) if init: return init != object.__init__ def hasnew(obj): - new = getattr(obj, '__new__', None) + new = getattr(obj, "__new__", None) if new: return new != object.__new__ class CallSpec2(object): + def __init__(self, metafunc): self.metafunc = metafunc self.funcargs = {} @@ -644,14 +738,14 @@ class CallSpec2(object): self._globalid_args = set() self._globalparam = NOTSET self._arg2scopenum = {} # used for sorting parametrized resources - self.keywords = {} + self.marks = [] self.indices = {} def copy(self, metafunc): cs = CallSpec2(self.metafunc) cs.funcargs.update(self.funcargs) cs.params.update(self.params) - cs.keywords.update(self.keywords) + cs.marks.extend(self.marks) cs.indices.update(self.indices) cs._arg2scopenum.update(self._arg2scopenum) cs._idlist = list(self._idlist) @@ -676,8 +770,7 @@ class CallSpec2(object): def id(self): return "-".join(map(str, filter(None, self._idlist))) - def setmulti(self, valtypes, argnames, valset, id, keywords, scopenum, - param_index): + def setmulti2(self, valtypes, argnames, valset, id, marks, scopenum, param_index): for arg, val in zip(argnames, valset): self._checkargnotcontained(arg) valtype_for_arg = valtypes[arg] @@ -685,7 +778,7 @@ class CallSpec2(object): self.indices[arg] = param_index self._arg2scopenum[arg] = scopenum self._idlist.append(id) - self.keywords.update(keywords) + self.marks.extend(marks) def setall(self, funcargs, id, param): for x in funcargs: @@ -702,21 +795,26 @@ class CallSpec2(object): class Metafunc(fixtures.FuncargnamesCompatAttr): """ - Metafunc objects are passed to the ``pytest_generate_tests`` hook. + Metafunc objects are passed to the :func:`pytest_generate_tests <_pytest.hookspec.pytest_generate_tests>` hook. They help to inspect a test function and to generate tests according to test configuration or values specified in the class or module where a test function is defined. """ - def __init__(self, function, fixtureinfo, config, cls=None, module=None): + def __init__(self, definition, fixtureinfo, config, cls=None, module=None): #: access to the :class:`_pytest.config.Config` object for the test session + assert ( + isinstance(definition, FunctionDefinition) + or type(definition).__name__ == "DefinitionMock" + ) + self.definition = definition self.config = config #: the module object where the test function is defined in. self.module = module #: underlying python test function - self.function = function + self.function = definition.obj #: set of fixture names required by the test function self.fixturenames = fixtureinfo.names_closure @@ -725,11 +823,10 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): self.cls = cls self._calls = [] - self._ids = py.builtin.set() + self._ids = set() self._arg2fixturedefs = fixtureinfo.name2fixturedefs - def parametrize(self, argnames, argvalues, indirect=False, ids=None, - scope=None): + def parametrize(self, argnames, argvalues, indirect=False, ids=None, scope=None): """ Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources @@ -768,44 +865,33 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): to set a dynamic scope using test context or configuration. """ from _pytest.fixtures import scope2index - from _pytest.mark import MARK_GEN, ParameterSet + from _pytest.mark import ParameterSet from py.io import saferepr - if not isinstance(argnames, (tuple, list)): - argnames = [x.strip() for x in argnames.split(",") if x.strip()] - force_tuple = len(argnames) == 1 - else: - force_tuple = False - parameters = [ - ParameterSet.extract_from(x, legacy_force_tuple=force_tuple) - for x in argvalues] + argnames, parameters = ParameterSet._for_parametrize( + argnames, argvalues, self.function, self.config + ) del argvalues - - if not parameters: - fs, lineno = getfslineno(self.function) - reason = "got empty parameter set %r, function %s at %s:%d" % ( - argnames, self.function.__name__, fs, lineno) - mark = MARK_GEN.skip(reason=reason) - parameters.append(ParameterSet( - values=(NOTSET,) * len(argnames), - marks=[mark], - id=None, - )) + default_arg_names = set(get_default_arg_names(self.function)) if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) - scopenum = scope2index(scope, descr='call to {0}'.format(self.parametrize)) + scopenum = scope2index(scope, descr="call to {}".format(self.parametrize)) valtypes = {} for arg in argnames: if arg not in self.fixturenames: - if isinstance(indirect, (tuple, list)): - name = 'fixture' if arg in indirect else 'argument' + if arg in default_arg_names: + raise ValueError( + "%r already takes an argument %r with a default value" + % (self.function, arg) + ) else: - name = 'fixture' if indirect else 'argument' - raise ValueError( - "%r uses no %s %r" % ( - self.function, name, arg)) + if isinstance(indirect, (tuple, list)): + name = "fixture" if arg in indirect else "argument" + else: + name = "fixture" if indirect else "argument" + raise ValueError("%r uses no %s %r" % (self.function, name, arg)) if indirect is True: valtypes = dict.fromkeys(argnames, "params") @@ -815,8 +901,10 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): valtypes = dict.fromkeys(argnames, "funcargs") for arg in indirect: if arg not in argnames: - raise ValueError("indirect given to %r: fixture %r doesn't exist" % ( - self.function, arg)) + raise ValueError( + "indirect given to %r: fixture %r doesn't exist" + % (self.function, arg) + ) valtypes[arg] = "params" idfn = None if callable(ids): @@ -824,12 +912,15 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): ids = None if ids: if len(ids) != len(parameters): - raise ValueError('%d tests specified with %d ids' % ( - len(parameters), len(ids))) + raise ValueError( + "%d tests specified with %d ids" % (len(parameters), len(ids)) + ) for id_value in ids: - if id_value is not None and not isinstance(id_value, py.builtin._basestring): - msg = 'ids must be list of strings, found: %s (type: %s)' - raise ValueError(msg % (saferepr(id_value), type(id_value).__name__)) + if id_value is not None and not isinstance(id_value, six.string_types): + msg = "ids must be list of strings, found: %s (type: %s)" + raise ValueError( + msg % (saferepr(id_value), type(id_value).__name__) + ) ids = idmaker(argnames, parameters, idfn, ids, self.config) newcalls = [] for callspec in self._calls or [CallSpec2(self)]: @@ -837,19 +928,32 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): for a_id, param, param_index in elements: if len(param.values) != len(argnames): raise ValueError( - 'In "parametrize" the number of values ({0}) must be ' - 'equal to the number of names ({1})'.format( - param.values, argnames)) + 'In "parametrize" the number of values ({}) must be ' + "equal to the number of names ({})".format( + param.values, argnames + ) + ) newcallspec = callspec.copy(self) - newcallspec.setmulti(valtypes, argnames, param.values, a_id, - param.deprecated_arg_dict, scopenum, param_index) + newcallspec.setmulti2( + valtypes, + argnames, + param.values, + a_id, + param.marks, + scopenum, + param_index, + ) newcalls.append(newcallspec) self._calls = newcalls def addcall(self, funcargs=None, id=NOTSET, param=NOTSET): - """ (deprecated, use parametrize) Add a new call to the underlying - test function during the collection phase of a test run. Note that - request.addcall() is called during the test collection phase prior and + """ Add a new call to the underlying test function during the collection phase of a test run. + + .. deprecated:: 3.3 + + Use :meth:`parametrize` instead. + + Note that request.addcall() is called during the test collection phase prior and independently to actual test execution. You should only use addcall() if you need to specify multiple arguments of a test function. @@ -862,6 +966,10 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): :arg param: a parameter which will be exposed to a later fixture function invocation through the ``request.param`` attribute. """ + if self.config: + self.config.warn( + "C1", message=deprecated.METAFUNC_ADD_CALL, fslocation=None + ) assert funcargs is None or isinstance(funcargs, dict) if funcargs is not None: for name in funcargs: @@ -894,9 +1002,11 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect): Related to issue #1832, based on code posted by @Kingdread. """ from _pytest.fixtures import scopes + indirect_as_list = isinstance(indirect, (list, tuple)) - all_arguments_are_fixtures = indirect is True or \ - indirect_as_list and len(indirect) == argnames + all_arguments_are_fixtures = indirect is True or indirect_as_list and len( + indirect + ) == argnames if all_arguments_are_fixtures: fixturedefs = arg2fixturedefs or {} used_scopes = [fixturedef[0].scope for name, fixturedef in fixturedefs.items()] @@ -906,7 +1016,7 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect): if scope in used_scopes: return scope - return 'function' + return "function" def _idval(val, argname, idx, idfn, config=None): @@ -917,27 +1027,31 @@ def _idval(val, argname, idx, idfn, config=None): except Exception: # See issue https://github.com/pytest-dev/pytest/issues/2169 import warnings - msg = "Raised while trying to determine id of parameter %s at position %d." % (argname, idx) - msg += '\nUpdate your code as this will raise an error in pytest-4.0.' + + msg = "Raised while trying to determine id of parameter %s at position %d." % ( + argname, idx + ) + msg += "\nUpdate your code as this will raise an error in pytest-4.0." warnings.warn(msg, DeprecationWarning) if s: - return _ascii_escaped(s) + return ascii_escaped(s) if config: hook_id = config.hook.pytest_make_parametrize_id( - config=config, val=val, argname=argname) + config=config, val=val, argname=argname + ) if hook_id: return hook_id if isinstance(val, STRING_TYPES): - return _ascii_escaped(val) + return ascii_escaped(val) elif isinstance(val, (float, int, bool, NoneType)): return str(val) elif isinstance(val, REGEX_TYPE): - return _ascii_escaped(val.pattern) + return ascii_escaped(val.pattern) elif enum is not None and isinstance(val, enum.Enum): return str(val) - elif isclass(val) and hasattr(val, '__name__'): + elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"): return val.__name__ return str(argname) + str(idx) @@ -946,16 +1060,20 @@ def _idvalset(idx, parameterset, argnames, idfn, ids, config=None): if parameterset.id is not None: return parameterset.id if ids is None or (idx >= len(ids) or ids[idx] is None): - this_id = [_idval(val, argname, idx, idfn, config) - for val, argname in zip(parameterset.values, argnames)] + this_id = [ + _idval(val, argname, idx, idfn, config) + for val, argname in zip(parameterset.values, argnames) + ] return "-".join(this_id) else: - return _ascii_escaped(ids[idx]) + return ascii_escaped(ids[idx]) def idmaker(argnames, parametersets, idfn=None, ids=None, config=None): - ids = [_idvalset(valindex, parameterset, argnames, idfn, ids, config) - for valindex, parameterset in enumerate(parametersets)] + ids = [ + _idvalset(valindex, parameterset, argnames, idfn, ids, config) + for valindex, parameterset in enumerate(parametersets) + ] if len(set(ids)) != len(ids): # The ids are not unique duplicates = [testid for testid in ids if ids.count(testid) > 1] @@ -969,11 +1087,13 @@ def idmaker(argnames, parametersets, idfn=None, ids=None, config=None): def show_fixtures_per_test(config): from _pytest.main import wrap_session + return wrap_session(config, _show_fixtures_per_test) def _show_fixtures_per_test(config, session): import _pytest.config + session.perform_collect() curdir = py.path.local() tw = _pytest.config.create_terminal_writer(config) @@ -989,7 +1109,7 @@ def _show_fixtures_per_test(config, session): return if verbose > 0: bestrel = get_best_relpath(fixture_def.func) - funcargspec = "{0} -- {1}".format(argname, bestrel) + funcargspec = "{} -- {}".format(argname, bestrel) else: funcargspec = argname tw.line(funcargspec, green=True) @@ -997,7 +1117,7 @@ def _show_fixtures_per_test(config, session): if fixture_doc: write_docstring(tw, fixture_doc) else: - tw.line(' no docstring available', red=True) + tw.line(" no docstring available", red=True) def write_item(item): try: @@ -1009,8 +1129,8 @@ def _show_fixtures_per_test(config, session): # this test item does not use any fixtures return tw.line() - tw.sep('-', 'fixtures used by {0}'.format(item.name)) - tw.sep('-', '({0})'.format(get_best_relpath(item.function))) + tw.sep("-", "fixtures used by {}".format(item.name)) + tw.sep("-", "({})".format(get_best_relpath(item.function))) # dict key not used in loop but needed for sorting for _, fixturedefs in sorted(info.name2fixturedefs.items()): assert fixturedefs is not None @@ -1025,11 +1145,13 @@ def _show_fixtures_per_test(config, session): def showfixtures(config): from _pytest.main import wrap_session + return wrap_session(config, _showfixtures_main) def _showfixtures_main(config, session): import _pytest.config + session.perform_collect() curdir = py.path.local() tw = _pytest.config.create_terminal_writer(config) @@ -1049,10 +1171,15 @@ def _showfixtures_main(config, session): if (fixturedef.argname, loc) in seen: continue seen.add((fixturedef.argname, loc)) - available.append((len(fixturedef.baseid), - fixturedef.func.__module__, - curdir.bestrelpath(loc), - fixturedef.argname, fixturedef)) + available.append( + ( + len(fixturedef.baseid), + fixturedef.func.__module__, + curdir.bestrelpath(loc), + fixturedef.argname, + fixturedef, + ) + ) available.sort() currentmodule = None @@ -1065,7 +1192,7 @@ def _showfixtures_main(config, session): if verbose <= 0 and argname[0] == "_": continue if verbose > 0: - funcargspec = "%s -- %s" % (argname, bestrel,) + funcargspec = "%s -- %s" % (argname, bestrel) else: funcargspec = argname tw.line(funcargspec, green=True) @@ -1074,8 +1201,7 @@ def _showfixtures_main(config, session): if doc: write_docstring(tw, doc) else: - tw.line(" %s: no docstring available" % (loc,), - red=True) + tw.line(" %s: no docstring available" % (loc,), red=True) def write_docstring(tw, doc): @@ -1094,32 +1220,51 @@ def write_docstring(tw, doc): tw.write(INDENT + line + "\n") -class Function(FunctionMixin, main.Item, fixtures.FuncargnamesCompatAttr): +class Function(FunctionMixin, nodes.Item, fixtures.FuncargnamesCompatAttr): """ a Function Item is responsible for setting up and executing a Python test function. """ _genid = None + # disable since functions handle it themselfes + _ALLOW_MARKERS = False - def __init__(self, name, parent, args=None, config=None, - callspec=None, callobj=NOTSET, keywords=None, session=None, - fixtureinfo=None, originalname=None): - super(Function, self).__init__(name, parent, config=config, - session=session) + def __init__( + self, + name, + parent, + args=None, + config=None, + callspec=None, + callobj=NOTSET, + keywords=None, + session=None, + fixtureinfo=None, + originalname=None, + ): + super(Function, self).__init__(name, parent, config=config, session=session) self._args = args if callobj is not NOTSET: self.obj = callobj self.keywords.update(self.obj.__dict__) + self.own_markers.extend(get_unpacked_marks(self.obj)) if callspec: self.callspec = callspec - self.keywords.update(callspec.keywords) + # this is total hostile and a mess + # keywords are broken by design by now + # this will be redeemed later + for mark in callspec.marks: + # feel free to cry, this was broken for years before + # and keywords cant fix it per design + self.keywords[mark.name] = mark + self.own_markers.extend(callspec.marks) if keywords: self.keywords.update(keywords) if fixtureinfo is None: fixtureinfo = self.session._fixturemanager.getfixtureinfo( - self.parent, self.obj, self.cls, - funcargs=not self._isyieldedfunction()) + self, self.obj, self.cls, funcargs=not self._isyieldedfunction() + ) self._fixtureinfo = fixtureinfo self.fixturenames = fixtureinfo.names_closure self._initrequest() @@ -1133,8 +1278,9 @@ class Function(FunctionMixin, main.Item, fixtures.FuncargnamesCompatAttr): def _initrequest(self): self.funcargs = {} if self._isyieldedfunction(): - assert not hasattr(self, "callspec"), ( - "yielded functions (deprecated) cannot have funcargs") + assert not hasattr( + self, "callspec" + ), "yielded functions (deprecated) cannot have funcargs" else: if hasattr(self, "callspec"): callspec = self.callspec @@ -1147,7 +1293,7 @@ class Function(FunctionMixin, main.Item, fixtures.FuncargnamesCompatAttr): @property def function(self): "underlying python 'function' object" - return getattr(self.obj, 'im_func', self.obj) + return getattr(self.obj, "im_func", self.obj) def _getobj(self): name = self.name @@ -1171,3 +1317,15 @@ class Function(FunctionMixin, main.Item, fixtures.FuncargnamesCompatAttr): def setup(self): super(Function, self).setup() fixtures.fillfixtures(self) + + +class FunctionDefinition(Function): + """ + internal hack until we get actual definition nodes instead of the + crappy metafunc hack + """ + + def runtest(self): + raise RuntimeError("function definitions are not supposed to be used") + + setup = runtest diff --git a/third_party/python/pytest/_pytest/python_api.py b/third_party/python/pytest/src/_pytest/python_api.py similarity index 82% rename from third_party/python/pytest/_pytest/python_api.py rename to third_party/python/pytest/src/_pytest/python_api.py index 80684c1311b2..9257d210c270 100644 --- a/third_party/python/pytest/_pytest/python_api.py +++ b/third_party/python/pytest/src/_pytest/python_api.py @@ -2,11 +2,19 @@ import math import sys import py +from six.moves import zip, filterfalse +from more_itertools.more import always_iterable + +from _pytest.compat import isclass + +from _pytest.compat import Mapping, Sequence +from _pytest.compat import STRING_TYPES -from _pytest.compat import isclass, izip from _pytest.outcomes import fail import _pytest._code +BASE_TYPE = (type, STRING_TYPES) + def _cmp_raises_type_error(self, other): """__cmp__ implementation which raises TypeError. Used @@ -17,7 +25,9 @@ def _cmp_raises_type_error(self, other): other operators at all. """ __tracebackhide__ = True - raise TypeError('Comparison operators other than == and != not supported by approx objects') + raise TypeError( + "Comparison operators other than == and != not supported by approx objects" + ) # builtin pytest.approx helper @@ -29,6 +39,10 @@ class ApproxBase(object): or sequences of numbers. """ + # Tell numpy to use our `__eq__` operator instead of its + __array_ufunc__ = None + __array_priority__ = 100 + def __init__(self, expected, rel=None, abs=None, nan_ok=False): self.expected = expected self.abs = abs @@ -40,8 +54,8 @@ class ApproxBase(object): def __eq__(self, actual): return all( - a == self._approx_scalar(x) - for a, x in self._yield_comparisons(actual)) + a == self._approx_scalar(x) for a, x in self._yield_comparisons(actual) + ) __hash__ = None @@ -67,14 +81,14 @@ class ApproxNumpy(ApproxBase): Perform approximate comparisons for numpy arrays. """ - # Tell numpy to use our `__eq__` operator instead of its. - __array_priority__ = 100 - def __repr__(self): # It might be nice to rewrite this function to account for the # shape of the array... - return "approx({0!r})".format(list( - self._approx_scalar(x) for x in self.expected)) + import numpy as np + + return "approx({!r})".format( + list(self._approx_scalar(x) for x in np.asarray(self.expected)) + ) if sys.version_info[0] == 2: __cmp__ = _cmp_raises_type_error @@ -82,12 +96,15 @@ class ApproxNumpy(ApproxBase): def __eq__(self, actual): import numpy as np - try: - actual = np.asarray(actual) - except: # noqa - raise TypeError("cannot compare '{0}' to numpy.ndarray".format(actual)) + # self.expected is supposed to always be an array here - if actual.shape != self.expected.shape: + if not np.isscalar(actual): + try: + actual = np.asarray(actual) + except: # noqa + raise TypeError("cannot compare '{}' to numpy.ndarray".format(actual)) + + if not np.isscalar(actual) and actual.shape != self.expected.shape: return False return ApproxBase.__eq__(self, actual) @@ -95,11 +112,16 @@ class ApproxNumpy(ApproxBase): def _yield_comparisons(self, actual): import numpy as np - # We can be sure that `actual` is a numpy array, because it's - # casted in `__eq__` before being passed to `ApproxBase.__eq__`, - # which is the only method that calls this one. - for i in np.ndindex(self.expected.shape): - yield actual[i], self.expected[i] + # `actual` can either be a numpy array or a scalar, it is treated in + # `__eq__` before being passed to `ApproxBase.__eq__`, which is the + # only method that calls this one. + + if np.isscalar(actual): + for i in np.ndindex(self.expected.shape): + yield actual, np.asscalar(self.expected[i]) + else: + for i in np.ndindex(self.expected.shape): + yield np.asscalar(actual[i]), np.asscalar(self.expected[i]) class ApproxMapping(ApproxBase): @@ -109,9 +131,9 @@ class ApproxMapping(ApproxBase): """ def __repr__(self): - return "approx({0!r})".format(dict( - (k, self._approx_scalar(v)) - for k, v in self.expected.items())) + return "approx({!r})".format( + {k: self._approx_scalar(v) for k, v in self.expected.items()} + ) def __eq__(self, actual): if set(actual.keys()) != set(self.expected.keys()): @@ -129,15 +151,13 @@ class ApproxSequence(ApproxBase): Perform approximate comparisons for sequences of numbers. """ - # Tell numpy to use our `__eq__` operator instead of its. - __array_priority__ = 100 - def __repr__(self): seq_type = type(self.expected) if seq_type not in (tuple, list, set): seq_type = list - return "approx({0!r})".format(seq_type( - self._approx_scalar(x) for x in self.expected)) + return "approx({!r})".format( + seq_type(self._approx_scalar(x) for x in self.expected) + ) def __eq__(self, actual): if len(actual) != len(self.expected): @@ -145,13 +165,15 @@ class ApproxSequence(ApproxBase): return ApproxBase.__eq__(self, actual) def _yield_comparisons(self, actual): - return izip(actual, self.expected) + return zip(actual, self.expected) class ApproxScalar(ApproxBase): """ Perform approximate comparisons for single numbers only. """ + DEFAULT_ABSOLUTE_TOLERANCE = 1e-12 + DEFAULT_RELATIVE_TOLERANCE = 1e-6 def __repr__(self): """ @@ -171,20 +193,22 @@ class ApproxScalar(ApproxBase): # If a sensible tolerance can't be calculated, self.tolerance will # raise a ValueError. In this case, display '???'. try: - vetted_tolerance = '{:.1e}'.format(self.tolerance) + vetted_tolerance = "{:.1e}".format(self.tolerance) except ValueError: - vetted_tolerance = '???' + vetted_tolerance = "???" if sys.version_info[0] == 2: - return '{0} +- {1}'.format(self.expected, vetted_tolerance) + return "{} +- {}".format(self.expected, vetted_tolerance) else: - return u'{0} \u00b1 {1}'.format(self.expected, vetted_tolerance) + return u"{} \u00b1 {}".format(self.expected, vetted_tolerance) def __eq__(self, actual): """ Return true if the given value is equal to the expected value within the pre-specified tolerance. """ + if _is_numpy_array(actual): + return ApproxNumpy(actual, self.abs, self.rel, self.nan_ok) == self.expected # Short-circuit exact equality. if actual == self.expected: @@ -217,15 +241,18 @@ class ApproxScalar(ApproxBase): absolute tolerance or a relative tolerance, depending on what the user specified or which would be larger. """ + def set_default(x, default): return x if x is not None else default # Figure out what the absolute tolerance should be. ``self.abs`` is # either None or a value specified by the user. - absolute_tolerance = set_default(self.abs, 1e-12) + absolute_tolerance = set_default(self.abs, self.DEFAULT_ABSOLUTE_TOLERANCE) if absolute_tolerance < 0: - raise ValueError("absolute tolerance can't be negative: {}".format(absolute_tolerance)) + raise ValueError( + "absolute tolerance can't be negative: {}".format(absolute_tolerance) + ) if math.isnan(absolute_tolerance): raise ValueError("absolute tolerance can't be NaN.") @@ -240,10 +267,16 @@ class ApproxScalar(ApproxBase): # we've made sure the user didn't ask for an absolute tolerance only, # because we don't want to raise errors about the relative tolerance if # we aren't even going to use it. - relative_tolerance = set_default(self.rel, 1e-6) * abs(self.expected) + relative_tolerance = set_default( + self.rel, self.DEFAULT_RELATIVE_TOLERANCE + ) * abs( + self.expected + ) if relative_tolerance < 0: - raise ValueError("relative tolerance can't be negative: {}".format(absolute_tolerance)) + raise ValueError( + "relative tolerance can't be negative: {}".format(absolute_tolerance) + ) if math.isnan(relative_tolerance): raise ValueError("relative tolerance can't be NaN.") @@ -251,6 +284,13 @@ class ApproxScalar(ApproxBase): return max(relative_tolerance, absolute_tolerance) +class ApproxDecimal(ApproxScalar): + from decimal import Decimal + + DEFAULT_ABSOLUTE_TOLERANCE = Decimal("1e-12") + DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6") + + def approx(expected, rel=None, abs=None, nan_ok=False): """ Assert that two numbers (or two sets of numbers) are equal to each other @@ -297,12 +337,18 @@ def approx(expected, rel=None, abs=None, nan_ok=False): >>> {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6}) True - And ``numpy`` arrays:: + ``numpy`` arrays:: >>> import numpy as np # doctest: +SKIP >>> np.array([0.1, 0.2]) + np.array([0.2, 0.4]) == approx(np.array([0.3, 0.6])) # doctest: +SKIP True + And for a ``numpy`` array against a scalar:: + + >>> import numpy as np # doctest: +SKIP + >>> np.array([0.1, 0.2]) + np.array([0.2, 0.1]) == approx(0.3) # doctest: +SKIP + True + By default, ``approx`` considers numbers within a relative tolerance of ``1e-6`` (i.e. one part in a million) of its expected value to be equal. This treatment would lead to surprising results if the expected value was @@ -398,8 +444,7 @@ def approx(expected, rel=None, abs=None, nan_ok=False): __ https://docs.python.org/3/reference/datamodel.html#object.__ge__ """ - from collections import Mapping, Sequence - from _pytest.compat import STRING_TYPES as String + from decimal import Decimal # Delegate the comparison to a class that knows how to deal with the type # of the expected value (e.g. int, float, list, dict, numpy.array, etc). @@ -419,8 +464,10 @@ def approx(expected, rel=None, abs=None, nan_ok=False): cls = ApproxNumpy elif isinstance(expected, Mapping): cls = ApproxMapping - elif isinstance(expected, Sequence) and not isinstance(expected, String): + elif isinstance(expected, Sequence) and not isinstance(expected, STRING_TYPES): cls = ApproxSequence + elif isinstance(expected, Decimal): + cls = ApproxDecimal else: cls = ApproxScalar @@ -435,9 +482,10 @@ def _is_numpy_array(obj): import inspect for cls in inspect.getmro(type(obj)): - if cls.__module__ == 'numpy': + if cls.__module__ == "numpy": try: import numpy as np + return isinstance(obj, np.ndarray) except ImportError: pass @@ -447,15 +495,19 @@ def _is_numpy_array(obj): # builtin pytest.raises helper + def raises(expected_exception, *args, **kwargs): - """ + r""" Assert that a code block/function call raises ``expected_exception`` and raise a failure exception otherwise. + :arg message: if specified, provides a custom failure message if the + exception is not raised + :arg match: if specified, asserts that the exception matches a text or regex + This helper produces a ``ExceptionInfo()`` object (see below). - If using Python 2.5 or above, you may use this function as a - context manager:: + You may use this function as a context manager:: >>> with raises(ZeroDivisionError): ... 1/0 @@ -531,8 +583,9 @@ def raises(expected_exception, *args, **kwargs): The string will be evaluated using the same ``locals()`` and ``globals()`` at the moment of the ``raises`` call. - .. autoclass:: _pytest._code.ExceptionInfo - :members: + .. currentmodule:: _pytest._code + + Consult the API of ``excinfo`` objects: :class:`ExceptionInfo`. .. note:: Similar to caught exception objects in Python, explicitly clearing @@ -550,16 +603,14 @@ def raises(expected_exception, *args, **kwargs): """ __tracebackhide__ = True - msg = ("exceptions must be old-style classes or" - " derived from BaseException, not %s") - if isinstance(expected_exception, tuple): - for exc in expected_exception: - if not isclass(exc): - raise TypeError(msg % type(exc)) - elif not isclass(expected_exception): - raise TypeError(msg % type(expected_exception)) + for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): + msg = ( + "exceptions must be old-style classes or" + " derived from BaseException, not %s" + ) + raise TypeError(msg % type(exc)) - message = "DID NOT RAISE {0}".format(expected_exception) + message = "DID NOT RAISE {}".format(expected_exception) match_expr = None if not args: @@ -567,7 +618,10 @@ def raises(expected_exception, *args, **kwargs): message = kwargs.pop("message") if "match" in kwargs: match_expr = kwargs.pop("match") - message += " matching '{0}'".format(match_expr) + if kwargs: + msg = "Unexpected keyword arguments passed to pytest.raises: " + msg += ", ".join(kwargs.keys()) + raise TypeError(msg) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): code, = args @@ -596,6 +650,7 @@ raises.Exception = fail.Exception class RaisesContext(object): + def __init__(self, expected_exception, message, match_expr): self.expected_exception = expected_exception self.message = message @@ -610,17 +665,10 @@ class RaisesContext(object): __tracebackhide__ = True if tp[0] is None: fail(self.message) - if sys.version_info < (2, 7): - # py26: on __exit__() exc_value often does not contain the - # exception value. - # http://bugs.python.org/issue7853 - if not isinstance(tp[1], BaseException): - exc_type, value, traceback = tp - tp = exc_type, exc_type(value), traceback self.excinfo.__init__(tp) suppress_exception = issubclass(self.excinfo.type, self.expected_exception) if sys.version_info[0] == 2 and suppress_exception: sys.exc_clear() - if self.match_expr: + if self.match_expr and suppress_exception: self.excinfo.match(self.match_expr) return suppress_exception diff --git a/third_party/python/pytest/_pytest/recwarn.py b/third_party/python/pytest/src/_pytest/recwarn.py similarity index 65% rename from third_party/python/pytest/_pytest/recwarn.py rename to third_party/python/pytest/src/_pytest/recwarn.py index c9fa872c0770..7839f5700c87 100644 --- a/third_party/python/pytest/_pytest/recwarn.py +++ b/third_party/python/pytest/src/_pytest/recwarn.py @@ -8,23 +8,22 @@ import py import sys import warnings +import re + from _pytest.fixtures import yield_fixture from _pytest.outcomes import fail @yield_fixture def recwarn(): - """Return a WarningsRecorder instance that provides these methods: - - * ``pop(category=None)``: return last warning matching the category. - * ``clear()``: clear list of warnings + """Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. See http://docs.python.org/library/warnings.html for information on warning categories. """ wrec = WarningsRecorder() with wrec: - warnings.simplefilter('default') + warnings.simplefilter("default") yield wrec @@ -77,20 +76,22 @@ class _DeprecatedCallContext(object): if exc_type is None: deprecation_categories = (DeprecationWarning, PendingDeprecationWarning) - if not any(issubclass(c, deprecation_categories) for c in self._captured_categories): + if not any( + issubclass(c, deprecation_categories) for c in self._captured_categories + ): __tracebackhide__ = True msg = "Did not produce DeprecationWarning or PendingDeprecationWarning" raise AssertionError(msg) def warns(expected_warning, *args, **kwargs): - """Assert that code raises a particular class of warning. + r"""Assert that code raises a particular class of warning. - Specifically, the input @expected_warning can be a warning class or - tuple of warning classes, and the code must return that warning - (if a single class) or one of those warnings (if a tuple). + Specifically, the parameter ``expected_warning`` can be a warning class or + sequence of warning classes, and the inside the ``with`` block must issue a warning of that class or + classes. - This helper produces a list of ``warnings.WarningMessage`` objects, + This helper produces a list of :class:`warnings.WarningMessage` objects, one for each warning raised. This function can be used as a context manager, or any of the other ways @@ -98,10 +99,28 @@ def warns(expected_warning, *args, **kwargs): >>> with warns(RuntimeWarning): ... warnings.warn("my warning", RuntimeWarning) + + In the context manager form you may use the keyword argument ``match`` to assert + that the exception matches a text or regex:: + + >>> with warns(UserWarning, match='must be 0 or None'): + ... warnings.warn("value must be 0 or None", UserWarning) + + >>> with warns(UserWarning, match=r'must be \d+$'): + ... warnings.warn("value must be 42", UserWarning) + + >>> with warns(UserWarning, match=r'must be \d+$'): + ... warnings.warn("this is not here", UserWarning) + Traceback (most recent call last): + ... + Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted... + """ - wcheck = WarningsChecker(expected_warning) + match_expr = None if not args: - return wcheck + if "match" in kwargs: + match_expr = kwargs.pop("match") + return WarningsChecker(expected_warning, match_expr=match_expr) elif isinstance(args[0], str): code, = args assert isinstance(code, str) @@ -109,12 +128,12 @@ def warns(expected_warning, *args, **kwargs): loc = frame.f_locals.copy() loc.update(kwargs) - with wcheck: + with WarningsChecker(expected_warning, match_expr=match_expr): code = _pytest._code.Source(code).compile() py.builtin.exec_(code, frame.f_globals, loc) else: func = args[0] - with wcheck: + with WarningsChecker(expected_warning, match_expr=match_expr): return func(*args[1:], **kwargs) @@ -163,7 +182,7 @@ class WarningsRecorder(warnings.catch_warnings): __tracebackhide__ = True raise RuntimeError("Cannot enter %r twice" % self) self._list = super(WarningsRecorder, self).__enter__() - warnings.simplefilter('always') + warnings.simplefilter("always") return self def __exit__(self, *exc_info): @@ -174,11 +193,13 @@ class WarningsRecorder(warnings.catch_warnings): class WarningsChecker(WarningsRecorder): - def __init__(self, expected_warning=None): + + def __init__(self, expected_warning=None, match_expr=None): super(WarningsChecker, self).__init__() - msg = ("exceptions must be old-style classes or " - "derived from Warning, not %s") + msg = ( + "exceptions must be old-style classes or " "derived from Warning, not %s" + ) if isinstance(expected_warning, tuple): for exc in expected_warning: if not inspect.isclass(exc): @@ -189,6 +210,7 @@ class WarningsChecker(WarningsRecorder): raise TypeError(msg % type(expected_warning)) self.expected_warning = expected_warning + self.match_expr = match_expr def __exit__(self, *exc_info): super(WarningsChecker, self).__exit__(*exc_info) @@ -196,10 +218,26 @@ class WarningsChecker(WarningsRecorder): # only check if we're not currently handling an exception if all(a is None for a in exc_info): if self.expected_warning is not None: - if not any(issubclass(r.category, self.expected_warning) - for r in self): + if not any(issubclass(r.category, self.expected_warning) for r in self): __tracebackhide__ = True - fail("DID NOT WARN. No warnings of type {0} was emitted. " - "The list of emitted warnings is: {1}.".format( - self.expected_warning, - [each.message for each in self])) + fail( + "DID NOT WARN. No warnings of type {} was emitted. " + "The list of emitted warnings is: {}.".format( + self.expected_warning, [each.message for each in self] + ) + ) + elif self.match_expr is not None: + for r in self: + if issubclass(r.category, self.expected_warning): + if re.compile(self.match_expr).search(str(r.message)): + break + else: + fail( + "DID NOT WARN. No warnings of type {} matching" + " ('{}') was emitted. The list of emitted warnings" + " is: {}.".format( + self.expected_warning, + self.match_expr, + [each.message for each in self], + ) + ) diff --git a/third_party/python/pytest/_pytest/resultlog.py b/third_party/python/pytest/src/_pytest/resultlog.py similarity index 78% rename from third_party/python/pytest/_pytest/resultlog.py rename to third_party/python/pytest/src/_pytest/resultlog.py index 9f9c2d1f6536..8f300c983001 100644 --- a/third_party/python/pytest/_pytest/resultlog.py +++ b/third_party/python/pytest/src/_pytest/resultlog.py @@ -9,28 +9,34 @@ import os def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "resultlog plugin options") - group.addoption('--resultlog', '--result-log', action="store", - metavar="path", default=None, - help="DEPRECATED path for machine-readable result log.") + group.addoption( + "--resultlog", + "--result-log", + action="store", + metavar="path", + default=None, + help="DEPRECATED path for machine-readable result log.", + ) def pytest_configure(config): resultlog = config.option.resultlog # prevent opening resultlog on slave nodes (xdist) - if resultlog and not hasattr(config, 'slaveinput'): + if resultlog and not hasattr(config, "slaveinput"): dirname = os.path.dirname(os.path.abspath(resultlog)) if not os.path.isdir(dirname): os.makedirs(dirname) - logfile = open(resultlog, 'w', 1) # line buffered + logfile = open(resultlog, "w", 1) # line buffered config._resultlog = ResultLog(config, logfile) config.pluginmanager.register(config._resultlog) from _pytest.deprecated import RESULT_LOG - config.warn('C1', RESULT_LOG) + + config.warn("C1", RESULT_LOG) def pytest_unconfigure(config): - resultlog = getattr(config, '_resultlog', None) + resultlog = getattr(config, "_resultlog", None) if resultlog: resultlog.logfile.close() del config._resultlog @@ -46,22 +52,23 @@ def generic_path(item): newfspath = node.fspath if newfspath == fspath: if fspart: - gpath.append(':') + gpath.append(":") fspart = False else: - gpath.append('.') + gpath.append(".") else: - gpath.append('/') + gpath.append("/") fspart = True name = node.name - if name[0] in '([': + if name[0] in "([": gpath.pop() gpath.append(name) fspath = newfspath - return ''.join(gpath) + return "".join(gpath) class ResultLog(object): + def __init__(self, config, logfile): self.config = config self.logfile = logfile # preferably line buffered @@ -72,7 +79,7 @@ class ResultLog(object): print(" %s" % line, file=self.logfile) def log_outcome(self, report, lettercode, longrepr): - testpath = getattr(report, 'nodeid', None) + testpath = getattr(report, "nodeid", None) if testpath is None: testpath = report.fspath self.write_log_entry(testpath, lettercode, longrepr) @@ -82,10 +89,10 @@ class ResultLog(object): return res = self.config.hook.pytest_report_teststatus(report=report) code = res[1] - if code == 'x': + if code == "x": longrepr = str(report.longrepr) - elif code == 'X': - longrepr = '' + elif code == "X": + longrepr = "" elif report.passed: longrepr = "" elif report.failed: @@ -106,8 +113,8 @@ class ResultLog(object): self.log_outcome(report, code, longrepr) def pytest_internalerror(self, excrepr): - reprcrash = getattr(excrepr, 'reprcrash', None) + reprcrash = getattr(excrepr, "reprcrash", None) path = getattr(reprcrash, "path", None) if path is None: path = "cwd:%s" % py.path.local() - self.write_log_entry(path, '!', str(excrepr)) + self.write_log_entry(path, "!", str(excrepr)) diff --git a/third_party/python/pytest/_pytest/runner.py b/third_party/python/pytest/src/_pytest/runner.py similarity index 76% rename from third_party/python/pytest/_pytest/runner.py rename to third_party/python/pytest/src/_pytest/runner.py index b643fa3c91a7..18e925509ecc 100644 --- a/third_party/python/pytest/_pytest/runner.py +++ b/third_party/python/pytest/src/_pytest/runner.py @@ -7,7 +7,6 @@ import sys from time import time import py -from _pytest.compat import _PY2 from _pytest._code.code import TerminalRepr, ExceptionInfo from _pytest.outcomes import skip, Skipped, TEST_OUTCOME @@ -17,9 +16,14 @@ from _pytest.outcomes import skip, Skipped, TEST_OUTCOME def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "reporting", after="general") - group.addoption('--durations', - action="store", type=int, default=None, metavar="N", - help="show N slowest setup/test durations (N=0 for all)."), + group.addoption( + "--durations", + action="store", + type=int, + default=None, + metavar="N", + help="show N slowest setup/test durations (N=0 for all).", + ), def pytest_terminal_summary(terminalreporter): @@ -30,7 +34,7 @@ def pytest_terminal_summary(terminalreporter): dlist = [] for replist in tr.stats.values(): for rep in replist: - if hasattr(rep, 'duration'): + if hasattr(rep, "duration"): dlist.append(rep) if not dlist: return @@ -44,8 +48,7 @@ def pytest_terminal_summary(terminalreporter): for rep in dlist: nodeid = rep.nodeid.replace("::()::", "::") - tr.write_line("%02.2fs %-8s %s" % - (rep.duration, rep.when, nodeid)) + tr.write_line("%02.2fs %-8s %s" % (rep.duration, rep.when, nodeid)) def pytest_sessionstart(session): @@ -57,10 +60,9 @@ def pytest_sessionfinish(session): def pytest_runtest_protocol(item, nextitem): - item.ihook.pytest_runtest_logstart( - nodeid=item.nodeid, location=item.location, - ) + item.ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location) runtestprotocol(item, nextitem=nextitem) + item.ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location) return True @@ -75,8 +77,7 @@ def runtestprotocol(item, log=True, nextitem=None): show_test_item(item) if not item.config.option.setuponly: reports.append(call_and_report(item, "call", log)) - reports.append(call_and_report(item, "teardown", log, - nextitem=nextitem)) + reports.append(call_and_report(item, "teardown", log, nextitem=nextitem)) # after all teardown hooks have been called # want funcargs and request info to go away if hasrequest: @@ -89,20 +90,21 @@ def show_test_item(item): """Show test function, parameters and the fixtures of the test item.""" tw = item.config.get_terminal_writer() tw.line() - tw.write(' ' * 8) + tw.write(" " * 8) tw.write(item._nodeid) used_fixtures = sorted(item._fixtureinfo.name2fixturedefs.keys()) if used_fixtures: - tw.write(' (fixtures used: {0})'.format(', '.join(used_fixtures))) + tw.write(" (fixtures used: {})".format(", ".join(used_fixtures))) def pytest_runtest_setup(item): - _update_current_test_var(item, 'setup') + _update_current_test_var(item, "setup") item.session._setupstate.prepare(item) def pytest_runtest_call(item): - _update_current_test_var(item, 'call') + _update_current_test_var(item, "call") + sys.last_type, sys.last_value, sys.last_traceback = (None, None, None) try: item.runtest() except Exception: @@ -112,12 +114,12 @@ def pytest_runtest_call(item): sys.last_type = type sys.last_value = value sys.last_traceback = tb - del tb # Get rid of it in this namespace + del type, value, tb # Get rid of these in this frame raise def pytest_runtest_teardown(item, nextitem): - _update_current_test_var(item, 'teardown') + _update_current_test_var(item, "teardown") item.session._setupstate.teardown_exact(item, nextitem) _update_current_test_var(item, None) @@ -128,12 +130,11 @@ def _update_current_test_var(item, when): If ``when`` is None, delete PYTEST_CURRENT_TEST from the environment. """ - var_name = 'PYTEST_CURRENT_TEST' + var_name = "PYTEST_CURRENT_TEST" if when: - value = '{0} ({1})'.format(item.nodeid, when) - if _PY2: - # python 2 doesn't like null bytes on environment variables (see #2644) - value = value.replace('\x00', '(null)') + value = "{} ({})".format(item.nodeid, when) + # don't allow null bytes on environment variables (see #2644, #2957) + value = value.replace("\x00", "(null)") os.environ[var_name] = value else: os.environ.pop(var_name) @@ -153,6 +154,7 @@ def pytest_report_teststatus(report): # # Implementation + def call_and_report(item, when, log=True, **kwds): call = call_runtest_hook(item, when, **kwds) hook = item.ihook @@ -166,23 +168,28 @@ def call_and_report(item, when, log=True, **kwds): def check_interactive_exception(call, report): return call.excinfo and not ( - hasattr(report, "wasxfail") or - call.excinfo.errisinstance(skip.Exception) or - call.excinfo.errisinstance(bdb.BdbQuit)) + hasattr(report, "wasxfail") + or call.excinfo.errisinstance(skip.Exception) + or call.excinfo.errisinstance(bdb.BdbQuit) + ) def call_runtest_hook(item, when, **kwds): hookname = "pytest_runtest_" + when ihook = getattr(item.ihook, hookname) - return CallInfo(lambda: ihook(item=item, **kwds), when=when) + return CallInfo( + lambda: ihook(item=item, **kwds), + when=when, + treat_keyboard_interrupt_as_exception=item.config.getvalue("usepdb"), + ) -class CallInfo: +class CallInfo(object): """ Result/Exception info a function invocation. """ #: None or ExceptionInfo object. excinfo = None - def __init__(self, func, when): + def __init__(self, func, when, treat_keyboard_interrupt_as_exception=False): #: context of invocation: one of "setup", "call", #: "teardown", "memocollect" self.when = when @@ -190,8 +197,11 @@ class CallInfo: try: self.result = func() except KeyboardInterrupt: - self.stop = time() - raise + if treat_keyboard_interrupt_as_exception: + self.excinfo = ExceptionInfo() + else: + self.stop = time() + raise except: # noqa self.excinfo = ExceptionInfo() self.stop = time() @@ -209,9 +219,10 @@ def getslaveinfoline(node): return node._slaveinfocache except AttributeError: d = node.slaveinfo - ver = "%s.%s.%s" % d['version_info'][:3] + ver = "%s.%s.%s" % d["version_info"][:3] node._slaveinfocache = s = "[%s] %s -- Python %s %s" % ( - d['id'], d['sysplatform'], ver, d['executable']) + d["id"], d["sysplatform"], ver, d["executable"] + ) return s @@ -221,14 +232,14 @@ class BaseReport(object): self.__dict__.update(kw) def toterminal(self, out): - if hasattr(self, 'node'): + if hasattr(self, "node"): out.line(getslaveinfoline(self.node)) longrepr = self.longrepr if longrepr is None: return - if hasattr(longrepr, 'toterminal'): + if hasattr(longrepr, "toterminal"): longrepr.toterminal(out) else: try: @@ -255,13 +266,25 @@ class BaseReport(object): exc = tw.stringio.getvalue() return exc.strip() + @property + def caplog(self): + """Return captured log lines, if log capturing is enabled + + .. versionadded:: 3.5 + """ + return "\n".join( + content for (prefix, content) in self.get_sections("Captured log") + ) + @property def capstdout(self): """Return captured text from stdout, if capturing is enabled .. versionadded:: 3.0 """ - return ''.join(content for (prefix, content) in self.get_sections('Captured stdout')) + return "".join( + content for (prefix, content) in self.get_sections("Captured stdout") + ) @property def capstderr(self): @@ -269,7 +292,9 @@ class BaseReport(object): .. versionadded:: 3.0 """ - return ''.join(content for (prefix, content) in self.get_sections('Captured stderr')) + return "".join( + content for (prefix, content) in self.get_sections("Captured stderr") + ) passed = property(lambda x: x.outcome == "passed") failed = property(lambda x: x.outcome == "failed") @@ -283,7 +308,7 @@ class BaseReport(object): def pytest_runtest_makereport(item, call): when = call.when duration = call.stop - call.start - keywords = dict([(x, 1) for x in item.keywords]) + keywords = {x: 1 for x in item.keywords} excinfo = call.excinfo sections = [] if not call.excinfo: @@ -302,13 +327,22 @@ def pytest_runtest_makereport(item, call): if call.when == "call": longrepr = item.repr_failure(excinfo) else: # exception in setup or teardown - longrepr = item._repr_failure_py(excinfo, - style=item.config.option.tbstyle) + longrepr = item._repr_failure_py( + excinfo, style=item.config.option.tbstyle + ) for rwhen, key, content in item._report_sections: sections.append(("Captured %s %s" % (key, rwhen), content)) - return TestReport(item.nodeid, item.location, - keywords, outcome, longrepr, when, - sections, duration) + return TestReport( + item.nodeid, + item.location, + keywords, + outcome, + longrepr, + when, + sections, + duration, + user_properties=item.user_properties, + ) class TestReport(BaseReport): @@ -316,8 +350,19 @@ class TestReport(BaseReport): they fail). """ - def __init__(self, nodeid, location, keywords, outcome, - longrepr, when, sections=(), duration=0, **extra): + def __init__( + self, + nodeid, + location, + keywords, + outcome, + longrepr, + when, + sections=(), + duration=0, + user_properties=(), + **extra + ): #: normalized collection node id self.nodeid = nodeid @@ -339,6 +384,10 @@ class TestReport(BaseReport): #: one of 'setup', 'call', 'teardown' to indicate runtest phase. self.when = when + #: user properties is a list of tuples (name, value) that holds user + #: defined properties of the test + self.user_properties = user_properties + #: list of pairs ``(str, str)`` of extra information which needs to #: marshallable. Used by pytest to add captured text #: from ``stdout`` and ``stderr``, but may be used by other plugins @@ -352,7 +401,8 @@ class TestReport(BaseReport): def __repr__(self): return "" % ( - self.nodeid, self.when, self.outcome) + self.nodeid, self.when, self.outcome + ) class TeardownErrorReport(BaseReport): @@ -366,14 +416,13 @@ class TeardownErrorReport(BaseReport): def pytest_make_collect_report(collector): - call = CallInfo( - lambda: list(collector.collect()), - 'collect') + call = CallInfo(lambda: list(collector.collect()), "collect") longrepr = None if not call.excinfo: outcome = "passed" else: from _pytest import nose + skip_exceptions = (Skipped,) + nose.get_skip_exceptions() if call.excinfo.errisinstance(skip_exceptions): outcome = "skipped" @@ -385,15 +434,16 @@ def pytest_make_collect_report(collector): if not hasattr(errorinfo, "toterminal"): errorinfo = CollectErrorRepr(errorinfo) longrepr = errorinfo - rep = CollectReport(collector.nodeid, outcome, longrepr, - getattr(call, 'result', None)) + rep = CollectReport( + collector.nodeid, outcome, longrepr, getattr(call, "result", None) + ) rep.call = call # see collect_one_node return rep class CollectReport(BaseReport): - def __init__(self, nodeid, outcome, longrepr, result, - sections=(), **extra): + + def __init__(self, nodeid, outcome, longrepr, result, sections=(), **extra): self.nodeid = nodeid self.outcome = outcome self.longrepr = longrepr @@ -407,10 +457,12 @@ class CollectReport(BaseReport): def __repr__(self): return "" % ( - self.nodeid, len(self.result), self.outcome) + self.nodeid, len(self.result), self.outcome + ) class CollectErrorRepr(TerminalRepr): + def __init__(self, msg): self.longrepr = msg @@ -431,7 +483,7 @@ class SetupState(object): is called at the end of teardown_all(). """ assert colitem and not isinstance(colitem, tuple) - assert py.builtin.callable(finalizer) + assert callable(finalizer) # assert colitem in self.stack # some unit tests don't setup stack :/ self._finalizers.setdefault(colitem, []).append(finalizer) @@ -459,8 +511,9 @@ class SetupState(object): if hasattr(colitem, "teardown"): colitem.teardown() for colitem in self._finalizers: - assert colitem is None or colitem in self.stack \ - or isinstance(colitem, tuple) + assert ( + colitem is None or colitem in self.stack or isinstance(colitem, tuple) + ) def teardown_all(self): while self.stack: @@ -474,10 +527,19 @@ class SetupState(object): self._teardown_towards(needed_collectors) def _teardown_towards(self, needed_collectors): + exc = None while self.stack: if self.stack == needed_collectors[:len(self.stack)]: break - self._pop_and_teardown() + try: + self._pop_and_teardown() + except TEST_OUTCOME: + # XXX Only first exception will be seen by user, + # ideally all should be reported. + if exc is None: + exc = sys.exc_info() + if exc: + py.builtin._reraise(*exc) def prepare(self, colitem): """ setup objects along the collector chain to the test-method @@ -487,7 +549,7 @@ class SetupState(object): # check if the last collection node has raised an error for col in self.stack: - if hasattr(col, '_prepare_exc'): + if hasattr(col, "_prepare_exc"): py.builtin._reraise(*col._prepare_exc) for col in needed_collectors[len(self.stack):]: self.stack.append(col) diff --git a/third_party/python/pytest/_pytest/setuponly.py b/third_party/python/pytest/src/_pytest/setuponly.py similarity index 55% rename from third_party/python/pytest/_pytest/setuponly.py rename to third_party/python/pytest/src/_pytest/setuponly.py index 15e195ad5a11..81240d9d055e 100644 --- a/third_party/python/pytest/_pytest/setuponly.py +++ b/third_party/python/pytest/src/_pytest/setuponly.py @@ -6,10 +6,18 @@ import sys def pytest_addoption(parser): group = parser.getgroup("debugconfig") - group.addoption('--setuponly', '--setup-only', action="store_true", - help="only setup fixtures, do not execute tests.") - group.addoption('--setupshow', '--setup-show', action="store_true", - help="show setup of fixtures while executing tests.") + group.addoption( + "--setuponly", + "--setup-only", + action="store_true", + help="only setup fixtures, do not execute tests.", + ) + group.addoption( + "--setupshow", + "--setup-show", + action="store_true", + help="show setup of fixtures while executing tests.", + ) @pytest.hookimpl(hookwrapper=True) @@ -17,53 +25,55 @@ def pytest_fixture_setup(fixturedef, request): yield config = request.config if config.option.setupshow: - if hasattr(request, 'param'): + if hasattr(request, "param"): # Save the fixture parameter so ._show_fixture_action() can # display it now and during the teardown (in .finish()). if fixturedef.ids: if callable(fixturedef.ids): fixturedef.cached_param = fixturedef.ids(request.param) else: - fixturedef.cached_param = fixturedef.ids[ - request.param_index] + fixturedef.cached_param = fixturedef.ids[request.param_index] else: fixturedef.cached_param = request.param - _show_fixture_action(fixturedef, 'SETUP') + _show_fixture_action(fixturedef, "SETUP") def pytest_fixture_post_finalizer(fixturedef): if hasattr(fixturedef, "cached_result"): config = fixturedef._fixturemanager.config if config.option.setupshow: - _show_fixture_action(fixturedef, 'TEARDOWN') + _show_fixture_action(fixturedef, "TEARDOWN") if hasattr(fixturedef, "cached_param"): del fixturedef.cached_param def _show_fixture_action(fixturedef, msg): config = fixturedef._fixturemanager.config - capman = config.pluginmanager.getplugin('capturemanager') + capman = config.pluginmanager.getplugin("capturemanager") if capman: - out, err = capman.suspendcapture() + out, err = capman.suspend_global_capture() tw = config.get_terminal_writer() tw.line() - tw.write(' ' * 2 * fixturedef.scopenum) - tw.write('{step} {scope} {fixture}'.format( - step=msg.ljust(8), # align the output to TEARDOWN - scope=fixturedef.scope[0].upper(), - fixture=fixturedef.argname)) + tw.write(" " * 2 * fixturedef.scopenum) + tw.write( + "{step} {scope} {fixture}".format( + step=msg.ljust(8), # align the output to TEARDOWN + scope=fixturedef.scope[0].upper(), + fixture=fixturedef.argname, + ) + ) - if msg == 'SETUP': - deps = sorted(arg for arg in fixturedef.argnames if arg != 'request') + if msg == "SETUP": + deps = sorted(arg for arg in fixturedef.argnames if arg != "request") if deps: - tw.write(' (fixtures used: {0})'.format(', '.join(deps))) + tw.write(" (fixtures used: {})".format(", ".join(deps))) - if hasattr(fixturedef, 'cached_param'): - tw.write('[{0}]'.format(fixturedef.cached_param)) + if hasattr(fixturedef, "cached_param"): + tw.write("[{}]".format(fixturedef.cached_param)) if capman: - capman.resumecapture() + capman.resume_global_capture() sys.stdout.write(out) sys.stderr.write(err) diff --git a/third_party/python/pytest/_pytest/setupplan.py b/third_party/python/pytest/src/_pytest/setupplan.py similarity index 75% rename from third_party/python/pytest/_pytest/setupplan.py rename to third_party/python/pytest/src/_pytest/setupplan.py index e11bd40698b7..23f4f97e6d97 100644 --- a/third_party/python/pytest/_pytest/setupplan.py +++ b/third_party/python/pytest/src/_pytest/setupplan.py @@ -5,9 +5,13 @@ import pytest def pytest_addoption(parser): group = parser.getgroup("debugconfig") - group.addoption('--setupplan', '--setup-plan', action="store_true", - help="show what fixtures and tests would be executed but " - "don't execute anything.") + group.addoption( + "--setupplan", + "--setup-plan", + action="store_true", + help="show what fixtures and tests would be executed but " + "don't execute anything.", + ) @pytest.hookimpl(tryfirst=True) diff --git a/third_party/python/pytest/src/_pytest/skipping.py b/third_party/python/pytest/src/_pytest/skipping.py new file mode 100644 index 000000000000..a348d5484cdb --- /dev/null +++ b/third_party/python/pytest/src/_pytest/skipping.py @@ -0,0 +1,293 @@ +""" support for skip/xfail functions and markers. """ +from __future__ import absolute_import, division, print_function + +from _pytest.config import hookimpl +from _pytest.mark.evaluate import MarkEvaluator +from _pytest.outcomes import fail, skip, xfail + + +def pytest_addoption(parser): + group = parser.getgroup("general") + group.addoption( + "--runxfail", + action="store_true", + dest="runxfail", + default=False, + help="run tests even if they are marked xfail", + ) + + parser.addini( + "xfail_strict", + "default for the strict parameter of xfail " + "markers when not given explicitly (default: False)", + default=False, + type="bool", + ) + + +def pytest_configure(config): + if config.option.runxfail: + # yay a hack + import pytest + + old = pytest.xfail + config._cleanup.append(lambda: setattr(pytest, "xfail", old)) + + def nop(*args, **kwargs): + pass + + nop.Exception = xfail.Exception + setattr(pytest, "xfail", nop) + + config.addinivalue_line( + "markers", + "skip(reason=None): skip the given test function with an optional reason. " + 'Example: skip(reason="no way of currently testing this") skips the ' + "test.", + ) + config.addinivalue_line( + "markers", + "skipif(condition): skip the given test function if eval(condition) " + "results in a True value. Evaluation happens within the " + "module global context. Example: skipif('sys.platform == \"win32\"') " + "skips the test if we are on the win32 platform. see " + "http://pytest.org/latest/skipping.html", + ) + config.addinivalue_line( + "markers", + "xfail(condition, reason=None, run=True, raises=None, strict=False): " + "mark the test function as an expected failure if eval(condition) " + "has a True value. Optionally specify a reason for better reporting " + "and run=False if you don't even want to execute the test function. " + "If only specific exception(s) are expected, you can list them in " + "raises, and if the test fails in other ways, it will be reported as " + "a true failure. See http://pytest.org/latest/skipping.html", + ) + + +@hookimpl(tryfirst=True) +def pytest_runtest_setup(item): + # Check if skip or skipif are specified as pytest marks + item._skipped_by_mark = False + eval_skipif = MarkEvaluator(item, "skipif") + if eval_skipif.istrue(): + item._skipped_by_mark = True + skip(eval_skipif.getexplanation()) + + for skip_info in item.iter_markers(name="skip"): + item._skipped_by_mark = True + if "reason" in skip_info.kwargs: + skip(skip_info.kwargs["reason"]) + elif skip_info.args: + skip(skip_info.args[0]) + else: + skip("unconditional skip") + + item._evalxfail = MarkEvaluator(item, "xfail") + check_xfail_no_run(item) + + +@hookimpl(hookwrapper=True) +def pytest_pyfunc_call(pyfuncitem): + check_xfail_no_run(pyfuncitem) + outcome = yield + passed = outcome.excinfo is None + if passed: + check_strict_xfail(pyfuncitem) + + +def check_xfail_no_run(item): + """check xfail(run=False)""" + if not item.config.option.runxfail: + evalxfail = item._evalxfail + if evalxfail.istrue(): + if not evalxfail.get("run", True): + xfail("[NOTRUN] " + evalxfail.getexplanation()) + + +def check_strict_xfail(pyfuncitem): + """check xfail(strict=True) for the given PASSING test""" + evalxfail = pyfuncitem._evalxfail + if evalxfail.istrue(): + strict_default = pyfuncitem.config.getini("xfail_strict") + is_strict_xfail = evalxfail.get("strict", strict_default) + if is_strict_xfail: + del pyfuncitem._evalxfail + explanation = evalxfail.getexplanation() + fail("[XPASS(strict)] " + explanation, pytrace=False) + + +@hookimpl(hookwrapper=True) +def pytest_runtest_makereport(item, call): + outcome = yield + rep = outcome.get_result() + evalxfail = getattr(item, "_evalxfail", None) + # unitttest special case, see setting of _unexpectedsuccess + if hasattr(item, "_unexpectedsuccess") and rep.when == "call": + from _pytest.compat import _is_unittest_unexpected_success_a_failure + + if item._unexpectedsuccess: + rep.longrepr = "Unexpected success: {}".format(item._unexpectedsuccess) + else: + rep.longrepr = "Unexpected success" + if _is_unittest_unexpected_success_a_failure(): + rep.outcome = "failed" + else: + rep.outcome = "passed" + rep.wasxfail = rep.longrepr + elif item.config.option.runxfail: + pass # don't interefere + elif call.excinfo and call.excinfo.errisinstance(xfail.Exception): + rep.wasxfail = "reason: " + call.excinfo.value.msg + rep.outcome = "skipped" + elif evalxfail and not rep.skipped and evalxfail.wasvalid() and evalxfail.istrue(): + if call.excinfo: + if evalxfail.invalidraise(call.excinfo.value): + rep.outcome = "failed" + else: + rep.outcome = "skipped" + rep.wasxfail = evalxfail.getexplanation() + elif call.when == "call": + strict_default = item.config.getini("xfail_strict") + is_strict_xfail = evalxfail.get("strict", strict_default) + explanation = evalxfail.getexplanation() + if is_strict_xfail: + rep.outcome = "failed" + rep.longrepr = "[XPASS(strict)] {}".format(explanation) + else: + rep.outcome = "passed" + rep.wasxfail = explanation + elif getattr(item, "_skipped_by_mark", False) and rep.skipped and type( + rep.longrepr + ) is tuple: + # skipped by mark.skipif; change the location of the failure + # to point to the item definition, otherwise it will display + # the location of where the skip exception was raised within pytest + filename, line, reason = rep.longrepr + filename, line = item.location[:2] + rep.longrepr = filename, line, reason + + +# called by terminalreporter progress reporting + + +def pytest_report_teststatus(report): + if hasattr(report, "wasxfail"): + if report.skipped: + return "xfailed", "x", "xfail" + elif report.passed: + return "xpassed", "X", ("XPASS", {"yellow": True}) + + +# called by the terminalreporter instance/plugin + + +def pytest_terminal_summary(terminalreporter): + tr = terminalreporter + if not tr.reportchars: + # for name in "xfailed skipped failed xpassed": + # if not tr.stats.get(name, 0): + # tr.write_line("HINT: use '-r' option to see extra " + # "summary info about tests") + # break + return + + lines = [] + for char in tr.reportchars: + action = REPORTCHAR_ACTIONS.get(char, lambda tr, lines: None) + action(terminalreporter, lines) + + if lines: + tr._tw.sep("=", "short test summary info") + for line in lines: + tr._tw.line(line) + + +def show_simple(terminalreporter, lines, stat, format): + failed = terminalreporter.stats.get(stat) + if failed: + for rep in failed: + pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid) + lines.append(format % (pos,)) + + +def show_xfailed(terminalreporter, lines): + xfailed = terminalreporter.stats.get("xfailed") + if xfailed: + for rep in xfailed: + pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid) + reason = rep.wasxfail + lines.append("XFAIL %s" % (pos,)) + if reason: + lines.append(" " + str(reason)) + + +def show_xpassed(terminalreporter, lines): + xpassed = terminalreporter.stats.get("xpassed") + if xpassed: + for rep in xpassed: + pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid) + reason = rep.wasxfail + lines.append("XPASS %s %s" % (pos, reason)) + + +def folded_skips(skipped): + d = {} + for event in skipped: + key = event.longrepr + assert len(key) == 3, (event, key) + keywords = getattr(event, "keywords", {}) + # folding reports with global pytestmark variable + # this is workaround, because for now we cannot identify the scope of a skip marker + # TODO: revisit after marks scope would be fixed + when = getattr(event, "when", None) + if when == "setup" and "skip" in keywords and "pytestmark" not in keywords: + key = (key[0], None, key[2]) + d.setdefault(key, []).append(event) + values = [] + for key, events in d.items(): + values.append((len(events),) + key) + return values + + +def show_skipped(terminalreporter, lines): + tr = terminalreporter + skipped = tr.stats.get("skipped", []) + if skipped: + # if not tr.hasopt('skipped'): + # tr.write_line( + # "%d skipped tests, specify -rs for more info" % + # len(skipped)) + # return + fskips = folded_skips(skipped) + if fskips: + # tr.write_sep("_", "skipped test summary") + for num, fspath, lineno, reason in fskips: + if reason.startswith("Skipped: "): + reason = reason[9:] + if lineno is not None: + lines.append( + "SKIP [%d] %s:%d: %s" % (num, fspath, lineno + 1, reason) + ) + else: + lines.append("SKIP [%d] %s: %s" % (num, fspath, reason)) + + +def shower(stat, format): + + def show_(terminalreporter, lines): + return show_simple(terminalreporter, lines, stat, format) + + return show_ + + +REPORTCHAR_ACTIONS = { + "x": show_xfailed, + "X": show_xpassed, + "f": shower("failed", "FAIL %s"), + "F": shower("failed", "FAIL %s"), + "s": show_skipped, + "S": show_skipped, + "p": shower("passed", "PASSED %s"), + "E": shower("error", "ERROR %s"), +} diff --git a/third_party/python/pytest/_pytest/terminal.py b/third_party/python/pytest/src/_pytest/terminal.py similarity index 60% rename from third_party/python/pytest/_pytest/terminal.py rename to third_party/python/pytest/src/_pytest/terminal.py index 9da94d0c917a..9c4eec7531a9 100644 --- a/third_party/python/pytest/_pytest/terminal.py +++ b/third_party/python/pytest/src/_pytest/terminal.py @@ -5,74 +5,170 @@ This is a good source for looking at the various reporting hooks. from __future__ import absolute_import, division, print_function import itertools -from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \ - EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED -import pytest -import py +import platform import sys import time -import platform +import pluggy +import py +import six +from more_itertools import collapse + +import pytest from _pytest import nodes -import _pytest._pluggy as pluggy +from _pytest.main import ( + EXIT_OK, + EXIT_TESTSFAILED, + EXIT_INTERRUPTED, + EXIT_USAGEERROR, + EXIT_NOTESTSCOLLECTED, +) + + +import argparse + + +class MoreQuietAction(argparse.Action): + """ + a modified copy of the argparse count action which counts down and updates + the legacy quiet attribute at the same time + + used to unify verbosity handling + """ + + def __init__(self, option_strings, dest, default=None, required=False, help=None): + super(MoreQuietAction, self).__init__( + option_strings=option_strings, + dest=dest, + nargs=0, + default=default, + required=required, + help=help, + ) + + def __call__(self, parser, namespace, values, option_string=None): + new_count = getattr(namespace, self.dest, 0) - 1 + setattr(namespace, self.dest, new_count) + # todo Deprecate config.quiet + namespace.quiet = getattr(namespace, "quiet", 0) + 1 def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "reporting", after="general") - group._addoption('-v', '--verbose', action="count", - dest="verbose", default=0, help="increase verbosity."), - group._addoption('-q', '--quiet', action="count", - dest="quiet", default=0, help="decrease verbosity."), - group._addoption('-r', - action="store", dest="reportchars", default='', metavar="chars", - help="show extra test summary info as specified by chars (f)ailed, " - "(E)error, (s)skipped, (x)failed, (X)passed, " - "(p)passed, (P)passed with output, (a)all except pP. " - "Warnings are displayed at all times except when " - "--disable-warnings is set") - group._addoption('--disable-warnings', '--disable-pytest-warnings', default=False, - dest='disable_warnings', action='store_true', - help='disable warnings summary') - group._addoption('-l', '--showlocals', - action="store_true", dest="showlocals", default=False, - help="show locals in tracebacks (disabled by default).") - group._addoption('--tb', metavar="style", - action="store", dest="tbstyle", default='auto', - choices=['auto', 'long', 'short', 'no', 'line', 'native'], - help="traceback print mode (auto/long/short/line/native/no).") - group._addoption('--fulltrace', '--full-trace', - action="store_true", default=False, - help="don't cut any tracebacks (default is to cut).") - group._addoption('--color', metavar="color", - action="store", dest="color", default='auto', - choices=['yes', 'no', 'auto'], - help="color terminal output (yes/no/auto).") + group._addoption( + "-v", + "--verbose", + action="count", + default=0, + dest="verbose", + help="increase verbosity.", + ), + group._addoption( + "-q", + "--quiet", + action=MoreQuietAction, + default=0, + dest="verbose", + help="decrease verbosity.", + ), + group._addoption( + "--verbosity", dest="verbose", type=int, default=0, help="set verbosity" + ) + group._addoption( + "-r", + action="store", + dest="reportchars", + default="", + metavar="chars", + help="show extra test summary info as specified by chars (f)ailed, " + "(E)error, (s)skipped, (x)failed, (X)passed, " + "(p)passed, (P)passed with output, (a)all except pP. " + "Warnings are displayed at all times except when " + "--disable-warnings is set", + ) + group._addoption( + "--disable-warnings", + "--disable-pytest-warnings", + default=False, + dest="disable_warnings", + action="store_true", + help="disable warnings summary", + ) + group._addoption( + "-l", + "--showlocals", + action="store_true", + dest="showlocals", + default=False, + help="show locals in tracebacks (disabled by default).", + ) + group._addoption( + "--tb", + metavar="style", + action="store", + dest="tbstyle", + default="auto", + choices=["auto", "long", "short", "no", "line", "native"], + help="traceback print mode (auto/long/short/line/native/no).", + ) + group._addoption( + "--show-capture", + action="store", + dest="showcapture", + choices=["no", "stdout", "stderr", "log", "all"], + default="all", + help="Controls how captured stdout/stderr/log is shown on failed tests. " + "Default is 'all'.", + ) + group._addoption( + "--fulltrace", + "--full-trace", + action="store_true", + default=False, + help="don't cut any tracebacks (default is to cut).", + ) + group._addoption( + "--color", + metavar="color", + action="store", + dest="color", + default="auto", + choices=["yes", "no", "auto"], + help="color terminal output (yes/no/auto).", + ) + + parser.addini( + "console_output_style", + help="console output: classic or with additional progress information (classic|progress).", + default="progress", + ) def pytest_configure(config): - config.option.verbose -= config.option.quiet reporter = TerminalReporter(config, sys.stdout) - config.pluginmanager.register(reporter, 'terminalreporter') + config.pluginmanager.register(reporter, "terminalreporter") if config.option.debug or config.option.traceconfig: + def mywriter(tags, args): msg = " ".join(map(str, args)) reporter.write_line("[traceconfig] " + msg) + config.trace.root.setprocessor("pytest:config", mywriter) def getreportopt(config): reportopts = "" reportchars = config.option.reportchars - if not config.option.disable_warnings and 'w' not in reportchars: - reportchars += 'w' - elif config.option.disable_warnings and 'w' in reportchars: - reportchars = reportchars.replace('w', '') + if not config.option.disable_warnings and "w" not in reportchars: + reportchars += "w" + elif config.option.disable_warnings and "w" in reportchars: + reportchars = reportchars.replace("w", "") if reportchars: for char in reportchars: - if char not in reportopts and char != 'a': + if char not in reportopts and char != "a": reportopts += char - elif char == 'a': - reportopts = 'fEsxXw' + elif char == "a": + reportopts = "fEsxXw" return reportopts @@ -88,7 +184,7 @@ def pytest_report_teststatus(report): return report.outcome, letter, report.outcome.upper() -class WarningReport: +class WarningReport(object): """ Simple structure to hold warnings information captured by ``pytest_logwarning``. """ @@ -117,40 +213,59 @@ class WarningReport: if isinstance(self.fslocation, tuple) and len(self.fslocation) >= 2: filename, linenum = self.fslocation[:2] relpath = py.path.local(filename).relto(config.invocation_dir) - return '%s:%s' % (relpath, linenum) + return "%s:%s" % (relpath, linenum) else: return str(self.fslocation) return None -class TerminalReporter: +class TerminalReporter(object): + def __init__(self, config, file=None): import _pytest.config + self.config = config self.verbosity = self.config.option.verbose self.showheader = self.verbosity >= 0 self.showfspath = self.verbosity >= 0 self.showlongtestinfo = self.verbosity > 0 self._numcollected = 0 + self._session = None self.stats = {} self.startdir = py.path.local() if file is None: file = sys.stdout - self._tw = self.writer = _pytest.config.create_terminal_writer(config, - file) + self._tw = _pytest.config.create_terminal_writer(config, file) + # self.writer will be deprecated in pytest-3.4 + self.writer = self._tw + self._screen_width = self._tw.fullwidth self.currentfspath = None self.reportchars = getreportopt(config) self.hasmarkup = self._tw.hasmarkup self.isatty = file.isatty() + self._progress_nodeids_reported = set() + self._show_progress_info = self._determine_show_progress_info() + + def _determine_show_progress_info(self): + """Return True if we should display progress information based on the current config""" + # do not show progress if we are not capturing output (#3038) + if self.config.getoption("capture") == "no": + return False + # do not show progress if we are showing fixture setup/teardown + if self.config.getoption("setupshow"): + return False + return self.config.getini("console_output_style") == "progress" def hasopt(self, char): - char = {'xfailed': 'x', 'skipped': 's'}.get(char, char) + char = {"xfailed": "x", "skipped": "s"}.get(char, char) return char in self.reportchars def write_fspath_result(self, nodeid, res): fspath = self.config.rootdir.join(nodeid.split("::")[0]) if fspath != self.currentfspath: + if self.currentfspath is not None: + self._write_progress_information_filling_space() self.currentfspath = fspath fspath = self.startdir.bestrelpath(fspath) self._tw.line() @@ -175,8 +290,8 @@ class TerminalReporter: self._tw.write(content, **markup) def write_line(self, line, **markup): - if not py.builtin._istext(line): - line = py.builtin.text(line, errors="replace") + if not isinstance(line, six.text_type): + line = six.text_type(line, errors="replace") self.ensure_newline() self._tw.line(line, **markup) @@ -189,12 +304,12 @@ class TerminalReporter: The rest of the keyword arguments are markup instructions. """ - erase = markup.pop('erase', False) + erase = markup.pop("erase", False) if erase: - fill_count = self._tw.fullwidth - len(line) - fill = ' ' * fill_count + fill_count = self._tw.fullwidth - len(line) - 1 + fill = " " * fill_count else: - fill = '' + fill = "" line = str(line) self._tw.write("\r" + line + fill, **markup) @@ -209,14 +324,15 @@ class TerminalReporter: self._tw.line(msg, **kw) def pytest_internalerror(self, excrepr): - for line in py.builtin.text(excrepr).split("\n"): + for line in six.text_type(excrepr).split("\n"): self.write_line("INTERNALERROR> " + line) return 1 def pytest_logwarning(self, code, fslocation, message, nodeid): warnings = self.stats.setdefault("warnings", []) - warning = WarningReport(code=code, fslocation=fslocation, - message=message, nodeid=nodeid) + warning = WarningReport( + code=code, fslocation=fslocation, message=message, nodeid=nodeid + ) warnings.append(warning) def pytest_plugin_registered(self, plugin): @@ -228,7 +344,7 @@ class TerminalReporter: self.write_line(msg) def pytest_deselected(self, items): - self.stats.setdefault('deselected', []).extend(items) + self.stats.setdefault("deselected", []).extend(items) def pytest_runtest_logstart(self, nodeid, location): # ensure that the path is printed before the @@ -244,38 +360,82 @@ class TerminalReporter: rep = report res = self.config.hook.pytest_report_teststatus(report=rep) cat, letter, word = res + if isinstance(word, tuple): + word, markup = word + else: + markup = None self.stats.setdefault(cat, []).append(rep) self._tests_ran = True if not letter and not word: # probably passed setup/teardown return + running_xdist = hasattr(rep, "node") if self.verbosity <= 0: - if not hasattr(rep, 'node') and self.showfspath: + if not running_xdist and self.showfspath: self.write_fspath_result(rep.nodeid, letter) else: self._tw.write(letter) else: - if isinstance(word, tuple): - word, markup = word - else: + self._progress_nodeids_reported.add(rep.nodeid) + if markup is None: if rep.passed: - markup = {'green': True} + markup = {"green": True} elif rep.failed: - markup = {'red': True} + markup = {"red": True} elif rep.skipped: - markup = {'yellow': True} + markup = {"yellow": True} + else: + markup = {} line = self._locationline(rep.nodeid, *rep.location) - if not hasattr(rep, 'node'): + if not running_xdist: self.write_ensure_prefix(line, word, **markup) - # self._tw.write(word, **markup) + if self._show_progress_info: + self._write_progress_information_filling_space() else: self.ensure_newline() - if hasattr(rep, 'node'): - self._tw.write("[%s] " % rep.node.gateway.id) + self._tw.write("[%s]" % rep.node.gateway.id) + if self._show_progress_info: + self._tw.write( + self._get_progress_information_message() + " ", cyan=True + ) + else: + self._tw.write(" ") self._tw.write(word, **markup) self._tw.write(" " + line) self.currentfspath = -2 + def pytest_runtest_logfinish(self, nodeid): + if self.verbosity <= 0 and self._show_progress_info: + self._progress_nodeids_reported.add(nodeid) + last_item = len( + self._progress_nodeids_reported + ) == self._session.testscollected + if last_item: + self._write_progress_information_filling_space() + else: + past_edge = self._tw.chars_on_current_line + self._PROGRESS_LENGTH + 1 >= self._screen_width + if past_edge: + msg = self._get_progress_information_message() + self._tw.write(msg + "\n", cyan=True) + + _PROGRESS_LENGTH = len(" [100%]") + + def _get_progress_information_message(self): + if self.config.getoption("capture") == "no": + return "" + collected = self._session.testscollected + if collected: + progress = len(self._progress_nodeids_reported) * 100 // collected + return " [{:3d}%]".format(progress) + return " [100%]" + + def _write_progress_information_filling_space(self): + msg = self._get_progress_information_message() + fill = " " * ( + self._tw.fullwidth - self._tw.chars_on_current_line - len(msg) - 1 + ) + self.write(fill + msg, cyan=True) + def pytest_collection(self): if not self.isatty and self.config.option.verbose >= 1: self.write("collecting ... ", bold=True) @@ -295,51 +455,63 @@ class TerminalReporter: if self.config.option.verbose < 0: return - errors = len(self.stats.get('error', [])) - skipped = len(self.stats.get('skipped', [])) + errors = len(self.stats.get("error", [])) + skipped = len(self.stats.get("skipped", [])) + deselected = len(self.stats.get("deselected", [])) if final: line = "collected " else: line = "collecting " - line += str(self._numcollected) + " item" + ('' if self._numcollected == 1 else 's') + line += str(self._numcollected) + " item" + ( + "" if self._numcollected == 1 else "s" + ) if errors: line += " / %d errors" % errors + if deselected: + line += " / %d deselected" % deselected if skipped: line += " / %d skipped" % skipped if self.isatty: self.rewrite(line, bold=True, erase=True) if final: - self.write('\n') + self.write("\n") else: self.write_line(line) + @pytest.hookimpl(trylast=True) def pytest_collection_modifyitems(self): self.report_collect(True) @pytest.hookimpl(trylast=True) def pytest_sessionstart(self, session): + self._session = session self._sessionstarttime = time.time() if not self.showheader: return self.write_sep("=", "test session starts", bold=True) verinfo = platform.python_version() msg = "platform %s -- Python %s" % (sys.platform, verinfo) - if hasattr(sys, 'pypy_version_info'): + if hasattr(sys, "pypy_version_info"): verinfo = ".".join(map(str, sys.pypy_version_info[:3])) msg += "[pypy-%s-%s]" % (verinfo, sys.pypy_version_info[3]) msg += ", pytest-%s, py-%s, pluggy-%s" % ( - pytest.__version__, py.__version__, pluggy.__version__) - if self.verbosity > 0 or self.config.option.debug or \ - getattr(self.config.option, 'pastebin', None): + pytest.__version__, py.__version__, pluggy.__version__ + ) + if ( + self.verbosity > 0 + or self.config.option.debug + or getattr(self.config.option, "pastebin", None) + ): msg += " -- " + str(sys.executable) self.write_line(msg) lines = self.config.hook.pytest_report_header( - config=self.config, startdir=self.startdir) + config=self.config, startdir=self.startdir + ) self._write_report_lines_from_hooks(lines) def _write_report_lines_from_hooks(self, lines): lines.reverse() - for line in flatten(lines): + for line in collapse(lines): self.write_line(line) def pytest_report_header(self, config): @@ -351,21 +523,21 @@ class TerminalReporter: plugininfo = config.pluginmanager.list_plugin_distinfo() if plugininfo: - lines.append( - "plugins: %s" % ", ".join(_plugin_nameversions(plugininfo))) + lines.append("plugins: %s" % ", ".join(_plugin_nameversions(plugininfo))) return lines def pytest_collection_finish(self, session): if self.config.option.collectonly: self._printcollecteditems(session.items) - if self.stats.get('failed'): + if self.stats.get("failed"): self._tw.sep("!", "collection failures") - for rep in self.stats.get('failed'): + for rep in self.stats.get("failed"): rep.toterminal(self._tw) return 1 return 0 lines = self.config.hook.pytest_report_collectionfinish( - config=self.config, startdir=self.startdir, items=session.items) + config=self.config, startdir=self.startdir, items=session.items + ) self._write_report_lines_from_hooks(lines) def _printcollecteditems(self, items): @@ -376,7 +548,7 @@ class TerminalReporter: if self.config.option.verbose < -1: counts = {} for item in items: - name = item.nodeid.split('::', 1)[0] + name = item.nodeid.split("::", 1)[0] counts[name] = counts.get(name, 0) + 1 for name, count in sorted(counts.items()): self._tw.line("%s: %d" % (name, count)) @@ -407,26 +579,34 @@ class TerminalReporter: outcome.get_result() self._tw.line("") summary_exit_codes = ( - EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, EXIT_USAGEERROR, - EXIT_NOTESTSCOLLECTED) + EXIT_OK, + EXIT_TESTSFAILED, + EXIT_INTERRUPTED, + EXIT_USAGEERROR, + EXIT_NOTESTSCOLLECTED, + ) if exitstatus in summary_exit_codes: - self.config.hook.pytest_terminal_summary(terminalreporter=self, - exitstatus=exitstatus) - self.summary_errors() - self.summary_failures() - self.summary_warnings() - self.summary_passes() + self.config.hook.pytest_terminal_summary( + terminalreporter=self, exitstatus=exitstatus + ) if exitstatus == EXIT_INTERRUPTED: self._report_keyboardinterrupt() del self._keyboardinterrupt_memo - self.summary_deselected() self.summary_stats() + @pytest.hookimpl(hookwrapper=True) + def pytest_terminal_summary(self): + self.summary_errors() + self.summary_failures() + yield + self.summary_warnings() + self.summary_passes() + def pytest_keyboard_interrupt(self, excinfo): self._keyboardinterrupt_memo = excinfo.getrepr(funcargs=True) def pytest_unconfigure(self): - if hasattr(self, '_keyboardinterrupt_memo'): + if hasattr(self, "_keyboardinterrupt_memo"): self._report_keyboardinterrupt() def _report_keyboardinterrupt(self): @@ -437,18 +617,23 @@ class TerminalReporter: if self.config.option.fulltrace: excrepr.toterminal(self._tw) else: - self._tw.line("to show a full traceback on KeyboardInterrupt use --fulltrace", yellow=True) excrepr.reprcrash.toterminal(self._tw) + self._tw.line( + "(to show a full traceback on KeyboardInterrupt use --fulltrace)", + yellow=True, + ) def _locationline(self, nodeid, fspath, lineno, domain): + def mkrel(nodeid): line = self.config.cwd_relative_nodeid(nodeid) if domain and line.endswith(domain): line = line[:-len(domain)] values = domain.split("[") - values[0] = values[0].replace('.', '::') # don't replace '.' in params + values[0] = values[0].replace(".", "::") # don't replace '.' in params line += "[".join(values) return line + # collect_fspath comes from testid which has a "/"-normalized path if fspath: @@ -460,7 +645,7 @@ class TerminalReporter: return res + " " def _getfailureheadline(self, rep): - if hasattr(rep, 'location'): + if hasattr(rep, "location"): fspath, lineno, domain = rep.location return domain else: @@ -481,7 +666,7 @@ class TerminalReporter: def getreports(self, name): values = [] for x in self.stats.get(name, []): - if not hasattr(x, '_pdbshown'): + if not hasattr(x, "_pdbshown"): values.append(x) return values @@ -491,22 +676,24 @@ class TerminalReporter: if not all_warnings: return - grouped = itertools.groupby(all_warnings, key=lambda wr: wr.get_location(self.config)) + grouped = itertools.groupby( + all_warnings, key=lambda wr: wr.get_location(self.config) + ) self.write_sep("=", "warnings summary", yellow=True, bold=False) - for location, warnings in grouped: - self._tw.line(str(location) or '') - for w in warnings: + for location, warning_records in grouped: + self._tw.line(str(location) if location else "") + for w in warning_records: lines = w.message.splitlines() - indented = '\n'.join(' ' + x for x in lines) + indented = "\n".join(" " + x for x in lines) self._tw.line(indented) self._tw.line() - self._tw.line('-- Docs: http://doc.pytest.org/en/latest/warnings.html') + self._tw.line("-- Docs: http://doc.pytest.org/en/latest/warnings.html") def summary_passes(self): if self.config.option.tbstyle != "no": if self.hasopt("P"): - reports = self.getreports('passed') + reports = self.getreports("passed") if not reports: return self.write_sep("=", "PASSES") @@ -517,15 +704,15 @@ class TerminalReporter: def print_teardown_sections(self, rep): for secname, content in rep.sections: - if 'teardown' in secname: - self._tw.sep('-', secname) + if "teardown" in secname: + self._tw.sep("-", secname) if content[-1:] == "\n": content = content[:-1] self._tw.line(content) def summary_failures(self): if self.config.option.tbstyle != "no": - reports = self.getreports('failed') + reports = self.getreports("failed") if not reports: return self.write_sep("=", "FAILURES") @@ -535,22 +722,22 @@ class TerminalReporter: self.write_line(line) else: msg = self._getfailureheadline(rep) - markup = {'red': True, 'bold': True} + markup = {"red": True, "bold": True} self.write_sep("_", msg, **markup) self._outrep_summary(rep) - for report in self.getreports(''): - if report.nodeid == rep.nodeid and report.when == 'teardown': + for report in self.getreports(""): + if report.nodeid == rep.nodeid and report.when == "teardown": self.print_teardown_sections(report) def summary_errors(self): if self.config.option.tbstyle != "no": - reports = self.getreports('error') + reports = self.getreports("error") if not reports: return self.write_sep("=", "ERRORS") - for rep in self.stats['error']: + for rep in self.stats["error"]: msg = self._getfailureheadline(rep) - if not hasattr(rep, 'when'): + if not hasattr(rep, "when"): # collect msg = "ERROR collecting " + msg elif rep.when == "setup": @@ -562,7 +749,12 @@ class TerminalReporter: def _outrep_summary(self, rep): rep.toterminal(self._tw) + showcapture = self.config.option.showcapture + if showcapture == "no": + return for secname, content in rep.sections: + if showcapture != "all" and showcapture not in secname: + continue self._tw.sep("-", secname) if content[-1:] == "\n": content = content[:-1] @@ -572,18 +764,13 @@ class TerminalReporter: session_duration = time.time() - self._sessionstarttime (line, color) = build_summary_stats_line(self.stats) msg = "%s in %.2f seconds" % (line, session_duration) - markup = {color: True, 'bold': True} + markup = {color: True, "bold": True} if self.verbosity >= 0: self.write_sep("=", msg, **markup) if self.verbosity == -1: self.write_line(msg, **markup) - def summary_deselected(self): - if 'deselected' in self.stats: - self.write_sep("=", "%d tests deselected" % ( - len(self.stats['deselected'])), bold=True) - def repr_pythonversion(v=None): if v is None: @@ -594,18 +781,10 @@ def repr_pythonversion(v=None): return str(v) -def flatten(values): - for x in values: - if isinstance(x, (list, tuple)): - for y in flatten(x): - yield y - else: - yield x - - def build_summary_stats_line(stats): - keys = ("failed passed skipped deselected " - "xfailed xpassed warnings error").split() + keys = ( + "failed passed skipped deselected " "xfailed xpassed warnings error" + ).split() unknown_key_seen = False for key in stats.keys(): if key not in keys: @@ -623,14 +802,14 @@ def build_summary_stats_line(stats): else: line = "no tests ran" - if 'failed' in stats or 'error' in stats: - color = 'red' - elif 'warnings' in stats or unknown_key_seen: - color = 'yellow' - elif 'passed' in stats: - color = 'green' + if "failed" in stats or "error" in stats: + color = "red" + elif "warnings" in stats or unknown_key_seen: + color = "yellow" + elif "passed" in stats: + color = "green" else: - color = 'yellow' + color = "yellow" return (line, color) @@ -639,7 +818,7 @@ def _plugin_nameversions(plugininfo): values = [] for plugin, dist in plugininfo: # gets us name and version! - name = '{dist.project_name}-{dist.version}'.format(dist=dist) + name = "{dist.project_name}-{dist.version}".format(dist=dist) # questionable convenience, but it keeps things short if name.startswith("pytest-"): name = name[7:] diff --git a/third_party/python/pytest/_pytest/tmpdir.py b/third_party/python/pytest/src/_pytest/tmpdir.py similarity index 86% rename from third_party/python/pytest/_pytest/tmpdir.py rename to third_party/python/pytest/src/_pytest/tmpdir.py index da1b032237af..260d28422343 100644 --- a/third_party/python/pytest/_pytest/tmpdir.py +++ b/third_party/python/pytest/src/_pytest/tmpdir.py @@ -8,7 +8,7 @@ import py from _pytest.monkeypatch import MonkeyPatch -class TempdirFactory: +class TempdirFactory(object): """Factory for temporary directories under the common base temp directory. The base directory can be configured using the ``--basetemp`` option. @@ -37,8 +37,9 @@ class TempdirFactory: if not numbered: p = basetemp.mkdir(basename) else: - p = py.path.local.make_numbered_dir(prefix=basename, - keep=0, rootdir=basetemp, lock_timeout=None) + p = py.path.local.make_numbered_dir( + prefix=basename, keep=0, rootdir=basetemp, lock_timeout=None + ) self.trace("mktemp", p) return p @@ -59,12 +60,13 @@ class TempdirFactory: if user: # use a sub-directory in the temproot to speed-up # make_numbered_dir() call - rootdir = temproot.join('pytest-of-%s' % user) + rootdir = temproot.join("pytest-of-%s" % user) else: rootdir = temproot rootdir.ensure(dir=1) - basetemp = py.path.local.make_numbered_dir(prefix='pytest-', - rootdir=rootdir) + basetemp = py.path.local.make_numbered_dir( + prefix="pytest-", rootdir=rootdir + ) self._basetemp = t = basetemp.realpath() self.trace("new basetemp", t) return t @@ -78,6 +80,7 @@ def get_user(): in the current environment (see #1010). """ import getpass + try: return getpass.getuser() except (ImportError, KeyError): @@ -98,11 +101,11 @@ def pytest_configure(config): mp = MonkeyPatch() t = TempdirFactory(config) config._cleanup.extend([mp.undo, t.finish]) - mp.setattr(config, '_tmpdirhandler', t, raising=False) - mp.setattr(pytest, 'ensuretemp', t.ensuretemp, raising=False) + mp.setattr(config, "_tmpdirhandler", t, raising=False) + mp.setattr(pytest, "ensuretemp", t.ensuretemp, raising=False) -@pytest.fixture(scope='session') +@pytest.fixture(scope="session") def tmpdir_factory(request): """Return a TempdirFactory instance for the test session. """ @@ -116,6 +119,8 @@ def tmpdir(request, tmpdir_factory): created as a sub directory of the base temporary directory. The returned object is a `py.path.local`_ path object. + + .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html """ name = request.node.name name = re.sub(r"[\W]", "_", name) diff --git a/third_party/python/pytest/_pytest/unittest.py b/third_party/python/pytest/src/_pytest/unittest.py similarity index 77% rename from third_party/python/pytest/_pytest/unittest.py rename to third_party/python/pytest/src/_pytest/unittest.py index 52c9813e8b3a..6ad9fda88705 100644 --- a/third_party/python/pytest/_pytest/unittest.py +++ b/third_party/python/pytest/src/_pytest/unittest.py @@ -9,7 +9,6 @@ import _pytest._code from _pytest.config import hookimpl from _pytest.outcomes import fail, skip, xfail from _pytest.python import transfer_markers, Class, Module, Function -from _pytest.skipping import MarkEvaluator def pytest_pycollect_makeitem(collector, name, obj): @@ -30,18 +29,19 @@ class UnitTestCase(Class): def setup(self): cls = self.obj - if getattr(cls, '__unittest_skip__', False): + if getattr(cls, "__unittest_skip__", False): return # skipped - setup = getattr(cls, 'setUpClass', None) + setup = getattr(cls, "setUpClass", None) if setup is not None: setup() - teardown = getattr(cls, 'tearDownClass', None) + teardown = getattr(cls, "tearDownClass", None) if teardown is not None: self.addfinalizer(teardown) super(UnitTestCase, self).setup() def collect(self): from unittest import TestLoader + cls = self.obj if not getattr(cls, "__test__", True): return @@ -51,29 +51,30 @@ class UnitTestCase(Class): foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) - if not getattr(x, '__test__', True): + if not getattr(x, "__test__", True): continue - funcobj = getattr(x, 'im_func', x) + funcobj = getattr(x, "im_func", x) transfer_markers(funcobj, cls, module) - yield TestCaseFunction(name, parent=self) + yield TestCaseFunction(name, parent=self, callobj=funcobj) foundsomething = True if not foundsomething: - runtest = getattr(self.obj, 'runTest', None) + runtest = getattr(self.obj, "runTest", None) if runtest is not None: ut = sys.modules.get("twisted.trial.unittest", None) if ut is None or runtest != ut.TestCase.runTest: - yield TestCaseFunction('runTest', parent=self) + yield TestCaseFunction("runTest", parent=self) class TestCaseFunction(Function): + nofuncargs = True _excinfo = None def setup(self): self._testcase = self.parent.obj(self.name) self._fix_unittest_skip_decorator() self._obj = getattr(self._testcase, self.name) - if hasattr(self._testcase, 'setup_method'): + if hasattr(self._testcase, "setup_method"): self._testcase.setup_method(self._obj) if hasattr(self, "_request"): self._request._fillfixtures() @@ -92,7 +93,7 @@ class TestCaseFunction(Function): setattr(self._testcase, "__name__", self.name) def teardown(self): - if hasattr(self._testcase, 'teardown_method'): + if hasattr(self._testcase, "teardown_method"): self._testcase.teardown_method(self._obj) # Allow garbage collection on TestCase instance attributes. self._testcase = None @@ -103,26 +104,32 @@ class TestCaseFunction(Function): def _addexcinfo(self, rawexcinfo): # unwrap potential exception info (see twisted trial support below) - rawexcinfo = getattr(rawexcinfo, '_rawexcinfo', rawexcinfo) + rawexcinfo = getattr(rawexcinfo, "_rawexcinfo", rawexcinfo) try: excinfo = _pytest._code.ExceptionInfo(rawexcinfo) except TypeError: try: try: values = traceback.format_exception(*rawexcinfo) - values.insert(0, "NOTE: Incompatible Exception Representation, " - "displaying natively:\n\n") + values.insert( + 0, + "NOTE: Incompatible Exception Representation, " + "displaying natively:\n\n", + ) fail("".join(values), pytrace=False) except (fail.Exception, KeyboardInterrupt): raise except: # noqa - fail("ERROR: Unknown Incompatible Exception " - "representation:\n%r" % (rawexcinfo,), pytrace=False) + fail( + "ERROR: Unknown Incompatible Exception " + "representation:\n%r" % (rawexcinfo,), + pytrace=False, + ) except KeyboardInterrupt: raise except fail.Exception: excinfo = _pytest._code.ExceptionInfo() - self.__dict__.setdefault('_excinfo', []).append(excinfo) + self.__dict__.setdefault("_excinfo", []).append(excinfo) def addError(self, testcase, rawexcinfo): self._addexcinfo(rawexcinfo) @@ -134,8 +141,7 @@ class TestCaseFunction(Function): try: skip(reason) except skip.Exception: - self._evalskip = MarkEvaluator(self, 'SkipTest') - self._evalskip.result = True + self._skipped_by_mark = True self._addexcinfo(sys.exc_info()) def addExpectedFailure(self, testcase, rawexcinfo, reason=""): @@ -157,11 +163,15 @@ class TestCaseFunction(Function): # implements the skipping machinery (see #2137) # analog to pythons Lib/unittest/case.py:run testMethod = getattr(self._testcase, self._testcase._testMethodName) - if (getattr(self._testcase.__class__, "__unittest_skip__", False) or - getattr(testMethod, "__unittest_skip__", False)): + if ( + getattr(self._testcase.__class__, "__unittest_skip__", False) + or getattr(testMethod, "__unittest_skip__", False) + ): # If the class or method was skipped. - skip_why = (getattr(self._testcase.__class__, '__unittest_skip_why__', '') or - getattr(testMethod, '__unittest_skip_why__', '')) + skip_why = ( + getattr(self._testcase.__class__, "__unittest_skip_why__", "") + or getattr(testMethod, "__unittest_skip_why__", "") + ) try: # PY3, unittest2 on PY2 self._testcase._addSkip(self, self._testcase, skip_why) except TypeError: # PY2 @@ -183,7 +193,8 @@ class TestCaseFunction(Function): def _prunetraceback(self, excinfo): Function._prunetraceback(self, excinfo) traceback = excinfo.traceback.filter( - lambda x: not x.frame.f_globals.get('__unittest')) + lambda x: not x.frame.f_globals.get("__unittest") + ) if traceback: excinfo.traceback = traceback @@ -198,19 +209,20 @@ def pytest_runtest_makereport(item, call): except AttributeError: pass + # twisted trial support @hookimpl(hookwrapper=True) def pytest_runtest_protocol(item): - if isinstance(item, TestCaseFunction) and \ - 'twisted.trial.unittest' in sys.modules: - ut = sys.modules['twisted.python.failure'] + if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules: + ut = sys.modules["twisted.python.failure"] Failure__init__ = ut.Failure.__init__ check_testcase_implements_trial_reporter() - def excstore(self, exc_value=None, exc_type=None, exc_tb=None, - captureVars=None): + def excstore( + self, exc_value=None, exc_type=None, exc_tb=None, captureVars=None + ): if exc_value is None: self._rawexcinfo = sys.exc_info() else: @@ -218,8 +230,9 @@ def pytest_runtest_protocol(item): exc_type = type(exc_value) self._rawexcinfo = (exc_type, exc_value, exc_tb) try: - Failure__init__(self, exc_value, exc_type, exc_tb, - captureVars=captureVars) + Failure__init__( + self, exc_value, exc_type, exc_tb, captureVars=captureVars + ) except TypeError: Failure__init__(self, exc_value, exc_type, exc_tb) @@ -235,5 +248,6 @@ def check_testcase_implements_trial_reporter(done=[]): return from zope.interface import classImplements from twisted.trial.itrial import IReporter + classImplements(TestCaseFunction, IReporter) done.append(1) diff --git a/third_party/python/pytest/_pytest/warnings.py b/third_party/python/pytest/src/_pytest/warnings.py similarity index 62% rename from third_party/python/pytest/_pytest/warnings.py rename to third_party/python/pytest/src/_pytest/warnings.py index 926b1f581123..e023d0ab491b 100644 --- a/third_party/python/pytest/_pytest/warnings.py +++ b/third_party/python/pytest/src/_pytest/warnings.py @@ -12,13 +12,12 @@ def _setoption(wmod, arg): """ Copy of the warning._setoption function but does not escape arguments. """ - parts = arg.split(':') + parts = arg.split(":") if len(parts) > 5: raise wmod._OptionError("too many fields (max 5): %r" % (arg,)) while len(parts) < 5: - parts.append('') - action, message, category, module, lineno = [s.strip() - for s in parts] + parts.append("") + action, message, category, module, lineno = [s.strip() for s in parts] action = wmod._getaction(action) category = wmod._getcategory(category) if lineno: @@ -36,12 +35,18 @@ def _setoption(wmod, arg): def pytest_addoption(parser): group = parser.getgroup("pytest-warnings") group.addoption( - '-W', '--pythonwarnings', action='append', - help="set which warnings to report, see -W option of python itself.") - parser.addini("filterwarnings", type="linelist", - help="Each line specifies a pattern for " - "warnings.filterwarnings. " - "Processed after -W and --pythonwarnings.") + "-W", + "--pythonwarnings", + action="append", + help="set which warnings to report, see -W option of python itself.", + ) + parser.addini( + "filterwarnings", + type="linelist", + help="Each line specifies a pattern for " + "warnings.filterwarnings. " + "Processed after -W and --pythonwarnings.", + ) @contextmanager @@ -51,7 +56,7 @@ def catch_warnings_for_item(item): of the given item and after it is done posts them as warnings to this item. """ - args = item.config.getoption('pythonwarnings') or [] + args = item.config.getoption("pythonwarnings") or [] inifilters = item.config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: for arg in args: @@ -60,8 +65,7 @@ def catch_warnings_for_item(item): for arg in inifilters: _setoption(warnings, arg) - mark = item.get_marker('filterwarnings') - if mark: + for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: warnings._setoption(arg) @@ -71,21 +75,35 @@ def catch_warnings_for_item(item): warn_msg = warning.message unicode_warning = False - if compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args): - new_args = [compat.safe_str(m) for m in warn_msg.args] - unicode_warning = warn_msg.args != new_args + if ( + compat._PY2 + and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args) + ): + new_args = [] + for m in warn_msg.args: + new_args.append( + compat.ascii_escaped(m) + if isinstance(m, compat.UNICODE_TYPES) + else m + ) + unicode_warning = list(warn_msg.args) != new_args warn_msg.args = new_args msg = warnings.formatwarning( - warn_msg, warning.category, - warning.filename, warning.lineno, warning.line) + warn_msg, + warning.category, + warning.filename, + warning.lineno, + warning.line, + ) item.warn("unused", msg) if unicode_warning: warnings.warn( "Warning is using unicode non convertible to ascii, " "converting to a safe representation:\n %s" % msg, - UnicodeWarning) + UnicodeWarning, + ) @pytest.hookimpl(hookwrapper=True) diff --git a/third_party/python/pytest/pytest.py b/third_party/python/pytest/src/pytest.py similarity index 52% rename from third_party/python/pytest/pytest.py rename to third_party/python/pytest/src/pytest.py index 1c914a6edffe..f27f5a1955b2 100644 --- a/third_party/python/pytest/pytest.py +++ b/third_party/python/pytest/src/pytest.py @@ -6,10 +6,7 @@ pytest: unit and functional testing with Python. # else we are imported -from _pytest.config import ( - main, UsageError, _preloadplugins, cmdline, - hookspec, hookimpl -) +from _pytest.config import main, UsageError, cmdline, hookspec, hookimpl from _pytest.fixtures import fixture, yield_fixture from _pytest.assertion import register_assert_rewrite from _pytest.freeze_support import freeze_includes @@ -18,61 +15,58 @@ from _pytest.debugging import pytestPDB as __pytestPDB from _pytest.recwarn import warns, deprecated_call from _pytest.outcomes import fail, skip, importorskip, exit, xfail from _pytest.mark import MARK_GEN as mark, param -from _pytest.main import Item, Collector, File, Session +from _pytest.main import Session +from _pytest.nodes import Item, Collector, File from _pytest.fixtures import fillfixtures as _fillfuncargs -from _pytest.python import ( - Module, Class, Instance, Function, Generator, -) +from _pytest.python import Module, Class, Instance, Function, Generator from _pytest.python_api import approx, raises set_trace = __pytestPDB.set_trace __all__ = [ - 'main', - 'UsageError', - 'cmdline', - 'hookspec', - 'hookimpl', - '__version__', - 'register_assert_rewrite', - 'freeze_includes', - 'set_trace', - 'warns', - 'deprecated_call', - 'fixture', - 'yield_fixture', - 'fail', - 'skip', - 'xfail', - 'importorskip', - 'exit', - 'mark', - 'param', - 'approx', - '_fillfuncargs', - - 'Item', - 'File', - 'Collector', - 'Session', - 'Module', - 'Class', - 'Instance', - 'Function', - 'Generator', - 'raises', - - + "main", + "UsageError", + "cmdline", + "hookspec", + "hookimpl", + "__version__", + "register_assert_rewrite", + "freeze_includes", + "set_trace", + "warns", + "deprecated_call", + "fixture", + "yield_fixture", + "fail", + "skip", + "xfail", + "importorskip", + "exit", + "mark", + "param", + "approx", + "_fillfuncargs", + "Item", + "File", + "Collector", + "Session", + "Module", + "Class", + "Instance", + "Function", + "Generator", + "raises", ] -if __name__ == '__main__': +if __name__ == "__main__": # if run as a script or by 'python -m pytest' # we trigger the below "else" condition by the following import import pytest + raise SystemExit(pytest.main()) else: from _pytest.compat import _setup_collect_fakemodule - _preloadplugins() # to populate pytest.* namespace so help(pytest) works + _setup_collect_fakemodule() diff --git a/third_party/python/pytest/tasks/__init__.py b/third_party/python/pytest/tasks/__init__.py index 992f4a4ade68..ea5b1293e33e 100644 --- a/third_party/python/pytest/tasks/__init__.py +++ b/third_party/python/pytest/tasks/__init__.py @@ -4,10 +4,7 @@ Invoke tasks to help with pytest development and release process. import invoke -from . import generate, vendoring +from . import generate -ns = invoke.Collection( - generate, - vendoring -) +ns = invoke.Collection(generate) diff --git a/third_party/python/pytest/tasks/generate.py b/third_party/python/pytest/tasks/generate.py index fa8ee6557dfd..398af70c9427 100644 --- a/third_party/python/pytest/tasks/generate.py +++ b/third_party/python/pytest/tasks/generate.py @@ -1,68 +1,82 @@ -import os +""" +Invoke development tasks. +""" from pathlib import Path from subprocess import check_output, check_call import invoke -@invoke.task(help={ - 'version': 'version being released', -}) +@invoke.task(help={"version": "version being released"}) def announce(ctx, version): """Generates a new release announcement entry in the docs.""" # Get our list of authors - stdout = check_output(["git", "describe", "--abbrev=0", '--tags']) - stdout = stdout.decode('utf-8') + stdout = check_output(["git", "describe", "--abbrev=0", "--tags"]) + stdout = stdout.decode("utf-8") last_version = stdout.strip() - stdout = check_output(["git", "log", "{}..HEAD".format(last_version), "--format=%aN"]) - stdout = stdout.decode('utf-8') + stdout = check_output( + ["git", "log", "{}..HEAD".format(last_version), "--format=%aN"] + ) + stdout = stdout.decode("utf-8") contributors = set(stdout.splitlines()) - template_name = 'release.minor.rst' if version.endswith('.0') else 'release.patch.rst' - template_text = Path(__file__).parent.joinpath(template_name).read_text(encoding='UTF-8') + template_name = "release.minor.rst" if version.endswith( + ".0" + ) else "release.patch.rst" + template_text = Path(__file__).parent.joinpath(template_name).read_text( + encoding="UTF-8" + ) - contributors_text = '\n'.join('* {}'.format(name) for name in sorted(contributors)) + '\n' + contributors_text = "\n".join( + "* {}".format(name) for name in sorted(contributors) + ) + "\n" text = template_text.format(version=version, contributors=contributors_text) - target = Path(__file__).parent.joinpath('../doc/en/announce/release-{}.rst'.format(version)) - target.write_text(text, encoding='UTF-8') + target = Path(__file__).parent.joinpath( + "../doc/en/announce/release-{}.rst".format(version) + ) + target.write_text(text, encoding="UTF-8") print("[generate.announce] Generated {}".format(target.name)) # Update index with the new release entry - index_path = Path(__file__).parent.joinpath('../doc/en/announce/index.rst') - lines = index_path.read_text(encoding='UTF-8').splitlines() - indent = ' ' + index_path = Path(__file__).parent.joinpath("../doc/en/announce/index.rst") + lines = index_path.read_text(encoding="UTF-8").splitlines() + indent = " " for index, line in enumerate(lines): - if line.startswith('{}release-'.format(indent)): + if line.startswith("{}release-".format(indent)): new_line = indent + target.stem if line != new_line: lines.insert(index, new_line) - index_path.write_text('\n'.join(lines) + '\n', encoding='UTF-8') + index_path.write_text("\n".join(lines) + "\n", encoding="UTF-8") print("[generate.announce] Updated {}".format(index_path.name)) else: - print("[generate.announce] Skip {} (already contains release)".format(index_path.name)) + print( + "[generate.announce] Skip {} (already contains release)".format( + index_path.name + ) + ) break - check_call(['git', 'add', str(target)]) + check_call(["git", "add", str(target)]) @invoke.task() def regen(ctx): """Call regendoc tool to update examples and pytest output in the docs.""" print("[generate.regen] Updating docs") - check_call(['tox', '-e', 'regen']) + check_call(["tox", "-e", "regen"]) @invoke.task() def make_tag(ctx, version): - """Create a new (local) tag for the release, only if the repository is clean.""" + """Create a new, local tag for the release, only if the repository is clean.""" from git import Repo - repo = Repo('.') + repo = Repo(".") if repo.is_dirty(): - print('Current repository is dirty. Please commit any changes and try again.') + print("Current repository is dirty. Please commit any changes and try again.") raise invoke.Exit(code=2) tag_names = [x.name for x in repo.tags] @@ -74,89 +88,31 @@ def make_tag(ctx, version): repo.create_tag(version) -@invoke.task() -def devpi_upload(ctx, version, user, password=None): - """Creates and uploads a package to devpi for testing.""" - if password: - print("[generate.devpi_upload] devpi login {}".format(user)) - check_call(['devpi', 'login', user, '--password', password]) - - check_call(['devpi', 'use', 'https://devpi.net/{}/dev'.format(user)]) - - env = os.environ.copy() - env['SETUPTOOLS_SCM_PRETEND_VERSION'] = version - check_call(['devpi', 'upload', '--formats', 'sdist,bdist_wheel'], env=env) - print("[generate.devpi_upload] package uploaded") - - -@invoke.task(help={ - 'version': 'version being released', - 'user': 'name of the user on devpi to stage the generated package', - 'password': 'user password on devpi to stage the generated package ' - '(if not given assumed logged in)', -}) -def pre_release(ctx, version, user, password=None): - """Generates new docs, release announcements and uploads a new release to devpi for testing.""" +@invoke.task(help={"version": "version being released"}) +def pre_release(ctx, version): + """Generates new docs, release announcements and creates a local tag.""" announce(ctx, version) regen(ctx) changelog(ctx, version, write_out=True) - msg = 'Preparing release version {}'.format(version) - check_call(['git', 'commit', '-a', '-m', msg]) - + msg = "Preparing release version {}".format(version) + check_call(["git", "commit", "-a", "-m", msg]) + make_tag(ctx, version) - devpi_upload(ctx, version=version, user=user, password=password) - print() - print('[generate.pre_release] Please push your branch and open a PR.') + print("[generate.pre_release] Please push your branch and open a PR.") -@invoke.task(help={ - 'version': 'version being released', - 'user': 'name of the user on devpi to stage the generated package', - 'pypi_name': 'name of the pypi configuration section in your ~/.pypirc', -}) -def publish_release(ctx, version, user, pypi_name): - """Publishes a package previously created by the 'pre_release' command.""" - from git import Repo - repo = Repo('.') - tag_names = [x.name for x in repo.tags] - if version not in tag_names: - print('Could not find tag for version {}, exiting...'.format(version)) - raise invoke.Exit(code=2) - - check_call(['devpi', 'use', 'https://devpi.net/{}/dev'.format(user)]) - check_call(['devpi', 'push', 'pytest=={}'.format(version), 'pypi:{}'.format(pypi_name)]) - check_call(['git', 'push', 'git@github.com:pytest-dev/pytest.git', version]) - - emails = [ - 'pytest-dev@python.org', - 'python-announce-list@python.org' - ] - if version.endswith('.0'): - emails.append('testing-in-python@lists.idyll.org') - print('Version {} has been published to PyPI!'.format(version)) - print() - print('Please send an email announcement with the contents from:') - print() - print(' doc/en/announce/release-{}.rst'.format(version)) - print() - print('To the following mail lists:') - print() - print(' ', ','.join(emails)) - print() - print('And announce it on twitter adding the #pytest hash tag.') - - -@invoke.task(help={ - 'version': 'version being released', - 'write_out': 'write changes to the actial changelog' -}) +@invoke.task( + help={ + "version": "version being released", + "write_out": "write changes to the actual changelog", + } +) def changelog(ctx, version, write_out=False): if write_out: addopts = [] else: - addopts = ['--draft'] - check_call(['towncrier', '--version', version] + addopts) - + addopts = ["--draft"] + check_call(["towncrier", "--yes", "--version", version] + addopts) diff --git a/third_party/python/pytest/tasks/release.minor.rst b/third_party/python/pytest/tasks/release.minor.rst index 3c0b7d718a1c..bdd8282cfad9 100644 --- a/third_party/python/pytest/tasks/release.minor.rst +++ b/third_party/python/pytest/tasks/release.minor.rst @@ -3,7 +3,7 @@ pytest-{version} The pytest team is proud to announce the {version} release! -pytest is a mature Python testing tool with more than a 1600 tests +pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged diff --git a/third_party/python/pytest/tasks/release.patch.rst b/third_party/python/pytest/tasks/release.patch.rst index 56764b913079..1982dc353c4b 100644 --- a/third_party/python/pytest/tasks/release.patch.rst +++ b/third_party/python/pytest/tasks/release.patch.rst @@ -6,7 +6,7 @@ pytest {version} has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest - + The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: diff --git a/third_party/python/pytest/tasks/requirements.txt b/third_party/python/pytest/tasks/requirements.txt index 6392de0cc0a5..db54e76e85c8 100644 --- a/third_party/python/pytest/tasks/requirements.txt +++ b/third_party/python/pytest/tasks/requirements.txt @@ -1,5 +1,6 @@ -invoke -tox +-e . gitpython +invoke towncrier +tox wheel diff --git a/third_party/python/pytest/tasks/vendoring.py b/third_party/python/pytest/tasks/vendoring.py deleted file mode 100644 index 867f2946b2e4..000000000000 --- a/third_party/python/pytest/tasks/vendoring.py +++ /dev/null @@ -1,23 +0,0 @@ -from __future__ import absolute_import, print_function -import py -import invoke - -VENDOR_TARGET = py.path.local("_pytest/vendored_packages") -GOOD_FILES = 'README.md', '__init__.py' - -@invoke.task() -def remove_libs(ctx): - print("removing vendored libs") - for path in VENDOR_TARGET.listdir(): - if path.basename not in GOOD_FILES: - print(" ", path) - path.remove() - -@invoke.task(pre=[remove_libs]) -def update_libs(ctx): - print("installing libs") - ctx.run("pip install -t {target} pluggy".format(target=VENDOR_TARGET)) - ctx.run("git add {target}".format(target=VENDOR_TARGET)) - print("Please commit to finish the update after running the tests:") - print() - print(' git commit -am "Updated vendored libs"') diff --git a/third_party/python/pytest/testing/acceptance_test.py b/third_party/python/pytest/testing/acceptance_test.py index 8a8c3276280d..c2eed419c9cb 100644 --- a/third_party/python/pytest/testing/acceptance_test.py +++ b/third_party/python/pytest/testing/acceptance_test.py @@ -2,6 +2,9 @@ from __future__ import absolute_import, division, print_function import os import sys +import types + +import six import _pytest._code import py @@ -10,17 +13,18 @@ from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_USAGEERROR class TestGeneralUsage(object): + def test_config_error(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_configure(config): import pytest raise pytest.UsageError("hello") - """) + """ + ) result = testdir.runpytest(testdir.tmpdir) assert result.ret != 0 - result.stderr.fnmatch_lines([ - '*ERROR: hello' - ]) + result.stderr.fnmatch_lines(["*ERROR: hello"]) def test_root_conftest_syntax_error(self, testdir): testdir.makepyfile(conftest="raise SyntaxError\n") @@ -29,37 +33,38 @@ class TestGeneralUsage(object): assert result.ret != 0 def test_early_hook_error_issue38_1(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_sessionstart(): 0 / 0 - """) + """ + ) result = testdir.runpytest(testdir.tmpdir) assert result.ret != 0 # tracestyle is native by default for hook failures - result.stdout.fnmatch_lines([ - '*INTERNALERROR*File*conftest.py*line 2*', - '*0 / 0*', - ]) + result.stdout.fnmatch_lines( + ["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"] + ) result = testdir.runpytest(testdir.tmpdir, "--fulltrace") assert result.ret != 0 # tracestyle is native by default for hook failures - result.stdout.fnmatch_lines([ - '*INTERNALERROR*def pytest_sessionstart():*', - '*INTERNALERROR*0 / 0*', - ]) + result.stdout.fnmatch_lines( + ["*INTERNALERROR*def pytest_sessionstart():*", "*INTERNALERROR*0 / 0*"] + ) def test_early_hook_configure_error_issue38(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_configure(): 0 / 0 - """) + """ + ) result = testdir.runpytest(testdir.tmpdir) assert result.ret != 0 # here we get it on stderr - result.stderr.fnmatch_lines([ - '*INTERNALERROR*File*conftest.py*line 2*', - '*0 / 0*', - ]) + result.stderr.fnmatch_lines( + ["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"] + ) def test_file_not_found(self, testdir): result = testdir.runpytest("asd") @@ -67,61 +72,65 @@ class TestGeneralUsage(object): result.stderr.fnmatch_lines(["ERROR: file not found*asd"]) def test_file_not_found_unconfigure_issue143(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_configure(): print("---configure") def pytest_unconfigure(): print("---unconfigure") - """) + """ + ) result = testdir.runpytest("-s", "asd") assert result.ret == 4 # EXIT_USAGEERROR result.stderr.fnmatch_lines(["ERROR: file not found*asd"]) - result.stdout.fnmatch_lines([ - "*---configure", - "*---unconfigure", - ]) + result.stdout.fnmatch_lines(["*---configure", "*---unconfigure"]) def test_config_preparse_plugin_option(self, testdir): - testdir.makepyfile(pytest_xyz=""" + testdir.makepyfile( + pytest_xyz=""" def pytest_addoption(parser): parser.addoption("--xyz", dest="xyz", action="store") - """) - testdir.makepyfile(test_one=""" + """ + ) + testdir.makepyfile( + test_one=""" def test_option(pytestconfig): assert pytestconfig.option.xyz == "123" - """) + """ + ) result = testdir.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*1 passed*', - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_assertion_magic(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_this(): x = 0 assert x - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "> assert x", - "E assert 0", - ]) + result.stdout.fnmatch_lines(["> assert x", "E assert 0"]) assert result.ret == 1 def test_nested_import_error(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import import_fails def test_this(): assert import_fails.a == 1 - """) + """ + ) testdir.makepyfile(import_fails="import does_not_work") result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - # XXX on jython this fails: "> import import_fails", - "ImportError while importing test module*", - "*No module named *does_not_work*", - ]) + result.stdout.fnmatch_lines( + [ + # XXX on jython this fails: "> import import_fails", + "ImportError while importing test module*", + "*No module named *does_not_work*", + ] + ) assert result.ret == 2 def test_not_collectable_arguments(self, testdir): @@ -129,38 +138,41 @@ class TestGeneralUsage(object): p2 = testdir.makefile(".pyc", "123") result = testdir.runpytest(p1, p2) assert result.ret - result.stderr.fnmatch_lines([ - "*ERROR: not found:*%s" % (p2.basename,) - ]) + result.stderr.fnmatch_lines(["*ERROR: not found:*%s" % (p2.basename,)]) def test_issue486_better_reporting_on_conftest_load_failure(self, testdir): testdir.makepyfile("") testdir.makeconftest("import qwerty") result = testdir.runpytest("--help") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *--version* *warning*conftest.py* - """) + """ + ) result = testdir.runpytest() - result.stderr.fnmatch_lines(""" + result.stderr.fnmatch_lines( + """ *ERROR*could not load*conftest.py* - """) + """ + ) def test_early_skip(self, testdir): testdir.mkdir("xyz") - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_collect_directory(): pytest.skip("early") - """) + """ + ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED - result.stdout.fnmatch_lines([ - "*1 skip*" - ]) + result.stdout.fnmatch_lines(["*1 skip*"]) def test_issue88_initial_file_multinodes(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyFile(pytest.File): def collect(self): @@ -169,40 +181,46 @@ class TestGeneralUsage(object): return MyFile(path, parent) class MyItem(pytest.Item): pass - """) + """ + ) p = testdir.makepyfile("def test_hello(): pass") result = testdir.runpytest(p, "--collect-only") - result.stdout.fnmatch_lines([ - "*MyFile*test_issue88*", - "*Module*test_issue88*", - ]) + result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"]) def test_issue93_initialnode_importing_capturing(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import sys print ("should not be seen") sys.stderr.write("stder42\\n") - """) + """ + ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED assert "should not be seen" not in result.stdout.str() assert "stderr42" not in result.stderr.str() def test_conftest_printing_shows_if_error(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ print ("should be seen") assert 0 - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 assert "should be seen" in result.stdout.str() - @pytest.mark.skipif(not hasattr(py.path.local, 'mksymlinkto'), - reason="symlink not available on this platform") + @pytest.mark.skipif( + not hasattr(py.path.local, "mksymlinkto"), + reason="symlink not available on this platform", + ) def test_chdir(self, testdir): testdir.tmpdir.join("py").mksymlinkto(py._pydir) p = testdir.tmpdir.join("main.py") - p.write(_pytest._code.Source(""" + p.write( + _pytest._code.Source( + """ import sys, os sys.path.insert(0, '') import py @@ -210,13 +228,15 @@ class TestGeneralUsage(object): print (py.__path__) os.chdir(os.path.dirname(os.getcwd())) print (py.log) - """)) + """ + ) + ) result = testdir.runpython(p) assert not result.ret def test_issue109_sibling_conftests_not_loaded(self, testdir): - sub1 = testdir.tmpdir.mkdir("sub1") - sub2 = testdir.tmpdir.mkdir("sub2") + sub1 = testdir.mkdir("sub1") + sub2 = testdir.mkdir("sub2") sub1.join("conftest.py").write("assert 0") result = testdir.runpytest(sub2) assert result.ret == EXIT_NOTESTSCOLLECTED @@ -228,20 +248,21 @@ class TestGeneralUsage(object): assert result.ret == EXIT_USAGEERROR def test_directory_skipped(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_ignore_collect(): pytest.skip("intentional") - """) + """ + ) testdir.makepyfile("def test_hello(): pass") result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED - result.stdout.fnmatch_lines([ - "*1 skipped*" - ]) + result.stdout.fnmatch_lines(["*1 skipped*"]) def test_multiple_items_per_collector_byid(self, testdir): - c = testdir.makeconftest(""" + c = testdir.makeconftest( + """ import pytest class MyItem(pytest.Item): def runtest(self): @@ -252,15 +273,15 @@ class TestGeneralUsage(object): def pytest_collect_file(path, parent): if path.basename.startswith("conftest"): return MyCollector(path, parent) - """) + """ + ) result = testdir.runpytest(c.basename + "::" + "xyz") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*1 pass*", - ]) + result.stdout.fnmatch_lines(["*1 pass*"]) def test_skip_on_generated_funcarg_id(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_generate_tests(metafunc): metafunc.addcall({'x': 3}, id='hello-123') @@ -269,86 +290,89 @@ class TestGeneralUsage(object): if 'hello-123' in item.keywords: pytest.skip("hello") assert 0 - """) + """ + ) p = testdir.makepyfile("""def test_func(x): pass""") res = testdir.runpytest(p) assert res.ret == 0 res.stdout.fnmatch_lines(["*1 skipped*"]) def test_direct_addressing_selects(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall({'i': 1}, id="1") metafunc.addcall({'i': 2}, id="2") def test_func(i): pass - """) + """ + ) res = testdir.runpytest(p.basename + "::" + "test_func[1]") assert res.ret == 0 res.stdout.fnmatch_lines(["*1 passed*"]) def test_direct_addressing_notfound(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_func(): pass - """) + """ + ) res = testdir.runpytest(p.basename + "::" + "test_notfound") assert res.ret res.stderr.fnmatch_lines(["*ERROR*not found*"]) def test_docstring_on_hookspec(self): from _pytest import hookspec + for name, value in vars(hookspec).items(): if name.startswith("pytest_"): assert value.__doc__, "no docstring for %s" % name def test_initialization_error_issue49(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_configure(): x - """) + """ + ) result = testdir.runpytest() assert result.ret == 3 # internal error - result.stderr.fnmatch_lines([ - "INTERNAL*pytest_configure*", - "INTERNAL*x*", - ]) - assert 'sessionstarttime' not in result.stderr.str() + result.stderr.fnmatch_lines(["INTERNAL*pytest_configure*", "INTERNAL*x*"]) + assert "sessionstarttime" not in result.stderr.str() - @pytest.mark.parametrize('lookfor', ['test_fun.py::test_a']) + @pytest.mark.parametrize("lookfor", ["test_fun.py::test_a"]) def test_issue134_report_error_when_collecting_member(self, testdir, lookfor): - testdir.makepyfile(test_fun=""" + testdir.makepyfile( + test_fun=""" def test_a(): pass - def""") + def""" + ) result = testdir.runpytest(lookfor) - result.stdout.fnmatch_lines(['*SyntaxError*']) - if '::' in lookfor: - result.stderr.fnmatch_lines([ - '*ERROR*', - ]) + result.stdout.fnmatch_lines(["*SyntaxError*"]) + if "::" in lookfor: + result.stderr.fnmatch_lines(["*ERROR*"]) assert result.ret == 4 # usage error only if item not found def test_report_all_failed_collections_initargs(self, testdir): testdir.makepyfile(test_a="def", test_b="def") result = testdir.runpytest("test_a.py::a", "test_b.py::b") - result.stderr.fnmatch_lines([ - "*ERROR*test_a.py::a*", - "*ERROR*test_b.py::b*", - ]) + result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"]) - @pytest.mark.usefixtures('recwarn') + @pytest.mark.usefixtures("recwarn") def test_namespace_import_doesnt_confuse_import_hook(self, testdir): """ Ref #383. Python 3.3's namespace package messed with our import hooks Importing a module that didn't exist, even if the ImportError was gracefully handled, would make our test crash. - Use recwarn here to silence this warning in Python 2.6 and 2.7: + Use recwarn here to silence this warning in Python 2.7: ImportWarning: Not importing directory '...\not_a_package': missing __init__.py """ - testdir.mkdir('not_a_package') - p = testdir.makepyfile(""" + testdir.mkdir("not_a_package") + p = testdir.makepyfile( + """ try: from not_a_package import doesnt_exist except ImportError: @@ -357,19 +381,23 @@ class TestGeneralUsage(object): def test_whatever(): pass - """) + """ + ) res = testdir.runpytest(p.basename) assert res.ret == 0 def test_unknown_option(self, testdir): result = testdir.runpytest("--qwlkej") - result.stderr.fnmatch_lines(""" + result.stderr.fnmatch_lines( + """ *unrecognized* - """) + """ + ) def test_getsourcelines_error_issue553(self, testdir, monkeypatch): monkeypatch.setattr("inspect.getsourcelines", None) - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def raise_error(obj): raise IOError('source code not available') @@ -378,12 +406,12 @@ class TestGeneralUsage(object): def test_foo(invalid_fixture): pass - """) + """ + ) res = testdir.runpytest(p) - res.stdout.fnmatch_lines([ - "*source code not available*", - "E*fixture 'invalid_fixture' not found", - ]) + res.stdout.fnmatch_lines( + ["*source code not available*", "E*fixture 'invalid_fixture' not found"] + ) def test_plugins_given_as_strings(self, tmpdir, monkeypatch): """test that str values passed to main() as `plugins` arg @@ -391,62 +419,67 @@ class TestGeneralUsage(object): #855. """ with pytest.raises(ImportError) as excinfo: - pytest.main([str(tmpdir)], plugins=['invalid.module']) - assert 'invalid' in str(excinfo.value) + pytest.main([str(tmpdir)], plugins=["invalid.module"]) + assert "invalid" in str(excinfo.value) - p = tmpdir.join('test_test_plugins_given_as_strings.py') - p.write('def test_foo(): pass') - mod = py.std.types.ModuleType("myplugin") - monkeypatch.setitem(sys.modules, 'myplugin', mod) - assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0 + p = tmpdir.join("test_test_plugins_given_as_strings.py") + p.write("def test_foo(): pass") + mod = types.ModuleType("myplugin") + monkeypatch.setitem(sys.modules, "myplugin", mod) + assert pytest.main(args=[str(tmpdir)], plugins=["myplugin"]) == 0 def test_parametrized_with_bytes_regex(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import re import pytest @pytest.mark.parametrize('r', [re.compile(b'foo')]) def test_stuff(r): pass """ - ) + ) res = testdir.runpytest(p) - res.stdout.fnmatch_lines([ - '*1 passed*' - ]) + res.stdout.fnmatch_lines(["*1 passed*"]) def test_parametrized_with_null_bytes(self, testdir): - """Test parametrization with values that contain null bytes and unicode characters (#2644)""" - p = testdir.makepyfile(u""" + """Test parametrization with values that contain null bytes and unicode characters (#2644, #2957)""" + p = testdir.makepyfile( + u""" # encoding: UTF-8 import pytest - @pytest.mark.parametrize("data", ["\\x00", u'ação']) + @pytest.mark.parametrize("data", [b"\\x00", "\\x00", u'ação']) def test_foo(data): assert data - """) + """ + ) res = testdir.runpytest(p) - res.assert_outcomes(passed=2) + res.assert_outcomes(passed=3) class TestInvocationVariants(object): + def test_earlyinit(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest assert hasattr(pytest, 'mark') - """) + """ + ) result = testdir.runpython(p) assert result.ret == 0 @pytest.mark.xfail("sys.platform.startswith('java')") def test_pydoc(self, testdir): - for name in ('py.test', 'pytest'): + for name in ("py.test", "pytest"): result = testdir.runpython_c("import %s;help(%s)" % (name, name)) assert result.ret == 0 s = result.stdout.str() - assert 'MarkGenerator' in s + assert "MarkGenerator" in s def test_import_star_py_dot_test(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from py.test import * #collect #cmdline @@ -456,51 +489,55 @@ class TestInvocationVariants(object): main skip xfail - """) + """ + ) result = testdir.runpython(p) assert result.ret == 0 def test_import_star_pytest(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from pytest import * #Item #File main skip xfail - """) + """ + ) result = testdir.runpython(p) assert result.ret == 0 def test_double_pytestcmdline(self, testdir): - p = testdir.makepyfile(run=""" + p = testdir.makepyfile( + run=""" import pytest pytest.main() pytest.main() - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_hello(): pass - """) + """ + ) result = testdir.runpython(p) - result.stdout.fnmatch_lines([ - "*1 passed*", - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"]) def test_python_minus_m_invocation_ok(self, testdir): p1 = testdir.makepyfile("def test_hello(): pass") - res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1)) + res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 0 def test_python_minus_m_invocation_fail(self, testdir): p1 = testdir.makepyfile("def test_fail(): 0/0") - res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1)) + res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 1 def test_python_pytest_package(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") - res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1)) + res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 0 res.stdout.fnmatch_lines(["*1 passed*"]) @@ -520,7 +557,9 @@ class TestInvocationVariants(object): out, err = capsys.readouterr() def test_invoke_plugin_api(self, testdir, capsys): + class MyPlugin(object): + def pytest_addoption(self, parser): parser.addoption("--myopt") @@ -529,63 +568,55 @@ class TestInvocationVariants(object): assert "--myopt" in out def test_pyargs_importerror(self, testdir, monkeypatch): - monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False) + monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) path = testdir.mkpydir("tpkg") - path.join("test_hello.py").write('raise ImportError') + path.join("test_hello.py").write("raise ImportError") - result = testdir.runpytest_subprocess("--pyargs", "tpkg.test_hello") + result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) assert result.ret != 0 - result.stdout.fnmatch_lines([ - "collected*0*items*/*1*errors" - ]) + result.stdout.fnmatch_lines(["collected*0*items*/*1*errors"]) def test_cmdline_python_package(self, testdir, monkeypatch): import warnings - monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False) + + monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) path = testdir.mkpydir("tpkg") path.join("test_hello.py").write("def test_hello(): pass") path.join("test_world.py").write("def test_world(): pass") result = testdir.runpytest("--pyargs", "tpkg") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*2 passed*" - ]) - result = testdir.runpytest("--pyargs", "tpkg.test_hello") + result.stdout.fnmatch_lines(["*2 passed*"]) + result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def join_pythonpath(what): - cur = py.std.os.environ.get('PYTHONPATH') + cur = os.environ.get("PYTHONPATH") if cur: return str(what) + os.pathsep + cur return what + empty_package = testdir.mkpydir("empty_package") - monkeypatch.setenv('PYTHONPATH', join_pythonpath(empty_package)) + monkeypatch.setenv("PYTHONPATH", join_pythonpath(empty_package)) # the path which is not a package raises a warning on pypy; # no idea why only pypy and not normal python warn about it here with warnings.catch_warnings(): - warnings.simplefilter('ignore', ImportWarning) + warnings.simplefilter("ignore", ImportWarning) result = testdir.runpytest("--pyargs", ".") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*2 passed*" - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) - monkeypatch.setenv('PYTHONPATH', join_pythonpath(testdir)) - result = testdir.runpytest("--pyargs", "tpkg.test_missing") + monkeypatch.setenv("PYTHONPATH", join_pythonpath(testdir)) + result = testdir.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True) assert result.ret != 0 - result.stderr.fnmatch_lines([ - "*not*found*test_missing*", - ]) + result.stderr.fnmatch_lines(["*not*found*test_missing*"]) def test_cmdline_python_namespace_package(self, testdir, monkeypatch): """ test --pyargs option with namespace packages (#1567) """ - monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', raising=False) + monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) search_path = [] for dirname in "hello", "world": @@ -593,21 +624,22 @@ class TestInvocationVariants(object): search_path.append(d) ns = d.mkdir("ns_pkg") ns.join("__init__.py").write( - "__import__('pkg_resources').declare_namespace(__name__)") + "__import__('pkg_resources').declare_namespace(__name__)" + ) lib = ns.mkdir(dirname) lib.ensure("__init__.py") - lib.join("test_{0}.py".format(dirname)). \ - write("def test_{0}(): pass\n" - "def test_other():pass".format(dirname)) + lib.join("test_{}.py".format(dirname)).write( + "def test_{}(): pass\n" "def test_other():pass".format(dirname) + ) # The structure of the test directory is now: # . # ├── hello - # │   └── ns_pkg - # │   ├── __init__.py - # │   └── hello - # │   ├── __init__.py - # │   └── test_hello.py + # │ └── ns_pkg + # │ ├── __init__.py + # │ └── hello + # │ ├── __init__.py + # │ └── test_hello.py # └── world # └── ns_pkg # ├── __init__.py @@ -616,45 +648,122 @@ class TestInvocationVariants(object): # └── test_world.py def join_pythonpath(*dirs): - cur = py.std.os.environ.get('PYTHONPATH') + cur = os.environ.get("PYTHONPATH") if cur: dirs += (cur,) return os.pathsep.join(str(p) for p in dirs) - monkeypatch.setenv('PYTHONPATH', join_pythonpath(*search_path)) + + monkeypatch.setenv("PYTHONPATH", join_pythonpath(*search_path)) for p in search_path: monkeypatch.syspath_prepend(p) - os.chdir('world') # mixed module and filenames: + os.chdir("world") result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world") - testdir.chdir() assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*test_hello.py::test_hello*PASSED", - "*test_hello.py::test_other*PASSED", - "*test_world.py::test_world*PASSED", - "*test_world.py::test_other*PASSED", - "*4 passed*" - ]) + result.stdout.fnmatch_lines( + [ + "*test_hello.py::test_hello*PASSED*", + "*test_hello.py::test_other*PASSED*", + "*test_world.py::test_world*PASSED*", + "*test_world.py::test_other*PASSED*", + "*4 passed*", + ] + ) # specify tests within a module - result = testdir.runpytest("--pyargs", "-v", "ns_pkg.world.test_world::test_other") + testdir.chdir() + result = testdir.runpytest( + "--pyargs", "-v", "ns_pkg.world.test_world::test_other" + ) assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*test_world.py::test_other*PASSED", - "*1 passed*" - ]) + result.stdout.fnmatch_lines( + ["*test_world.py::test_other*PASSED*", "*1 passed*"] + ) + + @pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks") + def test_cmdline_python_package_symlink(self, testdir, monkeypatch): + """ + test --pyargs option with packages with path containing symlink can + have conftest.py in their package (#2985) + """ + # dummy check that we can actually create symlinks: on Windows `os.symlink` is available, + # but normal users require special admin privileges to create symlinks. + if sys.platform == "win32": + try: + os.symlink( + str(testdir.tmpdir.ensure("tmpfile")), + str(testdir.tmpdir.join("tmpfile2")), + ) + except OSError as e: + pytest.skip(six.text_type(e.args[0])) + monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) + + search_path = ["lib", os.path.join("local", "lib")] + + dirname = "lib" + d = testdir.mkdir(dirname) + foo = d.mkdir("foo") + foo.ensure("__init__.py") + lib = foo.mkdir("bar") + lib.ensure("__init__.py") + lib.join("test_bar.py").write( + "def test_bar(): pass\n" "def test_other(a_fixture):pass" + ) + lib.join("conftest.py").write( + "import pytest\n" "@pytest.fixture\n" "def a_fixture():pass" + ) + + d_local = testdir.mkdir("local") + symlink_location = os.path.join(str(d_local), "lib") + if six.PY2: + os.symlink(str(d), symlink_location) + else: + os.symlink(str(d), symlink_location, target_is_directory=True) + + # The structure of the test directory is now: + # . + # ├── local + # │ └── lib -> ../lib + # └── lib + # └── foo + # ├── __init__.py + # └── bar + # ├── __init__.py + # ├── conftest.py + # └── test_bar.py + + def join_pythonpath(*dirs): + cur = os.getenv("PYTHONPATH") + if cur: + dirs += (cur,) + return os.pathsep.join(str(p) for p in dirs) + + monkeypatch.setenv("PYTHONPATH", join_pythonpath(*search_path)) + for p in search_path: + monkeypatch.syspath_prepend(p) + + # module picked up in symlink-ed directory: + result = testdir.runpytest("--pyargs", "-v", "foo.bar") + testdir.chdir() + assert result.ret == 0 + result.stdout.fnmatch_lines( + [ + "*lib/foo/bar/test_bar.py::test_bar*PASSED*", + "*lib/foo/bar/test_bar.py::test_other*PASSED*", + "*2 passed*", + ] + ) def test_cmdline_python_package_not_exists(self, testdir): result = testdir.runpytest("--pyargs", "tpkgwhatv") assert result.ret - result.stderr.fnmatch_lines([ - "ERROR*file*or*package*not*found*", - ]) + result.stderr.fnmatch_lines(["ERROR*file*or*package*not*found*"]) @pytest.mark.xfail(reason="decide: feature or bug") def test_noclass_discovery_if_not_testcase(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class TestHello(object): def test_hello(self): @@ -662,36 +771,40 @@ class TestInvocationVariants(object): class RealTest(unittest.TestCase, TestHello): attr = 42 - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=1) def test_doctest_id(self, testdir): - testdir.makefile('.txt', """ + testdir.makefile( + ".txt", + """ >>> x=3 >>> x 4 - """) + """, + ) result = testdir.runpytest("-rf") lines = result.stdout.str().splitlines() for line in lines: if line.startswith("FAIL "): testid = line[5:].strip() break - result = testdir.runpytest(testid, '-rf') - result.stdout.fnmatch_lines([ - line, - "*1 failed*", - ]) + result = testdir.runpytest(testid, "-rf") + result.stdout.fnmatch_lines([line, "*1 failed*"]) def test_core_backward_compatibility(self): """Test backward compatibility for get_plugin_manager function. See #787.""" import _pytest.config - assert type(_pytest.config.get_plugin_manager()) is _pytest.config.PytestPluginManager + + assert type( + _pytest.config.get_plugin_manager() + ) is _pytest.config.PytestPluginManager def test_has_plugin(self, request): """Test hasplugin function of the plugin manager (#932).""" - assert request.config.pluginmanager.hasplugin('python') + assert request.config.pluginmanager.hasplugin("python") class TestDurations(object): @@ -712,12 +825,9 @@ class TestDurations(object): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=10") assert result.ret == 0 - result.stdout.fnmatch_lines_random([ - "*durations*", - "*call*test_3*", - "*call*test_2*", - "*call*test_1*", - ]) + result.stdout.fnmatch_lines_random( + ["*durations*", "*call*test_3*", "*call*test_2*", "*call*test_1*"] + ) def test_calls_show_2(self, testdir): testdir.makepyfile(self.source) @@ -731,7 +841,7 @@ class TestDurations(object): result = testdir.runpytest("--durations=0") assert result.ret == 0 for x in "123": - for y in 'call', : # 'setup', 'call', 'teardown': + for y in ("call",): # 'setup', 'call', 'teardown': for line in result.stdout.lines: if ("test_%s" % x) in line and y in line: break @@ -742,19 +852,14 @@ class TestDurations(object): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=2", "-k test_1") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*durations*", - "*call*test_1*", - ]) + result.stdout.fnmatch_lines(["*durations*", "*call*test_1*"]) def test_with_failing_collection(self, testdir): testdir.makepyfile(self.source) testdir.makepyfile(test_collecterror="""xyz""") result = testdir.runpytest("--durations=2", "-k test_1") assert result.ret == 2 - result.stdout.fnmatch_lines([ - "*Interrupted: 1 errors during collection*", - ]) + result.stdout.fnmatch_lines(["*Interrupted: 1 errors during collection*"]) # Collection errors abort test execution, therefore no duration is # output assert "duration" not in result.stdout.str() @@ -782,42 +887,48 @@ class TestDurationWithFixture(object): result = testdir.runpytest("--durations=10") assert result.ret == 0 - result.stdout.fnmatch_lines_random(""" + result.stdout.fnmatch_lines_random( + """ *durations* * setup *test_1* * call *test_1* - """) + """ + ) def test_zipimport_hook(testdir, tmpdir): """Test package loader is being used correctly (see #1837).""" - zipapp = pytest.importorskip('zipapp') - testdir.tmpdir.join('app').ensure(dir=1) - testdir.makepyfile(**{ - 'app/foo.py': """ + zipapp = pytest.importorskip("zipapp") + testdir.tmpdir.join("app").ensure(dir=1) + testdir.makepyfile( + **{ + "app/foo.py": """ import pytest def main(): pytest.main(['--pyarg', 'foo']) - """, - }) - target = tmpdir.join('foo.zip') - zipapp.create_archive(str(testdir.tmpdir.join('app')), str(target), main='foo:main') + """ + } + ) + target = tmpdir.join("foo.zip") + zipapp.create_archive(str(testdir.tmpdir.join("app")), str(target), main="foo:main") result = testdir.runpython(target) assert result.ret == 0 - result.stderr.fnmatch_lines(['*not found*foo*']) - assert 'INTERNALERROR>' not in result.stdout.str() + result.stderr.fnmatch_lines(["*not found*foo*"]) + assert "INTERNALERROR>" not in result.stdout.str() def test_import_plugin_unicode_name(testdir): + testdir.makepyfile(myplugin="") testdir.makepyfile( - myplugin='', - ) - testdir.makepyfile(""" + """ def test(): pass - """) - testdir.makeconftest(""" + """ + ) + testdir.makeconftest( + """ pytest_plugins = [u'myplugin'] - """) + """ + ) r = testdir.runpytest() assert r.ret == 0 @@ -827,24 +938,129 @@ def test_deferred_hook_checking(testdir): Check hooks as late as possible (#1821). """ testdir.syspathinsert() - testdir.makepyfile(**{ - 'plugin.py': """ - class Hooks: + testdir.makepyfile( + **{ + "plugin.py": """ + class Hooks(object): def pytest_my_hook(self, config): pass def pytest_configure(config): config.pluginmanager.add_hookspecs(Hooks) """, - 'conftest.py': """ + "conftest.py": """ pytest_plugins = ['plugin'] def pytest_my_hook(config): return 40 """, - 'test_foo.py': """ + "test_foo.py": """ def test(request): assert request.config.hook.pytest_my_hook(config=request.config) == [40] - """ - }) + """, + } + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 1 passed *']) + result.stdout.fnmatch_lines(["* 1 passed *"]) + + +def test_fixture_values_leak(testdir): + """Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected + life-times (#2981). + """ + testdir.makepyfile( + """ + import attr + import gc + import pytest + import weakref + + @attr.s + class SomeObj(object): + name = attr.ib() + + fix_of_test1_ref = None + session_ref = None + + @pytest.fixture(scope='session') + def session_fix(): + global session_ref + obj = SomeObj(name='session-fixture') + session_ref = weakref.ref(obj) + return obj + + @pytest.fixture + def fix(session_fix): + global fix_of_test1_ref + obj = SomeObj(name='local-fixture') + fix_of_test1_ref = weakref.ref(obj) + return obj + + def test1(fix): + assert fix_of_test1_ref() is fix + + def test2(): + gc.collect() + # fixture "fix" created during test1 must have been destroyed by now + assert fix_of_test1_ref() is None + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["* 2 passed *"]) + + +def test_fixture_order_respects_scope(testdir): + """Ensure that fixtures are created according to scope order, regression test for #2405 + """ + testdir.makepyfile( + """ + import pytest + + data = {} + + @pytest.fixture(scope='module') + def clean_data(): + data.clear() + + @pytest.fixture(autouse=True) + def add_data(): + data.update(value=True) + + @pytest.mark.usefixtures('clean_data') + def test_value(): + assert data.get('value') + """ + ) + result = testdir.runpytest() + assert result.ret == 0 + + +def test_frame_leak_on_failing_test(testdir): + """pytest would leak garbage referencing the frames of tests that failed that could never be reclaimed (#2798) + + Unfortunately it was not possible to remove the actual circles because most of them + are made of traceback objects which cannot be weakly referenced. Those objects at least + can be eventually claimed by the garbage collector. + """ + testdir.makepyfile( + """ + import gc + import weakref + + class Obj: + pass + + ref = None + + def test1(): + obj = Obj() + global ref + ref = weakref.ref(obj) + assert 0 + + def test2(): + gc.collect() + assert ref() is None + """ + ) + result = testdir.runpytest_subprocess() + result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"]) diff --git a/third_party/python/pytest/testing/code/test_code.py b/third_party/python/pytest/testing/code/test_code.py index 209a8ef19a02..bfae3691882e 100644 --- a/third_party/python/pytest/testing/code/test_code.py +++ b/third_party/python/pytest/testing/code/test_code.py @@ -6,18 +6,19 @@ import _pytest._code import py import pytest from test_excinfo import TWMock +from six import text_type def test_ne(): - code1 = _pytest._code.Code(compile('foo = "bar"', '', 'exec')) + code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec")) assert code1 == code1 - code2 = _pytest._code.Code(compile('foo = "baz"', '', 'exec')) + code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec")) assert code2 != code1 def test_code_gives_back_name_for_not_existing_file(): - name = 'abc-123' - co_code = compile("pass\n", name, 'exec') + name = "abc-123" + co_code = compile("pass\n", name, "exec") assert co_code.co_filename == name code = _pytest._code.Code(co_code) assert str(code.path) == name @@ -25,12 +26,15 @@ def test_code_gives_back_name_for_not_existing_file(): def test_code_with_class(): + class A(object): pass + pytest.raises(TypeError, "_pytest._code.Code(A)") if True: + def x(): pass @@ -38,7 +42,7 @@ if True: def test_code_fullsource(): code = _pytest._code.Code(x) full = code.fullsource - assert 'test_code_fullsource()' in str(full) + assert "test_code_fullsource()" in str(full) def test_code_source(): @@ -50,8 +54,10 @@ def test_code_source(): def test_frame_getsourcelineno_myself(): + def func(): return sys._getframe(0) + f = func() f = _pytest._code.Frame(f) source, lineno = f.code.fullsource, f.lineno @@ -59,8 +65,10 @@ def test_frame_getsourcelineno_myself(): def test_getstatement_empty_fullsource(): + def func(): return sys._getframe(0) + f = func() f = _pytest._code.Frame(f) prop = f.code.__class__.fullsource @@ -78,7 +86,7 @@ def test_code_from_func(): def test_unicode_handling(): - value = py.builtin._totext('\xc4\x85\xc4\x87\n', 'utf-8').encode('utf8') + value = py.builtin._totext("\xc4\x85\xc4\x87\n", "utf-8").encode("utf8") def f(): raise Exception(value) @@ -86,65 +94,74 @@ def test_unicode_handling(): excinfo = pytest.raises(Exception, f) str(excinfo) if sys.version_info[0] < 3: - unicode(excinfo) + text_type(excinfo) -@pytest.mark.skipif(sys.version_info[0] >= 3, reason='python 2 only issue') +@pytest.mark.skipif(sys.version_info[0] >= 3, reason="python 2 only issue") def test_unicode_handling_syntax_error(): - value = py.builtin._totext('\xc4\x85\xc4\x87\n', 'utf-8').encode('utf8') + value = py.builtin._totext("\xc4\x85\xc4\x87\n", "utf-8").encode("utf8") def f(): - raise SyntaxError('invalid syntax', (None, 1, 3, value)) + raise SyntaxError("invalid syntax", (None, 1, 3, value)) excinfo = pytest.raises(Exception, f) str(excinfo) if sys.version_info[0] < 3: - unicode(excinfo) + text_type(excinfo) def test_code_getargs(): + def f1(x): pass + c1 = _pytest._code.Code(f1) - assert c1.getargs(var=True) == ('x',) + assert c1.getargs(var=True) == ("x",) def f2(x, *y): pass + c2 = _pytest._code.Code(f2) - assert c2.getargs(var=True) == ('x', 'y') + assert c2.getargs(var=True) == ("x", "y") def f3(x, **z): pass + c3 = _pytest._code.Code(f3) - assert c3.getargs(var=True) == ('x', 'z') + assert c3.getargs(var=True) == ("x", "z") def f4(x, *y, **z): pass + c4 = _pytest._code.Code(f4) - assert c4.getargs(var=True) == ('x', 'y', 'z') + assert c4.getargs(var=True) == ("x", "y", "z") def test_frame_getargs(): + def f1(x): return sys._getframe(0) - fr1 = _pytest._code.Frame(f1('a')) - assert fr1.getargs(var=True) == [('x', 'a')] + + fr1 = _pytest._code.Frame(f1("a")) + assert fr1.getargs(var=True) == [("x", "a")] def f2(x, *y): return sys._getframe(0) - fr2 = _pytest._code.Frame(f2('a', 'b', 'c')) - assert fr2.getargs(var=True) == [('x', 'a'), ('y', ('b', 'c'))] + + fr2 = _pytest._code.Frame(f2("a", "b", "c")) + assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))] def f3(x, **z): return sys._getframe(0) - fr3 = _pytest._code.Frame(f3('a', b='c')) - assert fr3.getargs(var=True) == [('x', 'a'), ('z', {'b': 'c'})] + + fr3 = _pytest._code.Frame(f3("a", b="c")) + assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})] def f4(x, *y, **z): return sys._getframe(0) - fr4 = _pytest._code.Frame(f4('a', 'b', c='d')) - assert fr4.getargs(var=True) == [('x', 'a'), ('y', ('b',)), - ('z', {'c': 'd'})] + + fr4 = _pytest._code.Frame(f4("a", "b", c="d")) + assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})] class TestExceptionInfo(object): @@ -173,7 +190,7 @@ class TestTracebackEntry(object): entry = exci.traceback[0] source = entry.getsource() assert len(source) == 6 - assert 'assert False' in source[5] + assert "assert False" in source[5] class TestReprFuncArgs(object): @@ -183,14 +200,11 @@ class TestReprFuncArgs(object): tw = TWMock() - args = [ - ('unicode_string', u"São Paulo"), - ('utf8_string', 'S\xc3\xa3o Paulo'), - ] + args = [("unicode_string", u"São Paulo"), ("utf8_string", "S\xc3\xa3o Paulo")] r = ReprFuncArgs(args) r.toterminal(tw) if sys.version_info[0] >= 3: - assert tw.lines[0] == 'unicode_string = São Paulo, utf8_string = São Paulo' + assert tw.lines[0] == "unicode_string = São Paulo, utf8_string = São Paulo" else: - assert tw.lines[0] == 'unicode_string = São Paulo, utf8_string = São Paulo' + assert tw.lines[0] == "unicode_string = São Paulo, utf8_string = São Paulo" diff --git a/third_party/python/pytest/testing/code/test_excinfo.py b/third_party/python/pytest/testing/code/test_excinfo.py index 263d053b5ef6..f4044b6ecc44 100644 --- a/third_party/python/pytest/testing/code/test_excinfo.py +++ b/third_party/python/pytest/testing/code/test_excinfo.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function -import sys import operator +import os +import sys import _pytest import py import pytest @@ -10,7 +11,9 @@ from _pytest._code.code import ( ExceptionInfo, FormattedExcinfo, ReprExceptionInfo, - ExceptionChainRepr) + ExceptionChainRepr, +) +from six.moves import queue from test_source import astonly @@ -21,8 +24,6 @@ except ImportError: else: invalidate_import_caches = getattr(importlib, "invalidate_caches", None) -queue = py.builtin._tryimport('queue', 'Queue') - failsonjython = pytest.mark.xfail("sys.platform.startswith('java')") pytest_version_info = tuple(map(int, pytest.__version__.split(".")[:3])) @@ -64,6 +65,7 @@ def test_excinfo_simple(): def test_excinfo_getstatement(): + def g(): raise ValueError @@ -74,9 +76,11 @@ def test_excinfo_getstatement(): f() except ValueError: excinfo = _pytest._code.ExceptionInfo() - linenumbers = [_pytest._code.getrawcode(f).co_firstlineno - 1 + 4, - _pytest._code.getrawcode(f).co_firstlineno - 1 + 1, - _pytest._code.getrawcode(g).co_firstlineno - 1 + 1, ] + linenumbers = [ + _pytest._code.getrawcode(f).co_firstlineno - 1 + 4, + _pytest._code.getrawcode(f).co_firstlineno - 1 + 1, + _pytest._code.getrawcode(g).co_firstlineno - 1 + 1, + ] values = list(excinfo.traceback) foundlinenumbers = [x.lineno for x in values] assert foundlinenumbers == linenumbers @@ -84,6 +88,7 @@ def test_excinfo_getstatement(): # print "%s:%d %s" %(x.path.relto(root), x.lineno, x.statement) # xxx + # testchain for getentries test below @@ -107,6 +112,7 @@ def h(): class TestTraceback_f_g_h(object): + def setup_method(self, method): try: h() @@ -118,7 +124,7 @@ class TestTraceback_f_g_h(object): entries = list(tb) assert len(tb) == 4 # maybe fragile test assert len(entries) == 4 # maybe fragile test - names = ['f', 'g', 'h'] + names = ["f", "g", "h"] for entry in entries: try: names.remove(entry.frame.code.name) @@ -135,14 +141,16 @@ class TestTraceback_f_g_h(object): @astonly @failsonjython def test_traceback_entry_getsource_in_construct(self): - source = _pytest._code.Source("""\ + source = _pytest._code.Source( + """\ def xyz(): try: raise ValueError except somenoname: pass xyz() - """) + """ + ) try: exec(source.compile()) except NameError: @@ -167,7 +175,7 @@ class TestTraceback_f_g_h(object): basedir = py.path.local(pytest.__file__).dirpath() newtraceback = excinfo.traceback.cut(excludepath=basedir) for x in newtraceback: - if hasattr(x, 'path'): + if hasattr(x, "path"): assert not py.path.local(x.path).relto(basedir) assert newtraceback[-1].frame.code.path == p @@ -176,13 +184,17 @@ class TestTraceback_f_g_h(object): ntraceback = traceback.filter() assert len(ntraceback) == len(traceback) - 1 - @pytest.mark.parametrize('tracebackhide, matching', [ - (lambda info: True, True), - (lambda info: False, False), - (operator.methodcaller('errisinstance', ValueError), True), - (operator.methodcaller('errisinstance', IndexError), False), - ]) + @pytest.mark.parametrize( + "tracebackhide, matching", + [ + (lambda info: True, True), + (lambda info: False, False), + (operator.methodcaller("errisinstance", ValueError), True), + (operator.methodcaller("errisinstance", IndexError), False), + ], + ) def test_traceback_filter_selective(self, tracebackhide, matching): + def f(): # raise ValueError @@ -202,8 +214,8 @@ class TestTraceback_f_g_h(object): excinfo = pytest.raises(ValueError, h) traceback = excinfo.traceback ntraceback = traceback.filter() - print('old: {0!r}'.format(traceback)) - print('new: {0!r}'.format(ntraceback)) + print("old: {!r}".format(traceback)) + print("new: {!r}".format(ntraceback)) if matching: assert len(ntraceback) == len(traceback) - 2 @@ -212,16 +224,19 @@ class TestTraceback_f_g_h(object): assert len(ntraceback) == len(traceback) - 1 def test_traceback_recursion_index(self): + def f(n): if n < 10: n += 1 f(n) + excinfo = pytest.raises(RuntimeError, f, 8) traceback = excinfo.traceback recindex = traceback.recursionindex() assert recindex == 3 def test_traceback_only_specific_recursion_errors(self, monkeypatch): + def f(n): if n == 0: raise RuntimeError("hello") @@ -233,11 +248,13 @@ class TestTraceback_f_g_h(object): assert "RuntimeError: hello" in str(repr.reprcrash) def test_traceback_no_recursion_index(self): + def do_stuff(): raise RuntimeError def reraise_me(): import sys + exc, val, tb = sys.exc_info() py.builtin._reraise(exc, val, tb) @@ -254,15 +271,16 @@ class TestTraceback_f_g_h(object): def test_traceback_messy_recursion(self): # XXX: simplified locally testable version - decorator = pytest.importorskip('decorator').decorator + decorator = pytest.importorskip("decorator").decorator def log(f, *k, **kw): - print('%s %s' % (k, kw)) + print("%s %s" % (k, kw)) f(*k, **kw) + log = decorator(log) def fail(): - raise ValueError('') + raise ValueError("") fail = log(log(fail)) @@ -270,6 +288,7 @@ class TestTraceback_f_g_h(object): assert excinfo.traceback.recursionindex() is None def test_traceback_getcrashentry(self): + def i(): __tracebackhide__ = True raise ValueError @@ -290,9 +309,10 @@ class TestTraceback_f_g_h(object): co = _pytest._code.Code(h) assert entry.frame.code.path == co.path assert entry.lineno == co.firstlineno + 1 - assert entry.frame.code.name == 'h' + assert entry.frame.code.name == "h" def test_traceback_getcrashentry_empty(self): + def g(): __tracebackhide__ = True raise ValueError @@ -307,16 +327,15 @@ class TestTraceback_f_g_h(object): co = _pytest._code.Code(g) assert entry.frame.code.path == co.path assert entry.lineno == co.firstlineno + 2 - assert entry.frame.code.name == 'g' + assert entry.frame.code.name == "g" def test_excinfo_exconly(): excinfo = pytest.raises(ValueError, h) - assert excinfo.exconly().startswith('ValueError') - excinfo = pytest.raises(ValueError, - "raise ValueError('hello\\nworld')") + assert excinfo.exconly().startswith("ValueError") + excinfo = pytest.raises(ValueError, "raise ValueError('hello\\nworld')") msg = excinfo.exconly(tryshort=True) - assert msg.startswith('ValueError') + assert msg.startswith("ValueError") assert msg.endswith("world") @@ -345,27 +364,23 @@ def test_excinfo_no_sourcecode(): except ValueError: excinfo = _pytest._code.ExceptionInfo() s = str(excinfo.traceback[-1]) - if py.std.sys.version_info < (2, 5): - assert s == " File '':1 in ?\n ???\n" - else: - assert s == " File '':1 in \n ???\n" + assert s == " File '':1 in \n ???\n" def test_excinfo_no_python_sourcecode(tmpdir): # XXX: simplified locally testable version - tmpdir.join('test.txt').write("{{ h()}}:") + tmpdir.join("test.txt").write("{{ h()}}:") - jinja2 = pytest.importorskip('jinja2') + jinja2 = pytest.importorskip("jinja2") loader = jinja2.FileSystemLoader(str(tmpdir)) env = jinja2.Environment(loader=loader) - template = env.get_template('test.txt') - excinfo = pytest.raises(ValueError, - template.render, h=h) + template = env.get_template("test.txt") + excinfo = pytest.raises(ValueError, template.render, h=h) for item in excinfo.traceback: print(item) # XXX: for some reason jinja.Template.render is printed in full item.source # shouldnt fail - if item.path.basename == 'test.txt': - assert str(item.source) == '{{ h()}}:' + if item.path.basename == "test.txt": + assert str(item.source) == "{{ h()}}:" def test_entrysource_Queue_example(): @@ -395,28 +410,29 @@ def test_codepath_Queue_example(): def test_match_succeeds(): with pytest.raises(ZeroDivisionError) as excinfo: 0 // 0 - excinfo.match(r'.*zero.*') + excinfo.match(r".*zero.*") def test_match_raises_error(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_division_zero(): with pytest.raises(ZeroDivisionError) as excinfo: 0 / 0 excinfo.match(r'[123]+') - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*AssertionError*Pattern*[123]*not found*", - ]) + result.stdout.fnmatch_lines(["*AssertionError*Pattern*[123]*not found*"]) class TestFormattedExcinfo(object): @pytest.fixture def importasmod(self, request): + def importasmod(source): source = _pytest._code.Source(source) tmpdir = request.getfixturevalue("tmpdir") @@ -426,6 +442,7 @@ class TestFormattedExcinfo(object): if invalidate_import_caches is not None: invalidate_import_caches() return modpath.pyimport() + return importasmod def excinfo_from_exec(self, source): @@ -440,10 +457,12 @@ class TestFormattedExcinfo(object): def test_repr_source(self): pr = FormattedExcinfo() - source = _pytest._code.Source(""" + source = _pytest._code.Source( + """ def f(x): pass - """).strip() + """ + ).strip() pr.flow_marker = "|" lines = pr.get_source(source, 0) assert len(lines) == 2 @@ -453,19 +472,17 @@ class TestFormattedExcinfo(object): def test_repr_source_excinfo(self): """ check if indentation is right """ pr = FormattedExcinfo() - excinfo = self.excinfo_from_exec(""" + excinfo = self.excinfo_from_exec( + """ def f(): assert 0 f() - """) + """ + ) pr = FormattedExcinfo() source = pr._getentrysource(excinfo.traceback[-1]) lines = pr.get_source(source, 1, excinfo) - assert lines == [ - ' def f():', - '> assert 0', - 'E AssertionError' - ] + assert lines == [" def f():", "> assert 0", "E AssertionError"] def test_repr_source_not_existing(self): pr = FormattedExcinfo() @@ -476,32 +493,37 @@ class TestFormattedExcinfo(object): excinfo = _pytest._code.ExceptionInfo() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" def test_repr_many_line_source_not_existing(self): pr = FormattedExcinfo() - co = compile(""" + co = compile( + """ a = 1 raise ValueError() -""", "", "exec") +""", + "", + "exec", + ) try: exec(co) except ValueError: excinfo = _pytest._code.ExceptionInfo() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" def test_repr_source_failing_fullsource(self): pr = FormattedExcinfo() class FakeCode(object): - class raw(object): - co_filename = '?' - path = '?' + class raw(object): + co_filename = "?" + + path = "?" firstlineno = 5 def fullsource(self): @@ -515,6 +537,7 @@ raise ValueError() f_globals = {} class FakeTracebackEntry(_pytest._code.Traceback.Entry): + def __init__(self, tb, excinfo=None): self.lineno = 5 + 3 @@ -546,33 +569,35 @@ raise ValueError() tb = FakeRawTB() excinfo.traceback = Traceback(tb) - fail = IOError() # noqa + fail = IOError() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" fail = py.error.ENOENT # noqa repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" def test_repr_local(self): p = FormattedExcinfo(showlocals=True) - loc = {'y': 5, 'z': 7, 'x': 3, '@x': 2, '__builtins__': {}} + loc = {"y": 5, "z": 7, "x": 3, "@x": 2, "__builtins__": {}} reprlocals = p.repr_locals(loc) assert reprlocals.lines - assert reprlocals.lines[0] == '__builtins__ = ' - assert reprlocals.lines[1] == 'x = 3' - assert reprlocals.lines[2] == 'y = 5' - assert reprlocals.lines[3] == 'z = 7' + assert reprlocals.lines[0] == "__builtins__ = " + assert reprlocals.lines[1] == "x = 3" + assert reprlocals.lines[2] == "y = 5" + assert reprlocals.lines[3] == "z = 7" def test_repr_tracebackentry_lines(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def func1(): raise ValueError("hello\\nworld") - """) + """ + ) excinfo = pytest.raises(ValueError, mod.func1) excinfo.traceback = excinfo.traceback.filter() p = FormattedExcinfo() @@ -580,17 +605,17 @@ raise ValueError() # test as intermittent entry lines = reprtb.lines - assert lines[0] == ' def func1():' + assert lines[0] == " def func1():" assert lines[1] == '> raise ValueError("hello\\nworld")' # test as last entry p = FormattedExcinfo(showlocals=True) repr_entry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo) lines = repr_entry.lines - assert lines[0] == ' def func1():' + assert lines[0] == " def func1():" assert lines[1] == '> raise ValueError("hello\\nworld")' - assert lines[2] == 'E ValueError: hello' - assert lines[3] == 'E world' + assert lines[2] == "E ValueError: hello" + assert lines[3] == "E world" assert not lines[4:] loc = repr_entry.reprlocals is not None @@ -600,42 +625,46 @@ raise ValueError() # assert loc.message == "ValueError: hello" def test_repr_tracebackentry_lines2(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def func1(m, x, y, z): raise ValueError("hello\\nworld") - """) + """ + ) excinfo = pytest.raises(ValueError, mod.func1, "m" * 90, 5, 13, "z" * 120) excinfo.traceback = excinfo.traceback.filter() entry = excinfo.traceback[-1] p = FormattedExcinfo(funcargs=True) reprfuncargs = p.repr_args(entry) - assert reprfuncargs.args[0] == ('m', repr("m" * 90)) - assert reprfuncargs.args[1] == ('x', '5') - assert reprfuncargs.args[2] == ('y', '13') - assert reprfuncargs.args[3] == ('z', repr("z" * 120)) + assert reprfuncargs.args[0] == ("m", repr("m" * 90)) + assert reprfuncargs.args[1] == ("x", "5") + assert reprfuncargs.args[2] == ("y", "13") + assert reprfuncargs.args[3] == ("z", repr("z" * 120)) p = FormattedExcinfo(funcargs=True) repr_entry = p.repr_traceback_entry(entry) assert repr_entry.reprfuncargs.args == reprfuncargs.args tw = TWMock() repr_entry.toterminal(tw) - assert tw.lines[0] == "m = " + repr('m' * 90) + assert tw.lines[0] == "m = " + repr("m" * 90) assert tw.lines[1] == "x = 5, y = 13" - assert tw.lines[2] == "z = " + repr('z' * 120) + assert tw.lines[2] == "z = " + repr("z" * 120) def test_repr_tracebackentry_lines_var_kw_args(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def func1(x, *y, **z): raise ValueError("hello\\nworld") - """) - excinfo = pytest.raises(ValueError, mod.func1, 'a', 'b', c='d') + """ + ) + excinfo = pytest.raises(ValueError, mod.func1, "a", "b", c="d") excinfo.traceback = excinfo.traceback.filter() entry = excinfo.traceback[-1] p = FormattedExcinfo(funcargs=True) reprfuncargs = p.repr_args(entry) - assert reprfuncargs.args[0] == ('x', repr('a')) - assert reprfuncargs.args[1] == ('y', repr(('b',))) - assert reprfuncargs.args[2] == ('z', repr({'c': 'd'})) + assert reprfuncargs.args[0] == ("x", repr("a")) + assert reprfuncargs.args[1] == ("y", repr(("b",))) + assert reprfuncargs.args[2] == ("z", repr({"c": "d"})) p = FormattedExcinfo(funcargs=True) repr_entry = p.repr_traceback_entry(entry) @@ -645,18 +674,20 @@ raise ValueError() assert tw.lines[0] == "x = 'a', y = ('b',), z = {'c': 'd'}" def test_repr_tracebackentry_short(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def func1(): raise ValueError("hello") def entry(): func1() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo(style="short") reprtb = p.repr_traceback_entry(excinfo.traceback[-2]) lines = reprtb.lines basename = py.path.local(mod.__file__).basename - assert lines[0] == ' func1()' + assert lines[0] == " func1()" assert basename in str(reprtb.reprfileloc.path) assert reprtb.reprfileloc.lineno == 5 @@ -665,17 +696,19 @@ raise ValueError() reprtb = p.repr_traceback_entry(excinfo.traceback[-1], excinfo) lines = reprtb.lines assert lines[0] == ' raise ValueError("hello")' - assert lines[1] == 'E ValueError: hello' + assert lines[1] == "E ValueError: hello" assert basename in str(reprtb.reprfileloc.path) assert reprtb.reprfileloc.lineno == 3 def test_repr_tracebackentry_no(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def func1(): raise ValueError("hello") def entry(): func1() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo(style="no") p.repr_traceback_entry(excinfo.traceback[-2]) @@ -683,16 +716,18 @@ raise ValueError() p = FormattedExcinfo(style="no") reprentry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo) lines = reprentry.lines - assert lines[0] == 'E ValueError: hello' + assert lines[0] == "E ValueError: hello" assert not lines[1:] def test_repr_traceback_tbfilter(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def f(x): raise ValueError(x) def entry(): f(0) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo(tbfilter=True) reprtb = p.repr_traceback(excinfo) @@ -702,15 +737,18 @@ raise ValueError() assert len(reprtb.reprentries) == 3 def test_traceback_short_no_source(self, importasmod, monkeypatch): - mod = importasmod(""" + mod = importasmod( + """ def func1(): raise ValueError("hello") def entry(): func1() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) from _pytest._code.code import Code - monkeypatch.setattr(Code, 'path', 'bogus') + + monkeypatch.setattr(Code, "path", "bogus") excinfo.traceback[0].frame.code.path = "bogus" p = FormattedExcinfo(style="short") reprtb = p.repr_traceback_entry(excinfo.traceback[-2]) @@ -719,18 +757,20 @@ raise ValueError() last_reprtb = last_p.repr_traceback_entry(excinfo.traceback[-1], excinfo) last_lines = last_reprtb.lines monkeypatch.undo() - assert lines[0] == ' func1()' + assert lines[0] == " func1()" assert last_lines[0] == ' raise ValueError("hello")' - assert last_lines[1] == 'E ValueError: hello' + assert last_lines[1] == "E ValueError: hello" def test_repr_traceback_and_excinfo(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def f(x): raise ValueError(x) def entry(): f(0) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) for style in ("long", "short"): @@ -742,19 +782,21 @@ raise ValueError() repr = p.repr_excinfo(excinfo) assert repr.reprtraceback assert len(repr.reprtraceback.reprentries) == len(reprtb.reprentries) - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0] assert len(repr.chain[0][0].reprentries) == len(reprtb.reprentries) assert repr.reprcrash.path.endswith("mod.py") assert repr.reprcrash.message == "ValueError: 0" def test_repr_traceback_with_invalid_cwd(self, importasmod, monkeypatch): - mod = importasmod(""" + mod = importasmod( + """ def f(x): raise ValueError(x) def entry(): f(0) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo() @@ -762,15 +804,17 @@ raise ValueError() def raiseos(): raise OSError(2) - monkeypatch.setattr(py.std.os, 'getcwd', raiseos) + monkeypatch.setattr(os, "getcwd", raiseos) assert p._makepath(__file__) == __file__ p.repr_traceback(excinfo) def test_repr_excinfo_addouterr(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def entry(): raise ValueError() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) repr = excinfo.getrepr() repr.addsection("title", "content") @@ -780,10 +824,12 @@ raise ValueError() assert twmock.lines[-2] == ("-", "title") def test_repr_excinfo_reprcrash(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def entry(): raise ValueError() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) repr = excinfo.getrepr() assert repr.reprcrash.path.endswith("mod.py") @@ -792,14 +838,16 @@ raise ValueError() assert str(repr.reprcrash).endswith("mod.py:3: ValueError") def test_repr_traceback_recursion(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def rec2(x): return rec1(x+1) def rec1(x): return rec2(x-1) def entry(): rec1(42) - """) + """ + ) excinfo = pytest.raises(RuntimeError, mod.entry) for style in ("short", "long", "no"): @@ -809,21 +857,23 @@ raise ValueError() assert str(reprtb) def test_reprexcinfo_getrepr(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def f(x): raise ValueError(x) def entry(): f(0) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.entry) for style in ("short", "long", "no"): for showlocals in (True, False): repr = excinfo.getrepr(style=style, showlocals=showlocals) - if py.std.sys.version_info[0] < 3: + if sys.version_info[0] < 3: assert isinstance(repr, ReprExceptionInfo) assert repr.reprtraceback.style == style - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert isinstance(repr, ExceptionChainRepr) for repr in repr.chain: assert repr[0].style == style @@ -832,6 +882,7 @@ raise ValueError() from _pytest._code.code import TerminalRepr class MyRepr(TerminalRepr): + def toterminal(self, tw): tw.line(py.builtin._totext("я", "utf-8")) @@ -839,12 +890,14 @@ raise ValueError() assert x == py.builtin._totext("я", "utf-8") def test_toterminal_long(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def g(x): raise ValueError(x) def f(): g(3) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.f) excinfo.traceback = excinfo.traceback.filter() repr = excinfo.getrepr() @@ -869,14 +922,16 @@ raise ValueError() assert tw.lines[12] == ":3: ValueError" def test_toterminal_long_missing_source(self, importasmod, tmpdir): - mod = importasmod(""" + mod = importasmod( + """ def g(x): raise ValueError(x) def f(): g(3) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.f) - tmpdir.join('mod.py').remove() + tmpdir.join("mod.py").remove() excinfo.traceback = excinfo.traceback.filter() repr = excinfo.getrepr() tw = TWMock() @@ -898,14 +953,16 @@ raise ValueError() assert tw.lines[10] == ":3: ValueError" def test_toterminal_long_incomplete_source(self, importasmod, tmpdir): - mod = importasmod(""" + mod = importasmod( + """ def g(x): raise ValueError(x) def f(): g(3) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.f) - tmpdir.join('mod.py').write('asdf') + tmpdir.join("mod.py").write("asdf") excinfo.traceback = excinfo.traceback.filter() repr = excinfo.getrepr() tw = TWMock() @@ -927,10 +984,12 @@ raise ValueError() assert tw.lines[10] == ":3: ValueError" def test_toterminal_long_filenames(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def f(): raise ValueError() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.f) tw = TWMock() path = py.path.local(mod.__file__) @@ -953,20 +1012,30 @@ raise ValueError() finally: old.chdir() - @pytest.mark.parametrize('reproptions', [ - {'style': style, 'showlocals': showlocals, - 'funcargs': funcargs, 'tbfilter': tbfilter - } for style in ("long", "short", "no") - for showlocals in (True, False) - for tbfilter in (True, False) - for funcargs in (True, False)]) + @pytest.mark.parametrize( + "reproptions", + [ + { + "style": style, + "showlocals": showlocals, + "funcargs": funcargs, + "tbfilter": tbfilter, + } + for style in ("long", "short", "no") + for showlocals in (True, False) + for tbfilter in (True, False) + for funcargs in (True, False) + ], + ) def test_format_excinfo(self, importasmod, reproptions): - mod = importasmod(""" + mod = importasmod( + """ def g(x): raise ValueError(x) def f(): g(3) - """) + """ + ) excinfo = pytest.raises(ValueError, mod.f) tw = py.io.TerminalWriter(stringio=True) repr = excinfo.getrepr(**reproptions) @@ -974,7 +1043,8 @@ raise ValueError() assert tw.stringio.getvalue() def test_traceback_repr_style(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def f(): g() def g(): @@ -983,7 +1053,8 @@ raise ValueError() i() def i(): raise ValueError() - """) + """ + ) excinfo = pytest.raises(ValueError, mod.f) excinfo.traceback = excinfo.traceback.filter() excinfo.traceback[1].set_repr_style("short") @@ -1019,7 +1090,8 @@ raise ValueError() @pytest.mark.skipif("sys.version_info[0] < 3") def test_exc_chain_repr(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ class Err(Exception): pass def f(): @@ -1034,7 +1106,8 @@ raise ValueError() def h(): raise AttributeError() - """) + """ + ) excinfo = pytest.raises(AttributeError, mod.f) r = excinfo.getrepr(style="long") tw = TWMock() @@ -1047,8 +1120,8 @@ raise ValueError() assert tw.lines[3] == "> g()" assert tw.lines[4] == "" line = tw.get_write_msg(5) - assert line.endswith('mod.py') - assert tw.lines[6] == ':6: ' + assert line.endswith("mod.py") + assert tw.lines[6] == ":6: " assert tw.lines[7] == ("_ ", None) assert tw.lines[8] == "" assert tw.lines[9] == " def g():" @@ -1056,10 +1129,13 @@ raise ValueError() assert tw.lines[11] == "E ValueError" assert tw.lines[12] == "" line = tw.get_write_msg(13) - assert line.endswith('mod.py') - assert tw.lines[14] == ':12: ValueError' + assert line.endswith("mod.py") + assert tw.lines[14] == ":12: ValueError" assert tw.lines[15] == "" - assert tw.lines[16] == "The above exception was the direct cause of the following exception:" + assert ( + tw.lines[16] + == "The above exception was the direct cause of the following exception:" + ) assert tw.lines[17] == "" assert tw.lines[18] == " def f():" assert tw.lines[19] == " try:" @@ -1069,10 +1145,13 @@ raise ValueError() assert tw.lines[23] == "E test_exc_chain_repr0.mod.Err" assert tw.lines[24] == "" line = tw.get_write_msg(25) - assert line.endswith('mod.py') + assert line.endswith("mod.py") assert tw.lines[26] == ":8: Err" assert tw.lines[27] == "" - assert tw.lines[28] == "During handling of the above exception, another exception occurred:" + assert ( + tw.lines[28] + == "During handling of the above exception, another exception occurred:" + ) assert tw.lines[29] == "" assert tw.lines[30] == " def f():" assert tw.lines[31] == " try:" @@ -1083,21 +1162,22 @@ raise ValueError() assert tw.lines[36] == "> h()" assert tw.lines[37] == "" line = tw.get_write_msg(38) - assert line.endswith('mod.py') + assert line.endswith("mod.py") assert tw.lines[39] == ":10: " - assert tw.lines[40] == ('_ ', None) + assert tw.lines[40] == ("_ ", None) assert tw.lines[41] == "" assert tw.lines[42] == " def h():" assert tw.lines[43] == "> raise AttributeError()" assert tw.lines[44] == "E AttributeError" assert tw.lines[45] == "" line = tw.get_write_msg(46) - assert line.endswith('mod.py') + assert line.endswith("mod.py") assert tw.lines[47] == ":15: AttributeError" @pytest.mark.skipif("sys.version_info[0] < 3") def test_exc_repr_with_raise_from_none_chain_suppression(self, importasmod): - mod = importasmod(""" + mod = importasmod( + """ def f(): try: g() @@ -1105,7 +1185,8 @@ raise ValueError() raise AttributeError() from None def g(): raise ValueError() - """) + """ + ) excinfo = pytest.raises(AttributeError, mod.f) r = excinfo.getrepr(style="long") tw = TWMock() @@ -1121,15 +1202,24 @@ raise ValueError() assert tw.lines[6] == "E AttributeError" assert tw.lines[7] == "" line = tw.get_write_msg(8) - assert line.endswith('mod.py') + assert line.endswith("mod.py") assert tw.lines[9] == ":6: AttributeError" assert len(tw.lines) == 10 @pytest.mark.skipif("sys.version_info[0] < 3") - @pytest.mark.parametrize('reason, description', [ - ('cause', 'The above exception was the direct cause of the following exception:'), - ('context', 'During handling of the above exception, another exception occurred:'), - ]) + @pytest.mark.parametrize( + "reason, description", + [ + ( + "cause", + "The above exception was the direct cause of the following exception:", + ), + ( + "context", + "During handling of the above exception, another exception occurred:", + ), + ], + ) def test_exc_chain_repr_without_traceback(self, importasmod, reason, description): """ Handle representation of exception chains where one of the exceptions doesn't have a @@ -1137,8 +1227,10 @@ raise ValueError() module (#1984). """ from _pytest.pytester import LineMatcher - exc_handling_code = ' from e' if reason == 'cause' else '' - mod = importasmod(""" + + exc_handling_code = " from e" if reason == "cause" else "" + mod = importasmod( + """ def f(): try: g() @@ -1146,13 +1238,16 @@ raise ValueError() raise RuntimeError('runtime problem'){exc_handling_code} def g(): raise ValueError('invalid value') - """.format(exc_handling_code=exc_handling_code)) + """.format( + exc_handling_code=exc_handling_code + ) + ) with pytest.raises(RuntimeError) as excinfo: mod.f() # emulate the issue described in #1984 - attr = '__%s__' % reason + attr = "__%s__" % reason getattr(excinfo.value, attr).__traceback__ = None r = excinfo.getrepr() @@ -1161,19 +1256,21 @@ raise ValueError() r.toterminal(tw) matcher = LineMatcher(tw.stringio.getvalue().splitlines()) - matcher.fnmatch_lines([ - "ValueError: invalid value", - description, - "* except Exception as e:", - "> * raise RuntimeError('runtime problem')" + exc_handling_code, - "E *RuntimeError: runtime problem", - ]) + matcher.fnmatch_lines( + [ + "ValueError: invalid value", + description, + "* except Exception as e:", + "> * raise RuntimeError('runtime problem')" + exc_handling_code, + "E *RuntimeError: runtime problem", + ] + ) @pytest.mark.parametrize("style", ["short", "long"]) @pytest.mark.parametrize("encoding", [None, "utf8", "utf16"]) def test_repr_traceback_with_unicode(style, encoding): - msg = u'☹' + msg = u"☹" if encoding is not None: msg = msg.encode(encoding) try: @@ -1186,28 +1283,33 @@ def test_repr_traceback_with_unicode(style, encoding): def test_cwd_deleted(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test(tmpdir): tmpdir.chdir() tmpdir.remove() assert False - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 1 failed in *']) - assert 'INTERNALERROR' not in result.stdout.str() + result.stderr.str() + result.stdout.fnmatch_lines(["* 1 failed in *"]) + assert "INTERNALERROR" not in result.stdout.str() + result.stderr.str() def test_exception_repr_extraction_error_on_recursion(): """ Ensure we can properly detect a recursion error even - if some locals raise error on comparision (#2459). + if some locals raise error on comparison (#2459). """ + class numpy_like(object): def __eq__(self, other): if type(other) is numpy_like: - raise ValueError('The truth value of an array ' - 'with more than one element is ambiguous.') + raise ValueError( + "The truth value of an array " + "with more than one element is ambiguous." + ) def a(x): return b(numpy_like()) @@ -1220,14 +1322,17 @@ def test_exception_repr_extraction_error_on_recursion(): except: # noqa from _pytest._code.code import ExceptionInfo from _pytest.pytester import LineMatcher + exc_info = ExceptionInfo() matcher = LineMatcher(str(exc_info.getrepr()).splitlines()) - matcher.fnmatch_lines([ - '!!! Recursion error detected, but an error occurred locating the origin of recursion.', - '*The following exception happened*', - '*ValueError: The truth value of an array*', - ]) + matcher.fnmatch_lines( + [ + "!!! Recursion error detected, but an error occurred locating the origin of recursion.", + "*The following exception happened*", + "*ValueError: The truth value of an array*", + ] + ) def test_no_recursion_index_on_recursion_error(): @@ -1236,17 +1341,17 @@ def test_no_recursion_index_on_recursion_error(): during a recursion error (#2486). """ try: + class RecursionDepthError(object): + def __getattr__(self, attr): - return getattr(self, '_' + attr) + return getattr(self, "_" + attr) RecursionDepthError().trigger except: # noqa from _pytest._code.code import ExceptionInfo + exc_info = ExceptionInfo() - if sys.version_info[:2] == (2, 6): - assert "'RecursionDepthError' object has no attribute '___" in str(exc_info.getrepr()) - else: - assert 'maximum recursion' in str(exc_info.getrepr()) + assert "maximum recursion" in str(exc_info.getrepr()) else: assert 0 diff --git a/third_party/python/pytest/testing/code/test_source.py b/third_party/python/pytest/testing/code/test_source.py index 4f3796cb4209..56dad75671fa 100644 --- a/third_party/python/pytest/testing/code/test_source.py +++ b/third_party/python/pytest/testing/code/test_source.py @@ -2,19 +2,17 @@ # disable flake check on this file because some constructs are strange # or redundant on purpose and can't be disable on a line-by-line basis from __future__ import absolute_import, division, print_function +import inspect import sys import _pytest._code import py import pytest from _pytest._code import Source -from _pytest._code.source import _ast +from _pytest._code.source import ast -if _ast is not None: - astonly = pytest.mark.nothing -else: - astonly = pytest.mark.xfail("True", reason="only works with AST-compile") +astonly = pytest.mark.nothing failsonjython = pytest.mark.xfail("sys.platform.startswith('java')") @@ -25,14 +23,20 @@ def test_source_str_function(): x = Source(" 3") assert str(x) == "3" - x = Source(""" + x = Source( + """ 3 - """, rstrip=False) + """, + rstrip=False, + ) assert str(x) == "\n3\n " - x = Source(""" + x = Source( + """ 3 - """, rstrip=True) + """, + rstrip=True, + ) assert str(x) == "\n3" @@ -43,70 +47,81 @@ def test_unicode(): return x = Source(unicode("4")) assert str(x) == "4" - co = _pytest._code.compile(unicode('u"\xc3\xa5"', 'utf8'), mode='eval') + co = _pytest._code.compile(unicode('u"\xc3\xa5"', "utf8"), mode="eval") val = eval(co) assert isinstance(val, unicode) def test_source_from_function(): source = _pytest._code.Source(test_source_str_function) - assert str(source).startswith('def test_source_str_function():') + assert str(source).startswith("def test_source_str_function():") def test_source_from_method(): + class TestClass(object): + def test_method(self): pass + source = _pytest._code.Source(TestClass().test_method) - assert source.lines == ["def test_method(self):", - " pass"] + assert source.lines == ["def test_method(self):", " pass"] def test_source_from_lines(): lines = ["a \n", "b\n", "c"] source = _pytest._code.Source(lines) - assert source.lines == ['a ', 'b', 'c'] + assert source.lines == ["a ", "b", "c"] def test_source_from_inner_function(): + def f(): pass + source = _pytest._code.Source(f, deindent=False) - assert str(source).startswith(' def f():') + assert str(source).startswith(" def f():") source = _pytest._code.Source(f) - assert str(source).startswith('def f():') + assert str(source).startswith("def f():") def test_source_putaround_simple(): source = Source("raise ValueError") source = source.putaround( - "try:", """\ + "try:", + """\ except ValueError: x = 42 else: - x = 23""") - assert str(source) == """\ + x = 23""", + ) + assert ( + str(source) + == """\ try: raise ValueError except ValueError: x = 42 else: x = 23""" + ) def test_source_putaround(): source = Source() - source = source.putaround(""" + source = source.putaround( + """ if 1: x=1 - """) + """ + ) assert str(source).strip() == "if 1:\n x=1" def test_source_strips(): source = Source("") assert source == Source() - assert str(source) == '' + assert str(source) == "" assert source.strip() == source @@ -118,10 +133,10 @@ def test_source_strip_multiline(): def test_syntaxerror_rerepresentation(): - ex = pytest.raises(SyntaxError, _pytest._code.compile, 'xyz xyz') + ex = pytest.raises(SyntaxError, _pytest._code.compile, "xyz xyz") assert ex.value.lineno == 1 assert ex.value.offset in (4, 7) # XXX pypy/jython versus cpython? - assert ex.value.text.strip(), 'x x' + assert ex.value.text.strip(), "x x" def test_isparseable(): @@ -134,12 +149,14 @@ def test_isparseable(): class TestAccesses(object): - source = Source("""\ + source = Source( + """\ def f(x): pass def g(x): pass - """) + """ + ) def test_getrange(self): x = self.source[0:2] @@ -160,18 +177,20 @@ class TestAccesses(object): class TestSourceParsingAndCompiling(object): - source = Source("""\ + source = Source( + """\ def f(x): assert (x == 3 + 4) - """).strip() + """ + ).strip() def test_compile(self): co = _pytest._code.compile("x=3") d = {} exec(co, d) - assert d['x'] == 3 + assert d["x"] == 3 def test_compile_and_getsource_simple(self): co = _pytest._code.compile("x=3") @@ -180,20 +199,26 @@ class TestSourceParsingAndCompiling(object): assert str(source) == "x=3" def test_compile_and_getsource_through_same_function(self): + def gensource(source): return _pytest._code.compile(source) - co1 = gensource(""" + + co1 = gensource( + """ def f(): raise KeyError() - """) - co2 = gensource(""" + """ + ) + co2 = gensource( + """ def f(): raise ValueError() - """) - source1 = py.std.inspect.getsource(co1) - assert 'KeyError' in source1 - source2 = py.std.inspect.getsource(co2) - assert 'ValueError' in source2 + """ + ) + source1 = inspect.getsource(co1) + assert "KeyError" in source1 + source2 = inspect.getsource(co2) + assert "ValueError" in source2 def test_getstatement(self): # print str(self.source) @@ -201,13 +226,15 @@ class TestSourceParsingAndCompiling(object): for i in range(1, 4): # print "trying start in line %r" % self.source[i] s = self.source.getstatement(i) - #x = s.deindent() + # x = s.deindent() assert str(s) == ass def test_getstatementrange_triple_quoted(self): # print str(self.source) - source = Source("""hello(''' - ''')""") + source = Source( + """hello(''' + ''')""" + ) s = source.getstatement(0) assert s == str(source) s = source.getstatement(1) @@ -215,7 +242,8 @@ class TestSourceParsingAndCompiling(object): @astonly def test_getstatementrange_within_constructs(self): - source = Source("""\ + source = Source( + """\ try: try: raise ValueError @@ -223,7 +251,8 @@ class TestSourceParsingAndCompiling(object): pass finally: 42 - """) + """ + ) assert len(source) == 7 # check all lineno's that could occur in a traceback # assert source.getstatementrange(0) == (0, 7) @@ -235,19 +264,22 @@ class TestSourceParsingAndCompiling(object): assert source.getstatementrange(6) == (6, 7) def test_getstatementrange_bug(self): - source = Source("""\ + source = Source( + """\ try: x = ( y + z) except: pass - """) + """ + ) assert len(source) == 6 assert source.getstatementrange(2) == (1, 4) def test_getstatementrange_bug2(self): - source = Source("""\ + source = Source( + """\ assert ( 33 == @@ -257,23 +289,25 @@ class TestSourceParsingAndCompiling(object): ), ] ) - """) + """ + ) assert len(source) == 9 assert source.getstatementrange(5) == (0, 9) def test_getstatementrange_ast_issue58(self): - source = Source("""\ + source = Source( + """\ def test_some(): for a in [a for a in CAUSE_ERROR]: pass x = 3 - """) + """ + ) assert getstatement(2, source).lines == source.lines[2:3] assert getstatement(3, source).lines == source.lines[3:4] - @pytest.mark.skipif("sys.version_info < (2,6)") def test_getstatementrange_out_of_bounds_py3(self): source = Source("if xxx:\n from .collections import something") r = source.getstatementrange(1) @@ -283,9 +317,9 @@ class TestSourceParsingAndCompiling(object): source = Source(":") pytest.raises(SyntaxError, lambda: source.getstatementrange(0)) - @pytest.mark.skipif("sys.version_info < (2,6)") def test_compile_to_ast(self): import ast + source = Source("x = 4") mod = source.compile(flag=ast.PyCF_ONLY_AST) assert isinstance(mod, ast.Module) @@ -299,16 +333,17 @@ class TestSourceParsingAndCompiling(object): frame = excinfo.traceback[-1].frame stmt = frame.code.fullsource.getstatement(frame.lineno) # print "block", str(block) - assert str(stmt).strip().startswith('assert') + assert str(stmt).strip().startswith("assert") - @pytest.mark.parametrize('name', ['', None, 'my']) + @pytest.mark.parametrize("name", ["", None, "my"]) def test_compilefuncs_and_path_sanity(self, name): + def check(comp, name): co = comp(self.source, name) if not name: - expected = "codegen %s:%d>" % (mypath, mylineno + 2 + 2) + expected = "codegen %s:%d>" % (mypath, mylineno + 2 + 3) else: - expected = "codegen %r %s:%d>" % (name, mypath, mylineno + 2 + 2) + expected = "codegen %r %s:%d>" % (name, mypath, mylineno + 2 + 3) fn = co.co_filename assert fn.endswith(expected) @@ -320,33 +355,41 @@ class TestSourceParsingAndCompiling(object): check(comp, name) def test_offsetless_synerr(self): - pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode='eval') + pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode="eval") def test_getstartingblock_singleline(): + class A(object): + def __init__(self, *args): frame = sys._getframe(1) self.source = _pytest._code.Frame(frame).statement - x = A('x', 'y') + x = A("x", "y") values = [i for i in x.source.lines if i.strip()] assert len(values) == 1 def test_getline_finally(): - def c(): pass - excinfo = pytest.raises(TypeError, """ + + def c(): + pass + + excinfo = pytest.raises( + TypeError, + """ teardown = None try: c(1) finally: if teardown: teardown() - """) + """, + ) source = excinfo.traceback[-1].statement - assert str(source).strip() == 'c(1)' + assert str(source).strip() == "c(1)" def test_getfuncsource_dynamic(): @@ -358,29 +401,35 @@ def test_getfuncsource_dynamic(): """ co = _pytest._code.compile(source) py.builtin.exec_(co, globals()) - assert str(_pytest._code.Source(f)).strip() == 'def f():\n raise ValueError' - assert str(_pytest._code.Source(g)).strip() == 'def g(): pass' + assert str(_pytest._code.Source(f)).strip() == "def f():\n raise ValueError" + assert str(_pytest._code.Source(g)).strip() == "def g(): pass" def test_getfuncsource_with_multine_string(): + def f(): - c = '''while True: + c = """while True: pass -''' - assert str(_pytest._code.Source(f)).strip() == "def f():\n c = '''while True:\n pass\n'''" +""" + + assert ( + str(_pytest._code.Source(f)).strip() + == 'def f():\n c = """while True:\n pass\n"""' + ) def test_deindent(): from _pytest._code.source import deindent as deindent - assert deindent(['\tfoo', '\tbar', ]) == ['foo', 'bar'] + + assert deindent(["\tfoo", "\tbar"]) == ["foo", "bar"] def f(): - c = '''while True: + c = """while True: pass -''' - import inspect +""" + lines = deindent(inspect.getsource(f).splitlines()) - assert lines == ["def f():", " c = '''while True:", " pass", "'''"] + assert lines == ["def f():", ' c = """while True:', " pass", '"""'] source = """ def f(): @@ -388,17 +437,19 @@ def test_deindent(): pass """ lines = deindent(source.splitlines()) - assert lines == ['', 'def f():', ' def g():', ' pass', ' '] + assert lines == ["", "def f():", " def g():", " pass", " "] def test_source_of_class_at_eof_without_newline(tmpdir): # this test fails because the implicit inspect.getsource(A) below # does not return the "x = 1" last line. - source = _pytest._code.Source(''' + source = _pytest._code.Source( + """ class A(object): def method(self): x = 1 - ''') + """ + ) path = tmpdir.join("a.py") path.write(source) s2 = _pytest._code.Source(tmpdir.join("a.py").pyimport().A) @@ -406,12 +457,14 @@ def test_source_of_class_at_eof_without_newline(tmpdir): if True: + def x(): pass def test_getsource_fallback(): from _pytest._code.source import getsource + expected = """def x(): pass""" src = getsource(x) @@ -420,6 +473,7 @@ def test_getsource_fallback(): def test_idem_compile_and_getsource(): from _pytest._code.source import getsource + expected = "def x(): pass" co = _pytest._code.compile(expected) src = getsource(co) @@ -428,25 +482,29 @@ def test_idem_compile_and_getsource(): def test_findsource_fallback(): from _pytest._code.source import findsource + src, lineno = findsource(x) - assert 'test_findsource_simple' in str(src) - assert src[lineno] == ' def x():' + assert "test_findsource_simple" in str(src) + assert src[lineno] == " def x():" def test_findsource(): from _pytest._code.source import findsource - co = _pytest._code.compile("""if 1: + + co = _pytest._code.compile( + """if 1: def x(): pass -""") +""" + ) src, lineno = findsource(co) - assert 'if 1:' in str(src) + assert "if 1:" in str(src) d = {} eval(co, d) - src, lineno = findsource(d['x']) - assert 'if 1:' in str(src) + src, lineno = findsource(d["x"]) + assert "if 1:" in str(src) assert src[lineno] == " def x():" @@ -466,7 +524,7 @@ def test_getfslineno(): fspath, lineno = getfslineno(A) - _, A_lineno = py.std.inspect.findsource(A) + _, A_lineno = inspect.findsource(A) assert fspath.basename == "test_source.py" assert lineno == A_lineno @@ -474,30 +532,37 @@ def test_getfslineno(): class B(object): pass + B.__name__ = "B2" assert getfslineno(B)[1] == -1 def test_code_of_object_instance_with_call(): + class A(object): pass + pytest.raises(TypeError, lambda: _pytest._code.Source(A())) class WithCall(object): + def __call__(self): pass code = _pytest._code.Code(WithCall()) - assert 'pass' in str(code.source()) + assert "pass" in str(code.source()) class Hello(object): + def __call__(self): pass + pytest.raises(TypeError, lambda: _pytest._code.Code(Hello)) def getstatement(lineno, source): from _pytest._code.source import getstatementrange_ast + source = _pytest._code.Source(source, deindent=False) ast, start, end = getstatementrange_ast(lineno, source) return source[start:end] @@ -510,9 +575,14 @@ def test_oneline(): def test_comment_and_no_newline_at_end(): from _pytest._code.source import getstatementrange_ast - source = Source(['def test_basic_complex():', - ' assert 1 == 2', - '# vim: filetype=pyopencl:fdm=marker']) + + source = Source( + [ + "def test_basic_complex():", + " assert 1 == 2", + "# vim: filetype=pyopencl:fdm=marker", + ] + ) ast, start, end = getstatementrange_ast(1, source) assert end == 2 @@ -522,8 +592,7 @@ def test_oneline_and_comment(): assert str(source) == "raise ValueError" -@pytest.mark.xfail(hasattr(sys, "pypy_version_info"), - reason='does not work on pypy') +@pytest.mark.xfail(hasattr(sys, "pypy_version_info"), reason="does not work on pypy") def test_comments(): source = '''def test(): "comment 1" @@ -538,20 +607,22 @@ comment 4 """ ''' for line in range(2, 6): - assert str(getstatement(line, source)) == ' x = 1' + assert str(getstatement(line, source)) == " x = 1" for line in range(6, 10): - assert str(getstatement(line, source)) == ' assert False' + assert str(getstatement(line, source)) == " assert False" assert str(getstatement(10, source)) == '"""' def test_comment_in_statement(): - source = '''test(foo=1, + source = """test(foo=1, # comment 1 bar=2) -''' +""" for line in range(1, 3): - assert str(getstatement(line, source)) == \ - 'test(foo=1,\n # comment 1\n bar=2)' + assert ( + str(getstatement(line, source)) + == "test(foo=1,\n # comment 1\n bar=2)" + ) def test_single_line_else(): @@ -565,19 +636,24 @@ def test_single_line_finally(): def test_issue55(): - source = ('def round_trip(dinp):\n assert 1 == dinp\n' - 'def test_rt():\n round_trip("""\n""")\n') + source = ( + "def round_trip(dinp):\n assert 1 == dinp\n" + 'def test_rt():\n round_trip("""\n""")\n' + ) s = getstatement(3, source) assert str(s) == ' round_trip("""\n""")' def XXXtest_multiline(): - source = getstatement(0, """\ + source = getstatement( + 0, + """\ raise ValueError( 23 ) x = 3 -""") +""", + ) assert str(source) == "raise ValueError(\n 23\n)" diff --git a/third_party/python/pytest/testing/code/test_source_multiline_block.py b/third_party/python/pytest/testing/code/test_source_multiline_block.py index b356d191f0b0..92f7412eb8cf 100644 --- a/third_party/python/pytest/testing/code/test_source_multiline_block.py +++ b/third_party/python/pytest/testing/code/test_source_multiline_block.py @@ -12,15 +12,18 @@ def test_getstartingblock_multiline(): see hhatto/autopep8#307). It was considered better to just move this single test to its own file and exclude it from autopep8 than try to complicate things. """ + class A(object): + def __init__(self, *args): frame = sys._getframe(1) self.source = _pytest._code.Frame(frame).statement + # fmt: off x = A('x', 'y' , 'z') - + # fmt: on values = [i for i in x.source.lines if i.strip()] assert len(values) == 4 diff --git a/third_party/python/pytest/testing/deprecated_test.py b/third_party/python/pytest/testing/deprecated_test.py index 3f244a53c756..39ff1f1fc385 100644 --- a/third_party/python/pytest/testing/deprecated_test.py +++ b/third_party/python/pytest/testing/deprecated_test.py @@ -3,7 +3,8 @@ import pytest def test_yield_tests_deprecation(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def func1(arg, arg2): assert arg == arg2 def test_gen(): @@ -12,73 +13,253 @@ def test_yield_tests_deprecation(testdir): def test_gen2(): for k in range(10): yield func1, 1, 1 - """) - result = testdir.runpytest('-ra') - result.stdout.fnmatch_lines([ - '*yield tests are deprecated, and scheduled to be removed in pytest 4.0*', - '*2 passed*', - ]) - assert result.stdout.str().count('yield tests are deprecated') == 2 + """ + ) + result = testdir.runpytest("-ra") + result.stdout.fnmatch_lines( + [ + "*yield tests are deprecated, and scheduled to be removed in pytest 4.0*", + "*2 passed*", + ] + ) + assert result.stdout.str().count("yield tests are deprecated") == 2 def test_funcarg_prefix_deprecation(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def pytest_funcarg__value(): return 10 def test_funcarg_prefix(value): assert value == 10 - """) - result = testdir.runpytest('-ra') - result.stdout.fnmatch_lines([ - ('*pytest_funcarg__value: ' - 'declaring fixtures using "pytest_funcarg__" prefix is deprecated ' - 'and scheduled to be removed in pytest 4.0. ' - 'Please remove the prefix and use the @pytest.fixture decorator instead.'), - '*1 passed*', - ]) + """ + ) + result = testdir.runpytest("-ra") + result.stdout.fnmatch_lines( + [ + ( + "*pytest_funcarg__value: " + 'declaring fixtures using "pytest_funcarg__" prefix is deprecated ' + "and scheduled to be removed in pytest 4.0. " + "Please remove the prefix and use the @pytest.fixture decorator instead." + ), + "*1 passed*", + ] + ) def test_pytest_setup_cfg_deprecated(testdir): - testdir.makefile('.cfg', setup=''' + testdir.makefile( + ".cfg", + setup=""" [pytest] addopts = --verbose - ''') + """, + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['*pytest*section in setup.cfg files is deprecated*use*tool:pytest*instead*']) + result.stdout.fnmatch_lines( + ["*pytest*section in setup.cfg files is deprecated*use*tool:pytest*instead*"] + ) + + +def test_pytest_custom_cfg_deprecated(testdir): + testdir.makefile( + ".cfg", + custom=""" + [pytest] + addopts = --verbose + """, + ) + result = testdir.runpytest("-c", "custom.cfg") + result.stdout.fnmatch_lines( + ["*pytest*section in custom.cfg files is deprecated*use*tool:pytest*instead*"] + ) def test_str_args_deprecated(tmpdir, testdir): """Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0.""" from _pytest.main import EXIT_NOTESTSCOLLECTED + warnings = [] class Collect(object): + def pytest_logwarning(self, message): warnings.append(message) ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()]) - testdir.delete_loaded_modules() - msg = ('passing a string to pytest.main() is deprecated, ' - 'pass a list of arguments instead.') + msg = ( + "passing a string to pytest.main() is deprecated, " + "pass a list of arguments instead." + ) assert msg in warnings assert ret == EXIT_NOTESTSCOLLECTED def test_getfuncargvalue_is_deprecated(request): - pytest.deprecated_call(request.getfuncargvalue, 'tmpdir') + pytest.deprecated_call(request.getfuncargvalue, "tmpdir") def test_resultlog_is_deprecated(testdir): - result = testdir.runpytest('--help') - result.stdout.fnmatch_lines(['*DEPRECATED path for machine-readable result log*']) + result = testdir.runpytest("--help") + result.stdout.fnmatch_lines(["*DEPRECATED path for machine-readable result log*"]) - testdir.makepyfile(''' + testdir.makepyfile( + """ def test(): pass - ''') - result = testdir.runpytest('--result-log=%s' % testdir.tmpdir.join('result.log')) - result.stdout.fnmatch_lines([ - '*--result-log is deprecated and scheduled for removal in pytest 4.0*', - '*See https://docs.pytest.org/*/usage.html#creating-resultlog-format-files for more information*', - ]) + """ + ) + result = testdir.runpytest("--result-log=%s" % testdir.tmpdir.join("result.log")) + result.stdout.fnmatch_lines( + [ + "*--result-log is deprecated and scheduled for removal in pytest 4.0*", + "*See https://docs.pytest.org/*/usage.html#creating-resultlog-format-files for more information*", + ] + ) + + +@pytest.mark.filterwarnings("always:Metafunc.addcall is deprecated") +def test_metafunc_addcall_deprecated(testdir): + testdir.makepyfile( + """ + def pytest_generate_tests(metafunc): + metafunc.addcall({'i': 1}) + metafunc.addcall({'i': 2}) + def test_func(i): + pass + """ + ) + res = testdir.runpytest("-s") + assert res.ret == 0 + res.stdout.fnmatch_lines( + ["*Metafunc.addcall is deprecated*", "*2 passed, 2 warnings*"] + ) + + +def test_terminal_reporter_writer_attr(pytestconfig): + """Check that TerminalReporter._tw is also available as 'writer' (#2984) + This attribute is planned to be deprecated in 3.4. + """ + try: + import xdist # noqa + + pytest.skip("xdist workers disable the terminal reporter plugin") + except ImportError: + pass + terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter") + assert terminal_reporter.writer is terminal_reporter._tw + + +@pytest.mark.parametrize("plugin", ["catchlog", "capturelog"]) +def test_pytest_catchlog_deprecated(testdir, plugin): + testdir.makepyfile( + """ + def test_func(pytestconfig): + pytestconfig.pluginmanager.register(None, 'pytest_{}') + """.format( + plugin + ) + ) + res = testdir.runpytest() + assert res.ret == 0 + res.stdout.fnmatch_lines( + ["*pytest-*log plugin has been merged into the core*", "*1 passed, 1 warnings*"] + ) + + +def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir): + from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST + + subdirectory = testdir.tmpdir.join("subdirectory") + subdirectory.mkdir() + # create the inner conftest with makeconftest and then move it to the subdirectory + testdir.makeconftest( + """ + pytest_plugins=['capture'] + """ + ) + testdir.tmpdir.join("conftest.py").move(subdirectory.join("conftest.py")) + # make the top level conftest + testdir.makeconftest( + """ + import warnings + warnings.filterwarnings('always', category=DeprecationWarning) + """ + ) + testdir.makepyfile( + """ + def test_func(): + pass + """ + ) + res = testdir.runpytest_subprocess() + assert res.ret == 0 + res.stderr.fnmatch_lines( + "*" + str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] + ) + + +def test_pytest_plugins_in_non_top_level_conftest_deprecated_no_top_level_conftest( + testdir +): + from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST + + subdirectory = testdir.tmpdir.join("subdirectory") + subdirectory.mkdir() + testdir.makeconftest( + """ + import warnings + warnings.filterwarnings('always', category=DeprecationWarning) + pytest_plugins=['capture'] + """ + ) + testdir.tmpdir.join("conftest.py").move(subdirectory.join("conftest.py")) + + testdir.makepyfile( + """ + def test_func(): + pass + """ + ) + + res = testdir.runpytest_subprocess() + assert res.ret == 0 + res.stderr.fnmatch_lines( + "*" + str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] + ) + + +def test_pytest_plugins_in_non_top_level_conftest_deprecated_no_false_positives( + testdir +): + from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST + + subdirectory = testdir.tmpdir.join("subdirectory") + subdirectory.mkdir() + testdir.makeconftest( + """ + pass + """ + ) + testdir.tmpdir.join("conftest.py").move(subdirectory.join("conftest.py")) + + testdir.makeconftest( + """ + import warnings + warnings.filterwarnings('always', category=DeprecationWarning) + pytest_plugins=['capture'] + """ + ) + testdir.makepyfile( + """ + def test_func(): + pass + """ + ) + res = testdir.runpytest_subprocess() + assert res.ret == 0 + assert str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[ + 0 + ] not in res.stderr.str() diff --git a/third_party/python/pytest/testing/freeze/.gitignore b/third_party/python/pytest/testing/freeze/.gitignore index 490310b6c116..b53319087248 100644 --- a/third_party/python/pytest/testing/freeze/.gitignore +++ b/third_party/python/pytest/testing/freeze/.gitignore @@ -1,3 +1,3 @@ build/ dist/ -*.spec \ No newline at end of file +*.spec diff --git a/third_party/python/pytest/testing/freeze/create_executable.py b/third_party/python/pytest/testing/freeze/create_executable.py index f4f6088ef7d0..98aa2034c9d7 100644 --- a/third_party/python/pytest/testing/freeze/create_executable.py +++ b/third_party/python/pytest/testing/freeze/create_executable.py @@ -1,12 +1,12 @@ """ Generates an executable with pytest runner embedded using PyInstaller. """ -if __name__ == '__main__': +if __name__ == "__main__": import pytest import subprocess hidden = [] for x in pytest.freeze_includes(): - hidden.extend(['--hidden-import', x]) - args = ['pyinstaller', '--noconfirm'] + hidden + ['runtests_script.py'] - subprocess.check_call(' '.join(args), shell=True) + hidden.extend(["--hidden-import", x]) + args = ["pyinstaller", "--noconfirm"] + hidden + ["runtests_script.py"] + subprocess.check_call(" ".join(args), shell=True) diff --git a/third_party/python/pytest/testing/freeze/runtests_script.py b/third_party/python/pytest/testing/freeze/runtests_script.py index d281601c0684..d03bca8406de 100644 --- a/third_party/python/pytest/testing/freeze/runtests_script.py +++ b/third_party/python/pytest/testing/freeze/runtests_script.py @@ -3,7 +3,8 @@ This is the script that is actually frozen into an executable: simply executes py.test main(). """ -if __name__ == '__main__': +if __name__ == "__main__": import sys import pytest + sys.exit(pytest.main()) diff --git a/third_party/python/pytest/testing/freeze/tests/test_trivial.py b/third_party/python/pytest/testing/freeze/tests/test_trivial.py index 45622b850bb5..08a55552abbf 100644 --- a/third_party/python/pytest/testing/freeze/tests/test_trivial.py +++ b/third_party/python/pytest/testing/freeze/tests/test_trivial.py @@ -1,7 +1,6 @@ - def test_upper(): - assert 'foo'.upper() == 'FOO' + assert "foo".upper() == "FOO" def test_lower(): - assert 'FOO'.lower() == 'foo' + assert "FOO".lower() == "foo" diff --git a/third_party/python/pytest/testing/freeze/tox_run.py b/third_party/python/pytest/testing/freeze/tox_run.py index 3fc388040957..678a69c858a2 100644 --- a/third_party/python/pytest/testing/freeze/tox_run.py +++ b/third_party/python/pytest/testing/freeze/tox_run.py @@ -2,11 +2,11 @@ Called by tox.ini: uses the generated executable to run the tests in ./tests/ directory. """ -if __name__ == '__main__': +if __name__ == "__main__": import os import sys - executable = os.path.join(os.getcwd(), 'dist', 'runtests_script', 'runtests_script') - if sys.platform.startswith('win'): - executable += '.exe' - sys.exit(os.system('%s tests' % executable)) + executable = os.path.join(os.getcwd(), "dist", "runtests_script", "runtests_script") + if sys.platform.startswith("win"): + executable += ".exe" + sys.exit(os.system("%s tests" % executable)) diff --git a/third_party/python/pytest/testing/logging/test_fixture.py b/third_party/python/pytest/testing/logging/test_fixture.py new file mode 100644 index 000000000000..8d9ae6b518da --- /dev/null +++ b/third_party/python/pytest/testing/logging/test_fixture.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +import logging + +import pytest + +logger = logging.getLogger(__name__) +sublogger = logging.getLogger(__name__ + ".baz") + + +def test_fixture_help(testdir): + result = testdir.runpytest("--fixtures") + result.stdout.fnmatch_lines(["*caplog*"]) + + +def test_change_level(caplog): + caplog.set_level(logging.INFO) + logger.debug("handler DEBUG level") + logger.info("handler INFO level") + + caplog.set_level(logging.CRITICAL, logger=sublogger.name) + sublogger.warning("logger WARNING level") + sublogger.critical("logger CRITICAL level") + + assert "DEBUG" not in caplog.text + assert "INFO" in caplog.text + assert "WARNING" not in caplog.text + assert "CRITICAL" in caplog.text + + +def test_change_level_undo(testdir): + """Ensure that 'set_level' is undone after the end of the test""" + testdir.makepyfile( + """ + import logging + + def test1(caplog): + caplog.set_level(logging.INFO) + # using + operator here so fnmatch_lines doesn't match the code in the traceback + logging.info('log from ' + 'test1') + assert 0 + + def test2(caplog): + # using + operator here so fnmatch_lines doesn't match the code in the traceback + logging.info('log from ' + 'test2') + assert 0 + """ + ) + result = testdir.runpytest_subprocess() + result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"]) + assert "log from test2" not in result.stdout.str() + + +def test_with_statement(caplog): + with caplog.at_level(logging.INFO): + logger.debug("handler DEBUG level") + logger.info("handler INFO level") + + with caplog.at_level(logging.CRITICAL, logger=sublogger.name): + sublogger.warning("logger WARNING level") + sublogger.critical("logger CRITICAL level") + + assert "DEBUG" not in caplog.text + assert "INFO" in caplog.text + assert "WARNING" not in caplog.text + assert "CRITICAL" in caplog.text + + +def test_log_access(caplog): + caplog.set_level(logging.INFO) + logger.info("boo %s", "arg") + assert caplog.records[0].levelname == "INFO" + assert caplog.records[0].msg == "boo %s" + assert "boo arg" in caplog.text + + +def test_record_tuples(caplog): + caplog.set_level(logging.INFO) + logger.info("boo %s", "arg") + + assert caplog.record_tuples == [(__name__, logging.INFO, "boo arg")] + + +def test_unicode(caplog): + caplog.set_level(logging.INFO) + logger.info(u"bū") + assert caplog.records[0].levelname == "INFO" + assert caplog.records[0].msg == u"bū" + assert u"bū" in caplog.text + + +def test_clear(caplog): + caplog.set_level(logging.INFO) + logger.info(u"bū") + assert len(caplog.records) + assert caplog.text + caplog.clear() + assert not len(caplog.records) + assert not caplog.text + + +@pytest.fixture +def logging_during_setup_and_teardown(caplog): + caplog.set_level("INFO") + logger.info("a_setup_log") + yield + logger.info("a_teardown_log") + assert [x.message for x in caplog.get_records("teardown")] == ["a_teardown_log"] + + +def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardown): + assert not caplog.records + assert not caplog.get_records("call") + logger.info("a_call_log") + assert [x.message for x in caplog.get_records("call")] == ["a_call_log"] + + assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"] + + # This reachers into private API, don't use this type of thing in real tests! + assert set(caplog._item.catch_log_handlers.keys()) == {"setup", "call"} diff --git a/third_party/python/pytest/testing/logging/test_formatter.py b/third_party/python/pytest/testing/logging/test_formatter.py new file mode 100644 index 000000000000..ca2a41065376 --- /dev/null +++ b/third_party/python/pytest/testing/logging/test_formatter.py @@ -0,0 +1,37 @@ +import logging + +import py.io +from _pytest.logging import ColoredLevelFormatter + + +def test_coloredlogformatter(): + logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s" + + record = logging.LogRecord( + name="dummy", + level=logging.INFO, + pathname="dummypath", + lineno=10, + msg="Test Message", + args=(), + exc_info=False, + ) + + class ColorConfig(object): + + class option(object): + pass + + tw = py.io.TerminalWriter() + tw.hasmarkup = True + formatter = ColoredLevelFormatter(tw, logfmt) + output = formatter.format(record) + assert ( + output + == ("dummypath 10 " "\x1b[32mINFO \x1b[0m Test Message") + ) + + tw.hasmarkup = False + formatter = ColoredLevelFormatter(tw, logfmt) + output = formatter.format(record) + assert output == ("dummypath 10 " "INFO Test Message") diff --git a/third_party/python/pytest/testing/logging/test_reporting.py b/third_party/python/pytest/testing/logging/test_reporting.py new file mode 100644 index 000000000000..91ed2e4758cb --- /dev/null +++ b/third_party/python/pytest/testing/logging/test_reporting.py @@ -0,0 +1,874 @@ +# -*- coding: utf-8 -*- +import re +import os + +import six + +import pytest + + +def test_nothing_logged(testdir): + testdir.makepyfile( + """ + import sys + + def test_foo(): + sys.stdout.write('text going to stdout') + sys.stderr.write('text going to stderr') + assert False + """ + ) + result = testdir.runpytest() + assert result.ret == 1 + result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) + result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) + with pytest.raises(pytest.fail.Exception): + result.stdout.fnmatch_lines(["*- Captured *log call -*"]) + + +def test_messages_logged(testdir): + testdir.makepyfile( + """ + import sys + import logging + + logger = logging.getLogger(__name__) + + def test_foo(): + sys.stdout.write('text going to stdout') + sys.stderr.write('text going to stderr') + logger.info('text going to logger') + assert False + """ + ) + result = testdir.runpytest("--log-level=INFO") + assert result.ret == 1 + result.stdout.fnmatch_lines(["*- Captured *log call -*", "*text going to logger*"]) + result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) + result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) + + +def test_root_logger_affected(testdir): + testdir.makepyfile( + """ + import logging + logger = logging.getLogger() + def test_foo(): + logger.info('info text ' + 'going to logger') + logger.warning('warning text ' + 'going to logger') + logger.error('error text ' + 'going to logger') + + assert 0 + """ + ) + log_file = testdir.tmpdir.join("pytest.log").strpath + result = testdir.runpytest("--log-level=ERROR", "--log-file=pytest.log") + assert result.ret == 1 + + # the capture log calls in the stdout section only contain the + # logger.error msg, because --log-level=ERROR + result.stdout.fnmatch_lines(["*error text going to logger*"]) + with pytest.raises(pytest.fail.Exception): + result.stdout.fnmatch_lines(["*warning text going to logger*"]) + with pytest.raises(pytest.fail.Exception): + result.stdout.fnmatch_lines(["*info text going to logger*"]) + + # the log file should contain the warning and the error log messages and + # not the info one, because the default level of the root logger is + # WARNING. + assert os.path.isfile(log_file) + with open(log_file) as rfh: + contents = rfh.read() + assert "info text going to logger" not in contents + assert "warning text going to logger" in contents + assert "error text going to logger" in contents + + +def test_log_cli_level_log_level_interaction(testdir): + testdir.makepyfile( + """ + import logging + logger = logging.getLogger() + + def test_foo(): + logger.debug('debug text ' + 'going to logger') + logger.info('info text ' + 'going to logger') + logger.warning('warning text ' + 'going to logger') + logger.error('error text ' + 'going to logger') + assert 0 + """ + ) + + result = testdir.runpytest("--log-cli-level=INFO", "--log-level=ERROR") + assert result.ret == 1 + + result.stdout.fnmatch_lines( + [ + "*-- live log call --*", + "*INFO*info text going to logger", + "*WARNING*warning text going to logger", + "*ERROR*error text going to logger", + "=* 1 failed in *=", + ] + ) + assert "DEBUG" not in result.stdout.str() + + +def test_setup_logging(testdir): + testdir.makepyfile( + """ + import logging + + logger = logging.getLogger(__name__) + + def setup_function(function): + logger.info('text going to logger from setup') + + def test_foo(): + logger.info('text going to logger from call') + assert False + """ + ) + result = testdir.runpytest("--log-level=INFO") + assert result.ret == 1 + result.stdout.fnmatch_lines( + [ + "*- Captured *log setup -*", + "*text going to logger from setup*", + "*- Captured *log call -*", + "*text going to logger from call*", + ] + ) + + +def test_teardown_logging(testdir): + testdir.makepyfile( + """ + import logging + + logger = logging.getLogger(__name__) + + def test_foo(): + logger.info('text going to logger from call') + + def teardown_function(function): + logger.info('text going to logger from teardown') + assert False + """ + ) + result = testdir.runpytest("--log-level=INFO") + assert result.ret == 1 + result.stdout.fnmatch_lines( + [ + "*- Captured *log call -*", + "*text going to logger from call*", + "*- Captured *log teardown -*", + "*text going to logger from teardown*", + ] + ) + + +def test_disable_log_capturing(testdir): + testdir.makepyfile( + """ + import sys + import logging + + logger = logging.getLogger(__name__) + + def test_foo(): + sys.stdout.write('text going to stdout') + logger.warning('catch me if you can!') + sys.stderr.write('text going to stderr') + assert False + """ + ) + result = testdir.runpytest("--no-print-logs") + print(result.stdout) + assert result.ret == 1 + result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) + result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) + with pytest.raises(pytest.fail.Exception): + result.stdout.fnmatch_lines(["*- Captured *log call -*"]) + + +def test_disable_log_capturing_ini(testdir): + testdir.makeini( + """ + [pytest] + log_print=False + """ + ) + testdir.makepyfile( + """ + import sys + import logging + + logger = logging.getLogger(__name__) + + def test_foo(): + sys.stdout.write('text going to stdout') + logger.warning('catch me if you can!') + sys.stderr.write('text going to stderr') + assert False + """ + ) + result = testdir.runpytest() + print(result.stdout) + assert result.ret == 1 + result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) + result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) + with pytest.raises(pytest.fail.Exception): + result.stdout.fnmatch_lines(["*- Captured *log call -*"]) + + +@pytest.mark.parametrize("enabled", [True, False]) +def test_log_cli_enabled_disabled(testdir, enabled): + msg = "critical message logged by test" + testdir.makepyfile( + """ + import logging + def test_log_cli(): + logging.critical("{}") + """.format( + msg + ) + ) + if enabled: + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + result = testdir.runpytest() + if enabled: + result.stdout.fnmatch_lines( + [ + "test_log_cli_enabled_disabled.py::test_log_cli ", + "*-- live log call --*", + "test_log_cli_enabled_disabled.py* CRITICAL critical message logged by test", + "PASSED*", + ] + ) + else: + assert msg not in result.stdout.str() + + +def test_log_cli_default_level(testdir): + # Default log file level + testdir.makepyfile( + """ + import pytest + import logging + def test_log_cli(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_cli_handler.level == logging.NOTSET + logging.getLogger('catchlog').info("INFO message won't be shown") + logging.getLogger('catchlog').warning("WARNING message will be shown") + """ + ) + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + + result = testdir.runpytest() + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines( + [ + "test_log_cli_default_level.py::test_log_cli ", + "test_log_cli_default_level.py*WARNING message will be shown*", + ] + ) + assert "INFO message won't be shown" not in result.stdout.str() + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + + +def test_log_cli_default_level_multiple_tests(testdir, request): + """Ensure we reset the first newline added by the live logger between tests""" + filename = request.node.name + ".py" + testdir.makepyfile( + """ + import logging + + def test_log_1(): + logging.warning("log message from test_log_1") + + def test_log_2(): + logging.warning("log message from test_log_2") + """ + ) + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "{}::test_log_1 ".format(filename), + "*WARNING*log message from test_log_1*", + "PASSED *50%*", + "{}::test_log_2 ".format(filename), + "*WARNING*log message from test_log_2*", + "PASSED *100%*", + "=* 2 passed in *=", + ] + ) + + +def test_log_cli_default_level_sections(testdir, request): + """Check that with live logging enable we are printing the correct headers during + start/setup/call/teardown/finish.""" + filename = request.node.name + ".py" + testdir.makeconftest( + """ + import pytest + import logging + + def pytest_runtest_logstart(): + logging.warning('>>>>> START >>>>>') + + def pytest_runtest_logfinish(): + logging.warning('<<<<< END <<<<<<<') + """ + ) + + testdir.makepyfile( + """ + import pytest + import logging + + @pytest.fixture + def fix(request): + logging.warning("log message from setup of {}".format(request.node.name)) + yield + logging.warning("log message from teardown of {}".format(request.node.name)) + + def test_log_1(fix): + logging.warning("log message from test_log_1") + + def test_log_2(fix): + logging.warning("log message from test_log_2") + """ + ) + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "{}::test_log_1 ".format(filename), + "*-- live log start --*", + "*WARNING* >>>>> START >>>>>*", + "*-- live log setup --*", + "*WARNING*log message from setup of test_log_1*", + "*-- live log call --*", + "*WARNING*log message from test_log_1*", + "PASSED *50%*", + "*-- live log teardown --*", + "*WARNING*log message from teardown of test_log_1*", + "*-- live log finish --*", + "*WARNING* <<<<< END <<<<<<<*", + "{}::test_log_2 ".format(filename), + "*-- live log start --*", + "*WARNING* >>>>> START >>>>>*", + "*-- live log setup --*", + "*WARNING*log message from setup of test_log_2*", + "*-- live log call --*", + "*WARNING*log message from test_log_2*", + "PASSED *100%*", + "*-- live log teardown --*", + "*WARNING*log message from teardown of test_log_2*", + "*-- live log finish --*", + "*WARNING* <<<<< END <<<<<<<*", + "=* 2 passed in *=", + ] + ) + + +def test_live_logs_unknown_sections(testdir, request): + """Check that with live logging enable we are printing the correct headers during + start/setup/call/teardown/finish.""" + filename = request.node.name + ".py" + testdir.makeconftest( + """ + import pytest + import logging + + def pytest_runtest_protocol(item, nextitem): + logging.warning('Unknown Section!') + + def pytest_runtest_logstart(): + logging.warning('>>>>> START >>>>>') + + def pytest_runtest_logfinish(): + logging.warning('<<<<< END <<<<<<<') + """ + ) + + testdir.makepyfile( + """ + import pytest + import logging + + @pytest.fixture + def fix(request): + logging.warning("log message from setup of {}".format(request.node.name)) + yield + logging.warning("log message from teardown of {}".format(request.node.name)) + + def test_log_1(fix): + logging.warning("log message from test_log_1") + + """ + ) + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "*WARNING*Unknown Section*", + "{}::test_log_1 ".format(filename), + "*WARNING* >>>>> START >>>>>*", + "*-- live log setup --*", + "*WARNING*log message from setup of test_log_1*", + "*-- live log call --*", + "*WARNING*log message from test_log_1*", + "PASSED *100%*", + "*-- live log teardown --*", + "*WARNING*log message from teardown of test_log_1*", + "*WARNING* <<<<< END <<<<<<<*", + "=* 1 passed in *=", + ] + ) + + +def test_sections_single_new_line_after_test_outcome(testdir, request): + """Check that only a single new line is written between log messages during + teardown/finish.""" + filename = request.node.name + ".py" + testdir.makeconftest( + """ + import pytest + import logging + + def pytest_runtest_logstart(): + logging.warning('>>>>> START >>>>>') + + def pytest_runtest_logfinish(): + logging.warning('<<<<< END <<<<<<<') + logging.warning('<<<<< END <<<<<<<') + """ + ) + + testdir.makepyfile( + """ + import pytest + import logging + + @pytest.fixture + def fix(request): + logging.warning("log message from setup of {}".format(request.node.name)) + yield + logging.warning("log message from teardown of {}".format(request.node.name)) + logging.warning("log message from teardown of {}".format(request.node.name)) + + def test_log_1(fix): + logging.warning("log message from test_log_1") + """ + ) + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "{}::test_log_1 ".format(filename), + "*-- live log start --*", + "*WARNING* >>>>> START >>>>>*", + "*-- live log setup --*", + "*WARNING*log message from setup of test_log_1*", + "*-- live log call --*", + "*WARNING*log message from test_log_1*", + "PASSED *100%*", + "*-- live log teardown --*", + "*WARNING*log message from teardown of test_log_1*", + "*-- live log finish --*", + "*WARNING* <<<<< END <<<<<<<*", + "*WARNING* <<<<< END <<<<<<<*", + "=* 1 passed in *=", + ] + ) + assert re.search( + r"(.+)live log teardown(.+)\n(.+)WARNING(.+)\n(.+)WARNING(.+)", + result.stdout.str(), + re.MULTILINE, + ) is not None + assert re.search( + r"(.+)live log finish(.+)\n(.+)WARNING(.+)\n(.+)WARNING(.+)", + result.stdout.str(), + re.MULTILINE, + ) is not None + + +def test_log_cli_level(testdir): + # Default log file level + testdir.makepyfile( + """ + import pytest + import logging + def test_log_cli(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_cli_handler.level == logging.INFO + logging.getLogger('catchlog').debug("This log message won't be shown") + logging.getLogger('catchlog').info("This log message will be shown") + print('PASSED') + """ + ) + testdir.makeini( + """ + [pytest] + log_cli=true + """ + ) + + result = testdir.runpytest("-s", "--log-cli-level=INFO") + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines( + [ + "test_log_cli_level.py*This log message will be shown", + "PASSED", # 'PASSED' on its own line because the log message prints a new line + ] + ) + assert "This log message won't be shown" not in result.stdout.str() + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + + result = testdir.runpytest("-s", "--log-level=INFO") + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines( + [ + "test_log_cli_level.py* This log message will be shown", + "PASSED", # 'PASSED' on its own line because the log message prints a new line + ] + ) + assert "This log message won't be shown" not in result.stdout.str() + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + + +def test_log_cli_ini_level(testdir): + testdir.makeini( + """ + [pytest] + log_cli=true + log_cli_level = INFO + """ + ) + testdir.makepyfile( + """ + import pytest + import logging + def test_log_cli(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_cli_handler.level == logging.INFO + logging.getLogger('catchlog').debug("This log message won't be shown") + logging.getLogger('catchlog').info("This log message will be shown") + print('PASSED') + """ + ) + + result = testdir.runpytest("-s") + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines( + [ + "test_log_cli_ini_level.py* This log message will be shown", + "PASSED", # 'PASSED' on its own line because the log message prints a new line + ] + ) + assert "This log message won't be shown" not in result.stdout.str() + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + + +@pytest.mark.parametrize( + "cli_args", + ["", "--log-level=WARNING", "--log-file-level=WARNING", "--log-cli-level=WARNING"], +) +def test_log_cli_auto_enable(testdir, request, cli_args): + """Check that live logs are enabled if --log-level or --log-cli-level is passed on the CLI. + It should not be auto enabled if the same configs are set on the INI file. + """ + testdir.makepyfile( + """ + import pytest + import logging + + def test_log_1(): + logging.info("log message from test_log_1 not to be shown") + logging.warning("log message from test_log_1") + + """ + ) + testdir.makeini( + """ + [pytest] + log_level=INFO + log_cli_level=INFO + """ + ) + + result = testdir.runpytest(cli_args) + if cli_args == "--log-cli-level=WARNING": + result.stdout.fnmatch_lines( + [ + "*::test_log_1 ", + "*-- live log call --*", + "*WARNING*log message from test_log_1*", + "PASSED *100%*", + "=* 1 passed in *=", + ] + ) + assert "INFO" not in result.stdout.str() + else: + result.stdout.fnmatch_lines( + ["*test_log_cli_auto_enable*100%*", "=* 1 passed in *="] + ) + assert "INFO" not in result.stdout.str() + assert "WARNING" not in result.stdout.str() + + +def test_log_file_cli(testdir): + # Default log file level + testdir.makepyfile( + """ + import pytest + import logging + def test_log_file(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_file_handler.level == logging.WARNING + logging.getLogger('catchlog').info("This log message won't be shown") + logging.getLogger('catchlog').warning("This log message will be shown") + print('PASSED') + """ + ) + + log_file = testdir.tmpdir.join("pytest.log").strpath + + result = testdir.runpytest( + "-s", "--log-file={}".format(log_file), "--log-file-level=WARNING" + ) + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines(["test_log_file_cli.py PASSED"]) + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + assert os.path.isfile(log_file) + with open(log_file) as rfh: + contents = rfh.read() + assert "This log message will be shown" in contents + assert "This log message won't be shown" not in contents + + +def test_log_file_cli_level(testdir): + # Default log file level + testdir.makepyfile( + """ + import pytest + import logging + def test_log_file(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_file_handler.level == logging.INFO + logging.getLogger('catchlog').debug("This log message won't be shown") + logging.getLogger('catchlog').info("This log message will be shown") + print('PASSED') + """ + ) + + log_file = testdir.tmpdir.join("pytest.log").strpath + + result = testdir.runpytest( + "-s", "--log-file={}".format(log_file), "--log-file-level=INFO" + ) + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines(["test_log_file_cli_level.py PASSED"]) + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + assert os.path.isfile(log_file) + with open(log_file) as rfh: + contents = rfh.read() + assert "This log message will be shown" in contents + assert "This log message won't be shown" not in contents + + +def test_log_level_not_changed_by_default(testdir): + testdir.makepyfile( + """ + import logging + def test_log_file(): + assert logging.getLogger().level == logging.WARNING + """ + ) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines("* 1 passed in *") + + +def test_log_file_ini(testdir): + log_file = testdir.tmpdir.join("pytest.log").strpath + + testdir.makeini( + """ + [pytest] + log_file={} + log_file_level=WARNING + """.format( + log_file + ) + ) + testdir.makepyfile( + """ + import pytest + import logging + def test_log_file(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_file_handler.level == logging.WARNING + logging.getLogger('catchlog').info("This log message won't be shown") + logging.getLogger('catchlog').warning("This log message will be shown") + print('PASSED') + """ + ) + + result = testdir.runpytest("-s") + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines(["test_log_file_ini.py PASSED"]) + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + assert os.path.isfile(log_file) + with open(log_file) as rfh: + contents = rfh.read() + assert "This log message will be shown" in contents + assert "This log message won't be shown" not in contents + + +def test_log_file_ini_level(testdir): + log_file = testdir.tmpdir.join("pytest.log").strpath + + testdir.makeini( + """ + [pytest] + log_file={} + log_file_level = INFO + """.format( + log_file + ) + ) + testdir.makepyfile( + """ + import pytest + import logging + def test_log_file(request): + plugin = request.config.pluginmanager.getplugin('logging-plugin') + assert plugin.log_file_handler.level == logging.INFO + logging.getLogger('catchlog').debug("This log message won't be shown") + logging.getLogger('catchlog').info("This log message will be shown") + print('PASSED') + """ + ) + + result = testdir.runpytest("-s") + + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines(["test_log_file_ini_level.py PASSED"]) + + # make sure that that we get a '0' exit code for the testsuite + assert result.ret == 0 + assert os.path.isfile(log_file) + with open(log_file) as rfh: + contents = rfh.read() + assert "This log message will be shown" in contents + assert "This log message won't be shown" not in contents + + +@pytest.mark.parametrize("has_capture_manager", [True, False]) +def test_live_logging_suspends_capture(has_capture_manager, request): + """Test that capture manager is suspended when we emitting messages for live logging. + + This tests the implementation calls instead of behavior because it is difficult/impossible to do it using + ``testdir`` facilities because they do their own capturing. + + We parametrize the test to also make sure _LiveLoggingStreamHandler works correctly if no capture manager plugin + is installed. + """ + import logging + from functools import partial + from _pytest.capture import CaptureManager + from _pytest.logging import _LiveLoggingStreamHandler + + class MockCaptureManager: + calls = [] + + def suspend_global_capture(self): + self.calls.append("suspend_global_capture") + + def resume_global_capture(self): + self.calls.append("resume_global_capture") + + # sanity check + assert CaptureManager.suspend_capture_item + assert CaptureManager.resume_global_capture + + class DummyTerminal(six.StringIO): + + def section(self, *args, **kwargs): + pass + + out_file = DummyTerminal() + capture_manager = MockCaptureManager() if has_capture_manager else None + handler = _LiveLoggingStreamHandler(out_file, capture_manager) + handler.set_when("call") + + logger = logging.getLogger(__name__ + ".test_live_logging_suspends_capture") + logger.addHandler(handler) + request.addfinalizer(partial(logger.removeHandler, handler)) + + logger.critical("some message") + if has_capture_manager: + assert ( + MockCaptureManager.calls + == ["suspend_global_capture", "resume_global_capture"] + ) + else: + assert MockCaptureManager.calls == [] + assert out_file.getvalue() == "\nsome message\n" diff --git a/third_party/python/pytest/testing/python/approx.py b/third_party/python/pytest/testing/python/approx.py index d591b8ba53cc..9e25feb0b4d6 100644 --- a/third_party/python/pytest/testing/python/approx.py +++ b/third_party/python/pytest/testing/python/approx.py @@ -8,7 +8,8 @@ from pytest import approx from operator import eq, ne from decimal import Decimal from fractions import Fraction -inf, nan = float('inf'), float('nan') + +inf, nan = float("inf"), float("nan") class MyDocTestRunner(doctest.DocTestRunner): @@ -17,32 +18,47 @@ class MyDocTestRunner(doctest.DocTestRunner): doctest.DocTestRunner.__init__(self) def report_failure(self, out, test, example, got): - raise AssertionError("'{}' evaluates to '{}', not '{}'".format( - example.source.strip(), got.strip(), example.want.strip())) + raise AssertionError( + "'{}' evaluates to '{}', not '{}'".format( + example.source.strip(), got.strip(), example.want.strip() + ) + ) class TestApprox(object): def test_repr_string(self): - # for some reason in Python 2.6 it is not displaying the tolerance representation correctly - plus_minus = u'\u00b1' if sys.version_info[0] > 2 else u'+-' - tol1, tol2, infr = '1.0e-06', '2.0e-06', 'inf' - if sys.version_info[:2] == (2, 6): - tol1, tol2, infr = '???', '???', '???' - assert repr(approx(1.0)) == '1.0 {pm} {tol1}'.format(pm=plus_minus, tol1=tol1) - assert repr(approx([1.0, 2.0])) == 'approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])'.format( - pm=plus_minus, tol1=tol1, tol2=tol2) - assert repr(approx((1.0, 2.0))) == 'approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))'.format( - pm=plus_minus, tol1=tol1, tol2=tol2) - assert repr(approx(inf)) == 'inf' - assert repr(approx(1.0, rel=nan)) == '1.0 {pm} ???'.format(pm=plus_minus) - assert repr(approx(1.0, rel=inf)) == '1.0 {pm} {infr}'.format(pm=plus_minus, infr=infr) - assert repr(approx(1.0j, rel=inf)) == '1j' + plus_minus = u"\u00b1" if sys.version_info[0] > 2 else u"+-" + tol1, tol2, infr = "1.0e-06", "2.0e-06", "inf" + assert repr(approx(1.0)) == "1.0 {pm} {tol1}".format(pm=plus_minus, tol1=tol1) + assert ( + repr(approx([1.0, 2.0])) + == "approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])".format( + pm=plus_minus, tol1=tol1, tol2=tol2 + ) + ) + assert ( + repr(approx((1.0, 2.0))) + == "approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))".format( + pm=plus_minus, tol1=tol1, tol2=tol2 + ) + ) + assert repr(approx(inf)) == "inf" + assert repr(approx(1.0, rel=nan)) == "1.0 {pm} ???".format(pm=plus_minus) + assert ( + repr(approx(1.0, rel=inf)) + == "1.0 {pm} {infr}".format(pm=plus_minus, infr=infr) + ) + assert repr(approx(1.0j, rel=inf)) == "1j" # Dictionaries aren't ordered, so we need to check both orders. - assert repr(approx({'a': 1.0, 'b': 2.0})) in ( - "approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format(pm=plus_minus, tol1=tol1, tol2=tol2), - "approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format(pm=plus_minus, tol1=tol1, tol2=tol2), + assert repr(approx({"a": 1.0, "b": 2.0})) in ( + "approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format( + pm=plus_minus, tol1=tol1, tol2=tol2 + ), + "approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format( + pm=plus_minus, tol1=tol1, tol2=tol2 + ), ) def test_operator_overloading(self): @@ -59,25 +75,19 @@ class TestApprox(object): (12345, 12345.0), (0.0, -0.0), (345678, 345678), - (Decimal('1.0001'), Decimal('1.0001')), + (Decimal("1.0001"), Decimal("1.0001")), (Fraction(1, 3), Fraction(-1, -3)), ] for a, x in examples: assert a == approx(x) def test_opposite_sign(self): - examples = [ - (eq, 1e-100, -1e-100), - (ne, 1e100, -1e100), - ] + examples = [(eq, 1e-100, -1e-100), (ne, 1e100, -1e100)] for op, a, x in examples: assert op(a, approx(x)) def test_zero_tolerance(self): - within_1e10 = [ - (1.1e-100, 1e-100), - (-1.1e-100, -1e-100), - ] + within_1e10 = [(1.1e-100, 1e-100), (-1.1e-100, -1e-100)] for a, x in within_1e10: assert x == approx(x, rel=0.0, abs=0.0) assert a != approx(x, rel=0.0, abs=0.0) @@ -101,12 +111,7 @@ class TestApprox(object): def test_inf_tolerance(self): # Everything should be equal if the tolerance is infinite. - large_diffs = [ - (1, 1000), - (1e-50, 1e50), - (-1.0, -1e300), - (0.0, 10), - ] + large_diffs = [(1, 1000), (1e-50, 1e50), (-1.0, -1e300), (0.0, 10)] for a, x in large_diffs: assert a != approx(x, rel=0.0, abs=0.0) assert a == approx(x, rel=inf, abs=0.0) @@ -116,20 +121,13 @@ class TestApprox(object): def test_inf_tolerance_expecting_zero(self): # If the relative tolerance is zero but the expected value is infinite, # the actual tolerance is a NaN, which should be an error. - illegal_kwargs = [ - dict(rel=inf, abs=0.0), - dict(rel=inf, abs=inf), - ] + illegal_kwargs = [dict(rel=inf, abs=0.0), dict(rel=inf, abs=inf)] for kwargs in illegal_kwargs: with pytest.raises(ValueError): 1 == approx(0, **kwargs) def test_nan_tolerance(self): - illegal_kwargs = [ - dict(rel=nan), - dict(abs=nan), - dict(rel=nan, abs=nan), - ] + illegal_kwargs = [dict(rel=nan), dict(abs=nan), dict(rel=nan, abs=nan)] for kwargs in illegal_kwargs: with pytest.raises(ValueError): 1.1 == approx(1, **kwargs) @@ -151,8 +149,8 @@ class TestApprox(object): (eq, 1e0 + 1e-6, 1e0), (ne, 1e0 + 2e-6, 1e0), # Absolute tolerance used. - (eq, 1e-100, + 1e-106), - (eq, 1e-100, + 2e-106), + (eq, 1e-100, +1e-106), + (eq, 1e-100, +2e-106), (eq, 1e-100, 0), ] for op, a, x in examples: @@ -175,21 +173,13 @@ class TestApprox(object): assert 1e-8 + 1e-16 != approx(1e-8, rel=5e-9, abs=5e-17) def test_relative_tolerance(self): - within_1e8_rel = [ - (1e8 + 1e0, 1e8), - (1e0 + 1e-8, 1e0), - (1e-8 + 1e-16, 1e-8), - ] + within_1e8_rel = [(1e8 + 1e0, 1e8), (1e0 + 1e-8, 1e0), (1e-8 + 1e-16, 1e-8)] for a, x in within_1e8_rel: assert a == approx(x, rel=5e-8, abs=0.0) assert a != approx(x, rel=5e-9, abs=0.0) def test_absolute_tolerance(self): - within_1e8_abs = [ - (1e8 + 9e-9, 1e8), - (1e0 + 9e-9, 1e0), - (1e-8 + 9e-9, 1e-8), - ] + within_1e8_abs = [(1e8 + 9e-9, 1e8), (1e0 + 9e-9, 1e0), (1e-8 + 9e-9, 1e-8)] for a, x in within_1e8_abs: assert a == approx(x, rel=0, abs=5e-8) assert a != approx(x, rel=0, abs=5e-9) @@ -236,10 +226,7 @@ class TestApprox(object): assert op(a, approx(x, nan_ok=True)) def test_int(self): - within_1e6 = [ - (1000001, 1000000), - (-1000001, -1000000), - ] + within_1e6 = [(1000001, 1000000), (-1000001, -1000000)] for a, x in within_1e6: assert a == approx(x, rel=5e-6, abs=0) assert a != approx(x, rel=5e-7, abs=0) @@ -248,14 +235,15 @@ class TestApprox(object): def test_decimal(self): within_1e6 = [ - (Decimal('1.000001'), Decimal('1.0')), - (Decimal('-1.000001'), Decimal('-1.0')), + (Decimal("1.000001"), Decimal("1.0")), + (Decimal("-1.000001"), Decimal("-1.0")), ] for a, x in within_1e6: - assert a == approx(x, rel=Decimal('5e-6'), abs=0) - assert a != approx(x, rel=Decimal('5e-7'), abs=0) - assert approx(x, rel=Decimal('5e-6'), abs=0) == a - assert approx(x, rel=Decimal('5e-7'), abs=0) != a + assert a == approx(x) + assert a == approx(x, rel=Decimal("5e-6"), abs=0) + assert a != approx(x, rel=Decimal("5e-7"), abs=0) + assert approx(x, rel=Decimal("5e-6"), abs=0) == a + assert approx(x, rel=Decimal("5e-7"), abs=0) != a def test_fraction(self): within_1e6 = [ @@ -310,10 +298,10 @@ class TestApprox(object): assert (1, 2) != approx((1, 2, 3)) def test_dict(self): - actual = {'a': 1 + 1e-7, 'b': 2 + 1e-8} + actual = {"a": 1 + 1e-7, "b": 2 + 1e-8} # Dictionaries became ordered in python3.6, so switch up the order here # to make sure it doesn't matter. - expected = {'b': 2, 'a': 1} + expected = {"b": 2, "a": 1} # Return false if any element is outside the tolerance. assert actual == approx(expected, rel=5e-7, abs=0) @@ -322,12 +310,12 @@ class TestApprox(object): assert approx(expected, rel=5e-8, abs=0) != actual def test_dict_wrong_len(self): - assert {'a': 1, 'b': 2} != approx({'a': 1}) - assert {'a': 1, 'b': 2} != approx({'a': 1, 'c': 2}) - assert {'a': 1, 'b': 2} != approx({'a': 1, 'b': 2, 'c': 3}) + assert {"a": 1, "b": 2} != approx({"a": 1}) + assert {"a": 1, "b": 2} != approx({"a": 1, "c": 2}) + assert {"a": 1, "b": 2} != approx({"a": 1, "b": 2, "c": 3}) def test_numpy_array(self): - np = pytest.importorskip('numpy') + np = pytest.importorskip("numpy") actual = np.array([1 + 1e-7, 2 + 1e-8]) expected = np.array([1, 2]) @@ -345,7 +333,7 @@ class TestApprox(object): assert actual != approx(list(expected), rel=5e-8, abs=0) def test_numpy_array_wrong_shape(self): - np = pytest.importorskip('numpy') + np = pytest.importorskip("numpy") a12 = np.array([[1, 2]]) a21 = np.array([[1], [2]]) @@ -356,10 +344,7 @@ class TestApprox(object): def test_doctests(self): parser = doctest.DocTestParser() test = parser.get_doctest( - approx.__doc__, - {'approx': approx}, - approx.__name__, - None, None, + approx.__doc__, {"approx": approx}, approx.__name__, None, None ) runner = MyDocTestRunner() runner.run(test) @@ -369,30 +354,53 @@ class TestApprox(object): Comparing approx instances inside lists should not produce an error in the detailed diff. Integration test for issue #2111. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_foo(): assert [3] == [pytest.approx(4)] - """) - expected = '4.0e-06' - # for some reason in Python 2.6 it is not displaying the tolerance representation correctly - if sys.version_info[:2] == (2, 6): - expected = '???' + """ + ) + expected = "4.0e-06" result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*At index 0 diff: 3 != 4 * {0}'.format(expected), - '=* 1 failed in *=', - ]) + result.stdout.fnmatch_lines( + ["*At index 0 diff: 3 != 4 * {}".format(expected), "=* 1 failed in *="] + ) - @pytest.mark.parametrize('op', [ - pytest.param(operator.le, id='<='), - pytest.param(operator.lt, id='<'), - pytest.param(operator.ge, id='>='), - pytest.param(operator.gt, id='>'), - ]) + @pytest.mark.parametrize( + "op", + [ + pytest.param(operator.le, id="<="), + pytest.param(operator.lt, id="<"), + pytest.param(operator.ge, id=">="), + pytest.param(operator.gt, id=">"), + ], + ) def test_comparison_operator_type_error(self, op): """ pytest.approx should raise TypeError for operators other than == and != (#2003). """ with pytest.raises(TypeError): op(1, approx(1, rel=1e-6, abs=1e-12)) + + def test_numpy_array_with_scalar(self): + np = pytest.importorskip("numpy") + + actual = np.array([1 + 1e-7, 1 - 1e-8]) + expected = 1.0 + + assert actual == approx(expected, rel=5e-7, abs=0) + assert actual != approx(expected, rel=5e-8, abs=0) + assert approx(expected, rel=5e-7, abs=0) == actual + assert approx(expected, rel=5e-8, abs=0) != actual + + def test_numpy_scalar_with_array(self): + np = pytest.importorskip("numpy") + + actual = 1.0 + expected = np.array([1 + 1e-7, 1 - 1e-8]) + + assert actual == approx(expected, rel=5e-7, abs=0) + assert actual != approx(expected, rel=5e-8, abs=0) + assert approx(expected, rel=5e-7, abs=0) == actual + assert approx(expected, rel=5e-8, abs=0) != actual diff --git a/third_party/python/pytest/testing/python/collect.py b/third_party/python/pytest/testing/python/collect.py index 7b361a89ee39..724504b1af70 100644 --- a/third_party/python/pytest/testing/python/collect.py +++ b/third_party/python/pytest/testing/python/collect.py @@ -4,18 +4,17 @@ import sys from textwrap import dedent import _pytest._code -import py import pytest -from _pytest.main import ( - Collector, - EXIT_NOTESTSCOLLECTED +from _pytest.main import EXIT_NOTESTSCOLLECTED +from _pytest.nodes import Collector + +ignore_parametrized_marks = pytest.mark.filterwarnings( + "ignore:Applying marks directly to parameters" ) -ignore_parametrized_marks = pytest.mark.filterwarnings('ignore:Applying marks directly to parameters') - - class TestModule(object): + def test_failing_import(self, testdir): modcol = testdir.getmodulecol("import alksdjalskdjalkjals") pytest.raises(Collector.CollectError, modcol.collect) @@ -25,17 +24,19 @@ class TestModule(object): b = testdir.mkdir("b") p = a.ensure("test_whatever.py") p.pyimport() - del py.std.sys.modules['test_whatever'] + del sys.modules["test_whatever"] b.ensure("test_whatever.py") result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*import*mismatch*", - "*imported*test_whatever*", - "*%s*" % a.join("test_whatever.py"), - "*not the same*", - "*%s*" % b.join("test_whatever.py"), - "*HINT*", - ]) + result.stdout.fnmatch_lines( + [ + "*import*mismatch*", + "*imported*test_whatever*", + "*%s*" % a.join("test_whatever.py"), + "*not the same*", + "*%s*" % b.join("test_whatever.py"), + "*HINT*", + ] + ) def test_import_prepend_append(self, testdir, monkeypatch): syspath = list(sys.path) @@ -46,11 +47,16 @@ class TestModule(object): root2.ensure("x456.py") p = root2.join("test_x456.py") monkeypatch.syspath_prepend(str(root1)) - p.write(dedent("""\ + p.write( + dedent( + """\ import x456 def test(): assert x456.__file__.startswith(%r) - """ % str(root2))) + """ + % str(root2) + ) + ) with root2.as_cwd(): reprec = testdir.inline_run("--import-mode=append") reprec.assertoutcome(passed=0, failed=1) @@ -67,15 +73,17 @@ class TestModule(object): pytest.raises(ImportError, lambda: modcol.obj) def test_invalid_test_module_name(self, testdir): - a = testdir.mkdir('a') - a.ensure('test_one.part1.py') + a = testdir.mkdir("a") + a.ensure("test_one.part1.py") result = testdir.runpytest("-rw") - result.stdout.fnmatch_lines([ - "ImportError while importing test module*test_one.part1*", - "Hint: make sure your test modules/packages have valid Python names.", - ]) + result.stdout.fnmatch_lines( + [ + "ImportError while importing test module*test_one.part1*", + "Hint: make sure your test modules/packages have valid Python names.", + ] + ) - @pytest.mark.parametrize('verbose', [0, 1, 2]) + @pytest.mark.parametrize("verbose", [0, 1, 2]) def test_show_traceback_import_error(self, testdir, verbose): """Import errors when collecting modules should display the traceback (#1976). @@ -87,21 +95,25 @@ class TestModule(object): """, bar_traceback_import_error="", ) - testdir.makepyfile(""" + testdir.makepyfile( + """ import foo_traceback_import_error - """) - args = ('-v',) * verbose + """ + ) + args = ("-v",) * verbose result = testdir.runpytest(*args) - result.stdout.fnmatch_lines([ - "ImportError while importing test module*", - "Traceback:", - "*from bar_traceback_import_error import NOT_AVAILABLE", - "*cannot import name *NOT_AVAILABLE*", - ]) + result.stdout.fnmatch_lines( + [ + "ImportError while importing test module*", + "Traceback:", + "*from bar_traceback_import_error import NOT_AVAILABLE", + "*cannot import name *NOT_AVAILABLE*", + ] + ) assert result.ret == 2 stdout = result.stdout.str() - for name in ('_pytest', os.path.join('py', '_path')): + for name in ("_pytest", os.path.join("py", "_path")): if verbose == 2: assert name in stdout else: @@ -111,44 +123,54 @@ class TestModule(object): """Check test modules collected which raise ImportError with unicode messages are handled properly (#2336). """ - testdir.makepyfile(u""" + testdir.makepyfile( + u""" # -*- coding: utf-8 -*- raise ImportError(u'Something bad happened ☺') - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "ImportError while importing test module*", - "Traceback:", - "*raise ImportError*Something bad happened*", - ]) + result.stdout.fnmatch_lines( + [ + "ImportError while importing test module*", + "Traceback:", + "*raise ImportError*Something bad happened*", + ] + ) assert result.ret == 2 class TestClass(object): + def test_class_with_init_warning(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class TestClass1(object): def __init__(self): pass - """) + """ + ) result = testdir.runpytest("-rw") - result.stdout.fnmatch_lines([ - "*cannot collect test class 'TestClass1' because it has a __init__ constructor", - ]) + result.stdout.fnmatch_lines( + [ + "*cannot collect test class 'TestClass1' because it has a __init__ constructor" + ] + ) def test_class_subclassobject(self, testdir): - testdir.getmodulecol(""" + testdir.getmodulecol( + """ class test(object): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*collected 0*", - ]) + result.stdout.fnmatch_lines(["*collected 0*"]) def test_static_method(self, testdir): """Support for collecting staticmethod tests (#2528, #2699)""" - testdir.getmodulecol(""" + testdir.getmodulecol( + """ import pytest class Test(object): @staticmethod @@ -162,22 +184,14 @@ class TestClass(object): @staticmethod def test_fix(fix): assert fix == 1 - """) + """ + ) result = testdir.runpytest() - if sys.version_info < (2, 7): - # in 2.6, the code to handle static methods doesn't work - result.stdout.fnmatch_lines([ - "*collected 0 items*", - "*cannot collect static method*", - ]) - else: - result.stdout.fnmatch_lines([ - "*collected 2 items*", - "*2 passed in*", - ]) + result.stdout.fnmatch_lines(["*collected 2 items*", "*2 passed in*"]) def test_setup_teardown_class_as_classmethod(self, testdir): - testdir.makepyfile(test_mod1=""" + testdir.makepyfile( + test_mod1=""" class TestClassMethod(object): @classmethod def setup_class(cls): @@ -187,55 +201,63 @@ class TestClass(object): @classmethod def teardown_class(cls): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_issue1035_obj_has_getattr(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ class Chameleon(object): def __getattr__(self, name): return True chameleon = Chameleon() - """) + """ + ) colitems = modcol.collect() assert len(colitems) == 0 def test_issue1579_namedtuple(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import collections TestCase = collections.namedtuple('TestCase', ['a']) - """) - result = testdir.runpytest('-rw') + """ + ) + result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( "*cannot collect test class 'TestCase' " "because it has a __new__ constructor*" ) def test_issue2234_property(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class TestCase(object): @property def prop(self): raise NotImplementedError() - """) + """ + ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED class TestGenerator(object): + def test_generative_functions(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def func1(arg, arg2): assert arg == arg2 def test_gen(): yield func1, 17, 3*5 yield func1, 42, 6*7 - """) + """ + ) colitems = modcol.collect() assert len(colitems) == 1 gencol = colitems[0] @@ -244,36 +266,40 @@ class TestGenerator(object): assert len(gencolitems) == 2 assert isinstance(gencolitems[0], pytest.Function) assert isinstance(gencolitems[1], pytest.Function) - assert gencolitems[0].name == '[0]' - assert gencolitems[0].obj.__name__ == 'func1' + assert gencolitems[0].name == "[0]" + assert gencolitems[0].obj.__name__ == "func1" def test_generative_methods(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def func1(arg, arg2): assert arg == arg2 class TestGenMethods(object): def test_gen(self): yield func1, 17, 3*5 yield func1, 42, 6*7 - """) + """ + ) gencol = modcol.collect()[0].collect()[0].collect()[0] assert isinstance(gencol, pytest.Generator) gencolitems = gencol.collect() assert len(gencolitems) == 2 assert isinstance(gencolitems[0], pytest.Function) assert isinstance(gencolitems[1], pytest.Function) - assert gencolitems[0].name == '[0]' - assert gencolitems[0].obj.__name__ == 'func1' + assert gencolitems[0].name == "[0]" + assert gencolitems[0].obj.__name__ == "func1" def test_generative_functions_with_explicit_names(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def func1(arg, arg2): assert arg == arg2 def test_gen(): yield "seventeen", func1, 17, 3*5 yield "fortytwo", func1, 42, 6*7 - """) + """ + ) colitems = modcol.collect() assert len(colitems) == 1 gencol = colitems[0] @@ -283,18 +309,20 @@ class TestGenerator(object): assert isinstance(gencolitems[0], pytest.Function) assert isinstance(gencolitems[1], pytest.Function) assert gencolitems[0].name == "['seventeen']" - assert gencolitems[0].obj.__name__ == 'func1' + assert gencolitems[0].obj.__name__ == "func1" assert gencolitems[1].name == "['fortytwo']" - assert gencolitems[1].obj.__name__ == 'func1' + assert gencolitems[1].obj.__name__ == "func1" def test_generative_functions_unique_explicit_names(self, testdir): # generative - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def func(): pass def test_gen(): yield "name", func yield "name", func - """) + """ + ) colitems = modcol.collect() assert len(colitems) == 1 gencol = colitems[0] @@ -302,14 +330,16 @@ class TestGenerator(object): pytest.raises(ValueError, "gencol.collect()") def test_generative_methods_with_explicit_names(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def func1(arg, arg2): assert arg == arg2 class TestGenMethods(object): def test_gen(self): yield "m1", func1, 17, 3*5 yield "m2", func1, 42, 6*7 - """) + """ + ) gencol = modcol.collect()[0].collect()[0].collect()[0] assert isinstance(gencol, pytest.Generator) gencolitems = gencol.collect() @@ -317,12 +347,13 @@ class TestGenerator(object): assert isinstance(gencolitems[0], pytest.Function) assert isinstance(gencolitems[1], pytest.Function) assert gencolitems[0].name == "['m1']" - assert gencolitems[0].obj.__name__ == 'func1' + assert gencolitems[0].obj.__name__ == "func1" assert gencolitems[1].name == "['m2']" - assert gencolitems[1].obj.__name__ == 'func1' + assert gencolitems[1].obj.__name__ == "func1" def test_order_of_execution_generator_same_codeline(self, testdir, tmpdir): - o = testdir.makepyfile(""" + o = testdir.makepyfile( + """ from __future__ import print_function def test_generative_order_of_execution(): import py, pytest @@ -340,14 +371,16 @@ class TestGenerator(object): for i in expected_list: yield list_append, i yield assert_order_of_execution - """) + """ + ) reprec = testdir.inline_run(o) passed, skipped, failed = reprec.countoutcomes() assert passed == 7 assert not skipped and not failed def test_order_of_execution_generator_different_codeline(self, testdir): - o = testdir.makepyfile(""" + o = testdir.makepyfile( + """ from __future__ import print_function def test_generative_tests_different_codeline(): import py, pytest @@ -372,7 +405,8 @@ class TestGenerator(object): yield list_append_1 yield list_append_2 yield assert_order_of_execution - """) + """ + ) reprec = testdir.inline_run(o) passed, skipped, failed = reprec.countoutcomes() assert passed == 4 @@ -385,7 +419,8 @@ class TestGenerator(object): # that the old 1.3.4 behaviour is preserved such that all # yielded functions all share the same "self" instance that # has been used during collection. - o = testdir.makepyfile(""" + o = testdir.makepyfile( + """ setuplist = [] class TestClass(object): def setup_method(self, func): @@ -414,33 +449,38 @@ class TestGenerator(object): assert len(setuplist) == 3, len(setuplist) assert setuplist[0] == setuplist[2], setuplist assert setuplist[1] != setuplist[2], setuplist - """) - reprec = testdir.inline_run(o, '-v') + """ + ) + reprec = testdir.inline_run(o, "-v") passed, skipped, failed = reprec.countoutcomes() assert passed == 4 assert not skipped and not failed class TestFunction(object): + def test_getmodulecollector(self, testdir): item = testdir.getitem("def test_func(): pass") modcol = item.getparent(pytest.Module) assert isinstance(modcol, pytest.Module) - assert hasattr(modcol.obj, 'test_func') + assert hasattr(modcol.obj, "test_func") def test_function_as_object_instance_ignored(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class A(object): def __call__(self, tmpdir): 0/0 test_a = A() - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome() def test_function_equality(self, testdir, tmpdir): from _pytest.fixtures import FixtureManager + config = testdir.parseconfigure() session = testdir.Session(config) session._fixturemanager = FixtureManager(session) @@ -451,35 +491,40 @@ class TestFunction(object): def func2(): pass - f1 = pytest.Function(name="name", parent=session, config=config, - args=(1,), callobj=func1) + f1 = pytest.Function( + name="name", parent=session, config=config, args=(1,), callobj=func1 + ) assert f1 == f1 - f2 = pytest.Function(name="name", config=config, - callobj=func2, parent=session) + f2 = pytest.Function(name="name", config=config, callobj=func2, parent=session) assert f1 != f2 def test_issue197_parametrize_emptyset(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('arg', []) def test_function(arg): pass - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=1) def test_single_tuple_unwraps_values(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize(('arg',), [(1,)]) def test_function(arg): assert arg == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_issue213_parametrize_value_no_equal(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest class A(object): def __eq__(self, other): @@ -487,13 +532,15 @@ class TestFunction(object): @pytest.mark.parametrize('arg', [A()]) def test_function(arg): assert arg.__class__.__name__ == "A" - """) + """ + ) reprec = testdir.inline_run("--fulltrace") reprec.assertoutcome(passed=1) def test_parametrize_with_non_hashable_values(self, testdir): """Test parametrization with non-hashable values.""" - testdir.makepyfile(""" + testdir.makepyfile( + """ archival_mapping = { '1.0': {'tag': '1.0'}, '1.2.2a1': {'tag': 'release-1.2.2a1'}, @@ -505,13 +552,15 @@ class TestFunction(object): def test_archival_to_version(key, value): assert key in archival_mapping assert value == archival_mapping[key] - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(passed=2) def test_parametrize_with_non_hashable_values_indirect(self, testdir): """Test parametrization with non-hashable values with indirect parametrization.""" - testdir.makepyfile(""" + testdir.makepyfile( + """ archival_mapping = { '1.0': {'tag': '1.0'}, '1.2.2a1': {'tag': 'release-1.2.2a1'}, @@ -532,13 +581,15 @@ class TestFunction(object): def test_archival_to_version(key, value): assert key in archival_mapping assert value == archival_mapping[key] - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(passed=2) def test_parametrize_overrides_fixture(self, testdir): """Test parametrization when parameter overrides existing fixture with same name.""" - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -559,13 +610,15 @@ class TestFunction(object): def test_overridden_via_multiparam(other, value): assert other == 'foo' assert value == 'overridden' - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(passed=3) def test_parametrize_overrides_parametrized_fixture(self, testdir): """Test parametrization when parameter overrides existing parametrized fixture with same name.""" - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1, 2]) @@ -576,13 +629,15 @@ class TestFunction(object): ['overridden']) def test_overridden_via_param(value): assert value == 'overridden' - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(passed=1) @ignore_parametrized_marks def test_parametrize_with_mark(self, testdir): - items = testdir.getitems(""" + items = testdir.getitems( + """ import pytest @pytest.mark.foo @pytest.mark.parametrize('arg', [ @@ -591,18 +646,25 @@ class TestFunction(object): ]) def test_function(arg): pass - """) + """ + ) keywords = [item.keywords for item in items] - assert 'foo' in keywords[0] and 'bar' not in keywords[0] and 'baz' not in keywords[0] - assert 'foo' in keywords[1] and 'bar' in keywords[1] and 'baz' in keywords[1] + assert ( + "foo" in keywords[0] + and "bar" not in keywords[0] + and "baz" not in keywords[0] + ) + assert "foo" in keywords[1] and "bar" in keywords[1] and "baz" in keywords[1] def test_function_equality_with_callspec(self, testdir, tmpdir): - items = testdir.getitems(""" + items = testdir.getitems( + """ import pytest @pytest.mark.parametrize('arg', [1,2]) def test_function(arg): pass - """) + """ + ) assert items[0] != items[1] assert not (items[0] == items[1]) @@ -611,10 +673,12 @@ class TestFunction(object): config = item.config class MyPlugin1(object): + def pytest_pyfunc_call(self, pyfuncitem): raise ValueError class MyPlugin2(object): + def pytest_pyfunc_call(self, pyfuncitem): return True @@ -624,21 +688,24 @@ class TestFunction(object): config.hook.pytest_pyfunc_call(pyfuncitem=item) def test_multiple_parametrize(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ import pytest @pytest.mark.parametrize('x', [0, 1]) @pytest.mark.parametrize('y', [2, 3]) def test1(x, y): pass - """) + """ + ) colitems = modcol.collect() - assert colitems[0].name == 'test1[2-0]' - assert colitems[1].name == 'test1[2-1]' - assert colitems[2].name == 'test1[3-0]' - assert colitems[3].name == 'test1[3-1]' + assert colitems[0].name == "test1[2-0]" + assert colitems[1].name == "test1[2-1]" + assert colitems[2].name == "test1[3-0]" + assert colitems[3].name == "test1[3-1]" def test_issue751_multiple_parametrize_with_ids(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ import pytest @pytest.mark.parametrize('x', [0], ids=['c']) @pytest.mark.parametrize('y', [0, 1], ids=['a', 'b']) @@ -647,16 +714,18 @@ class TestFunction(object): pass def test2(self, x, y): pass - """) + """ + ) colitems = modcol.collect()[0].collect()[0].collect() - assert colitems[0].name == 'test1[a-c]' - assert colitems[1].name == 'test1[b-c]' - assert colitems[2].name == 'test2[a-c]' - assert colitems[3].name == 'test2[b-c]' + assert colitems[0].name == "test1[a-c]" + assert colitems[1].name == "test1[b-c]" + assert colitems[2].name == "test2[a-c]" + assert colitems[3].name == "test2[b-c]" @ignore_parametrized_marks def test_parametrize_skipif(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest m = pytest.mark.skipif('True') @@ -664,13 +733,15 @@ class TestFunction(object): @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_skip_if(x): assert x < 2 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('* 2 passed, 1 skipped in *') + result.stdout.fnmatch_lines("* 2 passed, 1 skipped in *") @ignore_parametrized_marks def test_parametrize_skip(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest m = pytest.mark.skip('') @@ -678,13 +749,15 @@ class TestFunction(object): @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_skip(x): assert x < 2 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('* 2 passed, 1 skipped in *') + result.stdout.fnmatch_lines("* 2 passed, 1 skipped in *") @ignore_parametrized_marks def test_parametrize_skipif_no_skip(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest m = pytest.mark.skipif('False') @@ -692,13 +765,15 @@ class TestFunction(object): @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_skipif_no_skip(x): assert x < 2 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('* 1 failed, 2 passed in *') + result.stdout.fnmatch_lines("* 1 failed, 2 passed in *") @ignore_parametrized_marks def test_parametrize_xfail(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest m = pytest.mark.xfail('True') @@ -706,13 +781,15 @@ class TestFunction(object): @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_xfail(x): assert x < 2 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('* 2 passed, 1 xfailed in *') + result.stdout.fnmatch_lines("* 2 passed, 1 xfailed in *") @ignore_parametrized_marks def test_parametrize_passed(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest m = pytest.mark.xfail('True') @@ -720,13 +797,15 @@ class TestFunction(object): @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_xfail(x): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('* 2 passed, 1 xpassed in *') + result.stdout.fnmatch_lines("* 2 passed, 1 xpassed in *") @ignore_parametrized_marks def test_parametrize_xfail_passed(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest m = pytest.mark.xfail('False') @@ -734,26 +813,32 @@ class TestFunction(object): @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_passed(x): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('* 3 passed in *') + result.stdout.fnmatch_lines("* 3 passed in *") def test_function_original_name(self, testdir): - items = testdir.getitems(""" + items = testdir.getitems( + """ import pytest @pytest.mark.parametrize('arg', [1,2]) def test_func(arg): pass - """) - assert [x.originalname for x in items] == ['test_func', 'test_func'] + """ + ) + assert [x.originalname for x in items] == ["test_func", "test_func"] class TestSorting(object): + def test_check_equality(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def test_pass(): pass def test_fail(): assert 0 - """) + """ + ) fn1 = testdir.collect_by_name(modcol, "test_pass") assert isinstance(fn1, pytest.Function) fn2 = testdir.collect_by_name(modcol, "test_pass") @@ -761,8 +846,8 @@ class TestSorting(object): assert fn1 == fn2 assert fn1 != modcol - if py.std.sys.version_info < (3, 0): - assert cmp(fn1, fn2) == 0 + if sys.version_info < (3, 0): + assert cmp(fn1, fn2) == 0 # NOQA assert hash(fn1) == hash(fn2) fn3 = testdir.collect_by_name(modcol, "test_fail") @@ -778,7 +863,8 @@ class TestSorting(object): assert modcol != fn def test_allow_sane_sorting_for_decorators(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def dec(f): g = lambda: f(2) g.place_as = f @@ -792,59 +878,76 @@ class TestSorting(object): def test_a(y): pass test_a = dec(test_a) - """) + """ + ) colitems = modcol.collect() assert len(colitems) == 2 - assert [item.name for item in colitems] == ['test_b', 'test_a'] + assert [item.name for item in colitems] == ["test_b", "test_a"] class TestConftestCustomization(object): + def test_pytest_pycollect_module(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyModule(pytest.Module): pass def pytest_pycollect_makemodule(path, parent): if path.basename == "test_xyz.py": return MyModule(path, parent) - """) + """ + ) testdir.makepyfile("def test_some(): pass") testdir.makepyfile(test_xyz="def test_func(): pass") result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "* 3 def test_traceback_error_during_import(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ x = 1 x = 2 x = 17 asd - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 out = result.stdout.str() assert "x = 1" not in out assert "x = 2" not in out - result.stdout.fnmatch_lines([ - " *asd*", - "E*NameError*", - ]) + result.stdout.fnmatch_lines([" *asd*", "E*NameError*"]) result = testdir.runpytest("--fulltrace") out = result.stdout.str() assert "x = 1" in out assert "x = 2" in out - result.stdout.fnmatch_lines([ - ">*asd*", - "E*NameError*", - ]) + result.stdout.fnmatch_lines([">*asd*", "E*NameError*"]) def test_traceback_filter_error_during_fixture_collection(self, testdir): """integration test for issue #995. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def fail_me(func): @@ -1014,15 +1133,13 @@ class TestTracebackCutting(object): def test_failing_fixture(fail_fixture): pass - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 out = result.stdout.str() assert "INTERNALERROR>" not in out - result.stdout.fnmatch_lines([ - "*ValueError: fail me*", - "* 1 error in *", - ]) + result.stdout.fnmatch_lines(["*ValueError: fail me*", "* 1 error in *"]) def test_filter_traceback_generated_code(self): """test that filter_traceback() works with the fact that @@ -1033,10 +1150,11 @@ class TestTracebackCutting(object): This fixes #995. """ from _pytest.python import filter_traceback + try: ns = {} - exec('def foo(): raise ValueError', ns) - ns['foo']() + exec("def foo(): raise ValueError", ns) + ns["foo"]() except ValueError: _, _, tb = sys.exc_info() @@ -1051,26 +1169,32 @@ class TestTracebackCutting(object): This fixes #1133. """ from _pytest.python import filter_traceback + testdir.syspathinsert() - testdir.makepyfile(filter_traceback_entry_as_str=''' + testdir.makepyfile( + filter_traceback_entry_as_str=""" def foo(): raise ValueError - ''') + """ + ) try: import filter_traceback_entry_as_str + filter_traceback_entry_as_str.foo() except ValueError: _, _, tb = sys.exc_info() - testdir.tmpdir.join('filter_traceback_entry_as_str.py').remove() + testdir.tmpdir.join("filter_traceback_entry_as_str.py").remove() tb = _pytest._code.Traceback(tb) assert isinstance(tb[-1].path, str) assert filter_traceback(tb[-1]) class TestReportInfo(object): + def test_itemreport_reportinfo(self, testdir, linecomp): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyFunction(pytest.Function): def reportinfo(self): @@ -1078,7 +1202,8 @@ class TestReportInfo(object): def pytest_pycollect_makeitem(collector, name, obj): if name == "test_func": return MyFunction(name, parent=collector) - """) + """ + ) item = testdir.getitem("def test_func(): pass") item.config.pluginmanager.getplugin("runner") assert item.location == ("ABCDE", 42, "custom") @@ -1091,11 +1216,13 @@ class TestReportInfo(object): assert modpath == "test_func" def test_class_reportinfo(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ # lineno 0 class TestClass(object): def test_hello(self): pass - """) + """ + ) classcol = testdir.collect_by_name(modcol, "TestClass") fspath, lineno, msg = classcol.reportinfo() assert fspath == modcol.fspath @@ -1103,13 +1230,15 @@ class TestReportInfo(object): assert msg == "TestClass" def test_generator_reportinfo(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ # lineno 0 def test_gen(): def check(x): assert x yield check, 3 - """) + """ + ) gencol = testdir.collect_by_name(modcol, "test_gen") fspath, lineno, modpath = gencol.reportinfo() assert fspath == modcol.fspath @@ -1135,7 +1264,8 @@ class TestReportInfo(object): def test_reportinfo_with_nasty_getattr(self, testdir): # https://github.com/pytest-dev/pytest/issues/1204 - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ # lineno 0 class TestClass(object): def __getattr__(self, name): @@ -1143,85 +1273,88 @@ class TestReportInfo(object): def test_foo(self): pass - """) + """ + ) classcol = testdir.collect_by_name(modcol, "TestClass") instance = classcol.collect()[0] fspath, lineno, msg = instance.reportinfo() def test_customized_python_discovery(testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] python_files=check_*.py python_classes=Check python_functions=check - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ def check_simple(): pass class CheckMyApp(object): def check_meth(self): pass - """) + """ + ) p2 = p.new(basename=p.basename.replace("test", "check")) p.move(p2) result = testdir.runpytest("--collect-only", "-s") - result.stdout.fnmatch_lines([ - "*check_customized*", - "*check_simple*", - "*CheckMyApp*", - "*check_meth*", - ]) + result.stdout.fnmatch_lines( + ["*check_customized*", "*check_simple*", "*CheckMyApp*", "*check_meth*"] + ) result = testdir.runpytest() assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*2 passed*", - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) def test_customized_python_discovery_functions(testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] python_functions=_test - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def _test_underscore(): pass - """) + """ + ) result = testdir.runpytest("--collect-only", "-s") - result.stdout.fnmatch_lines([ - "*_test_underscore*", - ]) + result.stdout.fnmatch_lines(["*_test_underscore*"]) result = testdir.runpytest() assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_collector_attributes(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_pycollect_makeitem(collector): assert collector.Function == pytest.Function assert collector.Class == pytest.Class assert collector.Instance == pytest.Instance assert collector.Module == pytest.Module - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_hello(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_customize_through_attributes(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyFunction(pytest.Function): pass @@ -1233,22 +1366,24 @@ def test_customize_through_attributes(testdir): def pytest_pycollect_makeitem(collector, name, obj): if name.startswith("MyTestClass"): return MyClass(name, parent=collector) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ class MyTestClass(object): def test_hello(self): pass - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*MyClass*", - "*MyInstance*", - "*MyFunction*test_hello*", - ]) + result.stdout.fnmatch_lines( + ["*MyClass*", "*MyInstance*", "*MyFunction*test_hello*"] + ) def test_unorderable_types(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class TestJoinEmpty(object): pass @@ -1258,7 +1393,8 @@ def test_unorderable_types(testdir): Test.__name__ = "TestFoo" return Test TestFoo = make_test() - """) + """ + ) result = testdir.runpytest() assert "TypeError" not in result.stdout.str() assert result.ret == EXIT_NOTESTSCOLLECTED @@ -1269,7 +1405,8 @@ def test_collect_functools_partial(testdir): Test that collection of functools.partial object works, and arguments to the wrapped functions are dealt correctly (see #811). """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import functools import pytest @@ -1303,7 +1440,8 @@ def test_collect_functools_partial(testdir): test_fail_1 = functools.partial(check2, 2) test_fail_2 = functools.partial(check3, 2) - """) + """ + ) result = testdir.inline_run() result.assertoutcome(passed=6, failed=2) @@ -1314,7 +1452,8 @@ def test_dont_collect_non_function_callable(testdir): In this case an INTERNALERROR occurred trying to report the failure of a test like this one because py test failed to get the source lines. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ class Oh(object): def __call__(self): pass @@ -1323,13 +1462,16 @@ def test_dont_collect_non_function_callable(testdir): def test_real(): pass - """) - result = testdir.runpytest('-rw') - result.stdout.fnmatch_lines([ - '*collected 1 item*', - "*cannot collect 'test_a' because it is not a function*", - '*1 passed, 1 warnings in *', - ]) + """ + ) + result = testdir.runpytest("-rw") + result.stdout.fnmatch_lines( + [ + "*collected 1 item*", + "*cannot collect 'test_a' because it is not a function*", + "*1 passed, 1 warnings in *", + ] + ) def test_class_injection_does_not_break_collection(testdir): @@ -1339,36 +1481,38 @@ def test_class_injection_does_not_break_collection(testdir): is modified during collection time, and the original method list is still used for collection. """ - testdir.makeconftest(""" + testdir.makeconftest( + """ from test_inject import TestClass def pytest_generate_tests(metafunc): TestClass.changed_var = {} - """) - testdir.makepyfile(test_inject=''' + """ + ) + testdir.makepyfile( + test_inject=''' class TestClass(object): def test_injection(self): """Test being parametrized.""" pass - ''') + ''' + ) result = testdir.runpytest() assert "RuntimeError: dictionary changed size during iteration" not in result.stdout.str() - result.stdout.fnmatch_lines(['*1 passed*']) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_syntax_error_with_non_ascii_chars(testdir): """Fix decoding issue while formatting SyntaxErrors during collection (#578) """ - testdir.makepyfile(u""" + testdir.makepyfile( + u""" # -*- coding: UTF-8 -*- ☃ - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*ERROR collecting*', - '*SyntaxError*', - '*1 error in*', - ]) + result.stdout.fnmatch_lines(["*ERROR collecting*", "*SyntaxError*", "*1 error in*"]) def test_skip_duplicates_by_default(testdir): @@ -1378,15 +1522,17 @@ def test_skip_duplicates_by_default(testdir): """ a = testdir.mkdir("a") fh = a.join("test_a.py") - fh.write(_pytest._code.Source(""" + fh.write( + _pytest._code.Source( + """ import pytest def test_real(): pass - """)) + """ + ) + ) result = testdir.runpytest(a.strpath, a.strpath) - result.stdout.fnmatch_lines([ - '*collected 1 item*', - ]) + result.stdout.fnmatch_lines(["*collected 1 item*"]) def test_keep_duplicates(testdir): @@ -1396,12 +1542,14 @@ def test_keep_duplicates(testdir): """ a = testdir.mkdir("a") fh = a.join("test_a.py") - fh.write(_pytest._code.Source(""" + fh.write( + _pytest._code.Source( + """ import pytest def test_real(): pass - """)) + """ + ) + ) result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath) - result.stdout.fnmatch_lines([ - '*collected 2 item*', - ]) + result.stdout.fnmatch_lines(["*collected 2 item*"]) diff --git a/third_party/python/pytest/testing/python/fixture.py b/third_party/python/pytest/testing/python/fixture.py index ac385c99564b..6d2bb663b6cd 100644 --- a/third_party/python/pytest/testing/python/fixture.py +++ b/third_party/python/pytest/testing/python/fixture.py @@ -2,30 +2,35 @@ from textwrap import dedent import _pytest._code import pytest -import sys from _pytest.pytester import get_public_names -from _pytest.fixtures import FixtureLookupError +from _pytest.fixtures import FixtureLookupError, FixtureRequest from _pytest import fixtures def test_getfuncargnames(): + def f(): pass + assert not fixtures.getfuncargnames(f) def g(arg): pass - assert fixtures.getfuncargnames(g) == ('arg',) + + assert fixtures.getfuncargnames(g) == ("arg",) def h(arg1, arg2="hello"): pass - assert fixtures.getfuncargnames(h) == ('arg1',) + + assert fixtures.getfuncargnames(h) == ("arg1",) def h(arg1, arg2, arg3="hello"): pass - assert fixtures.getfuncargnames(h) == ('arg1', 'arg2') + + assert fixtures.getfuncargnames(h) == ("arg1", "arg2") class A(object): + def f(self, arg1, arg2="hello"): pass @@ -33,20 +38,19 @@ def test_getfuncargnames(): def static(arg1, arg2): pass - assert fixtures.getfuncargnames(A().f) == ('arg1',) - if sys.version_info < (3, 0): - assert fixtures.getfuncargnames(A.f) == ('arg1',) - - assert fixtures.getfuncargnames(A.static, cls=A) == ('arg1', 'arg2') + assert fixtures.getfuncargnames(A().f) == ("arg1",) + assert fixtures.getfuncargnames(A.static, cls=A) == ("arg1", "arg2") class TestFillFixtures(object): + def test_fillfuncargs_exposed(self): # used by oejskit, kept for compatibility assert pytest._fillfuncargs == fixtures.fillfixtures def test_funcarg_lookupfails(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -55,17 +59,17 @@ class TestFillFixtures(object): def test_func(some): pass - """) + """ + ) result = testdir.runpytest() # "--collect-only") assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*def test_func(some)*", - "*fixture*some*not found*", - "*xyzsomething*", - ]) + result.stdout.fnmatch_lines( + ["*def test_func(some)*", "*fixture*some*not found*", "*xyzsomething*"] + ) def test_funcarg_basic(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.fixture @@ -76,15 +80,17 @@ class TestFillFixtures(object): return 42 def test_func(some, other): pass - """) + """ + ) fixtures.fillfixtures(item) del item.funcargs["request"] assert len(get_public_names(item.funcargs)) == 2 - assert item.funcargs['some'] == "test_func" - assert item.funcargs['other'] == 42 + assert item.funcargs["some"] == "test_func" + assert item.funcargs["other"] == 42 def test_funcarg_lookup_modulelevel(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -96,12 +102,14 @@ class TestFillFixtures(object): assert something == "test_method" def test_func(something): assert something == "test_func" - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_funcarg_lookup_classlevel(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest class TestClass(object): @@ -111,27 +119,34 @@ class TestFillFixtures(object): def test_method(self, something): assert something is self - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_conftest_funcargs_only_available_in_subdir(self, testdir): sub1 = testdir.mkpydir("sub1") sub2 = testdir.mkpydir("sub2") - sub1.join("conftest.py").write(_pytest._code.Source(""" + sub1.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def arg1(request): pytest.raises(Exception, "request.getfixturevalue('arg2')") - """)) - sub2.join("conftest.py").write(_pytest._code.Source(""" + """ + ) + ) + sub2.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def arg2(request): pytest.raises(Exception, "request.getfixturevalue('arg1')") - """)) + """ + ) + ) sub1.join("test_in_sub1.py").write("def test_1(arg1): pass") sub2.join("test_in_sub2.py").write("def test_2(arg2): pass") @@ -139,7 +154,8 @@ class TestFillFixtures(object): result.assert_outcomes(passed=2) def test_extend_fixture_module_class(self, testdir): - testfile = testdir.makepyfile(""" + testfile = testdir.makepyfile( + """ import pytest @pytest.fixture @@ -154,21 +170,25 @@ class TestFillFixtures(object): def test_spam(self, spam): assert spam == 'spamspam' - """) + """ + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*1 passed*"]) def test_extend_fixture_conftest_module(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def spam(): return 'spam' - """) - testfile = testdir.makepyfile(""" + """ + ) + testfile = testdir.makepyfile( + """ import pytest @pytest.fixture @@ -177,48 +197,62 @@ class TestFillFixtures(object): def test_spam(spam): assert spam == 'spamspam' - """) + """ + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*1 passed*"]) def test_extend_fixture_conftest_conftest(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def spam(): return 'spam' - """) + """ + ) pkg = testdir.mkpydir("pkg") - pkg.join("conftest.py").write(_pytest._code.Source(""" + pkg.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def spam(spam): return spam * 2 - """)) + """ + ) + ) testfile = pkg.join("test_spam.py") - testfile.write(_pytest._code.Source(""" + testfile.write( + _pytest._code.Source( + """ def test_spam(spam): assert spam == "spamspam" - """)) + """ + ) + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*1 passed*"]) def test_extend_fixture_conftest_plugin(self, testdir): - testdir.makepyfile(testplugin=""" + testdir.makepyfile( + testplugin=""" import pytest @pytest.fixture def foo(): return 7 - """) + """ + ) testdir.syspathinsert() - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest pytest_plugins = 'testplugin' @@ -226,50 +260,62 @@ class TestFillFixtures(object): @pytest.fixture def foo(foo): return foo + 7 - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_foo(foo): assert foo == 14 - """) - result = testdir.runpytest('-s') + """ + ) + result = testdir.runpytest("-s") assert result.ret == 0 def test_extend_fixture_plugin_plugin(self, testdir): # Two plugins should extend each order in loading order - testdir.makepyfile(testplugin0=""" + testdir.makepyfile( + testplugin0=""" import pytest @pytest.fixture def foo(): return 7 - """) - testdir.makepyfile(testplugin1=""" + """ + ) + testdir.makepyfile( + testplugin1=""" import pytest @pytest.fixture def foo(foo): return foo + 7 - """) + """ + ) testdir.syspathinsert() - testdir.makepyfile(""" + testdir.makepyfile( + """ pytest_plugins = ['testplugin0', 'testplugin1'] def test_foo(foo): assert foo == 14 - """) + """ + ) result = testdir.runpytest() assert result.ret == 0 def test_override_parametrized_fixture_conftest_module(self, testdir): """Test override of the parametrized fixture with non-parametrized one on the test module level.""" - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param - """) - testfile = testdir.makepyfile(""" + """ + ) + testfile = testdir.makepyfile( + """ import pytest @pytest.fixture @@ -278,7 +324,8 @@ class TestFillFixtures(object): def test_spam(spam): assert spam == 'spam' - """) + """ + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) @@ -286,26 +333,36 @@ class TestFillFixtures(object): def test_override_parametrized_fixture_conftest_conftest(self, testdir): """Test override of the parametrized fixture with non-parametrized one on the conftest level.""" - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param - """) - subdir = testdir.mkpydir('subdir') - subdir.join("conftest.py").write(_pytest._code.Source(""" + """ + ) + subdir = testdir.mkpydir("subdir") + subdir.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def spam(): return 'spam' - """)) + """ + ) + ) testfile = subdir.join("test_spam.py") - testfile.write(_pytest._code.Source(""" + testfile.write( + _pytest._code.Source( + """ def test_spam(spam): assert spam == "spam" - """)) + """ + ) + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) @@ -313,14 +370,17 @@ class TestFillFixtures(object): def test_override_non_parametrized_fixture_conftest_module(self, testdir): """Test override of the non-parametrized fixture with parametrized one on the test module level.""" - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def spam(): return 'spam' - """) - testfile = testdir.makepyfile(""" + """ + ) + testfile = testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1, 2, 3]) @@ -332,7 +392,8 @@ class TestFillFixtures(object): def test_spam(spam): assert spam == params['spam'] params['spam'] += 1 - """) + """ + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) result = testdir.runpytest(testfile) @@ -340,61 +401,83 @@ class TestFillFixtures(object): def test_override_non_parametrized_fixture_conftest_conftest(self, testdir): """Test override of the non-parametrized fixture with parametrized one on the conftest level.""" - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def spam(): return 'spam' - """) - subdir = testdir.mkpydir('subdir') - subdir.join("conftest.py").write(_pytest._code.Source(""" + """ + ) + subdir = testdir.mkpydir("subdir") + subdir.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param - """)) + """ + ) + ) testfile = subdir.join("test_spam.py") - testfile.write(_pytest._code.Source(""" + testfile.write( + _pytest._code.Source( + """ params = {'spam': 1} def test_spam(spam): assert spam == params['spam'] params['spam'] += 1 - """)) + """ + ) + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*3 passed*"]) - def test_override_autouse_fixture_with_parametrized_fixture_conftest_conftest(self, testdir): + def test_override_autouse_fixture_with_parametrized_fixture_conftest_conftest( + self, testdir + ): """Test override of the autouse fixture with parametrized one on the conftest level. This test covers the issue explained in issue 1601 """ - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture(autouse=True) def spam(): return 'spam' - """) - subdir = testdir.mkpydir('subdir') - subdir.join("conftest.py").write(_pytest._code.Source(""" + """ + ) + subdir = testdir.mkpydir("subdir") + subdir.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param - """)) + """ + ) + ) testfile = subdir.join("test_spam.py") - testfile.write(_pytest._code.Source(""" + testfile.write( + _pytest._code.Source( + """ params = {'spam': 1} def test_spam(spam): assert spam == params['spam'] params['spam'] += 1 - """)) + """ + ) + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) result = testdir.runpytest(testfile) @@ -403,25 +486,30 @@ class TestFillFixtures(object): def test_autouse_fixture_plugin(self, testdir): # A fixture from a plugin has no baseid set, which screwed up # the autouse fixture handling. - testdir.makepyfile(testplugin=""" + testdir.makepyfile( + testplugin=""" import pytest @pytest.fixture(autouse=True) def foo(request): request.function.foo = 7 - """) + """ + ) testdir.syspathinsert() - testdir.makepyfile(""" + testdir.makepyfile( + """ pytest_plugins = 'testplugin' def test_foo(request): assert request.function.foo == 7 - """) + """ + ) result = testdir.runpytest() assert result.ret == 0 def test_funcarg_lookup_error(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture @@ -435,25 +523,31 @@ class TestFillFixtures(object): @pytest.fixture def d_fixture(): pass - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_lookup_error(unknown): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ERROR at setup of test_lookup_error*", - " def test_lookup_error(unknown):*", - "E fixture 'unknown' not found", - "> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*", # sorted - "> use 'py*test --fixtures *' for help on them.", - "*1 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERROR at setup of test_lookup_error*", + " def test_lookup_error(unknown):*", + "E fixture 'unknown' not found", + "> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*", # sorted + "> use 'py*test --fixtures *' for help on them.", + "*1 error*", + ] + ) assert "INTERNAL" not in result.stdout.str() def test_fixture_excinfo_leak(self, testdir): # on python2 sys.excinfo would leak into fixture executions - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys import traceback import pytest @@ -469,31 +563,36 @@ class TestFillFixtures(object): if sys.exc_info()[0]: # python3 bug :) traceback.print_exc() assert sys.exc_info() == (None, None, None) - """) + """ + ) result = testdir.runpytest() assert result.ret == 0 class TestRequestBasic(object): + def test_request_attributes(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.fixture def something(request): pass def test_func(something): pass - """) + """ + ) req = fixtures.FixtureRequest(item) assert req.function == item.obj assert req.keywords == item.keywords - assert hasattr(req.module, 'test_func') + assert hasattr(req.module, "test_func") assert req.cls is None assert req.function.__name__ == "test_func" assert req.config == item.config assert repr(req).find(req.function.__name__) != -1 def test_request_attributes_method(self, testdir): - item, = testdir.getitems(""" + item, = testdir.getitems( + """ import pytest class TestB(object): @@ -502,13 +601,15 @@ class TestRequestBasic(object): return 1 def test_func(self, something): pass - """) + """ + ) req = item._request assert req.cls.__name__ == "TestB" assert req.instance.__class__ == req.cls def test_request_contains_funcarg_arg2fixturedefs(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ import pytest @pytest.fixture def something(request): @@ -516,22 +617,64 @@ class TestRequestBasic(object): class TestClass(object): def test_method(self, something): pass - """) + """ + ) item1, = testdir.genitems([modcol]) assert item1.name == "test_method" arg2fixturedefs = fixtures.FixtureRequest(item1)._arg2fixturedefs assert len(arg2fixturedefs) == 1 - assert arg2fixturedefs['something'][0].argname == "something" + assert arg2fixturedefs["something"][0].argname == "something" + + def test_request_garbage(self, testdir): + testdir.makepyfile( + """ + import sys + import pytest + from _pytest.fixtures import PseudoFixtureDef + import gc + + @pytest.fixture(autouse=True) + def something(request): + # this method of test doesn't work on pypy + if hasattr(sys, "pypy_version_info"): + yield + else: + original = gc.get_debug() + gc.set_debug(gc.DEBUG_SAVEALL) + gc.collect() + + yield + + gc.collect() + leaked_types = sum(1 for _ in gc.garbage + if isinstance(_, PseudoFixtureDef)) + + gc.garbage[:] = [] + + try: + assert leaked_types == 0 + finally: + gc.set_debug(original) + + def test_func(): + pass + """ + ) + reprec = testdir.inline_run() + reprec.assertoutcome(passed=1) def test_getfixturevalue_recursive(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def something(request): return 1 - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -539,14 +682,15 @@ class TestRequestBasic(object): return request.getfixturevalue("something") + 1 def test_func(something): assert something == 2 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) - @pytest.mark.parametrize( - 'getfixmethod', ('getfixturevalue', 'getfuncargvalue')) + @pytest.mark.parametrize("getfixmethod", ("getfixturevalue", "getfuncargvalue")) def test_getfixturevalue(self, testdir, getfixmethod): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest values = [2] @pytest.fixture @@ -555,9 +699,11 @@ class TestRequestBasic(object): def other(request): return values.pop() def test_func(something): pass - """) + """ + ) import contextlib - if getfixmethod == 'getfuncargvalue': + + if getfixmethod == "getfuncargvalue": warning_expectation = pytest.warns(DeprecationWarning) else: # see #1830 for a cleaner way to accomplish this @@ -586,14 +732,16 @@ class TestRequestBasic(object): assert "request" in item.funcargs def test_request_addfinalizer(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest teardownlist = [] @pytest.fixture def something(request): request.addfinalizer(lambda: teardownlist.append(1)) def test_func(something): pass - """) + """ + ) item.session._setupstate.prepare(item) pytest._fillfuncargs(item) # successively check finalization calls @@ -605,22 +753,27 @@ class TestRequestBasic(object): assert teardownlist == [1] def test_mark_as_fixture_with_prefix_and_decorator_fails(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def pytest_funcarg__marked_with_prefix_and_decorator(): pass - """) + """ + ) result = testdir.runpytest_subprocess() assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*AssertionError: fixtures cannot have*@pytest.fixture*", - "*pytest_funcarg__marked_with_prefix_and_decorator*" - ]) + result.stdout.fnmatch_lines( + [ + "*AssertionError: fixtures cannot have*@pytest.fixture*", + "*pytest_funcarg__marked_with_prefix_and_decorator*", + ] + ) def test_request_addfinalizer_failing_setup(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [1] @pytest.fixture @@ -631,12 +784,14 @@ class TestRequestBasic(object): pass def test_finalizer_ran(): assert not values - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(failed=1, passed=1) def test_request_addfinalizer_failing_setup_module(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [1, 2] @pytest.fixture(scope="module") @@ -646,13 +801,15 @@ class TestRequestBasic(object): assert 0 def test_fix(myfix): pass - """) + """ + ) reprec = testdir.inline_run("-s") mod = reprec.getcalls("pytest_runtest_setup")[0].item.module assert not mod.values def test_request_addfinalizer_partial_setup_failure(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest values = [] @pytest.fixture @@ -662,18 +819,20 @@ class TestRequestBasic(object): pass def test_second(): assert len(values) == 1 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*1 error*" # XXX the whole module collection fails - ]) + result.stdout.fnmatch_lines( + ["*1 error*"] # XXX the whole module collection fails + ) def test_request_subrequest_addfinalizer_exceptions(self, testdir): """ Ensure exceptions raised during teardown by a finalizer are suppressed until all finalizers are called, re-raising the first exception (#2440) """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] def _excepts(where): @@ -694,12 +853,12 @@ class TestRequestBasic(object): pass def test_second(): assert values == [3, 2, 1] - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*Exception: Error in excepts fixture', - '* 2 passed, 1 error in *', - ]) + result.stdout.fnmatch_lines( + ["*Exception: Error in excepts fixture", "* 2 passed, 1 error in *"] + ) def test_request_getmodulepath(self, testdir): modcol = testdir.getmodulecol("def test_somefunc(): pass") @@ -708,7 +867,8 @@ class TestRequestBasic(object): assert req.fspath == modcol.fspath def test_request_fixturenames(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest from _pytest.pytester import get_public_names @pytest.fixture() @@ -724,12 +884,14 @@ class TestRequestBasic(object): assert set(get_public_names(request.fixturenames)) == \ set(["tmpdir", "sarg", "arg1", "request", "farg", "tmpdir_factory"]) - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_funcargnames_compatattr(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): assert metafunc.funcargnames == metafunc.fixturenames @@ -741,12 +903,14 @@ class TestRequestBasic(object): def test_hello(fn): assert fn[0] == fn[1] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_setupdecorator_and_xunit(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(scope='module', autouse=True) @@ -771,47 +935,57 @@ class TestRequestBasic(object): def test_all(): assert values == ["module", "function", "class", "function", "method", "function"] - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=3) def test_fixtures_sub_subdir_normalize_sep(self, testdir): # this tests that normalization of nodeids takes place b = testdir.mkdir("tests").mkdir("unit") - b.join("conftest.py").write(_pytest._code.Source(""" + b.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def arg1(): pass - """)) + """ + ) + ) p = b.join("test_module.py") p.write("def test_func(arg1): pass") result = testdir.runpytest(p, "--fixtures") assert result.ret == 0 - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *fixtures defined*conftest* *arg1* - """) + """ + ) def test_show_fixtures_color_yes(self, testdir): testdir.makepyfile("def test_this(): assert 1") - result = testdir.runpytest('--color=yes', '--fixtures') - assert '\x1b[32mtmpdir' in result.stdout.str() + result = testdir.runpytest("--color=yes", "--fixtures") + assert "\x1b[32mtmpdir" in result.stdout.str() def test_newstyle_with_request(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture() def arg(request): pass def test_1(arg): pass - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_setupcontext_no_param(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1,2]) def arg(request): @@ -822,14 +996,17 @@ class TestRequestBasic(object): assert not hasattr(request, "param") def test_1(arg): assert arg in (1,2) - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) class TestRequestMarking(object): + def test_applymarker(self, testdir): - item1, item2 = testdir.getitems(""" + item1, item2 = testdir.getitems( + """ import pytest @pytest.fixture @@ -840,18 +1017,20 @@ class TestRequestMarking(object): pass def test_func2(self, something): pass - """) + """ + ) req1 = fixtures.FixtureRequest(item1) - assert 'xfail' not in item1.keywords + assert "xfail" not in item1.keywords req1.applymarker(pytest.mark.xfail) - assert 'xfail' in item1.keywords - assert 'skipif' not in item1.keywords + assert "xfail" in item1.keywords + assert "skipif" not in item1.keywords req1.applymarker(pytest.mark.skipif) - assert 'skipif' in item1.keywords + assert "skipif" in item1.keywords pytest.raises(ValueError, "req1.applymarker(42)") def test_accesskeywords(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture() def keywords(request): @@ -860,12 +1039,14 @@ class TestRequestMarking(object): def test_function(keywords): assert keywords["XYZ"] assert "abc" not in keywords - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_accessmarker_dynamic(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture() def keywords(request): @@ -874,8 +1055,10 @@ class TestRequestMarking(object): @pytest.fixture(scope="class", autouse=True) def marking(request): request.applymarker(pytest.mark.XYZ("hello")) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest def test_fun1(keywords): assert keywords["XYZ"] is not None @@ -883,14 +1066,17 @@ class TestRequestMarking(object): def test_fun2(keywords): assert keywords["XYZ"] is not None assert "abc" not in keywords - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) class TestRequestCachedSetup(object): + def test_request_cachedsetup_defaultmodule(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ mysetup = ["hello",].pop import pytest @@ -904,11 +1090,13 @@ class TestRequestCachedSetup(object): class TestClass(object): def test_func1a(self, something): assert something == "hello" - """) + """ + ) reprec.assertoutcome(passed=2) def test_request_cachedsetup_class(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ mysetup = ["hello", "hello2", "hello3"].pop import pytest @@ -924,7 +1112,8 @@ class TestRequestCachedSetup(object): assert something == "hello" def test_func2b(self, something): assert something == "hello" - """) + """ + ) reprec.assertoutcome(passed=4) def test_request_cachedsetup_extrakey(self, testdir): @@ -956,7 +1145,7 @@ class TestRequestCachedSetup(object): values.append("teardown") req1.cached_setup(setup, teardown, scope="function") - assert values == ['setup'] + assert values == ["setup"] # artificial call of finalizer setupstate = req1._pyfuncitem.session._setupstate setupstate._callfinalizers(item1) @@ -967,7 +1156,8 @@ class TestRequestCachedSetup(object): assert values == ["setup", "teardown", "setup", "teardown"] def test_request_cached_setup_two_args(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -978,14 +1168,14 @@ class TestRequestCachedSetup(object): return request.cached_setup(lambda: 17) def test_two_different_setups(arg1, arg2): assert arg1 != arg2 - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_request_cached_setup_getfixturevalue(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -997,14 +1187,14 @@ class TestRequestCachedSetup(object): return request.cached_setup(lambda: 10) def test_two_funcarg(arg1): assert arg1 == 11 - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_request_cached_setup_functional(self, testdir): - testdir.makepyfile(test_0=""" + testdir.makepyfile( + test_0=""" import pytest values = [] @pytest.fixture @@ -1021,19 +1211,21 @@ class TestRequestCachedSetup(object): assert something == [1] def test_list_twice(something): assert something == [1] - """) - testdir.makepyfile(test_1=""" + """ + ) + testdir.makepyfile( + test_1=""" import test_0 # should have run already def test_check_test0_has_teardown_correct(): assert test_0.values == [2] - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*3 passed*" - ]) + result.stdout.fnmatch_lines(["*3 passed*"]) def test_issue117_sessionscopeteardown(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -1045,18 +1237,18 @@ class TestRequestCachedSetup(object): return app def test_func(app): pass - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*3/x*", - "*ZeroDivisionError*", - ]) + result.stdout.fnmatch_lines(["*3/x*", "*ZeroDivisionError*"]) class TestFixtureUsages(object): + def test_noargfixturedec(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture def arg1(): @@ -1064,12 +1256,14 @@ class TestFixtureUsages(object): def test_func(arg1): assert arg1 == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_receives_funcargs(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture() def arg1(): @@ -1084,12 +1278,14 @@ class TestFixtureUsages(object): def test_all(arg1, arg2): assert arg1 == 1 assert arg2 == 2 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_receives_funcargs_scope_mismatch(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="function") def arg1(): @@ -1101,17 +1297,21 @@ class TestFixtureUsages(object): def test_add(arg2): assert arg2 == 2 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ScopeMismatch*involved factories*", - "* def arg2*", - "* def arg1*", - "*1 error*" - ]) + result.stdout.fnmatch_lines( + [ + "*ScopeMismatch*involved factories*", + "* def arg2*", + "* def arg1*", + "*1 error*", + ] + ) def test_receives_funcargs_scope_mismatch_issue660(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="function") def arg1(): @@ -1123,16 +1323,16 @@ class TestFixtureUsages(object): def test_add(arg1, arg2): assert arg2 == 2 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ScopeMismatch*involved factories*", - "* def arg2*", - "*1 error*" - ]) + result.stdout.fnmatch_lines( + ["*ScopeMismatch*involved factories*", "* def arg2*", "*1 error*"] + ) def test_invalid_scope(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="functions") def badscope(): @@ -1140,15 +1340,19 @@ class TestFixtureUsages(object): def test_nothing(badscope): pass - """) + """ + ) result = testdir.runpytest_inprocess() result.stdout.fnmatch_lines( - ("*ValueError: fixture badscope from test_invalid_scope.py has an unsupported" - " scope value 'functions'") + ( + "*ValueError: fixture badscope from test_invalid_scope.py has an unsupported" + " scope value 'functions'" + ) ) def test_funcarg_parametrized_and_used_twice(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(params=[1,2]) @@ -1163,14 +1367,14 @@ class TestFixtureUsages(object): def test_add(arg1, arg2): assert arg2 == arg1 + 1 assert len(values) == arg1 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*2 passed*" - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) def test_factory_uses_unknown_funcarg_as_dependency_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture() @@ -1183,31 +1387,37 @@ class TestFixtureUsages(object): def test_missing(call_fail): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *pytest.fixture()* *def call_fail(fail)* *pytest.fixture()* *def fail* *fixture*'missing'*not found* - """) + """ + ) def test_factory_setup_as_classes_fails(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest class arg1(object): def __init__(self, request): self.x = 1 arg1 = pytest.fixture()(arg1) - """) + """ + ) reprec = testdir.inline_run() values = reprec.getfailedcollections() assert len(values) == 1 def test_request_can_be_overridden(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture() def request(request): @@ -1215,12 +1425,14 @@ class TestFixtureUsages(object): return request def test_request(request): assert request.a == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_usefixtures_marker(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @@ -1238,41 +1450,51 @@ class TestFixtureUsages(object): assert self.hello == "world" assert len(values) == 1 pytest.mark.usefixtures("myfix")(TestClass) - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_usefixtures_ini(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] usefixtures = myfix - """) - testdir.makeconftest(""" + """ + ) + testdir.makeconftest( + """ import pytest @pytest.fixture(scope="class") def myfix(request): request.cls.hello = "world" - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ class TestClass(object): def test_one(self): assert self.hello == "world" def test_two(self): assert self.hello == "world" - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_usefixtures_seen_in_showmarkers(self, testdir): result = testdir.runpytest("--markers") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *usefixtures(fixturename1*mark tests*fixtures* - """) + """ + ) def test_request_instance_issue203(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest class TestClass(object): @@ -1282,12 +1504,14 @@ class TestFixtureUsages(object): self.arg1 = 1 def test_hello(self, setup1): assert self.arg1 == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_fixture_parametrized_with_iterator(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @@ -1307,7 +1531,8 @@ class TestFixtureUsages(object): values.append(arg) def test_2(arg2): values.append(arg2*10) - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=4) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values @@ -1319,7 +1544,8 @@ class TestFixtureManagerParseFactories(object): @pytest.fixture def testdir(self, request): testdir = request.getfixturevalue("testdir") - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture @@ -1333,11 +1559,13 @@ class TestFixtureManagerParseFactories(object): @pytest.fixture def item(request): return request._pyfuncitem - """) + """ + ) return testdir def test_parsefactories_evil_objects_issue214(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class A(object): def __call__(self): pass @@ -1346,24 +1574,28 @@ class TestFixtureManagerParseFactories(object): a = A() def test_hello(): pass - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1, failed=0) def test_parsefactories_conftest(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(item, fm): for name in ("fm", "hello", "item"): faclist = fm.getfixturedefs(name, item.nodeid) assert len(faclist) == 1 fac = faclist[0] assert fac.func.__name__ == name - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_parsefactories_conftest_and_module_and_class(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -1380,7 +1612,8 @@ class TestFixtureManagerParseFactories(object): assert faclist[0].func(item._request) == "conftest" assert faclist[1].func(item._request) == "module" assert faclist[2].func(item._request) == "class" - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) @@ -1389,28 +1622,44 @@ class TestFixtureManagerParseFactories(object): # https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html runner = testdir.mkdir("runner") package = testdir.mkdir("package") - package.join("conftest.py").write(dedent("""\ + package.join("conftest.py").write( + dedent( + """\ import pytest @pytest.fixture def one(): return 1 - """)) - package.join("test_x.py").write(dedent("""\ + """ + ) + ) + package.join("test_x.py").write( + dedent( + """\ def test_x(one): assert one == 1 - """)) + """ + ) + ) sub = package.mkdir("sub") sub.join("__init__.py").ensure() - sub.join("conftest.py").write(dedent("""\ + sub.join("conftest.py").write( + dedent( + """\ import pytest @pytest.fixture def one(): return 2 - """)) - sub.join("test_y.py").write(dedent("""\ + """ + ) + ) + sub.join("test_y.py").write( + dedent( + """\ def test_x(one): assert one == 2 - """)) + """ + ) + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) with runner.as_cwd(): @@ -1422,7 +1671,8 @@ class TestAutouseDiscovery(object): @pytest.fixture def testdir(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture(autouse=True) def perfunction(request, tmpdir): @@ -1442,23 +1692,27 @@ class TestAutouseDiscovery(object): @pytest.fixture def item(request): return request._pyfuncitem - """) + """ + ) return testdir def test_parsefactories_conftest(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from _pytest.pytester import get_public_names def test_check_setup(item, fm): autousenames = fm._getautousenames(item.nodeid) assert len(get_public_names(autousenames)) == 2 assert "perfunction2" in autousenames assert "perfunction" in autousenames - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_two_classes_separated_autouse(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest class TestA(object): values = [] @@ -1474,12 +1728,14 @@ class TestAutouseDiscovery(object): self.values.append(1) def test_setup2(self): assert self.values == [1] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_setup_at_classlevel(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest class TestClass(object): @pytest.fixture(autouse=True) @@ -1489,13 +1745,15 @@ class TestAutouseDiscovery(object): assert self.funcname == "test_method1" def test_method2(self): assert self.funcname == "test_method2" - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) @pytest.mark.xfail(reason="'enabled' feature not implemented") def test_setup_enabled_functionnode(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def enabled(parentnode, markers): @@ -1515,16 +1773,18 @@ class TestAutouseDiscovery(object): @pytest.mark.needsdb def test_func2(request): assert "db" in request.fixturenames - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) def test_callables_nocode(self, testdir): """ - a imported mock.call would break setup/factory discovery + an imported mock.call would break setup/factory discovery due to it being callable and __code__ not being a code object """ - testdir.makepyfile(""" + testdir.makepyfile( + """ class _call(tuple): def __call__(self, *k, **kw): pass @@ -1532,29 +1792,35 @@ class TestAutouseDiscovery(object): return self call = _call() - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(failed=0, passed=0) def test_autouse_in_conftests(self, testdir): a = testdir.mkdir("a") b = testdir.mkdir("a1") - conftest = testdir.makeconftest(""" + conftest = testdir.makeconftest( + """ import pytest @pytest.fixture(autouse=True) def hello(): xxx - """) + """ + ) conftest.move(a.join(conftest.basename)) a.join("test_something.py").write("def test_func(): pass") b.join("test_otherthing.py").write("def test_func(): pass") result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *1 passed*1 error* - """) + """ + ) def test_autouse_in_module_and_two_classes(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(autouse=True) @@ -1572,32 +1838,43 @@ class TestAutouseDiscovery(object): class TestA2(object): def test_world(self): assert values == ["module", "module", "A", "module"], values - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=3) class TestAutouseManagement(object): + def test_autouse_conftest_mid_directory(self, testdir): pkgdir = testdir.mkpydir("xyz123") - pkgdir.join("conftest.py").write(_pytest._code.Source(""" + pkgdir.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture(autouse=True) def app(): import sys sys._myapp = "hello" - """)) + """ + ) + ) t = pkgdir.ensure("tests", "test_app.py") - t.write(_pytest._code.Source(""" + t.write( + _pytest._code.Source( + """ import sys def test_app(): assert sys._myapp == "hello" - """)) + """ + ) + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_autouse_honored_for_yield(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(autouse=True) def tst(): @@ -1608,12 +1885,14 @@ class TestAutouseManagement(object): assert x == abs(hello) yield f, 3 yield f, -3 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_funcarg_and_setup(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(scope="module") @@ -1633,12 +1912,14 @@ class TestAutouseManagement(object): assert len(values) == 2 assert values == [1,2] assert arg == 0 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_uses_parametrized_resource(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(params=[1,2]) @@ -1657,12 +1938,14 @@ class TestAutouseManagement(object): else: 0/0 - """) + """ + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) def test_session_parametrized_function(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @@ -1682,12 +1965,14 @@ class TestAutouseManagement(object): def test_result(arg): assert len(values) == arg assert values[:arg] == [1,2][:arg] - """) + """ + ) reprec = testdir.inline_run("-v", "-s") reprec.assertoutcome(passed=4) def test_class_function_parametrization_finalization(self, testdir): - p = testdir.makeconftest(""" + p = testdir.makeconftest( + """ import pytest import pprint @@ -1706,8 +1991,10 @@ class TestAutouseManagement(object): def fin(): values.append("fin_%s%s" % (carg, farg)) request.addfinalizer(fin) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest class TestClass(object): @@ -1716,8 +2003,9 @@ class TestAutouseManagement(object): class TestClass2(object): def test_2(self): pass - """) - confcut = "--confcutdir={0}".format(testdir.tmpdir) + """ + ) + confcut = "--confcutdir={}".format(testdir.tmpdir) reprec = testdir.inline_run("-v", "-s", confcut) reprec.assertoutcome(passed=8) config = reprec.getcalls("pytest_unconfigure")[0].config @@ -1725,7 +2013,8 @@ class TestAutouseManagement(object): assert values == ["fin_a1", "fin_a2", "fin_b1", "fin_b2"] * 2 def test_scope_ordering(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(scope="function", autouse=True) @@ -1741,15 +2030,19 @@ class TestAutouseManagement(object): class TestHallo(object): def test_method(self): assert values == [1,3,2] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_parametrization_setup_teardown_ordering(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] def pytest_generate_tests(metafunc): + if metafunc.cls is None: + assert metafunc.function is test_finish if metafunc.cls is not None: metafunc.parametrize("item", [1,2], scope="class") class TestClass(object): @@ -1766,12 +2059,14 @@ class TestAutouseManagement(object): print (values) assert values == ["setup-1", "step1-1", "step2-1", "teardown-1", "setup-2", "step1-2", "step2-2", "teardown-2",] - """) - reprec = testdir.inline_run() + """ + ) + reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=5) def test_ordering_autouse_before_explicit(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @@ -1783,7 +2078,8 @@ class TestAutouseManagement(object): values.append(2) def test_hello(arg1): assert values == [1,2] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @@ -1791,7 +2087,8 @@ class TestAutouseManagement(object): @pytest.mark.parametrize("param1", ["", "params=[1]"], ids=["p00", "p01"]) @pytest.mark.parametrize("param2", ["", "params=[1]"], ids=["p10", "p11"]) def test_ordering_dependencies_torndown_first(self, testdir, param1, param2): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(%(param1)s) @@ -1807,14 +2104,18 @@ class TestAutouseManagement(object): pass def test_check(): assert values == ["new1", "new2", "fin2", "fin1"] - """ % locals()) + """ + % locals() + ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) class TestFixtureMarker(object): + def test_parametrize(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=["a", "b", "c"]) def arg(request): @@ -1824,12 +2125,14 @@ class TestFixtureMarker(object): values.append(arg) def test_result(): assert values == list("abc") - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=4) def test_multiple_parametrization_issue_736(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1,2,3]) @@ -1840,17 +2143,22 @@ class TestFixtureMarker(object): def test_issue(foo, foobar): assert foo in [1,2,3] assert foobar in [4,5,6] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=9) - @pytest.mark.parametrize('param_args', ["'fixt, val'", "'fixt,val'", "['fixt', 'val']", "('fixt', 'val')"]) + @pytest.mark.parametrize( + "param_args", + ["'fixt, val'", "'fixt,val'", "['fixt', 'val']", "('fixt', 'val')"], + ) def test_override_parametrized_fixture_issue_979(self, testdir, param_args): """Make sure a parametrized argument can override a parametrized fixture. This was a regression introduced in the fix for #736. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1, 2]) @@ -1860,12 +2168,15 @@ class TestFixtureMarker(object): @pytest.mark.parametrize(%s, [(3, 'x'), (4, 'x')]) def test_foo(fixt, val): pass - """ % param_args) + """ + % param_args + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_scope_session(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(scope="module") @@ -1882,12 +2193,14 @@ class TestFixtureMarker(object): def test3(self, arg): assert arg == 1 assert len(values) == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=3) def test_scope_session_exc(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(scope="session") @@ -1901,12 +2214,14 @@ class TestFixtureMarker(object): pass def test_last(): assert values == [1] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=2, passed=1) def test_scope_session_exc_two_fix(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] m = [] @@ -1925,7 +2240,8 @@ class TestFixtureMarker(object): def test_last(): assert values == [1] assert m == [] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=2, passed=1) @@ -1953,13 +2269,14 @@ class TestFixtureMarker(object): test_real=""" def test_last(req_list): assert req_list == [1] - """ + """, ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=2, passed=1) def test_scope_module_uses_session(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(scope="module") @@ -1976,12 +2293,14 @@ class TestFixtureMarker(object): def test3(self, arg): assert arg == 1 assert len(values) == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=3) def test_scope_module_and_finalizer(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest finalized_list = [] created_list = [] @@ -1996,7 +2315,8 @@ class TestFixtureMarker(object): @pytest.fixture def finalized(request): return len(finalized_list) - """) + """ + ) testdir.makepyfile( test_mod1=""" def test_1(arg, created, finalized): @@ -2013,23 +2333,30 @@ class TestFixtureMarker(object): def test_4(arg, created, finalized): assert created == 3 assert finalized == 2 - """) + """, + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=4) - @pytest.mark.parametrize("method", [ - 'request.getfixturevalue("arg")', - 'request.cached_setup(lambda: None, scope="function")', - ], ids=["getfixturevalue", "cached_setup"]) + @pytest.mark.parametrize( + "method", + [ + 'request.getfixturevalue("arg")', + 'request.cached_setup(lambda: None, scope="function")', + ], + ids=["getfixturevalue", "cached_setup"], + ) def test_scope_mismatch_various(self, testdir, method): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest finalized = [] created = [] @pytest.fixture(scope="function") def arg(request): pass - """) + """ + ) testdir.makepyfile( test_mod1=""" import pytest @@ -2038,20 +2365,24 @@ class TestFixtureMarker(object): %s def test_1(arg): pass - """ % method) + """ + % method + ) result = testdir.runpytest() assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*ScopeMismatch*You tried*function*session*request*", - ]) + result.stdout.fnmatch_lines( + ["*ScopeMismatch*You tried*function*session*request*"] + ) def test_register_only_with_mark(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture() def arg(): return 1 - """) + """ + ) testdir.makepyfile( test_mod1=""" import pytest @@ -2060,12 +2391,14 @@ class TestFixtureMarker(object): return arg + 1 def test_1(arg): assert arg == 2 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_parametrize_and_scope(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="module", params=["a", "b", "c"]) def arg(request): @@ -2073,7 +2406,8 @@ class TestFixtureMarker(object): values = [] def test_param(arg): values.append(arg) - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=3) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values @@ -2083,28 +2417,30 @@ class TestFixtureMarker(object): assert "c" in values def test_scope_mismatch(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture(scope="function") def arg(request): pass - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="session") def arg(arg): pass def test_mismatch(arg): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ScopeMismatch*", - "*1 error*", - ]) + result.stdout.fnmatch_lines(["*ScopeMismatch*", "*1 error*"]) def test_parametrize_separated_order(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="module", params=[1, 2]) @@ -2116,14 +2452,22 @@ class TestFixtureMarker(object): values.append(arg) def test_2(arg): values.append(arg) - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=4) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert values == [1, 1, 2, 2] def test_module_parametrized_ordering(self, testdir): - testdir.makeconftest(""" + testdir.makeini( + """ + [pytest] + console_output_style=classic + """ + ) + testdir.makeconftest( + """ import pytest @pytest.fixture(scope="session", params="s1 s2".split()) @@ -2132,13 +2476,16 @@ class TestFixtureMarker(object): @pytest.fixture(scope="module", params="m1 m2".split()) def marg(): pass - """) - testdir.makepyfile(test_mod1=""" + """ + ) + testdir.makepyfile( + test_mod1=""" def test_func(sarg): pass def test_func1(marg): pass - """, test_mod2=""" + """, + test_mod2=""" def test_func2(sarg): pass def test_func3(sarg, marg): @@ -2147,9 +2494,11 @@ class TestFixtureMarker(object): pass def test_func4(marg): pass - """) + """, + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ test_mod1.py::test_func[s1] PASSED test_mod2.py::test_func2[s1] PASSED test_mod2.py::test_func3[s1-m1] PASSED @@ -2166,10 +2515,67 @@ class TestFixtureMarker(object): test_mod2.py::test_func4[m2] PASSED test_mod1.py::test_func1[m1] PASSED test_mod1.py::test_func1[m2] PASSED - """) + """ + ) + + def test_dynamic_parametrized_ordering(self, testdir): + testdir.makeini( + """ + [pytest] + console_output_style=classic + """ + ) + testdir.makeconftest( + """ + import pytest + + def pytest_configure(config): + class DynamicFixturePlugin(object): + @pytest.fixture(scope='session', params=['flavor1', 'flavor2']) + def flavor(self, request): + return request.param + config.pluginmanager.register(DynamicFixturePlugin(), 'flavor-fixture') + + @pytest.fixture(scope='session', params=['vxlan', 'vlan']) + def encap(request): + return request.param + + @pytest.fixture(scope='session', autouse='True') + def reprovision(request, flavor, encap): + pass + """ + ) + testdir.makepyfile( + """ + def test(reprovision): + pass + def test2(reprovision): + pass + """ + ) + result = testdir.runpytest("-v") + result.stdout.fnmatch_lines( + """ + test_dynamic_parametrized_ordering.py::test[flavor1-vxlan] PASSED + test_dynamic_parametrized_ordering.py::test2[flavor1-vxlan] PASSED + test_dynamic_parametrized_ordering.py::test[flavor2-vxlan] PASSED + test_dynamic_parametrized_ordering.py::test2[flavor2-vxlan] PASSED + test_dynamic_parametrized_ordering.py::test[flavor2-vlan] PASSED + test_dynamic_parametrized_ordering.py::test2[flavor2-vlan] PASSED + test_dynamic_parametrized_ordering.py::test[flavor1-vlan] PASSED + test_dynamic_parametrized_ordering.py::test2[flavor1-vlan] PASSED + """ + ) def test_class_ordering(self, testdir): - testdir.makeconftest(""" + testdir.makeini( + """ + [pytest] + console_output_style=classic + """ + ) + testdir.makeconftest( + """ import pytest values = [] @@ -2187,8 +2593,10 @@ class TestFixtureMarker(object): def fin(): values.append("fin_%s%s" % (carg, farg)) request.addfinalizer(fin) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest class TestClass2(object): @@ -2199,25 +2607,29 @@ class TestFixtureMarker(object): class TestClass(object): def test_3(self): pass - """) + """ + ) result = testdir.runpytest("-vs") - result.stdout.fnmatch_lines(""" - test_class_ordering.py::TestClass2::test_1[1-a] PASSED - test_class_ordering.py::TestClass2::test_1[2-a] PASSED - test_class_ordering.py::TestClass2::test_2[1-a] PASSED - test_class_ordering.py::TestClass2::test_2[2-a] PASSED - test_class_ordering.py::TestClass2::test_1[1-b] PASSED - test_class_ordering.py::TestClass2::test_1[2-b] PASSED - test_class_ordering.py::TestClass2::test_2[1-b] PASSED - test_class_ordering.py::TestClass2::test_2[2-b] PASSED - test_class_ordering.py::TestClass::test_3[1-a] PASSED - test_class_ordering.py::TestClass::test_3[2-a] PASSED - test_class_ordering.py::TestClass::test_3[1-b] PASSED - test_class_ordering.py::TestClass::test_3[2-b] PASSED - """) + result.stdout.re_match_lines( + r""" + test_class_ordering.py::TestClass2::test_1\[a-1\] PASSED + test_class_ordering.py::TestClass2::test_1\[a-2\] PASSED + test_class_ordering.py::TestClass2::test_2\[a-1\] PASSED + test_class_ordering.py::TestClass2::test_2\[a-2\] PASSED + test_class_ordering.py::TestClass2::test_1\[b-1\] PASSED + test_class_ordering.py::TestClass2::test_1\[b-2\] PASSED + test_class_ordering.py::TestClass2::test_2\[b-1\] PASSED + test_class_ordering.py::TestClass2::test_2\[b-2\] PASSED + test_class_ordering.py::TestClass::test_3\[a-1\] PASSED + test_class_ordering.py::TestClass::test_3\[a-2\] PASSED + test_class_ordering.py::TestClass::test_3\[b-1\] PASSED + test_class_ordering.py::TestClass::test_3\[b-2\] PASSED + """ + ) def test_parametrize_separated_order_higher_scope_first(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="function", params=[1, 2]) @@ -2243,25 +2655,57 @@ class TestFixtureMarker(object): values.append("test3") def test_4(modarg, arg): values.append("test4") - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=12) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values expected = [ - 'create:1', 'test1', 'fin:1', 'create:2', 'test1', - 'fin:2', 'create:mod1', 'test2', 'create:1', 'test3', - 'fin:1', 'create:2', 'test3', 'fin:2', 'create:1', - 'test4', 'fin:1', 'create:2', 'test4', 'fin:2', - 'fin:mod1', 'create:mod2', 'test2', 'create:1', 'test3', - 'fin:1', 'create:2', 'test3', 'fin:2', 'create:1', - 'test4', 'fin:1', 'create:2', 'test4', 'fin:2', - 'fin:mod2'] + "create:1", + "test1", + "fin:1", + "create:2", + "test1", + "fin:2", + "create:mod1", + "test2", + "create:1", + "test3", + "fin:1", + "create:2", + "test3", + "fin:2", + "create:1", + "test4", + "fin:1", + "create:2", + "test4", + "fin:2", + "fin:mod1", + "create:mod2", + "test2", + "create:1", + "test3", + "fin:1", + "create:2", + "test3", + "fin:2", + "create:1", + "test4", + "fin:1", + "create:2", + "test4", + "fin:2", + "fin:mod2", + ] import pprint + pprint.pprint(list(zip(values, expected))) assert values == expected def test_parametrized_fixture_teardown_order(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1,2], scope="class") def param1(request): @@ -2290,15 +2734,19 @@ class TestFixtureMarker(object): def test_finish(): assert not values - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *3 passed* - """) + """ + ) assert "error" not in result.stdout.str() def test_fixture_finalizer(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest import sys @@ -2309,9 +2757,12 @@ class TestFixtureMarker(object): sys.stdout.write('Finalized') request.addfinalizer(finalize) return {} - """) + """ + ) b = testdir.mkdir("subdir") - b.join("test_overridden_fixture_finalizer.py").write(dedent(""" + b.join("test_overridden_fixture_finalizer.py").write( + dedent( + """ import pytest @pytest.fixture def browser(browser): @@ -2320,13 +2771,16 @@ class TestFixtureMarker(object): def test_browser(browser): assert browser['visited'] is True - """)) + """ + ) + ) reprec = testdir.runpytest("-s") - for test in ['test_browser']: - reprec.stdout.fnmatch_lines('*Finalized*') + for test in ["test_browser"]: + reprec.stdout.fnmatch_lines("*Finalized*") def test_class_scope_with_normal_tests(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import pytest class Box(object): @@ -2346,13 +2800,15 @@ class TestFixtureMarker(object): class Test2(object): def test_c(self, a): - assert a == 3""") + assert a == 3""" + ) reprec = testdir.inline_run(testpath) - for test in ['test_a', 'test_b', 'test_c']: + for test in ["test_a", "test_b", "test_c"]: assert reprec.matchreport(test).passed def test_request_is_clean(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(params=[1, 2]) @@ -2360,13 +2816,15 @@ class TestFixtureMarker(object): request.addfinalizer(lambda: values.append(request.param)) def test_fix(fix): pass - """) + """ + ) reprec = testdir.inline_run("-s") values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert values == [1, 2] def test_parametrize_separated_lifecycle(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @@ -2379,11 +2837,13 @@ class TestFixtureMarker(object): values.append(arg) def test_2(arg): values.append(arg) - """) + """ + ) reprec = testdir.inline_run("-vs") reprec.assertoutcome(passed=4) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values import pprint + pprint.pprint(values) # assert len(values) == 6 assert values[0] == values[1] == 1 @@ -2392,7 +2852,8 @@ class TestFixtureMarker(object): assert values[5] == "fin2" def test_parametrize_function_scoped_finalizers_called(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="function", params=[1, 2]) @@ -2409,14 +2870,16 @@ class TestFixtureMarker(object): def test_3(): assert len(values) == 8 assert values == [1, "fin1", 2, "fin2", 1, "fin1", 2, "fin2"] - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=5) @pytest.mark.issue246 @pytest.mark.parametrize("scope", ["session", "function", "module"]) def test_finalizer_order_on_parametrization(self, scope, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @@ -2443,13 +2906,16 @@ class TestFixtureMarker(object): pass def test_other(): pass - """ % {"scope": scope}) + """ + % {"scope": scope} + ) reprec = testdir.inline_run("-lvs") reprec.assertoutcome(passed=3) @pytest.mark.issue396 def test_class_scope_parametrization_ordering(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] @pytest.fixture(params=["John", "Doe"], scope="class") @@ -2467,16 +2933,30 @@ class TestFixtureMarker(object): def test_population(self, human): values.append("test_population") - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=6) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values - assert values == ["test_hello", "fin John", "test_hello", "fin Doe", - "test_name", "test_population", "fin John", - "test_name", "test_population", "fin Doe"] + assert ( + values + == [ + "test_hello", + "fin John", + "test_hello", + "fin Doe", + "test_name", + "test_population", + "fin John", + "test_name", + "test_population", + "fin Doe", + ] + ) def test_parametrize_setup_function(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="module", params=[1, 2]) @@ -2502,12 +2982,14 @@ class TestFixtureMarker(object): assert values == ["setup1", 1, 1, "fin1", "setup2", 2, 2, ] - """) + """ + ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=6) def test_fixture_marked_function_not_collected_as_test(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture def test_app(): @@ -2515,12 +2997,14 @@ class TestFixtureMarker(object): def test_something(test_app): assert test_app == 1 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_params_and_ids(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(params=[object(), object()], @@ -2530,14 +3014,14 @@ class TestFixtureMarker(object): def test_foo(fix): assert 1 - """) - res = testdir.runpytest('-v') - res.stdout.fnmatch_lines([ - '*test_foo*alpha*', - '*test_foo*beta*']) + """ + ) + res = testdir.runpytest("-v") + res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"]) def test_params_and_ids_yieldfixture(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.yield_fixture(params=[object(), object()], @@ -2547,15 +3031,15 @@ class TestFixtureMarker(object): def test_foo(fix): assert 1 - """) - res = testdir.runpytest('-v') - res.stdout.fnmatch_lines([ - '*test_foo*alpha*', - '*test_foo*beta*']) + """ + ) + res = testdir.runpytest("-v") + res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"]) @pytest.mark.issue920 def test_deterministic_fixture_collection(self, testdir, monkeypatch): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="module", @@ -2576,27 +3060,40 @@ class TestFixtureMarker(object): # sometimes we get unexpected passes. hashing B seems # to help? assert hash(B) or True - """) + """ + ) monkeypatch.setenv("PYTHONHASHSEED", "1") out1 = testdir.runpytest_subprocess("-v") monkeypatch.setenv("PYTHONHASHSEED", "2") out2 = testdir.runpytest_subprocess("-v") - out1 = [line for line in out1.outlines if line.startswith("test_deterministic_fixture_collection.py::test_foo")] - out2 = [line for line in out2.outlines if line.startswith("test_deterministic_fixture_collection.py::test_foo")] + out1 = [ + line + for line in out1.outlines + if line.startswith("test_deterministic_fixture_collection.py::test_foo") + ] + out2 = [ + line + for line in out2.outlines + if line.startswith("test_deterministic_fixture_collection.py::test_foo") + ] assert len(out1) == 12 assert out1 == out2 class TestRequestScopeAccess(object): - pytestmark = pytest.mark.parametrize(("scope", "ok", "error"), [ - ["session", "", "fspath class function module"], - ["module", "module fspath", "cls function"], - ["class", "module fspath cls", "function"], - ["function", "module fspath cls function", ""] - ]) + pytestmark = pytest.mark.parametrize( + ("scope", "ok", "error"), + [ + ["session", "", "fspath class function module"], + ["module", "module fspath", "cls function"], + ["class", "module fspath cls", "function"], + ["function", "module fspath cls function", ""], + ], + ) def test_setup(self, testdir, scope, ok, error): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope=%r, autouse=True) def myscoped(request): @@ -2609,12 +3106,15 @@ class TestRequestScopeAccess(object): assert request.config def test_func(): pass - """ % (scope, ok.split(), error.split())) + """ + % (scope, ok.split(), error.split()) + ) reprec = testdir.inline_run("-l") reprec.assertoutcome(passed=1) def test_funcarg(self, testdir, scope, ok, error): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope=%r) def arg(request): @@ -2627,31 +3127,35 @@ class TestRequestScopeAccess(object): assert request.config def test_func(arg): pass - """ % (scope, ok.split(), error.split())) + """ + % (scope, ok.split(), error.split()) + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) class TestErrors(object): + def test_subfactory_missing_funcarg(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture() def gen(qwe123): return 1 def test_something(gen): pass - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*def gen(qwe123):*", - "*fixture*qwe123*not found*", - "*1 error*", - ]) + result.stdout.fnmatch_lines( + ["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"] + ) def test_issue498_fixture_finalizer_failing(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture def fix1(request): @@ -2667,55 +3171,54 @@ class TestErrors(object): values.append(fix1) def test_3(): assert values[0] != values[1] - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *ERROR*teardown*test_1* *KeyError* *ERROR*teardown*test_2* *KeyError* *3 pass*2 error* - """) + """ + ) def test_setupfunc_missing_funcarg(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(autouse=True) def gen(qwe123): return 1 def test_something(): pass - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*def gen(qwe123):*", - "*fixture*qwe123*not found*", - "*1 error*", - ]) + result.stdout.fnmatch_lines( + ["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"] + ) class TestShowFixtures(object): + def test_funcarg_compat(self, testdir): config = testdir.parseconfigure("--funcargs") assert config.option.showfixtures def test_show_fixtures(self, testdir): result = testdir.runpytest("--fixtures") - result.stdout.fnmatch_lines([ - "*tmpdir*", - "*temporary directory*", - ]) + result.stdout.fnmatch_lines(["*tmpdir*", "*temporary directory*"]) def test_show_fixtures_verbose(self, testdir): result = testdir.runpytest("--fixtures", "-v") - result.stdout.fnmatch_lines([ - "*tmpdir*--*tmpdir.py*", - "*temporary directory*", - ]) + result.stdout.fnmatch_lines(["*tmpdir*--*tmpdir.py*", "*temporary directory*"]) def test_show_fixtures_testmodule(self, testdir): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def _arg0(): @@ -2723,39 +3226,50 @@ class TestShowFixtures(object): @pytest.fixture def arg1(): """ hello world """ - ''') + ''' + ) result = testdir.runpytest("--fixtures", p) - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *tmpdir *fixtures defined from* *arg1* *hello world* - """) + """ + ) assert "arg0" not in result.stdout.str() @pytest.mark.parametrize("testmod", [True, False]) def test_show_fixtures_conftest(self, testdir, testmod): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture def arg1(): """ hello world """ - ''') + ''' + ) if testmod: - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(): pass - """) + """ + ) result = testdir.runpytest("--fixtures") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *tmpdir* *fixtures defined from*conftest* *arg1* *hello world* - """) + """ + ) def test_show_fixtures_trimmed_doc(self, testdir): - p = testdir.makepyfile(dedent(''' + p = testdir.makepyfile( + dedent( + ''' import pytest @pytest.fixture def arg1(): @@ -2771,9 +3285,13 @@ class TestShowFixtures(object): line2 """ - ''')) + ''' + ) + ) result = testdir.runpytest("--fixtures", p) - result.stdout.fnmatch_lines(dedent(""" + result.stdout.fnmatch_lines( + dedent( + """ * fixtures defined from test_show_fixtures_trimmed_doc * arg2 line1 @@ -2782,10 +3300,14 @@ class TestShowFixtures(object): line1 line2 - """)) + """ + ) + ) def test_show_fixtures_indented_doc(self, testdir): - p = testdir.makepyfile(dedent(''' + p = testdir.makepyfile( + dedent( + ''' import pytest @pytest.fixture def fixture1(): @@ -2793,17 +3315,25 @@ class TestShowFixtures(object): line1 indented line """ - ''')) + ''' + ) + ) result = testdir.runpytest("--fixtures", p) - result.stdout.fnmatch_lines(dedent(""" + result.stdout.fnmatch_lines( + dedent( + """ * fixtures defined from test_show_fixtures_indented_doc * fixture1 line1 indented line - """)) + """ + ) + ) def test_show_fixtures_indented_doc_first_line_unindented(self, testdir): - p = testdir.makepyfile(dedent(''' + p = testdir.makepyfile( + dedent( + ''' import pytest @pytest.fixture def fixture1(): @@ -2811,41 +3341,56 @@ class TestShowFixtures(object): line2 indented line """ - ''')) + ''' + ) + ) result = testdir.runpytest("--fixtures", p) - result.stdout.fnmatch_lines(dedent(""" + result.stdout.fnmatch_lines( + dedent( + """ * fixtures defined from test_show_fixtures_indented_doc_first_line_unindented * fixture1 line1 line2 indented line - """)) + """ + ) + ) def test_show_fixtures_indented_in_class(self, testdir): - p = testdir.makepyfile(dedent(''' + p = testdir.makepyfile( + dedent( + ''' import pytest - class TestClass: + class TestClass(object): @pytest.fixture - def fixture1(): + def fixture1(self): """line1 line2 indented line """ - ''')) + ''' + ) + ) result = testdir.runpytest("--fixtures", p) - result.stdout.fnmatch_lines(dedent(""" + result.stdout.fnmatch_lines( + dedent( + """ * fixtures defined from test_show_fixtures_indented_in_class * fixture1 line1 line2 indented line - """)) + """ + ) + ) def test_show_fixtures_different_files(self, testdir): """ #833: --fixtures only shows fixtures from first file """ - testdir.makepyfile(test_a=''' + testdir.makepyfile( + test_a=''' import pytest @pytest.fixture @@ -2855,8 +3400,10 @@ class TestShowFixtures(object): def test_a(fix_a): pass - ''') - testdir.makepyfile(test_b=''' + ''' + ) + testdir.makepyfile( + test_b=''' import pytest @pytest.fixture @@ -2866,9 +3413,11 @@ class TestShowFixtures(object): def test_b(fix_b): pass - ''') + ''' + ) result = testdir.runpytest("--fixtures") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ * fixtures defined from test_a * fix_a Fixture A @@ -2876,21 +3425,27 @@ class TestShowFixtures(object): * fixtures defined from test_b * fix_b Fixture B - """) + """ + ) def test_show_fixtures_with_same_name(self, testdir): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture def arg1(): """Hello World in conftest.py""" return "Hello World" - ''') - testdir.makepyfile(''' + ''' + ) + testdir.makepyfile( + """ def test_foo(arg1): assert arg1 == "Hello World" - ''') - testdir.makepyfile(''' + """ + ) + testdir.makepyfile( + ''' import pytest @pytest.fixture def arg1(): @@ -2898,9 +3453,11 @@ class TestShowFixtures(object): return "Hi" def test_bar(arg1): assert arg1 == "Hi" - ''') + ''' + ) result = testdir.runpytest("--fixtures") - result.stdout.fnmatch_lines(''' + result.stdout.fnmatch_lines( + """ * fixtures defined from conftest * arg1 Hello World in conftest.py @@ -2908,14 +3465,25 @@ class TestShowFixtures(object): * fixtures defined from test_show_fixtures_with_same_name * arg1 Hi from test module - ''') + """ + ) + + def test_fixture_disallow_twice(self): + """Test that applying @pytest.fixture twice generates an error (#2334).""" + with pytest.raises(ValueError): + + @pytest.fixture + @pytest.fixture + def foo(): + pass -@pytest.mark.parametrize('flavor', ['fixture', 'yield_fixture']) +@pytest.mark.parametrize("flavor", ["fixture", "yield_fixture"]) class TestContextManagerFixtureFuncs(object): def test_simple(self, testdir, flavor): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.{flavor} def arg1(): @@ -2927,19 +3495,25 @@ class TestContextManagerFixtureFuncs(object): def test_2(arg1): print ("test2 %s" % arg1) assert 0 - """.format(flavor=flavor)) + """.format( + flavor=flavor + ) + ) result = testdir.runpytest("-s") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *setup* *test1 1* *teardown* *setup* *test2 1* *teardown* - """) + """ + ) def test_scoped(self, testdir, flavor): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.{flavor}(scope="module") def arg1(): @@ -2950,17 +3524,23 @@ class TestContextManagerFixtureFuncs(object): print ("test1 %s" % arg1) def test_2(arg1): print ("test2 %s" % arg1) - """.format(flavor=flavor)) + """.format( + flavor=flavor + ) + ) result = testdir.runpytest("-s") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *setup* *test1 1* *test2 1* *teardown* - """) + """ + ) def test_setup_exception(self, testdir, flavor): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.{flavor}(scope="module") def arg1(): @@ -2968,15 +3548,21 @@ class TestContextManagerFixtureFuncs(object): yield 1 def test_1(arg1): pass - """.format(flavor=flavor)) + """.format( + flavor=flavor + ) + ) result = testdir.runpytest("-s") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *pytest.fail*setup* *1 error* - """) + """ + ) def test_teardown_exception(self, testdir, flavor): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.{flavor}(scope="module") def arg1(): @@ -2984,15 +3570,21 @@ class TestContextManagerFixtureFuncs(object): pytest.fail("teardown") def test_1(arg1): pass - """.format(flavor=flavor)) + """.format( + flavor=flavor + ) + ) result = testdir.runpytest("-s") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *pytest.fail*teardown* *1 passed*1 error* - """) + """ + ) def test_yields_more_than_one(self, testdir, flavor): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.{flavor}(scope="module") def arg1(): @@ -3000,29 +3592,40 @@ class TestContextManagerFixtureFuncs(object): yield 2 def test_1(arg1): pass - """.format(flavor=flavor)) + """.format( + flavor=flavor + ) + ) result = testdir.runpytest("-s") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *fixture function* *test_yields*:2* - """) + """ + ) def test_custom_name(self, testdir, flavor): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.{flavor}(name='meow') def arg1(): return 'mew' def test_1(meow): print(meow) - """.format(flavor=flavor)) + """.format( + flavor=flavor + ) + ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines("*mew*") class TestParameterizedSubRequest(object): + def test_call_from_fixture(self, testdir): - testfile = testdir.makepyfile(""" + testfile = testdir.makepyfile( + """ import pytest @pytest.fixture(params=[0, 1, 2]) @@ -3035,20 +3638,26 @@ class TestParameterizedSubRequest(object): def test_foo(request, get_named_fixture): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ E*Failed: The requested fixture has no parameter defined for the current test. E* E*Requested fixture 'fix_with_param' defined in: - E*{0}:4 + E*{}:4 E*Requested here: - E*{1}:9 + E*{}:9 *1 error* - """.format(testfile.basename, testfile.basename)) + """.format( + testfile.basename, testfile.basename + ) + ) def test_call_from_test(self, testdir): - testfile = testdir.makepyfile(""" + testfile = testdir.makepyfile( + """ import pytest @pytest.fixture(params=[0, 1, 2]) @@ -3057,71 +3666,346 @@ class TestParameterizedSubRequest(object): def test_foo(request): request.getfixturevalue('fix_with_param') - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ E*Failed: The requested fixture has no parameter defined for the current test. E* E*Requested fixture 'fix_with_param' defined in: - E*{0}:4 + E*{}:4 E*Requested here: - E*{1}:8 + E*{}:8 *1 failed* - """.format(testfile.basename, testfile.basename)) + """.format( + testfile.basename, testfile.basename + ) + ) def test_external_fixture(self, testdir): - conffile = testdir.makeconftest(""" + conffile = testdir.makeconftest( + """ import pytest @pytest.fixture(params=[0, 1, 2]) def fix_with_param(request): return request.param - """) + """ + ) - testfile = testdir.makepyfile(""" + testfile = testdir.makepyfile( + """ def test_foo(request): request.getfixturevalue('fix_with_param') - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ E*Failed: The requested fixture has no parameter defined for the current test. E* E*Requested fixture 'fix_with_param' defined in: - E*{0}:4 + E*{}:4 E*Requested here: - E*{1}:2 + E*{}:2 *1 failed* - """.format(conffile.basename, testfile.basename)) + """.format( + conffile.basename, testfile.basename + ) + ) def test_non_relative_path(self, testdir): - tests_dir = testdir.mkdir('tests') - fixdir = testdir.mkdir('fixtures') + tests_dir = testdir.mkdir("tests") + fixdir = testdir.mkdir("fixtures") fixfile = fixdir.join("fix.py") - fixfile.write(_pytest._code.Source(""" + fixfile.write( + _pytest._code.Source( + """ import pytest @pytest.fixture(params=[0, 1, 2]) def fix_with_param(request): return request.param - """)) + """ + ) + ) testfile = tests_dir.join("test_foos.py") - testfile.write(_pytest._code.Source(""" + testfile.write( + _pytest._code.Source( + """ from fix import fix_with_param def test_foo(request): request.getfixturevalue('fix_with_param') - """)) + """ + ) + ) tests_dir.chdir() testdir.syspathinsert(fixdir) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ E*Failed: The requested fixture has no parameter defined for the current test. E* E*Requested fixture 'fix_with_param' defined in: - E*{0}:5 + E*{}:5 E*Requested here: - E*{1}:5 + E*{}:5 *1 failed* - """.format(fixfile.strpath, testfile.basename)) + """.format( + fixfile.strpath, testfile.basename + ) + ) + + +def test_pytest_fixture_setup_and_post_finalizer_hook(testdir): + testdir.makeconftest( + """ + from __future__ import print_function + def pytest_fixture_setup(fixturedef, request): + print('ROOT setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) + def pytest_fixture_post_finalizer(fixturedef, request): + print('ROOT finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) + """ + ) + testdir.makepyfile( + **{ + "tests/conftest.py": """ + from __future__ import print_function + def pytest_fixture_setup(fixturedef, request): + print('TESTS setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) + def pytest_fixture_post_finalizer(fixturedef, request): + print('TESTS finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) + """, + "tests/test_hooks.py": """ + from __future__ import print_function + import pytest + + @pytest.fixture() + def my_fixture(): + return 'some' + + def test_func(my_fixture): + print('TEST test_func') + assert my_fixture == 'some' + """, + } + ) + result = testdir.runpytest("-s") + assert result.ret == 0 + result.stdout.fnmatch_lines( + [ + "*TESTS setup hook called for my_fixture from test_func*", + "*ROOT setup hook called for my_fixture from test_func*", + "*TEST test_func*", + "*TESTS finalizer hook called for my_fixture from test_func*", + "*ROOT finalizer hook called for my_fixture from test_func*", + ] + ) + + +class TestScopeOrdering(object): + """Class of tests that ensure fixtures are ordered based on their scopes (#2405)""" + + @pytest.mark.parametrize("use_mark", [True, False]) + def test_func_closure_module_auto(self, testdir, use_mark): + """Semantically identical to the example posted in #2405 when ``use_mark=True``""" + testdir.makepyfile( + """ + import pytest + + @pytest.fixture(scope='module', autouse={autouse}) + def m1(): pass + + if {use_mark}: + pytestmark = pytest.mark.usefixtures('m1') + + @pytest.fixture(scope='function', autouse=True) + def f1(): pass + + def test_func(m1): + pass + """.format( + autouse=not use_mark, use_mark=use_mark + ) + ) + items, _ = testdir.inline_genitems() + request = FixtureRequest(items[0]) + assert request.fixturenames == "m1 f1".split() + + def test_func_closure_with_native_fixtures(self, testdir, monkeypatch): + """Sanity check that verifies the order returned by the closures and the actual fixture execution order: + The execution order may differ because of fixture inter-dependencies. + """ + monkeypatch.setattr(pytest, "FIXTURE_ORDER", [], raising=False) + testdir.makepyfile( + """ + import pytest + + FIXTURE_ORDER = pytest.FIXTURE_ORDER + + @pytest.fixture(scope="session") + def s1(): + FIXTURE_ORDER.append('s1') + + @pytest.fixture(scope="module") + def m1(): + FIXTURE_ORDER.append('m1') + + @pytest.fixture(scope='session') + def my_tmpdir_factory(): + FIXTURE_ORDER.append('my_tmpdir_factory') + + @pytest.fixture + def my_tmpdir(my_tmpdir_factory): + FIXTURE_ORDER.append('my_tmpdir') + + @pytest.fixture + def f1(my_tmpdir): + FIXTURE_ORDER.append('f1') + + @pytest.fixture + def f2(): + FIXTURE_ORDER.append('f2') + + def test_foo(f1, m1, f2, s1): pass + """ + ) + items, _ = testdir.inline_genitems() + request = FixtureRequest(items[0]) + # order of fixtures based on their scope and position in the parameter list + assert request.fixturenames == "s1 my_tmpdir_factory m1 f1 f2 my_tmpdir".split() + testdir.runpytest() + # actual fixture execution differs: dependent fixtures must be created first ("my_tmpdir") + assert pytest.FIXTURE_ORDER == "s1 my_tmpdir_factory m1 my_tmpdir f1 f2".split() + + def test_func_closure_module(self, testdir): + testdir.makepyfile( + """ + import pytest + + @pytest.fixture(scope='module') + def m1(): pass + + @pytest.fixture(scope='function') + def f1(): pass + + def test_func(f1, m1): + pass + """ + ) + items, _ = testdir.inline_genitems() + request = FixtureRequest(items[0]) + assert request.fixturenames == "m1 f1".split() + + def test_func_closure_scopes_reordered(self, testdir): + """Test ensures that fixtures are ordered by scope regardless of the order of the parameters, although + fixtures of same scope keep the declared order + """ + testdir.makepyfile( + """ + import pytest + + @pytest.fixture(scope='session') + def s1(): pass + + @pytest.fixture(scope='module') + def m1(): pass + + @pytest.fixture(scope='function') + def f1(): pass + + @pytest.fixture(scope='function') + def f2(): pass + + class Test: + + @pytest.fixture(scope='class') + def c1(cls): pass + + def test_func(self, f2, f1, c1, m1, s1): + pass + """ + ) + items, _ = testdir.inline_genitems() + request = FixtureRequest(items[0]) + assert request.fixturenames == "s1 m1 c1 f2 f1".split() + + def test_func_closure_same_scope_closer_root_first(self, testdir): + """Auto-use fixtures of same scope are ordered by closer-to-root first""" + testdir.makeconftest( + """ + import pytest + + @pytest.fixture(scope='module', autouse=True) + def m_conf(): pass + """ + ) + testdir.makepyfile( + **{ + "sub/conftest.py": """ + import pytest + + @pytest.fixture(scope='module', autouse=True) + def m_sub(): pass + """, + "sub/test_func.py": """ + import pytest + + @pytest.fixture(scope='module', autouse=True) + def m_test(): pass + + @pytest.fixture(scope='function') + def f1(): pass + + def test_func(m_test, f1): + pass + """, + } + ) + items, _ = testdir.inline_genitems() + request = FixtureRequest(items[0]) + assert request.fixturenames == "m_conf m_sub m_test f1".split() + + def test_func_closure_all_scopes_complex(self, testdir): + """Complex test involving all scopes and mixing autouse with normal fixtures""" + testdir.makeconftest( + """ + import pytest + + @pytest.fixture(scope='session') + def s1(): pass + """ + ) + testdir.makepyfile( + """ + import pytest + + @pytest.fixture(scope='module', autouse=True) + def m1(): pass + + @pytest.fixture(scope='module') + def m2(s1): pass + + @pytest.fixture(scope='function') + def f1(): pass + + @pytest.fixture(scope='function') + def f2(): pass + + class Test: + + @pytest.fixture(scope='class', autouse=True) + def c1(self): + pass + + def test_func(self, f2, f1, m2): + pass + """ + ) + items, _ = testdir.inline_genitems() + request = FixtureRequest(items[0]) + assert request.fixturenames == "s1 m1 m2 c1 f2 f1".split() diff --git a/third_party/python/pytest/testing/python/integration.py b/third_party/python/pytest/testing/python/integration.py index 6ea29fa98b9d..f348fdc29fd2 100644 --- a/third_party/python/pytest/testing/python/integration.py +++ b/third_party/python/pytest/testing/python/integration.py @@ -4,8 +4,10 @@ from _pytest import runner class TestOEJSKITSpecials(object): + def test_funcarg_non_pycollectobj(self, testdir): # rough jstests usage - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_pycollect_makeitem(collector, name, obj): if name == "MyClass": @@ -13,25 +15,29 @@ class TestOEJSKITSpecials(object): class MyCollector(pytest.Collector): def reportinfo(self): return self.fspath, 3, "xyz" - """) - modcol = testdir.getmodulecol(""" + """ + ) + modcol = testdir.getmodulecol( + """ import pytest @pytest.fixture def arg1(request): return 42 class MyClass(object): pass - """) + """ + ) # this hook finds funcarg factories rep = runner.collect_one_node(collector=modcol) clscol = rep.result[0] clscol.obj = lambda arg1: None clscol.funcargs = {} pytest._fillfuncargs(clscol) - assert clscol.funcargs['arg1'] == 42 + assert clscol.funcargs["arg1"] == 42 def test_autouse_fixture(self, testdir): # rough jstests usage - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_pycollect_makeitem(collector, name, obj): if name == "MyClass": @@ -39,8 +45,10 @@ class TestOEJSKITSpecials(object): class MyCollector(pytest.Collector): def reportinfo(self): return self.fspath, 3, "xyz" - """) - modcol = testdir.getmodulecol(""" + """ + ) + modcol = testdir.getmodulecol( + """ import pytest @pytest.fixture(autouse=True) def hello(): @@ -50,7 +58,8 @@ class TestOEJSKITSpecials(object): return 42 class MyClass(object): pass - """) + """ + ) # this hook finds funcarg factories rep = runner.collect_one_node(modcol) clscol = rep.result[0] @@ -61,6 +70,7 @@ class TestOEJSKITSpecials(object): def test_wrapped_getfslineno(): + def func(): pass @@ -72,12 +82,14 @@ def test_wrapped_getfslineno(): @wrap def wrapped_func(x, y, z): pass + fs, lineno = python.getfslineno(wrapped_func) fs2, lineno2 = python.getfslineno(wrap) assert lineno > lineno2, "getfslineno does not unwrap correctly" class TestMockDecoration(object): + def test_wrapped_getfuncargnames(self): from _pytest.compat import getfuncargnames @@ -96,12 +108,17 @@ class TestMockDecoration(object): values = getfuncargnames(f) assert values == ("x",) + @pytest.mark.xfail( + strict=False, reason="getfuncargnames breaks if mock is imported" + ) def test_wrapped_getfuncargnames_patching(self): from _pytest.compat import getfuncargnames def wrap(f): + def func(): pass + func.__wrapped__ = f func.patchings = ["qwe"] return func @@ -115,7 +132,8 @@ class TestMockDecoration(object): def test_unittest_mock(self, testdir): pytest.importorskip("unittest.mock") - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest.mock class T(unittest.TestCase): @unittest.mock.patch("os.path.abspath") @@ -123,13 +141,15 @@ class TestMockDecoration(object): import os os.path.abspath("hello") abspath.assert_any_call("hello") - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_unittest_mock_and_fixture(self, testdir): pytest.importorskip("unittest.mock") - testdir.makepyfile(""" + testdir.makepyfile( + """ import os.path import unittest.mock import pytest @@ -143,13 +163,39 @@ class TestMockDecoration(object): def test_hello(inject_me): import os os.path.abspath("hello") - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) + def test_unittest_mock_and_pypi_mock(self, testdir): + pytest.importorskip("unittest.mock") + pytest.importorskip("mock", "1.0.1") + testdir.makepyfile( + """ + import mock + import unittest.mock + class TestBoth(object): + @unittest.mock.patch("os.path.abspath") + def test_hello(self, abspath): + import os + os.path.abspath("hello") + abspath.assert_any_call("hello") + + @mock.patch("os.path.abspath") + def test_hello_mock(self, abspath): + import os + os.path.abspath("hello") + abspath.assert_any_call("hello") + """ + ) + reprec = testdir.inline_run() + reprec.assertoutcome(passed=2) + def test_mock(self, testdir): pytest.importorskip("mock", "1.0.1") - testdir.makepyfile(""" + testdir.makepyfile( + """ import os import unittest import mock @@ -169,17 +215,20 @@ class TestMockDecoration(object): os.path.normpath(os.path.abspath("hello")) normpath.assert_any_call("this") assert os.path.basename("123") == "mock_basename" - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) calls = reprec.getcalls("pytest_runtest_logreport") - funcnames = [call.report.location[2] for call in calls - if call.report.when == "call"] + funcnames = [ + call.report.location[2] for call in calls if call.report.when == "call" + ] assert funcnames == ["T.test_hello", "test_someting"] def test_mock_sorting(self, testdir): pytest.importorskip("mock", "1.0.1") - testdir.makepyfile(""" + testdir.makepyfile( + """ import os import mock @@ -192,7 +241,8 @@ class TestMockDecoration(object): @mock.patch("os.path.abspath") def test_three(abspath): pass - """) + """ + ) reprec = testdir.inline_run() calls = reprec.getreports("pytest_runtest_logreport") calls = [x for x in calls if x.when == "call"] @@ -201,7 +251,8 @@ class TestMockDecoration(object): def test_mock_double_patch_issue473(self, testdir): pytest.importorskip("mock", "1.0.1") - testdir.makepyfile(""" + testdir.makepyfile( + """ from mock import patch from pytest import mark @@ -211,20 +262,25 @@ class TestMockDecoration(object): class TestSimple(object): def test_simple_thing(self, mock_path, mock_getcwd): pass - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) class TestReRunTests(object): + def test_rerun(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ from _pytest.runner import runtestprotocol def pytest_runtest_protocol(item, nextitem): runtestprotocol(item, log=False, nextitem=nextitem) runtestprotocol(item, log=True, nextitem=nextitem) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest count = 0 req = None @@ -237,36 +293,46 @@ class TestReRunTests(object): count += 1 def test_fix(fix): pass - """) + """ + ) result = testdir.runpytest("-s") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *fix count 0* *fix count 1* - """) - result.stdout.fnmatch_lines(""" + """ + ) + result.stdout.fnmatch_lines( + """ *2 passed* - """) + """ + ) def test_pytestconfig_is_session_scoped(): from _pytest.fixtures import pytestconfig + assert pytestconfig._pytestfixturefunction.scope == "session" class TestNoselikeTestAttribute(object): + def test_module_with_global_test(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ __test__ = False def test_hello(): pass - """) + """ + ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls def test_class_and_method(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ __test__ = True def test_func(): pass @@ -276,14 +342,16 @@ class TestNoselikeTestAttribute(object): __test__ = False def test_method(self): pass - """) + """ + ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls def test_unittest_class(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest class TC(unittest.TestCase): def test_1(self): @@ -292,7 +360,8 @@ class TestNoselikeTestAttribute(object): __test__ = False def test_2(self): pass - """) + """ + ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() call = reprec.getcalls("pytest_collection_modifyitems")[0] @@ -306,7 +375,8 @@ class TestNoselikeTestAttribute(object): RPC wrapper), we shouldn't assume this meant "__test__ = True". """ # https://github.com/pytest-dev/pytest/issues/1204 - testdir.makepyfile(""" + testdir.makepyfile( + """ class MetaModel(type): def __getattr__(cls, key): @@ -322,7 +392,8 @@ class TestNoselikeTestAttribute(object): def test_blah(self): pass - """) + """ + ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() call = reprec.getcalls("pytest_collection_modifyitems")[0] @@ -333,7 +404,8 @@ class TestNoselikeTestAttribute(object): class TestParameterize(object): def test_idfn_marker(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def idfn(param): @@ -347,15 +419,14 @@ class TestParameterize(object): @pytest.mark.parametrize('a,b', [(0, 2), (1, 2)], ids=idfn) def test_params(a, b): pass - """) - res = testdir.runpytest('--collect-only') - res.stdout.fnmatch_lines([ - "*spam-2*", - "*ham-2*", - ]) + """ + ) + res = testdir.runpytest("--collect-only") + res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) def test_idfn_fixture(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def idfn(param): @@ -376,9 +447,7 @@ class TestParameterize(object): def test_params(a, b): pass - """) - res = testdir.runpytest('--collect-only') - res.stdout.fnmatch_lines([ - "*spam-2*", - "*ham-2*", - ]) + """ + ) + res = testdir.runpytest("--collect-only") + res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) diff --git a/third_party/python/pytest/testing/python/metafunc.py b/third_party/python/pytest/testing/python/metafunc.py index 2acdf669e619..e181d3131f57 100644 --- a/third_party/python/pytest/testing/python/metafunc.py +++ b/third_party/python/pytest/testing/python/metafunc.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import re import sys - +import attr import _pytest._code import py import pytest @@ -14,7 +14,8 @@ PY3 = sys.version_info >= (3, 0) class TestMetafunc(object): - def Metafunc(self, func): + + def Metafunc(self, func, config=None): # the unit tests of this class check if things work correctly # on the funcarg level, so we don't need a full blown # initiliazation @@ -24,39 +25,52 @@ class TestMetafunc(object): def __init__(self, names): self.names_closure = names + @attr.s + class DefinitionMock(object): + obj = attr.ib() + names = fixtures.getfuncargnames(func) fixtureinfo = FixtureInfo(names) - return python.Metafunc(func, fixtureinfo, None) + definition = DefinitionMock(func) + return python.Metafunc(definition, fixtureinfo, config) def test_no_funcargs(self, testdir): + def function(): pass + metafunc = self.Metafunc(function) assert not metafunc.fixturenames repr(metafunc._calls) def test_function_basic(self): + def func(arg1, arg2="qwe"): pass + metafunc = self.Metafunc(func) assert len(metafunc.fixturenames) == 1 - assert 'arg1' in metafunc.fixturenames + assert "arg1" in metafunc.fixturenames assert metafunc.function is func assert metafunc.cls is None def test_addcall_no_args(self): + def func(arg1): pass + metafunc = self.Metafunc(func) metafunc.addcall() assert len(metafunc._calls) == 1 call = metafunc._calls[0] assert call.id == "0" - assert not hasattr(call, 'param') + assert not hasattr(call, "param") def test_addcall_id(self): + def func(arg1): pass + metafunc = self.Metafunc(func) pytest.raises(ValueError, "metafunc.addcall(id=None)") @@ -69,8 +83,10 @@ class TestMetafunc(object): assert metafunc._calls[1].id == "2" def test_addcall_param(self): + def func(arg1): pass + metafunc = self.Metafunc(func) class obj(object): @@ -85,6 +101,7 @@ class TestMetafunc(object): assert metafunc._calls[2].getparam("arg1") == 1 def test_addcall_funcargs(self): + def func(x): pass @@ -97,13 +114,15 @@ class TestMetafunc(object): metafunc.addcall(funcargs={"x": 3}) pytest.raises(pytest.fail.Exception, "metafunc.addcall({'xyz': 0})") assert len(metafunc._calls) == 2 - assert metafunc._calls[0].funcargs == {'x': 2} - assert metafunc._calls[1].funcargs == {'x': 3} - assert not hasattr(metafunc._calls[1], 'param') + assert metafunc._calls[0].funcargs == {"x": 2} + assert metafunc._calls[1].funcargs == {"x": 3} + assert not hasattr(metafunc._calls[1], "param") def test_parametrize_error(self): + def func(x, y): pass + metafunc = self.Metafunc(func) metafunc.parametrize("x", [1, 2]) pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6])) @@ -113,56 +132,84 @@ class TestMetafunc(object): pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6])) def test_parametrize_bad_scope(self, testdir): + def func(x): pass + metafunc = self.Metafunc(func) try: - metafunc.parametrize("x", [1], scope='doggy') + metafunc.parametrize("x", [1], scope="doggy") except ValueError as ve: assert "has an unsupported scope value 'doggy'" in str(ve) def test_parametrize_and_id(self): + def func(x, y): pass + metafunc = self.Metafunc(func) - metafunc.parametrize("x", [1, 2], ids=['basic', 'advanced']) + metafunc.parametrize("x", [1, 2], ids=["basic", "advanced"]) metafunc.parametrize("y", ["abc", "def"]) ids = [x.id for x in metafunc._calls] assert ids == ["basic-abc", "basic-def", "advanced-abc", "advanced-def"] def test_parametrize_and_id_unicode(self): """Allow unicode strings for "ids" parameter in Python 2 (##1905)""" + def func(x): pass + metafunc = self.Metafunc(func) - metafunc.parametrize("x", [1, 2], ids=[u'basic', u'advanced']) + metafunc.parametrize("x", [1, 2], ids=[u"basic", u"advanced"]) ids = [x.id for x in metafunc._calls] assert ids == [u"basic", u"advanced"] def test_parametrize_with_wrong_number_of_ids(self, testdir): + def func(x, y): pass + metafunc = self.Metafunc(func) - pytest.raises(ValueError, lambda: - metafunc.parametrize("x", [1, 2], ids=['basic'])) + pytest.raises( + ValueError, lambda: metafunc.parametrize("x", [1, 2], ids=["basic"]) + ) - pytest.raises(ValueError, lambda: - metafunc.parametrize(("x", "y"), [("abc", "def"), - ("ghi", "jkl")], ids=["one"])) + pytest.raises( + ValueError, + lambda: metafunc.parametrize( + ("x", "y"), [("abc", "def"), ("ghi", "jkl")], ids=["one"] + ), + ) @pytest.mark.issue510 def test_parametrize_empty_list(self): + def func(y): pass - metafunc = self.Metafunc(func) + + class MockConfig(object): + + def getini(self, name): + return "" + + @property + def hook(self): + return self + + def pytest_make_parametrize_id(self, **kw): + pass + + metafunc = self.Metafunc(func, MockConfig()) metafunc.parametrize("y", []) - assert 'skip' in metafunc._calls[0].keywords + assert "skip" == metafunc._calls[0].marks[0].name def test_parametrize_with_userobjects(self): + def func(x, y): pass + metafunc = self.Metafunc(func) class A(object): @@ -176,14 +223,18 @@ class TestMetafunc(object): assert metafunc._calls[3].id == "x1-b" @hypothesis.given(strategies.text() | strategies.binary()) + @hypothesis.settings( + deadline=400.0 + ) # very close to std deadline and CI boxes are not reliable in CPU power def test_idval_hypothesis(self, value): from _pytest.python import _idval - escaped = _idval(value, 'a', 6, None) + + escaped = _idval(value, "a", 6, None) assert isinstance(escaped, str) if PY3: - escaped.encode('ascii') + escaped.encode("ascii") else: - escaped.decode('ascii') + escaped.decode("ascii") def test_unicode_idval(self): """This tests that Unicode strings outside the ASCII character set get @@ -192,31 +243,20 @@ class TestMetafunc(object): """ from _pytest.python import _idval + values = [ + (u"", ""), + (u"ascii", "ascii"), + (u"ação", "a\\xe7\\xe3o"), + (u"josé@blah.com", "jos\\xe9@blah.com"), ( - u'', - '' - ), - ( - u'ascii', - 'ascii' - ), - ( - u'ação', - 'a\\xe7\\xe3o' - ), - ( - u'josé@blah.com', - 'jos\\xe9@blah.com' - ), - ( - u'δοκ.ιμή@παράδειγμα.δοκιμή', - '\\u03b4\\u03bf\\u03ba.\\u03b9\\u03bc\\u03ae@\\u03c0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3' - '\\u03bc\\u03b1.\\u03b4\\u03bf\\u03ba\\u03b9\\u03bc\\u03ae' + u"δοκ.ιμή@παράδειγμα.δοκιμή", + "\\u03b4\\u03bf\\u03ba.\\u03b9\\u03bc\\u03ae@\\u03c0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3" + "\\u03bc\\u03b1.\\u03b4\\u03bf\\u03ba\\u03b9\\u03bc\\u03ae", ), ] for val, expected in values: - assert _idval(val, 'a', 6, None) == expected + assert _idval(val, "a", 6, None) == expected def test_bytes_idval(self): """unittest for the expected behavior to obtain ids for parametrized @@ -226,69 +266,99 @@ class TestMetafunc(object): - python3: bytes objects are always escaped using "binary escape". """ from _pytest.python import _idval + values = [ - (b'', ''), - (b'\xc3\xb4\xff\xe4', '\\xc3\\xb4\\xff\\xe4'), - (b'ascii', 'ascii'), - (u'αρά'.encode('utf-8'), '\\xce\\xb1\\xcf\\x81\\xce\\xac'), + (b"", ""), + (b"\xc3\xb4\xff\xe4", "\\xc3\\xb4\\xff\\xe4"), + (b"ascii", "ascii"), + (u"αρά".encode("utf-8"), "\\xce\\xb1\\xcf\\x81\\xce\\xac"), ] for val, expected in values: - assert _idval(val, 'a', 6, None) == expected + assert _idval(val, "a", 6, None) == expected + + def test_class_or_function_idval(self): + """unittest for the expected behavior to obtain ids for parametrized + values that are classes or functions: their __name__. + """ + from _pytest.python import _idval + + class TestClass(object): + pass + + def test_function(): + pass + + values = [(TestClass, "TestClass"), (test_function, "test_function")] + for val, expected in values: + assert _idval(val, "a", 6, None) == expected @pytest.mark.issue250 def test_idmaker_autoname(self): from _pytest.python import idmaker - result = idmaker(("a", "b"), [pytest.param("string", 1.0), - pytest.param("st-ring", 2.0)]) + + result = idmaker( + ("a", "b"), [pytest.param("string", 1.0), pytest.param("st-ring", 2.0)] + ) assert result == ["string-1.0", "st-ring-2.0"] - result = idmaker(("a", "b"), [pytest.param(object(), 1.0), - pytest.param(object(), object())]) + result = idmaker( + ("a", "b"), [pytest.param(object(), 1.0), pytest.param(object(), object())] + ) assert result == ["a0-1.0", "a1-b1"] # unicode mixing, issue250 result = idmaker( - (py.builtin._totext("a"), "b"), - [pytest.param({}, b'\xc3\xb4')]) - assert result == ['a0-\\xc3\\xb4'] + (py.builtin._totext("a"), "b"), [pytest.param({}, b"\xc3\xb4")] + ) + assert result == ["a0-\\xc3\\xb4"] def test_idmaker_with_bytes_regex(self): from _pytest.python import idmaker - result = idmaker(("a"), [pytest.param(re.compile(b'foo'), 1.0)]) + + result = idmaker(("a"), [pytest.param(re.compile(b"foo"), 1.0)]) assert result == ["foo"] def test_idmaker_native_strings(self): from _pytest.python import idmaker + totext = py.builtin._totext - result = idmaker(("a", "b"), [ - pytest.param(1.0, -1.1), - pytest.param(2, -202), - pytest.param("three", "three hundred"), - pytest.param(True, False), - pytest.param(None, None), - pytest.param(re.compile('foo'), re.compile('bar')), - pytest.param(str, int), - pytest.param(list("six"), [66, 66]), - pytest.param(set([7]), set("seven")), - pytest.param(tuple("eight"), (8, -8, 8)), - pytest.param(b'\xc3\xb4', b"name"), - pytest.param(b'\xc3\xb4', totext("other")), - ]) - assert result == ["1.0--1.1", - "2--202", - "three-three hundred", - "True-False", - "None-None", - "foo-bar", - "str-int", - "a7-b7", - "a8-b8", - "a9-b9", - "\\xc3\\xb4-name", - "\\xc3\\xb4-other", - ] + result = idmaker( + ("a", "b"), + [ + pytest.param(1.0, -1.1), + pytest.param(2, -202), + pytest.param("three", "three hundred"), + pytest.param(True, False), + pytest.param(None, None), + pytest.param(re.compile("foo"), re.compile("bar")), + pytest.param(str, int), + pytest.param(list("six"), [66, 66]), + pytest.param({7}, set("seven")), + pytest.param(tuple("eight"), (8, -8, 8)), + pytest.param(b"\xc3\xb4", b"name"), + pytest.param(b"\xc3\xb4", totext("other")), + ], + ) + assert ( + result + == [ + "1.0--1.1", + "2--202", + "three-three hundred", + "True-False", + "None-None", + "foo-bar", + "str-int", + "a7-b7", + "a8-b8", + "a9-b9", + "\\xc3\\xb4-name", + "\\xc3\\xb4-other", + ] + ) def test_idmaker_enum(self): from _pytest.python import idmaker + enum = pytest.importorskip("enum") e = enum.Enum("Foo", "one, two") result = idmaker(("a", "b"), [pytest.param(e.one, e.two)]) @@ -302,31 +372,34 @@ class TestMetafunc(object): if isinstance(val, Exception): return repr(val) - result = idmaker(("a", "b"), [ - pytest.param(10.0, IndexError()), - pytest.param(20, KeyError()), - pytest.param("three", [1, 2, 3]), - ], idfn=ids) - assert result == ["10.0-IndexError()", - "20-KeyError()", - "three-b2", - ] + result = idmaker( + ("a", "b"), + [ + pytest.param(10.0, IndexError()), + pytest.param(20, KeyError()), + pytest.param("three", [1, 2, 3]), + ], + idfn=ids, + ) + assert result == ["10.0-IndexError()", "20-KeyError()", "three-b2"] @pytest.mark.issue351 def test_idmaker_idfn_unique_names(self): from _pytest.python import idmaker def ids(val): - return 'a' + return "a" - result = idmaker(("a", "b"), [pytest.param(10.0, IndexError()), - pytest.param(20, KeyError()), - pytest.param("three", [1, 2, 3]), - ], idfn=ids) - assert result == ["a-a0", - "a-a1", - "a-a2", - ] + result = idmaker( + ("a", "b"), + [ + pytest.param(10.0, IndexError()), + pytest.param(20, KeyError()), + pytest.param("three", [1, 2, 3]), + ], + idfn=ids, + ) + assert result == ["a-a0", "a-a1", "a-a2"] @pytest.mark.issue351 def test_idmaker_idfn_exception(self): @@ -341,33 +414,41 @@ class TestMetafunc(object): rec = WarningsRecorder() with rec: - idmaker(("a", "b"), [ - pytest.param(10.0, IndexError()), - pytest.param(20, KeyError()), - pytest.param("three", [1, 2, 3]), - ], idfn=ids) + idmaker( + ("a", "b"), + [ + pytest.param(10.0, IndexError()), + pytest.param(20, KeyError()), + pytest.param("three", [1, 2, 3]), + ], + idfn=ids, + ) - assert [str(i.message) for i in rec.list] == [ - "Raised while trying to determine id of parameter a at position 0." - "\nUpdate your code as this will raise an error in pytest-4.0.", - "Raised while trying to determine id of parameter b at position 0." - "\nUpdate your code as this will raise an error in pytest-4.0.", - "Raised while trying to determine id of parameter a at position 1." - "\nUpdate your code as this will raise an error in pytest-4.0.", - "Raised while trying to determine id of parameter b at position 1." - "\nUpdate your code as this will raise an error in pytest-4.0.", - "Raised while trying to determine id of parameter a at position 2." - "\nUpdate your code as this will raise an error in pytest-4.0.", - "Raised while trying to determine id of parameter b at position 2." - "\nUpdate your code as this will raise an error in pytest-4.0.", - ] + assert ( + [str(i.message) for i in rec.list] + == [ + "Raised while trying to determine id of parameter a at position 0." + "\nUpdate your code as this will raise an error in pytest-4.0.", + "Raised while trying to determine id of parameter b at position 0." + "\nUpdate your code as this will raise an error in pytest-4.0.", + "Raised while trying to determine id of parameter a at position 1." + "\nUpdate your code as this will raise an error in pytest-4.0.", + "Raised while trying to determine id of parameter b at position 1." + "\nUpdate your code as this will raise an error in pytest-4.0.", + "Raised while trying to determine id of parameter a at position 2." + "\nUpdate your code as this will raise an error in pytest-4.0.", + "Raised while trying to determine id of parameter b at position 2." + "\nUpdate your code as this will raise an error in pytest-4.0.", + ] + ) def test_parametrize_ids_exception(self, testdir): """ :param testdir: the instance of Testdir class, a temporary test directory. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def ids(arg): @@ -376,54 +457,67 @@ class TestMetafunc(object): @pytest.mark.parametrize("arg", ["a", "b"], ids=ids) def test_foo(arg): pass - """) + """ + ) with pytest.warns(DeprecationWarning): result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "", - " ", - " ", - ]) + result.stdout.fnmatch_lines( + [ + "", + " ", + " ", + ] + ) def test_idmaker_with_ids(self): from _pytest.python import idmaker - result = idmaker(("a", "b"), [pytest.param(1, 2), - pytest.param(3, 4)], - ids=["a", None]) + + result = idmaker( + ("a", "b"), [pytest.param(1, 2), pytest.param(3, 4)], ids=["a", None] + ) assert result == ["a", "3-4"] def test_idmaker_with_paramset_id(self): from _pytest.python import idmaker - result = idmaker(("a", "b"), [pytest.param(1, 2, id="me"), - pytest.param(3, 4, id="you")], - ids=["a", None]) + + result = idmaker( + ("a", "b"), + [pytest.param(1, 2, id="me"), pytest.param(3, 4, id="you")], + ids=["a", None], + ) assert result == ["me", "you"] def test_idmaker_with_ids_unique_names(self): from _pytest.python import idmaker - result = idmaker(("a"), map(pytest.param, [1, 2, 3, 4, 5]), - ids=["a", "a", "b", "c", "b"]) + + result = idmaker( + ("a"), map(pytest.param, [1, 2, 3, 4, 5]), ids=["a", "a", "b", "c", "b"] + ) assert result == ["a0", "a1", "b0", "c", "b1"] def test_addcall_and_parametrize(self): + def func(x, y): pass + metafunc = self.Metafunc(func) - metafunc.addcall({'x': 1}) - metafunc.parametrize('y', [2, 3]) + metafunc.addcall({"x": 1}) + metafunc.parametrize("y", [2, 3]) assert len(metafunc._calls) == 2 - assert metafunc._calls[0].funcargs == {'x': 1, 'y': 2} - assert metafunc._calls[1].funcargs == {'x': 1, 'y': 3} + assert metafunc._calls[0].funcargs == {"x": 1, "y": 2} + assert metafunc._calls[1].funcargs == {"x": 1, "y": 3} assert metafunc._calls[0].id == "0-2" assert metafunc._calls[1].id == "0-3" @pytest.mark.issue714 def test_parametrize_indirect(self): + def func(x, y): pass + metafunc = self.Metafunc(func) - metafunc.parametrize('x', [1], indirect=True) - metafunc.parametrize('y', [2, 3], indirect=True) + metafunc.parametrize("x", [1], indirect=True) + metafunc.parametrize("y", [2, 3], indirect=True) assert len(metafunc._calls) == 2 assert metafunc._calls[0].funcargs == {} assert metafunc._calls[1].funcargs == {} @@ -432,29 +526,35 @@ class TestMetafunc(object): @pytest.mark.issue714 def test_parametrize_indirect_list(self): + def func(x, y): pass + metafunc = self.Metafunc(func) - metafunc.parametrize('x, y', [('a', 'b')], indirect=['x']) - assert metafunc._calls[0].funcargs == dict(y='b') - assert metafunc._calls[0].params == dict(x='a') + metafunc.parametrize("x, y", [("a", "b")], indirect=["x"]) + assert metafunc._calls[0].funcargs == dict(y="b") + assert metafunc._calls[0].params == dict(x="a") @pytest.mark.issue714 def test_parametrize_indirect_list_all(self): + def func(x, y): pass + metafunc = self.Metafunc(func) - metafunc.parametrize('x, y', [('a', 'b')], indirect=['x', 'y']) + metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "y"]) assert metafunc._calls[0].funcargs == {} - assert metafunc._calls[0].params == dict(x='a', y='b') + assert metafunc._calls[0].params == dict(x="a", y="b") @pytest.mark.issue714 def test_parametrize_indirect_list_empty(self): + def func(x, y): pass + metafunc = self.Metafunc(func) - metafunc.parametrize('x, y', [('a', 'b')], indirect=[]) - assert metafunc._calls[0].funcargs == dict(x='a', y='b') + metafunc.parametrize("x, y", [("a", "b")], indirect=[]) + assert metafunc._calls[0].funcargs == dict(x="a", y="b") assert metafunc._calls[0].params == {} @pytest.mark.issue714 @@ -468,7 +568,8 @@ class TestMetafunc(object): :param testdir: the instance of Testdir class, a temporary test directory. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='function') def x(request): @@ -480,20 +581,20 @@ class TestMetafunc(object): def test_simple(x,y): assert len(x) == 3 assert len(y) == 1 - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*test_simple*a-b*", - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*test_simple*a-b*", "*1 passed*"]) @pytest.mark.issue714 def test_parametrize_indirect_list_error(self, testdir): + def func(x, y): pass + metafunc = self.Metafunc(func) with pytest.raises(ValueError): - metafunc.parametrize('x, y', [('a', 'b')], indirect=['x', 'z']) + metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "z"]) @pytest.mark.issue714 def test_parametrize_uses_no_fixture_error_indirect_false(self, testdir): @@ -502,21 +603,22 @@ class TestMetafunc(object): fixtures in their test function, rather than silently ignoring this and letting the test potentially pass. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=False) def test_simple(x): assert len(x) == 3 - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*uses no argument 'y'*", - ]) + result.stdout.fnmatch_lines(["*uses no argument 'y'*"]) @pytest.mark.issue714 def test_parametrize_uses_no_fixture_error_indirect_true(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='function') def x(request): @@ -528,15 +630,15 @@ class TestMetafunc(object): @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=True) def test_simple(x): assert len(x) == 3 - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*uses no fixture 'y'*", - ]) + result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) @pytest.mark.issue714 def test_parametrize_indirect_uses_no_fixture_error_indirect_string(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='function') def x(request): @@ -545,15 +647,15 @@ class TestMetafunc(object): @pytest.mark.parametrize('x, y', [('a', 'b')], indirect='y') def test_simple(x): assert len(x) == 3 - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*uses no fixture 'y'*", - ]) + result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) @pytest.mark.issue714 def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='function') def x(request): @@ -562,15 +664,15 @@ class TestMetafunc(object): @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['y']) def test_simple(x): assert len(x) == 3 - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*uses no fixture 'y'*", - ]) + result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) @pytest.mark.issue714 def test_parametrize_argument_not_in_indirect_list(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='function') def x(request): @@ -579,19 +681,37 @@ class TestMetafunc(object): @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x']) def test_simple(x): assert len(x) == 3 - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*uses no argument 'y'*", - ]) + result.stdout.fnmatch_lines(["*uses no argument 'y'*"]) + + def test_parametrize_gives_indicative_error_on_function_with_default_argument( + self, testdir + ): + testdir.makepyfile( + """ + import pytest + + @pytest.mark.parametrize('x, y', [('a', 'b')]) + def test_simple(x, y=1): + assert len(x) == 1 + """ + ) + result = testdir.runpytest("--collect-only") + result.stdout.fnmatch_lines( + ["*already takes an argument 'y' with a default value"] + ) def test_addcalls_and_parametrize_indirect(self): + def func(x, y): pass + metafunc = self.Metafunc(func) metafunc.addcall(param="123") - metafunc.parametrize('x', [1], indirect=True) - metafunc.parametrize('y', [2, 3], indirect=True) + metafunc.parametrize("x", [1], indirect=True) + metafunc.parametrize("y", [2, 3], indirect=True) assert len(metafunc._calls) == 2 assert metafunc._calls[0].funcargs == {} assert metafunc._calls[1].funcargs == {} @@ -599,7 +719,8 @@ class TestMetafunc(object): assert metafunc._calls[1].params == dict(x=1, y=3) def test_parametrize_functional(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize('x', [1,2], indirect=True) @@ -611,13 +732,12 @@ class TestMetafunc(object): def test_simple(x,y): assert x in (10,20) assert y == 2 - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*test_simple*1-2*", - "*test_simple*2-2*", - "*2 passed*", - ]) + result.stdout.fnmatch_lines( + ["*test_simple*1-2*", "*test_simple*2-2*", "*2 passed*"] + ) def test_parametrize_onearg(self): metafunc = self.Metafunc(lambda x: None) @@ -646,7 +766,8 @@ class TestMetafunc(object): assert metafunc._calls[1].id == "3-4" def test_parametrize_multiple_times(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest pytestmark = pytest.mark.parametrize("x", [1,2]) def test_func(x): @@ -655,23 +776,27 @@ class TestMetafunc(object): pytestmark = pytest.mark.parametrize("y", [3,4]) def test_meth(self, x, y): assert 0, x - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 result.assert_outcomes(failed=6) def test_parametrize_CSV(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("x, y,", [(1,2), (2,3)]) def test_func(x, y): assert x+1 == y - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_parametrize_class_scenarios(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ # same as doc/en/example/parametrize scenario example def pytest_generate_tests(metafunc): idlist = [] @@ -695,10 +820,12 @@ class TestMetafunc(object): def test_3(self, arg, arg2): pass - """) + """ + ) result = testdir.runpytest("-v") assert result.ret == 0 - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *test_1*1* *test_2*1* *test_3*1* @@ -706,31 +833,39 @@ class TestMetafunc(object): *test_2*2* *test_3*2* *6 passed* - """) + """ + ) def test_format_args(self): + def function1(): pass - assert fixtures._format_args(function1) == '()' + + assert fixtures._format_args(function1) == "()" def function2(arg1): pass + assert fixtures._format_args(function2) == "(arg1)" def function3(arg1, arg2="qwe"): pass + assert fixtures._format_args(function3) == "(arg1, arg2='qwe')" def function4(arg1, *args, **kwargs): pass + assert fixtures._format_args(function4) == "(arg1, *args, **kwargs)" class TestMetafuncFunctional(object): + def test_attributes(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ # assumes that generate/provide runs in the same process - import py, pytest + import sys, pytest def pytest_generate_tests(metafunc): metafunc.addcall(param=metafunc) @@ -749,40 +884,44 @@ class TestMetafuncFunctional(object): def test_method(self, metafunc, pytestconfig): assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ - if py.std.sys.version_info > (3, 0): + if sys.version_info > (3, 0): unbound = TestClass.test_method else: unbound = TestClass.test_method.im_func # XXX actually have an unbound test function here? assert metafunc.function == unbound assert metafunc.cls == TestClass - """) + """ + ) result = testdir.runpytest(p, "-v") result.assert_outcomes(passed=2) def test_addcall_with_two_funcargs_generators(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_generate_tests(metafunc): assert "arg1" in metafunc.fixturenames metafunc.addcall(funcargs=dict(arg1=1, arg2=2)) - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall(funcargs=dict(arg1=1, arg2=1)) class TestClass(object): def test_myfunc(self, arg1, arg2): assert arg1 == arg2 - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_myfunc*0*PASS*", - "*test_myfunc*1*FAIL*", - "*1 failed, 1 passed*" - ]) + result.stdout.fnmatch_lines( + ["*test_myfunc*0*PASS*", "*test_myfunc*1*FAIL*", "*1 failed, 1 passed*"] + ) def test_two_functions(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall(param=10) metafunc.addcall(param=20) @@ -796,34 +935,42 @@ class TestMetafuncFunctional(object): assert arg1 == 10 def test_func2(arg1): assert arg1 in (10, 20) - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_func1*0*PASS*", - "*test_func1*1*FAIL*", - "*test_func2*PASS*", - "*1 failed, 3 passed*" - ]) + result.stdout.fnmatch_lines( + [ + "*test_func1*0*PASS*", + "*test_func1*1*FAIL*", + "*test_func2*PASS*", + "*1 failed, 3 passed*", + ] + ) def test_noself_in_method(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): assert 'xyz' not in metafunc.fixturenames class TestHello(object): def test_hello(xyz): pass - """) + """ + ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_generate_plugin_and_module(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_generate_tests(metafunc): assert "arg1" in metafunc.fixturenames metafunc.addcall(id="world", param=(2,100)) - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall(param=(1,1), id="hello") @@ -838,31 +985,34 @@ class TestMetafuncFunctional(object): class TestClass(object): def test_myfunc(self, arg1, arg2): assert arg1 == arg2 - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_myfunc*hello*PASS*", - "*test_myfunc*world*FAIL*", - "*1 failed, 1 passed*" - ]) + result.stdout.fnmatch_lines( + [ + "*test_myfunc*hello*PASS*", + "*test_myfunc*world*FAIL*", + "*1 failed, 1 passed*", + ] + ) def test_generate_tests_in_class(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ class TestClass(object): def pytest_generate_tests(self, metafunc): metafunc.addcall(funcargs={'hello': 'world'}, id="hello") def test_myfunc(self, hello): assert hello == "world" - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_myfunc*hello*PASS*", - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*test_myfunc*hello*PASS*", "*1 passed*"]) def test_two_functions_not_same_instance(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall({'arg1': 10}) metafunc.addcall({'arg1': 20}) @@ -871,16 +1021,16 @@ class TestMetafuncFunctional(object): def test_func(self, arg1): assert not hasattr(self, 'x') self.x = 1 - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_func*0*PASS*", - "*test_func*1*PASS*", - "*2 pass*", - ]) + result.stdout.fnmatch_lines( + ["*test_func*0*PASS*", "*test_func*1*PASS*", "*2 pass*"] + ) def test_issue28_setup_method_in_generate_tests(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall({'arg1': 1}) @@ -889,29 +1039,29 @@ class TestMetafuncFunctional(object): assert arg1 == self.val def setup_method(self, func): self.val = 1 - """) + """ + ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_parametrize_functional2(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.parametrize("arg1", [1,2]) metafunc.parametrize("arg2", [4,5]) def test_hello(arg1, arg2): assert 0, (arg1, arg2) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*(1, 4)*", - "*(1, 5)*", - "*(2, 4)*", - "*(2, 5)*", - "*4 failed*", - ]) + result.stdout.fnmatch_lines( + ["*(1, 4)*", "*(1, 5)*", "*(2, 4)*", "*(2, 5)*", "*4 failed*"] + ) def test_parametrize_and_inner_getfixturevalue(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.parametrize("arg1", [1], indirect=True) metafunc.parametrize("arg2", [10], indirect=True) @@ -928,15 +1078,14 @@ class TestMetafuncFunctional(object): def test_func1(arg1, arg2): assert arg1 == 11 - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_func1*1*PASS*", - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*test_func1*1*PASS*", "*1 passed*"]) def test_parametrize_on_setup_arg(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def pytest_generate_tests(metafunc): assert "arg1" in metafunc.fixturenames metafunc.parametrize("arg1", [1], indirect=True) @@ -952,15 +1101,20 @@ class TestMetafuncFunctional(object): def test_func(arg2): assert arg2 == 10 - """) + """ + ) result = testdir.runpytest("-v", p) - result.stdout.fnmatch_lines([ - "*test_func*1*PASS*", - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*test_func*1*PASS*", "*1 passed*"]) def test_parametrize_with_ids(self, testdir): - testdir.makepyfile(""" + testdir.makeini( + """ + [pytest] + console_output_style=classic + """ + ) + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,1), (1,2)], @@ -968,16 +1122,17 @@ class TestMetafuncFunctional(object): def test_function(a, b): assert a == b - """) + """ + ) result = testdir.runpytest("-v") assert result.ret == 1 - result.stdout.fnmatch_lines_random([ - "*test_function*basic*PASSED", - "*test_function*advanced*FAILED", - ]) + result.stdout.fnmatch_lines_random( + ["*test_function*basic*PASSED", "*test_function*advanced*FAILED"] + ) def test_parametrize_without_ids(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), @@ -985,15 +1140,19 @@ class TestMetafuncFunctional(object): def test_function(a, b): assert 1 - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *test_function*1-b0* *test_function*1.3-b1* - """) + """ + ) def test_parametrize_with_None_in_ids(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,1), (1,1), (1,2)], @@ -1001,18 +1160,22 @@ class TestMetafuncFunctional(object): def test_function(a, b): assert a == b - """) + """ + ) result = testdir.runpytest("-v") assert result.ret == 1 - result.stdout.fnmatch_lines_random([ - "*test_function*basic*PASSED", - "*test_function*1-1*PASSED", - "*test_function*advanced*FAILED", - ]) + result.stdout.fnmatch_lines_random( + [ + "*test_function*basic*PASSED*", + "*test_function*1-1*PASSED*", + "*test_function*advanced*FAILED*", + ] + ) def test_fixture_parametrized_empty_ids(self, testdir): """Fixtures parametrized with empty ids cause an internal error (#1849).""" - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="module", ids=[], params=[]) @@ -1021,36 +1184,44 @@ class TestMetafuncFunctional(object): def test_temp(temp): pass - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 1 skipped *']) + result.stdout.fnmatch_lines(["* 1 skipped *"]) def test_parametrized_empty_ids(self, testdir): """Tests parametrized with empty ids cause an internal error (#1849).""" - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('temp', [], ids=list()) def test_temp(temp): pass - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 1 skipped *']) + result.stdout.fnmatch_lines(["* 1 skipped *"]) def test_parametrized_ids_invalid_type(self, testdir): """Tests parametrized with ids as non-strings (#1857).""" - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("x, expected", [(10, 20), (40, 80)], ids=(None, 2)) def test_ids_numbers(x,expected): assert x * 2 == expected - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['*ids must be list of strings, found: 2 (type: int)*']) + result.stdout.fnmatch_lines( + ["*ids must be list of strings, found: 2 (type: int)*"] + ) def test_parametrize_with_identical_ids_get_unique_names(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,1), (1,2)], @@ -1058,18 +1229,18 @@ class TestMetafuncFunctional(object): def test_function(a, b): assert a == b - """) + """ + ) result = testdir.runpytest("-v") assert result.ret == 1 - result.stdout.fnmatch_lines_random([ - "*test_function*a0*PASSED", - "*test_function*a1*FAILED" - ]) + result.stdout.fnmatch_lines_random( + ["*test_function*a0*PASSED*", "*test_function*a1*FAILED*"] + ) - @pytest.mark.parametrize(("scope", "length"), - [("module", 2), ("function", 4)]) + @pytest.mark.parametrize(("scope", "length"), [("module", 2), ("function", 4)]) def test_parametrize_scope_overrides(self, testdir, scope, length): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest values = [] def pytest_generate_tests(metafunc): @@ -1086,12 +1257,15 @@ class TestMetafuncFunctional(object): assert arg in (1,2) def test_checklength(): assert len(values) == %d - """ % (scope, length)) + """ + % (scope, length) + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=5) def test_parametrize_issue323(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='module', params=range(966)) @@ -1102,12 +1276,14 @@ class TestMetafuncFunctional(object): pass def test_it2(foo): pass - """) + """ + ) reprec = testdir.inline_run("--collect-only") assert not reprec.getcalls("pytest_internalerror") def test_usefixtures_seen_in_generate_tests(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): assert "abc" in metafunc.fixturenames @@ -1116,28 +1292,38 @@ class TestMetafuncFunctional(object): @pytest.mark.usefixtures("abc") def test_function(): pass - """) + """ + ) reprec = testdir.runpytest() reprec.assert_outcomes(passed=1) def test_generate_tests_only_done_in_subdir(self, testdir): sub1 = testdir.mkpydir("sub1") sub2 = testdir.mkpydir("sub2") - sub1.join("conftest.py").write(_pytest._code.Source(""" + sub1.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_generate_tests(metafunc): assert metafunc.function.__name__ == "test_1" - """)) - sub2.join("conftest.py").write(_pytest._code.Source(""" + """ + ) + ) + sub2.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_generate_tests(metafunc): assert metafunc.function.__name__ == "test_2" - """)) + """ + ) + ) sub1.join("test_in_sub1.py").write("def test_1(): pass") sub2.join("test_in_sub2.py").write("def test_2(): pass") result = testdir.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1) result.assert_outcomes(passed=3) def test_generate_same_function_names_issue403(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def make_tests(): @@ -1148,25 +1334,31 @@ class TestMetafuncFunctional(object): test_x = make_tests() test_y = make_tests() - """) + """ + ) reprec = testdir.runpytest() reprec.assert_outcomes(passed=4) @pytest.mark.issue463 - @pytest.mark.parametrize('attr', ['parametrise', 'parameterize', - 'parameterise']) + @pytest.mark.parametrize("attr", ["parametrise", "parameterize", "parameterise"]) def test_parametrize_misspelling(self, testdir, attr): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest - @pytest.mark.{0}("x", range(2)) + @pytest.mark.{}("x", range(2)) def test_foo(x): pass - """.format(attr)) - reprec = testdir.inline_run('--collectonly') + """.format( + attr + ) + ) + reprec = testdir.inline_run("--collectonly") failures = reprec.getfailures() assert len(failures) == 1 - expectederror = "MarkerError: test_foo has '{0}', spelling should be 'parametrize'".format(attr) + expectederror = "MarkerError: test_foo has '{}', spelling should be 'parametrize'".format( + attr + ) assert expectederror in failures[0].longrepr.reprcrash.message @@ -1176,7 +1368,8 @@ class TestMetafuncFunctionalAuto(object): """ def test_parametrize_auto_scope(self, testdir): - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='session', autouse=True) @@ -1191,12 +1384,14 @@ class TestMetafuncFunctionalAuto(object): def test_2(animal): assert animal == 'fish' - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 3 passed *']) + result.stdout.fnmatch_lines(["* 3 passed *"]) def test_parametrize_auto_scope_indirect(self, testdir): - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='session') @@ -1212,12 +1407,14 @@ class TestMetafuncFunctionalAuto(object): def test_2(animal, echo): assert animal == 'fish' assert echo in (1, 2, 3) - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 3 passed *']) + result.stdout.fnmatch_lines(["* 3 passed *"]) def test_parametrize_auto_scope_override_fixture(self, testdir): - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='session', autouse=True) @@ -1227,12 +1424,14 @@ class TestMetafuncFunctionalAuto(object): @pytest.mark.parametrize('animal', ["dog", "cat"]) def test_1(animal): assert animal in ('dog', 'cat') - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 2 passed *']) + result.stdout.fnmatch_lines(["* 2 passed *"]) def test_parametrize_all_indirects(self, testdir): - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.fixture() @@ -1252,12 +1451,14 @@ class TestMetafuncFunctionalAuto(object): def test_2(animal, echo): assert animal == 'fish' assert echo in (1, 2, 3) - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['* 3 passed *']) + result.stdout.fnmatch_lines(["* 3 passed *"]) def test_parametrize_issue634(self, testdir): - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='module') @@ -1282,14 +1483,15 @@ class TestMetafuncFunctionalAuto(object): if test_with: params = test_with metafunc.parametrize('foo', params, indirect=True) - ''') + """ + ) result = testdir.runpytest("-s") output = result.stdout.str() - assert output.count('preparing foo-2') == 1 - assert output.count('preparing foo-3') == 1 + assert output.count("preparing foo-2") == 1 + assert output.count("preparing foo-3") == 1 -@pytest.mark.filterwarnings('ignore:Applying marks directly to parameters') +@pytest.mark.filterwarnings("ignore:Applying marks directly to parameters") @pytest.mark.issue308 class TestMarkersWithParametrization(object): @@ -1309,10 +1511,10 @@ class TestMarkersWithParametrization(object): items = testdir.getitems(s) assert len(items) == 3 for item in items: - assert 'foo' in item.keywords - assert 'bar' not in items[0].keywords - assert 'bar' in items[1].keywords - assert 'bar' not in items[2].keywords + assert "foo" in item.keywords + assert "bar" not in items[0].keywords + assert "bar" in items[1].keywords + assert "bar" not in items[2].keywords def test_select_based_on_mark(self, testdir): s = """ @@ -1327,7 +1529,7 @@ class TestMarkersWithParametrization(object): assert n + 1 == expected """ testdir.makepyfile(s) - rec = testdir.inline_run("-m", 'foo') + rec = testdir.inline_run("-m", "foo") passed, skipped, fail = rec.listoutcomes() assert len(passed) == 1 assert len(skipped) == 0 @@ -1349,7 +1551,7 @@ class TestMarkersWithParametrization(object): """ items = testdir.getitems(s) assert len(items) == 3 - for mark in ['foo', 'bar']: + for mark in ["foo", "bar"]: assert mark not in items[0].keywords assert mark in items[1].keywords assert mark not in items[2].keywords @@ -1435,7 +1637,7 @@ class TestMarkersWithParametrization(object): reprec = testdir.inline_run() reprec.assertoutcome(passed=2, skipped=1) - @pytest.mark.parametrize('strict', [True, False]) + @pytest.mark.parametrize("strict", [True, False]) def test_xfail_passing_is_xpass(self, testdir, strict): s = """ import pytest @@ -1447,7 +1649,9 @@ class TestMarkersWithParametrization(object): ]) def test_increment(n, expected): assert n + 1 == expected - """.format(strict=strict) + """.format( + strict=strict + ) testdir.makepyfile(s) reprec = testdir.inline_run() passed, failed = (2, 1) if strict else (3, 0) @@ -1478,7 +1682,8 @@ class TestMarkersWithParametrization(object): @pytest.mark.issue290 def test_parametrize_ID_generation_string_int_works(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -1488,11 +1693,12 @@ class TestMarkersWithParametrization(object): 'limit', (0, '0')) def test_limit(limit, myfixture): return - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) - @pytest.mark.parametrize('strict', [True, False]) + @pytest.mark.parametrize("strict", [True, False]) def test_parametrize_marked_value(self, testdir, strict): s = """ import pytest @@ -1509,36 +1715,42 @@ class TestMarkersWithParametrization(object): ]) def test_increment(n, expected): assert n + 1 == expected - """.format(strict=strict) + """.format( + strict=strict + ) testdir.makepyfile(s) reprec = testdir.inline_run() passed, failed = (0, 2) if strict else (2, 0) reprec.assertoutcome(passed=passed, failed=failed) def test_pytest_make_parametrize_id(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_make_parametrize_id(config, val): return str(val * 2) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("x", range(2)) def test_func(x): pass - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*test_func*0*PASS*", - "*test_func*2*PASS*", - ]) + result.stdout.fnmatch_lines(["*test_func*0*PASS*", "*test_func*2*PASS*"]) def test_pytest_make_parametrize_id_with_argname(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_make_parametrize_id(config, val, argname): return str(val * 2 if argname == 'x' else val * 10) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("x", range(2)) @@ -1548,10 +1760,9 @@ class TestMarkersWithParametrization(object): @pytest.mark.parametrize("y", [1]) def test_func_b(y): pass - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*test_func_a*0*PASS*", - "*test_func_a*2*PASS*", - "*test_func_b*10*PASS*", - ]) + result.stdout.fnmatch_lines( + ["*test_func_a*0*PASS*", "*test_func_a*2*PASS*", "*test_func_b*10*PASS*"] + ) diff --git a/third_party/python/pytest/testing/python/raises.py b/third_party/python/pytest/testing/python/raises.py index 321ee349ee60..64199c3b6f5f 100644 --- a/third_party/python/pytest/testing/python/raises.py +++ b/third_party/python/pytest/testing/python/raises.py @@ -1,8 +1,10 @@ +from _pytest.outcomes import Failed import pytest import sys class TestRaises(object): + def test_raises(self): source = "int('qwe')" excinfo = pytest.raises(ValueError, source) @@ -17,19 +19,23 @@ class TestRaises(object): pytest.raises(SyntaxError, "qwe qwe qwe") def test_raises_function(self): - pytest.raises(ValueError, int, 'hello') + pytest.raises(ValueError, int, "hello") def test_raises_callable_no_exception(self): + class A(object): + def __call__(self): pass + try: pytest.raises(ValueError, A()) except pytest.raises.Exception: pass def test_raises_as_contextmanager(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from __future__ import with_statement import py, pytest import _pytest._code @@ -51,25 +57,29 @@ class TestRaises(object): with pytest.raises(ZeroDivisionError): with pytest.raises(ValueError): 1/0 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*3 passed*', - ]) + result.stdout.fnmatch_lines(["*3 passed*"]) def test_noclass(self): with pytest.raises(TypeError): - pytest.raises('wrong', lambda: None) + pytest.raises("wrong", lambda: None) + + def test_invalid_arguments_to_raises(self): + with pytest.raises(TypeError, match="unknown"): + with pytest.raises(TypeError, unknown="bogus"): + raise ValueError() def test_tuple(self): with pytest.raises((KeyError, ValueError)): - raise KeyError('oops') + raise KeyError("oops") def test_no_raise_message(self): try: - pytest.raises(ValueError, int, '0') + pytest.raises(ValueError, int, "0") except pytest.raises.Exception as e: - assert e.msg == "DID NOT RAISE {0}".format(repr(ValueError)) + assert e.msg == "DID NOT RAISE {}".format(repr(ValueError)) else: assert False, "Expected pytest.raises.Exception" @@ -77,7 +87,7 @@ class TestRaises(object): with pytest.raises(ValueError): pass except pytest.raises.Exception as e: - assert e.msg == "DID NOT RAISE {0}".format(repr(ValueError)) + assert e.msg == "DID NOT RAISE {}".format(repr(ValueError)) else: assert False, "Expected pytest.raises.Exception" @@ -91,7 +101,7 @@ class TestRaises(object): else: assert False, "Expected pytest.raises.Exception" - @pytest.mark.parametrize('method', ['function', 'with']) + @pytest.mark.parametrize("method", ["function", "with"]) def test_raises_cyclic_reference(self, method): """ Ensure pytest.raises does not leave a reference cycle (#1965). @@ -99,11 +109,12 @@ class TestRaises(object): import gc class T(object): + def __call__(self): raise ValueError t = T() - if method == 'function': + if method == "function": pytest.raises(ValueError, t) else: with pytest.raises(ValueError): @@ -121,14 +132,46 @@ class TestRaises(object): def test_raises_match(self): msg = r"with base \d+" with pytest.raises(ValueError, match=msg): - int('asdf') + int("asdf") msg = "with base 10" with pytest.raises(ValueError, match=msg): - int('asdf') + int("asdf") msg = "with base 16" - expr = r"Pattern '{0}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format(msg) + expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format( + msg + ) with pytest.raises(AssertionError, match=expr): with pytest.raises(ValueError, match=msg): - int('asdf', base=10) + int("asdf", base=10) + + def test_raises_match_wrong_type(self): + """Raising an exception with the wrong type and match= given. + + pytest should throw the unexpected exception - the pattern match is not + really relevant if we got a different exception. + """ + with pytest.raises(ValueError): + with pytest.raises(IndexError, match="nomatch"): + int("asdf") + + def test_raises_exception_looks_iterable(self): + from six import add_metaclass + + class Meta(type(object)): + + def __getitem__(self, item): + return 1 / 0 + + def __len__(self): + return 1 + + @add_metaclass(Meta) + class ClassLooksIterableException(Exception): + pass + + with pytest.raises( + Failed, match="DID NOT RAISE " + ): + pytest.raises(ClassLooksIterableException, lambda: None) diff --git a/third_party/python/pytest/testing/python/setup_only.py b/third_party/python/pytest/testing/python/setup_only.py index 18af56477747..4ae24b15a6da 100644 --- a/third_party/python/pytest/testing/python/setup_only.py +++ b/third_party/python/pytest/testing/python/setup_only.py @@ -1,14 +1,14 @@ import pytest -@pytest.fixture(params=['--setup-only', '--setup-plan', '--setup-show'], - scope='module') +@pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module") def mode(request): return request.param def test_show_only_active_fixtures(testdir, mode): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def _arg0(): @@ -18,21 +18,21 @@ def test_show_only_active_fixtures(testdir, mode): """arg1 docstring""" def test_arg1(arg1): pass - ''') + ''' + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*SETUP F arg1*', - '*test_arg1 (fixtures used: arg1)*', - '*TEARDOWN F arg1*', - ]) + result.stdout.fnmatch_lines( + ["*SETUP F arg1*", "*test_arg1 (fixtures used: arg1)*", "*TEARDOWN F arg1*"] + ) assert "_arg0" not in result.stdout.str() def test_show_different_scopes(testdir, mode): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def arg_function(): @@ -42,50 +42,60 @@ def test_show_different_scopes(testdir, mode): """session scoped fixture""" def test_arg1(arg_session, arg_function): pass - ''') + ''' + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - 'SETUP S arg_session*', - '*SETUP F arg_function*', - '*test_arg1 (fixtures used: arg_function, arg_session)*', - '*TEARDOWN F arg_function*', - 'TEARDOWN S arg_session*', - ]) + result.stdout.fnmatch_lines( + [ + "SETUP S arg_session*", + "*SETUP F arg_function*", + "*test_arg1 (fixtures used: arg_function, arg_session)*", + "*TEARDOWN F arg_function*", + "TEARDOWN S arg_session*", + ] + ) def test_show_nested_fixtures(testdir, mode): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture(scope='session') def arg_same(): """session scoped fixture""" - ''') - p = testdir.makepyfile(''' + ''' + ) + p = testdir.makepyfile( + ''' import pytest @pytest.fixture(scope='function') def arg_same(arg_same): """function scoped fixture""" def test_arg1(arg_same): pass - ''') + ''' + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - 'SETUP S arg_same*', - '*SETUP F arg_same (fixtures used: arg_same)*', - '*test_arg1 (fixtures used: arg_same)*', - '*TEARDOWN F arg_same*', - 'TEARDOWN S arg_same*', - ]) + result.stdout.fnmatch_lines( + [ + "SETUP S arg_same*", + "*SETUP F arg_same (fixtures used: arg_same)*", + "*test_arg1 (fixtures used: arg_same)*", + "*TEARDOWN F arg_same*", + "TEARDOWN S arg_same*", + ] + ) def test_show_fixtures_with_autouse(testdir, mode): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def arg_function(): @@ -95,92 +105,104 @@ def test_show_fixtures_with_autouse(testdir, mode): """session scoped fixture""" def test_arg1(arg_function): pass - ''') + ''' + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - 'SETUP S arg_session*', - '*SETUP F arg_function*', - '*test_arg1 (fixtures used: arg_function, arg_session)*', - ]) + result.stdout.fnmatch_lines( + [ + "SETUP S arg_session*", + "*SETUP F arg_function*", + "*test_arg1 (fixtures used: arg_function, arg_session)*", + ] + ) def test_show_fixtures_with_parameters(testdir, mode): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture(scope='session', params=['foo', 'bar']) def arg_same(): """session scoped fixture""" - ''') - p = testdir.makepyfile(''' + ''' + ) + p = testdir.makepyfile( + ''' import pytest @pytest.fixture(scope='function') def arg_other(arg_same): """function scoped fixture""" def test_arg1(arg_other): pass - ''') + ''' + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - 'SETUP S arg_same?foo?', - 'TEARDOWN S arg_same?foo?', - 'SETUP S arg_same?bar?', - 'TEARDOWN S arg_same?bar?', - ]) + result.stdout.fnmatch_lines( + [ + "SETUP S arg_same?foo?", + "TEARDOWN S arg_same?foo?", + "SETUP S arg_same?bar?", + "TEARDOWN S arg_same?bar?", + ] + ) def test_show_fixtures_with_parameter_ids(testdir, mode): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture( scope='session', params=['foo', 'bar'], ids=['spam', 'ham']) def arg_same(): """session scoped fixture""" - ''') - p = testdir.makepyfile(''' + ''' + ) + p = testdir.makepyfile( + ''' import pytest @pytest.fixture(scope='function') def arg_other(arg_same): """function scoped fixture""" def test_arg1(arg_other): pass - ''') + ''' + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - 'SETUP S arg_same?spam?', - 'SETUP S arg_same?ham?', - ]) + result.stdout.fnmatch_lines( + ["SETUP S arg_same?spam?", "SETUP S arg_same?ham?"] + ) def test_show_fixtures_with_parameter_ids_function(testdir, mode): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + """ import pytest @pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper()) def foobar(): pass def test_foobar(foobar): pass - ''') + """ + ) result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*SETUP F foobar?FOO?', - '*SETUP F foobar?BAR?', - ]) + result.stdout.fnmatch_lines(["*SETUP F foobar?FOO?", "*SETUP F foobar?BAR?"]) def test_dynamic_fixture_request(testdir): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + """ import pytest @pytest.fixture() def dynamically_requested_fixture(): @@ -190,19 +212,23 @@ def test_dynamic_fixture_request(testdir): request.getfixturevalue('dynamically_requested_fixture') def test_dyn(dependent_fixture): pass - ''') + """ + ) - result = testdir.runpytest('--setup-only', p) + result = testdir.runpytest("--setup-only", p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*SETUP F dynamically_requested_fixture', - '*TEARDOWN F dynamically_requested_fixture' - ]) + result.stdout.fnmatch_lines( + [ + "*SETUP F dynamically_requested_fixture", + "*TEARDOWN F dynamically_requested_fixture", + ] + ) def test_capturing(testdir): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + """ import pytest, sys @pytest.fixture() def one(): @@ -213,31 +239,31 @@ def test_capturing(testdir): assert 0 def test_capturing(two): pass - ''') + """ + ) - result = testdir.runpytest('--setup-only', p) - result.stdout.fnmatch_lines([ - 'this should be captured', - 'this should also be captured' - ]) + result = testdir.runpytest("--setup-only", p) + result.stdout.fnmatch_lines( + ["this should be captured", "this should also be captured"] + ) def test_show_fixtures_and_execute_test(testdir): """ Verifies that setups are shown and tests are executed. """ - p = testdir.makepyfile(''' + p = testdir.makepyfile( + """ import pytest @pytest.fixture def arg(): assert True def test_arg(arg): assert False - ''') + """ + ) result = testdir.runpytest("--setup-show", p) assert result.ret == 1 - result.stdout.fnmatch_lines([ - '*SETUP F arg*', - '*test_arg (fixtures used: arg)F', - '*TEARDOWN F arg*', - ]) + result.stdout.fnmatch_lines( + ["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"] + ) diff --git a/third_party/python/pytest/testing/python/setup_plan.py b/third_party/python/pytest/testing/python/setup_plan.py index 8c98224692a8..0321939a8aac 100644 --- a/third_party/python/pytest/testing/python/setup_plan.py +++ b/third_party/python/pytest/testing/python/setup_plan.py @@ -1,19 +1,19 @@ def test_show_fixtures_and_test(testdir): """ Verifies that fixtures are not executed. """ - p = testdir.makepyfile(''' + p = testdir.makepyfile( + """ import pytest @pytest.fixture def arg(): assert False def test_arg(arg): assert False - ''') + """ + ) result = testdir.runpytest("--setup-plan", p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*SETUP F arg*', - '*test_arg (fixtures used: arg)', - '*TEARDOWN F arg*', - ]) + result.stdout.fnmatch_lines( + ["*SETUP F arg*", "*test_arg (fixtures used: arg)", "*TEARDOWN F arg*"] + ) diff --git a/third_party/python/pytest/testing/python/show_fixtures_per_test.py b/third_party/python/pytest/testing/python/show_fixtures_per_test.py index 741f33946a0e..e14344d4ebf7 100644 --- a/third_party/python/pytest/testing/python/show_fixtures_per_test.py +++ b/third_party/python/pytest/testing/python/show_fixtures_per_test.py @@ -2,13 +2,14 @@ def test_no_items_should_not_show_output(testdir): - result = testdir.runpytest('--fixtures-per-test') - assert 'fixtures used by' not in result.stdout.str() + result = testdir.runpytest("--fixtures-per-test") + assert "fixtures used by" not in result.stdout.str() assert result.ret == 0 def test_fixtures_in_module(testdir): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def _arg0(): @@ -18,22 +19,26 @@ def test_fixtures_in_module(testdir): """arg1 docstring""" def test_arg1(arg1): pass - ''') + ''' + ) result = testdir.runpytest("--fixtures-per-test", p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*fixtures used by test_arg1*', - '*(test_fixtures_in_module.py:9)*', - 'arg1', - ' arg1 docstring', - ]) + result.stdout.fnmatch_lines( + [ + "*fixtures used by test_arg1*", + "*(test_fixtures_in_module.py:9)*", + "arg1", + " arg1 docstring", + ] + ) assert "_arg0" not in result.stdout.str() def test_fixtures_in_conftest(testdir): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture def arg1(): @@ -46,35 +51,41 @@ def test_fixtures_in_conftest(testdir): """arg3 docstring """ - ''') - p = testdir.makepyfile(''' + ''' + ) + p = testdir.makepyfile( + """ def test_arg2(arg2): pass def test_arg3(arg3): pass - ''') + """ + ) result = testdir.runpytest("--fixtures-per-test", p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*fixtures used by test_arg2*', - '*(test_fixtures_in_conftest.py:2)*', - 'arg2', - ' arg2 docstring', - '*fixtures used by test_arg3*', - '*(test_fixtures_in_conftest.py:4)*', - 'arg1', - ' arg1 docstring', - 'arg2', - ' arg2 docstring', - 'arg3', - ' arg3', - ' docstring', - ]) + result.stdout.fnmatch_lines( + [ + "*fixtures used by test_arg2*", + "*(test_fixtures_in_conftest.py:2)*", + "arg2", + " arg2 docstring", + "*fixtures used by test_arg3*", + "*(test_fixtures_in_conftest.py:4)*", + "arg1", + " arg1 docstring", + "arg2", + " arg2 docstring", + "arg3", + " arg3", + " docstring", + ] + ) def test_should_show_fixtures_used_by_test(testdir): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture def arg1(): @@ -82,30 +93,36 @@ def test_should_show_fixtures_used_by_test(testdir): @pytest.fixture def arg2(): """arg2 from conftest""" - ''') - p = testdir.makepyfile(''' + ''' + ) + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def arg1(): """arg1 from testmodule""" def test_args(arg1, arg2): pass - ''') + ''' + ) result = testdir.runpytest("--fixtures-per-test", p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*fixtures used by test_args*', - '*(test_should_show_fixtures_used_by_test.py:6)*', - 'arg1', - ' arg1 from testmodule', - 'arg2', - ' arg2 from conftest', - ]) + result.stdout.fnmatch_lines( + [ + "*fixtures used by test_args*", + "*(test_should_show_fixtures_used_by_test.py:6)*", + "arg1", + " arg1 from testmodule", + "arg2", + " arg2 from conftest", + ] + ) def test_verbose_include_private_fixtures_and_loc(testdir): - testdir.makeconftest(''' + testdir.makeconftest( + ''' import pytest @pytest.fixture def _arg1(): @@ -113,46 +130,54 @@ def test_verbose_include_private_fixtures_and_loc(testdir): @pytest.fixture def arg2(_arg1): """arg2 from conftest""" - ''') - p = testdir.makepyfile(''' + ''' + ) + p = testdir.makepyfile( + ''' import pytest @pytest.fixture def arg3(): """arg3 from testmodule""" def test_args(arg2, arg3): pass - ''') + ''' + ) result = testdir.runpytest("--fixtures-per-test", "-v", p) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*fixtures used by test_args*', - '*(test_verbose_include_private_fixtures_and_loc.py:6)*', - '_arg1 -- conftest.py:3', - ' _arg1 from conftest', - 'arg2 -- conftest.py:6', - ' arg2 from conftest', - 'arg3 -- test_verbose_include_private_fixtures_and_loc.py:3', - ' arg3 from testmodule', - ]) + result.stdout.fnmatch_lines( + [ + "*fixtures used by test_args*", + "*(test_verbose_include_private_fixtures_and_loc.py:6)*", + "_arg1 -- conftest.py:3", + " _arg1 from conftest", + "arg2 -- conftest.py:6", + " arg2 from conftest", + "arg3 -- test_verbose_include_private_fixtures_and_loc.py:3", + " arg3 from testmodule", + ] + ) def test_doctest_items(testdir): - testdir.makepyfile(''' + testdir.makepyfile( + ''' def foo(): """ >>> 1 + 1 2 """ - ''') - testdir.maketxtfile(''' + ''' + ) + testdir.maketxtfile( + """ >>> 1 + 1 2 - ''') - result = testdir.runpytest("--fixtures-per-test", "--doctest-modules", - "--doctest-glob=*.txt", "-v") + """ + ) + result = testdir.runpytest( + "--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v" + ) assert result.ret == 0 - result.stdout.fnmatch_lines([ - '*collected 2 items*', - ]) + result.stdout.fnmatch_lines(["*collected 2 items*"]) diff --git a/third_party/python/pytest/testing/python/test_deprecations.py b/third_party/python/pytest/testing/python/test_deprecations.py new file mode 100644 index 000000000000..b0c11f0b03b4 --- /dev/null +++ b/third_party/python/pytest/testing/python/test_deprecations.py @@ -0,0 +1,22 @@ +import pytest + +from _pytest.python import PyCollector + + +class PyCollectorMock(PyCollector): + """evil hack""" + + def __init__(self): + self.called = False + + def _makeitem(self, *k): + """hack to disable the actual behaviour""" + self.called = True + + +def test_pycollector_makeitem_is_deprecated(): + + collector = PyCollectorMock() + with pytest.deprecated_call(): + collector.makeitem("foo", "bar") + assert collector.called diff --git a/third_party/python/pytest/testing/test_argcomplete.py b/third_party/python/pytest/testing/test_argcomplete.py index c9261257743f..b042de5ce5be 100644 --- a/third_party/python/pytest/testing/test_argcomplete.py +++ b/third_party/python/pytest/testing/test_argcomplete.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, division, print_function -import py +import subprocess +import sys import pytest # test for _argcomplete but not specific for any application @@ -10,12 +11,13 @@ def equal_with_bash(prefix, ffc, fc, out=None): res_bash = set(fc(prefix)) retval = set(res) == res_bash if out: - out.write('equal_with_bash %s %s\n' % (retval, res)) + out.write("equal_with_bash %s %s\n" % (retval, res)) if not retval: - out.write(' python - bash: %s\n' % (set(res) - res_bash)) - out.write(' bash - python: %s\n' % (res_bash - set(res))) + out.write(" python - bash: %s\n" % (set(res) - res_bash)) + out.write(" bash - python: %s\n" % (res_bash - set(res))) return retval + # copied from argcomplete.completers as import from there # also pulls in argcomplete.__init__ which opens filedescriptor 9 # this gives an IOError at the end of testrun @@ -23,67 +25,76 @@ def equal_with_bash(prefix, ffc, fc, out=None): def _wrapcall(*args, **kargs): try: - if py.std.sys.version_info > (2, 7): - return py.std.subprocess.check_output(*args, **kargs).decode().splitlines() - if 'stdout' in kargs: - raise ValueError('stdout argument not allowed, it will be overridden.') - process = py.std.subprocess.Popen( - stdout=py.std.subprocess.PIPE, *args, **kargs) + if sys.version_info > (2, 7): + return subprocess.check_output(*args, **kargs).decode().splitlines() + if "stdout" in kargs: + raise ValueError("stdout argument not allowed, it will be overridden.") + process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kargs.get("args") if cmd is None: cmd = args[0] - raise py.std.subprocess.CalledProcessError(retcode, cmd) + raise subprocess.CalledProcessError(retcode, cmd) return output.decode().splitlines() - except py.std.subprocess.CalledProcessError: + except subprocess.CalledProcessError: return [] class FilesCompleter(object): - 'File completer class, optionally takes a list of allowed extensions' + "File completer class, optionally takes a list of allowed extensions" def __init__(self, allowednames=(), directories=True): # Fix if someone passes in a string instead of a list if type(allowednames) is str: allowednames = [allowednames] - self.allowednames = [x.lstrip('*').lstrip('.') for x in allowednames] + self.allowednames = [x.lstrip("*").lstrip(".") for x in allowednames] self.directories = directories def __call__(self, prefix, **kwargs): completion = [] if self.allowednames: if self.directories: - files = _wrapcall(['bash', '-c', - "compgen -A directory -- '{p}'".format(p=prefix)]) - completion += [f + '/' for f in files] + files = _wrapcall( + ["bash", "-c", "compgen -A directory -- '{p}'".format(p=prefix)] + ) + completion += [f + "/" for f in files] for x in self.allowednames: - completion += _wrapcall(['bash', '-c', - "compgen -A file -X '!*.{0}' -- '{p}'".format(x, p=prefix)]) + completion += _wrapcall( + [ + "bash", + "-c", + "compgen -A file -X '!*.{0}' -- '{p}'".format(x, p=prefix), + ] + ) else: - completion += _wrapcall(['bash', '-c', - "compgen -A file -- '{p}'".format(p=prefix)]) + completion += _wrapcall( + ["bash", "-c", "compgen -A file -- '{p}'".format(p=prefix)] + ) - anticomp = _wrapcall(['bash', '-c', - "compgen -A directory -- '{p}'".format(p=prefix)]) + anticomp = _wrapcall( + ["bash", "-c", "compgen -A directory -- '{p}'".format(p=prefix)] + ) completion = list(set(completion) - set(anticomp)) if self.directories: - completion += [f + '/' for f in anticomp] + completion += [f + "/" for f in anticomp] return completion class TestArgComplete(object): + @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") def test_compare_with_compgen(self): from _pytest._argcomplete import FastFilesCompleter + ffc = FastFilesCompleter() fc = FilesCompleter() - for x in ['/', '/d', '/data', 'qqq', '']: - assert equal_with_bash(x, ffc, fc, out=py.std.sys.stdout) + for x in ["/", "/d", "/data", "qqq", ""]: + assert equal_with_bash(x, ffc, fc, out=sys.stdout) @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") def test_remove_dir_prefix(self): @@ -91,7 +102,8 @@ class TestArgComplete(object): ls /usr/ """ from _pytest._argcomplete import FastFilesCompleter + ffc = FastFilesCompleter() fc = FilesCompleter() - for x in '/usr/'.split(): - assert not equal_with_bash(x, ffc, fc, out=py.std.sys.stdout) + for x in "/usr/".split(): + assert not equal_with_bash(x, ffc, fc, out=sys.stdout) diff --git a/third_party/python/pytest/testing/test_assertion.py b/third_party/python/pytest/testing/test_assertion.py index 4cd050d8c9c5..393cf817c7e9 100644 --- a/third_party/python/pytest/testing/test_assertion.py +++ b/third_party/python/pytest/testing/test_assertion.py @@ -19,23 +19,23 @@ def mock_config(): verbose = False def getoption(self, name): - if name == 'verbose': + if name == "verbose": return self.verbose - raise KeyError('Not mocked out: %s' % name) + raise KeyError("Not mocked out: %s" % name) return Config() class TestImportHookInstallation(object): - @pytest.mark.parametrize('initial_conftest', [True, False]) - @pytest.mark.parametrize('mode', ['plain', 'rewrite']) + @pytest.mark.parametrize("initial_conftest", [True, False]) + @pytest.mark.parametrize("mode", ["plain", "rewrite"]) def test_conftest_assertion_rewrite(self, testdir, initial_conftest, mode): """Test that conftest files are using assertion rewrite on import. (#1619) """ - testdir.tmpdir.join('foo/tests').ensure(dir=1) - conftest_path = 'conftest.py' if initial_conftest else 'foo/conftest.py' + testdir.tmpdir.join("foo/tests").ensure(dir=1) + conftest_path = "conftest.py" if initial_conftest else "foo/conftest.py" contents = { conftest_path: """ import pytest @@ -45,17 +45,17 @@ class TestImportHookInstallation(object): assert values.pop(0) == value return check """, - 'foo/tests/test_foo.py': """ + "foo/tests/test_foo.py": """ def test(check_first): check_first([10, 30], 30) - """ + """, } testdir.makepyfile(**contents) - result = testdir.runpytest_subprocess('--assert=%s' % mode) - if mode == 'plain': - expected = 'E AssertionError' - elif mode == 'rewrite': - expected = '*assert 10 == 30*' + result = testdir.runpytest_subprocess("--assert=%s" % mode) + if mode == "plain": + expected = "E AssertionError" + elif mode == "rewrite": + expected = "*assert 10 == 30*" else: assert 0 result.stdout.fnmatch_lines([expected]) @@ -65,25 +65,25 @@ class TestImportHookInstallation(object): Assertions in the pytester plugin must also benefit from assertion rewriting (#1920). """ - testdir.makepyfile(""" + testdir.makepyfile( + """ pytest_plugins = ['pytester'] def test_dummy_failure(testdir): # how meta! testdir.makepyfile('def test(): assert 0') r = testdir.inline_run() r.assertoutcome(passed=1) - """) + """ + ) result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines([ - '*assert 1 == 0*', - ]) + result.stdout.fnmatch_lines(["*assert 1 == 0*"]) - @pytest.mark.parametrize('mode', ['plain', 'rewrite']) + @pytest.mark.parametrize("mode", ["plain", "rewrite"]) def test_pytest_plugins_rewrite(self, testdir, mode): contents = { - 'conftest.py': """ + "conftest.py": """ pytest_plugins = ['ham'] """, - 'ham.py': """ + "ham.py": """ import pytest @pytest.fixture def check_first(): @@ -91,52 +91,72 @@ class TestImportHookInstallation(object): assert values.pop(0) == value return check """, - 'test_foo.py': """ + "test_foo.py": """ def test_foo(check_first): check_first([10, 30], 30) """, } testdir.makepyfile(**contents) - result = testdir.runpytest_subprocess('--assert=%s' % mode) - if mode == 'plain': - expected = 'E AssertionError' - elif mode == 'rewrite': - expected = '*assert 10 == 30*' + result = testdir.runpytest_subprocess("--assert=%s" % mode) + if mode == "plain": + expected = "E AssertionError" + elif mode == "rewrite": + expected = "*assert 10 == 30*" else: assert 0 result.stdout.fnmatch_lines([expected]) - @pytest.mark.parametrize('mode', ['str', 'list']) + @pytest.mark.parametrize("mode", ["str", "list"]) def test_pytest_plugins_rewrite_module_names(self, testdir, mode): """Test that pluginmanager correct marks pytest_plugins variables for assertion rewriting if they are defined as plain strings or list of strings (#1888). """ - plugins = '"ham"' if mode == 'str' else '["ham"]' + plugins = '"ham"' if mode == "str" else '["ham"]' contents = { - 'conftest.py': """ + "conftest.py": """ pytest_plugins = {plugins} - """.format(plugins=plugins), - 'ham.py': """ + """.format( + plugins=plugins + ), + "ham.py": """ import pytest """, - 'test_foo.py': """ + "test_foo.py": """ def test_foo(pytestconfig): assert 'ham' in pytestconfig.pluginmanager.rewrite_hook._must_rewrite """, } testdir.makepyfile(**contents) - result = testdir.runpytest_subprocess('--assert=rewrite') + result = testdir.runpytest_subprocess("--assert=rewrite") assert result.ret == 0 - @pytest.mark.parametrize('mode', ['plain', 'rewrite']) - @pytest.mark.parametrize('plugin_state', ['development', 'installed']) + def test_pytest_plugins_rewrite_module_names_correctly(self, testdir): + """Test that we match files correctly when they are marked for rewriting (#2939).""" + contents = { + "conftest.py": """ + pytest_plugins = "ham" + """, + "ham.py": "", + "hamster.py": "", + "test_foo.py": """ + def test_foo(pytestconfig): + assert pytestconfig.pluginmanager.rewrite_hook.find_module('ham') is not None + assert pytestconfig.pluginmanager.rewrite_hook.find_module('hamster') is None + """, + } + testdir.makepyfile(**contents) + result = testdir.runpytest_subprocess("--assert=rewrite") + assert result.ret == 0 + + @pytest.mark.parametrize("mode", ["plain", "rewrite"]) + @pytest.mark.parametrize("plugin_state", ["development", "installed"]) def test_installed_plugin_rewrite(self, testdir, mode, plugin_state): # Make sure the hook is installed early enough so that plugins - # installed via setuptools are re-written. - testdir.tmpdir.join('hampkg').ensure(dir=1) + # installed via setuptools are rewritten. + testdir.tmpdir.join("hampkg").ensure(dir=1) contents = { - 'hampkg/__init__.py': """ + "hampkg/__init__.py": """ import pytest @pytest.fixture @@ -145,7 +165,7 @@ class TestImportHookInstallation(object): assert values.pop(0) == value return check """, - 'spamplugin.py': """ + "spamplugin.py": """ import pytest from hampkg import check_first2 @@ -155,7 +175,7 @@ class TestImportHookInstallation(object): assert values.pop(0) == value return check """, - 'mainwrapper.py': """ + "mainwrapper.py": """ import pytest, pkg_resources plugin_state = "{plugin_state}" @@ -191,8 +211,10 @@ class TestImportHookInstallation(object): pkg_resources.iter_entry_points = iter_entry_points pytest.main() - """.format(plugin_state=plugin_state), - 'test_foo.py': """ + """.format( + plugin_state=plugin_state + ), + "test_foo.py": """ def test(check_first): check_first([10, 30], 30) @@ -201,42 +223,44 @@ class TestImportHookInstallation(object): """, } testdir.makepyfile(**contents) - result = testdir.run(sys.executable, 'mainwrapper.py', '-s', '--assert=%s' % mode) - if mode == 'plain': - expected = 'E AssertionError' - elif mode == 'rewrite': - expected = '*assert 10 == 30*' + result = testdir.run( + sys.executable, "mainwrapper.py", "-s", "--assert=%s" % mode + ) + if mode == "plain": + expected = "E AssertionError" + elif mode == "rewrite": + expected = "*assert 10 == 30*" else: assert 0 result.stdout.fnmatch_lines([expected]) def test_rewrite_ast(self, testdir): - testdir.tmpdir.join('pkg').ensure(dir=1) + testdir.tmpdir.join("pkg").ensure(dir=1) contents = { - 'pkg/__init__.py': """ + "pkg/__init__.py": """ import pytest pytest.register_assert_rewrite('pkg.helper') """, - 'pkg/helper.py': """ + "pkg/helper.py": """ def tool(): a, b = 2, 3 assert a == b """, - 'pkg/plugin.py': """ + "pkg/plugin.py": """ import pytest, pkg.helper @pytest.fixture def tool(): return pkg.helper.tool """, - 'pkg/other.py': """ + "pkg/other.py": """ values = [3, 2] def tool(): assert values.pop() == 3 """, - 'conftest.py': """ + "conftest.py": """ pytest_plugins = ['pkg.plugin'] """, - 'test_pkg.py': """ + "test_pkg.py": """ import pkg.other def test_tool(tool): tool() @@ -245,23 +269,29 @@ class TestImportHookInstallation(object): """, } testdir.makepyfile(**contents) - result = testdir.runpytest_subprocess('--assert=rewrite') - result.stdout.fnmatch_lines(['>*assert a == b*', - 'E*assert 2 == 3*', - '>*assert values.pop() == 3*', - 'E*AssertionError']) + result = testdir.runpytest_subprocess("--assert=rewrite") + result.stdout.fnmatch_lines( + [ + ">*assert a == b*", + "E*assert 2 == 3*", + ">*assert values.pop() == 3*", + "E*AssertionError", + ] + ) def test_register_assert_rewrite_checks_types(self): with pytest.raises(TypeError): - pytest.register_assert_rewrite(['pytest_tests_internal_non_existing']) - pytest.register_assert_rewrite('pytest_tests_internal_non_existing', - 'pytest_tests_internal_non_existing2') + pytest.register_assert_rewrite(["pytest_tests_internal_non_existing"]) + pytest.register_assert_rewrite( + "pytest_tests_internal_non_existing", "pytest_tests_internal_non_existing2" + ) class TestBinReprIntegration(object): def test_pytest_assertrepr_compare_called(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest values = [] def pytest_assertrepr_compare(op, left, right): @@ -270,91 +300,107 @@ class TestBinReprIntegration(object): @pytest.fixture def list(request): return values - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_hello(): assert 0 == 1 def test_check(list): assert list == [("==", 0, 1)] - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "*test_hello*FAIL*", - "*test_check*PASS*", - ]) + result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"]) def callequal(left, right, verbose=False): config = mock_config() config.verbose = verbose - return plugin.pytest_assertrepr_compare(config, '==', left, right) + return plugin.pytest_assertrepr_compare(config, "==", left, right) class TestAssert_reprcompare(object): + def test_different_types(self): - assert callequal([0, 1], 'foo') is None + assert callequal([0, 1], "foo") is None def test_summary(self): summary = callequal([0, 1], [0, 2])[0] assert len(summary) < 65 def test_text_diff(self): - diff = callequal('spam', 'eggs')[1:] - assert '- spam' in diff - assert '+ eggs' in diff + diff = callequal("spam", "eggs")[1:] + assert "- spam" in diff + assert "+ eggs" in diff def test_text_skipping(self): - lines = callequal('a' * 50 + 'spam', 'a' * 50 + 'eggs') - assert 'Skipping' in lines[1] + lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs") + assert "Skipping" in lines[1] for line in lines: - assert 'a' * 50 not in line + assert "a" * 50 not in line def test_text_skipping_verbose(self): - lines = callequal('a' * 50 + 'spam', 'a' * 50 + 'eggs', verbose=True) - assert '- ' + 'a' * 50 + 'spam' in lines - assert '+ ' + 'a' * 50 + 'eggs' in lines + lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs", verbose=True) + assert "- " + "a" * 50 + "spam" in lines + assert "+ " + "a" * 50 + "eggs" in lines def test_multiline_text_diff(self): - left = 'foo\nspam\nbar' - right = 'foo\neggs\nbar' + left = "foo\nspam\nbar" + right = "foo\neggs\nbar" diff = callequal(left, right) - assert '- spam' in diff - assert '+ eggs' in diff + assert "- spam" in diff + assert "+ eggs" in diff def test_list(self): expl = callequal([0, 1], [0, 2]) assert len(expl) > 1 @pytest.mark.parametrize( - ['left', 'right', 'expected'], [ - ([0, 1], [0, 2], """ + ["left", "right", "expected"], + [ + ( + [0, 1], + [0, 2], + """ Full diff: - [0, 1] ? ^ + [0, 2] ? ^ - """), - ({0: 1}, {0: 2}, """ + """, + ), + ( + {0: 1}, + {0: 2}, + """ Full diff: - {0: 1} ? ^ + {0: 2} ? ^ - """), - (set([0, 1]), set([0, 2]), """ + """, + ), + ( + {0, 1}, + {0, 2}, + """ Full diff: - set([0, 1]) ? ^ + set([0, 2]) ? ^ - """ if not PY3 else """ + """ + if not PY3 + else """ Full diff: - {0, 1} ? ^ + {0, 2} ? ^ - """) - ] + """, + ), + ], ) def test_iterable_full_diff(self, left, right, expected): """Test the full diff assertion failure explanation. @@ -363,8 +409,8 @@ class TestAssert_reprcompare(object): when verbose is True, then ndiff of the pprint is returned. """ expl = callequal(left, right, verbose=False) - assert expl[-1] == 'Use -v to get the full diff' - expl = '\n'.join(callequal(left, right, verbose=True)) + assert expl[-1] == "Use -v to get the full diff" + expl = "\n".join(callequal(left, right, verbose=True)) assert expl.endswith(textwrap.dedent(expected).strip()) def test_list_different_lengths(self): @@ -374,48 +420,46 @@ class TestAssert_reprcompare(object): assert len(expl) > 1 def test_dict(self): - expl = callequal({'a': 0}, {'a': 1}) + expl = callequal({"a": 0}, {"a": 1}) assert len(expl) > 1 def test_dict_omitting(self): - lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}) - assert lines[1].startswith('Omitting 1 identical item') - assert 'Common items' not in lines + lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}) + assert lines[1].startswith("Omitting 1 identical item") + assert "Common items" not in lines for line in lines[1:]: - assert 'b' not in line + assert "b" not in line def test_dict_omitting_with_verbosity_1(self): """ Ensure differing items are visible for verbosity=1 (#1512) """ - lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=1) - assert lines[1].startswith('Omitting 1 identical item') - assert lines[2].startswith('Differing items') + lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}, verbose=1) + assert lines[1].startswith("Omitting 1 identical item") + assert lines[2].startswith("Differing items") assert lines[3] == "{'a': 0} != {'a': 1}" - assert 'Common items' not in lines + assert "Common items" not in lines def test_dict_omitting_with_verbosity_2(self): - lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=2) - assert lines[1].startswith('Common items:') - assert 'Omitting' not in lines[1] + lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}, verbose=2) + assert lines[1].startswith("Common items:") + assert "Omitting" not in lines[1] assert lines[2] == "{'b': 1}" def test_set(self): - expl = callequal(set([0, 1]), set([0, 2])) + expl = callequal({0, 1}, {0, 2}) assert len(expl) > 1 def test_frozenzet(self): - expl = callequal(frozenset([0, 1]), set([0, 2])) + expl = callequal(frozenset([0, 1]), {0, 2}) assert len(expl) > 1 def test_Sequence(self): - col = py.builtin._tryimport( - "collections.abc", - "collections", - "sys") + col = py.builtin._tryimport("collections.abc", "collections", "sys") if not hasattr(col, "MutableSequence"): pytest.skip("cannot import MutableSequence") MutableSequence = col.MutableSequence class TestSequence(MutableSequence): # works with a Sequence subclass + def __init__(self, iterable): self.elements = list(iterable) @@ -444,62 +488,71 @@ class TestAssert_reprcompare(object): assert len(expl) > 1 def test_list_bad_repr(self): + class A(object): + def __repr__(self): raise ValueError(42) + expl = callequal([], [A()]) - assert 'ValueError' in "".join(expl) - expl = callequal({}, {'1': A()}) - assert 'faulty' in "".join(expl) + assert "ValueError" in "".join(expl) + expl = callequal({}, {"1": A()}) + assert "faulty" in "".join(expl) def test_one_repr_empty(self): """ the faulty empty string repr did trigger - a unbound local error in _diff_text + an unbound local error in _diff_text """ + class A(str): + def __repr__(self): - return '' - expl = callequal(A(), '') + return "" + + expl = callequal(A(), "") assert not expl def test_repr_no_exc(self): - expl = ' '.join(callequal('foo', 'bar')) - assert 'raised in repr()' not in expl + expl = " ".join(callequal("foo", "bar")) + assert "raised in repr()" not in expl def test_unicode(self): - left = py.builtin._totext('£€', 'utf-8') - right = py.builtin._totext('£', 'utf-8') + left = py.builtin._totext("£€", "utf-8") + right = py.builtin._totext("£", "utf-8") expl = callequal(left, right) - assert expl[0] == py.builtin._totext("'£€' == '£'", 'utf-8') - assert expl[1] == py.builtin._totext('- £€', 'utf-8') - assert expl[2] == py.builtin._totext('+ £', 'utf-8') + assert expl[0] == py.builtin._totext("'£€' == '£'", "utf-8") + assert expl[1] == py.builtin._totext("- £€", "utf-8") + assert expl[2] == py.builtin._totext("+ £", "utf-8") def test_nonascii_text(self): """ :issue: 877 non ascii python2 str caused a UnicodeDecodeError """ + class A(str): + def __repr__(self): - return '\xff' - expl = callequal(A(), '1') + return "\xff" + + expl = callequal(A(), "1") assert expl def test_format_nonascii_explanation(self): - assert util.format_explanation('λ') + assert util.format_explanation("λ") def test_mojibake(self): # issue 429 - left = 'e' - right = '\xc3\xa9' - if not isinstance(left, py.builtin.bytes): - left = py.builtin.bytes(left, 'utf-8') - right = py.builtin.bytes(right, 'utf-8') + left = "e" + right = "\xc3\xa9" + if not isinstance(left, bytes): + left = bytes(left, "utf-8") + right = bytes(right, "utf-8") expl = callequal(left, right) for line in expl: assert isinstance(line, py.builtin.text) - msg = py.builtin._totext('\n').join(expl) + msg = py.builtin._totext("\n").join(expl) assert msg @@ -507,91 +560,87 @@ class TestFormatExplanation(object): def test_special_chars_full(self, testdir): # Issue 453, for the bug this would raise IndexError - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): assert '\\n}' == '' - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*AssertionError*", - ]) + result.stdout.fnmatch_lines(["*AssertionError*"]) def test_fmt_simple(self): - expl = 'assert foo' - assert util.format_explanation(expl) == 'assert foo' + expl = "assert foo" + assert util.format_explanation(expl) == "assert foo" def test_fmt_where(self): - expl = '\n'.join(['assert 1', - '{1 = foo', - '} == 2']) - res = '\n'.join(['assert 1 == 2', - ' + where 1 = foo']) + expl = "\n".join(["assert 1", "{1 = foo", "} == 2"]) + res = "\n".join(["assert 1 == 2", " + where 1 = foo"]) assert util.format_explanation(expl) == res def test_fmt_and(self): - expl = '\n'.join(['assert 1', - '{1 = foo', - '} == 2', - '{2 = bar', - '}']) - res = '\n'.join(['assert 1 == 2', - ' + where 1 = foo', - ' + and 2 = bar']) + expl = "\n".join(["assert 1", "{1 = foo", "} == 2", "{2 = bar", "}"]) + res = "\n".join(["assert 1 == 2", " + where 1 = foo", " + and 2 = bar"]) assert util.format_explanation(expl) == res def test_fmt_where_nested(self): - expl = '\n'.join(['assert 1', - '{1 = foo', - '{foo = bar', - '}', - '} == 2']) - res = '\n'.join(['assert 1 == 2', - ' + where 1 = foo', - ' + where foo = bar']) + expl = "\n".join(["assert 1", "{1 = foo", "{foo = bar", "}", "} == 2"]) + res = "\n".join(["assert 1 == 2", " + where 1 = foo", " + where foo = bar"]) assert util.format_explanation(expl) == res def test_fmt_newline(self): - expl = '\n'.join(['assert "foo" == "bar"', - '~- foo', - '~+ bar']) - res = '\n'.join(['assert "foo" == "bar"', - ' - foo', - ' + bar']) + expl = "\n".join(['assert "foo" == "bar"', "~- foo", "~+ bar"]) + res = "\n".join(['assert "foo" == "bar"', " - foo", " + bar"]) assert util.format_explanation(expl) == res def test_fmt_newline_escaped(self): - expl = '\n'.join(['assert foo == bar', - 'baz']) - res = 'assert foo == bar\\nbaz' + expl = "\n".join(["assert foo == bar", "baz"]) + res = "assert foo == bar\\nbaz" assert util.format_explanation(expl) == res def test_fmt_newline_before_where(self): - expl = '\n'.join(['the assertion message here', - '>assert 1', - '{1 = foo', - '} == 2', - '{2 = bar', - '}']) - res = '\n'.join(['the assertion message here', - 'assert 1 == 2', - ' + where 1 = foo', - ' + and 2 = bar']) + expl = "\n".join( + [ + "the assertion message here", + ">assert 1", + "{1 = foo", + "} == 2", + "{2 = bar", + "}", + ] + ) + res = "\n".join( + [ + "the assertion message here", + "assert 1 == 2", + " + where 1 = foo", + " + and 2 = bar", + ] + ) assert util.format_explanation(expl) == res def test_fmt_multi_newline_before_where(self): - expl = '\n'.join(['the assertion', - '~message here', - '>assert 1', - '{1 = foo', - '} == 2', - '{2 = bar', - '}']) - res = '\n'.join(['the assertion', - ' message here', - 'assert 1 == 2', - ' + where 1 = foo', - ' + and 2 = bar']) + expl = "\n".join( + [ + "the assertion", + "~message here", + ">assert 1", + "{1 = foo", + "} == 2", + "{2 = bar", + "}", + ] + ) + res = "\n".join( + [ + "the assertion", + " message here", + "assert 1 == 2", + " + where 1 = foo", + " + and 2 = bar", + ] + ) assert util.format_explanation(expl) == res @@ -609,58 +658,58 @@ class TestTruncateExplanation(object): assert result == expl def test_doesnt_truncate_at_when_input_is_5_lines_and_LT_max_chars(self): - expl = ['a' * 100 for x in range(5)] + expl = ["a" * 100 for x in range(5)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80) assert result == expl def test_truncates_at_8_lines_when_given_list_of_empty_strings(self): - expl = ['' for x in range(50)] + expl = ["" for x in range(50)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100) assert result != expl assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "43 lines hidden" in result[-1] - last_line_before_trunc_msg = result[- self.LINES_IN_TRUNCATION_MSG - 1] + last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_8_lines_when_first_8_lines_are_LT_max_chars(self): - expl = ['a' for x in range(100)] + expl = ["a" for x in range(100)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80) assert result != expl assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "93 lines hidden" in result[-1] - last_line_before_trunc_msg = result[- self.LINES_IN_TRUNCATION_MSG - 1] + last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_8_lines_when_first_8_lines_are_EQ_max_chars(self): - expl = ['a' * 80 for x in range(16)] + expl = ["a" * 80 for x in range(16)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80) assert result != expl assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "9 lines hidden" in result[-1] - last_line_before_trunc_msg = result[- self.LINES_IN_TRUNCATION_MSG - 1] + last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_4_lines_when_first_4_lines_are_GT_max_chars(self): - expl = ['a' * 250 for x in range(10)] + expl = ["a" * 250 for x in range(10)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=999) assert result != expl assert len(result) == 4 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "7 lines hidden" in result[-1] - last_line_before_trunc_msg = result[- self.LINES_IN_TRUNCATION_MSG - 1] + last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_1_line_when_first_line_is_GT_max_chars(self): - expl = ['a' * 250 for x in range(1000)] + expl = ["a" * 250 for x in range(1000)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100) assert result != expl assert len(result) == 1 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "1000 lines hidden" in result[-1] - last_line_before_trunc_msg = result[- self.LINES_IN_TRUNCATION_MSG - 1] + last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_full_output_truncated(self, monkeypatch, testdir): @@ -669,128 +718,160 @@ class TestTruncateExplanation(object): line_count = 7 line_len = 100 expected_truncated_lines = 2 - testdir.makepyfile(r""" + testdir.makepyfile( + r""" def test_many_lines(): a = list([str(i)[0] * %d for i in range(%d)]) b = a[::2] a = '\n'.join(map(str, a)) b = '\n'.join(map(str, b)) assert a == b - """ % (line_len, line_count)) - monkeypatch.delenv('CI', raising=False) + """ + % (line_len, line_count) + ) + monkeypatch.delenv("CI", raising=False) result = testdir.runpytest() # without -vv, truncate the message showing a few diff lines only - result.stdout.fnmatch_lines([ - "*- 1*", - "*- 3*", - "*- 5*", - "*truncated (%d lines hidden)*use*-vv*" % expected_truncated_lines, - ]) + result.stdout.fnmatch_lines( + [ + "*- 1*", + "*- 3*", + "*- 5*", + "*truncated (%d lines hidden)*use*-vv*" % expected_truncated_lines, + ] + ) - result = testdir.runpytest('-vv') - result.stdout.fnmatch_lines([ - "* 6*", - ]) + result = testdir.runpytest("-vv") + result.stdout.fnmatch_lines(["* 6*"]) - monkeypatch.setenv('CI', '1') + monkeypatch.setenv("CI", "1") result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "* 6*", - ]) + result.stdout.fnmatch_lines(["* 6*"]) def test_python25_compile_issue257(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_rewritten(): assert 1 == 2 # some comment - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *E*assert 1 == 2* *1 failed* - """) + """ + ) def test_rewritten(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_rewritten(): assert "@py_builtins" in globals() - """) + """ + ) assert testdir.runpytest().ret == 0 def test_reprcompare_notin(mock_config): detail = plugin.pytest_assertrepr_compare( - mock_config, 'not in', 'foo', 'aaafoobbb')[1:] - assert detail == ["'foo' is contained here:", ' aaafoobbb', '? +++'] + mock_config, "not in", "foo", "aaafoobbb" + )[ + 1: + ] + assert detail == ["'foo' is contained here:", " aaafoobbb", "? +++"] + + +def test_reprcompare_whitespaces(mock_config): + detail = plugin.pytest_assertrepr_compare(mock_config, "==", "\r\n", "\n") + assert ( + detail + == [ + r"'\r\n' == '\n'", + r"Strings contain only whitespace, escaping them using repr()", + r"- '\r\n'", + r"? --", + r"+ '\n'", + ] + ) def test_pytest_assertrepr_compare_integration(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(): x = set(range(100)) y = x.copy() y.remove(50) assert x == y - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*def test_hello():*", - "*assert x == y*", - "*E*Extra items*left*", - "*E*50*", - ]) + result.stdout.fnmatch_lines( + ["*def test_hello():*", "*assert x == y*", "*E*Extra items*left*", "*E*50*"] + ) def test_sequence_comparison_uses_repr(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(): x = set("hello x") y = set("hello y") assert x == y - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*def test_hello():*", - "*assert x == y*", - "*E*Extra items*left*", - "*E*'x'*", - "*E*Extra items*right*", - "*E*'y'*", - ]) + result.stdout.fnmatch_lines( + [ + "*def test_hello():*", + "*assert x == y*", + "*E*Extra items*left*", + "*E*'x'*", + "*E*Extra items*right*", + "*E*'y'*", + ] + ) def test_assertrepr_loaded_per_dir(testdir): - testdir.makepyfile(test_base=['def test_base(): assert 1 == 2']) - a = testdir.mkdir('a') - a_test = a.join('test_a.py') - a_test.write('def test_a(): assert 1 == 2') - a_conftest = a.join('conftest.py') + testdir.makepyfile(test_base=["def test_base(): assert 1 == 2"]) + a = testdir.mkdir("a") + a_test = a.join("test_a.py") + a_test.write("def test_a(): assert 1 == 2") + a_conftest = a.join("conftest.py") a_conftest.write('def pytest_assertrepr_compare(): return ["summary a"]') - b = testdir.mkdir('b') - b_test = b.join('test_b.py') - b_test.write('def test_b(): assert 1 == 2') - b_conftest = b.join('conftest.py') + b = testdir.mkdir("b") + b_test = b.join("test_b.py") + b_test.write("def test_b(): assert 1 == 2") + b_conftest = b.join("conftest.py") b_conftest.write('def pytest_assertrepr_compare(): return ["summary b"]') result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*def test_base():*', - '*E*assert 1 == 2*', - '*def test_a():*', - '*E*assert summary a*', - '*def test_b():*', - '*E*assert summary b*']) + result.stdout.fnmatch_lines( + [ + "*def test_base():*", + "*E*assert 1 == 2*", + "*def test_a():*", + "*E*assert summary a*", + "*def test_b():*", + "*E*assert summary b*", + ] + ) def test_assertion_options(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(): x = 3 assert x == 4 - """) + """ + ) result = testdir.runpytest() assert "3 == 4" in result.stdout.str() result = testdir.runpytest_subprocess("--assert=plain") @@ -798,72 +879,82 @@ def test_assertion_options(testdir): def test_triple_quoted_string_issue113(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(): assert "" == ''' - '''""") + '''""" + ) result = testdir.runpytest("--fulltrace") - result.stdout.fnmatch_lines([ - "*1 failed*", - ]) - assert 'SyntaxError' not in result.stdout.str() + result.stdout.fnmatch_lines(["*1 failed*"]) + assert "SyntaxError" not in result.stdout.str() def test_traceback_failure(testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ def g(): return 2 def f(x): assert x == g() def test_onefails(): f(3) - """) + """ + ) result = testdir.runpytest(p1, "--tb=long") - result.stdout.fnmatch_lines([ - "*test_traceback_failure.py F", - "====* FAILURES *====", - "____*____", - "", - " def test_onefails():", - "> f(3)", - "", - "*test_*.py:6: ", - "_ _ _ *", - # "", - " def f(x):", - "> assert x == g()", - "E assert 3 == 2", - "E + where 2 = g()", - "", - "*test_traceback_failure.py:4: AssertionError" - ]) + result.stdout.fnmatch_lines( + [ + "*test_traceback_failure.py F*", + "====* FAILURES *====", + "____*____", + "", + " def test_onefails():", + "> f(3)", + "", + "*test_*.py:6: ", + "_ _ _ *", + # "", + " def f(x):", + "> assert x == g()", + "E assert 3 == 2", + "E + where 2 = g()", + "", + "*test_traceback_failure.py:4: AssertionError", + ] + ) result = testdir.runpytest(p1) # "auto" - result.stdout.fnmatch_lines([ - "*test_traceback_failure.py F", - "====* FAILURES *====", - "____*____", - "", - " def test_onefails():", - "> f(3)", - "", - "*test_*.py:6: ", - "", - " def f(x):", - "> assert x == g()", - "E assert 3 == 2", - "E + where 2 = g()", - "", - "*test_traceback_failure.py:4: AssertionError" - ]) + result.stdout.fnmatch_lines( + [ + "*test_traceback_failure.py F*", + "====* FAILURES *====", + "____*____", + "", + " def test_onefails():", + "> f(3)", + "", + "*test_*.py:6: ", + "", + " def f(x):", + "> assert x == g()", + "E assert 3 == 2", + "E + where 2 = g()", + "", + "*test_traceback_failure.py:4: AssertionError", + ] + ) -@pytest.mark.skipif(sys.version_info[:2] <= (3, 3), reason='Python 3.4+ shows chained exceptions on multiprocess') +@pytest.mark.skipif( + sys.version_info[:2] <= (3, 3), + reason="Python 3.4+ shows chained exceptions on multiprocess", +) def test_exception_handling_no_traceback(testdir): """ Handle chain exceptions in tasks submitted by the multiprocess module (#1984). """ - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ from multiprocessing import Pool def process_task(n): @@ -876,64 +967,76 @@ def test_exception_handling_no_traceback(testdir): def test_multitask_job(): multitask_job() - """) + """ + ) result = testdir.runpytest(p1, "--tb=long") - result.stdout.fnmatch_lines([ - "====* FAILURES *====", - "*multiprocessing.pool.RemoteTraceback:*", - "Traceback (most recent call last):", - "*assert n == 10", - "The above exception was the direct cause of the following exception:", - "> * multitask_job()", - ]) + result.stdout.fnmatch_lines( + [ + "====* FAILURES *====", + "*multiprocessing.pool.RemoteTraceback:*", + "Traceback (most recent call last):", + "*assert n == 10", + "The above exception was the direct cause of the following exception:", + "> * multitask_job()", + ] + ) -@pytest.mark.skipif("'__pypy__' in sys.builtin_module_names or sys.platform.startswith('java')") +@pytest.mark.skipif( + "'__pypy__' in sys.builtin_module_names or sys.platform.startswith('java')" +) def test_warn_missing(testdir): testdir.makepyfile("") result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h") - result.stderr.fnmatch_lines([ - "*WARNING*assert statements are not executed*", - ]) + result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"]) result = testdir.run(sys.executable, "-OO", "-m", "pytest") - result.stderr.fnmatch_lines([ - "*WARNING*assert statements are not executed*", - ]) + result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"]) def test_recursion_source_decode(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_something(): pass - """) - testdir.makeini(""" + """ + ) + testdir.makeini( + """ [pytest] python_files = *.py - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ - """) + """ + ) def test_AssertionError_message(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_hello(): x,y = 1,2 assert 0, (x,y) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *def test_hello* *assert 0, (x,y)* *AssertionError: (1, 2)* - """) + """ + ) -@pytest.mark.skipif(PY3, reason='This bug does not exist on PY3') +@pytest.mark.skipif(PY3, reason="This bug does not exist on PY3") def test_set_with_unsortable_elements(): # issue #718 class UnsortableKey(object): + def __init__(self, name): self.name = name @@ -941,7 +1044,7 @@ def test_set_with_unsortable_elements(): raise RuntimeError() def __repr__(self): - return 'repr({0})'.format(self.name) + return "repr({})".format(self.name) def __eq__(self, other): return self.name == other.name @@ -949,12 +1052,13 @@ def test_set_with_unsortable_elements(): def __hash__(self): return hash(self.name) - left_set = set(UnsortableKey(str(i)) for i in range(1, 3)) - right_set = set(UnsortableKey(str(i)) for i in range(2, 4)) + left_set = {UnsortableKey(str(i)) for i in range(1, 3)} + right_set = {UnsortableKey(str(i)) for i in range(2, 4)} expl = callequal(left_set, right_set, verbose=True) # skip first line because it contains the "construction" of the set, which does not have a guaranteed order expl = expl[1:] - dedent = textwrap.dedent(""" + dedent = textwrap.dedent( + """ Extra items in the left set: repr(1) Extra items in the right set: @@ -963,86 +1067,106 @@ def test_set_with_unsortable_elements(): - repr(1) repr(2) + repr(3) - """).strip() - assert '\n'.join(expl) == dedent + """ + ).strip() + assert "\n".join(expl) == dedent def test_diff_newline_at_end(monkeypatch, testdir): - testdir.makepyfile(r""" + testdir.makepyfile( + r""" def test_diff(): assert 'asdf' == 'asdf\n' - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(r""" + result.stdout.fnmatch_lines( + r""" *assert 'asdf' == 'asdf\n' * - asdf * + asdf * ? + - """) + """ + ) def test_assert_tuple_warning(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_tuple(): assert(False, 'you shall not pass') - """) - result = testdir.runpytest('-rw') - result.stdout.fnmatch_lines([ - '*test_assert_tuple_warning.py:2', - '*assertion is always true*', - ]) + """ + ) + result = testdir.runpytest("-rw") + result.stdout.fnmatch_lines( + ["*test_assert_tuple_warning.py:2", "*assertion is always true*"] + ) def test_assert_indirect_tuple_no_warning(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_tuple(): tpl = ('foo', 'bar') assert tpl - """) - result = testdir.runpytest('-rw') - output = '\n'.join(result.stdout.lines) - assert 'WR1' not in output + """ + ) + result = testdir.runpytest("-rw") + output = "\n".join(result.stdout.lines) + assert "WR1" not in output def test_assert_with_unicode(monkeypatch, testdir): - testdir.makepyfile(u""" + testdir.makepyfile( + u""" # -*- coding: utf-8 -*- def test_unicode(): assert u'유니코드' == u'Unicode' - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['*AssertionError*']) + result.stdout.fnmatch_lines(["*AssertionError*"]) def test_raise_unprintable_assertion_error(testdir): - testdir.makepyfile(r""" + testdir.makepyfile( + r""" def test_raise_assertion_error(): raise AssertionError('\xff') - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([r"> raise AssertionError('\xff')", 'E AssertionError: *']) + result.stdout.fnmatch_lines( + [r"> raise AssertionError('\xff')", "E AssertionError: *"] + ) def test_raise_assertion_error_raisin_repr(testdir): - testdir.makepyfile(u""" + testdir.makepyfile( + u""" class RaisingRepr(object): def __repr__(self): raise Exception() def test_raising_repr(): raise AssertionError(RaisingRepr()) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['E AssertionError: ']) + result.stdout.fnmatch_lines( + ["E AssertionError: "] + ) def test_issue_1944(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def f(): return assert f() == 10 - """) + """ + ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 error*"]) assert "AttributeError: 'Module' object has no attribute '_obj'" not in result.stdout.str() diff --git a/third_party/python/pytest/testing/test_assertrewrite.py b/third_party/python/pytest/testing/test_assertrewrite.py index 31e99605218b..144f625bc6c3 100644 --- a/third_party/python/pytest/testing/test_assertrewrite.py +++ b/third_party/python/pytest/testing/test_assertrewrite.py @@ -5,13 +5,18 @@ import os import py_compile import stat import sys +import textwrap import zipfile import py import pytest import _pytest._code from _pytest.assertion import util -from _pytest.assertion.rewrite import rewrite_asserts, PYTEST_TAG, AssertionRewritingHook +from _pytest.assertion.rewrite import ( + rewrite_asserts, + PYTEST_TAG, + AssertionRewritingHook, +) from _pytest.main import EXIT_NOTESTSCOLLECTED ast = pytest.importorskip("ast") @@ -38,7 +43,7 @@ def rewrite(src): def getmsg(f, extra_ns=None, must_pass=False): """Rewrite the assertions in f, run it, and get the failure message.""" - src = '\n'.join(_pytest._code.Code(f).source().lines) + src = "\n".join(_pytest._code.Code(f).source().lines) mod = rewrite(src) code = compile(mod, "", "exec") ns = {} @@ -60,18 +65,35 @@ def getmsg(f, extra_ns=None, must_pass=False): pytest.fail("function didn't raise at all") +def adjust_body_for_new_docstring_in_module_node(m): + """Module docstrings in 3.8 are part of Module node. + This was briefly in 3.7 as well but got reverted in beta 5. + + It's not in the body so we remove it so the following body items have + the same indexes on all Python versions: + + TODO: + + We have a complicated sys.version_info if in here to ease testing on + various Python 3.7 versions, but we should remove the 3.7 check after + 3.7 is released as stable to make this check more straightforward. + """ + if ( + sys.version_info < (3, 8) + and not ((3, 7) <= sys.version_info <= (3, 7, 0, "beta", 4)) + ): + assert len(m.body) > 1 + assert isinstance(m.body[0], ast.Expr) + assert isinstance(m.body[0].value, ast.Str) + del m.body[0] + + class TestAssertionRewrite(object): def test_place_initial_imports(self): s = """'Doc string'\nother = stuff""" m = rewrite(s) - # Module docstrings in 3.7 are part of Module node, it's not in the body - # so we remove it so the following body items have the same indexes on - # all Python versions - if sys.version_info < (3, 7): - assert isinstance(m.body[0], ast.Expr) - assert isinstance(m.body[0].value, ast.Str) - del m.body[0] + adjust_body_for_new_docstring_in_module_node(m) for imp in m.body[0:2]: assert isinstance(imp, ast.Import) assert imp.lineno == 2 @@ -87,10 +109,7 @@ class TestAssertionRewrite(object): assert isinstance(m.body[3], ast.Expr) s = """'doc string'\nfrom __future__ import with_statement""" m = rewrite(s) - if sys.version_info < (3, 7): - assert isinstance(m.body[0], ast.Expr) - assert isinstance(m.body[0].value, ast.Str) - del m.body[0] + adjust_body_for_new_docstring_in_module_node(m) assert isinstance(m.body[0], ast.ImportFrom) for imp in m.body[1:3]: assert isinstance(imp, ast.Import) @@ -98,10 +117,7 @@ class TestAssertionRewrite(object): assert imp.col_offset == 0 s = """'doc string'\nfrom __future__ import with_statement\nother""" m = rewrite(s) - if sys.version_info < (3, 7): - assert isinstance(m.body[0], ast.Expr) - assert isinstance(m.body[0].value, ast.Str) - del m.body[0] + adjust_body_for_new_docstring_in_module_node(m) assert isinstance(m.body[0], ast.ImportFrom) for imp in m.body[1:3]: assert isinstance(imp, ast.Import) @@ -119,18 +135,25 @@ class TestAssertionRewrite(object): def test_dont_rewrite(self): s = """'PYTEST_DONT_REWRITE'\nassert 14""" m = rewrite(s) - if sys.version_info < (3, 7): - assert len(m.body) == 2 - assert isinstance(m.body[0], ast.Expr) - assert isinstance(m.body[0].value, ast.Str) - del m.body[0] - else: - assert len(m.body) == 1 + adjust_body_for_new_docstring_in_module_node(m) + assert len(m.body) == 1 assert m.body[0].msg is None + def test_dont_rewrite_plugin(self, testdir): + contents = { + "conftest.py": "pytest_plugins = 'plugin'; import plugin", + "plugin.py": "'PYTEST_DONT_REWRITE'", + "test_foo.py": "def test_foo(): pass", + } + testdir.makepyfile(**contents) + result = testdir.runpytest_subprocess() + assert "warnings" not in "".join(result.outlines) + def test_name(self): + def f(): assert False + assert getmsg(f) == "assert False" def f(): @@ -158,72 +181,77 @@ class TestAssertionRewrite(object): assert getmsg(f, {"cls": X}) == "assert cls == 42" def test_assert_already_has_message(self): + def f(): assert False, "something bad!" + assert getmsg(f) == "AssertionError: something bad!\nassert False" def test_assertion_message(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): assert 1 == 2, "The failure message" - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*AssertionError*The failure message*", - "*assert 1 == 2*", - ]) + result.stdout.fnmatch_lines( + ["*AssertionError*The failure message*", "*assert 1 == 2*"] + ) def test_assertion_message_multiline(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): assert 1 == 2, "A multiline\\nfailure message" - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*AssertionError*A multiline*", - "*failure message*", - "*assert 1 == 2*", - ]) + result.stdout.fnmatch_lines( + ["*AssertionError*A multiline*", "*failure message*", "*assert 1 == 2*"] + ) def test_assertion_message_tuple(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): assert 1 == 2, (1, 2) - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*AssertionError*%s*" % repr((1, 2)), - "*assert 1 == 2*", - ]) + result.stdout.fnmatch_lines( + ["*AssertionError*%s*" % repr((1, 2)), "*assert 1 == 2*"] + ) def test_assertion_message_expr(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): assert 1 == 2, 1 + 2 - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*AssertionError*3*", - "*assert 1 == 2*", - ]) + result.stdout.fnmatch_lines(["*AssertionError*3*", "*assert 1 == 2*"]) def test_assertion_message_escape(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): assert 1 == 2, 'To be escaped: %' - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*AssertionError: To be escaped: %", - "*assert 1 == 2", - ]) + result.stdout.fnmatch_lines( + ["*AssertionError: To be escaped: %", "*assert 1 == 2"] + ) def test_boolop(self): + def f(): f = g = False assert f and g @@ -262,14 +290,20 @@ class TestAssertionRewrite(object): def f(): assert x() and x() - assert getmsg(f, {"x": x}) == """assert (False) + assert ( + getmsg(f, {"x": x}) + == """assert (False) + where False = x()""" + ) def f(): assert False or x() - assert getmsg(f, {"x": x}) == """assert (False or False) + assert ( + getmsg(f, {"x": x}) + == """assert (False or False) + where False = x()""" + ) def f(): assert 1 in {} and 2 in {} @@ -297,6 +331,7 @@ class TestAssertionRewrite(object): getmsg(f, must_pass=True) def test_short_circuit_evaluation(self): + def f(): assert True or explode # noqa @@ -309,6 +344,7 @@ class TestAssertionRewrite(object): getmsg(f, must_pass=True) def test_unary_op(self): + def f(): x = True assert not x @@ -334,6 +370,7 @@ class TestAssertionRewrite(object): assert getmsg(f) == "assert (+0 + 0)" def test_binary_op(self): + def f(): x = 1 y = -1 @@ -343,9 +380,11 @@ class TestAssertionRewrite(object): def f(): assert not 5 % 4 + assert getmsg(f) == "assert not (5 % 4)" def test_boolop_percent(self): + def f(): assert 3 % 2 and False @@ -353,11 +392,13 @@ class TestAssertionRewrite(object): def f(): assert False or 4 % 2 + assert getmsg(f) == "assert (False or (4 % 2))" @pytest.mark.skipif("sys.version_info < (3,5)") def test_at_operator_issue1290(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class Matrix(object): def __init__(self, num): self.num = num @@ -365,10 +406,12 @@ class TestAssertionRewrite(object): return self.num * other.num def test_multmat_operator(): - assert Matrix(2) @ Matrix(3) == 6""") + assert Matrix(2) @ Matrix(3) == 6""" + ) testdir.runpytest().assert_outcomes(passed=1) def test_call(self): + def g(a=42, *args, **kwargs): return False @@ -377,48 +420,70 @@ class TestAssertionRewrite(object): def f(): assert g() - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g()""" + ) def f(): assert g(1) - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g(1)""" + ) def f(): assert g(1, 2) - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g(1, 2)""" + ) def f(): assert g(1, g=42) - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g(1, g=42)""" + ) def f(): assert g(1, 3, g=23) - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g(1, 3, g=23)""" + ) def f(): seq = [1, 2, 3] assert g(*seq) - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g(*[1, 2, 3])""" + ) def f(): x = "a" assert g(**{x: 2}) - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = g(**{'a': 2})""" + ) def test_attribute(self): + class X(object): g = 3 @@ -427,15 +492,21 @@ class TestAssertionRewrite(object): def f(): assert not x.g # noqa - assert getmsg(f, ns) == """assert not 3 + assert ( + getmsg(f, ns) + == """assert not 3 + where 3 = x.g""" + ) def f(): x.a = False # noqa - assert x.a # noqa + assert x.a # noqa - assert getmsg(f, ns) == """assert False + assert ( + getmsg(f, ns) + == """assert False + where False = x.a""" + ) def test_comparisons(self): @@ -476,10 +547,13 @@ class TestAssertionRewrite(object): values = list(range(10)) assert len(values) == 11 - assert getmsg(f).startswith("""assert 10 == 11 - + where 10 = len([""") + assert getmsg(f).startswith( + """assert 10 == 11 + + where 10 = len([""" + ) def test_custom_reprcompare(self, monkeypatch): + def my_reprcompare(op, left, right): return "42" @@ -501,7 +575,9 @@ class TestAssertionRewrite(object): assert getmsg(f) == "assert 5 <= 4" def test_assert_raising_nonzero_in_comparison(self): + def f(): + class A(object): def __nonzero__(self): @@ -521,13 +597,16 @@ class TestAssertionRewrite(object): assert " < 0" in getmsg(f) def test_formatchar(self): + def f(): assert "%test" == "test" assert getmsg(f).startswith("assert '%test' == 'test'") def test_custom_repr(self): + def f(): + class Foo(object): a = 1 @@ -544,18 +623,22 @@ class TestRewriteOnImport(object): def test_pycache_is_a_file(self, testdir): testdir.tmpdir.join("__pycache__").write("Hello") - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_rewritten(): - assert "@py_builtins" in globals()""") + assert "@py_builtins" in globals()""" + ) assert testdir.runpytest().ret == 0 def test_pycache_is_readonly(self, testdir): cache = testdir.tmpdir.mkdir("__pycache__") old_mode = cache.stat().mode cache.chmod(old_mode ^ stat.S_IWRITE) - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_rewritten(): - assert "@py_builtins" in globals()""") + assert "@py_builtins" in globals()""" + ) try: assert testdir.runpytest().ret == 0 finally: @@ -571,19 +654,28 @@ class TestRewriteOnImport(object): finally: f.close() z.chmod(256) - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys sys.path.append(%r) - import test_gum.test_lizard""" % (z_fn,)) + import test_gum.test_lizard""" + % (z_fn,) + ) assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED def test_readonly(self, testdir): sub = testdir.mkdir("testing") sub.join("test_readonly.py").write( - py.builtin._totext(""" + py.builtin._totext( + """ def test_rewritten(): assert "@py_builtins" in globals() - """).encode("utf-8"), "wb") + """ + ).encode( + "utf-8" + ), + "wb", + ) old_mode = sub.stat().mode sub.chmod(320) try: @@ -592,27 +684,33 @@ def test_rewritten(): sub.chmod(old_mode) def test_dont_write_bytecode(self, testdir, monkeypatch): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os def test_no_bytecode(): assert "__pycache__" in __cached__ assert not os.path.exists(__cached__) - assert not os.path.exists(os.path.dirname(__cached__))""") + assert not os.path.exists(os.path.dirname(__cached__))""" + ) monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1") assert testdir.runpytest_subprocess().ret == 0 def test_orphaned_pyc_file(self, testdir): - if sys.version_info < (3, 0) and hasattr(sys, 'pypy_version_info'): + if sys.version_info < (3, 0) and hasattr(sys, "pypy_version_info"): pytest.skip("pypy2 doesn't run orphaned pyc files") - testdir.makepyfile(""" + testdir.makepyfile( + """ import orphan def test_it(): assert orphan.value == 17 - """) - testdir.makepyfile(orphan=""" + """ + ) + testdir.makepyfile( + orphan=""" value = 17 - """) + """ + ) py_compile.compile("orphan.py") os.remove("orphan.py") @@ -628,14 +726,16 @@ def test_rewritten(): @pytest.mark.skipif('"__pypy__" in sys.modules') def test_pyc_vs_pyo(self, testdir, monkeypatch): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_optimized(): "hello" assert test_optimized.__doc__ is None""" - ) - p = py.path.local.make_numbered_dir(prefix="runpytest-", keep=None, - rootdir=testdir.tmpdir) + ) + p = py.path.local.make_numbered_dir( + prefix="runpytest-", keep=None, rootdir=testdir.tmpdir + ) tmp = "--basetemp=%s" % p monkeypatch.setenv("PYTHONOPTIMIZE", "2") monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) @@ -651,9 +751,11 @@ def test_rewritten(): pkg = testdir.tmpdir.join("pkg") pkg.mkdir() pkg.join("__init__.py").ensure() - pkg.join("test_blah.py").write(""" + pkg.join("test_blah.py").write( + """ def test_rewritten(): - assert "@py_builtins" in globals()""") + assert "@py_builtins" in globals()""" + ) assert testdir.runpytest().ret == 0 def test_translate_newlines(self, testdir): @@ -662,11 +764,13 @@ def test_rewritten(): testdir.tmpdir.join("test_newlines.py").write(b, "wb") assert testdir.runpytest().ret == 0 - @pytest.mark.skipif(sys.version_info < (3, 3), - reason='packages without __init__.py not supported on python 2') + @pytest.mark.skipif( + sys.version_info < (3, 4), + reason="packages without __init__.py not supported on python 2", + ) def test_package_without__init__py(self, testdir): - pkg = testdir.mkdir('a_package_without_init_py') - pkg.join('module.py').ensure() + pkg = testdir.mkdir("a_package_without_init_py") + pkg.join("module.py").ensure() testdir.makepyfile("import a_package_without_init_py.module") assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED @@ -677,18 +781,22 @@ def test_rewritten(): def mywarn(code, msg): warnings.append((code, msg)) - monkeypatch.setattr(hook.config, 'warn', mywarn) - hook.mark_rewrite('_pytest') - assert '_pytest' in warnings[0][1] + monkeypatch.setattr(hook.config, "warn", mywarn) + hook.mark_rewrite("_pytest") + assert "_pytest" in warnings[0][1] def test_rewrite_module_imported_from_conftest(self, testdir): - testdir.makeconftest(''' + testdir.makeconftest( + """ import test_rewrite_module_imported - ''') - testdir.makepyfile(test_rewrite_module_imported=''' + """ + ) + testdir.makepyfile( + test_rewrite_module_imported=""" def test_rewritten(): assert "@py_builtins" in globals() - ''') + """ + ) assert testdir.runpytest_subprocess().ret == 0 def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch): @@ -697,46 +805,50 @@ def test_rewritten(): doesn't give false positives (#2005). """ monkeypatch.syspath_prepend(testdir.tmpdir) - testdir.makepyfile(test_remember_rewritten_modules='') + testdir.makepyfile(test_remember_rewritten_modules="") warnings = [] hook = AssertionRewritingHook(pytestconfig) - monkeypatch.setattr(hook.config, 'warn', lambda code, msg: warnings.append(msg)) - hook.find_module('test_remember_rewritten_modules') - hook.load_module('test_remember_rewritten_modules') - hook.mark_rewrite('test_remember_rewritten_modules') - hook.mark_rewrite('test_remember_rewritten_modules') + monkeypatch.setattr(hook.config, "warn", lambda code, msg: warnings.append(msg)) + hook.find_module("test_remember_rewritten_modules") + hook.load_module("test_remember_rewritten_modules") + hook.mark_rewrite("test_remember_rewritten_modules") + hook.mark_rewrite("test_remember_rewritten_modules") assert warnings == [] def test_rewrite_warning_using_pytest_plugins(self, testdir): - testdir.makepyfile(**{ - 'conftest.py': "pytest_plugins = ['core', 'gui', 'sci']", - 'core.py': "", - 'gui.py': "pytest_plugins = ['core', 'sci']", - 'sci.py': "pytest_plugins = ['core']", - 'test_rewrite_warning_pytest_plugins.py': "def test(): pass", - }) + testdir.makepyfile( + **{ + "conftest.py": "pytest_plugins = ['core', 'gui', 'sci']", + "core.py": "", + "gui.py": "pytest_plugins = ['core', 'sci']", + "sci.py": "pytest_plugins = ['core']", + "test_rewrite_warning_pytest_plugins.py": "def test(): pass", + } + ) testdir.chdir() result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(['*= 1 passed in *=*']) - assert 'pytest-warning summary' not in result.stdout.str() + result.stdout.fnmatch_lines(["*= 1 passed in *=*"]) + assert "pytest-warning summary" not in result.stdout.str() def test_rewrite_warning_using_pytest_plugins_env_var(self, testdir, monkeypatch): - monkeypatch.setenv('PYTEST_PLUGINS', 'plugin') - testdir.makepyfile(**{ - 'plugin.py': "", - 'test_rewrite_warning_using_pytest_plugins_env_var.py': """ + monkeypatch.setenv("PYTEST_PLUGINS", "plugin") + testdir.makepyfile( + **{ + "plugin.py": "", + "test_rewrite_warning_using_pytest_plugins_env_var.py": """ import plugin pytest_plugins = ['plugin'] def test(): pass """, - }) + } + ) testdir.chdir() result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(['*= 1 passed in *=*']) - assert 'pytest-warning summary' not in result.stdout.str() + result.stdout.fnmatch_lines(["*= 1 passed in *=*"]) + assert "pytest-warning summary" not in result.stdout.str() - @pytest.mark.skipif(sys.version_info[0] > 2, reason='python 2 only') + @pytest.mark.skipif(sys.version_info[0] > 2, reason="python 2 only") def test_rewrite_future_imports(self, testdir): """Test that rewritten modules don't inherit the __future__ flags from the assertrewrite module. @@ -746,28 +858,32 @@ def test_rewritten(): The test below will fail if __future__.division is enabled """ - testdir.makepyfile(''' + testdir.makepyfile( + """ def test(): x = 1 / 2 assert type(x) is int - ''') + """ + ) result = testdir.runpytest() assert result.ret == 0 class TestAssertionRewriteHookDetails(object): + def test_loader_is_package_false_for_module(self, testdir): - testdir.makepyfile(test_fun=""" + testdir.makepyfile( + test_fun=""" def test_loader(): assert not __loader__.is_package(__name__) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "* 1 passed*", - ]) + result.stdout.fnmatch_lines(["* 1 passed*"]) def test_loader_is_package_true_for_package(self, testdir): - testdir.makepyfile(test_fun=""" + testdir.makepyfile( + test_fun=""" def test_loader(): assert not __loader__.is_package(__name__) @@ -776,12 +892,11 @@ class TestAssertionRewriteHookDetails(object): def test_missing(): assert not __loader__.is_package('pytest_not_there') - """) - testdir.mkpydir('fun') + """ + ) + testdir.mkpydir("fun") result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '* 3 passed*', - ]) + result.stdout.fnmatch_lines(["* 3 passed*"]) @pytest.mark.skipif("sys.version_info[0] >= 3") @pytest.mark.xfail("hasattr(sys, 'pypy_translation_info')") @@ -794,56 +909,65 @@ class TestAssertionRewriteHookDetails(object): @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie(self, testdir): - testdir.makepyfile(test_cookie=""" + testdir.makepyfile( + test_cookie=""" # -*- coding: utf-8 -*- u"St\xc3\xa4d" def test_rewritten(): - assert "@py_builtins" in globals()""") + assert "@py_builtins" in globals()""" + ) assert testdir.runpytest().ret == 0 @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie_second_line(self, testdir): - testdir.makepyfile(test_cookie=""" + testdir.makepyfile( + test_cookie=""" # -*- coding: utf-8 -*- u"St\xc3\xa4d" def test_rewritten(): - assert "@py_builtins" in globals()""") + assert "@py_builtins" in globals()""" + ) assert testdir.runpytest().ret == 0 @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie_crlf(self, testdir): - testdir.makepyfile(test_cookie=""" + testdir.makepyfile( + test_cookie=""" # -*- coding: utf-8 -*- u"St\xc3\xa4d" def test_rewritten(): - assert "@py_builtins" in globals()""") + assert "@py_builtins" in globals()""" + ) assert testdir.runpytest().ret == 0 def test_sys_meta_path_munged(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_meta_path(): - import sys; sys.meta_path = []""") + import sys; sys.meta_path = []""" + ) assert testdir.runpytest().ret == 0 def test_write_pyc(self, testdir, tmpdir, monkeypatch): from _pytest.assertion.rewrite import _write_pyc from _pytest.assertion import AssertionState - try: - import __builtin__ as b - except ImportError: - import builtins as b + import atomicwrites + from contextlib import contextmanager + config = testdir.parseconfig([]) state = AssertionState(config, "rewrite") source_path = tmpdir.ensure("source.py") pycpath = tmpdir.join("pyc").strpath assert _write_pyc(state, [1], source_path.stat(), pycpath) - def open(*args): + @contextmanager + def atomic_write_failed(fn, mode="r", overwrite=False): e = IOError() e.errno = 10 raise e + yield # noqa - monkeypatch.setattr(b, "open", open) + monkeypatch.setattr(atomicwrites, "atomic_write", atomic_write_failed) assert not _write_pyc(state, [1], source_path.stat(), pycpath) def test_resources_provider_for_loader(self, testdir): @@ -855,9 +979,9 @@ class TestAssertionRewriteHookDetails(object): """ pytest.importorskip("pkg_resources") - testdir.mkpydir('testpkg') + testdir.mkpydir("testpkg") contents = { - 'testpkg/test_pkg': """ + "testpkg/test_pkg": """ import pkg_resources import pytest @@ -868,10 +992,10 @@ class TestAssertionRewriteHookDetails(object): res = pkg_resources.resource_string(__name__, 'resource.txt') res = res.decode('ascii') assert res == 'Load me please.' - """, + """ } testdir.makepyfile(**contents) - testdir.maketxtfile(**{'testpkg/resource': "Load me please."}) + testdir.maketxtfile(**{"testpkg/resource": "Load me please."}) result = testdir.runpytest_subprocess() result.assert_outcomes(passed=1) @@ -885,28 +1009,33 @@ class TestAssertionRewriteHookDetails(object): import py_compile from _pytest.assertion.rewrite import _read_pyc - source = tmpdir.join('source.py') - pyc = source + 'c' + source = tmpdir.join("source.py") + pyc = source + "c" - source.write('def test(): pass') + source.write("def test(): pass") py_compile.compile(str(source), str(pyc)) - contents = pyc.read(mode='rb') + contents = pyc.read(mode="rb") strip_bytes = 20 # header is around 8 bytes, strip a little more assert len(contents) > strip_bytes - pyc.write(contents[:strip_bytes], mode='wb') + pyc.write(contents[:strip_bytes], mode="wb") assert _read_pyc(source, str(pyc)) is None # no error def test_reload_is_same(self, testdir): # A file that will be picked up during collecting. testdir.tmpdir.join("file.py").ensure() - testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent(""" + testdir.tmpdir.join("pytest.ini").write( + textwrap.dedent( + """ [pytest] python_files = *.py - """)) + """ + ) + ) - testdir.makepyfile(test_fun=""" + testdir.makepyfile( + test_fun=""" import sys try: from imp import reload @@ -916,30 +1045,34 @@ class TestAssertionRewriteHookDetails(object): def test_loader(): import file assert sys.modules["file"] is reload(file) - """) - result = testdir.runpytest('-s') - result.stdout.fnmatch_lines([ - "* 1 passed*", - ]) + """ + ) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines(["* 1 passed*"]) def test_get_data_support(self, testdir): """Implement optional PEP302 api (#808). """ path = testdir.mkpydir("foo") - path.join("test_foo.py").write(_pytest._code.Source(""" + path.join("test_foo.py").write( + _pytest._code.Source( + """ class Test(object): def test_foo(self): import pkgutil data = pkgutil.get_data('foo.test_foo', 'data.txt') assert data == b'Hey' - """)) - path.join('data.txt').write('Hey') + """ + ) + ) + path.join("data.txt").write("Hey") result = testdir.runpytest() - result.stdout.fnmatch_lines('*1 passed*') + result.stdout.fnmatch_lines("*1 passed*") def test_issue731(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class LongReprWithBraces(object): def __repr__(self): return 'LongReprWithBraces({' + ('a' * 80) + '}' + ('a' * 120) + ')' @@ -950,47 +1083,62 @@ def test_issue731(testdir): def test_long_repr(): obj = LongReprWithBraces() assert obj.some_method() - """) + """ + ) result = testdir.runpytest() - assert 'unbalanced braces' not in result.stdout.str() + assert "unbalanced braces" not in result.stdout.str() class TestIssue925(object): + def test_simple_case(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_ternary_display(): assert (False == False) == False - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('*E*assert (False == False) == False') + result.stdout.fnmatch_lines("*E*assert (False == False) == False") def test_long_case(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_ternary_display(): assert False == (False == True) == True - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('*E*assert (False == True) == True') + result.stdout.fnmatch_lines("*E*assert (False == True) == True") def test_many_brackets(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_ternary_display(): assert True == ((False == True) == True) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('*E*assert True == ((False == True) == True)') + result.stdout.fnmatch_lines("*E*assert True == ((False == True) == True)") class TestIssue2121(): + def test_simple(self, testdir): - testdir.tmpdir.join("tests/file.py").ensure().write(""" + testdir.tmpdir.join("tests/file.py").ensure().write( + """ def test_simple_failure(): assert 1 + 1 == 3 -""") - testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent(""" +""" + ) + testdir.tmpdir.join("pytest.ini").write( + textwrap.dedent( + """ [pytest] python_files = tests/**.py - """)) + """ + ) + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('*E*assert (1 + 1) == 3') + result.stdout.fnmatch_lines("*E*assert (1 + 1) == 3") diff --git a/third_party/python/pytest/testing/test_cache.py b/third_party/python/pytest/testing/test_cache.py deleted file mode 100755 index a37170cdd2b1..000000000000 --- a/third_party/python/pytest/testing/test_cache.py +++ /dev/null @@ -1,605 +0,0 @@ -from __future__ import absolute_import, division, print_function -import sys -import py -import _pytest -import pytest -import os -import shutil - -pytest_plugins = "pytester", - - -class TestNewAPI(object): - def test_config_cache_makedir(self, testdir): - testdir.makeini("[pytest]") - config = testdir.parseconfigure() - with pytest.raises(ValueError): - config.cache.makedir("key/name") - - p = config.cache.makedir("name") - assert p.check() - - def test_config_cache_dataerror(self, testdir): - testdir.makeini("[pytest]") - config = testdir.parseconfigure() - cache = config.cache - pytest.raises(TypeError, lambda: cache.set("key/name", cache)) - config.cache.set("key/name", 0) - config.cache._getvaluepath("key/name").write("123invalid") - val = config.cache.get("key/name", -2) - assert val == -2 - - def test_cache_writefail_cachfile_silent(self, testdir): - testdir.makeini("[pytest]") - testdir.tmpdir.join('.cache').write('gone wrong') - config = testdir.parseconfigure() - cache = config.cache - cache.set('test/broken', []) - - @pytest.mark.skipif(sys.platform.startswith('win'), reason='no chmod on windows') - def test_cache_writefail_permissions(self, testdir): - testdir.makeini("[pytest]") - testdir.tmpdir.ensure_dir('.cache').chmod(0) - config = testdir.parseconfigure() - cache = config.cache - cache.set('test/broken', []) - - @pytest.mark.skipif(sys.platform.startswith('win'), reason='no chmod on windows') - def test_cache_failure_warns(self, testdir): - testdir.tmpdir.ensure_dir('.cache').chmod(0) - testdir.makepyfile(""" - def test_error(): - raise Exception - - """) - result = testdir.runpytest('-rw') - assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*could not create cache path*", - "*1 warnings*", - ]) - - def test_config_cache(self, testdir): - testdir.makeconftest(""" - def pytest_configure(config): - # see that we get cache information early on - assert hasattr(config, "cache") - """) - testdir.makepyfile(""" - def test_session(pytestconfig): - assert hasattr(pytestconfig, "cache") - """) - result = testdir.runpytest() - assert result.ret == 0 - result.stdout.fnmatch_lines(["*1 passed*"]) - - def test_cachefuncarg(self, testdir): - testdir.makepyfile(""" - import pytest - def test_cachefuncarg(cache): - val = cache.get("some/thing", None) - assert val is None - cache.set("some/thing", [1]) - pytest.raises(TypeError, lambda: cache.get("some/thing")) - val = cache.get("some/thing", []) - assert val == [1] - """) - result = testdir.runpytest() - assert result.ret == 0 - result.stdout.fnmatch_lines(["*1 passed*"]) - - def test_custom_rel_cache_dir(self, testdir): - rel_cache_dir = os.path.join('custom_cache_dir', 'subdir') - testdir.makeini(""" - [pytest] - cache_dir = {cache_dir} - """.format(cache_dir=rel_cache_dir)) - testdir.makepyfile(test_errored='def test_error():\n assert False') - testdir.runpytest() - assert testdir.tmpdir.join(rel_cache_dir).isdir() - - def test_custom_abs_cache_dir(self, testdir, tmpdir_factory): - tmp = str(tmpdir_factory.mktemp('tmp')) - abs_cache_dir = os.path.join(tmp, 'custom_cache_dir') - testdir.makeini(""" - [pytest] - cache_dir = {cache_dir} - """.format(cache_dir=abs_cache_dir)) - testdir.makepyfile(test_errored='def test_error():\n assert False') - testdir.runpytest() - assert py.path.local(abs_cache_dir).isdir() - - def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch): - monkeypatch.setenv('env_var', 'custom_cache_dir') - testdir.makeini(""" - [pytest] - cache_dir = {cache_dir} - """.format(cache_dir='$env_var')) - testdir.makepyfile(test_errored='def test_error():\n assert False') - testdir.runpytest() - assert testdir.tmpdir.join('custom_cache_dir').isdir() - - -def test_cache_reportheader(testdir): - testdir.makepyfile(""" - def test_hello(): - pass - """) - result = testdir.runpytest("-v") - result.stdout.fnmatch_lines([ - "cachedir: .cache" - ]) - - -def test_cache_show(testdir): - result = testdir.runpytest("--cache-show") - assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*cache is empty*" - ]) - testdir.makeconftest(""" - def pytest_configure(config): - config.cache.set("my/name", [1,2,3]) - config.cache.set("other/some", {1:2}) - dp = config.cache.makedir("mydb") - dp.ensure("hello") - dp.ensure("world") - """) - result = testdir.runpytest() - assert result.ret == 5 # no tests executed - result = testdir.runpytest("--cache-show") - result.stdout.fnmatch_lines_random([ - "*cachedir:*", - "-*cache values*-", - "*my/name contains:", - " [1, 2, 3]", - "*other/some contains*", - " {*1*: 2}", - "-*cache directories*-", - "*mydb/hello*length 0*", - "*mydb/world*length 0*", - ]) - - -class TestLastFailed(object): - - def test_lastfailed_usecase(self, testdir, monkeypatch): - monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) - p = testdir.makepyfile(""" - def test_1(): - assert 0 - def test_2(): - assert 0 - def test_3(): - assert 1 - """) - result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*2 failed*", - ]) - p.write(_pytest._code.Source(""" - def test_1(): - assert 1 - - def test_2(): - assert 1 - - def test_3(): - assert 0 - """)) - result = testdir.runpytest("--lf") - result.stdout.fnmatch_lines([ - "*2 passed*1 desel*", - ]) - result = testdir.runpytest("--lf") - result.stdout.fnmatch_lines([ - "*1 failed*2 passed*", - ]) - result = testdir.runpytest("--lf", "--cache-clear") - result.stdout.fnmatch_lines([ - "*1 failed*2 passed*", - ]) - - # Run this again to make sure clear-cache is robust - if os.path.isdir('.cache'): - shutil.rmtree('.cache') - result = testdir.runpytest("--lf", "--cache-clear") - result.stdout.fnmatch_lines([ - "*1 failed*2 passed*", - ]) - - def test_failedfirst_order(self, testdir): - testdir.tmpdir.join('test_a.py').write(_pytest._code.Source(""" - def test_always_passes(): - assert 1 - """)) - testdir.tmpdir.join('test_b.py').write(_pytest._code.Source(""" - def test_always_fails(): - assert 0 - """)) - result = testdir.runpytest() - # Test order will be collection order; alphabetical - result.stdout.fnmatch_lines([ - "test_a.py*", - "test_b.py*", - ]) - result = testdir.runpytest("--ff") - # Test order will be failing tests firs - result.stdout.fnmatch_lines([ - "test_b.py*", - "test_a.py*", - ]) - - def test_lastfailed_failedfirst_order(self, testdir): - testdir.makepyfile(**{ - 'test_a.py': """ - def test_always_passes(): - assert 1 - """, - 'test_b.py': """ - def test_always_fails(): - assert 0 - """, - }) - result = testdir.runpytest() - # Test order will be collection order; alphabetical - result.stdout.fnmatch_lines([ - "test_a.py*", - "test_b.py*", - ]) - result = testdir.runpytest("--lf", "--ff") - # Test order will be failing tests firs - result.stdout.fnmatch_lines([ - "test_b.py*", - ]) - assert 'test_a.py' not in result.stdout.str() - - def test_lastfailed_difference_invocations(self, testdir, monkeypatch): - monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) - testdir.makepyfile(test_a=""" - def test_a1(): - assert 0 - def test_a2(): - assert 1 - """, test_b=""" - def test_b1(): - assert 0 - """) - p = testdir.tmpdir.join("test_a.py") - p2 = testdir.tmpdir.join("test_b.py") - - result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*2 failed*", - ]) - result = testdir.runpytest("--lf", p2) - result.stdout.fnmatch_lines([ - "*1 failed*", - ]) - p2.write(_pytest._code.Source(""" - def test_b1(): - assert 1 - """)) - result = testdir.runpytest("--lf", p2) - result.stdout.fnmatch_lines([ - "*1 passed*", - ]) - result = testdir.runpytest("--lf", p) - result.stdout.fnmatch_lines([ - "*1 failed*1 desel*", - ]) - - def test_lastfailed_usecase_splice(self, testdir, monkeypatch): - monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) - testdir.makepyfile(""" - def test_1(): - assert 0 - """) - p2 = testdir.tmpdir.join("test_something.py") - p2.write(_pytest._code.Source(""" - def test_2(): - assert 0 - """)) - result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*2 failed*", - ]) - result = testdir.runpytest("--lf", p2) - result.stdout.fnmatch_lines([ - "*1 failed*", - ]) - result = testdir.runpytest("--lf") - result.stdout.fnmatch_lines([ - "*2 failed*", - ]) - - def test_lastfailed_xpass(self, testdir): - testdir.inline_runsource(""" - import pytest - @pytest.mark.xfail - def test_hello(): - assert 1 - """) - config = testdir.parseconfigure() - lastfailed = config.cache.get("cache/lastfailed", -1) - assert lastfailed == -1 - - def test_non_serializable_parametrize(self, testdir): - """Test that failed parametrized tests with unmarshable parameters - don't break pytest-cache. - """ - testdir.makepyfile(r""" - import pytest - - @pytest.mark.parametrize('val', [ - b'\xac\x10\x02G', - ]) - def test_fail(val): - assert False - """) - result = testdir.runpytest() - result.stdout.fnmatch_lines('*1 failed in*') - - def test_terminal_report_lastfailed(self, testdir): - test_a = testdir.makepyfile(test_a=""" - def test_a1(): - pass - def test_a2(): - pass - """) - test_b = testdir.makepyfile(test_b=""" - def test_b1(): - assert 0 - def test_b2(): - assert 0 - """) - result = testdir.runpytest() - result.stdout.fnmatch_lines([ - 'collected 4 items', - '*2 failed, 2 passed in*', - ]) - - result = testdir.runpytest('--lf') - result.stdout.fnmatch_lines([ - 'collected 4 items', - 'run-last-failure: rerun previous 2 failures', - '*2 failed, 2 deselected in*', - ]) - - result = testdir.runpytest(test_a, '--lf') - result.stdout.fnmatch_lines([ - 'collected 2 items', - 'run-last-failure: run all (no recorded failures)', - '*2 passed in*', - ]) - - result = testdir.runpytest(test_b, '--lf') - result.stdout.fnmatch_lines([ - 'collected 2 items', - 'run-last-failure: rerun previous 2 failures', - '*2 failed in*', - ]) - - result = testdir.runpytest('test_b.py::test_b1', '--lf') - result.stdout.fnmatch_lines([ - 'collected 1 item', - 'run-last-failure: rerun previous 1 failure', - '*1 failed in*', - ]) - - def test_terminal_report_failedfirst(self, testdir): - testdir.makepyfile(test_a=""" - def test_a1(): - assert 0 - def test_a2(): - pass - """) - result = testdir.runpytest() - result.stdout.fnmatch_lines([ - 'collected 2 items', - '*1 failed, 1 passed in*', - ]) - - result = testdir.runpytest('--ff') - result.stdout.fnmatch_lines([ - 'collected 2 items', - 'run-last-failure: rerun previous 1 failure first', - '*1 failed, 1 passed in*', - ]) - - def test_lastfailed_collectfailure(self, testdir, monkeypatch): - - testdir.makepyfile(test_maybe=""" - import py - env = py.std.os.environ - if '1' == env['FAILIMPORT']: - raise ImportError('fail') - def test_hello(): - assert '0' == env['FAILTEST'] - """) - - def rlf(fail_import, fail_run): - monkeypatch.setenv('FAILIMPORT', fail_import) - monkeypatch.setenv('FAILTEST', fail_run) - - testdir.runpytest('-q') - config = testdir.parseconfigure() - lastfailed = config.cache.get("cache/lastfailed", -1) - return lastfailed - - lastfailed = rlf(fail_import=0, fail_run=0) - assert lastfailed == -1 - - lastfailed = rlf(fail_import=1, fail_run=0) - assert list(lastfailed) == ['test_maybe.py'] - - lastfailed = rlf(fail_import=0, fail_run=1) - assert list(lastfailed) == ['test_maybe.py::test_hello'] - - def test_lastfailed_failure_subset(self, testdir, monkeypatch): - - testdir.makepyfile(test_maybe=""" - import py - env = py.std.os.environ - if '1' == env['FAILIMPORT']: - raise ImportError('fail') - def test_hello(): - assert '0' == env['FAILTEST'] - """) - - testdir.makepyfile(test_maybe2=""" - import py - env = py.std.os.environ - if '1' == env['FAILIMPORT']: - raise ImportError('fail') - def test_hello(): - assert '0' == env['FAILTEST'] - - def test_pass(): - pass - """) - - def rlf(fail_import, fail_run, args=()): - monkeypatch.setenv('FAILIMPORT', fail_import) - monkeypatch.setenv('FAILTEST', fail_run) - - result = testdir.runpytest('-q', '--lf', *args) - config = testdir.parseconfigure() - lastfailed = config.cache.get("cache/lastfailed", -1) - return result, lastfailed - - result, lastfailed = rlf(fail_import=0, fail_run=0) - assert lastfailed == -1 - result.stdout.fnmatch_lines([ - '*3 passed*', - ]) - - result, lastfailed = rlf(fail_import=1, fail_run=0) - assert sorted(list(lastfailed)) == ['test_maybe.py', 'test_maybe2.py'] - - result, lastfailed = rlf(fail_import=0, fail_run=0, - args=('test_maybe2.py',)) - assert list(lastfailed) == ['test_maybe.py'] - - # edge case of test selection - even if we remember failures - # from other tests we still need to run all tests if no test - # matches the failures - result, lastfailed = rlf(fail_import=0, fail_run=0, - args=('test_maybe2.py',)) - assert list(lastfailed) == ['test_maybe.py'] - result.stdout.fnmatch_lines([ - '*2 passed*', - ]) - - def test_lastfailed_creates_cache_when_needed(self, testdir): - # Issue #1342 - testdir.makepyfile(test_empty='') - testdir.runpytest('-q', '--lf') - assert not os.path.exists('.cache') - - testdir.makepyfile(test_successful='def test_success():\n assert True') - testdir.runpytest('-q', '--lf') - assert not os.path.exists('.cache') - - testdir.makepyfile(test_errored='def test_error():\n assert False') - testdir.runpytest('-q', '--lf') - assert os.path.exists('.cache') - - def test_xfail_not_considered_failure(self, testdir): - testdir.makepyfile(''' - import pytest - @pytest.mark.xfail - def test(): - assert 0 - ''') - result = testdir.runpytest() - result.stdout.fnmatch_lines('*1 xfailed*') - assert self.get_cached_last_failed(testdir) == [] - - def test_xfail_strict_considered_failure(self, testdir): - testdir.makepyfile(''' - import pytest - @pytest.mark.xfail(strict=True) - def test(): - pass - ''') - result = testdir.runpytest() - result.stdout.fnmatch_lines('*1 failed*') - assert self.get_cached_last_failed(testdir) == ['test_xfail_strict_considered_failure.py::test'] - - @pytest.mark.parametrize('mark', ['mark.xfail', 'mark.skip']) - def test_failed_changed_to_xfail_or_skip(self, testdir, mark): - testdir.makepyfile(''' - import pytest - def test(): - assert 0 - ''') - result = testdir.runpytest() - assert self.get_cached_last_failed(testdir) == ['test_failed_changed_to_xfail_or_skip.py::test'] - assert result.ret == 1 - - testdir.makepyfile(''' - import pytest - @pytest.{mark} - def test(): - assert 0 - '''.format(mark=mark)) - result = testdir.runpytest() - assert result.ret == 0 - assert self.get_cached_last_failed(testdir) == [] - assert result.ret == 0 - - def get_cached_last_failed(self, testdir): - config = testdir.parseconfigure() - return sorted(config.cache.get("cache/lastfailed", {})) - - def test_cache_cumulative(self, testdir): - """ - Test workflow where user fixes errors gradually file by file using --lf. - """ - # 1. initial run - test_bar = testdir.makepyfile(test_bar=""" - def test_bar_1(): - pass - def test_bar_2(): - assert 0 - """) - test_foo = testdir.makepyfile(test_foo=""" - def test_foo_3(): - pass - def test_foo_4(): - assert 0 - """) - testdir.runpytest() - assert self.get_cached_last_failed(testdir) == ['test_bar.py::test_bar_2', 'test_foo.py::test_foo_4'] - - # 2. fix test_bar_2, run only test_bar.py - testdir.makepyfile(test_bar=""" - def test_bar_1(): - pass - def test_bar_2(): - pass - """) - result = testdir.runpytest(test_bar) - result.stdout.fnmatch_lines('*2 passed*') - # ensure cache does not forget that test_foo_4 failed once before - assert self.get_cached_last_failed(testdir) == ['test_foo.py::test_foo_4'] - - result = testdir.runpytest('--last-failed') - result.stdout.fnmatch_lines('*1 failed, 3 deselected*') - assert self.get_cached_last_failed(testdir) == ['test_foo.py::test_foo_4'] - - # 3. fix test_foo_4, run only test_foo.py - test_foo = testdir.makepyfile(test_foo=""" - def test_foo_3(): - pass - def test_foo_4(): - pass - """) - result = testdir.runpytest(test_foo, '--last-failed') - result.stdout.fnmatch_lines('*1 passed, 1 deselected*') - assert self.get_cached_last_failed(testdir) == [] - - result = testdir.runpytest('--last-failed') - result.stdout.fnmatch_lines('*4 passed*') - assert self.get_cached_last_failed(testdir) == [] diff --git a/third_party/python/pytest/testing/test_cacheprovider.py b/third_party/python/pytest/testing/test_cacheprovider.py new file mode 100644 index 000000000000..33d1dd8448c1 --- /dev/null +++ b/third_party/python/pytest/testing/test_cacheprovider.py @@ -0,0 +1,820 @@ +from __future__ import absolute_import, division, print_function +import sys +import py +import _pytest +import pytest +import os +import shutil + +pytest_plugins = "pytester", + + +class TestNewAPI(object): + + def test_config_cache_makedir(self, testdir): + testdir.makeini("[pytest]") + config = testdir.parseconfigure() + with pytest.raises(ValueError): + config.cache.makedir("key/name") + + p = config.cache.makedir("name") + assert p.check() + + def test_config_cache_dataerror(self, testdir): + testdir.makeini("[pytest]") + config = testdir.parseconfigure() + cache = config.cache + pytest.raises(TypeError, lambda: cache.set("key/name", cache)) + config.cache.set("key/name", 0) + config.cache._getvaluepath("key/name").write("123invalid") + val = config.cache.get("key/name", -2) + assert val == -2 + + def test_cache_writefail_cachfile_silent(self, testdir): + testdir.makeini("[pytest]") + testdir.tmpdir.join(".pytest_cache").write("gone wrong") + config = testdir.parseconfigure() + cache = config.cache + cache.set("test/broken", []) + + @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") + def test_cache_writefail_permissions(self, testdir): + testdir.makeini("[pytest]") + testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0) + config = testdir.parseconfigure() + cache = config.cache + cache.set("test/broken", []) + + @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") + def test_cache_failure_warns(self, testdir): + testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0) + testdir.makepyfile( + """ + def test_error(): + raise Exception + + """ + ) + result = testdir.runpytest("-rw") + assert result.ret == 1 + result.stdout.fnmatch_lines(["*could not create cache path*", "*2 warnings*"]) + + def test_config_cache(self, testdir): + testdir.makeconftest( + """ + def pytest_configure(config): + # see that we get cache information early on + assert hasattr(config, "cache") + """ + ) + testdir.makepyfile( + """ + def test_session(pytestconfig): + assert hasattr(pytestconfig, "cache") + """ + ) + result = testdir.runpytest() + assert result.ret == 0 + result.stdout.fnmatch_lines(["*1 passed*"]) + + def test_cachefuncarg(self, testdir): + testdir.makepyfile( + """ + import pytest + def test_cachefuncarg(cache): + val = cache.get("some/thing", None) + assert val is None + cache.set("some/thing", [1]) + pytest.raises(TypeError, lambda: cache.get("some/thing")) + val = cache.get("some/thing", []) + assert val == [1] + """ + ) + result = testdir.runpytest() + assert result.ret == 0 + result.stdout.fnmatch_lines(["*1 passed*"]) + + def test_custom_rel_cache_dir(self, testdir): + rel_cache_dir = os.path.join("custom_cache_dir", "subdir") + testdir.makeini( + """ + [pytest] + cache_dir = {cache_dir} + """.format( + cache_dir=rel_cache_dir + ) + ) + testdir.makepyfile(test_errored="def test_error():\n assert False") + testdir.runpytest() + assert testdir.tmpdir.join(rel_cache_dir).isdir() + + def test_custom_abs_cache_dir(self, testdir, tmpdir_factory): + tmp = str(tmpdir_factory.mktemp("tmp")) + abs_cache_dir = os.path.join(tmp, "custom_cache_dir") + testdir.makeini( + """ + [pytest] + cache_dir = {cache_dir} + """.format( + cache_dir=abs_cache_dir + ) + ) + testdir.makepyfile(test_errored="def test_error():\n assert False") + testdir.runpytest() + assert py.path.local(abs_cache_dir).isdir() + + def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch): + monkeypatch.setenv("env_var", "custom_cache_dir") + testdir.makeini( + """ + [pytest] + cache_dir = {cache_dir} + """.format( + cache_dir="$env_var" + ) + ) + testdir.makepyfile(test_errored="def test_error():\n assert False") + testdir.runpytest() + assert testdir.tmpdir.join("custom_cache_dir").isdir() + + +def test_cache_reportheader(testdir): + testdir.makepyfile( + """ + def test_hello(): + pass + """ + ) + result = testdir.runpytest("-v") + result.stdout.fnmatch_lines(["cachedir: .pytest_cache"]) + + +def test_cache_show(testdir): + result = testdir.runpytest("--cache-show") + assert result.ret == 0 + result.stdout.fnmatch_lines(["*cache is empty*"]) + testdir.makeconftest( + """ + def pytest_configure(config): + config.cache.set("my/name", [1,2,3]) + config.cache.set("other/some", {1:2}) + dp = config.cache.makedir("mydb") + dp.ensure("hello") + dp.ensure("world") + """ + ) + result = testdir.runpytest() + assert result.ret == 5 # no tests executed + result = testdir.runpytest("--cache-show") + result.stdout.fnmatch_lines_random( + [ + "*cachedir:*", + "-*cache values*-", + "*my/name contains:", + " [1, 2, 3]", + "*other/some contains*", + " {*1*: 2}", + "-*cache directories*-", + "*mydb/hello*length 0*", + "*mydb/world*length 0*", + ] + ) + + +class TestLastFailed(object): + + def test_lastfailed_usecase(self, testdir, monkeypatch): + monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) + p = testdir.makepyfile( + """ + def test_1(): + assert 0 + def test_2(): + assert 0 + def test_3(): + assert 1 + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*2 failed*"]) + p.write( + _pytest._code.Source( + """ + def test_1(): + assert 1 + + def test_2(): + assert 1 + + def test_3(): + assert 0 + """ + ) + ) + result = testdir.runpytest("--lf") + result.stdout.fnmatch_lines(["*2 passed*1 desel*"]) + result = testdir.runpytest("--lf") + result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) + result = testdir.runpytest("--lf", "--cache-clear") + result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) + + # Run this again to make sure clear-cache is robust + if os.path.isdir(".pytest_cache"): + shutil.rmtree(".pytest_cache") + result = testdir.runpytest("--lf", "--cache-clear") + result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) + + def test_failedfirst_order(self, testdir): + testdir.tmpdir.join("test_a.py").write( + _pytest._code.Source( + """ + def test_always_passes(): + assert 1 + """ + ) + ) + testdir.tmpdir.join("test_b.py").write( + _pytest._code.Source( + """ + def test_always_fails(): + assert 0 + """ + ) + ) + result = testdir.runpytest() + # Test order will be collection order; alphabetical + result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"]) + result = testdir.runpytest("--ff") + # Test order will be failing tests firs + result.stdout.fnmatch_lines(["test_b.py*", "test_a.py*"]) + + def test_lastfailed_failedfirst_order(self, testdir): + testdir.makepyfile( + **{ + "test_a.py": """ + def test_always_passes(): + assert 1 + """, + "test_b.py": """ + def test_always_fails(): + assert 0 + """, + } + ) + result = testdir.runpytest() + # Test order will be collection order; alphabetical + result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"]) + result = testdir.runpytest("--lf", "--ff") + # Test order will be failing tests firs + result.stdout.fnmatch_lines(["test_b.py*"]) + assert "test_a.py" not in result.stdout.str() + + def test_lastfailed_difference_invocations(self, testdir, monkeypatch): + monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) + testdir.makepyfile( + test_a=""" + def test_a1(): + assert 0 + def test_a2(): + assert 1 + """, + test_b=""" + def test_b1(): + assert 0 + """, + ) + p = testdir.tmpdir.join("test_a.py") + p2 = testdir.tmpdir.join("test_b.py") + + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*2 failed*"]) + result = testdir.runpytest("--lf", p2) + result.stdout.fnmatch_lines(["*1 failed*"]) + p2.write( + _pytest._code.Source( + """ + def test_b1(): + assert 1 + """ + ) + ) + result = testdir.runpytest("--lf", p2) + result.stdout.fnmatch_lines(["*1 passed*"]) + result = testdir.runpytest("--lf", p) + result.stdout.fnmatch_lines(["*1 failed*1 desel*"]) + + def test_lastfailed_usecase_splice(self, testdir, monkeypatch): + monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) + testdir.makepyfile( + """ + def test_1(): + assert 0 + """ + ) + p2 = testdir.tmpdir.join("test_something.py") + p2.write( + _pytest._code.Source( + """ + def test_2(): + assert 0 + """ + ) + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*2 failed*"]) + result = testdir.runpytest("--lf", p2) + result.stdout.fnmatch_lines(["*1 failed*"]) + result = testdir.runpytest("--lf") + result.stdout.fnmatch_lines(["*2 failed*"]) + + def test_lastfailed_xpass(self, testdir): + testdir.inline_runsource( + """ + import pytest + @pytest.mark.xfail + def test_hello(): + assert 1 + """ + ) + config = testdir.parseconfigure() + lastfailed = config.cache.get("cache/lastfailed", -1) + assert lastfailed == -1 + + def test_non_serializable_parametrize(self, testdir): + """Test that failed parametrized tests with unmarshable parameters + don't break pytest-cache. + """ + testdir.makepyfile( + r""" + import pytest + + @pytest.mark.parametrize('val', [ + b'\xac\x10\x02G', + ]) + def test_fail(val): + assert False + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines("*1 failed in*") + + def test_terminal_report_lastfailed(self, testdir): + test_a = testdir.makepyfile( + test_a=""" + def test_a1(): + pass + def test_a2(): + pass + """ + ) + test_b = testdir.makepyfile( + test_b=""" + def test_b1(): + assert 0 + def test_b2(): + assert 0 + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["collected 4 items", "*2 failed, 2 passed in*"]) + + result = testdir.runpytest("--lf") + result.stdout.fnmatch_lines( + [ + "collected 4 items / 2 deselected", + "run-last-failure: rerun previous 2 failures", + "*2 failed, 2 deselected in*", + ] + ) + + result = testdir.runpytest(test_a, "--lf") + result.stdout.fnmatch_lines( + [ + "collected 2 items", + "run-last-failure: run all (no recorded failures)", + "*2 passed in*", + ] + ) + + result = testdir.runpytest(test_b, "--lf") + result.stdout.fnmatch_lines( + [ + "collected 2 items", + "run-last-failure: rerun previous 2 failures", + "*2 failed in*", + ] + ) + + result = testdir.runpytest("test_b.py::test_b1", "--lf") + result.stdout.fnmatch_lines( + [ + "collected 1 item", + "run-last-failure: rerun previous 1 failure", + "*1 failed in*", + ] + ) + + def test_terminal_report_failedfirst(self, testdir): + testdir.makepyfile( + test_a=""" + def test_a1(): + assert 0 + def test_a2(): + pass + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["collected 2 items", "*1 failed, 1 passed in*"]) + + result = testdir.runpytest("--ff") + result.stdout.fnmatch_lines( + [ + "collected 2 items", + "run-last-failure: rerun previous 1 failure first", + "*1 failed, 1 passed in*", + ] + ) + + def test_lastfailed_collectfailure(self, testdir, monkeypatch): + + testdir.makepyfile( + test_maybe=""" + import os + env = os.environ + if '1' == env['FAILIMPORT']: + raise ImportError('fail') + def test_hello(): + assert '0' == env['FAILTEST'] + """ + ) + + def rlf(fail_import, fail_run): + monkeypatch.setenv("FAILIMPORT", fail_import) + monkeypatch.setenv("FAILTEST", fail_run) + + testdir.runpytest("-q") + config = testdir.parseconfigure() + lastfailed = config.cache.get("cache/lastfailed", -1) + return lastfailed + + lastfailed = rlf(fail_import=0, fail_run=0) + assert lastfailed == -1 + + lastfailed = rlf(fail_import=1, fail_run=0) + assert list(lastfailed) == ["test_maybe.py"] + + lastfailed = rlf(fail_import=0, fail_run=1) + assert list(lastfailed) == ["test_maybe.py::test_hello"] + + def test_lastfailed_failure_subset(self, testdir, monkeypatch): + + testdir.makepyfile( + test_maybe=""" + import os + env = os.environ + if '1' == env['FAILIMPORT']: + raise ImportError('fail') + def test_hello(): + assert '0' == env['FAILTEST'] + """ + ) + + testdir.makepyfile( + test_maybe2=""" + import os + env = os.environ + if '1' == env['FAILIMPORT']: + raise ImportError('fail') + def test_hello(): + assert '0' == env['FAILTEST'] + + def test_pass(): + pass + """ + ) + + def rlf(fail_import, fail_run, args=()): + monkeypatch.setenv("FAILIMPORT", fail_import) + monkeypatch.setenv("FAILTEST", fail_run) + + result = testdir.runpytest("-q", "--lf", *args) + config = testdir.parseconfigure() + lastfailed = config.cache.get("cache/lastfailed", -1) + return result, lastfailed + + result, lastfailed = rlf(fail_import=0, fail_run=0) + assert lastfailed == -1 + result.stdout.fnmatch_lines(["*3 passed*"]) + + result, lastfailed = rlf(fail_import=1, fail_run=0) + assert sorted(list(lastfailed)) == ["test_maybe.py", "test_maybe2.py"] + + result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",)) + assert list(lastfailed) == ["test_maybe.py"] + + # edge case of test selection - even if we remember failures + # from other tests we still need to run all tests if no test + # matches the failures + result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",)) + assert list(lastfailed) == ["test_maybe.py"] + result.stdout.fnmatch_lines(["*2 passed*"]) + + def test_lastfailed_creates_cache_when_needed(self, testdir): + # Issue #1342 + testdir.makepyfile(test_empty="") + testdir.runpytest("-q", "--lf") + assert not os.path.exists(".pytest_cache/v/cache/lastfailed") + + testdir.makepyfile(test_successful="def test_success():\n assert True") + testdir.runpytest("-q", "--lf") + assert not os.path.exists(".pytest_cache/v/cache/lastfailed") + + testdir.makepyfile(test_errored="def test_error():\n assert False") + testdir.runpytest("-q", "--lf") + assert os.path.exists(".pytest_cache/v/cache/lastfailed") + + def test_xfail_not_considered_failure(self, testdir): + testdir.makepyfile( + """ + import pytest + @pytest.mark.xfail + def test(): + assert 0 + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines("*1 xfailed*") + assert self.get_cached_last_failed(testdir) == [] + + def test_xfail_strict_considered_failure(self, testdir): + testdir.makepyfile( + """ + import pytest + @pytest.mark.xfail(strict=True) + def test(): + pass + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines("*1 failed*") + assert ( + self.get_cached_last_failed(testdir) + == ["test_xfail_strict_considered_failure.py::test"] + ) + + @pytest.mark.parametrize("mark", ["mark.xfail", "mark.skip"]) + def test_failed_changed_to_xfail_or_skip(self, testdir, mark): + testdir.makepyfile( + """ + import pytest + def test(): + assert 0 + """ + ) + result = testdir.runpytest() + assert ( + self.get_cached_last_failed(testdir) + == ["test_failed_changed_to_xfail_or_skip.py::test"] + ) + assert result.ret == 1 + + testdir.makepyfile( + """ + import pytest + @pytest.{mark} + def test(): + assert 0 + """.format( + mark=mark + ) + ) + result = testdir.runpytest() + assert result.ret == 0 + assert self.get_cached_last_failed(testdir) == [] + assert result.ret == 0 + + def get_cached_last_failed(self, testdir): + config = testdir.parseconfigure() + return sorted(config.cache.get("cache/lastfailed", {})) + + def test_cache_cumulative(self, testdir): + """ + Test workflow where user fixes errors gradually file by file using --lf. + """ + # 1. initial run + test_bar = testdir.makepyfile( + test_bar=""" + def test_bar_1(): + pass + def test_bar_2(): + assert 0 + """ + ) + test_foo = testdir.makepyfile( + test_foo=""" + def test_foo_3(): + pass + def test_foo_4(): + assert 0 + """ + ) + testdir.runpytest() + assert ( + self.get_cached_last_failed(testdir) + == ["test_bar.py::test_bar_2", "test_foo.py::test_foo_4"] + ) + + # 2. fix test_bar_2, run only test_bar.py + testdir.makepyfile( + test_bar=""" + def test_bar_1(): + pass + def test_bar_2(): + pass + """ + ) + result = testdir.runpytest(test_bar) + result.stdout.fnmatch_lines("*2 passed*") + # ensure cache does not forget that test_foo_4 failed once before + assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"] + + result = testdir.runpytest("--last-failed") + result.stdout.fnmatch_lines("*1 failed, 3 deselected*") + assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"] + + # 3. fix test_foo_4, run only test_foo.py + test_foo = testdir.makepyfile( + test_foo=""" + def test_foo_3(): + pass + def test_foo_4(): + pass + """ + ) + result = testdir.runpytest(test_foo, "--last-failed") + result.stdout.fnmatch_lines("*1 passed, 1 deselected*") + assert self.get_cached_last_failed(testdir) == [] + + result = testdir.runpytest("--last-failed") + result.stdout.fnmatch_lines("*4 passed*") + assert self.get_cached_last_failed(testdir) == [] + + def test_lastfailed_no_failures_behavior_all_passed(self, testdir): + testdir.makepyfile( + """ + def test_1(): + assert True + def test_2(): + assert True + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*2 passed*"]) + result = testdir.runpytest("--lf") + result.stdout.fnmatch_lines(["*2 passed*"]) + result = testdir.runpytest("--lf", "--lfnf", "all") + result.stdout.fnmatch_lines(["*2 passed*"]) + result = testdir.runpytest("--lf", "--lfnf", "none") + result.stdout.fnmatch_lines(["*2 desel*"]) + + def test_lastfailed_no_failures_behavior_empty_cache(self, testdir): + testdir.makepyfile( + """ + def test_1(): + assert True + def test_2(): + assert False + """ + ) + result = testdir.runpytest("--lf", "--cache-clear") + result.stdout.fnmatch_lines(["*1 failed*1 passed*"]) + result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "all") + result.stdout.fnmatch_lines(["*1 failed*1 passed*"]) + result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "none") + result.stdout.fnmatch_lines(["*2 desel*"]) + + +class TestNewFirst(object): + + def test_newfirst_usecase(self, testdir): + testdir.makepyfile( + **{ + "test_1/test_1.py": """ + def test_1(): assert 1 + def test_2(): assert 1 + def test_3(): assert 1 + """, + "test_2/test_2.py": """ + def test_1(): assert 1 + def test_2(): assert 1 + def test_3(): assert 1 + """, + } + ) + + testdir.tmpdir.join("test_1/test_1.py").setmtime(1) + + result = testdir.runpytest("-v") + result.stdout.fnmatch_lines( + [ + "*test_1/test_1.py::test_1 PASSED*", + "*test_1/test_1.py::test_2 PASSED*", + "*test_1/test_1.py::test_3 PASSED*", + "*test_2/test_2.py::test_1 PASSED*", + "*test_2/test_2.py::test_2 PASSED*", + "*test_2/test_2.py::test_3 PASSED*", + ] + ) + + result = testdir.runpytest("-v", "--nf") + + result.stdout.fnmatch_lines( + [ + "*test_2/test_2.py::test_1 PASSED*", + "*test_2/test_2.py::test_2 PASSED*", + "*test_2/test_2.py::test_3 PASSED*", + "*test_1/test_1.py::test_1 PASSED*", + "*test_1/test_1.py::test_2 PASSED*", + "*test_1/test_1.py::test_3 PASSED*", + ] + ) + + testdir.tmpdir.join("test_1/test_1.py").write( + "def test_1(): assert 1\n" + "def test_2(): assert 1\n" + "def test_3(): assert 1\n" + "def test_4(): assert 1\n" + ) + testdir.tmpdir.join("test_1/test_1.py").setmtime(1) + + result = testdir.runpytest("-v", "--nf") + + result.stdout.fnmatch_lines( + [ + "*test_1/test_1.py::test_4 PASSED*", + "*test_2/test_2.py::test_1 PASSED*", + "*test_2/test_2.py::test_2 PASSED*", + "*test_2/test_2.py::test_3 PASSED*", + "*test_1/test_1.py::test_1 PASSED*", + "*test_1/test_1.py::test_2 PASSED*", + "*test_1/test_1.py::test_3 PASSED*", + ] + ) + + def test_newfirst_parametrize(self, testdir): + testdir.makepyfile( + **{ + "test_1/test_1.py": """ + import pytest + @pytest.mark.parametrize('num', [1, 2]) + def test_1(num): assert num + """, + "test_2/test_2.py": """ + import pytest + @pytest.mark.parametrize('num', [1, 2]) + def test_1(num): assert num + """, + } + ) + + testdir.tmpdir.join("test_1/test_1.py").setmtime(1) + + result = testdir.runpytest("-v") + result.stdout.fnmatch_lines( + [ + "*test_1/test_1.py::test_1[1*", + "*test_1/test_1.py::test_1[2*", + "*test_2/test_2.py::test_1[1*", + "*test_2/test_2.py::test_1[2*", + ] + ) + + result = testdir.runpytest("-v", "--nf") + + result.stdout.fnmatch_lines( + [ + "*test_2/test_2.py::test_1[1*", + "*test_2/test_2.py::test_1[2*", + "*test_1/test_1.py::test_1[1*", + "*test_1/test_1.py::test_1[2*", + ] + ) + + testdir.tmpdir.join("test_1/test_1.py").write( + "import pytest\n" + "@pytest.mark.parametrize('num', [1, 2, 3])\n" + "def test_1(num): assert num\n" + ) + testdir.tmpdir.join("test_1/test_1.py").setmtime(1) + + result = testdir.runpytest("-v", "--nf") + + result.stdout.fnmatch_lines( + [ + "*test_1/test_1.py::test_1[3*", + "*test_2/test_2.py::test_1[1*", + "*test_2/test_2.py::test_1[2*", + "*test_1/test_1.py::test_1[1*", + "*test_1/test_1.py::test_1[2*", + ] + ) diff --git a/third_party/python/pytest/testing/test_capture.py b/third_party/python/pytest/testing/test_capture.py index eb10f3c07250..e14b3af78517 100644 --- a/third_party/python/pytest/testing/test_capture.py +++ b/third_party/python/pytest/testing/test_capture.py @@ -1,4 +1,5 @@ from __future__ import absolute_import, division, print_function + # note: py.io capture tests where copied from # pylib 1.4.20.dev2 (rev 13d9af95547e) from __future__ import with_statement @@ -11,7 +12,7 @@ import _pytest._code import py import pytest import contextlib - +from six import binary_type, text_type from _pytest import capture from _pytest.capture import CaptureManager from _pytest.main import EXIT_NOTESTSCOLLECTED @@ -19,30 +20,19 @@ from _pytest.main import EXIT_NOTESTSCOLLECTED needsosdup = pytest.mark.xfail("not hasattr(os, 'dup')") -if sys.version_info >= (3, 0): - def tobytes(obj): - if isinstance(obj, str): - obj = obj.encode('UTF-8') - assert isinstance(obj, bytes) - return obj - def totext(obj): - if isinstance(obj, bytes): - obj = str(obj, 'UTF-8') - assert isinstance(obj, str) - return obj -else: - def tobytes(obj): - if isinstance(obj, unicode): - obj = obj.encode('UTF-8') - assert isinstance(obj, str) - return obj +def tobytes(obj): + if isinstance(obj, text_type): + obj = obj.encode("UTF-8") + assert isinstance(obj, binary_type) + return obj - def totext(obj): - if isinstance(obj, str): - obj = unicode(obj, 'UTF-8') - assert isinstance(obj, unicode) - return obj + +def totext(obj): + if isinstance(obj, binary_type): + obj = text_type(obj, "UTF-8") + assert isinstance(obj, text_type) + return obj def oswritebytes(fd, obj): @@ -58,43 +48,46 @@ def StdCapture(out=True, err=True, in_=True): class TestCaptureManager(object): + def test_getmethod_default_no_fd(self, monkeypatch): from _pytest.capture import pytest_addoption - from _pytest.config import Parser + from _pytest.config.argparsing import Parser + parser = Parser() pytest_addoption(parser) default = parser._groups[0].options[0].default assert default == "fd" if hasattr(os, "dup") else "sys" parser = Parser() - monkeypatch.delattr(os, 'dup', raising=False) + monkeypatch.delattr(os, "dup", raising=False) pytest_addoption(parser) assert parser._groups[0].options[0].default == "sys" @needsosdup - @pytest.mark.parametrize("method", - ['no', 'sys', pytest.mark.skipif('not hasattr(os, "dup")', 'fd')]) + @pytest.mark.parametrize( + "method", ["no", "sys", pytest.mark.skipif('not hasattr(os, "dup")', "fd")] + ) def test_capturing_basic_api(self, method): capouter = StdCaptureFD() old = sys.stdout, sys.stderr, sys.stdin try: capman = CaptureManager(method) - capman.init_capturings() - outerr = capman.suspendcapture() + capman.start_global_capturing() + outerr = capman.suspend_global_capture() assert outerr == ("", "") - outerr = capman.suspendcapture() + outerr = capman.suspend_global_capture() assert outerr == ("", "") print("hello") - out, err = capman.suspendcapture() + out, err = capman.suspend_global_capture() if method == "no": assert old == (sys.stdout, sys.stderr, sys.stdin) else: assert not out - capman.resumecapture() + capman.resume_global_capture() print("hello") - out, err = capman.suspendcapture() + out, err = capman.suspend_global_capture() if method != "no": assert out == "hello\n" - capman.reset_capturings() + capman.stop_global_capturing() finally: capouter.stop_capturing() @@ -103,14 +96,14 @@ class TestCaptureManager(object): capouter = StdCaptureFD() try: capman = CaptureManager("fd") - capman.init_capturings() - pytest.raises(AssertionError, "capman.init_capturings()") - capman.reset_capturings() + capman.start_global_capturing() + pytest.raises(AssertionError, "capman.start_global_capturing()") + capman.stop_global_capturing() finally: capouter.stop_capturing() -@pytest.mark.parametrize("method", ['fd', 'sys']) +@pytest.mark.parametrize("method", ["fd", "sys"]) def test_capturing_unicode(testdir, method): if hasattr(sys, "pypy_version_info") and sys.pypy_version_info < (2, 2): pytest.xfail("does not work on pypy < 2.2") @@ -118,47 +111,49 @@ def test_capturing_unicode(testdir, method): obj = "'b\u00f6y'" else: obj = "u'\u00f6y'" - testdir.makepyfile(""" + testdir.makepyfile( + """ # coding=utf8 # taken from issue 227 from nosetests def test_unicode(): import sys print (sys.stdout) print (%s) - """ % obj) + """ + % obj + ) result = testdir.runpytest("--capture=%s" % method) - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) -@pytest.mark.parametrize("method", ['fd', 'sys']) +@pytest.mark.parametrize("method", ["fd", "sys"]) def test_capturing_bytes_in_utf8_encoding(testdir, method): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_unicode(): print ('b\\u00f6y') - """) + """ + ) result = testdir.runpytest("--capture=%s" % method) - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_collect_capturing(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ print ("collect %s failure" % 13) import xyz42123 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*Captured stdout*", - "*collect 13 failure*", - ]) + result.stdout.fnmatch_lines(["*Captured stdout*", "*collect 13 failure*"]) class TestPerTestCapturing(object): + def test_capture_and_fixtures(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def setup_module(mod): print ("setup module") def setup_function(function): @@ -169,19 +164,23 @@ class TestPerTestCapturing(object): def test_func2(): print ("in func2") assert 0 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "setup module*", - "setup test_func1*", - "in func1*", - "setup test_func2*", - "in func2*", - ]) + result.stdout.fnmatch_lines( + [ + "setup module*", + "setup test_func1*", + "in func1*", + "setup test_func2*", + "in func2*", + ] + ) @pytest.mark.xfail(reason="unimplemented feature") def test_capture_scope_cache(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import sys def setup_module(func): print ("module-setup") @@ -192,32 +191,38 @@ class TestPerTestCapturing(object): assert 0 def teardown_function(func): print ("in teardown") - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*test_func():*", - "*Captured stdout during setup*", - "module-setup*", - "function-setup*", - "*Captured stdout*", - "in teardown*", - ]) + result.stdout.fnmatch_lines( + [ + "*test_func():*", + "*Captured stdout during setup*", + "module-setup*", + "function-setup*", + "*Captured stdout*", + "in teardown*", + ] + ) def test_no_carry_over(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_func1(): print ("in func1") def test_func2(): print ("in func2") assert 0 - """) + """ + ) result = testdir.runpytest(p) s = result.stdout.str() assert "in func1" not in s assert "in func2" in s def test_teardown_capturing(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def setup_function(function): print ("setup func1") def teardown_function(function): @@ -226,35 +231,43 @@ class TestPerTestCapturing(object): def test_func1(): print ("in func1") pass - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - '*teardown_function*', - '*Captured stdout*', - "setup func1*", - "in func1*", - "teardown func1*", - # "*1 fixture failure*" - ]) + result.stdout.fnmatch_lines( + [ + "*teardown_function*", + "*Captured stdout*", + "setup func1*", + "in func1*", + "teardown func1*", + # "*1 fixture failure*" + ] + ) def test_teardown_capturing_final(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def teardown_module(mod): print ("teardown module") assert 0 def test_func(): pass - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*def teardown_module(mod):*", - "*Captured stdout*", - "*teardown module*", - "*1 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*def teardown_module(mod):*", + "*Captured stdout*", + "*teardown module*", + "*1 error*", + ] + ) def test_capturing_outerr(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import sys def test_capturing(): print (42) @@ -263,35 +276,42 @@ class TestPerTestCapturing(object): print (1) sys.stderr.write(str(2)) raise ValueError - """) + """ + ) result = testdir.runpytest(p1) - result.stdout.fnmatch_lines([ - "*test_capturing_outerr.py .F", - "====* FAILURES *====", - "____*____", - "*test_capturing_outerr.py:8: ValueError", - "*--- Captured stdout *call*", - "1", - "*--- Captured stderr *call*", - "2", - ]) + result.stdout.fnmatch_lines( + [ + "*test_capturing_outerr.py .F*", + "====* FAILURES *====", + "____*____", + "*test_capturing_outerr.py:8: ValueError", + "*--- Captured stdout *call*", + "1", + "*--- Captured stderr *call*", + "2", + ] + ) class TestLoggingInteraction(object): + def test_logging_stream_ownership(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_logging(): import logging import pytest stream = capture.CaptureIO() logging.basicConfig(stream=stream) stream.close() # to free memory/release resources - """) + """ + ) result = testdir.runpytest_subprocess(p) assert result.stderr.str().find("atexit") == -1 def test_logging_and_immediate_setupteardown(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import logging def setup_function(function): logging.warn("hello1") @@ -303,21 +323,21 @@ class TestLoggingInteraction(object): def teardown_function(function): logging.warn("hello3") assert 0 - """) - for optargs in (('--capture=sys',), ('--capture=fd',)): + """ + ) + for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) result = testdir.runpytest_subprocess(p, *optargs) s = result.stdout.str() - result.stdout.fnmatch_lines([ - "*WARN*hello3", # errors show first! - "*WARN*hello1", - "*WARN*hello2", - ]) + result.stdout.fnmatch_lines( + ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors show first! + ) # verify proper termination assert "closed" not in s def test_logging_and_crossscope_fixtures(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import logging def setup_module(function): logging.warn("hello1") @@ -329,265 +349,390 @@ class TestLoggingInteraction(object): def teardown_module(function): logging.warn("hello3") assert 0 - """) - for optargs in (('--capture=sys',), ('--capture=fd',)): + """ + ) + for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) result = testdir.runpytest_subprocess(p, *optargs) s = result.stdout.str() - result.stdout.fnmatch_lines([ - "*WARN*hello3", # errors come first - "*WARN*hello1", - "*WARN*hello2", - ]) + result.stdout.fnmatch_lines( + ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors come first + ) # verify proper termination assert "closed" not in s - def test_logging_initialized_in_test(self, testdir): - p = testdir.makepyfile(""" - import sys - def test_something(): - # pytest does not import logging - assert 'logging' not in sys.modules - import logging - logging.basicConfig() - logging.warn("hello432") - assert 0 - """) - result = testdir.runpytest_subprocess( - p, "--traceconfig", - "-p", "no:capturelog", "-p", "no:hypothesis", "-p", "no:hypothesispytest") - assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*hello432*", - ]) - assert 'operation on closed file' not in result.stderr.str() - def test_conftestlogging_is_shown(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import logging logging.basicConfig() logging.warn("hello435") - """) + """ + ) # make sure that logging is still captured in tests result = testdir.runpytest_subprocess("-s", "-p", "no:capturelog") assert result.ret == EXIT_NOTESTSCOLLECTED - result.stderr.fnmatch_lines([ - "WARNING*hello435*", - ]) - assert 'operation on closed file' not in result.stderr.str() + result.stderr.fnmatch_lines(["WARNING*hello435*"]) + assert "operation on closed file" not in result.stderr.str() def test_conftestlogging_and_test_logging(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import logging logging.basicConfig() - """) + """ + ) # make sure that logging is still captured in tests - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_hello(): import logging logging.warn("hello433") assert 0 - """) + """ + ) result = testdir.runpytest_subprocess(p, "-p", "no:capturelog") assert result.ret != 0 - result.stdout.fnmatch_lines([ - "WARNING*hello433*", - ]) - assert 'something' not in result.stderr.str() - assert 'operation on closed file' not in result.stderr.str() + result.stdout.fnmatch_lines(["WARNING*hello433*"]) + assert "something" not in result.stderr.str() + assert "operation on closed file" not in result.stderr.str() class TestCaptureFixture(object): + @pytest.mark.parametrize("opt", [[], ["-s"]]) def test_std_functional(self, testdir, opt): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ def test_hello(capsys): print (42) out, err = capsys.readouterr() assert out.startswith("42") - """, *opt) + """, + *opt + ) reprec.assertoutcome(passed=1) def test_capsyscapfd(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_one(capsys, capfd): pass def test_two(capfd, capsys): pass - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*ERROR*setup*test_one*", - "E*capsys*capfd*same*time*", - "*ERROR*setup*test_two*", - "E*capsys*capfd*same*time*", - "*2 error*"]) + result.stdout.fnmatch_lines( + [ + "*ERROR*setup*test_one*", + "E*capfd*capsys*same*time*", + "*ERROR*setup*test_two*", + "E*capsys*capfd*same*time*", + "*2 error*", + ] + ) def test_capturing_getfixturevalue(self, testdir): """Test that asking for "capfd" and "capsys" using request.getfixturevalue in the same test is an error. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_one(capsys, request): request.getfixturevalue("capfd") def test_two(capfd, request): request.getfixturevalue("capsys") - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*test_one*", - "*capsys*capfd*same*time*", - "*test_two*", - "*capsys*capfd*same*time*", - "*2 failed in*", - ]) + result.stdout.fnmatch_lines( + [ + "*test_one*", + "*capsys*capfd*same*time*", + "*test_two*", + "*capfd*capsys*same*time*", + "*2 failed in*", + ] + ) + + def test_capsyscapfdbinary(self, testdir): + p = testdir.makepyfile( + """ + def test_one(capsys, capfdbinary): + pass + """ + ) + result = testdir.runpytest(p) + result.stdout.fnmatch_lines( + ["*ERROR*setup*test_one*", "E*capfdbinary*capsys*same*time*", "*1 error*"] + ) @pytest.mark.parametrize("method", ["sys", "fd"]) def test_capture_is_represented_on_failure_issue128(self, testdir, method): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_hello(cap%s): print ("xxx42xxx") assert 0 - """ % method) + """ + % method + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "xxx42xxx", - ]) + result.stdout.fnmatch_lines(["xxx42xxx"]) @needsosdup def test_stdfd_functional(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ def test_hello(capfd): import os os.write(1, "42".encode('ascii')) out, err = capfd.readouterr() assert out.startswith("42") capfd.close() - """) + """ + ) reprec.assertoutcome(passed=1) + @needsosdup + def test_capfdbinary(self, testdir): + reprec = testdir.inline_runsource( + """ + def test_hello(capfdbinary): + import os + # some likely un-decodable bytes + os.write(1, b'\\xfe\\x98\\x20') + out, err = capfdbinary.readouterr() + assert out == b'\\xfe\\x98\\x20' + assert err == b'' + """ + ) + reprec.assertoutcome(passed=1) + + @pytest.mark.skipif( + sys.version_info < (3,), reason="only have capsysbinary in python 3" + ) + def test_capsysbinary(self, testdir): + reprec = testdir.inline_runsource( + """ + def test_hello(capsysbinary): + import sys + # some likely un-decodable bytes + sys.stdout.buffer.write(b'\\xfe\\x98\\x20') + out, err = capsysbinary.readouterr() + assert out == b'\\xfe\\x98\\x20' + assert err == b'' + """ + ) + reprec.assertoutcome(passed=1) + + @pytest.mark.skipif( + sys.version_info >= (3,), reason="only have capsysbinary in python 3" + ) + def test_capsysbinary_forbidden_in_python2(self, testdir): + testdir.makepyfile( + """ + def test_hello(capsysbinary): + pass + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "*test_hello*", + "*capsysbinary is only supported on python 3*", + "*1 error in*", + ] + ) + def test_partial_setup_failure(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_hello(capsys, missingarg): pass - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*test_partial_setup_failure*", - "*1 error*", - ]) + result.stdout.fnmatch_lines(["*test_partial_setup_failure*", "*1 error*"]) @needsosdup def test_keyboardinterrupt_disables_capturing(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_hello(capfd): import os os.write(1, str(42).encode('ascii')) raise KeyboardInterrupt() - """) + """ + ) result = testdir.runpytest_subprocess(p) - result.stdout.fnmatch_lines([ - "*KeyboardInterrupt*" - ]) + result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) assert result.ret == 2 @pytest.mark.issue14 def test_capture_and_logging(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import logging def test_log(capsys): logging.error('x') - """) + """ + ) result = testdir.runpytest_subprocess(p) - assert 'closed' not in result.stderr.str() + assert "closed" not in result.stderr.str() - @pytest.mark.parametrize('fixture', ['capsys', 'capfd']) - def test_disabled_capture_fixture(self, testdir, fixture): - testdir.makepyfile(""" + @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) + @pytest.mark.parametrize("no_capture", [True, False]) + def test_disabled_capture_fixture(self, testdir, fixture, no_capture): + testdir.makepyfile( + """ def test_disabled({fixture}): print('captured before') with {fixture}.disabled(): print('while capture is disabled') print('captured after') - """.format(fixture=fixture)) - result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(""" + assert {fixture}.readouterr() == ('captured before\\ncaptured after\\n', '') + + def test_normal(): + print('test_normal executed') + """.format( + fixture=fixture + ) + ) + args = ("-s",) if no_capture else () + result = testdir.runpytest_subprocess(*args) + result.stdout.fnmatch_lines( + """ *while capture is disabled* - """) - assert 'captured before' not in result.stdout.str() - assert 'captured after' not in result.stdout.str() + """ + ) + assert "captured before" not in result.stdout.str() + assert "captured after" not in result.stdout.str() + if no_capture: + assert "test_normal executed" in result.stdout.str() + else: + assert "test_normal executed" not in result.stdout.str() + + @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) + def test_fixture_use_by_other_fixtures(self, testdir, fixture): + """ + Ensure that capsys and capfd can be used by other fixtures during setup and teardown. + """ + testdir.makepyfile( + """ + from __future__ import print_function + import sys + import pytest + + @pytest.fixture + def captured_print({fixture}): + print('stdout contents begin') + print('stderr contents begin', file=sys.stderr) + out, err = {fixture}.readouterr() + + yield out, err + + print('stdout contents end') + print('stderr contents end', file=sys.stderr) + out, err = {fixture}.readouterr() + assert out == 'stdout contents end\\n' + assert err == 'stderr contents end\\n' + + def test_captured_print(captured_print): + out, err = captured_print + assert out == 'stdout contents begin\\n' + assert err == 'stderr contents begin\\n' + """.format( + fixture=fixture + ) + ) + result = testdir.runpytest_subprocess() + result.stdout.fnmatch_lines("*1 passed*") + assert "stdout contents begin" not in result.stdout.str() + assert "stderr contents begin" not in result.stdout.str() def test_setup_failure_does_not_kill_capturing(testdir): sub1 = testdir.mkpydir("sub1") - sub1.join("conftest.py").write(_pytest._code.Source(""" + sub1.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_runtest_setup(item): raise ValueError(42) - """)) + """ + ) + ) sub1.join("test_mod.py").write("def test_func1(): pass") - result = testdir.runpytest(testdir.tmpdir, '--traceconfig') - result.stdout.fnmatch_lines([ - "*ValueError(42)*", - "*1 error*" - ]) + result = testdir.runpytest(testdir.tmpdir, "--traceconfig") + result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"]) def test_fdfuncarg_skips_on_no_osdup(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os if hasattr(os, 'dup'): del os.dup def test_hello(capfd): pass - """) + """ + ) result = testdir.runpytest_subprocess("--capture=no") - result.stdout.fnmatch_lines([ - "*1 skipped*" - ]) + result.stdout.fnmatch_lines(["*1 skipped*"]) def test_capture_conftest_runtest_setup(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_runtest_setup(): print ("hello19") - """) + """ + ) testdir.makepyfile("def test_func(): pass") result = testdir.runpytest() assert result.ret == 0 - assert 'hello19' not in result.stdout.str() + assert "hello19" not in result.stdout.str() def test_capture_badoutput_issue412(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os def test_func(): omg = bytearray([1,129,1]) os.write(1, omg) assert 0 - """) - result = testdir.runpytest('--cap=fd') - result.stdout.fnmatch_lines(''' + """ + ) + result = testdir.runpytest("--cap=fd") + result.stdout.fnmatch_lines( + """ *def test_func* *assert 0* *Captured* *1 failed* - ''') + """ + ) def test_capture_early_option_parsing(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_runtest_setup(): print ("hello19") - """) + """ + ) testdir.makepyfile("def test_func(): pass") result = testdir.runpytest("-vs") assert result.ret == 0 - assert 'hello19' in result.stdout.str() + assert "hello19" in result.stdout.str() def test_capture_binary_output(testdir): - testdir.makepyfile(r""" + testdir.makepyfile( + r""" import pytest def test_a(): @@ -600,31 +745,33 @@ def test_capture_binary_output(testdir): if __name__ == '__main__': test_foo() - """) - result = testdir.runpytest('--assert=plain') + """ + ) + result = testdir.runpytest("--assert=plain") result.assert_outcomes(passed=2) def test_error_during_readouterr(testdir): """Make sure we suspend capturing if errors occur during readouterr""" - testdir.makepyfile(pytest_xyz=""" + testdir.makepyfile( + pytest_xyz=""" from _pytest.capture import FDCapture def bad_snap(self): raise Exception('boom') assert FDCapture.snap FDCapture.snap = bad_snap - """) + """ + ) result = testdir.runpytest_subprocess( "-p", "pytest_xyz", "--version", syspathinsert=True ) - result.stderr.fnmatch_lines([ - "*in bad_snap", - " raise Exception('boom')", - "Exception: boom", - ]) + result.stderr.fnmatch_lines( + ["*in bad_snap", " raise Exception('boom')", "Exception: boom"] + ) class TestCaptureIO(object): + def test_text(self): f = capture.CaptureIO() f.write("hello") @@ -638,23 +785,20 @@ class TestCaptureIO(object): f.write("\u00f6") pytest.raises(TypeError, "f.write(bytes('hello', 'UTF-8'))") else: - f.write(unicode("\u00f6", 'UTF-8')) + f.write(text_type("\u00f6", "UTF-8")) f.write("hello") # bytes s = f.getvalue() f.close() - assert isinstance(s, unicode) + assert isinstance(s, text_type) - @pytest.mark.skipif( - sys.version_info[0] == 2, - reason='python 3 only behaviour', - ) + @pytest.mark.skipif(sys.version_info[0] == 2, reason="python 3 only behaviour") def test_write_bytes_to_buffer(self): """In python3, stdout / stderr are text io wrappers (exposing a buffer property of the underlying bytestream). See issue #1407 """ f = capture.CaptureIO() - f.buffer.write(b'foo\r\n') - assert f.getvalue() == 'foo\r\n' + f.buffer.write(b"foo\r\n") + assert f.getvalue() == "foo\r\n" def test_bytes_io(): @@ -667,31 +811,36 @@ def test_bytes_io(): def test_dontreadfrominput(): from _pytest.capture import DontReadFromInput + f = DontReadFromInput() assert not f.isatty() pytest.raises(IOError, f.read) pytest.raises(IOError, f.readlines) - pytest.raises(IOError, iter, f) + iter_f = iter(f) + pytest.raises(IOError, next, iter_f) pytest.raises(UnsupportedOperation, f.fileno) f.close() # just for completeness -@pytest.mark.skipif('sys.version_info < (3,)', reason='python2 has no buffer') +@pytest.mark.skipif("sys.version_info < (3,)", reason="python2 has no buffer") def test_dontreadfrominput_buffer_python3(): from _pytest.capture import DontReadFromInput + f = DontReadFromInput() fb = f.buffer assert not fb.isatty() pytest.raises(IOError, fb.read) pytest.raises(IOError, fb.readlines) - pytest.raises(IOError, iter, fb) + iter_f = iter(f) + pytest.raises(IOError, next, iter_f) pytest.raises(ValueError, fb.fileno) f.close() # just for completeness -@pytest.mark.skipif('sys.version_info >= (3,)', reason='python2 has no buffer') +@pytest.mark.skipif("sys.version_info >= (3,)", reason="python2 has no buffer") def test_dontreadfrominput_buffer_python2(): from _pytest.capture import DontReadFromInput + f = DontReadFromInput() with pytest.raises(AttributeError): f.buffer @@ -700,7 +849,7 @@ def test_dontreadfrominput_buffer_python2(): @pytest.yield_fixture def tmpfile(testdir): - f = testdir.makepyfile("").open('wb+') + f = testdir.makepyfile("").open("wb+") yield f if not f.closed: f.close() @@ -738,7 +887,7 @@ def test_dupfile_on_bytesio(): f = capture.safe_text_dupfile(io, "wb") f.write("hello") assert io.getvalue() == b"hello" - assert 'BytesIO object' in f.name + assert "BytesIO object" in f.name def test_dupfile_on_textio(): @@ -746,7 +895,7 @@ def test_dupfile_on_textio(): f = capture.safe_text_dupfile(io, "wb") f.write("hello") assert io.getvalue() == "hello" - assert not hasattr(f, 'name') + assert not hasattr(f, "name") @contextlib.contextmanager @@ -788,7 +937,7 @@ class TestFDCapture(object): def test_simple_many_check_open_files(self, testdir): with lsof_check(): - with testdir.makepyfile("").open('wb+') as tmpfile: + with testdir.makepyfile("").open("wb+") as tmpfile: self.test_simple_many(tmpfile) def test_simple_fail_second_start(self, tmpfile): @@ -810,7 +959,7 @@ class TestFDCapture(object): cap.start() x = os.read(0, 100).strip() cap.done() - assert x == tobytes('') + assert x == tobytes("") def test_writeorg(self, tmpfile): data1, data2 = tobytes("foo"), tobytes("bar") @@ -822,7 +971,7 @@ class TestFDCapture(object): scap = cap.snap() cap.done() assert scap == totext(data1) - with open(tmpfile.name, 'rb') as stmp_file: + with open(tmpfile.name, "rb") as stmp_file: stmp = stmp_file.read() assert stmp == data2 @@ -898,20 +1047,29 @@ class TestStdCapture(object): out, err = cap.readouterr() assert err == "error2" + def test_capture_results_accessible_by_attribute(self): + with self.getcapture() as cap: + sys.stdout.write("hello") + sys.stderr.write("world") + capture_result = cap.readouterr() + assert capture_result.out == "hello" + assert capture_result.err == "world" + def test_capturing_readouterr_unicode(self): with self.getcapture() as cap: print("hx\xc4\x85\xc4\x87") out, err = cap.readouterr() assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8") - @pytest.mark.skipif('sys.version_info >= (3,)', - reason='text output different for bytes on python3') + @pytest.mark.skipif( + "sys.version_info >= (3,)", reason="text output different for bytes on python3" + ) def test_capturing_readouterr_decode_error_handling(self): with self.getcapture() as cap: - # triggered a internal error in pytest - print('\xa6') + # triggered an internal error in pytest + print("\xa6") out, err = cap.readouterr() - assert out == py.builtin._totext('\ufffd\n', 'unicode-escape') + assert out == py.builtin._totext("\ufffd\n", "unicode-escape") def test_reset_twice_error(self): with self.getcapture() as cap: @@ -983,18 +1141,22 @@ class TestStdCaptureFD(TestStdCapture): captureclass = staticmethod(StdCaptureFD) def test_simple_only_fd(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os def test_x(): os.write(1, "hello\\n".encode("ascii")) assert 0 - """) + """ + ) result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *test_x* *assert 0* *Captured stdout* - """) + """ + ) def test_intermingling(self): with self.getcapture() as cap: @@ -1021,7 +1183,8 @@ class TestStdCaptureFDinvalidFD(object): pytestmark = needsosdup def test_stdcapture_fd_invalid_fd(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os from _pytest import capture def StdCaptureFD(out=True, err=True, in_=True): @@ -1039,10 +1202,11 @@ class TestStdCaptureFDinvalidFD(object): os.close(0) cap = StdCaptureFD(out=False, err=False, in_=True) cap.stop_capturing() - """) + """ + ) result = testdir.runpytest_subprocess("--capture=fd") assert result.ret == 0 - assert result.parseoutcomes()['passed'] == 3 + assert result.parseoutcomes()["passed"] == 3 def test_capture_not_started_but_reset(): @@ -1051,16 +1215,24 @@ def test_capture_not_started_but_reset(): def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys): - test_text = 'test text' + test_text = "test text" - print(test_text.encode(sys.stdout.encoding, 'replace')) + print(test_text.encode(sys.stdout.encoding, "replace")) (out, err) = capsys.readouterr() assert out - assert err == '' + assert err == "" + + +def test_capsys_results_accessible_by_attribute(capsys): + sys.stdout.write("spam") + sys.stderr.write("eggs") + capture_result = capsys.readouterr() + assert capture_result.out == "spam" + assert capture_result.err == "eggs" @needsosdup -@pytest.mark.parametrize('use', [True, False]) +@pytest.mark.parametrize("use", [True, False]) def test_fdcapture_tmpfile_remains_the_same(tmpfile, use): if not use: tmpfile = True @@ -1077,29 +1249,34 @@ def test_fdcapture_tmpfile_remains_the_same(tmpfile, use): @needsosdup def test_close_and_capture_again(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os def test_close(): os.close(1) def test_capture_again(): os.write(1, b"hello\\n") assert 0 - """) + """ + ) result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *test_capture_again* *assert 0* *stdout* *hello* - """) + """ + ) -@pytest.mark.parametrize('method', ['SysCapture', 'FDCapture']) +@pytest.mark.parametrize("method", ["SysCapture", "FDCapture"]) def test_capturing_and_logging_fundamentals(testdir, method): - if method == "StdCaptureFD" and not hasattr(os, 'dup'): + if method == "StdCaptureFD" and not hasattr(os, "dup"): pytest.skip("need os.dup") # here we check a fundamental feature - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import sys, os import py, logging from _pytest import capture @@ -1117,31 +1294,41 @@ def test_capturing_and_logging_fundamentals(testdir, method): outerr = cap.readouterr() print ("suspend2, captured %%s" %% (outerr,)) - """ % (method,)) + """ + % (method,) + ) result = testdir.runpython(p) - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ suspend, captured*hello1* suspend2, captured*WARNING:root:hello3* - """) - result.stderr.fnmatch_lines(""" + """ + ) + result.stderr.fnmatch_lines( + """ WARNING:root:hello2 - """) + """ + ) assert "atexit" not in result.stderr.str() def test_error_attribute_issue555(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys def test_capattr(): assert sys.stdout.errors == "strict" assert sys.stderr.errors == "strict" - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) -@pytest.mark.skipif(not sys.platform.startswith('win') and sys.version_info[:2] >= (3, 6), - reason='only py3.6+ on windows') +@pytest.mark.skipif( + not sys.platform.startswith("win") and sys.version_info[:2] >= (3, 6), + reason="only py3.6+ on windows", +) def test_py36_windowsconsoleio_workaround_non_standard_streams(): """ Ensure _py36_windowsconsoleio_workaround function works with objects that @@ -1149,7 +1336,8 @@ def test_py36_windowsconsoleio_workaround_non_standard_streams(): """ from _pytest.capture import _py36_windowsconsoleio_workaround - class DummyStream: + class DummyStream(object): + def write(self, s): pass @@ -1158,18 +1346,46 @@ def test_py36_windowsconsoleio_workaround_non_standard_streams(): def test_dontreadfrominput_has_encoding(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys def test_capattr(): # should not raise AttributeError assert sys.stdout.encoding assert sys.stderr.encoding - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) -def test_pickling_and_unpickling_enocded_file(): +def test_crash_on_closing_tmpfile_py27(testdir): + testdir.makepyfile( + """ + from __future__ import print_function + import time + import threading + import sys + + def spam(): + f = sys.stderr + while True: + print('.', end='', file=f) + + def test_silly(): + t = threading.Thread(target=spam) + t.daemon = True + t.start() + time.sleep(0.5) + + """ + ) + result = testdir.runpytest_subprocess() + assert result.ret == 0 + assert "IOError" not in result.stdout.str() + + +def test_pickling_and_unpickling_encoded_file(): # See https://bitbucket.org/pytest-dev/pytest/pull-request/194 # pickle.loads() raises infinite recursion if # EncodedFile.__getattr__ is not implemented properly diff --git a/third_party/python/pytest/testing/test_collection.py b/third_party/python/pytest/testing/test_collection.py index cf13d4b00cba..657d64c74e24 100644 --- a/third_party/python/pytest/testing/test_collection.py +++ b/third_party/python/pytest/testing/test_collection.py @@ -1,22 +1,27 @@ from __future__ import absolute_import, division, print_function +import pprint +import sys import pytest -import py import _pytest._code from _pytest.main import Session, EXIT_NOTESTSCOLLECTED, _in_venv class TestCollector(object): + def test_collect_versus_item(self): from pytest import Collector, Item + assert not issubclass(Collector, Item) assert not issubclass(Item, Collector) def test_compat_attributes(self, testdir, recwarn): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def test_pass(): pass def test_fail(): assert 0 - """) + """ + ) recwarn.clear() assert modcol.Module == pytest.Module assert modcol.Class == pytest.Class @@ -25,10 +30,12 @@ class TestCollector(object): assert modcol.Function == pytest.Function def test_check_equality(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def test_pass(): pass def test_fail(): assert 0 - """) + """ + ) fn1 = testdir.collect_by_name(modcol, "test_pass") assert isinstance(fn1, pytest.Function) fn2 = testdir.collect_by_name(modcol, "test_pass") @@ -36,8 +43,8 @@ class TestCollector(object): assert fn1 == fn2 assert fn1 != modcol - if py.std.sys.version_info < (3, 0): - assert cmp(fn1, fn2) == 0 + if sys.version_info < (3, 0): + assert cmp(fn1, fn2) == 0 # NOQA assert hash(fn1) == hash(fn2) fn3 = testdir.collect_by_name(modcol, "test_fail") @@ -53,14 +60,15 @@ class TestCollector(object): assert modcol != fn def test_getparent(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ class TestClass(object): def test_foo(): pass - """) + """ + ) cls = testdir.collect_by_name(modcol, "TestClass") - fn = testdir.collect_by_name( - testdir.collect_by_name(cls, "()"), "test_foo") + fn = testdir.collect_by_name(testdir.collect_by_name(cls, "()"), "test_foo") parent = fn.getparent(pytest.Module) assert parent is modcol @@ -73,14 +81,16 @@ class TestCollector(object): def test_getcustomfile_roundtrip(self, testdir): hello = testdir.makefile(".xxx", hello="world") - testdir.makepyfile(conftest=""" + testdir.makepyfile( + conftest=""" import pytest class CustomFile(pytest.File): pass def pytest_collect_file(path, parent): if path.ext == ".xxx": return CustomFile(path, parent=parent) - """) + """ + ) node = testdir.getpathnode(hello) assert isinstance(node, pytest.File) assert node.name == "hello.xxx" @@ -90,32 +100,32 @@ class TestCollector(object): def test_can_skip_class_with_test_attr(self, testdir): """Assure test class is skipped when using `__test__=False` (See #2007).""" - testdir.makepyfile(""" + testdir.makepyfile( + """ class TestFoo(object): __test__ = False def __init__(self): pass def test_foo(): assert True - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - 'collected 0 items', - '*no tests ran in*', - ]) + result.stdout.fnmatch_lines(["collected 0 items", "*no tests ran in*"]) class TestCollectFS(object): + def test_ignored_certain_directories(self, testdir): tmpdir = testdir.tmpdir - tmpdir.ensure("build", 'test_notfound.py') - tmpdir.ensure("dist", 'test_notfound.py') - tmpdir.ensure("_darcs", 'test_notfound.py') - tmpdir.ensure("CVS", 'test_notfound.py') - tmpdir.ensure("{arch}", 'test_notfound.py') - tmpdir.ensure(".whatever", 'test_notfound.py') - tmpdir.ensure(".bzr", 'test_notfound.py') - tmpdir.ensure("normal", 'test_found.py') + tmpdir.ensure("build", "test_notfound.py") + tmpdir.ensure("dist", "test_notfound.py") + tmpdir.ensure("_darcs", "test_notfound.py") + tmpdir.ensure("CVS", "test_notfound.py") + tmpdir.ensure("{arch}", "test_notfound.py") + tmpdir.ensure(".whatever", "test_notfound.py") + tmpdir.ensure(".bzr", "test_notfound.py") + tmpdir.ensure("normal", "test_found.py") for x in tmpdir.visit("test_*.py"): x.write("def test_hello(): pass") @@ -124,11 +134,19 @@ class TestCollectFS(object): assert "test_notfound" not in s assert "test_found" in s - @pytest.mark.parametrize('fname', - ("activate", "activate.csh", "activate.fish", - "Activate", "Activate.bat", "Activate.ps1")) + @pytest.mark.parametrize( + "fname", + ( + "activate", + "activate.csh", + "activate.fish", + "Activate", + "Activate.bat", + "Activate.ps1", + ), + ) def test_ignored_virtualenvs(self, testdir, fname): - bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" + bindir = "Scripts" if sys.platform.startswith("win") else "bin" testdir.tmpdir.ensure("virtual", bindir, fname) testfile = testdir.tmpdir.ensure("virtual", "test_invenv.py") testfile.write("def test_hello(): pass") @@ -143,11 +161,19 @@ class TestCollectFS(object): result = testdir.runpytest("virtual") assert "test_invenv" in result.stdout.str() - @pytest.mark.parametrize('fname', - ("activate", "activate.csh", "activate.fish", - "Activate", "Activate.bat", "Activate.ps1")) + @pytest.mark.parametrize( + "fname", + ( + "activate", + "activate.csh", + "activate.fish", + "Activate", + "Activate.bat", + "Activate.ps1", + ), + ) def test_ignored_virtualenvs_norecursedirs_precedence(self, testdir, fname): - bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" + bindir = "Scripts" if sys.platform.startswith("win") else "bin" # norecursedirs takes priority testdir.tmpdir.ensure(".virtual", bindir, fname) testfile = testdir.tmpdir.ensure(".virtual", "test_invenv.py") @@ -158,24 +184,34 @@ class TestCollectFS(object): result = testdir.runpytest("--collect-in-virtualenv", ".virtual") assert "test_invenv" in result.stdout.str() - @pytest.mark.parametrize('fname', - ("activate", "activate.csh", "activate.fish", - "Activate", "Activate.bat", "Activate.ps1")) + @pytest.mark.parametrize( + "fname", + ( + "activate", + "activate.csh", + "activate.fish", + "Activate", + "Activate.bat", + "Activate.ps1", + ), + ) def test__in_venv(self, testdir, fname): """Directly test the virtual env detection function""" - bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" + bindir = "Scripts" if sys.platform.startswith("win") else "bin" # no bin/activate, not a virtualenv - base_path = testdir.tmpdir.mkdir('venv') + base_path = testdir.tmpdir.mkdir("venv") assert _in_venv(base_path) is False # with bin/activate, totally a virtualenv base_path.ensure(bindir, fname) assert _in_venv(base_path) is True def test_custom_norecursedirs(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] norecursedirs = mydir xyz* - """) + """ + ) tmpdir = testdir.tmpdir tmpdir.ensure("mydir", "test_hello.py").write("def test_1(): pass") tmpdir.ensure("xyz123", "test_2.py").write("def test_2(): 0/0") @@ -186,10 +222,12 @@ class TestCollectFS(object): rec.assertoutcome(failed=1) def test_testpaths_ini(self, testdir, monkeypatch): - testdir.makeini(""" + testdir.makeini( + """ [pytest] testpaths = gui uts - """) + """ + ) tmpdir = testdir.tmpdir tmpdir.ensure("env", "test_1.py").write("def test_env(): pass") tmpdir.ensure("gui", "test_2.py").write("def test_gui(): pass") @@ -197,28 +235,30 @@ class TestCollectFS(object): # executing from rootdir only tests from `testpaths` directories # are collected - items, reprec = testdir.inline_genitems('-v') - assert [x.name for x in items] == ['test_gui', 'test_uts'] + items, reprec = testdir.inline_genitems("-v") + assert [x.name for x in items] == ["test_gui", "test_uts"] # check that explicitly passing directories in the command-line # collects the tests - for dirname in ('env', 'gui', 'uts'): + for dirname in ("env", "gui", "uts"): items, reprec = testdir.inline_genitems(tmpdir.join(dirname)) - assert [x.name for x in items] == ['test_%s' % dirname] + assert [x.name for x in items] == ["test_%s" % dirname] # changing cwd to each subdirectory and running pytest without # arguments collects the tests in that directory normally - for dirname in ('env', 'gui', 'uts'): + for dirname in ("env", "gui", "uts"): monkeypatch.chdir(testdir.tmpdir.join(dirname)) items, reprec = testdir.inline_genitems() - assert [x.name for x in items] == ['test_%s' % dirname] + assert [x.name for x in items] == ["test_%s" % dirname] class TestCollectPluginHookRelay(object): + def test_pytest_collect_file(self, testdir): wascalled = [] class Plugin(object): + def pytest_collect_file(self, path, parent): if not path.basename.startswith("."): # Ignore hidden files, e.g. .testmondata. @@ -227,12 +267,13 @@ class TestCollectPluginHookRelay(object): testdir.makefile(".abc", "xyz") pytest.main([testdir.tmpdir], plugins=[Plugin()]) assert len(wascalled) == 1 - assert wascalled[0].ext == '.abc' + assert wascalled[0].ext == ".abc" def test_pytest_collect_directory(self, testdir): wascalled = [] class Plugin(object): + def pytest_collect_directory(self, path, parent): wascalled.append(path.basename) @@ -246,10 +287,13 @@ class TestCollectPluginHookRelay(object): class TestPrunetraceback(object): def test_custom_repr_failure(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import not_exists - """) - testdir.makeconftest(""" + """ + ) + testdir.makeconftest( + """ import pytest def pytest_collect_file(path, parent): return MyFile(path, parent) @@ -262,40 +306,44 @@ class TestPrunetraceback(object): if excinfo.errisinstance(MyError): return "hello world" return pytest.File.repr_failure(self, excinfo) - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*ERROR collecting*", - "*hello world*", - ]) + result.stdout.fnmatch_lines(["*ERROR collecting*", "*hello world*"]) @pytest.mark.xfail(reason="other mechanism for adding to reporting needed") def test_collect_report_postprocessing(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import not_exists - """) - testdir.makeconftest(""" + """ + ) + testdir.makeconftest( + """ import pytest - def pytest_make_collect_report(__multicall__): - rep = __multicall__.execute() + @pytest.hookimpl(hookwrapper=True) + def pytest_make_collect_report(): + outcome = yield + rep = outcome.get_result() rep.headerlines += ["header1"] - return rep - """) + outcome.force_result(rep) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*ERROR collecting*", - "*header1*", - ]) + result.stdout.fnmatch_lines(["*ERROR collecting*", "*header1*"]) class TestCustomConftests(object): + def test_ignore_collect_path(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_ignore_collect(path, config): return path.basename.startswith("x") or \ path.basename == "test_one.py" - """) + """ + ) sub = testdir.mkdir("xy123") sub.ensure("test_hello.py").write("syntax error") sub.join("conftest.py").write("syntax error") @@ -306,10 +354,12 @@ class TestCustomConftests(object): result.stdout.fnmatch_lines(["*1 passed*"]) def test_ignore_collect_not_called_on_argument(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_ignore_collect(path, config): return True - """) + """ + ) p = testdir.makepyfile("def test_hello(): pass") result = testdir.runpytest(p) assert result.ret == 0 @@ -319,14 +369,16 @@ class TestCustomConftests(object): result.stdout.fnmatch_lines("*collected 0 items*") def test_collectignore_exclude_on_option(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ collect_ignore = ['hello', 'test_world.py'] def pytest_addoption(parser): parser.addoption("--XX", action="store_true", default=False) def pytest_configure(config): if config.getvalue("XX"): collect_ignore[:] = [] - """) + """ + ) testdir.mkdir("hello") testdir.makepyfile(test_world="def test_hello(): pass") result = testdir.runpytest() @@ -337,55 +389,55 @@ class TestCustomConftests(object): assert "passed" in result.stdout.str() def test_pytest_fs_collect_hooks_are_seen(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyModule(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": return MyModule(path, parent) - """) + """ + ) testdir.mkdir("sub") testdir.makepyfile("def test_x(): pass") result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*MyModule*", - "*test_x*" - ]) + result.stdout.fnmatch_lines(["*MyModule*", "*test_x*"]) def test_pytest_collect_file_from_sister_dir(self, testdir): sub1 = testdir.mkpydir("sub1") sub2 = testdir.mkpydir("sub2") - conf1 = testdir.makeconftest(""" + conf1 = testdir.makeconftest( + """ import pytest class MyModule1(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": return MyModule1(path, parent) - """) + """ + ) conf1.move(sub1.join(conf1.basename)) - conf2 = testdir.makeconftest(""" + conf2 = testdir.makeconftest( + """ import pytest class MyModule2(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": return MyModule2(path, parent) - """) + """ + ) conf2.move(sub2.join(conf2.basename)) p = testdir.makepyfile("def test_x(): pass") p.copy(sub1.join(p.basename)) p.copy(sub2.join(p.basename)) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*MyModule1*", - "*MyModule2*", - "*test_x*" - ]) + result.stdout.fnmatch_lines(["*MyModule1*", "*MyModule2*", "*test_x*"]) class TestSession(object): + def test_parsearg(self, testdir): p = testdir.makepyfile("def test_func(): pass") subdir = testdir.mkdir("sub") @@ -422,9 +474,13 @@ class TestSession(object): def get_reported_items(self, hookrec): """Return pytest.Item instances reported by the pytest_collectreport hook""" - calls = hookrec.getcalls('pytest_collectreport') - return [x for call in calls for x in call.report.result - if isinstance(x, pytest.Item)] + calls = hookrec.getcalls("pytest_collectreport") + return [ + x + for call in calls + for x in call.report.result + if isinstance(x, pytest.Item) + ] def test_collect_protocol_single_function(self, testdir): p = testdir.makepyfile("def test_func(): pass") @@ -434,42 +490,48 @@ class TestSession(object): assert item.name == "test_func" newid = item.nodeid assert newid == id - py.std.pprint.pprint(hookrec.calls) + pprint.pprint(hookrec.calls) topdir = testdir.tmpdir # noqa - hookrec.assert_contains([ - ("pytest_collectstart", "collector.fspath == topdir"), - ("pytest_make_collect_report", "collector.fspath == topdir"), - ("pytest_collectstart", "collector.fspath == p"), - ("pytest_make_collect_report", "collector.fspath == p"), - ("pytest_pycollect_makeitem", "name == 'test_func'"), - ("pytest_collectreport", "report.result[0].name == 'test_func'"), - ]) + hookrec.assert_contains( + [ + ("pytest_collectstart", "collector.fspath == topdir"), + ("pytest_make_collect_report", "collector.fspath == topdir"), + ("pytest_collectstart", "collector.fspath == p"), + ("pytest_make_collect_report", "collector.fspath == p"), + ("pytest_pycollect_makeitem", "name == 'test_func'"), + ("pytest_collectreport", "report.result[0].name == 'test_func'"), + ] + ) # ensure we are reporting the collection of the single test item (#2464) - assert [x.name for x in self.get_reported_items(hookrec)] == ['test_func'] + assert [x.name for x in self.get_reported_items(hookrec)] == ["test_func"] def test_collect_protocol_method(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ class TestClass(object): def test_method(self): pass - """) + """ + ) normid = p.basename + "::TestClass::()::test_method" - for id in [p.basename, - p.basename + "::TestClass", - p.basename + "::TestClass::()", - normid, - ]: + for id in [ + p.basename, + p.basename + "::TestClass", + p.basename + "::TestClass::()", + normid, + ]: items, hookrec = testdir.inline_genitems(id) assert len(items) == 1 assert items[0].name == "test_method" newid = items[0].nodeid assert newid == normid # ensure we are reporting the collection of the single test item (#2464) - assert [x.name for x in self.get_reported_items(hookrec)] == ['test_method'] + assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"] def test_collect_custom_nodes_multi_id(self, testdir): p = testdir.makepyfile("def test_func(): pass") - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class SpecialItem(pytest.Item): def runtest(self): @@ -480,22 +542,26 @@ class TestSession(object): def pytest_collect_file(path, parent): if path.basename == %r: return SpecialFile(fspath=path, parent=parent) - """ % p.basename) + """ + % p.basename + ) id = p.basename items, hookrec = testdir.inline_genitems(id) - py.std.pprint.pprint(hookrec.calls) + pprint.pprint(hookrec.calls) assert len(items) == 2 - hookrec.assert_contains([ - ("pytest_collectstart", - "collector.fspath == collector.session.fspath"), - ("pytest_collectstart", - "collector.__class__.__name__ == 'SpecialFile'"), - ("pytest_collectstart", - "collector.__class__.__name__ == 'Module'"), - ("pytest_pycollect_makeitem", "name == 'test_func'"), - ("pytest_collectreport", "report.nodeid.startswith(p.basename)"), - ]) + hookrec.assert_contains( + [ + ("pytest_collectstart", "collector.fspath == collector.session.fspath"), + ( + "pytest_collectstart", + "collector.__class__.__name__ == 'SpecialFile'", + ), + ("pytest_collectstart", "collector.__class__.__name__ == 'Module'"), + ("pytest_pycollect_makeitem", "name == 'test_func'"), + ("pytest_collectreport", "report.nodeid.startswith(p.basename)"), + ] + ) assert len(self.get_reported_items(hookrec)) == 2 def test_collect_subdir_event_ordering(self, testdir): @@ -506,13 +572,14 @@ class TestSession(object): items, hookrec = testdir.inline_genitems() assert len(items) == 1 - py.std.pprint.pprint(hookrec.calls) - hookrec.assert_contains([ - ("pytest_collectstart", "collector.fspath == test_aaa"), - ("pytest_pycollect_makeitem", "name == 'test_func'"), - ("pytest_collectreport", - "report.nodeid.startswith('aaa/test_aaa.py')"), - ]) + pprint.pprint(hookrec.calls) + hookrec.assert_contains( + [ + ("pytest_collectstart", "collector.fspath == test_aaa"), + ("pytest_pycollect_makeitem", "name == 'test_func'"), + ("pytest_collectreport", "report.nodeid.startswith('aaa/test_aaa.py')"), + ] + ) def test_collect_two_commandline_args(self, testdir): p = testdir.makepyfile("def test_func(): pass") @@ -527,15 +594,17 @@ class TestSession(object): items, hookrec = testdir.inline_genitems(id) assert len(items) == 2 - py.std.pprint.pprint(hookrec.calls) - hookrec.assert_contains([ - ("pytest_collectstart", "collector.fspath == test_aaa"), - ("pytest_pycollect_makeitem", "name == 'test_func'"), - ("pytest_collectreport", "report.nodeid == 'aaa/test_aaa.py'"), - ("pytest_collectstart", "collector.fspath == test_bbb"), - ("pytest_pycollect_makeitem", "name == 'test_func'"), - ("pytest_collectreport", "report.nodeid == 'bbb/test_bbb.py'"), - ]) + pprint.pprint(hookrec.calls) + hookrec.assert_contains( + [ + ("pytest_collectstart", "collector.fspath == test_aaa"), + ("pytest_pycollect_makeitem", "name == 'test_func'"), + ("pytest_collectreport", "report.nodeid == 'aaa/test_aaa.py'"), + ("pytest_collectstart", "collector.fspath == test_bbb"), + ("pytest_pycollect_makeitem", "name == 'test_func'"), + ("pytest_collectreport", "report.nodeid == 'bbb/test_bbb.py'"), + ] + ) def test_serialization_byid(self, testdir): testdir.makepyfile("def test_func(): pass") @@ -548,29 +617,31 @@ class TestSession(object): assert item2.fspath == item.fspath def test_find_byid_without_instance_parents(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ class TestClass(object): def test_method(self): pass - """) + """ + ) arg = p.basename + "::TestClass::test_method" items, hookrec = testdir.inline_genitems(arg) assert len(items) == 1 item, = items assert item.nodeid.endswith("TestClass::()::test_method") # ensure we are reporting the collection of the single test item (#2464) - assert [x.name for x in self.get_reported_items(hookrec)] == ['test_method'] + assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"] class Test_getinitialnodes(object): + def test_global_file(self, testdir, tmpdir): x = tmpdir.ensure("x.py") with tmpdir.as_cwd(): config = testdir.parseconfigure(x) col = testdir.getnode(config, x) assert isinstance(col, pytest.Module) - assert col.name == 'x.py' - assert col.parent.name == testdir.tmpdir.basename + assert col.name == "x.py" assert col.parent.parent is None for col in col.listchain(): assert col.config is config @@ -584,21 +655,24 @@ class Test_getinitialnodes(object): config = testdir.parseconfigure(x) col = testdir.getnode(config, x) assert isinstance(col, pytest.Module) - assert col.name == 'x.py' + assert col.name == "x.py" assert col.parent.parent is None for col in col.listchain(): assert col.config is config class Test_genitems(object): + def test_check_collect_hashes(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_1(): pass def test_2(): pass - """) + """ + ) p.copy(p.dirpath(p.purebasename + "2" + ".py")) items, reprec = testdir.inline_genitems(p.dirpath()) assert len(items) == 4 @@ -609,7 +683,8 @@ class Test_genitems(object): assert i != j def test_example_items1(self, testdir): - p = testdir.makepyfile(''' + p = testdir.makepyfile( + """ def testone(): pass @@ -619,12 +694,13 @@ class Test_genitems(object): class TestY(TestX): pass - ''') + """ + ) items, reprec = testdir.inline_genitems(p) assert len(items) == 3 - assert items[0].name == 'testone' - assert items[1].name == 'testmethod_one' - assert items[2].name == 'testmethod_one' + assert items[0].name == "testone" + assert items[1].name == "testmethod_one" + assert items[2].name == "testmethod_one" # let's also test getmodpath here assert items[0].getmodpath() == "testone" @@ -640,12 +716,15 @@ class Test_genitems(object): tests that python_classes and python_functions config options work as prefixes and glob-like patterns (issue #600). """ - testdir.makeini(""" + testdir.makeini( + """ [pytest] python_classes = *Suite Test python_functions = *_test test - """) - p = testdir.makepyfile(''' + """ + ) + p = testdir.makepyfile( + """ class MyTestSuite(object): def x_test(self): pass @@ -653,14 +732,16 @@ class Test_genitems(object): class TestCase(object): def test_y(self): pass - ''') + """ + ) items, reprec = testdir.inline_genitems(p) ids = [x.getmodpath() for x in items] - assert ids == ['MyTestSuite.x_test', 'TestCase.test_y'] + assert ids == ["MyTestSuite.x_test", "TestCase.test_y"] def test_matchnodes_two_collections_same_file(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_configure(config): config.pluginmanager.register(Plugin2()) @@ -684,25 +765,25 @@ def test_matchnodes_two_collections_same_file(testdir): class Item2(pytest.Item): def runtest(self): pass - """) + """ + ) p = testdir.makefile(".abc", "") result = testdir.runpytest() assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*2 passed*", - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) res = testdir.runpytest("%s::hello" % p.basename) - res.stdout.fnmatch_lines([ - "*1 passed*", - ]) + res.stdout.fnmatch_lines(["*1 passed*"]) class TestNodekeywords(object): + def test_no_under(self, testdir): - modcol = testdir.getmodulecol(""" + modcol = testdir.getmodulecol( + """ def test_pass(): pass def test_fail(): assert 0 - """) + """ + ) values = list(modcol.keywords) assert modcol.name in values for x in values: @@ -710,13 +791,15 @@ class TestNodekeywords(object): assert modcol.name in repr(modcol.keywords) def test_issue345(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_should_not_be_selected(): assert False, 'I should not have been selected to run' def test___repr__(): pass - """) + """ + ) reprec = testdir.inline_run("-k repr") reprec.assertoutcome(passed=1, failed=0) @@ -750,13 +833,15 @@ def test_exit_on_collection_error(testdir): res = testdir.runpytest() assert res.ret == 2 - res.stdout.fnmatch_lines([ - "collected 2 items / 2 errors", - "*ERROR collecting test_02_import_error.py*", - "*No module named *asdfa*", - "*ERROR collecting test_03_import_error.py*", - "*No module named *asdfa*", - ]) + res.stdout.fnmatch_lines( + [ + "collected 2 items / 2 errors", + "*ERROR collecting test_02_import_error.py*", + "*No module named *asdfa*", + "*ERROR collecting test_03_import_error.py*", + "*No module named *asdfa*", + ] + ) def test_exit_on_collection_with_maxfail_smaller_than_n_errors(testdir): @@ -767,15 +852,13 @@ def test_exit_on_collection_with_maxfail_smaller_than_n_errors(testdir): testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest("--maxfail=1") - assert res.ret == 2 + assert res.ret == 1 - res.stdout.fnmatch_lines([ - "*ERROR collecting test_02_import_error.py*", - "*No module named *asdfa*", - "*Interrupted: stopping after 1 failures*", - ]) + res.stdout.fnmatch_lines( + ["*ERROR collecting test_02_import_error.py*", "*No module named *asdfa*"] + ) - assert 'test_03' not in res.stdout.str() + assert "test_03" not in res.stdout.str() def test_exit_on_collection_with_maxfail_bigger_than_n_errors(testdir): @@ -788,13 +871,15 @@ def test_exit_on_collection_with_maxfail_bigger_than_n_errors(testdir): res = testdir.runpytest("--maxfail=4") assert res.ret == 2 - res.stdout.fnmatch_lines([ - "collected 2 items / 2 errors", - "*ERROR collecting test_02_import_error.py*", - "*No module named *asdfa*", - "*ERROR collecting test_03_import_error.py*", - "*No module named *asdfa*", - ]) + res.stdout.fnmatch_lines( + [ + "collected 2 items / 2 errors", + "*ERROR collecting test_02_import_error.py*", + "*No module named *asdfa*", + "*ERROR collecting test_03_import_error.py*", + "*No module named *asdfa*", + ] + ) def test_continue_on_collection_errors(testdir): @@ -807,10 +892,9 @@ def test_continue_on_collection_errors(testdir): res = testdir.runpytest("--continue-on-collection-errors") assert res.ret == 1 - res.stdout.fnmatch_lines([ - "collected 2 items / 2 errors", - "*1 failed, 1 passed, 2 error*", - ]) + res.stdout.fnmatch_lines( + ["collected 2 items / 2 errors", "*1 failed, 1 passed, 2 error*"] + ) def test_continue_on_collection_errors_maxfail(testdir): @@ -824,24 +908,24 @@ def test_continue_on_collection_errors_maxfail(testdir): testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest("--continue-on-collection-errors", "--maxfail=3") - assert res.ret == 2 + assert res.ret == 1 - res.stdout.fnmatch_lines([ - "collected 2 items / 2 errors", - "*Interrupted: stopping after 3 failures*", - "*1 failed, 2 error*", - ]) + res.stdout.fnmatch_lines(["collected 2 items / 2 errors", "*1 failed, 2 error*"]) def test_fixture_scope_sibling_conftests(testdir): """Regression test case for https://github.com/pytest-dev/pytest/issues/2836""" foo_path = testdir.mkpydir("foo") - foo_path.join("conftest.py").write(_pytest._code.Source(""" + foo_path.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def fix(): return 1 - """)) + """ + ) + ) foo_path.join("test_foo.py").write("def test_foo(fix): assert fix == 1") # Tests in `food/` should not see the conftest fixture from `foo/` @@ -851,8 +935,10 @@ def test_fixture_scope_sibling_conftests(testdir): res = testdir.runpytest() assert res.ret == 1 - res.stdout.fnmatch_lines([ - "*ERROR at setup of test_food*", - "E*fixture 'fix' not found", - "*1 passed, 1 error*", - ]) + res.stdout.fnmatch_lines( + [ + "*ERROR at setup of test_food*", + "E*fixture 'fix' not found", + "*1 passed, 1 error*", + ] + ) diff --git a/third_party/python/pytest/testing/test_compat.py b/third_party/python/pytest/testing/test_compat.py index c74801c6c854..550a8f1b3544 100644 --- a/third_party/python/pytest/testing/test_compat.py +++ b/third_party/python/pytest/testing/test_compat.py @@ -7,6 +7,7 @@ from _pytest.outcomes import OutcomeException def test_is_generator(): + def zap(): yield @@ -20,6 +21,7 @@ def test_is_generator(): def test_real_func_loop_limit(): class Evil(object): + def __init__(self): self.left = 1000 @@ -28,7 +30,7 @@ def test_real_func_loop_limit(): def __getattr__(self, attr): if not self.left: - raise RuntimeError('its over') + raise RuntimeError("its over") self.left -= 1 return self @@ -39,10 +41,12 @@ def test_real_func_loop_limit(): print(res) -@pytest.mark.skipif(sys.version_info < (3, 4), - reason='asyncio available in Python 3.4+') +@pytest.mark.skipif( + sys.version_info < (3, 4), reason="asyncio available in Python 3.4+" +) def test_is_generator_asyncio(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from _pytest.compat import is_generator import asyncio @asyncio.coroutine @@ -51,17 +55,20 @@ def test_is_generator_asyncio(testdir): def test_is_generator_asyncio(): assert not is_generator(baz) - """) + """ + ) # avoid importing asyncio into pytest's own process, # which in turn imports logging (#8) result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(['*1 passed*']) + result.stdout.fnmatch_lines(["*1 passed*"]) -@pytest.mark.skipif(sys.version_info < (3, 5), - reason='async syntax available in Python 3.5+') +@pytest.mark.skipif( + sys.version_info < (3, 5), reason="async syntax available in Python 3.5+" +) def test_is_generator_async_syntax(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from _pytest.compat import is_generator def test_is_generator_py35(): async def foo(): @@ -72,19 +79,21 @@ def test_is_generator_async_syntax(testdir): assert not is_generator(foo) assert not is_generator(bar) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['*1 passed*']) + result.stdout.fnmatch_lines(["*1 passed*"]) class ErrorsHelper(object): + @property def raise_exception(self): - raise Exception('exception should be catched') + raise Exception("exception should be catched") @property def raise_fail(self): - pytest.fail('fail should be catched') + pytest.fail("fail should be catched") def test_helper_failures(): @@ -97,5 +106,5 @@ def test_helper_failures(): def test_safe_getattr(): helper = ErrorsHelper() - assert safe_getattr(helper, 'raise_exception', 'default') == 'default' - assert safe_getattr(helper, 'raise_fail', 'default') == 'default' + assert safe_getattr(helper, "raise_exception", "default") == "default" + assert safe_getattr(helper, "raise_fail", "default") == "default" diff --git a/third_party/python/pytest/testing/test_config.py b/third_party/python/pytest/testing/test_config.py index f21d1821e464..5fb048364f7c 100644 --- a/third_party/python/pytest/testing/test_config.py +++ b/third_party/python/pytest/testing/test_config.py @@ -1,78 +1,105 @@ from __future__ import absolute_import, division, print_function -import py +import sys +import textwrap import pytest import _pytest._code -from _pytest.config import getcfg, get_common_ancestor, determine_setup, _iter_rewritable_modules +from _pytest.config.findpaths import getcfg, get_common_ancestor, determine_setup +from _pytest.config import _iter_rewritable_modules from _pytest.main import EXIT_NOTESTSCOLLECTED class TestParseIni(object): - @pytest.mark.parametrize('section, filename', - [('pytest', 'pytest.ini'), ('tool:pytest', 'setup.cfg')]) + @pytest.mark.parametrize( + "section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")] + ) def test_getcfg_and_config(self, testdir, tmpdir, section, filename): sub = tmpdir.mkdir("sub") sub.chdir() - tmpdir.join(filename).write(_pytest._code.Source(""" + tmpdir.join(filename).write( + _pytest._code.Source( + """ [{section}] name = value - """.format(section=section))) + """.format( + section=section + ) + ) + ) rootdir, inifile, cfg = getcfg([sub]) - assert cfg['name'] == "value" + assert cfg["name"] == "value" config = testdir.parseconfigure(sub) - assert config.inicfg['name'] == 'value' + assert config.inicfg["name"] == "value" def test_getcfg_empty_path(self): """correctly handle zero length arguments (a la pytest '')""" - getcfg(['']) + getcfg([""]) def test_append_parse_args(self, testdir, tmpdir, monkeypatch): - monkeypatch.setenv('PYTEST_ADDOPTS', '--color no -rs --tb="short"') - tmpdir.join("pytest.ini").write(_pytest._code.Source(""" + monkeypatch.setenv("PYTEST_ADDOPTS", '--color no -rs --tb="short"') + tmpdir.join("pytest.ini").write( + _pytest._code.Source( + """ [pytest] addopts = --verbose - """)) + """ + ) + ) config = testdir.parseconfig(tmpdir) - assert config.option.color == 'no' - assert config.option.reportchars == 's' - assert config.option.tbstyle == 'short' + assert config.option.color == "no" + assert config.option.reportchars == "s" + assert config.option.tbstyle == "short" assert config.option.verbose def test_tox_ini_wrong_version(self, testdir): - testdir.makefile('.ini', tox=""" + testdir.makefile( + ".ini", + tox=""" [pytest] minversion=9.0 - """) + """, + ) result = testdir.runpytest() assert result.ret != 0 - result.stderr.fnmatch_lines([ - "*tox.ini:2*requires*9.0*actual*" - ]) + result.stderr.fnmatch_lines(["*tox.ini:2*requires*9.0*actual*"]) - @pytest.mark.parametrize("section, name", [ - ('tool:pytest', 'setup.cfg'), - ('pytest', 'tox.ini'), - ('pytest', 'pytest.ini')], + @pytest.mark.parametrize( + "section, name", + [("tool:pytest", "setup.cfg"), ("pytest", "tox.ini"), ("pytest", "pytest.ini")], ) def test_ini_names(self, testdir, name, section): - testdir.tmpdir.join(name).write(py.std.textwrap.dedent(""" + testdir.tmpdir.join(name).write( + textwrap.dedent( + """ [{section}] minversion = 1.0 - """.format(section=section))) + """.format( + section=section + ) + ) + ) config = testdir.parseconfig() assert config.getini("minversion") == "1.0" def test_toxini_before_lower_pytestini(self, testdir): sub = testdir.tmpdir.mkdir("sub") - sub.join("tox.ini").write(py.std.textwrap.dedent(""" + sub.join("tox.ini").write( + textwrap.dedent( + """ [pytest] minversion = 2.0 - """)) - testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent(""" + """ + ) + ) + testdir.tmpdir.join("pytest.ini").write( + textwrap.dedent( + """ [pytest] minversion = 1.5 - """)) + """ + ) + ) config = testdir.parseconfigure(sub) assert config.getini("minversion") == "2.0" @@ -80,47 +107,72 @@ class TestParseIni(object): def test_confcutdir(self, testdir): sub = testdir.mkdir("sub") sub.chdir() - testdir.makeini(""" + testdir.makeini( + """ [pytest] addopts = --qwe - """) + """ + ) result = testdir.inline_run("--confcutdir=.") assert result.ret == 0 class TestConfigCmdlineParsing(object): + def test_parsing_again_fails(self, testdir): config = testdir.parseconfig() pytest.raises(AssertionError, lambda: config.parse([])) def test_explicitly_specified_config_file_is_loaded(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("custom", "") - """) - testdir.makeini(""" + """ + ) + testdir.makeini( + """ [pytest] custom = 0 - """) - testdir.makefile(".cfg", custom=""" + """ + ) + testdir.makefile( + ".cfg", + custom=""" [pytest] custom = 1 - """) + """, + ) config = testdir.parseconfig("-c", "custom.cfg") assert config.getini("custom") == "1" + testdir.makefile( + ".cfg", + custom_tool_pytest_section=""" + [tool:pytest] + custom = 1 + """, + ) + config = testdir.parseconfig("-c", "custom_tool_pytest_section.cfg") + assert config.getini("custom") == "1" + def test_absolute_win32_path(self, testdir): - temp_cfg_file = testdir.makefile(".cfg", custom=""" + temp_cfg_file = testdir.makefile( + ".cfg", + custom=""" [pytest] addopts = --version - """) + """, + ) from os.path import normpath + temp_cfg_file = normpath(str(temp_cfg_file)) ret = pytest.main("-c " + temp_cfg_file) assert ret == _pytest.main.EXIT_OK class TestConfigAPI(object): + def test_config_trace(self, testdir): config = testdir.parseconfig() values = [] @@ -130,46 +182,51 @@ class TestConfigAPI(object): assert values[0] == "hello [config]\n" def test_config_getoption(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addoption("--hello", "-X", dest="hello") - """) + """ + ) config = testdir.parseconfig("--hello=this") for x in ("hello", "--hello", "-X"): assert config.getoption(x) == "this" pytest.raises(ValueError, "config.getoption('qweqwe')") - @pytest.mark.skipif('sys.version_info[:2] not in [(2, 6), (2, 7)]') + @pytest.mark.skipif("sys.version_info[0] < 3") def test_config_getoption_unicode(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ from __future__ import unicode_literals def pytest_addoption(parser): parser.addoption('--hello', type=str) - """) - config = testdir.parseconfig('--hello=this') - assert config.getoption('hello') == 'this' + """ + ) + config = testdir.parseconfig("--hello=this") + assert config.getoption("hello") == "this" def test_config_getvalueorskip(self, testdir): config = testdir.parseconfig() - pytest.raises(pytest.skip.Exception, - "config.getvalueorskip('hello')") + pytest.raises(pytest.skip.Exception, "config.getvalueorskip('hello')") verbose = config.getvalueorskip("verbose") assert verbose == config.option.verbose def test_config_getvalueorskip_None(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addoption("--hello") - """) + """ + ) config = testdir.parseconfig() with pytest.raises(pytest.skip.Exception): - config.getvalueorskip('hello') + config.getvalueorskip("hello") def test_getoption(self, testdir): config = testdir.parseconfig() with pytest.raises(ValueError): - config.getvalue('x') + config.getvalue("x") assert config.getoption("x", 1) == 1 def test_getconftest_pathlist(self, testdir, tmpdir): @@ -177,54 +234,66 @@ class TestConfigAPI(object): p = tmpdir.join("conftest.py") p.write("pathlist = ['.', %r]" % str(somepath)) config = testdir.parseconfigure(p) - assert config._getconftest_pathlist('notexist', path=tmpdir) is None - pl = config._getconftest_pathlist('pathlist', path=tmpdir) + assert config._getconftest_pathlist("notexist", path=tmpdir) is None + pl = config._getconftest_pathlist("pathlist", path=tmpdir) print(pl) assert len(pl) == 2 assert pl[0] == tmpdir assert pl[1] == somepath def test_addini(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("myname", "my new ini value") - """) - testdir.makeini(""" + """ + ) + testdir.makeini( + """ [pytest] myname=hello - """) + """ + ) config = testdir.parseconfig() val = config.getini("myname") assert val == "hello" - pytest.raises(ValueError, config.getini, 'other') + pytest.raises(ValueError, config.getini, "other") def test_addini_pathlist(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("paths", "my new ini value", type="pathlist") parser.addini("abc", "abc value") - """) - p = testdir.makeini(""" + """ + ) + p = testdir.makeini( + """ [pytest] paths=hello world/sub.py - """) + """ + ) config = testdir.parseconfig() values = config.getini("paths") assert len(values) == 2 - assert values[0] == p.dirpath('hello') - assert values[1] == p.dirpath('world/sub.py') - pytest.raises(ValueError, config.getini, 'other') + assert values[0] == p.dirpath("hello") + assert values[1] == p.dirpath("world/sub.py") + pytest.raises(ValueError, config.getini, "other") def test_addini_args(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("args", "new args", type="args") parser.addini("a2", "", "args", default="1 2 3".split()) - """) - testdir.makeini(""" + """ + ) + testdir.makeini( + """ [pytest] args=123 "123 hello" "this" - """) + """ + ) config = testdir.parseconfig() values = config.getini("args") assert len(values) == 3 @@ -233,16 +302,20 @@ class TestConfigAPI(object): assert values == list("123") def test_addini_linelist(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("xy", "", type="linelist") parser.addini("a2", "", "linelist") - """) - testdir.makeini(""" + """ + ) + testdir.makeini( + """ [pytest] xy= 123 345 second line - """) + """ + ) config = testdir.parseconfig() values = config.getini("xy") assert len(values) == 2 @@ -250,30 +323,40 @@ class TestConfigAPI(object): values = config.getini("a2") assert values == [] - @pytest.mark.parametrize('str_val, bool_val', - [('True', True), ('no', False), ('no-ini', True)]) + @pytest.mark.parametrize( + "str_val, bool_val", [("True", True), ("no", False), ("no-ini", True)] + ) def test_addini_bool(self, testdir, str_val, bool_val): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("strip", "", type="bool", default=True) - """) - if str_val != 'no-ini': - testdir.makeini(""" + """ + ) + if str_val != "no-ini": + testdir.makeini( + """ [pytest] strip=%s - """ % str_val) + """ + % str_val + ) config = testdir.parseconfig() assert config.getini("strip") is bool_val def test_addinivalue_line_existing(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("xy", "", type="linelist") - """) - testdir.makeini(""" + """ + ) + testdir.makeini( + """ [pytest] xy= 123 - """) + """ + ) config = testdir.parseconfig() values = config.getini("xy") assert len(values) == 1 @@ -284,10 +367,12 @@ class TestConfigAPI(object): assert values == ["123", "456"] def test_addinivalue_line_new(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): parser.addini("xy", "", type="linelist") - """) + """ + ) config = testdir.parseconfig() assert not config.getini("xy") config.addinivalue_line("xy", "456") @@ -302,99 +387,124 @@ class TestConfigAPI(object): def test_confcutdir_check_isdir(self, testdir): """Give an error if --confcutdir is not a valid directory (#2078)""" with pytest.raises(pytest.UsageError): - testdir.parseconfig('--confcutdir', testdir.tmpdir.join('file').ensure(file=1)) + testdir.parseconfig( + "--confcutdir", testdir.tmpdir.join("file").ensure(file=1) + ) with pytest.raises(pytest.UsageError): - testdir.parseconfig('--confcutdir', testdir.tmpdir.join('inexistant')) - config = testdir.parseconfig('--confcutdir', testdir.tmpdir.join('dir').ensure(dir=1)) - assert config.getoption('confcutdir') == str(testdir.tmpdir.join('dir')) + testdir.parseconfig("--confcutdir", testdir.tmpdir.join("inexistant")) + config = testdir.parseconfig( + "--confcutdir", testdir.tmpdir.join("dir").ensure(dir=1) + ) + assert config.getoption("confcutdir") == str(testdir.tmpdir.join("dir")) - @pytest.mark.parametrize('names, expected', [ - (['bar.py'], ['bar']), - (['foo', 'bar.py'], []), - (['foo', 'bar.pyc'], []), - (['foo', '__init__.py'], ['foo']), - (['foo', 'bar', '__init__.py'], []), - ]) + @pytest.mark.parametrize( + "names, expected", + [ + (["bar.py"], ["bar"]), + (["foo", "bar.py"], []), + (["foo", "bar.pyc"], []), + (["foo", "__init__.py"], ["foo"]), + (["foo", "bar", "__init__.py"], []), + ], + ) def test_iter_rewritable_modules(self, names, expected): - assert list(_iter_rewritable_modules(['/'.join(names)])) == expected + assert list(_iter_rewritable_modules(["/".join(names)])) == expected class TestConfigFromdictargs(object): + def test_basic_behavior(self): from _pytest.config import Config - option_dict = { - 'verbose': 444, - 'foo': 'bar', - 'capture': 'no', - } - args = ['a', 'b'] + + option_dict = {"verbose": 444, "foo": "bar", "capture": "no"} + args = ["a", "b"] config = Config.fromdictargs(option_dict, args) with pytest.raises(AssertionError): - config.parse(['should refuse to parse again']) + config.parse(["should refuse to parse again"]) assert config.option.verbose == 444 - assert config.option.foo == 'bar' - assert config.option.capture == 'no' + assert config.option.foo == "bar" + assert config.option.capture == "no" assert config.args == args def test_origargs(self): """Show that fromdictargs can handle args in their "orig" format""" from _pytest.config import Config + option_dict = {} - args = ['-vvvv', '-s', 'a', 'b'] + args = ["-vvvv", "-s", "a", "b"] config = Config.fromdictargs(option_dict, args) - assert config.args == ['a', 'b'] + assert config.args == ["a", "b"] assert config._origargs == args assert config.option.verbose == 4 - assert config.option.capture == 'no' + assert config.option.capture == "no" def test_inifilename(self, tmpdir): - tmpdir.join("foo/bar.ini").ensure().write(_pytest._code.Source(""" + tmpdir.join("foo/bar.ini").ensure().write( + _pytest._code.Source( + """ [pytest] name = value - """)) + """ + ) + ) from _pytest.config import Config - inifile = '../../foo/bar.ini' - option_dict = { - 'inifilename': inifile, - 'capture': 'no', - } - cwd = tmpdir.join('a/b') - cwd.join('pytest.ini').ensure().write(_pytest._code.Source(""" + inifile = "../../foo/bar.ini" + option_dict = {"inifilename": inifile, "capture": "no"} + + cwd = tmpdir.join("a/b") + cwd.join("pytest.ini").ensure().write( + _pytest._code.Source( + """ [pytest] name = wrong-value should_not_be_set = true - """)) + """ + ) + ) with cwd.ensure(dir=True).as_cwd(): config = Config.fromdictargs(option_dict, ()) assert config.args == [str(cwd)] assert config.option.inifilename == inifile - assert config.option.capture == 'no' + assert config.option.capture == "no" # this indicates this is the file used for getting configuration values assert config.inifile == inifile - assert config.inicfg.get('name') == 'value' - assert config.inicfg.get('should_not_be_set') is None + assert config.inicfg.get("name") == "value" + assert config.inicfg.get("should_not_be_set") is None def test_options_on_small_file_do_not_blow_up(testdir): + def runfiletest(opts): reprec = testdir.inline_run(*opts) passed, skipped, failed = reprec.countoutcomes() assert failed == 2 assert skipped == passed == 0 - path = testdir.makepyfile(""" + + path = testdir.makepyfile( + """ def test_f1(): assert 0 def test_f2(): assert 0 - """) + """ + ) - for opts in ([], ['-l'], ['-s'], ['--tb=no'], ['--tb=short'], - ['--tb=long'], ['--fulltrace'], - ['--traceconfig'], ['-v'], ['-v', '-v']): + for opts in ( + [], + ["-l"], + ["-s"], + ["--tb=no"], + ["--tb=short"], + ["--tb=long"], + ["--fulltrace"], + ["--traceconfig"], + ["-v"], + ["-v", "-v"], + ): runfiletest(opts + [path]) @@ -405,27 +515,31 @@ def test_preparse_ordering_with_setuptools(testdir, monkeypatch): assert name == "pytest11" class Dist(object): - project_name = 'spam' - version = '1.0' + project_name = "spam" + version = "1.0" def _get_metadata(self, name): - return ['foo.txt,sha256=abc,123'] + return ["foo.txt,sha256=abc,123"] class EntryPoint(object): name = "mytestplugin" dist = Dist() def load(self): + class PseudoPlugin(object): x = 42 + return PseudoPlugin() return iter([EntryPoint()]) - monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter) - testdir.makeconftest(""" + monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter) + testdir.makeconftest( + """ pytest_plugins = "mytestplugin", - """) + """ + ) monkeypatch.setenv("PYTEST_PLUGINS", "mytestplugin") config = testdir.parseconfig() plugin = config.pluginmanager.getplugin("mytestplugin") @@ -439,11 +553,11 @@ def test_setuptools_importerror_issue1479(testdir, monkeypatch): assert name == "pytest11" class Dist(object): - project_name = 'spam' - version = '1.0' + project_name = "spam" + version = "1.0" def _get_metadata(self, name): - return ['foo.txt,sha256=abc,123'] + return ["foo.txt,sha256=abc,123"] class EntryPoint(object): name = "mytestplugin" @@ -454,89 +568,105 @@ def test_setuptools_importerror_issue1479(testdir, monkeypatch): return iter([EntryPoint()]) - monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter) + monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter) with pytest.raises(ImportError): testdir.parseconfig() -def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch): +@pytest.mark.parametrize("block_it", [True, False]) +def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch, block_it): pkg_resources = pytest.importorskip("pkg_resources") + plugin_module_placeholder = object() + def my_iter(name): assert name == "pytest11" class Dist(object): - project_name = 'spam' - version = '1.0' + project_name = "spam" + version = "1.0" def _get_metadata(self, name): - return ['foo.txt,sha256=abc,123'] + return ["foo.txt,sha256=abc,123"] class EntryPoint(object): name = "mytestplugin" dist = Dist() def load(self): - assert 0, "should not arrive here" + return plugin_module_placeholder return iter([EntryPoint()]) - monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter) - config = testdir.parseconfig("-p", "no:mytestplugin") - plugin = config.pluginmanager.getplugin("mytestplugin") - assert plugin is None + monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter) + args = ("-p", "no:mytestplugin") if block_it else () + config = testdir.parseconfig(*args) + config.pluginmanager.import_plugin("mytestplugin") + if block_it: + assert "mytestplugin" not in sys.modules + assert config.pluginmanager.get_plugin("mytestplugin") is None + else: + assert config.pluginmanager.get_plugin( + "mytestplugin" + ) is plugin_module_placeholder def test_cmdline_processargs_simple(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_cmdline_preparse(args): args.append("-h") - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*pytest*", - "*-h*", - ]) + result.stdout.fnmatch_lines(["*pytest*", "*-h*"]) def test_invalid_options_show_extra_information(testdir): """display extra information when pytest exits due to unrecognized options in the command-line""" - testdir.makeini(""" + testdir.makeini( + """ [pytest] addopts = --invalid-option - """) + """ + ) result = testdir.runpytest() - result.stderr.fnmatch_lines([ - "*error: unrecognized arguments: --invalid-option*", - "* inifile: %s*" % testdir.tmpdir.join('tox.ini'), - "* rootdir: %s*" % testdir.tmpdir, - ]) + result.stderr.fnmatch_lines( + [ + "*error: unrecognized arguments: --invalid-option*", + "* inifile: %s*" % testdir.tmpdir.join("tox.ini"), + "* rootdir: %s*" % testdir.tmpdir, + ] + ) -@pytest.mark.parametrize('args', [ - ['dir1', 'dir2', '-v'], - ['dir1', '-v', 'dir2'], - ['dir2', '-v', 'dir1'], - ['-v', 'dir2', 'dir1'], -]) +@pytest.mark.parametrize( + "args", + [ + ["dir1", "dir2", "-v"], + ["dir1", "-v", "dir2"], + ["dir2", "-v", "dir1"], + ["-v", "dir2", "dir1"], + ], +) def test_consider_args_after_options_for_rootdir_and_inifile(testdir, args): """ Consider all arguments in the command-line for rootdir and inifile discovery, even if they happen to occur after an option. #949 """ # replace "dir1" and "dir2" from "args" into their real directory - root = testdir.tmpdir.mkdir('myroot') - d1 = root.mkdir('dir1') - d2 = root.mkdir('dir2') + root = testdir.tmpdir.mkdir("myroot") + d1 = root.mkdir("dir1") + d2 = root.mkdir("dir2") for i, arg in enumerate(args): - if arg == 'dir1': + if arg == "dir1": args[i] = d1 - elif arg == 'dir2': + elif arg == "dir2": args[i] = d2 with root.as_cwd(): result = testdir.runpytest(*args) - result.stdout.fnmatch_lines(['*rootdir: *myroot, inifile:']) + result.stdout.fnmatch_lines(["*rootdir: *myroot, inifile:"]) @pytest.mark.skipif("sys.platform == 'win32'") @@ -546,25 +676,28 @@ def test_toolongargs_issue224(testdir): def test_config_in_subdirectory_colon_command_line_issue2148(testdir): - conftest_source = ''' + conftest_source = """ def pytest_addoption(parser): parser.addini('foo', 'foo') - ''' + """ - testdir.makefile('.ini', **{ - 'pytest': '[pytest]\nfoo = root', - 'subdir/pytest': '[pytest]\nfoo = subdir', - }) + testdir.makefile( + ".ini", + **{"pytest": "[pytest]\nfoo = root", "subdir/pytest": "[pytest]\nfoo = subdir"} + ) - testdir.makepyfile(**{ - 'conftest': conftest_source, - 'subdir/conftest': conftest_source, - 'subdir/test_foo': ''' + testdir.makepyfile( + **{ + "conftest": conftest_source, + "subdir/conftest": conftest_source, + "subdir/test_foo": """ def test_foo(pytestconfig): assert pytestconfig.getini('foo') == 'subdir' - '''}) + """, + } + ) - result = testdir.runpytest('subdir/test_foo.py::test_foo') + result = testdir.runpytest("subdir/test_foo.py::test_foo") assert result.ret == 0 @@ -576,6 +709,7 @@ def test_notify_exception(testdir, capfd): assert "ValueError" in err class A(object): + def pytest_internalerror(self, excrepr): return True @@ -587,9 +721,11 @@ def test_notify_exception(testdir, capfd): def test_load_initial_conftest_last_ordering(testdir): from _pytest.config import get_config + pm = get_config().pluginmanager class My(object): + def pytest_load_initial_conftests(self): pass @@ -597,49 +733,52 @@ def test_load_initial_conftest_last_ordering(testdir): pm.register(m) hc = pm.hook.pytest_load_initial_conftests values = hc._nonwrappers + hc._wrappers - expected = [ - "_pytest.config", - 'test_config', - '_pytest.capture', - ] + expected = ["_pytest.config", "test_config", "_pytest.capture"] assert [x.function.__module__ for x in values] == expected def test_get_plugin_specs_as_list(): from _pytest.config import _get_plugin_specs_as_list + with pytest.raises(pytest.UsageError): - _get_plugin_specs_as_list(set(['foo'])) + _get_plugin_specs_as_list({"foo"}) with pytest.raises(pytest.UsageError): _get_plugin_specs_as_list(dict()) assert _get_plugin_specs_as_list(None) == [] - assert _get_plugin_specs_as_list('') == [] - assert _get_plugin_specs_as_list('foo') == ['foo'] - assert _get_plugin_specs_as_list('foo,bar') == ['foo', 'bar'] - assert _get_plugin_specs_as_list(['foo', 'bar']) == ['foo', 'bar'] - assert _get_plugin_specs_as_list(('foo', 'bar')) == ['foo', 'bar'] + assert _get_plugin_specs_as_list("") == [] + assert _get_plugin_specs_as_list("foo") == ["foo"] + assert _get_plugin_specs_as_list("foo,bar") == ["foo", "bar"] + assert _get_plugin_specs_as_list(["foo", "bar"]) == ["foo", "bar"] + assert _get_plugin_specs_as_list(("foo", "bar")) == ["foo", "bar"] class TestWarning(object): + def test_warn_config(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ values = [] def pytest_configure(config): config.warn("C1", "hello") def pytest_logwarning(code, message): if message == "hello" and code == "C1": values.append(1) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_proper(pytestconfig): import conftest assert conftest.values == [1] - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_warn_on_test_item_from_request(self, testdir, request): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -648,20 +787,24 @@ class TestWarning(object): def test_hello(fix): pass - """) + """ + ) result = testdir.runpytest("--disable-pytest-warnings") assert result.parseoutcomes()["warnings"] > 0 assert "hello" not in result.stdout.str() result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ ===*warnings summary*=== *test_warn_on_test_item_from_request.py::test_hello* *hello* - """) + """ + ) class TestRootdir(object): + def test_simple_noini(self, tmpdir): assert get_common_ancestor([tmpdir]) == tmpdir a = tmpdir.mkdir("a") @@ -669,9 +812,9 @@ class TestRootdir(object): assert get_common_ancestor([tmpdir, a]) == tmpdir with tmpdir.as_cwd(): assert get_common_ancestor([]) == tmpdir - no_path = tmpdir.join('does-not-exist') + no_path = tmpdir.join("does-not-exist") assert get_common_ancestor([no_path]) == tmpdir - assert get_common_ancestor([no_path.join('a')]) == tmpdir + assert get_common_ancestor([no_path.join("a")]) == tmpdir @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) def test_with_ini(self, tmpdir, name): @@ -720,96 +863,136 @@ class TestRootdir(object): class TestOverrideIniArgs(object): + @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) def test_override_ini_names(self, testdir, name): - testdir.tmpdir.join(name).write(py.std.textwrap.dedent(""" + testdir.tmpdir.join(name).write( + textwrap.dedent( + """ [pytest] - custom = 1.0""")) - testdir.makeconftest(""" + custom = 1.0""" + ) + ) + testdir.makeconftest( + """ def pytest_addoption(parser): - parser.addini("custom", "")""") - testdir.makepyfile(""" + parser.addini("custom", "")""" + ) + testdir.makepyfile( + """ def test_pass(pytestconfig): ini_val = pytestconfig.getini("custom") - print('\\ncustom_option:%s\\n' % ini_val)""") + print('\\ncustom_option:%s\\n' % ini_val)""" + ) result = testdir.runpytest("--override-ini", "custom=2.0", "-s") assert result.ret == 0 result.stdout.fnmatch_lines(["custom_option:2.0"]) - result = testdir.runpytest("--override-ini", "custom=2.0", - "--override-ini=custom=3.0", "-s") + result = testdir.runpytest( + "--override-ini", "custom=2.0", "--override-ini=custom=3.0", "-s" + ) assert result.ret == 0 result.stdout.fnmatch_lines(["custom_option:3.0"]) def test_override_ini_pathlist(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): - parser.addini("paths", "my new ini value", type="pathlist")""") - testdir.makeini(""" + parser.addini("paths", "my new ini value", type="pathlist")""" + ) + testdir.makeini( + """ [pytest] - paths=blah.py""") - testdir.makepyfile(""" + paths=blah.py""" + ) + testdir.makepyfile( + """ import py.path def test_pathlist(pytestconfig): config_paths = pytestconfig.getini("paths") print(config_paths) for cpf in config_paths: - print('\\nuser_path:%s' % cpf.basename)""") - result = testdir.runpytest("--override-ini", - 'paths=foo/bar1.py foo/bar2.py', "-s") - result.stdout.fnmatch_lines(["user_path:bar1.py", - "user_path:bar2.py"]) + print('\\nuser_path:%s' % cpf.basename)""" + ) + result = testdir.runpytest( + "--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s" + ) + result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"]) def test_override_multiple_and_default(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_addoption(parser): addini = parser.addini addini("custom_option_1", "", default="o1") addini("custom_option_2", "", default="o2") addini("custom_option_3", "", default=False, type="bool") - addini("custom_option_4", "", default=True, type="bool")""") - testdir.makeini(""" + addini("custom_option_4", "", default=True, type="bool")""" + ) + testdir.makeini( + """ [pytest] custom_option_1=custom_option_1 - custom_option_2=custom_option_2""") - testdir.makepyfile(""" + custom_option_2=custom_option_2 + """ + ) + testdir.makepyfile( + """ def test_multiple_options(pytestconfig): prefix = "custom_option" for x in range(1, 5): ini_value=pytestconfig.getini("%s_%d" % (prefix, x)) - print('\\nini%d:%s' % (x, ini_value))""") + print('\\nini%d:%s' % (x, ini_value)) + """ + ) result = testdir.runpytest( - "--override-ini", 'custom_option_1=fulldir=/tmp/user1', - 'custom_option_2=url=/tmp/user2?a=b&d=e', - "-o", 'custom_option_3=True', - "-o", 'custom_option_4=no', "-s") - result.stdout.fnmatch_lines(["ini1:fulldir=/tmp/user1", - "ini2:url=/tmp/user2?a=b&d=e", - "ini3:True", - "ini4:False"]) + "--override-ini", + "custom_option_1=fulldir=/tmp/user1", + "-o", + "custom_option_2=url=/tmp/user2?a=b&d=e", + "-o", + "custom_option_3=True", + "-o", + "custom_option_4=no", + "-s", + ) + result.stdout.fnmatch_lines( + [ + "ini1:fulldir=/tmp/user1", + "ini2:url=/tmp/user2?a=b&d=e", + "ini3:True", + "ini4:False", + ] + ) def test_override_ini_usage_error_bad_style(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] xdist_strict=False - """) - result = testdir.runpytest("--override-ini", 'xdist_strict True', "-s") + """ + ) + result = testdir.runpytest("--override-ini", "xdist_strict True", "-s") result.stderr.fnmatch_lines(["*ERROR* *expects option=value*"]) - @pytest.mark.parametrize('with_ini', [True, False]) + @pytest.mark.parametrize("with_ini", [True, False]) def test_override_ini_handled_asap(self, testdir, with_ini): """-o should be handled as soon as possible and always override what's in ini files (#2238)""" if with_ini: - testdir.makeini(""" + testdir.makeini( + """ [pytest] python_files=test_*.py - """) - testdir.makepyfile(unittest_ini_handle=""" + """ + ) + testdir.makepyfile( + unittest_ini_handle=""" def test(): pass - """) - result = testdir.runpytest("--override-ini", 'python_files=unittest_*.py') + """ + ) + result = testdir.runpytest("--override-ini", "python_files=unittest_*.py") result.stdout.fnmatch_lines(["*1 passed in*"]) def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch): @@ -828,8 +1011,7 @@ class TestOverrideIniArgs(object): assert rootdir == a assert inifile == parsed_inifile - @pytest.mark.parametrize('dirs', ([], ['does-not-exist'], - ['a/does-not-exist'])) + @pytest.mark.parametrize("dirs", ([], ["does-not-exist"], ["a/does-not-exist"])) def test_with_non_dir_arg(self, dirs, tmpdir): with tmpdir.ensure(dir=True).as_cwd(): rootdir, inifile, inicfg = determine_setup(None, dirs) @@ -840,6 +1022,47 @@ class TestOverrideIniArgs(object): a = tmpdir.mkdir("a") a.ensure("exist") with tmpdir.as_cwd(): - rootdir, inifile, inicfg = determine_setup(None, ['a/exist']) + rootdir, inifile, inicfg = determine_setup(None, ["a/exist"]) assert rootdir == tmpdir assert inifile is None + + def test_addopts_before_initini(self, monkeypatch): + cache_dir = ".custom_cache" + monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir) + from _pytest.config import get_config + + config = get_config() + config._preparse([], addopts=True) + assert config._override_ini == ["cache_dir=%s" % cache_dir] + + def test_override_ini_does_not_contain_paths(self): + """Check that -o no longer swallows all options after it (#3103)""" + from _pytest.config import get_config + + config = get_config() + config._preparse(["-o", "cache_dir=/cache", "/some/test/path"]) + assert config._override_ini == ["cache_dir=/cache"] + + def test_multiple_override_ini_options(self, testdir, request): + """Ensure a file path following a '-o' option does not generate an error (#3103)""" + testdir.makepyfile( + **{ + "conftest.py": """ + def pytest_addoption(parser): + parser.addini('foo', default=None, help='some option') + parser.addini('bar', default=None, help='some option') + """, + "test_foo.py": """ + def test(pytestconfig): + assert pytestconfig.getini('foo') == '1' + assert pytestconfig.getini('bar') == '0' + """, + "test_bar.py": """ + def test(): + assert False + """, + } + ) + result = testdir.runpytest("-o", "foo=1", "-o", "bar=0", "test_foo.py") + assert "ERROR:" not in result.stderr.str() + result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="]) diff --git a/third_party/python/pytest/testing/test_conftest.py b/third_party/python/pytest/testing/test_conftest.py index c0411b723211..61b640976f11 100644 --- a/third_party/python/pytest/testing/test_conftest.py +++ b/third_party/python/pytest/testing/test_conftest.py @@ -11,6 +11,7 @@ from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_USAGEERROR @pytest.fixture(scope="module", params=["global", "inpackage"]) def basedir(request, tmpdir_factory): from _pytest.tmpdir import tmpdir + tmpdir = tmpdir(request, tmpdir_factory) tmpdir.ensure("adir/conftest.py").write("a=1 ; Directory = 3") tmpdir.ensure("adir/b/conftest.py").write("b=2 ; a = 1.5") @@ -27,15 +28,19 @@ def ConftestWithSetinitial(path): def conftest_setinitial(conftest, args, confcutdir=None): + class Namespace(object): + def __init__(self): self.file_or_dir = args self.confcutdir = str(confcutdir) self.noconftest = False + conftest._set_initial_conftests(Namespace()) class TestConftestValueAccessGlobal(object): + def test_basic_init(self, basedir): conftest = PytestPluginManager() p = basedir.join("adir") @@ -47,15 +52,15 @@ class TestConftestValueAccessGlobal(object): conftest._getconftestmodules(basedir) snap1 = len(conftest._path2confmods) # assert len(conftest._path2confmods) == snap1 + 1 - conftest._getconftestmodules(basedir.join('adir')) + conftest._getconftestmodules(basedir.join("adir")) assert len(conftest._path2confmods) == snap1 + 1 - conftest._getconftestmodules(basedir.join('b')) + conftest._getconftestmodules(basedir.join("b")) assert len(conftest._path2confmods) == snap1 + 2 def test_value_access_not_existing(self, basedir): conftest = ConftestWithSetinitial(basedir) with pytest.raises(KeyError): - conftest._rget_with_confmod('a', basedir) + conftest._rget_with_confmod("a", basedir) def test_value_access_by_path(self, basedir): conftest = ConftestWithSetinitial(basedir) @@ -84,7 +89,7 @@ def test_conftest_in_nonpkg_with_init(tmpdir): def test_doubledash_considered(testdir): conf = testdir.mkdir("--option") - conf.join("conftest.py").ensure() + conf.ensure("conftest.py") conftest = PytestPluginManager() conftest_setinitial(conftest, [conf.basename, conf.basename]) values = conftest._getconftestmodules(conf) @@ -105,7 +110,8 @@ def test_issue151_load_all_conftests(testdir): def test_conftest_global_import(testdir): testdir.makeconftest("x=3") - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import py, pytest from _pytest.config import PytestPluginManager conf = PytestPluginManager() @@ -120,7 +126,8 @@ def test_conftest_global_import(testdir): assert mod2.y == 4 import conftest assert conftest is mod2, (conftest, mod) - """) + """ + ) res = testdir.runpython(p) assert res.ret == 0 @@ -154,13 +161,13 @@ def test_conftestcutdir_inplace_considered(testdir): assert values[0].__file__.startswith(str(conf)) -@pytest.mark.parametrize("name", 'test tests whatever .dotdir'.split()) +@pytest.mark.parametrize("name", "test tests whatever .dotdir".split()) def test_setinitial_conftest_subdirs(testdir, name): sub = testdir.mkdir(name) subconftest = sub.ensure("conftest.py") conftest = PytestPluginManager() conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir) - if name not in ('whatever', '.dotdir'): + if name not in ("whatever", ".dotdir"): assert subconftest in conftest._conftestpath2mod assert len(conftest._conftestpath2mod) == 1 else: @@ -171,13 +178,17 @@ def test_setinitial_conftest_subdirs(testdir, name): def test_conftest_confcutdir(testdir): testdir.makeconftest("assert 0") x = testdir.mkdir("x") - x.join("conftest.py").write(_pytest._code.Source(""" + x.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true") - """)) + """ + ) + ) result = testdir.runpytest("-h", "--confcutdir=%s" % x, x) result.stdout.fnmatch_lines(["*--xyz*"]) - assert 'warning: could not load initial' not in result.stdout.str() + assert "warning: could not load initial" not in result.stdout.str() def test_no_conftest(testdir): @@ -191,10 +202,14 @@ def test_no_conftest(testdir): def test_conftest_existing_resultlog(testdir): x = testdir.mkdir("tests") - x.join("conftest.py").write(_pytest._code.Source(""" + x.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true") - """)) + """ + ) + ) testdir.makefile(ext=".log", result="") # Writes result.log result = testdir.runpytest("-h", "--resultlog", "result.log") result.stdout.fnmatch_lines(["*--xyz*"]) @@ -202,10 +217,14 @@ def test_conftest_existing_resultlog(testdir): def test_conftest_existing_junitxml(testdir): x = testdir.mkdir("tests") - x.join("conftest.py").write(_pytest._code.Source(""" + x.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true") - """)) + """ + ) + ) testdir.makefile(ext=".xml", junit="") # Writes junit.xml result = testdir.runpytest("-h", "--junitxml", "junit.xml") result.stdout.fnmatch_lines(["*--xyz*"]) @@ -222,7 +241,7 @@ def test_conftest_import_order(testdir, monkeypatch): conftest = PytestPluginManager() conftest._confcutdir = testdir.tmpdir - monkeypatch.setattr(conftest, '_importconftest', impct) + monkeypatch.setattr(conftest, "_importconftest", impct) assert conftest._getconftestmodules(sub) == [ct1, ct2] @@ -232,7 +251,9 @@ def test_fixture_dependency(testdir, monkeypatch): ct1.write("") sub = testdir.mkdir("sub") sub.join("__init__.py").write("") - sub.join("conftest.py").write(py.std.textwrap.dedent(""" + sub.join("conftest.py").write( + dedent( + """ import pytest @pytest.fixture @@ -246,10 +267,14 @@ def test_fixture_dependency(testdir, monkeypatch): @pytest.fixture def bar(foo): return 'bar' - """)) + """ + ) + ) subsub = sub.mkdir("subsub") subsub.join("__init__.py").write("") - subsub.join("test_bar.py").write(py.std.textwrap.dedent(""" + subsub.join("test_bar.py").write( + dedent( + """ import pytest @pytest.fixture @@ -258,112 +283,133 @@ def test_fixture_dependency(testdir, monkeypatch): def test_event_fixture(bar): assert bar == 'sub bar' - """)) + """ + ) + ) result = testdir.runpytest("sub") result.stdout.fnmatch_lines(["*1 passed*"]) def test_conftest_found_with_double_dash(testdir): sub = testdir.mkdir("sub") - sub.join("conftest.py").write(py.std.textwrap.dedent(""" + sub.join("conftest.py").write( + dedent( + """ def pytest_addoption(parser): parser.addoption("--hello-world", action="store_true") - """)) + """ + ) + ) p = sub.join("test_hello.py") - p.write(py.std.textwrap.dedent(""" - import pytest - def test_hello(found): - assert found == 1 - """)) + p.write("def test_hello(): pass") result = testdir.runpytest(str(p) + "::test_hello", "-h") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *--hello-world* - """) + """ + ) class TestConftestVisibility(object): + def _setup_tree(self, testdir): # for issue616 # example mostly taken from: # https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html runner = testdir.mkdir("empty") package = testdir.mkdir("package") - package.join("conftest.py").write(dedent("""\ + package.join("conftest.py").write( + dedent( + """\ import pytest @pytest.fixture def fxtr(): return "from-package" - """)) - package.join("test_pkgroot.py").write(dedent("""\ + """ + ) + ) + package.join("test_pkgroot.py").write( + dedent( + """\ def test_pkgroot(fxtr): assert fxtr == "from-package" - """)) + """ + ) + ) swc = package.mkdir("swc") swc.join("__init__.py").ensure() - swc.join("conftest.py").write(dedent("""\ + swc.join("conftest.py").write( + dedent( + """\ import pytest @pytest.fixture def fxtr(): return "from-swc" - """)) - swc.join("test_with_conftest.py").write(dedent("""\ + """ + ) + ) + swc.join("test_with_conftest.py").write( + dedent( + """\ def test_with_conftest(fxtr): assert fxtr == "from-swc" - """)) + """ + ) + ) snc = package.mkdir("snc") snc.join("__init__.py").ensure() - snc.join("test_no_conftest.py").write(dedent("""\ + snc.join("test_no_conftest.py").write( + dedent( + """\ def test_no_conftest(fxtr): assert fxtr == "from-package" # No local conftest.py, so should # use value from parent dir's - """)) + """ + ) + ) print("created directory structure:") for x in testdir.tmpdir.visit(): print(" " + x.relto(testdir.tmpdir)) - return { - "runner": runner, - "package": package, - "swc": swc, - "snc": snc} + return {"runner": runner, "package": package, "swc": swc, "snc": snc} # N.B.: "swc" stands for "subdir with conftest.py" # "snc" stands for "subdir no [i.e. without] conftest.py" - @pytest.mark.parametrize("chdir,testarg,expect_ntests_passed", [ - # Effective target: package/.. - ("runner", "..", 3), - ("package", "..", 3), - ("swc", "../..", 3), - ("snc", "../..", 3), - - # Effective target: package - ("runner", "../package", 3), - ("package", ".", 3), - ("swc", "..", 3), - ("snc", "..", 3), - - # Effective target: package/swc - ("runner", "../package/swc", 1), - ("package", "./swc", 1), - ("swc", ".", 1), - ("snc", "../swc", 1), - - # Effective target: package/snc - ("runner", "../package/snc", 1), - ("package", "./snc", 1), - ("swc", "../snc", 1), - ("snc", ".", 1), - ]) + @pytest.mark.parametrize( + "chdir,testarg,expect_ntests_passed", + [ + # Effective target: package/.. + ("runner", "..", 3), + ("package", "..", 3), + ("swc", "../..", 3), + ("snc", "../..", 3), + # Effective target: package + ("runner", "../package", 3), + ("package", ".", 3), + ("swc", "..", 3), + ("snc", "..", 3), + # Effective target: package/swc + ("runner", "../package/swc", 1), + ("package", "./swc", 1), + ("swc", ".", 1), + ("snc", "../swc", 1), + # Effective target: package/snc + ("runner", "../package/snc", 1), + ("package", "./snc", 1), + ("swc", "../snc", 1), + ("snc", ".", 1), + ], + ) @pytest.mark.issue616 def test_parsefactories_relative_node_ids( - self, testdir, chdir, testarg, expect_ntests_passed): + self, testdir, chdir, testarg, expect_ntests_passed + ): dirs = self._setup_tree(testdir) - print("pytest run in cwd: %s" % ( - dirs[chdir].relto(testdir.tmpdir))) + print("pytest run in cwd: %s" % (dirs[chdir].relto(testdir.tmpdir))) print("pytestarg : %s" % (testarg)) print("expected pass : %s" % (expect_ntests_passed)) with dirs[chdir].as_cwd(): @@ -371,108 +417,127 @@ class TestConftestVisibility(object): reprec.assertoutcome(passed=expect_ntests_passed) -@pytest.mark.parametrize('confcutdir,passed,error', [ - ('.', 2, 0), - ('src', 1, 1), - (None, 1, 1), -]) +@pytest.mark.parametrize( + "confcutdir,passed,error", [(".", 2, 0), ("src", 1, 1), (None, 1, 1)] +) def test_search_conftest_up_to_inifile(testdir, confcutdir, passed, error): - """Test that conftest files are detected only up to a ini file, unless + """Test that conftest files are detected only up to an ini file, unless an explicit --confcutdir option is given. """ root = testdir.tmpdir - src = root.join('src').ensure(dir=1) - src.join('pytest.ini').write('[pytest]') - src.join('conftest.py').write(_pytest._code.Source(""" + src = root.join("src").ensure(dir=1) + src.join("pytest.ini").write("[pytest]") + src.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def fix1(): pass - """)) - src.join('test_foo.py').write(_pytest._code.Source(""" + """ + ) + ) + src.join("test_foo.py").write( + _pytest._code.Source( + """ def test_1(fix1): pass def test_2(out_of_reach): pass - """)) - root.join('conftest.py').write(_pytest._code.Source(""" + """ + ) + ) + root.join("conftest.py").write( + _pytest._code.Source( + """ import pytest @pytest.fixture def out_of_reach(): pass - """)) + """ + ) + ) args = [str(src)] if confcutdir: - args = ['--confcutdir=%s' % root.join(confcutdir)] + args = ["--confcutdir=%s" % root.join(confcutdir)] result = testdir.runpytest(*args) - match = '' + match = "" if passed: - match += '*%d passed*' % passed + match += "*%d passed*" % passed if error: - match += '*%d error*' % error + match += "*%d error*" % error result.stdout.fnmatch_lines(match) def test_issue1073_conftest_special_objects(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ class DontTouchMe(object): def __getattr__(self, x): raise Exception('cant touch me') x = DontTouchMe() - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_some(): pass - """) + """ + ) res = testdir.runpytest() assert res.ret == 0 def test_conftest_exception_handling(testdir): - testdir.makeconftest(''' + testdir.makeconftest( + """ raise ValueError() - ''') - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_some(): pass - """) + """ + ) res = testdir.runpytest() assert res.ret == 4 - assert 'raise ValueError()' in [line.strip() for line in res.errlines] + assert "raise ValueError()" in [line.strip() for line in res.errlines] def test_hook_proxy(testdir): """Session's gethookproxy() would cache conftests incorrectly (#2016). It was decided to remove the cache altogether. """ - testdir.makepyfile(**{ - 'root/demo-0/test_foo1.py': "def test1(): pass", - - 'root/demo-a/test_foo2.py': "def test1(): pass", - 'root/demo-a/conftest.py': """ + testdir.makepyfile( + **{ + "root/demo-0/test_foo1.py": "def test1(): pass", + "root/demo-a/test_foo2.py": "def test1(): pass", + "root/demo-a/conftest.py": """ def pytest_ignore_collect(path, config): return True """, - - 'root/demo-b/test_foo3.py': "def test1(): pass", - 'root/demo-c/test_foo4.py': "def test1(): pass", - }) + "root/demo-b/test_foo3.py": "def test1(): pass", + "root/demo-c/test_foo4.py": "def test1(): pass", + } + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*test_foo1.py*', - '*test_foo3.py*', - '*test_foo4.py*', - '*3 passed*', - ]) + result.stdout.fnmatch_lines( + ["*test_foo1.py*", "*test_foo3.py*", "*test_foo4.py*", "*3 passed*"] + ) def test_required_option_help(testdir): testdir.makeconftest("assert 0") x = testdir.mkdir("x") - x.join("conftest.py").write(_pytest._code.Source(""" + x.join("conftest.py").write( + _pytest._code.Source( + """ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true", required=True) - """)) + """ + ) + ) result = testdir.runpytest("-h", x) - assert 'argument --xyz is required' not in result.stdout.str() - assert 'general:' in result.stdout.str() + assert "argument --xyz is required" not in result.stdout.str() + assert "general:" in result.stdout.str() diff --git a/third_party/python/pytest/testing/test_doctest.py b/third_party/python/pytest/testing/test_doctest.py index 6616d2eaebfc..7f3aff3b0e10 100644 --- a/third_party/python/pytest/testing/test_doctest.py +++ b/third_party/python/pytest/testing/test_doctest.py @@ -11,12 +11,14 @@ class TestDoctests(object): def test_collect_testtextfile(self, testdir): w = testdir.maketxtfile(whatever="") - checkfile = testdir.maketxtfile(test_something=""" + checkfile = testdir.maketxtfile( + test_something=""" alskdjalsdk >>> i = 5 >>> i-1 4 - """) + """ + ) for x in (testdir.tmpdir, checkfile): # print "checking that %s returns custom items" % (x,) @@ -31,28 +33,27 @@ class TestDoctests(object): def test_collect_module_empty(self, testdir): path = testdir.makepyfile(whatever="#") for p in (path, testdir.tmpdir): - items, reprec = testdir.inline_genitems(p, - '--doctest-modules') + items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 0 def test_collect_module_single_modulelevel_doctest(self, testdir): path = testdir.makepyfile(whatever='""">>> pass"""') for p in (path, testdir.tmpdir): - items, reprec = testdir.inline_genitems(p, - '--doctest-modules') + items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 1 assert isinstance(items[0], DoctestItem) assert isinstance(items[0].parent, DoctestModule) def test_collect_module_two_doctest_one_modulelevel(self, testdir): - path = testdir.makepyfile(whatever=""" + path = testdir.makepyfile( + whatever=""" '>>> x = None' def my_func(): ">>> magic = 42 " - """) + """ + ) for p in (path, testdir.tmpdir): - items, reprec = testdir.inline_genitems(p, - '--doctest-modules') + items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 2 assert isinstance(items[0], DoctestItem) assert isinstance(items[1], DoctestItem) @@ -60,7 +61,8 @@ class TestDoctests(object): assert items[0].parent is items[1].parent def test_collect_module_two_doctest_no_modulelevel(self, testdir): - path = testdir.makepyfile(whatever=""" + path = testdir.makepyfile( + whatever=""" '# Empty' def my_func(): ">>> magic = 42 " @@ -74,10 +76,10 @@ class TestDoctests(object): # This is another function >>> import os # this one does have a doctest ''' - """) + """ + ) for p in (path, testdir.tmpdir): - items, reprec = testdir.inline_genitems(p, - '--doctest-modules') + items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 2 assert isinstance(items[0], DoctestItem) assert isinstance(items[1], DoctestItem) @@ -85,99 +87,108 @@ class TestDoctests(object): assert items[0].parent is items[1].parent def test_simple_doctestfile(self, testdir): - p = testdir.maketxtfile(test_doc=""" + p = testdir.maketxtfile( + test_doc=""" >>> x = 1 >>> x == 1 False - """) - reprec = testdir.inline_run(p, ) + """ + ) + reprec = testdir.inline_run(p) reprec.assertoutcome(failed=1) def test_new_pattern(self, testdir): - p = testdir.maketxtfile(xdoc=""" + p = testdir.maketxtfile( + xdoc=""" >>> x = 1 >>> x == 1 False - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1) def test_multiple_patterns(self, testdir): """Test support for multiple --doctest-glob arguments (#1255). """ - testdir.maketxtfile(xdoc=""" + testdir.maketxtfile( + xdoc=""" >>> 1 1 - """) - testdir.makefile('.foo', test=""" + """ + ) + testdir.makefile( + ".foo", + test=""" >>> 1 1 - """) - testdir.maketxtfile(test_normal=""" + """, + ) + testdir.maketxtfile( + test_normal=""" >>> 1 1 - """) - expected = set(['xdoc.txt', 'test.foo', 'test_normal.txt']) - assert set(x.basename for x in testdir.tmpdir.listdir()) == expected + """ + ) + expected = {"xdoc.txt", "test.foo", "test_normal.txt"} + assert {x.basename for x in testdir.tmpdir.listdir()} == expected args = ["--doctest-glob=xdoc*.txt", "--doctest-glob=*.foo"] result = testdir.runpytest(*args) - result.stdout.fnmatch_lines([ - '*test.foo *', - '*xdoc.txt *', - '*2 passed*', - ]) + result.stdout.fnmatch_lines(["*test.foo *", "*xdoc.txt *", "*2 passed*"]) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*test_normal.txt *', - '*1 passed*', - ]) + result.stdout.fnmatch_lines(["*test_normal.txt *", "*1 passed*"]) @pytest.mark.parametrize( - ' test_string, encoding', - [ - (u'foo', 'ascii'), - (u'öäü', 'latin1'), - (u'öäü', 'utf-8') - ] + " test_string, encoding", + [(u"foo", "ascii"), (u"öäü", "latin1"), (u"öäü", "utf-8")], ) def test_encoding(self, testdir, test_string, encoding): """Test support for doctest_encoding ini option. """ - testdir.makeini(""" + testdir.makeini( + """ [pytest] - doctest_encoding={0} - """.format(encoding)) + doctest_encoding={} + """.format( + encoding + ) + ) doctest = u""" - >>> u"{0}" - {1} - """.format(test_string, repr(test_string)) + >>> u"{}" + {} + """.format( + test_string, repr(test_string) + ) testdir._makefile(".txt", [doctest], {}, encoding=encoding) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*1 passed*', - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_doctest_unexpected_exception(self, testdir): - testdir.maketxtfile(""" + testdir.maketxtfile( + """ >>> i = 0 >>> 0 / i 2 - """) + """ + ) result = testdir.runpytest("--doctest-modules") - result.stdout.fnmatch_lines([ - "*unexpected_exception*", - "*>>> i = 0*", - "*>>> 0 / i*", - "*UNEXPECTED*ZeroDivision*", - ]) + result.stdout.fnmatch_lines( + [ + "*unexpected_exception*", + "*>>> i = 0*", + "*>>> 0 / i*", + "*UNEXPECTED*ZeroDivision*", + ] + ) def test_docstring_partial_context_around_error(self, testdir): """Test that we show some context before the actual line of a failing doctest. """ - testdir.makepyfile(''' + testdir.makepyfile( + ''' def foo(): """ text-line-1 @@ -196,28 +207,32 @@ class TestDoctests(object): text-line-after """ - ''') - result = testdir.runpytest('--doctest-modules') - result.stdout.fnmatch_lines([ - '*docstring_partial_context_around_error*', - '005*text-line-3', - '006*text-line-4', - '013*text-line-11', - '014*>>> 1 + 1', - 'Expected:', - ' 3', - 'Got:', - ' 2', - ]) + ''' + ) + result = testdir.runpytest("--doctest-modules") + result.stdout.fnmatch_lines( + [ + "*docstring_partial_context_around_error*", + "005*text-line-3", + "006*text-line-4", + "013*text-line-11", + "014*>>> 1 + 1", + "Expected:", + " 3", + "Got:", + " 2", + ] + ) # lines below should be trimmed out - assert 'text-line-2' not in result.stdout.str() - assert 'text-line-after' not in result.stdout.str() + assert "text-line-2" not in result.stdout.str() + assert "text-line-after" not in result.stdout.str() def test_docstring_full_context_around_error(self, testdir): """Test that we show the whole context before the actual line of a failing doctest, provided that the context is up to 10 lines long. """ - testdir.makepyfile(''' + testdir.makepyfile( + ''' def foo(): """ text-line-1 @@ -226,21 +241,26 @@ class TestDoctests(object): >>> 1 + 1 3 """ - ''') - result = testdir.runpytest('--doctest-modules') - result.stdout.fnmatch_lines([ - '*docstring_full_context_around_error*', - '003*text-line-1', - '004*text-line-2', - '006*>>> 1 + 1', - 'Expected:', - ' 3', - 'Got:', - ' 2', - ]) + ''' + ) + result = testdir.runpytest("--doctest-modules") + result.stdout.fnmatch_lines( + [ + "*docstring_full_context_around_error*", + "003*text-line-1", + "004*text-line-2", + "006*>>> 1 + 1", + "Expected:", + " 3", + "Got:", + " 2", + ] + ) def test_doctest_linedata_missing(self, testdir): - testdir.tmpdir.join('hello.py').write(_pytest._code.Source(""" + testdir.tmpdir.join("hello.py").write( + _pytest._code.Source( + """ class Fun(object): @property def test(self): @@ -248,137 +268,176 @@ class TestDoctests(object): >>> a = 1 >>> 1/0 ''' - """)) + """ + ) + ) result = testdir.runpytest("--doctest-modules") - result.stdout.fnmatch_lines([ - "*hello*", - "*EXAMPLE LOCATION UNKNOWN, not showing all tests of that example*", - "*1/0*", - "*UNEXPECTED*ZeroDivision*", - "*1 failed*", - ]) + result.stdout.fnmatch_lines( + [ + "*hello*", + "*EXAMPLE LOCATION UNKNOWN, not showing all tests of that example*", + "*1/0*", + "*UNEXPECTED*ZeroDivision*", + "*1 failed*", + ] + ) def test_doctest_unex_importerror_only_txt(self, testdir): - testdir.maketxtfile(""" + testdir.maketxtfile( + """ >>> import asdalsdkjaslkdjasd >>> - """) + """ + ) result = testdir.runpytest() # doctest is never executed because of error during hello.py collection - result.stdout.fnmatch_lines([ - "*>>> import asdals*", - "*UNEXPECTED*{e}*".format(e=MODULE_NOT_FOUND_ERROR), - "{e}: No module named *asdal*".format(e=MODULE_NOT_FOUND_ERROR), - ]) + result.stdout.fnmatch_lines( + [ + "*>>> import asdals*", + "*UNEXPECTED*{e}*".format(e=MODULE_NOT_FOUND_ERROR), + "{e}: No module named *asdal*".format(e=MODULE_NOT_FOUND_ERROR), + ] + ) def test_doctest_unex_importerror_with_module(self, testdir): - testdir.tmpdir.join("hello.py").write(_pytest._code.Source(""" + testdir.tmpdir.join("hello.py").write( + _pytest._code.Source( + """ import asdalsdkjaslkdjasd - """)) - testdir.maketxtfile(""" + """ + ) + ) + testdir.maketxtfile( + """ >>> import hello >>> - """) + """ + ) result = testdir.runpytest("--doctest-modules") # doctest is never executed because of error during hello.py collection - result.stdout.fnmatch_lines([ - "*ERROR collecting hello.py*", - "*{e}: No module named *asdals*".format(e=MODULE_NOT_FOUND_ERROR), - "*Interrupted: 1 errors during collection*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERROR collecting hello.py*", + "*{e}: No module named *asdals*".format(e=MODULE_NOT_FOUND_ERROR), + "*Interrupted: 1 errors during collection*", + ] + ) def test_doctestmodule(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ ''' >>> x = 1 >>> x == 1 False ''' - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1) def test_doctestmodule_external_and_issue116(self, testdir): p = testdir.mkpydir("hello") - p.join("__init__.py").write(_pytest._code.Source(""" + p.join("__init__.py").write( + _pytest._code.Source( + """ def somefunc(): ''' >>> i = 0 >>> i + 1 2 ''' - """)) + """ + ) + ) result = testdir.runpytest(p, "--doctest-modules") - result.stdout.fnmatch_lines([ - '004 *>>> i = 0', - '005 *>>> i + 1', - '*Expected:', - "* 2", - "*Got:", - "* 1", - "*:5: DocTestFailure" - ]) + result.stdout.fnmatch_lines( + [ + "004 *>>> i = 0", + "005 *>>> i + 1", + "*Expected:", + "* 2", + "*Got:", + "* 1", + "*:5: DocTestFailure", + ] + ) def test_txtfile_failing(self, testdir): - p = testdir.maketxtfile(""" + p = testdir.maketxtfile( + """ >>> i = 0 >>> i + 1 2 - """) + """ + ) result = testdir.runpytest(p, "-s") - result.stdout.fnmatch_lines([ - '001 >>> i = 0', - '002 >>> i + 1', - 'Expected:', - " 2", - "Got:", - " 1", - "*test_txtfile_failing.txt:2: DocTestFailure" - ]) + result.stdout.fnmatch_lines( + [ + "001 >>> i = 0", + "002 >>> i + 1", + "Expected:", + " 2", + "Got:", + " 1", + "*test_txtfile_failing.txt:2: DocTestFailure", + ] + ) def test_txtfile_with_fixtures(self, testdir): - p = testdir.maketxtfile(""" + p = testdir.maketxtfile( + """ >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' - """) - reprec = testdir.inline_run(p, ) + """ + ) + reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) def test_txtfile_with_usefixtures_in_ini(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] usefixtures = myfixture - """) - testdir.makeconftest(""" + """ + ) + testdir.makeconftest( + """ import pytest @pytest.fixture def myfixture(monkeypatch): monkeypatch.setenv("HELLO", "WORLD") - """) + """ + ) - p = testdir.maketxtfile(""" + p = testdir.maketxtfile( + """ >>> import os >>> os.environ["HELLO"] 'WORLD' - """) - reprec = testdir.inline_run(p, ) + """ + ) + reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) def test_doctestmodule_with_fixtures(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ ''' >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' ''' - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=1) def test_doctestmodule_three_tests(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ ''' >>> dir = getfixture('tmpdir') >>> type(dir).__name__ @@ -398,12 +457,14 @@ class TestDoctests(object): >>> os is os True ''' - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=3) def test_doctestmodule_two_tests_one_fail(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ class MyClass(object): def bad_meth(self): ''' @@ -417,16 +478,20 @@ class TestDoctests(object): >>> magic - 42 0 ''' - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1, passed=1) def test_ignored_whitespace(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ class MyClass(object): ''' >>> a = "foo " @@ -434,16 +499,20 @@ class TestDoctests(object): foo ''' pass - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=1) def test_non_ignored_whitespace(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] doctest_optionflags = ELLIPSIS - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ class MyClass(object): ''' >>> a = "foo " @@ -451,55 +520,64 @@ class TestDoctests(object): foo ''' pass - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1, passed=0) def test_ignored_whitespace_glob(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE - """) - p = testdir.maketxtfile(xdoc=""" + """ + ) + p = testdir.maketxtfile( + xdoc=""" >>> a = "foo " >>> print(a) foo - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(passed=1) def test_non_ignored_whitespace_glob(self, testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] doctest_optionflags = ELLIPSIS - """) - p = testdir.maketxtfile(xdoc=""" + """ + ) + p = testdir.maketxtfile( + xdoc=""" >>> a = "foo " >>> print(a) foo - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1, passed=0) def test_contains_unicode(self, testdir): """Fix internal error with docstrings containing non-ascii characters. """ - testdir.makepyfile(u''' + testdir.makepyfile( + u''' # encoding: utf-8 def foo(): """ >>> name = 'с' # not letter 'c' but instead Cyrillic 's'. 'anything' """ - ''') - result = testdir.runpytest('--doctest-modules') - result.stdout.fnmatch_lines([ - 'Got nothing', - '* 1 failed in*', - ]) + ''' + ) + result = testdir.runpytest("--doctest-modules") + result.stdout.fnmatch_lines(["Got nothing", "* 1 failed in*"]) def test_ignore_import_errors_on_doctest(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import asdf def add_one(x): @@ -508,26 +586,29 @@ class TestDoctests(object): 2 ''' return x + 1 - """) + """ + ) - reprec = testdir.inline_run(p, "--doctest-modules", - "--doctest-ignore-import-errors") + reprec = testdir.inline_run( + p, "--doctest-modules", "--doctest-ignore-import-errors" + ) reprec.assertoutcome(skipped=1, failed=1, passed=0) def test_junit_report_for_doctest(self, testdir): """ #713: Fix --junit-xml option when used with --doctest-modules. """ - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def foo(): ''' >>> 1 + 1 3 ''' pass - """) - reprec = testdir.inline_run(p, "--doctest-modules", - "--junit-xml=junit.xml") + """ + ) + reprec = testdir.inline_run(p, "--doctest-modules", "--junit-xml=junit.xml") reprec.assertoutcome(failed=1) def test_unicode_doctest(self, testdir): @@ -535,7 +616,8 @@ class TestDoctests(object): Test case for issue 2434: DecodeError on Python 2 when doctest contains non-ascii characters. """ - p = testdir.maketxtfile(test_unicode_doctest=""" + p = testdir.maketxtfile( + test_unicode_doctest=""" .. doctest:: >>> print( @@ -545,19 +627,20 @@ class TestDoctests(object): Byé >>> 1/0 # Byé 1 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - '*UNEXPECTED EXCEPTION: ZeroDivisionError*', - '*1 failed*', - ]) + result.stdout.fnmatch_lines( + ["*UNEXPECTED EXCEPTION: ZeroDivisionError*", "*1 failed*"] + ) def test_unicode_doctest_module(self, testdir): """ Test case for issue 2434: DecodeError on Python 2 when doctest docstring contains non-ascii characters. """ - p = testdir.makepyfile(test_unicode_doctest_module=""" + p = testdir.makepyfile( + test_unicode_doctest_module=""" # -*- encoding: utf-8 -*- from __future__ import unicode_literals @@ -567,84 +650,155 @@ class TestDoctests(object): único ''' return "único" - """) - result = testdir.runpytest(p, '--doctest-modules') - result.stdout.fnmatch_lines(['* 1 passed *']) + """ + ) + result = testdir.runpytest(p, "--doctest-modules") + result.stdout.fnmatch_lines(["* 1 passed *"]) + + def test_print_unicode_value(self, testdir): + """ + Test case for issue 3583: Printing Unicode in doctest under Python 2.7 + doesn't work + """ + p = testdir.maketxtfile( + test_print_unicode_value=r""" + Here is a doctest:: + + >>> print(u'\xE5\xE9\xEE\xF8\xFC') + åéîøü + """ + ) + result = testdir.runpytest(p) + result.stdout.fnmatch_lines(["* 1 passed *"]) def test_reportinfo(self, testdir): - ''' + """ Test case to make sure that DoctestItem.reportinfo() returns lineno. - ''' - p = testdir.makepyfile(test_reportinfo=""" + """ + p = testdir.makepyfile( + test_reportinfo=""" def foo(x): ''' >>> foo('a') 'b' ''' return 'c' - """) - items, reprec = testdir.inline_genitems(p, '--doctest-modules') + """ + ) + items, reprec = testdir.inline_genitems(p, "--doctest-modules") reportinfo = items[0].reportinfo() assert reportinfo[1] == 1 + def test_valid_setup_py(self, testdir): + """ + Test to make sure that pytest ignores valid setup.py files when ran + with --doctest-modules + """ + p = testdir.makepyfile( + setup=""" + from setuptools import setup, find_packages + setup(name='sample', + version='0.0', + description='description', + packages=find_packages() + ) + """ + ) + result = testdir.runpytest(p, "--doctest-modules") + result.stdout.fnmatch_lines(["*collected 0 items*"]) + + def test_invalid_setup_py(self, testdir): + """ + Test to make sure that pytest reads setup.py files that are not used + for python packages when ran with --doctest-modules + """ + p = testdir.makepyfile( + setup=""" + def test_foo(): + return 'bar' + """ + ) + result = testdir.runpytest(p, "--doctest-modules") + result.stdout.fnmatch_lines(["*collected 1 item*"]) + class TestLiterals(object): - @pytest.mark.parametrize('config_mode', ['ini', 'comment']) + @pytest.mark.parametrize("config_mode", ["ini", "comment"]) def test_allow_unicode(self, testdir, config_mode): """Test that doctests which output unicode work in all python versions tested by pytest when the ALLOW_UNICODE option is used (either in the ini file or by an inline comment). """ - if config_mode == 'ini': - testdir.makeini(''' + if config_mode == "ini": + testdir.makeini( + """ [pytest] doctest_optionflags = ALLOW_UNICODE - ''') - comment = '' + """ + ) + comment = "" else: - comment = '#doctest: +ALLOW_UNICODE' + comment = "#doctest: +ALLOW_UNICODE" - testdir.maketxtfile(test_doc=""" + testdir.maketxtfile( + test_doc=""" >>> b'12'.decode('ascii') {comment} '12' - """.format(comment=comment)) - testdir.makepyfile(foo=""" + """.format( + comment=comment + ) + ) + testdir.makepyfile( + foo=""" def foo(): ''' >>> b'12'.decode('ascii') {comment} '12' ''' - """.format(comment=comment)) + """.format( + comment=comment + ) + ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=2) - @pytest.mark.parametrize('config_mode', ['ini', 'comment']) + @pytest.mark.parametrize("config_mode", ["ini", "comment"]) def test_allow_bytes(self, testdir, config_mode): """Test that doctests which output bytes work in all python versions tested by pytest when the ALLOW_BYTES option is used (either in the ini file or by an inline comment)(#1287). """ - if config_mode == 'ini': - testdir.makeini(''' + if config_mode == "ini": + testdir.makeini( + """ [pytest] doctest_optionflags = ALLOW_BYTES - ''') - comment = '' + """ + ) + comment = "" else: - comment = '#doctest: +ALLOW_BYTES' + comment = "#doctest: +ALLOW_BYTES" - testdir.maketxtfile(test_doc=""" + testdir.maketxtfile( + test_doc=""" >>> b'foo' {comment} 'foo' - """.format(comment=comment)) - testdir.makepyfile(foo=""" + """.format( + comment=comment + ) + ) + testdir.makepyfile( + foo=""" def foo(): ''' >>> b'foo' {comment} 'foo' ''' - """.format(comment=comment)) + """.format( + comment=comment + ) + ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=2) @@ -653,10 +807,12 @@ class TestLiterals(object): the ALLOW_UNICODE option is not used. The same test should pass in Python 3. """ - testdir.maketxtfile(test_doc=""" + testdir.maketxtfile( + test_doc=""" >>> b'12'.decode('ascii') '12' - """) + """ + ) reprec = testdir.inline_run() passed = int(sys.version_info[0] >= 3) reprec.assertoutcome(passed=passed, failed=int(not passed)) @@ -666,10 +822,12 @@ class TestLiterals(object): the ALLOW_BYTES option is not used. The same test should pass in Python 2 (#1287). """ - testdir.maketxtfile(test_doc=""" + testdir.maketxtfile( + test_doc=""" >>> b'foo' 'foo' - """) + """ + ) reprec = testdir.inline_run() passed = int(sys.version_info[0] == 2) reprec.assertoutcome(passed=passed, failed=int(not passed)) @@ -681,64 +839,93 @@ class TestDoctestSkips(object): the tests should be SKIPPED rather than PASSED. (#957) """ - @pytest.fixture(params=['text', 'module']) + @pytest.fixture(params=["text", "module"]) def makedoctest(self, testdir, request): + def makeit(doctest): mode = request.param - if mode == 'text': + if mode == "text": testdir.maketxtfile(doctest) else: - assert mode == 'module' + assert mode == "module" testdir.makepyfile('"""\n%s"""' % doctest) return makeit def test_one_skipped(self, testdir, makedoctest): - makedoctest(""" + makedoctest( + """ >>> 1 + 1 # doctest: +SKIP 2 >>> 2 + 2 4 - """) + """ + ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=1) def test_one_skipped_failed(self, testdir, makedoctest): - makedoctest(""" + makedoctest( + """ >>> 1 + 1 # doctest: +SKIP 2 >>> 2 + 2 200 - """) + """ + ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(failed=1) def test_all_skipped(self, testdir, makedoctest): - makedoctest(""" + makedoctest( + """ >>> 1 + 1 # doctest: +SKIP 2 >>> 2 + 2 # doctest: +SKIP 200 - """) + """ + ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(skipped=1) def test_vacuous_all_skipped(self, testdir, makedoctest): - makedoctest('') + makedoctest("") reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=0, skipped=0) + def test_continue_on_failure(self, testdir): + testdir.maketxtfile( + test_something=""" + >>> i = 5 + >>> def foo(): + ... raise ValueError('error1') + >>> foo() + >>> i + >>> i + 2 + 7 + >>> i + 1 + """ + ) + result = testdir.runpytest("--doctest-modules", "--doctest-continue-on-failure") + result.assert_outcomes(passed=0, failed=1) + # The lines that contains the failure are 4, 5, and 8. The first one + # is a stack trace and the other two are mismatches. + result.stdout.fnmatch_lines( + ["*4: UnexpectedException*", "*5: DocTestFailure*", "*8: DocTestFailure*"] + ) + class TestDoctestAutoUseFixtures(object): - SCOPES = ['module', 'session', 'class', 'function'] + SCOPES = ["module", "session", "class", "function"] def test_doctest_module_session_fixture(self, testdir): """Test that session fixtures are initialized for doctest modules (#768) """ # session fixture which changes some global data, which will # be accessed by doctests in a module - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest import sys @@ -748,8 +935,10 @@ class TestDoctestAutoUseFixtures(object): sys.pytest_session_data = 1 yield del sys.pytest_session_data - """) - testdir.makepyfile(foo=""" + """ + ) + testdir.makepyfile( + foo=""" import sys def foo(): @@ -761,24 +950,30 @@ class TestDoctestAutoUseFixtures(object): ''' >>> assert sys.pytest_session_data == 1 ''' - """) + """ + ) result = testdir.runpytest("--doctest-modules") - result.stdout.fnmatch_lines('*2 passed*') + result.stdout.fnmatch_lines("*2 passed*") - @pytest.mark.parametrize('scope', SCOPES) - @pytest.mark.parametrize('enable_doctest', [True, False]) + @pytest.mark.parametrize("scope", SCOPES) + @pytest.mark.parametrize("enable_doctest", [True, False]) def test_fixture_scopes(self, testdir, scope, enable_doctest): """Test that auto-use fixtures work properly with doctest modules. See #1057 and #1100. """ - testdir.makeconftest(''' + testdir.makeconftest( + """ import pytest @pytest.fixture(autouse=True, scope="{scope}") def auto(request): return 99 - '''.format(scope=scope)) - testdir.makepyfile(test_1=''' + """.format( + scope=scope + ) + ) + testdir.makepyfile( + test_1=''' def test_foo(): """ >>> getfixture('auto') + 1 @@ -786,47 +981,58 @@ class TestDoctestAutoUseFixtures(object): """ def test_bar(): assert 1 - ''') - params = ('--doctest-modules',) if enable_doctest else () + ''' + ) + params = ("--doctest-modules",) if enable_doctest else () passes = 3 if enable_doctest else 2 result = testdir.runpytest(*params) - result.stdout.fnmatch_lines(['*=== %d passed in *' % passes]) + result.stdout.fnmatch_lines(["*=== %d passed in *" % passes]) - @pytest.mark.parametrize('scope', SCOPES) - @pytest.mark.parametrize('autouse', [True, False]) - @pytest.mark.parametrize('use_fixture_in_doctest', [True, False]) - def test_fixture_module_doctest_scopes(self, testdir, scope, autouse, - use_fixture_in_doctest): + @pytest.mark.parametrize("scope", SCOPES) + @pytest.mark.parametrize("autouse", [True, False]) + @pytest.mark.parametrize("use_fixture_in_doctest", [True, False]) + def test_fixture_module_doctest_scopes( + self, testdir, scope, autouse, use_fixture_in_doctest + ): """Test that auto-use fixtures work properly with doctest files. See #1057 and #1100. """ - testdir.makeconftest(''' + testdir.makeconftest( + """ import pytest @pytest.fixture(autouse={autouse}, scope="{scope}") def auto(request): return 99 - '''.format(scope=scope, autouse=autouse)) + """.format( + scope=scope, autouse=autouse + ) + ) if use_fixture_in_doctest: - testdir.maketxtfile(test_doc=""" + testdir.maketxtfile( + test_doc=""" >>> getfixture('auto') 99 - """) + """ + ) else: - testdir.maketxtfile(test_doc=""" + testdir.maketxtfile( + test_doc=""" >>> 1 + 1 2 - """) - result = testdir.runpytest('--doctest-modules') - assert 'FAILURES' not in str(result.stdout.str()) - result.stdout.fnmatch_lines(['*=== 1 passed in *']) + """ + ) + result = testdir.runpytest("--doctest-modules") + assert "FAILURES" not in str(result.stdout.str()) + result.stdout.fnmatch_lines(["*=== 1 passed in *"]) - @pytest.mark.parametrize('scope', SCOPES) + @pytest.mark.parametrize("scope", SCOPES) def test_auto_use_request_attributes(self, testdir, scope): """Check that all attributes of a request in an autouse fixture behave as expected when requested for a doctest item. """ - testdir.makeconftest(''' + testdir.makeconftest( + """ import pytest @pytest.fixture(autouse=True, scope="{scope}") @@ -838,69 +1044,88 @@ class TestDoctestAutoUseFixtures(object): if "{scope}" == 'function': assert request.function is None return 99 - '''.format(scope=scope)) - testdir.maketxtfile(test_doc=""" + """.format( + scope=scope + ) + ) + testdir.maketxtfile( + test_doc=""" >>> 1 + 1 2 - """) - result = testdir.runpytest('--doctest-modules') - assert 'FAILURES' not in str(result.stdout.str()) - result.stdout.fnmatch_lines(['*=== 1 passed in *']) + """ + ) + result = testdir.runpytest("--doctest-modules") + assert "FAILURES" not in str(result.stdout.str()) + result.stdout.fnmatch_lines(["*=== 1 passed in *"]) class TestDoctestNamespaceFixture(object): - SCOPES = ['module', 'session', 'class', 'function'] + SCOPES = ["module", "session", "class", "function"] - @pytest.mark.parametrize('scope', SCOPES) + @pytest.mark.parametrize("scope", SCOPES) def test_namespace_doctestfile(self, testdir, scope): """ Check that inserting something into the namespace works in a simple text file doctest """ - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest import contextlib @pytest.fixture(autouse=True, scope="{scope}") def add_contextlib(doctest_namespace): doctest_namespace['cl'] = contextlib - """.format(scope=scope)) - p = testdir.maketxtfile(""" + """.format( + scope=scope + ) + ) + p = testdir.maketxtfile( + """ >>> print(cl.__name__) contextlib - """) + """ + ) reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) - @pytest.mark.parametrize('scope', SCOPES) + @pytest.mark.parametrize("scope", SCOPES) def test_namespace_pyfile(self, testdir, scope): """ Check that inserting something into the namespace works in a simple Python file docstring doctest """ - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest import contextlib @pytest.fixture(autouse=True, scope="{scope}") def add_contextlib(doctest_namespace): doctest_namespace['cl'] = contextlib - """.format(scope=scope)) - p = testdir.makepyfile(""" + """.format( + scope=scope + ) + ) + p = testdir.makepyfile( + """ def foo(): ''' >>> print(cl.__name__) contextlib ''' - """) + """ + ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=1) class TestDoctestReportingOption(object): + def _run_doctest_report(self, testdir, format): - testdir.makepyfile(""" + testdir.makepyfile( + """ def foo(): ''' >>> foo() @@ -913,63 +1138,69 @@ class TestDoctestReportingOption(object): '0 1 4\\n' '1 2 5\\n' '2 3 6') - """) + """ + ) return testdir.runpytest("--doctest-modules", "--doctest-report", format) - @pytest.mark.parametrize('format', ['udiff', 'UDIFF', 'uDiFf']) + @pytest.mark.parametrize("format", ["udiff", "UDIFF", "uDiFf"]) def test_doctest_report_udiff(self, testdir, format): result = self._run_doctest_report(testdir, format) - result.stdout.fnmatch_lines([ - ' 0 1 4', - ' -1 2 4', - ' +1 2 5', - ' 2 3 6', - ]) + result.stdout.fnmatch_lines( + [" 0 1 4", " -1 2 4", " +1 2 5", " 2 3 6"] + ) def test_doctest_report_cdiff(self, testdir): - result = self._run_doctest_report(testdir, 'cdiff') - result.stdout.fnmatch_lines([ - ' a b', - ' 0 1 4', - ' ! 1 2 4', - ' 2 3 6', - ' --- 1,4 ----', - ' a b', - ' 0 1 4', - ' ! 1 2 5', - ' 2 3 6', - ]) + result = self._run_doctest_report(testdir, "cdiff") + result.stdout.fnmatch_lines( + [ + " a b", + " 0 1 4", + " ! 1 2 4", + " 2 3 6", + " --- 1,4 ----", + " a b", + " 0 1 4", + " ! 1 2 5", + " 2 3 6", + ] + ) def test_doctest_report_ndiff(self, testdir): - result = self._run_doctest_report(testdir, 'ndiff') - result.stdout.fnmatch_lines([ - ' a b', - ' 0 1 4', - ' - 1 2 4', - ' ? ^', - ' + 1 2 5', - ' ? ^', - ' 2 3 6', - ]) + result = self._run_doctest_report(testdir, "ndiff") + result.stdout.fnmatch_lines( + [ + " a b", + " 0 1 4", + " - 1 2 4", + " ? ^", + " + 1 2 5", + " ? ^", + " 2 3 6", + ] + ) - @pytest.mark.parametrize('format', ['none', 'only_first_failure']) + @pytest.mark.parametrize("format", ["none", "only_first_failure"]) def test_doctest_report_none_or_only_first_failure(self, testdir, format): result = self._run_doctest_report(testdir, format) - result.stdout.fnmatch_lines([ - 'Expected:', - ' a b', - ' 0 1 4', - ' 1 2 4', - ' 2 3 6', - 'Got:', - ' a b', - ' 0 1 4', - ' 1 2 5', - ' 2 3 6', - ]) + result.stdout.fnmatch_lines( + [ + "Expected:", + " a b", + " 0 1 4", + " 1 2 4", + " 2 3 6", + "Got:", + " a b", + " 0 1 4", + " 1 2 5", + " 2 3 6", + ] + ) def test_doctest_report_invalid(self, testdir): - result = self._run_doctest_report(testdir, 'obviously_invalid_format') - result.stderr.fnmatch_lines([ - "*error: argument --doctest-report: invalid choice: 'obviously_invalid_format' (choose from*" - ]) + result = self._run_doctest_report(testdir, "obviously_invalid_format") + result.stderr.fnmatch_lines( + [ + "*error: argument --doctest-report: invalid choice: 'obviously_invalid_format' (choose from*" + ] + ) diff --git a/third_party/python/pytest/testing/test_entry_points.py b/third_party/python/pytest/testing/test_entry_points.py index 6ca68b481fa1..8f734778faa8 100644 --- a/third_party/python/pytest/testing/test_entry_points.py +++ b/third_party/python/pytest/testing/test_entry_points.py @@ -4,11 +4,11 @@ import pkg_resources import pytest -@pytest.mark.parametrize("entrypoint", ['py.test', 'pytest']) +@pytest.mark.parametrize("entrypoint", ["py.test", "pytest"]) def test_entry_point_exist(entrypoint): - assert entrypoint in pkg_resources.get_entry_map('pytest')['console_scripts'] + assert entrypoint in pkg_resources.get_entry_map("pytest")["console_scripts"] def test_pytest_entry_points_are_identical(): - entryMap = pkg_resources.get_entry_map('pytest')['console_scripts'] - assert entryMap['pytest'].module_name == entryMap['py.test'].module_name + entryMap = pkg_resources.get_entry_map("pytest")["console_scripts"] + assert entryMap["pytest"].module_name == entryMap["py.test"].module_name diff --git a/third_party/python/pytest/testing/test_helpconfig.py b/third_party/python/pytest/testing/test_helpconfig.py index 845005a0575a..b5424235b168 100644 --- a/third_party/python/pytest/testing/test_helpconfig.py +++ b/third_party/python/pytest/testing/test_helpconfig.py @@ -7,57 +7,53 @@ def test_version(testdir, pytestconfig): result = testdir.runpytest("--version") assert result.ret == 0 # p = py.path.local(py.__file__).dirpath() - result.stderr.fnmatch_lines([ - '*pytest*%s*imported from*' % (pytest.__version__, ) - ]) + result.stderr.fnmatch_lines(["*pytest*%s*imported from*" % (pytest.__version__,)]) if pytestconfig.pluginmanager.list_plugin_distinfo(): - result.stderr.fnmatch_lines([ - "*setuptools registered plugins:", - "*at*", - ]) + result.stderr.fnmatch_lines(["*setuptools registered plugins:", "*at*"]) def test_help(testdir): result = testdir.runpytest("--help") assert result.ret == 0 - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *-v*verbose* *setup.cfg* *minversion* *to see*markers*pytest --markers* *to see*fixtures*pytest --fixtures* - """) + """ + ) def test_hookvalidation_unknown(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_hello(xyz): pass - """) + """ + ) result = testdir.runpytest() assert result.ret != 0 - result.stdout.fnmatch_lines([ - '*unknown hook*pytest_hello*' - ]) + result.stdout.fnmatch_lines(["*unknown hook*pytest_hello*"]) def test_hookvalidation_optional(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.hookimpl(optionalhook=True) def pytest_hello(xyz): pass - """) + """ + ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED def test_traceconfig(testdir): result = testdir.runpytest("--traceconfig") - result.stdout.fnmatch_lines([ - "*using*pytest*py*", - "*active plugins*", - ]) + result.stdout.fnmatch_lines(["*using*pytest*py*", "*active plugins*"]) def test_debug(testdir, monkeypatch): @@ -71,7 +67,6 @@ def test_PYTEST_DEBUG(testdir, monkeypatch): monkeypatch.setenv("PYTEST_DEBUG", "1") result = testdir.runpytest_subprocess() assert result.ret == EXIT_NOTESTSCOLLECTED - result.stderr.fnmatch_lines([ - "*pytest_plugin_registered*", - "*manager*PluginManager*" - ]) + result.stderr.fnmatch_lines( + ["*pytest_plugin_registered*", "*manager*PluginManager*"] + ) diff --git a/third_party/python/pytest/testing/test_junitxml.py b/third_party/python/pytest/testing/test_junitxml.py index b604c02a3dea..d0be5f267596 100644 --- a/third_party/python/pytest/testing/test_junitxml.py +++ b/third_party/python/pytest/testing/test_junitxml.py @@ -23,12 +23,13 @@ def assert_attr(node, **kwargs): if anode is not None: return anode.value - expected = dict((name, str(value)) for name, value in kwargs.items()) - on_node = dict((name, nodeval(node, name)) for name in expected) + expected = {name: str(value) for name, value in kwargs.items()} + on_node = {name: nodeval(node, name) for name in expected} assert on_node == expected class DomNode(object): + def __init__(self, dom): self.__node = dom @@ -80,8 +81,10 @@ class DomNode(object): class TestPython(object): + def test_summing_simple(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_pass(): pass @@ -95,14 +98,16 @@ class TestPython(object): @pytest.mark.xfail def test_xpass(): assert 1 - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(name="pytest", errors=0, failures=1, skips=2, tests=5) def test_summing_simple_with_errors(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture def fixture(): @@ -119,14 +124,16 @@ class TestPython(object): @pytest.mark.xfail(strict=True) def test_xpass(): assert True - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(name="pytest", errors=1, failures=2, skips=1, tests=5) def test_timing_function(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import time, pytest def setup_module(): time.sleep(0.01) @@ -134,7 +141,8 @@ class TestPython(object): time.sleep(0.01) def test_sleep(): time.sleep(0.01) - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") @@ -142,7 +150,8 @@ class TestPython(object): assert round(float(val), 2) >= 0.03 def test_setup_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -150,7 +159,8 @@ class TestPython(object): raise ValueError() def test_function(arg): pass - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") @@ -160,13 +170,15 @@ class TestPython(object): file="test_setup_error.py", line="5", classname="test_setup_error", - name="test_function") + name="test_function", + ) fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="test setup failure") assert "ValueError" in fnode.toxml() def test_teardown_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -175,7 +187,8 @@ class TestPython(object): raise ValueError() def test_function(arg): pass - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") @@ -184,13 +197,15 @@ class TestPython(object): file="test_teardown_error.py", line="6", classname="test_teardown_error", - name="test_function") + name="test_function", + ) fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="test teardown failure") assert "ValueError" in fnode.toxml() def test_call_failure_teardown_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -199,7 +214,8 @@ class TestPython(object): raise Exception("Teardown Exception") def test_function(arg): raise Exception("Call Exception") - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") @@ -213,11 +229,13 @@ class TestPython(object): snode.assert_attr(message="test teardown failure") def test_skip_contains_name_reason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_skip(): pytest.skip("hello23") - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") @@ -227,17 +245,20 @@ class TestPython(object): file="test_skip_contains_name_reason.py", line="1", classname="test_skip_contains_name_reason", - name="test_skip") + name="test_skip", + ) snode = tnode.find_first_by_tag("skipped") - snode.assert_attr(type="pytest.skip", message="hello23", ) + snode.assert_attr(type="pytest.skip", message="hello23") def test_mark_skip_contains_name_reason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip(reason="hello24") def test_skip(): assert True - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") @@ -247,18 +268,21 @@ class TestPython(object): file="test_mark_skip_contains_name_reason.py", line="1", classname="test_mark_skip_contains_name_reason", - name="test_skip") + name="test_skip", + ) snode = tnode.find_first_by_tag("skipped") - snode.assert_attr(type="pytest.skip", message="hello24", ) + snode.assert_attr(type="pytest.skip", message="hello24") def test_mark_skipif_contains_name_reason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest GLOBAL_CONDITION = True @pytest.mark.skipif(GLOBAL_CONDITION, reason="hello25") def test_skip(): assert True - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") @@ -268,28 +292,33 @@ class TestPython(object): file="test_mark_skipif_contains_name_reason.py", line="2", classname="test_mark_skipif_contains_name_reason", - name="test_skip") + name="test_skip", + ) snode = tnode.find_first_by_tag("skipped") - snode.assert_attr(type="pytest.skip", message="hello25", ) + snode.assert_attr(type="pytest.skip", message="hello25") def test_mark_skip_doesnt_capture_output(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip(reason="foo") def test_skip(): print("bar!") - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node_xml = dom.find_first_by_tag("testsuite").toxml() assert "bar!" not in node_xml def test_classname_instance(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ class TestClass(object): def test_method(self): assert 0 - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") @@ -299,7 +328,8 @@ class TestPython(object): file="test_classname_instance.py", line="1", classname="test_classname_instance.TestClass", - name="test_method") + name="test_method", + ) def test_classname_nested_dir(self, testdir): p = testdir.tmpdir.ensure("sub", "test_hello.py") @@ -313,7 +343,8 @@ class TestPython(object): file=os.path.join("sub", "test_hello.py"), line="0", classname="sub.test_hello", - name="test_func") + name="test_func", + ) def test_internal_error(self, testdir): testdir.makeconftest("def pytest_runtest_protocol(): 0 / 0") @@ -328,41 +359,63 @@ class TestPython(object): fnode.assert_attr(message="internal error") assert "Division" in fnode.toxml() - def test_failure_function(self, testdir): - testdir.makepyfile(""" + @pytest.mark.parametrize("junit_logging", ["no", "system-out", "system-err"]) + def test_failure_function(self, testdir, junit_logging): + testdir.makepyfile( + """ + import logging import sys + def test_fail(): print ("hello-stdout") sys.stderr.write("hello-stderr\\n") + logging.info('info msg') + logging.warning('warning msg') raise ValueError(42) - """) + """ + ) - result, dom = runandparse(testdir) + result, dom = runandparse(testdir, "-o", "junit_logging=%s" % junit_logging) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr( file="test_failure_function.py", - line="1", + line="3", classname="test_failure_function", - name="test_fail") + name="test_fail", + ) fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="ValueError: 42") assert "ValueError" in fnode.toxml() systemout = fnode.next_siebling assert systemout.tag == "system-out" assert "hello-stdout" in systemout.toxml() + assert "info msg" not in systemout.toxml() systemerr = systemout.next_siebling assert systemerr.tag == "system-err" assert "hello-stderr" in systemerr.toxml() + assert "info msg" not in systemerr.toxml() + + if junit_logging == "system-out": + assert "warning msg" in systemout.toxml() + assert "warning msg" not in systemerr.toxml() + elif junit_logging == "system-err": + assert "warning msg" not in systemout.toxml() + assert "warning msg" in systemerr.toxml() + elif junit_logging == "no": + assert "warning msg" not in systemout.toxml() + assert "warning msg" not in systemerr.toxml() def test_failure_verbose_message(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys def test_fail(): assert 0, "An error" - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") @@ -371,13 +424,15 @@ class TestPython(object): fnode.assert_attr(message="AssertionError: An error assert 0") def test_failure_escape(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('arg1', "<&'", ids="<&'") def test_func(arg1): print(arg1) assert 0 - """) + """ + ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") @@ -390,19 +445,22 @@ class TestPython(object): file="test_failure_escape.py", line="1", classname="test_failure_escape", - name="test_func[%s]" % char) - sysout = tnode.find_first_by_tag('system-out') + name="test_func[%s]" % char, + ) + sysout = tnode.find_first_by_tag("system-out") text = sysout.text - assert text == '%s\n' % char + assert text == "%s\n" % char def test_junit_prefixing(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_func(): assert 0 class TestHello(object): def test_hello(self): pass - """) + """ + ) result, dom = runandparse(testdir, "--junitprefix=xyz") assert result.ret node = dom.find_first_by_tag("testsuite") @@ -412,21 +470,24 @@ class TestPython(object): file="test_junit_prefixing.py", line="0", classname="xyz.test_junit_prefixing", - name="test_func") + name="test_func", + ) tnode = node.find_nth_by_tag("testcase", 1) tnode.assert_attr( file="test_junit_prefixing.py", line="3", - classname="xyz.test_junit_prefixing." - "TestHello", - name="test_hello") + classname="xyz.test_junit_prefixing." "TestHello", + name="test_hello", + ) def test_xfailure_function(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_xfail(): pytest.xfail("42") - """) + """ + ) result, dom = runandparse(testdir) assert not result.ret node = dom.find_first_by_tag("testsuite") @@ -436,18 +497,40 @@ class TestPython(object): file="test_xfailure_function.py", line="1", classname="test_xfailure_function", - name="test_xfail") + name="test_xfail", + ) fnode = tnode.find_first_by_tag("skipped") fnode.assert_attr(message="expected test failure") # assert "ValueError" in fnode.toxml() + def test_xfail_captures_output_once(self, testdir): + testdir.makepyfile( + """ + import sys + import pytest + + @pytest.mark.xfail() + def test_fail(): + sys.stdout.write('XFAIL This is stdout') + sys.stderr.write('XFAIL This is stderr') + assert 0 + """ + ) + result, dom = runandparse(testdir) + node = dom.find_first_by_tag("testsuite") + tnode = node.find_first_by_tag("testcase") + assert len(tnode.find_by_tag("system-err")) == 1 + assert len(tnode.find_by_tag("system-out")) == 1 + def test_xfailure_xpass(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.xfail def test_xpass(): pass - """) + """ + ) result, dom = runandparse(testdir) # assert result.ret node = dom.find_first_by_tag("testsuite") @@ -457,15 +540,18 @@ class TestPython(object): file="test_xfailure_xpass.py", line="1", classname="test_xfailure_xpass", - name="test_xpass") + name="test_xpass", + ) def test_xfailure_xpass_strict(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.xfail(strict=True, reason="This needs to fail!") def test_xpass(): pass - """) + """ + ) result, dom = runandparse(testdir) # assert result.ret node = dom.find_first_by_tag("testsuite") @@ -475,7 +561,8 @@ class TestPython(object): file="test_xfailure_xpass_strict.py", line="1", classname="test_xfailure_xpass_strict", - name="test_xpass") + name="test_xpass", + ) fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="[XPASS(strict)] This needs to fail!") @@ -486,22 +573,23 @@ class TestPython(object): node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=1, tests=1) tnode = node.find_first_by_tag("testcase") - tnode.assert_attr( - file="test_collect_error.py", - name="test_collect_error") + tnode.assert_attr(file="test_collect_error.py", name="test_collect_error") assert tnode["line"] is None fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="collection failure") assert "SyntaxError" in fnode.toxml() def test_unicode(self, testdir): - value = 'hx\xc4\x85\xc4\x87\n' - testdir.makepyfile(""" + value = "hx\xc4\x85\xc4\x87\n" + testdir.makepyfile( + """ # coding: latin1 def test_hello(): print (%r) assert 0 - """ % value) + """ + % value + ) result, dom = runandparse(testdir) assert result.ret == 1 tnode = dom.find_first_by_tag("testcase") @@ -511,22 +599,26 @@ class TestPython(object): def test_assertion_binchars(self, testdir): """this test did fail when the escaping wasnt strict""" - testdir.makepyfile(""" + testdir.makepyfile( + """ M1 = '\x01\x02\x03\x04' M2 = '\x01\x02\x03\x05' def test_str_compare(): assert M1 == M2 - """) + """ + ) result, dom = runandparse(testdir) print(dom.toxml()) def test_pass_captures_stdout(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_pass(): print('hello-stdout') - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") @@ -534,11 +626,13 @@ class TestPython(object): assert "hello-stdout" in systemout.toxml() def test_pass_captures_stderr(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys def test_pass(): sys.stderr.write('hello-stderr') - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") @@ -546,7 +640,8 @@ class TestPython(object): assert "hello-stderr" in systemout.toxml() def test_setup_error_captures_stdout(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -555,7 +650,8 @@ class TestPython(object): raise ValueError() def test_function(arg): pass - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") @@ -563,7 +659,8 @@ class TestPython(object): assert "hello-stdout" in systemout.toxml() def test_setup_error_captures_stderr(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys import pytest @@ -573,7 +670,8 @@ class TestPython(object): raise ValueError() def test_function(arg): pass - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") @@ -581,7 +679,8 @@ class TestPython(object): assert "hello-stderr" in systemout.toxml() def test_avoid_double_stdout(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys import pytest @@ -592,7 +691,8 @@ class TestPython(object): raise ValueError() def test_function(arg): sys.stdout.write('hello-stdout call') - """) + """ + ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") @@ -603,8 +703,8 @@ class TestPython(object): def test_mangle_test_address(): from _pytest.junitxml import mangle_test_address - address = '::'.join( - ["a/my.py.thing.py", "Class", "()", "method", "[a-1-::]"]) + + address = "::".join(["a/my.py.thing.py", "Class", "()", "method", "[a-1-::]"]) newnames = mangle_test_address(address) assert newnames == ["a.my.py.thing", "Class", "method", "[a-1-::]"] @@ -613,6 +713,7 @@ def test_dont_configure_on_slaves(tmpdir): gotten = [] class FakeConfig(object): + def __init__(self): self.pluginmanager = self self.option = self @@ -622,11 +723,12 @@ def test_dont_configure_on_slaves(tmpdir): junitprefix = None # XXX: shouldnt need tmpdir ? - xmlpath = str(tmpdir.join('junix.xml')) + xmlpath = str(tmpdir.join("junix.xml")) register = gotten.append fake_config = FakeConfig() from _pytest import junitxml + junitxml.pytest_configure(fake_config) assert len(gotten) == 1 FakeConfig.slaveinput = None @@ -635,8 +737,10 @@ def test_dont_configure_on_slaves(tmpdir): class TestNonPython(object): + def test_summing_simple(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_collect_file(path, parent): if path.ext == ".xyz": @@ -649,7 +753,8 @@ class TestNonPython(object): raise ValueError(42) def repr_failure(self, excinfo): return "custom item runtest failed" - """) + """ + ) testdir.tmpdir.join("myfile.xyz").write("hello") result, dom = runandparse(testdir) assert result.ret @@ -664,33 +769,37 @@ class TestNonPython(object): def test_nullbyte(testdir): # A null byte can not occur in XML (see section 2.2 of the spec) - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys def test_print_nullbyte(): sys.stdout.write('Here the null -->' + chr(0) + '<--') sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--') assert False - """) - xmlf = testdir.tmpdir.join('junit.xml') - testdir.runpytest('--junitxml=%s' % xmlf) + """ + ) + xmlf = testdir.tmpdir.join("junit.xml") + testdir.runpytest("--junitxml=%s" % xmlf) text = xmlf.read() - assert '\x00' not in text - assert '#x00' in text + assert "\x00" not in text + assert "#x00" in text def test_nullbyte_replace(testdir): # Check if the null byte gets replaced - testdir.makepyfile(""" + testdir.makepyfile( + """ import sys def test_print_nullbyte(): sys.stdout.write('Here the null -->' + chr(0) + '<--') sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--') assert False - """) - xmlf = testdir.tmpdir.join('junit.xml') - testdir.runpytest('--junitxml=%s' % xmlf) + """ + ) + xmlf = testdir.tmpdir.join("junit.xml") + testdir.runpytest("--junitxml=%s" % xmlf) text = xmlf.read() - assert '#x0' in text + assert "#x0" in text def test_invalid_xml_escape(): @@ -707,9 +816,20 @@ def test_invalid_xml_escape(): unichr(65) except NameError: unichr = chr - invalid = (0x00, 0x1, 0xB, 0xC, 0xE, 0x19, 27, # issue #126 - 0xD800, 0xDFFF, 0xFFFE, 0x0FFFF) # , 0x110000) - valid = (0x9, 0xA, 0x20, ) + invalid = ( + 0x00, + 0x1, + 0xB, + 0xC, + 0xE, + 0x19, + 27, # issue #126 + 0xD800, + 0xDFFF, + 0xFFFE, + 0x0FFFF, + ) # , 0x110000) + valid = (0x9, 0xA, 0x20) # 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF) from _pytest.junitxml import bin_xml_escape @@ -717,34 +837,36 @@ def test_invalid_xml_escape(): for i in invalid: got = bin_xml_escape(unichr(i)).uniobj if i <= 0xFF: - expected = '#x%02X' % i + expected = "#x%02X" % i else: - expected = '#x%04X' % i + expected = "#x%04X" % i assert got == expected for i in valid: assert chr(i) == bin_xml_escape(unichr(i)).uniobj def test_logxml_path_expansion(tmpdir, monkeypatch): - home_tilde = py.path.local(os.path.expanduser('~')).join('test.xml') + home_tilde = py.path.local(os.path.expanduser("~")).join("test.xml") - xml_tilde = LogXML('~%stest.xml' % tmpdir.sep, None) + xml_tilde = LogXML("~%stest.xml" % tmpdir.sep, None) assert xml_tilde.logfile == home_tilde # this is here for when $HOME is not set correct monkeypatch.setenv("HOME", tmpdir) - home_var = os.path.normpath(os.path.expandvars('$HOME/test.xml')) + home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml")) - xml_var = LogXML('$HOME%stest.xml' % tmpdir.sep, None) + xml_var = LogXML("$HOME%stest.xml" % tmpdir.sep, None) assert xml_var.logfile == home_var def test_logxml_changingdir(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_func(): import os os.chdir("a") - """) + """ + ) testdir.tmpdir.mkdir("a") result = testdir.runpytest("--junitxml=a/x.xml") assert result.ret == 0 @@ -753,10 +875,12 @@ def test_logxml_changingdir(testdir): def test_logxml_makedir(testdir): """--junitxml should automatically create directories for the xml file""" - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_pass(): pass - """) + """ + ) result = testdir.runpytest("--junitxml=path/to/results.xml") assert result.ret == 0 assert testdir.tmpdir.join("path/to/results.xml").check() @@ -769,12 +893,14 @@ def test_logxml_check_isdir(testdir): def test_escaped_parametrized_names_xml(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('char', [u"\\x00"]) def test_func(char): assert char - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") @@ -782,33 +908,36 @@ def test_escaped_parametrized_names_xml(testdir): def test_double_colon_split_function_issue469(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('param', ["double::colon"]) def test_func(param): pass - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") node.assert_attr(classname="test_double_colon_split_function_issue469") - node.assert_attr(name='test_func[double::colon]') + node.assert_attr(name="test_func[double::colon]") def test_double_colon_split_method_issue469(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest class TestClass(object): @pytest.mark.parametrize('param', ["double::colon"]) def test_func(self, param): pass - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") - node.assert_attr( - classname="test_double_colon_split_method_issue469.TestClass") - node.assert_attr(name='test_func[double::colon]') + node.assert_attr(classname="test_double_colon_split_method_issue469.TestClass") + node.assert_attr(name="test_func[double::colon]") def test_unicode_issue368(testdir): @@ -821,7 +950,7 @@ def test_unicode_issue368(testdir): longrepr = ustr sections = [] nodeid = "something" - location = 'tests/filename.py', 42, 'TestClass.method' + location = "tests/filename.py", 42, "TestClass.method" test_report = Report() @@ -842,96 +971,123 @@ def test_unicode_issue368(testdir): def test_record_property(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture - def other(record_xml_property): - record_xml_property("bar", 1) - def test_record(record_xml_property, other): - record_xml_property("foo", "<1"); - """) - result, dom = runandparse(testdir, '-rw') + def other(record_property): + record_property("bar", 1) + def test_record(record_property, other): + record_property("foo", "<1"); + """ + ) + result, dom = runandparse(testdir, "-rwv") node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") - psnode = tnode.find_first_by_tag('properties') - pnodes = psnode.find_by_tag('property') + psnode = tnode.find_first_by_tag("properties") + pnodes = psnode.find_by_tag("property") pnodes[0].assert_attr(name="bar", value="1") pnodes[1].assert_attr(name="foo", value="<1") - result.stdout.fnmatch_lines([ - 'test_record_property.py::test_record', - '*record_xml_property*experimental*', - ]) def test_record_property_same_name(testdir): - testdir.makepyfile(""" - def test_record_with_same_name(record_xml_property): - record_xml_property("foo", "bar") - record_xml_property("foo", "baz") - """) - result, dom = runandparse(testdir, '-rw') + testdir.makepyfile( + """ + def test_record_with_same_name(record_property): + record_property("foo", "bar") + record_property("foo", "baz") + """ + ) + result, dom = runandparse(testdir, "-rw") node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") - psnode = tnode.find_first_by_tag('properties') - pnodes = psnode.find_by_tag('property') + psnode = tnode.find_first_by_tag("properties") + pnodes = psnode.find_by_tag("property") pnodes[0].assert_attr(name="foo", value="bar") pnodes[1].assert_attr(name="foo", value="baz") +def test_record_attribute(testdir): + testdir.makepyfile( + """ + import pytest + + @pytest.fixture + def other(record_xml_attribute): + record_xml_attribute("bar", 1) + def test_record(record_xml_attribute, other): + record_xml_attribute("foo", "<1"); + """ + ) + result, dom = runandparse(testdir, "-rw") + node = dom.find_first_by_tag("testsuite") + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr(bar="1") + tnode.assert_attr(foo="<1") + result.stdout.fnmatch_lines( + ["test_record_attribute.py::test_record", "*record_xml_attribute*experimental*"] + ) + + def test_random_report_log_xdist(testdir): """xdist calls pytest_runtest_logreport as they are executed by the slaves, with nodes from several nodes overlapping, so junitxml must cope with that to produce correct reports. #1064 """ - pytest.importorskip('xdist') - testdir.makepyfile(""" + pytest.importorskip("xdist") + testdir.makepyfile( + """ import pytest, time @pytest.mark.parametrize('i', list(range(30))) def test_x(i): assert i != 22 - """) - _, dom = runandparse(testdir, '-n2') + """ + ) + _, dom = runandparse(testdir, "-n2") suite_node = dom.find_first_by_tag("testsuite") failed = [] for case_node in suite_node.find_by_tag("testcase"): - if case_node.find_first_by_tag('failure'): - failed.append(case_node['name']) + if case_node.find_first_by_tag("failure"): + failed.append(case_node["name"]) - assert failed == ['test_x[22]'] + assert failed == ["test_x[22]"] def test_runs_twice(testdir): - f = testdir.makepyfile(''' + f = testdir.makepyfile( + """ def test_pass(): pass - ''') + """ + ) result, dom = runandparse(testdir, f, f) - assert 'INTERNALERROR' not in result.stdout.str() - first, second = [x['classname'] for x in dom.find_by_tag("testcase")] + assert "INTERNALERROR" not in result.stdout.str() + first, second = [x["classname"] for x in dom.find_by_tag("testcase")] assert first == second -@pytest.mark.xfail(reason='hangs', run=False) +@pytest.mark.xfail(reason="hangs", run=False) def test_runs_twice_xdist(testdir): - pytest.importorskip('xdist') - f = testdir.makepyfile(''' + pytest.importorskip("xdist") + f = testdir.makepyfile( + """ def test_pass(): pass - ''') + """ + ) - result, dom = runandparse( - testdir, f, - '--dist', 'each', '--tx', '2*popen',) - assert 'INTERNALERROR' not in result.stdout.str() - first, second = [x['classname'] for x in dom.find_by_tag("testcase")] + result, dom = runandparse(testdir, f, "--dist", "each", "--tx", "2*popen") + assert "INTERNALERROR" not in result.stdout.str() + first, second = [x["classname"] for x in dom.find_by_tag("testcase")] assert first == second def test_fancy_items_regression(testdir): # issue 1259 - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class FunItem(pytest.Item): def runtest(self): @@ -951,33 +1107,38 @@ def test_fancy_items_regression(testdir): def pytest_collect_file(path, parent): if path.check(ext='.py'): return FunCollector(path, parent) - """) + """ + ) - testdir.makepyfile(''' + testdir.makepyfile( + """ def test_pass(): pass - ''') + """ + ) result, dom = runandparse(testdir) - assert 'INTERNALERROR' not in result.stdout.str() + assert "INTERNALERROR" not in result.stdout.str() items = sorted( - '%(classname)s %(name)s %(file)s' % x - - for x in dom.find_by_tag("testcase")) + "%(classname)s %(name)s %(file)s" % x for x in dom.find_by_tag("testcase") + ) import pprint + pprint.pprint(items) - assert items == [ - u'conftest a conftest.py', - u'conftest a conftest.py', - u'conftest b conftest.py', - u'test_fancy_items_regression a test_fancy_items_regression.py', - u'test_fancy_items_regression a test_fancy_items_regression.py', - u'test_fancy_items_regression b test_fancy_items_regression.py', - u'test_fancy_items_regression test_pass' - u' test_fancy_items_regression.py', - ] + assert ( + items + == [ + u"conftest a conftest.py", + u"conftest a conftest.py", + u"conftest b conftest.py", + u"test_fancy_items_regression a test_fancy_items_regression.py", + u"test_fancy_items_regression a test_fancy_items_regression.py", + u"test_fancy_items_regression b test_fancy_items_regression.py", + u"test_fancy_items_regression test_pass" u" test_fancy_items_regression.py", + ] + ) def test_global_properties(testdir): @@ -990,26 +1151,26 @@ def test_global_properties(testdir): nodeid = "test_node_id" log.pytest_sessionstart() - log.add_global_property('foo', 1) - log.add_global_property('bar', 2) + log.add_global_property("foo", 1) + log.add_global_property("bar", 2) log.pytest_sessionfinish() dom = minidom.parse(str(path)) - properties = dom.getElementsByTagName('properties') + properties = dom.getElementsByTagName("properties") - assert (properties.length == 1), "There must be one node" + assert properties.length == 1, "There must be one node" - property_list = dom.getElementsByTagName('property') + property_list = dom.getElementsByTagName("property") - assert (property_list.length == 2), "There most be only 2 property nodes" + assert property_list.length == 2, "There most be only 2 property nodes" - expected = {'foo': '1', 'bar': '2'} + expected = {"foo": "1", "bar": "2"} actual = {} for p in property_list: - k = str(p.getAttribute('name')) - v = str(p.getAttribute('value')) + k = str(p.getAttribute("name")) + v = str(p.getAttribute("value")) actual[k] = v assert actual == expected @@ -1025,7 +1186,7 @@ def test_url_property(testdir): longrepr = "FooBarBaz" sections = [] nodeid = "something" - location = 'tests/filename.py', 42, 'TestClass.method' + location = "tests/filename.py", 42, "TestClass.method" url = test_url test_report = Report() @@ -1035,27 +1196,35 @@ def test_url_property(testdir): node_reporter.append_failure(test_report) log.pytest_sessionfinish() - test_case = minidom.parse(str(path)).getElementsByTagName('testcase')[0] + test_case = minidom.parse(str(path)).getElementsByTagName("testcase")[0] - assert (test_case.getAttribute('url') == test_url), "The URL did not get written to the xml" + assert ( + test_case.getAttribute("url") == test_url + ), "The URL did not get written to the xml" -@pytest.mark.parametrize('suite_name', ['my_suite', '']) +@pytest.mark.parametrize("suite_name", ["my_suite", ""]) def test_set_suite_name(testdir, suite_name): if suite_name: - testdir.makeini(""" + testdir.makeini( + """ [pytest] - junit_suite_name={0} - """.format(suite_name)) + junit_suite_name={} + """.format( + suite_name + ) + ) expected = suite_name else: - expected = 'pytest' - testdir.makepyfile(""" + expected = "pytest" + testdir.makepyfile( + """ import pytest def test_func(): pass - """) + """ + ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") diff --git a/third_party/python/pytest/testing/test_mark.py b/third_party/python/pytest/testing/test_mark.py index 3ac42daee118..e2e7369dc798 100644 --- a/third_party/python/pytest/testing/test_mark.py +++ b/third_party/python/pytest/testing/test_mark.py @@ -1,19 +1,31 @@ from __future__ import absolute_import, division, print_function import os import sys - +import mock import pytest -from _pytest.mark import MarkGenerator as Mark, ParameterSet, transfer_markers +from _pytest.mark import ( + MarkGenerator as Mark, + ParameterSet, + transfer_markers, + EMPTY_PARAMETERSET_OPTION, +) +from _pytest.nodes import Node + +ignore_markinfo = pytest.mark.filterwarnings( + "ignore:MarkInfo objects:_pytest.deprecated.RemovedInPytest4Warning" +) class TestMark(object): + def test_markinfo_repr(self): from _pytest.mark import MarkInfo, Mark - m = MarkInfo(Mark("hello", (1, 2), {})) + + m = MarkInfo.for_mark(Mark("hello", (1, 2), {})) repr(m) - @pytest.mark.parametrize('attr', ['mark', 'param']) - @pytest.mark.parametrize('modulename', ['py.test', 'pytest']) + @pytest.mark.parametrize("attr", ["mark", "param"]) + @pytest.mark.parametrize("modulename", ["py.test", "pytest"]) def test_pytest_exists_in_namespace_all(self, attr, modulename): module = sys.modules[modulename] assert attr in module.__all__ @@ -23,6 +35,7 @@ class TestMark(object): pytest.raises((AttributeError, TypeError), mark) def test_mark_with_param(self): + def some_function(abc): pass @@ -37,7 +50,7 @@ class TestMark(object): def test_pytest_mark_name_starts_with_underscore(self): mark = Mark() - pytest.raises(AttributeError, getattr, mark, '_some_name') + pytest.raises(AttributeError, getattr, mark, "_some_name") def test_pytest_mark_bare(self): mark = Mark() @@ -48,6 +61,7 @@ class TestMark(object): mark.hello(f) assert f.hello + @ignore_markinfo def test_pytest_mark_keywords(self): mark = Mark() @@ -56,9 +70,10 @@ class TestMark(object): mark.world(x=3, y=4)(f) assert f.world - assert f.world.kwargs['x'] == 3 - assert f.world.kwargs['y'] == 4 + assert f.world.kwargs["x"] == 3 + assert f.world.kwargs["y"] == 4 + @ignore_markinfo def test_apply_multiple_and_merge(self): mark = Mark() @@ -67,14 +82,15 @@ class TestMark(object): mark.world mark.world(x=3)(f) - assert f.world.kwargs['x'] == 3 + assert f.world.kwargs["x"] == 3 mark.world(y=4)(f) - assert f.world.kwargs['x'] == 3 - assert f.world.kwargs['y'] == 4 + assert f.world.kwargs["x"] == 3 + assert f.world.kwargs["y"] == 4 mark.world(y=1)(f) - assert f.world.kwargs['y'] == 1 + assert f.world.kwargs["y"] == 1 assert len(f.world.args) == 0 + @ignore_markinfo def test_pytest_mark_positional(self): mark = Mark() @@ -85,6 +101,7 @@ class TestMark(object): assert f.world.args[0] == "hello" mark.world("world")(f) + @ignore_markinfo def test_pytest_mark_positional_func_and_keyword(self): mark = Mark() @@ -100,6 +117,7 @@ class TestMark(object): assert g.world.args[0] is f assert g.world.kwargs["omega"] == "hello" + @ignore_markinfo def test_pytest_mark_reuse(self): mark = Mark() @@ -109,86 +127,119 @@ class TestMark(object): w = mark.some w("hello", reason="123")(f) assert f.some.args[0] == "hello" - assert f.some.kwargs['reason'] == "123" + assert f.some.kwargs["reason"] == "123" def g(): pass w("world", reason2="456")(g) assert g.some.args[0] == "world" - assert 'reason' not in g.some.kwargs - assert g.some.kwargs['reason2'] == "456" + assert "reason" not in g.some.kwargs + assert g.some.kwargs["reason2"] == "456" def test_marked_class_run_twice(testdir, request): """Test fails file is run twice that contains marked class. See issue#683. """ - py_file = testdir.makepyfile(""" + py_file = testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('abc', [1, 2, 3]) class Test1(object): def test_1(self, abc): assert abc in [1, 2, 3] - """) + """ + ) file_name = os.path.basename(py_file.strpath) rec = testdir.inline_run(file_name, file_name) rec.assertoutcome(passed=6) def test_ini_markers(testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] markers = a1: this is a webtest marker a2: this is a smoke marker - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_markers(pytestconfig): markers = pytestconfig.getini("markers") print (markers) assert len(markers) >= 2 assert markers[0].startswith("a1:") assert markers[1].startswith("a2:") - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(passed=1) def test_markers_option(testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] markers = a1: this is a webtest marker a1some: another marker - """) - result = testdir.runpytest("--markers", ) - result.stdout.fnmatch_lines([ - "*a1*this is a webtest*", - "*a1some*another marker", - ]) + nodescription + """ + ) + result = testdir.runpytest("--markers") + result.stdout.fnmatch_lines( + ["*a1*this is a webtest*", "*a1some*another marker", "*nodescription*"] + ) def test_ini_markers_whitespace(testdir): - testdir.makeini(""" + testdir.makeini( + """ [pytest] markers = a1 : this is a whitespace marker - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest @pytest.mark.a1 def test_markers(): assert True - """) + """ + ) rec = testdir.inline_run("--strict", "-m", "a1") rec.assertoutcome(passed=1) +def test_marker_without_description(testdir): + testdir.makefile( + ".cfg", + setup=""" + [tool:pytest] + markers=slow + """, + ) + testdir.makeconftest( + """ + import pytest + pytest.mark.xfail('FAIL') + """ + ) + ftdir = testdir.mkdir("ft1_dummy") + testdir.tmpdir.join("conftest.py").move(ftdir.join("conftest.py")) + rec = testdir.runpytest_subprocess("--strict") + rec.assert_outcomes() + + def test_markers_option_with_plugin_in_current_dir(testdir): testdir.makeconftest('pytest_plugins = "flip_flop"') - testdir.makepyfile(flip_flop="""\ + testdir.makepyfile( + flip_flop="""\ def pytest_configure(config): config.addinivalue_line("markers", "flip:flop") @@ -197,51 +248,60 @@ def test_markers_option_with_plugin_in_current_dir(testdir): mark = metafunc.function.flipper except AttributeError: return - metafunc.parametrize("x", (10, 20))""") - testdir.makepyfile("""\ + metafunc.parametrize("x", (10, 20))""" + ) + testdir.makepyfile( + """\ import pytest @pytest.mark.flipper def test_example(x): - assert x""") + assert x""" + ) result = testdir.runpytest("--markers") result.stdout.fnmatch_lines(["*flip*flop*"]) def test_mark_on_pseudo_function(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.r(lambda x: 0/0) def test_hello(): pass - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_strict_prohibits_unregistered_markers(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.unregisteredmark def test_hello(): pass - """) + """ + ) result = testdir.runpytest("--strict") assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*unregisteredmark*not*registered*", - ]) + result.stdout.fnmatch_lines(["*unregisteredmark*not*registered*"]) -@pytest.mark.parametrize("spec", [ - ("xyz", ("test_one",)), - ("xyz and xyz2", ()), - ("xyz2", ("test_two",)), - ("xyz or xyz2", ("test_one", "test_two"),) -]) +@pytest.mark.parametrize( + "spec", + [ + ("xyz", ("test_one",)), + ("xyz and xyz2", ()), + ("xyz2", ("test_two",)), + ("xyz or xyz2", ("test_one", "test_two")), + ], +) def test_mark_option(spec, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.xyz def test_one(): @@ -249,7 +309,8 @@ def test_mark_option(spec, testdir): @pytest.mark.xyz2 def test_two(): pass - """) + """ + ) opt, passed_result = spec rec = testdir.inline_run("-m", opt) passed, skipped, fail = rec.listoutcomes() @@ -258,24 +319,27 @@ def test_mark_option(spec, testdir): assert list(passed) == list(passed_result) -@pytest.mark.parametrize("spec", [ - ("interface", ("test_interface",)), - ("not interface", ("test_nointer",)), -]) +@pytest.mark.parametrize( + "spec", [("interface", ("test_interface",)), ("not interface", ("test_nointer",))] +) def test_mark_option_custom(spec, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_collection_modifyitems(items): for item in items: if "interface" in item.nodeid: - item.keywords["interface"] = pytest.mark.interface - """) - testdir.makepyfile(""" + item.add_marker(pytest.mark.interface) + """ + ) + testdir.makepyfile( + """ def test_interface(): pass def test_nointer(): pass - """) + """ + ) opt, passed_result = spec rec = testdir.inline_run("-m", opt) passed, skipped, fail = rec.listoutcomes() @@ -284,21 +348,26 @@ def test_mark_option_custom(spec, testdir): assert list(passed) == list(passed_result) -@pytest.mark.parametrize("spec", [ - ("interface", ("test_interface",)), - ("not interface", ("test_nointer", "test_pass")), - ("pass", ("test_pass",)), - ("not pass", ("test_interface", "test_nointer")), -]) +@pytest.mark.parametrize( + "spec", + [ + ("interface", ("test_interface",)), + ("not interface", ("test_nointer", "test_pass")), + ("pass", ("test_pass",)), + ("not pass", ("test_interface", "test_nointer")), + ], +) def test_keyword_option_custom(spec, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_interface(): pass def test_nointer(): pass def test_pass(): pass - """) + """ + ) opt, passed_result = spec rec = testdir.inline_run("-k", opt) passed, skipped, fail = rec.listoutcomes() @@ -307,18 +376,23 @@ def test_keyword_option_custom(spec, testdir): assert list(passed) == list(passed_result) -@pytest.mark.parametrize("spec", [ - ("None", ("test_func[None]",)), - ("1.3", ("test_func[1.3]",)), - ("2-3", ("test_func[2-3]",)) -]) +@pytest.mark.parametrize( + "spec", + [ + ("None", ("test_func[None]",)), + ("1.3", ("test_func[1.3]",)), + ("2-3", ("test_func[2-3]",)), + ], +) def test_keyword_option_parametrize(spec, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) def test_func(arg): pass - """) + """ + ) opt, passed_result = spec rec = testdir.inline_run("-k", opt) passed, skipped, fail = rec.listoutcomes() @@ -327,16 +401,41 @@ def test_keyword_option_parametrize(spec, testdir): assert list(passed) == list(passed_result) +@pytest.mark.parametrize( + "spec", + [ + ( + "foo or import", + "ERROR: Python keyword 'import' not accepted in expressions passed to '-k'", + ), + ("foo or", "ERROR: Wrong expression passed to '-k': foo or"), + ], +) +def test_keyword_option_wrong_arguments(spec, testdir, capsys): + testdir.makepyfile( + """ + def test_func(arg): + pass + """ + ) + opt, expected_result = spec + testdir.inline_run("-k", opt) + out = capsys.readouterr().err + assert expected_result in out + + def test_parametrized_collected_from_command_line(testdir): """Parametrized test not collected if test named specified in command line issue#649. """ - py_file = testdir.makepyfile(""" + py_file = testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) def test_func(arg): pass - """) + """ + ) file_name = os.path.basename(py_file.strpath) rec = testdir.inline_run(file_name + "::" + "test_func") rec.assertoutcome(passed=3) @@ -344,24 +443,29 @@ def test_parametrized_collected_from_command_line(testdir): def test_parametrized_collect_with_wrong_args(testdir): """Test collect parametrized func with wrong number of args.""" - py_file = testdir.makepyfile(""" + py_file = testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('foo, bar', [(1, 2, 3)]) def test_func(foo, bar): pass - """) + """ + ) result = testdir.runpytest(py_file) - result.stdout.fnmatch_lines([ - 'E ValueError: In "parametrize" the number of values ((1, 2, 3)) ' - 'must be equal to the number of names ([\'foo\', \'bar\'])' - ]) + result.stdout.fnmatch_lines( + [ + 'E ValueError: In "parametrize" the number of values ((1, 2, 3)) ' + "must be equal to the number of names (['foo', 'bar'])" + ] + ) def test_parametrized_with_kwargs(testdir): """Test collect parametrized func with wrong number of args.""" - py_file = testdir.makepyfile(""" + py_file = testdir.makepyfile( + """ import pytest @pytest.fixture(params=[1,2]) @@ -371,72 +475,84 @@ def test_parametrized_with_kwargs(testdir): @pytest.mark.parametrize(argnames='b', argvalues=[1, 2]) def test_func(a, b): pass - """) + """ + ) result = testdir.runpytest(py_file) - assert(result.ret == 0) + assert result.ret == 0 class TestFunctional(object): def test_mark_per_function(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.hello def test_hello(): assert hasattr(test_hello, 'hello') - """) + """ + ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*1 passed*"]) def test_mark_per_module(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest pytestmark = pytest.mark.hello def test_func(): pass - """) + """ + ) keywords = item.keywords - assert 'hello' in keywords + assert "hello" in keywords def test_marklist_per_class(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest class TestClass(object): pytestmark = [pytest.mark.hello, pytest.mark.world] def test_func(self): assert TestClass.test_func.hello assert TestClass.test_func.world - """) + """ + ) keywords = item.keywords - assert 'hello' in keywords + assert "hello" in keywords def test_marklist_per_module(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest pytestmark = [pytest.mark.hello, pytest.mark.world] class TestClass(object): def test_func(self): assert TestClass.test_func.hello assert TestClass.test_func.world - """) + """ + ) keywords = item.keywords - assert 'hello' in keywords - assert 'world' in keywords + assert "hello" in keywords + assert "world" in keywords def test_mark_per_class_decorator(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.hello class TestClass(object): def test_func(self): assert TestClass.test_func.hello - """) + """ + ) keywords = item.keywords - assert 'hello' in keywords + assert "hello" in keywords def test_mark_per_class_decorator_plus_existing_dec(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.hello class TestClass(object): @@ -444,13 +560,16 @@ class TestFunctional(object): def test_func(self): assert TestClass.test_func.hello assert TestClass.test_func.world - """) + """ + ) keywords = item.keywords - assert 'hello' in keywords - assert 'world' in keywords + assert "hello" in keywords + assert "world" in keywords + @ignore_markinfo def test_merging_markers(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest pytestmark = pytest.mark.hello("pos1", x=1, y=2) class TestClass(object): @@ -459,25 +578,26 @@ class TestFunctional(object): @pytest.mark.hello("pos0", z=4) def test_func(self): pass - """) + """ + ) items, rec = testdir.inline_genitems(p) item, = items keywords = item.keywords - marker = keywords['hello'] + marker = keywords["hello"] assert marker.args == ("pos0", "pos1") - assert marker.kwargs == {'x': 1, 'y': 2, 'z': 4} + assert marker.kwargs == {"x": 1, "y": 2, "z": 4} # test the new __iter__ interface values = list(marker) assert len(values) == 3 assert values[0].args == ("pos0",) assert values[1].args == () - assert values[2].args == ("pos1", ) + assert values[2].args == ("pos1",) - @pytest.mark.xfail(reason='unfixed') def test_merging_markers_deep(self, testdir): # issue 199 - propagate markers into nested classes - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest class TestA(object): pytestmark = pytest.mark.a @@ -487,14 +607,16 @@ class TestFunctional(object): # this one didnt get marked def test_d(self): assert True - """) + """ + ) items, rec = testdir.inline_genitems(p) for item in items: print(item, item.keywords) - assert 'a' in item.keywords + assert [x for x in item.iter_markers() if x.name == "a"] def test_mark_decorator_subclass_does_not_propagate_to_base(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.a @@ -506,14 +628,15 @@ class TestFunctional(object): class Test2(Base): def test_bar(self): pass - """) + """ + ) items, rec = testdir.inline_genitems(p) - self.assert_markers(items, test_foo=('a', 'b'), test_bar=('a',)) + self.assert_markers(items, test_foo=("a", "b"), test_bar=("a",)) @pytest.mark.issue568 - @pytest.mark.xfail(reason="markers smear on methods of base classes") def test_mark_should_not_pass_to_siebling_class(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest class TestBase(object): @@ -528,14 +651,24 @@ class TestFunctional(object): class TestOtherSub(TestBase): pass - """) + """ + ) items, rec = testdir.inline_genitems(p) base_item, sub_item, sub_item_other = items - assert not hasattr(base_item.obj, 'b') - assert not hasattr(sub_item_other.obj, 'b') + print(items, [x.nodeid for x in items]) + # legacy api smears + assert hasattr(base_item.obj, "b") + assert hasattr(sub_item_other.obj, "b") + assert hasattr(sub_item.obj, "b") + + # new api seregates + assert not list(base_item.iter_markers(name="b")) + assert not list(sub_item_other.iter_markers(name="b")) + assert list(sub_item.iter_markers(name="b")) def test_mark_decorator_baseclasses_merged(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.a @@ -551,61 +684,91 @@ class TestFunctional(object): class Test2(Base2): @pytest.mark.d def test_bar(self): pass - """) + """ + ) items, rec = testdir.inline_genitems(p) - self.assert_markers(items, test_foo=('a', 'b', 'c'), - test_bar=('a', 'b', 'd')) + self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d")) + + def test_mark_closest(self, testdir): + p = testdir.makepyfile( + """ + import pytest + + @pytest.mark.c(location="class") + class Test: + @pytest.mark.c(location="function") + def test_has_own(): + pass + + def test_has_inherited(): + pass + + """ + ) + items, rec = testdir.inline_genitems(p) + has_own, has_inherited = items + assert has_own.get_closest_marker("c").kwargs == {"location": "function"} + assert has_inherited.get_closest_marker("c").kwargs == {"location": "class"} + assert has_own.get_closest_marker("missing") is None def test_mark_with_wrong_marker(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ import pytest class pytestmark(object): pass def test_func(): pass - """) + """ + ) values = reprec.getfailedcollections() assert len(values) == 1 assert "TypeError" in str(values[0].longrepr) def test_mark_dynamically_in_funcarg(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest @pytest.fixture def arg(request): request.applymarker(pytest.mark.hello) def pytest_terminal_summary(terminalreporter): values = terminalreporter.stats['passed'] - terminalreporter.writer.line("keyword: %s" % values[0].keywords) - """) - testdir.makepyfile(""" + terminalreporter._tw.line("keyword: %s" % values[0].keywords) + """ + ) + testdir.makepyfile( + """ def test_func(arg): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "keyword: *hello*" - ]) + result.stdout.fnmatch_lines(["keyword: *hello*"]) + @ignore_markinfo def test_merging_markers_two_functions(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.hello("pos1", z=4) @pytest.mark.hello("pos0", z=3) def test_func(): pass - """) + """ + ) items, rec = testdir.inline_genitems(p) item, = items keywords = item.keywords - marker = keywords['hello'] + marker = keywords["hello"] values = list(marker) assert len(values) == 2 assert values[0].args == ("pos0",) assert values[1].args == ("pos1",) def test_no_marker_match_on_unmarked_names(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.shouldmatch def test_marked(): @@ -613,7 +776,8 @@ class TestFunctional(object): def test_unmarked(): assert 1 - """) + """ + ) reprec = testdir.inline_run("-m", "test_unmarked", p) passed, skipped, failed = reprec.listoutcomes() assert len(passed) + len(skipped) + len(failed) == 0 @@ -622,7 +786,8 @@ class TestFunctional(object): assert len(deselected_tests) == 2 def test_keywords_at_node_level(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope="session", autouse=True) def some(request): @@ -637,12 +802,15 @@ class TestFunctional(object): @pytest.mark.world def test_function(): pass - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) + @ignore_markinfo def test_keyword_added_for_session(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_collection_modifyitems(session): session.add_marker("mark1") @@ -650,8 +818,10 @@ class TestFunctional(object): session.add_marker(pytest.mark.mark3) pytest.raises(ValueError, lambda: session.add_marker(10)) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_some(request): assert "mark1" in request.keywords assert "mark2" in request.keywords @@ -661,7 +831,8 @@ class TestFunctional(object): assert marker.name == "mark1" assert marker.args == () assert marker.kwargs == {} - """) + """ + ) reprec = testdir.inline_run("-m", "mark1") reprec.assertoutcome(passed=1) @@ -673,17 +844,20 @@ class TestFunctional(object): to other modules. """ from _pytest.mark import MarkInfo - items = dict((x.name, x) for x in items) + + items = {x.name: x for x in items} for name, expected_markers in expected.items(): markers = items[name].keywords._markers - marker_names = set([name for (name, v) in markers.items() - if isinstance(v, MarkInfo)]) + marker_names = { + name for (name, v) in markers.items() if isinstance(v, MarkInfo) + } assert marker_names == set(expected_markers) - @pytest.mark.xfail(reason='callspec2.setmulti misuses keywords') @pytest.mark.issue1540 + @pytest.mark.filterwarnings("ignore") def test_mark_from_parameters(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest pytestmark = pytest.mark.skipif(True, reason='skip all') @@ -699,7 +873,8 @@ class TestFunctional(object): def test_1(parameter): assert True - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=1) @@ -707,37 +882,50 @@ class TestFunctional(object): class TestKeywordSelection(object): def test_select_simple(self, testdir): - file_test = testdir.makepyfile(""" + file_test = testdir.makepyfile( + """ def test_one(): assert 0 class TestClass(object): def test_method_one(self): assert 42 == 43 - """) + """ + ) def check(keyword, name): reprec = testdir.inline_run("-s", "-k", keyword, file_test) passed, skipped, failed = reprec.listoutcomes() assert len(failed) == 1 assert failed[0].nodeid.split("::")[-1] == name - assert len(reprec.getcalls('pytest_deselected')) == 1 + assert len(reprec.getcalls("pytest_deselected")) == 1 - for keyword in ['test_one', 'est_on']: - check(keyword, 'test_one') - check('TestClass and test', 'test_method_one') + for keyword in ["test_one", "est_on"]: + check(keyword, "test_one") + check("TestClass and test", "test_method_one") - @pytest.mark.parametrize("keyword", [ - 'xxx', 'xxx and test_2', 'TestClass', 'xxx and not test_1', - 'TestClass and test_2', 'xxx and TestClass and test_2']) + @pytest.mark.parametrize( + "keyword", + [ + "xxx", + "xxx and test_2", + "TestClass", + "xxx and not test_1", + "TestClass and test_2", + "xxx and TestClass and test_2", + ], + ) def test_select_extra_keywords(self, testdir, keyword): - p = testdir.makepyfile(test_select=""" + p = testdir.makepyfile( + test_select=""" def test_1(): pass class TestClass(object): def test_2(self): pass - """) - testdir.makepyfile(conftest=""" + """ + ) + testdir.makepyfile( + conftest=""" import pytest @pytest.hookimpl(hookwrapper=True) def pytest_pycollect_makeitem(name): @@ -745,22 +933,25 @@ class TestKeywordSelection(object): if name == "TestClass": item = outcome.get_result() item.extra_keyword_matches.add("xxx") - """) - reprec = testdir.inline_run(p.dirpath(), '-s', '-k', keyword) + """ + ) + reprec = testdir.inline_run(p.dirpath(), "-s", "-k", keyword) print("keyword", repr(keyword)) passed, skipped, failed = reprec.listoutcomes() assert len(passed) == 1 assert passed[0].nodeid.endswith("test_2") dlist = reprec.getcalls("pytest_deselected") assert len(dlist) == 1 - assert dlist[0].items[0].name == 'test_1' + assert dlist[0].items[0].name == "test_1" def test_select_starton(self, testdir): - threepass = testdir.makepyfile(test_threepass=""" + threepass = testdir.makepyfile( + test_threepass=""" def test_one(): assert 1 def test_two(): assert 1 def test_three(): assert 1 - """) + """ + ) reprec = testdir.inline_run("-k", "test_two:", threepass) passed, skipped, failed = reprec.listoutcomes() assert len(passed) == 2 @@ -771,22 +962,26 @@ class TestKeywordSelection(object): assert item.name == "test_one" def test_keyword_extra(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_one(): assert 0 test_one.mykeyword = True - """) + """ + ) reprec = testdir.inline_run("-k", "mykeyword", p) passed, skipped, failed = reprec.countoutcomes() assert failed == 1 @pytest.mark.xfail def test_keyword_extra_dash(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_one(): assert 0 test_one.mykeyword = True - """) + """ + ) # with argparse the argument to an option cannot # start with '-' reprec = testdir.inline_run("-k", "-mykeyword", p) @@ -798,9 +993,11 @@ class TestKeywordSelection(object): no double underscored values, like '__dict__', and no instance values, like '()'. """ - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_one(): assert 1 - """) + """ + ) def assert_test_is_not_selected(keyword): reprec = testdir.inline_run("-k", keyword, p) @@ -814,15 +1011,22 @@ class TestKeywordSelection(object): assert_test_is_not_selected("()") -@pytest.mark.parametrize('argval, expected', [ - (pytest.mark.skip()((1, 2)), - ParameterSet(values=(1, 2), marks=[pytest.mark.skip], id=None)), - (pytest.mark.xfail(pytest.mark.skip()((1, 2))), - ParameterSet(values=(1, 2), - marks=[pytest.mark.xfail, pytest.mark.skip], id=None)), - -]) -@pytest.mark.filterwarnings('ignore') +@pytest.mark.parametrize( + "argval, expected", + [ + ( + pytest.mark.skip()((1, 2)), + ParameterSet(values=(1, 2), marks=[pytest.mark.skip], id=None), + ), + ( + pytest.mark.xfail(pytest.mark.skip()((1, 2))), + ParameterSet( + values=(1, 2), marks=[pytest.mark.xfail, pytest.mark.skip], id=None + ), + ), + ], +) +@pytest.mark.filterwarnings("ignore") def test_parameterset_extractfrom(argval, expected): extracted = ParameterSet.extract_from(argval) assert extracted == expected @@ -851,11 +1055,80 @@ def test_legacy_transfer(): class TestMarkDecorator(object): - @pytest.mark.parametrize('lhs, rhs, expected', [ - (pytest.mark.foo(), pytest.mark.foo(), True), - (pytest.mark.foo(), pytest.mark.bar(), False), - (pytest.mark.foo(), 'bar', False), - ('foo', pytest.mark.bar(), False) - ]) + @pytest.mark.parametrize( + "lhs, rhs, expected", + [ + (pytest.mark.foo(), pytest.mark.foo(), True), + (pytest.mark.foo(), pytest.mark.bar(), False), + (pytest.mark.foo(), "bar", False), + ("foo", pytest.mark.bar(), False), + ], + ) def test__eq__(self, lhs, rhs, expected): assert (lhs == rhs) == expected + + +@pytest.mark.parametrize("mark", [None, "", "skip", "xfail"]) +def test_parameterset_for_parametrize_marks(testdir, mark): + if mark is not None: + testdir.makeini("[pytest]\n{}={}".format(EMPTY_PARAMETERSET_OPTION, mark)) + + config = testdir.parseconfig() + from _pytest.mark import pytest_configure, get_empty_parameterset_mark + + pytest_configure(config) + result_mark = get_empty_parameterset_mark(config, ["a"], all) + if mark in (None, ""): + # normalize to the requested name + mark = "skip" + assert result_mark.name == mark + assert result_mark.kwargs["reason"].startswith("got empty parameter set ") + if mark == "xfail": + assert result_mark.kwargs.get("run") is False + + +def test_parameterset_for_parametrize_bad_markname(testdir): + with pytest.raises(pytest.UsageError): + test_parameterset_for_parametrize_marks(testdir, "bad") + + +def test_mark_expressions_no_smear(testdir): + testdir.makepyfile( + """ + import pytest + + class BaseTests(object): + def test_something(self): + pass + + @pytest.mark.FOO + class TestFooClass(BaseTests): + pass + + @pytest.mark.BAR + class TestBarClass(BaseTests): + pass + """ + ) + + reprec = testdir.inline_run("-m", "FOO") + passed, skipped, failed = reprec.countoutcomes() + dlist = reprec.getcalls("pytest_deselected") + assert passed == 1 + assert skipped == failed == 0 + deselected_tests = dlist[0].items + assert len(deselected_tests) == 1 + + # keywords smear - expected behaviour + reprec_keywords = testdir.inline_run("-k", "FOO") + passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes() + assert passed_k == 2 + assert skipped_k == failed_k == 0 + + +def test_addmarker_getmarker(): + node = Node("Test", config=mock.Mock(), session=mock.Mock(), nodeid="Test") + node.add_marker(pytest.mark.a(1)) + node.add_marker("b") + node.get_marker("a").combined + node.get_marker("b").combined diff --git a/third_party/python/pytest/testing/test_modimport.py b/third_party/python/pytest/testing/test_modimport.py index 2ab86bf7af10..f7b92a0b6f67 100644 --- a/third_party/python/pytest/testing/test_modimport.py +++ b/third_party/python/pytest/testing/test_modimport.py @@ -5,21 +5,25 @@ import pytest import _pytest MODSET = [ - x for x in py.path.local(_pytest.__file__).dirpath().visit('*.py') - if x.purebasename != '__init__' + x + for x in py.path.local(_pytest.__file__).dirpath().visit("*.py") + if x.purebasename != "__init__" ] -@pytest.mark.parametrize('modfile', MODSET, ids=lambda x: x.purebasename) +@pytest.mark.parametrize("modfile", MODSET, ids=lambda x: x.purebasename) def test_fileimport(modfile): # this test ensures all internal packages can import # without needing the pytest namespace being set # this is critical for the initialization of xdist - res = subprocess.call([ - sys.executable, - '-c', 'import sys, py; py.path.local(sys.argv[1]).pyimport()', - modfile.strpath, - ]) + res = subprocess.call( + [ + sys.executable, + "-c", + "import sys, py; py.path.local(sys.argv[1]).pyimport()", + modfile.strpath, + ] + ) if res: pytest.fail("command result %s" % res) diff --git a/third_party/python/pytest/testing/test_monkeypatch.py b/third_party/python/pytest/testing/test_monkeypatch.py index 4427908ab3bc..c298ce0d9290 100644 --- a/third_party/python/pytest/testing/test_monkeypatch.py +++ b/third_party/python/pytest/testing/test_monkeypatch.py @@ -17,20 +17,21 @@ def mp(): def test_setattr(): + class A(object): x = 1 monkeypatch = MonkeyPatch() pytest.raises(AttributeError, "monkeypatch.setattr(A, 'notexists', 2)") - monkeypatch.setattr(A, 'y', 2, raising=False) + monkeypatch.setattr(A, "y", 2, raising=False) assert A.y == 2 monkeypatch.undo() - assert not hasattr(A, 'y') + assert not hasattr(A, "y") monkeypatch = MonkeyPatch() - monkeypatch.setattr(A, 'x', 2) + monkeypatch.setattr(A, "x", 2) assert A.x == 2 - monkeypatch.setattr(A, 'x', 3) + monkeypatch.setattr(A, "x", 3) assert A.x == 3 monkeypatch.undo() assert A.x == 1 @@ -41,6 +42,7 @@ def test_setattr(): class TestSetattrWithImportPath(object): + def test_string_expression(self, monkeypatch): monkeypatch.setattr("os.path.abspath", lambda x: "hello2") assert os.path.abspath("123") == "hello2" @@ -48,11 +50,13 @@ class TestSetattrWithImportPath(object): def test_string_expression_class(self, monkeypatch): monkeypatch.setattr("_pytest.config.Config", 42) import _pytest + assert _pytest.config.Config == 42 def test_unicode_string(self, monkeypatch): monkeypatch.setattr("_pytest.config.Config", 42) import _pytest + assert _pytest.config.Config == 42 monkeypatch.delattr("_pytest.config.Config") @@ -60,16 +64,16 @@ class TestSetattrWithImportPath(object): pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None)) def test_unknown_import(self, monkeypatch): - pytest.raises(ImportError, - lambda: monkeypatch.setattr("unkn123.classx", None)) + pytest.raises(ImportError, lambda: monkeypatch.setattr("unkn123.classx", None)) def test_unknown_attr(self, monkeypatch): - pytest.raises(AttributeError, - lambda: monkeypatch.setattr("os.path.qweqwe", None)) + pytest.raises( + AttributeError, lambda: monkeypatch.setattr("os.path.qweqwe", None) + ) def test_unknown_attr_non_raising(self, monkeypatch): # https://github.com/pytest-dev/pytest/issues/746 - monkeypatch.setattr('os.path.qweqwe', 42, raising=False) + monkeypatch.setattr("os.path.qweqwe", 42, raising=False) assert os.path.qweqwe == 42 def test_delattr(self, monkeypatch): @@ -80,48 +84,49 @@ class TestSetattrWithImportPath(object): def test_delattr(): + class A(object): x = 1 monkeypatch = MonkeyPatch() - monkeypatch.delattr(A, 'x') - assert not hasattr(A, 'x') + monkeypatch.delattr(A, "x") + assert not hasattr(A, "x") monkeypatch.undo() assert A.x == 1 monkeypatch = MonkeyPatch() - monkeypatch.delattr(A, 'x') + monkeypatch.delattr(A, "x") pytest.raises(AttributeError, "monkeypatch.delattr(A, 'y')") - monkeypatch.delattr(A, 'y', raising=False) - monkeypatch.setattr(A, 'x', 5, raising=False) + monkeypatch.delattr(A, "y", raising=False) + monkeypatch.setattr(A, "x", 5, raising=False) assert A.x == 5 monkeypatch.undo() assert A.x == 1 def test_setitem(): - d = {'x': 1} + d = {"x": 1} monkeypatch = MonkeyPatch() - monkeypatch.setitem(d, 'x', 2) - monkeypatch.setitem(d, 'y', 1700) - monkeypatch.setitem(d, 'y', 1700) - assert d['x'] == 2 - assert d['y'] == 1700 - monkeypatch.setitem(d, 'x', 3) - assert d['x'] == 3 + monkeypatch.setitem(d, "x", 2) + monkeypatch.setitem(d, "y", 1700) + monkeypatch.setitem(d, "y", 1700) + assert d["x"] == 2 + assert d["y"] == 1700 + monkeypatch.setitem(d, "x", 3) + assert d["x"] == 3 monkeypatch.undo() - assert d['x'] == 1 - assert 'y' not in d - d['x'] = 5 + assert d["x"] == 1 + assert "y" not in d + d["x"] = 5 monkeypatch.undo() - assert d['x'] == 5 + assert d["x"] == 5 def test_setitem_deleted_meanwhile(): d = {} monkeypatch = MonkeyPatch() - monkeypatch.setitem(d, 'x', 2) - del d['x'] + monkeypatch.setitem(d, "x", 2) + del d["x"] monkeypatch.undo() assert not d @@ -132,7 +137,7 @@ def test_setenv_deleted_meanwhile(before): if before: os.environ[key] = "world" monkeypatch = MonkeyPatch() - monkeypatch.setenv(key, 'hello') + monkeypatch.setenv(key, "hello") del os.environ[key] monkeypatch.undo() if before: @@ -143,33 +148,34 @@ def test_setenv_deleted_meanwhile(before): def test_delitem(): - d = {'x': 1} + d = {"x": 1} monkeypatch = MonkeyPatch() - monkeypatch.delitem(d, 'x') - assert 'x' not in d - monkeypatch.delitem(d, 'y', raising=False) + monkeypatch.delitem(d, "x") + assert "x" not in d + monkeypatch.delitem(d, "y", raising=False) pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')") assert not d - monkeypatch.setitem(d, 'y', 1700) - assert d['y'] == 1700 - d['hello'] = 'world' - monkeypatch.setitem(d, 'x', 1500) - assert d['x'] == 1500 + monkeypatch.setitem(d, "y", 1700) + assert d["y"] == 1700 + d["hello"] = "world" + monkeypatch.setitem(d, "x", 1500) + assert d["x"] == 1500 monkeypatch.undo() - assert d == {'hello': 'world', 'x': 1} + assert d == {"hello": "world", "x": 1} def test_setenv(): monkeypatch = MonkeyPatch() - monkeypatch.setenv('XYZ123', 2) + monkeypatch.setenv("XYZ123", 2) import os - assert os.environ['XYZ123'] == "2" + + assert os.environ["XYZ123"] == "2" monkeypatch.undo() - assert 'XYZ123' not in os.environ + assert "XYZ123" not in os.environ def test_delenv(): - name = 'xyz1234' + name = "xyz1234" assert name not in os.environ monkeypatch = MonkeyPatch() pytest.raises(KeyError, "monkeypatch.delenv(%r, raising=True)" % name) @@ -191,28 +197,31 @@ def test_delenv(): def test_setenv_prepend(): import os + monkeypatch = MonkeyPatch() - monkeypatch.setenv('XYZ123', 2, prepend="-") - assert os.environ['XYZ123'] == "2" - monkeypatch.setenv('XYZ123', 3, prepend="-") - assert os.environ['XYZ123'] == "3-2" + monkeypatch.setenv("XYZ123", 2, prepend="-") + assert os.environ["XYZ123"] == "2" + monkeypatch.setenv("XYZ123", 3, prepend="-") + assert os.environ["XYZ123"] == "3-2" monkeypatch.undo() - assert 'XYZ123' not in os.environ + assert "XYZ123" not in os.environ def test_monkeypatch_plugin(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ def test_method(monkeypatch): assert monkeypatch.__class__.__name__ == "MonkeyPatch" - """) + """ + ) res = reprec.countoutcomes() assert tuple(res) == (1, 0, 0), res def test_syspath_prepend(mp): old = list(sys.path) - mp.syspath_prepend('world') - mp.syspath_prepend('hello') + mp.syspath_prepend("world") + mp.syspath_prepend("hello") assert sys.path[0] == "hello" assert sys.path[1] == "world" mp.undo() @@ -222,11 +231,11 @@ def test_syspath_prepend(mp): def test_syspath_prepend_double_undo(mp): - mp.syspath_prepend('hello world') + mp.syspath_prepend("hello world") mp.undo() - sys.path.append('more hello world') + sys.path.append("more hello world") mp.undo() - assert sys.path[-1] == 'more hello world' + assert sys.path[-1] == "more hello world" def test_chdir_with_path_local(mp, tmpdir): @@ -255,37 +264,54 @@ def test_chdir_double_undo(mp, tmpdir): def test_issue185_time_breaks(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import time def test_m(monkeypatch): def f(): raise Exception monkeypatch.setattr(time, "time", f) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *1 passed* - """) + """ + ) def test_importerror(testdir): p = testdir.mkpydir("package") - p.join("a.py").write(textwrap.dedent("""\ + p.join("a.py").write( + textwrap.dedent( + """\ import doesnotexist x = 1 - """)) - testdir.tmpdir.join("test_importerror.py").write(textwrap.dedent("""\ + """ + ) + ) + testdir.tmpdir.join("test_importerror.py").write( + textwrap.dedent( + """\ def test_importerror(monkeypatch): monkeypatch.setattr('package.a.x', 2) - """)) + """ + ) + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *import error in package.a: No module named {0}doesnotexist{0}* - """.format("'" if sys.version_info > (3, 0) else "")) + """.format( + "'" if sys.version_info > (3, 0) else "" + ) + ) class SampleNew(object): + @staticmethod def hello(): return True @@ -306,14 +332,15 @@ class SampleOldInherit(SampleOld): pass -@pytest.mark.parametrize('Sample', [ - SampleNew, SampleNewInherit, - SampleOld, SampleOldInherit, -], ids=['new', 'new-inherit', 'old', 'old-inherit']) +@pytest.mark.parametrize( + "Sample", + [SampleNew, SampleNewInherit, SampleOld, SampleOldInherit], + ids=["new", "new-inherit", "old", "old-inherit"], +) def test_issue156_undo_staticmethod(Sample): monkeypatch = MonkeyPatch() - monkeypatch.setattr(Sample, 'hello', None) + monkeypatch.setattr(Sample, "hello", None) assert Sample.hello is None monkeypatch.undo() @@ -321,9 +348,21 @@ def test_issue156_undo_staticmethod(Sample): def test_issue1338_name_resolving(): - pytest.importorskip('requests') + pytest.importorskip("requests") monkeypatch = MonkeyPatch() try: - monkeypatch.delattr('requests.sessions.Session.request') + monkeypatch.delattr("requests.sessions.Session.request") finally: monkeypatch.undo() + + +def test_context(): + monkeypatch = MonkeyPatch() + + import functools + import inspect + + with monkeypatch.context() as m: + m.setattr(functools, "partial", 3) + assert not inspect.isclass(functools.partial) + assert inspect.isclass(functools.partial) diff --git a/third_party/python/pytest/testing/test_nodes.py b/third_party/python/pytest/testing/test_nodes.py index 6f4540f99b96..eee3ac8e9e4c 100644 --- a/third_party/python/pytest/testing/test_nodes.py +++ b/third_party/python/pytest/testing/test_nodes.py @@ -3,16 +3,19 @@ import pytest from _pytest import nodes -@pytest.mark.parametrize("baseid, nodeid, expected", ( - ('', '', True), - ('', 'foo', True), - ('', 'foo/bar', True), - ('', 'foo/bar::TestBaz::()', True), - ('foo', 'food', False), - ('foo/bar::TestBaz::()', 'foo/bar', False), - ('foo/bar::TestBaz::()', 'foo/bar::TestBop::()', False), - ('foo/bar', 'foo/bar::TestBop::()', True), -)) +@pytest.mark.parametrize( + "baseid, nodeid, expected", + ( + ("", "", True), + ("", "foo", True), + ("", "foo/bar", True), + ("", "foo/bar::TestBaz::()", True), + ("foo", "food", False), + ("foo/bar::TestBaz::()", "foo/bar", False), + ("foo/bar::TestBaz::()", "foo/bar::TestBop::()", False), + ("foo/bar", "foo/bar::TestBop::()", True), + ), +) def test_ischildnode(baseid, nodeid, expected): result = nodes.ischildnode(baseid, nodeid) assert result is expected diff --git a/third_party/python/pytest/testing/test_nose.py b/third_party/python/pytest/testing/test_nose.py index df3e1a94b05b..abe7323754ed 100644 --- a/third_party/python/pytest/testing/test_nose.py +++ b/third_party/python/pytest/testing/test_nose.py @@ -7,7 +7,8 @@ def setup_module(mod): def test_nose_setup(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ values = [] from nose.tools import with_setup @@ -20,16 +21,19 @@ def test_nose_setup(testdir): test_hello.setup = lambda: values.append(1) test_hello.teardown = lambda: values.append(2) - """) - result = testdir.runpytest(p, '-p', 'nose') + """ + ) + result = testdir.runpytest(p, "-p", "nose") result.assert_outcomes(passed=2) def test_setup_func_with_setup_decorator(): from _pytest.nose import call_optional + values = [] class A(object): + @pytest.fixture(autouse=True) def f(self): values.append(1) @@ -48,7 +52,8 @@ def test_setup_func_not_callable(): def test_nose_setup_func(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from nose.tools import with_setup values = [] @@ -70,13 +75,15 @@ def test_nose_setup_func(testdir): print (values) assert values == [1,2] - """) - result = testdir.runpytest(p, '-p', 'nose') + """ + ) + result = testdir.runpytest(p, "-p", "nose") result.assert_outcomes(passed=2) def test_nose_setup_func_failure(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from nose.tools import with_setup values = [] @@ -92,15 +99,15 @@ def test_nose_setup_func_failure(testdir): print (values) assert values == [1,2] - """) - result = testdir.runpytest(p, '-p', 'nose') - result.stdout.fnmatch_lines([ - "*TypeError: ()*" - ]) + """ + ) + result = testdir.runpytest(p, "-p", "nose") + result.stdout.fnmatch_lines(["*TypeError: ()*"]) def test_nose_setup_func_failure_2(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ values = [] my_setup = 1 @@ -111,14 +118,16 @@ def test_nose_setup_func_failure_2(testdir): test_hello.setup = my_setup test_hello.teardown = my_teardown - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_nose_setup_partial(testdir): pytest.importorskip("functools") - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from functools import partial values = [] @@ -144,15 +153,15 @@ def test_nose_setup_partial(testdir): test_hello.setup = my_setup_partial test_hello.teardown = my_teardown_partial - """) - result = testdir.runpytest(p, '-p', 'nose') - result.stdout.fnmatch_lines([ - "*2 passed*" - ]) + """ + ) + result = testdir.runpytest(p, "-p", "nose") + result.stdout.fnmatch_lines(["*2 passed*"]) def test_nose_test_generator_fixtures(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ # taken from nose-0.11.1 unit_tests/test_generator_fixtures.py from nose.tools import eq_ called = [] @@ -211,15 +220,15 @@ def test_nose_test_generator_fixtures(testdir): # expect.append('teardown') #expect.append('setup') eq_(self.called, expect) - """) - result = testdir.runpytest(p, '-p', 'nose') - result.stdout.fnmatch_lines([ - "*10 passed*" - ]) + """ + ) + result = testdir.runpytest(p, "-p", "nose") + result.stdout.fnmatch_lines(["*10 passed*"]) def test_module_level_setup(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from nose.tools import with_setup items = {} @@ -242,15 +251,15 @@ def test_module_level_setup(testdir): def test_local_setup(): assert items[2] == 2 assert 1 not in items - """) - result = testdir.runpytest('-p', 'nose') - result.stdout.fnmatch_lines([ - "*2 passed*", - ]) + """ + ) + result = testdir.runpytest("-p", "nose") + result.stdout.fnmatch_lines(["*2 passed*"]) def test_nose_style_setup_teardown(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ values = [] def setup_module(): @@ -264,15 +273,15 @@ def test_nose_style_setup_teardown(testdir): def test_world(): assert values == [1] - """) - result = testdir.runpytest('-p', 'nose') - result.stdout.fnmatch_lines([ - "*2 passed*", - ]) + """ + ) + result = testdir.runpytest("-p", "nose") + result.stdout.fnmatch_lines(["*2 passed*"]) def test_nose_setup_ordering(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def setup_module(mod): mod.visited = True @@ -281,17 +290,17 @@ def test_nose_setup_ordering(testdir): assert visited def test_first(self): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_apiwrapper_problem_issue260(testdir): - # this would end up trying a call a optional teardown on the class + # this would end up trying a call an optional teardown on the class # for plain unittests we dont want nose behaviour - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest class TestCase(unittest.TestCase): def setup(self): @@ -306,7 +315,8 @@ def test_apiwrapper_problem_issue260(testdir): print('teardown') def test_fun(self): pass - """) + """ + ) result = testdir.runpytest() result.assert_outcomes(passed=1) @@ -314,7 +324,8 @@ def test_apiwrapper_problem_issue260(testdir): def test_setup_teardown_linking_issue265(testdir): # we accidentally didnt integrate nose setupstate with normal setupstate # this test ensures that won't happen again - testdir.makepyfile(''' + testdir.makepyfile( + ''' import pytest class TestGeneric(object): @@ -332,51 +343,60 @@ def test_setup_teardown_linking_issue265(testdir): def teardown(self): """Undoes the setup.""" raise Exception("should not call teardown for skipped tests") - ''') + ''' + ) reprec = testdir.runpytest() reprec.assert_outcomes(passed=1, skipped=1) def test_SkipTest_during_collection(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import nose raise nose.SkipTest("during collection") def test_failing(): assert False - """) + """ + ) result = testdir.runpytest(p) result.assert_outcomes(skipped=1) def test_SkipTest_in_test(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import nose def test_skipping(): raise nose.SkipTest("in test") - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=1) def test_istest_function_decorator(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import nose.tools @nose.tools.istest def not_test_prefix(): pass - """) + """ + ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_nottest_function_decorator(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import nose.tools @nose.tools.nottest def test_prefix(): pass - """) + """ + ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") @@ -384,25 +404,29 @@ def test_nottest_function_decorator(testdir): def test_istest_class_decorator(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import nose.tools @nose.tools.istest class NotTestPrefix(object): def test_method(self): pass - """) + """ + ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_nottest_class_decorator(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import nose.tools @nose.tools.nottest class TestPrefix(object): def test_method(self): pass - """) + """ + ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") diff --git a/third_party/python/pytest/testing/test_parseopt.py b/third_party/python/pytest/testing/test_parseopt.py index 02fdf0adaac4..79076cafd810 100644 --- a/third_party/python/pytest/testing/test_parseopt.py +++ b/third_party/python/pytest/testing/test_parseopt.py @@ -1,9 +1,10 @@ from __future__ import absolute_import, division, print_function +import argparse import sys import os import py import pytest -from _pytest import config as parseopt +from _pytest.config import argparsing as parseopt @pytest.fixture @@ -12,6 +13,7 @@ def parser(): class TestParser(object): + def test_no_help_by_default(self, capsys): parser = parseopt.Parser(usage="xyz") pytest.raises(SystemExit, lambda: parser.parse(["-h"])) @@ -22,41 +24,43 @@ class TestParser(object): with pytest.raises(parseopt.ArgumentError): # need a short or long option argument = parseopt.Argument() - argument = parseopt.Argument('-t') - assert argument._short_opts == ['-t'] + argument = parseopt.Argument("-t") + assert argument._short_opts == ["-t"] assert argument._long_opts == [] - assert argument.dest == 't' - argument = parseopt.Argument('-t', '--test') - assert argument._short_opts == ['-t'] - assert argument._long_opts == ['--test'] - assert argument.dest == 'test' - argument = parseopt.Argument('-t', '--test', dest='abc') - assert argument.dest == 'abc' - assert str(argument) == ( - "Argument(_short_opts: ['-t'], _long_opts: ['--test'], dest: 'abc')" + assert argument.dest == "t" + argument = parseopt.Argument("-t", "--test") + assert argument._short_opts == ["-t"] + assert argument._long_opts == ["--test"] + assert argument.dest == "test" + argument = parseopt.Argument("-t", "--test", dest="abc") + assert argument.dest == "abc" + assert ( + str(argument) + == ("Argument(_short_opts: ['-t'], _long_opts: ['--test'], dest: 'abc')") ) def test_argument_type(self): - argument = parseopt.Argument('-t', dest='abc', type=int) + argument = parseopt.Argument("-t", dest="abc", type=int) assert argument.type is int - argument = parseopt.Argument('-t', dest='abc', type=str) + argument = parseopt.Argument("-t", dest="abc", type=str) assert argument.type is str - argument = parseopt.Argument('-t', dest='abc', type=float) + argument = parseopt.Argument("-t", dest="abc", type=float) assert argument.type is float with pytest.warns(DeprecationWarning): with pytest.raises(KeyError): - argument = parseopt.Argument('-t', dest='abc', type='choice') - argument = parseopt.Argument('-t', dest='abc', type=str, - choices=['red', 'blue']) + argument = parseopt.Argument("-t", dest="abc", type="choice") + argument = parseopt.Argument( + "-t", dest="abc", type=str, choices=["red", "blue"] + ) assert argument.type is str def test_argument_processopt(self): - argument = parseopt.Argument('-t', type=int) + argument = parseopt.Argument("-t", type=int) argument.default = 42 - argument.dest = 'abc' + argument.dest = "abc" res = argument.attrs() - assert res['default'] == 42 - assert res['dest'] == 'abc' + assert res["default"] == 42 + assert res["dest"] == "abc" def test_group_add_and_get(self, parser): group = parser.getgroup("hello", description="desc") @@ -89,13 +93,16 @@ class TestParser(object): group.addoption("--option1", "--option-1", action="store_true") with pytest.raises(ValueError) as err: group.addoption("--option1", "--option-one", action="store_true") - assert str(set(["--option1"])) in str(err.value) + assert str({"--option1"}) in str(err.value) def test_group_shortopt_lowercase(self, parser): group = parser.getgroup("hello") - pytest.raises(ValueError, """ + pytest.raises( + ValueError, + """ group.addoption("-x", action="store_true") - """) + """, + ) assert len(group.options) == 0 group._addoption("-x", action="store_true") assert len(group.options) == 1 @@ -108,7 +115,7 @@ class TestParser(object): def test_parse(self, parser): parser.addoption("--hello", dest="hello", action="store") - args = parser.parse(['--hello', 'world']) + args = parser.parse(["--hello", "world"]) assert args.hello == "world" assert not getattr(args, parseopt.FILE_OR_DIR) @@ -121,15 +128,16 @@ class TestParser(object): parser.addoption("--hello", action="store_true") ns = parser.parse_known_args(["x", "--y", "--hello", "this"]) assert ns.hello - assert ns.file_or_dir == ['x'] + assert ns.file_or_dir == ["x"] def test_parse_known_and_unknown_args(self, parser): parser.addoption("--hello", action="store_true") - ns, unknown = parser.parse_known_and_unknown_args(["x", "--y", - "--hello", "this"]) + ns, unknown = parser.parse_known_and_unknown_args( + ["x", "--y", "--hello", "this"] + ) assert ns.hello - assert ns.file_or_dir == ['x'] - assert unknown == ['--y', 'this'] + assert ns.file_or_dir == ["x"] + assert unknown == ["--y", "this"] def test_parse_will_set_default(self, parser): parser.addoption("--hello", dest="hello", default="x", action="store") @@ -147,38 +155,40 @@ class TestParser(object): pass option = A() - args = parser.parse_setoption(['--hello', 'world'], option) + args = parser.parse_setoption(["--hello", "world"], option) assert option.hello == "world" assert option.world == 42 assert not args def test_parse_special_destination(self, parser): parser.addoption("--ultimate-answer", type=int) - args = parser.parse(['--ultimate-answer', '42']) + args = parser.parse(["--ultimate-answer", "42"]) assert args.ultimate_answer == 42 def test_parse_split_positional_arguments(self, parser): - parser.addoption("-R", action='store_true') - parser.addoption("-S", action='store_false') - args = parser.parse(['-R', '4', '2', '-S']) - assert getattr(args, parseopt.FILE_OR_DIR) == ['4', '2'] - args = parser.parse(['-R', '-S', '4', '2', '-R']) - assert getattr(args, parseopt.FILE_OR_DIR) == ['4', '2'] + parser.addoption("-R", action="store_true") + parser.addoption("-S", action="store_false") + args = parser.parse(["-R", "4", "2", "-S"]) + assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] + args = parser.parse(["-R", "-S", "4", "2", "-R"]) + assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] assert args.R is True assert args.S is False - args = parser.parse(['-R', '4', '-S', '2']) - assert getattr(args, parseopt.FILE_OR_DIR) == ['4', '2'] + args = parser.parse(["-R", "4", "-S", "2"]) + assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] assert args.R is True assert args.S is False def test_parse_defaultgetter(self): + def defaultget(option): - if not hasattr(option, 'type'): + if not hasattr(option, "type"): return if option.type is int: option.default = 42 elif option.type is str: option.default = "world" + parser = parseopt.Parser(processopt=defaultget) parser.addoption("--this", dest="this", type=int, action="store") parser.addoption("--hello", dest="hello", type=str, action="store") @@ -189,73 +199,88 @@ class TestParser(object): assert option.no is False def test_drop_short_helper(self): - parser = py.std.argparse.ArgumentParser(formatter_class=parseopt.DropShorterLongHelpFormatter) - parser.add_argument('-t', '--twoword', '--duo', '--two-word', '--two', - help='foo').map_long_option = {'two': 'two-word'} + parser = argparse.ArgumentParser( + formatter_class=parseopt.DropShorterLongHelpFormatter + ) + parser.add_argument( + "-t", "--twoword", "--duo", "--two-word", "--two", help="foo" + ).map_long_option = { + "two": "two-word" + } # throws error on --deux only! - parser.add_argument('-d', '--deuxmots', '--deux-mots', - action='store_true', help='foo').map_long_option = {'deux': 'deux-mots'} - parser.add_argument('-s', action='store_true', help='single short') - parser.add_argument('--abc', '-a', - action='store_true', help='bar') - parser.add_argument('--klm', '-k', '--kl-m', - action='store_true', help='bar') - parser.add_argument('-P', '--pq-r', '-p', '--pqr', - action='store_true', help='bar') - parser.add_argument('--zwei-wort', '--zweiwort', '--zweiwort', - action='store_true', help='bar') - parser.add_argument('-x', '--exit-on-first', '--exitfirst', - action='store_true', help='spam').map_long_option = {'exitfirst': 'exit-on-first'} - parser.add_argument('files_and_dirs', nargs='*') - args = parser.parse_args(['-k', '--duo', 'hallo', '--exitfirst']) - assert args.twoword == 'hallo' + parser.add_argument( + "-d", "--deuxmots", "--deux-mots", action="store_true", help="foo" + ).map_long_option = { + "deux": "deux-mots" + } + parser.add_argument("-s", action="store_true", help="single short") + parser.add_argument("--abc", "-a", action="store_true", help="bar") + parser.add_argument("--klm", "-k", "--kl-m", action="store_true", help="bar") + parser.add_argument( + "-P", "--pq-r", "-p", "--pqr", action="store_true", help="bar" + ) + parser.add_argument( + "--zwei-wort", "--zweiwort", "--zweiwort", action="store_true", help="bar" + ) + parser.add_argument( + "-x", "--exit-on-first", "--exitfirst", action="store_true", help="spam" + ).map_long_option = { + "exitfirst": "exit-on-first" + } + parser.add_argument("files_and_dirs", nargs="*") + args = parser.parse_args(["-k", "--duo", "hallo", "--exitfirst"]) + assert args.twoword == "hallo" assert args.klm is True assert args.zwei_wort is False assert args.exit_on_first is True assert args.s is False - args = parser.parse_args(['--deux-mots']) + args = parser.parse_args(["--deux-mots"]) with pytest.raises(AttributeError): assert args.deux_mots is True assert args.deuxmots is True - args = parser.parse_args(['file', 'dir']) - assert '|'.join(args.files_and_dirs) == 'file|dir' + args = parser.parse_args(["file", "dir"]) + assert "|".join(args.files_and_dirs) == "file|dir" def test_drop_short_0(self, parser): - parser.addoption('--funcarg', '--func-arg', action='store_true') - parser.addoption('--abc-def', '--abc-def', action='store_true') - parser.addoption('--klm-hij', action='store_true') - args = parser.parse(['--funcarg', '--k']) + parser.addoption("--funcarg", "--func-arg", action="store_true") + parser.addoption("--abc-def", "--abc-def", action="store_true") + parser.addoption("--klm-hij", action="store_true") + args = parser.parse(["--funcarg", "--k"]) assert args.funcarg is True assert args.abc_def is False assert args.klm_hij is True def test_drop_short_2(self, parser): - parser.addoption('--func-arg', '--doit', action='store_true') - args = parser.parse(['--doit']) + parser.addoption("--func-arg", "--doit", action="store_true") + args = parser.parse(["--doit"]) assert args.func_arg is True def test_drop_short_3(self, parser): - parser.addoption('--func-arg', '--funcarg', '--doit', action='store_true') - args = parser.parse(['abcd']) + parser.addoption("--func-arg", "--funcarg", "--doit", action="store_true") + args = parser.parse(["abcd"]) assert args.func_arg is False - assert args.file_or_dir == ['abcd'] + assert args.file_or_dir == ["abcd"] def test_drop_short_help0(self, parser, capsys): - parser.addoption('--func-args', '--doit', help='foo', - action='store_true') + parser.addoption("--func-args", "--doit", help="foo", action="store_true") parser.parse([]) help = parser.optparser.format_help() - assert '--func-args, --doit foo' in help + assert "--func-args, --doit foo" in help # testing would be more helpful with all help generated def test_drop_short_help1(self, parser, capsys): group = parser.getgroup("general") - group.addoption('--doit', '--func-args', action='store_true', help='foo') - group._addoption("-h", "--help", action="store_true", dest="help", - help="show help message and configuration info") - parser.parse(['-h']) + group.addoption("--doit", "--func-args", action="store_true", help="foo") + group._addoption( + "-h", + "--help", + action="store_true", + dest="help", + help="show help message and configuration info", + ) + parser.parse(["-h"]) help = parser.optparser.format_help() - assert '-doit, --func-args foo' in help + assert "-doit, --func-args foo" in help def test_multiple_metavar_help(self, parser): """ @@ -263,22 +288,24 @@ class TestParser(object): in the form "--preferences=value1 value2 value3" (#2004). """ group = parser.getgroup("general") - group.addoption('--preferences', metavar=('value1', 'value2', 'value3'), nargs=3) + group.addoption( + "--preferences", metavar=("value1", "value2", "value3"), nargs=3 + ) group._addoption("-h", "--help", action="store_true", dest="help") - parser.parse(['-h']) + parser.parse(["-h"]) help = parser.optparser.format_help() - assert '--preferences=value1 value2 value3' in help + assert "--preferences=value1 value2 value3" in help def test_argcomplete(testdir, monkeypatch): - if not py.path.local.sysfind('bash'): + if not py.path.local.sysfind("bash"): pytest.skip("bash not available") script = str(testdir.tmpdir.join("test_argcomplete")) pytest_bin = sys.argv[0] if "pytest" not in os.path.basename(pytest_bin): pytest.skip("need to be run with pytest executable, not %s" % (pytest_bin,)) - with open(str(script), 'w') as fp: + with open(str(script), "w") as fp: # redirect output from argcomplete to stdin and stderr is not trivial # http://stackoverflow.com/q/12589419/1307905 # so we use bash @@ -286,30 +313,24 @@ def test_argcomplete(testdir, monkeypatch): # alternative would be exteneded Testdir.{run(),_run(),popen()} to be able # to handle a keyword argument env that replaces os.environ in popen or # extends the copy, advantage: could not forget to restore - monkeypatch.setenv('_ARGCOMPLETE', "1") - monkeypatch.setenv('_ARGCOMPLETE_IFS', "\x0b") - monkeypatch.setenv('COMP_WORDBREAKS', ' \\t\\n"\\\'><=;|&(:') + monkeypatch.setenv("_ARGCOMPLETE", "1") + monkeypatch.setenv("_ARGCOMPLETE_IFS", "\x0b") + monkeypatch.setenv("COMP_WORDBREAKS", " \\t\\n\"\\'><=;|&(:") - arg = '--fu' - monkeypatch.setenv('COMP_LINE', "pytest " + arg) - monkeypatch.setenv('COMP_POINT', str(len("pytest " + arg))) - result = testdir.run('bash', str(script), arg) + arg = "--fu" + monkeypatch.setenv("COMP_LINE", "pytest " + arg) + monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) + result = testdir.run("bash", str(script), arg) if result.ret == 255: # argcomplete not found pytest.skip("argcomplete not available") elif not result.stdout.str(): pytest.skip("bash provided no output, argcomplete not available?") else: - if py.std.sys.version_info < (2, 7): - result.stdout.lines = result.stdout.lines[0].split('\x0b') - result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"]) - else: - result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"]) - if py.std.sys.version_info < (2, 7): - return - os.mkdir('test_argcomplete.d') - arg = 'test_argc' - monkeypatch.setenv('COMP_LINE', "pytest " + arg) - monkeypatch.setenv('COMP_POINT', str(len('pytest ' + arg))) - result = testdir.run('bash', str(script), arg) + result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"]) + os.mkdir("test_argcomplete.d") + arg = "test_argc" + monkeypatch.setenv("COMP_LINE", "pytest " + arg) + monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) + result = testdir.run("bash", str(script), arg) result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"]) diff --git a/third_party/python/pytest/testing/test_pastebin.py b/third_party/python/pytest/testing/test_pastebin.py index 6b1742d14155..ad7c4d0c1c06 100644 --- a/third_party/python/pytest/testing/test_pastebin.py +++ b/third_party/python/pytest/testing/test_pastebin.py @@ -9,12 +9,13 @@ class TestPasteCapture(object): @pytest.fixture def pastebinlist(self, monkeypatch, request): pastebinlist = [] - plugin = request.config.pluginmanager.getplugin('pastebin') - monkeypatch.setattr(plugin, 'create_new_paste', pastebinlist.append) + plugin = request.config.pluginmanager.getplugin("pastebin") + monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append) return pastebinlist def test_failed(self, testdir, pastebinlist): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import pytest def test_pass(): pass @@ -22,7 +23,8 @@ class TestPasteCapture(object): assert 0 def test_skip(): pytest.skip("") - """) + """ + ) reprec = testdir.inline_run(testpath, "--paste=failed") assert len(pastebinlist) == 1 s = pastebinlist[0] @@ -31,7 +33,9 @@ class TestPasteCapture(object): def test_all(self, testdir, pastebinlist): from _pytest.pytester import LineMatcher - testpath = testdir.makepyfile(""" + + testpath = testdir.makepyfile( + """ import pytest def test_pass(): pass @@ -39,45 +43,52 @@ class TestPasteCapture(object): assert 0 def test_skip(): pytest.skip("") - """) - reprec = testdir.inline_run(testpath, "--pastebin=all", '-v') + """ + ) + reprec = testdir.inline_run(testpath, "--pastebin=all", "-v") assert reprec.countoutcomes() == [1, 1, 1] assert len(pastebinlist) == 1 - contents = pastebinlist[0].decode('utf-8') + contents = pastebinlist[0].decode("utf-8") matcher = LineMatcher(contents.splitlines()) - matcher.fnmatch_lines([ - '*test_pass PASSED*', - '*test_fail FAILED*', - '*test_skip SKIPPED*', - '*== 1 failed, 1 passed, 1 skipped in *' - ]) + matcher.fnmatch_lines( + [ + "*test_pass PASSED*", + "*test_fail FAILED*", + "*test_skip SKIPPED*", + "*== 1 failed, 1 passed, 1 skipped in *", + ] + ) def test_non_ascii_paste_text(self, testdir): """Make sure that text which contains non-ascii characters is pasted correctly. See #1219. """ - testdir.makepyfile(test_unicode=""" + testdir.makepyfile( + test_unicode=""" # encoding: utf-8 def test(): assert '☺' == 1 - """) - result = testdir.runpytest('--pastebin=all') + """ + ) + result = testdir.runpytest("--pastebin=all") if sys.version_info[0] == 3: expected_msg = "*assert '☺' == 1*" else: expected_msg = "*assert '\\xe2\\x98\\xba' == 1*" - result.stdout.fnmatch_lines([ - expected_msg, - "*== 1 failed in *", - '*Sending information to Paste Service*', - ]) + result.stdout.fnmatch_lines( + [ + expected_msg, + "*== 1 failed in *", + "*Sending information to Paste Service*", + ] + ) class TestPaste(object): @pytest.fixture def pastebin(self, request): - return request.config.pluginmanager.getplugin('pastebin') + return request.config.pluginmanager.getplugin("pastebin") @pytest.fixture def mocked_urlopen(self, monkeypatch): @@ -91,27 +102,31 @@ class TestPaste(object): calls.append((url, data)) class DummyFile(object): + def read(self): # part of html of a normal response return b'View raw.' + return DummyFile() if sys.version_info < (3, 0): import urllib - monkeypatch.setattr(urllib, 'urlopen', mocked) + + monkeypatch.setattr(urllib, "urlopen", mocked) else: import urllib.request - monkeypatch.setattr(urllib.request, 'urlopen', mocked) + + monkeypatch.setattr(urllib.request, "urlopen", mocked) return calls def test_create_new_paste(self, pastebin, mocked_urlopen): - result = pastebin.create_new_paste(b'full-paste-contents') - assert result == 'https://bpaste.net/show/3c0c6750bd' + result = pastebin.create_new_paste(b"full-paste-contents") + assert result == "https://bpaste.net/show/3c0c6750bd" assert len(mocked_urlopen) == 1 url, data = mocked_urlopen[0] assert type(data) is bytes - lexer = 'python3' if sys.version_info[0] == 3 else 'python' - assert url == 'https://bpaste.net' - assert 'lexer=%s' % lexer in data.decode() - assert 'code=full-paste-contents' in data.decode() - assert 'expiry=1week' in data.decode() + lexer = "python3" if sys.version_info[0] == 3 else "python" + assert url == "https://bpaste.net" + assert "lexer=%s" % lexer in data.decode() + assert "code=full-paste-contents" in data.decode() + assert "expiry=1week" in data.decode() diff --git a/third_party/python/pytest/testing/test_pdb.py b/third_party/python/pytest/testing/test_pdb.py index 70a5c3c5bdba..615d52e83454 100644 --- a/third_party/python/pytest/testing/test_pdb.py +++ b/third_party/python/pytest/testing/test_pdb.py @@ -1,11 +1,16 @@ from __future__ import absolute_import, division, print_function import sys import platform +import os import _pytest._code +from _pytest.debugging import SUPPORTS_BREAKPOINT_BUILTIN import pytest +_ENVIRON_PYTHONBREAKPOINT = os.environ.get("PYTHONBREAKPOINT", "") + + def runpdb_and_get_report(testdir, source): p = testdir.makepyfile(source) result = testdir.runpytest_inprocess("--pdb", p) @@ -20,6 +25,7 @@ def custom_pdb_calls(): # install dummy debugger class and track which methods were called on it class _CustomPdb(object): + def __init__(self, *args, **kwargs): called.append("init") @@ -33,6 +39,31 @@ def custom_pdb_calls(): return called +@pytest.fixture +def custom_debugger_hook(): + called = [] + + # install dummy debugger class and track which methods were called on it + class _CustomDebugger(object): + + def __init__(self, *args, **kwargs): + called.append("init") + + def reset(self): + called.append("reset") + + def interaction(self, *args): + called.append("interaction") + + def set_trace(self, frame): + print("**CustomDebugger**") + called.append("set_trace") + + _pytest._CustomDebugger = _CustomDebugger + yield called + del _pytest._CustomDebugger + + class TestPDB(object): @pytest.fixture @@ -43,54 +74,79 @@ class TestPDB(object): def mypdb(*args): pdblist.append(args) - plugin = request.config.pluginmanager.getplugin('debugging') - monkeypatch.setattr(plugin, 'post_mortem', mypdb) + plugin = request.config.pluginmanager.getplugin("debugging") + monkeypatch.setattr(plugin, "post_mortem", mypdb) return pdblist def test_pdb_on_fail(self, testdir, pdblist): - rep = runpdb_and_get_report(testdir, """ + rep = runpdb_and_get_report( + testdir, + """ def test_func(): assert 0 - """) + """, + ) assert rep.failed assert len(pdblist) == 1 tb = _pytest._code.Traceback(pdblist[0][0]) assert tb[-1].name == "test_func" def test_pdb_on_xfail(self, testdir, pdblist): - rep = runpdb_and_get_report(testdir, """ + rep = runpdb_and_get_report( + testdir, + """ import pytest @pytest.mark.xfail def test_func(): assert 0 - """) + """, + ) assert "xfail" in rep.keywords assert not pdblist def test_pdb_on_skip(self, testdir, pdblist): - rep = runpdb_and_get_report(testdir, """ + rep = runpdb_and_get_report( + testdir, + """ import pytest def test_func(): pytest.skip("hello") - """) + """, + ) assert rep.skipped assert len(pdblist) == 0 def test_pdb_on_BdbQuit(self, testdir, pdblist): - rep = runpdb_and_get_report(testdir, """ + rep = runpdb_and_get_report( + testdir, + """ import bdb def test_func(): raise bdb.BdbQuit - """) + """, + ) assert rep.failed assert len(pdblist) == 0 + def test_pdb_on_KeyboardInterrupt(self, testdir, pdblist): + rep = runpdb_and_get_report( + testdir, + """ + def test_func(): + raise KeyboardInterrupt + """, + ) + assert rep.failed + assert len(pdblist) == 1 + def test_pdb_interaction(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ def test_1(): i = 0 assert i == 1 - """) + """ + ) child = testdir.spawn_pytest("--pdb %s" % p1) child.expect(".*def test_1") child.expect(".*i = 0") @@ -103,13 +159,14 @@ class TestPDB(object): @staticmethod def flush(child): - if platform.system() == 'Darwin': + if platform.system() == "Darwin": return if child.isalive(): child.wait() def test_pdb_unittest_postmortem(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import unittest class Blub(unittest.TestCase): def tearDown(self): @@ -117,53 +174,136 @@ class TestPDB(object): def test_false(self): self.filename = 'debug' + '.me' assert 0 - """) + """ + ) child = testdir.spawn_pytest("--pdb %s" % p1) - child.expect('(Pdb)') - child.sendline('p self.filename') + child.expect("(Pdb)") + child.sendline("p self.filename") child.sendeof() rest = child.read().decode("utf8") - assert 'debug.me' in rest + assert "debug.me" in rest self.flush(child) def test_pdb_unittest_skip(self, testdir): """Test for issue #2137""" - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import unittest @unittest.skipIf(True, 'Skipping also with pdb active') class MyTestCase(unittest.TestCase): def test_one(self): assert 0 - """) + """ + ) child = testdir.spawn_pytest("-rs --pdb %s" % p1) - child.expect('Skipping also with pdb active') - child.expect('1 skipped in') + child.expect("Skipping also with pdb active") + child.expect("1 skipped in") child.sendeof() self.flush(child) - def test_pdb_interaction_capture(self, testdir): - p1 = testdir.makepyfile(""" + def test_pdb_print_captured_stdout(self, testdir): + p1 = testdir.makepyfile( + """ def test_1(): - print("getrekt") + print("get\\x20rekt") assert False - """) + """ + ) child = testdir.spawn_pytest("--pdb %s" % p1) - child.expect("getrekt") + child.expect("captured stdout") + child.expect("get rekt") + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + assert "get rekt" not in rest + self.flush(child) + + def test_pdb_print_captured_stderr(self, testdir): + p1 = testdir.makepyfile( + """ + def test_1(): + import sys + sys.stderr.write("get\\x20rekt") + assert False + """ + ) + child = testdir.spawn_pytest("--pdb %s" % p1) + child.expect("captured stderr") + child.expect("get rekt") + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + assert "get rekt" not in rest + self.flush(child) + + def test_pdb_dont_print_empty_captured_stdout_and_stderr(self, testdir): + p1 = testdir.makepyfile( + """ + def test_1(): + assert False + """ + ) + child = testdir.spawn_pytest("--pdb %s" % p1) + child.expect("(Pdb)") + output = child.before.decode("utf8") + child.sendeof() + assert "captured stdout" not in output + assert "captured stderr" not in output + self.flush(child) + + @pytest.mark.parametrize("showcapture", ["all", "no", "log"]) + def test_pdb_print_captured_logs(self, testdir, showcapture): + p1 = testdir.makepyfile( + """ + def test_1(): + import logging + logging.warn("get " + "rekt") + assert False + """ + ) + child = testdir.spawn_pytest("--show-capture=%s --pdb %s" % (showcapture, p1)) + if showcapture in ("all", "log"): + child.expect("captured log") + child.expect("get rekt") + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + self.flush(child) + + def test_pdb_print_captured_logs_nologging(self, testdir): + p1 = testdir.makepyfile( + """ + def test_1(): + import logging + logging.warn("get " + "rekt") + assert False + """ + ) + child = testdir.spawn_pytest( + "--show-capture=all --pdb " "-p no:logging %s" % p1 + ) + child.expect("get rekt") + output = child.before.decode("utf8") + assert "captured log" not in output child.expect("(Pdb)") child.sendeof() rest = child.read().decode("utf8") assert "1 failed" in rest - assert "getrekt" not in rest self.flush(child) def test_pdb_interaction_exception(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def globalfunc(): pass def test_1(): pytest.raises(ValueError, globalfunc) - """) + """ + ) child = testdir.spawn_pytest("--pdb %s" % p1) child.expect(".*def test_1") child.expect(".*pytest.raises.*globalfunc") @@ -175,10 +315,12 @@ class TestPDB(object): self.flush(child) def test_pdb_interaction_on_collection_issue181(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest xxx - """) + """ + ) child = testdir.spawn_pytest("--pdb %s" % p1) # child.expect(".*import pytest.*") child.expect("(Pdb)") @@ -187,10 +329,12 @@ class TestPDB(object): self.flush(child) def test_pdb_interaction_on_internal_error(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_runtest_protocol(): 0/0 - """) + """ + ) p1 = testdir.makepyfile("def test_func(): pass") child = testdir.spawn_pytest("--pdb %s" % p1) # child.expect(".*import pytest.*") @@ -199,14 +343,16 @@ class TestPDB(object): self.flush(child) def test_pdb_interaction_capturing_simple(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def test_1(): i = 0 print ("hello17") pytest.set_trace() x = 3 - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.expect("x = 3") @@ -219,11 +365,13 @@ class TestPDB(object): self.flush(child) def test_pdb_set_trace_interception(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pdb def test_1(): pdb.set_trace() - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.expect("(Pdb)") @@ -234,12 +382,14 @@ class TestPDB(object): self.flush(child) def test_pdb_and_capsys(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def test_1(capsys): print ("hello1") pytest.set_trace() - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.send("capsys.readouterr()\n") @@ -249,14 +399,16 @@ class TestPDB(object): self.flush(child) def test_set_trace_capturing_afterwards(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pdb def test_1(): pdb.set_trace() def test_2(): print ("hello") assert 0 - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.send("c\n") @@ -268,17 +420,19 @@ class TestPDB(object): self.flush(child) def test_pdb_interaction_doctest(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def function_1(): ''' >>> i = 0 >>> assert i == 1 ''' - """) + """ + ) child = testdir.spawn_pytest("--doctest-modules --pdb %s" % p1) child.expect("(Pdb)") - child.sendline('i') + child.sendline("i") child.expect("0") child.expect("(Pdb)") child.sendeof() @@ -287,7 +441,8 @@ class TestPDB(object): self.flush(child) def test_pdb_interaction_capturing_twice(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def test_1(): i = 0 @@ -297,12 +452,13 @@ class TestPDB(object): print ("hello18") pytest.set_trace() x = 4 - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.expect("x = 3") child.expect("(Pdb)") - child.sendline('c') + child.sendline("c") child.expect("x = 4") child.sendeof() rest = child.read().decode("utf8") @@ -313,94 +469,234 @@ class TestPDB(object): self.flush(child) def test_pdb_used_outside_test(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest pytest.set_trace() x = 5 - """) + """ + ) child = testdir.spawn("%s %s" % (sys.executable, p1)) child.expect("x = 5") child.sendeof() self.flush(child) def test_pdb_used_in_generate_tests(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def pytest_generate_tests(metafunc): pytest.set_trace() x = 5 def test_foo(a): pass - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("x = 5") child.sendeof() self.flush(child) def test_pdb_collection_failure_is_shown(self, testdir): - p1 = testdir.makepyfile("""xxx """) + p1 = testdir.makepyfile("xxx") result = testdir.runpytest_subprocess("--pdb", p1) - result.stdout.fnmatch_lines([ - "*NameError*xxx*", - "*1 error*", - ]) + result.stdout.fnmatch_lines(["*NameError*xxx*", "*1 error*"]) def test_enter_pdb_hook_is_called(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_enter_pdb(config): assert config.testing_verification == 'configured' print 'enter_pdb_hook' def pytest_configure(config): config.testing_verification = 'configured' - """) - p1 = testdir.makepyfile(""" + """ + ) + p1 = testdir.makepyfile( + """ import pytest def test_foo(): pytest.set_trace() - """) + """ + ) child = testdir.spawn_pytest(str(p1)) child.expect("enter_pdb_hook") - child.send('c\n') + child.send("c\n") child.sendeof() self.flush(child) def test_pdb_custom_cls(self, testdir, custom_pdb_calls): p1 = testdir.makepyfile("""xxx """) - result = testdir.runpytest_inprocess( - "--pdb", "--pdbcls=_pytest:_CustomPdb", p1) - result.stdout.fnmatch_lines([ - "*NameError*xxx*", - "*1 error*", - ]) + result = testdir.runpytest_inprocess("--pdb", "--pdbcls=_pytest:_CustomPdb", p1) + result.stdout.fnmatch_lines(["*NameError*xxx*", "*1 error*"]) assert custom_pdb_calls == ["init", "reset", "interaction"] def test_pdb_custom_cls_without_pdb(self, testdir, custom_pdb_calls): p1 = testdir.makepyfile("""xxx """) - result = testdir.runpytest_inprocess( - "--pdbcls=_pytest:_CustomPdb", p1) - result.stdout.fnmatch_lines([ - "*NameError*xxx*", - "*1 error*", - ]) + result = testdir.runpytest_inprocess("--pdbcls=_pytest:_CustomPdb", p1) + result.stdout.fnmatch_lines(["*NameError*xxx*", "*1 error*"]) assert custom_pdb_calls == [] def test_pdb_custom_cls_with_settrace(self, testdir, monkeypatch): - testdir.makepyfile(custom_pdb=""" + testdir.makepyfile( + custom_pdb=""" class CustomPdb(object): def set_trace(*args, **kwargs): print 'custom set_trace>' - """) - p1 = testdir.makepyfile(""" + """ + ) + p1 = testdir.makepyfile( + """ import pytest def test_foo(): pytest.set_trace() - """) - monkeypatch.setenv('PYTHONPATH', str(testdir.tmpdir)) + """ + ) + monkeypatch.setenv("PYTHONPATH", str(testdir.tmpdir)) child = testdir.spawn_pytest("--pdbcls=custom_pdb:CustomPdb %s" % str(p1)) - child.expect('custom set_trace>') - if child.isalive(): - child.wait() + child.expect("custom set_trace>") + self.flush(child) + + +class TestDebuggingBreakpoints(object): + + def test_supports_breakpoint_module_global(self): + """ + Test that supports breakpoint global marks on Python 3.7+ and not on + CPython 3.5, 2.7 + """ + if sys.version_info.major == 3 and sys.version_info.minor >= 7: + assert SUPPORTS_BREAKPOINT_BUILTIN is True + if sys.version_info.major == 3 and sys.version_info.minor == 5: + assert SUPPORTS_BREAKPOINT_BUILTIN is False + if sys.version_info.major == 2 and sys.version_info.minor == 7: + assert SUPPORTS_BREAKPOINT_BUILTIN is False + + @pytest.mark.skipif( + not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" + ) + @pytest.mark.parametrize("arg", ["--pdb", ""]) + def test_sys_breakpointhook_configure_and_unconfigure(self, testdir, arg): + """ + Test that sys.breakpointhook is set to the custom Pdb class once configured, test that + hook is reset to system value once pytest has been unconfigured + """ + testdir.makeconftest( + """ + import sys + from pytest import hookimpl + from _pytest.debugging import pytestPDB + + def pytest_configure(config): + config._cleanup.append(check_restored) + + def check_restored(): + assert sys.breakpointhook == sys.__breakpointhook__ + + def test_check(): + assert sys.breakpointhook == pytestPDB.set_trace + """ + ) + testdir.makepyfile( + """ + def test_nothing(): pass + """ + ) + args = (arg,) if arg else () + result = testdir.runpytest_subprocess(*args) + result.stdout.fnmatch_lines(["*1 passed in *"]) + + @pytest.mark.skipif( + not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" + ) + def test_pdb_custom_cls(self, testdir, custom_debugger_hook): + p1 = testdir.makepyfile( + """ + def test_nothing(): + breakpoint() + """ + ) + result = testdir.runpytest_inprocess( + "--pdb", "--pdbcls=_pytest:_CustomDebugger", p1 + ) + result.stdout.fnmatch_lines(["*CustomDebugger*", "*1 passed*"]) + assert custom_debugger_hook == ["init", "set_trace"] + + @pytest.mark.parametrize("arg", ["--pdb", ""]) + @pytest.mark.skipif( + not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" + ) + def test_environ_custom_class(self, testdir, custom_debugger_hook, arg): + testdir.makeconftest( + """ + import os + import sys + + os.environ['PYTHONBREAKPOINT'] = '_pytest._CustomDebugger.set_trace' + + def pytest_configure(config): + config._cleanup.append(check_restored) + + def check_restored(): + assert sys.breakpointhook == sys.__breakpointhook__ + + def test_check(): + import _pytest + assert sys.breakpointhook is _pytest._CustomDebugger.set_trace + """ + ) + testdir.makepyfile( + """ + def test_nothing(): pass + """ + ) + args = (arg,) if arg else () + result = testdir.runpytest_subprocess(*args) + result.stdout.fnmatch_lines(["*1 passed in *"]) + + @pytest.mark.skipif( + not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" + ) + @pytest.mark.skipif( + not _ENVIRON_PYTHONBREAKPOINT == "", + reason="Requires breakpoint() default value", + ) + def test_sys_breakpoint_interception(self, testdir): + p1 = testdir.makepyfile( + """ + def test_1(): + breakpoint() + """ + ) + child = testdir.spawn_pytest(str(p1)) + child.expect("test_1") + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + assert "reading from stdin while output" not in rest + TestPDB.flush(child) + + @pytest.mark.skipif( + not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" + ) + def test_pdb_not_altered(self, testdir): + p1 = testdir.makepyfile( + """ + import pdb + def test_1(): + pdb.set_trace() + """ + ) + child = testdir.spawn_pytest(str(p1)) + child.expect("test_1") + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + assert "reading from stdin while output" not in rest + TestPDB.flush(child) diff --git a/third_party/python/pytest/testing/test_pluginmanager.py b/third_party/python/pytest/testing/test_pluginmanager.py index 13c487c2616f..22cea4207c64 100644 --- a/third_party/python/pytest/testing/test_pluginmanager.py +++ b/third_party/python/pytest/testing/test_pluginmanager.py @@ -1,8 +1,10 @@ # encoding: UTF-8 from __future__ import absolute_import, division, print_function import pytest -import py import os +import re +import sys +import types from _pytest.config import get_config, PytestPluginManager from _pytest.main import EXIT_NOTESTSCOLLECTED, Session @@ -14,72 +16,86 @@ def pytestpm(): class TestPytestPluginInteractions(object): + def test_addhooks_conftestplugin(self, testdir): - testdir.makepyfile(newhooks=""" + testdir.makepyfile( + newhooks=""" def pytest_myhook(xyz): "new hook" - """) - conf = testdir.makeconftest(""" + """ + ) + conf = testdir.makeconftest( + """ import sys ; sys.path.insert(0, '.') import newhooks def pytest_addhooks(pluginmanager): pluginmanager.addhooks(newhooks) def pytest_myhook(xyz): return xyz + 1 - """) + """ + ) config = get_config() pm = config.pluginmanager pm.hook.pytest_addhooks.call_historic( - kwargs=dict(pluginmanager=config.pluginmanager)) + kwargs=dict(pluginmanager=config.pluginmanager) + ) config.pluginmanager._importconftest(conf) # print(config.pluginmanager.get_plugins()) res = config.hook.pytest_myhook(xyz=10) assert res == [11] def test_addhooks_nohooks(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import sys def pytest_addhooks(pluginmanager): pluginmanager.addhooks(sys) - """) + """ + ) res = testdir.runpytest() assert res.ret != 0 - res.stderr.fnmatch_lines([ - "*did not find*sys*" - ]) + res.stderr.fnmatch_lines(["*did not find*sys*"]) def test_namespace_early_from_import(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from pytest import Item from pytest import Item as Item2 assert Item is Item2 - """) + """ + ) result = testdir.runpython(p) assert result.ret == 0 def test_do_ext_namespace(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_namespace(): return {'hello': 'world'} - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ from pytest import hello import pytest def test_hello(): assert hello == "world" assert 'hello' in pytest.__all__ - """) + """ + ) reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) def test_do_option_postinitialize(self, testdir): config = testdir.parseconfigure() - assert not hasattr(config.option, 'test123') - p = testdir.makepyfile(""" + assert not hasattr(config.option, "test123") + p = testdir.makepyfile( + """ def pytest_addoption(parser): parser.addoption('--test123', action="store_true", default=True) - """) + """ + ) config.pluginmanager._importconftest(p) assert config.option.test123 @@ -88,6 +104,7 @@ class TestPytestPluginInteractions(object): values = [] class A(object): + def pytest_configure(self, config): values.append(self) @@ -108,10 +125,12 @@ class TestPytestPluginInteractions(object): saveindent = [] class api1(object): + def pytest_plugin_registered(self): saveindent.append(pytestpm.trace.root.indent) class api2(object): + def pytest_plugin_registered(self): saveindent.append(pytestpm.trace.root.indent) raise ValueError() @@ -125,8 +144,8 @@ class TestPytestPluginInteractions(object): pytestpm.register(p) assert pytestpm.trace.root.indent == indent assert len(values) >= 2 - assert 'pytest_plugin_registered' in values[0] - assert 'finish' in values[1] + assert "pytest_plugin_registered" in values[0] + assert "finish" in values[1] values[:] = [] with pytest.raises(ValueError): @@ -140,46 +159,28 @@ class TestPytestPluginInteractions(object): """Test the gethookproxy function(#2016)""" config = testdir.parseconfig() session = Session(config) - testdir.makepyfile(**{ - 'tests/conftest.py': '', - 'tests/subdir/conftest.py': '', - }) + testdir.makepyfile(**{"tests/conftest.py": "", "tests/subdir/conftest.py": ""}) - conftest1 = testdir.tmpdir.join('tests/conftest.py') - conftest2 = testdir.tmpdir.join('tests/subdir/conftest.py') + conftest1 = testdir.tmpdir.join("tests/conftest.py") + conftest2 = testdir.tmpdir.join("tests/subdir/conftest.py") config.pluginmanager._importconftest(conftest1) - ihook_a = session.gethookproxy(testdir.tmpdir.join('tests')) + ihook_a = session.gethookproxy(testdir.tmpdir.join("tests")) assert ihook_a is not None config.pluginmanager._importconftest(conftest2) - ihook_b = session.gethookproxy(testdir.tmpdir.join('tests')) + ihook_b = session.gethookproxy(testdir.tmpdir.join("tests")) assert ihook_a is not ihook_b - def test_warn_on_deprecated_multicall(self, pytestpm): - warnings = [] - - class get_warnings(object): - def pytest_logwarning(self, message): - warnings.append(message) - - class Plugin(object): - def pytest_configure(self, __multicall__): - pass - - pytestpm.register(get_warnings()) - before = list(warnings) - pytestpm.register(Plugin()) - assert len(warnings) == len(before) + 1 - assert "deprecated" in warnings[-1] - def test_warn_on_deprecated_addhooks(self, pytestpm): warnings = [] class get_warnings(object): + def pytest_logwarning(self, code, fslocation, message, nodeid): warnings.append(message) class Plugin(object): + def pytest_testhook(): pass @@ -191,20 +192,19 @@ class TestPytestPluginInteractions(object): def test_namespace_has_default_and_env_plugins(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest pytest.mark - """) + """ + ) result = testdir.runpython(p) assert result.ret == 0 def test_default_markers(testdir): result = testdir.runpytest("--markers") - result.stdout.fnmatch_lines([ - "*tryfirst*first*", - "*trylast*last*", - ]) + result.stdout.fnmatch_lines(["*tryfirst*first*", "*trylast*last*"]) def test_importplugin_error_message(testdir, pytestpm): @@ -214,20 +214,28 @@ def test_importplugin_error_message(testdir, pytestpm): See #375 and #1998. """ testdir.syspathinsert(testdir.tmpdir) - testdir.makepyfile(qwe=""" + testdir.makepyfile( + qwe=""" # encoding: UTF-8 - raise ImportError(u'Not possible to import: ☺') - """) + def test_traceback(): + raise ImportError(u'Not possible to import: ☺') + test_traceback() + """ + ) with pytest.raises(ImportError) as excinfo: pytestpm.import_plugin("qwe") - expected = '.*Error importing plugin "qwe": Not possible to import: .' - assert py.std.re.match(expected, str(excinfo.value)) + + expected_message = '.*Error importing plugin "qwe": Not possible to import: .' + expected_traceback = ".*in test_traceback" + assert re.match(expected_message, str(excinfo.value)) + assert re.match(expected_traceback, str(excinfo.traceback[-1])) class TestPytestPluginManager(object): + def test_register_imported_modules(self): pm = PytestPluginManager() - mod = py.std.types.ModuleType("x.y.pytest_hello") + mod = types.ModuleType("x.y.pytest_hello") pm.register(mod) assert pm.is_registered(mod) values = pm.get_plugins() @@ -238,18 +246,18 @@ class TestPytestPluginManager(object): assert pm.get_plugins() == values def test_canonical_import(self, monkeypatch): - mod = py.std.types.ModuleType("pytest_xyz") - monkeypatch.setitem(py.std.sys.modules, 'pytest_xyz', mod) + mod = types.ModuleType("pytest_xyz") + monkeypatch.setitem(sys.modules, "pytest_xyz", mod) pm = PytestPluginManager() - pm.import_plugin('pytest_xyz') - assert pm.get_plugin('pytest_xyz') == mod + pm.import_plugin("pytest_xyz") + assert pm.get_plugin("pytest_xyz") == mod assert pm.is_registered(mod) def test_consider_module(self, testdir, pytestpm): testdir.syspathinsert() testdir.makepyfile(pytest_p1="#") testdir.makepyfile(pytest_p2="#") - mod = py.std.types.ModuleType("temp") + mod = types.ModuleType("temp") mod.pytest_plugins = ["pytest_p1", "pytest_p2"] pytestpm.consider_module(mod) assert pytestpm.get_plugin("pytest_p1").__name__ == "pytest_p1" @@ -257,12 +265,12 @@ class TestPytestPluginManager(object): def test_consider_module_import_module(self, testdir): pytestpm = get_config().pluginmanager - mod = py.std.types.ModuleType("x") + mod = types.ModuleType("x") mod.pytest_plugins = "pytest_a" aplugin = testdir.makepyfile(pytest_a="#") reprec = testdir.make_hook_recorder(pytestpm) # syspath.prepend(aplugin.dirpath()) - py.std.sys.path.insert(0, str(aplugin.dirpath())) + sys.path.insert(0, str(aplugin.dirpath())) pytestpm.consider_module(mod) call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name) assert call.plugin.__name__ == "pytest_a" @@ -273,46 +281,49 @@ class TestPytestPluginManager(object): assert len(values) == 1 def test_consider_env_fails_to_import(self, monkeypatch, pytestpm): - monkeypatch.setenv('PYTEST_PLUGINS', 'nonexisting', prepend=",") + monkeypatch.setenv("PYTEST_PLUGINS", "nonexisting", prepend=",") with pytest.raises(ImportError): pytestpm.consider_env() def test_plugin_skip(self, testdir, monkeypatch): - p = testdir.makepyfile(skipping1=""" + p = testdir.makepyfile( + skipping1=""" import pytest pytest.skip("hello") - """) + """ + ) p.copy(p.dirpath("skipping2.py")) monkeypatch.setenv("PYTEST_PLUGINS", "skipping2") result = testdir.runpytest("-rw", "-p", "skipping1", syspathinsert=True) assert result.ret == EXIT_NOTESTSCOLLECTED - result.stdout.fnmatch_lines([ - "*skipped plugin*skipping1*hello*", - "*skipped plugin*skipping2*hello*", - ]) + result.stdout.fnmatch_lines( + ["*skipped plugin*skipping1*hello*", "*skipped plugin*skipping2*hello*"] + ) def test_consider_env_plugin_instantiation(self, testdir, monkeypatch, pytestpm): testdir.syspathinsert() testdir.makepyfile(xy123="#") - monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'xy123') + monkeypatch.setitem(os.environ, "PYTEST_PLUGINS", "xy123") l1 = len(pytestpm.get_plugins()) pytestpm.consider_env() l2 = len(pytestpm.get_plugins()) assert l2 == l1 + 1 - assert pytestpm.get_plugin('xy123') + assert pytestpm.get_plugin("xy123") pytestpm.consider_env() l3 = len(pytestpm.get_plugins()) assert l2 == l3 def test_pluginmanager_ENV_startup(self, testdir, monkeypatch): testdir.makepyfile(pytest_x500="#") - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def test_hello(pytestconfig): plugin = pytestconfig.pluginmanager.get_plugin('pytest_x500') assert plugin is not None - """) - monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",") + """ + ) + monkeypatch.setenv("PYTEST_PLUGINS", "pytest_x500", prepend=",") result = testdir.runpytest(p, syspathinsert=True) assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) @@ -330,7 +341,7 @@ class TestPytestPluginManager(object): len2 = len(pytestpm.get_plugins()) assert len1 == len2 plugin1 = pytestpm.get_plugin("pytest_hello") - assert plugin1.__name__.endswith('pytest_hello') + assert plugin1.__name__.endswith("pytest_hello") plugin2 = pytestpm.get_plugin("pytest_hello") assert plugin2 is plugin1 @@ -352,9 +363,11 @@ class TestPytestPluginManager(object): class TestPytestPluginManagerBootstrapming(object): + def test_preparse_args(self, pytestpm): - pytest.raises(ImportError, lambda: - pytestpm.consider_preparse(["xyz", "-p", "hello123"])) + pytest.raises( + ImportError, lambda: pytestpm.consider_preparse(["xyz", "-p", "hello123"]) + ) def test_plugin_prevent_register(self, pytestpm): pytestpm.consider_preparse(["xyz", "-p", "no:abc"]) diff --git a/third_party/python/pytest/testing/test_pytester.py b/third_party/python/pytest/testing/test_pytester.py index 0e8669698075..b74c0b7f70c2 100644 --- a/third_party/python/pytest/testing/test_pytester.py +++ b/third_party/python/pytest/testing/test_pytester.py @@ -1,7 +1,12 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function -import pytest import os +import py.path +import pytest +import sys +import _pytest.pytester as pytester from _pytest.pytester import HookRecorder +from _pytest.pytester import CwdSnapshot, SysModulesSnapshot, SysPathsSnapshot from _pytest.config import PytestPluginManager from _pytest.main import EXIT_OK, EXIT_TESTSFAILED @@ -32,6 +37,7 @@ def test_make_hook_recorder(testdir): failed = False skipped = True when = "call" + rep.passed = False rep.skipped = True recorder.hook.pytest_runtest_logreport(report=rep) @@ -66,24 +72,28 @@ def test_parseconfig(testdir): def test_testdir_runs_with_plugin(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ pytest_plugins = "pytester" def test_hello(testdir): assert 1 - """) + """ + ) result = testdir.runpytest() result.assert_outcomes(passed=1) def make_holder(): + class apiclass(object): + def pytest_xyz(self, arg): "x" def pytest_xyz_noarg(self): "x" - apimod = type(os)('api') + apimod = type(os)("api") def pytest_xyz(arg): "x" @@ -120,19 +130,272 @@ def test_makepyfile_unicode(testdir): testdir.makepyfile(unichr(0xfffd)) -def test_inline_run_clean_modules(testdir): - test_mod = testdir.makepyfile("def test_foo(): assert True") - result = testdir.inline_run(str(test_mod)) - assert result.ret == EXIT_OK - # rewrite module, now test should fail if module was re-imported - test_mod.write("def test_foo(): assert False") - result2 = testdir.inline_run(str(test_mod)) - assert result2.ret == EXIT_TESTSFAILED +def test_makepyfile_utf8(testdir): + """Ensure makepyfile accepts utf-8 bytes as input (#2738)""" + utf8_contents = u""" + def setup_function(function): + mixed_encoding = u'São Paulo' + """.encode( + "utf-8" + ) + p = testdir.makepyfile(utf8_contents) + assert u"mixed_encoding = u'São Paulo'".encode("utf-8") in p.read("rb") -def test_assert_outcomes_after_pytest_erro(testdir): +class TestInlineRunModulesCleanup(object): + + def test_inline_run_test_module_not_cleaned_up(self, testdir): + test_mod = testdir.makepyfile("def test_foo(): assert True") + result = testdir.inline_run(str(test_mod)) + assert result.ret == EXIT_OK + # rewrite module, now test should fail if module was re-imported + test_mod.write("def test_foo(): assert False") + result2 = testdir.inline_run(str(test_mod)) + assert result2.ret == EXIT_TESTSFAILED + + def spy_factory(self): + + class SysModulesSnapshotSpy(object): + instances = [] + + def __init__(self, preserve=None): + SysModulesSnapshotSpy.instances.append(self) + self._spy_restore_count = 0 + self._spy_preserve = preserve + self.__snapshot = SysModulesSnapshot(preserve=preserve) + + def restore(self): + self._spy_restore_count += 1 + return self.__snapshot.restore() + + return SysModulesSnapshotSpy + + def test_inline_run_taking_and_restoring_a_sys_modules_snapshot( + self, testdir, monkeypatch + ): + spy_factory = self.spy_factory() + monkeypatch.setattr(pytester, "SysModulesSnapshot", spy_factory) + original = dict(sys.modules) + testdir.syspathinsert() + testdir.makepyfile(import1="# you son of a silly person") + testdir.makepyfile(import2="# my hovercraft is full of eels") + test_mod = testdir.makepyfile( + """ + import import1 + def test_foo(): import import2""" + ) + testdir.inline_run(str(test_mod)) + assert len(spy_factory.instances) == 1 + spy = spy_factory.instances[0] + assert spy._spy_restore_count == 1 + assert sys.modules == original + assert all(sys.modules[x] is original[x] for x in sys.modules) + + def test_inline_run_sys_modules_snapshot_restore_preserving_modules( + self, testdir, monkeypatch + ): + spy_factory = self.spy_factory() + monkeypatch.setattr(pytester, "SysModulesSnapshot", spy_factory) + test_mod = testdir.makepyfile("def test_foo(): pass") + testdir.inline_run(str(test_mod)) + spy = spy_factory.instances[0] + assert not spy._spy_preserve("black_knight") + assert spy._spy_preserve("zope") + assert spy._spy_preserve("zope.interface") + assert spy._spy_preserve("zopelicious") + + def test_external_test_module_imports_not_cleaned_up(self, testdir): + testdir.syspathinsert() + testdir.makepyfile(imported="data = 'you son of a silly person'") + import imported + + test_mod = testdir.makepyfile( + """ + def test_foo(): + import imported + imported.data = 42""" + ) + testdir.inline_run(str(test_mod)) + assert imported.data == 42 + + +def test_inline_run_clean_sys_paths(testdir): + + def test_sys_path_change_cleanup(self, testdir): + test_path1 = testdir.tmpdir.join("boink1").strpath + test_path2 = testdir.tmpdir.join("boink2").strpath + test_path3 = testdir.tmpdir.join("boink3").strpath + sys.path.append(test_path1) + sys.meta_path.append(test_path1) + original_path = list(sys.path) + original_meta_path = list(sys.meta_path) + test_mod = testdir.makepyfile( + """ + import sys + sys.path.append({:test_path2}) + sys.meta_path.append({:test_path2}) + def test_foo(): + sys.path.append({:test_path3}) + sys.meta_path.append({:test_path3})""".format( + locals() + ) + ) + testdir.inline_run(str(test_mod)) + assert sys.path == original_path + assert sys.meta_path == original_meta_path + + def spy_factory(self): + + class SysPathsSnapshotSpy(object): + instances = [] + + def __init__(self): + SysPathsSnapshotSpy.instances.append(self) + self._spy_restore_count = 0 + self.__snapshot = SysPathsSnapshot() + + def restore(self): + self._spy_restore_count += 1 + return self.__snapshot.restore() + + return SysPathsSnapshotSpy + + def test_inline_run_taking_and_restoring_a_sys_paths_snapshot( + self, testdir, monkeypatch + ): + spy_factory = self.spy_factory() + monkeypatch.setattr(pytester, "SysPathsSnapshot", spy_factory) + test_mod = testdir.makepyfile("def test_foo(): pass") + testdir.inline_run(str(test_mod)) + assert len(spy_factory.instances) == 1 + spy = spy_factory.instances[0] + assert spy._spy_restore_count == 1 + + +def test_assert_outcomes_after_pytest_error(testdir): testdir.makepyfile("def test_foo(): assert True") - result = testdir.runpytest('--unexpected-argument') + result = testdir.runpytest("--unexpected-argument") with pytest.raises(ValueError, message="Pytest terminal report not found"): result.assert_outcomes(passed=0) + + +def test_cwd_snapshot(tmpdir): + foo = tmpdir.ensure("foo", dir=1) + bar = tmpdir.ensure("bar", dir=1) + foo.chdir() + snapshot = CwdSnapshot() + bar.chdir() + assert py.path.local() == bar + snapshot.restore() + assert py.path.local() == foo + + +class TestSysModulesSnapshot(object): + key = "my-test-module" + + def test_remove_added(self): + original = dict(sys.modules) + assert self.key not in sys.modules + snapshot = SysModulesSnapshot() + sys.modules[self.key] = "something" + assert self.key in sys.modules + snapshot.restore() + assert sys.modules == original + + def test_add_removed(self, monkeypatch): + assert self.key not in sys.modules + monkeypatch.setitem(sys.modules, self.key, "something") + assert self.key in sys.modules + original = dict(sys.modules) + snapshot = SysModulesSnapshot() + del sys.modules[self.key] + assert self.key not in sys.modules + snapshot.restore() + assert sys.modules == original + + def test_restore_reloaded(self, monkeypatch): + assert self.key not in sys.modules + monkeypatch.setitem(sys.modules, self.key, "something") + assert self.key in sys.modules + original = dict(sys.modules) + snapshot = SysModulesSnapshot() + sys.modules[self.key] = "something else" + snapshot.restore() + assert sys.modules == original + + def test_preserve_modules(self, monkeypatch): + key = [self.key + str(i) for i in range(3)] + assert not any(k in sys.modules for k in key) + for i, k in enumerate(key): + monkeypatch.setitem(sys.modules, k, "something" + str(i)) + original = dict(sys.modules) + + def preserve(name): + return name in (key[0], key[1], "some-other-key") + + snapshot = SysModulesSnapshot(preserve=preserve) + sys.modules[key[0]] = original[key[0]] = "something else0" + sys.modules[key[1]] = original[key[1]] = "something else1" + sys.modules[key[2]] = "something else2" + snapshot.restore() + assert sys.modules == original + + def test_preserve_container(self, monkeypatch): + original = dict(sys.modules) + assert self.key not in original + replacement = dict(sys.modules) + replacement[self.key] = "life of brian" + snapshot = SysModulesSnapshot() + monkeypatch.setattr(sys, "modules", replacement) + snapshot.restore() + assert sys.modules is replacement + assert sys.modules == original + + +@pytest.mark.parametrize("path_type", ("path", "meta_path")) +class TestSysPathsSnapshot(object): + other_path = {"path": "meta_path", "meta_path": "path"} + + @staticmethod + def path(n): + return "my-dirty-little-secret-" + str(n) + + def test_restore(self, monkeypatch, path_type): + other_path_type = self.other_path[path_type] + for i in range(10): + assert self.path(i) not in getattr(sys, path_type) + sys_path = [self.path(i) for i in range(6)] + monkeypatch.setattr(sys, path_type, sys_path) + original = list(sys_path) + original_other = list(getattr(sys, other_path_type)) + snapshot = SysPathsSnapshot() + transformation = { + "source": (0, 1, 2, 3, 4, 5), "target": (6, 2, 9, 7, 5, 8) + } # noqa: E201 + assert sys_path == [self.path(x) for x in transformation["source"]] + sys_path[1] = self.path(6) + sys_path[3] = self.path(7) + sys_path.append(self.path(8)) + del sys_path[4] + sys_path[3:3] = [self.path(9)] + del sys_path[0] + assert sys_path == [self.path(x) for x in transformation["target"]] + snapshot.restore() + assert getattr(sys, path_type) is sys_path + assert getattr(sys, path_type) == original + assert getattr(sys, other_path_type) == original_other + + def test_preserve_container(self, monkeypatch, path_type): + other_path_type = self.other_path[path_type] + original_data = list(getattr(sys, path_type)) + original_other = getattr(sys, other_path_type) + original_other_data = list(original_other) + new = [] + snapshot = SysPathsSnapshot() + monkeypatch.setattr(sys, path_type, new) + snapshot.restore() + assert getattr(sys, path_type) is new + assert getattr(sys, path_type) == original_data + assert getattr(sys, other_path_type) is original_other + assert getattr(sys, other_path_type) == original_other_data diff --git a/third_party/python/pytest/testing/test_recwarn.py b/third_party/python/pytest/testing/test_recwarn.py index 481bf0a043bf..a8e2fb803780 100644 --- a/third_party/python/pytest/testing/test_recwarn.py +++ b/third_party/python/pytest/testing/test_recwarn.py @@ -1,32 +1,34 @@ from __future__ import absolute_import, division, print_function import warnings import re -import py import pytest from _pytest.recwarn import WarningsRecorder def test_recwarn_functional(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ import warnings def test_method(recwarn): warnings.warn("hello") warn = recwarn.pop() assert isinstance(warn.message, UserWarning) - """) + """ + ) res = reprec.countoutcomes() assert tuple(res) == (1, 0, 0), res class TestWarningsRecorderChecker(object): + def test_recording(self): rec = WarningsRecorder() with rec: assert not rec.list - py.std.warnings.warn_explicit("hello", UserWarning, "xyz", 13) + warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 - py.std.warnings.warn(DeprecationWarning("hello")) + warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" @@ -38,10 +40,11 @@ class TestWarningsRecorderChecker(object): def test_typechecking(self): from _pytest.recwarn import WarningsChecker + with pytest.raises(TypeError): WarningsChecker(5) with pytest.raises(TypeError): - WarningsChecker(('hi', RuntimeWarning)) + WarningsChecker(("hi", RuntimeWarning)) with pytest.raises(TypeError): WarningsChecker([DeprecationWarning, RuntimeWarning]) @@ -64,19 +67,19 @@ class TestDeprecatedCall(object): def dep(self, i, j=None): if i == 0: - py.std.warnings.warn("is deprecated", DeprecationWarning, - stacklevel=1) + warnings.warn("is deprecated", DeprecationWarning, stacklevel=1) return 42 def dep_explicit(self, i): if i == 0: - py.std.warnings.warn_explicit("dep_explicit", category=DeprecationWarning, - filename="hello", lineno=3) + warnings.warn_explicit( + "dep_explicit", category=DeprecationWarning, filename="hello", lineno=3 + ) def test_deprecated_call_raises(self): with pytest.raises(AssertionError) as excinfo: pytest.deprecated_call(self.dep, 3, 5) - assert 'Did not produce' in str(excinfo) + assert "Did not produce" in str(excinfo) def test_deprecated_call(self): pytest.deprecated_call(self.dep, 0, 5) @@ -86,16 +89,16 @@ class TestDeprecatedCall(object): assert ret == 42 def test_deprecated_call_preserves(self): - onceregistry = py.std.warnings.onceregistry.copy() - filters = py.std.warnings.filters[:] - warn = py.std.warnings.warn - warn_explicit = py.std.warnings.warn_explicit + onceregistry = warnings.onceregistry.copy() + filters = warnings.filters[:] + warn = warnings.warn + warn_explicit = warnings.warn_explicit self.test_deprecated_call_raises() self.test_deprecated_call() - assert onceregistry == py.std.warnings.onceregistry - assert filters == py.std.warnings.filters - assert warn is py.std.warnings.warn - assert warn_explicit is py.std.warnings.warn_explicit + assert onceregistry == warnings.onceregistry + assert filters == warnings.filters + assert warn is warnings.warn + assert warn_explicit is warnings.warn_explicit def test_deprecated_explicit_call_raises(self): with pytest.raises(AssertionError): @@ -105,30 +108,34 @@ class TestDeprecatedCall(object): pytest.deprecated_call(self.dep_explicit, 0) pytest.deprecated_call(self.dep_explicit, 0) - @pytest.mark.parametrize('mode', ['context_manager', 'call']) + @pytest.mark.parametrize("mode", ["context_manager", "call"]) def test_deprecated_call_no_warning(self, mode): """Ensure deprecated_call() raises the expected failure when its block/function does not raise a deprecation warning. """ + def f(): pass - msg = 'Did not produce DeprecationWarning or PendingDeprecationWarning' - with pytest.raises(AssertionError, matches=msg): - if mode == 'call': + msg = "Did not produce DeprecationWarning or PendingDeprecationWarning" + with pytest.raises(AssertionError, match=msg): + if mode == "call": pytest.deprecated_call(f) else: with pytest.deprecated_call(): f() - @pytest.mark.parametrize('warning_type', [PendingDeprecationWarning, DeprecationWarning]) - @pytest.mark.parametrize('mode', ['context_manager', 'call']) - @pytest.mark.parametrize('call_f_first', [True, False]) - @pytest.mark.filterwarnings('ignore') + @pytest.mark.parametrize( + "warning_type", [PendingDeprecationWarning, DeprecationWarning] + ) + @pytest.mark.parametrize("mode", ["context_manager", "call"]) + @pytest.mark.parametrize("call_f_first", [True, False]) + @pytest.mark.filterwarnings("ignore") def test_deprecated_call_modes(self, warning_type, mode, call_f_first): """Ensure deprecated_call() captures a deprecation warning as expected inside its block/function. """ + def f(): warnings.warn(warning_type("hi")) return 10 @@ -136,31 +143,40 @@ class TestDeprecatedCall(object): # ensure deprecated_call() can capture the warning even if it has already been triggered if call_f_first: assert f() == 10 - if mode == 'call': + if mode == "call": assert pytest.deprecated_call(f) == 10 else: with pytest.deprecated_call(): assert f() == 10 - @pytest.mark.parametrize('mode', ['context_manager', 'call']) + @pytest.mark.parametrize("mode", ["context_manager", "call"]) def test_deprecated_call_exception_is_raised(self, mode): """If the block of the code being tested by deprecated_call() raises an exception, it must raise the exception undisturbed. """ - def f(): - raise ValueError('some exception') - with pytest.raises(ValueError, match='some exception'): - if mode == 'call': + def f(): + raise ValueError("some exception") + + with pytest.raises(ValueError, match="some exception"): + if mode == "call": pytest.deprecated_call(f) else: with pytest.deprecated_call(): f() def test_deprecated_call_specificity(self): - other_warnings = [Warning, UserWarning, SyntaxWarning, RuntimeWarning, - FutureWarning, ImportWarning, UnicodeWarning] + other_warnings = [ + Warning, + UserWarning, + SyntaxWarning, + RuntimeWarning, + FutureWarning, + ImportWarning, + UnicodeWarning, + ] for warning in other_warnings: + def f(): warnings.warn(warning("hi")) @@ -172,29 +188,35 @@ class TestDeprecatedCall(object): class TestWarns(object): + def test_strings(self): # different messages, b/c Python suppresses multiple identical warnings source1 = "warnings.warn('w1', RuntimeWarning)" source2 = "warnings.warn('w2', RuntimeWarning)" source3 = "warnings.warn('w3', RuntimeWarning)" pytest.warns(RuntimeWarning, source1) - pytest.raises(pytest.fail.Exception, - lambda: pytest.warns(UserWarning, source2)) + pytest.raises(pytest.fail.Exception, lambda: pytest.warns(UserWarning, source2)) pytest.warns(RuntimeWarning, source3) def test_function(self): - pytest.warns(SyntaxWarning, - lambda msg: warnings.warn(msg, SyntaxWarning), "syntax") + pytest.warns( + SyntaxWarning, lambda msg: warnings.warn(msg, SyntaxWarning), "syntax" + ) def test_warning_tuple(self): - pytest.warns((RuntimeWarning, SyntaxWarning), - lambda: warnings.warn('w1', RuntimeWarning)) - pytest.warns((RuntimeWarning, SyntaxWarning), - lambda: warnings.warn('w2', SyntaxWarning)) - pytest.raises(pytest.fail.Exception, - lambda: pytest.warns( - (RuntimeWarning, SyntaxWarning), - lambda: warnings.warn('w3', UserWarning))) + pytest.warns( + (RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w1", RuntimeWarning) + ) + pytest.warns( + (RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w2", SyntaxWarning) + ) + pytest.raises( + pytest.fail.Exception, + lambda: pytest.warns( + (RuntimeWarning, SyntaxWarning), + lambda: warnings.warn("w3", UserWarning), + ), + ) def test_as_contextmanager(self): with pytest.warns(RuntimeWarning): @@ -206,20 +228,26 @@ class TestWarns(object): with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(RuntimeWarning): warnings.warn("user", UserWarning) - excinfo.match(r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. " - r"The list of emitted warnings is: \[UserWarning\('user',\)\].") + excinfo.match( + r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. " + r"The list of emitted warnings is: \[UserWarning\('user',?\)\]." + ) with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(UserWarning): warnings.warn("runtime", RuntimeWarning) - excinfo.match(r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. " - r"The list of emitted warnings is: \[RuntimeWarning\('runtime',\)\].") + excinfo.match( + r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. " + r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]." + ) with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(UserWarning): pass - excinfo.match(r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. " - r"The list of emitted warnings is: \[\].") + excinfo.match( + r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. " + r"The list of emitted warnings is: \[\]." + ) warning_classes = (UserWarning, FutureWarning) with pytest.raises(pytest.fail.Exception) as excinfo: @@ -227,10 +255,17 @@ class TestWarns(object): warnings.warn("runtime", RuntimeWarning) warnings.warn("import", ImportWarning) - message_template = ("DID NOT WARN. No warnings of type {0} was emitted. " - "The list of emitted warnings is: {1}.") - excinfo.match(re.escape(message_template.format(warning_classes, - [each.message for each in warninfo]))) + message_template = ( + "DID NOT WARN. No warnings of type {0} was emitted. " + "The list of emitted warnings is: {1}." + ) + excinfo.match( + re.escape( + message_template.format( + warning_classes, [each.message for each in warninfo] + ) + ) + ) def test_record(self): with pytest.warns(UserWarning) as record: @@ -273,7 +308,8 @@ class TestWarns(object): def test_double_test(self, testdir): """If a test is run again, the warning should still be raised""" - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest import warnings @@ -281,6 +317,31 @@ class TestWarns(object): def test(run): with pytest.warns(RuntimeWarning): warnings.warn("runtime", RuntimeWarning) - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['*2 passed in*']) + result.stdout.fnmatch_lines(["*2 passed in*"]) + + def test_match_regex(self): + with pytest.warns(UserWarning, match=r"must be \d+$"): + warnings.warn("value must be 42", UserWarning) + + with pytest.raises(pytest.fail.Exception): + with pytest.warns(UserWarning, match=r"must be \d+$"): + warnings.warn("this is not here", UserWarning) + + with pytest.raises(pytest.fail.Exception): + with pytest.warns(FutureWarning, match=r"must be \d+$"): + warnings.warn("value must be 42", UserWarning) + + def test_one_from_multiple_warns(self): + with pytest.warns(UserWarning, match=r"aaa"): + warnings.warn("cccccccccc", UserWarning) + warnings.warn("bbbbbbbbbb", UserWarning) + warnings.warn("aaaaaaaaaa", UserWarning) + + def test_none_of_multiple_warns(self): + with pytest.raises(pytest.fail.Exception): + with pytest.warns(UserWarning, match=r"aaa"): + warnings.warn("bbbbbbbbbb", UserWarning) + warnings.warn("cccccccccc", UserWarning) diff --git a/third_party/python/pytest/testing/test_resultlog.py b/third_party/python/pytest/testing/test_resultlog.py index b7dd2687cdfd..173384ffb30b 100644 --- a/third_party/python/pytest/testing/test_resultlog.py +++ b/third_party/python/pytest/testing/test_resultlog.py @@ -4,72 +4,77 @@ import os import _pytest._code import py import pytest -from _pytest.main import Node, Item, FSCollector -from _pytest.resultlog import generic_path, ResultLog, \ - pytest_configure, pytest_unconfigure +from _pytest.nodes import Node, Item, FSCollector +from _pytest.resultlog import ( + generic_path, + ResultLog, + pytest_configure, + pytest_unconfigure, +) def test_generic_path(testdir): from _pytest.main import Session + config = testdir.parseconfig() session = Session(config) - p1 = Node('a', config=config, session=session) + p1 = Node("a", config=config, session=session, nodeid="a") # assert p1.fspath is None - p2 = Node('B', parent=p1) - p3 = Node('()', parent=p2) - item = Item('c', parent=p3) + p2 = Node("B", parent=p1) + p3 = Node("()", parent=p2) + item = Item("c", parent=p3) res = generic_path(item) - assert res == 'a.B().c' + assert res == "a.B().c" - p0 = FSCollector('proj/test', config=config, session=session) - p1 = FSCollector('proj/test/a', parent=p0) - p2 = Node('B', parent=p1) - p3 = Node('()', parent=p2) - p4 = Node('c', parent=p3) - item = Item('[1]', parent=p4) + p0 = FSCollector("proj/test", config=config, session=session) + p1 = FSCollector("proj/test/a", parent=p0) + p2 = Node("B", parent=p1) + p3 = Node("()", parent=p2) + p4 = Node("c", parent=p3) + item = Item("[1]", parent=p4) res = generic_path(item) - assert res == 'test/a:B().c[1]' + assert res == "test/a:B().c[1]" def test_write_log_entry(): reslog = ResultLog(None, None) reslog.logfile = py.io.TextIO() - reslog.write_log_entry('name', '.', '') + reslog.write_log_entry("name", ".", "") entry = reslog.logfile.getvalue() - assert entry[-1] == '\n' + assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 1 - assert entry_lines[0] == '. name' + assert entry_lines[0] == ". name" reslog.logfile = py.io.TextIO() - reslog.write_log_entry('name', 's', 'Skipped') + reslog.write_log_entry("name", "s", "Skipped") entry = reslog.logfile.getvalue() - assert entry[-1] == '\n' + assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 2 - assert entry_lines[0] == 's name' - assert entry_lines[1] == ' Skipped' + assert entry_lines[0] == "s name" + assert entry_lines[1] == " Skipped" reslog.logfile = py.io.TextIO() - reslog.write_log_entry('name', 's', 'Skipped\n') + reslog.write_log_entry("name", "s", "Skipped\n") entry = reslog.logfile.getvalue() - assert entry[-1] == '\n' + assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 2 - assert entry_lines[0] == 's name' - assert entry_lines[1] == ' Skipped' + assert entry_lines[0] == "s name" + assert entry_lines[1] == " Skipped" reslog.logfile = py.io.TextIO() - longrepr = ' tb1\n tb 2\nE tb3\nSome Error' - reslog.write_log_entry('name', 'F', longrepr) + longrepr = " tb1\n tb 2\nE tb3\nSome Error" + reslog.write_log_entry("name", "F", longrepr) entry = reslog.logfile.getvalue() - assert entry[-1] == '\n' + assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 5 - assert entry_lines[0] == 'F name' - assert entry_lines[1:] == [' ' + line for line in longrepr.splitlines()] + assert entry_lines[0] == "F name" + assert entry_lines[1:] == [" " + line for line in longrepr.splitlines()] class TestWithFunctionIntegration(object): @@ -98,7 +103,8 @@ class TestWithFunctionIntegration(object): assert "XXX" in "".join(lines[1:]) def test_log_test_outcomes(self, testdir): - mod = testdir.makepyfile(test_mod=""" + mod = testdir.makepyfile( + test_mod=""" import pytest def test_pass(): pass def test_skip(): pytest.skip("hello") @@ -109,7 +115,8 @@ class TestWithFunctionIntegration(object): @pytest.mark.xfail def test_xpass(): pass - """) + """ + ) lines = self.getresultlog(testdir, mod) assert len(lines) >= 3 assert lines[0].startswith(". ") @@ -123,11 +130,11 @@ class TestWithFunctionIntegration(object): tb = "".join(lines[4:8]) assert tb.find('raise ValueError("FAIL")') != -1 - assert lines[8].startswith('x ') + assert lines[8].startswith("x ") tb = "".join(lines[8:14]) assert tb.find('raise ValueError("XFAIL")') != -1 - assert lines[14].startswith('X ') + assert lines[14].startswith("X ") assert len(lines) == 15 @pytest.mark.parametrize("style", ("native", "long", "short")) @@ -143,16 +150,17 @@ class TestWithFunctionIntegration(object): entry = reslog.logfile.getvalue() entry_lines = entry.splitlines() - assert entry_lines[0].startswith('! ') + assert entry_lines[0].startswith("! ") if style != "native": assert os.path.basename(__file__)[:-9] in entry_lines[0] # .pyc/class - assert entry_lines[-1][0] == ' ' - assert 'ValueError' in entry + assert entry_lines[-1][0] == " " + assert "ValueError" in entry def test_generic(testdir, LineMatcher): testdir.plugins.append("resultlog") - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_pass(): pass @@ -166,51 +174,55 @@ def test_generic(testdir, LineMatcher): @pytest.mark.xfail(run=False) def test_xfail_norun(): assert 0 - """) + """ + ) testdir.runpytest("--resultlog=result.log") lines = testdir.tmpdir.join("result.log").readlines(cr=0) - LineMatcher(lines).fnmatch_lines([ - ". *:test_pass", - "F *:test_fail", - "s *:test_skip", - "x *:test_xfail", - "x *:test_xfail_norun", - ]) + LineMatcher(lines).fnmatch_lines( + [ + ". *:test_pass", + "F *:test_fail", + "s *:test_skip", + "x *:test_xfail", + "x *:test_xfail_norun", + ] + ) def test_makedir_for_resultlog(testdir, LineMatcher): """--resultlog should automatically create directories for the log file""" testdir.plugins.append("resultlog") - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_pass(): pass - """) + """ + ) testdir.runpytest("--resultlog=path/to/result.log") lines = testdir.tmpdir.join("path/to/result.log").readlines(cr=0) - LineMatcher(lines).fnmatch_lines([ - ". *:test_pass", - ]) + LineMatcher(lines).fnmatch_lines([". *:test_pass"]) def test_no_resultlog_on_slaves(testdir): config = testdir.parseconfig("-p", "resultlog", "--resultlog=resultlog") - assert not hasattr(config, '_resultlog') + assert not hasattr(config, "_resultlog") pytest_configure(config) - assert hasattr(config, '_resultlog') + assert hasattr(config, "_resultlog") pytest_unconfigure(config) - assert not hasattr(config, '_resultlog') + assert not hasattr(config, "_resultlog") config.slaveinput = {} pytest_configure(config) - assert not hasattr(config, '_resultlog') + assert not hasattr(config, "_resultlog") pytest_unconfigure(config) - assert not hasattr(config, '_resultlog') + assert not hasattr(config, "_resultlog") def test_failure_issue380(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyCollector(pytest.File): def collect(self): @@ -219,10 +231,13 @@ def test_failure_issue380(testdir): return "somestring" def pytest_collect_file(path, parent): return MyCollector(parent=parent, fspath=path) - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_func(): pass - """) + """ + ) result = testdir.runpytest("--resultlog=log") assert result.ret == 2 diff --git a/third_party/python/pytest/testing/test_runner.py b/third_party/python/pytest/testing/test_runner.py index 84d8f6c71e6a..f5430a90da53 100644 --- a/third_party/python/pytest/testing/test_runner.py +++ b/third_party/python/pytest/testing/test_runner.py @@ -2,14 +2,17 @@ from __future__ import absolute_import, division, print_function import _pytest._code +import inspect import os import py import pytest import sys +import types from _pytest import runner, main, outcomes class TestSetupState(object): + def test_setup(self, testdir): ss = runner.SetupState() item = testdir.getitem("def test_func(): pass") @@ -28,11 +31,13 @@ class TestSetupState(object): ss.teardown_exact(item, None) def test_setup_fails_and_failure_is_cached(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ def setup_module(mod): raise ValueError(42) def test_func(): pass - """) # noqa + """ + ) ss = runner.SetupState() pytest.raises(ValueError, lambda: ss.prepare(item)) pytest.raises(ValueError, lambda: ss.prepare(item)) @@ -41,13 +46,13 @@ class TestSetupState(object): r = [] def fin1(): - r.append('fin1') + r.append("fin1") def fin2(): - raise Exception('oops') + raise Exception("oops") def fin3(): - r.append('fin3') + r.append("fin3") item = testdir.getitem("def test_func(): pass") ss = runner.SetupState() @@ -56,17 +61,17 @@ class TestSetupState(object): ss.addfinalizer(fin3, item) with pytest.raises(Exception) as err: ss._callfinalizers(item) - assert err.value.args == ('oops',) - assert r == ['fin3', 'fin1'] + assert err.value.args == ("oops",) + assert r == ["fin3", "fin1"] def test_teardown_multiple_fail(self, testdir): # Ensure the first exception is the one which is re-raised. # Ideally both would be reported however. def fin1(): - raise Exception('oops1') + raise Exception("oops1") def fin2(): - raise Exception('oops2') + raise Exception("oops2") item = testdir.getitem("def test_func(): pass") ss = runner.SetupState() @@ -74,15 +79,36 @@ class TestSetupState(object): ss.addfinalizer(fin2, item) with pytest.raises(Exception) as err: ss._callfinalizers(item) - assert err.value.args == ('oops2',) + assert err.value.args == ("oops2",) + + def test_teardown_multiple_scopes_one_fails(self, testdir): + module_teardown = [] + + def fin_func(): + raise Exception("oops1") + + def fin_module(): + module_teardown.append("fin_module") + + item = testdir.getitem("def test_func(): pass") + ss = runner.SetupState() + ss.addfinalizer(fin_module, item.listchain()[-2]) + ss.addfinalizer(fin_func, item) + ss.prepare(item) + with pytest.raises(Exception, match="oops1"): + ss.teardown_exact(item, None) + assert module_teardown class BaseFunctionalTests(object): + def test_passfunction(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): pass - """) + """ + ) rep = reports[1] assert rep.passed assert not rep.failed @@ -90,10 +116,12 @@ class BaseFunctionalTests(object): assert not rep.longrepr def test_failfunction(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): assert 0 - """) + """ + ) rep = reports[1] assert not rep.passed assert not rep.skipped @@ -103,11 +131,13 @@ class BaseFunctionalTests(object): # assert isinstance(rep.longrepr, ReprExceptionInfo) def test_skipfunction(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ import pytest def test_func(): pytest.skip("hello") - """) + """ + ) rep = reports[1] assert not rep.failed assert not rep.passed @@ -121,13 +151,15 @@ class BaseFunctionalTests(object): # assert not rep.skipped.failurerepr def test_skip_in_setup_function(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ import pytest def setup_function(func): pytest.skip("hello") def test_func(): pass - """) + """ + ) print(reports) rep = reports[0] assert not rep.failed @@ -140,13 +172,15 @@ class BaseFunctionalTests(object): assert reports[1].passed # teardown def test_failure_in_setup_function(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ import pytest def setup_function(func): raise ValueError(42) def test_func(): pass - """) + """ + ) rep = reports[0] assert not rep.skipped assert not rep.passed @@ -155,13 +189,15 @@ class BaseFunctionalTests(object): assert len(reports) == 2 def test_failure_in_teardown_function(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ import pytest def teardown_function(func): raise ValueError(42) def test_func(): pass - """) + """ + ) print(reports) assert len(reports) == 3 rep = reports[2] @@ -173,17 +209,21 @@ class BaseFunctionalTests(object): # assert rep.longrepr.reprtraceback.reprentries def test_custom_failure_repr(self, testdir): - testdir.makepyfile(conftest=""" + testdir.makepyfile( + conftest=""" import pytest class Function(pytest.Function): def repr_failure(self, excinfo): return "hello" - """) - reports = testdir.runitem(""" + """ + ) + reports = testdir.runitem( + """ import pytest def test_func(): assert 0 - """) + """ + ) rep = reports[1] assert not rep.skipped assert not rep.passed @@ -194,16 +234,36 @@ class BaseFunctionalTests(object): # assert rep.failed.failurerepr == "hello" def test_teardown_final_returncode(self, testdir): - rec = testdir.inline_runsource(""" + rec = testdir.inline_runsource( + """ def test_func(): pass def teardown_function(func): raise ValueError(42) - """) + """ + ) assert rec.ret == 1 + def test_logstart_logfinish_hooks(self, testdir): + rec = testdir.inline_runsource( + """ + import pytest + def test_func(): + pass + """ + ) + reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish") + assert ( + [x._name for x in reps] + == ["pytest_runtest_logstart", "pytest_runtest_logfinish"] + ) + for rep in reps: + assert rep.nodeid == "test_logstart_logfinish_hooks.py::test_func" + assert rep.location == ("test_logstart_logfinish_hooks.py", 1, "test_func") + def test_exact_teardown_issue90(self, testdir): - rec = testdir.inline_runsource(""" + rec = testdir.inline_runsource( + """ import pytest class TestClass(object): @@ -224,7 +284,8 @@ class BaseFunctionalTests(object): traceback.format_exception(*excinfo) def teardown_function(func): raise ValueError(42) - """) + """ + ) reps = rec.getreports("pytest_runtest_logreport") print(reps) for i in range(2): @@ -242,7 +303,8 @@ class BaseFunctionalTests(object): def test_exact_teardown_issue1206(self, testdir): """issue shadowing error with wrong number of arguments on teardown_method.""" - rec = testdir.inline_runsource(""" + rec = testdir.inline_runsource( + """ import pytest class TestClass(object): @@ -251,18 +313,19 @@ class BaseFunctionalTests(object): def test_method(self): assert True - """) + """ + ) reps = rec.getreports("pytest_runtest_logreport") print(reps) assert len(reps) == 3 # assert reps[0].nodeid.endswith("test_method") assert reps[0].passed - assert reps[0].when == 'setup' + assert reps[0].when == "setup" # assert reps[1].nodeid.endswith("test_method") assert reps[1].passed - assert reps[1].when == 'call' + assert reps[1].when == "call" # assert reps[2].nodeid.endswith("test_method") assert reps[2].failed @@ -271,22 +334,26 @@ class BaseFunctionalTests(object): # python3 error "TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'", # python2 error - 'TypeError: teardown_method() takes exactly 4 arguments (2 given)' + "TypeError: teardown_method() takes exactly 4 arguments (2 given)", ) def test_failure_in_setup_function_ignores_custom_repr(self, testdir): - testdir.makepyfile(conftest=""" + testdir.makepyfile( + conftest=""" import pytest class Function(pytest.Function): def repr_failure(self, excinfo): assert 0 - """) - reports = testdir.runitem(""" + """ + ) + reports = testdir.runitem( + """ def setup_function(func): raise ValueError(42) def test_func(): pass - """) + """ + ) assert len(reports) == 2 rep = reports[0] print(rep) @@ -300,10 +367,12 @@ class BaseFunctionalTests(object): def test_systemexit_does_not_bail_out(self, testdir): try: - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): raise SystemExit(42) - """) + """ + ) except SystemExit: pytest.fail("runner did not catch SystemExit") rep = reports[1] @@ -312,11 +381,13 @@ class BaseFunctionalTests(object): def test_exit_propagates(self, testdir): try: - testdir.runitem(""" + testdir.runitem( + """ import pytest def test_func(): raise pytest.exit.Exception() - """) + """ + ) except pytest.exit.Exception: pass else: @@ -324,17 +395,22 @@ class BaseFunctionalTests(object): class TestExecutionNonForked(BaseFunctionalTests): + def getrunner(self): + def f(item): return runner.runtestprotocol(item, log=False) + return f def test_keyboardinterrupt_propagates(self, testdir): try: - testdir.runitem(""" + testdir.runitem( + """ def test_func(): raise KeyboardInterrupt("fake") - """) + """ + ) except KeyboardInterrupt: pass else: @@ -350,24 +426,29 @@ class TestExecutionForked(BaseFunctionalTests): return boxed.forked_run_report def test_suicide(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): import os os.kill(os.getpid(), 15) - """) + """ + ) rep = reports[0] assert rep.failed assert rep.when == "???" class TestSessionReports(object): + def test_collect_result(self, testdir): - col = testdir.getmodulecol(""" + col = testdir.getmodulecol( + """ def test_func1(): pass class TestClass(object): pass - """) + """ + ) rep = runner.collect_one_node(col) assert not rep.failed assert not rep.skipped @@ -390,35 +471,39 @@ reporttypes = [ ] -@pytest.mark.parametrize('reporttype', reporttypes, ids=[x.__name__ for x in reporttypes]) +@pytest.mark.parametrize( + "reporttype", reporttypes, ids=[x.__name__ for x in reporttypes] +) def test_report_extra_parameters(reporttype): - if hasattr(py.std.inspect, 'signature'): - args = list(py.std.inspect.signature(reporttype.__init__).parameters.keys())[1:] + if hasattr(inspect, "signature"): + args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:] else: - args = py.std.inspect.getargspec(reporttype.__init__)[0][1:] + args = inspect.getargspec(reporttype.__init__)[0][1:] basekw = dict.fromkeys(args, []) report = reporttype(newthing=1, **basekw) assert report.newthing == 1 def test_callinfo(): - ci = runner.CallInfo(lambda: 0, '123') + ci = runner.CallInfo(lambda: 0, "123") assert ci.when == "123" assert ci.result == 0 assert "result" in repr(ci) - ci = runner.CallInfo(lambda: 0 / 0, '123') + ci = runner.CallInfo(lambda: 0 / 0, "123") assert ci.when == "123" - assert not hasattr(ci, 'result') + assert not hasattr(ci, "result") assert ci.excinfo assert "exc" in repr(ci) + # design question: do we want general hooks in python files? # then something like the following functional tests makes sense @pytest.mark.xfail def test_runtest_in_module_ordering(testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def pytest_runtest_setup(item): # runs after class-level! item.function.mylist.append("module") @@ -441,22 +526,21 @@ def test_runtest_in_module_ordering(testdir): assert mylist == ['class', 'module'], mylist def pytest_runtest_teardown(item): del item.function.mylist - """) + """ + ) result = testdir.runpytest(p1) - result.stdout.fnmatch_lines([ - "*2 passed*" - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) def test_outcomeexception_exceptionattributes(): - outcome = outcomes.OutcomeException('test') + outcome = outcomes.OutcomeException("test") assert outcome.args[0] == outcome.msg def test_outcomeexception_passes_except_Exception(): with pytest.raises(outcomes.OutcomeException): try: - raise outcomes.OutcomeException('test') + raise outcomes.OutcomeException("test") except Exception: pass @@ -479,72 +563,76 @@ def test_pytest_fail(): def test_pytest_exit_msg(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest def pytest_configure(config): pytest.exit('oh noes') - """) + """ + ) result = testdir.runpytest() - result.stderr.fnmatch_lines([ - "Exit: oh noes", - ]) + result.stderr.fnmatch_lines(["Exit: oh noes"]) def test_pytest_fail_notrace(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_hello(): pytest.fail("hello", pytrace=False) def teardown_function(function): pytest.fail("world", pytrace=False) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "world", - "hello", - ]) - assert 'def teardown_function' not in result.stdout.str() + result.stdout.fnmatch_lines(["world", "hello"]) + assert "def teardown_function" not in result.stdout.str() -@pytest.mark.parametrize('str_prefix', ['u', '']) +@pytest.mark.parametrize("str_prefix", ["u", ""]) def test_pytest_fail_notrace_non_ascii(testdir, str_prefix): """Fix pytest.fail with pytrace=False with non-ascii characters (#1178). This tests with native and unicode strings containing non-ascii chars. """ - testdir.makepyfile(u""" + testdir.makepyfile( + u""" # coding: utf-8 import pytest def test_hello(): pytest.fail(%s'oh oh: ☺', pytrace=False) - """ % str_prefix) + """ + % str_prefix + ) result = testdir.runpytest() if sys.version_info[0] >= 3: - result.stdout.fnmatch_lines(['*test_hello*', "oh oh: ☺"]) + result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"]) else: - result.stdout.fnmatch_lines(['*test_hello*', "oh oh: *"]) - assert 'def test_hello' not in result.stdout.str() + result.stdout.fnmatch_lines(["*test_hello*", "oh oh: *"]) + assert "def test_hello" not in result.stdout.str() def test_pytest_no_tests_collected_exit_status(testdir): result = testdir.runpytest() - result.stdout.fnmatch_lines('*collected 0 items*') + result.stdout.fnmatch_lines("*collected 0 items*") assert result.ret == main.EXIT_NOTESTSCOLLECTED - testdir.makepyfile(test_foo=""" + testdir.makepyfile( + test_foo=""" def test_foo(): assert 1 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines('*collected 1 item*') - result.stdout.fnmatch_lines('*1 passed*') + result.stdout.fnmatch_lines("*collected 1 item*") + result.stdout.fnmatch_lines("*1 passed*") assert result.ret == main.EXIT_OK - result = testdir.runpytest('-k nonmatch') - result.stdout.fnmatch_lines('*collected 1 item*') - result.stdout.fnmatch_lines('*1 deselected*') + result = testdir.runpytest("-k nonmatch") + result.stdout.fnmatch_lines("*collected 1 item*") + result.stdout.fnmatch_lines("*1 deselected*") assert result.ret == main.EXIT_NOTESTSCOLLECTED @@ -564,10 +652,10 @@ def test_importorskip(monkeypatch): importorskip("asdlkj") try: - sys = importorskip("sys") # noqa - assert sys == py.std.sys + sysmod = importorskip("sys") + assert sysmod is sys # path = pytest.importorskip("os.path") - # assert path == py.std.os.path + # assert path == os.path excinfo = pytest.raises(pytest.skip.Exception, f) path = py.path.local(excinfo.getrepr().reprcrash.path) # check that importorskip reports the actual call @@ -575,12 +663,15 @@ def test_importorskip(monkeypatch): assert path.purebasename == "test_runner" pytest.raises(SyntaxError, "pytest.importorskip('x y z')") pytest.raises(SyntaxError, "pytest.importorskip('x=y')") - mod = py.std.types.ModuleType("hello123") + mod = types.ModuleType("hello123") mod.__version__ = "1.3" monkeypatch.setitem(sys.modules, "hello123", mod) - pytest.raises(pytest.skip.Exception, """ + pytest.raises( + pytest.skip.Exception, + """ pytest.importorskip("hello123", minversion="1.3.1") - """) + """, + ) mod2 = pytest.importorskip("hello123", minversion="1.3") assert mod2 == mod except pytest.skip.Exception: @@ -595,13 +686,16 @@ def test_importorskip_imports_last_module_part(): def test_importorskip_dev_module(monkeypatch): try: - mod = py.std.types.ModuleType("mockmodule") - mod.__version__ = '0.13.0.dev-43290' - monkeypatch.setitem(sys.modules, 'mockmodule', mod) - mod2 = pytest.importorskip('mockmodule', minversion='0.12.0') + mod = types.ModuleType("mockmodule") + mod.__version__ = "0.13.0.dev-43290" + monkeypatch.setitem(sys.modules, "mockmodule", mod) + mod2 = pytest.importorskip("mockmodule", minversion="0.12.0") assert mod2 == mod - pytest.raises(pytest.skip.Exception, """ - pytest.importorskip('mockmodule1', minversion='0.14.0')""") + pytest.raises( + pytest.skip.Exception, + """ + pytest.importorskip('mockmodule1', minversion='0.14.0')""", + ) except pytest.skip.Exception: print(_pytest._code.ExceptionInfo()) pytest.fail("spurious skip") @@ -609,26 +703,31 @@ def test_importorskip_dev_module(monkeypatch): def test_importorskip_module_level(testdir): """importorskip must be able to skip entire modules when used at module level""" - testdir.makepyfile(''' + testdir.makepyfile( + """ import pytest foobarbaz = pytest.importorskip("foobarbaz") def test_foo(): pass - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['*collected 0 items / 1 skipped*']) + result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"]) def test_pytest_cmdline_main(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def test_hello(): assert 1 if __name__ == '__main__': pytest.cmdline.main([__file__]) - """) + """ + ) import subprocess + popen = subprocess.Popen([sys.executable, str(p)], stdout=subprocess.PIPE) popen.communicate() ret = popen.wait() @@ -636,59 +735,71 @@ def test_pytest_cmdline_main(testdir): def test_unicode_in_longrepr(testdir): - testdir.makeconftest(""" - import py - def pytest_runtest_makereport(__multicall__): - rep = __multicall__.execute() + testdir.makeconftest( + """ + # -*- coding: utf-8 -*- + import pytest + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_makereport(): + outcome = yield + rep = outcome.get_result() if rep.when == "call": - rep.longrepr = py.builtin._totext("\\xc3\\xa4", "utf8") - return rep - """) - testdir.makepyfile(""" + rep.longrepr = u'ä' + """ + ) + testdir.makepyfile( + """ def test_out(): assert 0 - """) + """ + ) result = testdir.runpytest() assert result.ret == 1 assert "UnicodeEncodeError" not in result.stderr.str() def test_failure_in_setup(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def setup_module(): 0/0 def test_func(): pass - """) + """ + ) result = testdir.runpytest("--tb=line") assert "def setup_module" not in result.stdout.str() def test_makereport_getsource(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foo(): if False: pass else: assert False - """) + """ + ) result = testdir.runpytest() - assert 'INTERNALERROR' not in result.stdout.str() - result.stdout.fnmatch_lines(['*else: assert False*']) + assert "INTERNALERROR" not in result.stdout.str() + result.stdout.fnmatch_lines(["*else: assert False*"]) def test_makereport_getsource_dynamic_code(testdir, monkeypatch): """Test that exception in dynamically generated code doesn't break getting the source line.""" import inspect + original_findsource = inspect.findsource def findsource(obj, *args, **kwargs): # Can be triggered by dynamically created functions - if obj.__name__ == 'foo': + if obj.__name__ == "foo": raise IndexError() return original_findsource(obj, *args, **kwargs) - monkeypatch.setattr(inspect, 'findsource', findsource) + monkeypatch.setattr(inspect, "findsource", findsource) - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -697,36 +808,50 @@ def test_makereport_getsource_dynamic_code(testdir, monkeypatch): def test_fix(foo): assert False - """) - result = testdir.runpytest('-vv') - assert 'INTERNALERROR' not in result.stdout.str() + """ + ) + result = testdir.runpytest("-vv") + assert "INTERNALERROR" not in result.stdout.str() result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"]) -def test_store_except_info_on_eror(): +def test_store_except_info_on_error(): """ Test that upon test failure, the exception info is stored on sys.last_traceback and friends. """ - # Simulate item that raises a specific exception - class ItemThatRaises(object): - nodeid = 'item_that_raises' + # Simulate item that might raise a specific exception, depending on `raise_error` class var + class ItemMightRaise(object): + nodeid = "item_that_raises" + raise_error = True def runtest(self): - raise IndexError('TEST') + if self.raise_error: + raise IndexError("TEST") + try: - runner.pytest_runtest_call(ItemThatRaises()) + runner.pytest_runtest_call(ItemMightRaise()) except IndexError: pass # Check that exception info is stored on sys assert sys.last_type is IndexError - assert sys.last_value.args[0] == 'TEST' + assert sys.last_value.args[0] == "TEST" assert sys.last_traceback + # The next run should clear the exception info stored by the previous run + ItemMightRaise.raise_error = False + runner.pytest_runtest_call(ItemMightRaise()) + assert sys.last_type is None + assert sys.last_value is None + assert sys.last_traceback is None + def test_current_test_env_var(testdir, monkeypatch): pytest_current_test_vars = [] - monkeypatch.setattr(sys, 'pytest_current_test_vars', pytest_current_test_vars, raising=False) - testdir.makepyfile(''' + monkeypatch.setattr( + sys, "pytest_current_test_vars", pytest_current_test_vars, raising=False + ) + testdir.makepyfile( + """ import pytest import sys import os @@ -739,13 +864,20 @@ def test_current_test_env_var(testdir, monkeypatch): def test(fix): sys.pytest_current_test_vars.append(('call', os.environ['PYTEST_CURRENT_TEST'])) - ''') + """ + ) result = testdir.runpytest_inprocess() assert result.ret == 0 - test_id = 'test_current_test_env_var.py::test' - assert pytest_current_test_vars == [ - ('setup', test_id + ' (setup)'), ('call', test_id + ' (call)'), ('teardown', test_id + ' (teardown)')] - assert 'PYTEST_CURRENT_TEST' not in os.environ + test_id = "test_current_test_env_var.py::test" + assert ( + pytest_current_test_vars + == [ + ("setup", test_id + " (setup)"), + ("call", test_id + " (call)"), + ("teardown", test_id + " (teardown)"), + ] + ) + assert "PYTEST_CURRENT_TEST" not in os.environ class TestReportContents(object): @@ -757,24 +889,29 @@ class TestReportContents(object): return lambda item: runner.runtestprotocol(item, log=False) def test_longreprtext_pass(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): pass - """) + """ + ) rep = reports[1] - assert rep.longreprtext == '' + assert rep.longreprtext == "" def test_longreprtext_failure(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): x = 1 assert x == 4 - """) + """ + ) rep = reports[1] - assert 'assert 1 == 4' in rep.longreprtext + assert "assert 1 == 4" in rep.longreprtext def test_captured_text(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ import pytest import sys @@ -791,21 +928,24 @@ class TestReportContents(object): sys.stdout.write('call: stdout\\n') sys.stderr.write('call: stderr\\n') assert 0 - """) + """ + ) setup, call, teardown = reports - assert setup.capstdout == 'setup: stdout\n' - assert call.capstdout == 'setup: stdout\ncall: stdout\n' - assert teardown.capstdout == 'setup: stdout\ncall: stdout\nteardown: stdout\n' + assert setup.capstdout == "setup: stdout\n" + assert call.capstdout == "setup: stdout\ncall: stdout\n" + assert teardown.capstdout == "setup: stdout\ncall: stdout\nteardown: stdout\n" - assert setup.capstderr == 'setup: stderr\n' - assert call.capstderr == 'setup: stderr\ncall: stderr\n' - assert teardown.capstderr == 'setup: stderr\ncall: stderr\nteardown: stderr\n' + assert setup.capstderr == "setup: stderr\n" + assert call.capstderr == "setup: stderr\ncall: stderr\n" + assert teardown.capstderr == "setup: stderr\ncall: stderr\nteardown: stderr\n" def test_no_captured_text(self, testdir): - reports = testdir.runitem(""" + reports = testdir.runitem( + """ def test_func(): pass - """) + """ + ) rep = reports[1] - assert rep.capstdout == '' - assert rep.capstderr == '' + assert rep.capstdout == "" + assert rep.capstderr == "" diff --git a/third_party/python/pytest/testing/test_runner_xunit.py b/third_party/python/pytest/testing/test_runner_xunit.py index fc931f86720a..8316aafbf561 100644 --- a/third_party/python/pytest/testing/test_runner_xunit.py +++ b/third_party/python/pytest/testing/test_runner_xunit.py @@ -7,7 +7,8 @@ import pytest def test_module_and_function_setup(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ modlevel = [] def setup_module(module): assert not modlevel @@ -30,7 +31,8 @@ def test_module_and_function_setup(testdir): def test_module(self): assert modlevel[0] == 42 assert not hasattr(test_modlevel, 'answer') - """) + """ + ) rep = reprec.matchreport("test_modlevel") assert rep.passed rep = reprec.matchreport("test_module") @@ -38,7 +40,8 @@ def test_module_and_function_setup(testdir): def test_module_setup_failure_no_teardown(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ values = [] def setup_module(module): values.append(1) @@ -49,14 +52,16 @@ def test_module_setup_failure_no_teardown(testdir): def teardown_module(module): values.append(2) - """) + """ + ) reprec.assertoutcome(failed=1) calls = reprec.getcalls("pytest_runtest_setup") assert calls[0].item.module.values == [1] def test_setup_function_failure_no_teardown(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ modlevel = [] def setup_function(function): modlevel.append(1) @@ -67,13 +72,15 @@ def test_setup_function_failure_no_teardown(testdir): def test_func(): pass - """) + """ + ) calls = reprec.getcalls("pytest_runtest_setup") assert calls[0].item.module.modlevel == [1] def test_class_setup(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ class TestSimpleClassSetup(object): clslevel = [] def setup_class(cls): @@ -92,12 +99,14 @@ def test_class_setup(testdir): def test_cleanup(): assert not TestSimpleClassSetup.clslevel assert not TestInheritedClassSetupStillWorks.clslevel - """) + """ + ) reprec.assertoutcome(passed=1 + 2 + 1) def test_class_setup_failure_no_teardown(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ class TestSimpleClassSetup(object): clslevel = [] def setup_class(cls): @@ -111,12 +120,14 @@ def test_class_setup_failure_no_teardown(testdir): def test_cleanup(): assert not TestSimpleClassSetup.clslevel - """) + """ + ) reprec.assertoutcome(failed=1, passed=1) def test_method_setup(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ class TestSetupMethod(object): def setup_method(self, meth): self.methsetup = meth @@ -128,12 +139,14 @@ def test_method_setup(testdir): def test_other(self): assert self.methsetup == self.test_other - """) + """ + ) reprec.assertoutcome(passed=2) def test_method_setup_failure_no_teardown(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ class TestMethodSetup(object): clslevel = [] def setup_method(self, method): @@ -148,12 +161,14 @@ def test_method_setup_failure_no_teardown(testdir): def test_cleanup(): assert TestMethodSetup.clslevel == [1] - """) + """ + ) reprec.assertoutcome(failed=1, passed=1) def test_method_generator_setup(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ class TestSetupTeardownOnInstance(object): def setup_class(cls): cls.classsetup = True @@ -171,12 +186,14 @@ def test_method_generator_setup(testdir): assert self.classsetup assert self.methsetup == self.test_generate assert value == 5 - """) + """ + ) reprec.assertoutcome(passed=1, failed=1) def test_func_generator_setup(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ import sys def setup_module(mod): @@ -199,13 +216,15 @@ def test_func_generator_setup(testdir): assert x == [1] yield check assert x == [1] - """) + """ + ) rep = reprec.matchreport("test_one", names="pytest_runtest_logreport") assert rep.passed def test_method_setup_uses_fresh_instances(testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ class TestSelfState1(object): memory = [] def test_hello(self): @@ -213,12 +232,14 @@ def test_method_setup_uses_fresh_instances(testdir): def test_afterhello(self): assert self != self.memory[0] - """) + """ + ) reprec.assertoutcome(passed=2, failed=0) def test_setup_that_skips_calledagain(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def setup_module(mod): pytest.skip("x") @@ -226,13 +247,15 @@ def test_setup_that_skips_calledagain(testdir): pass def test_function2(): pass - """) + """ + ) reprec = testdir.inline_run(p) reprec.assertoutcome(skipped=2) def test_setup_fails_again_on_all_tests(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def setup_module(mod): raise ValueError(42) @@ -240,13 +263,15 @@ def test_setup_fails_again_on_all_tests(testdir): pass def test_function2(): pass - """) + """ + ) reprec = testdir.inline_run(p) reprec.assertoutcome(failed=2) def test_setup_funcarg_setup_when_outer_scope_fails(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def setup_module(mod): raise ValueError(42) @@ -257,25 +282,34 @@ def test_setup_funcarg_setup_when_outer_scope_fails(testdir): pass def test_function2(hello): pass - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*function1*", - "*ValueError*42*", - "*function2*", - "*ValueError*42*", - "*2 error*" - ]) + result.stdout.fnmatch_lines( + [ + "*function1*", + "*ValueError*42*", + "*function2*", + "*ValueError*42*", + "*2 error*", + ] + ) assert "xyz43" not in result.stdout.str() -@pytest.mark.parametrize('arg', ['', 'arg']) -def test_setup_teardown_function_level_with_optional_argument(testdir, monkeypatch, arg): +@pytest.mark.parametrize("arg", ["", "arg"]) +def test_setup_teardown_function_level_with_optional_argument( + testdir, monkeypatch, arg +): """parameter to setup/teardown xunit-style functions parameter is now optional (#1728).""" import sys + trace_setups_teardowns = [] - monkeypatch.setattr(sys, 'trace_setups_teardowns', trace_setups_teardowns, raising=False) - p = testdir.makepyfile(""" + monkeypatch.setattr( + sys, "trace_setups_teardowns", trace_setups_teardowns, raising=False + ) + p = testdir.makepyfile( + """ import pytest import sys @@ -296,24 +330,23 @@ def test_setup_teardown_function_level_with_optional_argument(testdir, monkeypat def test_method_1(self): pass def test_method_2(self): pass - """.format(arg=arg)) + """.format( + arg=arg + ) + ) result = testdir.inline_run(p) result.assertoutcome(passed=4) expected = [ - 'setup_module', - - 'setup_function', - 'teardown_function', - 'setup_function', - 'teardown_function', - - 'setup_method', - 'teardown_method', - - 'setup_method', - 'teardown_method', - - 'teardown_module', + "setup_module", + "setup_function", + "teardown_function", + "setup_function", + "teardown_function", + "setup_method", + "teardown_method", + "setup_method", + "teardown_method", + "teardown_module", ] assert trace_setups_teardowns == expected diff --git a/third_party/python/pytest/testing/test_session.py b/third_party/python/pytest/testing/test_session.py index 9ec13f523e65..4a594009bab5 100644 --- a/third_party/python/pytest/testing/test_session.py +++ b/third_party/python/pytest/testing/test_session.py @@ -1,12 +1,15 @@ from __future__ import absolute_import, division, print_function + import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED class SessionTests(object): + def test_basic_testitem_events(self, testdir): - tfile = testdir.makepyfile(""" + tfile = testdir.makepyfile( + """ def test_one(): pass def test_one_one(): @@ -16,7 +19,8 @@ class SessionTests(object): class TestClass(object): def test_two(self, someargs): pass - """) + """ + ) reprec = testdir.inline_run(tfile) passed, skipped, failed = reprec.listoutcomes() assert len(skipped) == 0 @@ -36,26 +40,31 @@ class SessionTests(object): # assert colreports[1].report.failed def test_nested_import_error(self, testdir): - tfile = testdir.makepyfile(""" + tfile = testdir.makepyfile( + """ import import_fails def test_this(): assert import_fails.a == 1 - """, import_fails=""" + """, + import_fails=""" import does_not_work a = 1 - """) + """, + ) reprec = testdir.inline_run(tfile) values = reprec.getfailedcollections() assert len(values) == 1 out = str(values[0].longrepr) - assert out.find('does_not_work') != -1 + assert out.find("does_not_work") != -1 def test_raises_output(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ import pytest def test_raises_doesnt(): pytest.raises(ValueError, int, "3") - """) + """ + ) passed, skipped, failed = reprec.listoutcomes() assert len(failed) == 1 out = failed[0].longrepr.reprcrash.message @@ -64,13 +73,15 @@ class SessionTests(object): pytest.fail("incorrect raises() output") def test_generator_yields_None(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ def test_1(): yield None - """) + """ + ) failures = reprec.getfailedcollections() out = failures[0].longrepr.reprcrash.message - i = out.find('TypeError') + i = out.find("TypeError") assert i != -1 def test_syntax_error_module(self, testdir): @@ -78,29 +89,36 @@ class SessionTests(object): values = reprec.getfailedcollections() assert len(values) == 1 out = str(values[0].longrepr) - assert out.find(str('not python')) != -1 + assert out.find(str("not python")) != -1 def test_exit_first_problem(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ def test_one(): assert 0 def test_two(): assert 0 - """, '--exitfirst') + """, + "--exitfirst", + ) passed, skipped, failed = reprec.countoutcomes() assert failed == 1 assert passed == skipped == 0 def test_maxfail(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ def test_one(): assert 0 def test_two(): assert 0 def test_three(): assert 0 - """, '--maxfail=2') + """, + "--maxfail=2", + ) passed, skipped, failed = reprec.countoutcomes() assert failed == 2 assert passed == skipped == 0 def test_broken_repr(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest class BrokenRepr1(object): foo=0 @@ -116,21 +134,30 @@ class SessionTests(object): t = BrokenRepr1() assert t.foo == 1 - """) + """ + ) reprec = testdir.inline_run(p) passed, skipped, failed = reprec.listoutcomes() assert len(failed) == 1 out = failed[0].longrepr.reprcrash.message - assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 # ' + assert ( + out.find( + """[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""" + ) + != -1 + ) # ' def test_skip_file_by_conftest(self, testdir): - testdir.makepyfile(conftest=""" + testdir.makepyfile( + conftest=""" import pytest def pytest_collect_file(): pytest.skip("intentional") - """, test_file=""" + """, + test_file=""" def test_one(): pass - """) + """, + ) try: reprec = testdir.inline_run(testdir.tmpdir) except pytest.skip.Exception: @@ -143,7 +170,8 @@ class SessionTests(object): class TestNewSession(SessionTests): def test_order_of_execution(self, testdir): - reprec = testdir.inline_runsource(""" + reprec = testdir.inline_runsource( + """ values = [] def test_1(): values.append(1) @@ -161,7 +189,8 @@ class TestNewSession(SessionTests): self.reslist.append(3) def test_4(self): assert self.reslist == [1,2,1,2,3] - """) + """ + ) passed, skipped, failed = reprec.countoutcomes() assert failed == skipped == 0 assert passed == 7 @@ -181,9 +210,9 @@ class TestNewSession(SessionTests): pass """, test_three="xxxdsadsadsadsa", - __init__="" + __init__="", ) - reprec = testdir.inline_run('--collect-only', p.dirpath()) + reprec = testdir.inline_run("--collect-only", p.dirpath()) itemstarted = reprec.getcalls("pytest_itemcollected") assert len(itemstarted) == 3 @@ -213,9 +242,12 @@ class TestNewSession(SessionTests): def test_plugin_specify(testdir): - pytest.raises(ImportError, """ + pytest.raises( + ImportError, + """ testdir.parseconfig("-p", "nqweotexistent") - """) + """, + ) # pytest.raises(ImportError, # "config.do_configure(config)" # ) @@ -223,7 +255,7 @@ def test_plugin_specify(testdir): def test_plugin_already_exists(testdir): config = testdir.parseconfig("-p", "terminal") - assert config.option.plugins == ['terminal'] + assert config.option.plugins == ["terminal"] config._do_configure() config._ensure_unconfigure() @@ -239,8 +271,27 @@ def test_exclude(testdir): result.stdout.fnmatch_lines(["*1 passed*"]) +def test_deselect(testdir): + testdir.makepyfile( + test_a=""" + import pytest + def test_a1(): pass + @pytest.mark.parametrize('b', range(3)) + def test_a2(b): pass + """ + ) + result = testdir.runpytest( + "-v", "--deselect=test_a.py::test_a2[1]", "--deselect=test_a.py::test_a2[2]" + ) + assert result.ret == 0 + result.stdout.fnmatch_lines(["*2 passed, 2 deselected*"]) + for line in result.stdout.lines: + assert not line.startswith(("test_a.py::test_a2[1]", "test_a.py::test_a2[2]")) + + def test_sessionfinish_with_start(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ import os values = [] def pytest_sessionstart(): @@ -250,6 +301,43 @@ def test_sessionfinish_with_start(testdir): def pytest_sessionfinish(): assert values[0] == os.getcwd() - """) + """ + ) res = testdir.runpytest("--collect-only") assert res.ret == EXIT_NOTESTSCOLLECTED + + +@pytest.mark.parametrize("path", ["root", "{relative}/root", "{environment}/root"]) +def test_rootdir_option_arg(testdir, monkeypatch, path): + monkeypatch.setenv("PY_ROOTDIR_PATH", str(testdir.tmpdir)) + path = path.format(relative=str(testdir.tmpdir), environment="$PY_ROOTDIR_PATH") + + rootdir = testdir.mkdir("root") + rootdir.mkdir("tests") + testdir.makepyfile( + """ + import os + def test_one(): + assert 1 + """ + ) + + result = testdir.runpytest("--rootdir={}".format(path)) + result.stdout.fnmatch_lines( + ["*rootdir: {}/root, inifile:*".format(testdir.tmpdir), "*1 passed*"] + ) + + +def test_rootdir_wrong_option_arg(testdir): + testdir.makepyfile( + """ + import os + def test_one(): + assert 1 + """ + ) + + result = testdir.runpytest("--rootdir=wrong_dir") + result.stderr.fnmatch_lines( + ["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"] + ) diff --git a/third_party/python/pytest/testing/test_skipping.py b/third_party/python/pytest/testing/test_skipping.py index a25c9460a803..5d970e2fea0a 100644 --- a/third_party/python/pytest/testing/test_skipping.py +++ b/third_party/python/pytest/testing/test_skipping.py @@ -7,20 +7,23 @@ from _pytest.runner import runtestprotocol class TestEvaluator(object): + def test_no_marker(self, testdir): item = testdir.getitem("def test_func(): pass") - evalskipif = MarkEvaluator(item, 'skipif') + evalskipif = MarkEvaluator(item, "skipif") assert not evalskipif assert not evalskipif.istrue() def test_marked_no_args(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xyz def test_func(): pass - """) - ev = MarkEvaluator(item, 'xyz') + """ + ) + ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() @@ -28,40 +31,46 @@ class TestEvaluator(object): assert not ev.get("run", False) def test_marked_one_arg(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xyz("hasattr(os, 'sep')") def test_func(): pass - """) - ev = MarkEvaluator(item, 'xyz') + """ + ) + ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: hasattr(os, 'sep')" - @pytest.mark.skipif('sys.version_info[0] >= 3') + @pytest.mark.skipif("sys.version_info[0] >= 3") def test_marked_one_arg_unicode(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xyz(u"hasattr(os, 'sep')") def test_func(): pass - """) - ev = MarkEvaluator(item, 'xyz') + """ + ) + ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: hasattr(os, 'sep')" def test_marked_one_arg_with_reason(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xyz("hasattr(os, 'sep')", attr=2, reason="hello world") def test_func(): pass - """) - ev = MarkEvaluator(item, 'xyz') + """ + ) + ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() @@ -70,58 +79,67 @@ class TestEvaluator(object): def test_marked_one_arg_twice(self, testdir): lines = [ - '''@pytest.mark.skipif("not hasattr(os, 'murks')")''', - '''@pytest.mark.skipif("hasattr(os, 'murks')")''' + """@pytest.mark.skipif("not hasattr(os, 'murks')")""", + """@pytest.mark.skipif("hasattr(os, 'murks')")""", ] for i in range(0, 2): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest %s %s def test_func(): pass - """ % (lines[i], lines[(i + 1) % 2])) - ev = MarkEvaluator(item, 'skipif') + """ + % (lines[i], lines[(i + 1) % 2]) + ) + ev = MarkEvaluator(item, "skipif") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: not hasattr(os, 'murks')" def test_marked_one_arg_twice2(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.skipif("hasattr(os, 'murks')") @pytest.mark.skipif("not hasattr(os, 'murks')") def test_func(): pass - """) - ev = MarkEvaluator(item, 'skipif') + """ + ) + ev = MarkEvaluator(item, "skipif") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: not hasattr(os, 'murks')" def test_marked_skip_with_not_string(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.skipif(False) def test_func(): pass - """) - ev = MarkEvaluator(item, 'skipif') + """ + ) + ev = MarkEvaluator(item, "skipif") exc = pytest.raises(pytest.fail.Exception, ev.istrue) assert """Failed: you need to specify reason=STRING when using booleans as conditions.""" in exc.value.msg def test_skipif_class(self, testdir): - item, = testdir.getitems(""" + item, = testdir.getitems( + """ import pytest class TestClass(object): pytestmark = pytest.mark.skipif("config._hackxyz") def test_func(self): pass - """) + """ + ) item.config._hackxyz = 3 - ev = MarkEvaluator(item, 'skipif') + ev = MarkEvaluator(item, "skipif") assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: config._hackxyz" @@ -129,14 +147,17 @@ class TestEvaluator(object): class TestXFail(object): - @pytest.mark.parametrize('strict', [True, False]) + @pytest.mark.parametrize("strict", [True, False]) def test_xfail_simple(self, testdir, strict): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xfail(strict=%s) def test_func(): assert 0 - """ % strict) + """ + % strict + ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] @@ -144,25 +165,46 @@ class TestXFail(object): assert callreport.wasxfail == "" def test_xfail_xpassed(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xfail(reason="this is an xfail") def test_func(): assert 1 - """) + """ + ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] assert callreport.passed assert callreport.wasxfail == "this is an xfail" + def test_xfail_using_platform(self, testdir): + """ + Verify that platform can be used with xfail statements. + """ + item = testdir.getitem( + """ + import pytest + @pytest.mark.xfail("platform.platform() == platform.platform()") + def test_func(): + assert 0 + """ + ) + reports = runtestprotocol(item, log=False) + assert len(reports) == 3 + callreport = reports[1] + assert callreport.wasxfail + def test_xfail_xpassed_strict(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xfail(strict=True, reason="nope") def test_func(): assert 1 - """) + """ + ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] @@ -171,48 +213,53 @@ class TestXFail(object): assert not hasattr(callreport, "wasxfail") def test_xfail_run_anyway(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.xfail def test_func(): assert 0 def test_func2(): pytest.xfail("hello") - """) + """ + ) result = testdir.runpytest("--runxfail") - result.stdout.fnmatch_lines([ - "*def test_func():*", - "*assert 0*", - "*1 failed*1 pass*", - ]) + result.stdout.fnmatch_lines( + ["*def test_func():*", "*assert 0*", "*1 failed*1 pass*"] + ) def test_xfail_evalfalse_but_fails(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.xfail('False') def test_func(): assert 0 - """) + """ + ) reports = runtestprotocol(item, log=False) callreport = reports[1] assert callreport.failed assert not hasattr(callreport, "wasxfail") - assert 'xfail' in callreport.keywords + assert "xfail" in callreport.keywords def test_xfail_not_report_default(self, testdir): - p = testdir.makepyfile(test_one=""" + p = testdir.makepyfile( + test_one=""" import pytest @pytest.mark.xfail def test_this(): assert 0 - """) - testdir.runpytest(p, '-v') + """ + ) + testdir.runpytest(p, "-v") # result.stdout.fnmatch_lines([ # "*HINT*use*-r*" # ]) def test_xfail_not_run_xfail_reporting(self, testdir): - p = testdir.makepyfile(test_one=""" + p = testdir.makepyfile( + test_one=""" import pytest @pytest.mark.xfail(run=False, reason="noway") def test_this(): @@ -223,90 +270,89 @@ class TestXFail(object): @pytest.mark.xfail("False", run=False, reason="huh") def test_this_false(): assert 1 - """) - result = testdir.runpytest(p, '-rx', ) - result.stdout.fnmatch_lines([ - "*test_one*test_this*", - "*NOTRUN*noway", - "*test_one*test_this_true*", - "*NOTRUN*condition:*True*", - "*1 passed*", - ]) + """ + ) + result = testdir.runpytest(p, "-rx") + result.stdout.fnmatch_lines( + [ + "*test_one*test_this*", + "*NOTRUN*noway", + "*test_one*test_this_true*", + "*NOTRUN*condition:*True*", + "*1 passed*", + ] + ) def test_xfail_not_run_no_setup_run(self, testdir): - p = testdir.makepyfile(test_one=""" + p = testdir.makepyfile( + test_one=""" import pytest @pytest.mark.xfail(run=False, reason="hello") def test_this(): assert 0 def setup_module(mod): raise ValueError(42) - """) - result = testdir.runpytest(p, '-rx', ) - result.stdout.fnmatch_lines([ - "*test_one*test_this*", - "*NOTRUN*hello", - "*1 xfailed*", - ]) + """ + ) + result = testdir.runpytest(p, "-rx") + result.stdout.fnmatch_lines( + ["*test_one*test_this*", "*NOTRUN*hello", "*1 xfailed*"] + ) def test_xfail_xpass(self, testdir): - p = testdir.makepyfile(test_one=""" + p = testdir.makepyfile( + test_one=""" import pytest @pytest.mark.xfail def test_that(): assert 1 - """) - result = testdir.runpytest(p, '-rX') - result.stdout.fnmatch_lines([ - "*XPASS*test_that*", - "*1 xpassed*" - ]) + """ + ) + result = testdir.runpytest(p, "-rX") + result.stdout.fnmatch_lines(["*XPASS*test_that*", "*1 xpassed*"]) assert result.ret == 0 def test_xfail_imperative(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def test_this(): pytest.xfail("hello") - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*1 xfailed*", - ]) + result.stdout.fnmatch_lines(["*1 xfailed*"]) result = testdir.runpytest(p, "-rx") - result.stdout.fnmatch_lines([ - "*XFAIL*test_this*", - "*reason:*hello*", - ]) + result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*reason:*hello*"]) result = testdir.runpytest(p, "--runxfail") result.stdout.fnmatch_lines("*1 pass*") def test_xfail_imperative_in_setup_function(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def setup_function(function): pytest.xfail("hello") def test_this(): assert 0 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*1 xfailed*", - ]) + result.stdout.fnmatch_lines(["*1 xfailed*"]) result = testdir.runpytest(p, "-rx") - result.stdout.fnmatch_lines([ - "*XFAIL*test_this*", - "*reason:*hello*", - ]) + result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*reason:*hello*"]) result = testdir.runpytest(p, "--runxfail") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *def test_this* *1 fail* - """) + """ + ) def xtest_dynamic_xfail_set_during_setup(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def setup_function(function): pytest.mark.xfail(function) @@ -314,147 +360,164 @@ class TestXFail(object): assert 0 def test_that(): assert 1 - """) - result = testdir.runpytest(p, '-rxX') - result.stdout.fnmatch_lines([ - "*XFAIL*test_this*", - "*XPASS*test_that*", - ]) + """ + ) + result = testdir.runpytest(p, "-rxX") + result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*XPASS*test_that*"]) def test_dynamic_xfail_no_run(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.fixture def arg(request): request.applymarker(pytest.mark.xfail(run=False)) def test_this(arg): assert 0 - """) - result = testdir.runpytest(p, '-rxX') - result.stdout.fnmatch_lines([ - "*XFAIL*test_this*", - "*NOTRUN*", - ]) + """ + ) + result = testdir.runpytest(p, "-rxX") + result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*NOTRUN*"]) def test_dynamic_xfail_set_during_funcarg_setup(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.fixture def arg(request): request.applymarker(pytest.mark.xfail) def test_this2(arg): assert 0 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*1 xfailed*", - ]) + result.stdout.fnmatch_lines(["*1 xfailed*"]) - @pytest.mark.parametrize('expected, actual, matchline', - [('TypeError', 'TypeError', "*1 xfailed*"), - ('(AttributeError, TypeError)', 'TypeError', "*1 xfailed*"), - ('TypeError', 'IndexError', "*1 failed*"), - ('(AttributeError, TypeError)', 'IndexError', "*1 failed*"), - ]) + @pytest.mark.parametrize( + "expected, actual, matchline", + [ + ("TypeError", "TypeError", "*1 xfailed*"), + ("(AttributeError, TypeError)", "TypeError", "*1 xfailed*"), + ("TypeError", "IndexError", "*1 failed*"), + ("(AttributeError, TypeError)", "IndexError", "*1 failed*"), + ], + ) def test_xfail_raises(self, expected, actual, matchline, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail(raises=%s) def test_raises(): raise %s() - """ % (expected, actual)) + """ + % (expected, actual) + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - matchline, - ]) + result.stdout.fnmatch_lines([matchline]) def test_strict_sanity(self, testdir): """sanity check for xfail(strict=True): a failing test should behave exactly like a normal xfail. """ - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail(reason='unsupported feature', strict=True) def test_foo(): assert 0 - """) - result = testdir.runpytest(p, '-rxX') - result.stdout.fnmatch_lines([ - '*XFAIL*', - '*unsupported feature*', - ]) + """ + ) + result = testdir.runpytest(p, "-rxX") + result.stdout.fnmatch_lines(["*XFAIL*", "*unsupported feature*"]) assert result.ret == 0 - @pytest.mark.parametrize('strict', [True, False]) + @pytest.mark.parametrize("strict", [True, False]) def test_strict_xfail(self, testdir, strict): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail(reason='unsupported feature', strict=%s) def test_foo(): with open('foo_executed', 'w'): pass # make sure test executes - """ % strict) - result = testdir.runpytest(p, '-rxX') + """ + % strict + ) + result = testdir.runpytest(p, "-rxX") if strict: - result.stdout.fnmatch_lines([ - '*test_foo*', - '*XPASS(strict)*unsupported feature*', - ]) + result.stdout.fnmatch_lines( + ["*test_foo*", "*XPASS(strict)*unsupported feature*"] + ) else: - result.stdout.fnmatch_lines([ - '*test_strict_xfail*', - 'XPASS test_strict_xfail.py::test_foo unsupported feature', - ]) + result.stdout.fnmatch_lines( + [ + "*test_strict_xfail*", + "XPASS test_strict_xfail.py::test_foo unsupported feature", + ] + ) assert result.ret == (1 if strict else 0) - assert testdir.tmpdir.join('foo_executed').isfile() + assert testdir.tmpdir.join("foo_executed").isfile() - @pytest.mark.parametrize('strict', [True, False]) + @pytest.mark.parametrize("strict", [True, False]) def test_strict_xfail_condition(self, testdir, strict): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail(False, reason='unsupported feature', strict=%s) def test_foo(): pass - """ % strict) - result = testdir.runpytest(p, '-rxX') - result.stdout.fnmatch_lines('*1 passed*') + """ + % strict + ) + result = testdir.runpytest(p, "-rxX") + result.stdout.fnmatch_lines("*1 passed*") assert result.ret == 0 - @pytest.mark.parametrize('strict', [True, False]) + @pytest.mark.parametrize("strict", [True, False]) def test_xfail_condition_keyword(self, testdir, strict): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail(condition=False, reason='unsupported feature', strict=%s) def test_foo(): pass - """ % strict) - result = testdir.runpytest(p, '-rxX') - result.stdout.fnmatch_lines('*1 passed*') + """ + % strict + ) + result = testdir.runpytest(p, "-rxX") + result.stdout.fnmatch_lines("*1 passed*") assert result.ret == 0 - @pytest.mark.parametrize('strict_val', ['true', 'false']) + @pytest.mark.parametrize("strict_val", ["true", "false"]) def test_strict_xfail_default_from_file(self, testdir, strict_val): - testdir.makeini(''' + testdir.makeini( + """ [pytest] xfail_strict = %s - ''' % strict_val) - p = testdir.makepyfile(""" + """ + % strict_val + ) + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail(reason='unsupported feature') def test_foo(): pass - """) - result = testdir.runpytest(p, '-rxX') - strict = strict_val == 'true' - result.stdout.fnmatch_lines('*1 failed*' if strict else '*1 xpassed*') + """ + ) + result = testdir.runpytest(p, "-rxX") + strict = strict_val == "true" + result.stdout.fnmatch_lines("*1 failed*" if strict else "*1 xpassed*") assert result.ret == (1 if strict else 0) class TestXFailwithSetupTeardown(object): + def test_failing_setup_issue9(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def setup_function(func): assert 0 @@ -462,14 +525,14 @@ class TestXFailwithSetupTeardown(object): @pytest.mark.xfail def test_func(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 xfail*", - ]) + result.stdout.fnmatch_lines(["*1 xfail*"]) def test_failing_teardown_issue9(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def teardown_function(func): assert 0 @@ -477,16 +540,17 @@ class TestXFailwithSetupTeardown(object): @pytest.mark.xfail def test_func(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 xfail*", - ]) + result.stdout.fnmatch_lines(["*1 xfail*"]) class TestSkip(object): + def test_skip_class(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip class TestSomething(object): @@ -497,61 +561,62 @@ class TestSkip(object): def test_baz(): pass - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(skipped=2, passed=1) def test_skips_on_false_string(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip('False') def test_foo(): pass - """) + """ + ) rec = testdir.inline_run() rec.assertoutcome(skipped=1) def test_arg_as_reason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip('testing stuff') def test_bar(): pass - """) - result = testdir.runpytest('-rs') - result.stdout.fnmatch_lines([ - "*testing stuff*", - "*1 skipped*", - ]) + """ + ) + result = testdir.runpytest("-rs") + result.stdout.fnmatch_lines(["*testing stuff*", "*1 skipped*"]) def test_skip_no_reason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip def test_foo(): pass - """) - result = testdir.runpytest('-rs') - result.stdout.fnmatch_lines([ - "*unconditional skip*", - "*1 skipped*", - ]) + """ + ) + result = testdir.runpytest("-rs") + result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"]) def test_skip_with_reason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip(reason="for lolz") def test_bar(): pass - """) - result = testdir.runpytest('-rs') - result.stdout.fnmatch_lines([ - "*for lolz*", - "*1 skipped*", - ]) + """ + ) + result = testdir.runpytest("-rs") + result.stdout.fnmatch_lines(["*for lolz*", "*1 skipped*"]) def test_only_skips_marked_test(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip def test_foo(): @@ -561,92 +626,112 @@ class TestSkip(object): pass def test_baz(): assert True - """) - result = testdir.runpytest('-rs') - result.stdout.fnmatch_lines([ - "*nothing in particular*", - "*1 passed*2 skipped*", - ]) + """ + ) + result = testdir.runpytest("-rs") + result.stdout.fnmatch_lines(["*nothing in particular*", "*1 passed*2 skipped*"]) def test_strict_and_skip(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skip def test_hello(): pass - """) + """ + ) result = testdir.runpytest("-rs") - result.stdout.fnmatch_lines([ - "*unconditional skip*", - "*1 skipped*", - ]) + result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"]) class TestSkipif(object): + def test_skipif_conditional(self, testdir): - item = testdir.getitem(""" + item = testdir.getitem( + """ import pytest @pytest.mark.skipif("hasattr(os, 'sep')") def test_func(): pass - """) # noqa - x = pytest.raises(pytest.skip.Exception, lambda: - pytest_runtest_setup(item)) + """ + ) + x = pytest.raises(pytest.skip.Exception, lambda: pytest_runtest_setup(item)) assert x.value.msg == "condition: hasattr(os, 'sep')" - @pytest.mark.parametrize('params', [ - '"hasattr(sys, \'platform\')"', - 'True, reason="invalid platform"', - ]) + @pytest.mark.parametrize( + "params", ["\"hasattr(sys, 'platform')\"", 'True, reason="invalid platform"'] + ) def test_skipif_reporting(self, testdir, params): - p = testdir.makepyfile(test_foo=""" + p = testdir.makepyfile( + test_foo=""" import pytest @pytest.mark.skipif(%(params)s) def test_that(): assert 0 - """ % dict(params=params)) - result = testdir.runpytest(p, '-s', '-rs') - result.stdout.fnmatch_lines([ - "*SKIP*1*test_foo.py*platform*", - "*1 skipped*" - ]) + """ + % dict(params=params) + ) + result = testdir.runpytest(p, "-s", "-rs") + result.stdout.fnmatch_lines(["*SKIP*1*test_foo.py*platform*", "*1 skipped*"]) assert result.ret == 0 - @pytest.mark.parametrize('marker, msg1, msg2', [ - ('skipif', 'SKIP', 'skipped'), - ('xfail', 'XPASS', 'xpassed'), - ]) + def test_skipif_using_platform(self, testdir): + item = testdir.getitem( + """ + import pytest + @pytest.mark.skipif("platform.platform() == platform.platform()") + def test_func(): + pass + """ + ) + pytest.raises(pytest.skip.Exception, lambda: pytest_runtest_setup(item)) + + @pytest.mark.parametrize( + "marker, msg1, msg2", + [("skipif", "SKIP", "skipped"), ("xfail", "XPASS", "xpassed")], + ) def test_skipif_reporting_multiple(self, testdir, marker, msg1, msg2): - testdir.makepyfile(test_foo=""" + testdir.makepyfile( + test_foo=""" import pytest @pytest.mark.{marker}(False, reason='first_condition') @pytest.mark.{marker}(True, reason='second_condition') def test_foobar(): assert 1 - """.format(marker=marker)) - result = testdir.runpytest('-s', '-rsxX') - result.stdout.fnmatch_lines([ - "*{msg1}*test_foo.py*second_condition*".format(msg1=msg1), - "*1 {msg2}*".format(msg2=msg2), - ]) + """.format( + marker=marker + ) + ) + result = testdir.runpytest("-s", "-rsxX") + result.stdout.fnmatch_lines( + [ + "*{msg1}*test_foo.py*second_condition*".format(msg1=msg1), + "*1 {msg2}*".format(msg2=msg2), + ] + ) assert result.ret == 0 def test_skip_not_report_default(testdir): - p = testdir.makepyfile(test_one=""" + p = testdir.makepyfile( + test_one=""" import pytest def test_this(): pytest.skip("hello") - """) - result = testdir.runpytest(p, '-v') - result.stdout.fnmatch_lines([ - # "*HINT*use*-r*", - "*1 skipped*", - ]) + """ + ) + result = testdir.runpytest(p, "-v") + result.stdout.fnmatch_lines( + [ + # "*HINT*use*-r*", + "*1 skipped*" + ] + ) def test_skipif_class(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest class TestClass(object): @@ -655,34 +740,40 @@ def test_skipif_class(testdir): assert 0 def test_though(self): assert 0 - """) + """ + ) result = testdir.runpytest(p) - result.stdout.fnmatch_lines([ - "*2 skipped*" - ]) + result.stdout.fnmatch_lines(["*2 skipped*"]) def test_skip_reasons_folding(): - path = 'xyz' + path = "xyz" lineno = 3 message = "justso" longrepr = (path, lineno, message) class X(object): pass + ev1 = X() ev1.when = "execute" ev1.skipped = True ev1.longrepr = longrepr ev2 = X() + ev2.when = "execute" ev2.longrepr = longrepr ev2.skipped = True - values = folded_skips([ev1, ev2]) + # ev3 might be a collection report + ev3 = X() + ev3.longrepr = longrepr + ev3.skipped = True + + values = folded_skips([ev1, ev2, ev3]) assert len(values) == 1 num, fspath, lineno, reason = values[0] - assert num == 2 + assert num == 3 assert fspath == path assert lineno == lineno assert reason == message @@ -704,17 +795,35 @@ def test_skipped_reasons_functional(testdir): import pytest def doskip(): pytest.skip('test') - """ + """, ) - result = testdir.runpytest('-rs') - result.stdout.fnmatch_lines([ - "*SKIP*2*conftest.py:4: test", - ]) + result = testdir.runpytest("-rs") + result.stdout.fnmatch_lines(["*SKIP*2*conftest.py:4: test"]) + assert result.ret == 0 + + +def test_skipped_folding(testdir): + testdir.makepyfile( + test_one=""" + import pytest + pytestmark = pytest.mark.skip("Folding") + def setup_function(func): + pass + def test_func(): + pass + class TestClass(object): + def test_method(self): + pass + """ + ) + result = testdir.runpytest("-rs") + result.stdout.fnmatch_lines(["*SKIP*2*test_one.py: Folding"]) assert result.ret == 0 def test_reportchars(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_1(): assert 0 @@ -726,14 +835,12 @@ def test_reportchars(testdir): pass def test_4(): pytest.skip("four") - """) + """ + ) result = testdir.runpytest("-rfxXs") - result.stdout.fnmatch_lines([ - "FAIL*test_1*", - "XFAIL*test_2*", - "XPASS*test_3*", - "SKIP*four*", - ]) + result.stdout.fnmatch_lines( + ["FAIL*test_1*", "XFAIL*test_2*", "XPASS*test_3*", "SKIP*four*"] + ) def test_reportchars_error(testdir): @@ -745,15 +852,15 @@ def test_reportchars_error(testdir): test_simple=""" def test_foo(): pass - """) - result = testdir.runpytest('-rE') - result.stdout.fnmatch_lines([ - 'ERROR*test_foo*', - ]) + """, + ) + result = testdir.runpytest("-rE") + result.stdout.fnmatch_lines(["ERROR*test_foo*"]) def test_reportchars_all(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_1(): assert 0 @@ -765,14 +872,12 @@ def test_reportchars_all(testdir): pass def test_4(): pytest.skip("four") - """) + """ + ) result = testdir.runpytest("-ra") - result.stdout.fnmatch_lines([ - "FAIL*test_1*", - "SKIP*four*", - "XFAIL*test_2*", - "XPASS*test_3*", - ]) + result.stdout.fnmatch_lines( + ["FAIL*test_1*", "SKIP*four*", "XFAIL*test_2*", "XPASS*test_3*"] + ) def test_reportchars_all_error(testdir): @@ -784,16 +889,16 @@ def test_reportchars_all_error(testdir): test_simple=""" def test_foo(): pass - """) - result = testdir.runpytest('-ra') - result.stdout.fnmatch_lines([ - 'ERROR*test_foo*', - ]) + """, + ) + result = testdir.runpytest("-ra") + result.stdout.fnmatch_lines(["ERROR*test_foo*"]) @pytest.mark.xfail("hasattr(sys, 'pypy_version_info')") def test_errors_in_xfail_skip_expressions(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skipif("asd") def test_nameerror(): @@ -804,27 +909,31 @@ def test_errors_in_xfail_skip_expressions(testdir): def test_func(): pass - """) + """ + ) result = testdir.runpytest() markline = " ^" if sys.platform.startswith("java"): # XXX report this to java markline = "*" + markline[8:] - result.stdout.fnmatch_lines([ - "*ERROR*test_nameerror*", - "*evaluating*skipif*expression*", - "*asd*", - "*ERROR*test_syntax*", - "*evaluating*xfail*expression*", - " syntax error", - markline, - "SyntaxError: invalid syntax", - "*1 pass*2 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERROR*test_nameerror*", + "*evaluating*skipif*expression*", + "*asd*", + "*ERROR*test_syntax*", + "*evaluating*xfail*expression*", + " syntax error", + markline, + "SyntaxError: invalid syntax", + "*1 pass*2 error*", + ] + ) def test_xfail_skipif_with_globals(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest x = 3 @pytest.mark.skipif("x == 3") @@ -833,55 +942,59 @@ def test_xfail_skipif_with_globals(testdir): @pytest.mark.xfail("x == 3") def test_boolean(): assert 0 - """) + """ + ) result = testdir.runpytest("-rsx") - result.stdout.fnmatch_lines([ - "*SKIP*x == 3*", - "*XFAIL*test_boolean*", - "*x == 3*", - ]) + result.stdout.fnmatch_lines(["*SKIP*x == 3*", "*XFAIL*test_boolean*", "*x == 3*"]) def test_direct_gives_error(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skipif(True) def test_skip1(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 error*", - ]) + result.stdout.fnmatch_lines(["*1 error*"]) def test_default_markers(testdir): result = testdir.runpytest("--markers") - result.stdout.fnmatch_lines([ - "*skipif(*condition)*skip*", - "*xfail(*condition, reason=None, run=True, raises=None, strict=False)*expected failure*", - ]) + result.stdout.fnmatch_lines( + [ + "*skipif(*condition)*skip*", + "*xfail(*condition, reason=None, run=True, raises=None, strict=False)*expected failure*", + ] + ) def test_xfail_test_setup_exception(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_runtest_setup(): 0 / 0 - """) - p = testdir.makepyfile(""" + """ + ) + p = testdir.makepyfile( + """ import pytest @pytest.mark.xfail def test_func(): assert 0 - """) + """ + ) result = testdir.runpytest(p) assert result.ret == 0 - assert 'xfailed' in result.stdout.str() - assert 'xpassed' not in result.stdout.str() + assert "xfailed" in result.stdout.str() + assert "xpassed" not in result.stdout.str() def test_imperativeskip_on_xfail_test(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.xfail def test_that_fails(): @@ -890,23 +1003,30 @@ def test_imperativeskip_on_xfail_test(testdir): @pytest.mark.skipif("True") def test_hello(): pass - """) - testdir.makeconftest(""" + """ + ) + testdir.makeconftest( + """ import pytest def pytest_runtest_setup(item): pytest.skip("abc") - """) + """ + ) result = testdir.runpytest("-rsxX") - result.stdout.fnmatch_lines_random(""" + result.stdout.fnmatch_lines_random( + """ *SKIP*abc* *SKIP*condition: True* *2 skipped* - """) + """ + ) class TestBooleanCondition(object): + def test_skipif(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skipif(True, reason="True123") def test_func1(): @@ -914,42 +1034,54 @@ class TestBooleanCondition(object): @pytest.mark.skipif(False, reason="True123") def test_func2(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *1 passed*1 skipped* - """) + """ + ) def test_skipif_noreason(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.skipif(True) def test_func(): pass - """) + """ + ) result = testdir.runpytest("-rs") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *1 error* - """) + """ + ) def test_xfail(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.xfail(True, reason="True123") def test_func(): assert 0 - """) + """ + ) result = testdir.runpytest("-rxs") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *XFAIL* *True123* *1 xfail* - """) + """ + ) def test_xfail_item(testdir): # Ensure pytest.xfail works with non-Python Item - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyItem(pytest.Item): @@ -959,11 +1091,12 @@ def test_xfail_item(testdir): def pytest_collect_file(path, parent): return MyItem("foo", parent) - """) + """ + ) result = testdir.inline_run() passed, skipped, failed = result.listoutcomes() assert not failed - xfailed = [r for r in skipped if hasattr(r, 'wasxfail')] + xfailed = [r for r in skipped if hasattr(r, "wasxfail")] assert xfailed @@ -971,21 +1104,56 @@ def test_module_level_skip_error(testdir): """ Verify that using pytest.skip at module level causes a collection error """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.skip def test_func(): assert True - """) - result = testdir.runpytest() - result.stdout.fnmatch_lines( - "*Using pytest.skip outside of a test is not allowed*" + """ ) + result = testdir.runpytest() + result.stdout.fnmatch_lines("*Using pytest.skip outside of a test is not allowed*") + + +def test_module_level_skip_with_allow_module_level(testdir): + """ + Verify that using pytest.skip(allow_module_level=True) is allowed + """ + testdir.makepyfile( + """ + import pytest + pytest.skip("skip_module_level", allow_module_level=True) + + def test_func(): + assert 0 + """ + ) + result = testdir.runpytest("-rxs") + result.stdout.fnmatch_lines("*SKIP*skip_module_level") + + +def test_invalid_skip_keyword_parameter(testdir): + """ + Verify that using pytest.skip() with unknown parameter raises an error + """ + testdir.makepyfile( + """ + import pytest + pytest.skip("skip_module_level", unknown=1) + + def test_func(): + assert 0 + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines("*TypeError:*['unknown']*") def test_mark_xfail_item(testdir): # Ensure pytest.mark.xfail works with non-Python Item - testdir.makeconftest(""" + testdir.makeconftest( + """ import pytest class MyItem(pytest.Item): @@ -998,9 +1166,29 @@ def test_mark_xfail_item(testdir): def pytest_collect_file(path, parent): return MyItem("foo", parent) - """) + """ + ) result = testdir.inline_run() passed, skipped, failed = result.listoutcomes() assert not failed - xfailed = [r for r in skipped if hasattr(r, 'wasxfail')] + xfailed = [r for r in skipped if hasattr(r, "wasxfail")] assert xfailed + + +def test_summary_list_after_errors(testdir): + """Ensure the list of errors/fails/xfails/skips appears after tracebacks in terminal reporting.""" + testdir.makepyfile( + """ + import pytest + def test_fail(): + assert 0 + """ + ) + result = testdir.runpytest("-ra") + result.stdout.fnmatch_lines( + [ + "=* FAILURES *=", + "*= short test summary info =*", + "FAIL test_summary_list_after_errors.py::test_fail", + ] + ) diff --git a/third_party/python/pytest/testing/test_terminal.py b/third_party/python/pytest/testing/test_terminal.py index 0fa98b26ed47..8f08ad34fa1f 100644 --- a/third_party/python/pytest/testing/test_terminal.py +++ b/third_party/python/pytest/testing/test_terminal.py @@ -5,7 +5,7 @@ from __future__ import absolute_import, division, print_function import collections import sys -import _pytest._pluggy as pluggy +import pluggy import _pytest._code import py import pytest @@ -14,10 +14,11 @@ from _pytest.terminal import TerminalReporter, repr_pythonversion, getreportopt from _pytest.terminal import build_summary_stats_line, _plugin_nameversions -DistInfo = collections.namedtuple('DistInfo', ['project_name', 'version']) +DistInfo = collections.namedtuple("DistInfo", ["project_name", "version"]) class Option(object): + def __init__(self, verbose=False, fulltrace=False): self.verbose = verbose self.fulltrace = fulltrace @@ -26,32 +27,40 @@ class Option(object): def args(self): values = [] if self.verbose: - values.append('-v') + values.append("-v") if self.fulltrace: - values.append('--fulltrace') + values.append("--fulltrace") return values -def pytest_generate_tests(metafunc): - if "option" in metafunc.fixturenames: - metafunc.addcall(id="default", - funcargs={'option': Option(verbose=False)}) - metafunc.addcall(id="verbose", - funcargs={'option': Option(verbose=True)}) - metafunc.addcall(id="quiet", - funcargs={'option': Option(verbose=-1)}) - metafunc.addcall(id="fulltrace", - funcargs={'option': Option(fulltrace=True)}) +@pytest.fixture( + params=[ + Option(verbose=False), + Option(verbose=True), + Option(verbose=-1), + Option(fulltrace=True), + ], + ids=["default", "verbose", "quiet", "fulltrace"], +) +def option(request): + return request.param -@pytest.mark.parametrize('input,expected', [ - ([DistInfo(project_name='test', version=1)], ['test-1']), - ([DistInfo(project_name='pytest-test', version=1)], ['test-1']), - ([ - DistInfo(project_name='test', version=1), - DistInfo(project_name='test', version=1) - ], ['test-1']), -], ids=['normal', 'prefix-strip', 'deduplicate']) +@pytest.mark.parametrize( + "input,expected", + [ + ([DistInfo(project_name="test", version=1)], ["test-1"]), + ([DistInfo(project_name="pytest-test", version=1)], ["test-1"]), + ( + [ + DistInfo(project_name="test", version=1), + DistInfo(project_name="test", version=1), + ], + ["test-1"], + ), + ], + ids=["normal", "prefix-strip", "deduplicate"], +) def test_plugin_nameversion(input, expected): pluginlist = [(None, x) for x in input] result = _plugin_nameversions(pluginlist) @@ -59,8 +68,10 @@ def test_plugin_nameversion(input, expected): class TestTerminal(object): + def test_pass_skip_fail(self, testdir, option): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_ok(): pass @@ -68,39 +79,36 @@ class TestTerminal(object): pytest.skip("xx") def test_func(): assert 0 - """) + """ + ) result = testdir.runpytest(*option.args) if option.verbose: - result.stdout.fnmatch_lines([ - "*test_pass_skip_fail.py::test_ok PASS*", - "*test_pass_skip_fail.py::test_skip SKIP*", - "*test_pass_skip_fail.py::test_func FAIL*", - ]) + result.stdout.fnmatch_lines( + [ + "*test_pass_skip_fail.py::test_ok PASS*", + "*test_pass_skip_fail.py::test_skip SKIP*", + "*test_pass_skip_fail.py::test_func FAIL*", + ] + ) else: - result.stdout.fnmatch_lines([ - "*test_pass_skip_fail.py .sF" - ]) - result.stdout.fnmatch_lines([ - " def test_func():", - "> assert 0", - "E assert 0", - ]) + result.stdout.fnmatch_lines(["*test_pass_skip_fail.py .sF*"]) + result.stdout.fnmatch_lines( + [" def test_func():", "> assert 0", "E assert 0"] + ) def test_internalerror(self, testdir, linecomp): modcol = testdir.getmodulecol("def test_one(): pass") rep = TerminalReporter(modcol.config, file=linecomp.stringio) excinfo = pytest.raises(ValueError, "raise ValueError('hello')") rep.pytest_internalerror(excinfo.getrepr()) - linecomp.assert_contains_lines([ - "INTERNALERROR> *ValueError*hello*" - ]) + linecomp.assert_contains_lines(["INTERNALERROR> *ValueError*hello*"]) def test_writeline(self, testdir, linecomp): modcol = testdir.getmodulecol("def test_one(): pass") rep = TerminalReporter(modcol.config, file=linecomp.stringio) rep.write_fspath_result(modcol.nodeid, ".") rep.write_line("hello world") - lines = linecomp.stringio.getvalue().split('\n') + lines = linecomp.stringio.getvalue().split("\n") assert not lines[0] assert lines[1].endswith(modcol.name + " .") assert lines[2] == "hello world" @@ -110,191 +118,212 @@ class TestTerminal(object): tr = TerminalReporter(item.config, file=linecomp.stringio) item.config.pluginmanager.register(tr) location = item.reportinfo() - tr.config.hook.pytest_runtest_logstart(nodeid=item.nodeid, - location=location, fspath=str(item.fspath)) - linecomp.assert_contains_lines([ - "*test_show_runtest_logstart.py*" - ]) + tr.config.hook.pytest_runtest_logstart( + nodeid=item.nodeid, location=location, fspath=str(item.fspath) + ) + linecomp.assert_contains_lines(["*test_show_runtest_logstart.py*"]) def test_runtest_location_shown_before_test_starts(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_1(): import time time.sleep(20) - """) + """ + ) child = testdir.spawn_pytest("") child.expect(".*test_runtest_location.*py") child.sendeof() child.kill(15) def test_itemreport_subclasses_show_subclassed_file(self, testdir): - testdir.makepyfile(test_p1=""" + testdir.makepyfile( + test_p1=""" class BaseTests(object): def test_p1(self): pass class TestClass(BaseTests): pass - """) - p2 = testdir.makepyfile(test_p2=""" + """ + ) + p2 = testdir.makepyfile( + test_p2=""" from test_p1 import BaseTests class TestMore(BaseTests): pass - """) + """ + ) result = testdir.runpytest(p2) - result.stdout.fnmatch_lines([ - "*test_p2.py .", - "*1 passed*", - ]) + result.stdout.fnmatch_lines(["*test_p2.py .*", "*1 passed*"]) result = testdir.runpytest("-v", p2) - result.stdout.fnmatch_lines([ - "*test_p2.py::TestMore::test_p1* <- *test_p1.py*PASSED", - ]) + result.stdout.fnmatch_lines( + ["*test_p2.py::TestMore::test_p1* <- *test_p1.py*PASSED*"] + ) def test_itemreport_directclasses_not_shown_as_subclasses(self, testdir): a = testdir.mkpydir("a123") - a.join("test_hello123.py").write(_pytest._code.Source(""" + a.join("test_hello123.py").write( + _pytest._code.Source( + """ class TestClass(object): def test_method(self): pass - """)) + """ + ) + ) result = testdir.runpytest("-v") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*a123/test_hello123.py*PASS*", - ]) + result.stdout.fnmatch_lines(["*a123/test_hello123.py*PASS*"]) assert " <- " not in result.stdout.str() def test_keyboard_interrupt(self, testdir, option): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foobar(): assert 0 def test_spamegg(): import py; pytest.skip('skip me please!') def test_interrupt_me(): raise KeyboardInterrupt # simulating the user - """) + """ + ) result = testdir.runpytest(*option.args, no_reraise_ctrlc=True) - result.stdout.fnmatch_lines([ - " def test_foobar():", - "> assert 0", - "E assert 0", - "*_keyboard_interrupt.py:6: KeyboardInterrupt*", - ]) + result.stdout.fnmatch_lines( + [ + " def test_foobar():", + "> assert 0", + "E assert 0", + "*_keyboard_interrupt.py:6: KeyboardInterrupt*", + ] + ) if option.fulltrace: - result.stdout.fnmatch_lines([ - "*raise KeyboardInterrupt # simulating the user*", - ]) + result.stdout.fnmatch_lines( + ["*raise KeyboardInterrupt # simulating the user*"] + ) else: - result.stdout.fnmatch_lines([ - "to show a full traceback on KeyboardInterrupt use --fulltrace" - ]) - result.stdout.fnmatch_lines(['*KeyboardInterrupt*']) + result.stdout.fnmatch_lines( + ["(to show a full traceback on KeyboardInterrupt use --fulltrace)"] + ) + result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) def test_keyboard_in_sessionstart(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_sessionstart(): raise KeyboardInterrupt - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ def test_foobar(): pass - """) + """ + ) result = testdir.runpytest(no_reraise_ctrlc=True) assert result.ret == 2 - result.stdout.fnmatch_lines(['*KeyboardInterrupt*']) + result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) def test_collect_single_item(self, testdir): """Use singular 'item' when reporting a single test item""" - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_foobar(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(['collected 1 item']) + result.stdout.fnmatch_lines(["collected 1 item"]) def test_rewrite(self, testdir, monkeypatch): config = testdir.parseconfig() f = py.io.TextIO() - monkeypatch.setattr(f, 'isatty', lambda *args: True) + monkeypatch.setattr(f, "isatty", lambda *args: True) tr = TerminalReporter(config, f) - tr.writer.fullwidth = 10 - tr.write('hello') - tr.rewrite('hey', erase=True) - assert f.getvalue() == 'hello' + '\r' + 'hey' + (7 * ' ') + tr._tw.fullwidth = 10 + tr.write("hello") + tr.rewrite("hey", erase=True) + assert f.getvalue() == "hello" + "\r" + "hey" + (6 * " ") class TestCollectonly(object): + def test_collectonly_basic(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_func(): pass - """) - result = testdir.runpytest("--collect-only",) - result.stdout.fnmatch_lines([ - "", - " ", - ]) + """ + ) + result = testdir.runpytest("--collect-only") + result.stdout.fnmatch_lines( + ["", " "] + ) def test_collectonly_skipped_module(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest pytest.skip("hello") - """) + """ + ) result = testdir.runpytest("--collect-only", "-rs") - result.stdout.fnmatch_lines([ - "*ERROR collecting*", - ]) + result.stdout.fnmatch_lines(["*ERROR collecting*"]) def test_collectonly_failed_module(self, testdir): testdir.makepyfile("""raise ValueError(0)""") result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*raise ValueError*", - "*1 error*", - ]) + result.stdout.fnmatch_lines(["*raise ValueError*", "*1 error*"]) def test_collectonly_fatal(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_collectstart(collector): assert 0, "urgs" - """) + """ + ) result = testdir.runpytest("--collect-only") - result.stdout.fnmatch_lines([ - "*INTERNAL*args*" - ]) + result.stdout.fnmatch_lines(["*INTERNAL*args*"]) assert result.ret == 3 def test_collectonly_simple(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_func1(): pass class TestClass(object): def test_method(self): pass - """) + """ + ) result = testdir.runpytest("--collect-only", p) # assert stderr.startswith("inserting into sys.path") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*", - "* ", - "* ", - # "* ", - "* ", - ]) + result.stdout.fnmatch_lines( + [ + "*", + "* ", + "* ", + # "* ", + "* ", + ] + ) def test_collectonly_error(self, testdir): p = testdir.makepyfile("import Errlkjqweqwe") result = testdir.runpytest("--collect-only", p) assert result.ret == 2 - result.stdout.fnmatch_lines(_pytest._code.Source(""" + result.stdout.fnmatch_lines( + _pytest._code.Source( + """ *ERROR* *ImportError* *No module named *Errlk* *1 error* - """).strip()) + """ + ).strip() + ) def test_collectonly_missing_path(self, testdir): """this checks issue 115, @@ -303,98 +332,105 @@ class TestCollectonly(object): """ result = testdir.runpytest("--collect-only", "uhm_missing_path") assert result.ret == 4 - result.stderr.fnmatch_lines([ - '*ERROR: file not found*', - ]) + result.stderr.fnmatch_lines(["*ERROR: file not found*"]) def test_collectonly_quiet(self, testdir): testdir.makepyfile("def test_foo(): pass") result = testdir.runpytest("--collect-only", "-q") - result.stdout.fnmatch_lines([ - '*test_foo*', - ]) + result.stdout.fnmatch_lines(["*test_foo*"]) def test_collectonly_more_quiet(self, testdir): testdir.makepyfile(test_fun="def test_foo(): pass") result = testdir.runpytest("--collect-only", "-qq") - result.stdout.fnmatch_lines([ - '*test_fun.py: 1*', - ]) + result.stdout.fnmatch_lines(["*test_fun.py: 1*"]) def test_repr_python_version(monkeypatch): try: - monkeypatch.setattr(sys, 'version_info', (2, 5, 1, 'final', 0)) + monkeypatch.setattr(sys, "version_info", (2, 5, 1, "final", 0)) assert repr_pythonversion() == "2.5.1-final-0" - py.std.sys.version_info = x = (2, 3) + sys.version_info = x = (2, 3) assert repr_pythonversion() == str(x) finally: monkeypatch.undo() # do this early as pytest can get confused class TestFixtureReporting(object): + def test_setup_fixture_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def setup_function(function): print ("setup func") assert 0 def test_nada(): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ERROR at setup of test_nada*", - "*setup_function(function):*", - "*setup func*", - "*assert 0*", - "*1 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERROR at setup of test_nada*", + "*setup_function(function):*", + "*setup func*", + "*assert 0*", + "*1 error*", + ] + ) assert result.ret != 0 def test_teardown_fixture_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_nada(): pass def teardown_function(function): print ("teardown func") assert 0 - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ERROR at teardown*", - "*teardown_function(function):*", - "*assert 0*", - "*Captured stdout*", - "*teardown func*", - "*1 passed*1 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERROR at teardown*", + "*teardown_function(function):*", + "*assert 0*", + "*Captured stdout*", + "*teardown func*", + "*1 passed*1 error*", + ] + ) def test_teardown_fixture_error_and_test_failure(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_fail(): assert 0, "failingfunc" def teardown_function(function): print ("teardown func") assert False - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ERROR at teardown of test_fail*", - "*teardown_function(function):*", - "*assert False*", - "*Captured stdout*", - "*teardown func*", - - "*test_fail*", - "*def test_fail():", - "*failingfunc*", - "*1 failed*1 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERROR at teardown of test_fail*", + "*teardown_function(function):*", + "*assert False*", + "*Captured stdout*", + "*teardown func*", + "*test_fail*", + "*def test_fail():", + "*failingfunc*", + "*1 failed*1 error*", + ] + ) def test_setup_teardown_output_and_test_failure(self, testdir): """ Test for issue #442 """ - testdir.makepyfile(""" + testdir.makepyfile( + """ def setup_function(function): print ("setup func") @@ -403,24 +439,28 @@ class TestFixtureReporting(object): def teardown_function(function): print ("teardown func") - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*test_fail*", - "*def test_fail():", - "*failingfunc*", - "*Captured stdout setup*", - "*setup func*", - "*Captured stdout teardown*", - "*teardown func*", - - "*1 failed*", - ]) + result.stdout.fnmatch_lines( + [ + "*test_fail*", + "*def test_fail():", + "*failingfunc*", + "*Captured stdout setup*", + "*setup func*", + "*Captured stdout teardown*", + "*teardown func*", + "*1 failed*", + ] + ) class TestTerminalFunctional(object): + def test_deselected(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ def test_one(): pass def test_two(): @@ -428,16 +468,44 @@ class TestTerminalFunctional(object): def test_three(): pass """ - ) + ) result = testdir.runpytest("-k", "test_two:", testpath) - result.stdout.fnmatch_lines([ - "*test_deselected.py ..", - "=* 1 test*deselected *=", - ]) + result.stdout.fnmatch_lines( + ["collected 3 items / 1 deselected", "*test_deselected.py ..*"] + ) + assert result.ret == 0 + + def test_show_deselected_items_using_markexpr_before_test_execution(self, testdir): + testdir.makepyfile( + """ + import pytest + + @pytest.mark.foo + def test_foobar(): + pass + + @pytest.mark.bar + def test_bar(): + pass + + def test_pass(): + pass + """ + ) + result = testdir.runpytest("-m", "not foo") + result.stdout.fnmatch_lines( + [ + "collected 3 items / 1 deselected", + "*test_show_des*.py ..*", + "*= 2 passed, 1 deselected in * =*", + ] + ) + assert "= 1 deselected =" not in result.stdout.str() assert result.ret == 0 def test_no_skip_summary_if_failure(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_ok(): pass @@ -445,66 +513,78 @@ class TestTerminalFunctional(object): assert 0 def test_skip(): pytest.skip("dontshow") - """) + """ + ) result = testdir.runpytest() assert result.stdout.str().find("skip test summary") == -1 assert result.ret == 1 def test_passes(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ def test_passes(): pass class TestClass(object): def test_method(self): pass - """) + """ + ) old = p1.dirpath().chdir() try: result = testdir.runpytest() finally: old.chdir() - result.stdout.fnmatch_lines([ - "test_passes.py ..", - "* 2 pass*", - ]) + result.stdout.fnmatch_lines(["test_passes.py ..*", "* 2 pass*"]) assert result.ret == 0 def test_header_trailer_info(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_passes(): pass - """) + """ + ) result = testdir.runpytest() - verinfo = ".".join(map(str, py.std.sys.version_info[:3])) - result.stdout.fnmatch_lines([ - "*===== test session starts ====*", - "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" % ( - py.std.sys.platform, verinfo, - pytest.__version__, py.__version__, pluggy.__version__), - "*test_header_trailer_info.py .", - "=* 1 passed*in *.[0-9][0-9] seconds *=", - ]) + verinfo = ".".join(map(str, sys.version_info[:3])) + result.stdout.fnmatch_lines( + [ + "*===== test session starts ====*", + "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" + % ( + sys.platform, + verinfo, + pytest.__version__, + py.__version__, + pluggy.__version__, + ), + "*test_header_trailer_info.py .*", + "=* 1 passed*in *.[0-9][0-9] seconds *=", + ] + ) if pytest.config.pluginmanager.list_plugin_distinfo(): - result.stdout.fnmatch_lines([ - "plugins: *", - ]) + result.stdout.fnmatch_lines(["plugins: *"]) def test_showlocals(self, testdir): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ def test_showlocals(): x = 3 y = "x" * 5000 assert 0 - """) - result = testdir.runpytest(p1, '-l') - result.stdout.fnmatch_lines([ - # "_ _ * Locals *", - "x* = 3", - "y* = 'xxxxxx*" - ]) + """ + ) + result = testdir.runpytest(p1, "-l") + result.stdout.fnmatch_lines( + [ + # "_ _ * Locals *", + "x* = 3", + "y* = 'xxxxxx*", + ] + ) def test_verbose_reporting(self, testdir, pytestconfig): - p1 = testdir.makepyfile(""" + p1 = testdir.makepyfile( + """ import pytest def test_fail(): raise ValueError() @@ -517,151 +597,155 @@ class TestTerminalFunctional(object): def check(x): assert x == 1 yield check, 0 - """) - result = testdir.runpytest(p1, '-v') - result.stdout.fnmatch_lines([ - "*test_verbose_reporting.py::test_fail *FAIL*", - "*test_verbose_reporting.py::test_pass *PASS*", - "*test_verbose_reporting.py::TestClass::test_skip *SKIP*", - "*test_verbose_reporting.py::test_gen*0* *FAIL*", - ]) + """ + ) + result = testdir.runpytest(p1, "-v") + result.stdout.fnmatch_lines( + [ + "*test_verbose_reporting.py::test_fail *FAIL*", + "*test_verbose_reporting.py::test_pass *PASS*", + "*test_verbose_reporting.py::TestClass::test_skip *SKIP*", + "*test_verbose_reporting.py::test_gen*0* *FAIL*", + ] + ) assert result.ret == 1 if not pytestconfig.pluginmanager.get_plugin("xdist"): pytest.skip("xdist plugin not installed") - result = testdir.runpytest(p1, '-v', '-n 1') - result.stdout.fnmatch_lines([ - "*FAIL*test_verbose_reporting.py::test_fail*", - ]) + result = testdir.runpytest(p1, "-v", "-n 1") + result.stdout.fnmatch_lines(["*FAIL*test_verbose_reporting.py::test_fail*"]) assert result.ret == 1 def test_quiet_reporting(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") - result = testdir.runpytest(p1, '-q') + result = testdir.runpytest(p1, "-q") s = result.stdout.str() - assert 'test session starts' not in s + assert "test session starts" not in s assert p1.basename not in s assert "===" not in s assert "passed" in s def test_more_quiet_reporting(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") - result = testdir.runpytest(p1, '-qq') + result = testdir.runpytest(p1, "-qq") s = result.stdout.str() - assert 'test session starts' not in s + assert "test session starts" not in s assert p1.basename not in s assert "===" not in s assert "passed" not in s def test_report_collectionfinish_hook(self, testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_report_collectionfinish(config, startdir, items): return ['hello from hook: {0} items'.format(len(items))] - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('i', range(3)) def test(i): pass - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "collected 3 items", - "hello from hook: 3 items", - ]) + result.stdout.fnmatch_lines(["collected 3 items", "hello from hook: 3 items"]) def test_fail_extra_reporting(testdir): testdir.makepyfile("def test_this(): assert 0") result = testdir.runpytest() - assert 'short test summary' not in result.stdout.str() - result = testdir.runpytest('-rf') - result.stdout.fnmatch_lines([ - "*test summary*", - "FAIL*test_fail_extra_reporting*", - ]) + assert "short test summary" not in result.stdout.str() + result = testdir.runpytest("-rf") + result.stdout.fnmatch_lines(["*test summary*", "FAIL*test_fail_extra_reporting*"]) def test_fail_reporting_on_pass(testdir): testdir.makepyfile("def test_this(): assert 1") - result = testdir.runpytest('-rf') - assert 'short test summary' not in result.stdout.str() + result = testdir.runpytest("-rf") + assert "short test summary" not in result.stdout.str() def test_pass_extra_reporting(testdir): testdir.makepyfile("def test_this(): assert 1") result = testdir.runpytest() - assert 'short test summary' not in result.stdout.str() - result = testdir.runpytest('-rp') - result.stdout.fnmatch_lines([ - "*test summary*", - "PASS*test_pass_extra_reporting*", - ]) + assert "short test summary" not in result.stdout.str() + result = testdir.runpytest("-rp") + result.stdout.fnmatch_lines(["*test summary*", "PASS*test_pass_extra_reporting*"]) def test_pass_reporting_on_fail(testdir): testdir.makepyfile("def test_this(): assert 0") - result = testdir.runpytest('-rp') - assert 'short test summary' not in result.stdout.str() + result = testdir.runpytest("-rp") + assert "short test summary" not in result.stdout.str() def test_pass_output_reporting(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_pass_output(): print("Four score and seven years ago...") - """) + """ + ) result = testdir.runpytest() - assert 'Four score and seven years ago...' not in result.stdout.str() - result = testdir.runpytest('-rP') - result.stdout.fnmatch_lines([ - "Four score and seven years ago...", - ]) + assert "Four score and seven years ago..." not in result.stdout.str() + result = testdir.runpytest("-rP") + result.stdout.fnmatch_lines(["Four score and seven years ago..."]) def test_color_yes(testdir): testdir.makepyfile("def test_this(): assert 1") - result = testdir.runpytest('--color=yes') - assert 'test session starts' in result.stdout.str() - assert '\x1b[1m' in result.stdout.str() + result = testdir.runpytest("--color=yes") + assert "test session starts" in result.stdout.str() + assert "\x1b[1m" in result.stdout.str() def test_color_no(testdir): testdir.makepyfile("def test_this(): assert 1") - result = testdir.runpytest('--color=no') - assert 'test session starts' in result.stdout.str() - assert '\x1b[1m' not in result.stdout.str() + result = testdir.runpytest("--color=no") + assert "test session starts" in result.stdout.str() + assert "\x1b[1m" not in result.stdout.str() -@pytest.mark.parametrize('verbose', [True, False]) +@pytest.mark.parametrize("verbose", [True, False]) def test_color_yes_collection_on_non_atty(testdir, verbose): """skip collect progress report when working on non-terminals. #1397 """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize('i', range(10)) def test_this(i): assert 1 - """) - args = ['--color=yes'] + """ + ) + args = ["--color=yes"] if verbose: - args.append('-vv') + args.append("-vv") result = testdir.runpytest(*args) - assert 'test session starts' in result.stdout.str() - assert '\x1b[1m' in result.stdout.str() - assert 'collecting 10 items' not in result.stdout.str() + assert "test session starts" in result.stdout.str() + assert "\x1b[1m" in result.stdout.str() + assert "collecting 10 items" not in result.stdout.str() if verbose: - assert 'collecting ...' in result.stdout.str() - assert 'collected 10 items' in result.stdout.str() + assert "collecting ..." in result.stdout.str() + assert "collected 10 items" in result.stdout.str() def test_getreportopt(): - class config(object): - class option(object): + + class Config(object): + + class Option(object): reportchars = "" disable_warnings = True + option = Option() + + config = Config() + config.option.reportchars = "sf" assert getreportopt(config) == "sf" @@ -679,7 +763,8 @@ def test_getreportopt(): def test_terminalreporter_reportopt_addopts(testdir): testdir.makeini("[pytest]\naddopts=-rs") - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture @@ -689,15 +774,15 @@ def test_terminalreporter_reportopt_addopts(testdir): def test_opt(tr): assert tr.hasopt('skipped') assert not tr.hasopt('qwe') - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*1 passed*" - ]) + result.stdout.fnmatch_lines(["*1 passed*"]) def test_tbstyle_short(testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest @pytest.fixture @@ -706,27 +791,22 @@ def test_tbstyle_short(testdir): def test_opt(arg): x = 0 assert x - """) + """ + ) result = testdir.runpytest("--tb=short") s = result.stdout.str() - assert 'arg = 42' not in s - assert 'x = 0' not in s - result.stdout.fnmatch_lines([ - "*%s:8*" % p.basename, - " assert x", - "E assert*", - ]) + assert "arg = 42" not in s + assert "x = 0" not in s + result.stdout.fnmatch_lines(["*%s:8*" % p.basename, " assert x", "E assert*"]) result = testdir.runpytest() s = result.stdout.str() - assert 'x = 0' in s - assert 'assert x' in s + assert "x = 0" in s + assert "assert x" in s def test_traceconfig(testdir, monkeypatch): result = testdir.runpytest("--traceconfig") - result.stdout.fnmatch_lines([ - "*active plugins*" - ]) + result.stdout.fnmatch_lines(["*active plugins*"]) assert result.ret == EXIT_NOTESTSCOLLECTED @@ -738,56 +818,56 @@ class TestGenericReporting(object): def test_collect_fail(self, testdir, option): testdir.makepyfile("import xyz\n") result = testdir.runpytest(*option.args) - result.stdout.fnmatch_lines([ - "ImportError while importing*", - "*No module named *xyz*", - "*1 error*", - ]) + result.stdout.fnmatch_lines( + ["ImportError while importing*", "*No module named *xyz*", "*1 error*"] + ) def test_maxfailures(self, testdir, option): - testdir.makepyfile(""" + testdir.makepyfile( + """ def test_1(): assert 0 def test_2(): assert 0 def test_3(): assert 0 - """) + """ + ) result = testdir.runpytest("--maxfail=2", *option.args) - result.stdout.fnmatch_lines([ - "*def test_1():*", - "*def test_2():*", - "*!! Interrupted: stopping after 2 failures*!!*", - "*2 failed*", - ]) + result.stdout.fnmatch_lines( + ["*def test_1():*", "*def test_2():*", "*2 failed*"] + ) def test_tb_option(self, testdir, option): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def g(): raise IndexError def test_func(): print (6*7) g() # --calling-- - """) + """ + ) for tbopt in ["long", "short", "no"]: - print('testing --tb=%s...' % tbopt) - result = testdir.runpytest('--tb=%s' % tbopt) + print("testing --tb=%s..." % tbopt) + result = testdir.runpytest("--tb=%s" % tbopt) s = result.stdout.str() if tbopt == "long": - assert 'print (6*7)' in s + assert "print (6*7)" in s else: - assert 'print (6*7)' not in s + assert "print (6*7)" not in s if tbopt != "no": - assert '--calling--' in s - assert 'IndexError' in s + assert "--calling--" in s + assert "IndexError" in s else: - assert 'FAILURES' not in s - assert '--calling--' not in s - assert 'IndexError' not in s + assert "FAILURES" not in s + assert "--calling--" not in s + assert "IndexError" not in s def test_tb_crashline(self, testdir, option): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def g(): raise IndexError @@ -796,38 +876,90 @@ class TestGenericReporting(object): g() # --calling-- def test_func2(): assert 0, "hello" - """) + """ + ) result = testdir.runpytest("--tb=line") bn = p.basename - result.stdout.fnmatch_lines([ - "*%s:3: IndexError*" % bn, - "*%s:8: AssertionError: hello*" % bn, - ]) + result.stdout.fnmatch_lines( + ["*%s:3: IndexError*" % bn, "*%s:8: AssertionError: hello*" % bn] + ) s = result.stdout.str() assert "def test_func2" not in s def test_pytest_report_header(self, testdir, option): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_sessionstart(session): session.config._somevalue = 42 def pytest_report_header(config): return "hello: %s" % config._somevalue - """) - testdir.mkdir("a").join("conftest.py").write(""" + """ + ) + testdir.mkdir("a").join("conftest.py").write( + """ def pytest_report_header(config, startdir): return ["line1", str(startdir)] -""") +""" + ) result = testdir.runpytest("a") - result.stdout.fnmatch_lines([ - "*hello: 42*", - "line1", - str(testdir.tmpdir), - ]) + result.stdout.fnmatch_lines(["*hello: 42*", "line1", str(testdir.tmpdir)]) + + def test_show_capture(self, testdir): + testdir.makepyfile( + """ + import sys + import logging + def test_one(): + sys.stdout.write('!This is stdout!') + sys.stderr.write('!This is stderr!') + logging.warning('!This is a warning log msg!') + assert False, 'Something failed' + """ + ) + + result = testdir.runpytest("--tb=short") + result.stdout.fnmatch_lines( + [ + "!This is stdout!", + "!This is stderr!", + "*WARNING*!This is a warning log msg!", + ] + ) + + result = testdir.runpytest("--show-capture=all", "--tb=short") + result.stdout.fnmatch_lines( + [ + "!This is stdout!", + "!This is stderr!", + "*WARNING*!This is a warning log msg!", + ] + ) + + stdout = testdir.runpytest("--show-capture=stdout", "--tb=short").stdout.str() + assert "!This is stderr!" not in stdout + assert "!This is stdout!" in stdout + assert "!This is a warning log msg!" not in stdout + + stdout = testdir.runpytest("--show-capture=stderr", "--tb=short").stdout.str() + assert "!This is stdout!" not in stdout + assert "!This is stderr!" in stdout + assert "!This is a warning log msg!" not in stdout + + stdout = testdir.runpytest("--show-capture=log", "--tb=short").stdout.str() + assert "!This is stdout!" not in stdout + assert "!This is stderr!" not in stdout + assert "!This is a warning log msg!" in stdout + + stdout = testdir.runpytest("--show-capture=no", "--tb=short").stdout.str() + assert "!This is stdout!" not in stdout + assert "!This is stderr!" not in stdout + assert "!This is a warning log msg!" not in stdout @pytest.mark.xfail("not hasattr(os, 'dup')") def test_fdopen_kept_alive_issue124(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import os, sys k = [] def test_open_file_and_keep_alive(capfd): @@ -837,15 +969,15 @@ def test_fdopen_kept_alive_issue124(testdir): def test_close_kept_alive_file(): stdout = k.pop() stdout.close() - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*2 passed*" - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) def test_tbstyle_native_setup_error(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture def setup_error_fixture(): @@ -853,115 +985,283 @@ def test_tbstyle_native_setup_error(testdir): def test_error_fixture(setup_error_fixture): pass - """) + """ + ) result = testdir.runpytest("--tb=native") - result.stdout.fnmatch_lines([ - '*File *test_tbstyle_native_setup_error.py", line *, in setup_error_fixture*' - ]) + result.stdout.fnmatch_lines( + ['*File *test_tbstyle_native_setup_error.py", line *, in setup_error_fixture*'] + ) def test_terminal_summary(testdir): - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_terminal_summary(terminalreporter, exitstatus): w = terminalreporter w.section("hello") w.line("world") w.line("exitstatus: {0}".format(exitstatus)) - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *==== hello ====* world exitstatus: 5 - """) + """ + ) def test_terminal_summary_warnings_are_displayed(testdir): """Test that warnings emitted during pytest_terminal_summary are displayed. (#1305). """ - testdir.makeconftest(""" + testdir.makeconftest( + """ def pytest_terminal_summary(terminalreporter): config = terminalreporter.config config.warn('C1', 'internal warning') - """) - result = testdir.runpytest('-rw') - result.stdout.fnmatch_lines([ - '*internal warning', - '*== 1 warnings in *', - ]) + """ + ) + result = testdir.runpytest("-rw") + result.stdout.fnmatch_lines( + ["", "*internal warning", "*== 1 warnings in *"] + ) + assert "None" not in result.stdout.str() -@pytest.mark.parametrize("exp_color, exp_line, stats_arg", [ - # The method under test only cares about the length of each - # dict value, not the actual contents, so tuples of anything - # suffice - - # Important statuses -- the highest priority of these always wins - ("red", "1 failed", {"failed": (1,)}), - ("red", "1 failed, 1 passed", {"failed": (1,), "passed": (1,)}), - - ("red", "1 error", {"error": (1,)}), - ("red", "1 passed, 1 error", {"error": (1,), "passed": (1,)}), - - # (a status that's not known to the code) - ("yellow", "1 weird", {"weird": (1,)}), - ("yellow", "1 passed, 1 weird", {"weird": (1,), "passed": (1,)}), - - ("yellow", "1 warnings", {"warnings": (1,)}), - ("yellow", "1 passed, 1 warnings", {"warnings": (1,), - "passed": (1,)}), - - ("green", "5 passed", {"passed": (1, 2, 3, 4, 5)}), - - - # "Boring" statuses. These have no effect on the color of the summary - # line. Thus, if *every* test has a boring status, the summary line stays - # at its default color, i.e. yellow, to warn the user that the test run - # produced no useful information - ("yellow", "1 skipped", {"skipped": (1,)}), - ("green", "1 passed, 1 skipped", {"skipped": (1,), "passed": (1,)}), - - ("yellow", "1 deselected", {"deselected": (1,)}), - ("green", "1 passed, 1 deselected", {"deselected": (1,), "passed": (1,)}), - - ("yellow", "1 xfailed", {"xfailed": (1,)}), - ("green", "1 passed, 1 xfailed", {"xfailed": (1,), "passed": (1,)}), - - ("yellow", "1 xpassed", {"xpassed": (1,)}), - ("green", "1 passed, 1 xpassed", {"xpassed": (1,), "passed": (1,)}), - - # Likewise if no tests were found at all - ("yellow", "no tests ran", {}), - - # Test the empty-key special case - ("yellow", "no tests ran", {"": (1,)}), - ("green", "1 passed", {"": (1,), "passed": (1,)}), - - - # A couple more complex combinations - ("red", "1 failed, 2 passed, 3 xfailed", - {"passed": (1, 2), "failed": (1,), "xfailed": (1, 2, 3)}), - - ("green", "1 passed, 2 skipped, 3 deselected, 2 xfailed", - {"passed": (1,), - "skipped": (1, 2), - "deselected": (1, 2, 3), - "xfailed": (1, 2)}), -]) +@pytest.mark.parametrize( + "exp_color, exp_line, stats_arg", + [ + # The method under test only cares about the length of each + # dict value, not the actual contents, so tuples of anything + # suffice + # Important statuses -- the highest priority of these always wins + ("red", "1 failed", {"failed": (1,)}), + ("red", "1 failed, 1 passed", {"failed": (1,), "passed": (1,)}), + ("red", "1 error", {"error": (1,)}), + ("red", "1 passed, 1 error", {"error": (1,), "passed": (1,)}), + # (a status that's not known to the code) + ("yellow", "1 weird", {"weird": (1,)}), + ("yellow", "1 passed, 1 weird", {"weird": (1,), "passed": (1,)}), + ("yellow", "1 warnings", {"warnings": (1,)}), + ("yellow", "1 passed, 1 warnings", {"warnings": (1,), "passed": (1,)}), + ("green", "5 passed", {"passed": (1, 2, 3, 4, 5)}), + # "Boring" statuses. These have no effect on the color of the summary + # line. Thus, if *every* test has a boring status, the summary line stays + # at its default color, i.e. yellow, to warn the user that the test run + # produced no useful information + ("yellow", "1 skipped", {"skipped": (1,)}), + ("green", "1 passed, 1 skipped", {"skipped": (1,), "passed": (1,)}), + ("yellow", "1 deselected", {"deselected": (1,)}), + ("green", "1 passed, 1 deselected", {"deselected": (1,), "passed": (1,)}), + ("yellow", "1 xfailed", {"xfailed": (1,)}), + ("green", "1 passed, 1 xfailed", {"xfailed": (1,), "passed": (1,)}), + ("yellow", "1 xpassed", {"xpassed": (1,)}), + ("green", "1 passed, 1 xpassed", {"xpassed": (1,), "passed": (1,)}), + # Likewise if no tests were found at all + ("yellow", "no tests ran", {}), + # Test the empty-key special case + ("yellow", "no tests ran", {"": (1,)}), + ("green", "1 passed", {"": (1,), "passed": (1,)}), + # A couple more complex combinations + ( + "red", + "1 failed, 2 passed, 3 xfailed", + {"passed": (1, 2), "failed": (1,), "xfailed": (1, 2, 3)}, + ), + ( + "green", + "1 passed, 2 skipped, 3 deselected, 2 xfailed", + { + "passed": (1,), + "skipped": (1, 2), + "deselected": (1, 2, 3), + "xfailed": (1, 2), + }, + ), + ], +) def test_summary_stats(exp_line, exp_color, stats_arg): print("Based on stats: %s" % stats_arg) - print("Expect summary: \"%s\"; with color \"%s\"" % (exp_line, exp_color)) + print('Expect summary: "%s"; with color "%s"' % (exp_line, exp_color)) (line, color) = build_summary_stats_line(stats_arg) - print("Actually got: \"%s\"; with color \"%s\"" % (line, color)) + print('Actually got: "%s"; with color "%s"' % (line, color)) assert line == exp_line assert color == exp_color def test_no_trailing_whitespace_after_inifile_word(testdir): - result = testdir.runpytest('') - assert 'inifile:\n' in result.stdout.str() + result = testdir.runpytest("") + assert "inifile:\n" in result.stdout.str() - testdir.makeini('[pytest]') - result = testdir.runpytest('') - assert 'inifile: tox.ini\n' in result.stdout.str() + testdir.makeini("[pytest]") + result = testdir.runpytest("") + assert "inifile: tox.ini\n" in result.stdout.str() + + +class TestProgress(object): + + @pytest.fixture + def many_tests_files(self, testdir): + testdir.makepyfile( + test_bar=""" + import pytest + @pytest.mark.parametrize('i', range(10)) + def test_bar(i): pass + """, + test_foo=""" + import pytest + @pytest.mark.parametrize('i', range(5)) + def test_foo(i): pass + """, + test_foobar=""" + import pytest + @pytest.mark.parametrize('i', range(5)) + def test_foobar(i): pass + """, + ) + + def test_zero_tests_collected(self, testdir): + """Some plugins (testmon for example) might issue pytest_runtest_logreport without any tests being + actually collected (#2971).""" + testdir.makeconftest( + """ + def pytest_collection_modifyitems(items, config): + from _pytest.runner import CollectReport + for node_id in ('nodeid1', 'nodeid2'): + rep = CollectReport(node_id, 'passed', None, None) + rep.when = 'passed' + rep.duration = 0.1 + config.hook.pytest_runtest_logreport(report=rep) + """ + ) + output = testdir.runpytest() + assert "ZeroDivisionError" not in output.stdout.str() + output.stdout.fnmatch_lines(["=* 2 passed in *="]) + + def test_normal(self, many_tests_files, testdir): + output = testdir.runpytest() + output.stdout.re_match_lines( + [ + r"test_bar.py \.{10} \s+ \[ 50%\]", + r"test_foo.py \.{5} \s+ \[ 75%\]", + r"test_foobar.py \.{5} \s+ \[100%\]", + ] + ) + + def test_verbose(self, many_tests_files, testdir): + output = testdir.runpytest("-v") + output.stdout.re_match_lines( + [ + r"test_bar.py::test_bar\[0\] PASSED \s+ \[ 5%\]", + r"test_foo.py::test_foo\[4\] PASSED \s+ \[ 75%\]", + r"test_foobar.py::test_foobar\[4\] PASSED \s+ \[100%\]", + ] + ) + + def test_xdist_normal(self, many_tests_files, testdir): + pytest.importorskip("xdist") + output = testdir.runpytest("-n2") + output.stdout.re_match_lines([r"\.{20} \s+ \[100%\]"]) + + def test_xdist_verbose(self, many_tests_files, testdir): + pytest.importorskip("xdist") + output = testdir.runpytest("-n2", "-v") + output.stdout.re_match_lines_random( + [ + r"\[gw\d\] \[\s*\d+%\] PASSED test_bar.py::test_bar\[1\]", + r"\[gw\d\] \[\s*\d+%\] PASSED test_foo.py::test_foo\[1\]", + r"\[gw\d\] \[\s*\d+%\] PASSED test_foobar.py::test_foobar\[1\]", + ] + ) + + def test_capture_no(self, many_tests_files, testdir): + output = testdir.runpytest("-s") + output.stdout.re_match_lines( + [r"test_bar.py \.{10}", r"test_foo.py \.{5}", r"test_foobar.py \.{5}"] + ) + + output = testdir.runpytest("--capture=no") + assert "%]" not in output.stdout.str() + + +class TestProgressWithTeardown(object): + """Ensure we show the correct percentages for tests that fail during teardown (#3088)""" + + @pytest.fixture + def contest_with_teardown_fixture(self, testdir): + testdir.makeconftest( + """ + import pytest + + @pytest.fixture + def fail_teardown(): + yield + assert False + """ + ) + + @pytest.fixture + def many_files(self, testdir, contest_with_teardown_fixture): + testdir.makepyfile( + test_bar=""" + import pytest + @pytest.mark.parametrize('i', range(5)) + def test_bar(fail_teardown, i): + pass + """, + test_foo=""" + import pytest + @pytest.mark.parametrize('i', range(15)) + def test_foo(fail_teardown, i): + pass + """, + ) + + def test_teardown_simple(self, testdir, contest_with_teardown_fixture): + testdir.makepyfile( + """ + def test_foo(fail_teardown): + pass + """ + ) + output = testdir.runpytest() + output.stdout.re_match_lines([r"test_teardown_simple.py \.E\s+\[100%\]"]) + + def test_teardown_with_test_also_failing( + self, testdir, contest_with_teardown_fixture + ): + testdir.makepyfile( + """ + def test_foo(fail_teardown): + assert False + """ + ) + output = testdir.runpytest() + output.stdout.re_match_lines( + [r"test_teardown_with_test_also_failing.py FE\s+\[100%\]"] + ) + + def test_teardown_many(self, testdir, many_files): + output = testdir.runpytest() + output.stdout.re_match_lines( + [r"test_bar.py (\.E){5}\s+\[ 25%\]", r"test_foo.py (\.E){15}\s+\[100%\]"] + ) + + def test_teardown_many_verbose(self, testdir, many_files): + output = testdir.runpytest("-v") + output.stdout.re_match_lines( + [ + r"test_bar.py::test_bar\[0\] PASSED\s+\[ 5%\]", + r"test_bar.py::test_bar\[0\] ERROR\s+\[ 5%\]", + r"test_bar.py::test_bar\[4\] PASSED\s+\[ 25%\]", + r"test_bar.py::test_bar\[4\] ERROR\s+\[ 25%\]", + ] + ) + + def test_xdist_normal(self, many_files, testdir): + pytest.importorskip("xdist") + output = testdir.runpytest("-n2") + output.stdout.re_match_lines([r"[\.E]{40} \s+ \[100%\]"]) diff --git a/third_party/python/pytest/testing/test_tmpdir.py b/third_party/python/pytest/testing/test_tmpdir.py index 467e77252e78..336249094957 100644 --- a/third_party/python/pytest/testing/test_tmpdir.py +++ b/third_party/python/pytest/testing/test_tmpdir.py @@ -7,13 +7,16 @@ from _pytest.tmpdir import tmpdir def test_funcarg(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ def pytest_generate_tests(metafunc): metafunc.addcall(id='a') metafunc.addcall(id='b') def test_func(tmpdir): pass - """) + """ + ) from _pytest.tmpdir import TempdirFactory + reprec = testdir.inline_run() calls = reprec.getcalls("pytest_runtest_setup") item = calls[0].item @@ -32,15 +35,17 @@ def test_funcarg(testdir): def test_ensuretemp(recwarn): - d1 = pytest.ensuretemp('hello') - d2 = pytest.ensuretemp('hello') + d1 = pytest.ensuretemp("hello") + d2 = pytest.ensuretemp("hello") assert d1 == d2 assert d1.check(dir=1) class TestTempdirHandler(object): + def test_mktemp(self, testdir): from _pytest.tmpdir import TempdirFactory + config = testdir.parseconfig() config.option.basetemp = testdir.mkdir("hello") t = TempdirFactory(config) @@ -54,35 +59,42 @@ class TestTempdirHandler(object): class TestConfigTmpdir(object): + def test_getbasetemp_custom_removes_old(self, testdir): mytemp = testdir.tmpdir.join("xyz") - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_1(tmpdir): pass - """) - testdir.runpytest(p, '--basetemp=%s' % mytemp) + """ + ) + testdir.runpytest(p, "--basetemp=%s" % mytemp) mytemp.check() mytemp.ensure("hello") - testdir.runpytest(p, '--basetemp=%s' % mytemp) + testdir.runpytest(p, "--basetemp=%s" % mytemp) mytemp.check() assert not mytemp.join("hello").check() def test_basetemp(testdir): mytemp = testdir.tmpdir.mkdir("mytemp") - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ import pytest def test_1(): pytest.ensuretemp("hello") - """) - result = testdir.runpytest(p, '--basetemp=%s' % mytemp) + """ + ) + result = testdir.runpytest(p, "--basetemp=%s" % mytemp) assert result.ret == 0 - assert mytemp.join('hello').check() + assert mytemp.join("hello").check() -@pytest.mark.skipif(not hasattr(py.path.local, 'mksymlinkto'), - reason="symlink not available on this platform") +@pytest.mark.skipif( + not hasattr(py.path.local, "mksymlinkto"), + reason="symlink not available on this platform", +) def test_tmpdir_always_is_realpath(testdir): # the reason why tmpdir should be a realpath is that # when you cd to it and do "os.getcwd()" you will anyway @@ -93,35 +105,41 @@ def test_tmpdir_always_is_realpath(testdir): realtemp = testdir.tmpdir.mkdir("myrealtemp") linktemp = testdir.tmpdir.join("symlinktemp") linktemp.mksymlinkto(realtemp) - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ def test_1(tmpdir): import os assert os.path.realpath(str(tmpdir)) == str(tmpdir) - """) - result = testdir.runpytest("-s", p, '--basetemp=%s/bt' % linktemp) + """ + ) + result = testdir.runpytest("-s", p, "--basetemp=%s/bt" % linktemp) assert not result.ret def test_tmpdir_too_long_on_parametrization(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.mark.parametrize("arg", ["1"*1000]) def test_some(arg, tmpdir): tmpdir.ensure("hello") - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_tmpdir_factory(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest @pytest.fixture(scope='session') def session_dir(tmpdir_factory): return tmpdir_factory.mktemp('data', numbered=False) def test_some(session_dir): session_dir.isdir() - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @@ -130,59 +148,65 @@ def test_tmpdir_fallback_tox_env(testdir, monkeypatch): """Test that tmpdir works even if environment variables required by getpass module are missing (#1010). """ - monkeypatch.delenv('USER', raising=False) - monkeypatch.delenv('USERNAME', raising=False) - testdir.makepyfile(""" + monkeypatch.delenv("USER", raising=False) + monkeypatch.delenv("USERNAME", raising=False) + testdir.makepyfile( + """ import pytest def test_some(tmpdir): assert tmpdir.isdir() - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.fixture def break_getuser(monkeypatch): - monkeypatch.setattr('os.getuid', lambda: -1) + monkeypatch.setattr("os.getuid", lambda: -1) # taken from python 2.7/3.4 - for envvar in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): + for envvar in ("LOGNAME", "USER", "LNAME", "USERNAME"): monkeypatch.delenv(envvar, raising=False) @pytest.mark.usefixtures("break_getuser") -@pytest.mark.skipif(sys.platform.startswith('win'), reason='no os.getuid on windows') +@pytest.mark.skipif(sys.platform.startswith("win"), reason="no os.getuid on windows") def test_tmpdir_fallback_uid_not_found(testdir): """Test that tmpdir works even if the current process's user id does not correspond to a valid user. """ - testdir.makepyfile(""" + testdir.makepyfile( + """ import pytest def test_some(tmpdir): assert tmpdir.isdir() - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.usefixtures("break_getuser") -@pytest.mark.skipif(sys.platform.startswith('win'), reason='no os.getuid on windows') +@pytest.mark.skipif(sys.platform.startswith("win"), reason="no os.getuid on windows") def test_get_user_uid_not_found(): """Test that get_user() function works even if the current process's user id does not correspond to a valid user (e.g. running pytest in a Docker container with 'docker run -u'. """ from _pytest.tmpdir import get_user + assert get_user() is None -@pytest.mark.skipif(not sys.platform.startswith('win'), reason='win only') +@pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only") def test_get_user(monkeypatch): """Test that get_user() function works even if environment variables required by getpass module are missing from the environment on Windows (#1010). """ from _pytest.tmpdir import get_user - monkeypatch.delenv('USER', raising=False) - monkeypatch.delenv('USERNAME', raising=False) + + monkeypatch.delenv("USER", raising=False) + monkeypatch.delenv("USERNAME", raising=False) assert get_user() is None diff --git a/third_party/python/pytest/testing/test_unittest.py b/third_party/python/pytest/testing/test_unittest.py index 3273e81aa981..65ffdb97538b 100644 --- a/third_party/python/pytest/testing/test_unittest.py +++ b/third_party/python/pytest/testing/test_unittest.py @@ -5,21 +5,24 @@ import gc def test_simple_unittest(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): def testpassing(self): self.assertEqual('foo', 'foo') def test_failing(self): self.assertEqual('foo', 'bar') - """) + """ + ) reprec = testdir.inline_run(testpath) assert reprec.matchreport("testpassing").passed assert reprec.matchreport("test_failing").failed def test_runTest_method(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest class MyTestCaseWithRunTest(unittest.TestCase): def runTest(self): @@ -29,29 +32,35 @@ def test_runTest_method(testdir): self.assertEqual('foo', 'foo') def test_something(self): pass - """) + """ + ) result = testdir.runpytest("-v") - result.stdout.fnmatch_lines(""" + result.stdout.fnmatch_lines( + """ *MyTestCaseWithRunTest::runTest* *MyTestCaseWithoutRunTest::test_something* *2 passed* - """) + """ + ) def test_isclasscheck_issue53(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class _E(object): def __getattr__(self, tag): pass E = _E() - """) + """ + ) result = testdir.runpytest(testpath) assert result.ret == EXIT_NOTESTSCOLLECTED def test_setup(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): def setUp(self): @@ -64,15 +73,17 @@ def test_setup(testdir): def teardown_method(self, method): assert 0, "42" - """) + """ + ) reprec = testdir.inline_run("-s", testpath) assert reprec.matchreport("test_both", when="call").passed rep = reprec.matchreport("test_both", when="teardown") - assert rep.failed and '42' in str(rep.longrepr) + assert rep.failed and "42" in str(rep.longrepr) def test_setUpModule(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ values = [] def setUpModule(): @@ -86,15 +97,15 @@ def test_setUpModule(testdir): def test_world(): assert values == [1] - """) + """ + ) result = testdir.runpytest(testpath) - result.stdout.fnmatch_lines([ - "*2 passed*", - ]) + result.stdout.fnmatch_lines(["*2 passed*"]) def test_setUpModule_failing_no_teardown(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ values = [] def setUpModule(): @@ -105,7 +116,8 @@ def test_setUpModule_failing_no_teardown(testdir): def test_hello(): pass - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=0, failed=1) call = reprec.getcalls("pytest_runtest_setup")[0] @@ -113,20 +125,23 @@ def test_setUpModule_failing_no_teardown(testdir): def test_new_instances(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): def test_func1(self): self.x = 2 def test_func2(self): assert not hasattr(self, 'x') - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=2) def test_teardown(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): values = [] @@ -137,7 +152,8 @@ def test_teardown(testdir): class Second(unittest.TestCase): def test_check(self): self.assertEqual(MyTestCase.values, [None]) - """) + """ + ) reprec = testdir.inline_run(testpath) passed, skipped, failed = reprec.countoutcomes() assert failed == 0, failed @@ -153,7 +169,8 @@ def test_teardown_issue1649(testdir): The TestCase will not be cleaned up if the test fails, because it would then exist in the stackframe. """ - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): def setUp(self): @@ -161,16 +178,17 @@ def test_teardown_issue1649(testdir): def test_demo(self): pass - """) + """ + ) testdir.inline_run("-s", testpath) gc.collect() for obj in gc.get_objects(): - assert type(obj).__name__ != 'TestCaseObjectsShouldBeCleanedUp' + assert type(obj).__name__ != "TestCaseObjectsShouldBeCleanedUp" -@pytest.mark.skipif("sys.version_info < (2,7)") def test_unittest_skip_issue148(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest @unittest.skip("hello") @@ -183,33 +201,39 @@ def test_unittest_skip_issue148(testdir): @classmethod def tearDownClass(self): xxx - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(skipped=1) def test_method_and_teardown_failing_reporting(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest, pytest class TC(unittest.TestCase): def tearDown(self): assert 0, "down1" def test_method(self): assert False, "down2" - """) + """ + ) result = testdir.runpytest("-s") assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*tearDown*", - "*assert 0*", - "*test_method*", - "*assert False*", - "*1 failed*1 error*", - ]) + result.stdout.fnmatch_lines( + [ + "*tearDown*", + "*assert 0*", + "*test_method*", + "*assert False*", + "*1 failed*1 error*", + ] + ) def test_setup_failure_is_shown(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest import pytest class TC(unittest.TestCase): @@ -218,19 +242,17 @@ def test_setup_failure_is_shown(testdir): def test_method(self): print ("never42") xyz - """) + """ + ) result = testdir.runpytest("-s") assert result.ret == 1 - result.stdout.fnmatch_lines([ - "*setUp*", - "*assert 0*down1*", - "*1 failed*", - ]) - assert 'never42' not in result.stdout.str() + result.stdout.fnmatch_lines(["*setUp*", "*assert 0*down1*", "*1 failed*"]) + assert "never42" not in result.stdout.str() def test_setup_setUpClass(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest import pytest class MyTestCase(unittest.TestCase): @@ -247,13 +269,15 @@ def test_setup_setUpClass(testdir): cls.x -= 1 def test_teareddown(): assert MyTestCase.x == 0 - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=3) def test_setup_class(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest import pytest class MyTestCase(unittest.TestCase): @@ -268,14 +292,16 @@ def test_setup_class(testdir): cls.x -= 1 def test_teareddown(): assert MyTestCase.x == 0 - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=3) -@pytest.mark.parametrize("type", ['Error', 'Failure']) +@pytest.mark.parametrize("type", ["Error", "Failure"]) def test_testcase_adderrorandfailure_defers(testdir, type): - testdir.makepyfile(""" + testdir.makepyfile( + """ from unittest import TestCase import pytest class MyTestCase(TestCase): @@ -289,14 +315,17 @@ def test_testcase_adderrorandfailure_defers(testdir, type): pytest.fail("add%s should not raise") def test_hello(self): pass - """ % (type, type)) + """ + % (type, type) + ) result = testdir.runpytest() - assert 'should not raise' not in result.stdout.str() + assert "should not raise" not in result.stdout.str() -@pytest.mark.parametrize("type", ['Error', 'Failure']) +@pytest.mark.parametrize("type", ["Error", "Failure"]) def test_testcase_custom_exception_info(testdir, type): - testdir.makepyfile(""" + testdir.makepyfile( + """ from unittest import TestCase import py, pytest import _pytest._code @@ -317,69 +346,83 @@ def test_testcase_custom_exception_info(testdir, type): mp.undo() def test_hello(self): pass - """ % locals()) + """ + % locals() + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "NOTE: Incompatible Exception Representation*", - "*ZeroDivisionError*", - "*1 failed*", - ]) + result.stdout.fnmatch_lines( + [ + "NOTE: Incompatible Exception Representation*", + "*ZeroDivisionError*", + "*1 failed*", + ] + ) def test_testcase_totally_incompatible_exception_info(testdir): - item, = testdir.getitems(""" + item, = testdir.getitems( + """ from unittest import TestCase class MyTestCase(TestCase): def test_hello(self): pass - """) + """ + ) item.addError(None, 42) excinfo = item._excinfo.pop(0) - assert 'ERROR: Unknown Incompatible' in str(excinfo.getrepr()) + assert "ERROR: Unknown Incompatible" in str(excinfo.getrepr()) def test_module_level_pytestmark(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest import pytest pytestmark = pytest.mark.xfail class MyTestCase(unittest.TestCase): def test_func1(self): assert 0 - """) + """ + ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) class TestTrialUnittest(object): + def setup_class(cls): cls.ut = pytest.importorskip("twisted.trial.unittest") # on windows trial uses a socket for a reactor and apparently doesn't close it properly # https://twistedmatrix.com/trac/ticket/9227 - cls.ignore_unclosed_socket_warning = ('-W', 'always') + cls.ignore_unclosed_socket_warning = ("-W", "always") def test_trial_testcase_runtest_not_collected(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from twisted.trial.unittest import TestCase class TC(TestCase): def test_hello(self): pass - """) + """ + ) reprec = testdir.inline_run(*self.ignore_unclosed_socket_warning) reprec.assertoutcome(passed=1) - testdir.makepyfile(""" + testdir.makepyfile( + """ from twisted.trial.unittest import TestCase class TC(TestCase): def runTest(self): pass - """) + """ + ) reprec = testdir.inline_run(*self.ignore_unclosed_socket_warning) reprec.assertoutcome(passed=1) def test_trial_exceptions_with_skips(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from twisted.trial import unittest import pytest class TC(unittest.TestCase): @@ -410,24 +453,31 @@ class TestTrialUnittest(object): pytest.skip("skip_in_setup_class") def test_method(self): pass - """) + """ + ) from _pytest.compat import _is_unittest_unexpected_success_a_failure + should_fail = _is_unittest_unexpected_success_a_failure() result = testdir.runpytest("-rxs", *self.ignore_unclosed_socket_warning) - result.stdout.fnmatch_lines_random([ - "*XFAIL*test_trial_todo*", - "*trialselfskip*", - "*skip_in_setup_class*", - "*iwanto*", - "*i2wanto*", - "*sys.version_info*", - "*skip_in_method*", - "*1 failed*4 skipped*3 xfailed*" if should_fail else "*4 skipped*3 xfail*1 xpass*", - ]) + result.stdout.fnmatch_lines_random( + [ + "*XFAIL*test_trial_todo*", + "*trialselfskip*", + "*skip_in_setup_class*", + "*iwanto*", + "*i2wanto*", + "*sys.version_info*", + "*skip_in_method*", + "*1 failed*4 skipped*3 xfailed*" + if should_fail + else "*4 skipped*3 xfail*1 xpass*", + ] + ) assert result.ret == (1 if should_fail else 0) def test_trial_error(self, testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ from twisted.trial.unittest import TestCase from twisted.internet.defer import Deferred from twisted.internet import reactor @@ -461,81 +511,97 @@ class TestTrialUnittest(object): reactor.callLater(0.3, d.callback, None) return d # will crash both at test time and at teardown - """) + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - "*ERRORS*", - "*DelayedCalls*", - "*test_four*", - "*NameError*crash*", - "*test_one*", - "*NameError*crash*", - "*test_three*", - "*DelayedCalls*", - "*test_two*", - "*crash*", - ]) + result.stdout.fnmatch_lines( + [ + "*ERRORS*", + "*DelayedCalls*", + "*test_four*", + "*NameError*crash*", + "*test_one*", + "*NameError*crash*", + "*test_three*", + "*DelayedCalls*", + "*test_two*", + "*crash*", + ] + ) def test_trial_pdb(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile( + """ from twisted.trial import unittest import pytest class TC(unittest.TestCase): def test_hello(self): assert 0, "hellopdb" - """) + """ + ) child = testdir.spawn_pytest(p) child.expect("hellopdb") child.sendeof() def test_trial_testcase_skip_property(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): skip = 'dont run' def test_func(self): pass - """) + """ + ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) def test_trial_testfunction_skip_property(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): def test_func(self): pass test_func.skip = 'dont run' - """) + """ + ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) def test_trial_testcase_todo_property(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): todo = 'dont run' def test_func(self): assert 0 - """) + """ + ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) def test_trial_testfunction_todo_property(self, testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): def test_func(self): assert 0 test_func.todo = 'dont run' - """) - reprec = testdir.inline_run(testpath, "-s", *self.ignore_unclosed_socket_warning) + """ + ) + reprec = testdir.inline_run( + testpath, "-s", *self.ignore_unclosed_socket_warning + ) reprec.assertoutcome(skipped=1) def test_djangolike_testcase(testdir): # contributed from Morten Breekevold - testdir.makepyfile(""" + testdir.makepyfile( + """ from unittest import TestCase, main class DjangoLikeTestCase(TestCase): @@ -575,32 +641,38 @@ def test_djangolike_testcase(testdir): def _post_teardown(self): print ("_post_teardown()") - """) + """ + ) result = testdir.runpytest("-s") assert result.ret == 0 - result.stdout.fnmatch_lines([ - "*_pre_setup()*", - "*setUp()*", - "*test_thing()*", - "*tearDown()*", - "*_post_teardown()*", - ]) + result.stdout.fnmatch_lines( + [ + "*_pre_setup()*", + "*setUp()*", + "*test_thing()*", + "*tearDown()*", + "*_post_teardown()*", + ] + ) def test_unittest_not_shown_in_traceback(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest class t(unittest.TestCase): def test_hello(self): x = 3 self.assertEqual(x, 4) - """) + """ + ) res = testdir.runpytest() assert "failUnlessEqual" not in res.stdout.str() def test_unorderable_types(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest class TestJoinEmpty(unittest.TestCase): pass @@ -611,28 +683,31 @@ def test_unorderable_types(testdir): Test.__name__ = "TestFoo" return Test TestFoo = make_test() - """) + """ + ) result = testdir.runpytest() assert "TypeError" not in result.stdout.str() assert result.ret == EXIT_NOTESTSCOLLECTED def test_unittest_typerror_traceback(testdir): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest class TestJoinEmpty(unittest.TestCase): def test_hello(self, arg1): pass - """) + """ + ) result = testdir.runpytest() assert "TypeError" in result.stdout.str() assert result.ret == 1 -@pytest.mark.skipif("sys.version_info < (2,7)") -@pytest.mark.parametrize('runner', ['pytest', 'unittest']) +@pytest.mark.parametrize("runner", ["pytest", "unittest"]) def test_unittest_expected_failure_for_failing_test_is_xfail(testdir, runner): - script = testdir.makepyfile(""" + script = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): @unittest.expectedFailure @@ -640,26 +715,23 @@ def test_unittest_expected_failure_for_failing_test_is_xfail(testdir, runner): assert False if __name__ == '__main__': unittest.main() - """) - if runner == 'pytest': + """ + ) + if runner == "pytest": result = testdir.runpytest("-rxX") - result.stdout.fnmatch_lines([ - "*XFAIL*MyTestCase*test_failing_test_is_xfail*", - "*1 xfailed*", - ]) + result.stdout.fnmatch_lines( + ["*XFAIL*MyTestCase*test_failing_test_is_xfail*", "*1 xfailed*"] + ) else: result = testdir.runpython(script) - result.stderr.fnmatch_lines([ - "*1 test in*", - "*OK*(expected failures=1)*", - ]) + result.stderr.fnmatch_lines(["*1 test in*", "*OK*(expected failures=1)*"]) assert result.ret == 0 -@pytest.mark.skipif("sys.version_info < (2,7)") -@pytest.mark.parametrize('runner', ['pytest', 'unittest']) +@pytest.mark.parametrize("runner", ["pytest", "unittest"]) def test_unittest_expected_failure_for_passing_test_is_fail(testdir, runner): - script = testdir.makepyfile(""" + script = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): @unittest.expectedFailure @@ -667,31 +739,32 @@ def test_unittest_expected_failure_for_passing_test_is_fail(testdir, runner): assert True if __name__ == '__main__': unittest.main() - """) + """ + ) from _pytest.compat import _is_unittest_unexpected_success_a_failure + should_fail = _is_unittest_unexpected_success_a_failure() - if runner == 'pytest': + if runner == "pytest": result = testdir.runpytest("-rxX") - result.stdout.fnmatch_lines([ - "*MyTestCase*test_passing_test_is_fail*", - "*1 failed*" if should_fail else "*1 xpassed*", - ]) + result.stdout.fnmatch_lines( + [ + "*MyTestCase*test_passing_test_is_fail*", + "*1 failed*" if should_fail else "*1 xpassed*", + ] + ) else: result = testdir.runpython(script) - result.stderr.fnmatch_lines([ - "*1 test in*", - "*(unexpected successes=1)*", - ]) + result.stderr.fnmatch_lines(["*1 test in*", "*(unexpected successes=1)*"]) assert result.ret == (1 if should_fail else 0) -@pytest.mark.parametrize('fix_type, stmt', [ - ('fixture', 'return'), - ('yield_fixture', 'yield'), -]) +@pytest.mark.parametrize( + "fix_type, stmt", [("fixture", "return"), ("yield_fixture", "yield")] +) def test_unittest_setup_interaction(testdir, fix_type, stmt): - testdir.makepyfile(""" + testdir.makepyfile( + """ import unittest import pytest class MyTestCase(unittest.TestCase): @@ -713,13 +786,17 @@ def test_unittest_setup_interaction(testdir, fix_type, stmt): def test_classattr(self): assert self.__class__.hello == "world" - """.format(fix_type=fix_type, stmt=stmt)) + """.format( + fix_type=fix_type, stmt=stmt + ) + ) result = testdir.runpytest() result.stdout.fnmatch_lines("*3 passed*") def test_non_unittest_no_setupclass_support(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ class TestFoo(object): x = 0 @@ -737,13 +814,15 @@ def test_non_unittest_no_setupclass_support(testdir): def test_not_teareddown(): assert TestFoo.x == 0 - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=2) def test_no_teardown_if_setupclass_failed(testdir): - testpath = testdir.makepyfile(""" + testpath = testdir.makepyfile( + """ import unittest class MyTestCase(unittest.TestCase): @@ -763,63 +842,77 @@ def test_no_teardown_if_setupclass_failed(testdir): def test_notTornDown(): assert MyTestCase.x == 1 - """) + """ + ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=1, failed=1) def test_issue333_result_clearing(testdir): - testdir.makeconftest(""" - def pytest_runtest_call(__multicall__, item): - __multicall__.execute() + testdir.makeconftest( + """ + import pytest + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_call(item): + yield assert 0 - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import unittest class TestIt(unittest.TestCase): def test_func(self): 0/0 - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(failed=1) -@pytest.mark.skipif("sys.version_info < (2,7)") def test_unittest_raise_skip_issue748(testdir): - testdir.makepyfile(test_foo=""" + testdir.makepyfile( + test_foo=""" import unittest class MyTestCase(unittest.TestCase): def test_one(self): raise unittest.SkipTest('skipping due to reasons') - """) - result = testdir.runpytest("-v", '-rs') - result.stdout.fnmatch_lines(""" + """ + ) + result = testdir.runpytest("-v", "-rs") + result.stdout.fnmatch_lines( + """ *SKIP*[1]*test_foo.py*skipping due to reasons* *1 skipped* - """) + """ + ) -@pytest.mark.skipif("sys.version_info < (2,7)") def test_unittest_skip_issue1169(testdir): - testdir.makepyfile(test_foo=""" + testdir.makepyfile( + test_foo=""" import unittest class MyTestCase(unittest.TestCase): @unittest.skip("skipping due to reasons") def test_skip(self): self.fail() - """) - result = testdir.runpytest("-v", '-rs') - result.stdout.fnmatch_lines(""" + """ + ) + result = testdir.runpytest("-v", "-rs") + result.stdout.fnmatch_lines( + """ *SKIP*[1]*skipping due to reasons* *1 skipped* - """) + """ + ) def test_class_method_containing_test_issue1558(testdir): - testdir.makepyfile(test_foo=""" + testdir.makepyfile( + test_foo=""" import unittest class MyTestCase(unittest.TestCase): @@ -828,6 +921,72 @@ def test_class_method_containing_test_issue1558(testdir): def test_should_not_run(self): pass test_should_not_run.__test__ = False - """) + """ + ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) + + +@pytest.mark.issue(3498) +@pytest.mark.parametrize( + "base", ["six.moves.builtins.object", "unittest.TestCase", "unittest2.TestCase"] +) +def test_usefixtures_marker_on_unittest(base, testdir): + module = base.rsplit(".", 1)[0] + pytest.importorskip(module) + testdir.makepyfile( + conftest=""" + import pytest + + @pytest.fixture(scope='function') + def fixture1(request, monkeypatch): + monkeypatch.setattr(request.instance, 'fixture1', True ) + + + @pytest.fixture(scope='function') + def fixture2(request, monkeypatch): + monkeypatch.setattr(request.instance, 'fixture2', True ) + + def node_and_marks(item): + print(item.nodeid) + for mark in item.iter_markers(): + print(" ", mark) + + @pytest.fixture(autouse=True) + def my_marks(request): + node_and_marks(request.node) + + def pytest_collection_modifyitems(items): + for item in items: + node_and_marks(item) + + """ + ) + + testdir.makepyfile( + """ + import pytest + import {module} + + class Tests({base}): + fixture1 = False + fixture2 = False + + @pytest.mark.usefixtures("fixture1") + def test_one(self): + assert self.fixture1 + assert not self.fixture2 + + @pytest.mark.usefixtures("fixture1", "fixture2") + def test_two(self): + assert self.fixture1 + assert self.fixture2 + + + """.format( + module=module, base=base + ) + ) + + result = testdir.runpytest("-s") + result.assert_outcomes(passed=2) diff --git a/third_party/python/pytest/testing/test_warnings.py b/third_party/python/pytest/testing/test_warnings.py index 12539f8ee052..c5bea052a47e 100644 --- a/third_party/python/pytest/testing/test_warnings.py +++ b/third_party/python/pytest/testing/test_warnings.py @@ -6,7 +6,7 @@ import sys import pytest -WARNINGS_SUMMARY_HEADER = 'warnings summary' +WARNINGS_SUMMARY_HEADER = "warnings summary" @pytest.fixture @@ -16,48 +16,52 @@ def pyfile_with_warnings(testdir, request): """ testdir.syspathinsert() test_name = request.function.__name__ - module_name = test_name.lstrip('test_') + '_module' - testdir.makepyfile(**{ - module_name: ''' + module_name = test_name.lstrip("test_") + "_module" + testdir.makepyfile( + **{ + module_name: """ import warnings def foo(): warnings.warn(UserWarning("user warning")) warnings.warn(RuntimeWarning("runtime warning")) return 1 - ''', - test_name: ''' + """, + test_name: """ import {module_name} def test_func(): assert {module_name}.foo() == 1 - '''.format(module_name=module_name) - }) + """.format( + module_name=module_name + ), + } + ) -@pytest.mark.filterwarnings('always') +@pytest.mark.filterwarnings("always") def test_normal_flow(testdir, pyfile_with_warnings): """ Check that the warnings section is displayed, containing test node ids followed by all warnings generated by that test node. """ result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*== %s ==*' % WARNINGS_SUMMARY_HEADER, - - '*test_normal_flow.py::test_func', - - '*normal_flow_module.py:3: UserWarning: user warning', - '* warnings.warn(UserWarning("user warning"))', - - '*normal_flow_module.py:4: RuntimeWarning: runtime warning', - '* warnings.warn(RuntimeWarning("runtime warning"))', - '* 1 passed, 2 warnings*', - ]) - assert result.stdout.str().count('test_normal_flow.py::test_func') == 1 + result.stdout.fnmatch_lines( + [ + "*== %s ==*" % WARNINGS_SUMMARY_HEADER, + "*test_normal_flow.py::test_func", + "*normal_flow_module.py:3: UserWarning: user warning", + '* warnings.warn(UserWarning("user warning"))', + "*normal_flow_module.py:4: RuntimeWarning: runtime warning", + '* warnings.warn(RuntimeWarning("runtime warning"))', + "* 1 passed, 2 warnings*", + ] + ) + assert result.stdout.str().count("test_normal_flow.py::test_func") == 1 -@pytest.mark.filterwarnings('always') +@pytest.mark.filterwarnings("always") def test_setup_teardown_warnings(testdir, pyfile_with_warnings): - testdir.makepyfile(''' + testdir.makepyfile( + """ import warnings import pytest @@ -69,57 +73,64 @@ def test_setup_teardown_warnings(testdir, pyfile_with_warnings): def test_func(fix): pass - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*== %s ==*' % WARNINGS_SUMMARY_HEADER, - - '*test_setup_teardown_warnings.py:6: UserWarning: warning during setup', - '*warnings.warn(UserWarning("warning during setup"))', - - '*test_setup_teardown_warnings.py:8: UserWarning: warning during teardown', - '*warnings.warn(UserWarning("warning during teardown"))', - '* 1 passed, 2 warnings*', - ]) + result.stdout.fnmatch_lines( + [ + "*== %s ==*" % WARNINGS_SUMMARY_HEADER, + "*test_setup_teardown_warnings.py:6: UserWarning: warning during setup", + '*warnings.warn(UserWarning("warning during setup"))', + "*test_setup_teardown_warnings.py:8: UserWarning: warning during teardown", + '*warnings.warn(UserWarning("warning during teardown"))', + "* 1 passed, 2 warnings*", + ] + ) -@pytest.mark.parametrize('method', ['cmdline', 'ini']) +@pytest.mark.parametrize("method", ["cmdline", "ini"]) def test_as_errors(testdir, pyfile_with_warnings, method): - args = ('-W', 'error') if method == 'cmdline' else () - if method == 'ini': - testdir.makeini(''' + args = ("-W", "error") if method == "cmdline" else () + if method == "ini": + testdir.makeini( + """ [pytest] filterwarnings= error - ''') + """ + ) result = testdir.runpytest(*args) - result.stdout.fnmatch_lines([ - 'E UserWarning: user warning', - 'as_errors_module.py:3: UserWarning', - '* 1 failed in *', - ]) + result.stdout.fnmatch_lines( + [ + "E UserWarning: user warning", + "as_errors_module.py:3: UserWarning", + "* 1 failed in *", + ] + ) -@pytest.mark.parametrize('method', ['cmdline', 'ini']) +@pytest.mark.parametrize("method", ["cmdline", "ini"]) def test_ignore(testdir, pyfile_with_warnings, method): - args = ('-W', 'ignore') if method == 'cmdline' else () - if method == 'ini': - testdir.makeini(''' + args = ("-W", "ignore") if method == "cmdline" else () + if method == "ini": + testdir.makeini( + """ [pytest] filterwarnings= ignore - ''') + """ + ) result = testdir.runpytest(*args) - result.stdout.fnmatch_lines([ - '* 1 passed in *', - ]) + result.stdout.fnmatch_lines(["* 1 passed in *"]) assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() -@pytest.mark.skipif(sys.version_info < (3, 0), - reason='warnings message is unicode is ok in python3') -@pytest.mark.filterwarnings('always') +@pytest.mark.skipif( + sys.version_info < (3, 0), reason="warnings message is unicode is ok in python3" +) +@pytest.mark.filterwarnings("always") def test_unicode(testdir, pyfile_with_warnings): - testdir.makepyfile(''' + testdir.makepyfile( + """ # -*- coding: utf8 -*- import warnings import pytest @@ -132,21 +143,30 @@ def test_unicode(testdir, pyfile_with_warnings): def test_func(fix): pass - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*== %s ==*' % WARNINGS_SUMMARY_HEADER, - '*test_unicode.py:8: UserWarning: \u6d4b\u8bd5*', - '* 1 passed, 1 warnings*', - ]) + result.stdout.fnmatch_lines( + [ + "*== %s ==*" % WARNINGS_SUMMARY_HEADER, + "*test_unicode.py:8: UserWarning: \u6d4b\u8bd5*", + "* 1 passed, 1 warnings*", + ] + ) -@pytest.mark.skipif(sys.version_info >= (3, 0), - reason='warnings message is broken as it is not str instance') +@pytest.mark.skipif( + sys.version_info >= (3, 0), + reason="warnings message is broken as it is not str instance", +) def test_py2_unicode(testdir, pyfile_with_warnings): - if getattr(sys, "pypy_version_info", ())[:2] == (5, 9) and sys.platform.startswith('win'): + if ( + getattr(sys, "pypy_version_info", ())[:2] == (5, 9) + and sys.platform.startswith("win") + ): pytest.xfail("fails with unicode error on PyPy2 5.9 and Windows (#2905)") - testdir.makepyfile(''' + testdir.makepyfile( + """ # -*- coding: utf8 -*- import warnings import pytest @@ -160,21 +180,48 @@ def test_py2_unicode(testdir, pyfile_with_warnings): @pytest.mark.filterwarnings('always') def test_func(fix): pass - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*== %s ==*' % WARNINGS_SUMMARY_HEADER, + result.stdout.fnmatch_lines( + [ + "*== %s ==*" % WARNINGS_SUMMARY_HEADER, + "*test_py2_unicode.py:8: UserWarning: \\u6d4b\\u8bd5", + '*warnings.warn(u"\u6d4b\u8bd5")', + "*warnings.py:*: UnicodeWarning: Warning is using unicode non*", + "* 1 passed, 2 warnings*", + ] + ) - '*test_py2_unicode.py:8: UserWarning: \u6d4b\u8bd5', - '*warnings.warn(u"\u6d4b\u8bd5")', - '*warnings.py:*: UnicodeWarning: Warning is using unicode non*', - '* 1 passed, 2 warnings*', - ]) + +def test_py2_unicode_ascii(testdir): + """Ensure that our warning about 'unicode warnings containing non-ascii messages' + does not trigger with ascii-convertible messages""" + testdir.makeini("[pytest]") + testdir.makepyfile( + """ + import pytest + import warnings + + @pytest.mark.filterwarnings('always') + def test_func(): + warnings.warn(u"hello") + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "*== %s ==*" % WARNINGS_SUMMARY_HEADER, + '*warnings.warn(u"hello")', + "* 1 passed, 1 warnings in*", + ] + ) def test_works_with_filterwarnings(testdir): """Ensure our warnings capture does not mess with pre-installed filters (#2430).""" - testdir.makepyfile(''' + testdir.makepyfile( + """ import warnings class MyWarning(Warning): @@ -189,24 +236,26 @@ def test_works_with_filterwarnings(testdir): assert False except MyWarning: assert True - ''') + """ + ) result = testdir.runpytest() - result.stdout.fnmatch_lines([ - '*== 1 passed in *', - ]) + result.stdout.fnmatch_lines(["*== 1 passed in *"]) -@pytest.mark.parametrize('default_config', ['ini', 'cmdline']) +@pytest.mark.parametrize("default_config", ["ini", "cmdline"]) def test_filterwarnings_mark(testdir, default_config): """ Test ``filterwarnings`` mark works and takes precedence over command line and ini options. """ - if default_config == 'ini': - testdir.makeini(""" + if default_config == "ini": + testdir.makeini( + """ [pytest] filterwarnings = always - """) - testdir.makepyfile(""" + """ + ) + testdir.makepyfile( + """ import warnings import pytest @@ -220,6 +269,22 @@ def test_filterwarnings_mark(testdir, default_config): def test_show_warning(): warnings.warn(RuntimeWarning()) - """) - result = testdir.runpytest('-W always' if default_config == 'cmdline' else '') - result.stdout.fnmatch_lines(['*= 1 failed, 2 passed, 1 warnings in *']) + """ + ) + result = testdir.runpytest("-W always" if default_config == "cmdline" else "") + result.stdout.fnmatch_lines(["*= 1 failed, 2 passed, 1 warnings in *"]) + + +def test_non_string_warning_argument(testdir): + """Non-str argument passed to warning breaks pytest (#2956)""" + testdir.makepyfile( + """ + import warnings + import pytest + + def test(): + warnings.warn(UserWarning(1, u'foo')) + """ + ) + result = testdir.runpytest("-W", "always") + result.stdout.fnmatch_lines(["*= 1 passed, 1 warnings in *"]) diff --git a/third_party/python/pytest/tox.ini b/third_party/python/pytest/tox.ini index 48913bd7b2c6..cb3846d5422a 100644 --- a/third_party/python/pytest/tox.ini +++ b/third_party/python/pytest/tox.ini @@ -1,40 +1,30 @@ [tox] minversion = 2.0 distshare = {homedir}/.tox/distshare -# make sure to update environment list on appveyor.yml +# make sure to update environment list in travis.yml and appveyor.yml envlist = linting - py26 py27 - py33 py34 py35 py36 py37 pypy - {py27,py36}-{pexpect,xdist,trial,numpy} + {py27,py36}-{pexpect,xdist,trial,numpy,pluggymaster} py27-nobyte doctesting - py35-freeze + py36-freeze docs [testenv] -commands = pytest --lsof -rfsxX {posargs:testing} +commands = pytest --lsof -ra {posargs:testing} passenv = USER USERNAME deps = - hypothesis>=3.5.2 + hypothesis>=3.56 nose mock requests -[testenv:py26] -commands = pytest --lsof -rfsxX {posargs:testing} -# pinning mock to last supported version for python 2.6 -deps = - hypothesis<3.0 - nose - mock<1.1 - [testenv:py27-subprocess] changedir = . deps = @@ -42,61 +32,54 @@ deps = mock nose commands = - pytest -n3 -rfsxX --runpytest=subprocess {posargs:testing} + pytest -n3 -ra --runpytest=subprocess {posargs:testing} [testenv:linting] skipsdist = True usedevelop = True -basepython = python2.7 -deps = - flake8 - # pygments required by rst-lint - pygments - restructuredtext_lint -commands = - flake8 pytest.py _pytest testing - {envpython} scripts/check-rst.py +basepython = python3.6 +deps = pre-commit +commands = pre-commit run --all-files --show-diff-on-failure [testenv:py27-xdist] deps = pytest-xdist>=1.13 mock nose - hypothesis>=3.5.2 + hypothesis>=3.56 changedir=testing commands = - pytest -n1 -rfsxX {posargs:.} + pytest -n8 -ra {posargs:.} [testenv:py36-xdist] deps = {[testenv:py27-xdist]deps} -commands = - pytest -n3 -rfsxX {posargs:testing} +commands = {[testenv:py27-xdist]commands} [testenv:py27-pexpect] changedir = testing platform = linux|darwin deps = pexpect commands = - pytest -rfsxX test_pdb.py test_terminal.py test_unittest.py + pytest -ra test_pdb.py test_terminal.py test_unittest.py [testenv:py36-pexpect] -changedir = testing -platform = linux|darwin +changedir = {[testenv:py27-pexpect]changedir} +platform = {[testenv:py27-pexpect]platform} deps = {[testenv:py27-pexpect]deps} -commands = - pytest -rfsxX test_pdb.py test_terminal.py test_unittest.py +commands = {[testenv:py27-pexpect]commands} [testenv:py27-nobyte] deps = pytest-xdist>=1.13 - hypothesis>=3.5.2 + hypothesis>=3.56 + mock distribute = true changedir=testing setenv = PYTHONDONTWRITEBYTECODE=1 commands = - pytest -n3 -rfsxX {posargs:.} + pytest -n3 -ra {posargs:.} [testenv:py27-trial] deps = twisted @@ -105,27 +88,38 @@ commands = [testenv:py36-trial] deps = {[testenv:py27-trial]deps} -commands = - pytest -ra {posargs:testing/test_unittest.py} +commands = {[testenv:py27-trial]commands} [testenv:py27-numpy] -deps=numpy +deps = numpy commands= - pytest -rfsxX {posargs:testing/python/approx.py} + pytest -ra {posargs:testing/python/approx.py} [testenv:py36-numpy] -deps=numpy -commands= - pytest -rfsxX {posargs:testing/python/approx.py} +deps = {[testenv:py27-numpy]deps} +commands = {[testenv:py27-numpy]commands} + +[testenv:py27-pluggymaster] +setenv= + _PYTEST_SETUP_SKIP_PLUGGY_DEP=1 +deps = + {[testenv]deps} + git+https://github.com/pytest-dev/pluggy.git@master + +[testenv:py36-pluggymaster] +setenv = {[testenv:py27-pluggymaster]setenv} +deps = {[testenv:py27-pluggymaster]deps} [testenv:docs] skipsdist = True usedevelop = True -basepython = python changedir = doc/en deps = - sphinx + attrs + more-itertools PyYAML + sphinx + sphinxcontrib-trio commands = sphinx-build -W -b html . _build @@ -134,12 +128,12 @@ commands = basepython = python usedevelop = True skipsdist = True -# ensure the given pyargs cant mean anytrhing else +# ensure the given pyargs can't mean anything else changedir = doc/ deps = PyYAML commands = - pytest -rfsxX en + pytest -ra en pytest --doctest-modules --pyargs _pytest [testenv:regen] @@ -163,14 +157,14 @@ usedevelop = True deps = autopep8 commands = - autopep8 --in-place -r --max-line-length=120 --exclude=vendored_packages,test_source_multiline_block.py _pytest testing + autopep8 --in-place -r --max-line-length=120 --exclude=test_source_multiline_block.py _pytest testing setup.py pytest.py [testenv:jython] changedir = testing commands = - {envpython} {envbindir}/py.test-jython -rfsxX {posargs} + {envpython} {envbindir}/py.test-jython -ra {posargs} -[testenv:py35-freeze] +[testenv:py36-freeze] changedir = testing/freeze deps = pyinstaller commands = @@ -193,8 +187,7 @@ commands = [pytest] minversion = 2.0 plugins = pytester -#--pyargs --doctest-modules --ignore=.tox -addopts = -rxsX -p pytester --ignore=testing/cx_freeze +addopts = -ra -p pytester --ignore=testing/cx_freeze rsyncdirs = tox.ini pytest.py _pytest testing python_files = test_*.py *_test.py testing/*/*.py python_classes = Test Acceptance @@ -211,7 +204,9 @@ filterwarnings = ignore:.*type argument to addoption.*:DeprecationWarning # produced by python >=3.5 on execnet (pytest-xdist) ignore:.*inspect.getargspec.*deprecated, use inspect.signature.*:DeprecationWarning + # ignore warning about package resolution using __spec__ or __package__ + # should be a temporary solution, see #3061 for discussion + ignore:.*can't resolve package from __spec__ or __package__.*:ImportWarning [flake8] max-line-length = 120 -exclude = _pytest/vendored_packages/pluggy.py