diff --git a/server/merge-snap.js b/server/merge-snap.js new file mode 100755 index 000000000..79713e631 --- /dev/null +++ b/server/merge-snap.js @@ -0,0 +1,45 @@ +const fs = require("fs"); + +// This script takes a path to a .test.ts file, finds its equivalent snapshot file +// and merges it with single-process-tap's snapshot file. + +const toMerge = process.argv[2]; + +if (!toMerge) { + throw new Error("no argv passed to merge-snap. Can't merge with nothing."); +} + +const BASE_FILE = "./tap-snapshots/src/test-utils/single-process-tap.ts.test.cjs"; +const NEW_FILE = `${toMerge.replace("tachi-server/src", "tachi-server/tap-snapshots/src")}.test.cjs`; + +const baseSet = require(BASE_FILE); + +const newSet = require(NEW_FILE); + +for (const [snapshotName, snapshotValue] of Object.entries(newSet)) { + baseSet[snapshotName] = snapshotValue; +} + + +function escapeBacktickString(str) { + return str.replace(/\`\$/, " ") +} + +const cases = []; + +for (const [snapshotName, snapshotValue] of Object.entries(baseSet)) { + cases.push(`exports[\`${escapeBacktickString(snapshotName)}\`] = \`${escapeBacktickString(snapshotValue)}\`\n`) +} + +let outFile = ` +/* IMPORTANT + * This snapshot file is auto-generated, but designed for humans. + * It should be checked into source control and tracked carefully. + * Re-generate by setting TAP_SNAPSHOT=1 and running tests. + * Make sure to inspect the output below. Do not ignore changes! + */ +'use strict' +${cases.join("\n")}` + +fs.writeFileSync(BASE_FILE, outFile); +fs.rmSync(NEW_FILE); \ No newline at end of file diff --git a/server/snap-single-file.sh b/server/snap-single-file.sh new file mode 100755 index 000000000..c87be32ce --- /dev/null +++ b/server/snap-single-file.sh @@ -0,0 +1,27 @@ +#! /bin/bash + +# This script is intended to be used as follows +# ./snap-single-file.sh path/to/test.TAP_SNAPSHOT +# This will result in that individual file being snapped. + +# Of course, we need to do this like this because we are abusing +# single process tap to get our tests done faster. + +# Put us in the directory this script is in. +cd "$(dirname "$0")" || exit 1 + +# So, TAP wont exit when ran on a single file because we never close it. +# This can be fixed another time, but until then I'm going to do this insane hack. +onsigint() +{ + node ./merge-snap.js "$1" +} + +# When we recieve a sigint, trap it and call our sigint handler. +# of course, this is bash, so trap onsigint doesn't do what you want. +# instead, we do 'onsigint "$@"', which means call onsigint with arguments like $1. +trap 'onsigint "$@"' SIGINT + +# Snapshot the argument. +TAP_SNAPSHOT=1 tap "$1" +