You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by jo...@apache.org on 2013/08/16 04:07:42 UTC
[17/18] git commit: [#6464] Refactored plain2markdown tests
[#6464] Refactored plain2markdown tests
Signed-off-by: Cory Johns <cj...@slashdotmedia.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/592fcedc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/592fcedc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/592fcedc
Branch: refs/heads/cj/6464
Commit: 592fcedc480f5428580112d337d135fff9964248
Parents: fd2b277
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Wed Aug 14 18:47:04 2013 +0000
Committer: Cory Johns <cj...@slashdotmedia.com>
Committed: Fri Aug 16 00:58:29 2013 +0000
----------------------------------------------------------------------
Allura/allura/tests/decorators.py | 11 +++
Allura/allura/tests/test_helpers.py | 85 ++++++++++++++++++++
ForgeBlog/forgeblog/tests/test_commands.py | 42 ----------
.../forgeimporters/google/__init__.py | 2 +-
ForgeImporters/forgeimporters/google/tracker.py | 2 +-
.../tests/google/functional/test_tracker.py | 18 +----
6 files changed, 101 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/592fcedc/Allura/allura/tests/decorators.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/decorators.py b/Allura/allura/tests/decorators.py
index 745b008..8c9e4c9 100644
--- a/Allura/allura/tests/decorators.py
+++ b/Allura/allura/tests/decorators.py
@@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
+import sys
from functools import wraps
import contextlib
@@ -104,3 +105,13 @@ class raises(object):
return False
else:
raise AssertionError('Did not raise %s' % self.ExcType)
+
+
+def without_module(*module_names):
+ def _without_module(func):
+ @wraps(func)
+ def wrapped(*a, **kw):
+ with patch.dict(sys.modules, {m: None for m in module_names}):
+ return func(*a, **kw)
+ return wrapped
+ return _without_module
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/592fcedc/Allura/allura/tests/test_helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 4ec91df..03a530c 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -23,6 +23,8 @@ from mock import Mock, patch
from pylons import tmpl_context as c
from nose.tools import eq_, assert_equals
+from IPython.testing.decorators import skipif, module_not_available
+from datadiff import tools as dd
from allura import model as M
from allura.lib import helpers as h
@@ -263,6 +265,89 @@ def test_notifications_disabled():
assert_equals(project.notifications_disabled, False)
+@skipif(module_not_available('html2text'))
+def test_plain2markdown_with_html2text():
+ """Test plain2markdown using html2text to escape markdown, if available."""
+ text = '''paragraph
+
+ 4 spaces before this
+
+ *blah*
+
+here's a <tag> that should be <b>preserved</b>
+Literal > Ò ¼ & & ሿ
+M & Ms - doesn't get escaped
+http://blah.com/?x=y&a=b - not escaped either
+'''
+
+ expected = '''paragraph
+
+4 spaces before this
+
+\*blah\*
+
+here's a <tag> that should be <b>preserved</b>
+Literal &gt; &Ograve; &frac14; &amp; &\#38; &\#x123F;
+M & Ms - doesn't get escaped
+http://blah.com/?x=y&a=b - not escaped either
+'''
+
+ assert_equals(h.plain2markdown(text), expected)
+
+ assert_equals(h.plain2markdown('a foo bar\n\n code here?', preserve_multiple_spaces=True),
+ 'a foo bar\n\n code here?')
+
+ assert_equals(h.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=True),
+ ' tab before \(stuff\)')
+
+ assert_equals(h.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=False),
+ 'tab before \(stuff\)')
+
+@td.without_module('html2text')
+def test_plain2markdown():
+ """Test plain2markdown using fallback regexp to escape markdown.
+
+ Potentially MD-special characters are aggresively escaped, as without
+ knowledge of the MD parsing rules it's better to be excessive but safe.
+ """
+ text = '''paragraph
+
+ 4 spaces before this
+
+ *blah*
+
+here's a <tag> that should be <b>preserved</b>
+Literal > Ò ¼ & & ሿ
+M & Ms - amp doesn't get escaped
+http://blah.com/?x=y&a=b - not escaped either
+back\\-slash escaped
+'''
+
+ expected = '''paragraph
+
+4 spaces before this
+
+\*blah\*
+
+here's a <tag> that should be <b>preserved</b>
+Literal &gt; &Ograve; &frac14; &amp; &\#38; &\#x123F;
+M & Ms \- amp doesn't get escaped
+http://blah\.com/?x=y&a=b \- not escaped either
+back\\\\\-slash escaped
+'''
+
+ dd.assert_equal(h.plain2markdown(text), expected)
+
+ dd.assert_equal(h.plain2markdown('a foo bar\n\n code here?', preserve_multiple_spaces=True),
+ 'a foo bar\n\n code here?')
+
+ dd.assert_equal(h.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=True),
+ ' tab before \(stuff\)')
+
+ dd.assert_equal(h.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=False),
+ 'tab before \(stuff\)')
+
+
class TestUrlOpen(TestCase):
@patch('allura.lib.helpers.urllib2')
def test_no_error(self, urllib2):
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/592fcedc/ForgeBlog/forgeblog/tests/test_commands.py
----------------------------------------------------------------------
diff --git a/ForgeBlog/forgeblog/tests/test_commands.py b/ForgeBlog/forgeblog/tests/test_commands.py
index 472efda..0b2804a 100644
--- a/ForgeBlog/forgeblog/tests/test_commands.py
+++ b/ForgeBlog/forgeblog/tests/test_commands.py
@@ -168,45 +168,3 @@ def test_plaintext_preprocessor_wrapped():
'<p>#foo bar <a class="" href="../baz">baz</a> foo bar </p>\n'
'<p>#foo bar <a class="" href="../baz"> baz </a></p></div>'
)
-
-@skipif(module_not_available('html2text'))
-def test_plain2markdown():
- text = '''paragraph
-
- 4 spaces before this
-
- *blah*
-
-here's a <tag> that should be <b>preserved</b>
-Literal > Ò ¼ & & ሿ
-M & Ms - doesn't get escaped
-http://blah.com/?x=y&a=b - not escaped either
-'''
-
- expected = '''paragraph
-
-4 spaces before this
-
-\*blah\*
-
-here's a <tag> that should be <b>preserved</b>
-Literal &gt; &Ograve; &frac14; &amp; &\#38; &\#x123F;
-M & Ms - doesn't get escaped
-http://blah.com/?x=y&a=b - not escaped either
-'''
- # note: the \# isn't necessary it could be just # but that's the way
- # html2text escapes all #s currently. The extra escaping of \# ends up
- # being ok though when rendered
-
- from forgeblog.command import rssfeeds
-
- assert_equal(rssfeeds.plain2markdown(text), expected)
-
- assert_equal(rssfeeds.plain2markdown('a foo bar\n\n code here?', preserve_multiple_spaces=True),
- 'a foo bar\n\n code here?')
-
- assert_equal(rssfeeds.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=True),
- ' tab before \(stuff\)')
-
- assert_equal(rssfeeds.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=False),
- 'tab before \(stuff\)')
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/592fcedc/ForgeImporters/forgeimporters/google/__init__.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/__init__.py b/ForgeImporters/forgeimporters/google/__init__.py
index 90319cd..f60bc58 100644
--- a/ForgeImporters/forgeimporters/google/__init__.py
+++ b/ForgeImporters/forgeimporters/google/__init__.py
@@ -269,7 +269,7 @@ class Comment(object):
u'{updates}'
).format(
author=self.author,
- body=h.plain2markdown(self.body, True),
+ body=h.plain2markdown(self.body, preserve_multiple_spaces=True),
updates='\n'.join(
'**%s** %s' % (k,v)
for k,v in self.updates.items()
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/592fcedc/ForgeImporters/forgeimporters/google/tracker.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/tracker.py b/ForgeImporters/forgeimporters/google/tracker.py
index fefd695..3257c5a 100644
--- a/ForgeImporters/forgeimporters/google/tracker.py
+++ b/ForgeImporters/forgeimporters/google/tracker.py
@@ -96,7 +96,7 @@ class GoogleCodeTrackerImporter(ToolImporter):
u'{body}').format(
creator=issue.get_issue_creator(),
owner=owner_line,
- body=h.plain2markdown(issue.get_issue_description(), True),
+ body=h.plain2markdown(issue.get_issue_description(), preserve_multiple_spaces=True),
)
ticket.add_multiple_attachments(issue.get_issue_attachments())
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/592fcedc/ForgeImporters/forgeimporters/tests/google/functional/test_tracker.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/tests/google/functional/test_tracker.py b/ForgeImporters/forgeimporters/tests/google/functional/test_tracker.py
index 206ac7c..7d67255 100644
--- a/ForgeImporters/forgeimporters/tests/google/functional/test_tracker.py
+++ b/ForgeImporters/forgeimporters/tests/google/functional/test_tracker.py
@@ -27,25 +27,13 @@ from pylons import tmpl_context as c
from IPython.testing.decorators import module_not_available, skipif
from alluratest.controller import setup_basic_test
+from allura.tests.decorators import without_module
from allura import model as M
from forgetracker import model as TM
from .... import google
from ....google import tracker
-def without_html2text(func):
- @wraps(func)
- def wrapped(*args, **kw):
- try:
- import html2text
- except ImportError:
- return func(*args, **kw)
- else:
- with mock.patch.object(html2text, 'escape_md_section') as ems:
- ems.side_effect = ImportError
- return func(*args, **kw)
- return wrapped
-
class TestGCTrackerImporter(TestCase):
def _make_extractor(self, html):
with mock.patch.object(google, 'urllib2') as urllib2:
@@ -85,7 +73,7 @@ class TestGCTrackerImporter(TestCase):
self.assertEqual(ticket.milestone, '')
self.assertEqual(ticket.custom_fields, {})
- @without_html2text
+ @without_module('html2text')
def test_issue_basic_fields(self):
anon = M.User.anonymous()
ticket = self._make_ticket(self.test_issue)
@@ -162,7 +150,7 @@ class TestGCTrackerImporter(TestCase):
('at2.txt', 'text/plain', 'http://allura-google-importer.googlecode.com/issues/attachment?aid=70000001&name=at2.txt&token=C9Hn4s1-g38hlSggRGo65VZM1ys%3A1376059941255'),
)
- @without_html2text
+ @without_module('html2text')
def test_comments(self):
anon = M.User.anonymous()
ticket = self._make_ticket(self.test_issue)