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:
returnv


defdocs()->argparse.ArgumentParser:
defdocs()->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
ifconfig.api_versionnotin ("4",):
ifconfig.api_versionnotin ("4",):# dead code # pragma: no cover
raiseModuleNotFoundError(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:
importargcomplete# type: ignore

argcomplete.autocomplete(parser)
argcomplete.autocomplete(parser)# pragma: no cover
exceptException:
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 @@
importpytest


@pytest.fixture(scope="session")
deftest_dir(pytestconfig):
returnpytestconfig.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
"""
importpytest
importrequests


@pytest.fixture(scope="session")
defavatar_path(test_dir):
returntest_dir/"fixtures"/"avatar.png"


deftest_create_user(gl,avatar_path):
deftest_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
assertuploaded_avatar==open(avatar_path,"rb").read()
assertuploaded_avatar==open(fixture_dir/"avatar.png","rb").read()


deftest_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 @@
importjson

fromgitlabimport__version__


deftest_main_entrypoint(script_runner,gitlab_config):
ret=script_runner.run("python","-m","gitlab","--config-file",gitlab_config)
assertret.returncode==2


deftest_version(script_runner):
ret=script_runner.run("gitlab","--version")
assertret.stdout.strip()==__version__


deftest_invalid_config(script_runner):
ret=script_runner.run("gitlab","--gitlab","invalid")
assertnotret.success
assertnotret.stdout


deftest_invalid_config_prints_help(script_runner):
ret=script_runner.run("gitlab","--gitlab","invalid","--help")
assertret.success
assertret.stdout


deftest_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")
assertnotret.success
assertret.stderr.startswith("Unsupported API version:")


deftest_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")
assertnotret.success
assert"401"inret.stderr


deftest_fields(gitlab_cli,project_file):
cmd="-o","json","--fields","default_branch","project","list"

ret=gitlab_cli(cmd)
assertret.success

content=json.loads(ret.stdout.strip())
assert ["default_branch"initemforitemincontent]
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 @@
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 DownExpand 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 DownExpand 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 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 @@
importgitlab


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


@pytest.fixture
defgl():
returngitlab.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 @@
"""

importjson
importos

importpytest
importresponses

fromgitlab.v4.objectsimportTodo

withopen(f"{os.path.dirname(__file__)}/../data/todo.json","r")asjson_file:
todo_content=json_file.read()
json_content=json.loads(todo_content)

@pytest.fixture()
defjson_content(fixture_dir):
withopen(fixture_dir/"todo.json","r")asjson_file:
todo_content=json_file.read()
returnjson.loads(todo_content)


@pytest.fixture
defresp_todo():
defresp_todo(json_content):
withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps:
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 @@
importpytest

fromgitlabimportcli
fromgitlab.exceptionsimportGitlabError


@pytest.mark.parametrize(
Expand DownExpand Up@@ -66,12 +67,19 @@ def test_cls_to_what(class_name, expected_what):
assertcli.cls_to_what(TestClass)==expected_what


deftest_die():
@pytest.mark.parametrize(
"message,error,expected",
[
("foobar",None,"foobar\n"),
("foo",GitlabError("bar"),"foo (bar)\n"),
],
)
deftest_die(message,error,expected):
fl=io.StringIO()
withredirect_stderr(fl):
withpytest.raises(SystemExit)astest:
cli.die("foobar")
assertfl.getvalue()=="foobar\n"
cli.die(message,error)
assertfl.getvalue()==expected
asserttest.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