1 from __future__
import absolute_import
, division
, print_function
, unicode_literals
7 from unittest
.mock
import Mock
, patch
# noqa pylint: disable=unused-import
13 from cola
import gitcfg
14 from cola
import gitcmds
15 from cola
.models
import main
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
)
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
)
37 raise AssertionError('Should not happen')
41 """Open and close a file to either create it or update its mtime"""
43 open(path
, 'a').close()
46 def write_file(path
, content
):
47 """Write content to the specified file path"""
48 with
open(path
, 'w') as f
:
52 def append_file(path
, content
):
53 """Open a file in append mode and write content to it"""
54 with
open(path
, 'a') as f
:
59 """Run git with the specified arguments"""
60 status
, out
, _
= core
.run_command(['git'] + list(args
))
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"""
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')
79 run_git('add', 'A', 'B')
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
)
91 context
.git
= git
.create()
92 context
.git
.set_worktree(core
.getcwd())
93 context
.cfg
= gitcfg
.create(context
)
94 context
.model
= main
.create(context
)
101 os
.chdir(current_directory
)
102 shutil
.rmtree(tmp_directory
, onerror
=remove_readonly
)