Skip to content

Commit

Permalink
Cleanups.
Browse files Browse the repository at this point in the history
- Remove broken __name__ == __main__ from tests
- Indentation in .travis.yml and ftesting.zcml
- Fix encodings for test_copy_then_delete_with_unicode_name to work on
  both Py2 and Py3.
- Use unittest.defaultTestLoader.loadTestsFromName more places.
- Remove one remaining redundant check for a string when we expect a list.
  • Loading branch information
jamadden committed Apr 23, 2017
1 parent 3d8a6ed commit 186fba6
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ install:
- pip install -e git+https://github.com/zopefoundation/zope.proxy.git#egg=zope.proxy
- pip install -U -e .[test]
script:
- coverage run -m zope.testrunner --test-path=src --auto-color --auto-progress
- coverage run -m zope.testrunner --test-path=src --auto-color --auto-progress
notifications:
email: false
after_success:
Expand Down
3 changes: 1 addition & 2 deletions src/zope/app/container/browser/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ def cutObjects(self):
if not ids:
self.error = _("You didn't specify any ids to cut.")
return
if isinstance(ids, six.string_types):
ids = [ids]

container_path = getPath(self.context)

# For each item, check that it can be moved; if so, save the
Expand Down
8 changes: 4 additions & 4 deletions src/zope/app/container/browser/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ def setUp(self):
BTrees._base._TreeItems.__Security_checker__ = checker
self.addCleanup(lambda: delattr(BTrees._base._TreeItems, '__Security_checker__'))

def publish(self, path, basic=None, form=None):
def publish(self, path, basic=None, form=None, headers=None):
if basic:
self._testapp.authorization = ('Basic', tuple(basic.split(':')))
else:
self._testapp.authorization = None
env = {'wsgi.handleErrors': False}
if form:
response = self._testapp.post(path, params=form, extra_environ=env,
content_type="application/x-www-form-urlencoded; charset=utf-8")
response = self._testapp.post(path, params=form,
extra_environ=env, headers=headers)
else:
response = self._testapp.get(path, extra_environ=env)
response = self._testapp.get(path, extra_environ=env, headers=headers)
return response
5 changes: 1 addition & 4 deletions src/zope/app/container/browser/tests/test_adding.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,8 @@ def test_isSingleMenuItem_with_ICNC():
])
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test),
unittest.defaultTestLoader.loadTestsFromName(__name__),
doctest.DocTestSuite(setUp=setUp,
tearDown=tearDown,
checker=checker),
))

if __name__=='__main__': # pragma: no cover
unittest.main(defaultTest='test_suite')
14 changes: 4 additions & 10 deletions src/zope/app/container/browser/tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
##############################################################################
"""Test Container Contents
"""
from unittest import TestCase, TestSuite, main, makeSuite
import unittest

from zope.interface import Interface, implementer
from zope.security import checker
Expand Down Expand Up @@ -229,7 +229,7 @@ def __repr__(self): # pragma: no cover
return "<%s.PrincipalAnnotations object>" % __name__


class TestCutCopyPaste(PlacefulSetup, TestCase):
class TestCutCopyPaste(PlacefulSetup, unittest.TestCase):

def setUp(self):
PlacefulSetup.setUp(self)
Expand Down Expand Up @@ -356,7 +356,7 @@ def _TestView__newView(self, container):
request.setPrincipal(Principal())
return Contents(container, request)

class Test(BaseTestContentsBrowserView, TestCase):
class Test(BaseTestContentsBrowserView, unittest.TestCase):

def _TestView__newContext(self):
from zope.app.container.sample import SampleContainer
Expand All @@ -373,10 +373,4 @@ def _TestView__newView(self, container):
return Contents(container, request)

def test_suite():
return TestSuite((
makeSuite(Test),
makeSuite(TestCutCopyPaste),
))

if __name__=='__main__': # pragma: no cover
main(defaultTest='test_suite')
return unittest.defaultTestLoader.loadTestsFromName(__name__)
41 changes: 27 additions & 14 deletions src/zope/app/container/browser/tests/test_contents_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ def test_inplace_add(self):
form={'type_name': u'BrowserAdd__zope.site.folder.Folder'})
body = response.unicode_normal_body
self.assertIn('type="hidden" name="type_name"', body)
#self.assertIn('input name="new_value"', body)
self.assertIn('type="submit" name="container_cancel_button"', body)
#self.assertIn('type="submit" name="container_rename_button"', body)

