15
15
import shutil
16
16
import pytest
17
17
import git
18
+ import logging
18
19
19
20
from mkdocs_git_revision_date_localized_plugin .util import Util
20
21
@@ -44,6 +45,8 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
44
45
45
46
# Create empty 'testproject' folder
46
47
if os .path .exists (testproject_path ):
48
+ logging .warning ("""This command does not work on windows.
49
+ Refactor your test to use setup_clean_mkdocs_folder() only once""" )
47
50
shutil .rmtree (testproject_path )
48
51
49
52
# Copy correct mkdocs.yml file and our test 'docs/'
@@ -62,6 +65,9 @@ def setup_commit_history(testproject_path):
62
65
63
66
Args:
64
67
testproject_path (Path): Path to test project
68
+
69
+ Returns:
70
+ repo (repo): git.Repo object
65
71
"""
66
72
assert not os .path .exists (testproject_path / '.git' )
67
73
@@ -75,8 +81,19 @@ def setup_commit_history(testproject_path):
75
81
try :
76
82
repo .git .add ('mkdocs.yml' )
77
83
repo .git .commit (message = 'add mkdocs' , author = author )
84
+
78
85
repo .git .add ('docs/first_page.md' )
79
86
repo .git .commit (message = 'first page' , author = author )
87
+ file_name = os .path .join (testproject_path , 'docs/first_page.md' )
88
+ with open (file_name , 'w+' ) as the_file :
89
+ the_file .write ('Hello\n ' )
90
+ repo .git .add ('docs/first_page.md' )
91
+ repo .git .commit (message = 'first page update 1' , author = author )
92
+ with open (file_name , 'w' ) as the_file :
93
+ the_file .write ('# First Test Page Edited\n \n Some Lorem text' )
94
+ repo .git .add ('docs/first_page.md' )
95
+ repo .git .commit (message = 'first page update 2' , author = author )
96
+
80
97
repo .git .add ('docs/second_page.md' )
81
98
repo .git .commit (message = 'second page' , author = author )
82
99
repo .git .add ('docs/index.md' )
@@ -87,6 +104,8 @@ def setup_commit_history(testproject_path):
87
104
except :
88
105
os .chdir (cwd )
89
106
raise
107
+
108
+ return repo
90
109
91
110
def build_docs_setup (testproject_path ):
92
111
"""
@@ -225,23 +244,45 @@ def test_type_unknown(tmp_path):
225
244
tmp_path ,
226
245
'tests/basic_setup/mkdocs_unknown_type.yml' )
227
246
228
- def test_low_fetch_depth (tmp_path ):
247
+ def test_low_fetch_depth (tmp_path , caplog ):
229
248
"""
230
249
On gitlab and github runners, a GIT might have a low fetch
231
250
depth, which means commits are not available.
232
251
This should throw informative errors.
233
252
"""
234
253
235
- pass
254
+ testproject_path = setup_clean_mkdocs_folder ('tests/basic_setup/mkdocs.yml' , tmp_path )
255
+ repo = setup_commit_history (testproject_path )
236
256
237
- # Test correct error messages when GIT is not available
238
- #target_dir = os.path.join(tmp_path, 'nogit')
239
- #shutil.copytree(os.path.join(os.getcwd(), 'test/basic_setup'),
240
- # target_dir)
257
+ # Create a second, clean folder to clone to
258
+ cloned_folder = tmp_path .parent / 'clonedrepo'
259
+ if os .path .exists (cloned_folder ):
260
+ shutil .rmtree (cloned_folder )
261
+ os .mkdir (cloned_folder )
262
+
263
+ # Clone the local repo with fetch depth of 1
264
+ repo = git .Repo .init (cloned_folder , bare = False )
265
+ origin = repo .create_remote ('origin' , testproject_path )
266
+ origin .fetch (depth = 1 , prune = True )
267
+ repo .create_head ('master' , origin .refs .master ) # create local branch "master" from remote "master"
268
+ repo .heads .master .set_tracking_branch (origin .refs .master ) # set local "master" to track remote "master
269
+ repo .heads .master .checkout () # checkout local "master" to working tree
270
+
271
+ # should not raise warning
272
+ result = build_docs_setup (cloned_folder )
273
+ assert result .exit_code == 0
274
+
275
+ # should raise warning
276
+ os .environ ["GITLAB_CI" ] = "1"
277
+ result = build_docs_setup (cloned_folder )
278
+ assert result .exit_code == 0
279
+ assert 'Running on a gitlab runner' in caplog .text
280
+
281
+ del os .environ ['GITLAB_CI' ]
282
+ os .environ ["GITHUB_ACTIONS" ] = "1"
283
+ result = build_docs_setup (cloned_folder )
284
+ assert result .exit_code == 0
285
+ assert 'Running on github actions might' in caplog .text
241
286
242
- # ...
243
- #result = build_docs_setup(os.path.join(target_dir,'mkdocs.yml'), target_dir)
244
287
245
- #with pytest.warns(UserWarning):
246
- # assert result.exit_code == 0, "'mkdocs build' command failed"
247
-
288
+ # TODO: Test correct error messages when GIT is not available
0 commit comments