Skip to content

Commit

Permalink
Enable flake8-pytest-style plugin for ruff (#2324)
Browse files Browse the repository at this point in the history
Enable flake8-pytest-style plugin for ruff

Warnings fixed:

* PT003 `scope='function'` is implied in `@pytest.fixture()`
* PT006 Wrong type passed to first argument of `@pytest.mark.parametrize`; expected `tuple`
* PT007 Wrong values type in `@pytest.mark.parametrize` expected `list` of `tuple`
* PT011 `pytest.raises(ValueError)` is too broad, set the `match` parameter or use a more specific exception
* PT013 Incorrect import of `pytest`; use `import pytest` instead
* PT017 Found assertion on exception `e` in `except` block, use `pytest.raises()` instead
* PT018 Assertion should be broken down into multiple parts
* PT021 Use `yield` instead of `request.addfinalizer`

Statistics:

26      PT003   [*] pytest-extraneous-scope-function
 5      PT017   [ ] pytest-assert-in-except
 3      PT006   [*] pytest-parametrize-names-wrong-type
 2      PT007   [*] pytest-parametrize-values-wrong-type
 2      PT021   [ ] pytest-fixture-finalizer-callback
 1      PT011   [ ] pytest-raises-too-broad
 1      PT013   [ ] pytest-incorrect-pytest-import
 1      PT018   [ ] pytest-composite-assertion
  • Loading branch information
cutwater authored Oct 21, 2024
1 parent e908016 commit 73b9637
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 92 deletions.
8 changes: 4 additions & 4 deletions galaxy_ng/tests/integration/api/test_aiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
from ..utils.legacy import cleanup_social_user


@pytest.fixture(scope="function")
@pytest.fixture
def flags(ansible_config):
config = ansible_config("admin")
api_client = get_client(config, request_token=True, require_auth=True)
api_prefix = api_client.config.get("api_prefix").rstrip("/")
return api_client(f"{api_prefix}/_ui/v1/feature-flags/")


@pytest.fixture(scope="function")
@pytest.fixture
def namespace(ansible_config, galaxy_client) -> str:
"""create a new namespace."""
gc = galaxy_client("partner_engineer")
return create_test_namespace(gc)


@pytest.fixture(scope="function")
@pytest.fixture
def pe_namespace(ansible_config, galaxy_client) -> str:
"""create a new namespace owned by PE user."""
config = ansible_config("partner_engineer")
Expand All @@ -53,7 +53,7 @@ def pe_namespace(ansible_config, galaxy_client) -> str:
return new_namespace


@pytest.fixture(scope="function")
@pytest.fixture
def legacy_namespace(ansible_config):
"""Creates a new legacy namespace owned by gh01 user"""

Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
pytestmark = pytest.mark.qa # noqa: F821


@pytest.mark.parametrize("profile", ("basic_user", "partner_engineer", "org_admin", "admin"))
@pytest.mark.parametrize("profile", ["basic_user", "partner_engineer", "org_admin", "admin"])
@pytest.mark.deployment_standalone
@pytest.mark.galaxyapi_smoke
@pytest.mark.skip_in_gw
Expand Down
4 changes: 2 additions & 2 deletions galaxy_ng/tests/integration/api/test_collection_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
NAMESPACE = "signing"


@pytest.fixture(scope="function")
@pytest.fixture
def flags(galaxy_client):
gc = galaxy_client("admin")
return gc.get("_ui/v1/feature-flags/")


@pytest.fixture(scope="function", autouse=True)
@pytest.fixture(autouse=True)
def namespace(galaxy_client):
# ensure namespace exists
gc = galaxy_client("admin")
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/api/test_container_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from galaxykit.utils import wait_for_task


@pytest.fixture(scope="function")
@pytest.fixture
def flags(galaxy_client):
gc = galaxy_client("admin")
return gc.get("_ui/v1/feature-flags/")
Expand Down
3 changes: 2 additions & 1 deletion galaxy_ng/tests/integration/api/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def test_gw_group_role_listing(galaxy_client, settings, test_data):
assert f'/groups/{group_response["id"]}/' in group_roles_response["results"][0]["pulp_href"]

delete_group_v3(gc, group_name)
with pytest.raises(ValueError):

with pytest.raises(ValueError, match=fr"^No group '{group_name}' found\.$"):
get_group_v3(gc, group_name)


Expand Down
20 changes: 8 additions & 12 deletions galaxy_ng/tests/integration/api/test_iqe_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,11 @@ def test_missing_object_role_push_image_to_ee(self, galaxy_client, ansible_confi
ee_name = create_emtpy_local_image_container(ansible_config("admin"), gc)
user, _ = add_new_user_to_new_group(gc)
gc_user = galaxy_client(user)
try:
# We expect the underlying podman command to fail, but we don't
# want to accidentally catch any other error, so we check that
# the error is the podman return code.
with pytest.raises(GalaxyClientError, match="retcode"):
gc_user.push_image(ee_name + ":latest")
except GalaxyClientError as e:
# We expect the underlying podman command to fail, but we don't
# want to accidentally catch any other error, so we check that
# the error is the podman return code.
assert "retcode" in str(e)

@pytest.mark.iqe_rbac_test
def test_object_role_push_image_to_ee(self, galaxy_client, ansible_config):
Expand Down Expand Up @@ -816,13 +814,11 @@ def test_missing_global_role_push_image_to_ee(self, galaxy_client, ansible_confi
gc.add_role_to_group(role_user, group["id"])
ee_name = create_emtpy_local_image_container(ansible_config("admin"), gc)
gc_user = galaxy_client(user)
try:
# We expect the underlying podman command to fail, but we don't
# want to accidentally catch any other error, so we check that
# the error is the podman return code.
with pytest.raises(GalaxyClientError, match="retcode"):
gc_user.push_image(ee_name + ":latest")
except GalaxyClientError as e:
# We expect the underlying podman command to fail, but we don't
# want to accidentally catch any other error, so we check that
# the error is the podman return code.
assert "retcode" in str(e)

@pytest.mark.iqe_rbac_test
def test_missing_object_role_delete_image_from_ee(self, galaxy_client, ansible_config):
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/api/test_ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
log = logging.getLogger(__name__)


@pytest.fixture(scope="function")
@pytest.fixture
def settings(ansible_config):
config = ansible_config("admin")
api_prefix = config.get("api_prefix").rstrip("/")
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/api/test_x_repo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def test_search_by_version(self, galaxy_client):

# FIXME: unskip when https://issues.redhat.com/browse/AAP-32675 is merged
@pytest.mark.skip_in_gw
@pytest.mark.parametrize("is_highest,cv_version", [(True, "4.0.2"), (False, "4.0.1")])
@pytest.mark.parametrize(("is_highest", "cv_version"), [(True, "4.0.2"), (False, "4.0.1")])
@pytest.mark.x_repo_search
def test_search_is_highest_true_false(self, galaxy_client, is_highest, cv_version):
"""
Expand Down
10 changes: 6 additions & 4 deletions galaxy_ng/tests/integration/cli/test_cli_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
@pytest.mark.cli
@pytest.mark.all
@pytest.mark.skip_in_gw
def test_publish_newer_version_collection(galaxy_client, cleanup_collections, uncertifiedv2):
@pytest.mark.usefixtures("cleanup_collections")
def test_publish_newer_version_collection(galaxy_client, uncertifiedv2):
"""Test whether a newer version of collection can be installed after being published.
If the collection version was not certified the version to be installed
Expand Down Expand Up @@ -45,9 +46,9 @@ def test_publish_newer_version_collection(galaxy_client, cleanup_collections, un
@pytest.mark.all
@pytest.mark.cli
@pytest.mark.skip_in_gw
@pytest.mark.usefixtures("cleanup_collections")
def test_publish_newer_certified_collection_version(
galaxy_client,
cleanup_collections,
certifiedv2,
settings,
skip_if_require_signature_for_approval
Expand Down Expand Up @@ -96,7 +97,8 @@ def test_publish_same_collection_version(ansible_config, galaxy_client):
@pytest.mark.all
@pytest.mark.cli
@pytest.mark.skip_in_gw
def test_publish_and_install_by_self(galaxy_client, published, cleanup_collections):
@pytest.mark.usefixtures("cleanup_collections")
def test_publish_and_install_by_self(galaxy_client, published):
"""A publishing user has the permission to install an uncertified version of their
own collection.
"""
Expand All @@ -111,10 +113,10 @@ def test_publish_and_install_by_self(galaxy_client, published, cleanup_collectio
@pytest.mark.cli
@pytest.mark.deployment_cloud
@pytest.mark.skip_in_gw
@pytest.mark.usefixtures("cleanup_collections")
def test_publish_and_expect_uncertified_hidden(
galaxy_client,
published,
cleanup_collections,
settings,
skip_if_require_signature_for_approval
):
Expand Down
8 changes: 4 additions & 4 deletions galaxy_ng/tests/integration/cli/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class DependencySpec:
@pytest.mark.cli
@pytest.mark.slow_in_cloud
@pytest.mark.skip_in_gw
@pytest.mark.usefixtures("cleanup_collections")
@pytest.mark.parametrize(
"params",
(
[
DependencySpec("normal", "1.0.0", 0),
DependencySpec("exact", "=1.0.0", 0),
DependencySpec("lt", "<2.0.0", 0),
Expand All @@ -40,11 +41,10 @@ class DependencySpec:
# DependencySpec("exception", ">0.0.0,!=1.0.0", 1, xfail="galaxy-dev#104"),
# DependencySpec("missing1", "2.0.0", 1, xfail="galaxy-dev#104"),
# DependencySpec("missing2", ">1.0.0", 1, xfail="galaxy-dev#104"),
),
],
ids=lambda s: s.name,
)
def test_collection_dependency_install(ansible_config, published, cleanup_collections,
params, galaxy_client):
def test_collection_dependency_install(ansible_config, published, params, galaxy_client):
"""Collections defining dependencies can be installed and their dependencies are installed
as well.
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/community/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def my_setup_module(ansible_config):
},
)
resp = upload_artifact(admin_config, admin_client, artifact)
resp = wait_for_task(admin_client, resp)
wait_for_task(admin_client, resp)
"""
{'name': 'galaxy_fake_collection', 'namespace': 'ansible', 'description':
'A collection to pretend to manage galaxy with infinidash support', 'type': 'collection',
Expand Down
47 changes: 22 additions & 25 deletions galaxy_ng/tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def ansible_config():
return get_ansible_config()


@pytest.fixture(scope="function")
@pytest.fixture
def published(ansible_config, artifact, galaxy_client):
# make sure the expected namespace exists ...
gc = galaxy_client("partner_engineer")
Expand All @@ -125,7 +125,7 @@ def published(ansible_config, artifact, galaxy_client):
return artifact


@pytest.fixture(scope="function")
@pytest.fixture
def certifiedv2(ansible_config, artifact, galaxy_client):
""" Create and publish+certify collection version N and N+1 """

Expand Down Expand Up @@ -164,7 +164,7 @@ def certifiedv2(ansible_config, artifact, galaxy_client):
return (artifact, artifact2)


@pytest.fixture(scope="function")
@pytest.fixture
def uncertifiedv2(ansible_config, artifact, settings, galaxy_client):
""" Create and publish collection version N and N+1 but only certify N"""

Expand Down Expand Up @@ -204,7 +204,7 @@ def uncertifiedv2(ansible_config, artifact, settings, galaxy_client):
return artifact, artifact2


@pytest.fixture(scope="function")
@pytest.fixture
def auto_approved_artifacts(ansible_config, artifact, galaxy_client):
""" Create and publish collection version N and N+1"""

Expand Down Expand Up @@ -249,7 +249,7 @@ def auto_approved_artifacts(ansible_config, artifact, galaxy_client):
return artifact, artifact2


@pytest.fixture(scope="function")
@pytest.fixture
def artifact():
"""Generate a randomized collection for testing."""

Expand All @@ -269,17 +269,14 @@ def upload_artifact():


@pytest.fixture
def cleanup_collections(request):
def cleanup_collections():
"""Clean created resources during test executions."""

def cleanup():
path = os.path.expanduser(
f"~/.ansible/collections/ansible_collections/{USERNAME_PUBLISHER}/"
)
if os.path.exists(path):
shutil.rmtree(path)
yield

request.addfinalizer(cleanup)
path = os.path.expanduser(f"~/.ansible/collections/ansible_collections/{USERNAME_PUBLISHER}/")
if os.path.exists(path):
shutil.rmtree(path)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -384,13 +381,13 @@ def sync_instance_crc():
return (manifest, config)


@pytest.fixture(scope="function")
@pytest.fixture
def settings(galaxy_client):
gc = galaxy_client("admin")
return gc.get("_ui/v1/settings/")


@pytest.fixture(scope="function")
@pytest.fixture
def use_collection_signatures(settings):
"""A shortcut to know if a test should attempt to work with signatures."""
service = settings["GALAXY_COLLECTION_SIGNING_SERVICE"]
Expand All @@ -400,15 +397,15 @@ def use_collection_signatures(settings):
return False


@pytest.fixture(scope="function")
@pytest.fixture
def autohubtest2(galaxy_client):
"""A carry over pre-created namespace from the original IQE tests."""
gc = galaxy_client("admin")
create_namespace(gc, "autohubtest2", "")
return {"name": "autohubtest2"}


@pytest.fixture(scope="function")
@pytest.fixture
def random_namespace(galaxy_client, settings):
"""Make a randomized namespace."""

Expand All @@ -423,7 +420,7 @@ def random_namespace(galaxy_client, settings):
return get_namespace(ns_name, gc=gc)


@pytest.fixture(scope="function")
@pytest.fixture
def random_username(galaxy_client):
"""Make a random username."""
return 'user_' + generate_namespace()
Expand Down Expand Up @@ -531,7 +528,7 @@ def hub_version(ansible_config):
return get_hub_version(ansible_config)


@pytest.fixture(scope="function")
@pytest.fixture
def gh_user_1_post(ansible_config):
"""
Returns a galaxy kit client with a GitHub user logged into beta galaxy stage
Expand All @@ -544,7 +541,7 @@ def gh_user_1_post(ansible_config):
remove_from_cache("github_user")


@pytest.fixture(scope="function")
@pytest.fixture
def gh_user_1(ansible_config):
"""
Returns a galaxy kit client with a GitHub user logged into beta galaxy stage
Expand All @@ -553,7 +550,7 @@ def gh_user_1(ansible_config):
return gc("github_user", github_social_auth=True)


@pytest.fixture(scope="function")
@pytest.fixture
def gh_user_2(ansible_config):
"""
Returns a galaxy kit client with a GitHub user logged into beta galaxy stage
Expand All @@ -562,7 +559,7 @@ def gh_user_2(ansible_config):
return gc("github_user_alt", github_social_auth=True)


@pytest.fixture(scope="function")
@pytest.fixture
def gh_user_1_pre(ansible_config):
"""
Removes everything related to the GitHub user and the user itself and
Expand All @@ -573,7 +570,7 @@ def gh_user_1_pre(ansible_config):
return gc("github_user", github_social_auth=True, ignore_cache=True)


@pytest.fixture(scope="function")
@pytest.fixture
def gw_user_1(ansible_config):
"""
Returns a galaxy kit client with a GitHub user logged into beta galaxy stage
Expand All @@ -582,7 +579,7 @@ def gw_user_1(ansible_config):
return gc("github_user", github_social_auth=True)


@pytest.fixture(scope="function")
@pytest.fixture
def generate_test_artifact(ansible_config):
"""
Generates a test artifact and deletes it after the test
Expand All @@ -600,7 +597,7 @@ def generate_test_artifact(ansible_config):
delete_collection(gc_admin, namespace=artifact.namespace, collection=artifact.name)


@pytest.fixture(scope="function")
@pytest.fixture
def keep_generated_test_artifact(ansible_config):
"""
Generates a test artifact
Expand Down
Loading

0 comments on commit 73b9637

Please sign in to comment.