gitcfg: remove unused `*paths` argument in GitConfig.hooks()
[git-cola.git] / test / helper.py
blob100988c17122de52dd5967fa0fe9fd65576e09a3
1 from __future__ import absolute_import, division, unicode_literals
2 import os
3 import shutil
4 import stat
5 import unittest
6 import tempfile
8 import mock
10 from cola import core
11 from cola import git
12 from cola import gitcfg
13 from cola import gitcmds
14 from cola.models import main
17 def tmp_path(*paths):
18 """Returns a path relative to the test/tmp directory"""
19 dirname = core.decode(os.path.dirname(__file__))
20 return os.path.join(dirname, 'tmp', *paths)
23 def fixture(*paths):
24 dirname = core.decode(os.path.dirname(__file__))
25 return os.path.join(dirname, 'fixtures', *paths)
28 def run_unittest(suite):
29 return unittest.TextTestRunner(verbosity=2).run(suite)
32 # shutil.rmtree() can't remove read-only files on Windows. This onerror
33 # handler, adapted from <http://stackoverflow.com/a/1889686/357338>, works
34 # around this by changing such files to be writable and then re-trying.
35 def remove_readonly(func, path, _exc_info):
36 if func is os.remove and not os.access(path, os.W_OK):
37 os.chmod(path, stat.S_IWRITE)
38 func(path)
39 else:
40 raise AssertionError('Should not happen')
43 class TmpPathTestCase(unittest.TestCase):
44 def setUp(self):
45 self._testdir = tempfile.mkdtemp('_cola_test')
46 os.chdir(self._testdir)
48 def tearDown(self):
49 """Remove the test directory and return to the tmp root."""
50 path = self._testdir
51 os.chdir(tmp_path())
52 shutil.rmtree(path, onerror=remove_readonly)
54 @staticmethod
55 def touch(*paths):
56 for path in paths:
57 open(path, 'a').close()
59 @staticmethod
60 def write_file(path, content):
61 with open(path, 'w') as f:
62 f.write(content)
64 @staticmethod
65 def append_file(path, content):
66 with open(path, 'a') as f:
67 f.write(content)
69 def test_path(self, *paths):
70 return os.path.join(self._testdir, *paths)
73 class GitRepositoryTestCase(TmpPathTestCase):
74 """Tests that operate on temporary git repositories."""
76 def setUp(self):
77 TmpPathTestCase.setUp(self)
78 self.initialize_repo()
79 self.context = context = mock.Mock()
80 context.git = git.create()
81 context.git.set_worktree(core.getcwd())
82 context.cfg = gitcfg.create(context)
83 context.model = self.model = main.create(self.context)
84 self.git = context.git
85 self.cfg = context.cfg
86 self.cfg.reset()
87 gitcmds.reset()
89 def run_git(self, *args):
90 status, out, _ = core.run_command(['git'] + list(args))
91 self.assertEqual(status, 0)
92 return out
94 def initialize_repo(self):
95 self.run_git('init')
96 self.run_git('symbolic-ref', 'HEAD', 'refs/heads/main')
97 self.run_git('config', '--local', 'user.name', 'Your Name')
98 self.run_git('config', '--local', 'user.email', 'you@example.com')
99 self.touch('A', 'B')
100 self.run_git('add', 'A', 'B')
102 def commit_files(self):
103 self.run_git('commit', '-m', 'initial commit')