gh_deploy_tests.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import unittest
  2. from unittest import mock
  3. from mkdocs.tests.base import load_config
  4. from mkdocs.commands import gh_deploy
  5. from mkdocs import __version__
  6. class TestGitHubDeploy(unittest.TestCase):
  7. def assert_mock_called_once(self, mock):
  8. """assert that the mock was called only once.
  9. The `mock.assert_called_once()` method was added in PY36.
  10. TODO: Remove this when PY35 support is dropped.
  11. """
  12. try:
  13. mock.assert_called_once()
  14. except AttributeError:
  15. if not mock.call_count == 1:
  16. msg = ("Expected '%s' to have been called once. Called %s times." %
  17. (mock._mock_name or 'mock', self.call_count))
  18. raise AssertionError(msg)
  19. @mock.patch('subprocess.Popen')
  20. def test_is_cwd_git_repo(self, mock_popeno):
  21. mock_popeno().wait.return_value = 0
  22. self.assertTrue(gh_deploy._is_cwd_git_repo())
  23. @mock.patch('subprocess.Popen')
  24. def test_is_cwd_not_git_repo(self, mock_popeno):
  25. mock_popeno().wait.return_value = 1
  26. self.assertFalse(gh_deploy._is_cwd_git_repo())
  27. @mock.patch('subprocess.Popen')
  28. def test_get_current_sha(self, mock_popeno):
  29. mock_popeno().communicate.return_value = (b'6d98394\n', b'')
  30. self.assertEqual(gh_deploy._get_current_sha('.'), '6d98394')
  31. @mock.patch('subprocess.Popen')
  32. def test_get_remote_url_ssh(self, mock_popeno):
  33. mock_popeno().communicate.return_value = (
  34. b'git@github.com:mkdocs/mkdocs.git\n',
  35. b''
  36. )
  37. expected = ('git@', 'mkdocs/mkdocs.git')
  38. self.assertEqual(expected, gh_deploy._get_remote_url('origin'))
  39. @mock.patch('subprocess.Popen')
  40. def test_get_remote_url_http(self, mock_popeno):
  41. mock_popeno().communicate.return_value = (
  42. b'https://github.com/mkdocs/mkdocs.git\n',
  43. b''
  44. )
  45. expected = ('https://', 'mkdocs/mkdocs.git')
  46. self.assertEqual(expected, gh_deploy._get_remote_url('origin'))
  47. @mock.patch('subprocess.Popen')
  48. def test_get_remote_url_enterprise(self, mock_popeno):
  49. mock_popeno().communicate.return_value = (
  50. b'https://notgh.com/mkdocs/mkdocs.git\n',
  51. b''
  52. )
  53. expected = (None, None)
  54. self.assertEqual(expected, gh_deploy._get_remote_url('origin'))
  55. @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
  56. @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
  57. @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', return_value=(None, None))
  58. @mock.patch('mkdocs.commands.gh_deploy._check_version')
  59. @mock.patch('mkdocs.commands.gh_deploy.ghp_import.ghp_import', return_value=(True, ''))
  60. def test_deploy(self, mock_import, check_version, get_remote, get_sha, is_repo):
  61. config = load_config(
  62. remote_branch='test',
  63. )
  64. gh_deploy.gh_deploy(config)
  65. @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
  66. @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
  67. @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', return_value=(None, None))
  68. @mock.patch('mkdocs.commands.gh_deploy._check_version')
  69. @mock.patch('mkdocs.commands.gh_deploy.ghp_import.ghp_import', return_value=(True, ''))
  70. @mock.patch('os.path.isfile', return_value=False)
  71. def test_deploy_no_cname(self, mock_isfile, mock_import, check_version, get_remote,
  72. get_sha, is_repo):
  73. config = load_config(
  74. remote_branch='test',
  75. )
  76. gh_deploy.gh_deploy(config)
  77. @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
  78. @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
  79. @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', return_value=(
  80. 'git@', 'mkdocs/mkdocs.git'))
  81. @mock.patch('mkdocs.commands.gh_deploy._check_version')
  82. @mock.patch('mkdocs.commands.gh_deploy.ghp_import.ghp_import', return_value=(True, ''))
  83. def test_deploy_hostname(self, mock_import, check_version, get_remote, get_sha, is_repo):
  84. config = load_config(
  85. remote_branch='test',
  86. )
  87. gh_deploy.gh_deploy(config)
  88. @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
  89. @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
  90. @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', return_value=(None, None))
  91. @mock.patch('mkdocs.commands.gh_deploy._check_version')
  92. @mock.patch('mkdocs.commands.gh_deploy.ghp_import.ghp_import', return_value=(True, ''))
  93. def test_deploy_ignore_version_default(self, mock_import, check_version, get_remote, get_sha, is_repo):
  94. config = load_config(
  95. remote_branch='test',
  96. )
  97. gh_deploy.gh_deploy(config)
  98. self.assert_mock_called_once(check_version)
  99. @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
  100. @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
  101. @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', return_value=(None, None))
  102. @mock.patch('mkdocs.commands.gh_deploy._check_version')
  103. @mock.patch('mkdocs.commands.gh_deploy.ghp_import.ghp_import', return_value=(True, ''))
  104. def test_deploy_ignore_version(self, mock_import, check_version, get_remote, get_sha, is_repo):
  105. config = load_config(
  106. remote_branch='test',
  107. )
  108. gh_deploy.gh_deploy(config, ignore_version=True)
  109. check_version.assert_not_called()
  110. @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
  111. @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
  112. @mock.patch('mkdocs.commands.gh_deploy._check_version')
  113. @mock.patch('mkdocs.utils.ghp_import.ghp_import')
  114. @mock.patch('mkdocs.commands.gh_deploy.log')
  115. def test_deploy_error(self, mock_log, mock_import, check_version, get_sha, is_repo):
  116. error_string = 'TestError123'
  117. mock_import.return_value = (False, error_string)
  118. config = load_config(
  119. remote_branch='test',
  120. )
  121. self.assertRaises(SystemExit, gh_deploy.gh_deploy, config)
  122. mock_log.error.assert_called_once_with('Failed to deploy to GitHub with error: \n%s',
  123. error_string)
  124. class TestGitHubDeployLogs(unittest.TestCase):
  125. @mock.patch('subprocess.Popen')
  126. def test_mkdocs_newer(self, mock_popeno):
  127. mock_popeno().communicate.return_value = (b'Deployed 12345678 with MkDocs version: 0.1.2\n', b'')
  128. with self.assertLogs('mkdocs', level='INFO') as cm:
  129. gh_deploy._check_version('gh-pages')
  130. self.assertEqual(
  131. cm.output, ['INFO:mkdocs.commands.gh_deploy:Previous deployment was done with MkDocs '
  132. 'version 0.1.2; you are deploying with a newer version ({})'.format(__version__)]
  133. )
  134. @mock.patch('subprocess.Popen')
  135. def test_mkdocs_older(self, mock_popeno):
  136. mock_popeno().communicate.return_value = (b'Deployed 12345678 with MkDocs version: 10.1.2\n', b'')
  137. with self.assertLogs('mkdocs', level='ERROR') as cm:
  138. self.assertRaises(SystemExit, gh_deploy._check_version, 'gh-pages')
  139. self.assertEqual(
  140. cm.output, ['ERROR:mkdocs.commands.gh_deploy:Deployment terminated: Previous deployment was made with '
  141. 'MkDocs version 10.1.2; you are attempting to deploy with an older version ({}). Use '
  142. '--ignore-version to deploy anyway.'.format(__version__)]
  143. )
  144. @mock.patch('subprocess.Popen')
  145. def test_version_unknown(self, mock_popeno):
  146. mock_popeno().communicate.return_value = (b'No version specified\n', b'')
  147. with self.assertLogs('mkdocs', level='WARNING') as cm:
  148. gh_deploy._check_version('gh-pages')
  149. self.assertEqual(
  150. cm.output,
  151. ['WARNING:mkdocs.commands.gh_deploy:Version check skipped: No version specified in previous deployment.']
  152. )