Skip to content

Commit 9603a71

Browse files
authored
Fail if a folder has no meta on any layer (#155)
1 parent 01b4172 commit 9603a71

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

perfact/zodbsync/tests/test_sync.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,3 +2329,27 @@ def test_layer_info_datafs(self):
23292329
)
23302330
self.run('record', '/')
23312331
assert getattr(self.app.blob, 'zodbsync_layer', None) is None
2332+
2333+
def test_fail_when_meta_is_missing(self):
2334+
"""
2335+
Check that playing back a structure where no layer has a meta file for
2336+
a given folder does not work.
2337+
"""
2338+
root = f'{self.repo.path}/__root__'
2339+
os.mkdir(f'{root}/newfolder')
2340+
os.mkdir(f'{root}/newobj')
2341+
with open(f'{root}/newobj/__source__.py', 'w'):
2342+
pass
2343+
with pytest.raises(AssertionError):
2344+
self.run('playback', '/')
2345+
2346+
def test_fail_when_meta_missing_layers(self):
2347+
"""
2348+
Check that playing back a structure where no layer has a meta file for
2349+
a given folder does not work (multi-layer).
2350+
"""
2351+
with self.addlayer() as layer:
2352+
os.mkdir(f'{self.repo.path}/__root__/newfolder')
2353+
os.mkdir(f'{layer}/workdir/__root__/newfolder')
2354+
with pytest.raises(AssertionError):
2355+
self.run('playback', '/')

perfact/zodbsync/zodbsync.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def mod_read(obj=None, onerrorstop=False, default_owner=None,
7979

8080
if meta_type not in known_types:
8181
if onerrorstop:
82-
assert False, "Unsupported type: %s" % meta_type
82+
raise AssertionError(f"Unsupported type: {meta_type}")
8383
else:
8484
meta['unsupported'] = meta_type
8585
return meta
@@ -139,7 +139,8 @@ def mod_write(data, parent=None, obj_id=None, override=False, root=None,
139139
temp_obj = None
140140
# ID exists? Check for type
141141
if obj and obj.meta_type != meta_type:
142-
assert override, "Type mismatch for object " + repr(data)
142+
if not override:
143+
raise AssertionError(f"Type mismatch for object {data!r}")
143144
contents = obj_contents(obj)
144145
if contents:
145146
# Rename so we can cut+paste the children
@@ -322,10 +323,11 @@ def start_transaction(self, note=''):
322323
# Log in as a manager
323324
uf = self.app.acl_users
324325
user = uf.getUser(self.manager_user).__of__(uf)
325-
assert user is not None, (
326-
'User %s is not available in database. Perhaps you need to set'
327-
' create_manager_user in config.py?' % self.manager_user
328-
)
326+
if user is None:
327+
raise AssertionError(
328+
f'User {self.manager_user} is not available in database.'
329+
' Perhaps you need to set create_manager_user in config.py?'
330+
)
329331

330332
self.logger.info('Using user %s' % self.manager_user)
331333
AccessControl.SecurityManagement.newSecurityManager(None, user)
@@ -424,7 +426,8 @@ def fs_pathinfo(self, path):
424426
'layeridx': None,
425427
}
426428
path = path.lstrip('/')
427-
children = set()
429+
candidates = set() # subfolders on any layer
430+
children = set() # those with a __meta__ file on a some layer
428431
for idx, layer in enumerate(layers):
429432
fspath = os.path.join(layer['workdir'], self.site, path)
430433
if not os.path.isdir(fspath):
@@ -437,8 +440,14 @@ def fs_pathinfo(self, path):
437440
for entry in os.listdir(fspath):
438441
if entry in children or entry.startswith('__'):
439442
continue
443+
candidates.add(entry)
440444
if os.path.exists(os.path.join(fspath, entry, '__meta__')):
441445
children.add(entry)
446+
missing = candidates - children
447+
if missing:
448+
raise AssertionError(
449+
f"No __meta__ file on any layer: {path}/{children}"
450+
)
442451

443452
result['children'] = sorted(children)
444453
return result
@@ -632,11 +641,13 @@ def fs_parse(self, fspath, data=None):
632641
if data is None:
633642
data = self.fs_read(fspath)
634643

635-
assert 'meta' in data, 'Missing meta file: ' + fspath
644+
if 'meta' not in data:
645+
raise AssertionError(f"Missing meta file: {fspath}")
636646
src_fnames = data.get('src_fnames', [])
637-
assert len(src_fnames) <= 1, (
638-
"Multiple source files in " + fspath
639-
)
647+
if len(src_fnames) > 1:
648+
raise AssertionError(
649+
f"Multiple source files in {fspath}"
650+
)
640651
result = dict(literal_eval(data['meta']))
641652
if src_fnames:
642653
src_fname = src_fnames[0]

0 commit comments

Comments
 (0)