mirror of
https://github.com/openSUSE/osc.git
synced 2025-09-07 05:38:43 +02:00
commandline: New class-based commands
This is based on a prototype we've worked on together with Laurin Fäller <laurin.faeller@suse.com>.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from osc.commandline import Command
|
||||
from osc.commandline import MainCommand
|
||||
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
|
||||
@@ -11,6 +14,90 @@ from osc.oscerr import NoWorkingCopy, OscValueError
|
||||
from osc.store import Store
|
||||
|
||||
|
||||
class TestMainCommand(MainCommand):
|
||||
name = "osc-test"
|
||||
|
||||
def init_arguments(self, command=None):
|
||||
self.add_argument(
|
||||
"-A",
|
||||
"--apiurl",
|
||||
)
|
||||
|
||||
|
||||
class TestCommand(Command):
|
||||
name = "test-cmd"
|
||||
|
||||
|
||||
class TestCommandClasses(unittest.TestCase):
|
||||
def test_load_commands(self):
|
||||
main = TestMainCommand()
|
||||
main.load_commands()
|
||||
|
||||
def test_load_command(self):
|
||||
main = TestMainCommand()
|
||||
cmd = main.load_command(TestCommand, "test.osc.commands")
|
||||
self.assertTrue(str(cmd).startswith("<osc plugin test.osc.commands.TestCommand"))
|
||||
|
||||
def test_parent(self):
|
||||
class Parent(TestCommand):
|
||||
name = "parent"
|
||||
|
||||
class Child(TestCommand):
|
||||
name = "child"
|
||||
parent = "Parent"
|
||||
|
||||
main = TestMainCommand()
|
||||
main.load_command(Parent, "test.osc.commands")
|
||||
main.load_command(Child, "test.osc.commands")
|
||||
|
||||
main.parse_args(["parent", "child"])
|
||||
|
||||
def test_invalid_parent(self):
|
||||
class Parent(TestCommand):
|
||||
name = "parent"
|
||||
|
||||
class Child(TestCommand):
|
||||
name = "child"
|
||||
parent = "DoesNotExist"
|
||||
|
||||
main = TestMainCommand()
|
||||
main.load_command(Parent, "test.osc.commands")
|
||||
main.load_command(Child, "test.osc.commands")
|
||||
|
||||
def test_load_twice(self):
|
||||
class AnotherCommand(TestCommand):
|
||||
name = "another-command"
|
||||
aliases = ["test-cmd"]
|
||||
|
||||
main = TestMainCommand()
|
||||
main.load_command(TestCommand, "test.osc.commands")
|
||||
|
||||
# conflict between names
|
||||
self.assertRaises(argparse.ArgumentError, main.load_command, TestCommand, "test.osc.commands")
|
||||
|
||||
# conflict between a name and an alias
|
||||
self.assertRaises(argparse.ArgumentError, main.load_command, AnotherCommand, "test.osc.commands")
|
||||
|
||||
def test_intermixing(self):
|
||||
main = TestMainCommand()
|
||||
main.load_command(TestCommand, "test.osc.commands")
|
||||
|
||||
args = main.parse_args(["test-cmd", "--apiurl", "https://example.com"])
|
||||
self.assertEqual(args.apiurl, "https://example.com")
|
||||
|
||||
args = main.parse_args(["--apiurl", "https://example.com", "test-cmd"])
|
||||
self.assertEqual(args.apiurl, "https://example.com")
|
||||
|
||||
def test_unknown_options(self):
|
||||
main = TestMainCommand()
|
||||
main.load_command(TestCommand, "test.osc.commands")
|
||||
|
||||
args = main.parse_args(["test-cmd", "unknown-arg"])
|
||||
self.assertEqual(args.positional_args, ["unknown-arg"])
|
||||
|
||||
self.assertRaises(SystemExit, main.parse_args, ["test-cmd", "--unknown-option"])
|
||||
|
||||
|
||||
class TestPopProjectPackageFromArgs(unittest.TestCase):
|
||||
def _write_store(self, project=None, package=None):
|
||||
store = Store(self.tmpdir, check=False)
|
||||
|
79
tests/test_doc_plugins.py
Normal file
79
tests/test_doc_plugins.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
These tests make sure that the examples in the documentation
|
||||
about osc plugins are not outdated.
|
||||
"""
|
||||
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
|
||||
from osc.commandline import MainCommand
|
||||
from osc.commandline import OscMainCommand
|
||||
|
||||
|
||||
PLUGINS_DIR = os.path.join(os.path.dirname(__file__), "..", "doc", "plugins")
|
||||
|
||||
|
||||
class TestMainCommand(MainCommand):
|
||||
name = "osc-test"
|
||||
MODULES = (
|
||||
("test.osc.commands", PLUGINS_DIR),
|
||||
)
|
||||
|
||||
|
||||
class TestPopProjectPackageFromArgs(unittest.TestCase):
|
||||
def test_load_commands(self):
|
||||
"""
|
||||
Test if all plugins from the tutorial can be properly loaded
|
||||
"""
|
||||
main = TestMainCommand()
|
||||
main.load_commands()
|
||||
|
||||
def test_simple(self):
|
||||
"""
|
||||
Test the 'simple' command
|
||||
"""
|
||||
main = TestMainCommand()
|
||||
main.load_commands()
|
||||
args = main.parse_args(["simple", "arg1", "arg2"])
|
||||
self.assertEqual(args.command, "simple")
|
||||
self.assertEqual(args.bool_option, False)
|
||||
self.assertEqual(args.arguments, ["arg1", "arg2"])
|
||||
|
||||
def test_request_list(self):
|
||||
"""
|
||||
Test the 'request list' command
|
||||
"""
|
||||
main = TestMainCommand()
|
||||
main.load_commands()
|
||||
args = main.parse_args(["request", "list"])
|
||||
self.assertEqual(args.command, "list")
|
||||
self.assertEqual(args.message, None)
|
||||
|
||||
def test_request_accept(self):
|
||||
"""
|
||||
Test the 'request accept' command
|
||||
"""
|
||||
main = TestMainCommand()
|
||||
main.load_commands()
|
||||
args = main.parse_args(["request", "accept", "-m", "a message", "12345"])
|
||||
self.assertEqual(args.command, "accept")
|
||||
self.assertEqual(args.message, "a message")
|
||||
self.assertEqual(args.id, 12345)
|
||||
|
||||
def test_plugin_locations(self):
|
||||
osc_paths = [i[1] for i in OscMainCommand.MODULES]
|
||||
# skip the first line with osc.commands
|
||||
osc_paths = osc_paths[1:]
|
||||
|
||||
path = os.path.join(PLUGINS_DIR, "plugin_locations.rst")
|
||||
with open(path, "r") as f:
|
||||
# s
|
||||
doc_paths = f.readlines()
|
||||
# skip the first line with osc.commands
|
||||
doc_paths = doc_paths[1:]
|
||||
doc_paths = [i.lstrip(" -") for i in doc_paths]
|
||||
doc_paths = [i.rstrip("\n") for i in doc_paths]
|
||||
|
||||
self.assertEqual(doc_paths, osc_paths)
|
Reference in New Issue
Block a user