1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-12-25 17:36:13 +01:00

Improve argument handling in the 'develproject' command

This commit is contained in:
Daniel Mach 2022-11-07 10:36:03 +01:00
parent c6b30e500f
commit 6dcc45f272
10 changed files with 274 additions and 42 deletions

View File

@ -0,0 +1,16 @@
Feature: `osc develproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
And I execute osc with args "checkout test:factory test-pkgA"
And I set working directory to "{context.osc.temp}/test:factory/test-pkgA"
Scenario: Run `osc develproject`
When I execute osc with args "develproject"
Then stdout is
"""
test:devel/test-pkgA
"""

View File

@ -0,0 +1,17 @@
Feature: `osc develproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
And I execute osc with args "checkout test:factory"
And I set working directory to "{context.osc.temp}/test:factory"
Scenario: Run `osc develproject`
When I execute osc with args "develproject"
Then the exit code is 1
And stderr is
"""
Directory '{context.osc.temp}/test:factory' is not a working copy of a package
"""

View File

@ -0,0 +1,32 @@
Feature: `osc develproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
Scenario: Run `osc develproject`
When I execute osc with args "develproject test:factory test-pkgA"
Then stdout is
"""
test:devel/test-pkgA
"""
Scenario: Run `osc develproject`
When I execute osc with args "develproject test:factory test-pkgB"
Then the exit code is 1
And stderr is
"""
Package test:factory/test-pkgB has no devel project
"""
Scenario: Run `osc develproject`
When I execute osc with args "develproject test:factory/test-pkgB"
Then the exit code is 1
And stderr is
"""
Package test:factory/test-pkgB has no devel project
"""

View File

@ -0,0 +1,15 @@
Feature: `osc develproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
Scenario: Run `osc develproject`
When I execute osc with args "develproject test:factory"
Then the exit code is 1
And stderr is
"""
*** Error: Please specify a package
"""

View File

@ -0,0 +1,57 @@
Feature: `osc setdevelproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
And I execute osc with args "checkout test:factory test-pkgA"
And I set working directory to "{context.osc.temp}/test:factory/test-pkgA"
@destructive
Scenario: Run `osc setdevelproject <devel_project>`
When I execute osc with args "setdevelproject test:devel"
Then the exit code is 0
And stdout is
"""
Setting devel project of package 'test:factory/test-pkgA' to package 'test:devel/test-pkgA'
Sending meta data...
Done.
"""
@destructive
Scenario: Run `osc setdevelproject <devel_project> <devel_package>`
When I execute osc with args "setdevelproject test:devel test-pkgA"
Then the exit code is 0
And stdout is
"""
Setting devel project of package 'test:factory/test-pkgA' to package 'test:devel/test-pkgA'
Sending meta data...
Done.
"""
@destructive
Scenario: Run `osc setdevelproject <devel_project>/<devel_package>`
When I execute osc with args "setdevelproject test:devel/test-pkgA"
Then the exit code is 0
And stdout is
"""
Setting devel project of package 'test:factory/test-pkgA' to package 'test:devel/test-pkgA'
Sending meta data...
Done.
"""
@destructive
Scenario: Run `osc setdevelproject --unset`
Given I execute osc with args "setdevelproject test:devel"
When I execute osc with args "setdevelproject --unset"
Then the exit code is 0
And stdout is
"""
Unsetting devel project from package 'test:factory/test-pkgA'
Sending meta data...
Done.
"""

View File

@ -0,0 +1,17 @@
Feature: `osc setdevelproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
And I execute osc with args "checkout test:factory"
And I set working directory to "{context.osc.temp}/test:factory"
Scenario: Run `osc setdevelproject <devel_project>`
When I execute osc with args "setdevelproject devel"
Then the exit code is 1
And stderr is
"""
Directory '{context.osc.temp}/test:factory' is not a working copy of a package
"""

View File

@ -0,0 +1,54 @@
Feature: `osc setdevelproject` command
# common steps for all scenarios
Background:
Given I set working directory to "{context.osc.temp}"
@destructive
Scenario: Run `osc setdevelproject <project> <package> <devel_project>`
When I execute osc with args "setdevelproject test:factory test-pkgA test:devel"
Then the exit code is 0
And stdout is
"""
Setting devel project of package 'test:factory/test-pkgA' to package 'test:devel/test-pkgA'
Sending meta data...
Done.
"""
@destructive
Scenario: Run `osc setdevelproject <project> <package> <devel_project> <devel_package>`
When I execute osc with args "setdevelproject test:factory test-pkgB test:devel test-pkgA"
Then the exit code is 0
And stdout is
"""
Setting devel project of package 'test:factory/test-pkgB' to package 'test:devel/test-pkgA'
Sending meta data...
Done.
"""
@destructive
Scenario: Run `osc setdevelproject <project>/<package> <devel_project>/<devel_package>`
When I execute osc with args "setdevelproject test:factory/test-pkgB test:devel/test-pkgA"
Then the exit code is 0
And stdout is
"""
Setting devel project of package 'test:factory/test-pkgB' to package 'test:devel/test-pkgA'
Sending meta data...
Done.
"""
@destructive
Scenario: Run `osc setdevelproject <project> <package> --unset`
When I execute osc with args "setdevelproject test:factory test-pkgA --unset"
Then the exit code is 0
And stdout is
"""
Unsetting devel project from package 'test:factory/test-pkgA'
Sending meta data...
Done.
"""

View File

