Skip to content

Commit

Permalink
Rename Runtime.exec to Runtime.run (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed May 1, 2023
1 parent 26f70be commit ff385f6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ ignore = [
"E501", # we use black
"RET504", # Unnecessary variable assignment before `return` statement
# Temporary disabled during adoption:
"A003", # Class attribute `exec` is shadowing a python builtin
"S607", # Starting a process with a partial executable path

]
Expand Down
12 changes: 6 additions & 6 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def clean(self) -> None:
if self.cache_dir:
shutil.rmtree(self.cache_dir, ignore_errors=True)

def exec(
def run(
self,
args: Union[str, list[str]],
*,
Expand Down Expand Up @@ -225,7 +225,7 @@ def version(self) -> packaging.version.Version:
if self._version:
return self._version

proc = self.exec(["ansible", "--version"])
proc = self.run(["ansible", "--version"])
if proc.returncode == 0:
self._version = parse_ansible_version(proc.stdout)
return self._version
Expand Down Expand Up @@ -284,7 +284,7 @@ def install_collection(
cmd.append(f"{collection}")

_logger.info("Running from %s : %s", Path.cwd(), " ".join(cmd))
run = self.exec(
run = self.run(
cmd,
retry=True,
env={**self.environ, ansible_collections_path(): ":".join(cpaths)},
Expand Down Expand Up @@ -314,7 +314,7 @@ def install_collection_from_disk(
str(path),
]
_logger.info("Running %s", " ".join(cmd))
run = self.exec(cmd, retry=False)
run = self.run(cmd, retry=False)
if run.returncode != 0:
_logger.error(run.stdout)
raise AnsibleCommandError(run)
Expand Down Expand Up @@ -364,7 +364,7 @@ def install_requirements( # noqa: C901,PLR0912
else:
_logger.info("Running %s", " ".join(cmd))

result = self.exec(cmd, retry=retry)
result = self.run(cmd, retry=retry)
if result.returncode != 0:
_logger.error(result.stdout)
raise AnsibleCommandError(result)
Expand Down Expand Up @@ -392,7 +392,7 @@ def install_requirements( # noqa: C901,PLR0912
# pylint: disable=no-member
cpaths.insert(0, dest_path)
_logger.info("Running %s", " ".join(cmd))
result = self.exec(
result = self.run(
cmd,
retry=retry,
env={**os.environ, "ANSIBLE_COLLECTIONS_PATH": ":".join(cpaths)},
Expand Down
22 changes: 11 additions & 11 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_runtime_version_fail_module(mocker: MockerFixture) -> None:
def test_runtime_version_fail_cli(mocker: MockerFixture) -> None:
"""Tests for failure to detect Ansible version."""
mocker.patch(
"ansible_compat.runtime.Runtime.exec",
"ansible_compat.runtime.Runtime.run",
return_value=CompletedProcess(
["x"],
returncode=123,
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_runtime_install_role(
runtime = Runtime(isolated=isolated, project_dir=project_dir)
runtime.prepare_environment(install_local=True)
# check that role appears as installed now
result = runtime.exec(["ansible-galaxy", "list"])
result = runtime.run(["ansible-galaxy", "list"])
assert result.returncode == 0, result
assert role_name in result.stdout
if isolated:
Expand Down Expand Up @@ -573,7 +573,7 @@ def test_install_galaxy_role_no_checks(runtime_tmp: Runtime) -> None:
""",
)
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=2)
result = runtime_tmp.exec(["ansible-galaxy", "list"])
result = runtime_tmp.run(["ansible-galaxy", "list"])
assert "- acme.foo," in result.stdout
assert result.returncode == 0, result

Expand Down Expand Up @@ -658,7 +658,7 @@ def test_install_collection_from_disk(path: str, scenario: str) -> None:
runtime.prepare_environment(install_local=True)
# that molecule converge playbook can be used without molecule and
# should validate that the installed collection is available.
result = runtime.exec(["ansible-playbook", f"molecule/{scenario}/converge.yml"])
result = runtime.run(["ansible-playbook", f"molecule/{scenario}/converge.yml"])
assert result.returncode == 0, result.stdout
runtime.clean()

Expand Down Expand Up @@ -691,8 +691,8 @@ def test_prepare_environment_offline_role() -> None:

def test_runtime_run(runtime: Runtime) -> None:
"""Check if tee and non tee mode return same kind of results."""
result1 = runtime.exec(["seq", "10"])
result2 = runtime.exec(["seq", "10"], tee=True)
result1 = runtime.run(["seq", "10"])
result2 = runtime.run(["seq", "10"], tee=True)
assert result1.returncode == result2.returncode
assert result1.stderr == result2.stderr
assert result1.stdout == result2.stdout
Expand All @@ -701,20 +701,20 @@ def test_runtime_run(runtime: Runtime) -> None:
def test_runtime_exec_cwd(runtime: Runtime) -> None:
"""Check if passing cwd works as expected."""
path = Path("/")
result1 = runtime.exec(["pwd"], cwd=path)
result2 = runtime.exec(["pwd"])
result1 = runtime.run(["pwd"], cwd=path)
result2 = runtime.run(["pwd"])
assert result1.stdout.rstrip() == str(path)
assert result1.stdout != result2.stdout


def test_runtime_exec_env(runtime: Runtime) -> None:
"""Check if passing env works."""
result = runtime.exec(["printenv", "FOO"])
result = runtime.run(["printenv", "FOO"])
assert not result.stdout

result = runtime.exec(["printenv", "FOO"], env={"FOO": "bar"})
result = runtime.run(["printenv", "FOO"], env={"FOO": "bar"})
assert result.stdout.rstrip() == "bar"

runtime.environ["FOO"] = "bar"
result = runtime.exec(["printenv", "FOO"])
result = runtime.run(["printenv", "FOO"])
assert result.stdout.rstrip() == "bar"
2 changes: 1 addition & 1 deletion test/test_runtime_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def test_runtime_example() -> None:
runtime.install_collection("examples/reqs_v2/community-molecule-0.1.0.tar.gz")

# Execute a command
result = runtime.exec(["ansible-doc", "--list"])
result = runtime.run(["ansible-doc", "--list"])
assert result.returncode == 0

0 comments on commit ff385f6

Please sign in to comment.