From 83e6afd55d6b083778c931cfef374b607fb720d5 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sat, 17 Jul 2021 13:17:42 -0700 Subject: [PATCH] tests: use pytest fixtures in browse_model_test Signed-off-by: David Aguilar --- test/browse_model_test.py | 121 ++++++++++++++++++++++++++-------------------- test/helper.py | 112 ++++++++++++++++++++++++++++++------------ 2 files changed, 148 insertions(+), 85 deletions(-) rewrite test/browse_model_test.py (92%) diff --git a/test/browse_model_test.py b/test/browse_model_test.py dissimilarity index 92% index f6b8a5e5..f009a8a7 100644 --- a/test/browse_model_test.py +++ b/test/browse_model_test.py @@ -1,53 +1,68 @@ -"""Covers interfaces used by the browser (git cola browse)""" -from __future__ import absolute_import, division, unicode_literals - -from cola import core -from cola import gitcmds - -from . import helper - - -class ClassicModelTestCase(helper.GitRepositoryTestCase): - """Tests interfaces used by the browser (git cola browse)""" - - def test_stage_paths_untracked(self): - """Test stage_paths() with an untracked file.""" - core.makedirs('foo/bar') - self.touch('foo/bar/baz') - gitcmds.add(self.context, ['foo']) - self.model.update_file_status() - - self.assertTrue('foo/bar/baz' in self.model.staged) - self.assertTrue('foo/bar/baz' not in self.model.modified) - self.assertTrue('foo/bar/baz' not in self.model.untracked) - - def test_unstage_paths(self): - """Test a simple usage of unstage_paths().""" - self.commit_files() - self.write_file('A', 'change') - self.run_git('add', 'A') - gitcmds.unstage_paths(self.context, ['A']) - self.model.update_status() - - self.assertTrue('A' not in self.model.staged) - self.assertTrue('A' in self.model.modified) - - def test_unstage_paths_init(self): - """Test unstage_paths() on the root commit.""" - gitcmds.unstage_paths(self.context, ['A']) - self.model.update_status() - - self.assertTrue('A' not in self.model.staged) - self.assertTrue('A' in self.model.untracked) - - def test_unstage_paths_subdir(self): - """Test unstage_paths() in a subdirectory.""" - self.run_git('commit', '-m', 'initial commit') - core.makedirs('foo/bar') - self.touch('foo/bar/baz') - self.run_git('add', 'foo/bar/baz') - gitcmds.unstage_paths(self.context, ['foo']) - self.model.update_status() - - self.assertTrue('foo/bar/baz' in self.model.untracked) - self.assertTrue('foo/bar/baz' not in self.model.staged) +"""Test interfaces used by the browser (git cola browse)""" +# pylint: disable=redefined-outer-name +from __future__ import absolute_import, division, unicode_literals + +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 + + +def test_stage_paths_untracked(app_context): + """Test stage_paths() with an untracked file.""" + model = app_context.model + core.makedirs('foo/bar') + helper.touch('foo/bar/baz') + gitcmds.add(app_context, ['foo']) + app_context.model.update_file_status() + + assert 'foo/bar/baz' in model.staged + assert 'foo/bar/baz' not in model.modified + assert 'foo/bar/baz' not in model.untracked + + +def test_unstage_paths(app_context): + """Test a simple usage of unstage_paths().""" + helper.commit_files() + helper.write_file('A', 'change') + helper.run_git('add', 'A') + model = app_context.model + + gitcmds.unstage_paths(app_context, ['A']) + model.update_status() + + assert 'A' not in model.staged + assert 'A' in model.modified + + +def test_unstage_paths_init(app_context): + """Test unstage_paths() on the root commit.""" + model = app_context.model + gitcmds.unstage_paths(app_context, ['A']) + model.update_status() + + assert 'A' not in model.staged + assert 'A' in model.untracked + + +def test_unstage_paths_subdir(app_context): + """Test unstage_paths() in a subdirectory.""" + helper.run_git('commit', '-m', 'initial commit') + core.makedirs('foo/bar') + helper.touch('foo/bar/baz') + helper.run_git('add', 'foo/bar/baz') + model = app_context.model + + gitcmds.unstage_paths(app_context, ['foo']) + model.update_status() + + assert 'foo/bar/baz' in model.untracked + assert 'foo/bar/baz' not in model.staged diff --git a/test/helper.py b/test/helper.py index 40b541c4..49076a3a 100644 --- a/test/helper.py +++ b/test/helper.py @@ -6,6 +6,7 @@ import unittest import tempfile import mock +import pytest from cola import core from cola import git @@ -36,6 +37,37 @@ 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: + open(path, 'a').close() + + +def write_file(path, content): + """Write content to the specified file path""" + with open(path, 'w') as f: + f.write(content) + + +def append_file(path, content): + """Open a file in append mode and write content to it""" + with open(path, 'a') as f: + f.write(content) + + class TmpPathTestCase(unittest.TestCase): """Run operations in a temporary directory""" @@ -49,23 +81,53 @@ class TmpPathTestCase(unittest.TestCase): os.chdir(tmp_path()) shutil.rmtree(path, onerror=remove_readonly) - @staticmethod - def touch(*paths): - for path in paths: - open(path, 'a').close() + def test_path(self, *paths): + return os.path.join(self._testdir, *paths) + + append_file = staticmethod(append_file) - @staticmethod - def write_file(path, content): - with open(path, 'w') as f: - f.write(content) + touch = staticmethod(touch) - @staticmethod - def append_file(path, content): - with open(path, 'a') as f: - f.write(content) + write_file = staticmethod(write_file) - def test_path(self, *paths): - return os.path.join(self._testdir, *paths) + +def run_git(*args): + """Run git with the specified arguments""" + status, out, _ = core.run_command(['git'] + list(args)) + assert status == 0 + return out + + +def commit_files(): + """Commit the current state as the initial commit""" + run_git('commit', '-m', 'initial commit') + + +def initialize_repo(): + """Initialize a git repository in the current directory""" + run_git('init') + run_git('symbolic-ref', 'HEAD', 'refs/heads/main') + run_git('config', '--local', 'user.name', 'Your Name') + run_git('config', '--local', 'user.email', 'you@example.com') + run_git('config', '--local', 'commit.gpgsign', 'false') + run_git('config', '--local', 'tag.gpgsign', 'false') + touch('A', 'B') + run_git('add', 'A', 'B') + + +@pytest.fixture +def app_context(run_in_tmpdir): # pylint: disable=redefined-outer-name,unused-argument + """Create a repository in a temporary directory and return its ApplicationContext""" + initialize_repo() + context = mock.Mock() + context.git = git.create() + context.git.set_worktree(core.getcwd()) + context.cfg = gitcfg.create(context) + context.model = main.create(context) + + context.cfg.reset() + gitcmds.reset() + return context class GitRepositoryTestCase(TmpPathTestCase): @@ -73,7 +135,7 @@ class GitRepositoryTestCase(TmpPathTestCase): def setUp(self): TmpPathTestCase.setUp(self) - self.initialize_repo() + initialize_repo() self.context = context = mock.Mock() context.git = git.create() context.git.set_worktree(core.getcwd()) @@ -84,20 +146,6 @@ class GitRepositoryTestCase(TmpPathTestCase): self.cfg.reset() gitcmds.reset() - def run_git(self, *args): - status, out, _ = core.run_command(['git'] + list(args)) - self.assertEqual(status, 0) - return out - - def initialize_repo(self): - self.run_git('init') - self.run_git('symbolic-ref', 'HEAD', 'refs/heads/main') - self.run_git('config', '--local', 'user.name', 'Your Name') - self.run_git('config', '--local', 'user.email', 'you@example.com') - self.run_git('config', '--local', 'commit.gpgsign', 'false') - self.run_git('config', '--local', 'tag.gpgsign', 'false') - self.touch('A', 'B') - self.run_git('add', 'A', 'B') - - def commit_files(self): - self.run_git('commit', '-m', 'initial commit') + commit_files = staticmethod(commit_files) + + run_git = staticmethod(run_git) -- 2.11.4.GIT