| 1 |
import os |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from mkdocs.structure.files import File, Files, _sort_files, file_sort_key, get_files |
| 7 |
from mkdocs.tests.base import PathAssertionMixin, load_config, tempdir |
| 8 |
|
| 9 |
|
| 10 |
class TestFiles(PathAssertionMixin, unittest.TestCase): |
| 11 |
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 12 |
def test_src_path_windows(self): |
| 13 |
f = File('foo\\a.md', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 14 |
self.assertEqual(f.src_uri, 'foo/a.md') |
| 15 |
self.assertEqual(f.src_path, 'foo\\a.md') |
| 16 |
f.src_uri = 'foo/b.md' |
| 17 |
self.assertEqual(f.src_uri, 'foo/b.md') |
| 18 |
self.assertEqual(f.src_path, 'foo\\b.md') |
| 19 |
f.src_path = 'foo/c.md' |
| 20 |
self.assertEqual(f.src_uri, 'foo/c.md') |
| 21 |
self.assertEqual(f.src_path, 'foo\\c.md') |
| 22 |
f.src_path = 'foo\\d.md' |
| 23 |
self.assertEqual(f.src_uri, 'foo/d.md') |
| 24 |
self.assertEqual(f.src_path, 'foo\\d.md') |
| 25 |
f.src_uri = 'foo\\e.md' |
| 26 |
self.assertEqual(f.src_uri, 'foo\\e.md') |
| 27 |
self.assertEqual(f.src_path, 'foo\\e.md') |
| 28 |
|
| 29 |
def test_sort_files(self): |
| 30 |
self.assertEqual( |
| 31 |
_sort_files(['b.md', 'bb.md', 'a.md', 'index.md', 'aa.md']), |
| 32 |
['index.md', 'a.md', 'aa.md', 'b.md', 'bb.md'], |
| 33 |
) |
| 34 |
|
| 35 |
self.assertEqual( |
| 36 |
_sort_files(['b.md', 'index.html', 'a.md', 'index.md']), |
| 37 |
['index.html', 'index.md', 'a.md', 'b.md'], |
| 38 |
) |
| 39 |
|
| 40 |
self.assertEqual( |
| 41 |
_sort_files(['a.md', 'index.md', 'b.md', 'index.html']), |
| 42 |
['index.html', 'index.md', 'a.md', 'b.md'], |
| 43 |
) |
| 44 |
|
| 45 |
self.assertEqual( |
| 46 |
_sort_files(['.md', '_.md', 'a.md', 'index.md', '1.md']), |
| 47 |
['index.md', '.md', '1.md', '_.md', 'a.md'], |
| 48 |
) |
| 49 |
|
| 50 |
self.assertEqual( |
| 51 |
_sort_files(['a.md', 'b.md', 'a.md']), |
| 52 |
['a.md', 'a.md', 'b.md'], |
| 53 |
) |
| 54 |
|
| 55 |
self.assertEqual( |
| 56 |
_sort_files(['A.md', 'B.md', 'README.md']), |
| 57 |
['README.md', 'A.md', 'B.md'], |
| 58 |
) |
| 59 |
|
| 60 |
def test_file_sort_key(self): |
| 61 |
for case in [ |
| 62 |
["a/b.md", "b/index.md", "b/a.md"], |
| 63 |
["SUMMARY.md", "foo/z.md", "foo/bar/README.md", "foo/bar/index.md", "foo/bar/a.md"], |
| 64 |
]: |
| 65 |
with self.subTest(case): |
| 66 |
files = [File(f, "", "", use_directory_urls=True) for f in case] |
| 67 |
for a, b in zip(files, files[1:]): |
| 68 |
self.assertLess(file_sort_key(a), file_sort_key(b)) |
| 69 |
|
| 70 |
def test_md_file(self): |
| 71 |
for use_directory_urls in True, False: |
| 72 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 73 |
f = File('foo.md', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 74 |
self.assertEqual(f.src_uri, 'foo.md') |
| 75 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo.md') |
| 76 |
if use_directory_urls: |
| 77 |
self.assertEqual(f.dest_uri, 'foo/index.html') |
| 78 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/index.html') |
| 79 |
self.assertEqual(f.url, 'foo/') |
| 80 |
else: |
| 81 |
self.assertEqual(f.dest_uri, 'foo.html') |
| 82 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo.html') |
| 83 |
self.assertEqual(f.url, 'foo.html') |
| 84 |
self.assertEqual(f.name, 'foo') |
| 85 |
self.assertTrue(f.is_documentation_page()) |
| 86 |
self.assertFalse(f.is_static_page()) |
| 87 |
self.assertFalse(f.is_media_file()) |
| 88 |
self.assertFalse(f.is_javascript()) |
| 89 |
self.assertFalse(f.is_css()) |
| 90 |
|
| 91 |
def test_md_file_nested(self): |
| 92 |
for use_directory_urls in True, False: |
| 93 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 94 |
f = File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 95 |
self.assertEqual(f.src_uri, 'foo/bar.md') |
| 96 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.md') |
| 97 |
if use_directory_urls: |
| 98 |
self.assertEqual(f.dest_uri, 'foo/bar/index.html') |
| 99 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar/index.html') |
| 100 |
self.assertEqual(f.url, 'foo/bar/') |
| 101 |
else: |
| 102 |
self.assertEqual(f.dest_uri, 'foo/bar.html') |
| 103 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.html') |
| 104 |
self.assertEqual(f.url, 'foo/bar.html') |
| 105 |
self.assertEqual(f.name, 'bar') |
| 106 |
self.assertTrue(f.is_documentation_page()) |
| 107 |
self.assertFalse(f.is_static_page()) |
| 108 |
self.assertFalse(f.is_media_file()) |
| 109 |
self.assertFalse(f.is_javascript()) |
| 110 |
self.assertFalse(f.is_css()) |
| 111 |
|
| 112 |
def test_md_index_file(self): |
| 113 |
for use_directory_urls in True, False: |
| 114 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 115 |
f = File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 116 |
self.assertEqual(f.src_uri, 'index.md') |
| 117 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/index.md') |
| 118 |
self.assertEqual(f.dest_uri, 'index.html') |
| 119 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/index.html') |
| 120 |
if use_directory_urls: |
| 121 |
self.assertEqual(f.url, './') |
| 122 |
else: |
| 123 |
self.assertEqual(f.url, 'index.html') |
| 124 |
self.assertEqual(f.name, 'index') |
| 125 |
self.assertTrue(f.is_documentation_page()) |
| 126 |
self.assertFalse(f.is_static_page()) |
| 127 |
self.assertFalse(f.is_media_file()) |
| 128 |
self.assertFalse(f.is_javascript()) |
| 129 |
self.assertFalse(f.is_css()) |
| 130 |
|
| 131 |
def test_md_readme_index_file(self): |
| 132 |
for use_directory_urls in True, False: |
| 133 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 134 |
f = File('README.md', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 135 |
self.assertEqual(f.src_uri, 'README.md') |
| 136 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/README.md') |
| 137 |
self.assertEqual(f.dest_uri, 'index.html') |
| 138 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/index.html') |
| 139 |
if use_directory_urls: |
| 140 |
self.assertEqual(f.url, './') |
| 141 |
else: |
| 142 |
self.assertEqual(f.url, 'index.html') |
| 143 |
self.assertEqual(f.name, 'index') |
| 144 |
self.assertTrue(f.is_documentation_page()) |
| 145 |
self.assertFalse(f.is_static_page()) |
| 146 |
self.assertFalse(f.is_media_file()) |
| 147 |
self.assertFalse(f.is_javascript()) |
| 148 |
self.assertFalse(f.is_css()) |
| 149 |
|
| 150 |
def test_md_index_file_nested(self): |
| 151 |
for use_directory_urls in True, False: |
| 152 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 153 |
f = File('foo/index.md', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 154 |
self.assertEqual(f.src_uri, 'foo/index.md') |
| 155 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/index.md') |
| 156 |
self.assertEqual(f.dest_uri, 'foo/index.html') |
| 157 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/index.html') |
| 158 |
if use_directory_urls: |
| 159 |
self.assertEqual(f.url, 'foo/') |
| 160 |
else: |
| 161 |
self.assertEqual(f.url, 'foo/index.html') |
| 162 |
self.assertEqual(f.name, 'index') |
| 163 |
self.assertTrue(f.is_documentation_page()) |
| 164 |
self.assertFalse(f.is_static_page()) |
| 165 |
self.assertFalse(f.is_media_file()) |
| 166 |
self.assertFalse(f.is_javascript()) |
| 167 |
self.assertFalse(f.is_css()) |
| 168 |
|
| 169 |
def test_static_file(self): |
| 170 |
for use_directory_urls in True, False: |
| 171 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 172 |
f = File('foo/bar.html', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 173 |
self.assertEqual(f.src_uri, 'foo/bar.html') |
| 174 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.html') |
| 175 |
self.assertEqual(f.dest_uri, 'foo/bar.html') |
| 176 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.html') |
| 177 |
self.assertEqual(f.url, 'foo/bar.html') |
| 178 |
self.assertEqual(f.name, 'bar') |
| 179 |
self.assertFalse(f.is_documentation_page()) |
| 180 |
self.assertTrue(f.is_static_page()) |
| 181 |
self.assertFalse(f.is_media_file()) |
| 182 |
self.assertFalse(f.is_javascript()) |
| 183 |
self.assertFalse(f.is_css()) |
| 184 |
|
| 185 |
def test_media_file(self): |
| 186 |
for use_directory_urls in True, False: |
| 187 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 188 |
f = File('foo/bar.jpg', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 189 |
self.assertEqual(f.src_uri, 'foo/bar.jpg') |
| 190 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.jpg') |
| 191 |
self.assertEqual(f.dest_uri, 'foo/bar.jpg') |
| 192 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.jpg') |
| 193 |
self.assertEqual(f.url, 'foo/bar.jpg') |
| 194 |
self.assertEqual(f.name, 'bar') |
| 195 |
self.assertFalse(f.is_documentation_page()) |
| 196 |
self.assertFalse(f.is_static_page()) |
| 197 |
self.assertTrue(f.is_media_file()) |
| 198 |
self.assertFalse(f.is_javascript()) |
| 199 |
self.assertFalse(f.is_css()) |
| 200 |
|
| 201 |
def test_javascript_file(self): |
| 202 |
for use_directory_urls in True, False: |
| 203 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 204 |
f = File('foo/bar.js', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 205 |
self.assertEqual(f.src_uri, 'foo/bar.js') |
| 206 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.js') |
| 207 |
self.assertEqual(f.dest_uri, 'foo/bar.js') |
| 208 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.js') |
| 209 |
self.assertEqual(f.url, 'foo/bar.js') |
| 210 |
self.assertEqual(f.name, 'bar') |
| 211 |
self.assertFalse(f.is_documentation_page()) |
| 212 |
self.assertFalse(f.is_static_page()) |
| 213 |
self.assertTrue(f.is_media_file()) |
| 214 |
self.assertTrue(f.is_javascript()) |
| 215 |
self.assertFalse(f.is_css()) |
| 216 |
|
| 217 |
def test_css_file(self): |
| 218 |
for use_directory_urls in True, False: |
| 219 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 220 |
f = File('foo/bar.css', '/path/to/docs', '/path/to/site', use_directory_urls) |
| 221 |
self.assertEqual(f.src_uri, 'foo/bar.css') |
| 222 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.css') |
| 223 |
self.assertEqual(f.dest_uri, 'foo/bar.css') |
| 224 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.css') |
| 225 |
self.assertEqual(f.url, 'foo/bar.css') |
| 226 |
self.assertEqual(f.name, 'bar') |
| 227 |
self.assertFalse(f.is_documentation_page()) |
| 228 |
self.assertFalse(f.is_static_page()) |
| 229 |
self.assertTrue(f.is_media_file()) |
| 230 |
self.assertFalse(f.is_javascript()) |
| 231 |
self.assertTrue(f.is_css()) |
| 232 |
|
| 233 |
def test_file_name_with_space(self): |
| 234 |
f = File('foo bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 235 |
self.assertEqual(f.src_uri, 'foo bar.md') |
| 236 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo bar.md') |
| 237 |
self.assertEqual(f.dest_uri, 'foo bar.html') |
| 238 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo bar.html') |
| 239 |
self.assertEqual(f.url, 'foo%20bar.html') |
| 240 |
self.assertEqual(f.name, 'foo bar') |
| 241 |
|
| 242 |
def test_file_name_with_custom_dest_uri(self): |
| 243 |
for use_directory_urls in True, False: |
| 244 |
with self.subTest(use_directory_urls=use_directory_urls): |
| 245 |
f = File( |
| 246 |
'stuff/foo.md', |
| 247 |
src_dir='/path/to/docs', |
| 248 |
dest_dir='/path/to/site', |
| 249 |
use_directory_urls=use_directory_urls, |
| 250 |
dest_uri='stuff/1-foo/index.html', |
| 251 |
) |
| 252 |
self.assertEqual(f.src_uri, 'stuff/foo.md') |
| 253 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/stuff/foo.md') |
| 254 |
self.assertEqual(f.dest_uri, 'stuff/1-foo/index.html') |
| 255 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/stuff/1-foo/index.html') |
| 256 |
if use_directory_urls: |
| 257 |
self.assertEqual(f.url, 'stuff/1-foo/') |
| 258 |
else: |
| 259 |
self.assertEqual(f.url, 'stuff/1-foo/index.html') |
| 260 |
self.assertEqual(f.name, 'foo') |
| 261 |
|
| 262 |
def test_file_overwrite_attrs(self): |
| 263 |
f = File('foo.md', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 264 |
self.assertEqual(f.src_uri, 'foo.md') |
| 265 |
|
| 266 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo.md') |
| 267 |
f.abs_src_path = '/tmp/foo.md' |
| 268 |
self.assertPathsEqual(f.abs_src_path, '/tmp/foo.md') |
| 269 |
del f.abs_src_path |
| 270 |
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo.md') |
| 271 |
|
| 272 |
f.dest_uri = 'a.html' |
| 273 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/a.html') |
| 274 |
self.assertEqual(f.url, 'a.html') |
| 275 |
f.abs_dest_path = '/path/to/site/b.html' |
| 276 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/b.html') |
| 277 |
self.assertEqual(f.url, 'a.html') |
| 278 |
del f.url |
| 279 |
del f.dest_uri |
| 280 |
del f.abs_dest_path |
| 281 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo.html') |
| 282 |
|
| 283 |
self.assertTrue(f.is_documentation_page()) |
| 284 |
f.src_uri = 'foo.html' |
| 285 |
del f.name |
| 286 |
self.assertFalse(f.is_documentation_page()) |
| 287 |
|
| 288 |
def test_generated_file(self): |
| 289 |
f = File( |
| 290 |
'foo/bar.md', |
| 291 |
src_dir=None, |
| 292 |
dest_dir='/path/to/site', |
| 293 |
use_directory_urls=False, |
| 294 |
) |
| 295 |
f.content_string = 'вміст' |
| 296 |
f.generated_by = 'some-plugin' |
| 297 |
self.assertEqual(f.generated_by, 'some-plugin') |
| 298 |
self.assertEqual(f.src_uri, 'foo/bar.md') |
| 299 |
self.assertIsNone(f.abs_src_path) |
| 300 |
self.assertIsNone(f.src_dir) |
| 301 |
self.assertEqual(f.dest_uri, 'foo/bar.html') |
| 302 |
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.html') |
| 303 |
self.assertEqual(f.content_string, 'вміст') |
| 304 |
self.assertEqual(f.edit_uri, None) |
| 305 |
|
| 306 |
@tempdir(files={'x.md': 'вміст'}) |
| 307 |
def test_generated_file_constructor(self, tdir) -> None: |
| 308 |
config = load_config(site_dir='/path/to/site', use_directory_urls=False) |
| 309 |
config.plugins._current_plugin = 'foo' |
| 310 |
for f in [ |
| 311 |
File.generated(config, 'foo/bar.md', content='вміст'), |
| 312 |
File.generated(config, 'foo/bar.md', content='вміст'.encode()), |
| 313 |
File.generated(config, 'foo/bar.md', abs_src_path=os.path.join(tdir, 'x.md')), |
| 314 |
]: |
| 315 |
self.assertEqual(f.src_uri, 'foo/bar.md') |
| 316 |
self.assertIsNone(f.src_dir) |
| 317 |
self.assertEqual(f.dest_uri, 'foo/bar.html') |
| 318 |
self.assertPathsEqual(f.abs_dest_path, os.path.abspath('/path/to/site/foo/bar.html')) |
| 319 |
self.assertEqual(f.content_string, 'вміст') |
| 320 |
self.assertEqual(f.content_bytes, 'вміст'.encode()) |
| 321 |
self.assertEqual(f.edit_uri, None) |
| 322 |
|
| 323 |
def test_files(self): |
| 324 |
fs = [ |
| 325 |
File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 326 |
File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 327 |
File('foo/bar.html', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 328 |
File('foo/bar.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 329 |
File('foo/bar.js', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 330 |
File('foo/bar.css', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 331 |
] |
| 332 |
files = Files(fs) |
| 333 |
self.assertEqual(list(files), fs) |
| 334 |
self.assertEqual(len(files), 6) |
| 335 |
self.assertEqual(files.documentation_pages(), [fs[0], fs[1]]) |
| 336 |
self.assertEqual(files.static_pages(), [fs[2]]) |
| 337 |
self.assertEqual(files.media_files(), [fs[3], fs[4], fs[5]]) |
| 338 |
self.assertEqual(files.javascript_files(), [fs[4]]) |
| 339 |
self.assertEqual(files.css_files(), [fs[5]]) |
| 340 |
self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3]) |
| 341 |
self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3]) |
| 342 |
self.assertEqual(files.get_file_from_path('missing.jpg'), None) |
| 343 |
self.assertTrue(fs[2].src_uri in files.src_uris) |
| 344 |
extra_file = File('extra.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 345 |
self.assertFalse(extra_file.src_uri in files.src_uris) |
| 346 |
files.append(extra_file) |
| 347 |
self.assertEqual(len(files), 7) |
| 348 |
self.assertTrue(extra_file.src_uri in files.src_uris) |
| 349 |
self.assertEqual(files.documentation_pages(), [fs[0], fs[1], extra_file]) |
| 350 |
files.remove(fs[1]) |
| 351 |
self.assertEqual(files.documentation_pages(), [fs[0], extra_file]) |
| 352 |
|
| 353 |
@tempdir( |
| 354 |
files=[ |
| 355 |
'favicon.ico', |
| 356 |
'index.md', |
| 357 |
] |
| 358 |
) |
| 359 |
@tempdir( |
| 360 |
files=[ |
| 361 |
'base.html', |
| 362 |
'favicon.ico', |
| 363 |
'style.css', |
| 364 |
'foo.md', |
| 365 |
'README', |
| 366 |
'.ignore.txt', |
| 367 |
'.ignore/file.txt', |
| 368 |
'foo/.ignore.txt', |
| 369 |
'foo/.ignore/file.txt', |
| 370 |
] |
| 371 |
) |
| 372 |
def test_add_files_from_theme(self, tdir, ddir): |
| 373 |
config = load_config(docs_dir=ddir, theme={'name': None, 'custom_dir': tdir}) |
| 374 |
env = config.theme.get_env() |
| 375 |
files = get_files(config) |
| 376 |
self.assertEqual( |
| 377 |
[file.src_uri for file in files], |
| 378 |
['index.md', 'favicon.ico'], |
| 379 |
) |
| 380 |
files.add_files_from_theme(env, config) |
| 381 |
self.assertEqual( |
| 382 |
[file.src_uri for file in files], |
| 383 |
['index.md', 'favicon.ico', 'style.css'], |
| 384 |
) |
| 385 |
# Ensure theme file does not override docs_dir file |
| 386 |
self.assertEqual( |
| 387 |
files.get_file_from_path('favicon.ico').abs_src_path, |
| 388 |
os.path.normpath(os.path.join(ddir, 'favicon.ico')), |
| 389 |
) |
| 390 |
|
| 391 |
def test_get_relative_url_use_directory_urls(self): |
| 392 |
to_files = [ |
| 393 |
'index.md', |
| 394 |
'foo/index.md', |
| 395 |
'foo/bar/index.md', |
| 396 |
'foo/bar/baz/index.md', |
| 397 |
'foo.md', |
| 398 |
'foo/bar.md', |
| 399 |
'foo/bar/baz.md', |
| 400 |
] |
| 401 |
to_file_urls = [ |
| 402 |
'./', |
| 403 |
'foo/', |
| 404 |
'foo/bar/', |
| 405 |
'foo/bar/baz/', |
| 406 |
'foo/', |
| 407 |
'foo/bar/', |
| 408 |
'foo/bar/baz/', |
| 409 |
] |
| 410 |
|
| 411 |
from_file = File('img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 412 |
self.assertEqual(from_file.url, 'img.jpg') |
| 413 |
|
| 414 |
expected = [ |
| 415 |
'img.jpg', # img.jpg relative to . |
| 416 |
'../img.jpg', # img.jpg relative to foo/ |
| 417 |
'../../img.jpg', # img.jpg relative to foo/bar/ |
| 418 |
'../../../img.jpg', # img.jpg relative to foo/bar/baz/ |
| 419 |
'../img.jpg', # img.jpg relative to foo |
| 420 |
'../../img.jpg', # img.jpg relative to foo/bar |
| 421 |
'../../../img.jpg', # img.jpg relative to foo/bar/baz |
| 422 |
] |
| 423 |
for i, filename in enumerate(to_files): |
| 424 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 425 |
self.assertEqual(file.url, to_file_urls[i]) |
| 426 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 427 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 428 |
|
| 429 |
from_file = File('foo/img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 430 |
self.assertEqual(from_file.url, 'foo/img.jpg') |
| 431 |
|
| 432 |
expected = [ |
| 433 |
'foo/img.jpg', # foo/img.jpg relative to . |
| 434 |
'img.jpg', # foo/img.jpg relative to foo/ |
| 435 |
'../img.jpg', # foo/img.jpg relative to foo/bar/ |
| 436 |
'../../img.jpg', # foo/img.jpg relative to foo/bar/baz/ |
| 437 |
'img.jpg', # foo/img.jpg relative to foo |
| 438 |
'../img.jpg', # foo/img.jpg relative to foo/bar |
| 439 |
'../../img.jpg', # foo/img.jpg relative to foo/bar/baz |
| 440 |
] |
| 441 |
for i, filename in enumerate(to_files): |
| 442 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 443 |
self.assertEqual(file.url, to_file_urls[i]) |
| 444 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 445 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 446 |
|
| 447 |
from_file = File('index.html', '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 448 |
self.assertEqual(from_file.url, './') |
| 449 |
|
| 450 |
expected = [ |
| 451 |
'./', # . relative to . |
| 452 |
'../', # . relative to foo/ |
| 453 |
'../../', # . relative to foo/bar/ |
| 454 |
'../../../', # . relative to foo/bar/baz/ |
| 455 |
'../', # . relative to foo |
| 456 |
'../../', # . relative to foo/bar |
| 457 |
'../../../', # . relative to foo/bar/baz |
| 458 |
] |
| 459 |
for i, filename in enumerate(to_files): |
| 460 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 461 |
self.assertEqual(file.url, to_file_urls[i]) |
| 462 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 463 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 464 |
|
| 465 |
from_file = File('file.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 466 |
self.assertEqual(from_file.url, 'file/') |
| 467 |
|
| 468 |
expected = [ |
| 469 |
'file/', # file relative to . |
| 470 |
'../file/', # file relative to foo/ |
| 471 |
'../../file/', # file relative to foo/bar/ |
| 472 |
'../../../file/', # file relative to foo/bar/baz/ |
| 473 |
'../file/', # file relative to foo |
| 474 |
'../../file/', # file relative to foo/bar |
| 475 |
'../../../file/', # file relative to foo/bar/baz |
| 476 |
] |
| 477 |
for i, filename in enumerate(to_files): |
| 478 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 479 |
self.assertEqual(file.url, to_file_urls[i]) |
| 480 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 481 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 482 |
|
| 483 |
def test_get_relative_url(self): |
| 484 |
to_files = [ |
| 485 |
'index.md', |
| 486 |
'foo/index.md', |
| 487 |
'foo/bar/index.md', |
| 488 |
'foo/bar/baz/index.md', |
| 489 |
'foo.md', |
| 490 |
'foo/bar.md', |
| 491 |
'foo/bar/baz.md', |
| 492 |
] |
| 493 |
to_file_urls = [ |
| 494 |
'index.html', |
| 495 |
'foo/index.html', |
| 496 |
'foo/bar/index.html', |
| 497 |
'foo/bar/baz/index.html', |
| 498 |
'foo.html', |
| 499 |
'foo/bar.html', |
| 500 |
'foo/bar/baz.html', |
| 501 |
] |
| 502 |
|
| 503 |
from_file = File('img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 504 |
self.assertEqual(from_file.url, 'img.jpg') |
| 505 |
|
| 506 |
expected = [ |
| 507 |
'img.jpg', # img.jpg relative to . |
| 508 |
'../img.jpg', # img.jpg relative to foo/ |
| 509 |
'../../img.jpg', # img.jpg relative to foo/bar/ |
| 510 |
'../../../img.jpg', # img.jpg relative to foo/bar/baz/ |
| 511 |
'img.jpg', # img.jpg relative to foo.html |
| 512 |
'../img.jpg', # img.jpg relative to foo/bar.html |
| 513 |
'../../img.jpg', # img.jpg relative to foo/bar/baz.html |
| 514 |
] |
| 515 |
for i, filename in enumerate(to_files): |
| 516 |
with self.subTest(from_file=from_file.src_path, to_file=filename): |
| 517 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 518 |
self.assertEqual(file.url, to_file_urls[i]) |
| 519 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 520 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 521 |
|
| 522 |
from_file = File('foo/img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 523 |
self.assertEqual(from_file.url, 'foo/img.jpg') |
| 524 |
|
| 525 |
expected = [ |
| 526 |
'foo/img.jpg', # foo/img.jpg relative to . |
| 527 |
'img.jpg', # foo/img.jpg relative to foo/ |
| 528 |
'../img.jpg', # foo/img.jpg relative to foo/bar/ |
| 529 |
'../../img.jpg', # foo/img.jpg relative to foo/bar/baz/ |
| 530 |
'foo/img.jpg', # foo/img.jpg relative to foo.html |
| 531 |
'img.jpg', # foo/img.jpg relative to foo/bar.html |
| 532 |
'../img.jpg', # foo/img.jpg relative to foo/bar/baz.html |
| 533 |
] |
| 534 |
for i, filename in enumerate(to_files): |
| 535 |
with self.subTest(from_file=from_file.src_path, to_file=filename): |
| 536 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 537 |
self.assertEqual(file.url, to_file_urls[i]) |
| 538 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 539 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 540 |
|
| 541 |
from_file = File('index.html', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 542 |
self.assertEqual(from_file.url, 'index.html') |
| 543 |
|
| 544 |
expected = [ |
| 545 |
'index.html', # index.html relative to . |
| 546 |
'../index.html', # index.html relative to foo/ |
| 547 |
'../../index.html', # index.html relative to foo/bar/ |
| 548 |
'../../../index.html', # index.html relative to foo/bar/baz/ |
| 549 |
'index.html', # index.html relative to foo.html |
| 550 |
'../index.html', # index.html relative to foo/bar.html |
| 551 |
'../../index.html', # index.html relative to foo/bar/baz.html |
| 552 |
] |
| 553 |
for i, filename in enumerate(to_files): |
| 554 |
with self.subTest(from_file=from_file.src_path, to_file=filename): |
| 555 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 556 |
self.assertEqual(file.url, to_file_urls[i]) |
| 557 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 558 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 559 |
|
| 560 |
from_file = File('file.html', '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 561 |
self.assertEqual(from_file.url, 'file.html') |
| 562 |
|
| 563 |
expected = [ |
| 564 |
'file.html', # file.html relative to . |
| 565 |
'../file.html', # file.html relative to foo/ |
| 566 |
'../../file.html', # file.html relative to foo/bar/ |
| 567 |
'../../../file.html', # file.html relative to foo/bar/baz/ |
| 568 |
'file.html', # file.html relative to foo.html |
| 569 |
'../file.html', # file.html relative to foo/bar.html |
| 570 |
'../../file.html', # file.html relative to foo/bar/baz.html |
| 571 |
] |
| 572 |
for i, filename in enumerate(to_files): |
| 573 |
with self.subTest(from_file=from_file.src_path, to_file=filename): |
| 574 |
file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) |
| 575 |
self.assertEqual(file.url, to_file_urls[i]) |
| 576 |
self.assertEqual(from_file.url_relative_to(file.url), expected[i]) |
| 577 |
self.assertEqual(from_file.url_relative_to(file), expected[i]) |
| 578 |
|
| 579 |
@tempdir( |
| 580 |
files=[ |
| 581 |
'index.md', |
| 582 |
'readme.md', |
| 583 |
'bar.css', |
| 584 |
'bar.html', |
| 585 |
'bar.jpg', |
| 586 |
'bar.js', |
| 587 |
'bar.md', |
| 588 |
'.dotfile', |
| 589 |
'templates/foo.html', |
| 590 |
] |
| 591 |
) |
| 592 |
def test_get_files(self, tdir): |
| 593 |
config = load_config(docs_dir=tdir, extra_css=['bar.css'], extra_javascript=['bar.js']) |
| 594 |
files = get_files(config) |
| 595 |
self.assertIsInstance(files, Files) |
| 596 |
self.assertEqual( |
| 597 |
[f.src_uri for f in files if f.inclusion.is_included()], |
| 598 |
['index.md', 'bar.css', 'bar.html', 'bar.jpg', 'bar.js', 'bar.md', 'readme.md'], |
| 599 |
) |
| 600 |
self.assertEqual( |
| 601 |
[f.src_uri for f in files if f.inclusion.is_excluded()], |
| 602 |
['.dotfile', 'templates/foo.html'], |
| 603 |
) |
| 604 |
|
| 605 |
@tempdir( |
| 606 |
files=[ |
| 607 |
'README.md', |
| 608 |
'foo.md', |
| 609 |
] |
| 610 |
) |
| 611 |
def test_get_files_include_readme_without_index(self, tdir): |
| 612 |
config = load_config(docs_dir=tdir) |
| 613 |
files = get_files(config) |
| 614 |
self.assertIsInstance(files, Files) |
| 615 |
self.assertEqual([f.src_uri for f in files], ['README.md', 'foo.md']) |
| 616 |
|
| 617 |
@tempdir( |
| 618 |
files=[ |
| 619 |
'index.md', |
| 620 |
'README.md', |
| 621 |
'foo.md', |
| 622 |
] |
| 623 |
) |
| 624 |
def test_get_files_exclude_readme_with_index(self, tdir): |
| 625 |
config = load_config(docs_dir=tdir) |
| 626 |
with self.assertLogs('mkdocs') as cm: |
| 627 |
files = get_files(config) |
| 628 |
self.assertRegex( |
| 629 |
'\n'.join(cm.output), |
| 630 |
r"^WARNING:mkdocs.structure.files:" |
| 631 |
r"Excluding 'README.md' from the site because it conflicts with 'index.md'.$", |
| 632 |
) |
| 633 |
self.assertIsInstance(files, Files) |
| 634 |
self.assertEqual([f.src_uri for f in files], ['index.md', 'foo.md']) |
| 635 |
|
| 636 |
@tempdir() |
| 637 |
@tempdir(files={'test.txt': 'source content'}) |
| 638 |
def test_copy_file(self, src_dir, dest_dir): |
| 639 |
file = File('test.txt', src_dir, dest_dir, use_directory_urls=False) |
| 640 |
dest_path = os.path.join(dest_dir, 'test.txt') |
| 641 |
self.assertPathNotExists(dest_path) |
| 642 |
file.copy_file() |
| 643 |
self.assertPathIsFile(dest_path) |
| 644 |
|
| 645 |
@tempdir(files={'test.txt': 'source content'}) |
| 646 |
def test_copy_file_same_file(self, dest_dir): |
| 647 |
file = File('test.txt', dest_dir, dest_dir, use_directory_urls=False) |
| 648 |
dest_path = os.path.join(dest_dir, 'test.txt') |
| 649 |
file.copy_file() |
| 650 |
self.assertPathIsFile(dest_path) |
| 651 |
with open(dest_path, encoding='utf-8') as f: |
| 652 |
self.assertEqual(f.read(), 'source content') |
| 653 |
|
| 654 |
@tempdir(files={'test.txt': 'destination content'}) |
| 655 |
@tempdir(files={'test.txt': 'source content'}) |
| 656 |
def test_copy_file_clean_modified(self, src_dir, dest_dir): |
| 657 |
file = File('test.txt', src_dir, dest_dir, use_directory_urls=False) |
| 658 |
self.assertEqual(file.content_string, 'source content') |
| 659 |
self.assertEqual(file.content_bytes, b'source content') |
| 660 |
file.is_modified = mock.Mock(return_value=True) |
| 661 |
dest_path = os.path.join(dest_dir, 'test.txt') |
| 662 |
file.copy_file(dirty=False) |
| 663 |
self.assertPathIsFile(dest_path) |
| 664 |
with open(dest_path, encoding='utf-8') as f: |
| 665 |
self.assertEqual(f.read(), 'source content') |
| 666 |
|
| 667 |
@tempdir(files={'test.txt': 'destination content'}) |
| 668 |
@tempdir(files={'test.txt': 'source content'}) |
| 669 |
def test_copy_file_dirty_modified(self, src_dir, dest_dir): |
| 670 |
file = File('test.txt', src_dir, dest_dir, use_directory_urls=False) |
| 671 |
file.is_modified = mock.Mock(return_value=True) |
| 672 |
dest_path = os.path.join(dest_dir, 'test.txt') |
| 673 |
file.copy_file(dirty=True) |
| 674 |
self.assertPathIsFile(dest_path) |
| 675 |
with open(dest_path, encoding='utf-8') as f: |
| 676 |
self.assertEqual(f.read(), 'source content') |
| 677 |
|
| 678 |
@tempdir(files={'test.txt': 'destination content'}) |
| 679 |
@tempdir(files={'test.txt': 'source content'}) |
| 680 |
def test_copy_file_dirty_not_modified(self, src_dir, dest_dir): |
| 681 |
file = File('test.txt', src_dir, dest_dir, use_directory_urls=False) |
| 682 |
file.is_modified = mock.Mock(return_value=False) |
| 683 |
dest_path = os.path.join(dest_dir, 'test.txt') |
| 684 |
file.copy_file(dirty=True) |
| 685 |
self.assertPathIsFile(dest_path) |
| 686 |
with open(dest_path, encoding='utf-8') as f: |
| 687 |
self.assertEqual(f.read(), 'destination content') |
| 688 |
|
| 689 |
@tempdir() |
| 690 |
def test_copy_file_from_content(self, dest_dir): |
| 691 |
file = File('test.txt', src_dir='unused', dest_dir=dest_dir, use_directory_urls=False) |
| 692 |
file.content_string = 'ö' |
| 693 |
self.assertIsNone(file.abs_src_path) |
| 694 |
dest_path = os.path.join(dest_dir, 'test.txt') |
| 695 |
|
| 696 |
file.copy_file() |
| 697 |
self.assertPathIsFile(dest_path) |
| 698 |
with open(dest_path, encoding='utf-8') as f: |
| 699 |
self.assertEqual(f.read(), 'ö') |
| 700 |
|
| 701 |
file.content_bytes = b'\x01\x02\x03' |
| 702 |
file.copy_file() |
| 703 |
with open(dest_path, 'rb') as f: |
| 704 |
self.assertEqual(f.read(), b'\x01\x02\x03') |
| 705 |
|
| 706 |
file.content_bytes = b'\xc3\xb6' |
| 707 |
file.copy_file() |
| 708 |
with open(dest_path, encoding='utf-8') as f: |
| 709 |
self.assertEqual(f.read(), 'ö') |
| 710 |
|
| 711 |
def test_files_append_remove_src_paths(self): |
| 712 |
fs = [ |
| 713 |
File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 714 |
File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 715 |
File('foo/bar.html', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 716 |
File('foo/bar.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 717 |
File('foo/bar.js', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 718 |
File('foo/bar.css', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 719 |
] |
| 720 |
files = Files(fs) |
| 721 |
self.assertEqual(len(files), 6) |
| 722 |
self.assertEqual(len(files.src_uris), 6) |
| 723 |
extra_file = File('extra.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) |
| 724 |
self.assertFalse(extra_file.src_uri in files.src_uris) |
| 725 |
files.append(extra_file) |
| 726 |
self.assertEqual(len(files), 7) |
| 727 |
self.assertEqual(len(files.src_uris), 7) |
| 728 |
self.assertTrue(extra_file.src_uri in files.src_uris) |
| 729 |
files.remove(extra_file) |
| 730 |
self.assertEqual(len(files), 6) |
| 731 |
self.assertEqual(len(files.src_uris), 6) |
| 732 |
self.assertFalse(extra_file.src_uri in files.src_uris) |
| 733 |
|
| 734 |
def test_files_move_to_end(self): |
| 735 |
fs = [ |
| 736 |
File('a.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 737 |
File('b.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True), |
| 738 |
] |
| 739 |
files = Files(fs) |
| 740 |
self.assertEqual(len(files), 2) |
| 741 |
self.assertEqual(list(files)[0].src_uri, 'a.md') |
| 742 |
with self.assertWarns(DeprecationWarning): |
| 743 |
files.append(fs[0]) |
| 744 |
self.assertEqual(len(files), 2) |
| 745 |
self.assertEqual(list(files)[0].src_uri, 'b.jpg') |
| 746 |
self.assertEqual(list(files)[1].src_uri, 'a.md') |