| Mike Frysinger | f7c5160 | 2019-06-18 17:23:39 -0400 | [diff] [blame] | 1 | # Copyright (C) 2019 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| Mike Frysinger | 87deaef | 2019-07-26 21:14:55 -0400 | [diff] [blame] | 15 | """Unittests for the project.py module.""" |
| 16 | |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 17 | import contextlib |
| 18 | import os |
| Mike Frysinger | 2a089cf | 2021-11-13 23:29:42 -0500 | [diff] [blame] | 19 | from pathlib import Path |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 20 | import subprocess |
| 21 | import tempfile |
| Mike Frysinger | f7c5160 | 2019-06-18 17:23:39 -0400 | [diff] [blame] | 22 | import unittest |
| 23 | |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 24 | import error |
| Fredrik de Groot | 6342d56 | 2020-12-01 15:58:53 +0100 | [diff] [blame] | 25 | import git_command |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 26 | import git_config |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 27 | import platform_utils |
| Mike Frysinger | f7c5160 | 2019-06-18 17:23:39 -0400 | [diff] [blame] | 28 | import project |
| Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame^] | 29 | import repo_trace |
| Mike Frysinger | f7c5160 | 2019-06-18 17:23:39 -0400 | [diff] [blame] | 30 | |
| 31 | |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 32 | @contextlib.contextmanager |
| 33 | def TempGitTree(): |
| 34 | """Create a new empty git checkout for testing.""" |
| Mike Frysinger | 74737da | 2022-05-20 06:26:50 -0400 | [diff] [blame] | 35 | with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir: |
| Fredrik de Groot | 6342d56 | 2020-12-01 15:58:53 +0100 | [diff] [blame] | 36 | # Tests need to assume, that main is default branch at init, |
| 37 | # which is not supported in config until 2.28. |
| 38 | cmd = ['git', 'init'] |
| 39 | if git_command.git_require((2, 28, 0)): |
| 40 | cmd += ['--initial-branch=main'] |
| 41 | else: |
| 42 | # Use template dir for init. |
| 43 | templatedir = tempfile.mkdtemp(prefix='.test-template') |
| 44 | with open(os.path.join(templatedir, 'HEAD'), 'w') as fp: |
| 45 | fp.write('ref: refs/heads/main\n') |
| Peter Kjellerstedt | b8bf291 | 2021-04-12 23:25:55 +0200 | [diff] [blame] | 46 | cmd += ['--template', templatedir] |
| Fredrik de Groot | 6342d56 | 2020-12-01 15:58:53 +0100 | [diff] [blame] | 47 | subprocess.check_call(cmd, cwd=tempdir) |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 48 | yield tempdir |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 49 | |
| 50 | |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 51 | class FakeProject(object): |
| 52 | """A fake for Project for basic functionality.""" |
| 53 | |
| 54 | def __init__(self, worktree): |
| 55 | self.worktree = worktree |
| 56 | self.gitdir = os.path.join(worktree, '.git') |
| 57 | self.name = 'fakeproject' |
| 58 | self.work_git = project.Project._GitGetByExec( |
| 59 | self, bare=False, gitdir=self.gitdir) |
| 60 | self.bare_git = project.Project._GitGetByExec( |
| 61 | self, bare=True, gitdir=self.gitdir) |
| 62 | self.config = git_config.GitConfig.ForRepository(gitdir=self.gitdir) |
| 63 | |
| 64 | |
| 65 | class ReviewableBranchTests(unittest.TestCase): |
| 66 | """Check ReviewableBranch behavior.""" |
| 67 | |
| Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame^] | 68 | def setUp(self): |
| 69 | self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests') |
| 70 | repo_trace._TRACE_FILE = os.path.join(self.tempdirobj.name, 'TRACE_FILE_from_test') |
| 71 | |
| 72 | def tearDown(self): |
| 73 | self.tempdirobj.cleanup() |
| 74 | |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 75 | def test_smoke(self): |
| 76 | """A quick run through everything.""" |
| 77 | with TempGitTree() as tempdir: |
| 78 | fakeproj = FakeProject(tempdir) |
| 79 | |
| 80 | # Generate some commits. |
| 81 | with open(os.path.join(tempdir, 'readme'), 'w') as fp: |
| 82 | fp.write('txt') |
| 83 | fakeproj.work_git.add('readme') |
| 84 | fakeproj.work_git.commit('-mAdd file') |
| 85 | fakeproj.work_git.checkout('-b', 'work') |
| 86 | fakeproj.work_git.rm('-f', 'readme') |
| 87 | fakeproj.work_git.commit('-mDel file') |
| 88 | |
| 89 | # Start off with the normal details. |
| 90 | rb = project.ReviewableBranch( |
| Mike Frysinger | e283b95 | 2020-11-16 22:56:35 -0500 | [diff] [blame] | 91 | fakeproj, fakeproj.config.GetBranch('work'), 'main') |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 92 | self.assertEqual('work', rb.name) |
| 93 | self.assertEqual(1, len(rb.commits)) |
| 94 | self.assertIn('Del file', rb.commits[0]) |
| 95 | d = rb.unabbrev_commits |
| 96 | self.assertEqual(1, len(d)) |
| 97 | short, long = next(iter(d.items())) |
| 98 | self.assertTrue(long.startswith(short)) |
| 99 | self.assertTrue(rb.base_exists) |
| 100 | # Hard to assert anything useful about this. |
| 101 | self.assertTrue(rb.date) |
| 102 | |
| 103 | # Now delete the tracking branch! |
| Mike Frysinger | e283b95 | 2020-11-16 22:56:35 -0500 | [diff] [blame] | 104 | fakeproj.work_git.branch('-D', 'main') |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 105 | rb = project.ReviewableBranch( |
| Mike Frysinger | e283b95 | 2020-11-16 22:56:35 -0500 | [diff] [blame] | 106 | fakeproj, fakeproj.config.GetBranch('work'), 'main') |
| Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 107 | self.assertEqual(0, len(rb.commits)) |
| 108 | self.assertFalse(rb.base_exists) |
| 109 | # Hard to assert anything useful about this. |
| 110 | self.assertTrue(rb.date) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 111 | |
| 112 | |
| 113 | class CopyLinkTestCase(unittest.TestCase): |
| 114 | """TestCase for stub repo client checkouts. |
| 115 | |
| 116 | It'll have a layout like: |
| 117 | tempdir/ # self.tempdir |
| 118 | checkout/ # self.topdir |
| 119 | git-project/ # self.worktree |
| 120 | |
| 121 | Attributes: |
| 122 | tempdir: A dedicated temporary directory. |
| 123 | worktree: The top of the repo client checkout. |
| 124 | topdir: The top of a project checkout. |
| 125 | """ |
| 126 | |
| 127 | def setUp(self): |
| Mike Frysinger | 74737da | 2022-05-20 06:26:50 -0400 | [diff] [blame] | 128 | self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests') |
| 129 | self.tempdir = self.tempdirobj.name |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 130 | self.topdir = os.path.join(self.tempdir, 'checkout') |
| 131 | self.worktree = os.path.join(self.topdir, 'git-project') |
| 132 | os.makedirs(self.topdir) |
| 133 | os.makedirs(self.worktree) |
| 134 | |
| 135 | def tearDown(self): |
| Mike Frysinger | 74737da | 2022-05-20 06:26:50 -0400 | [diff] [blame] | 136 | self.tempdirobj.cleanup() |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 137 | |
| 138 | @staticmethod |
| 139 | def touch(path): |
| David Pursehouse | 348e218 | 2020-02-12 11:36:14 +0900 | [diff] [blame] | 140 | with open(path, 'w'): |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 141 | pass |
| 142 | |
| 143 | def assertExists(self, path, msg=None): |
| 144 | """Make sure |path| exists.""" |
| 145 | if os.path.exists(path): |
| 146 | return |
| 147 | |
| 148 | if msg is None: |
| 149 | msg = ['path is missing: %s' % path] |
| 150 | while path != '/': |
| 151 | path = os.path.dirname(path) |
| 152 | if not path: |
| 153 | # If we're given something like "foo", abort once we get to "". |
| 154 | break |
| 155 | result = os.path.exists(path) |
| 156 | msg.append('\tos.path.exists(%s): %s' % (path, result)) |
| 157 | if result: |
| 158 | msg.append('\tcontents: %r' % os.listdir(path)) |
| 159 | break |
| 160 | msg = '\n'.join(msg) |
| 161 | |
| 162 | raise self.failureException(msg) |
| 163 | |
| 164 | |
| 165 | class CopyFile(CopyLinkTestCase): |
| 166 | """Check _CopyFile handling.""" |
| 167 | |
| 168 | def CopyFile(self, src, dest): |
| 169 | return project._CopyFile(self.worktree, src, self.topdir, dest) |
| 170 | |
| 171 | def test_basic(self): |
| 172 | """Basic test of copying a file from a project to the toplevel.""" |
| 173 | src = os.path.join(self.worktree, 'foo.txt') |
| 174 | self.touch(src) |
| 175 | cf = self.CopyFile('foo.txt', 'foo') |
| 176 | cf._Copy() |
| 177 | self.assertExists(os.path.join(self.topdir, 'foo')) |
| 178 | |
| 179 | def test_src_subdir(self): |
| 180 | """Copy a file from a subdir of a project.""" |
| 181 | src = os.path.join(self.worktree, 'bar', 'foo.txt') |
| 182 | os.makedirs(os.path.dirname(src)) |
| 183 | self.touch(src) |
| 184 | cf = self.CopyFile('bar/foo.txt', 'new.txt') |
| 185 | cf._Copy() |
| 186 | self.assertExists(os.path.join(self.topdir, 'new.txt')) |
| 187 | |
| 188 | def test_dest_subdir(self): |
| 189 | """Copy a file to a subdir of a checkout.""" |
| 190 | src = os.path.join(self.worktree, 'foo.txt') |
| 191 | self.touch(src) |
| 192 | cf = self.CopyFile('foo.txt', 'sub/dir/new.txt') |
| 193 | self.assertFalse(os.path.exists(os.path.join(self.topdir, 'sub'))) |
| 194 | cf._Copy() |
| 195 | self.assertExists(os.path.join(self.topdir, 'sub', 'dir', 'new.txt')) |
| 196 | |
| 197 | def test_update(self): |
| 198 | """Make sure changed files get copied again.""" |
| 199 | src = os.path.join(self.worktree, 'foo.txt') |
| 200 | dest = os.path.join(self.topdir, 'bar') |
| 201 | with open(src, 'w') as f: |
| 202 | f.write('1st') |
| 203 | cf = self.CopyFile('foo.txt', 'bar') |
| 204 | cf._Copy() |
| 205 | self.assertExists(dest) |
| 206 | with open(dest) as f: |
| 207 | self.assertEqual(f.read(), '1st') |
| 208 | |
| 209 | with open(src, 'w') as f: |
| 210 | f.write('2nd!') |
| 211 | cf._Copy() |
| 212 | with open(dest) as f: |
| 213 | self.assertEqual(f.read(), '2nd!') |
| 214 | |
| 215 | def test_src_block_symlink(self): |
| 216 | """Do not allow reading from a symlinked path.""" |
| 217 | src = os.path.join(self.worktree, 'foo.txt') |
| 218 | sym = os.path.join(self.worktree, 'sym') |
| 219 | self.touch(src) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 220 | platform_utils.symlink('foo.txt', sym) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 221 | self.assertExists(sym) |
| 222 | cf = self.CopyFile('sym', 'foo') |
| 223 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 224 | |
| 225 | def test_src_block_symlink_traversal(self): |
| 226 | """Do not allow reading through a symlink dir.""" |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 227 | realfile = os.path.join(self.tempdir, 'file.txt') |
| 228 | self.touch(realfile) |
| 229 | src = os.path.join(self.worktree, 'bar', 'file.txt') |
| 230 | platform_utils.symlink(self.tempdir, os.path.join(self.worktree, 'bar')) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 231 | self.assertExists(src) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 232 | cf = self.CopyFile('bar/file.txt', 'foo') |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 233 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 234 | |
| David Pursehouse | 4bbba7d | 2020-02-12 11:14:55 +0900 | [diff] [blame] | 235 | def test_src_block_copy_from_dir(self): |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 236 | """Do not allow copying from a directory.""" |
| 237 | src = os.path.join(self.worktree, 'dir') |
| 238 | os.makedirs(src) |
| 239 | cf = self.CopyFile('dir', 'foo') |
| 240 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 241 | |
| 242 | def test_dest_block_symlink(self): |
| 243 | """Do not allow writing to a symlink.""" |
| 244 | src = os.path.join(self.worktree, 'foo.txt') |
| 245 | self.touch(src) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 246 | platform_utils.symlink('dest', os.path.join(self.topdir, 'sym')) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 247 | cf = self.CopyFile('foo.txt', 'sym') |
| 248 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 249 | |
| 250 | def test_dest_block_symlink_traversal(self): |
| 251 | """Do not allow writing through a symlink dir.""" |
| 252 | src = os.path.join(self.worktree, 'foo.txt') |
| 253 | self.touch(src) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 254 | platform_utils.symlink(tempfile.gettempdir(), |
| 255 | os.path.join(self.topdir, 'sym')) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 256 | cf = self.CopyFile('foo.txt', 'sym/foo.txt') |
| 257 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 258 | |
| David Pursehouse | 4bbba7d | 2020-02-12 11:14:55 +0900 | [diff] [blame] | 259 | def test_src_block_copy_to_dir(self): |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 260 | """Do not allow copying to a directory.""" |
| 261 | src = os.path.join(self.worktree, 'foo.txt') |
| 262 | self.touch(src) |
| 263 | os.makedirs(os.path.join(self.topdir, 'dir')) |
| 264 | cf = self.CopyFile('foo.txt', 'dir') |
| 265 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 266 | |
| 267 | |
| 268 | class LinkFile(CopyLinkTestCase): |
| 269 | """Check _LinkFile handling.""" |
| 270 | |
| 271 | def LinkFile(self, src, dest): |
| 272 | return project._LinkFile(self.worktree, src, self.topdir, dest) |
| 273 | |
| 274 | def test_basic(self): |
| 275 | """Basic test of linking a file from a project into the toplevel.""" |
| 276 | src = os.path.join(self.worktree, 'foo.txt') |
| 277 | self.touch(src) |
| 278 | lf = self.LinkFile('foo.txt', 'foo') |
| 279 | lf._Link() |
| 280 | dest = os.path.join(self.topdir, 'foo') |
| 281 | self.assertExists(dest) |
| 282 | self.assertTrue(os.path.islink(dest)) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 283 | self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest)) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 284 | |
| 285 | def test_src_subdir(self): |
| 286 | """Link to a file in a subdir of a project.""" |
| 287 | src = os.path.join(self.worktree, 'bar', 'foo.txt') |
| 288 | os.makedirs(os.path.dirname(src)) |
| 289 | self.touch(src) |
| 290 | lf = self.LinkFile('bar/foo.txt', 'foo') |
| 291 | lf._Link() |
| 292 | self.assertExists(os.path.join(self.topdir, 'foo')) |
| 293 | |
| Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 294 | def test_src_self(self): |
| 295 | """Link to the project itself.""" |
| 296 | dest = os.path.join(self.topdir, 'foo', 'bar') |
| 297 | lf = self.LinkFile('.', 'foo/bar') |
| 298 | lf._Link() |
| 299 | self.assertExists(dest) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 300 | self.assertEqual(os.path.join('..', 'git-project'), os.readlink(dest)) |
| Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 301 | |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 302 | def test_dest_subdir(self): |
| 303 | """Link a file to a subdir of a checkout.""" |
| 304 | src = os.path.join(self.worktree, 'foo.txt') |
| 305 | self.touch(src) |
| 306 | lf = self.LinkFile('foo.txt', 'sub/dir/foo/bar') |
| 307 | self.assertFalse(os.path.exists(os.path.join(self.topdir, 'sub'))) |
| 308 | lf._Link() |
| 309 | self.assertExists(os.path.join(self.topdir, 'sub', 'dir', 'foo', 'bar')) |
| 310 | |
| Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 311 | def test_src_block_relative(self): |
| 312 | """Do not allow relative symlinks.""" |
| 313 | BAD_SOURCES = ( |
| 314 | './', |
| 315 | '..', |
| 316 | '../', |
| 317 | 'foo/.', |
| 318 | 'foo/./bar', |
| 319 | 'foo/..', |
| 320 | 'foo/../foo', |
| 321 | ) |
| 322 | for src in BAD_SOURCES: |
| 323 | lf = self.LinkFile(src, 'foo') |
| 324 | self.assertRaises(error.ManifestInvalidPathError, lf._Link) |
| 325 | |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 326 | def test_update(self): |
| 327 | """Make sure changed targets get updated.""" |
| 328 | dest = os.path.join(self.topdir, 'sym') |
| 329 | |
| 330 | src = os.path.join(self.worktree, 'foo.txt') |
| 331 | self.touch(src) |
| 332 | lf = self.LinkFile('foo.txt', 'sym') |
| 333 | lf._Link() |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 334 | self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest)) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 335 | |
| 336 | # Point the symlink somewhere else. |
| 337 | os.unlink(dest) |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 338 | platform_utils.symlink(self.tempdir, dest) |
| Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 339 | lf._Link() |
| Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 340 | self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest)) |
| Mike Frysinger | 2a089cf | 2021-11-13 23:29:42 -0500 | [diff] [blame] | 341 | |
| 342 | |
| 343 | class MigrateWorkTreeTests(unittest.TestCase): |
| 344 | """Check _MigrateOldWorkTreeGitDir handling.""" |
| 345 | |
| 346 | _SYMLINKS = { |
| 347 | 'config', 'description', 'hooks', 'info', 'logs', 'objects', |
| 348 | 'packed-refs', 'refs', 'rr-cache', 'shallow', 'svn', |
| 349 | } |
| 350 | _FILES = { |
| 351 | 'COMMIT_EDITMSG', 'FETCH_HEAD', 'HEAD', 'index', 'ORIG_HEAD', |
| Mike Frysinger | 89ed8ac | 2022-01-06 05:42:24 -0500 | [diff] [blame] | 352 | 'unknown-file-should-be-migrated', |
| 353 | } |
| 354 | _CLEAN_FILES = { |
| 355 | 'a-vim-temp-file~', '#an-emacs-temp-file#', |
| Mike Frysinger | 2a089cf | 2021-11-13 23:29:42 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @classmethod |
| 359 | @contextlib.contextmanager |
| 360 | def _simple_layout(cls): |
| 361 | """Create a simple repo client checkout to test against.""" |
| 362 | with tempfile.TemporaryDirectory() as tempdir: |
| 363 | tempdir = Path(tempdir) |
| 364 | |
| 365 | gitdir = tempdir / '.repo/projects/src/test.git' |
| 366 | gitdir.mkdir(parents=True) |
| 367 | cmd = ['git', 'init', '--bare', str(gitdir)] |
| 368 | subprocess.check_call(cmd) |
| 369 | |
| 370 | dotgit = tempdir / 'src/test/.git' |
| 371 | dotgit.mkdir(parents=True) |
| 372 | for name in cls._SYMLINKS: |
| 373 | (dotgit / name).symlink_to(f'../../../.repo/projects/src/test.git/{name}') |
| Mike Frysinger | 89ed8ac | 2022-01-06 05:42:24 -0500 | [diff] [blame] | 374 | for name in cls._FILES | cls._CLEAN_FILES: |
| Mike Frysinger | 2a089cf | 2021-11-13 23:29:42 -0500 | [diff] [blame] | 375 | (dotgit / name).write_text(name) |
| 376 | |
| Mike Frysinger | 2a089cf | 2021-11-13 23:29:42 -0500 | [diff] [blame] | 377 | yield tempdir |
| 378 | |
| 379 | def test_standard(self): |
| 380 | """Migrate a standard checkout that we expect.""" |
| 381 | with self._simple_layout() as tempdir: |
| 382 | dotgit = tempdir / 'src/test/.git' |
| 383 | project.Project._MigrateOldWorkTreeGitDir(str(dotgit)) |
| 384 | |
| 385 | # Make sure the dir was transformed into a symlink. |
| 386 | self.assertTrue(dotgit.is_symlink()) |
| Sebastian Wagner | a3ac816 | 2022-01-11 12:12:55 +0100 | [diff] [blame] | 387 | self.assertEqual(os.readlink(dotgit), '../../.repo/projects/src/test.git') |
| Mike Frysinger | 2a089cf | 2021-11-13 23:29:42 -0500 | [diff] [blame] | 388 | |
| 389 | # Make sure files were moved over. |
| 390 | gitdir = tempdir / '.repo/projects/src/test.git' |
| 391 | for name in self._FILES: |
| 392 | self.assertEqual(name, (gitdir / name).read_text()) |
| Mike Frysinger | 89ed8ac | 2022-01-06 05:42:24 -0500 | [diff] [blame] | 393 | # Make sure files were removed. |
| 394 | for name in self._CLEAN_FILES: |
| 395 | self.assertFalse((gitdir / name).exists()) |
| 396 | |
| 397 | def test_unknown(self): |
| 398 | """A checkout with unknown files should abort.""" |
| 399 | with self._simple_layout() as tempdir: |
| 400 | dotgit = tempdir / 'src/test/.git' |
| 401 | (tempdir / '.repo/projects/src/test.git/random-file').write_text('one') |
| 402 | (dotgit / 'random-file').write_text('two') |
| 403 | with self.assertRaises(error.GitError): |
| 404 | project.Project._MigrateOldWorkTreeGitDir(str(dotgit)) |
| 405 | |
| 406 | # Make sure no content was actually changed. |
| 407 | self.assertTrue(dotgit.is_dir()) |
| 408 | for name in self._FILES: |
| 409 | self.assertTrue((dotgit / name).is_file()) |
| 410 | for name in self._CLEAN_FILES: |
| 411 | self.assertTrue((dotgit / name).is_file()) |
| 412 | for name in self._SYMLINKS: |
| 413 | self.assertTrue((dotgit / name).is_symlink()) |