|
4 | 4 | import os |
5 | 5 | import shutil |
6 | 6 |
|
7 | | -from mixin_git import GitMixin |
| 7 | +from mixin_git import ( |
| 8 | + GitMixin, |
| 9 | + GitRepository, |
| 10 | + ) |
8 | 11 | import testutils |
9 | 12 |
|
10 | 13 |
|
@@ -34,7 +37,7 @@ def setUp(self): |
34 | 37 |
|
35 | 38 | assert self.repo |
36 | 39 |
|
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. |
38 | 41 | self.update_scripts() |
39 | 42 |
|
40 | 43 | def update_scripts(self): |
@@ -97,6 +100,49 @@ def write_style(self, style_dict): |
97 | 100 | self.repo.write_file('.clang-format', content) |
98 | 101 |
|
99 | 102 |
|
| 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 | + |
100 | 146 | class CloneRepoMixin(): |
101 | 147 | ''' |
102 | 148 | A mixin representing a git repository cloned from this git repository. |
|
0 commit comments