response = self.publish(
'/@@contents.html',
Expand Down Expand Up @@ -320,24 +318,42 @@ def test_unmoveable_object(self):
body = response.text
self.assertIn("cannot be moved", body)

@unittest.skipIf(str is not bytes, #Py2 only
"Only Python 2 can do str(b'encoded') and get the right thing")
def test_copy_then_delete_with_unicode_name(self):
# Tests unicode on object copied then deleted (#238579)
# The zope.publisher.browser conversion methods for text/str
# rely on the default encoding, which breaks on this unicode name in Python 3.
# We either wind up with "b'voil\\xe0'" or a list of the ints that make
# up the bytes, depending on which order we try for the type names.

# The 'Accept-Charset' is important on Python 3 to get
# the encoding correct. The query string is actually correctly
# decoded to unicode (if it's ever even *encoded*) on the
# receiving side, but
# zope.publisher.browser.BrowserRequest._decode assumes it was
# smuggled as latin-1, and thus encodes it back to those
# bytes. If we don't specify latin-1 as a charset
# zope.publsher.http.HTTPCharsets will only let it try to be
# decoded as utf-8, which doesn't work.
# On Python 2, if we don't encode the parameter, it's improperly
# decoded before it ever gets to _decode, arriving as 'voill?'.

# This is most likely a mismatch
# somewhere in the webtest.TestApp/zope.app.wsgi.testlayer/zope.publisher
# stack.

# create a file with an accentuated unicode name
root = self.getRootFolder()
root[u'voil\xe0'] = File()
transaction.commit()

# copy the object
response = self.publish('/@@contents.html', basic='mgr:mgrpw', form={
'ids:list' : (u'voil\xe0'.encode('utf-8'),),
'container_copy_button' : '' })
response = self.publish(
'/@@contents.html',
basic='mgr:mgrpw',
form={
'ids:list' : u'voil\xe0'.encode('utf-8'),
'container_copy_button' : ''
},
headers={
'Accept-Charset': 'latin-1, utf-8',
},
)
self.assertEqual(response.status_int, 302)
self.assertEqual(response.headers.get('Location'),
'http://localhost/@@contents.html')
Expand Down Expand Up @@ -367,6 +383,3 @@ def _http(query_str, *args, **kwargs):
index.layer = AppContainerLayer
suite.addTest(index)
return suite

if __name__=='__main__': # pragma: no cover
unittest.main(defaultTest='test_suite')
3 changes: 0 additions & 3 deletions src/zope/app/container/browser/tests/test_view_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,3 @@ def test_suite():
Tests.layer = AppContainerLayer
suite.addTest(unittest.makeSuite(Tests))
return suite

if __name__=='__main__': # pragma: no cover
unittest.main(defaultTest='test_suite')
12 changes: 7 additions & 5 deletions src/zope/app/container/ftesting.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<include package="zope.app.basicskin" />

<include package="zope.app.publication" />
<include package="zope.app.container.browser.tests" />
<include package="zope.app.pagetemplate" />
<include package="zope.password" />
<include package="zope.browserresource" />
Expand Down Expand Up @@ -82,18 +81,21 @@
title="Views"
description="Menu for displaying alternate representations of an object"
/>

<include package="zope.app.container.browser.tests" />

<securityPolicy
component="zope.securitypolicy.zopepolicy.ZopeSecurityPolicy" />

<role id="zope.Anonymous" title="Everybody"
description="All users have this role implicitly" />
description="All users have this role implicitly" />
<role id="zope.Manager" title="Site Manager" />

<!-- Replace the following directive if you don't want public access -->
<grant permission="zope.View"
role="zope.Anonymous" />
role="zope.Anonymous" />
<grant permission="zope.dublincore.view"
role="zope.Anonymous" />
role="zope.Anonymous" />

<grantAll role="zope.Manager" />

Expand All @@ -112,7 +114,7 @@
<grant role="zope.Manager" principal="zope.mgr" />

<class class="zope.site.folder.Folder">
<allow attributes="items __getitem__ get __contains__ __setitem__" />
<allow attributes="items __getitem__ get __contains__ __setitem__" />
</class>

</configure>
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ envlist =

[testenv]
commands =
zope-testrunner --test-path=src
zope-testrunner --test-path=src []
deps =
.[test]

0 comments on commit 186fba6

Please sign in to comment.