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