Skip to content

Commit

Permalink
Fix sync writing __source__ falsely into frozen layers (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiseqube authored Sep 10, 2024
1 parent 5de3a17 commit 803e4c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
23.1.3+dev.2
* Fixed a bug where zodbsync was writing `source`-files into frozen
layers

23.1.2+dev.1
* Fix layer-update for cases where the last element in both checksum files is
not the same.
Expand Down
36 changes: 35 additions & 1 deletion perfact/zodbsync/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ def test_playback_hook_failed(self):
assert 'NewFolder2' not in self.app.objectIds()

@contextmanager
def addlayer(self, seqnum='00'):
def addlayer(self, seqnum='00', frozen=True):
"""
Create a temp directory and add a config that uses this as additional
code layer.
Expand All @@ -1666,6 +1666,7 @@ def addlayer(self, seqnum='00'):
with tempfile.TemporaryDirectory() as layer:
with open(path, 'w') as f:
f.write('base_dir = "{}"\n'.format(layer))
f.write('frozen = {}\n'.format(frozen))
os.mkdir(os.path.join(layer, '__root__'))
# Force re-reading config
if hasattr(self, 'runner'):
Expand Down Expand Up @@ -2152,3 +2153,36 @@ def test_layer_update_warn(self, caplog):
assert expect + '/Test' in caplog.text
assert 'AttributeError' not in caplog.text
assert expect + '/ToDelete/Sub' in caplog.text

def test_layer_frozen(self):
"""
Verify that changed files are properly written into the custom
layer in case the layer below is frozen.
"""
with self.runner.sync.tm:
self.app.manage_addProduct['OFSP'].manage_addFile(id='blob')

with self.addlayer(frozen=True) as layer:
self.run('record', '/blob')
shutil.move(
'{}/__root__/blob'.format(self.repo.path),
'{}/__root__/blob'.format(layer),
)
with self.runner.sync.tm:
self.app.blob.manage_edit(
filedata='text_content',
content_type='text/plain',
title='BLOB'
)
self.run('record', '/')
root = os.path.join(self.repo.path, '__root__')
# both meta and source file are in custom layer
assert os.path.exists(os.path.join(root, 'blob/__meta__'))
assert os.path.exists(os.path.join(root, 'blob/__source__.txt'))
source_fmt = '{}/__root__/blob/__source__.txt'
with open(source_fmt.format(layer)) as f:
# source in layer should still be empty
assert f.read() == ''
with open(source_fmt.format(self.repo.path)) as f:
# ... content is in custom layer!
assert f.read() == 'text_content'
3 changes: 1 addition & 2 deletions perfact/zodbsync/zodbsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ def fs_write(self, path, data):
obj_id=path.rstrip('/').rsplit('/', 1)[-1],
)
src_fname = '{}.{}'.format(base, ext)
src_path = os.path.join(base_dir, src_fname)
new_data['src_fnames'] = [src_fname]
new_data['source'] = source

Expand All @@ -494,7 +493,7 @@ def fs_write(self, path, data):
self.logger.debug(
"Will write %d bytes of source" % len(source)
)
with open(src_path, 'wb') as f:
with open(os.path.join(write_base, src_fname), 'wb') as f:
f.write(source)

# We wrote the object to the topmost layer, so the index where the
Expand Down

0 comments on commit 803e4c8

Please sign in to comment.