6 from unittest
.mock
import Mock
, patch
# noqa pylint: disable=unused-import
12 from cola
import gitcfg
13 from cola
import gitcmds
14 from cola
.models
import main
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
)
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
)
36 raise AssertionError('Should not happen')
40 """Open and close a file to either create it or update its mtime"""
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
:
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
:
58 """Run git with the specified arguments"""
59 status
, out
, _
= core
.run_command(['git'] + list(args
))
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"""
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')
78 run_git('add', 'A', 'B')
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
)
90 context
.git
= git
.create()
91 context
.git
.set_worktree(core
.getcwd())
92 context
.cfg
= gitcfg
.create(context
)
93 context
.model
= main
.create(context
)
100 os
.chdir(current_directory
)
101 shutil
.rmtree(tmp_directory
, onerror
=remove_readonly
)