Skip to content

Commit

Permalink
Catch ScannerError explicitly
Browse files Browse the repository at this point in the history
* OSBS-5904

Signed-off-by: Adam Cmiel <acmiel@redhat.com>
  • Loading branch information
chmeliik authored and MartinBasti committed May 15, 2019
1 parent bd01849 commit 903024e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
8 changes: 7 additions & 1 deletion osbs/repo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from __future__ import absolute_import

from osbs.exceptions import OsbsException
from osbs.constants import REPO_CONFIG_FILE, ADDITIONAL_TAGS_FILE, REPO_CONTAINER_CONFIG
from six import StringIO
from six.moves.configparser import ConfigParser
Expand Down Expand Up @@ -61,7 +62,12 @@ def __init__(self, dir_path='', file_name=REPO_CONFIG_FILE, depth=None):
file_path = os.path.join(dir_path, REPO_CONTAINER_CONFIG)
if os.path.exists(file_path):
with open(file_path) as f:
self.container = (yaml.load(f) or {})
try:
self.container = yaml.load(f) or {}
except yaml.scanner.ScannerError as e:
msg = ('Failed to parse YAML file "{file}": {reason}'
.format(file=REPO_CONTAINER_CONFIG, reason=e))
raise OsbsException(msg)

# container values may be set to None
container_compose = self.container.get('compose') or {}
Expand Down
10 changes: 2 additions & 8 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,10 @@ def get_repo_info(*args, **kwargs):
'user': TEST_USER
}

with pytest.raises(Exception) as exc_info:
with pytest.raises(OsbsException) as exc_info:
osbs.create_build(**kwargs)

err_msg = (
'while scanning a simple key\n'
' in "{file}", line 2, column 1\n'
"could not find expected ':'\n"
' in "{file}", line 2, column 4'
).format(file=repo_config)

err_msg = 'Failed to parse YAML file "{file}"'.format(file=REPO_CONTAINER_CONFIG)
assert err_msg in str(exc_info.value)

# osbs is a fixture here
Expand Down
17 changes: 17 additions & 0 deletions tests/test_repo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from __future__ import absolute_import

from flexmock import flexmock
from osbs.exceptions import OsbsException
from osbs.constants import REPO_CONFIG_FILE, ADDITIONAL_TAGS_FILE, REPO_CONTAINER_CONFIG
from osbs.repo_utils import RepoInfo, RepoConfiguration, AdditionalTagsConfig, ModuleSpec
from textwrap import dedent
Expand Down Expand Up @@ -42,6 +43,22 @@ def test_default_values(self):
conf = RepoConfiguration()
assert conf.is_autorebuild_enabled() is False

def test_invalid_yaml(self, tmpdir):
yaml_file = tmpdir.join(REPO_CONTAINER_CONFIG)
yaml_file.write('\n'.join(['hallo: 1', 'bye']))

with pytest.raises(OsbsException) as exc_info:
RepoConfiguration(dir_path=str(tmpdir))

err_msg = (
'Failed to parse YAML file "{file_basename}": while scanning a simple key\n'
' in "{file}", line 2, column 1\n'
"could not find expected ':'\n"
' in "{file}", line 2, column 4'
).format(file=yaml_file, file_basename=REPO_CONTAINER_CONFIG)

assert str(exc_info.value) == err_msg

@pytest.mark.parametrize(('config_value', 'expected_value'), (
(None, False),
('false', False),
Expand Down

0 comments on commit 903024e

Please sign in to comment.