Skip to content
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

CI: Ensure immutability of test cases #1712

Merged
merged 17 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ jobs:

- name: All required files are present
run: bin/check_required_files_present

immutability:
name: No test has been mutated
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
path: 'new'

# Pull Requests
- name: Checkout the target branch (PRs)
uses: actions/checkout@v2
if: github.event_name == 'pull_request'
with:
ref: '${{ github.base_ref }}'
path: 'old'

# Pushes
- name: Checkout the previous commit (Pushes)
uses: actions/checkout@v2
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
with:
ref: '${{ github.sha }}~'
Copy link
Contributor Author

@SaschaMann SaschaMann Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only gonna work reliably on the main branch fwiw

path: 'old'

- name: Check that no test has been mutated
run: |
for oldf in old/exercises/*/canonical-data.json; do
newf=${oldf//old/new}
./new/bin/check-immutability.py "$oldf" "$newf"
done

schema-validation:
name: Schema validation
Expand Down
54 changes: 54 additions & 0 deletions bin/check-immutability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.8 is the default Python in the GHA env.


import json
import subprocess
import sys

oldf = sys.argv[1]
newf = sys.argv[2]

immutable_keys = ('property', 'input', 'expected')

# Use jq to flatten the test data, and parse it
old = json.loads(subprocess.run([f"jq -r '[.. | objects | select(.uuid != null)]' {oldf}"], stdout=subprocess.PIPE, shell=True).stdout.decode('utf-8'))
new = json.loads(subprocess.run([f"jq -r '[.. | objects | select(.uuid != null)]' {newf}"], stdout=subprocess.PIPE, shell=True).stdout.decode('utf-8'))

# Convert new to dict uuid => case
new = {c['uuid']: c for c in new}

fails = set()
deleted = set()

# Iterate through old cases as only those could potentially be mutated
for case in old:
uuid = case['uuid']

# Check if the case has been deleted
if uuid not in new:
deleted.add(uuid)
continue

# Check that scenarios are only updated additively
if 'scenarios' in case and not set(case['scenarios']).issubset(set(new[uuid]['scenarios'])):
fails.add(uuid)
continue
# Check for changes to immutable keys
for k in immutable_keys:
if case[k] != new[uuid][k]:
fails.add(uuid)
break

if len(fails) == 0 and len(deleted) == 0:
exit(0)

if len(fails) > 0:
print('The following tests contain illegal mutations:')
for f in fails:
print(f" - {f} ({new[f]['description']})")

if len(deleted) > 0:
print('The following tests have been deleted illegally:')
for d in deleted:
print(f" - {d}")

exit(1)