From 012e186c2407483c516107fa25945a0d24c480bb Mon Sep 17 00:00:00 2001 From: IanCa Date: Thu, 11 Jan 2024 17:39:49 -0600 Subject: [PATCH] Add an automatic validation check to schemas when changed --- .github/workflows/check_schemas.yaml | 45 ++++++++++++++++++++++++++++ tests/validate_schemas.py | 43 ++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 .github/workflows/check_schemas.yaml create mode 100644 tests/validate_schemas.py diff --git a/.github/workflows/check_schemas.yaml b/.github/workflows/check_schemas.yaml new file mode 100644 index 00000000..226855e9 --- /dev/null +++ b/.github/workflows/check_schemas.yaml @@ -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 diff --git a/tests/validate_schemas.py b/tests/validate_schemas.py new file mode 100644 index 00000000..7800aebf --- /dev/null +++ b/tests/validate_schemas.py @@ -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())