1 from __future__
import absolute_import
, division
, print_function
, unicode_literals
10 from unittest
.mock
import Mock
, patch
# noqa pylint: disable=unused-import
12 from mock
import Mock
, patch
# noqa pylint: disable=unused-import
16 from cola
import gitcfg
17 from cola
import gitcmds
18 from cola
.models
import main
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
)
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
)
40 raise AssertionError('Should not happen')
44 """Open and close a file to either create it or update its mtime"""
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
:
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
:
62 """Run git with the specified arguments"""
63 status
, out
, _
= core
.run_command(['git'] + list(args
))
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"""
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')
82 run_git('add', 'A', 'B')
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
)
94 context
.git
= git
.create()
95 context
.git
.set_worktree(core
.getcwd())
96 context
.cfg
= gitcfg
.create(context
)
97 context
.model
= main
.create(context
)
104 os
.chdir(current_directory
)
105 shutil
.rmtree(tmp_directory
, onerror
=remove_readonly
)