#2574: --fixtures, --fixtures-per-test keep indentation of docstring

This commit is contained in:
Martin Altmayer 2017-07-15 13:33:11 +02:00
parent 3578f4e405
commit e5169a026a
2 changed files with 81 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import sys
import os import os
import collections import collections
import math import math
from textwrap import dedent
from itertools import count from itertools import count
import py import py
@ -1003,14 +1004,12 @@ def _show_fixtures_per_test(config, session):
funcargspec = argname funcargspec = argname
tw.line(funcargspec, green=True) tw.line(funcargspec, green=True)
INDENT = ' {0}'
fixture_doc = fixture_def.func.__doc__ fixture_doc = fixture_def.func.__doc__
if fixture_doc: if fixture_doc:
for line in fixture_doc.strip().split('\n'): write_docstring(tw, fixture_doc)
tw.line(INDENT.format(line.strip()))
else: else:
tw.line(INDENT.format('no docstring available'), red=True) tw.line(' no docstring available', red=True)
def write_item(item): def write_item(item):
name2fixturedefs = item._fixtureinfo.name2fixturedefs name2fixturedefs = item._fixtureinfo.name2fixturedefs
@ -1084,13 +1083,28 @@ def _showfixtures_main(config, session):
loc = getlocation(fixturedef.func, curdir) loc = getlocation(fixturedef.func, curdir)
doc = fixturedef.func.__doc__ or "" doc = fixturedef.func.__doc__ or ""
if doc: if doc:
for line in doc.strip().split("\n"): write_docstring(tw, doc)
tw.line(" " + line.strip())
else: else:
tw.line(" %s: no docstring available" %(loc,), tw.line(" %s: no docstring available" %(loc,),
red=True) red=True)
def write_docstring(tw, doc):
INDENT = " "
doc = doc.rstrip()
if "\n" in doc:
firstline, rest = doc.split("\n", 1)
else:
firstline, rest = doc, ""
if firstline.strip():
tw.line(INDENT + firstline.strip())
if rest:
for line in dedent(rest).split("\n"):
tw.write(INDENT + line + "\n")
# builtin pytest.raises helper # builtin pytest.raises helper
def raises(expected_exception, *args, **kwargs): def raises(expected_exception, *args, **kwargs):

View File

@ -2713,7 +2713,7 @@ class TestShowFixtures(object):
""") """)
def test_show_fixtures_trimmed_doc(self, testdir): def test_show_fixtures_trimmed_doc(self, testdir):
p = testdir.makepyfile(''' p = testdir.makepyfile(dedent('''
import pytest import pytest
@pytest.fixture @pytest.fixture
def arg1(): def arg1():
@ -2729,9 +2729,9 @@ class TestShowFixtures(object):
line2 line2
""" """
''') '''))
result = testdir.runpytest("--fixtures", p) result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(""" result.stdout.fnmatch_lines(dedent("""
* fixtures defined from test_show_fixtures_trimmed_doc * * fixtures defined from test_show_fixtures_trimmed_doc *
arg2 arg2
line1 line1
@ -2740,7 +2740,64 @@ class TestShowFixtures(object):
line1 line1
line2 line2
""") """))
def test_show_fixtures_indented_doc(self, testdir):
p = testdir.makepyfile(dedent('''
import pytest
@pytest.fixture
def fixture1():
"""
line1
indented line
"""
'''))
result = testdir.runpytest("--fixtures", p)
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('''
import pytest
@pytest.fixture
def fixture1():
"""line1
line2
indented line
"""
'''))
result = testdir.runpytest("--fixtures", p)
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('''
import pytest
class TestClass:
@pytest.fixture
def fixture1():
"""line1
line2
indented line
"""
'''))
result = testdir.runpytest("--fixtures", p)
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): def test_show_fixtures_different_files(self, testdir):