@@ -12,32 +12,40 @@ const fs = require('fs');
1212const path = require ( 'path' ) ;
1313
1414const apiPath = path . resolve ( __dirname , '..' , '..' , 'out' , 'doc' , 'api' ) ;
15- const docs = fs . readdirSync ( apiPath ) ;
16- assert . ok ( docs . includes ( '_toc.html' ) ) ;
15+ const allDocs = fs . readdirSync ( apiPath ) ;
16+ assert . ok ( allDocs . includes ( '_toc.html' ) ) ;
17+
18+ const filter = [ 'assets' , '_toc.html' , '.md' ] ;
19+ const actualDocs = allDocs . filter (
20+ ( name ) => ! filter . some ( ( str ) => name . includes ( str ) )
21+ ) ;
1722
1823const toc = fs . readFileSync ( path . resolve ( apiPath , '_toc.html' ) , 'utf8' ) ;
1924const re = / h r e f = " ( [ ^ / ] + \. h t m l ) " / ;
2025const globalRe = new RegExp ( re , 'g' ) ;
2126const links = toc . match ( globalRe ) ;
2227assert . notStrictEqual ( links , null ) ;
2328
24- // Test that all the relative links in the TOC of the documentation
25- // work and all the generated documents are linked in TOC.
26- const linkedHtmls = links . map ( ( link ) => link . match ( re ) [ 1 ] ) ;
27- for ( const html of linkedHtmls ) {
28- assert . ok ( docs . includes ( html ) , `${ html } does not exist` ) ;
29+ // Filter out duplicate links, leave just filenames, add expected JSON files.
30+ const linkedHtmls = [ ...new Set ( links ) ] . map ( ( link ) => link . match ( re ) [ 1 ] ) ;
31+ const expectedJsons = linkedHtmls
32+ . map ( ( name ) => name . replace ( '.html' , '.json' ) )
33+ . concat ( '_toc.json' ) ;
34+ const expectedDocs = linkedHtmls . concat ( expectedJsons ) ;
35+
36+ // Test that all the relative links in the TOC match to the actual documents.
37+ for ( const expectedDoc of expectedDocs ) {
38+ assert . ok ( actualDocs . includes ( expectedDoc ) , `${ expectedDoc } does not exist` ) ;
2939}
3040
31- const excludes = [ '.json' , '.md' , '_toc' , 'assets' ] ;
32- const generatedHtmls = docs . filter ( function ( doc ) {
33- for ( const exclude of excludes ) {
34- if ( doc . includes ( exclude ) ) {
35- return false ;
36- }
37- }
38- return true ;
39- } ) ;
40-
41- for ( const html of generatedHtmls ) {
42- assert . ok ( linkedHtmls . includes ( html ) , `${ html } is not linked in toc` ) ;
41+ // Test that all the actual documents match to the relative links in the TOC
42+ // and that they are not empty files.
43+ for ( const actualDoc of actualDocs ) {
44+ assert . ok (
45+ expectedDocs . includes ( actualDoc ) , `${ actualDoc } does not not match TOC` ) ;
46+
47+ assert . ok (
48+ fs . statSync ( path . join ( apiPath , actualDoc ) ) . size !== 0 ,
49+ `${ actualDoc } is empty`
50+ ) ;
4351}
0 commit comments