1- #[ macro_use]
2- extern crate pretty_assertions;
3-
41mod dummy_book;
52
63use crate :: dummy_book:: { assert_contains_strings, assert_doesnt_contain_strings, DummyBook } ;
@@ -10,6 +7,7 @@ use mdbook::config::Config;
107use mdbook:: errors:: * ;
118use mdbook:: utils:: fs:: write_file;
129use mdbook:: MDBook ;
10+ use pretty_assertions:: assert_eq;
1311use select:: document:: Document ;
1412use select:: predicate:: { Class , Name , Predicate } ;
1513use std:: collections:: HashMap ;
@@ -842,3 +840,111 @@ mod search {
842840 }
843841 }
844842}
843+
844+ #[ test]
845+ fn custom_fonts ( ) {
846+ // Tests to ensure custom fonts are copied as expected.
847+ let builtin_fonts = [
848+ "OPEN-SANS-LICENSE.txt" ,
849+ "SOURCE-CODE-PRO-LICENSE.txt" ,
850+ "fonts.css" ,
851+ "open-sans-v17-all-charsets-300.woff2" ,
852+ "open-sans-v17-all-charsets-300italic.woff2" ,
853+ "open-sans-v17-all-charsets-600.woff2" ,
854+ "open-sans-v17-all-charsets-600italic.woff2" ,
855+ "open-sans-v17-all-charsets-700.woff2" ,
856+ "open-sans-v17-all-charsets-700italic.woff2" ,
857+ "open-sans-v17-all-charsets-800.woff2" ,
858+ "open-sans-v17-all-charsets-800italic.woff2" ,
859+ "open-sans-v17-all-charsets-italic.woff2" ,
860+ "open-sans-v17-all-charsets-regular.woff2" ,
861+ "source-code-pro-v11-all-charsets-500.woff2" ,
862+ ] ;
863+ let actual_files = |path : & Path | -> Vec < String > {
864+ let mut actual: Vec < _ > = path
865+ . read_dir ( )
866+ . unwrap ( )
867+ . map ( |entry| entry. unwrap ( ) . file_name ( ) . into_string ( ) . unwrap ( ) )
868+ . collect ( ) ;
869+ actual. sort ( ) ;
870+ actual
871+ } ;
872+ let has_fonts_css = |path : & Path | -> bool {
873+ let contents = fs:: read_to_string ( path. join ( "book/index.html" ) ) . unwrap ( ) ;
874+ contents. contains ( "fonts/fonts.css" )
875+ } ;
876+
877+ // No theme:
878+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
879+ let p = temp. path ( ) ;
880+ MDBook :: init ( p) . build ( ) . unwrap ( ) ;
881+ MDBook :: load ( p) . unwrap ( ) . build ( ) . unwrap ( ) ;
882+ assert_eq ! ( actual_files( & p. join( "book/fonts" ) ) , & builtin_fonts) ;
883+ assert ! ( has_fonts_css( p) ) ;
884+
885+ // Full theme.
886+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
887+ let p = temp. path ( ) ;
888+ MDBook :: init ( p) . copy_theme ( true ) . build ( ) . unwrap ( ) ;
889+ assert_eq ! ( actual_files( & p. join( "theme/fonts" ) ) , & builtin_fonts) ;
890+ MDBook :: load ( p) . unwrap ( ) . build ( ) . unwrap ( ) ;
891+ assert_eq ! ( actual_files( & p. join( "book/fonts" ) ) , & builtin_fonts) ;
892+ assert ! ( has_fonts_css( p) ) ;
893+
894+ // Mixed with copy_fonts=true
895+ // This should generate a deprecation warning.
896+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
897+ let p = temp. path ( ) ;
898+ MDBook :: init ( p) . build ( ) . unwrap ( ) ;
899+ write_file ( & p. join ( "theme/fonts" ) , "fonts.css" , b"/*custom*/" ) . unwrap ( ) ;
900+ write_file ( & p. join ( "theme/fonts" ) , "myfont.woff" , b"" ) . unwrap ( ) ;
901+ MDBook :: load ( p) . unwrap ( ) . build ( ) . unwrap ( ) ;
902+ assert ! ( has_fonts_css( p) ) ;
903+ let mut expected = Vec :: from ( builtin_fonts) ;
904+ expected. push ( "myfont.woff" ) ;
905+ expected. sort ( ) ;
906+ assert_eq ! ( actual_files( & p. join( "book/fonts" ) ) , expected. as_slice( ) ) ;
907+
908+ // copy-fonts=false, no theme
909+ // This should generate a deprecation warning.
910+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
911+ let p = temp. path ( ) ;
912+ MDBook :: init ( p) . build ( ) . unwrap ( ) ;
913+ let config = Config :: from_str ( "output.html.copy-fonts = false" ) . unwrap ( ) ;
914+ MDBook :: load_with_config ( p, config)
915+ . unwrap ( )
916+ . build ( )
917+ . unwrap ( ) ;
918+ assert ! ( !has_fonts_css( p) ) ;
919+ assert ! ( !p. join( "book/fonts" ) . exists( ) ) ;
920+
921+ // copy-fonts=false with empty fonts.css
922+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
923+ let p = temp. path ( ) ;
924+ MDBook :: init ( p) . build ( ) . unwrap ( ) ;
925+ write_file ( & p. join ( "theme/fonts" ) , "fonts.css" , b"" ) . unwrap ( ) ;
926+ let config = Config :: from_str ( "output.html.copy-fonts = false" ) . unwrap ( ) ;
927+ MDBook :: load_with_config ( p, config)
928+ . unwrap ( )
929+ . build ( )
930+ . unwrap ( ) ;
931+ assert ! ( !has_fonts_css( p) ) ;
932+ assert ! ( !p. join( "book/fonts" ) . exists( ) ) ;
933+
934+ // copy-fonts=false with fonts theme
935+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
936+ let p = temp. path ( ) ;
937+ MDBook :: init ( p) . build ( ) . unwrap ( ) ;
938+ write_file ( & p. join ( "theme/fonts" ) , "fonts.css" , b"/*custom*/" ) . unwrap ( ) ;
939+ write_file ( & p. join ( "theme/fonts" ) , "myfont.woff" , b"" ) . unwrap ( ) ;
940+ let config = Config :: from_str ( "output.html.copy-fonts = false" ) . unwrap ( ) ;
941+ MDBook :: load_with_config ( p, config)
942+ . unwrap ( )
943+ . build ( )
944+ . unwrap ( ) ;
945+ assert ! ( has_fonts_css( p) ) ;
946+ assert_eq ! (
947+ actual_files( & p. join( "book/fonts" ) ) ,
948+ & [ "fonts.css" , "myfont.woff" ]
949+ ) ;
950+ }
0 commit comments