Skip to content

Commit

Permalink
Add an automatic validation check to schemas when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
IanCa committed Jan 11, 2024
1 parent d19292e commit 012e186
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/check_schemas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Process Files

on:
push:
branches: [ * ]
pull_request:
branches: [ * ]

jobs:
process-changed-files:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # OR "2" -> To retrieve the preceding commit.

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v41
# To compare changes between the current commit and the last pushed remote commit set `since_last_remote_commit: true`. e.g
# with:
# since_last_remote_commit: true

- name: List all changed files
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
for file in "$ALL_CHANGED_FILES"; do
echo "$file was changed"
done
- name: Install dependencies
run: pip install git+https://github.com/hed-standard/hed-python.git@develop

- name: Run command on file list
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
python tests/validate_schemas.py $ALL_CHANGED_FILES
43 changes: 43 additions & 0 deletions tests/validate_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys
from hed.schema import load_schema, from_string
from hed.errors import get_printable_issue_string

def main():
validation_issues = []
saving_failures = []
for file_path in sys.argv[1:]: # sys.argv[1:] contains all the arguments passed to the script
# print(file_path)
if file_path.endswith(".xml") or file_path.endswith(".mediawiki"):
base_schema = load_schema(file_path)
issues = base_schema.check_compliance()
if issues:
validation_issues.extend(issues)
print(get_printable_issue_string(issues, title=file_path))

mediawiki_string = base_schema.get_as_mediawiki_string()
reloaded_schema = from_string(mediawiki_string, schema_format=".mediawiki")

if reloaded_schema != base_schema:
error_text = f"Failed to reload {file_path} as mediawiki. There is either a problem with the source file, or the saving/loading code."
saving_failures.append(error_text)
print(error_text)

xml_string = base_schema.get_as_xml_string()
reloaded_schema = from_string(xml_string, schema_format=".xml")

if reloaded_schema != base_schema:
error_text = f"Failed to reload {file_path} as xml. There is either a problem with the source file, or the saving/loading code."
saving_failures.append(error_text)
print(error_text)

# if saving_failures:
# for issue in saving_failures:
# print(issue)

if validation_issues or saving_failures:
return 1
return 0


if __name__ == "__main__":
exit(main())

0 comments on commit 012e186

Please sign in to comment.