Skip to content

Commit 43ee958

Browse files
authored
Drop py dependency from test code (#98)
Issue #86 asked for a removal of the legacy `py` library dependency. This change implements a minimal replacement for py.code.Source that we can use to deindent the inlined Python source code embedded in the pytest-random-order's own tests.
1 parent d144ae7 commit 43ee958

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

tests/conftest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,33 @@ def twenty_cls_tests():
4848
for i in range(20):
4949
code.append("\tdef test_b{0}(self): self.assertTrue\n".format(str(i).zfill(2)))
5050
return "".join(code)
51+
52+
53+
def _deindent_source(source):
54+
"""
55+
A minimal replacement for py.code.Source to deindent inlined test code.
56+
Looks for the first non-empty line to determine deindent offset, doesn't
57+
attempt to understand the code, line continuations, etc.
58+
"""
59+
lines = source.splitlines()
60+
for line in lines:
61+
stripped = line.lstrip()
62+
if stripped:
63+
offset = len(line) - len(stripped)
64+
break
65+
else:
66+
offset = 0
67+
68+
output_lines = []
69+
for line in lines:
70+
output_lines.append(line[offset:])
71+
72+
return "\n".join(output_lines)
73+
74+
75+
@pytest.fixture
76+
def deindent_source():
77+
"""
78+
Returns a helper function to deindent inlined source code.
79+
"""
80+
return _deindent_source

tests/test_actual_test_runs.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
import collections
33
import re
44

5-
import py
65
import pytest
76

87

98
@pytest.fixture
10-
def tmp_tree_of_tests(testdir):
9+
def tmp_tree_of_tests(testdir, deindent_source):
1110
"""
1211
Creates a directory structure:
1312
tmpdir/
@@ -25,7 +24,7 @@ def tmp_tree_of_tests(testdir):
2524
sup = testdir.mkpydir("shallow_tests")
2625

2726
sup.join("test_a.py").write(
28-
py.code.Source("""
27+
deindent_source("""
2928
def test_a1():
3029
assert False
3130
def test_a2():
@@ -36,7 +35,7 @@ def test_a3():
3635
)
3736

3837
sup.join("test_ax.py").write(
39-
py.code.Source("""
38+
deindent_source("""
4039
def test_ax1():
4140
assert True
4241
def test_ax2():
@@ -49,7 +48,7 @@ def test_ax3():
4948
sub = testdir.mkpydir("shallow_tests/deep_tests")
5049

5150
sub.join("test_b.py").write(
52-
py.code.Source("""
51+
deindent_source("""
5352
def test_b1():
5453
assert True
5554
def test_b2():
@@ -60,14 +59,14 @@ def test_b3():
6059
)
6160

6261
sub.join("test_c.py").write(
63-
py.code.Source("""
62+
deindent_source("""
6463
def test_c1():
6564
assert True
6665
""")
6766
)
6867

6968
sub.join("test_d.py").write(
70-
py.code.Source("""
69+
deindent_source("""
7170
def test_d1():
7271
assert True
7372
def test_d2():
@@ -76,7 +75,7 @@ def test_d2():
7675
)
7776

7877
sub.join("test_e.py").write(
79-
py.code.Source("""
78+
deindent_source("""
8079
from unittest import TestCase
8180
class EeTest(TestCase):
8281
def test_ee1(self):

tests/test_doctests.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# -*- coding: utf-8 -*-
2-
import py
32
import pytest
43

54

65
@pytest.fixture
7-
def tmp_tree_of_tests(testdir):
6+
def tmp_tree_of_tests(testdir, deindent_source):
87
"""
98
Creates a directory structure:
109
tmpdir/
@@ -16,7 +15,7 @@ def tmp_tree_of_tests(testdir):
1615
utils_package.join("__init__.py").write("")
1716

1817
utils_package.join("foo.py").write(
19-
py.code.Source('''
18+
deindent_source('''
2019
def add(a, b):
2120
"""
2221
>>> add(1, 1)

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ envlist = py{39,310,311,312,313}
44

55
[testenv]
66
deps =
7-
py
87
pytest-xdist
98
commands =
109
pytest tests --color=yes

0 commit comments

Comments
 (0)