diff options
Diffstat (limited to 'gitlab/tests/test_cli.py')
-rw-r--r-- | gitlab/tests/test_cli.py | 220 |
1 files changed, 105 insertions, 115 deletions
diff --git a/gitlab/tests/test_cli.py b/gitlab/tests/test_cli.py index 4820103..2246369 100644 --- a/gitlab/tests/test_cli.py +++ b/gitlab/tests/test_cli.py @@ -19,128 +19,118 @@ import argparse import os import tempfile -import unittest import io -try: - from contextlib import redirect_stderr # noqa: H302 -except ImportError: - from contextlib import contextmanager # noqa: H302 - import sys - - @contextmanager - def redirect_stderr(new_target): - old_target, sys.stderr = sys.stderr, new_target - yield - sys.stderr = old_target +from contextlib import redirect_stderr # noqa: H302 +import pytest from gitlab import cli import gitlab.v4.cli -class TestCLI(unittest.TestCase): - def test_what_to_cls(self): - self.assertEqual("Foo", cli.what_to_cls("foo")) - self.assertEqual("FooBar", cli.what_to_cls("foo-bar")) - - def test_cls_to_what(self): - class Class(object): - pass - - class TestClass(object): - pass - - self.assertEqual("test-class", cli.cls_to_what(TestClass)) - self.assertEqual("class", cli.cls_to_what(Class)) - - def test_die(self): - fl = io.StringIO() - with redirect_stderr(fl): - with self.assertRaises(SystemExit) as test: - cli.die("foobar") - self.assertEqual(fl.getvalue(), "foobar\n") - self.assertEqual(test.exception.code, 1) - - def test_parse_value(self): - ret = cli._parse_value("foobar") - self.assertEqual(ret, "foobar") - - ret = cli._parse_value(True) - self.assertEqual(ret, True) - - ret = cli._parse_value(1) - self.assertEqual(ret, 1) - - ret = cli._parse_value(None) - self.assertEqual(ret, None) - - fd, temp_path = tempfile.mkstemp() - os.write(fd, b"content") - os.close(fd) - ret = cli._parse_value("@%s" % temp_path) - self.assertEqual(ret, "content") - os.unlink(temp_path) - - fl = io.StringIO() - with redirect_stderr(fl): - with self.assertRaises(SystemExit) as exc: - cli._parse_value("@/thisfileprobablydoesntexist") - self.assertEqual( - fl.getvalue(), - "[Errno 2] No such file or directory:" - " '/thisfileprobablydoesntexist'\n", - ) - self.assertEqual(exc.exception.code, 1) - - def test_base_parser(self): - parser = cli._get_base_parser() - args = parser.parse_args( - ["-v", "-g", "gl_id", "-c", "foo.cfg", "-c", "bar.cfg"] - ) - self.assertTrue(args.verbose) - self.assertEqual(args.gitlab, "gl_id") - self.assertEqual(args.config_file, ["foo.cfg", "bar.cfg"]) - - -class TestV4CLI(unittest.TestCase): - def test_parse_args(self): - parser = cli._get_parser(gitlab.v4.cli) - args = parser.parse_args(["project", "list"]) - self.assertEqual(args.what, "project") - self.assertEqual(args.whaction, "list") - - def test_parser(self): - parser = cli._get_parser(gitlab.v4.cli) - subparsers = next( - action - for action in parser._actions - if isinstance(action, argparse._SubParsersAction) - ) - self.assertIsNotNone(subparsers) - self.assertIn("project", subparsers.choices) +def test_what_to_cls(): + assert "Foo" == cli.what_to_cls("foo") + assert "FooBar" == cli.what_to_cls("foo-bar") - user_subparsers = next( - action - for action in subparsers.choices["project"]._actions - if isinstance(action, argparse._SubParsersAction) - ) - self.assertIsNotNone(user_subparsers) - self.assertIn("list", user_subparsers.choices) - self.assertIn("get", user_subparsers.choices) - self.assertIn("delete", user_subparsers.choices) - self.assertIn("update", user_subparsers.choices) - self.assertIn("create", user_subparsers.choices) - self.assertIn("archive", user_subparsers.choices) - self.assertIn("unarchive", user_subparsers.choices) - - actions = user_subparsers.choices["create"]._option_string_actions - self.assertFalse(actions["--description"].required) - - user_subparsers = next( - action - for action in subparsers.choices["group"]._actions - if isinstance(action, argparse._SubParsersAction) + +def test_cls_to_what(): + class Class(object): + pass + + class TestClass(object): + pass + + assert "test-class" == cli.cls_to_what(TestClass) + assert "class" == cli.cls_to_what(Class) + + +def test_die(): + fl = io.StringIO() + with redirect_stderr(fl): + with pytest.raises(SystemExit) as test: + cli.die("foobar") + assert fl.getvalue() == "foobar\n" + assert test.value.code == 1 + + +def test_parse_value(): + ret = cli._parse_value("foobar") + assert ret == "foobar" + + ret = cli._parse_value(True) + assert ret is True + + ret = cli._parse_value(1) + assert ret == 1 + + ret = cli._parse_value(None) + assert ret is None + + fd, temp_path = tempfile.mkstemp() + os.write(fd, b"content") + os.close(fd) + ret = cli._parse_value("@%s" % temp_path) + assert ret == "content" + os.unlink(temp_path) + + fl = io.StringIO() + with redirect_stderr(fl): + with pytest.raises(SystemExit) as exc: + cli._parse_value("@/thisfileprobablydoesntexist") + assert ( + fl.getvalue() == "[Errno 2] No such file or directory:" + " '/thisfileprobablydoesntexist'\n" ) - actions = user_subparsers.choices["create"]._option_string_actions - self.assertTrue(actions["--name"].required) + assert exc.value.code == 1 + + +def test_base_parser(): + parser = cli._get_base_parser() + args = parser.parse_args(["-v", "-g", "gl_id", "-c", "foo.cfg", "-c", "bar.cfg"]) + assert args.verbose + assert args.gitlab == "gl_id" + assert args.config_file == ["foo.cfg", "bar.cfg"] + + +def test_v4_parse_args(): + parser = cli._get_parser(gitlab.v4.cli) + args = parser.parse_args(["project", "list"]) + assert args.what == "project" + assert args.whaction == "list" + + +def test_v4_parser(): + parser = cli._get_parser(gitlab.v4.cli) + subparsers = next( + action + for action in parser._actions + if isinstance(action, argparse._SubParsersAction) + ) + assert subparsers is not None + assert "project" in subparsers.choices + + user_subparsers = next( + action + for action in subparsers.choices["project"]._actions + if isinstance(action, argparse._SubParsersAction) + ) + assert user_subparsers is not None + assert "list" in user_subparsers.choices + assert "get" in user_subparsers.choices + assert "delete" in user_subparsers.choices + assert "update" in user_subparsers.choices + assert "create" in user_subparsers.choices + assert "archive" in user_subparsers.choices + assert "unarchive" in user_subparsers.choices + + actions = user_subparsers.choices["create"]._option_string_actions + assert not actions["--description"].required + + user_subparsers = next( + action + for action in subparsers.choices["group"]._actions + if isinstance(action, argparse._SubParsersAction) + ) + actions = user_subparsers.choices["create"]._option_string_actions + assert actions["--name"].required |