sequenceeditor: use qtutils.SimpleTask instead of a raw Thread
[git-cola.git] / test / helper.py
blob2e722ffb87db9828de4a8c7fbe886f55c557978b
1 import os
2 import shutil
3 import stat
4 import tempfile
6 from unittest.mock import Mock, patch # noqa pylint: disable=unused-import
8 import pytest
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 # shutil.rmtree() can't remove read-only files on Windows. This onerror
29 # handler, adapted from <http://stackoverflow.com/a/1889686/357338>, works
30 # around this by changing such files to be writable and then re-trying.
31 def remove_readonly(func, path, _exc_info):
32 if func is os.unlink and not os.access(path, os.W_OK):
33 os.chmod(path, stat.S_IWRITE)
34 func(path)
35 else:
36 raise AssertionError('Should not happen')
39 def touch(*paths):
40 """Open and close a file to either create it or update its mtime"""
41 for path in paths:
42 core.open_append(path).close()
45 def write_file(path, content):
46 """Write content to the specified file path"""
47 with core.open_write(path) as f:
48 f.write(content)
51 def append_file(path, content):
52 """Open a file in append mode and write content to it"""
53 with core.open_append(path) as f:
54 f.write(content)
57 def run_git(*args):
58 """Run git with the specified arguments"""
59 status, out, _ = core.run_command(['git'] + list(args))
60 assert status == 0
61 return out
64 def commit_files():
65 """Commit the current state as the initial commit"""
66 run_git('commit', '-m', 'initial commit')
69 def initialize_repo():
70 """Initialize a git repository in the current directory"""
71 run_git('init')
72 run_git('symbolic-ref', 'HEAD', 'refs/heads/main')
73 run_git('config', '--local', 'user.name', 'Your Name')
74 run_git('config', '--local', 'user.email', 'you@example.com')
75 run_git('config', '--local', 'commit.gpgsign', 'false')
76 run_git('config', '--local', 'tag.gpgsign', 'false')
77 touch('A', 'B')
78 run_git('add', 'A', 'B')
81 @pytest.fixture
82 def app_context():
83 """Create a repository in a temporary directory and return its ApplicationContext"""
84 tmp_directory = tempfile.mkdtemp('-cola-test')
85 current_directory = os.getcwd()
86 os.chdir(tmp_directory)
88 initialize_repo()
89 context = Mock()
90 context.git = git.create()
91 context.git.set_worktree(core.getcwd())
92 context.cfg = gitcfg.create(context)
93 context.model = main.create(context)
95 context.cfg.reset()
96 gitcmds.reset()
98 yield context
100 os.chdir(current_directory)
101 shutil.rmtree(tmp_directory, onerror=remove_readonly)