Skip to content

Commit

Permalink
✨ update exit code. resolves #157. update #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Jan 11, 2019
1 parent a0cf4df commit 39b544d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
1 change: 1 addition & 0 deletions .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ releases:
- action: Updated
details:
- "`#154`: introduce first ever positional argument for string base template."
- "`#157`: the exit code behavior changed. for backward compactibility please use --exit-code. Otherwise, moban will not tell if there is any changes."
date: 11-1-2019
version: 0.3.8
- changes:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Updated

#. `#154 <https://github.com/moremoban/moban/issues/154>`_: introduce first ever
positional argument for string base template.
#. `#157 <https://github.com/moremoban/moban/issues/157>`_: the exit code
behavior changed. for backward compactibility please use --exit-code.
Otherwise, moban will not tell if there is any changes.

0.3.7 - 6-1-2019
--------------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@ Usage
the template type, default is jinja2
-f force moban to template all files despite of
.moban.hashes
--exit-code tell moban to change exit code
-m MOBANFILE, --mobanfile MOBANFILE
custom moban file
exit codes
Exit codes
--------------------------------------------------------------------------------
By default:
- 0 : no changes
- 1 : error occured
With `--exit-code`:
- 0 : no changes
- 1 : has changes
Expand Down
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
LABEL_MOBANFILE = "mobanfile"
LABEL_FORCE = "force"
LABEL_REQUIRES = "requires"
LABEL_EXIT_CODE = "exit-code"

DEFAULT_CONFIGURATION_DIRNAME = ".moban.cd"
DEFAULT_TEMPLATE_DIRNAME = ".moban.td"
Expand Down
26 changes: 20 additions & 6 deletions moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,30 @@ def main():
if moban_file:
try:
count = handle_moban_file(moban_file, options)
if count:
sys.exit(count)
moban_exit(options[constants.LABEL_EXIT_CODE], count)
except (
exceptions.DirectoryNotFound,
exceptions.NoThirdPartyEngine,
exceptions.MobanfileGrammarException,
) as e:
reporter.report_error_message(str(e))
sys.exit(constants.ERROR)
moban_exit(options[constants.LABEL_EXIT_CODE], constants.ERROR)
else:
try:
count = handle_command_line(options)
if count:
sys.exit(count)
moban_exit(options[constants.LABEL_EXIT_CODE], count)
except exceptions.NoTemplate as e:
reporter.report_error_message(str(e))
sys.exit(constants.ERROR)
moban_exit(options[constants.LABEL_EXIT_CODE], constants.ERROR)


def moban_exit(exit_code_toggle_flag, exit_code):
if exit_code_toggle_flag:
if exit_code:
sys.exit(exit_code)
else:
if exit_code == constants.ERROR:
sys.exit(1)


def create_parser():
Expand Down Expand Up @@ -91,6 +98,13 @@ def create_parser():
default=False,
help="force moban to template all files despite of .moban.hashes",
)
parser.add_argument(
"--%s" % constants.LABEL_EXIT_CODE,
action="store_true",
dest=constants.LABEL_EXIT_CODE,
default=False,
help="tell moban to change exit code",
)
parser.add_argument(
"-m", "--%s" % constants.LABEL_MOBANFILE, help="custom moban file"
)
Expand Down
26 changes: 25 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ def tearDown(self):
os.unlink(".moban.hashes")

@raises(SystemExit)
@patch("moban.main.handle_moban_file")
@patch("moban.mobanfile.find_default_moban_file")
def test_has_many_files_with_exit_code(
self, fake_find_file, fake_moban_file
):
fake_find_file.return_value = "abc"
fake_moban_file.return_value = 1
from moban.main import main

with patch.object(sys, "argv", ["moban", "--exit-code"]):
main()

@raises(SystemExit)
@patch("moban.main.handle_command_line")
@patch("moban.mobanfile.find_default_moban_file")
def test_handle_single_change_with_exit_code(
self, fake_find_file, fake_command_line
):
fake_find_file.return_value = None
fake_command_line.return_value = 1
from moban.main import main

with patch.object(sys, "argv", ["moban", "--exit-code"]):
main()

@patch("moban.main.handle_moban_file")
@patch("moban.mobanfile.find_default_moban_file")
def test_has_many_files(self, fake_find_file, fake_moban_file):
Expand All @@ -147,7 +172,6 @@ def test_has_many_files(self, fake_find_file, fake_moban_file):
with patch.object(sys, "argv", ["moban"]):
main()

@raises(SystemExit)
@patch("moban.main.handle_command_line")
@patch("moban.mobanfile.find_default_moban_file")
def test_handle_single_change(self, fake_find_file, fake_command_line):
Expand Down

0 comments on commit 39b544d

Please sign in to comment.