Skip to content

Commit

Permalink
Fix test that fails on PyPy: https://travis-ci.org/github/lemon24/rea…
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Jun 20, 2020
1 parent 2dfbf53 commit 3076a57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/reader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ def _parse_feed_for_update(
try:
return feed, self._parser(feed.url, feed.http_etag, feed.http_last_modified)
except Exception as e:
log.debug(
"_parse_feed_for_update exception, traceback follows", exc_info=True
)
return feed, e

def _update_feed(
Expand Down
23 changes: 18 additions & 5 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,28 @@ def test_integration(reader, feed_type, data_dir, monkeypatch):
feed_filename = 'full.{}'.format(feed_type)
feed_url = str(data_dir.join(feed_filename))

import unittest.mock
# On CPython, we can't mock datetime.datetime.utcnow because
# datetime.datetime is a built-in/extension type; we can mock the class.
# On PyPy, we can mock the class, but it results in weird type errors
# when the mock/subclass and original datetime class interact.

datetime_mock = unittest.mock.MagicMock(wraps=datetime)
monkeypatch.setattr('datetime.datetime', datetime_mock)
try:
# if we can set attributes on the class, we just patch utcnow() directly
# (we don't use monkeypatch because it breaks cleanup if it doesn't work)
datetime.utcnow = datetime.utcnow
datetime_mock = datetime
except TypeError:
# otherwise, we monkeypatch the datetime class on the module
class datetime_mock(datetime):
pass

monkeypatch.setattr('datetime.datetime', datetime_mock)

datetime_mock.utcnow = lambda: datetime(2010, 1, 1)
monkeypatch.setattr(datetime_mock, 'utcnow', lambda: datetime(2010, 1, 1))
reader.add_feed(feed_url)
datetime_mock.utcnow = lambda: datetime(2010, 1, 2)
monkeypatch.setattr(datetime_mock, 'utcnow', lambda: datetime(2010, 1, 2))
reader.update_feeds()
monkeypatch.undo()

(feed,) = reader.get_feeds()
entries = set(reader.get_entries())
Expand Down

0 comments on commit 3076a57

Please sign in to comment.