From 403eea94ced7e6f1a1a9ae36823dee1ebfc396bc Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Mon, 15 Dec 2025 15:08:34 +0100 Subject: [PATCH] Fix running 'osc build --alternative-project=...' when there's no .osc in the current directory --- behave/features/build.feature | 18 ++++++++++++++++++ behave/features/steps/common.py | 6 ++++++ osc/commandline.py | 11 +++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/behave/features/build.feature b/behave/features/build.feature index a00359ad..bd620246 100644 --- a/behave/features/build.feature +++ b/behave/features/build.feature @@ -11,3 +11,21 @@ Background: Scenario: Run `osc build --just-print-buildroot` When I execute osc with args "build --just-print-buildroot" Then the exit code is 0 + + +Scenario: Run `osc build --alternative-project in an empty directory` + Given I set working directory to "{context.osc.temp}" + # we point to a spec that doesn't exist, so the command parses the arguments but fails to run the build + When I execute osc with args "build --alternative-project=test:factory does-not-exist.spec" + Then stdout contains "Building does-not-exist.spec for standard/x86_64" + And the exit code is 2 + + +Scenario: Run `osc build --alternative-project in an empty directory that has .git in the parent tree` + Given I set working directory to "{context.osc.temp}" + And I execute "git init -b main" + And I create directory "{context.osc.temp}/package" + # we point to a spec that doesn't exist, so the command parses the arguments but fails to run the build + When I execute osc with args "build --alternative-project=test:factory does-not-exist.spec" + Then stdout contains "Building does-not-exist.spec for standard/x86_64" + And the exit code is 2 diff --git a/behave/features/steps/common.py b/behave/features/steps/common.py index 4aca9b23..521e1b27 100644 --- a/behave/features/steps/common.py +++ b/behave/features/steps/common.py @@ -205,6 +205,12 @@ def step_impl(context): raise AssertionError(f"Stderr doesn't match:\n{expected}\n\nActual stderr:\n{found}") +@behave.step('I create directory "{path}"') +def step_impl(context, path): + path = path.format(context=context) + os.makedirs(path, exist_ok=True) + + @behave.step('I set working directory to "{path}"') def step_impl(context, path): path = path.format(context=context) diff --git a/osc/commandline.py b/osc/commandline.py index d6cff9a3..9710c469 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -7554,7 +7554,9 @@ Please submit there instead, or use --nodevelproject to force direct submission. self._debug("arg_repository: ", arg_repository) self._debug("arg_descr: ", arg_descr) - store_obj = osc_store.get_store(".") + # we shouldn't make any assumptions if the working copy is valid or not, we simply *try* to read the store and get the metadata from it + # the code that calls parse_repoarchdescr() is responsible for checking the store if necessary + store_obj = osc_store.get_store(".", check=False) repositories = [] # store list of repos for potential offline use @@ -7907,7 +7909,12 @@ Please submit there instead, or use --nodevelproject to force direct submission. except oscerr.NoWorkingCopy: if opts.apiurl and opts.alternative_project: # HACK: ignore invalid working copy and run the build anyway if --alternative-project is specified - store = git_scm.GitStore(Path.cwd(), check=False) + try: + store = git_scm.GitStore(Path.cwd(), check=False) + except oscerr.NoWorkingCopy: + # HACK: if running from an empty directory that has no .git in the parent tree, initialize an empty Store() object + # this allows running the build with --alternative-project even if .osc doesn't exist at all + store = osc_store.Store(Path.cwd(), check=False) else: raise