From 58bddb294a392ed702627691688a2b4df043af5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Trimaille?= Date: Mon, 22 Jan 2024 10:41:13 +0100 Subject: [PATCH] Fix use of the changelog CLI without configuration file (#279) --- qgispluginci/cli.py | 15 +++++++++++---- qgispluginci/parameters.py | 19 +++++++++++++------ test/test_parameters.py | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 test/test_parameters.py diff --git a/qgispluginci/cli.py b/qgispluginci/cli.py index d27bd19a..802da9c0 100755 --- a/qgispluginci/cli.py +++ b/qgispluginci/cli.py @@ -84,8 +84,10 @@ def cli(): ) changelog_parser.add_argument( "release_version", - help="The version to be released. If nothing is speficied, \ - the latest version specified into the changelog is used.", + help=( + "The version to be released. If nothing is specified, the latest version specified into the changelog is " + "used." + ), default="latest", ) @@ -182,11 +184,12 @@ def cli(): exit_val = 0 - # Initialize Parameters - parameters = Parameters.make_from(args=args) # CHANGELOG if args.command == "changelog": try: + # Initialize Parameters + # Configuration file is optional at this stage + parameters = Parameters.make_from(args=args, optional_configuration=True) c = ChangelogParser( changelog_path=parameters.changelog_path, ) @@ -198,6 +201,10 @@ def cli(): return exit_val + # Initialize Parameters + # Configuration file is now required + parameters = Parameters.make_from(args=args) + # PACKAGE if args.command == "package": release( diff --git a/qgispluginci/parameters.py b/qgispluginci/parameters.py index 2a558434..3e814749 100755 --- a/qgispluginci/parameters.py +++ b/qgispluginci/parameters.py @@ -112,7 +112,11 @@ class Parameters: @classmethod def make_from( - cls, *, args: Optional[Any] = None, path_to_config_file: Optional[Path] = None + cls, + *, + args: Optional[Any] = None, + path_to_config_file: Optional[Path] = None, + optional_configuration: bool = False, ) -> "Parameters": """ Instantiate from a config file or by exploring the filesystem @@ -157,6 +161,9 @@ def explore_config() -> Dict[str, Any]: pass raise configuration_not_found + if optional_configuration and not path_to_config_file: + return cls({}) + if path_to_config_file: file_name = path_to_config_file.name @@ -172,6 +179,11 @@ def explore_config() -> Dict[str, Any]: def __init__(self, definition: Dict[str, Any]): self.plugin_path = definition.get("plugin_path") + self.changelog_path = definition.get("changelog_path", "CHANGELOG.md") + + if not self.plugin_path: + # This tool can be used outside of a QGIS plugin to read a changelog file + return get_metadata = self.collect_metadata() self.plugin_name = get_metadata("name") @@ -230,13 +242,8 @@ def __init__(self, definition: Dict[str, Any]): self.changelog_number_of_entries = definition.get( "changelog_number_of_entries", 3 ) - self.changelog_path = definition.get("changelog_path", "CHANGELOG.md") # read from metadata - if not self.plugin_path: - # This tool can be used outside of a QGIS plugin to read a changelog file - return - self.author = get_metadata("author", "") self.description = get_metadata("description") self.qgis_minimum_version = get_metadata("qgisMinimumVersion") diff --git a/test/test_parameters.py b/test/test_parameters.py new file mode 100644 index 00000000..63cf33a4 --- /dev/null +++ b/test/test_parameters.py @@ -0,0 +1,16 @@ +#! /usr/bin/env python + +# standard +import unittest + +# Project +from qgispluginci.parameters import Parameters + + +class TestParameters(unittest.TestCase): + def test_changelog_parameters(self): + """Test parameters for changelog command.""" + # For the changelog command, the configuration file is optional. + parameters = Parameters.make_from(args={}, optional_configuration=True) + self.assertEqual("CHANGELOG.md", parameters.changelog_path) + self.assertIsNone(parameters.plugin_path)