Replace Source with dedent where possible

This commit is contained in:
Anthony Sottile
2018-08-23 09:06:17 -07:00
parent 6f7365509d
commit 85482d575e
14 changed files with 562 additions and 568 deletions

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os
import sys
from textwrap import dedent
import textwrap
import _pytest._code
import pytest
@@ -47,13 +47,14 @@ class TestModule(object):
p = root2.join("test_x456.py")
monkeypatch.syspath_prepend(str(root1))
p.write(
dedent(
textwrap.dedent(
"""\
import x456
def test():
assert x456.__file__.startswith(%r)
"""
% str(root2)
import x456
def test():
assert x456.__file__.startswith({!r})
""".format(
str(root2)
)
)
)
with root2.as_cwd():
@@ -929,23 +930,23 @@ class TestConftestCustomization(object):
def test_customized_pymakemodule_issue205_subdir(self, testdir):
b = testdir.mkdir("a").mkdir("b")
b.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makemodule():
outcome = yield
mod = outcome.get_result()
mod.obj.hello = "world"
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makemodule():
outcome = yield
mod = outcome.get_result()
mod.obj.hello = "world"
"""
)
)
b.join("test_module.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
def test_hello():
assert hello == "world"
"""
def test_hello():
assert hello == "world"
"""
)
)
reprec = testdir.inline_run()
@@ -954,31 +955,31 @@ class TestConftestCustomization(object):
def test_customized_pymakeitem(self, testdir):
b = testdir.mkdir("a").mkdir("b")
b.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makeitem():
outcome = yield
if outcome.excinfo is None:
result = outcome.get_result()
if result:
for func in result:
func._some123 = "world"
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makeitem():
outcome = yield
if outcome.excinfo is None:
result = outcome.get_result()
if result:
for func in result:
func._some123 = "world"
"""
)
)
b.join("test_module.py").write(
_pytest._code.Source(
"""
import pytest
textwrap.dedent(
"""\
import pytest
@pytest.fixture()
def obj(request):
return request.node._some123
def test_hello(obj):
assert obj == "world"
"""
@pytest.fixture()
def obj(request):
return request.node._some123
def test_hello(obj):
assert obj == "world"
"""
)
)
reprec = testdir.inline_run()
@@ -1033,7 +1034,7 @@ class TestConftestCustomization(object):
)
testdir.makefile(
".narf",
"""
"""\
def test_something():
assert 1 + 1 == 2""",
)
@@ -1046,29 +1047,29 @@ def test_setup_only_available_in_subdir(testdir):
sub1 = testdir.mkpydir("sub1")
sub2 = testdir.mkpydir("sub2")
sub1.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub1"
"""
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub1"
"""
)
)
sub2.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub2"
"""
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub2"
"""
)
)
sub1.join("test_in_sub1.py").write("def test_1(): pass")
@@ -1547,12 +1548,12 @@ def test_skip_duplicates_by_default(testdir):
a = testdir.mkdir("a")
fh = a.join("test_a.py")
fh.write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def test_real():
pass
"""
import pytest
def test_real():
pass
"""
)
)
result = testdir.runpytest(a.strpath, a.strpath)
@@ -1567,12 +1568,12 @@ def test_keep_duplicates(testdir):
a = testdir.mkdir("a")
fh = a.join("test_a.py")
fh.write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def test_real():
pass
"""
import pytest
def test_real():
pass
"""
)
)
result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath)

View File

