generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: test migration ci check bug (#2192)
- Fixed a bug with not treating new files as ignored. - Cleaned up and added unit tests.
- Loading branch information
Showing
5 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,12 +138,19 @@ tidy: | |
|
||
# Check for changes in existing SQL migrations compared to main | ||
ensure-frozen-migrations: | ||
scripts/ensure-frozen-migrations | ||
@scripts/ensure-frozen-migrations | ||
|
||
# Run backend tests | ||
test-backend: | ||
@gotestsum --hide-summary skipped --format-hide-empty-pkg -- -short -fullpath ./... | ||
|
||
test-scripts: | ||
GIT_COMMITTER_NAME="CI" \ | ||
GIT_COMMITTER_EMAIL="[email protected]" \ | ||
GIT_AUTHOR_NAME="CI" \ | ||
GIT_AUTHOR_EMAIL="[email protected]" \ | ||
scripts/tests/test-ensure-frozen-migrations.sh | ||
|
||
# Lint the frontend | ||
lint-frontend: build-frontend | ||
@cd frontend && npm run lint && tsc | ||
|
@@ -152,6 +159,9 @@ lint-frontend: build-frontend | |
lint-backend: | ||
@golangci-lint run --new-from-rev=$(git merge-base origin/main HEAD) ./... | ||
|
||
lint-scripts: | ||
@shellcheck -f gcc -e SC2016 $(find scripts -type f -not -path scripts/tests) | to-annotation | ||
|
||
# Run live docs server | ||
docs: | ||
git submodule update --init --recursive | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
set -x | ||
|
||
script_path='scripts/ensure-frozen-migrations' | ||
schema_dir='backend/controller/sql/schema' | ||
|
||
function create() { | ||
echo "CREATE TABLE testing (id SERIAL PRIMARY KEY);" > "$schema_dir/testing.sql" | ||
} | ||
|
||
function remove_existing() { | ||
rm "$schema_dir/20240704103403_create_module_secrets.sql" | ||
} | ||
|
||
function modify_comment_in_existing() { | ||
sed -i.bak 's/Function for deployment notifications./🧐/' "$schema_dir/20231103205514_init.sql" | ||
rm "$schema_dir/20231103205514_init.sql.bak" | ||
} | ||
|
||
function modify_content_in_existing() { | ||
sed -i.bak 's/CREATE OR REPLACE FUNCTION notify_event()/CREATE 🥲 notify_event()/' "$schema_dir/20231103205514_init.sql" | ||
rm "$schema_dir/20231103205514_init.sql.bak" | ||
} | ||
|
||
function no_changes() { | ||
echo "no-changes" >> .gitignore | ||
git add .gitignore | ||
} | ||
|
||
function commit_migrations_dir() { | ||
git add "$schema_dir" | ||
git commit -m "ci: automated test commit, this should not be pushed!" | ||
} | ||
|
||
|
||
# higher order test function, accepting a function as an argument, and an expected fail or pass. | ||
# omitted means it should pass. | ||
# fail should be used when the test is expected to fail. | ||
function test() { | ||
local test_function=$1 | ||
local expected_result=${2:-pass} | ||
local saved_commit | ||
saved_commit=$(git rev-parse HEAD) | ||
|
||
echo "---- $test_function -------------------------" | ||
|
||
$test_function | ||
|
||
git status | ||
commit_migrations_dir | ||
|
||
local did_fail=false | ||
if ! $script_path; then | ||
if [ "$expected_result" == "pass" ]; then | ||
echo "❌ $test_function expected to pass but failed" | ||
did_fail=true | ||
fi | ||
else | ||
if [ "$expected_result" == "fail" ]; then | ||
echo "❌ $test_function expected to fail but passed" | ||
did_fail=true | ||
fi | ||
fi | ||
|
||
# only clean up the schema dir | ||
rm "$schema_dir"/* | ||
git reset "$saved_commit" | ||
git checkout .gitignore "$schema_dir" | ||
|
||
if $did_fail; then | ||
echo "❌ FAIL $test_function" | ||
exit 1 | ||
else | ||
echo "✅ PASS $test_function" | ||
fi | ||
} | ||
|
||
function main() { | ||
# cd into the project root | ||
cd "$(git rev-parse --show-toplevel)" || exit 1 | ||
|
||
test no_changes | ||
test create | ||
test remove_existing fail | ||
test modify_comment_in_existing | ||
test modify_content_in_existing fail | ||
|
||
echo "✅ All tests passed 🎉" | ||
} | ||
|
||
main |