-
Notifications
You must be signed in to change notification settings - Fork 5
🧪 Add comprehensive tests for idstack-migrate #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||
| # Unit tests for bin/idstack-migrate. | ||||||||||||||
| # Run from the repo root (or sourced by smoke-test.sh). | ||||||||||||||
|
|
||||||||||||||
| set -e | ||||||||||||||
|
|
||||||||||||||
| PASS=0 | ||||||||||||||
| FAIL=0 | ||||||||||||||
| TOTAL=0 | ||||||||||||||
|
|
||||||||||||||
| REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" | ||||||||||||||
| MIGRATE="$REPO_ROOT/bin/idstack-migrate" | ||||||||||||||
| FIXTURE_DIR="$REPO_ROOT/test/fixtures" | ||||||||||||||
|
|
||||||||||||||
| assert() { | ||||||||||||||
| TOTAL=$((TOTAL + 1)) | ||||||||||||||
| if eval "$2" >/dev/null 2>&1; then | ||||||||||||||
| PASS=$((PASS + 1)) | ||||||||||||||
| echo " PASS: $1" | ||||||||||||||
| else | ||||||||||||||
| FAIL=$((FAIL + 1)) | ||||||||||||||
| echo " FAIL: $1" | ||||||||||||||
| fi | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| # Skip the suite if python3 is missing (the tool requires python3). | ||||||||||||||
| if ! command -v python3 >/dev/null 2>&1; then | ||||||||||||||
| echo "test-migrate: python3 not available, skipping" | ||||||||||||||
| exit 0 | ||||||||||||||
| fi | ||||||||||||||
|
|
||||||||||||||
| if [ ! -x "$MIGRATE" ]; then | ||||||||||||||
| echo "test-migrate: $MIGRATE missing or not executable" | ||||||||||||||
| exit 1 | ||||||||||||||
| fi | ||||||||||||||
|
|
||||||||||||||
| WORK=$(mktemp -d) | ||||||||||||||
| trap 'rm -rf "$WORK"' EXIT | ||||||||||||||
|
|
||||||||||||||
| echo "test-migrate" | ||||||||||||||
| echo "" | ||||||||||||||
|
|
||||||||||||||
| # --- Test: v1.0 → v1.4 chained migration --- | ||||||||||||||
| cp "$FIXTURE_DIR/manifest-v1.0.json" "$WORK/project.json" | ||||||||||||||
| "$MIGRATE" "$WORK/project.json" >/dev/null 2>&1 | ||||||||||||||
| assert "v1.0→v1.4: version bumped" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['version']=='1.4'\"" | ||||||||||||||
| assert "v1.0→v1.4: has preferences" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert 'preferences' in d\"" | ||||||||||||||
| assert "v1.0→v1.4: preserves project_name" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['project_name']=='Test Course v1.0'\"" | ||||||||||||||
|
|
||||||||||||||
| # --- Test: v1.2 → v1.4 migration --- | ||||||||||||||
| cp "$FIXTURE_DIR/manifest-v1.2.json" "$WORK/project.json" | ||||||||||||||
| "$MIGRATE" "$WORK/project.json" >/dev/null 2>&1 | ||||||||||||||
| assert "v1.2→v1.4: version bumped" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['version']=='1.4'\"" | ||||||||||||||
| assert "v1.2→v1.4: has preferences" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['preferences']['verbosity']=='normal'\"" | ||||||||||||||
|
|
||||||||||||||
| # --- Test: idempotent --- | ||||||||||||||
| assert "v1.2→v1.4: idempotent" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['version']=='1.4'\" && '$MIGRATE' '$WORK/project.json' >/dev/null 2>&1 && python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['version']=='1.4'\"" | ||||||||||||||
|
|
||||||||||||||
| # --- Test: v1.3-drifted → v1.4 cleanup migration --- | ||||||||||||||
| if [ -f "$FIXTURE_DIR/manifest-v1.3-drifted.json" ]; then | ||||||||||||||
| cp "$FIXTURE_DIR/manifest-v1.3-drifted.json" "$WORK/project.json" | ||||||||||||||
| "$MIGRATE" "$WORK/project.json" >/dev/null 2>&1 | ||||||||||||||
| assert "v1.3-drifted→v1.4: version bumped" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert d['version']=='1.4'\"" | ||||||||||||||
| assert "v1.3-drifted→v1.4: red_team summary renamed to findings_summary" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); rt=d['red_team_audit']; assert 'summary' not in rt; assert rt['findings_summary']=={'critical': 3, 'warning': 5, 'info': 2}\"" | ||||||||||||||
| assert "v1.3-drifted→v1.4: _import_quality_flags moved into import_metadata" "python3 -c \"import json; d=json.load(open('$WORK/project.json')); assert '_import_quality_flags' not in d; details=d['import_metadata']['quality_flag_details']; assert len(details)==2 and details[0]['key']=='orphan_module_8'\"" | ||||||||||||||
| fi | ||||||||||||||
|
|
||||||||||||||
| # --- Test: file does not exist --- | ||||||||||||||
| rm -f "$WORK/project.json" | ||||||||||||||
| "$MIGRATE" "$WORK/project.json" >/dev/null 2>&1 | ||||||||||||||
| EC=$? | ||||||||||||||
| assert "missing manifest exits 0" "[ $EC -eq 0 ]" | ||||||||||||||
|
Comment on lines
+70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| # --- Test: malformed json gracefully degraded (fallback to cat, exits 0) --- | ||||||||||||||
| echo "invalid json" > "$WORK/project.json" | ||||||||||||||
| # capture output to check fallback mechanism | ||||||||||||||
| OUT=$("$MIGRATE" "$WORK/project.json" 2>/dev/null) | ||||||||||||||
| EC=$? | ||||||||||||||
| assert "malformed manifest exits 0" "[ $EC -eq 0 ]" | ||||||||||||||
|
Comment on lines
+77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||
| assert "malformed manifest outputs original" "[ \"\$OUT\" = 'invalid json' ]" | ||||||||||||||
|
|
||||||||||||||
| echo "" | ||||||||||||||
| echo "migrate: $PASS/$TOTAL passed, $FAIL failed" | ||||||||||||||
| [ "$FAIL" -eq 0 ] && exit 0 || exit 1 | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
test-migrate.shis missing or not executable, the smoke test will silently skip these unit tests instead of failing. It is safer to run the test script directly so that any execution failure or missing file is caught.