From ef4bfa3bac8025197503a7cdde27c2b556bc0b76 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sat, 17 Jul 2021 18:27:18 -0700 Subject: [PATCH] tests: combine the run_in_tmpdir and app_context fixtures Nothing uses run_in_tmpdir directly so combine the functionality for simplicity. This removes some unused-import cruft and generlaly cleans up the test suite. Signed-off-by: David Aguilar --- test/browse_model_test.py | 3 --- test/dag_test.py | 3 --- test/gitcfg_test.py | 3 --- test/gitcmds_test.py | 3 --- test/gitops_test.py | 3 --- test/helper.py | 25 ++++++++++--------------- test/main_model_test.py | 3 --- 7 files changed, 10 insertions(+), 33 deletions(-) diff --git a/test/browse_model_test.py b/test/browse_model_test.py index f009a8a7..7580f733 100644 --- a/test/browse_model_test.py +++ b/test/browse_model_test.py @@ -6,13 +6,10 @@ from cola import core from cola import gitcmds from . import helper -# NOTE: run_in_tmpdir is required by pytest even though it is only used indirectly. -from .helper import run_in_tmpdir from .helper import app_context # These assertions make flake8 happy. It considers them unused imports otherwise. -assert run_in_tmpdir is not None assert app_context is not None diff --git a/test/dag_test.py b/test/dag_test.py index f9cdcd94..3f5b2e34 100644 --- a/test/dag_test.py +++ b/test/dag_test.py @@ -6,13 +6,10 @@ import pytest from cola.models import dag -# NOTE: run_in_tmpdir is required by pytest even though it is only used indirectly. -from .helper import run_in_tmpdir from .helper import app_context from .helper import patch -assert run_in_tmpdir is not None assert app_context is not None diff --git a/test/gitcfg_test.py b/test/gitcfg_test.py index ec467a63..3d194b4c 100644 --- a/test/gitcfg_test.py +++ b/test/gitcfg_test.py @@ -3,13 +3,10 @@ from __future__ import absolute_import, division, unicode_literals from . import helper -# NOTE: run_in_tmpdir is required by pytest even though it is only used indirectly. -from .helper import run_in_tmpdir from .helper import app_context # These assertions make flake8 happy. It considers them unused imports otherwise. -assert run_in_tmpdir is not None assert app_context is not None diff --git a/test/gitcmds_test.py b/test/gitcmds_test.py index 3ba90273..220828c3 100644 --- a/test/gitcmds_test.py +++ b/test/gitcmds_test.py @@ -7,13 +7,10 @@ from cola import gitcmds from cola.widgets.remote import get_default_remote from . import helper -# NOTE: run_in_tmpdir is required by pytest even though it is only used indirectly. -from .helper import run_in_tmpdir from .helper import app_context # These assertions make flake8 happy. It considers them unused imports otherwise. -assert run_in_tmpdir is not None assert app_context is not None diff --git a/test/gitops_test.py b/test/gitops_test.py index 7996a44a..39cbe1ef 100644 --- a/test/gitops_test.py +++ b/test/gitops_test.py @@ -3,13 +3,10 @@ from __future__ import absolute_import, division, unicode_literals from . import helper -# NOTE: run_in_tmpdir is required by pytest even though it is only used indirectly. -from .helper import run_in_tmpdir from .helper import app_context # These assertions make flake8 happy. It considers them unused imports otherwise. -assert run_in_tmpdir is not None assert app_context is not None diff --git a/test/helper.py b/test/helper.py index 555fd011..6f1c5405 100644 --- a/test/helper.py +++ b/test/helper.py @@ -39,19 +39,6 @@ def remove_readonly(func, path, _exc_info): raise AssertionError('Should not happen') -@pytest.fixture -def run_in_tmpdir(): - """Run tests in a temporary directory and yield the tmp directory""" - tmp_directory = tempfile.mkdtemp('-cola-test') - current_directory = os.getcwd() - os.chdir(tmp_directory) - - yield tmp_directory - - os.chdir(current_directory) - shutil.rmtree(tmp_directory, onerror=remove_readonly) - - def touch(*paths): """Open and close a file to either create it or update its mtime""" for path in paths: @@ -95,8 +82,12 @@ def initialize_repo(): @pytest.fixture -def app_context(run_in_tmpdir): # pylint: disable=redefined-outer-name,unused-argument +def app_context(): """Create a repository in a temporary directory and return its ApplicationContext""" + tmp_directory = tempfile.mkdtemp('-cola-test') + current_directory = os.getcwd() + os.chdir(tmp_directory) + initialize_repo() context = Mock() context.git = git.create() @@ -106,4 +97,8 @@ def app_context(run_in_tmpdir): # pylint: disable=redefined-outer-name,unused-a context.cfg.reset() gitcmds.reset() - return context + + yield context + + os.chdir(current_directory) + shutil.rmtree(tmp_directory, onerror=remove_readonly) diff --git a/test/main_model_test.py b/test/main_model_test.py index bc57d334..2c441f9d 100644 --- a/test/main_model_test.py +++ b/test/main_model_test.py @@ -9,14 +9,11 @@ from cola import git from cola.models import main from . import helper -# NOTE: run_in_tmpdir is required by pytest even though it is only used indirectly. -from .helper import run_in_tmpdir from .helper import app_context from .helper import Mock # These assertions make flake8 happy. It considers them unused imports otherwise. -assert run_in_tmpdir is not None assert app_context is not None REMOTE = 'server' -- 2.11.4.GIT