blob: acd44ccc383aee37b1904facaa3e977244878efb [file] [log] [blame]
Mike Frysingerf7c51602019-06-18 17:23:39 -04001# 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 Frysinger87deaef2019-07-26 21:14:55 -040015"""Unittests for the project.py module."""
16
Mike Frysinger6da17752019-09-11 18:43:17 -040017import contextlib
18import os
Mike Frysinger2a089cf2021-11-13 23:29:42 -050019from pathlib import Path
Mike Frysinger6da17752019-09-11 18:43:17 -040020import subprocess
21import tempfile
Mike Frysingerf7c51602019-06-18 17:23:39 -040022import unittest
23
Mike Frysingere6a202f2019-08-02 15:57:57 -040024import error
Fredrik de Groot6342d562020-12-01 15:58:53 +010025import git_command
Mike Frysinger6da17752019-09-11 18:43:17 -040026import git_config
Mike Frysingerd9254592020-02-19 22:36:26 -050027import platform_utils
Mike Frysingerf7c51602019-06-18 17:23:39 -040028import project
29
30
Mike Frysinger6da17752019-09-11 18:43:17 -040031@contextlib.contextmanager
32def TempGitTree():
33 """Create a new empty git checkout for testing."""
Mike Frysinger74737da2022-05-20 06:26:50 -040034 with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir:
Fredrik de Groot6342d562020-12-01 15:58:53 +010035 # 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 Kjellerstedtb8bf2912021-04-12 23:25:55 +020045 cmd += ['--template', templatedir]
Fredrik de Groot6342d562020-12-01 15:58:53 +010046 subprocess.check_call(cmd, cwd=tempdir)
Mike Frysinger6da17752019-09-11 18:43:17 -040047 yield tempdir
Mike Frysinger6da17752019-09-11 18:43:17 -040048
49
Mike Frysinger6da17752019-09-11 18:43:17 -040050class 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
64class 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 Frysingere283b952020-11-16 22:56:35 -050083 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -040084 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 Frysingere283b952020-11-16 22:56:35 -050096 fakeproj.work_git.branch('-D', 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -040097 rb = project.ReviewableBranch(
Mike Frysingere283b952020-11-16 22:56:35 -050098 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -040099 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 Frysingere6a202f2019-08-02 15:57:57 -0400103
104
105class 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 Frysinger74737da2022-05-20 06:26:50 -0400120 self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
121 self.tempdir = self.tempdirobj.name
Mike Frysingere6a202f2019-08-02 15:57:57 -0400122 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 Frysinger74737da2022-05-20 06:26:50 -0400128 self.tempdirobj.cleanup()
Mike Frysingere6a202f2019-08-02 15:57:57 -0400129
130 @staticmethod
131 def touch(path):
David Pursehouse348e2182020-02-12 11:36:14 +0900132 with open(path, 'w'):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400133 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
157class 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 Frysingerd9254592020-02-19 22:36:26 -0500212 platform_utils.symlink('foo.txt', sym)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400213 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 Frysingerd9254592020-02-19 22:36:26 -0500219 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 Frysingere6a202f2019-08-02 15:57:57 -0400223 self.assertExists(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500224 cf = self.CopyFile('bar/file.txt', 'foo')
Mike Frysingere6a202f2019-08-02 15:57:57 -0400225 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
226
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900227 def test_src_block_copy_from_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400228 """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 Frysingerd9254592020-02-19 22:36:26 -0500238 platform_utils.symlink('dest', os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400239 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 Frysingerd9254592020-02-19 22:36:26 -0500246 platform_utils.symlink(tempfile.gettempdir(),
247 os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400248 cf = self.CopyFile('foo.txt', 'sym/foo.txt')
249 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
250
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900251 def test_src_block_copy_to_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400252 """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
260class 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 Frysingerd9254592020-02-19 22:36:26 -0500275 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400276
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 Frysinger07392ed2020-02-10 21:35:48 -0500286 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 Frysingerd9254592020-02-19 22:36:26 -0500292 self.assertEqual(os.path.join('..', 'git-project'), os.readlink(dest))
Mike Frysinger07392ed2020-02-10 21:35:48 -0500293
Mike Frysingere6a202f2019-08-02 15:57:57 -0400294 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 Frysinger07392ed2020-02-10 21:35:48 -0500303 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 Frysingere6a202f2019-08-02 15:57:57 -0400318 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 Frysingerd9254592020-02-19 22:36:26 -0500326 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400327
328 # Point the symlink somewhere else.
329 os.unlink(dest)
Mike Frysingerd9254592020-02-19 22:36:26 -0500330 platform_utils.symlink(self.tempdir, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400331 lf._Link()
Mike Frysingerd9254592020-02-19 22:36:26 -0500332 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500333
334
335class 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 Frysinger89ed8ac2022-01-06 05:42:24 -0500344 'unknown-file-should-be-migrated',
345 }
346 _CLEAN_FILES = {
347 'a-vim-temp-file~', '#an-emacs-temp-file#',
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500348 }
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 Frysinger89ed8ac2022-01-06 05:42:24 -0500366 for name in cls._FILES | cls._CLEAN_FILES:
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500367 (dotgit / name).write_text(name)
368
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500369 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 Wagnera3ac8162022-01-11 12:12:55 +0100379 self.assertEqual(os.readlink(dotgit), '../../.repo/projects/src/test.git')
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500380
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 Frysinger89ed8ac2022-01-06 05:42:24 -0500385 # 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())