Debian Codegraph @

mkdocs/tests/gh_deploy_tests.py

raw
1 import unittest
2 from unittest import mock
3
4 from ghp_import import GhpError # type: ignore
5
6 from mkdocs import __version__
7 from mkdocs.commands import gh_deploy
8 from mkdocs.exceptions import Abort
9 from mkdocs.tests.base import load_config
10
11
12 class TestGitHubDeploy(unittest.TestCase):
13 @mock.patch('subprocess.Popen')
14 def test_is_cwd_git_repo(self, mock_popeno):
15 mock_popeno().wait.return_value = 0
16
17 self.assertTrue(gh_deploy._is_cwd_git_repo())
18
19 @mock.patch('subprocess.Popen')
20 def test_is_cwd_not_git_repo(self, mock_popeno):
21 mock_popeno().wait.return_value = 1
22
23 self.assertFalse(gh_deploy._is_cwd_git_repo())
24
25 @mock.patch('subprocess.Popen')
26 def test_get_current_sha(self, mock_popeno):
27 mock_popeno().communicate.return_value = (b'6d98394\n', b'')
28
29 self.assertEqual(gh_deploy._get_current_sha('.'), '6d98394')
30
31 @mock.patch('subprocess.Popen')
32 def test_get_remote_url_ssh(self, mock_popeno):
33 mock_popeno().communicate.return_value = (
34 b'[email protected]:mkdocs/mkdocs.git\n',
35 b'',
36 )
37
38 expected = ('git@', 'mkdocs/mkdocs.git')
39 self.assertEqual(expected, gh_deploy._get_remote_url('origin'))
40
41 @mock.patch('subprocess.Popen')
42 def test_get_remote_url_http(self, mock_popeno):
43 mock_popeno().communicate.return_value = (
44 b'https://github.com/mkdocs/mkdocs.git\n',
45 b'',
46 )
47
48 expected = ('https://', 'mkdocs/mkdocs.git')
49 self.assertEqual(expected, gh_deploy._get_remote_url('origin'))
50
51 @mock.patch('subprocess.Popen')
52 def test_get_remote_url_enterprise(self, mock_popeno):
53 mock_popeno().communicate.return_value = (
54 b'https://notgh.com/mkdocs/mkdocs.git\n',
55 b'',
56 )
57
58 expected = (None, None)
59 self.assertEqual(expected, gh_deploy._get_remote_url('origin'))
60
61 @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', mock.Mock(return_value=True))
62 @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', mock.Mock(return_value='shashas'))
63 @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', mock.Mock(return_value=(None, None)))
64 @mock.patch('mkdocs.commands.gh_deploy._check_version', mock.Mock())
65 @mock.patch('ghp_import.ghp_import', mock.Mock())
66 def test_deploy(self):
67 config = load_config(
68 remote_branch='test',
69 )
70 gh_deploy.gh_deploy(config)
71
72 @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', mock.Mock(return_value=True))
73 @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', mock.Mock(return_value='shashas'))
74 @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', mock.Mock(return_value=(None, None)))
75 @mock.patch('mkdocs.commands.gh_deploy._check_version', mock.Mock())
76 @mock.patch('ghp_import.ghp_import', mock.Mock())
77 @mock.patch('os.path.isfile', mock.Mock(return_value=False))
78 def test_deploy_no_cname(self):
79 config = load_config(
80 remote_branch='test',
81 )
82 gh_deploy.gh_deploy(config)
83
84 @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', mock.Mock(return_value=True))
85 @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', mock.Mock(return_value='shashas'))
86 @mock.patch(
87 'mkdocs.commands.gh_deploy._get_remote_url',
88 mock.Mock(return_value=('git@', 'mkdocs/mkdocs.git')),
89 )
90 @mock.patch('mkdocs.commands.gh_deploy._check_version', mock.Mock())
91 @mock.patch('ghp_import.ghp_import', mock.Mock())
92 def test_deploy_hostname(self):
93 config = load_config(
94 remote_branch='test',
95 )
96 gh_deploy.gh_deploy(config)
97
98 @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', mock.Mock(return_value=True))
99 @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', mock.Mock(return_value='shashas'))
100 @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', mock.Mock(return_value=(None, None)))
101 @mock.patch('mkdocs.commands.gh_deploy._check_version')
102 @mock.patch('ghp_import.ghp_import', mock.Mock())
103 def test_deploy_ignore_version_default(self, check_version):
104 config = load_config(
105 remote_branch='test',
106 )
107 gh_deploy.gh_deploy(config)
108 check_version.assert_called_once()
109
110 @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', mock.Mock(return_value=True))
111 @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', mock.Mock(return_value='shashas'))
112 @mock.patch('mkdocs.commands.gh_deploy._get_remote_url', mock.Mock(return_value=(None, None)))
113 @mock.patch('mkdocs.commands.gh_deploy._check_version')
114 @mock.patch('ghp_import.ghp_import', mock.Mock())
115 def test_deploy_ignore_version(self, check_version):
116 config = load_config(
117 remote_branch='test',
118 )
119 gh_deploy.gh_deploy(config, ignore_version=True)
120 check_version.assert_not_called()
121
122 @mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', mock.Mock(return_value=True))
123 @mock.patch('mkdocs.commands.gh_deploy._get_current_sha', mock.Mock(return_value='shashas'))
124 @mock.patch('mkdocs.commands.gh_deploy._check_version', mock.Mock())
125 @mock.patch('ghp_import.ghp_import', mock.Mock(side_effect=GhpError('TestError123')))
126 def test_deploy_error(self):
127 config = load_config(
128 remote_branch='test',
129 )
130
131 with self.assertLogs('mkdocs', level='ERROR') as cm:
132 with self.assertRaises(Abort):
133 gh_deploy.gh_deploy(config)
134 self.assertEqual(
135 cm.output,
136 [
137 'ERROR:mkdocs.commands.gh_deploy:Failed to deploy to GitHub with error: \n'
138 'TestError123'
139 ],
140 )
141
142
143 class TestGitHubDeployLogs(unittest.TestCase):
144 @mock.patch('subprocess.Popen')
145 def test_mkdocs_newer(self, mock_popeno):
146 mock_popeno().communicate.return_value = (
147 b'Deployed 12345678 with MkDocs version: 0.1.2\n',
148 b'',
149 )
150
151 with self.assertLogs('mkdocs') as cm:
152 gh_deploy._check_version('gh-pages')
153 self.assertEqual(
154 '\n'.join(cm.output),
155 f'INFO:mkdocs.commands.gh_deploy:Previous deployment was done with MkDocs '
156 f'version 0.1.2; you are deploying with a newer version ({__version__})',
157 )
158
159 @mock.patch('subprocess.Popen')
160 def test_mkdocs_older(self, mock_popeno):
161 mock_popeno().communicate.return_value = (
162 b'Deployed 12345678 with MkDocs version: 10.1.2\n',
163 b'',
164 )
165
166 with self.assertLogs('mkdocs', level='ERROR') as cm:
167 with self.assertRaises(Abort):
168 gh_deploy._check_version('gh-pages')
169 self.assertEqual(
170 '\n'.join(cm.output),
171 f'ERROR:mkdocs.commands.gh_deploy:Deployment terminated: Previous deployment was made with '
172 f'MkDocs version 10.1.2; you are attempting to deploy with an older version ({__version__}).'
173 f' Use --ignore-version to deploy anyway.',
174 )
175
176 @mock.patch('subprocess.Popen')
177 def test_version_unknown(self, mock_popeno):
178 mock_popeno().communicate.return_value = (b'No version specified\n', b'')
179
180 with self.assertLogs('mkdocs') as cm:
181 gh_deploy._check_version('gh-pages')
182 self.assertEqual(
183 '\n'.join(cm.output),
184 'WARNING:mkdocs.commands.gh_deploy:Version check skipped: No version specified in previous deployment.',
185 )