diff --git a/moban/plugins.py b/moban/plugins.py index df47c409..67dbd058 100644 --- a/moban/plugins.py +++ b/moban/plugins.py @@ -197,7 +197,7 @@ def get_data(self, file_name): # If data file doesn't exist: # 1. Alert the user of their (potential) mistake # 2. Attempt to use environment vars as data - reporter.report_error_message(str(exception)) + reporter.report_info_message(str(exception)) reporter.report_using_env_vars() data = os.environ return data diff --git a/moban/reporter.py b/moban/reporter.py index fac242de..bbfbab1e 100644 --- a/moban/reporter.py +++ b/moban/reporter.py @@ -49,6 +49,14 @@ def report_error_message(message): print(crayons.white("Error: ", bold=True) + crayons.red(message)) +def report_warning_message(message): + print(crayons.white("Warning: ", bold=True) + crayons.yellow(message)) + + +def report_info_message(message): + print(crayons.white("Info: ") + crayons.green(message)) + + def report_up_to_date(): print(crayons.green(MESSAGE_UP_TO_DATE, bold=True)) @@ -92,12 +100,12 @@ def report_git_clone(repo): def report_using_env_vars(): - print(crayons.yellow(MESSAGE_USING_ENV_VARS, bold=True)) + report_warning_message(MESSAGE_USING_ENV_VARS) def report_template_not_in_moban_file(template): message = MESSAGE_TEMPLATE_NOT_IN_MOBAN_FILE.format(template) - print(crayons.yellow(message, bold=True)) + report_warning_message(message) def _format_single(message, count): diff --git a/tests/integration_tests/test_command_line_options.py b/tests/integration_tests/test_command_line_options.py index 2d9647c3..a4c667c9 100644 --- a/tests/integration_tests/test_command_line_options.py +++ b/tests/integration_tests/test_command_line_options.py @@ -143,12 +143,7 @@ def test_single_command_with_a_few_options(self, fake_template_doer): main() call_args = list(fake_template_doer.call_args[0][0]) - eq_( - call_args, - [ - ("abc.jj2", "data.yaml", "xyz.output"), - ], - ) + eq_(call_args, [("abc.jj2", "data.yaml", "xyz.output")]) @patch("moban.plugins.BaseEngine.render_to_files") def test_single_command_with_options(self, fake_template_doer): @@ -166,12 +161,7 @@ def test_single_command_with_options(self, fake_template_doer): main() call_args = list(fake_template_doer.call_args[0][0]) - eq_( - call_args, - [ - ("abc.jj2", "new.yml", "xyz.output"), - ], - ) + eq_(call_args, [("abc.jj2", "new.yml", "xyz.output")]) @raises(Exception) def test_single_command_without_output_option(self, fake_template_doer): diff --git a/tests/test_main.py b/tests/test_main.py index 3ae6b72b..308e8b64 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -108,6 +108,20 @@ def test_no_third_party_engine( with patch.object(sys, "argv", ["moban"]): main() + @raises(SystemExit) + @patch("os.path.exists") + @patch("moban.main.handle_moban_file") + @patch("moban.reporter.report_error_message") + def test_double_underscore_main( + self, fake_reporter, fake_moban_file, fake_file + ): + fake_file.return_value = True + fake_moban_file.side_effect = exceptions.DirectoryNotFound + from moban.__main__ import main + + with patch.object(sys, "argv", ["moban"]): + main() + class TestExitCodes: def setUp(self): diff --git a/tests/test_reporter.py b/tests/test_reporter.py index b5757362..5e60daeb 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -36,6 +36,22 @@ def test_error_message(): eq_(fake_stdout.getvalue(), "Error: something wrong\n") +def test_info_message(): + patcher = patch("sys.stdout", new_callable=StringIO) + fake_stdout = patcher.start() + reporter.report_info_message("for your information") + patcher.stop() + eq_(fake_stdout.getvalue(), "Info: for your information\n") + + +def test_warning_message(): + patcher = patch("sys.stdout", new_callable=StringIO) + fake_stdout = patcher.start() + reporter.report_warning_message("Maybe you wanna know") + patcher.stop() + eq_(fake_stdout.getvalue(), "Warning: Maybe you wanna know\n") + + def test_report_templating(): patcher = patch("sys.stdout", new_callable=StringIO) fake_stdout = patcher.start() @@ -56,3 +72,14 @@ def test_format_single(): message = "1 files" ret = reporter._format_single(message, 1) eq_(ret, "1 file") + + +def test_report_template_not_in_moban_file(): + patcher = patch("sys.stdout", new_callable=StringIO) + fake_stdout = patcher.start() + reporter.report_template_not_in_moban_file("test.jj2") + patcher.stop() + eq_( + fake_stdout.getvalue(), + "Warning: test.jj2 is not defined in your moban file!\n", + ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 13176cc7..cd3a4bf8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -9,6 +9,7 @@ from moban.utils import ( mkdir_p, + open_json, get_repo_name, write_file_out, file_permissions, @@ -169,3 +170,9 @@ def test_get_repo_name(): actual = [get_repo_name(repo) for repo in repos] expected = ["repo", "repo"] eq_(expected, actual) + + +def test_open_json(): + content = open_json(os.path.join("tests", "fixtures"), "child.json") + expected = {"key": "hello world", "pass": "ox"} + eq_(expected, content)