Skip to content

Commit

Permalink
added excpetion handling to vadliate_dir. natcap#62
Browse files Browse the repository at this point in the history
  • Loading branch information
davemfish committed Feb 4, 2025
1 parent 8706288 commit 7afb099
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Release History
https://github.com/natcap/geometamaker/issues/71
* Fixed a bug in formatting of validation messages about nested attributes
https://github.com/natcap/geometamaker/issues/65
* Added exception handling to make validating directories more resilient to
unreadable yaml files. https://github.com/natcap/geometamaker/issues/62

0.1.0 (2025-01-10)
------------------
Expand Down
12 changes: 7 additions & 5 deletions src/geometamaker/geometamaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,17 @@ def validate_dir(directory, recursive=False):
for filepath in file_list:
if filepath.endswith('.yml'):
yaml_files.append(filepath)
msg = ''
try:
error = validate(filepath)
if error:
messages.append(error)
else:
messages.append('')
msg = error
except ValueError:
messages.append(
'does not appear to be a geometamaker document')
msg = 'does not appear to be a geometamaker document'
except yaml.YAMLError as exc:
LOGGER.debug(exc)
msg = 'is not a readable yaml document'
messages.append(msg)

return (yaml_files, messages)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_geometamaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,22 @@ def test_describe_validate_dir(self):
self.workspace_dir, recursive=True)
self.assertEqual(len(yaml_files), 2)

def test_validate_dir_handles_exception(self):
"""Test validate_dir function handles yaml exceptions."""
import geometamaker

yaml_path = os.path.join(self.workspace_dir, 'foo.yml')
with open(yaml_path, 'w') as file:
# An example from a yaml file containing some jinja templating.
# This should raise a yaml.scanner.ScannerError:
# while scanning for the next token
# found character '%' that cannot start any token
file.write('{% set name = "simplejson" %}')

yaml_files, msgs = geometamaker.validate_dir(self.workspace_dir)
self.assertEqual(len(yaml_files), 1)
self.assertEqual(msgs[0], 'is not a readable yaml document')

def test_describe_dir_with_shapefile(self):
"""Test describe directory containing a multi-file dataset."""
import geometamaker
Expand Down

0 comments on commit 7afb099

Please sign in to comment.