@ -69,12 +69,14 @@ def pop_project_package_from_args(args, default_project=None, default_package=No
assert isinstance(args, list)
path = Path.cwd()
used_default_project = False
try:
project = args.pop(0)
except IndexError:
if not default_project:
raise oscerr.OscValueError("Please specify a project")
project = default_project
used_default_project = True
if not isinstance(project, str):
raise TypeError(f"Project should be 'str', found: {type(project).__name__}")
@ -99,6 +101,10 @@ def pop_project_package_from_args(args, default_project=None, default_package=No
try:
package = args.pop(0)
except IndexError:
if not package_is_optional and not used_default_project:
# package is not optional and it wasn't specified together with the project
raise oscerr.OscValueError("Please specify a package")
if default_package:
package = default_package
else:
@ -748,28 +754,22 @@ class Osc(cmdln.Cmdln):
Print the devel project / package of a package
Examples:
osc develproject PRJ PKG
osc develproject
osc develproject [PROJECT PACKAGE]
"""
args = slash_split(args)
apiurl = self.get_api_url()
if len(args) == 0:
project = store_read_project(Path.cwd())
package = store_read_package(Path.cwd())
elif len(args) == 2:
project = self._process_project_name(args[0])
package = args[1]
else:
raise oscerr.WrongArgs('need Project and Package')
args = list(args)
project, package = pop_project_package_from_args(
args, default_project=".", default_package=".", package_is_optional=False
)
devprj, devpkg = show_devel_project(apiurl, project, package)
if devprj is None:
print('%s / %s has no devel project' % (project, package))
elif devpkg and devpkg != package:
print("%s %s" % (devprj, devpkg))
else:
print(devprj)
devel_project, devel_package = show_devel_project(apiurl, project, package)
if not devel_project:
print(f"Package {project}/{package} has no devel project", file=sys.stderr)
sys.exit(1)
print(f"{devel_project}/{devel_package}")
@cmdln.alias('ca')
def do_cleanassets(self, subcmd, opts, *args):
@ -794,29 +794,41 @@ class Osc(cmdln.Cmdln):
"""Set the devel project / package of a package
Examples:
osc setdevelproject [PRJ PKG] DEVPRJ [DEVPKG]
osc setdevelproject [PROJECT PACKAGE] DEVEL_PROJECT [DEVEL_PACKAGE]
"""
args = slash_split(args)
apiurl = self.get_api_url()
devprj, devpkg = None, None
if len(args) == 3 or len(args) == 4:
project, package = self._process_project_name(args[0]), args[1]
devprj = self._process_project_name(args[2])
if len(args) == 4:
devpkg = args[3]
elif len(args) >= 1 and len(args) <= 2:
project, package = store_read_project(Path.cwd()), store_read_package(Path.cwd())
devprj = self._process_project_name(args[0])
if len(args) == 2:
devpkg = args[1]
args = list(args)
if opts.unset:
project, package = pop_project_package_from_args(
args, default_project=".", default_package=".", package_is_optional=False
)
devel_project = None
devel_package = None
else:
if opts.unset:
project, package = store_read_project(Path.cwd()), store_read_package(Path.cwd())
else:
raise oscerr.WrongArgs('need at least DEVPRJ (and possibly DEVPKG)')
args_backup = args.copy()
set_devel_project(apiurl, project, package, devprj, devpkg)
try:
# try this sequence first: project package devel_project [devel_package]
project, package = pop_project_package_from_args(args, package_is_optional=False)
devel_project, devel_package = pop_project_package_from_args(
args, default_package=package, package_is_optional=True
)
except oscerr.OscValueError:
# then read project and package from working copy and try devel_project [devel_package]
args = args_backup.copy()
project, package = pop_project_package_from_args(
[], default_project=".", default_package=".", package_is_optional=False
)
devel_project, devel_package = pop_project_package_from_args(
args, default_package=package, package_is_optional=True
)
if args:
args_str = ", ".join(args)
self.argparse_error(f"Unknown arguments: {args_str}")
set_devel_project(apiurl, project, package, devel_project, devel_package, print_to="stdout")
def do_showlinked(self, subcmd, opts, *args):
"""

View File

@ -43,6 +43,7 @@ except ImportError:
distro = None
from . import __version__
from . import _private
from . import conf
from . import meter
from . import oscerr
@ -3688,7 +3689,21 @@ def show_devel_project(apiurl, prj, pac):
return node.get('project'), node.get('package', None)
def set_devel_project(apiurl, prj, pac, devprj=None, devpac=None):
def set_devel_project(apiurl, prj, pac, devprj=None, devpac=None, print_to="debug"):
if devprj:
msg = "Setting devel project of"
else:
msg = "Unsetting devel project from"
msg = _private.format_msg_project_package_options(
msg,
prj,
pac,
devprj,
devpac,
)
_private.print_msg(msg, print_to=print_to)
meta = show_package_meta(apiurl, prj, pac)
root = ET.fromstring(b''.join(meta))
node = root.find('devel')

View File

@ -4,7 +4,7 @@ import tempfile
import unittest
from osc.commandline import pop_project_package_from_args
from osc.oscerr import NoWorkingCopy
from osc.oscerr import NoWorkingCopy, OscValueError
from osc.store import Store
@ -38,10 +38,7 @@ class TestPopProjectPackageFromArgs(unittest.TestCase):
def test_defaults(self):
args = ["project"]
project, package = pop_project_package_from_args(args, default_package="default-package")
self.assertEqual(project, "project")
self.assertEqual(package, "default-package")
self.assertEqual(args, [])
self.assertRaises(OscValueError, pop_project_package_from_args, args, default_package="default-package")
args = ["project"]
project, package = pop_project_package_from_args(