1 from __future__
import absolute_import
, division
, print_function
, unicode_literals
9 from unittest
.mock
import Mock
, patch
# noqa pylint: disable=unused-import
11 from mock
import Mock
, patch
# noqa pylint: disable=unused-import
15 from cola
import gitcfg
16 from cola
import gitcmds
17 from cola
.models
import main
21 """Returns a path relative to the test/tmp directory"""
22 dirname
= core
.decode(os
.path
.dirname(__file__
))
23 return os
.path
.join(dirname
, 'tmp', *paths
)
27 dirname
= core
.decode(os
.path
.dirname(__file__
))
28 return os
.path
.join(dirname
, 'fixtures', *paths
)
31 # shutil.rmtree() can't remove read-only files on Windows. This onerror
32 # handler, adapted from <http://stackoverflow.com/a/1889686/357338>, works
33 # around this by changing such files to be writable and then re-trying.
34 def remove_readonly(func
, path
, _exc_info
):
35 if func
is os
.remove
and not os
.access(path
, os
.W_OK
):
36 os
.chmod(path
, stat
.S_IWRITE
)
39 raise AssertionError('Should not happen')
43 """Open and close a file to either create it or update its mtime"""
45 open(path
, 'a').close()
48 def write_file(path
, content
):
49 """Write content to the specified file path"""
50 with
open(path
, 'w') as f
:
54 def append_file(path
, content
):
55 """Open a file in append mode and write content to it"""
56 with
open(path
, 'a') as f
:
61 """Run git with the specified arguments"""
62 status
, out
, _
= core
.run_command(['git'] + list(args
))
68 """Commit the current state as the initial commit"""
69 run_git('commit', '-m', 'initial commit')
72 def initialize_repo():
73 """Initialize a git repository in the current directory"""
75 run_git('symbolic-ref', 'HEAD', 'refs/heads/main')
76 run_git('config', '--local', 'user.name', 'Your Name')
77 run_git('config', '--local', 'user.email', 'you@example.com')
78 run_git('config', '--local', 'commit.gpgsign', 'false')
79 run_git('config', '--local', 'tag.gpgsign', 'false')
81 run_git('add', 'A', 'B')
86 """Create a repository in a temporary directory and return its ApplicationContext"""
87 tmp_directory
= tempfile
.mkdtemp('-cola-test')
88 current_directory
= os
.getcwd()
89 os
.chdir(tmp_directory
)
93 context
.git
= git
.create()
94 context
.git
.set_worktree(core
.getcwd())
95 context
.cfg
= gitcfg
.create(context
)
96 context
.model
= main
.create(context
)
103 os
.chdir(current_directory
)
104 shutil
.rmtree(tmp_directory
, onerror
=remove_readonly
)