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 <henning.schild@siemens.com> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
		
				
					committed by
					
						
						Jan Kiszka
					
				
			
			
				
	
			
			
			
						parent
						
							b107a60118
						
					
				
				
					commit
					0672592768
				
			@@ -25,23 +25,27 @@ import stat
 | 
				
			|||||||
import shutil
 | 
					import shutil
 | 
				
			||||||
from kas import kas
 | 
					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'))
 | 
					    tdir = str(tmpdir.mkdir('test_patch'))
 | 
				
			||||||
    shutil.rmtree(tdir, ignore_errors=True)
 | 
					    shutil.rmtree(tdir, ignore_errors=True)
 | 
				
			||||||
    shutil.copytree('tests/test_patch', tdir)
 | 
					    shutil.copytree('tests/test_patch', tdir)
 | 
				
			||||||
    prev_dir = os.path.realpath(os.getcwd())
 | 
					 | 
				
			||||||
    os.chdir(tdir)
 | 
					    os.chdir(tdir)
 | 
				
			||||||
    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
					    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
				
			||||||
    for f in ['kas/tests/test_patch/hello.sh', 'hello/hello.sh']:
 | 
					    for f in ['kas/tests/test_patch/hello.sh', 'hello/hello.sh']:
 | 
				
			||||||
        assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR
 | 
					        assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR
 | 
				
			||||||
    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
					    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
 | 
					        Test that patches are applied correctly after switching refspec from
 | 
				
			||||||
        a branch to a commit hash and vice-versa with both git and mercurial
 | 
					        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)
 | 
					    shutil.rmtree(tdir, ignore_errors=True)
 | 
				
			||||||
    print(os.getcwd())
 | 
					    print(os.getcwd())
 | 
				
			||||||
    shutil.copytree('tests/test_patch', tdir)
 | 
					    shutil.copytree('tests/test_patch', tdir)
 | 
				
			||||||
    prev_dir = os.path.realpath(os.getcwd())
 | 
					 | 
				
			||||||
    os.chdir(tdir)
 | 
					    os.chdir(tdir)
 | 
				
			||||||
    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
					    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
				
			||||||
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
 | 
					    kas.kas(['shell', 'test2.yml', '-c', 'true'])
 | 
				
			||||||
    for f in ['kas/tests/test_patch/hello.sh', 'hello/hello.sh']:
 | 
					    for f in ['kas/tests/test_patch/hello.sh', 'hello/hello.sh']:
 | 
				
			||||||
        assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR
 | 
					        assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR
 | 
				
			||||||
    os.chdir(prev_dir)
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,10 +25,16 @@ import shutil
 | 
				
			|||||||
from kas import kas
 | 
					from kas import kas
 | 
				
			||||||
from kas.libkas import run_cmd
 | 
					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
 | 
					        Test that the local git clone is correctly updated when switching
 | 
				
			||||||
        between a commit hash refspec and a branch refspec.
 | 
					        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'))
 | 
					    tdir = str(tmpdir.mkdir('test_refspec_switch'))
 | 
				
			||||||
    shutil.rmtree(tdir, ignore_errors=True)
 | 
					    shutil.rmtree(tdir, ignore_errors=True)
 | 
				
			||||||
    shutil.copytree('tests/test_refspec', tdir)
 | 
					    shutil.copytree('tests/test_refspec', tdir)
 | 
				
			||||||
    prev_dir = os.path.realpath(os.getcwd())
 | 
					 | 
				
			||||||
    os.chdir(tdir)
 | 
					    os.chdir(tdir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
					    kas.kas(['shell', 'test.yml', '-c', 'true'])
 | 
				
			||||||
@@ -66,5 +71,3 @@ def test_refspec_switch(tmpdir):
 | 
				
			|||||||
                           fail=False, liveupdate=False)
 | 
					                           fail=False, liveupdate=False)
 | 
				
			||||||
    assert rc == 0
 | 
					    assert rc == 0
 | 
				
			||||||
    assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
 | 
					    assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
 | 
				
			||||||
 | 
					 | 
				
			||||||
    os.chdir(prev_dir)
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user