Skip to content

Commit

Permalink
Merge pull request #8596 from google/font-tag-ci
Browse files Browse the repository at this point in the history
ci test_font_tags: Add more checks
  • Loading branch information
m4rc1e authored Nov 26, 2024
2 parents 87c2ac1 + 2eb8371 commit dcdf296
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions .ci/test_font_tags.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
import pytest
import json
from urllib.request import urlopen
import sys

dev_data = json.loads(
urlopen("https://fonts.google.com/metadata/fonts").read().decode("utf-8")
)
dev_families = set(f["family"] for f in dev_data["familyMetadataList"])

csv_data = (
urlopen("https://raw.githubusercontent.com/google/fonts/main/tags/all/families.csv")
.read()
.decode("utf-8")
)
csv_families = set(
line.split(",")[0]
for line in csv_data.split("\n")
if line
if line.split(",")[0] != "Family"
)

families_missing_tags = sorted(dev_families - csv_families)

if families_missing_tags:
import csv


@pytest.fixture
def family_metadata():
data = json.loads(
urlopen("https://fonts.google.com/metadata/fonts").read().decode("utf-8")
)
return data["familyMetadataList"]


@pytest.fixture
def family_tags():
csv_data = (
urlopen(
"https://raw.githubusercontent.com/google/fonts/main/tags/all/families.csv"
)
.read()
.decode("utf-8")
)
reader = csv.reader(csv_data.splitlines())
res = []
for row in reader:
res.append([row[0], row[1], float(row[2])])
return res


def test_families_missing_tags(family_tags, family_metadata):
tagged_families = set(f[0] for f in family_tags)
families_in_gf = set(f["family"] for f in family_metadata)
families_missing_tags = sorted(families_in_gf - tagged_families)
missing_list = "\n".join(families_missing_tags)
raise ValueError(

assert len(families_missing_tags) == 0, (
f"The following {len(families_missing_tags)} families are missing tags:\n\n"
f"{missing_list}\n\n"
"Please add tags for these families using the following webapp: "
"https://google.github.io/fonts/tags.html"
)

sys.exit(0)

def test_no_duplicate_families(family_tags):
seen = set()
dups = []
for family, cat, _ in family_tags:
key = (family, cat)
if key in seen:
dups.append(",".join(key))
seen.add(key)
assert not dups, f"Duplicate tags found: {dups}"


def test_tag_vals_in_range(family_tags):
out_of_range = []
for family, cat, val in family_tags:
if val <= 0 or val > 100:
out_of_range.append((family, cat, val))
assert not out_of_range, f"Values out of range 1-100: {out_of_range}"

0 comments on commit dcdf296

Please sign in to comment.