Skip to content

Commit

Permalink
Create addtoconfig context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Dick committed Jan 31, 2024
1 parent 19b2967 commit 2761250
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions perfact/zodbsync/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pickle
import pytest
import shutil
from contextlib import contextmanager

import ZEO
import transaction
Expand Down Expand Up @@ -1507,6 +1508,22 @@ def test_replace_child_by_property(self):
self.run('reset', c2)
self.run('reset', c1)

@contextmanager
def appendtoconf(self, text):
"""
Append some text to the config and restore afterward
"""
with open(self.config.path) as f:
orig_config = f.read()
with open(self.config.path, 'a') as f:
f.write('\n' + text + '\n')
# Avoid error regarding reusing runner with changed config
del self.runner
yield
with open(self.config.path, 'w') as f:
f.write(orig_config)
del self.runner

def test_playback_postprocess(self):
"""
Add configuration option for a postprocessing script and check that
Expand All @@ -1521,20 +1538,10 @@ def test_playback_postprocess(self):
with open(fname, 'w') as f:
f.write(script)
os.chmod(fname, 0o700)
with open(self.config.path) as f:
orig_config = f.read()
with open(self.config.path, 'a') as f:
f.write('\nrun_after_playback = "{}"\n'.format(fname))

# Avoid error regarding reusing runner with changed config
del self.runner
self.test_reset()
with open(outfile) as f:
assert json.loads(f.read()) == {"paths": ["/index_html/"]}

with open(self.config.path, 'w') as f:
f.write(orig_config)
del self.runner
with self.appendtoconf('run_after_playback = "{}"'.format(fname)):
self.test_reset()
with open(outfile) as f:
assert json.loads(f.read()) == {"paths": ["/index_html/"]}

def test_playback_hook(self):
"""
Expand Down Expand Up @@ -1621,19 +1628,19 @@ def test_playback_hook_failed(self):
with open(fname, 'w') as f:
f.write(script)
os.chmod(fname, 0o700)
with open(self.config.path) as f:
orig_config = f.read()
with open(self.config.path, 'a') as f:
f.write('\nplayback_hook = "{}"\n'.format(fname))
with self.appendtoconf('playback_hook = "{}"'.format(fname)):
with pytest.raises(AssertionError):
self.run('pick', 'HEAD..{}'.format(commit))

# Avoid error regarding reusing runner with changed config
del self.runner
with pytest.raises(AssertionError):
self.run('pick', 'HEAD..{}'.format(commit))

assert 'NewFolder' not in self.app.objectIds()
assert 'NewFolder2' not in self.app.objectIds()
assert 'NewFolder' not in self.app.objectIds()
assert 'NewFolder2' not in self.app.objectIds()

with open(self.config.path, 'w') as f:
f.write(orig_config)
del self.runner
def test_layer_record(self):
"""
Adjust config to contain an additional fixed layer and record
everything.
"""
path = self.zopeconfig.path + '/layers'
os.mkdir(path)
with self.appendtoconf('layers = "{}"'.format(path)):
self.run('record', '/')

0 comments on commit 2761250

Please sign in to comment.