summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2024-02-04 06:26:24 -0500
committerGitHub <noreply@github.com>2024-02-04 06:26:24 -0500
commit0b92679e1656dd486654ff45de85c528e96d5b26 (patch)
tree7676365c72e29a120ccfe6eb07f35825ae637d58 /scripts
parentb6660610d02a88aeb23fbcda8378be67a85259e7 (diff)
other: add v1 schema + versioning + tests (#1407)
* other: add v1.0 schema * add tests, rename some files for consistency
Diffstat (limited to 'scripts')
-rw-r--r--scripts/schema/bad_file.toml2
-rw-r--r--scripts/schema/requirements.txt2
-rw-r--r--scripts/schema/validator.py55
3 files changed, 59 insertions, 0 deletions
diff --git a/scripts/schema/bad_file.toml b/scripts/schema/bad_file.toml
new file mode 100644
index 00000000..8107aef5
--- /dev/null
+++ b/scripts/schema/bad_file.toml
@@ -0,0 +1,2 @@
+[flags]
+hide_avg_cpu = 'bad'
diff --git a/scripts/schema/requirements.txt b/scripts/schema/requirements.txt
new file mode 100644
index 00000000..1db290b1
--- /dev/null
+++ b/scripts/schema/requirements.txt
@@ -0,0 +1,2 @@
+jsonschema-rs == 0.17.1
+toml == 0.10.2 \ No newline at end of file
diff --git a/scripts/schema/validator.py b/scripts/schema/validator.py
new file mode 100644
index 00000000..d537f928
--- /dev/null
+++ b/scripts/schema/validator.py
@@ -0,0 +1,55 @@
+#!/bin/python3
+
+# A simple script to validate that a schema is valid for a file.
+
+import argparse
+import toml
+import jsonschema_rs
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Validates a file against a JSON schema"
+ )
+ parser.add_argument(
+ "-f", "--file", type=str, required=True, help="The file to check."
+ )
+ parser.add_argument(
+ "-s", "--schema", type=str, required=True, help="The schema to use."
+ )
+ parser.add_argument(
+ "--should_fail",
+ required=False,
+ action="store_true",
+ help="Whether the checked file should fail.",
+ )
+ args = parser.parse_args()
+
+ file = args.file
+ schema = args.schema
+ should_fail = args.should_fail
+
+ with open(file) as f, open(schema) as s:
+ try:
+ validator = jsonschema_rs.JSONSchema.from_str(s.read())
+ except:
+ print("Coudln't create validator.")
+ exit()
+
+ is_valid = validator.is_valid(toml.load(f))
+ if is_valid:
+ if should_fail:
+ print("Fail!")
+ exit(1)
+ else:
+ print("All good!")
+ else:
+ if should_fail:
+ print("Caught error, good!")
+ else:
+ print("Fail!")
+ exit(1)
+
+
+if __name__ == "__main__":
+ main()