From 0672592768cfae49552f2efed49b4926d97abb7e Mon Sep 17 00:00:00 2001 From: Henning Schild Date: Thu, 2 Jul 2020 10:35:31 +0200 Subject: [PATCH] tests: introduce fixture to change back to right dir Tests changing the working directory could leave us stranded somewhere where all following tests could fail. Make sure to always go back on failure. Signed-off-by: Henning Schild Signed-off-by: Jan Kiszka --- tests/test_patch.py | 16 +++++++++------- tests/test_refspec.py | 13 ++++++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/test_patch.py b/tests/test_patch.py index 0eee295..3f735a1 100644 --- a/tests/test_patch.py +++ b/tests/test_patch.py @@ -25,23 +25,27 @@ import stat import shutil from kas import kas -import pytest # noqa: F401; flake8 'pytest' imported but unused +import pytest -def test_patch(tmpdir): +@pytest.fixture +def changedir(): + yield + os.chdir(os.path.join(os.path.dirname(__file__), '..')) + + +def test_patch(changedir, tmpdir): tdir = str(tmpdir.mkdir('test_patch')) shutil.rmtree(tdir, ignore_errors=True) shutil.copytree('tests/test_patch', tdir) - prev_dir = os.path.realpath(os.getcwd()) os.chdir(tdir) kas.kas(['shell', 'test.yml', '-c', 'true']) for f in ['kas/tests/test_patch/hello.sh', 'hello/hello.sh']: assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR kas.kas(['shell', 'test.yml', '-c', 'true']) - os.chdir(prev_dir) -def test_patch_update(tmpdir): +def test_patch_update(changedir, tmpdir): """ Test that patches are applied correctly after switching refspec from a branch to a commit hash and vice-versa with both git and mercurial @@ -51,10 +55,8 @@ def test_patch_update(tmpdir): shutil.rmtree(tdir, ignore_errors=True) print(os.getcwd()) shutil.copytree('tests/test_patch', tdir) - prev_dir = os.path.realpath(os.getcwd()) os.chdir(tdir) kas.kas(['shell', 'test.yml', '-c', 'true']) kas.kas(['shell', 'test2.yml', '-c', 'true']) for f in ['kas/tests/test_patch/hello.sh', 'hello/hello.sh']: assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR - os.chdir(prev_dir) diff --git a/tests/test_refspec.py b/tests/test_refspec.py index 28dc577..0157d86 100644 --- a/tests/test_refspec.py +++ b/tests/test_refspec.py @@ -25,10 +25,16 @@ import shutil from kas import kas from kas.libkas import run_cmd -import pytest # noqa: F401; flake8 'pytest' imported but unused +import pytest -def test_refspec_switch(tmpdir): +@pytest.fixture +def changedir(): + yield + os.chdir(os.path.join(os.path.dirname(__file__), '..')) + + +def test_refspec_switch(changedir, tmpdir): """ Test that the local git clone is correctly updated when switching between a commit hash refspec and a branch refspec. @@ -36,7 +42,6 @@ def test_refspec_switch(tmpdir): tdir = str(tmpdir.mkdir('test_refspec_switch')) shutil.rmtree(tdir, ignore_errors=True) shutil.copytree('tests/test_refspec', tdir) - prev_dir = os.path.realpath(os.getcwd()) os.chdir(tdir) kas.kas(['shell', 'test.yml', '-c', 'true']) @@ -66,5 +71,3 @@ def test_refspec_switch(tmpdir): fail=False, liveupdate=False) assert rc == 0 assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77' - - os.chdir(prev_dir)