Skip to content

Commit

Permalink
Fix crash when tests changed cwd during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Mar 1, 2018
1 parent df61fcd commit ba7b6fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.2.1
=====

* Fix crash ``IOError`` when tests changed the current working directory in the middle
of the testing session.

0.2.0
=====

Expand Down
2 changes: 2 additions & 0 deletions pytest_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ReplayPlugin(object):

def __init__(self, config):
self.dir = config.getoption('replay_record_dir')
if self.dir:
self.dir = os.path.abspath(self.dir)
self.ext = '.txt'
self.cleanup_scripts()
self.written_nodeids = set()
Expand Down
22 changes: 22 additions & 0 deletions tests/test_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,25 @@ def test(i):
test_ids.extend(x.strip() for x in f.readlines())
expected_ids = ['test_xdist.py::test[{}]'.format(x) for x in range(10)]
assert sorted(test_ids) == sorted(expected_ids)


def test_cwd_changed(testdir):
"""Ensure that the plugin works even if some tests changes cwd."""
testdir.tmpdir.join('subdir').ensure(dir=1)
testdir.makepyfile("""
import os
def test_1():
os.chdir('subdir')
def test_2():
pass
""")
dir = testdir.tmpdir / 'replay'
result = testdir.runpytest_subprocess('--replay-record-dir={}'.format('replay'))
replay_file = dir / '.pytest-replay.txt'
contents = replay_file.readlines(True)
expected = [
'test_cwd_changed.py::test_1\n',
'test_cwd_changed.py::test_2\n',
]
assert contents == expected
assert result.ret == 0

0 comments on commit ba7b6fb

Please sign in to comment.