diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e3cc3746a..3365bb4ab 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -95,6 +95,18 @@ jobs: format: cobertura fail-on-error: false + test-alpine: + name: Test on Alpine Linux + permissions: + contents: read + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + - name: Run tests in Alpine + run: make test-alpine + test-distribution: name: Check built packages permissions: diff --git a/Makefile b/Makefile index 818f8abbb..f4d1e4d66 100644 --- a/Makefile +++ b/Makefile @@ -149,6 +149,10 @@ docs-all: .venv clean vale doctest docstring html linkcheckbroken ## Clean docs test: .venv ## Run code tests and coverage @uv run tox +.PHONY: test-alpine +test-alpine: ## Run the tests on Alpine Linux with system packages + docker compose -f scripts/docker/compose.yaml run --rm alpine + .PHONY: test-dist test-dist: ## Build and test the distribution files @src/icalendar/tests/test_create_release.sh diff --git a/news/812.bugfix b/news/812.bugfix new file mode 100644 index 000000000..694a5a920 --- /dev/null +++ b/news/812.bugfix @@ -0,0 +1 @@ +Fixed ``dateutil`` timezone detection when it falls back to its bundled zoneinfo. The timezone tests no longer depend on the installed tzdata version, and CI now runs the test suite on Alpine Linux. Reviewed by GPT-6 Luna. :user:`SemTiOne` diff --git a/scripts/docker/compose.yaml b/scripts/docker/compose.yaml new file mode 100644 index 000000000..2f31732e4 --- /dev/null +++ b/scripts/docker/compose.yaml @@ -0,0 +1,26 @@ +services: + alpine: + image: alpine:3.24.4 + working_dir: /tmp + volumes: + - ../..:/src:ro + command: + - sh + - -eu + - -c + - | + apk add --no-cache \ + py3-coverage \ + py3-dateutil \ + py3-hypothesis \ + py3-pytest \ + py3-tox \ + py3-tz \ + py3-tzdata + + mkdir -p /tmp/icalendar + tar -C /src --exclude=.git --exclude=.venv --exclude=.tox -cf - . \ + | tar -C /tmp/icalendar -xf - + + cd /tmp/icalendar + tox -e alpine --override testenv.runner=virtualenv diff --git a/src/icalendar/tests/test_issue_722_generate_vtimezone.py b/src/icalendar/tests/test_issue_722_generate_vtimezone.py index e73c68c4d..a4654336d 100644 --- a/src/icalendar/tests/test_issue_722_generate_vtimezone.py +++ b/src/icalendar/tests/test_issue_722_generate_vtimezone.py @@ -14,10 +14,12 @@ import zoneinfo from datetime import date, datetime, timedelta from re import findall +from types import SimpleNamespace import pytest from dateutil.tz import gettz +import icalendar.timezone.tzid as tzid_module from icalendar import Calendar, Component, Event, Timezone from icalendar.timezone import tzid_from_tzinfo, tzids_from_tzinfo @@ -348,14 +350,13 @@ def test_dateutil_timezone_is_not_found_with_tzname(calendars, no_pytz): assert "dateutil" in repr(cal.events[0].start.tzinfo.__class__) -@pytest.mark.parametrize("tzname", ["America/New_York", "Arctic/Longyearbyen"]) -# @pytest.mark.parametrize("component", ["STANDARD", "DAYLIGHT"]) -def test_dateutil_timezone_is_matched_with_tzname(tzname): +def test_dateutil_timezone_is_matched_with_tzname(): """dateutil is an example of a timezone that has no tzid. In this test we make sure that the timezone is matched by its tzname() in the timezone in the STANDARD and DAYLIGHT components. """ + tzname = "America/New_York" cal = Calendar() event = Event() event.start = datetime(2024, 11, 12, tzinfo=gettz(tzname)) @@ -435,3 +436,10 @@ def test_we_can_identify_dateutil_timezones(tzid): """ tz = gettz(tzid) assert tz is None or tzid in tzids_from_tzinfo(tz) + + +def test_dateutil_timezone_with_bare_id_is_identified(monkeypatch): + """dateutil returns a bare timezone id when it uses its bundled zoneinfo.""" + monkeypatch.setattr(tzid_module, "DATEUTIL_ZONEINFO_PATH", None) + tzfile = SimpleNamespace(_filename="Europe/Berlin") + assert "Europe/Berlin" in tzids_from_tzinfo(tzfile) diff --git a/src/icalendar/timezone/equivalent_timezone_ids.py b/src/icalendar/timezone/equivalent_timezone_ids.py index e86f979e2..e44a1ed19 100644 --- a/src/icalendar/timezone/equivalent_timezone_ids.py +++ b/src/icalendar/timezone/equivalent_timezone_ids.py @@ -54,8 +54,8 @@ def main( if we mix timezone implementations. """ unsorted_tzids = available_timezones() - unsorted_tzids.remove("localtime") - unsorted_tzids.remove("Factory") + unsorted_tzids.discard("localtime") + unsorted_tzids.discard("Factory") class TZ(NamedTuple): tz: tzinfo diff --git a/src/icalendar/timezone/tzid.py b/src/icalendar/timezone/tzid.py index 304291ffd..17622212f 100644 --- a/src/icalendar/timezone/tzid.py +++ b/src/icalendar/timezone/tzid.py @@ -38,13 +38,16 @@ def tzids_from_tzinfo(tzinfo: tzinfo | None) -> tuple[str]: >>> import zoneinfo >>> from icalendar.timezone.tzid import tzids_from_tzinfo - >>> tzids_from_tzinfo(zoneinfo.ZoneInfo("Arctic/Longyearbyen")) - ('Arctic/Longyearbyen', 'Atlantic/Jan_Mayen', 'Europe/Berlin', 'Europe/Budapest', 'Europe/Copenhagen', 'Europe/Oslo', 'Europe/Stockholm', 'Europe/Vienna') + >>> tzids = tzids_from_tzinfo(zoneinfo.ZoneInfo("Arctic/Longyearbyen")) + >>> "Arctic/Longyearbyen" in tzids + True + >>> "Europe/Berlin" in tzids + True >>> from dateutil.tz import gettz - >>> tzids_from_tzinfo(gettz("Europe/Berlin")) - ('Europe/Berlin', 'Arctic/Longyearbyen', 'Atlantic/Jan_Mayen', 'Europe/Budapest', 'Europe/Copenhagen', 'Europe/Oslo', 'Europe/Stockholm', 'Europe/Vienna') + >>> "Europe/Berlin" in tzids_from_tzinfo(gettz("Europe/Berlin")) + True - """ # The example might need to change if you recreate the lookup tree # noqa: E501 + """ # The example might need to change if you recreate the lookup tree if tzinfo is None: return () if isinstance(tzinfo, timezone): @@ -63,14 +66,15 @@ def tzids_from_tzinfo(tzinfo: tzinfo | None) -> tuple[str]: if olson is not None: return _get_equivalent_tzids(olson) return _get_equivalent_tzids(tzinfo._name) # noqa: SLF001 - if hasattr(tzinfo, "_filename"): # dateutil.tz.tzfile # noqa: SIM102 + if hasattr(tzinfo, "_filename"): # dateutil.tz.tzfile + # tzfile('/usr/share/zoneinfo/Europe/Berlin'), or a bare timezone id + # when dateutil uses its bundled zoneinfo. + path = tzinfo._filename # noqa: SLF001 if DATEUTIL_ZONEINFO_PATH is not None: - # tzfile('/usr/share/zoneinfo/Europe/Berlin') - path = tzinfo._filename # noqa: SLF001 - if path.startswith(str(DATEUTIL_ZONEINFO_PATH)): - tzid = str(Path(path).relative_to(DATEUTIL_ZONEINFO_PATH)) - return _get_equivalent_tzids(tzid) - return _get_equivalent_tzids(path) + path_obj = Path(path) + if path_obj.is_relative_to(DATEUTIL_ZONEINFO_PATH): + path = path_obj.relative_to(DATEUTIL_ZONEINFO_PATH).as_posix() + return _get_equivalent_tzids(path) if isinstance(tzinfo, tz.tzutc): return _get_equivalent_tzids("UTC") return () diff --git a/tox.ini b/tox.ini index 4f066b308..7f7bffe6b 100644 --- a/tox.ini +++ b/tox.ini @@ -15,6 +15,19 @@ commands = coverage html coverage xml +[testenv:alpine] +description = test environment with Alpine Linux packages +package = skip +basepython = python3 +runner = virtualenv +system_site_packages = true +deps = +dependency_groups = +allowlist_externals = + coverage +set_env = + PYTHONPATH = src + [testenv:nopytz] # install with dependencies usedevelop = False