Skip to content

Commit

Permalink
Merge pull request #28 from posit-dev:feat/default-system-font-source
Browse files Browse the repository at this point in the history
feat: Default font source is now "system"
  • Loading branch information
gadenbuie authored Oct 16, 2024
2 parents 57fb01d + 8778868 commit 025bcde
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg-py/src/brand_yaml/typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
11 changes: 0 additions & 11 deletions pkg-py/tests/__snapshots__/test_utils_yaml.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 27 additions & 7 deletions pkg-py/tests/test_typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] == [
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions pkg-py/tests/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
from contextlib import contextmanager
from pathlib import Path

from pydantic import BaseModel
Expand All @@ -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]

0 comments on commit 025bcde

Please sign in to comment.