diff options
-rw-r--r-- | tests/unit/conftest.py | 10 | ||||
-rw-r--r-- | tests/unit/helpers.py | 9 | ||||
-rw-r--r-- | tests/unit/test_cli.py | 23 |
3 files changed, 42 insertions, 0 deletions
diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index be38b6d..d726371 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -22,6 +22,16 @@ def fake_object(fake_manager): @pytest.fixture +def fake_object_no_id(fake_manager): + return helpers.FakeObjectWithoutId(fake_manager, {}) + + +@pytest.fixture +def fake_object_long_repr(fake_manager): + return helpers.FakeObjectWithLongRepr(fake_manager, {"test": "a" * 100}) + + +@pytest.fixture def fake_object_with_parent(fake_manager_with_parent): return helpers.FakeObject( fake_manager_with_parent, {"attr1": "foo", "alist": [1, 2, 3]} diff --git a/tests/unit/helpers.py b/tests/unit/helpers.py index bcf967e..1093728 100644 --- a/tests/unit/helpers.py +++ b/tests/unit/helpers.py @@ -15,6 +15,15 @@ class FakeObject(base.RESTObject): pass +class FakeObjectWithoutId(base.RESTObject): + _id_attr = None + + +class FakeObjectWithLongRepr(base.RESTObject): + _id_attr = None + _repr_attr = "test" + + class OtherFakeObject(FakeObject): _id_attr = "foo" diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 9440e96..bd72317 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -1,14 +1,17 @@ import argparse import io import os +import sys import tempfile from contextlib import redirect_stderr # noqa: H302 +from unittest import mock import pytest import gitlab.base from gitlab import cli from gitlab.exceptions import GitlabError +from gitlab.v4 import cli as v4_cli @pytest.mark.parametrize( @@ -146,3 +149,23 @@ def test_v4_parser(): ) actions = user_subparsers.choices["create"]._option_string_actions assert actions["--name"].required + + +@pytest.mark.skipif(sys.version_info < (3, 8), reason="added in 3.8") +def test_legacy_display_without_fields_warns(fake_object_no_id): + printer = v4_cli.LegacyPrinter() + + with mock.patch("builtins.print") as mocked: + printer.display(fake_object_no_id, obj=fake_object_no_id) + + assert "No default fields to show" in mocked.call_args.args[0] + + +@pytest.mark.skipif(sys.version_info < (3, 8), reason="added in 3.8") +def test_legacy_display_with_long_repr_truncates(fake_object_long_repr): + printer = v4_cli.LegacyPrinter() + + with mock.patch("builtins.print") as mocked: + printer.display(fake_object_long_repr, obj=fake_object_long_repr) + + assert len(mocked.call_args.args[0]) < 80 |