From bc468b7710f19ef8f1e098e512569c4abc31b243 Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Mon, 3 Apr 2023 09:12:01 +0200 Subject: [PATCH] commandline: Fix regression in handling default apiurl from oscrc The default apiurl (https://api.opensuse.org) was always used as default regardless the settings in oscrc. --- osc/commandline.py | 11 +++++----- tests/test_commandline.py | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index 0e11b4e9..f0c2b367 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -388,11 +388,9 @@ class OscMainCommand(MainCommand): # try reading the apiurl from the working copy args.apiurl = osc_store.Store(Path.cwd()).apiurl except oscerr.NoWorkingCopy: - # use the default apiurl from conf (if it was configured already) - args.apiurl = conf.config["apiurl"] - - if not args.apiurl: - self.parser.error("Could not determine apiurl, use -A/--apiurl to specify one") + # we can't use conf.config["apiurl"] because it contains the default "https://api.opensuse.org" + # let's leave setting the right value to conf.get_config() + pass conf.get_config( override_apiurl=args.apiurl, @@ -415,6 +413,9 @@ class OscMainCommand(MainCommand): if conf.config["show_download_progress"]: self.download_progress = create_text_meter() + if not args.apiurl: + self.parser.error("Could not determine apiurl, use -A/--apiurl to specify one") + # needed for LegacyOsc class self.args = args diff --git a/tests/test_commandline.py b/tests/test_commandline.py index 88d4e1c7..09c25cc5 100644 --- a/tests/test_commandline.py +++ b/tests/test_commandline.py @@ -6,6 +6,7 @@ import unittest from osc.commandline import Command from osc.commandline import MainCommand +from osc.commandline import OscMainCommand from osc.commandline import pop_project_package_from_args from osc.commandline import pop_project_package_repository_arch_from_args from osc.commandline import pop_project_package_targetproject_targetpackage_from_args @@ -28,7 +29,35 @@ class TestCommand(Command): name = "test-cmd" +OSCRC_LOCALHOST = """ +[general] +apiurl = https://localhost + +[https://localhost] +user=Admin +pass=opensuse +""".lstrip() + + class TestCommandClasses(unittest.TestCase): + def setUp(self): + os.environ.pop("OSC_CONFIG", None) + self.tmpdir = tempfile.mkdtemp(prefix="osc_test") + os.chdir(self.tmpdir) + self.oscrc = None + + def tearDown(self): + os.environ.pop("OSC_CONFIG", None) + try: + shutil.rmtree(self.tmpdir) + except OSError: + pass + + def write_oscrc_localhost(self): + self.oscrc = os.path.join(self.tmpdir, "oscrc") + with open(self.oscrc, "w") as f: + f.write(OSCRC_LOCALHOST) + def test_load_commands(self): main = TestMainCommand() main.load_commands() @@ -97,6 +126,19 @@ class TestCommandClasses(unittest.TestCase): self.assertRaises(SystemExit, main.parse_args, ["test-cmd", "--unknown-option"]) + def test_default_apiurl(self): + class TestMainCommand(OscMainCommand): + name = "osc-test" + + main = TestMainCommand() + main.load_command(TestCommand, "test.osc.commands") + + self.write_oscrc_localhost() + os.environ["OSC_CONFIG"] = self.oscrc + args = main.parse_args(["test-cmd"]) + main.post_parse_args(args) + self.assertEqual(args.apiurl, "https://localhost") + class TestPopProjectPackageFromArgs(unittest.TestCase): def _write_store(self, project=None, package=None):