@@ -1,6 +1,5 @@
from textwrap import dedent
import textwrap
import _pytest._code
import pytest
from _pytest.pytester import get_public_names
from _pytest.fixtures import FixtureLookupError, FixtureRequest
@@ -208,23 +207,23 @@ class TestFillFixtures(object):
)
subdir = testdir.mkpydir("subdir")
subdir.join("conftest.py").write(
_pytest._code.Source(
"""
import pytest
textwrap.dedent(
"""\
import pytest
@pytest.fixture
def spam():
return 'spam'
"""
@pytest.fixture
def spam():
return 'spam'
"""
)
)
testfile = subdir.join("test_spam.py")
testfile.write(
_pytest._code.Source(
textwrap.dedent(
"""\
def test_spam(spam):
assert spam == "spam"
"""
def test_spam(spam):
assert spam == "spam"
"""
)
)
result = testdir.runpytest()
@@ -276,26 +275,26 @@ class TestFillFixtures(object):
)
subdir = testdir.mkpydir("subdir")
subdir.join("conftest.py").write(
_pytest._code.Source(
"""
import pytest
textwrap.dedent(
"""\
import pytest
@pytest.fixture(params=[1, 2, 3])
def spam(request):
return request.param
"""
@pytest.fixture(params=[1, 2, 3])
def spam(request):
return request.param
"""
)
)
testfile = subdir.join("test_spam.py")
testfile.write(
_pytest._code.Source(
"""
params = {'spam': 1}
textwrap.dedent(
"""\
params = {'spam': 1}
def test_spam(spam):
assert spam == params['spam']
params['spam'] += 1
"""
def test_spam(spam):
assert spam == params['spam']
params['spam'] += 1
"""
)
)
result = testdir.runpytest()
@@ -320,26 +319,26 @@ class TestFillFixtures(object):
)
subdir = testdir.mkpydir("subdir")
subdir.join("conftest.py").write(
_pytest._code.Source(
"""
import pytest
textwrap.dedent(
"""\
import pytest
@pytest.fixture(params=[1, 2, 3])
def spam(request):
return request.param
"""
@pytest.fixture(params=[1, 2, 3])
def spam(request):
return request.param
"""
)
)
testfile = subdir.join("test_spam.py")
testfile.write(
_pytest._code.Source(
"""
params = {'spam': 1}
textwrap.dedent(
"""\
params = {'spam': 1}
def test_spam(spam):
assert spam == params['spam']
params['spam'] += 1
"""
def test_spam(spam):
assert spam == params['spam']
params['spam'] += 1
"""
)
)
result = testdir.runpytest()
@@ -807,13 +806,13 @@ class TestRequestBasic(object):
# this tests that normalization of nodeids takes place
b = testdir.mkdir("tests").mkdir("unit")
b.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
@pytest.fixture
def arg1():
pass
"""
import pytest
@pytest.fixture
def arg1():
pass
"""
)
)
p = b.join("test_module.py")
@@ -1484,41 +1483,41 @@ class TestFixtureManagerParseFactories(object):
runner = testdir.mkdir("runner")
package = testdir.mkdir("package")
package.join("conftest.py").write(
dedent(
textwrap.dedent(
"""\
import pytest
@pytest.fixture
def one():
return 1
"""
"""
)
)
package.join("test_x.py").write(
dedent(
textwrap.dedent(
"""\
def test_x(one):
assert one == 1
"""
def test_x(one):
assert one == 1
"""
)
)
sub = package.mkdir("sub")
sub.join("__init__.py").ensure()
sub.join("conftest.py").write(
dedent(
textwrap.dedent(
"""\
import pytest
@pytest.fixture
def one():
return 2
"""
import pytest
@pytest.fixture
def one():
return 2
"""
)
)
sub.join("test_y.py").write(
dedent(
textwrap.dedent(
"""\
def test_x(one):
assert one == 2
"""
def test_x(one):
assert one == 2
"""
)
)
reprec = testdir.inline_run()
@@ -1535,44 +1534,44 @@ class TestFixtureManagerParseFactories(object):
)
package = testdir.mkdir("package")
package.join("__init__.py").write(
dedent(
textwrap.dedent(
"""\
from .. import values
def setup_module():
values.append("package")
def teardown_module():
values[:] = []
"""
from .. import values
def setup_module():
values.append("package")
def teardown_module():
values[:] = []
"""
)
)
package.join("test_x.py").write(
dedent(
textwrap.dedent(
"""\
from .. import values
def test_x():
assert values == ["package"]
"""
from .. import values
def test_x():
assert values == ["package"]
"""
)
)
package = testdir.mkdir("package2")
package.join("__init__.py").write(
dedent(
textwrap.dedent(
"""\
from .. import values
def setup_module():
values.append("package2")
def teardown_module():
values[:] = []
"""
from .. import values
def setup_module():
values.append("package2")
def teardown_module():
values[:] = []
"""
)
)
package.join("test_x.py").write(
dedent(
textwrap.dedent(
"""\
from .. import values
def test_x():
assert values == ["package2"]
"""
from .. import values
def test_x():
assert values == ["package2"]
"""
)
)
reprec = testdir.inline_run()
@@ -1587,32 +1586,32 @@ class TestFixtureManagerParseFactories(object):
package = testdir.mkdir("package")
package.join("__init__.py").write("")
package.join("conftest.py").write(
dedent(
textwrap.dedent(
"""\
import pytest
from .. import values
@pytest.fixture(scope="package")
def one():
values.append("package")
yield values
values.pop()
@pytest.fixture(scope="package", autouse=True)
def two():
values.append("package-auto")
yield values
values.pop()
"""
import pytest
from .. import values
@pytest.fixture(scope="package")
def one():
values.append("package")
yield values
values.pop()
@pytest.fixture(scope="package", autouse=True)
def two():
values.append("package-auto")
yield values
values.pop()
"""
)
)
package.join("test_x.py").write(
dedent(
textwrap.dedent(
"""\
from .. import values
def test_package_autouse():
assert values == ["package-auto"]
def test_package(one):
assert values == ["package-auto", "package"]
"""
from .. import values
def test_package_autouse():
assert values == ["package-auto"]
def test_package(one):
assert values == ["package-auto", "package"]
"""
)
)
reprec = testdir.inline_run()
@@ -1804,24 +1803,24 @@ class TestAutouseManagement(object):
def test_autouse_conftest_mid_directory(self, testdir):
pkgdir = testdir.mkpydir("xyz123")
pkgdir.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
@pytest.fixture(autouse=True)
def app():
import sys
sys._myapp = "hello"
"""
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(
textwrap.dedent(
"""\
import sys
def test_app():
assert sys._myapp == "hello"
"""
import sys
def test_app():
assert sys._myapp == "hello"
"""
)
)
reprec = testdir.inline_run("-s")
@@ -2715,17 +2714,17 @@ class TestFixtureMarker(object):
)
b = testdir.mkdir("subdir")
b.join("test_overridden_fixture_finalizer.py").write(
dedent(
"""
import pytest
@pytest.fixture
def browser(browser):
browser['visited'] = True
return browser
textwrap.dedent(
"""\
import pytest
@pytest.fixture
def browser(browser):
browser['visited'] = True
return browser
def test_browser(browser):
assert browser['visited'] is True
"""
def test_browser(browser):
assert browser['visited'] is True
"""
)
)
reprec = testdir.runpytest("-s")
@@ -3217,120 +3216,119 @@ class TestShowFixtures(object):
def test_show_fixtures_trimmed_doc(self, testdir):
p = testdir.makepyfile(
dedent(
textwrap.dedent(
'''\
import pytest
@pytest.fixture
def arg1():
"""
line1
line2
"""
@pytest.fixture
def arg2():
"""
line1
line2
"""
'''
import pytest
@pytest.fixture
def arg1():
"""
line1
line2
"""
@pytest.fixture
def arg2():
"""
line1
line2
"""
'''
)
)
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(
dedent(
textwrap.dedent(
"""\
* fixtures defined from test_show_fixtures_trimmed_doc *
arg2
line1
line2
arg1
line1
line2
"""
* fixtures defined from test_show_fixtures_trimmed_doc *
arg2
line1
line2
arg1
line1
line2
"""
)
)
def test_show_fixtures_indented_doc(self, testdir):
p = testdir.makepyfile(
dedent(
textwrap.dedent(
'''\
import pytest
@pytest.fixture
def fixture1():
"""
line1
indented line
"""
'''
import pytest
@pytest.fixture
def fixture1():
"""
line1
indented line
"""
'''
)
)
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(
dedent(
textwrap.dedent(
"""\
* fixtures defined from test_show_fixtures_indented_doc *
fixture1
line1
indented line
"""
* 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(
textwrap.dedent(
'''\
import pytest
@pytest.fixture
def fixture1():
"""line1
line2
indented line
"""
'''
import pytest
@pytest.fixture
def fixture1():
"""line1
line2
indented line
"""
'''
)
)
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(
dedent(
textwrap.dedent(
"""\
* fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *
fixture1
line1
line2
indented line
"""
* 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(
textwrap.dedent(
'''\
import pytest
class TestClass(object):
@pytest.fixture
def fixture1(self):
"""line1
line2
indented line
"""
'''
import pytest
class TestClass(object):
@pytest.fixture
def fixture1(self):
"""line1
line2
indented line
"""
'''
)
)
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(
dedent(
textwrap.dedent(
"""\
* fixtures defined from test_show_fixtures_indented_in_class *
fixture1
line1
line2
indented line
"""
* fixtures defined from test_show_fixtures_indented_in_class *
fixture1
line1
line2
indented line
"""
)
)
@@ -3667,26 +3665,26 @@ class TestParameterizedSubRequest(object):
fixdir = testdir.mkdir("fixtures")
fixfile = fixdir.join("fix.py")
fixfile.write(
_pytest._code.Source(
"""
import pytest
textwrap.dedent(
"""\
import pytest
@pytest.fixture(params=[0, 1, 2])
def fix_with_param(request):
return request.param
"""
@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(
"""
from fix import fix_with_param
textwrap.dedent(
"""\
from fix import fix_with_param
def test_foo(request):
request.getfixturevalue('fix_with_param')
"""
def test_foo(request):
request.getfixturevalue('fix_with_param')
"""
)
)
@@ -3698,9 +3696,9 @@ class TestParameterizedSubRequest(object):
E*Failed: The requested fixture has no parameter defined for the current test.
E*
E*Requested fixture 'fix_with_param' defined in:
E*fix.py:5
E*fix.py:4
E*Requested here:
E*test_foos.py:5
E*test_foos.py:4
*1 failed*
"""
)

View File

@@ -2,7 +2,7 @@
import re
import sys
import attr
import _pytest._code
import textwrap
import pytest
from _pytest import python, fixtures
@@ -1271,19 +1271,19 @@ class TestMetafuncFunctional(object):
sub1 = testdir.mkpydir("sub1")
sub2 = testdir.mkpydir("sub2")
sub1.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
def pytest_generate_tests(metafunc):
assert metafunc.function.__name__ == "test_1"
"""
def pytest_generate_tests(metafunc):
assert metafunc.function.__name__ == "test_1"
"""
)
)
sub2.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
def pytest_generate_tests(metafunc):
assert metafunc.function.__name__ == "test_2"
"""
def pytest_generate_tests(metafunc):
assert metafunc.function.__name__ == "test_2"
"""
)
)
sub1.join("test_in_sub1.py").write("def test_1(): pass")