Use fixture to wrap temporary directories in local e2e tests
@@ -8,12 +8,18 @@ from tempfile import mkdtemp
import pytest
-@pytest.mark.int
-@pytest.mark.local
-def test_int_local_file_no_changes(helpers):
+@pytest.fixture()
+def temp_dir():
temp_dir = mkdtemp()
-
try:
+ yield temp_dir
+ finally:
+ rmtree(temp_dir)
+
+
+@pytest.mark.int
+@pytest.mark.local
+def test_int_local_file_no_changes(helpers, temp_dir):
helpers.run_check_output( # first run = create the file
'pyinfra -v @local files.file _testfile',
expected_lines=['@local] Success'],
@@ -25,16 +31,11 @@ def test_int_local_file_no_changes(helpers):
expected_lines=['@local] No changes'],
cwd=temp_dir,
)
- finally:
- rmtree(temp_dir)
@pytest.mark.int
@pytest.mark.local
-def test_int_local_directory_no_changes(helpers):
- temp_dir = mkdtemp()
-
- try:
+def test_int_local_directory_no_changes(helpers, temp_dir):
helpers.run_check_output( # first run = create the directory
'pyinfra -v @local files.directory _testdir',
expected_lines=['@local] Success'],
@@ -58,16 +59,11 @@ def test_int_local_directory_no_changes(helpers):
expected_lines=['@local] No changes'],
cwd=temp_dir,
)
- finally:
- rmtree(temp_dir)
@pytest.mark.int
@pytest.mark.local
-def test_int_local_link_no_changes(helpers):
- temp_dir = mkdtemp()
-
- try:
+def test_int_local_link_no_changes(helpers, temp_dir):
helpers.run_check_output( # first run = create the link
'pyinfra -v @local files.link _testlink target=_testfile',
expected_lines=['@local] Success'],
@@ -79,16 +75,11 @@ def test_int_local_link_no_changes(helpers):
expected_lines=['@local] No changes'],
cwd=temp_dir,
)
- finally:
- rmtree(temp_dir)
@pytest.mark.int
@pytest.mark.local
-def test_int_local_line_no_changes(helpers):
- temp_dir = mkdtemp()
-
- try:
+def test_int_local_line_no_changes(helpers, temp_dir):
helpers.run_check_output( # first run = create the line
'pyinfra -v @local files.line _testfile someline',
expected_lines=['@local] Success'],
@@ -112,8 +103,6 @@ def test_int_local_line_no_changes(helpers):
expected_lines=['@local] No changes'],
cwd=temp_dir,
)
- finally:
- rmtree(temp_dir)
@pytest.mark.int
