From fe019a002224fbfbf7d1c79f53237981c307bbe7 Mon Sep 17 00:00:00 2001 From: Matthias Friedrich Date: Tue, 16 Dec 2025 19:12:05 +0100 Subject: [PATCH] Add some unit tests --- .github/workflows/CI.yml | 27 ++++++++++++++++++++++++++ pyproject.toml | 4 ++++ tests/data/invalid.css | 5 +++++ tests/data/main.css | 5 +++++ tests/data/normalize.css | 3 +++ tests/test_bundle_css.py | 30 +++++++++++++++++++++++++++++ tests/test_process_stylesheet.py | 33 ++++++++++++++++++++++++++++++++ 7 files changed, 107 insertions(+) create mode 100644 tests/data/invalid.css create mode 100644 tests/data/main.css create mode 100644 tests/data/normalize.css create mode 100644 tests/test_bundle_css.py create mode 100644 tests/test_process_stylesheet.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 3085f29..6ebf35f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -87,6 +87,33 @@ jobs: name: wheels-${{ runner.os }}-${{ matrix.target }} path: dist/* + test: + name: Test wheels on ${{ matrix.os }} (${{ matrix.python-version }}) + needs: linux + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest] + python-version: ['3.10'] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Download x86_64 wheels + uses: actions/download-artifact@v4 + with: + name: wheels-Linux-x86_64 + path: dist + - name: Install wheel and dependencies + run: | + pip install --upgrade pip + pip install --no-index --find-links=dist lightningcss + pip install --group dev + - name: Run tests + run: pytest + sdist: runs-on: ubuntu-latest steps: diff --git a/pyproject.toml b/pyproject.toml index 59e9530..ad0a8a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,10 @@ classifiers = [ [project.urls] Source = "https://github.com/pydsigner/python-lightningcss" +[dependency-groups] +dev = [ + "pytest>=9.0.2", +] [tool.maturin] features = ["pyo3/extension-module"] diff --git a/tests/data/invalid.css b/tests/data/invalid.css new file mode 100644 index 0000000..e2cf0d4 --- /dev/null +++ b/tests/data/invalid.css @@ -0,0 +1,5 @@ +* { + box-sizing: border-box; +} + +something invalid diff --git a/tests/data/main.css b/tests/data/main.css new file mode 100644 index 0000000..fe59f08 --- /dev/null +++ b/tests/data/main.css @@ -0,0 +1,5 @@ +@import "normalize.css" layer(normalize); + +a { + color: blue; +} diff --git a/tests/data/normalize.css b/tests/data/normalize.css new file mode 100644 index 0000000..1236d8e --- /dev/null +++ b/tests/data/normalize.css @@ -0,0 +1,3 @@ +* { + box-sizing: border-box; +} diff --git a/tests/test_bundle_css.py b/tests/test_bundle_css.py new file mode 100644 index 0000000..da1be14 --- /dev/null +++ b/tests/test_bundle_css.py @@ -0,0 +1,30 @@ +from pathlib import Path +import pytest +import lightningcss as lcss + +TEST_DATA_DIR = Path(__file__).parent / "data" +MAIN_CSS_PATH = TEST_DATA_DIR / "main.css" +NORMALIZE_CSS_PATH = TEST_DATA_DIR / "normalize.css" +MISSING_CSS_PATH = TEST_DATA_DIR / "not-there.css" +INVALID_CSS_PATH = TEST_DATA_DIR / "invalid.css" + + +def test_minify(): + expected = "@layer normalize{*{box-sizing:border-box}}a{color:#00f}" + + result = lcss.bundle_css(str(MAIN_CSS_PATH)) + assert result == expected + +def test_pretty_print(): + expected = "* {\n box-sizing: border-box;\n}\n" + + result = lcss.bundle_css(str(NORMALIZE_CSS_PATH), minify=False) + assert result == expected + +def test_file_not_found(): + with pytest.raises(OSError): + lcss.bundle_css(str(MISSING_CSS_PATH)) + +def test_parse_error(): + with pytest.raises(ValueError): + lcss.bundle_css(str(INVALID_CSS_PATH)) diff --git a/tests/test_process_stylesheet.py b/tests/test_process_stylesheet.py new file mode 100644 index 0000000..31040cb --- /dev/null +++ b/tests/test_process_stylesheet.py @@ -0,0 +1,33 @@ +import lightningcss as lcss +import pytest + + +def test_minify(): + code = ".content { margin-inline: 3vw; padding: 1rem 2rem 1rem 2rem; }" + expected = ".content{margin-inline:3vw;padding:1rem 2rem}" + + result = lcss.process_stylesheet(code) + assert result == expected + +def test_pretty_print(): + code = ".content { margin: 3vw; }" + expected = ".content {\n margin: 3vw;\n}\n" + + result = lcss.process_stylesheet(code, minify=False) + assert result == expected + +def test_parse_error(): + code = "img { display: block; } nonsense }{ non-terminated" + + with pytest.raises(ValueError): + lcss.process_stylesheet(code) + +def test_error_recovery(): + code = "img { display: block; } nonsense }{ non-terminated" + expected = "img{display:block}" + + with pytest.raises(ValueError): + lcss.process_stylesheet(code, error_recovery=False) + + result = lcss.process_stylesheet(code, error_recovery=True) + assert result == expected