diff --git a/.githooks/pre-push b/.githooks/pre-push index a76a593..844a7b1 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -21,18 +21,15 @@ return 1 } - if ! read_branches; then echo "Skipping tests: not pushing testing, staging, main, or production." - exit 0 -fi +else + COMMIT_HASH=$(git rev-parse HEAD) -COMMIT_HASH=$(git rev-parse HEAD) -pwd - -if ! "./scripts/pre-push-tests.sh" "$COMMIT_HASH"; then - echo "Tests failed. Aborting push." - exit 1 + if ! "./scripts/pre-push-tests.sh" "$COMMIT_HASH"; then + echo "Tests failed. Aborting push." + exit 1 + fi fi cd content diff --git a/.gitignore b/.gitignore index 9ea2e52..8fb06ba 100755 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ data/* !data/.gitkeep .last_tested_commit +.last_unit_tested_commit certs/* test/logs/* diff --git a/scripts/runTests.js b/scripts/runTests.js index 6a4a7e7..4336a7f 100644 --- a/scripts/runTests.js +++ b/scripts/runTests.js @@ -2,65 +2,70 @@ const { execSync } = require("child_process"); const fs = require("fs"); -const CACHE_FILE = ".last_tested_commit"; - -function getCurrentCommitHash() { - return execSync("git rev-parse HEAD").toString().trim(); -} - -function getLastTestedCommit() { - try { - return fs.readFileSync(CACHE_FILE, "utf-8").trim(); - } catch (e) { - if (e.code === "ENOENT") return null; - throw e; +module.exports = class TestRunner { + constructor(cacheFile) { + this.cacheFile = cacheFile; } -} -function cacheTestedCommit(commitHash) { - fs.writeFileSync(CACHE_FILE, commitHash + "\n"); -} + getCurrentCommitHash() { + return execSync("git rev-parse HEAD").toString().trim(); + } -async function runTestFile(filePath, description) { - console.log(`Running ${description}...`); - - const mocha = new Mocha({ - reporter: "spec", - timeout: 5000, - }); - - mocha.addFile(filePath); - - return new Promise((resolve, reject) => { - mocha.run((failures) => { - if (failures) { - reject(new Error(`${description} failed with ${failures} failures`)); - } else { - resolve(); - } - }); - }); -} - -async function runTests() { - try { - const commitHash = getCurrentCommitHash(); - const lastCommit = getLastTestedCommit(); - - if (lastCommit === commitHash) { - process.exit(0); + getLastTestedCommit() { + try { + return fs.readFileSync(this.cacheFile, "utf-8").trim(); + } catch (e) { + if (e.code === "ENOENT") return null; + throw e; } - - await runTestFile("./test/env.test.js", "environment validation tests"); - console.log("✓ Environment validation passed. Running route tests..."); - - await runTestFile("./test/routes.test.js", "route tests"); - console.log("✓ All tests passed!"); - cacheTestedCommit(commitHash); - } catch (error) { - console.error("Test execution failed:", error.message); - process.exit(1); } -} -runTests(); + cacheTestedCommit(commitHash) { + fs.writeFileSync(this.cacheFile, commitHash + "\n"); + } + + runTestFile(filePath, description) { + console.log(`Running ${description}...`); + + const mocha = new Mocha({ + reporter: "spec", + timeout: 5000, + }); + + mocha.addFile(filePath); + + return new Promise((resolve, reject) => { + mocha.run((failures) => { + if (failures) { + reject(new Error(`${description} failed with ${failures} failures`)); + } else { + resolve(); + } + }); + }); + } + + async run() { + try { + const commitHash = this.getCurrentCommitHash(); + const lastCommit = this.getLastTestedCommit(); + + if (lastCommit === commitHash) { + process.exit(0); + } + + await this.runTestFile( + "./test/env.test.js", + "environment validation tests" + ); + console.log("✓ Environment validation passed. Running route tests..."); + + await this.runTestFile("./test/routes.test.js", "route tests"); + console.log("✓ All tests passed!"); + this.cacheTestedCommit(commitHash); + } catch (error) { + console.error("Test execution failed:", error.message); + process.exit(1); + } + } +}; diff --git a/scripts/test-postreceive.js b/scripts/test-postreceive.js index 8d1e15d..aebb0cf 100644 --- a/scripts/test-postreceive.js +++ b/scripts/test-postreceive.js @@ -1 +1,3 @@ -require("./runTests"); +const TestRunner = require("./runTests"); + +new TestRunner(".last_successful_postreceive_test").run(); diff --git a/scripts/test-prepush.js b/scripts/test-prepush.js index 8d1e15d..c204888 100644 --- a/scripts/test-prepush.js +++ b/scripts/test-prepush.js @@ -1 +1,3 @@ -require("./runTests"); +const TestRunner = require("./runTests"); + +new TestRunner(".last_successful_prepush_test").run();