Skip to content

Commit

Permalink
Report location where the value is empty in mobanfile
Browse files Browse the repository at this point in the history
If empty value is found in mobanfile, moban will report the
line number of that and the name of the file.

Closes #37
  • Loading branch information
seeeturtle committed Dec 2, 2018
1 parent f4b7570 commit 9806b55
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Updated

#. `#37 <https://github.com/moremoban/moban/issues/37>`_: switch from pyyaml to
ruamel.yaml
#. moban will report line number where the value is empty and the name of
mobanfile

0.3.4.1 - 28-11-2018
--------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
ERROR_INVALID_MOBAN_FILE = "%s is an invalid yaml file."
ERROR_NO_TARGETS = "No targets in %s"
ERROR_NO_TEMPLATE = "No template found"
ERROR_MALFORMED_YAML = "Empty value in %s at line %s"


# Shell messages
Expand Down
23 changes: 23 additions & 0 deletions moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def handle_moban_file(moban_file, options):
raise exceptions.MobanfileGrammarException(
constants.ERROR_NO_TARGETS % moban_file
)
check_none(moban_file_configurations, moban_file)
version = moban_file_configurations.get(
constants.MOBAN_VERSION, constants.DEFAULT_MOBAN_VERSION
)
Expand All @@ -125,6 +126,28 @@ def handle_moban_file(moban_file, options):
HASH_STORE.save_hashes()


def check_none(data, moban_file):
"""
check whether the yaml data has empty value such as:
"""
if isinstance(data, dict):
for k, v in data.items():
if check_none(v, moban_file) is None:
loc = data.lc.key(k)
raise exceptions.MobanfileGrammarException(
constants.ERROR_MALFORMED_YAML
% (moban_file, loc[0] + 1) # line number starts from 0
)
elif isinstance(data, list):
for i, x in enumerate(data):
if check_none(x, moban_file) is None:
loc = data.lc.item(i)
raise exceptions.MobanfileGrammarException(
constants.ERROR_MALFORMED_YAML % (moban_file, loc[0] + 1)
)
return data


def handle_command_line(options):
"""
act upon command options
Expand Down
4 changes: 2 additions & 2 deletions moban/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import stat
import errno

from ruamel.yaml import YAML
import moban.reporter as reporter
import moban.constants as constants
import moban.exceptions as exceptions
from ruamel.yaml import YAML


def merge(left, right):
Expand Down Expand Up @@ -37,7 +37,7 @@ def open_yaml(base_dir, file_name):
"""
the_yaml_file = search_file(base_dir, file_name)
with open(the_yaml_file, "r") as data_yaml:
yaml = YAML(typ='safe')
yaml = YAML(typ="rt")
data = yaml.load(data_yaml)
if data is not None:
parent_data = None
Expand Down
37 changes: 36 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import moban.exceptions as exceptions
from mock import patch
from nose.tools import raises
from nose.tools import raises, assert_raises


class TestException:
Expand All @@ -30,6 +30,41 @@ def test_handle_moban_file(self):

main.handle_moban_file(self.moban_file, {})

def test_check_none(self):
import moban.main as main
from ruamel.yaml import YAML

yaml = YAML()

invalid_data = [
"""targets:
- output:""",
"""configuration:
template_dir:
- cool-templates
-""",
]

valid_data = [
"""targets:
- output: template""",
"""configuration:
template_dir:
- cool-templates
- custom-templates""",
]

for data in (yaml.load(d) for d in invalid_data):
assert_raises(
exceptions.MobanfileGrammarException,
main.check_none,
data,
".moban.yaml",
)

for data in (yaml.load(d) for d in valid_data):
main.check_none(data, ".moban.yaml")

@raises(exceptions.MobanfileGrammarException)
def test_version_1_is_recognized(self):
copyfile(
Expand Down

0 comments on commit 9806b55

Please sign in to comment.