@@ -55,6 +55,7 @@ def setUp(self):
5555 self .write ("content/en/docs/v1.6/removed.md" , '---\n title: Removed\n ---\n Old page' )
5656 self .write ("content/en/private.md" , '---\n title: Private\n params:\n noindex: true\n ---\n Private' )
5757 self .write ("content/en/print.md" , '---\n title: Print\n outputs: [HTML, PRINT]\n ---\n Print' )
58+ self .write ("data/docs-legacy-urls.json" , "{}" )
5859 self .configure ()
5960
6061 def write (self , name , text ):
@@ -371,6 +372,164 @@ def log_message(self, *args):
371372 thread .join ()
372373 server .server_close ()
373374
375+ def legacy_site (self ):
376+ self .write ("data/docs-legacy-urls.json" , (REPO / "data/docs-legacy-urls.json" ).read_text ())
377+ for version in ("v1.6" , "v1.7" ):
378+ for path , name in (("applications/_index.md" , "Applications" ),
379+ ("applications/mariadb.md" , "MariaDB" ),
380+ ("install/talos/boot-to-talos.md" , "Boot to Talos" ),
381+ ("roadmap.md" , "Roadmap" ), ("storage/_index.md" , "Storage" )):
382+ self .write (f"content/en/docs/{ version } /{ path } " , f'---\n title: { name } \n ---\n { name } { version } \n \n ## Backups\n ' )
383+ self .write ("content/en/docs/v0/operations/configuration/bundles.md" ,
384+ '---\n title: Archived bundles\n ---\n Archived bundles\n \n ## distro-hosted\n ' )
385+ return {
386+ "/docs/reference/applications/" : "/docs/applications/" ,
387+ "/docs/reference/applications/mysql/" : "/docs/applications/mariadb/" ,
388+ "/docs/install/talos/kexec/" : "/docs/install/talos/boot-to-talos/" ,
389+ "/roadmap/" : "/docs/roadmap/" ,
390+ "/docs/bundles/distro-hosted/" : None ,
391+ "/docs/operations/storage/linstor/" : "/docs/storage/" ,
392+ "/docs/v1/reference/applications/" : "/docs/applications/" ,
393+ "/docs/v1/reference/applications/mysql/" : "/docs/applications/mariadb/" ,
394+ "/docs/v1/install/talos/kexec/" : "/docs/install/talos/boot-to-talos/" ,
395+ "/docs/v1/operations/storage/linstor/" : "/docs/storage/" ,
396+ }
397+
398+ def test_legacy_urls_serve_current_and_archived_content (self ):
399+ expected = self .legacy_site ()
400+ self .build ()
401+ for old , target in expected .items ():
402+ with self .subTest (url = old ):
403+ page = self .head (old .lstrip ("/" ) + "index.html" )
404+ self .assertFalse (page .refresh )
405+ if target :
406+ self .assertEqual (page .canonical , ["https://example.org" + target ])
407+ self .assertEqual (page .og_urls , page .canonical )
408+ self .assertEqual (page .robots , ["index, follow" ])
409+ self .assertIn ("backups" , page .ids )
410+ else :
411+ self .assertEqual (page .canonical , [])
412+ self .assertEqual (page .robots , ["noindex, follow" ])
413+ self .assertIn ("distro-hosted" , page .ids )
414+ self .configure ("v1.7" )
415+ self .build ()
416+ for old , target in expected .items ():
417+ html = (self .public / old .lstrip ("/" ) / "index.html" ).read_text ()
418+ self .assertIn ("v1.7" if target else "Archived bundles" , html )
419+ archive = (self .public / "docs/v1.6/applications/mariadb/index.html" ).read_bytes ()
420+ self .mirror ()
421+ self .assertEqual ((self .public / "docs/v1.6/applications/mariadb/index.html" ).read_bytes (), archive )
422+
423+ def test_legacy_urls_preserve_queries_and_fragments_without_javascript (self ):
424+ expected = self .legacy_site ()
425+ self .configure (base = "https://example.org/project/" )
426+ self .build ()
427+ class QuietHandler (SimpleHTTPRequestHandler ):
428+ def log_message (self , * args ):
429+ pass
430+ server = ThreadingHTTPServer (("127.0.0.1" , 0 ), functools .partial (QuietHandler , directory = self .public ))
431+ thread = threading .Thread (target = server .serve_forever , daemon = True )
432+ thread .start ()
433+ try :
434+ for old , target in expected .items ():
435+ fragment = "backups" if target else "distro-hosted"
436+ url = f"http://127.0.0.1:{ server .server_port } { old } ?source=test#{ fragment } "
437+ with urlopen (url ) as response :
438+ page = Head (response .read ().decode ())
439+ self .assertEqual (response .status , 200 )
440+ self .assertEqual (response .geturl (), url )
441+ self .assertFalse (page .refresh )
442+ self .assertIn (fragment , page .ids )
443+ if target :
444+ self .assertEqual (page .canonical , ["https://example.org/project" + target ])
445+ finally :
446+ server .shutdown ()
447+ thread .join ()
448+ server .server_close ()
449+
450+ def test_invalid_legacy_map_preserves_previous_output (self ):
451+ expected = self .legacy_site ()
452+ self .build ()
453+ before = {old : (self .public / old .lstrip ("/" ) / "index.html" ).read_bytes () for old in expected }
454+ source = self .public / "docs/v1.6/applications/index.html"
455+ source .write_text (source .read_text ().replace ("Applications v1.6" , "Changed applications" ))
456+ self .write ("public/docs/blocked" , "existing file" )
457+ for old , target in (("/docs/unknown/" , "/docs/missing/" ),
458+ ("/docs/v0/overwrite/" , "/docs/storage/" ),
459+ ("/docs/../outside/" , "/docs/storage/" ),
460+ ("/docs/unknown/" , "https://other.example/" ),
461+ ("/docs/blocked/guide/" , "/docs/storage/" ),
462+ ("/docs/applications/" , "/docs/storage/" )):
463+ with self .subTest (old = old , target = target ):
464+ mapping = {"/docs/reference/applications/" : "/docs/applications/" , old : target }
465+ self .write ("data/docs-legacy-urls.json" , json .dumps (mapping ))
466+ self .mirror (success = False )
467+ for path , content in before .items ():
468+ self .assertEqual ((self .public / path .lstrip ("/" ) / "index.html" ).read_bytes (), content )
469+ for mapping in ([], None , {"/docs/unknown/" : None },
470+ {"/docs/unknown/" : "/docs/storage/?x=one" },
471+ {"/docs/%2e%2e/outside/" : "/docs/storage/" }):
472+ self .write ("data/docs-legacy-urls.json" , json .dumps (mapping ))
473+ self .mirror (success = False )
474+ for path , content in before .items ():
475+ self .assertEqual ((self .public / path .lstrip ("/" ) / "index.html" ).read_bytes (), content )
476+
477+ def test_legacy_map_refuses_symlinks (self ):
478+ self .legacy_site ()
479+ self .build ()
480+ archive = self .public / "docs/v0/guide/index.html"
481+ original = archive .read_bytes ()
482+ old = self .public / "docs/reference/applications/mysql/index.html"
483+ old .unlink ()
484+ old .symlink_to (archive )
485+ self .mirror (success = False )
486+ self .assertTrue (old .is_symlink ())
487+ self .assertEqual (archive .read_bytes (), original )
488+
489+ def test_legacy_map_refuses_archive_source_symlink_and_alias_target (self ):
490+ self .legacy_site ()
491+ self .write ("content/en/docs/v1.6/guide.md" , '---\n title: Guide\n aliases: [/docs/v1.6/alias-guide/]\n ---\n Guide' )
492+ self .build ()
493+ self .write ("data/docs-legacy-urls.json" , json .dumps ({"/docs/old-guide/" : "/docs/alias-guide/" }))
494+ self .mirror (success = False )
495+ self .write ("data/docs-legacy-urls.json" , (REPO / "data/docs-legacy-urls.json" ).read_text ())
496+ source = self .public / "docs/v0/operations/configuration/bundles/index.html"
497+ source .unlink ()
498+ source .symlink_to (self .public / "docs/v0/guide/index.html" )
499+ self .mirror (success = False )
500+ self .assertTrue (source .is_symlink ())
501+
502+ def test_legacy_map_preserves_real_roadmap_page (self ):
503+ self .legacy_site ()
504+ self .write ("content/en/roadmap.md" , '---\n title: Standalone roadmap\n ---\n Standalone roadmap' )
505+ result = subprocess .run ([HUGO , "--minify" ], cwd = self .root , capture_output = True , text = True )
506+ self .assertEqual (result .returncode , 0 , result .stderr )
507+ before = (self .public / "roadmap/index.html" ).read_bytes ()
508+ self .mirror (success = False )
509+ self .assertEqual ((self .public / "roadmap/index.html" ).read_bytes (), before )
510+
511+ def test_legacy_preview_has_no_indexable_copies (self ):
512+ expected = self .legacy_site ()
513+ self .build ("preview" )
514+ for old in expected :
515+ page = self .head (old .lstrip ("/" ) + "index.html" )
516+ self .assertEqual (page .robots , ["noindex, nofollow" ])
517+ self .assertEqual (page .canonical , [])
518+ self .assertEqual (self .locations (), [])
519+
520+ def test_legacy_major_aliases_are_cleaned_on_major_rollover (self ):
521+ self .legacy_site ()
522+ self .build ()
523+ source = self .root / "content/en/docs/v1.7"
524+ for file in source .rglob ("*.md" ):
525+ self .write ("content/en/docs/v2.0/" + str (file .relative_to (source )),
526+ file .read_text ().replace ("v1.7" , "v2.0" ))
527+ self .configure ("v2.0" )
528+ self .build ()
529+ self .assertFalse ((self .public / "docs/v1/reference/applications/mysql/index.html" ).exists ())
530+ self .assertIn ("v2.0" , (self .public / "docs/v2/reference/applications/mysql/index.html" ).read_text ())
531+ self .assertTrue ((self .public / "docs/v1.6/applications/mariadb/index.html" ).is_file ())
532+
374533
375534if __name__ == "__main__" :
376535 unittest .main ()
0 commit comments