From f93b427c189f3303c8b9c5125022844531970df0 Mon Sep 17 00:00:00 2001 From: "Alexander J. Vincent" Date: Sat, 6 Jul 2024 22:54:25 -0700 Subject: [PATCH] #97, adjust file hashing functions for reuse. --- .gitignore | 2 ++ stage_3_snapshot/spec-snapshot/fileHashes.ts | 17 +++++---- utilities/source/hash-all-files.ts | 38 ++++++++++---------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index f541088..8847c55 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ stage_3_integration/typings-snapshot stage_3_snapshot/snapshot .tsimp + +stage_*/**/directoryHashes.json diff --git a/stage_3_snapshot/spec-snapshot/fileHashes.ts b/stage_3_snapshot/spec-snapshot/fileHashes.ts index 3450244..297b6f8 100644 --- a/stage_3_snapshot/spec-snapshot/fileHashes.ts +++ b/stage_3_snapshot/spec-snapshot/fileHashes.ts @@ -131,7 +131,7 @@ async function compareSnapshots( stage_three_hashes ] = await hashDirectories(stage_two_dir, stage_three_dir); const diffFileHashes = getArrayDiff(stage_two_hashes, stage_three_hashes); - expect(diffFileHashes).withContext("file hashes").toEqual([]); + expect(diffFileHashes).withContext("file hashes for " + localPath).toEqual([]); } async function compareOneSnapshot( @@ -145,9 +145,9 @@ async function compareOneSnapshot( if (compareFileLists(stage_two_snapshot, stage_three_snapshot, [ stage_two_file ], [ stage_three_file ]) === false) return; - const stage_two_hash = await hashOneFile(stage_two_snapshot, stage_two_file); - const stage_three_hash = await hashOneFile(stage_three_snapshot, stage_three_file); - expect(stage_three_hash).withContext("file hashes").toEqual(stage_two_hash); + const stage_two_hash = await hashOneFile(stage_two_file); + const stage_three_hash = await hashOneFile(stage_three_file); + expect(stage_three_hash).withContext("file hashes for " + localPath).toEqual(stage_two_hash); } function compareFileLists( @@ -212,10 +212,13 @@ async function hashDirectories( ): Promise<[readonly string[], readonly string[]]> { const [stage_two_hash, stage_three_hash] = await Promise.all([ - hashAllFiles(stage_two_dir, true), - hashAllFiles(stage_three_dir, true) + hashAllFiles(stage_two_dir), + hashAllFiles(stage_three_dir) ]); - return [stage_two_hash.split("\n"), stage_three_hash.split("\n")]; + return [ + stage_two_hash.map(([key, value]) => key + " " + value), + stage_three_hash.map(([key, value]) => key + " " + value) + ]; } function getArrayDiff( diff --git a/utilities/source/hash-all-files.ts b/utilities/source/hash-all-files.ts index bb635ce..8cbeb91 100644 --- a/utilities/source/hash-all-files.ts +++ b/utilities/source/hash-all-files.ts @@ -1,5 +1,6 @@ import crypto from "crypto"; import fs from "fs/promises"; +import path from "path"; import readDirsDeep from "./readDirsDeep.js"; import { PromiseAllParallel } from "./PromiseTypes.js"; @@ -29,7 +30,9 @@ export async function getHashFileList( root: string ): Promise { - return (await readDirsDeep(root)).files; + return (await readDirsDeep(root)).files.filter( + file => path.basename(file) !== "directoryHashes.json" + ); } /** @@ -40,33 +43,32 @@ export async function getHashFileList( * @returns The hash of all non-ignored contents. */ export async function hashAllFiles( - root: string, - verbose: boolean -): Promise + root: string +): Promise { - const allFiles = await getHashFileList(root); - const fileHashes = await PromiseAllParallel( - allFiles, async file => hashOneFile(root, file) - ); - const contents = fileHashes.join("\n"); + const allFiles: string[] = await getHashFileList(root); - if (verbose) { - return contents; - } - const hash = crypto.createHash('sha512'); - hash.update(contents); - return hash.digest('hex'); + const fileHashes: [string, string][] = await PromiseAllParallel( + allFiles, async file => [file.replace(root, ""), await hashOneFile(file)] + ); + return fileHashes; } export async function hashOneFile( - root: string, file: string, ): Promise { const contents = await fs.readFile(file, "utf-8"); + return hashOneSource(contents); +} + +export function hashOneSource( + contents: string +): string +{ const hash = crypto.createHash('sha512'); hash.update(contents); - return hash.digest('hex') + " " + file.replace(root, ""); -} + return hash.digest('hex'); +} \ No newline at end of file