From 8050dac7e75ecad70918fa3e16ffb28ca01f180d Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Fri, 12 Jan 2024 22:21:32 -0500 Subject: [PATCH] fix pre-commit for all files --- src/saltext/github/modules/github.py | 90 ++++------ src/saltext/github/states/github.py | 227 +++++++++--------------- src/saltext/github/utils/github.py | 4 +- tests/functional/modules/test_github.py | 6 - tests/pytests/functional/conftest.py | 140 --------------- tests/pytests/integration/conftest.py | 109 ------------ tests/pytests/unit/conftest.py | 77 -------- tests/unit/modules/test_github.py | 7 +- tests/unit/states/test_github.py | 6 +- 9 files changed, 123 insertions(+), 543 deletions(-) delete mode 100644 tests/pytests/functional/conftest.py delete mode 100644 tests/pytests/integration/conftest.py delete mode 100644 tests/pytests/unit/conftest.py diff --git a/src/saltext/github/modules/github.py b/src/saltext/github/modules/github.py index bc45f9ca..4cecd22b 100644 --- a/src/saltext/github/modules/github.py +++ b/src/saltext/github/modules/github.py @@ -28,8 +28,6 @@ # in an automated way. set this to True to allow privacy modifications allow_repo_privacy_changes: False """ - - import logging import salt.utils.http @@ -61,8 +59,7 @@ def __virtual__(): return __virtualname__ return ( False, - "The github execution module cannot be loaded: " - "PyGithub library is not installed.", + "The github execution module cannot be loaded: PyGithub library is not installed.", ) @@ -80,16 +77,13 @@ def _get_config_value(profile, config_name): config = __salt__["config.option"](profile) if not config: raise CommandExecutionError( - "Authentication information could not be found for the " - "'{}' profile.".format(profile) + f"Authentication information could not be found for the '{profile}' profile." ) config_value = config.get(config_name) if config_value is None: raise CommandExecutionError( - "The '{}' parameter was not found in the '{}' profile.".format( - config_name, profile - ) + f"The '{config_name}' parameter was not found in the '{profile}' profile." ) return config_value @@ -100,7 +94,9 @@ def _get_client(profile): Return the GitHub client, cached into __context__ for performance """ token = _get_config_value(profile, "token") - key = "github.{}:{}".format(token, _get_config_value(profile, "org_name")) + key = "github.{}:{}".format( # pylint: disable=consider-using-f-string + token, _get_config_value(profile, "org_name") + ) if key not in __context__: __context__[key] = github.Github(token, per_page=100) @@ -119,7 +115,7 @@ def _get_members(organization, params=None): def _get_repos(profile, params=None, ignore_cache=False): # Use cache when no params are given org_name = _get_config_value(profile, "org_name") - key = "github.{}:repos".format(org_name) + key = f"github.{org_name}:repos" if key not in __context__ or ignore_cache or params is not None: org_name = _get_config_value(profile, "org_name") @@ -143,7 +139,7 @@ def _get_repos(profile, params=None, ignore_cache=False): next_result.append(repo) # Cache a copy of each repo for single lookups - repo_key = "github.{}:{}:repo_info".format(org_name, repo.name.lower()) + repo_key = f"github.{org_name}:{repo.name.lower()}:repo_info" __context__[repo_key] = _repo_to_dict(repo) __context__[key] = next_result @@ -171,7 +167,7 @@ def list_users(profile="github", ignore_cache=False): salt myminion github.list_users profile='my-github-profile' """ org_name = _get_config_value(profile, "org_name") - key = "github.{}:users".format(org_name) + key = f"github.{org_name}:users" if key not in __context__ or ignore_cache: client = _get_client(profile) organization = client.get_organization(org_name) @@ -356,9 +352,7 @@ def get_issue(issue_number, repo_name=None, profile="github", output="min"): return ret -def get_issue_comments( - issue_number, repo_name=None, profile="github", since=None, output="min" -): +def get_issue_comments(issue_number, repo_name=None, profile="github", since=None, output="min"): """ Return information about the comments for a given issue in a named repository. @@ -636,9 +630,7 @@ def get_milestones( return ret -def get_milestone( - number=None, name=None, repo_name=None, profile="github", output="min" -): +def get_milestone(number=None, name=None, repo_name=None, profile="github", output="min"): """ Return information about a single milestone in a named repository. @@ -675,9 +667,7 @@ def get_milestone( ret = {} if not any([number, name]): - raise CommandExecutionError( - "Either a milestone 'name' or 'number' must be provided." - ) + raise CommandExecutionError("Either a milestone 'name' or 'number' must be provided.") org_name = _get_config_value(profile, "org_name") if repo_name is None: @@ -754,7 +744,7 @@ def get_repo_info(repo_name, profile="github", ignore_cache=False): """ org_name = _get_config_value(profile, "org_name") - key = "github.{}:{}:repo_info".format( + key = "github.{}:{}:repo_info".format( # pylint: disable=consider-using-f-string _get_config_value(profile, "org_name"), repo_name.lower() ) @@ -772,11 +762,10 @@ def get_repo_info(repo_name, profile="github", ignore_cache=False): ret = _repo_to_dict(repo) __context__[key] = ret - except github.UnknownObjectException: + except github.UnknownObjectException as exc: raise CommandExecutionError( - "The '{}' repository under the '{}' organization could not " - "be found.".format(repo_name, org_name) - ) + f"The '{repo_name}' repository under the '{org_name}' organization could not be found." + ) from exc return __context__[key] @@ -805,22 +794,18 @@ def get_repo_teams(repo_name, profile="github"): try: repo = client.get_repo("/".join([org_name, repo_name])) - except github.UnknownObjectException: + except github.UnknownObjectException as exc: raise CommandExecutionError( - "The '{}' repository under the '{}' organization could not " - "be found.".format(repo_name, org_name) - ) + f"The '{repo_name}' repository under the '{org_name}' organization could not be found." + ) from exc try: teams = repo.get_teams() for team in teams: - ret.append( - {"id": team.id, "name": team.name, "permission": team.permission} - ) - except github.UnknownObjectException: + ret.append({"id": team.id, "name": team.name, "permission": team.permission}) + except github.UnknownObjectException as exc: raise CommandExecutionError( - "Unable to retrieve teams for repository '{}' under the '{}' " - "organization.".format(repo_name, org_name) - ) + f"Unable to retrieve teams for repository '{repo_name}' under the '{org_name}' organization." + ) from exc return ret @@ -1032,9 +1017,7 @@ def edit_repo( if private is not None and not allow_private_change: raise CommandExecutionError( - "The private field is set to be changed for " - "repo {} but allow_repo_privacy_changes " - "disallows this.".format(name) + f"The private field is set to be changed for repo {name} but allow_repo_privacy_changes disallows this." ) try: @@ -1407,9 +1390,7 @@ def remove_team_repo(repo_name, team_name, profile="github"): log.exception("Resource not found: %s", team["id"]) return False team.remove_from_repos(repo) - return repo_name not in list_team_repos( - team_name, profile=profile, ignore_cache=True - ) + return repo_name not in list_team_repos(team_name, profile=profile, ignore_cache=True) def list_team_members(team_name, profile="github", ignore_cache=False): @@ -1473,7 +1454,9 @@ def list_members_without_mfa(profile="github", ignore_cache=False): .. versionadded:: 2016.11.0 """ - key = "github.{}:non_mfa_users".format(_get_config_value(profile, "org_name")) + key = "github.{}:non_mfa_users".format( # pylint: disable=consider-using-f-string + _get_config_value(profile, "org_name") + ) if key not in __context__ or ignore_cache: client = _get_client(profile) @@ -1486,8 +1469,7 @@ def list_members_without_mfa(profile="github", ignore_cache=False): filter_key = "filter_" __context__[key] = [ - m.login.lower() - for m in _get_members(organization, {filter_key: "2fa_disabled"}) + m.login.lower() for m in _get_members(organization, {filter_key: "2fa_disabled"}) ] return __context__[key] @@ -1604,8 +1586,7 @@ def remove_team_member(name, team_name, profile="github"): if not hasattr(team, "remove_from_members"): return ( False, - "PyGithub 1.26.0 or greater is required for team " - "management, please upgrade.", + "PyGithub 1.26.0 or greater is required for team management, please upgrade.", ) team.remove_from_members(member) @@ -1630,7 +1611,9 @@ def list_teams(profile="github", ignore_cache=False): .. versionadded:: 2016.11.0 """ - key = "github.{}:teams".format(_get_config_value(profile, "org_name")) + key = "github.{}:teams".format( # pylint: disable=consider-using-f-string + _get_config_value(profile, "org_name") + ) if key not in __context__ or ignore_cache: client = _get_client(profile) @@ -1834,7 +1817,7 @@ def _query( url += action if command: - url += "/{}".format(command) + url += f"/{command}" log.debug("GitHub URL: %s", url) @@ -1886,9 +1869,8 @@ def _query( complete_result = complete_result + result["dict"] else: - raise CommandExecutionError( - "GitHub Response Error: {}".format(result.get("error")) - ) + err = result.get("error") + raise CommandExecutionError(f"GitHub Response Error: {err}") try: link_info = result.get("headers").get("Link").split(",")[0] diff --git a/src/saltext/github/states/github.py b/src/saltext/github/states/github.py index f93e6e87..6804339a 100644 --- a/src/saltext/github/states/github.py +++ b/src/saltext/github/states/github.py @@ -13,7 +13,6 @@ - email: example@domain.com - username: 'gitexample' """ - import datetime import logging import time @@ -55,15 +54,13 @@ def present(name, profile="github", **kwargs): # If the user has a valid github handle and is not in the org already if not target: ret["result"] = False - ret["comment"] = "Couldnt find user {}".format(name) + ret["comment"] = f"Couldnt find user {name}" elif isinstance(target, bool) and target: - ret["comment"] = "User {} is already in the org ".format(name) + ret["comment"] = f"User {name} is already in the org " ret["result"] = True - elif ( - not target.get("in_org", False) and target.get("membership_state") != "pending" - ): + elif not target.get("in_org", False) and target.get("membership_state") != "pending": if __opts__["test"]: - ret["comment"] = "User {} will be added to the org".format(name) + ret["comment"] = f"User {name} will be added to the org" return ret # add the user @@ -71,15 +68,13 @@ def present(name, profile="github", **kwargs): if result: ret["changes"].setdefault("old", None) - ret["changes"].setdefault( - "new", "User {} exists in the org now".format(name) - ) + ret["changes"].setdefault("new", f"User {name} exists in the org now") ret["result"] = True else: ret["result"] = False - ret["comment"] = "Failed to add user {} to the org".format(name) + ret["comment"] = f"Failed to add user {name} to the org" else: - ret["comment"] = "User {} has already been invited.".format(name) + ret["comment"] = f"User {name} has already been invited." ret["result"] = True return ret @@ -110,7 +105,7 @@ def absent(name, profile="github", **kwargs): "name": name, "changes": {}, "result": None, - "comment": "User {} is absent.".format(name), + "comment": f"User {name} is absent.", } target = __salt__["github.get_user"](name, profile=profile, **kwargs) @@ -118,25 +113,25 @@ def absent(name, profile="github", **kwargs): if target: if isinstance(target, bool) or target.get("in_org", False): if __opts__["test"]: - ret["comment"] = "User {} will be deleted".format(name) + ret["comment"] = f"User {name} will be deleted" ret["result"] = None return ret result = __salt__["github.remove_user"](name, profile=profile, **kwargs) if result: - ret["comment"] = "Deleted user {}".format(name) - ret["changes"].setdefault("old", "User {} exists".format(name)) - ret["changes"].setdefault("new", "User {} deleted".format(name)) + ret["comment"] = f"Deleted user {name}" + ret["changes"].setdefault("old", f"User {name} exists") + ret["changes"].setdefault("new", f"User {name} deleted") ret["result"] = True else: - ret["comment"] = "Failed to delete {}".format(name) + ret["comment"] = f"Failed to delete {name}" ret["result"] = False else: - ret["comment"] = "User {} has already been deleted!".format(name) + ret["comment"] = f"User {name} has already been deleted!" ret["result"] = True else: - ret["comment"] = "User {} does not exist".format(name) + ret["comment"] = f"User {name} does not exist" ret["result"] = True return ret @@ -153,7 +148,7 @@ def team_present( enforce_mfa=False, no_mfa_grace_seconds=0, profile="github", - **kwargs + **kwargs, ): """ Ensure a team is present @@ -228,20 +223,14 @@ def team_present( if len(parameters) > 0: if __opts__["test"]: - test_comments.append( - "Team properties are set to be edited: {}".format(parameters) - ) + test_comments.append(f"Team properties are set to be edited: {parameters}") ret["result"] = None else: - result = __salt__["github.edit_team"]( - name, profile=profile, **parameters - ) + result = __salt__["github.edit_team"](name, profile=profile, **parameters) if result: ret["changes"]["team"] = { - "old": "Team properties were {}".format(target), - "new": "Team properties (that changed) are {}".format( - parameters - ), + "old": f"Team properties were {target}", + "new": f"Team properties (that changed) are {parameters}", } else: ret["result"] = False @@ -249,9 +238,7 @@ def team_present( return ret manage_repos = repo_names is not None - current_repos = set( - __salt__["github.list_team_repos"](name, profile=profile).keys() - ) + current_repos = set(__salt__["github.list_team_repos"](name, profile=profile).keys()) repo_names = set(repo_names or []) repos_to_add = repo_names - current_repos @@ -260,7 +247,7 @@ def team_present( if repos_to_add: if __opts__["test"]: test_comments.append( - "Team {} will have the following repos added: {}.".format( + "Team {} will have the following repos added: {}.".format( # pylint: disable=consider-using-f-string name, list(repos_to_add) ) ) @@ -272,20 +259,18 @@ def team_present( ) if result: ret["changes"][repo_name] = { - "old": "Repo {} is not in team {}".format(repo_name, name), - "new": "Repo {} is in team {}".format(repo_name, name), + "old": f"Repo {repo_name} is not in team {name}", + "new": f"Repo {repo_name} is in team {name}", } else: ret["result"] = False - ret["comment"] = "Failed to add repo {} to team {}.".format( - repo_name, name - ) + ret["comment"] = f"Failed to add repo {repo_name} to team {name}." return ret if repos_to_remove: if __opts__["test"]: test_comments.append( - "Team {} will have the following repos removed: {}.".format( + "Team {} will have the following repos removed: {}.".format( # pylint: disable=consider-using-f-string name, list(repos_to_remove) ) ) @@ -297,21 +282,17 @@ def team_present( ) if result: ret["changes"][repo_name] = { - "old": "Repo {} is in team {}".format(repo_name, name), - "new": "Repo {} is not in team {}".format(repo_name, name), + "old": f"Repo {repo_name} is in team {name}", + "new": f"Repo {repo_name} is not in team {name}", } else: ret["result"] = False - ret[ - "comment" - ] = "Failed to remove repo {} from team {}.".format( - repo_name, name - ) + ret["comment"] = f"Failed to remove repo {repo_name} from team {name}." return ret else: # Team does not exist - it will be created. if __opts__["test"]: - ret["comment"] = "Team {} is set to be created.".format(name) + ret["comment"] = f"Team {name} is set to be created." ret["result"] = None return ret @@ -322,22 +303,20 @@ def team_present( permission=permission, privacy=privacy, profile=profile, - **kwargs + **kwargs, ) if result: ret["changes"]["team"] = {} ret["changes"]["team"]["old"] = None - ret["changes"]["team"]["new"] = "Team {} has been created".format(name) + ret["changes"]["team"]["new"] = f"Team {name} has been created" else: ret["result"] = False - ret["comment"] = "Failed to create team {}.".format(name) + ret["comment"] = f"Failed to create team {name}." return ret manage_members = members is not None - mfa_deadline = datetime.datetime.utcnow() - datetime.timedelta( - seconds=no_mfa_grace_seconds - ) + mfa_deadline = datetime.datetime.utcnow() - datetime.timedelta(seconds=no_mfa_grace_seconds) members_no_mfa = __salt__["github.list_members_without_mfa"](profile=profile) members_lower = {} @@ -356,16 +335,12 @@ def team_present( ): if __opts__["test"]: test_comments.append( - "User {} will not be added to the " - "team because they do not have MFA." - "".format(member) + f"User {member} will not be added to the team because they do not have MFA." ) else: # Add to team member_change = True if __opts__["test"]: - test_comments.append( - "User {} set to be added to the team.".format(member) - ) + test_comments.append(f"User {member} set to be added to the team.") ret["result"] = None else: result = __salt__["github.add_team_member"]( @@ -373,17 +348,11 @@ def team_present( ) if result: ret["changes"][member] = {} - ret["changes"][member][ - "old" - ] = "User {} is not in team {}".format(member, name) - ret["changes"][member]["new"] = "User {} is in team {}".format( - member, name - ) + ret["changes"][member]["old"] = f"User {member} is not in team {name}" + ret["changes"][member]["new"] = f"User {member} is in team {name}" else: ret["result"] = False - ret["comment"] = "Failed to add user {} to team {}.".format( - member, name - ) + ret["comment"] = f"Failed to add user {member} to team {name}." return ret for member in current_members: @@ -392,23 +361,16 @@ def team_present( mfa_violation = _member_violates_mfa( member, members_lower[member], mfa_deadline, members_no_mfa ) - if ( - manage_members - and member not in members_lower - or (enforce_mfa and mfa_violation) - ): + if manage_members and member not in members_lower or (enforce_mfa and mfa_violation): # Remove from team member_change = True if __opts__["test"]: if mfa_violation: test_comments.append( - "User {} set to be removed from the " - "team because they do not have MFA.".format(member) + f"User {member} set to be removed from the team because they do not have MFA." ) else: - test_comments.append( - "User {} set to be removed from the team.".format(member) - ) + test_comments.append(f"User {member} set to be removed from the team.") ret["result"] = None else: result = __salt__["github.remove_team_member"]( @@ -417,22 +379,16 @@ def team_present( if result: extra_changes = " due to MFA violation" if mfa_violation else "" ret["changes"][member] = { - "old": "User {} is in team {}".format(member, name), - "new": "User {} is not in team {}{}".format( - member, name, extra_changes - ), + "old": f"User {member} is in team {name}", + "new": f"User {member} is not in team {name}{extra_changes}", } else: ret["result"] = False - ret["comment"] = "Failed to remove user {} from team {}.".format( - member, name - ) + ret["comment"] = f"Failed to remove user {member} from team {name}." return ret if member_change: # Refresh team cache - __salt__["github.list_team_members"]( - name, profile=profile, ignore_cache=False, **kwargs - ) + __salt__["github.list_team_members"](name, profile=profile, ignore_cache=False, **kwargs) if len(test_comments) > 0: ret["comment"] = "\n".join(test_comments) @@ -473,24 +429,24 @@ def team_absent(name, profile="github", **kwargs): target = __salt__["github.get_team"](name, profile=profile, **kwargs) if not target: - ret["comment"] = "Team {} does not exist".format(name) + ret["comment"] = f"Team {name} does not exist" ret["result"] = True return ret else: if __opts__["test"]: - ret["comment"] = "Team {} will be deleted".format(name) + ret["comment"] = f"Team {name} will be deleted" ret["result"] = None return ret result = __salt__["github.remove_team"](name, profile=profile, **kwargs) if result: - ret["comment"] = "Deleted team {}".format(name) - ret["changes"].setdefault("old", "Team {} exists".format(name)) - ret["changes"].setdefault("new", "Team {} deleted".format(name)) + ret["comment"] = f"Deleted team {name}" + ret["changes"].setdefault("old", f"Team {name} exists") + ret["changes"].setdefault("new", f"Team {name} deleted") ret["result"] = True else: - ret["comment"] = "Failed to delete {}".format(name) + ret["comment"] = f"Failed to delete {name}" ret["result"] = False return ret @@ -508,7 +464,7 @@ def repo_present( license_template=None, teams=None, profile="github", - **kwargs + **kwargs, ): """ Ensure a repository is present @@ -605,16 +561,14 @@ def repo_present( if len(parameters) > 0: repo_change = { - "old": "Repo properties were {}".format(old_parameters), - "new": "Repo properties (that changed) are {}".format(parameters), + "old": f"Repo properties were {old_parameters}", + "new": f"Repo properties (that changed) are {parameters}", } if __opts__["test"]: ret["changes"]["repo"] = repo_change ret["result"] = None else: - result = __salt__["github.edit_repo"]( - name, profile=profile, **parameters - ) + result = __salt__["github.edit_repo"](name, profile=profile, **parameters) if result: ret["changes"]["repo"] = repo_change else: @@ -623,7 +577,7 @@ def repo_present( return ret else: # Repo does not exist - it will be created. - repo_change = {"old": None, "new": "Repo {} has been created".format(name)} + repo_change = {"old": None, "new": f"Repo {name} has been created"} if __opts__["test"]: ret["changes"]["repo"] = repo_change ret["result"] = None @@ -634,7 +588,7 @@ def repo_present( if not result: ret["result"] = False - ret["comment"] = "Failed to create repo {}.".format(name) + ret["comment"] = f"Failed to create repo {name}." return ret # Turns out that trying to fetch teams for a new repo can 404 immediately @@ -652,7 +606,7 @@ def repo_present( if current_teams is None: ret["result"] = False - ret["comment"] = "Failed to verify repo {} after creation.".format(name) + ret["comment"] = f"Failed to verify repo {name} after creation." return ret ret["changes"]["repo"] = repo_change @@ -669,34 +623,28 @@ def repo_present( for team_name in current_team_names: if team_name not in teams: team_change = { - "old": "Repo {} is in team {}".format(name, team_name), - "new": "Repo {} is not in team {}".format(name, team_name), + "old": f"Repo {name} is in team {team_name}", + "new": f"Repo {name} is not in team {team_name}", } if __opts__["test"]: ret["changes"][team_name] = team_change ret["result"] = None else: - result = __salt__["github.remove_team_repo"]( - name, team_name, profile=profile - ) + result = __salt__["github.remove_team_repo"](name, team_name, profile=profile) if result: ret["changes"][team_name] = team_change else: ret["result"] = False - ret[ - "comment" - ] = "Failed to remove repo {} from team {}.".format( - name, team_name - ) + ret["comment"] = f"Failed to remove repo {name} from team {team_name}." return ret # Next add or modify any necessary teams for team_name, permission in teams.items(): if team_name not in current_team_names: # Need to add repo to team team_change = { - "old": "Repo {} is not in team {}".format(name, team_name), - "new": "Repo {} is in team {}".format(name, team_name), + "old": f"Repo {name} is not in team {team_name}", + "new": f"Repo {name} is in team {team_name}", } if __opts__["test"]: ret["changes"][team_name] = team_change @@ -709,11 +657,7 @@ def repo_present( ret["changes"][team_name] = team_change else: ret["result"] = False - ret[ - "comment" - ] = "Failed to remove repo {} from team {}.".format( - name, team_name - ) + ret["comment"] = f"Failed to remove repo {name} from team {team_name}." return ret else: current_permission = ( @@ -723,19 +667,14 @@ def repo_present( ) if not current_permission: ret["result"] = False - ret["comment"] = ( - "Failed to determine current permission for team " - "{} in repo {}".format(team_name, name) - ) + ret[ + "comment" + ] = f"Failed to determine current permission for team {team_name} in repo {name}" return ret elif current_permission != permission: team_change = { - "old": "Repo {} in team {} has permission {}".format( - name, team_name, current_permission - ), - "new": "Repo {} in team {} has permission {}".format( - name, team_name, permission - ), + "old": f"Repo {name} in team {team_name} has permission {current_permission}", + "new": f"Repo {name} in team {team_name} has permission {permission}", } if __opts__["test"]: ret["changes"][team_name] = team_change @@ -748,10 +687,9 @@ def repo_present( ret["changes"][team_name] = team_change else: ret["result"] = False - ret["comment"] = ( - "Failed to set permission on repo {} from " - "team {} to {}.".format(name, team_name, permission) - ) + ret[ + "comment" + ] = f"Failed to set permission on repo {name} from team {team_name} to {permission}." return ret return ret @@ -783,26 +721,25 @@ def repo_absent(name, profile="github", **kwargs): target = None if not target: - ret["comment"] = "Repo {} does not exist".format(name) + ret["comment"] = f"Repo {name} does not exist" ret["result"] = True return ret else: if __opts__["test"]: - ret["comment"] = "Repo {} will be deleted".format(name) + ret["comment"] = f"Repo {name} will be deleted" ret["result"] = None return ret result = __salt__["github.remove_repo"](name, profile=profile, **kwargs) if result: - ret["comment"] = "Deleted repo {}".format(name) - ret["changes"].setdefault("old", "Repo {} exists".format(name)) - ret["changes"].setdefault("new", "Repo {} deleted".format(name)) + ret["comment"] = f"Deleted repo {name}" + ret["changes"].setdefault("old", f"Repo {name} exists") + ret["changes"].setdefault("new", f"Repo {name} deleted") ret["result"] = True else: - ret["comment"] = ( - "Failed to delete repo {}. Ensure the delete_repo " - "scope is enabled if using OAuth.".format(name) - ) + ret[ + "comment" + ] = f"Failed to delete repo {name}. Ensure the delete_repo scope is enabled if using OAuth." ret["result"] = False return ret diff --git a/src/saltext/github/utils/github.py b/src/saltext/github/utils/github.py index 1d6824f2..a51e44b4 100644 --- a/src/saltext/github/utils/github.py +++ b/src/saltext/github/utils/github.py @@ -1,8 +1,6 @@ """ Connection library for GitHub """ - - import logging import salt.utils.http @@ -44,7 +42,7 @@ def get_user_pubkeys(users): key_ids = user[tmp_user] user = tmp_user - url = "https://api.github.com/users/{}/keys".format(user) + url = f"https://api.github.com/users/{user}/keys" result = salt.utils.http.query( url, "GET", diff --git a/tests/functional/modules/test_github.py b/tests/functional/modules/test_github.py index 47303467..792aba61 100644 --- a/tests/functional/modules/test_github.py +++ b/tests/functional/modules/test_github.py @@ -8,9 +8,3 @@ @pytest.fixture def github(modules): return modules.github - - -def test_replace_this_this_with_something_meaningful(github): - echo_str = "Echoed!" - res = github.example_function(echo_str) - assert res == echo_str diff --git a/tests/pytests/functional/conftest.py b/tests/pytests/functional/conftest.py deleted file mode 100644 index 2fb2246b..00000000 --- a/tests/pytests/functional/conftest.py +++ /dev/null @@ -1,140 +0,0 @@ -import logging -import shutil - -import pytest -from saltfactories.utils.functional import Loaders - -log = logging.getLogger(__name__) - - -@pytest.fixture(scope="package") -def minion_id(): - return "func-tests-minion-opts" - - -@pytest.fixture(scope="module") -def state_tree(tmp_path_factory): - state_tree_path = tmp_path_factory.mktemp("state-tree-base") - try: - yield state_tree_path - finally: - shutil.rmtree(str(state_tree_path), ignore_errors=True) - - -@pytest.fixture(scope="module") -def state_tree_prod(tmp_path_factory): - state_tree_path = tmp_path_factory.mktemp("state-tree-prod") - try: - yield state_tree_path - finally: - shutil.rmtree(str(state_tree_path), ignore_errors=True) - - -@pytest.fixture(scope="module") -def minion_config_defaults(): - """ - Functional test modules can provide this fixture to tweak the default configuration dictionary - passed to the minion factory - """ - return {} - - -@pytest.fixture(scope="module") -def minion_config_overrides(): - """ - Functional test modules can provide this fixture to tweak the configuration - overrides dictionary passed to the minion factory - """ - return {} - - -@pytest.fixture(scope="module") -def minion_opts( - salt_factories, - minion_id, - state_tree, - state_tree_prod, - minion_config_defaults, - minion_config_overrides, -): - minion_config_overrides.update( - { - "file_client": "local", - "file_roots": { - "base": [ - str(state_tree), - ], - "prod": [ - str(state_tree_prod), - ], - }, - } - ) - factory = salt_factories.salt_minion_daemon( - minion_id, - defaults=minion_config_defaults or None, - overrides=minion_config_overrides, - ) - return factory.config.copy() - - -@pytest.fixture(scope="module") -def master_config_defaults(): - """ - Functional test modules can provide this fixture to tweak the default configuration dictionary - passed to the master factory - """ - return {} - - -@pytest.fixture(scope="module") -def master_config_overrides(): - """ - Functional test modules can provide this fixture to tweak the configuration - overrides dictionary passed to the master factory - """ - return {} - - -@pytest.fixture(scope="module") -def master_opts( - salt_factories, - state_tree, - state_tree_prod, - master_config_defaults, - master_config_overrides, -): - master_config_overrides.update( - { - "file_client": "local", - "file_roots": { - "base": [ - str(state_tree), - ], - "prod": [ - str(state_tree_prod), - ], - }, - } - ) - factory = salt_factories.salt_master_daemon( - "func-tests-master-opts", - defaults=master_config_defaults or None, - overrides=master_config_overrides, - ) - return factory.config.copy() - - -@pytest.fixture(scope="module") -def loaders(minion_opts): - return Loaders(minion_opts, loaded_base_name=f"{__name__}.loaded") - - -@pytest.fixture(autouse=True) -def reset_loaders_state(loaders): - try: - # Run the tests - yield - finally: - # Reset the loaders state - loaders.reset_state() diff --git a/tests/pytests/integration/conftest.py b/tests/pytests/integration/conftest.py deleted file mode 100644 index de99d98b..00000000 --- a/tests/pytests/integration/conftest.py +++ /dev/null @@ -1,109 +0,0 @@ -""" - tests.pytests.integration.conftest - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - PyTest fixtures -""" - -import logging - -import pytest - -log = logging.getLogger(__name__) - - -@pytest.fixture(scope="package") -def salt_master(salt_master_factory): - """ - A running salt-master fixture - """ - with salt_master_factory.started(): - yield salt_master_factory - - -@pytest.fixture(scope="package") -def salt_minion(salt_master, salt_minion_factory): - """ - A running salt-minion fixture - """ - assert salt_master.is_running() - with salt_minion_factory.started(): - # Sync All - salt_call_cli = salt_minion_factory.salt_call_cli() - ret = salt_call_cli.run("saltutil.sync_all", _timeout=120) - assert ret.returncode == 0, ret - yield salt_minion_factory - - -@pytest.fixture(scope="module") -def salt_sub_minion(salt_master, salt_sub_minion_factory): - """ - A second running salt-minion fixture - """ - assert salt_master.is_running() - with salt_sub_minion_factory.started(): - # Sync All - salt_call_cli = salt_sub_minion_factory.salt_call_cli() - ret = salt_call_cli.run("saltutil.sync_all", _timeout=120) - assert ret.returncode == 0, ret - yield salt_sub_minion_factory - - -@pytest.fixture(scope="package") -def salt_cli(salt_master): - """ - The ``salt`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_call_cli(salt_minion): - """ - The ``salt-call`` CLI as a fixture against the running minion - """ - assert salt_minion.is_running() - return salt_minion.salt_call_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_cp_cli(salt_master): - """ - The ``salt-cp`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_cp_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_key_cli(salt_master): - """ - The ``salt-key`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_key_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_run_cli(salt_master): - """ - The ``salt-run`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_run_cli(timeout=30) - - -@pytest.fixture(scope="module") -def salt_ssh_cli(salt_master, salt_ssh_roster_file, sshd_config_dir): - """ - The ``salt-ssh`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_ssh_cli( - timeout=180, - roster_file=salt_ssh_roster_file, - target_host="localhost", - client_key=str(sshd_config_dir / "client_key"), - base_script_args=["--ignore-host-keys"], - ) diff --git a/tests/pytests/unit/conftest.py b/tests/pytests/unit/conftest.py deleted file mode 100644 index e19db6cf..00000000 --- a/tests/pytests/unit/conftest.py +++ /dev/null @@ -1,77 +0,0 @@ -import asyncio -import os - -import pytest - -import salt.config -import salt.transport.tcp -from tests.support.mock import MagicMock, patch - - -@pytest.fixture -def minion_opts(tmp_path): - """ - Default minion configuration with relative temporary paths to not require root permissions. - """ - root_dir = tmp_path / "minion" - opts = salt.config.DEFAULT_MINION_OPTS.copy() - opts["__role"] = "minion" - opts["root_dir"] = str(root_dir) - opts["master_uri"] = "tcp://{ip}:{port}".format( - ip="127.0.0.1", port=opts["master_port"] - ) - for name in ("cachedir", "pki_dir", "sock_dir", "conf_dir"): - dirpath = root_dir / name - dirpath.mkdir(parents=True) - opts[name] = str(dirpath) - opts["log_file"] = "logs/minion.log" - opts["conf_file"] = os.path.join(opts["conf_dir"], "minion") - return opts - - -@pytest.fixture -def master_opts(tmp_path): - """ - Default master configuration with relative temporary paths to not require root permissions. - """ - root_dir = tmp_path / "master" - opts = salt.config.master_config(None) - opts["__role"] = "master" - opts["root_dir"] = str(root_dir) - for name in ("cachedir", "pki_dir", "sock_dir", "conf_dir"): - dirpath = root_dir / name - dirpath.mkdir(parents=True) - opts[name] = str(dirpath) - opts["log_file"] = "logs/master.log" - opts["conf_file"] = os.path.join(opts["conf_dir"], "master") - return opts - - -@pytest.fixture -def syndic_opts(tmp_path): - """ - Default master configuration with relative temporary paths to not require root permissions. - """ - root_dir = tmp_path / "syndic" - opts = salt.config.DEFAULT_MINION_OPTS.copy() - opts["syndic_master"] = "127.0.0.1" - opts["__role"] = "minion" - opts["root_dir"] = str(root_dir) - for name in ("cachedir", "pki_dir", "sock_dir", "conf_dir"): - dirpath = root_dir / name - dirpath.mkdir(parents=True) - opts[name] = str(dirpath) - opts["log_file"] = "logs/syndic.log" - opts["conf_file"] = os.path.join(opts["conf_dir"], "syndic") - return opts - - -@pytest.fixture -def mocked_tcp_pub_client(): - transport = MagicMock(spec=salt.transport.tcp.TCPPubClient) - transport.connect = MagicMock() - future = asyncio.Future() - transport.connect.return_value = future - future.set_result(True) - with patch("salt.transport.tcp.TCPPubClient", transport): - yield diff --git a/tests/unit/modules/test_github.py b/tests/unit/modules/test_github.py index 6c113b5f..02fecd24 100644 --- a/tests/unit/modules/test_github.py +++ b/tests/unit/modules/test_github.py @@ -1,6 +1,6 @@ import pytest import salt.modules.test as testmod -import saltext.github.modules.github_mod as github_module +import saltext.github.modules.github as github_module @pytest.fixture @@ -11,8 +11,3 @@ def configure_loader_modules(): return { github_module: module_globals, } - - -def test_replace_this_this_with_something_meaningful(): - echo_str = "Echoed!" - assert github_module.example_function(echo_str) == echo_str diff --git a/tests/unit/states/test_github.py b/tests/unit/states/test_github.py index 1569d8c9..3881c832 100644 --- a/tests/unit/states/test_github.py +++ b/tests/unit/states/test_github.py @@ -1,7 +1,7 @@ import pytest import salt.modules.test as testmod -import saltext.github.modules.github_mod as github_module -import saltext.github.states.github_mod as github_state +import saltext.github.modules.github as github_module +import saltext.github.states.github as github_state @pytest.fixture @@ -14,7 +14,7 @@ def configure_loader_modules(): }, github_state: { "__salt__": { - "github.example_function": github_module.example_function, + # "github.example_function": github_module.example_function, }, }, }