Skip to content

Commit 7022049

Browse files
committed
Add test variants for repositories using git worktrees
1 parent 2fa4b9d commit 7022049

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

tests/mixin_scripts_repo.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import os
55
import shutil
66

7-
from mixin_git import GitMixin
7+
from mixin_git import (
8+
GitMixin,
9+
GitRepository,
10+
)
811
import testutils
912

1013

@@ -34,7 +37,7 @@ def setUp(self):
3437

3538
assert self.repo
3639

37-
# If case we clone the current repo but there's unstaged content.
40+
# If case we cloned the current repo but there's unstaged content.
3841
self.update_scripts()
3942

4043
def update_scripts(self):
@@ -97,6 +100,49 @@ def write_style(self, style_dict):
97100
self.repo.write_file('.clang-format', content)
98101

99102

103+
class ScriptsWorkTreeRepoMixin(ScriptsRepoMixin):
104+
'''
105+
A mixin used to represent a git repository which uses work trees.
106+
107+
How the scripts end up in the repo is decided by derived classes.
108+
By using classes derived from this mixin, you can get your tests to run in various
109+
configuration.
110+
'''
111+
112+
def setUp(self):
113+
super(ScriptsWorkTreeRepoMixin, self).setUp()
114+
115+
# Now the repo should be setup, but we create an alternative worktree dir
116+
# and use that one instead of the main one.
117+
118+
assert self.repo
119+
120+
# checkout -f in case there are modified scripts (copied by update_scripts).
121+
# We will re-update the scipts anyway later.
122+
self.repo.git_check_output('checkout', '-f')
123+
124+
# We don't know on which branch we are. It could be master, a work branch
125+
# or some branch created by GitHub.
126+
# We need a branch to use for the worktree and a different one on which
127+
# the old repo should be, so we just create too.
128+
worktree_branch = 'other-for-worktree'
129+
self.repo.git_check_output('checkout', '-b', worktree_branch)
130+
main_repo_branch = 'main-repo'
131+
self.repo.git_check_output('checkout', '-b', main_repo_branch)
132+
133+
worktree_branch_path = os.path.join(self.make_tmp_sub_dir(),
134+
'worktree-dir-for-branch--' + worktree_branch)
135+
self.repo.git_check_output('worktree', 'add', worktree_branch_path, worktree_branch)
136+
137+
self.repo = GitRepository(worktree_branch_path)
138+
139+
# The new module may have submodules, make sure they are synced.
140+
self.repo.git_check_output('submodule', 'update', '--init', '--recursive')
141+
142+
# If case we cloned the current repo but there's unstaged content.
143+
self.update_scripts()
144+
145+
100146
class CloneRepoMixin():
101147
'''
102148
A mixin representing a git repository cloned from this git repository.

tests/test_apply_format.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from mixin_scripts_repo import (
1111
ScriptsRepoMixin,
12+
ScriptsWorkTreeRepoMixin,
1213
CloneRepoMixin,
1314
SubmoduleMixin,
1415
CopiedFilesMixin,
@@ -293,3 +294,24 @@ class FormatCopiedScriptsTestCase(CopiedFilesMixin,
293294
FormatTestCaseBase,
294295
unittest.TestCase):
295296
pass
297+
298+
299+
class FormatClonedWorkTreeTestCase(CloneRepoMixin,
300+
ScriptsWorkTreeRepoMixin,
301+
FormatTestCaseBase,
302+
unittest.TestCase):
303+
pass
304+
305+
306+
class FormatSubmoduleWorkTreeTestCase(SubmoduleMixin,
307+
ScriptsWorkTreeRepoMixin,
308+
FormatTestCaseBase,
309+
unittest.TestCase):
310+
pass
311+
312+
313+
class FormatCopiedScriptsWorkTreeTestCase(CopiedFilesMixin,
314+
ScriptsWorkTreeRepoMixin,
315+
FormatTestCaseBase,
316+
unittest.TestCase):
317+
pass

tests/test_hook.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from mixin_scripts_repo import (
1515
ScriptsRepoMixin,
16+
ScriptsWorkTreeRepoMixin,
1617
CloneRepoMixin,
1718
SubmoduleMixin,
1819
CopiedFilesMixin,
@@ -225,3 +226,24 @@ class HookCopiedScriptsTestCase(CopiedFilesMixin,
225226
HookTestCaseBase,
226227
unittest.TestCase):
227228
pass
229+
230+
231+
class HookClonedWorkTreeTestCase(CloneRepoMixin,
232+
ScriptsWorkTreeRepoMixin,
233+
HookTestCaseBase,
234+
unittest.TestCase):
235+
pass
236+
237+
238+
class HookSubmoduleWorkTreeTestCase(SubmoduleMixin,
239+
ScriptsWorkTreeRepoMixin,
240+
HookTestCaseBase,
241+
unittest.TestCase):
242+
pass
243+
244+
245+
class HookCopiedScriptsWorkTreeTestCase(CopiedFilesMixin,
246+
ScriptsWorkTreeRepoMixin,
247+
HookTestCaseBase,
248+
unittest.TestCase):
249+
pass

0 commit comments

Comments
 (0)