Skip to content

Commit

Permalink
#97, adjust file hashing functions for reuse.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jul 7, 2024
1 parent 9f37da0 commit f93b427
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ stage_3_integration/typings-snapshot
stage_3_snapshot/snapshot

.tsimp

stage_*/**/directoryHashes.json
17 changes: 10 additions & 7 deletions stage_3_snapshot/spec-snapshot/fileHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
38 changes: 20 additions & 18 deletions utilities/source/hash-all-files.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -29,7 +30,9 @@ export async function getHashFileList(
root: string
): Promise<string[]>
{
return (await readDirsDeep(root)).files;
return (await readDirsDeep(root)).files.filter(
file => path.basename(file) !== "directoryHashes.json"
);
}

/**
Expand All @@ -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<string>
root: string
): Promise<readonly [string, string][]>
{
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<string>
{
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');
}

0 comments on commit f93b427

Please sign in to comment.