Skip to content

Commit

Permalink
Add tests for universal newline handling and fix it.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed May 18, 2017
1 parent 1d21724 commit 489d7ad
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Expand Up @@ -11,3 +11,4 @@ recursive-include src *.png
recursive-include src *.pt
recursive-include src *.zcml
recursive-include src *.rst
recursive-include src *.txt
3 changes: 3 additions & 0 deletions src/zope/app/apidoc/codemodule/.gitattributes
@@ -0,0 +1,3 @@
# make sure git doesn't normalize these files.
_test_crlf.txt text=false
_test_cr.txt text=false
1 change: 1 addition & 0 deletions src/zope/app/apidoc/codemodule/_test_cr.txt
@@ -0,0 +1 @@
This fileuses Mac line endings.
Expand Down
4 changes: 4 additions & 0 deletions src/zope/app/apidoc/codemodule/_test_crlf.txt
@@ -0,0 +1,4 @@
This file
uses
Windows
line endings.
29 changes: 28 additions & 1 deletion src/zope/app/apidoc/codemodule/tests.py
Expand Up @@ -14,17 +14,43 @@
"""Tests for the Code Documentation Module
"""

import os.path
import unittest
import doctest

from zope.component import testing

from zope.app.apidoc.codemodule.text import TextFile

from zope.app.apidoc.tests import standard_checker
from zope.app.apidoc.tests import standard_option_flags
from zope.app.apidoc.tests import LayerDocFileSuite
import zope.app.apidoc.codemodule

here = os.path.dirname(__file__)

class TestText(unittest.TestCase):

def _read_via_text(self, path):
return TextFile(os.path.join(here, path), path, None).getContent()

def _read_bytes(self, path):
with open(os.path.join(here, path), 'rb') as f:
return f.read()

def test_crlf(self):
self.assertEqual(self._read_bytes('_test_crlf.txt'),
b'This file\r\nuses \r\nWindows \r\nline endings.')

self.assertEqual(self._read_via_text('_test_crlf.txt'),
u'This file\nuses \nWindows \nline endings.')

def test_cr(self):
self.assertEqual(self._read_bytes('_test_cr.txt'),
b'This file\ruses \rMac \rline endings.')

self.assertEqual(self._read_via_text('_test_cr.txt'),
u'This file\nuses \nMac \nline endings.')

def test_suite():
checker = standard_checker()
Expand All @@ -39,6 +65,7 @@ def test_suite():
tearDown=testing.tearDown,
checker=checker,
optionflags=standard_option_flags),
unittest.defaultTestLoader.loadTestsFromName(__name__),
))

if __name__ == '__main__':
Expand Down
8 changes: 7 additions & 1 deletion src/zope/app/apidoc/codemodule/text.py
Expand Up @@ -15,6 +15,7 @@
"""
__docformat__ = 'restructuredtext'

from zope.interface import implementer
from zope.location.interfaces import ILocation

Expand All @@ -31,6 +32,11 @@ def __init__(self, path, name, package):
self.__name__ = name

def getContent(self):
with open(self.path, 'rUb') as f:
with open(self.path, 'rb') as f:
content = f.read()

# Make newlines universal
content = content.replace(b'\r\n', b'\n')
content = content.replace(b'\r', b'\n')

return content.decode('utf-8')

0 comments on commit 489d7ad

Please sign in to comment.