Modernize the downloadManifestFiles/ensurePDFsDownloaded test helper functions
				
					
				
			The test helper code largely predates the introduction of modern JavaScript features and should be refactored to improve readability. In particular callbacks make the code harder to understand and maintain. This commit: - replaces the callback argument with returning a promise; - replaces the recursive function calls with a simple loop; - uses `const`/`let` instead of `var`; - uses arrow functions for shorter code; - uses template strings for shorter string formatting code.
This commit is contained in:
		
							parent
							
								
									86bee4409a
								
							
						
					
					
						commit
						0a10a7b57b
					
				| @ -73,15 +73,17 @@ function downloadFile(file, url, redirects = 0) { | |||||||
|   }); |   }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function downloadManifestFiles(manifest, callback) { | async function downloadManifestFiles(manifest) { | ||||||
|   async function downloadNext() { |   const links = manifest | ||||||
|     if (i >= links.length) { |     .filter(item => item.link && !fs.existsSync(item.file)) | ||||||
|       callback(); |     .map(item => { | ||||||
|       return; |       let url = fs.readFileSync(`${item.file}.link`).toString(); | ||||||
|     } |       url = url.replace(/\s+$/, ""); | ||||||
|     var file = links[i].file; |       return { file: item.file, url }; | ||||||
|     var url = links[i].url; |     }); | ||||||
|     console.log("Downloading " + url + " to " + file + "..."); | 
 | ||||||
|  |   for (const { file, url } of links) { | ||||||
|  |     console.log(`Downloading ${url} to ${file}...`); | ||||||
|     try { |     try { | ||||||
|       await downloadFile(file, url); |       await downloadFile(file, url); | ||||||
|     } catch (ex) { |     } catch (ex) { | ||||||
| @ -89,24 +91,7 @@ function downloadManifestFiles(manifest, callback) { | |||||||
|       fs.writeFileSync(file, ""); // making it empty file
 |       fs.writeFileSync(file, ""); // making it empty file
 | ||||||
|       fs.writeFileSync(`${file}.error`, ex); |       fs.writeFileSync(`${file}.error`, ex); | ||||||
|     } |     } | ||||||
|     i++; |  | ||||||
|     downloadNext(); |  | ||||||
|   } |   } | ||||||
| 
 |  | ||||||
|   var links = manifest |  | ||||||
|     .filter(function (item) { |  | ||||||
|       return item.link && !fs.existsSync(item.file); |  | ||||||
|     }) |  | ||||||
|     .map(function (item) { |  | ||||||
|       var file = item.file; |  | ||||||
|       var linkfile = file + ".link"; |  | ||||||
|       var url = fs.readFileSync(linkfile).toString(); |  | ||||||
|       url = url.replace(/\s+$/, ""); |  | ||||||
|       return { file, url }; |  | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
|   var i = 0; |  | ||||||
|   downloadNext(); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function calculateMD5(file) { | function calculateMD5(file) { | ||||||
|  | |||||||
| @ -268,7 +268,7 @@ function examineRefImages() { | |||||||
|   }); |   }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function startRefTest(masterMode, showRefImages) { | async function startRefTest(masterMode, showRefImages) { | ||||||
|   function finalize() { |   function finalize() { | ||||||
|     stopServer(); |     stopServer(); | ||||||
|     let numRuns = 0; |     let numRuns = 0; | ||||||
| @ -402,11 +402,10 @@ function startRefTest(masterMode, showRefImages) { | |||||||
|   if (!manifest) { |   if (!manifest) { | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   if (options.noDownload) { |   if (!options.noDownload) { | ||||||
|     checkRefsTmp(); |     await ensurePDFsDownloaded(); | ||||||
|   } else { |  | ||||||
|     ensurePDFsDownloaded(checkRefsTmp); |  | ||||||
|   } |   } | ||||||
|  |   checkRefsTmp(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function handleSessionTimeout(session) { | function handleSessionTimeout(session) { | ||||||
| @ -1071,9 +1070,9 @@ async function closeSession(browser) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function ensurePDFsDownloaded(callback) { | async function ensurePDFsDownloaded() { | ||||||
|   var manifest = getTestManifest(); |   const manifest = getTestManifest(); | ||||||
|   downloadManifestFiles(manifest, async function () { |   await downloadManifestFiles(manifest); | ||||||
|   try { |   try { | ||||||
|     await verifyManifestFiles(manifest); |     await verifyManifestFiles(manifest); | ||||||
|   } catch { |   } catch { | ||||||
| @ -1089,29 +1088,25 @@ function ensurePDFsDownloaded(callback) { | |||||||
|       process.exit(1); |       process.exit(1); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|     callback(); |  | ||||||
|   }); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function main() { | async function main() { | ||||||
|   if (options.statsFile) { |   if (options.statsFile) { | ||||||
|     stats = []; |     stats = []; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   if (options.downloadOnly) { |   if (options.downloadOnly) { | ||||||
|     ensurePDFsDownloaded(function () {}); |     await ensurePDFsDownloaded(); | ||||||
|   } else if (options.unitTest) { |   } else if (options.unitTest) { | ||||||
|     // Allows linked PDF files in unit-tests as well.
 |     // Allows linked PDF files in unit-tests as well.
 | ||||||
|     ensurePDFsDownloaded(function () { |     await ensurePDFsDownloaded(); | ||||||
|     startUnitTest("/test/unit/unit_test.html", "unit"); |     startUnitTest("/test/unit/unit_test.html", "unit"); | ||||||
|     }); |  | ||||||
|   } else if (options.fontTest) { |   } else if (options.fontTest) { | ||||||
|     startUnitTest("/test/font/font_test.html", "font"); |     startUnitTest("/test/font/font_test.html", "font"); | ||||||
|   } else if (options.integration) { |   } else if (options.integration) { | ||||||
|     // Allows linked PDF files in integration-tests as well.
 |     // Allows linked PDF files in integration-tests as well.
 | ||||||
|     ensurePDFsDownloaded(function () { |     await ensurePDFsDownloaded(); | ||||||
|     startIntegrationTest(); |     startIntegrationTest(); | ||||||
|     }); |  | ||||||
|   } else { |   } else { | ||||||
|     startRefTest(options.masterMode, options.reftest); |     startRefTest(options.masterMode, options.reftest); | ||||||
|   } |   } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user