Make tests support python2.6

Old unittest is not working for unknown reason, using unittest2 instead
This commit is contained in:
ZyX 2013-03-03 20:40:35 +04:00 committed by Kim Silkebækken
parent 0a05b2961a
commit aa0a8bf76d
9 changed files with 96 additions and 16 deletions

View File

@ -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

View File

@ -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,
)

View File

@ -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

10
tests/install.sh Executable file
View File

@ -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

12
tests/test.sh Executable file
View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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)