From aa0a8bf76dc73afb55802f45bedb766b41efa7cc Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 3 Mar 2013 20:40:35 +0400 Subject: [PATCH] Make tests support python2.6 Old unittest is not working for unknown reason, using unittest2 instead --- .travis.yml | 5 +++-- setup.py | 5 ++++- tests/__init__.py | 5 +++++ tests/install.sh | 10 ++++++++++ tests/test.sh | 12 ++++++++++++ tests/test_configuration.py | 9 +++++++-- tests/test_lib.py | 22 ++++++++++++++++++++-- tests/test_segments.py | 17 +++++++++++------ tests/vim.py | 27 ++++++++++++++++++++++++--- 9 files changed, 96 insertions(+), 16 deletions(-) create mode 100755 tests/install.sh create mode 100755 tests/test.sh diff --git a/.travis.yml b/.travis.yml index 3e469f10..774a6f60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,11 @@ language: python python: + - "2.6" - "2.7" - "3.2" - "3.3" -install: "pip install ." -script: python setup.py test +install: tests/install.sh +script: tests/test.sh branches: only: - tests diff --git a/setup.py b/setup.py index b7a09c99..17ea49fc 100755 --- a/setup.py +++ b/setup.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import os +import sys from setuptools import setup, find_packages @@ -11,6 +12,8 @@ try: except IOError: README = '' +old_python = sys.version_info < (2, 7) + setup( name='Powerline', version='beta', @@ -33,5 +36,5 @@ setup( 'Sphinx', ], }, - test_suite='tests', + test_suite='tests' if not old_python else None, ) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29b..b6e78fe6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,5 @@ +import sys +if sys.version_info < (2, 7): + from unittest2 import TestCase, main # NOQA +else: + from unittest import TestCase, main # NOQA diff --git a/tests/install.sh b/tests/install.sh new file mode 100755 index 00000000..ddce7e74 --- /dev/null +++ b/tests/install.sh @@ -0,0 +1,10 @@ +#!/bin/sh +pip install . +if python -c 'import sys; sys.exit(1 * (sys.version_info[0] != 2))' ; then + # Python 2 + pip install mercurial + if python -c 'import sys; sys.exit(1 * (sys.version_info[1] >= 7))' ; then + # Python 2.6 + pip install unittest2 + fi +fi diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 00000000..7a018357 --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/sh +if python -c 'import sys; sys.exit(1 * (sys.version_info >= (2, 7)))' ; then + # Python 2.6 + export PYTHONPATH="${PYTHONPATH}:`realpath .`" + for file in tests/test_*.py ; do + if ! python $file ; then + exit 1 + fi + done +else + python setup.py test +fi diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 6eed7b76..529b48b4 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -3,12 +3,12 @@ '''Dynamic configuration files tests.''' -from unittest import TestCase import tests.vim as vim_module import sys import os import json -from .lib import Args, urllib_read, replace_module_attr +from tests.lib import Args, urllib_read, replace_module_attr +from tests import TestCase VBLOCK = chr(ord('V') - 0x40) @@ -75,3 +75,8 @@ def tearDownModule(): os.chdir(old_cwd) old_cwd = None sys.path.pop(0) + + +if __name__ == '__main__': + from tests import main + main() diff --git a/tests/test_lib.py b/tests/test_lib.py index 0d4e667a..46dd03b3 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -1,9 +1,9 @@ from powerline.lib import mergedicts, underscore_to_camelcase, add_divider_highlight_group, humanize_bytes from powerline.lib.vcs import guess -from unittest import TestCase from subprocess import call, PIPE import os import sys +from tests import TestCase class TestLib(TestCase): @@ -79,8 +79,13 @@ class TestVCS(TestCase): os.remove(os.path.join('hg_repo', 'file')) +old_HGRCPATH = None +old_cwd = None + + def setUpModule(): global old_cwd + global old_HGRCPATH old_cwd = os.getcwd() os.chdir(os.path.dirname(__file__)) call(['git', 'init', '--quiet', 'git_repo']) @@ -88,6 +93,8 @@ def setUpModule(): call(['git', 'config', '--local', 'user.email', 'bar@example.org'], cwd='git_repo') call(['git', 'commit', '--allow-empty', '--message', 'Initial commit', '--quiet'], cwd='git_repo') if use_mercurial: + old_HGRCPATH = os.environ.get('HGRCPATH') + os.environ['HGRCPATH'] = '' call(['hg', 'init', 'hg_repo']) with open(os.path.join('hg_repo', '.hg', 'hgrc'), 'w') as hgrc: hgrc.write('[ui]\n') @@ -96,6 +103,7 @@ def setUpModule(): def tearDownModule(): global old_cwd + global old_HGRCPATH for repo_dir in ['git_repo'] + (['hg_repo'] if use_mercurial else []): for root, dirs, files in list(os.walk(repo_dir, topdown=False)): for file in files: @@ -103,4 +111,14 @@ def tearDownModule(): for dir in dirs: os.rmdir(os.path.join(root, dir)) os.rmdir(repo_dir) - os.chdir(old_cwd) # NOQA + if use_mercurial: + if old_HGRCPATH is None: + os.environ.pop('HGRCPATH') + else: + os.environ['HGRCPATH'] = old_HGRCPATH + os.chdir(old_cwd) + + +if __name__ == '__main__': + from tests import main + main() diff --git a/tests/test_segments.py b/tests/test_segments.py index e42724ef..d3edf072 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- -from unittest import TestCase from powerline.segments import shell, common import tests.vim as vim_module import sys import os -from .lib import Args, urllib_read, replace_module, replace_module_attr, new_module, replace_module_module, replace_env +from tests.lib import Args, urllib_read, replace_module, replace_module_attr, new_module, replace_module_module, replace_env +from tests import TestCase vim = None @@ -96,11 +96,9 @@ class TestCommon(TestCase): self.assertEqual(common.cwd(dir_limit_depth=2, dir_shorten_len=2), [{'contents': '[not found]', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}]) new_os.getcwdu = lambda: raises(OSError()) - with self.assertRaises(OSError): - common.cwd(dir_limit_depth=2, dir_shorten_len=2), + self.assertRaises(OSError, common.cwd, tuple(), {'dir_limit_depth': 2, 'dir_shorten_len': 2}) new_os.getcwdu = lambda: raises(ValueError()) - with self.assertRaises(ValueError): - common.cwd(dir_limit_depth=2, dir_shorten_len=2), + self.assertRaises(ValueError, common.cwd, tuple(), {'dir_limit_depth': 2, 'dir_shorten_len': 2}) def test_date(self): with replace_module_attr(common, 'datetime', Args(now=lambda: Args(strftime=lambda fmt: fmt))): @@ -280,8 +278,10 @@ old_cwd = None def setUpModule(): global old_cwd + global __file__ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path'))) old_cwd = os.getcwd() + __file__ = os.path.abspath(__file__) os.chdir(os.path.dirname(__file__)) from powerline.segments import vim globals()['vim'] = vim @@ -291,3 +291,8 @@ def tearDownModule(): global old_cwd os.chdir(old_cwd) sys.path.pop(0) + + +if __name__ == '__main__': + from tests import main + main() diff --git a/tests/vim.py b/tests/vim.py index de3c9fe4..c49a0da5 100644 --- a/tests/vim.py +++ b/tests/vim.py @@ -22,13 +22,19 @@ def _logged(func): from functools import wraps @wraps(func) - def f(*args): + def f(*args, **kwargs): _log.append((func.__name__, args)) - return func(*args) + return func(*args, **kwargs) return f +def _log_print(): + import sys + for entry in _log: + sys.stdout.write(repr(entry) + '\n') + + @_logged def command(cmd): if cmd.startswith('let g:'): @@ -237,13 +243,13 @@ class _Buffer(object): _dict = None +@_logged def _init(): global _dict if _dict: return _dict - import imp _dict = {} for varname, value in globals().items(): if varname[0] != '_': @@ -252,6 +258,7 @@ def _init(): return _dict +@_logged def _get_segment_info(): mode_translations = { chr(ord('V') - 0x40): '^V', @@ -268,10 +275,12 @@ def _get_segment_info(): } +@_logged def _launch_event(event): pass +@_logged def _start_mode(mode): global _mode if mode == 'i': @@ -281,6 +290,7 @@ def _start_mode(mode): _mode = mode +@_logged def _undo(): if len(_undostate[_buffer()]) == 1: return @@ -290,6 +300,7 @@ def _undo(): _buf_options[_buffer()]['modified'] = 0 +@_logged def _edit(name=None): global _last_bufnr if _buffer() and buffers[_buffer()].name is None: @@ -300,12 +311,14 @@ def _edit(name=None): windows[_window - 1].buffer = buf +@_logged def _new(name=None): global _window _Window(buffer={'name': name}) _window = len(windows) +@_logged def _del_window(winnr): win = windows.pop(winnr - 1) _win_scopes.pop(winnr) @@ -314,6 +327,7 @@ def _del_window(winnr): return win +@_logged def _close(winnr, wipe=True): global _window win = _del_window(winnr) @@ -329,6 +343,7 @@ def _close(winnr, wipe=True): _Window() +@_logged def _bw(bufnr=None): bufnr = bufnr or _buffer() winnr = 1 @@ -342,10 +357,12 @@ def _bw(bufnr=None): _b(max(buffers.keys())) +@_logged def _b(bufnr): windows[_window - 1].buffer = buffers[bufnr] +@_logged def _set_cursor(line, col): windows[_window - 1].cursor = (line, col) if _mode == 'n': @@ -354,10 +371,12 @@ def _set_cursor(line, col): _launch_event('CursorMovedI') +@_logged def _get_buffer(): return buffers[_buffer()] +@_logged def _set_bufoption(option, value, bufnr=None): _buf_options[bufnr or _buffer()][option] = value if option == 'filetype': @@ -377,6 +396,7 @@ class _WithNewBuffer(object): _bw(self.bufnr) +@_logged def _set_dict(d, new, setfunc=None): if not setfunc: def setfunc(k, v): @@ -432,6 +452,7 @@ class _WithDict(object): self.d.pop(k) +@_logged def _with(key, *args, **kwargs): if key == 'buffer': return _WithNewBuffer(_edit, *args, **kwargs)