Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 2 commits intomainfromtest/cli-coverage
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsgitlab/cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionspyproject.toml
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionrequirements-docker.txt
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionrequirements-test.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
coverage
httmock
mock
pytest
pytest-console-scripts==1.2.1
pytest-cov
responses
6 changes: 6 additions & 0 deletionstests/conftest.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/functional/api/test_users.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/functional/cli/test_cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/functional/conftest.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,11 @@
importgitlab


@pytest.fixture(scope="session")
deffixture_dir(test_dir):
returntest_dir/"functional"/"fixtures"


defreset_gitlab(gl):
# previously tools/reset_gitlab.py
forprojectingl.projects.list():
Expand All@@ -26,8 +31,8 @@ def reset_gitlab(gl):
user.delete(hard_delete=True)


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

withopen(set_token_rb,"r")asf:
set_token_command=f.read().strip()
Expand DownExpand Up@@ -68,13 +73,8 @@ def temp_dir():


@pytest.fixture(scope="session")
deftest_dir(pytestconfig):
returnpytestconfig.rootdir/"tests"/"functional"


@pytest.fixture(scope="session")
defdocker_compose_file(test_dir):
returntest_dir/"fixtures"/"docker-compose.yml"
defdocker_compose_file(fixture_dir):
returnfixture_dir/"docker-compose.yml"


@pytest.fixture(scope="session")
Expand DownExpand Up@@ -129,15 +129,15 @@ def _wait(timeout=30, step=0.5):


@pytest.fixture(scope="session")
defgitlab_config(check_is_alive,docker_ip,docker_services,temp_dir,test_dir):
defgitlab_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 deletionstests/functional/fixtures/invalid_auth.cfg
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
[test]
url = https://gitlab.com
private_token = abc123
3 changes: 3 additions & 0 deletionstests/functional/fixtures/invalid_version.cfg
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
[test]
api_version = 3
url = https://gitlab.example.com
5 changes: 5 additions & 0 deletionstests/unit/conftest.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/unit/objects/test_todos.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/unit/test_cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
import pytest

from gitlab import cli
from gitlab.exceptions import GitlabError


@pytest.mark.parametrize(
Expand DownExpand 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 deletiontests/unit/test_config.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,8 +18,8 @@
importio
importos
fromtextwrapimportdedent
fromunittestimportmock

importmock
importpytest

fromgitlabimportconfig,USER_AGENT
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp