Skip to content

Commit

Permalink
replace use of kas.run_cmd in tests
Browse files Browse the repository at this point in the history
The kas.run_cmd function internally uses the async variant that requires
an event loop. As a preparation to get rid of the ambient event loop
(fix for Python 3.12), we need to port all external users over
subprocess.

As run_cmd is used quite often in test_refspec we just provide a
replacement implementation. By that we do not disturb the history too
much.

Signed-off-by: Felix Moessbauer <[email protected]>
  • Loading branch information
fmoessbauer committed Nov 13, 2024
1 parent 7d2304e commit dabcd65
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
18 changes: 8 additions & 10 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import subprocess
import pytest
from kas import kas
from kas.libkas import run_cmd
from kas.libkas import TaskExecError, KasUserError
from kas.attestation import file_digest_slow

Expand Down Expand Up @@ -128,22 +127,21 @@ def test_checkout_shallow(monkeykas, tmpdir):
mp.setenv('KAS_CLONE_DEPTH', '1')
kas.kas(['checkout', 'test-shallow.yml'])
for repo in ['kas_1', 'kas_2', 'kas_3', 'kas_4']:
(rc, output) = run_cmd(['git', 'rev-list', '--count', 'HEAD'],
cwd=repo, fail=False, liveupdate=False)
assert rc == 0
output = subprocess.check_output(
['git', 'rev-list', '--count', 'HEAD'], cwd=repo)
count = int(output.decode('utf-8').strip())
if repo == 'kas_4':
assert output.strip() >= '1'
assert count >= 1
else:
assert output.strip() == '1'
assert count == 1


@pytest.mark.online
def test_shallow_updates(monkeykas, tmpdir):
def _get_commit(repo):
(rc, output) = run_cmd(['git', 'rev-parse', '--verify', 'HEAD'],
cwd=repo, fail=False, liveupdate=False)
assert rc == 0
return output.strip()
output = subprocess.check_output(
['git', 'rev-parse', '--verify', 'HEAD'], cwd=repo)
return output.decode('utf-8').strip()

tdir = tmpdir / 'test_commands'
tdir.mkdir()
Expand Down
14 changes: 6 additions & 8 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@
import pytest
import subprocess
from kas import kas
from kas.libkas import run_cmd
from kas.repos import PatchApplyError, PatchFileNotFound, PatchMappingError


def git_get_commit(path):
(rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd=path, fail=False)
assert rc == 0
return output.strip()
output = subprocess.check_output(
['git', 'rev-parse', 'HEAD'], cwd=path)
return output.decode('utf-8').strip()


def mercurial_get_commit(path):
(rc, output) = run_cmd(['hg', 'log', '-r', '.', '--template', '{node}\n'],
cwd=path, fail=False)
assert rc == 0
return output.strip()
output = subprocess.check_output(
['hg', 'log', '-r', '.', '--template', '{node}\n'], cwd=path)
return output.decode('utf-8').strip()


@pytest.mark.online
Expand Down
17 changes: 16 additions & 1 deletion tests/test_refspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@
import pytest
import shutil
import yaml
import subprocess
from kas import kas
from kas.libkas import run_cmd
from kas.repos import RepoRefError, Repo


def run_cmd(cmd, cwd=None, fail=True):
"""
Run a command and return the return code and output.
Replacement for kas internal run_cmd function which
cannot be used outside of kas as there is no event loop.
"""
try:
output = subprocess.check_output(cmd, cwd=cwd)
return (0, output.decode('utf-8'))
except subprocess.CalledProcessError as e:
if fail:
raise e
return (e.returncode, e.output.decode('utf-8'))


@pytest.mark.online
def test_refspec_switch(monkeykas, tmpdir):
"""
Expand Down

0 comments on commit dabcd65

Please sign in to comment.