Skip to content

Commit 8a1e963

Browse files
authored
feat: add version check and fix version
Merge pull request #6 from etalab/add-version-test
2 parents 6d5e05a + d90e170 commit 8a1e963

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import json
2+
import re
3+
import os
4+
5+
pattern = r"v?\d+\.\d+\.\d+"
6+
7+
8+
def check(obj, version, parents=""):
9+
"""
10+
This functions recursively parses all fields in the schema looking for
11+
version names that would not be the same as the one mentionned
12+
in the 'version' field
13+
"""
14+
errors = []
15+
# if field is a string, we check for a potential version
16+
if isinstance(obj, str):
17+
tmp = re.search(pattern, obj)
18+
if tmp and tmp[0] != version:
19+
errors += [(parents, tmp[0])]
20+
# if field is a list, we check every item
21+
elif isinstance(obj, list):
22+
for idx, k in enumerate(obj):
23+
errors += check(k, version, parents=parents + f"[{str(idx)}]")
24+
# if field is a dict, we check every value
25+
elif isinstance(obj, dict):
26+
for k in obj:
27+
# not checking the fields
28+
if k != "fields":
29+
errors += check(
30+
obj[k],
31+
version,
32+
parents=parents + "." + k if parents else k
33+
)
34+
return errors
35+
36+
37+
to_check = []
38+
39+
if "schema.json" in os.listdir():
40+
to_check.append("schema.json")
41+
42+
elif "datapackage.json" in os.listdir():
43+
with open("datapackage.json", "r") as f:
44+
datapackage = json.load(f)
45+
for r in datapackage["resources"]:
46+
to_check.append(r["schema"])
47+
48+
else:
49+
raise Exception("No required file found")
50+
51+
for schema_path in to_check:
52+
with open(schema_path, "r") as f:
53+
schema = json.load(f)
54+
version = schema["version"]
55+
56+
errors = check(schema, version)
57+
if errors:
58+
message = (
59+
f"Versions are mismatched within the schema '{schema['name']}', "
60+
f"expected version '{version}' but:"
61+
)
62+
for e in errors:
63+
message += f"\n- {e[0]} has version '{e[1]}'"
64+
raise Exception(message)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Vérification de la cohérence des versions dans tous les champs du schéma
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Checkout Repository
12+
uses: actions/checkout@v4
13+
14+
- name: Set up Python
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: "3.10"
18+
19+
- run: python .github/workflows/assert_version.py
File renamed without changes.

schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"countryCode":"FR",
1313
"homepage":"https://github.com/etalab/schema-flotte-publique-vehicule",
14-
"path":"https://github.com/etalab/schema-flotte-publique-vehicule/raw/v0.1.2/schema.json",
14+
"path":"https://github.com/etalab/schema-flotte-publique-vehicule/raw/v0.1.3/schema.json",
1515
"image":"",
1616
"licenses":[
1717
{
@@ -43,7 +43,7 @@
4343
],
4444
"created":"2021-09-15",
4545
"lastModified":"2022-12-23",
46-
"version":"0.1.3",
46+
"version":"v0.1.3",
4747
"contributors":[
4848
{
4949
"title":"Julieta Barado",

0 commit comments

Comments
 (0)