1 from __future__
import absolute_import
, division
, unicode_literals
12 from cola
import gitcfg
13 from cola
import gitcmds
17 """Returns a path relative to the test/tmp directory"""
18 return os
.path
.join(os
.path
.dirname(__file__
), 'tmp', *paths
)
22 return os
.path
.join(os
.path
.dirname(__file__
), 'fixtures', *paths
)
25 def run_unittest(suite
):
26 return unittest
.TextTestRunner(verbosity
=2).run(suite
)
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
== os
.remove
and not os
.access(path
, os
.W_OK
):
34 os
.chmod(path
, stat
.S_IWRITE
)
40 class TmpPathTestCase(unittest
.TestCase
):
43 self
._testdir
= tempfile
.mkdtemp('_cola_test')
44 os
.chdir(self
._testdir
)
47 """Remove the test directory and return to the tmp root."""
50 shutil
.rmtree(path
, onerror
=remove_readonly
)
55 open(path
, 'a').close()
58 def write_file(path
, content
):
59 with
open(path
, 'w') as f
:
63 def append_file(path
, content
):
64 with
open(path
, 'a') as f
:
67 def test_path(self
, *paths
):
68 return os
.path
.join(self
._testdir
, *paths
)
71 class GitRepositoryTestCase(TmpPathTestCase
):
72 """Tests that operate on temporary git repositories."""
74 def setUp(self
, commit
=True):
75 TmpPathTestCase
.setUp(self
)
76 self
.initialize_repo()
79 git
.current().set_worktree(core
.getcwd())
80 gitcfg
.current().reset()
84 p
= subprocess
.Popen(['git'] + list(args
), stdout
=subprocess
.PIPE
,
85 stderr
=subprocess
.PIPE
)
86 output
, error
= p
.communicate()
87 self
.failIf(p
.returncode
!= 0)
90 def initialize_repo(self
):
92 self
.git('config', '--local', 'user.name', 'Your Name')
93 self
.git('config', '--local', 'user.email', 'you@example.com')
95 self
.git('add', 'A', 'B')
97 def commit_files(self
):
98 self
.git('commit', '-m', 'initial commit')