test_setup.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Copyright (c) 2011 OpenStack Foundation
  2. # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
  3. # All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # 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, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. from __future__ import print_function
  17. import os
  18. try:
  19. import cStringIO as io
  20. BytesIO = io.StringIO
  21. except ImportError:
  22. import io
  23. BytesIO = io.BytesIO
  24. import fixtures
  25. from pbr import git
  26. from pbr import options
  27. from pbr.tests import base
  28. class SkipFileWrites(base.BaseTestCase):
  29. scenarios = [
  30. ('changelog_option_true',
  31. dict(option_key='skip_changelog', option_value='True',
  32. env_key='SKIP_WRITE_GIT_CHANGELOG', env_value=None,
  33. pkg_func=git.write_git_changelog, filename='ChangeLog')),
  34. ('changelog_option_false',
  35. dict(option_key='skip_changelog', option_value='False',
  36. env_key='SKIP_WRITE_GIT_CHANGELOG', env_value=None,
  37. pkg_func=git.write_git_changelog, filename='ChangeLog')),
  38. ('changelog_env_true',
  39. dict(option_key='skip_changelog', option_value='False',
  40. env_key='SKIP_WRITE_GIT_CHANGELOG', env_value='True',
  41. pkg_func=git.write_git_changelog, filename='ChangeLog')),
  42. ('changelog_both_true',
  43. dict(option_key='skip_changelog', option_value='True',
  44. env_key='SKIP_WRITE_GIT_CHANGELOG', env_value='True',
  45. pkg_func=git.write_git_changelog, filename='ChangeLog')),
  46. ('authors_option_true',
  47. dict(option_key='skip_authors', option_value='True',
  48. env_key='SKIP_GENERATE_AUTHORS', env_value=None,
  49. pkg_func=git.generate_authors, filename='AUTHORS')),
  50. ('authors_option_false',
  51. dict(option_key='skip_authors', option_value='False',
  52. env_key='SKIP_GENERATE_AUTHORS', env_value=None,
  53. pkg_func=git.generate_authors, filename='AUTHORS')),
  54. ('authors_env_true',
  55. dict(option_key='skip_authors', option_value='False',
  56. env_key='SKIP_GENERATE_AUTHORS', env_value='True',
  57. pkg_func=git.generate_authors, filename='AUTHORS')),
  58. ('authors_both_true',
  59. dict(option_key='skip_authors', option_value='True',
  60. env_key='SKIP_GENERATE_AUTHORS', env_value='True',
  61. pkg_func=git.generate_authors, filename='AUTHORS')),
  62. ]
  63. def setUp(self):
  64. super(SkipFileWrites, self).setUp()
  65. self.temp_path = self.useFixture(fixtures.TempDir()).path
  66. self.root_dir = os.path.abspath(os.path.curdir)
  67. self.git_dir = os.path.join(self.root_dir, ".git")
  68. if not os.path.exists(self.git_dir):
  69. self.skipTest("%s is missing; skipping git-related checks"
  70. % self.git_dir)
  71. return
  72. self.filename = os.path.join(self.temp_path, self.filename)
  73. self.option_dict = dict()
  74. if self.option_key is not None:
  75. self.option_dict[self.option_key] = ('setup.cfg',
  76. self.option_value)
  77. self.useFixture(
  78. fixtures.EnvironmentVariable(self.env_key, self.env_value))
  79. def test_skip(self):
  80. self.pkg_func(git_dir=self.git_dir,
  81. dest_dir=self.temp_path,
  82. option_dict=self.option_dict)
  83. self.assertEqual(
  84. not os.path.exists(self.filename),
  85. (self.option_value.lower() in options.TRUE_VALUES or
  86. self.env_value is not None))
  87. _changelog_content = """7780758\x00Break parser\x00 (tag: refs/tags/1_foo.1)
  88. 04316fe\x00Make python\x00 (refs/heads/review/monty_taylor/27519)
  89. 378261a\x00Add an integration test script.\x00
  90. 3c373ac\x00Merge "Lib\x00 (HEAD, tag: refs/tags/2013.2.rc2, tag: refs/tags/2013.2, refs/heads/mile-proposed)
  91. 182feb3\x00Fix pip invocation for old versions of pip.\x00 (tag: refs/tags/0.5.17)
  92. fa4f46e\x00Remove explicit depend on distribute.\x00 (tag: refs/tags/0.5.16)
  93. d1c53dd\x00Use pip instead of easy_install for installation.\x00
  94. a793ea1\x00Merge "Skip git-checkout related tests when .git is missing"\x00
  95. 6c27ce7\x00Skip git-checkout related tests when .git is missing\x00
  96. 451e513\x00Bug fix: create_stack() fails when waiting\x00
  97. 4c8cfe4\x00Improve test coverage: network delete API\x00 (tag: refs/tags/(evil))
  98. d7e6167\x00Bug fix: Fix pass thru filtering in list_networks\x00 (tag: refs/tags/ev()il)
  99. c47ec15\x00Consider 'in-use' a non-pending volume for caching\x00 (tag: refs/tags/ev)il)
  100. 8696fbd\x00Improve test coverage: private extension API\x00 (tag: refs/tags/ev(il)
  101. f0440f8\x00Improve test coverage: hypervisor list\x00 (tag: refs/tags/e(vi)l)
  102. 04984a5\x00Refactor hooks file.\x00 (HEAD, tag: 0.6.7,b, tag: refs/tags/(12), refs/heads/master)
  103. a65e8ee\x00Remove jinja pin.\x00 (tag: refs/tags/0.5.14, tag: refs/tags/0.5.13)
  104. """ # noqa
  105. def _make_old_git_changelog_format(line):
  106. """Convert post-1.8.1 git log format to pre-1.8.1 git log format"""
  107. if not line.strip():
  108. return line
  109. sha, msg, refname = line.split('\x00')
  110. refname = refname.replace('tag: ', '')
  111. return '\x00'.join((sha, msg, refname))
  112. _old_git_changelog_content = '\n'.join(
  113. _make_old_git_changelog_format(line)
  114. for line in _changelog_content.split('\n'))
  115. class GitLogsTest(base.BaseTestCase):
  116. scenarios = [
  117. ('pre1.8.3', {'changelog': _old_git_changelog_content}),
  118. ('post1.8.3', {'changelog': _changelog_content}),
  119. ]
  120. def setUp(self):
  121. super(GitLogsTest, self).setUp()
  122. self.temp_path = self.useFixture(fixtures.TempDir()).path
  123. self.root_dir = os.path.abspath(os.path.curdir)
  124. self.git_dir = os.path.join(self.root_dir, ".git")
  125. self.useFixture(
  126. fixtures.EnvironmentVariable('SKIP_GENERATE_AUTHORS'))
  127. self.useFixture(
  128. fixtures.EnvironmentVariable('SKIP_WRITE_GIT_CHANGELOG'))
  129. def test_write_git_changelog(self):
  130. self.useFixture(fixtures.FakePopen(lambda _: {
  131. "stdout": BytesIO(self.changelog.encode('utf-8'))
  132. }))
  133. git.write_git_changelog(git_dir=self.git_dir,
  134. dest_dir=self.temp_path)
  135. with open(os.path.join(self.temp_path, "ChangeLog"), "r") as ch_fh:
  136. changelog_contents = ch_fh.read()
  137. self.assertIn("2013.2", changelog_contents)
  138. self.assertIn("0.5.17", changelog_contents)
  139. self.assertIn("------", changelog_contents)
  140. self.assertIn("Refactor hooks file", changelog_contents)
  141. self.assertIn(
  142. r"Bug fix: create\_stack() fails when waiting",
  143. changelog_contents)
  144. self.assertNotIn("Refactor hooks file.", changelog_contents)
  145. self.assertNotIn("182feb3", changelog_contents)
  146. self.assertNotIn("review/monty_taylor/27519", changelog_contents)
  147. self.assertNotIn("0.5.13", changelog_contents)
  148. self.assertNotIn("0.6.7", changelog_contents)
  149. self.assertNotIn("12", changelog_contents)
  150. self.assertNotIn("(evil)", changelog_contents)
  151. self.assertNotIn("ev()il", changelog_contents)
  152. self.assertNotIn("ev(il", changelog_contents)
  153. self.assertNotIn("ev)il", changelog_contents)
  154. self.assertNotIn("e(vi)l", changelog_contents)
  155. self.assertNotIn('Merge "', changelog_contents)
  156. self.assertNotIn(r'1\_foo.1', changelog_contents)
  157. def test_generate_authors(self):
  158. author_old = u"Foo Foo <email@foo.com>"
  159. author_new = u"Bar Bar <email@bar.com>"
  160. co_author = u"Foo Bar <foo@bar.com>"
  161. co_author_by = u"Co-authored-by: " + co_author
  162. git_log_cmd = (
  163. "git --git-dir=%s log --format=%%aN <%%aE>"
  164. % self.git_dir)
  165. git_co_log_cmd = ("git --git-dir=%s log" % self.git_dir)
  166. git_top_level = "git rev-parse --show-toplevel"
  167. cmd_map = {
  168. git_log_cmd: author_new,
  169. git_co_log_cmd: co_author_by,
  170. git_top_level: self.root_dir,
  171. }
  172. exist_files = [self.git_dir,
  173. os.path.join(self.temp_path, "AUTHORS.in")]
  174. self.useFixture(fixtures.MonkeyPatch(
  175. "os.path.exists",
  176. lambda path: os.path.abspath(path) in exist_files))
  177. def _fake_run_shell_command(cmd, **kwargs):
  178. return cmd_map[" ".join(cmd)]
  179. self.useFixture(fixtures.MonkeyPatch(
  180. "pbr.git._run_shell_command",
  181. _fake_run_shell_command))
  182. with open(os.path.join(self.temp_path, "AUTHORS.in"), "w") as auth_fh:
  183. auth_fh.write("%s\n" % author_old)
  184. git.generate_authors(git_dir=self.git_dir,
  185. dest_dir=self.temp_path)
  186. with open(os.path.join(self.temp_path, "AUTHORS"), "r") as auth_fh:
  187. authors = auth_fh.read()
  188. self.assertIn(author_old, authors)
  189. self.assertIn(author_new, authors)
  190. self.assertIn(co_author, authors)