maint: code formatting
[git-cola.git] / test / helper.py
blob5db35e8a617aa455afc8addf612112765a3a6b0b
1 from __future__ import absolute_import, division, print_function, unicode_literals
2 import os
3 import shutil
4 import stat
5 import tempfile
7 import pytest
9 try:
10 from unittest.mock import Mock, patch # noqa pylint: disable=unused-import
11 except ImportError:
12 from mock import Mock, patch # noqa pylint: disable=unused-import
14 from cola import core
15 from cola import git
16 from cola import gitcfg
17 from cola import gitcmds
18 from cola.models import main
21 def tmp_path(*paths):
22 """Returns a path relative to the test/tmp directory"""
23 dirname = core.decode(os.path.dirname(__file__))
24 return os.path.join(dirname, 'tmp', *paths)
27 def fixture(*paths):
28 dirname = core.decode(os.path.dirname(__file__))
29 return os.path.join(dirname, 'fixtures', *paths)
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 def touch(*paths):
44 """Open and close a file to either create it or update its mtime"""
45 for path in paths:
46 open(path, 'a').close()
49 def write_file(path, content):
50 """Write content to the specified file path"""
51 with open(path, 'w') as f:
52 f.write(content)
55 def append_file(path, content):
56 """Open a file in append mode and write content to it"""
57 with open(path, 'a') as f:
58 f.write(content)
61 def run_git(*args):
62 """Run git with the specified arguments"""
63 status, out, _ = core.run_command(['git'] + list(args))
64 assert status == 0
65 return out
68 def commit_files():
69 """Commit the current state as the initial commit"""
70 run_git('commit', '-m', 'initial commit')
73 def initialize_repo():
74 """Initialize a git repository in the current directory"""
75 run_git('init')
76 run_git('symbolic-ref', 'HEAD', 'refs/heads/main')
77 run_git('config', '--local', 'user.name', 'Your Name')
78 run_git('config', '--local', 'user.email', 'you@example.com')
79 run_git('config', '--local', 'commit.gpgsign', 'false')
80 run_git('config', '--local', 'tag.gpgsign', 'false')
81 touch('A', 'B')
82 run_git('add', 'A', 'B')
85 @pytest.fixture
86 def app_context():
87 """Create a repository in a temporary directory and return its ApplicationContext"""
88 tmp_directory = tempfile.mkdtemp('-cola-test')
89 current_directory = os.getcwd()
90 os.chdir(tmp_directory)
92 initialize_repo()
93 context = Mock()
94 context.git = git.create()
95 context.git.set_worktree(core.getcwd())
96 context.cfg = gitcfg.create(context)
97 context.model = main.create(context)
99 context.cfg.reset()
100 gitcmds.reset()
102 yield context
104 os.chdir(current_directory)
105 shutil.rmtree(tmp_directory, onerror=remove_readonly)