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 |
| 19 | import shutil |
| 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.""" |
| 34 | # TODO(vapier): Convert this to tempfile.TemporaryDirectory once we drop |
| 35 | # Python 2 support entirely. |
| 36 | try: |
| 37 | tempdir = tempfile.mkdtemp(prefix='repo-tests') |
Fredrik de Groot | 6342d56 | 2020-12-01 15:58:53 +0100 | [diff] [blame] | 38 | |
| 39 | # Tests need to assume, that main is default branch at init, |
| 40 | # which is not supported in config until 2.28. |
| 41 | cmd = ['git', 'init'] |
| 42 | if git_command.git_require((2, 28, 0)): |
| 43 | cmd += ['--initial-branch=main'] |
| 44 | else: |
| 45 | # Use template dir for init. |
| 46 | templatedir = tempfile.mkdtemp(prefix='.test-template') |
| 47 | with open(os.path.join(templatedir, 'HEAD'), 'w') as fp: |
| 48 | fp.write('ref: refs/heads/main\n') |
Peter Kjellerstedt | b8bf291 | 2021-04-12 23:25:55 +0200 | [diff] [blame] | 49 | cmd += ['--template', templatedir] |
Fredrik de Groot | 6342d56 | 2020-12-01 15:58:53 +0100 | [diff] [blame] | 50 | subprocess.check_call(cmd, cwd=tempdir) |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 51 | yield tempdir |
| 52 | finally: |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 53 | platform_utils.rmtree(tempdir) |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 54 | |
| 55 | |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 56 | class FakeProject(object): |
| 57 | """A fake for Project for basic functionality.""" |
| 58 | |
| 59 | def __init__(self, worktree): |
| 60 | self.worktree = worktree |
| 61 | self.gitdir = os.path.join(worktree, '.git') |
| 62 | self.name = 'fakeproject' |
| 63 | self.work_git = project.Project._GitGetByExec( |
| 64 | self, bare=False, gitdir=self.gitdir) |
| 65 | self.bare_git = project.Project._GitGetByExec( |
| 66 | self, bare=True, gitdir=self.gitdir) |
| 67 | self.config = git_config.GitConfig.ForRepository(gitdir=self.gitdir) |
| 68 | |
| 69 | |
| 70 | class ReviewableBranchTests(unittest.TestCase): |
| 71 | """Check ReviewableBranch behavior.""" |
| 72 | |
| 73 | def test_smoke(self): |
| 74 | """A quick run through everything.""" |
| 75 | with TempGitTree() as tempdir: |
| 76 | fakeproj = FakeProject(tempdir) |
| 77 | |
| 78 | # Generate some commits. |
| 79 | with open(os.path.join(tempdir, 'readme'), 'w') as fp: |
| 80 | fp.write('txt') |
| 81 | fakeproj.work_git.add('readme') |
| 82 | fakeproj.work_git.commit('-mAdd file') |
| 83 | fakeproj.work_git.checkout('-b', 'work') |
| 84 | fakeproj.work_git.rm('-f', 'readme') |
| 85 | fakeproj.work_git.commit('-mDel file') |
| 86 | |
| 87 | # Start off with the normal details. |
| 88 | rb = project.ReviewableBranch( |
Mike Frysinger | e283b95 | 2020-11-16 22:56:35 -0500 | [diff] [blame] | 89 | fakeproj, fakeproj.config.GetBranch('work'), 'main') |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 90 | self.assertEqual('work', rb.name) |
| 91 | self.assertEqual(1, len(rb.commits)) |
| 92 | self.assertIn('Del file', rb.commits[0]) |
| 93 | d = rb.unabbrev_commits |
| 94 | self.assertEqual(1, len(d)) |
| 95 | short, long = next(iter(d.items())) |
| 96 | self.assertTrue(long.startswith(short)) |
| 97 | self.assertTrue(rb.base_exists) |
| 98 | # Hard to assert anything useful about this. |
| 99 | self.assertTrue(rb.date) |
| 100 | |
| 101 | # Now delete the tracking branch! |
Mike Frysinger | e283b95 | 2020-11-16 22:56:35 -0500 | [diff] [blame] | 102 | fakeproj.work_git.branch('-D', 'main') |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 103 | rb = project.ReviewableBranch( |
Mike Frysinger | e283b95 | 2020-11-16 22:56:35 -0500 | [diff] [blame] | 104 | fakeproj, fakeproj.config.GetBranch('work'), 'main') |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 105 | self.assertEqual(0, len(rb.commits)) |
| 106 | self.assertFalse(rb.base_exists) |
| 107 | # Hard to assert anything useful about this. |
| 108 | self.assertTrue(rb.date) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 109 | |
| 110 | |
| 111 | class CopyLinkTestCase(unittest.TestCase): |
| 112 | """TestCase for stub repo client checkouts. |
| 113 | |
| 114 | It'll have a layout like: |
| 115 | tempdir/ # self.tempdir |
| 116 | checkout/ # self.topdir |
| 117 | git-project/ # self.worktree |
| 118 | |
| 119 | Attributes: |
| 120 | tempdir: A dedicated temporary directory. |
| 121 | worktree: The top of the repo client checkout. |
| 122 | topdir: The top of a project checkout. |
| 123 | """ |
| 124 | |
| 125 | def setUp(self): |
| 126 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') |
| 127 | self.topdir = os.path.join(self.tempdir, 'checkout') |
| 128 | self.worktree = os.path.join(self.topdir, 'git-project') |
| 129 | os.makedirs(self.topdir) |
| 130 | os.makedirs(self.worktree) |
| 131 | |
| 132 | def tearDown(self): |
| 133 | shutil.rmtree(self.tempdir, ignore_errors=True) |
| 134 | |
| 135 | @staticmethod |
| 136 | def touch(path): |
David Pursehouse | 348e218 | 2020-02-12 11:36:14 +0900 | [diff] [blame] | 137 | with open(path, 'w'): |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 138 | pass |
| 139 | |
| 140 | def assertExists(self, path, msg=None): |
| 141 | """Make sure |path| exists.""" |
| 142 | if os.path.exists(path): |
| 143 | return |
| 144 | |
| 145 | if msg is None: |
| 146 | msg = ['path is missing: %s' % path] |
| 147 | while path != '/': |
| 148 | path = os.path.dirname(path) |
| 149 | if not path: |
| 150 | # If we're given something like "foo", abort once we get to "". |
| 151 | break |
| 152 | result = os.path.exists(path) |
| 153 | msg.append('\tos.path.exists(%s): %s' % (path, result)) |
| 154 | if result: |
| 155 | msg.append('\tcontents: %r' % os.listdir(path)) |
| 156 | break |
| 157 | msg = '\n'.join(msg) |
| 158 | |
| 159 | raise self.failureException(msg) |
| 160 | |
| 161 | |
| 162 | class CopyFile(CopyLinkTestCase): |
| 163 | """Check _CopyFile handling.""" |
| 164 | |
| 165 | def CopyFile(self, src, dest): |
| 166 | return project._CopyFile(self.worktree, src, self.topdir, dest) |
| 167 | |
| 168 | def test_basic(self): |
| 169 | """Basic test of copying a file from a project to the toplevel.""" |
| 170 | src = os.path.join(self.worktree, 'foo.txt') |
| 171 | self.touch(src) |
| 172 | cf = self.CopyFile('foo.txt', 'foo') |
| 173 | cf._Copy() |
| 174 | self.assertExists(os.path.join(self.topdir, 'foo')) |
| 175 | |
| 176 | def test_src_subdir(self): |
| 177 | """Copy a file from a subdir of a project.""" |
| 178 | src = os.path.join(self.worktree, 'bar', 'foo.txt') |
| 179 | os.makedirs(os.path.dirname(src)) |
| 180 | self.touch(src) |
| 181 | cf = self.CopyFile('bar/foo.txt', 'new.txt') |
| 182 | cf._Copy() |
| 183 | self.assertExists(os.path.join(self.topdir, 'new.txt')) |
| 184 | |
| 185 | def test_dest_subdir(self): |
| 186 | """Copy a file to a subdir of a checkout.""" |
| 187 | src = os.path.join(self.worktree, 'foo.txt') |
| 188 | self.touch(src) |
| 189 | cf = self.CopyFile('foo.txt', 'sub/dir/new.txt') |
| 190 | self.assertFalse(os.path.exists(os.path.join(self.topdir, 'sub'))) |
| 191 | cf._Copy() |
| 192 | self.assertExists(os.path.join(self.topdir, 'sub', 'dir', 'new.txt')) |
| 193 | |
| 194 | def test_update(self): |
| 195 | """Make sure changed files get copied again.""" |
| 196 | src = os.path.join(self.worktree, 'foo.txt') |
| 197 | dest = os.path.join(self.topdir, 'bar') |
| 198 | with open(src, 'w') as f: |
| 199 | f.write('1st') |
| 200 | cf = self.CopyFile('foo.txt', 'bar') |
| 201 | cf._Copy() |
| 202 | self.assertExists(dest) |
| 203 | with open(dest) as f: |
| 204 | self.assertEqual(f.read(), '1st') |
| 205 | |
| 206 | with open(src, 'w') as f: |
| 207 | f.write('2nd!') |
| 208 | cf._Copy() |
| 209 | with open(dest) as f: |
| 210 | self.assertEqual(f.read(), '2nd!') |
| 211 | |
| 212 | def test_src_block_symlink(self): |
| 213 | """Do not allow reading from a symlinked path.""" |
| 214 | src = os.path.join(self.worktree, 'foo.txt') |
| 215 | sym = os.path.join(self.worktree, 'sym') |
| 216 | self.touch(src) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 217 | platform_utils.symlink('foo.txt', sym) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 218 | self.assertExists(sym) |
| 219 | cf = self.CopyFile('sym', 'foo') |
| 220 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 221 | |
| 222 | def test_src_block_symlink_traversal(self): |
| 223 | """Do not allow reading through a symlink dir.""" |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 224 | realfile = os.path.join(self.tempdir, 'file.txt') |
| 225 | self.touch(realfile) |
| 226 | src = os.path.join(self.worktree, 'bar', 'file.txt') |
| 227 | platform_utils.symlink(self.tempdir, os.path.join(self.worktree, 'bar')) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 228 | self.assertExists(src) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 229 | cf = self.CopyFile('bar/file.txt', 'foo') |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 230 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 231 | |
David Pursehouse | 4bbba7d | 2020-02-12 11:14:55 +0900 | [diff] [blame] | 232 | def test_src_block_copy_from_dir(self): |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 233 | """Do not allow copying from a directory.""" |
| 234 | src = os.path.join(self.worktree, 'dir') |
| 235 | os.makedirs(src) |
| 236 | cf = self.CopyFile('dir', 'foo') |
| 237 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 238 | |
| 239 | def test_dest_block_symlink(self): |
| 240 | """Do not allow writing to a symlink.""" |
| 241 | src = os.path.join(self.worktree, 'foo.txt') |
| 242 | self.touch(src) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 243 | platform_utils.symlink('dest', os.path.join(self.topdir, 'sym')) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 244 | cf = self.CopyFile('foo.txt', 'sym') |
| 245 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 246 | |
| 247 | def test_dest_block_symlink_traversal(self): |
| 248 | """Do not allow writing through a symlink dir.""" |
| 249 | src = os.path.join(self.worktree, 'foo.txt') |
| 250 | self.touch(src) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 251 | platform_utils.symlink(tempfile.gettempdir(), |
| 252 | os.path.join(self.topdir, 'sym')) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 253 | cf = self.CopyFile('foo.txt', 'sym/foo.txt') |
| 254 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 255 | |
David Pursehouse | 4bbba7d | 2020-02-12 11:14:55 +0900 | [diff] [blame] | 256 | def test_src_block_copy_to_dir(self): |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 257 | """Do not allow copying to a directory.""" |
| 258 | src = os.path.join(self.worktree, 'foo.txt') |
| 259 | self.touch(src) |
| 260 | os.makedirs(os.path.join(self.topdir, 'dir')) |
| 261 | cf = self.CopyFile('foo.txt', 'dir') |
| 262 | self.assertRaises(error.ManifestInvalidPathError, cf._Copy) |
| 263 | |
| 264 | |
| 265 | class LinkFile(CopyLinkTestCase): |
| 266 | """Check _LinkFile handling.""" |
| 267 | |
| 268 | def LinkFile(self, src, dest): |
| 269 | return project._LinkFile(self.worktree, src, self.topdir, dest) |
| 270 | |
| 271 | def test_basic(self): |
| 272 | """Basic test of linking a file from a project into the toplevel.""" |
| 273 | src = os.path.join(self.worktree, 'foo.txt') |
| 274 | self.touch(src) |
| 275 | lf = self.LinkFile('foo.txt', 'foo') |
| 276 | lf._Link() |
| 277 | dest = os.path.join(self.topdir, 'foo') |
| 278 | self.assertExists(dest) |
| 279 | self.assertTrue(os.path.islink(dest)) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 280 | self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest)) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 281 | |
| 282 | def test_src_subdir(self): |
| 283 | """Link to a file in a subdir of a project.""" |
| 284 | src = os.path.join(self.worktree, 'bar', 'foo.txt') |
| 285 | os.makedirs(os.path.dirname(src)) |
| 286 | self.touch(src) |
| 287 | lf = self.LinkFile('bar/foo.txt', 'foo') |
| 288 | lf._Link() |
| 289 | self.assertExists(os.path.join(self.topdir, 'foo')) |
| 290 | |
Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 291 | def test_src_self(self): |
| 292 | """Link to the project itself.""" |
| 293 | dest = os.path.join(self.topdir, 'foo', 'bar') |
| 294 | lf = self.LinkFile('.', 'foo/bar') |
| 295 | lf._Link() |
| 296 | self.assertExists(dest) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 297 | self.assertEqual(os.path.join('..', 'git-project'), os.readlink(dest)) |
Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 298 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 299 | def test_dest_subdir(self): |
| 300 | """Link a file to a subdir of a checkout.""" |
| 301 | src = os.path.join(self.worktree, 'foo.txt') |
| 302 | self.touch(src) |
| 303 | lf = self.LinkFile('foo.txt', 'sub/dir/foo/bar') |
| 304 | self.assertFalse(os.path.exists(os.path.join(self.topdir, 'sub'))) |
| 305 | lf._Link() |
| 306 | self.assertExists(os.path.join(self.topdir, 'sub', 'dir', 'foo', 'bar')) |
| 307 | |
Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 308 | def test_src_block_relative(self): |
| 309 | """Do not allow relative symlinks.""" |
| 310 | BAD_SOURCES = ( |
| 311 | './', |
| 312 | '..', |
| 313 | '../', |
| 314 | 'foo/.', |
| 315 | 'foo/./bar', |
| 316 | 'foo/..', |
| 317 | 'foo/../foo', |
| 318 | ) |
| 319 | for src in BAD_SOURCES: |
| 320 | lf = self.LinkFile(src, 'foo') |
| 321 | self.assertRaises(error.ManifestInvalidPathError, lf._Link) |
| 322 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 323 | def test_update(self): |
| 324 | """Make sure changed targets get updated.""" |
| 325 | dest = os.path.join(self.topdir, 'sym') |
| 326 | |
| 327 | src = os.path.join(self.worktree, 'foo.txt') |
| 328 | self.touch(src) |
| 329 | lf = self.LinkFile('foo.txt', 'sym') |
| 330 | lf._Link() |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 331 | self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest)) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 332 | |
| 333 | # Point the symlink somewhere else. |
| 334 | os.unlink(dest) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 335 | platform_utils.symlink(self.tempdir, dest) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 336 | lf._Link() |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 337 | self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest)) |