# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. import os import sys import mozunit import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import token_resolve # noqa: E402 LAYERS = "@layer tokens-foundation, tokens-foundation-nova;\n" def write_sheets(directory, foundation, nova=""): shared = f"{LAYERS}:root {{\n @layer tokens-foundation {{\n{foundation}\n }}\n" if nova: shared += ( ' @media -moz-pref("browser.nova.enabled") {\n' f" @layer tokens-foundation-nova {{\n{nova}\n }}\n }}\n" ) shared += "}\n" (directory / "tokens-shared.css").write_text(shared) for name in ("tokens-platform.css", "tokens-brand.css"): (directory / name).write_text(":root {\n}\n") return str(directory) def test_resolving_tokens_pass(tmp_path, capsys): directory = write_sheets(tmp_path, "--a: #fff;\n--b: var(--a);") assert token_resolve.check(directory) == 0 assert capsys.readouterr().out == "every token resolves\n" def test_bare_keyword_passes(tmp_path): directory = write_sheets(tmp_path, "--a: unset;\n--b: var(--a);") assert token_resolve.check(directory) == 0 def test_cycle_fails(tmp_path, capsys): directory = write_sheets(tmp_path, "--a: var(--b);\n--b: var(--a);") assert token_resolve.check(directory) == 1 assert "--a in chrome: a var() cycle\n" in capsys.readouterr().out def test_depth_limit_fails(tmp_path, capsys): chain = "\n".join(f"--t{i}: var(--t{i + 1});" for i in range(40)) directory = write_sheets(tmp_path, f"{chain}\n--t40: #fff;") assert token_resolve.check(directory) == 1 assert ( "--t0 in chrome: more than 32 nested var() references\n" in capsys.readouterr().out ) def test_undefined_reference_fails(tmp_path, capsys): directory = write_sheets(tmp_path, "--a: var(--fake-var);") assert token_resolve.check(directory) == 1 assert "--a in chrome: `--fake-var` is undefined\n" in capsys.readouterr().out def test_undefined_reference_with_fallback_passes(tmp_path): directory = write_sheets(tmp_path, "--a: var(--fake-var, #fff);") assert token_resolve.check(directory) == 0 def test_keyword_inside_function_fails(tmp_path, capsys): directory = write_sheets( tmp_path, "--ghost: inherit;\n--a: light-dark(var(--ghost), #fff);" ) assert token_resolve.check(directory) == 1 assert ( "--a in chrome: `inherit` inside light-dark(), from --ghost: inherit\n" " declared as: light-dark(var(--ghost), #fff)\n" " resolves to: light-dark(inherit, #fff)\n" ) in capsys.readouterr().out def test_nova_reference_to_inherit_token_fails(tmp_path, capsys): directory = write_sheets( tmp_path, "--button-text-color: currentColor;\n" "--button-text-color-hover: var(--button-text-color);\n" "--button-text-color-ghost-hover: inherit;\n" "--button-text-color-menu-hover: var(--button-text-color-ghost-hover);", "--button-text-color-menu-hover: light-dark(" "var(--button-text-color-ghost-hover), var(--button-text-color-hover));", ) assert token_resolve.check(directory) == 1 out = capsys.readouterr().out assert ( "--button-text-color-menu-hover in chrome: `inherit` inside light-dark()," " from --button-text-color-ghost-hover: inherit\n" " declared as: light-dark(var(--button-text-color-ghost-hover)," " var(--button-text-color-hover))\n" " resolves to: light-dark(inherit, currentColor)\n" " wins in: default\n" ) in out assert "browser.nova.enabled=false" not in out def test_cycle_does_not_blame_a_keyword_token(tmp_path, capsys): directory = write_sheets( tmp_path, "--k: inherit;\n--a: var(--b) var(--k);\n--b: var(--a);" ) assert token_resolve.check(directory) == 1 assert "--a in chrome: a var() cycle\n" in capsys.readouterr().out def test_important_declaration_reported_once(tmp_path, capsys): directory = write_sheets( tmp_path, "--ghost: inherit;\n--a: light-dark(var(--ghost), #fff) !important;", ) assert token_resolve.check(directory) == 1 out = capsys.readouterr().out assert out.count("--a in chrome:") == 1 assert out.count("--a in in-content:") == 1 def test_nova_only_reference_passes(tmp_path): directory = write_sheets(tmp_path, "--a: var(--b);", "--b: #fff;") assert token_resolve.check(directory) == 0 def component(path, body): path.parent.mkdir(parents=True, exist_ok=True) path.write_text( f"{LAYERS}:root,\n:host {{\n @layer tokens-foundation {{\n{body}\n }}\n}}\n" ) return str(path) def test_component_reads_realm_tokens(tmp_path): directory = write_sheets(tmp_path, "--a: #fff;") sheet = component(tmp_path / "c" / "x.tokens.css", "--x-a: var(--a);") assert token_resolve.check(directory, [(("chrome",), [sheet])]) == 0 def test_component_undefined_reference_fails(tmp_path, capsys): directory = write_sheets(tmp_path, "--a: #fff;") sheet = component(tmp_path / "c" / "x.tokens.css", "--x-a: var(--fake-var);") assert token_resolve.check(directory, [(("chrome",), [sheet])]) == 1 out = capsys.readouterr().out assert "--x-a in chrome (x.tokens.css): `--fake-var` is undefined\n" in out assert "in-content" not in out def test_components_resolve_with_their_group(tmp_path): directory = write_sheets(tmp_path, "--a: #fff;") x = component(tmp_path / "c" / "x.tokens.css", "--x-a: var(--y-a);") y = component(tmp_path / "c" / "y.tokens.css", "--y-a: var(--a);") assert token_resolve.check(directory, [(("chrome",), [x, y])]) == 0 def test_component_pass_reports_only_component_tokens(tmp_path, capsys): directory = write_sheets(tmp_path, "--a: var(--fake-var);") sheet = component(tmp_path / "c" / "x.tokens.css", "--x-a: #fff;") assert token_resolve.check(directory, [(("chrome",), [sheet])]) == 1 out = capsys.readouterr().out assert "(x.tokens.css)" not in out assert "2 broken token value(s)" in out def test_component_sheets_by_directory(tmp_path): dist = tmp_path / "toolkit" / "themes" / "shared" / "design-system" / "dist" dist.mkdir(parents=True) paths = dist.parent / "config" / "helpers" / "general" / "token-paths.mjs" paths.parent.mkdir(parents=True) paths.write_text( "export const COMPONENT_TOKEN_PATHS = [\n" ' { dir: "src/tokens/components", isGlobal: true },\n' ' { dir: "../../../content/widgets", nameTransform: n => n },\n' ' { dir: "../../../../browser/themes/shared" },\n' "];\n" ) widgets = tmp_path / "toolkit" / "content" / "widgets" shared = tmp_path / "browser" / "themes" / "shared" for path in ( shared / "urlbar" / "urlbar.tokens.css", shared / "urlbar" / "urlbarview.tokens.css", widgets / "moz-badge" / "moz-badge.tokens.css", widgets / "moz-page-nav" / "moz-page-nav.tokens.css", widgets / "moz-badge" / "moz-badge.css", ): component(path, "") groups = { tuple(os.path.basename(p) for p in paths): realms for realms, paths in token_resolve.component_sheets(str(dist)) } assert groups == { ("urlbar.tokens.css", "urlbarview.tokens.css"): ("chrome", "in-content"), ("moz-badge.tokens.css",): ("chrome", "in-content"), ("moz-page-nav.tokens.css",): ("in-content",), } def test_unreadable_token_paths_entry_exits(tmp_path): dist = tmp_path / "design-system" / "dist" dist.mkdir(parents=True) paths = dist.parent / "config" / "helpers" / "general" / "token-paths.mjs" paths.parent.mkdir(parents=True) paths.write_text( "export const COMPONENT_TOKEN_PATHS = [\n" ' { dir: "../widgets", nameTransform: n => { return n; } },\n' "];\n" ) with pytest.raises(SystemExit): token_resolve.component_dirs(str(dist)) if __name__ == "__main__": mozunit.main()