blob: d578fe848d1cc6e9e82c0658b5a8ac5e66e2d43f [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 shutil
21import subprocess
22import tempfile
Mike Frysingerf7c51602019-06-18 17:23:39 -040023import unittest
24
Mike Frysingere6a202f2019-08-02 15:57:57 -040025import error
Fredrik de Groot6342d562020-12-01 15:58:53 +010026import git_command
Mike Frysinger6da17752019-09-11 18:43:17 -040027import git_config
Mike Frysingerd9254592020-02-19 22:36:26 -050028import platform_utils
Mike Frysingerf7c51602019-06-18 17:23:39 -040029import project
30
31
Mike Frysinger6da17752019-09-11 18:43:17 -040032@contextlib.contextmanager
33def TempGitTree():
34 """Create a new empty git checkout for testing."""
35 # TODO(vapier): Convert this to tempfile.TemporaryDirectory once we drop
36 # Python 2 support entirely.
37 try:
38 tempdir = tempfile.mkdtemp(prefix='repo-tests')
Fredrik de Groot6342d562020-12-01 15:58:53 +010039
40 # Tests need to assume, that main is default branch at init,
41 # which is not supported in config until 2.28.
42 cmd = ['git', 'init']
43 if git_command.git_require((2, 28, 0)):
44 cmd += ['--initial-branch=main']
45 else:
46 # Use template dir for init.
47 templatedir = tempfile.mkdtemp(prefix='.test-template')
48 with open(os.path.join(templatedir, 'HEAD'), 'w') as fp:
49 fp.write('ref: refs/heads/main\n')
Peter Kjellerstedtb8bf2912021-04-12 23:25:55 +020050 cmd += ['--template', templatedir]
Fredrik de Groot6342d562020-12-01 15:58:53 +010051 subprocess.check_call(cmd, cwd=tempdir)
Mike Frysinger6da17752019-09-11 18:43:17 -040052 yield tempdir
53 finally:
Mike Frysingerd9254592020-02-19 22:36:26 -050054 platform_utils.rmtree(tempdir)
Mike Frysinger6da17752019-09-11 18:43:17 -040055
56
Mike Frysinger6da17752019-09-11 18:43:17 -040057class FakeProject(object):
58 """A fake for Project for basic functionality."""
59
60 def __init__(self, worktree):
61 self.worktree = worktree
62 self.gitdir = os.path.join(worktree, '.git')
63 self.name = 'fakeproject'
64 self.work_git = project.Project._GitGetByExec(
65 self, bare=False, gitdir=self.gitdir)
66 self.bare_git = project.Project._GitGetByExec(
67 self, bare=True, gitdir=self.gitdir)
68 self.config = git_config.GitConfig.ForRepository(gitdir=self.gitdir)
69
70
71class ReviewableBranchTests(unittest.TestCase):
72 """Check ReviewableBranch behavior."""
73
74 def test_smoke(self):
75 """A quick run through everything."""
76 with TempGitTree() as tempdir:
77 fakeproj = FakeProject(tempdir)
78
79 # Generate some commits.
80 with open(os.path.join(tempdir, 'readme'), 'w') as fp:
81 fp.write('txt')
82 fakeproj.work_git.add('readme')
83 fakeproj.work_git.commit('-mAdd file')
84 fakeproj.work_git.checkout('-b', 'work')
85 fakeproj.work_git.rm('-f', 'readme')
86 fakeproj.work_git.commit('-mDel file')
87
88 # Start off with the normal details.
89 rb = project.ReviewableBranch(
Mike Frysingere283b952020-11-16 22:56:35 -050090 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -040091 self.assertEqual('work', rb.name)
92 self.assertEqual(1, len(rb.commits))
93 self.assertIn('Del file', rb.commits[0])
94 d = rb.unabbrev_commits
95 self.assertEqual(1, len(d))
96 short, long = next(iter(d.items()))
97 self.assertTrue(long.startswith(short))
98 self.assertTrue(rb.base_exists)
99 # Hard to assert anything useful about this.
100 self.assertTrue(rb.date)
101
102 # Now delete the tracking branch!
Mike Frysingere283b952020-11-16 22:56:35 -0500103 fakeproj.work_git.branch('-D', 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -0400104 rb = project.ReviewableBranch(
Mike Frysingere283b952020-11-16 22:56:35 -0500105 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -0400106 self.assertEqual(0, len(rb.commits))
107 self.assertFalse(rb.base_exists)
108 # Hard to assert anything useful about this.
109 self.assertTrue(rb.date)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400110
111
112class CopyLinkTestCase(unittest.TestCase):
113 """TestCase for stub repo client checkouts.
114
115 It'll have a layout like:
116 tempdir/ # self.tempdir
117 checkout/ # self.topdir
118 git-project/ # self.worktree
119
120 Attributes:
121 tempdir: A dedicated temporary directory.
122 worktree: The top of the repo client checkout.
123 topdir: The top of a project checkout.
124 """
125
126 def setUp(self):
127 self.tempdir = tempfile.mkdtemp(prefix='repo_tests')
128 self.topdir = os.path.join(self.tempdir, 'checkout')
129 self.worktree = os.path.join(self.topdir, 'git-project')
130 os.makedirs(self.topdir)
131 os.makedirs(self.worktree)
132
133 def tearDown(self):
134 shutil.rmtree(self.tempdir, ignore_errors=True)
135
136 @staticmethod
137 def touch(path):
David Pursehouse348e2182020-02-12 11:36:14 +0900138 with open(path, 'w'):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400139 pass
140
141 def assertExists(self, path, msg=None):
142 """Make sure |path| exists."""
143 if os.path.exists(path):
144 return
145
146 if msg is None:
147 msg = ['path is missing: %s' % path]
148 while path != '/':
149 path = os.path.dirname(path)
150 if not path:
151 # If we're given something like "foo", abort once we get to "".
152 break
153 result = os.path.exists(path)
154 msg.append('\tos.path.exists(%s): %s' % (path, result))
155 if result:
156 msg.append('\tcontents: %r' % os.listdir(path))
157 break
158 msg = '\n'.join(msg)
159
160 raise self.failureException(msg)
161
162
163class CopyFile(CopyLinkTestCase):
164 """Check _CopyFile handling."""
165
166 def CopyFile(self, src, dest):
167 return project._CopyFile(self.worktree, src, self.topdir, dest)
168
169 def test_basic(self):
170 """Basic test of copying a file from a project to the toplevel."""
171 src = os.path.join(self.worktree, 'foo.txt')
172 self.touch(src)
173 cf = self.CopyFile('foo.txt', 'foo')
174 cf._Copy()
175 self.assertExists(os.path.join(self.topdir, 'foo'))
176
177 def test_src_subdir(self):
178 """Copy a file from a subdir of a project."""
179 src = os.path.join(self.worktree, 'bar', 'foo.txt')
180 os.makedirs(os.path.dirname(src))
181 self.touch(src)
182 cf = self.CopyFile('bar/foo.txt', 'new.txt')
183 cf._Copy()
184 self.assertExists(os.path.join(self.topdir, 'new.txt'))
185
186 def test_dest_subdir(self):
187 """Copy a file to a subdir of a checkout."""
188 src = os.path.join(self.worktree, 'foo.txt')
189 self.touch(src)
190 cf = self.CopyFile('foo.txt', 'sub/dir/new.txt')
191 self.assertFalse(os.path.exists(os.path.join(self.topdir, 'sub')))
192 cf._Copy()
193 self.assertExists(os.path.join(self.topdir, 'sub', 'dir', 'new.txt'))
194
195 def test_update(self):
196 """Make sure changed files get copied again."""
197 src = os.path.join(self.worktree, 'foo.txt')
198 dest = os.path.join(self.topdir, 'bar')
199 with open(src, 'w') as f:
200 f.write('1st')
201 cf = self.CopyFile('foo.txt', 'bar')
202 cf._Copy()
203 self.assertExists(dest)
204 with open(dest) as f:
205 self.assertEqual(f.read(), '1st')
206
207 with open(src, 'w') as f:
208 f.write('2nd!')
209 cf._Copy()
210 with open(dest) as f:
211 self.assertEqual(f.read(), '2nd!')
212
213 def test_src_block_symlink(self):
214 """Do not allow reading from a symlinked path."""
215 src = os.path.join(self.worktree, 'foo.txt')
216 sym = os.path.join(self.worktree, 'sym')
217 self.touch(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500218 platform_utils.symlink('foo.txt', sym)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400219 self.assertExists(sym)
220 cf = self.CopyFile('sym', 'foo')
221 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
222
223 def test_src_block_symlink_traversal(self):
224 """Do not allow reading through a symlink dir."""
Mike Frysingerd9254592020-02-19 22:36:26 -0500225 realfile = os.path.join(self.tempdir, 'file.txt')
226 self.touch(realfile)
227 src = os.path.join(self.worktree, 'bar', 'file.txt')
228 platform_utils.symlink(self.tempdir, os.path.join(self.worktree, 'bar'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400229 self.assertExists(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500230 cf = self.CopyFile('bar/file.txt', 'foo')
Mike Frysingere6a202f2019-08-02 15:57:57 -0400231 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
232
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900233 def test_src_block_copy_from_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400234 """Do not allow copying from a directory."""
235 src = os.path.join(self.worktree, 'dir')
236 os.makedirs(src)
237 cf = self.CopyFile('dir', 'foo')
238 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
239
240 def test_dest_block_symlink(self):
241 """Do not allow writing to a symlink."""
242 src = os.path.join(self.worktree, 'foo.txt')
243 self.touch(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500244 platform_utils.symlink('dest', os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400245 cf = self.CopyFile('foo.txt', 'sym')
246 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
247
248 def test_dest_block_symlink_traversal(self):
249 """Do not allow writing through a symlink dir."""
250 src = os.path.join(self.worktree, 'foo.txt')
251 self.touch(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500252 platform_utils.symlink(tempfile.gettempdir(),
253 os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400254 cf = self.CopyFile('foo.txt', 'sym/foo.txt')
255 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
256
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900257 def test_src_block_copy_to_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400258 """Do not allow copying to a directory."""
259 src = os.path.join(self.worktree, 'foo.txt')
260 self.touch(src)
261 os.makedirs(os.path.join(self.topdir, 'dir'))
262 cf = self.CopyFile('foo.txt', 'dir')
263 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
264
265
266class LinkFile(CopyLinkTestCase):
267 """Check _LinkFile handling."""
268
269 def LinkFile(self, src, dest):
270 return project._LinkFile(self.worktree, src, self.topdir, dest)
271
272 def test_basic(self):
273 """Basic test of linking a file from a project into the toplevel."""
274 src = os.path.join(self.worktree, 'foo.txt')
275 self.touch(src)
276 lf = self.LinkFile('foo.txt', 'foo')
277 lf._Link()
278 dest = os.path.join(self.topdir, 'foo')
279 self.assertExists(dest)
280 self.assertTrue(os.path.islink(dest))
Mike Frysingerd9254592020-02-19 22:36:26 -0500281 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400282
283 def test_src_subdir(self):
284 """Link to a file in a subdir of a project."""
285 src = os.path.join(self.worktree, 'bar', 'foo.txt')
286 os.makedirs(os.path.dirname(src))
287 self.touch(src)
288 lf = self.LinkFile('bar/foo.txt', 'foo')
289 lf._Link()
290 self.assertExists(os.path.join(self.topdir, 'foo'))
291
Mike Frysinger07392ed2020-02-10 21:35:48 -0500292 def test_src_self(self):
293 """Link to the project itself."""
294 dest = os.path.join(self.topdir, 'foo', 'bar')
295 lf = self.LinkFile('.', 'foo/bar')
296 lf._Link()
297 self.assertExists(dest)
Mike Frysingerd9254592020-02-19 22:36:26 -0500298 self.assertEqual(os.path.join('..', 'git-project'), os.readlink(dest))
Mike Frysinger07392ed2020-02-10 21:35:48 -0500299
Mike Frysingere6a202f2019-08-02 15:57:57 -0400300 def test_dest_subdir(self):
301 """Link a file to a subdir of a checkout."""
302 src = os.path.join(self.worktree, 'foo.txt')
303 self.touch(src)
304 lf = self.LinkFile('foo.txt', 'sub/dir/foo/bar')
305 self.assertFalse(os.path.exists(os.path.join(self.topdir, 'sub')))
306 lf._Link()
307 self.assertExists(os.path.join(self.topdir, 'sub', 'dir', 'foo', 'bar'))
308
Mike Frysinger07392ed2020-02-10 21:35:48 -0500309 def test_src_block_relative(self):
310 """Do not allow relative symlinks."""
311 BAD_SOURCES = (
312 './',
313 '..',
314 '../',
315 'foo/.',
316 'foo/./bar',
317 'foo/..',
318 'foo/../foo',
319 )
320 for src in BAD_SOURCES:
321 lf = self.LinkFile(src, 'foo')
322 self.assertRaises(error.ManifestInvalidPathError, lf._Link)
323
Mike Frysingere6a202f2019-08-02 15:57:57 -0400324 def test_update(self):
325 """Make sure changed targets get updated."""
326 dest = os.path.join(self.topdir, 'sym')
327
328 src = os.path.join(self.worktree, 'foo.txt')
329 self.touch(src)
330 lf = self.LinkFile('foo.txt', 'sym')
331 lf._Link()
Mike Frysingerd9254592020-02-19 22:36:26 -0500332 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400333
334 # Point the symlink somewhere else.
335 os.unlink(dest)
Mike Frysingerd9254592020-02-19 22:36:26 -0500336 platform_utils.symlink(self.tempdir, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400337 lf._Link()
Mike Frysingerd9254592020-02-19 22:36:26 -0500338 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysinger2a089cf2021-11-13 23:29:42 -0500339
340
341class MigrateWorkTreeTests(unittest.TestCase):
342 """Check _MigrateOldWorkTreeGitDir handling."""
343
344 _SYMLINKS = {
345 'config', 'description', 'hooks', 'info', 'logs', 'objects',
346 'packed-refs', 'refs', 'rr-cache', 'shallow', 'svn',
347 }
348 _FILES = {
349 'COMMIT_EDITMSG', 'FETCH_HEAD', 'HEAD', 'index', 'ORIG_HEAD',
350 }
351
352 @classmethod
353 @contextlib.contextmanager
354 def _simple_layout(cls):
355 """Create a simple repo client checkout to test against."""
356 with tempfile.TemporaryDirectory() as tempdir:
357 tempdir = Path(tempdir)
358
359 gitdir = tempdir / '.repo/projects/src/test.git'
360 gitdir.mkdir(parents=True)
361 cmd = ['git', 'init', '--bare', str(gitdir)]
362 subprocess.check_call(cmd)
363
364 dotgit = tempdir / 'src/test/.git'
365 dotgit.mkdir(parents=True)
366 for name in cls._SYMLINKS:
367 (dotgit / name).symlink_to(f'../../../.repo/projects/src/test.git/{name}')
368 for name in cls._FILES:
369 (dotgit / name).write_text(name)
370
371 subprocess.run(['tree', '-a', str(dotgit)])
372 yield tempdir
373
374 def test_standard(self):
375 """Migrate a standard checkout that we expect."""
376 with self._simple_layout() as tempdir:
377 dotgit = tempdir / 'src/test/.git'
378 project.Project._MigrateOldWorkTreeGitDir(str(dotgit))
379
380 # Make sure the dir was transformed into a symlink.
381 self.assertTrue(dotgit.is_symlink())
382 self.assertEqual(str(dotgit.readlink()), '../../.repo/projects/src/test.git')
383
384 # Make sure files were moved over.
385 gitdir = tempdir / '.repo/projects/src/test.git'
386 for name in self._FILES:
387 self.assertEqual(name, (gitdir / name).read_text())