blob: 9b2cc4e91c0b3063c50f0215103ef22a38cffcb0 [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
19import shutil
20import 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."""
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 Groot6342d562020-12-01 15:58:53 +010038
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 Kjellerstedtb8bf2912021-04-12 23:25:55 +020049 cmd += ['--template', templatedir]
Fredrik de Groot6342d562020-12-01 15:58:53 +010050 subprocess.check_call(cmd, cwd=tempdir)
Mike Frysinger6da17752019-09-11 18:43:17 -040051 yield tempdir
52 finally:
Mike Frysingerd9254592020-02-19 22:36:26 -050053 platform_utils.rmtree(tempdir)
Mike Frysinger6da17752019-09-11 18:43:17 -040054
55
Mike Frysinger6da17752019-09-11 18:43:17 -040056class 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
70class 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 Frysingere283b952020-11-16 22:56:35 -050089 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -040090 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 Frysingere283b952020-11-16 22:56:35 -0500102 fakeproj.work_git.branch('-D', 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -0400103 rb = project.ReviewableBranch(
Mike Frysingere283b952020-11-16 22:56:35 -0500104 fakeproj, fakeproj.config.GetBranch('work'), 'main')
Mike Frysinger6da17752019-09-11 18:43:17 -0400105 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 Frysingere6a202f2019-08-02 15:57:57 -0400109
110
111class 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 Pursehouse348e2182020-02-12 11:36:14 +0900137 with open(path, 'w'):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400138 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
162class 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 Frysingerd9254592020-02-19 22:36:26 -0500217 platform_utils.symlink('foo.txt', sym)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400218 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 Frysingerd9254592020-02-19 22:36:26 -0500224 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 Frysingere6a202f2019-08-02 15:57:57 -0400228 self.assertExists(src)
Mike Frysingerd9254592020-02-19 22:36:26 -0500229 cf = self.CopyFile('bar/file.txt', 'foo')
Mike Frysingere6a202f2019-08-02 15:57:57 -0400230 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
231
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900232 def test_src_block_copy_from_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400233 """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 Frysingerd9254592020-02-19 22:36:26 -0500243 platform_utils.symlink('dest', os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400244 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 Frysingerd9254592020-02-19 22:36:26 -0500251 platform_utils.symlink(tempfile.gettempdir(),
252 os.path.join(self.topdir, 'sym'))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400253 cf = self.CopyFile('foo.txt', 'sym/foo.txt')
254 self.assertRaises(error.ManifestInvalidPathError, cf._Copy)
255
David Pursehouse4bbba7d2020-02-12 11:14:55 +0900256 def test_src_block_copy_to_dir(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400257 """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
265class 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 Frysingerd9254592020-02-19 22:36:26 -0500280 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400281
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 Frysinger07392ed2020-02-10 21:35:48 -0500291 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 Frysingerd9254592020-02-19 22:36:26 -0500297 self.assertEqual(os.path.join('..', 'git-project'), os.readlink(dest))
Mike Frysinger07392ed2020-02-10 21:35:48 -0500298
Mike Frysingere6a202f2019-08-02 15:57:57 -0400299 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 Frysinger07392ed2020-02-10 21:35:48 -0500308 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 Frysingere6a202f2019-08-02 15:57:57 -0400323 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 Frysingerd9254592020-02-19 22:36:26 -0500331 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))
Mike Frysingere6a202f2019-08-02 15:57:57 -0400332
333 # Point the symlink somewhere else.
334 os.unlink(dest)
Mike Frysingerd9254592020-02-19 22:36:26 -0500335 platform_utils.symlink(self.tempdir, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400336 lf._Link()
Mike Frysingerd9254592020-02-19 22:36:26 -0500337 self.assertEqual(os.path.join('git-project', 'foo.txt'), os.readlink(dest))