@@ -21,6 +21,16 @@ def fake_object(fake_manager): | |||
| 21 | 21 | return helpers.FakeObject(fake_manager, {"attr1": "foo", "alist": [1, 2, 3]}) | |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | + @pytest.fixture | ||
| 25 | + def fake_object_no_id(fake_manager): | ||
| 26 | + return helpers.FakeObjectWithoutId(fake_manager, {}) | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + @pytest.fixture | ||
| 30 | + def fake_object_long_repr(fake_manager): | ||
| 31 | + return helpers.FakeObjectWithLongRepr(fake_manager, {"test": "a" * 100}) | ||
| 32 | + | ||
| 33 | + | ||
| 24 | 34 | @pytest.fixture | |
| 25 | 35 | def fake_object_with_parent(fake_manager_with_parent): | |
| 26 | 36 | return helpers.FakeObject( | |
@@ -15,6 +15,15 @@ class FakeObject(base.RESTObject): | |||
| 15 | 15 | pass | |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | + class FakeObjectWithoutId(base.RESTObject): | ||
| 19 | + _id_attr = None | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + class FakeObjectWithLongRepr(base.RESTObject): | ||
| 23 | + _id_attr = None | ||
| 24 | + _repr_attr = "test" | ||
| 25 | + | ||
| 26 | + | ||
| 18 | 27 | class OtherFakeObject(FakeObject): | |
| 19 | 28 | _id_attr = "foo" | |
| 20 | 29 | ||
@@ -1,14 +1,17 @@ | |||
| 1 | 1 | import argparse | |
| 2 | 2 | import io | |
| 3 | 3 | import os | |
| 4 | + import sys | ||
| 4 | 5 | import tempfile | |
| 5 | 6 | from contextlib import redirect_stderr # noqa: H302 | |
| 7 | + from unittest import mock | ||
| 6 | 8 | ||
| 7 | 9 | import pytest | |
| 8 | 10 | ||
| 9 | 11 | import gitlab.base | |
| 10 | 12 | from gitlab import cli | |
| 11 | 13 | from gitlab.exceptions import GitlabError | |
| 14 | + from gitlab.v4 import cli as v4_cli | ||
| 12 | 15 | ||
| 13 | 16 | ||
| 14 | 17 | @pytest.mark.parametrize( | |
@@ -146,3 +149,23 @@ def test_v4_parser(): | |||
| 146 | 149 | ) | |
| 147 | 150 | actions = user_subparsers.choices["create"]._option_string_actions | |
| 148 | 151 | assert actions["--name"].required | |
| 152 | + | ||
| 153 | + | ||
| 154 | + @pytest.mark.skipif(sys.version_info < (3, 8), reason="added in 3.8") | ||
| 155 | + def test_legacy_display_without_fields_warns(fake_object_no_id): | ||
| 156 | + printer = v4_cli.LegacyPrinter() | ||
| 157 | + | ||
| 158 | + with mock.patch("builtins.print") as mocked: | ||
| 159 | + printer.display(fake_object_no_id, obj=fake_object_no_id) | ||
| 160 | + | ||
| 161 | + assert "No default fields to show" in mocked.call_args.args[0] | ||
| 162 | + | ||
| 163 | + | ||
| 164 | + @pytest.mark.skipif(sys.version_info < (3, 8), reason="added in 3.8") | ||
| 165 | + def test_legacy_display_with_long_repr_truncates(fake_object_long_repr): | ||
| 166 | + printer = v4_cli.LegacyPrinter() | ||
| 167 | + | ||
| 168 | + with mock.patch("builtins.print") as mocked: | ||
| 169 | + printer.display(fake_object_long_repr, obj=fake_object_long_repr) | ||
| 170 | + | ||
| 171 | + assert len(mocked.call_args.args[0]) < 80 | ||
0 commit comments