From a8bde4a4383d79e118c4f3671b61e4bee3487145 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Sat, 6 Jul 2013 11:38:32 +0100 Subject: [PATCH] Add version option to CLI. --- openphoto/main.py | 12 +++++++++--- tests/unit/test_cli.py | 21 ++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/openphoto/main.py b/openphoto/main.py index 59a41a7..4aacbc0 100644 --- a/openphoto/main.py +++ b/openphoto/main.py @@ -4,7 +4,7 @@ import sys import json from optparse import OptionParser -from openphoto import OpenPhoto +import openphoto CONFIG_ERROR = """ You must create a configuration file with the following contents: @@ -44,6 +44,8 @@ def main(args=sys.argv[1:]): action="store_true", dest="pretty", default=False) parser.add_option('-v', help="Verbose output", action="store_true", dest="verbose", default=False) + parser.add_option('--version', help="Display the current version information", + action="store_true") parser.add_option('--help', help='show this help message', action="store_true") @@ -53,6 +55,10 @@ def main(args=sys.argv[1:]): parser.print_help() return + if options.version: + print(openphoto.__version__) + return + if args: parser.error("Unknown argument: %s" % args) @@ -64,10 +70,10 @@ def main(args=sys.argv[1:]): # Host option overrides config file settings if options.host: - client = OpenPhoto(host=options.host) + client = openphoto.OpenPhoto(host=options.host) else: try: - client = OpenPhoto(config_file=options.config_file) + client = openphoto.OpenPhoto(config_file=options.config_file) except IOError as error: print(error) print(CONFIG_ERROR) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index c33c47b..ea182ee 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -23,7 +23,7 @@ def raise_exception(_): class TestCli(unittest.TestCase): test_file = os.path.join("tests", "unit", "data", "test_file.txt") - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_defaults(self, _, mock_openphoto): """Check that the default behaviour is correct""" @@ -32,14 +32,14 @@ class TestCli(unittest.TestCase): mock_openphoto.assert_called_with(config_file=None) get.assert_called_with("/photos/list.json", process_response=False) - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_config(self, _, mock_openphoto): """Check that a config file can be specified""" main(["--config=test"]) mock_openphoto.assert_called_with(config_file="test") - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_get(self, mock_stdout, mock_openphoto): """Check that the get operation is working""" @@ -52,7 +52,7 @@ class TestCli(unittest.TestCase): process_response=False) self.assertEqual(mock_stdout.getvalue(), "Result\n") - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_post(self, mock_stdout, mock_openphoto): """Check that the post operation is working""" @@ -65,7 +65,7 @@ class TestCli(unittest.TestCase): files={}, process_response=False) self.assertEqual(mock_stdout.getvalue(), "Result\n") - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_post_files(self, _, mock_openphoto): """Check that files are posted correctly""" @@ -104,7 +104,7 @@ class TestCli(unittest.TestCase): mock_stdout.getvalue()) self.assertIn("To get your credentials", mock_stdout.getvalue()) - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_verbose(self, mock_stdout, _): """Check that the verbose option is working""" @@ -112,7 +112,7 @@ class TestCli(unittest.TestCase): self.assertIn("Method: GET", mock_stdout.getvalue()) self.assertIn("Endpoint: /photos/list.json", mock_stdout.getvalue()) - @mock.patch.object(openphoto.main, "OpenPhoto") + @mock.patch.object(openphoto.main.openphoto, "OpenPhoto") @mock.patch('sys.stdout', new_callable=io.StringIO) def test_pretty_print(self, mock_stdout, mock_openphoto): """Check that the pretty-print option is working""" @@ -120,3 +120,10 @@ class TestCli(unittest.TestCase): get.return_value = '{"test":1}' main(["-p"]) self.assertEqual(mock_stdout.getvalue(), '{\n "test":1\n}\n') + + @mock.patch('sys.stdout', new_callable=io.StringIO) + def test_version(self, mock_stdout): + """Check that the version string is correctly printed""" + main(["--version"]) + self.assertEqual(mock_stdout.getvalue(), openphoto.__version__ + "\n") +