pylint: remove bad-option-value from the configuration
[git-cola.git] / test / helper.py
blob49f35e41b4946f7a4a9449e72b964651eb7dbc90
1 from __future__ import absolute_import, division, print_function, unicode_literals
2 import os
3 import shutil
4 import stat
5 import tempfile
7 from unittest.mock import Mock, patch # noqa pylint: disable=unused-import
9 import pytest
11 from cola import core
12 from cola import git
13 from cola import gitcfg
14 from cola import gitcmds
15 from cola.models import main
18 def tmp_path(*paths):
19 """Returns a path relative to the test/tmp directory"""
20 dirname = core.decode(os.path.dirname(__file__))
21 return os.path.join(dirname, 'tmp', *paths)
24 def fixture(*paths):
25 dirname = core.decode(os.path.dirname(__file__))
26 return os.path.join(dirname, 'fixtures', *paths)
29 # shutil.rmtree() can't remove read-only files on Windows. This onerror
30 # handler, adapted from <http://stackoverflow.com/a/1889686/357338>, works
31 # around this by changing such files to be writable and then re-trying.
32 def remove_readonly(func, path, _exc_info):
33 if func is os.remove and not os.access(path, os.W_OK):
34 os.chmod(path, stat.S_IWRITE)
35 func(path)
36 else:
37 raise AssertionError('Should not happen')
40 def touch(*paths):
41 """Open and close a file to either create it or update its mtime"""
42 for path in paths:
43 core.open_append(path).close()
46 def write_file(path, content):
47 """Write content to the specified file path"""
48 with core.open_write(path) as f:
49 f.write(content)
52 def append_file(path, content):
53 """Open a file in append mode and write content to it"""
54 with core.open_append(path) as f:
55 f.write(content)
58 def run_git(*args):
59 """Run git with the specified arguments"""
60 status, out, _ = core.run_command(['git'] + list(args))
61 assert status == 0
62 return out
65 def commit_files():
66 """Commit the current state as the initial commit"""
67 run_git('commit', '-m', 'initial commit')
70 def initialize_repo():
71 """Initialize a git repository in the current directory"""
72 run_git('init')
73 run_git('symbolic-ref', 'HEAD', 'refs/heads/main')
74 run_git('config', '--local', 'user.name', 'Your Name')
75 run_git('config', '--local', 'user.email', 'you@example.com')
76 run_git('config', '--local', 'commit.gpgsign', 'false')
77 run_git('config', '--local', 'tag.gpgsign', 'false')
78 touch('A', 'B')
79 run_git('add', 'A', 'B')
82 @pytest.fixture
83 def app_context():
84 """Create a repository in a temporary directory and return its ApplicationContext"""
85 tmp_directory = tempfile.mkdtemp('-cola-test')
86 current_directory = os.getcwd()
87 os.chdir(tmp_directory)
89 initialize_repo()
90 context = Mock()
91 context.git = git.create()
92 context.git.set_worktree(core.getcwd())
93 context.cfg = gitcfg.create(context)
94 context.model = main.create(context)
96 context.cfg.reset()
97 gitcmds.reset()
99 yield context
101 os.chdir(current_directory)
102 shutil.rmtree(tmp_directory, onerror=remove_readonly)