From 8778868c1d9ada8b1c59c74b215a51b1b1a68188 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 16 Oct 2024 09:41:27 -0400 Subject: [PATCH] feat: Default font source is now "system" For `brand_yaml` but is configurable via an envvar --- pkg-py/src/brand_yaml/typography.py | 2 +- ...d_typography_ex_minimal_mixed_source.json} | 0 ...st_brand_typography_ex_simple_google.json} | 0 ...est_brand_typography_ex_simple_system.json | 48 +++++++++++++++++++ .../tests/__snapshots__/test_utils_yaml.ambr | 11 ----- pkg-py/tests/test_typography.py | 34 ++++++++++--- pkg-py/tests/utils/__init__.py | 18 +++++++ 7 files changed, 94 insertions(+), 19 deletions(-) rename pkg-py/tests/__snapshots__/test_typography/{test_brand_typography_ex_minimal_system.json => test_brand_typography_ex_minimal_mixed_source.json} (100%) rename pkg-py/tests/__snapshots__/test_typography/{test_brand_typography_ex_simple.json => test_brand_typography_ex_simple_google.json} (100%) create mode 100644 pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple_system.json diff --git a/pkg-py/src/brand_yaml/typography.py b/pkg-py/src/brand_yaml/typography.py index 3c8bd320..5e77a33f 100644 --- a/pkg-py/src/brand_yaml/typography.py +++ b/pkg-py/src/brand_yaml/typography.py @@ -1383,7 +1383,7 @@ def _default_fonts_provider(cls, data: Any): "family": data[field]["family"], "source": os.environ.get( "BRAND_YAML_DEFAULT_FONT_SOURCE", - "google", + "system", ), } ) diff --git a/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_minimal_system.json b/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_minimal_mixed_source.json similarity index 100% rename from pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_minimal_system.json rename to pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_minimal_mixed_source.json diff --git a/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple.json b/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple_google.json similarity index 100% rename from pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple.json rename to pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple_google.json diff --git a/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple_system.json b/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple_system.json new file mode 100644 index 00000000..1b2c4407 --- /dev/null +++ b/pkg-py/tests/__snapshots__/test_typography/test_brand_typography_ex_simple_system.json @@ -0,0 +1,48 @@ +{ + "color": { + "primary": "blue" + }, + "meta": { + "name": { + "full": "examples/brand-typography-simple.yml" + } + }, + "typography": { + "base": { + "family": "Open Sans", + "line-height": 1.25, + "size": "1rem" + }, + "fonts": [ + { + "family": "Open Sans", + "source": "system" + }, + { + "family": "Roboto Slab", + "source": "system" + }, + { + "family": "Fira Code", + "source": "system" + } + ], + "headings": { + "color": "blue", + "family": "Roboto Slab", + "weight": 600 + }, + "monospace": { + "family": "Fira Code", + "size": "0.9em" + }, + "monospace-block": { + "family": "Fira Code", + "size": "0.9em" + }, + "monospace-inline": { + "family": "Fira Code", + "size": "0.9em" + } + } +} diff --git a/pkg-py/tests/__snapshots__/test_utils_yaml.ambr b/pkg-py/tests/__snapshots__/test_utils_yaml.ambr index b6ae1c55..65b512c7 100644 --- a/pkg-py/tests/__snapshots__/test_utils_yaml.ambr +++ b/pkg-py/tests/__snapshots__/test_utils_yaml.ambr @@ -11,17 +11,6 @@ typography: fonts: - family: Raleway - weight: - - 100 - - 200 - - 300 - - 400 - - 500 - - 600 - - 700 - - 800 - - 900 - url: https://fonts.googleapis.com/ headings: family: Raleway diff --git a/pkg-py/tests/test_typography.py b/pkg-py/tests/test_typography.py index f2a38062..cca1c326 100644 --- a/pkg-py/tests/test_typography.py +++ b/pkg-py/tests/test_typography.py @@ -26,7 +26,7 @@ validate_font_weight, ) from syrupy.extensions.json import JSONSnapshotExtension -from utils import path_examples, pydantic_data_from_json +from utils import path_examples, pydantic_data_from_json, set_env_var @pytest.fixture @@ -488,11 +488,29 @@ def test_brand_typography_font_bunny_import_url(): ) -def test_brand_typography_ex_simple(snapshot_json): +def test_brand_typography_ex_simple_system(snapshot_json): brand = read_brand_yaml(path_examples("brand-typography-simple.yml")) assert isinstance(brand.typography, BrandTypography) + assert isinstance(brand.typography.fonts, list) + assert len(brand.typography.fonts) == 3 + assert [f.family for f in brand.typography.fonts] == [ + "Open Sans", + "Roboto Slab", + "Fira Code", + ] + assert [f.source for f in brand.typography.fonts] == ["system"] * 3 + + assert snapshot_json == pydantic_data_from_json(brand) + + +def test_brand_typography_ex_simple_google(snapshot_json): + with set_env_var("BRAND_YAML_DEFAULT_FONT_SOURCE", "google"): + brand = read_brand_yaml(path_examples("brand-typography-simple.yml")) + + assert isinstance(brand.typography, BrandTypography) + assert isinstance(brand.typography.fonts, list) assert len(brand.typography.fonts) == 3 assert [f.family for f in brand.typography.fonts] == [ @@ -604,7 +622,8 @@ def test_brand_typography_ex_color(snapshot_json): def test_brand_typography_ex_minimal(snapshot_json): - brand = read_brand_yaml(path_examples("brand-typography-minimal.yml")) + with set_env_var("BRAND_YAML_DEFAULT_FONT_SOURCE", "google"): + brand = read_brand_yaml(path_examples("brand-typography-minimal.yml")) assert isinstance(brand.typography, BrandTypography) @@ -622,10 +641,11 @@ def test_brand_typography_ex_minimal(snapshot_json): assert snapshot_json == pydantic_data_from_json(brand) -def test_brand_typography_ex_minimal_system(snapshot_json): - brand = read_brand_yaml( - path_examples("brand-typography-minimal-system.yml") - ) +def test_brand_typography_ex_minimal_mixed_source(snapshot_json): + with set_env_var("BRAND_YAML_DEFAULT_FONT_SOURCE", "google"): + brand = read_brand_yaml( + path_examples("brand-typography-minimal-system.yml") + ) assert isinstance(brand.typography, BrandTypography) diff --git a/pkg-py/tests/utils/__init__.py b/pkg-py/tests/utils/__init__.py index 13f9f519..378a9ca3 100644 --- a/pkg-py/tests/utils/__init__.py +++ b/pkg-py/tests/utils/__init__.py @@ -1,4 +1,6 @@ import json +import os +from contextlib import contextmanager from pathlib import Path from pydantic import BaseModel @@ -17,3 +19,19 @@ def pydantic_data_from_json(model: BaseModel) -> dict: exclude_none=True, ) return json.loads(data) + + +@contextmanager +def set_env_var(key, value): + # Store the original value + original_value = os.environ.get(key) + # Set the new value + os.environ[key] = value + try: + yield + finally: + # Restore the original value + if original_value is not None: + os.environ[key] = original_value + else: + del os.environ[key]