Skip to content

test(cli): improve basic CLI coverage #1721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gitlab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -208,15 +208,15 @@ 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
parser = _get_parser()
try:
import argcomplete # type: ignore

argcomplete.autocomplete(parser)
argcomplete.autocomplete(parser) # pragma: no cover
except Exception:
pass
args = parser.parse_args()
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ files = "."
module = [
"docs.*",
"docs.ext.*",
"tests.*",
"tests.functional.*",
"tests.functional.api.*",
"tests.unit.*",
Expand Down
1 change: 0 additions & 1 deletion requirements-docker.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
coverage
httmock
mock
pytest
pytest-console-scripts==1.2.1
pytest-cov
responses
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture(scope="session")
def test_dir(pytestconfig):
return pytestconfig.rootdir / "tests"
12 changes: 3 additions & 9 deletions tests/functional/api/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
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"),
}
)

Expand All @@ -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):
Expand Down
49 changes: 49 additions & 0 deletions tests/functional/cli/test_cli.py
Original file line number Diff line number Diff line change
@@ -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]
22 changes: 11 additions & 11 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
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():
Expand All @@ -26,8 +31,8 @@ def reset_gitlab(gl):
user.delete(hard_delete=True)


def set_token(container, rootdir):
set_token_rb = rootdir / "fixtures" / "set_token.rb"
def set_token(container, fixture_dir):
set_token_rb = fixture_dir / "set_token.rb"

with open(set_token_rb, "r") as f:
set_token_command = f.read().strip()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -129,15 +129,15 @@ def _wait(timeout=30, step=0.5):


@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)

docker_services.wait_until_responsive(
timeout=200, pause=5, check=lambda: check_is_alive("gitlab-test")
)

token = set_token("gitlab-test", rootdir=test_dir)
token = set_token("gitlab-test", fixture_dir=fixture_dir)

config = f"""[global]
default = local
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/fixtures/invalid_auth.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[test]
url = https://gitlab.com
private_token = abc123
3 changes: 3 additions & 0 deletions tests/functional/fixtures/invalid_version.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[test]
api_version = 3
url = https://gitlab.example.com
5 changes: 5 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import gitlab


@pytest.fixture(scope="session")
def fixture_dir(test_dir):
return test_dir / "unit" / "fixtures"


@pytest.fixture
def gl():
return gitlab.Gitlab(
Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions tests/unit/objects/test_todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
"""

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,
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import pytest

from gitlab import cli
from gitlab.exceptions import GitlabError


@pytest.mark.parametrize(
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down