ghp_import_tests.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. from unittest import mock
  3. import os
  4. import subprocess
  5. import tempfile
  6. import unittest
  7. import shutil
  8. from mkdocs.utils import ghp_import
  9. class UtilsTests(unittest.TestCase):
  10. @mock.patch('subprocess.call', auto_spec=True)
  11. @mock.patch('subprocess.Popen', auto_spec=True)
  12. def test_try_rebase(self, mock_popen, mock_call):
  13. popen = mock.Mock()
  14. mock_popen.return_value = popen
  15. popen.communicate.return_value = (
  16. '4c82346e4b1b816be89dd709d35a6b169aa3df61\n', '')
  17. popen.wait.return_value = 0
  18. ghp_import.try_rebase('origin', 'gh-pages')
  19. mock_popen.assert_called_once_with(
  20. ['git', 'rev-list', '--max-count=1', 'origin/gh-pages'],
  21. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  22. stderr=subprocess.PIPE)
  23. mock_call.assert_called_once_with(
  24. ['git', 'update-ref', 'refs/heads/gh-pages',
  25. '4c82346e4b1b816be89dd709d35a6b169aa3df61'])
  26. @mock.patch('subprocess.Popen', auto_spec=True)
  27. def test_get_prev_commit(self, mock_popen):
  28. popen = mock.Mock()
  29. mock_popen.return_value = popen
  30. popen.communicate.return_value = (
  31. b'4c82346e4b1b816be89dd709d35a6b169aa3df61\n', '')
  32. popen.wait.return_value = 0
  33. result = ghp_import.get_prev_commit('test-branch')
  34. self.assertEqual(result, '4c82346e4b1b816be89dd709d35a6b169aa3df61')
  35. mock_popen.assert_called_once_with(
  36. ['git', 'rev-list', '--max-count=1', 'test-branch', '--'],
  37. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  38. stderr=subprocess.PIPE)
  39. @mock.patch('subprocess.Popen', auto_spec=True)
  40. def test_get_config(self, mock_popen):
  41. popen = mock.Mock()
  42. mock_popen.return_value = popen
  43. popen.communicate.return_value = (
  44. b'Dougal Matthews\n', '')
  45. result = ghp_import.get_config('user.name')
  46. self.assertEqual(result, 'Dougal Matthews')
  47. mock_popen.assert_called_once_with(
  48. ['git', 'config', 'user.name'],
  49. stdout=subprocess.PIPE, stdin=subprocess.PIPE)
  50. @mock.patch('mkdocs.utils.ghp_import.get_prev_commit')
  51. @mock.patch('mkdocs.utils.ghp_import.get_config')
  52. def test_start_commit(self, mock_get_config, mock_get_prev_commit):
  53. pipe = mock.Mock()
  54. mock_get_config.side_effect = ['username', 'email']
  55. mock_get_prev_commit.return_value = 'SHA'
  56. ghp_import.start_commit(pipe, 'test-branch', 'test-message')
  57. mock_get_prev_commit.assert_called_once_with('test-branch')
  58. self.assertEqual(pipe.stdin.write.call_count, 5)
  59. @mock.patch('mkdocs.utils.ghp_import.try_rebase', return_value=True)
  60. @mock.patch('mkdocs.utils.ghp_import.get_prev_commit', return_value='sha')
  61. @mock.patch('mkdocs.utils.ghp_import.get_config', return_value='config')
  62. @mock.patch('subprocess.call', auto_spec=True)
  63. @mock.patch('subprocess.Popen', auto_spec=True)
  64. def test_ghp_import(self, mock_popen, mock_call, mock_get_config,
  65. mock_get_prev_commit, mock_try_rebase):
  66. directory = tempfile.mkdtemp()
  67. open(os.path.join(directory, 'file'), 'a').close()
  68. try:
  69. popen = mock.Mock()
  70. mock_popen.return_value = popen
  71. popen.communicate.return_value = ('', '')
  72. popen.wait.return_value = 0
  73. ghp_import.ghp_import(directory, "test message",
  74. remote='fake-remote-name',
  75. branch='fake-branch-name')
  76. self.assertEqual(mock_popen.call_count, 2)
  77. self.assertEqual(mock_call.call_count, 0)
  78. finally:
  79. shutil.rmtree(directory)
  80. @mock.patch('mkdocs.utils.ghp_import.try_rebase', return_value=True)
  81. @mock.patch('mkdocs.utils.ghp_import.get_prev_commit', return_value='sha')
  82. @mock.patch('mkdocs.utils.ghp_import.get_config', return_value='config')
  83. @mock.patch('mkdocs.utils.ghp_import.run_import')
  84. @mock.patch('subprocess.call', auto_spec=True)
  85. @mock.patch('subprocess.Popen', auto_spec=True)
  86. def test_ghp_import_error(self, mock_popen, mock_call, mock_get_config,
  87. mock_run_import, mock_get_prev_commit, mock_try_rebase):
  88. directory = tempfile.mkdtemp()
  89. open(os.path.join(directory, 'file'), 'a').close()
  90. try:
  91. popen = mock.Mock()
  92. mock_popen.return_value = popen
  93. error_string = 'TestError123'
  94. popen.communicate.return_value = ('', error_string)
  95. popen.wait.return_value = 1
  96. result, ghp_error = ghp_import.ghp_import(directory, "test message",
  97. remote='fake-remote-name',
  98. branch='fake-branch-name')
  99. self.assertEqual(result, False)
  100. self.assertEqual(ghp_error, error_string)
  101. finally:
  102. shutil.rmtree(directory)