blob: 5c600be7980b358dd7625252a9409c3e896589f7 [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
Joanna Wanga6c52f52022-11-03 16:51:19 -040029import repo_trace
Mike Frysingerf7c51602019-06-18 17:23:39 -040030
31
Mike Frysinger6da17752019-09-11 18:43:17 -040032@contextlib.contextmanager
33def TempGitTree():
34 """Create a new empty git checkout for testing."""
Mike Frysinger74737da2022-05-20 06:26:50 -040035 with tempfile.TemporaryDirectory(prefix='repo-tests') as tempdir:
Fredrik de Groot6342d562020-12-01 15:58:53 +010036 # 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 Kjellerstedtb8bf2912021-04-12 23:25:55 +020046 cmd += ['--template', templatedir]
Fredrik de Groot6342d562020-12-01 15:58:53 +010047 subprocess.check_call(cmd, cwd=tempdir)
Mike Frysinger6da17752019-09-11 18:43:17 -040048 yield tempdir
Mike Frysinger6da17752019-09-11 18:43:17 -040049
50
Mike Frysinger6da17752019-09-11 18:43:17 -040051class 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
65class ReviewableBranchTests(unittest.TestCase):
66 """Check ReviewableBranch behavior."""
67
Joanna Wanga6c52f52022-11-03 16:51:19 -040068 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 Frysinger6da17752019-09-11 18:43:17 -040075 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 Frysingere283b952020-11-16 22:56:35 -050091 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -040092 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 Frysingere283b952020-11-16 22:56:35 -0500104 fakeproj.work_git.branch('-D', 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -0400105 rb = project.ReviewableBranch(
Mike Frysingere283b952020-11-16 22:56:35 -0500106 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -0400107 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 Frysingere6a202f2019-08-02 15:57:57 -0400111
112
113class 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 Frysinger74737da2022-05-20 06:26:50 -0400128 self.tempdirobj = tempfile.TemporaryDirectory(prefix='repo_tests')
129 self.tempdir = self.tempdirobj.name
Mike Frysingere6a202f2019-08-02 15:57:57 -0400130 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 Frysinger74737da2022-05-20 06:26:50 -0400136 self.tempdirobj.cleanup()
Mike Frysingere6a202f2019-08-02 15:57:57 -0400137
138 @staticmethod
139 def touch(path):
David Pursehouse348e2182020-02-12 11:36:14 +0900140 with open(path, 'w'):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400141 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
165class 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 Frysingerd9254592020-02-19 22:36:26 -0500220 platform_utils.symlink('foo.txt', sym)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400221 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 Frysingerd9254592020-02-19 22:36:26 -0500227 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 Frysingere6a202f2019-08-02 15:57:57 -0400231 self.assertExists(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500232 cf = self.CopyFile('bar/file.txt', 'foo')
Mike Frysingere6a202f2019-08-02 15:57:57 -0400233 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
234
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900235 def test_src_block_copy_from_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400236 """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 Frysingerd9254592020-02-19 22:36:26 -0500246 platform_utils.symlink('dest', os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400247 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 Frysingerd9254592020-02-19 22:36:26 -0500254 platform_utils.symlink(tempfile.gettempdir(),
255 os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400256 cf = self.CopyFile('foo.txt', 'sym/foo.txt')
257 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
258
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900259 def test_src_block_copy_to_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400260 """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
268class 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 Frysingerd9254592020-02-19 22:36:26 -0500283 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400284
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 Frysinger07392ed2020-02-10 21:35:48 -0500294 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 Frysingerd9254592020-02-19 22:36:26 -0500300 self.assertEqual(os.path.join('..', 'git-project'), os.readlink(dest))
Mike Frysinger07392ed2020-02-10 21:35:48 -0500301
Mike Frysingere6a202f2019-08-02 15:57:57 -0400302 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 Frysinger07392ed2020-02-10 21:35:48 -0500311 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 Frysingere6a202f2019-08-02 15:57:57 -0400326 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 Frysingerd9254592020-02-19 22:36:26 -0500334 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400335
336 # Point the symlink somewhere else.
337 os.unlink(dest)
Mike Frysingerd9254592020-02-19 22:36:26 -0500338 platform_utils.symlink(self.tempdir, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400339 lf._Link()
Mike Frysingerd9254592020-02-19 22:36:26 -0500340 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500341
342
343class 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 Frysinger89ed8ac2022-01-06 05:42:24 -0500352 'unknown-file-should-be-migrated',
353 }
354 _CLEAN_FILES = {
355 'a-vim-temp-file~', '#an-emacs-temp-file#',
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500356 }
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 Frysinger89ed8ac2022-01-06 05:42:24 -0500374 for name in cls._FILES | cls._CLEAN_FILES:
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500375 (dotgit / name).write_text(name)
376
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500377 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 Wagnera3ac8162022-01-11 12:12:55 +0100387 self.assertEqual(os.readlink(dotgit), '../../.repo/projects/src/test.git')
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500388
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 Frysinger89ed8ac2022-01-06 05:42:24 -0500393 # 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())