diff --git a/.githooks/pre-push b/.githooks/pre-push index d031749..5e10ec0 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -9,62 +9,60 @@ set -euo pipefail set -x + COMMIT_HASH=$(git rev-parse HEAD) -CHECKSUM_FILE=".last_unit_tested_commit" +pwd -if [ -f "$CHECKSUM_FILE" ] && [ "$(cat "$CHECKSUM_FILE")" = "$COMMIT_HASH" ]; then - echo "✓ Skipping tests, already tested commit: $COMMIT_HASH" - exit 0 -fi - -# Run Mocha directly, fail if any test fails -if ! node_modules/.bin/mocha "test/**/*.unit.test.js" "test/**/*.property.test.js"; then - echo "Initial test suite failed. Skipping prepush and aborting push." - - if kill -0 "$APP_PID" 2>/dev/null; then - echo "Stopping app (PID: $APP_PID)..." - kill "$APP_PID" - sleep 1 - if kill -0 "$APP_PID" 2>/dev/null; then - kill -9 "$APP_PID" 2>/dev/null || true - fi - fi - - wait "$APP_PID" 2>/dev/null || true +if ! "./scripts/pre-push-tests.sh" "$COMMIT_HASH"; then + echo "Tests failed. Aborting push." exit 1 fi -echo "$COMMIT_HASH" > "$CHECKSUM_FILE" -echo "✓ All tests passed for commit: $COMMIT_HASH" +# # Run Mocha directly, fail if any test fails +# if ! node_modules/.bin/mocha "test/**/*.unit.test.js" "test/**/*.property.test.js"; then +# echo "Initial test suite failed. Skipping prepush and aborting push." -node src/app.js >/dev/null 2>&1 & - APP_PID=$! +# if kill -0 "$APP_PID" 2>/dev/null; then +# echo "Stopping app (PID: $APP_PID)..." +# kill "$APP_PID" +# sleep 1 +# if kill -0 "$APP_PID" 2>/dev/null; then +# kill -9 "$APP_PID" 2>/dev/null || true +# fi +# fi -sleep 2 +# wait "$APP_PID" 2>/dev/null || true +# exit 1 +# fi -npm run test:prepush -TEST_RESULT=$? +# node src/app.js >/dev/null 2>&1 & +# APP_PID=$! - # Clean up the app process - if kill -0 $APP_PID 2>/dev/null; then - echo "Stopping app (PID: $APP_PID)..." - kill $APP_PID - # Give it time to shut down gracefully - sleep 1 - # Force kill if still running - if kill -0 $APP_PID 2>/dev/null; then - kill -9 $APP_PID 2>/dev/null || true - fi - fi +# sleep 2 + +# npm run test:prepush +# TEST_RESULT=$? + +# # Clean up the app process +# if kill -0 $APP_PID 2>/dev/null; then +# echo "Stopping app (PID: $APP_PID)..." +# kill $APP_PID +# # Give it time to shut down gracefully +# sleep 1 +# # Force kill if still running +# if kill -0 $APP_PID 2>/dev/null; then +# kill -9 $APP_PID 2>/dev/null || true +# fi +# fi - # Wait for process to fully terminate - wait $APP_PID 2>/dev/null || true +# # Wait for process to fully terminate +# wait $APP_PID 2>/dev/null || true -if [ $TEST_RESULT -ne 0 ]; then - echo "Tests failed. Push aborted." - exit 1 -fi +# if [ $TEST_RESULT -ne 0 ]; then +# echo "Tests failed. Push aborted." +# exit 1 +# fi cd content git push "$remote" main diff --git a/scripts/pre-push-tests.sh b/scripts/pre-push-tests.sh new file mode 100755 index 0000000..f022632 --- /dev/null +++ b/scripts/pre-push-tests.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +set -eu +set -x + +COMMIT_HASH="$1" +CHECKSUM_FILE=".last_unit_tested_commit" + +if [ -f "$CHECKSUM_FILE" ] && [ "$(cat "$CHECKSUM_FILE")" = "$COMMIT_HASH" ]; then + echo "✓ Skipping tests, already tested commit: $COMMIT_HASH" + exit 0 +fi + +# Run Mocha directly, fail if any test fails +if ! node_modules/.bin/mocha "test/**/*.unit.test.js" "test/**/*.property.test.js"; then + echo "Initial test suite failed. Skipping prepush and aborting push." + + if kill -0 "$APP_PID" 2>/dev/null; then + echo "Stopping app (PID: $APP_PID)..." + kill "$APP_PID" + sleep 1 + if kill -0 "$APP_PID" 2>/dev/null; then + kill -9 "$APP_PID" 2>/dev/null || true + fi + fi + + wait "$APP_PID" 2>/dev/null || true + exit 1 +fi + +echo "$COMMIT_HASH" > "$CHECKSUM_FILE" +echo "✓ All tests passed for commit: $COMMIT_HASH" + +node src/app.js >/dev/null 2>&1 & + APP_PID=$! + +sleep 2 + +npm run test:prepush +TEST_RESULT=$? + + # Clean up the app process + if kill -0 $APP_PID 2>/dev/null; then + echo "Stopping app (PID: $APP_PID)..." + kill $APP_PID + # Give it time to shut down gracefully + sleep 1 + # Force kill if still running + if kill -0 $APP_PID 2>/dev/null; then + kill -9 $APP_PID 2>/dev/null || true + fi + fi + + # Wait for process to fully terminate + wait $APP_PID 2>/dev/null || true + + +if [ $TEST_RESULT -ne 0 ]; then + echo "Tests failed. Push aborted." + exit 1 +fi diff --git a/scripts/runTests.js b/scripts/runTests.js index baa69ab..6a4a7e7 100644 --- a/scripts/runTests.js +++ b/scripts/runTests.js @@ -2,6 +2,25 @@ 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; + } +} + +function cacheTestedCommit(commitHash) { + fs.writeFileSync(CACHE_FILE, commitHash + "\n"); +} + async function runTestFile(filePath, description) { console.log(`Running ${description}...`); @@ -25,8 +44,10 @@ async function runTests() { try { - const commitHash = execSync("git rev-parse HEAD").toString().trim(); - if (fs.readFileSync(".last_tested_commit", "utf-8").trim() === commitHash) { + const commitHash = getCurrentCommitHash(); + const lastCommit = getLastTestedCommit(); + + if (lastCommit === commitHash) { process.exit(0); } @@ -35,7 +56,7 @@ await runTestFile("./test/routes.test.js", "route tests"); console.log("✓ All tests passed!"); - fs.writeFileSync(".last_tested_commit", commitHash + "\n"); + cacheTestedCommit(commitHash); } catch (error) { console.error("Test execution failed:", error.message); process.exit(1);