test_project.py 11 KB

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