Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions news/812.bugfix
Original file line number Diff line number Diff line change
@@ -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`
26 changes: 26 additions & 0 deletions scripts/docker/compose.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 11 additions & 3 deletions src/icalendar/tests/test_issue_722_generate_vtimezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/icalendar/timezone/equivalent_timezone_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions src/icalendar/timezone/tzid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Comment thread
SemTiOne marked this conversation as resolved.
Resolved
if isinstance(tzinfo, tz.tzutc):
return _get_equivalent_tzids("UTC")
return ()
Expand Down
13 changes: 13 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down