diff options
author | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2008-06-30 10:59:50 +0200 |
---|---|---|
committer | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2008-06-30 10:59:50 +0200 |
commit | dd1bace6ad3b88d5496e3184684c5ac6eaaac497 (patch) | |
tree | 4b665d2b98763bfbf49f3c66cc66b66febb2f14f | |
parent | 08b003a786f56eb03be2560dd56fdbacb4f67a75 (diff) | |
parent | 880314b0830b7d411041ac6f8ab711c0a05c6e38 (diff) | |
download | logilab-common-dd1bace6ad3b88d5496e3184684c5ac6eaaac497.tar.gz |
merge
-rw-r--r-- | adbh.py | 2 | ||||
-rw-r--r-- | pytest.py | 17 | ||||
-rw-r--r-- | shellutils.py | 12 | ||||
-rw-r--r-- | test/unittest_date.py | 3 | ||||
-rw-r--r-- | test/unittest_modutils.py | 7 | ||||
-rw-r--r-- | test/unittest_pytest.py | 33 | ||||
-rw-r--r-- | test/unittest_shellutils.py | 74 |
7 files changed, 130 insertions, 18 deletions
@@ -447,7 +447,7 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): cmd = 'echo "%s;" | mysql -h %s -u %s -p' % ( self.sql_create_database(dbname, encoding), dbhost, dbuser) cmds.append(cmd) - cmd = 'mysql -h %s -u %s -p < %s' % (dbname, dbhost, dbuser, backupfile) + cmd = 'mysql -h %s -u %s -p %s < %s' % (dbname, dbhost, dbuser, backupfile) cmds.append(cmd) return cmds @@ -65,10 +65,11 @@ pytest --coverage test_foo.py (only if logilab.devtools is available) """ -import os, sys +import os, sys, re import os.path as osp from time import time, clock + from logilab.common.fileutils import abspath_listdir from logilab.common import testlib import doctest @@ -148,18 +149,15 @@ else: +TESTFILE_RE = re.compile("^((unit)?test.*|smoketest)\.py$") def this_is_a_testfile(filename): """returns True if `filename` seems to be a test file""" - filename = osp.basename(filename) - return ((filename.startswith('unittest') - or filename.startswith('test') - or filename.startswith('smoketest')) - and filename.endswith('.py')) + return TESTFILE_RE.match(osp.basename(filename)) - +TESTDIR_RE = re.compile("^(unit)?tests?$") def this_is_a_testdir(dirpath): """returns True if `filename` seems to be a test directory""" - return osp.basename(dirpath) in ('test', 'tests', 'unittests') + return TESTDIR_RE.match(osp.basename(dirpath)) def load_pytest_conf(path, parser): @@ -306,12 +304,13 @@ class PyTester(object): if skipped in dirs: dirs.remove(skipped) basename = osp.basename(dirname) - if basename in ('test', 'tests'): + if this_is_a_testdir(basename): print "going into", dirname # we found a testdir, let's explore it ! self.testonedir(dirname, exitfirst) dirs[:] = [] if self.report.ran == 0: + print "no test dir found testing here:", here # if no test was found during the visit, consider # the local directory as a test directory even if # it doesn't have a traditional test directory name diff --git a/shellutils.py b/shellutils.py index 6f8d929..cad5b33 100644 --- a/shellutils.py +++ b/shellutils.py @@ -191,8 +191,8 @@ def release_lock(lock_file): class ProgressBar(object): """a simple text progression bar""" - def __init__(self, nbops, size=20., stream=sys.stdout): - self._fstr = '\r[%%-%ss]' % size + def __init__(self, nbops, size=20, stream=sys.stdout): + self._fstr = '\r[%%-%ss]' % int(size) self._stream = stream self._total = nbops self._size = size @@ -205,5 +205,9 @@ class ProgressBar(object): progress = int((float(self._current)/float(self._total))*self._size) if progress > self._progress: self._progress = progress - self._stream.write(self._fstr % ('.' * progress) ) - self._stream.flush() + self.refresh() + + def refresh(self): + """refresh the progression bar display""" + self._stream.write(self._fstr % ('.' * min(self._progress, self._size)) ) + self._stream.flush() diff --git a/test/unittest_date.py b/test/unittest_date.py index fc4fa34..6ba98a9 100644 --- a/test/unittest_date.py +++ b/test/unittest_date.py @@ -11,7 +11,8 @@ try: from logilab.common.date import endOfMonth, add_days_worked, nb_open_days, \ get_national_holidays except ImportError: - from datetime import date as Date + from datetime import date as Date, datetime as DateTime, timedelta as RelativeDateTime + now = DateTime.now get_national_holidays = endOfMonth = add_days_worked = nb_open_days = None class DateTC(TestCase): diff --git a/test/unittest_modutils.py b/test/unittest_modutils.py index b21383f..09ec232 100644 --- a/test/unittest_modutils.py +++ b/test/unittest_modutils.py @@ -35,10 +35,13 @@ DATADIR = path.join(path.dirname(__file__), 'data') class TestCase(TLTestCase): def setUp(self): super(TestCase,self).setUp() - sys.path.remove(common.__path__[0]) + self.__common_in_path = common.__path__[0] in sys.path + if self.__common_in_path: + sys.path.remove(common.__path__[0]) def tearDown(self): - sys.path.insert(0, common.__path__[0]) + if self.__common_in_path: + sys.path.insert(0, common.__path__[0]) super(TestCase,self).tearDown() diff --git a/test/unittest_pytest.py b/test/unittest_pytest.py new file mode 100644 index 0000000..abd8d3e --- /dev/null +++ b/test/unittest_pytest.py @@ -0,0 +1,33 @@ +from os.path import join +from logilab.common.testlib import TestCase, unittest_main +from logilab.common.pytest import * + +class ModuleFunctionTC(TestCase): + def test_this_is_testdir(self): + self.assertTrue(this_is_a_testdir("test")) + self.assertTrue(this_is_a_testdir("tests")) + self.assertTrue(this_is_a_testdir("unittests")) + self.assertTrue(this_is_a_testdir("unittest")) + self.assertFalse(this_is_a_testdir("unit")) + self.assertFalse(this_is_a_testdir("units")) + self.assertFalse(this_is_a_testdir("undksjhqfl")) + self.assertFalse(this_is_a_testdir("this_is_not_a_dir_test")) + self.assertFalse(this_is_a_testdir("this_is_not_a_testdir")) + self.assertFalse(this_is_a_testdir("unittestsarenothere")) + self.assertTrue(this_is_a_testdir(join("coincoin","unittests"))) + self.assertFalse(this_is_a_testdir(join("unittests","spongebob"))) + + def test_this_is_testfile(self): + self.assertTrue(this_is_a_testfile("test.py")) + self.assertTrue(this_is_a_testfile("testbabar.py")) + self.assertTrue(this_is_a_testfile("unittest_celestine.py")) + self.assertTrue(this_is_a_testfile("smoketest.py")) + self.assertFalse(this_is_a_testfile("test.pyc")) + self.assertFalse(this_is_a_testfile("zephir_test.py")) + self.assertFalse(this_is_a_testfile("smoketest.pl")) + self.assertFalse(this_is_a_testfile("unittest")) + self.assertTrue(this_is_a_testfile(join("coincoin","unittest_bibi.py"))) + self.assertFalse(this_is_a_testfile(join("unittest","spongebob.py"))) + +if __name__ == '__main__': + unittest_main() diff --git a/test/unittest_shellutils.py b/test/unittest_shellutils.py index 0298ca2..5e63170 100644 --- a/test/unittest_shellutils.py +++ b/test/unittest_shellutils.py @@ -5,7 +5,8 @@ from os.path import join from logilab.common.testlib import TestCase, unittest_main -from logilab.common.fileutils import * +from logilab.common.shellutils import find, ProgressBar +from StringIO import StringIO DATA_DIR = join('data','find_test') @@ -40,5 +41,76 @@ class FindTC(TestCase): # self.assertEquals(files_by_ext(DATA_DIR, include_exts=('.py',), exclude_dirs=(DATA_DIR,)), # []) +class ProgressBarTC(TestCase): + def test_refresh(self): + pgb_stream = StringIO() + expected_stream = StringIO() + pgb = ProgressBar(20,stream=pgb_stream) + self.assertEquals(pgb_stream.getvalue(), expected_stream.getvalue()) # nothing print before refresh + pgb.refresh() + expected_stream.write("\r["+' '*20+"]") + self.assertEquals(pgb_stream.getvalue(), expected_stream.getvalue()) + + def test_refresh_g_size(self): + pgb_stream = StringIO() + expected_stream = StringIO() + pgb = ProgressBar(20,35,stream=pgb_stream) + pgb.refresh() + expected_stream.write("\r["+' '*35+"]") + self.assertEquals(pgb_stream.getvalue(), expected_stream.getvalue()) + + def test_refresh_l_size(self): + pgb_stream = StringIO() + expected_stream = StringIO() + pgb = ProgressBar(20,3,stream=pgb_stream) + pgb.refresh() + expected_stream.write("\r["+' '*3+"]") + self.assertEquals(pgb_stream.getvalue(), expected_stream.getvalue()) + + def _update_test(self, nbops, expected, size = None): + pgb_stream = StringIO() + expected_stream = StringIO() + if size is None: + pgb = ProgressBar(nbops, stream=pgb_stream) + size=20 + else: + pgb = ProgressBar(nbops, size, stream=pgb_stream) + last = 0 + for round in expected: + if not hasattr(round, '__int__'): + dots, update = round + else: + dots, update = round, None + pgb.update() + if update or (update is None and dots != last): + last = dots + expected_stream.write("\r["+('.'*dots)+(' '*(size-dots))+"]") + self.assertEquals(pgb_stream.getvalue(), expected_stream.getvalue()) + + def test_default(self): + self._update_test(20, xrange(1,21)) + + def test_nbops_gt_size(self): + """Test the progress bar for nbops > size""" + def half(total): + for counter in range(1,total+1): + yield counter / 2 + self._update_test(40, half(40)) + + def test_nbops_lt_size(self): + """Test the progress bar for nbops < size""" + def double(total): + for counter in range(1,total+1): + yield counter * 2 + self._update_test(10, double(10)) + + def test_nbops_nomul_size(self): + """Test the progress bar for size % nbops !=0 (non int number of dots per update)""" + self._update_test(3, (6,13,20)) + + def test_overflow(self): + self._update_test(5, (8, 16, 25, 33, 42, (42, True)), size=42) + + if __name__ == '__main__': unittest_main() |