-
-
Notifications
You must be signed in to change notification settings - Fork 35
Validate test cases against a schema #778
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab5101a
Validate test cases against provided schema
echeran bc16a30
Use command line JSON Schema validator from NPM
echeran 9f50b31
Insert omitted step to install JSON Schema validator tool
echeran 2511e34
Apply suggestions from code review
echeran f80c0f9
Add constraint for runs on PRs based on modified paths
echeran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,20 @@ | ||
| name: Validate test data | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| branches: '**' | ||
eemeli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| jobs: | ||
| run_all: | ||
| name: Validate tests for specific schema version | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repo | ||
| uses: actions/checkout@v4 | ||
| - name: Validate tests for schema v0 | ||
| run: | | ||
| cd test | ||
| python3 validate.py --schema-dir ./schemas/v0/ | ||
This file contains hidden or 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,59 @@ | ||
| #!/bin/env python3 | ||
|
|
||
| import argparse | ||
| import json | ||
| import jsonschema | ||
| import logging | ||
| import os | ||
| import sys | ||
|
|
||
|
|
||
| def parse_args(args): | ||
| parser = argparse.ArgumentParser( | ||
| prog='Test File Schema Validator', | ||
| description='Validate all of the test case JSON files against the JSON Schema file' | ||
| ) | ||
|
|
||
| parser.add_argument('--schema-dir', | ||
| help='The directory containing the JSON schema file for the test cases (dir named according to a semantic version)') | ||
| parser.add_argument('--test-dir', | ||
| default='tests', | ||
| help='The base directory in which all test files are contained') | ||
|
|
||
| # ignore the executable's name from the arg list, which is in the first position | ||
| return parser.parse_args(args[1:]) | ||
|
|
||
| def main(args): | ||
| # initialize logging | ||
| logging.basicConfig(format='[%(asctime)s] [%(levelname)s] %(message)s', level=logging.INFO) | ||
|
|
||
| # parse args | ||
| parsed_args = parse_args(args) | ||
|
|
||
| # get schema file from the schema dir | ||
| schema_dir = parsed_args.schema_dir | ||
| schema_file_paths = os.listdir(path = schema_dir) | ||
| assert len(schema_file_paths) == 1, "there should only be 1 schema(s) definition file for a given schema version" | ||
| schema_file_path = os.path.join(schema_dir, schema_file_paths[0]) | ||
| logging.debug("schema file path: %s", schema_file_path) | ||
| schema_file = open(schema_file_path, encoding='utf-8', mode='r') | ||
| schema = json.load(schema_file) | ||
|
|
||
| # iterate over all files in the test case file dir and validate against the schema | ||
| test_dir = parsed_args.test_dir | ||
| for (dirpath, dirnames, filenames) in os.walk(test_dir): | ||
| for test_filename in filenames: | ||
| test_file_path = os.path.join(dirpath, test_filename) | ||
| logging.debug("test file path: %s", test_file_path) | ||
| test_file = open(test_file_path, encoding='utf-8', mode='r') | ||
| logging.debug("verifying test case file: %s", test_file_path) | ||
| tests = json.load(test_file) | ||
| jsonschema.validate(tests, schema) | ||
|
|
||
| test_file.close() | ||
| schema_file.close() | ||
|
|
||
| logging.info("Completed verification of all test case files successfully") | ||
|
|
||
| if __name__ == "__main__": | ||
| main(sys.argv) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.