Skip to content

Commit

Permalink
tests: simplify timestamp mocking
Browse files Browse the repository at this point in the history
$SOURCE_DATE_EPOCH is now the officially supported mechanism for
guaranteeing stable output timestamps, which means we can simply run the
tests using that, and drop some code.

This also fixes asciidoc-py#131 since the variable would override the mocked time,
resulting in tests failing if $SOURCE_DATE_EPOCH was set in the
environment (e.g. for linux distro packaging).
  • Loading branch information
eli-schwartz committed Jul 28, 2020
1 parent a98a6da commit 2ab1656
Showing 1 changed file with 2 additions and 23 deletions.
25 changes: 2 additions & 23 deletions tests/testasciidoc.py
Expand Up @@ -68,28 +68,6 @@ def normalize_data(lines):
return result


def mock_localtime(f, _localtime=time.localtime):
"""Mock time module to generate stable output."""
_frozentime = 0X3DE170D6
_frozentz = 'UTC+00'

def _frozen_localtime(t=_frozentime + 1):
assert t > _frozentime, 'File created before first public release'
return _localtime(_frozentime)

def generate_expected(self, backend):
time.localtime = _frozen_localtime
os.environ['TZ'] = _frozentz
time.tzset()
try:
return f(self, backend)
finally:
time.localtime = _localtime
del os.environ['TZ']
time.tzset()
return generate_expected


class AsciiDocTest(object):
def __init__(self):
self.number = None # Test number (1..).
Expand Down Expand Up @@ -188,7 +166,6 @@ def get_expected(self, backend):
result = [s.rstrip() for s in result]
return result

@mock_localtime
def generate_expected(self, backend):
"""
Generate and return test data output for backend.
Expand Down Expand Up @@ -386,6 +363,8 @@ def read_until(self, regexp):


if __name__ == '__main__':
# guarantee a stable timestamp matching the test fixtures
os.environ['SOURCE_DATE_EPOCH'] = '1038184662'
# Process command line options.
from argparse import ArgumentParser
parser = ArgumentParser(description='Run AsciiDoc conformance tests specified in '
Expand Down

0 comments on commit 2ab1656

Please sign in to comment.