diff options
author | Nejc Habjan <hab.nejc@gmail.com> | 2021-11-27 20:28:15 +0100 |
---|---|---|
committer | Nejc Habjan <hab.nejc@gmail.com> | 2021-11-27 21:08:47 +0100 |
commit | 6b892e3dcb18d0f43da6020b08fd4ba891da3670 (patch) | |
tree | e97beaaca3b5b94764f78e0fd99df0c53fd19f8b | |
parent | 70b9870f929c4db32fd2e1406db2122de9958bfd (diff) | |
download | gitlab-test/cli-coverage.tar.gz |
test(cli): improve basic CLI coveragetest/cli-coverage
-rw-r--r-- | gitlab/cli.py | 6 | ||||
-rw-r--r-- | pyproject.toml | 1 | ||||
-rw-r--r-- | requirements-docker.txt | 1 | ||||
-rw-r--r-- | requirements-test.txt | 2 | ||||
-rw-r--r-- | tests/conftest.py | 6 | ||||
-rw-r--r-- | tests/functional/api/test_users.py | 12 | ||||
-rw-r--r-- | tests/functional/cli/test_cli.py | 49 | ||||
-rw-r--r-- | tests/functional/conftest.py | 20 | ||||
-rw-r--r-- | tests/functional/fixtures/invalid_auth.cfg | 3 | ||||
-rw-r--r-- | tests/functional/fixtures/invalid_version.cfg | 3 | ||||
-rw-r--r-- | tests/unit/conftest.py | 5 | ||||
-rw-r--r-- | tests/unit/fixtures/todo.json (renamed from tests/unit/data/todo.json) | 0 | ||||
-rw-r--r-- | tests/unit/objects/test_todos.py | 12 | ||||
-rw-r--r-- | tests/unit/test_cli.py | 14 | ||||
-rw-r--r-- | tests/unit/test_config.py | 2 |
15 files changed, 103 insertions, 33 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py index a0134ec..c1a1334 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -178,7 +178,7 @@ def _parse_value(v: Any) -> Any: return v -def docs() -> argparse.ArgumentParser: +def docs() -> argparse.ArgumentParser: # pragma: no cover """ Provide a statically generated parser for sphinx only, so we don't need to provide dummy gitlab config for readthedocs. @@ -208,7 +208,7 @@ def main() -> None: sys.exit(0) sys.exit(e) # We only support v4 API at this time - if config.api_version not in ("4",): + if config.api_version not in ("4",): # dead code # pragma: no cover raise ModuleNotFoundError(name=f"gitlab.v{config.api_version}.cli") # Now we build the entire set of subcommands and do the complete parsing @@ -216,7 +216,7 @@ def main() -> None: try: import argcomplete # type: ignore - argcomplete.autocomplete(parser) + argcomplete.autocomplete(parser) # pragma: no cover except Exception: pass args = parser.parse_args() diff --git a/pyproject.toml b/pyproject.toml index a19b28e..3e81169 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ files = "." module = [ "docs.*", "docs.ext.*", + "tests.*", "tests.functional.*", "tests.functional.api.*", "tests.unit.*", diff --git a/requirements-docker.txt b/requirements-docker.txt index 4ff5657..c7f6406 100644 --- a/requirements-docker.txt +++ b/requirements-docker.txt @@ -1,5 +1,4 @@ -r requirements.txt -r requirements-test.txt docker-compose==1.29.2 # prevent inconsistent .env behavior from system install -pytest-console-scripts pytest-docker diff --git a/requirements-test.txt b/requirements-test.txt index 8d61ad1..7fd386d 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,6 +1,6 @@ coverage httmock -mock pytest +pytest-console-scripts pytest-cov responses diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..12b573f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.fixture(scope="session") +def test_dir(pytestconfig): + return pytestconfig.rootdir / "tests" diff --git a/tests/functional/api/test_users.py b/tests/functional/api/test_users.py index 1ef237c..edbbca1 100644 --- a/tests/functional/api/test_users.py +++ b/tests/functional/api/test_users.py @@ -3,23 +3,17 @@ GitLab API: https://docs.gitlab.com/ee/api/users.html https://docs.gitlab.com/ee/api/users.html#delete-authentication-identity-from-user """ -import pytest import requests -@pytest.fixture(scope="session") -def avatar_path(test_dir): - return test_dir / "fixtures" / "avatar.png" - - -def test_create_user(gl, avatar_path): +def test_create_user(gl, fixture_dir): user = gl.users.create( { "email": "foo@bar.com", "username": "foo", "name": "foo", "password": "foo_password", - "avatar": open(avatar_path, "rb"), + "avatar": open(fixture_dir / "avatar.png", "rb"), } ) @@ -29,7 +23,7 @@ def test_create_user(gl, avatar_path): avatar_url = user.avatar_url.replace("gitlab.test", "localhost:8080") uploaded_avatar = requests.get(avatar_url).content - assert uploaded_avatar == open(avatar_path, "rb").read() + assert uploaded_avatar == open(fixture_dir / "avatar.png", "rb").read() def test_block_user(gl, user): diff --git a/tests/functional/cli/test_cli.py b/tests/functional/cli/test_cli.py new file mode 100644 index 0000000..c4e76a7 --- /dev/null +++ b/tests/functional/cli/test_cli.py @@ -0,0 +1,49 @@ +import json + +from gitlab import __version__ + + +def test_main_entrypoint(script_runner, gitlab_config): + ret = script_runner.run("python", "-m", "gitlab", "--config-file", gitlab_config) + assert ret.returncode == 2 + + +def test_version(script_runner): + ret = script_runner.run("gitlab", "--version") + assert ret.stdout.strip() == __version__ + + +def test_invalid_config(script_runner): + ret = script_runner.run("gitlab", "--gitlab", "invalid") + assert not ret.success + assert not ret.stdout + + +def test_invalid_config_prints_help(script_runner): + ret = script_runner.run("gitlab", "--gitlab", "invalid", "--help") + assert ret.success + assert ret.stdout + + +def test_invalid_api_version(script_runner, monkeypatch, fixture_dir): + monkeypatch.setenv("PYTHON_GITLAB_CFG", str(fixture_dir / "invalid_version.cfg")) + ret = script_runner.run("gitlab", "--gitlab", "test", "project", "list") + assert not ret.success + assert ret.stderr.startswith("Unsupported API version:") + + +def test_invalid_auth_config(script_runner, monkeypatch, fixture_dir): + monkeypatch.setenv("PYTHON_GITLAB_CFG", str(fixture_dir / "invalid_auth.cfg")) + ret = script_runner.run("gitlab", "--gitlab", "test", "project", "list") + assert not ret.success + assert "401" in ret.stderr + + +def test_fields(gitlab_cli, project_file): + cmd = "-o", "json", "--fields", "default_branch", "project", "list" + + ret = gitlab_cli(cmd) + assert ret.success + + content = json.loads(ret.stdout.strip()) + assert ["default_branch" in item for item in content] diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index b6fb9ed..854a273 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -9,6 +9,11 @@ import pytest import gitlab +@pytest.fixture(scope="session") +def fixture_dir(test_dir): + return test_dir / "functional" / "fixtures" + + def reset_gitlab(gl): # previously tools/reset_gitlab.py for project in gl.projects.list(): @@ -27,7 +32,7 @@ def reset_gitlab(gl): def set_token(container, rootdir): - set_token_rb = rootdir / "fixtures" / "set_token.rb" + set_token_rb = rootdir / "set_token.rb" with open(set_token_rb, "r") as f: set_token_command = f.read().strip() @@ -68,13 +73,8 @@ def temp_dir(): @pytest.fixture(scope="session") -def test_dir(pytestconfig): - return pytestconfig.rootdir / "tests" / "functional" - - -@pytest.fixture(scope="session") -def docker_compose_file(test_dir): - return test_dir / "fixtures" / "docker-compose.yml" +def docker_compose_file(fixture_dir): + return fixture_dir / "docker-compose.yml" @pytest.fixture(scope="session") @@ -129,7 +129,7 @@ def wait_for_sidekiq(gl): @pytest.fixture(scope="session") -def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, test_dir): +def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, fixture_dir): config_file = temp_dir / "python-gitlab.cfg" port = docker_services.port_for("gitlab", 80) @@ -137,7 +137,7 @@ def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, test_dir timeout=200, pause=5, check=lambda: check_is_alive("gitlab-test") ) - token = set_token("gitlab-test", rootdir=test_dir) + token = set_token("gitlab-test", rootdir=fixture_dir) config = f"""[global] default = local diff --git a/tests/functional/fixtures/invalid_auth.cfg b/tests/functional/fixtures/invalid_auth.cfg new file mode 100644 index 0000000..3d61d67 --- /dev/null +++ b/tests/functional/fixtures/invalid_auth.cfg @@ -0,0 +1,3 @@ +[test] +url = https://gitlab.com +private_token = abc123 diff --git a/tests/functional/fixtures/invalid_version.cfg b/tests/functional/fixtures/invalid_version.cfg new file mode 100644 index 0000000..31059a2 --- /dev/null +++ b/tests/functional/fixtures/invalid_version.cfg @@ -0,0 +1,3 @@ +[test] +api_version = 3 +url = https://gitlab.example.com diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index f58c77a..929be1a 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -3,6 +3,11 @@ import pytest import gitlab +@pytest.fixture(scope="session") +def fixture_dir(test_dir): + return test_dir / "unit" / "fixtures" + + @pytest.fixture def gl(): return gitlab.Gitlab( diff --git a/tests/unit/data/todo.json b/tests/unit/fixtures/todo.json index 93b2151..93b2151 100644 --- a/tests/unit/data/todo.json +++ b/tests/unit/fixtures/todo.json diff --git a/tests/unit/objects/test_todos.py b/tests/unit/objects/test_todos.py index 9d6b6b4..ded6cf9 100644 --- a/tests/unit/objects/test_todos.py +++ b/tests/unit/objects/test_todos.py @@ -3,20 +3,22 @@ GitLab API: https://docs.gitlab.com/ce/api/todos.html """ import json -import os import pytest import responses from gitlab.v4.objects import Todo -with open(f"{os.path.dirname(__file__)}/../data/todo.json", "r") as json_file: - todo_content = json_file.read() - json_content = json.loads(todo_content) + +@pytest.fixture() +def json_content(fixture_dir): + with open(fixture_dir / "todo.json", "r") as json_file: + todo_content = json_file.read() + return json.loads(todo_content) @pytest.fixture -def resp_todo(): +def resp_todo(json_content): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: rsps.add( method=responses.GET, diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index d5afe69..2ada1c3 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -25,6 +25,7 @@ from contextlib import redirect_stderr # noqa: H302 import pytest from gitlab import cli +from gitlab.exceptions import GitlabError @pytest.mark.parametrize( @@ -66,12 +67,19 @@ def test_cls_to_what(class_name, expected_what): assert cli.cls_to_what(TestClass) == expected_what -def test_die(): +@pytest.mark.parametrize( + "message,error,expected", + [ + ("foobar", None, "foobar\n"), + ("foo", GitlabError("bar"), "foo (bar)\n"), + ], +) +def test_die(message, error, expected): fl = io.StringIO() with redirect_stderr(fl): with pytest.raises(SystemExit) as test: - cli.die("foobar") - assert fl.getvalue() == "foobar\n" + cli.die(message, error) + assert fl.getvalue() == expected assert test.value.code == 1 diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index f7fffb2..82b9714 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -18,8 +18,8 @@ import io import os from textwrap import dedent +from unittest import mock -import mock import pytest from gitlab import config, USER_AGENT |