From 309ee60065a368a0b06010c26b98f9f3dfbe7efb Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 8 Nov 2019 19:50:32 +0800 Subject: [PATCH 1/6] refactor(command): make version a command instead of an argument --- commitizen/cli.py | 18 +++++++----------- commitizen/commands/__init__.py | 3 ++- commitizen/commands/version.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 commitizen/commands/version.py diff --git a/commitizen/cli.py b/commitizen/cli.py index 3cf75ce684..41f75f1348 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -5,8 +5,8 @@ from decli import cli -from commitizen import commands, config, out -from commitizen.__version__ import __version__ +from commitizen import commands, config + logger = logging.getLogger(__name__) data = { @@ -20,11 +20,6 @@ "arguments": [ {"name": "--debug", "action": "store_true", "help": "use debug mode"}, {"name": ["-n", "--name"], "help": "use the given commitizen"}, - { - "name": ["--version"], - "action": "store_true", - "help": "get the version of the installed commitizen", - }, ], "subcommands": { "title": "commands", @@ -99,6 +94,11 @@ }, ], }, + { + "name": ["version"], + "help": "get the version of the installed commitizen", + "func": commands.Version, + }, ], }, } @@ -125,8 +125,4 @@ def main(): ) logging.getLogger("commitizen").setLevel(logging.DEBUG) - if args.version: - out.line(__version__) - raise SystemExit() - args.func(conf, vars(args))() diff --git a/commitizen/commands/__init__.py b/commitizen/commands/__init__.py index 3343a53edc..644356a759 100644 --- a/commitizen/commands/__init__.py +++ b/commitizen/commands/__init__.py @@ -4,5 +4,6 @@ from .info import Info from .list_cz import ListCz from .schema import Schema +from .version import Version -__all__ = ("Bump", "Commit", "Example", "Info", "ListCz", "Schema") +__all__ = ("Bump", "Commit", "Example", "Info", "ListCz", "Schema", "Version") diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py new file mode 100644 index 0000000000..3b4b923d67 --- /dev/null +++ b/commitizen/commands/version.py @@ -0,0 +1,12 @@ +from commitizen import out +from commitizen.__version__ import __version__ + + +class Version: + """Get the version of the installed commitizen.""" + + def __init__(self, config: dict, *args): + self.config: dict = config + + def __call__(self): + out.line(__version__) From b94bc6b7201f5130d08a9d3b64f9fa540c442029 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 8 Nov 2019 19:52:45 +0800 Subject: [PATCH 2/6] fix(cli): enforce subcommand as required without enforcing subcommand, exception would be raised when running `cz -n test` or `cz --debug` --- commitizen/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/commitizen/cli.py b/commitizen/cli.py index 41f75f1348..26044351ac 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -23,6 +23,7 @@ ], "subcommands": { "title": "commands", + "required": True, "commands": [ { "name": "ls", From d5dc6179de95f696bf0273062fba37d682b4d6df Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 8 Nov 2019 21:46:46 +0800 Subject: [PATCH 3/6] fix(cli): handle the exception that command is not given --- commitizen/cli.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 26044351ac..740595d202 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -5,7 +5,7 @@ from decli import cli -from commitizen import commands, config +from commitizen import commands, config, out logger = logging.getLogger(__name__) @@ -114,7 +114,11 @@ def main(): parser.print_help(sys.stderr) raise SystemExit() - args = parser.parse_args() + try: + args = parser.parse_args() + except TypeError: + out.error("Command is required") + raise SystemExit() if args.name: conf.update({"name": args.name}) From 266e958d6032ea19c9b04d08d85c61b0a7eed35a Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 8 Nov 2019 21:49:43 +0800 Subject: [PATCH 4/6] refactor(command/version): use out.write instead of out.line --- commitizen/commands/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index 3b4b923d67..b5158ef641 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -9,4 +9,4 @@ def __init__(self, config: dict, *args): self.config: dict = config def __call__(self): - out.line(__version__) + out.write(__version__) From 77fe6fa2cc97a0544196536b1e67431798b39bee Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Fri, 8 Nov 2019 21:50:54 +0800 Subject: [PATCH 5/6] test(command/version): add unit test for version --- tests/test_commands.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 3dda7202f5..5ef2ad7466 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -95,3 +95,10 @@ def test_list_cz(): commands.ListCz(config)() mocked_write.assert_called_once() + + +def test_version(): + with mock.patch("commitizen.out.write") as mocked_write: + + commands.Version(config)() + mocked_write.assert_called_once() From 8ae5d8b424301f04d89b1202e76a94404011eb96 Mon Sep 17 00:00:00 2001 From: LeeW Date: Tue, 12 Nov 2019 20:30:56 -0800 Subject: [PATCH 6/6] refactor(cli): add back --version and remove subcommand required constraint these changes will apply when move into 2.0 --- commitizen/cli.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 740595d202..620707c588 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -20,10 +20,16 @@ "arguments": [ {"name": "--debug", "action": "store_true", "help": "use debug mode"}, {"name": ["-n", "--name"], "help": "use the given commitizen"}, + { + "name": ["--version"], + "action": "store_true", + "help": "get the version of the installed commitizen", + }, ], "subcommands": { "title": "commands", - "required": True, + # TODO: Add this constraint back in 2.0 + # "required": True, "commands": [ { "name": "ls", @@ -123,6 +129,13 @@ def main(): if args.name: conf.update({"name": args.name}) + if args.version: + warnings.warn( + "'cz --version' will be deprecated in next major version. " + "Please use 'cz version' command from your scripts" + ) + logging.getLogger("commitizen").setLevel(logging.DEBUG) + if args.debug: warnings.warn( "Debug will be deprecated in next major version. " @@ -130,4 +143,8 @@ def main(): ) logging.getLogger("commitizen").setLevel(logging.DEBUG) - args.func(conf, vars(args))() + # TODO: This try block can be removed after command is required in 2.0 + try: + args.func(conf, vars(args))() + except AttributeError: + out.error("Command is required")