Commits

Virgil Dupras  committed 7d693af

Added patch_time_ticking() and improved packaging.

  • Participants
  • Parent commits 942122c
  • Tags 1.1.0

Comments (0)

Files changed (6)

+syntax: glob
+
+__pycache__
+build
+dist
+*.egg-info
+Changes
+=======
+
+Version 1.1.0 -- 2012-09-18
+---------------------------
+
+* Added patch_time_ticking() function.
+
+Version 1.0.0 -- 2011-02-16
+---------------------------
+
+* Initial Release
+include CHANGES
 pytest-monkeyplus - monkeypatch with extras
 ===========================================
 
-The ``monkkeyplus`` plugin is a funcarg that subclasses ``monkeypatch`` and adds 
+The ``monkeyplus`` plugin is a funcarg that subclasses ``monkeypatch`` and adds 
 a few extra features to it.
 
 Extra features
 --------------
 
-``monkeyplus`` has two extra methods: ``patch_osstat`` and ``patch_today``.
+``monkeyplus`` has three extra methods: ``patch_osstat``, ``patch_today`` and
+``patch_time_ticking``.
 
 ``patch_osstat(path, **osstat_attrs)``
 
 
     monkeyplus.patch_today(2011, 2, 16)
 
+``patch_time_ticking(force_int_diff=False)``
+
+This monkeypatch ensures that all ``time.time()`` calls yield different results (and increasing). On
+many platforms, this is already guaranteed, but not on all of them.
+
+If ``force_int_diff`` is True, the patch ensures that all ``int(time.time())`` calls yield different
+results. Note that it also means that the patches function will return ``int`` values.
+
 Replacing monkeypatch
 ---------------------
 

File pytest_monkeyplus.py

         delta = today - new_today
         self.setattr(time, 'time', lambda: time_now - (delta.days * 24 * 60 * 60))
     
+    def patch_time_ticking(self, force_int_diff=False):
+        """Patches time.time() and ensures that it never returns the same value each time it's
+        called.
+        
+        If force_int_diff is True, the minimum difference between time() result is 1.
+        """
+        if hasattr(self, '_time_before_ticking_patch'):
+            # Already patched, do nothing.
+            return
+        self._last_time_tick = time.time()
+        if force_int_diff:
+            self._last_time_tick = int(self._last_time_tick)
+        self._time_before_ticking_patch = time.time
+        
+        def fake_time():
+            result = self._time_before_ticking_patch()
+            if force_int_diff:
+                result = int(result)
+            if result <= self._last_time_tick:
+                result = self._last_time_tick + 1
+            self._last_time_tick = result
+            return result
+        
+        self.setattr(time, 'time', fake_time)
     'Programming Language :: Python :: 3',
 ]
 
+LONG_DESC = open('README', 'rt').read() + '\n\n' + open('CHANGES', 'rt').read()
+
 setup(
     name='pytest-monkeyplus',
     description="pytest's monkeypatch subclass with extra functionalities",
-    long_description=open('README').read(),
+    long_description=LONG_DESC,
     classifiers=CLASSIFIERS,
-    version='1.0.0',
+    version='1.1.0',
     author='Virgil Dupras',
     author_email='hsoft@hardcoded.net',
     url='http://bitbucket.org/hsoft/pytest-monkeyplus/',