test_project.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2019 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unittests for the project.py module."""
  17. from __future__ import print_function
  18. import contextlib
  19. import os
  20. import shutil
  21. import subprocess
  22. import tempfile
  23. import unittest
  24. import git_config
  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. subprocess.check_call(['git', 'init'], cwd=tempdir)
  34. yield tempdir
  35. finally:
  36. shutil.rmtree(tempdir)
  37. class RepoHookShebang(unittest.TestCase):
  38. """Check shebang parsing in RepoHook."""
  39. def test_no_shebang(self):
  40. """Lines w/out shebangs should be rejected."""
  41. DATA = (
  42. '',
  43. '# -*- coding:utf-8 -*-\n',
  44. '#\n# foo\n',
  45. '# Bad shebang in script\n#!/foo\n'
  46. )
  47. for data in DATA:
  48. self.assertIsNone(project.RepoHook._ExtractInterpFromShebang(data))
  49. def test_direct_interp(self):
  50. """Lines whose shebang points directly to the interpreter."""
  51. DATA = (
  52. ('#!/foo', '/foo'),
  53. ('#! /foo', '/foo'),
  54. ('#!/bin/foo ', '/bin/foo'),
  55. ('#! /usr/foo ', '/usr/foo'),
  56. ('#! /usr/foo -args', '/usr/foo'),
  57. )
  58. for shebang, interp in DATA:
  59. self.assertEqual(project.RepoHook._ExtractInterpFromShebang(shebang),
  60. interp)
  61. def test_env_interp(self):
  62. """Lines whose shebang launches through `env`."""
  63. DATA = (
  64. ('#!/usr/bin/env foo', 'foo'),
  65. ('#!/bin/env foo', 'foo'),
  66. ('#! /bin/env /bin/foo ', '/bin/foo'),
  67. )
  68. for shebang, interp in DATA:
  69. self.assertEqual(project.RepoHook._ExtractInterpFromShebang(shebang),
  70. interp)
  71. class FakeProject(object):
  72. """A fake for Project for basic functionality."""
  73. def __init__(self, worktree):
  74. self.worktree = worktree
  75. self.gitdir = os.path.join(worktree, '.git')
  76. self.name = 'fakeproject'
  77. self.work_git = project.Project._GitGetByExec(
  78. self, bare=False, gitdir=self.gitdir)
  79. self.bare_git = project.Project._GitGetByExec(
  80. self, bare=True, gitdir=self.gitdir)
  81. self.config = git_config.GitConfig.ForRepository(gitdir=self.gitdir)
  82. class ReviewableBranchTests(unittest.TestCase):
  83. """Check ReviewableBranch behavior."""
  84. def test_smoke(self):
  85. """A quick run through everything."""
  86. with TempGitTree() as tempdir:
  87. fakeproj = FakeProject(tempdir)
  88. # Generate some commits.
  89. with open(os.path.join(tempdir, 'readme'), 'w') as fp:
  90. fp.write('txt')
  91. fakeproj.work_git.add('readme')
  92. fakeproj.work_git.commit('-mAdd file')
  93. fakeproj.work_git.checkout('-b', 'work')
  94. fakeproj.work_git.rm('-f', 'readme')
  95. fakeproj.work_git.commit('-mDel file')
  96. # Start off with the normal details.
  97. rb = project.ReviewableBranch(
  98. fakeproj, fakeproj.config.GetBranch('work'), 'master')
  99. self.assertEqual('work', rb.name)
  100. self.assertEqual(1, len(rb.commits))
  101. self.assertIn('Del file', rb.commits[0])
  102. d = rb.unabbrev_commits
  103. self.assertEqual(1, len(d))
  104. short, long = next(iter(d.items()))
  105. self.assertTrue(long.startswith(short))
  106. self.assertTrue(rb.base_exists)
  107. # Hard to assert anything useful about this.
  108. self.assertTrue(rb.date)
  109. # Now delete the tracking branch!
  110. fakeproj.work_git.branch('-D', 'master')
  111. rb = project.ReviewableBranch(
  112. fakeproj, fakeproj.config.GetBranch('work'), 'master')
  113. self.assertEqual(0, len(rb.commits))
  114. self.assertFalse(rb.base_exists)
  115. # Hard to assert anything useful about this.
  116. self.assertTrue(rb.date)