1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 22:56:15 +01:00

Add commandline.pop_project_package_from_args()

This commit is contained in:
Daniel Mach 2022-11-01 10:04:58 +01:00
parent ea17c6c884
commit 76b1821f19
2 changed files with 222 additions and 0 deletions

View File

@ -48,6 +48,81 @@ def get_parser():
return osc.argparser
def pop_project_package_from_args(args, default_project=None, default_package=None, package_is_optional=False):
"""
Get project and package from given `args`.
:param args: List of command-line arguments.
WARNING: `args` gets modified in this function call!
:type args: list(str)
:param default_project: Used if project cannot be retrieved from `args`.
Resolved from the current working copy if set to '.'.
:type default_project: str
:param default_package: Used if package cannot be retrieved from `args`.
Resolved from the current working copy if set to '.'.
:type default_package: str
:param package_is_optional: Whether to error out when package name cannot be retrieved.
:type package_is_optional: bool
:returns: Project name and package name.
:rtype: tuple(str)
"""
assert isinstance(args, list)
path = Path.cwd()
try:
project = args.pop(0)
except IndexError:
if not default_project:
raise oscerr.OscValueError("Please specify a project")
project = default_project
if not isinstance(project, str):
raise TypeError(f"Project should be 'str', found: {type(project).__name__}")
package = None
if project == "/":
# no project name (to support listing all projects via `osc ls /`)
project = None
elif project and "/" in project:
# project/package
if project.count("/") != 1:
raise oscerr.OscValueError(f"Argument doesn't match the '<project>/<package>' pattern: {project}")
project, package = project.split("/")
if project == ".":
# project name taken from the working copy
store = osc_store.Store(path)
project = store.project
if package is None:
try:
package = args.pop(0)
except IndexError:
if default_package:
package = default_package
else:
if package_is_optional:
return project, None
raise oscerr.OscValueError("Please specify a package")
if not isinstance(package, str):
raise TypeError(f"Package should be 'str', found: {type(package).__name__}")
if package == ".":
# package name taken from the working copy
try:
store = osc_store.Store(path)
store.assert_is_package()
package = store.package
except oscerr.NoWorkingCopy:
if not package_is_optional:
raise
package = None
return project, package
class Osc(cmdln.Cmdln):
"""
openSUSE commander is a command-line interface to the Open Build Service.

147
tests/test_commandline.py Normal file
View File

@ -0,0 +1,147 @@
import os
import shutil
import tempfile
import unittest
from osc.commandline import pop_project_package_from_args
from osc.oscerr import NoWorkingCopy
from osc.store import Store
class TestPopProjectPackageFromArgs(unittest.TestCase):
def _write_store(self, project=None, package=None):
store = Store(self.tmpdir, check=False)
if project:
store.project = project
store.is_project = True
if package:
store.package = package
store.is_project = False
store.is_package = True
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="osc_test")
os.chdir(self.tmpdir)
def tearDown(self):
try:
shutil.rmtree(self.tmpdir)
except OSError:
pass
def test_explicit_project_and_package(self):
args = ["project", "package", "another-arg"]
project, package = pop_project_package_from_args(args)
self.assertEqual(project, "project")
self.assertEqual(package, "package")
self.assertEqual(args, ["another-arg"])
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, [])
args = ["project"]
project, package = pop_project_package_from_args(
args, default_package="default-package", package_is_optional=True
)
self.assertEqual(project, "project")
self.assertEqual(package, "default-package")
self.assertEqual(args, [])
args = []
project, package = pop_project_package_from_args(
args, default_project="default-project", default_package="default-package"
)
self.assertEqual(project, "default-project")
self.assertEqual(package, "default-package")
self.assertEqual(args, [])
args = []
project, package = pop_project_package_from_args(
args, default_project="default-project", default_package="default-package", package_is_optional=True
)
self.assertEqual(project, "default-project")
self.assertEqual(package, "default-package")
self.assertEqual(args, [])
def test_slash_separator(self):
args = ["project/package", "another-arg"]
project, package = pop_project_package_from_args(args)
self.assertEqual(project, "project")
self.assertEqual(package, "package")
self.assertEqual(args, ["another-arg"])
args = ["project/", "another-arg"]
project, package = pop_project_package_from_args(args)
self.assertEqual(project, "project")
self.assertEqual(package, "")
self.assertEqual(args, ["another-arg"])
def test_no_working_copy(self):
args = [".", "."]
self.assertRaises(NoWorkingCopy, pop_project_package_from_args, args)
args = [".", "package"]
self.assertRaises(NoWorkingCopy, pop_project_package_from_args, args)
args = ["project", "."]
self.assertRaises(NoWorkingCopy, pop_project_package_from_args, args)
def test_project_and_package_from_project_working_copy(self):
self._write_store("store_project")
args = [".", ".", "another-arg"]
self.assertRaises(NoWorkingCopy, pop_project_package_from_args, args)
args = ["."]
project, package = pop_project_package_from_args(args, package_is_optional=True)
self.assertEqual(project, "store_project")
self.assertEqual(package, None)
self.assertEqual(args, [])
args = []
self.assertRaises(NoWorkingCopy, pop_project_package_from_args, args, default_project=".", default_package=".")
args = []
project, package = pop_project_package_from_args(
args, default_project=".", default_package=".", package_is_optional=True
)
self.assertEqual(project, "store_project")
self.assertEqual(package, None)
self.assertEqual(args, [])
def test_project_and_package_from_package_working_copy(self):
self._write_store("store_project", "store_package")
args = [".", ".", "another-arg"]
project, package = pop_project_package_from_args(args)
self.assertEqual(project, "store_project")
self.assertEqual(package, "store_package")
self.assertEqual(args, ["another-arg"])
args = ["."]
project, package = pop_project_package_from_args(args, package_is_optional=True)
self.assertEqual(project, "store_project")
self.assertEqual(package, None)
self.assertEqual(args, [])
args = []
project, package = pop_project_package_from_args(args, default_project=".", default_package=".")
self.assertEqual(project, "store_project")
self.assertEqual(package, "store_package")
self.assertEqual(args, [])
args = []
project, package = pop_project_package_from_args(
args, default_project=".", default_package=".", package_is_optional=True
)
self.assertEqual(project, "store_project")
self.assertEqual(package, "store_package")
self.assertEqual(args, [])
if __name__ == "__main__":
unittest.main()