diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2014-07-24 11:44:54 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2014-07-24 11:44:54 +0200 |
commit | 43f378d492991e9226075b1dd417d998628f3289 (patch) | |
tree | 9bf913b97d487f5108b34b6629add8084b2e20d5 | |
parent | 817404a42b548dc34e58b70d49b992b11ccd770f (diff) | |
download | astroid-43f378d492991e9226075b1dd417d998628f3289.tar.gz |
cleanup and fix modutils tests (among other, logilab.common.modutils was still actually tested)
-rw-r--r-- | modutils.py | 11 | ||||
-rw-r--r-- | test/data/MyPyPa-0.1.0.zip | bin | 206 -> 0 bytes | |||
-rw-r--r-- | test/unittest_manager.py | 6 | ||||
-rw-r--r-- | test/unittest_modutils.py | 109 |
4 files changed, 65 insertions, 61 deletions
diff --git a/modutils.py b/modutils.py index a13bb1d..e781cb3 100644 --- a/modutils.py +++ b/modutils.py @@ -1,21 +1,20 @@ -# -*- coding: utf-8 -*- -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # -# This file is part of logilab-common. +# This file is part of astroid. # -# logilab-common is free software: you can redistribute it and/or modify it under +# astroid is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 2.1 of the License, or (at your option) any # later version. # -# logilab-common is distributed in the hope that it will be useful, but WITHOUT +# astroid is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License along -# with logilab-common. If not, see <http://www.gnu.org/licenses/>. +# with astroid. If not, see <http://www.gnu.org/licenses/>. """Python modules manipulation utility functions. :type PY_SOURCE_EXTS: tuple(str) diff --git a/test/data/MyPyPa-0.1.0.zip b/test/data/MyPyPa-0.1.0.zip Binary files differdeleted file mode 100644 index a7b3125..0000000 --- a/test/data/MyPyPa-0.1.0.zip +++ /dev/null diff --git a/test/unittest_manager.py b/test/unittest_manager.py index bdc2cdc..7f1c23c 100644 --- a/test/unittest_manager.py +++ b/test/unittest_manager.py @@ -90,9 +90,9 @@ class AstroidManagerTC(TestCase): try: module = self.manager.ast_from_module_name('mypypa') self.assertEqual(module.name, 'mypypa') - end = join(archive, 'mypypa') + end = join(archive, 'mypypa') self.assertTrue(module.file.endswith(end), - module.file) + "%s doesn't endswith %s" % (module.file, end)) finally: # remove the module, else after importing egg, we don't get the zip if 'mypypa' in self.manager.astroid_cache: @@ -119,7 +119,7 @@ class AstroidManagerTC(TestCase): self.assertEqual(self.manager.zip_import_data('path'), None) def test_file_from_module(self): - """check if the unittest filepath is equals to the result of the method""" + """check if the unittest filepath is equals to the result of the method""" import unittest if PY3K: unittest_file = unittest.__file__ diff --git a/test/unittest_modutils.py b/test/unittest_modutils.py index 1031445..396545e 100644 --- a/test/unittest_modutils.py +++ b/test/unittest_modutils.py @@ -1,20 +1,20 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # -# This file is part of logilab-common. +# This file is part of astroid. # -# logilab-common is free software: you can redistribute it and/or modify it under +# astroid is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 2.1 of the License, or (at your option) any # later version. # -# logilab-common is distributed in the hope that it will be useful, but WITHOUT +# astroid is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License along -# with logilab-common. If not, see <http://www.gnu.org/licenses/>. +# with astroid. If not, see <http://www.gnu.org/licenses/>. """ unit tests for module modutils (module manipulation utilities) """ @@ -26,42 +26,27 @@ except NameError: __file__ = sys.argv[0] from logilab.common.testlib import TestCase, unittest_main -from logilab.common import modutils from os import path, getcwd, sep -from logilab import common -from logilab.common import tree +from astroid import modutils sys.path.insert(0, path.dirname(__file__)) -DATADIR = path.join(path.dirname(__file__), 'data') +DATADIR = path.abspath(path.normpath(path.join(path.dirname(__file__), 'data'))) -class ModutilsTestCase(TestCase): - def setUp(self): - super(ModutilsTestCase, self).setUp() - self.__common_in_path = common.__path__[0] in sys.path - if self.__common_in_path: - sys.path.remove(common.__path__[0]) - - def tearDown(self): - if self.__common_in_path: - sys.path.insert(0, common.__path__[0]) - super(ModutilsTestCase, self).tearDown() - - -class ModuleFileTC(ModutilsTestCase): +class ModuleFileTC(TestCase): package = "mypypa" - def setUp(self): - super(ModuleFileTC, self).setUp() + def tearDown(self): + super(ModuleFileTC, self).tearDown() for k in sys.path_importer_cache.keys(): if 'MyPyPa' in k: del sys.path_importer_cache[k] def test_find_zipped_module(self): - mtype, mfile = modutils._module_file([self.package], [path.join(DATADIR, 'MyPyPa-0.1.0.zip')]) + mtype, mfile = modutils._module_file([self.package], [path.join(DATADIR, 'MyPyPa-0.1.0-py2.5.zip')]) self.assertEqual(mtype, modutils.ZIPFILE) - self.assertEqual(mfile.split(sep)[-4:], ["test", "data", "MyPyPa-0.1.0.zip", self.package]) + self.assertEqual(mfile.split(sep)[-4:], ["test", "data", "MyPyPa-0.1.0-py2.5.zip", self.package]) def test_find_egg_module(self): mtype, mfile = modutils._module_file([self.package], [path.join(DATADIR, 'MyPyPa-0.1.0-py2.5.egg')]) @@ -69,7 +54,7 @@ class ModuleFileTC(ModutilsTestCase): self.assertEqual(mfile.split(sep)[-4:], ["test", "data", "MyPyPa-0.1.0-py2.5.egg", self.package]) -class load_module_from_name_tc(ModutilsTestCase): +class load_module_from_name_tc(TestCase): """ load a python module from it's name """ def test_knownValues_load_module_from_name_1(self): @@ -83,21 +68,21 @@ class load_module_from_name_tc(ModutilsTestCase): modutils.load_module_from_name, 'os.path', use_sys=0) -class get_module_part_tc(ModutilsTestCase): +class get_module_part_tc(TestCase): """given a dotted name return the module part of the name""" def test_knownValues_get_module_part_1(self): - self.assertEqual(modutils.get_module_part('logilab.common.modutils'), - 'logilab.common.modutils') + self.assertEqual(modutils.get_module_part('astroid.modutils'), + 'astroid.modutils') def test_knownValues_get_module_part_2(self): - self.assertEqual(modutils.get_module_part('logilab.common.modutils.get_module_part'), - 'logilab.common.modutils') + self.assertEqual(modutils.get_module_part('astroid.modutils.get_module_part'), + 'astroid.modutils') def test_knownValues_get_module_part_3(self): """relative import from given file""" - self.assertEqual(modutils.get_module_part('interface.Interface', - modutils.__file__), 'interface') + self.assertEqual(modutils.get_module_part('node_classes.AssName', + modutils.__file__), 'node_classes') def test_knownValues_get_compiled_module_part(self): self.assertEqual(modutils.get_module_part('math.log10'), 'math') @@ -112,12 +97,12 @@ class get_module_part_tc(ModutilsTestCase): modutils.__file__) -class modpath_from_file_tc(ModutilsTestCase): +class modpath_from_file_tc(TestCase): """ given an absolute file path return the python module's path as a list """ def test_knownValues_modpath_from_file_1(self): self.assertEqual(modutils.modpath_from_file(modutils.__file__), - ['logilab', 'common', 'modutils']) + ['astroid', 'modutils']) def test_knownValues_modpath_from_file_2(self): self.assertEqual(modutils.modpath_from_file('unittest_modutils.py', @@ -128,7 +113,7 @@ class modpath_from_file_tc(ModutilsTestCase): self.assertRaises(Exception, modutils.modpath_from_file, '/turlututu') -class load_module_from_path_tc(ModutilsTestCase): +class load_module_from_path_tc(TestCase): def test_do_not_load_twice(self): sys.path.insert(0, self.datadir) @@ -138,13 +123,14 @@ class load_module_from_path_tc(ModutilsTestCase): sys.path.pop(0) del sys.just_once -class file_from_modpath_tc(ModutilsTestCase): + +class file_from_modpath_tc(TestCase): """given a mod path (i.e. splited module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists""" def test_site_packages(self): - self.assertEqual(path.realpath(modutils.file_from_modpath(['logilab', 'common', 'modutils'])), + self.assertEqual(path.realpath(modutils.file_from_modpath(['astroid', 'modutils'])), path.realpath(modutils.__file__.replace('.pyc', '.py'))) def test_std_lib(self): @@ -171,17 +157,18 @@ class file_from_modpath_tc(ModutilsTestCase): self.assertRaises(ImportError, modutils.file_from_modpath, ['turlututu']) -class get_source_file_tc(ModutilsTestCase): +class get_source_file_tc(TestCase): def test(self): from os import path self.assertEqual(modutils.get_source_file(path.__file__), - path.__file__.replace('.pyc', '.py')) + path.normpath(path.__file__.replace('.pyc', '.py'))) def test_raise(self): self.assertRaises(modutils.NoSourceFile, modutils.get_source_file, 'whatever') -class is_standard_module_tc(ModutilsTestCase): + +class is_standard_module_tc(TestCase): """ return true if the module may be considered as a module from the standard library @@ -204,8 +191,13 @@ class is_standard_module_tc(ModutilsTestCase): def test_unknown(self): self.assertEqual(modutils.is_standard_module('unknown'), False) - def test_4(self): + def test_builtin(self): self.assertEqual(modutils.is_standard_module('marshal'), True) + + def test_4(self): + import astroid + if sys.version_info > (3, 0) and '.tox' in astroid.__file__: + self.skipTest('py3 tox environments are known to have bad STD_LIB_DIR, breaking this test') self.assertEqual(modutils.is_standard_module('hashlib'), True) self.assertEqual(modutils.is_standard_module('pickle'), True) self.assertEqual(modutils.is_standard_module('email'), True) @@ -213,10 +205,15 @@ class is_standard_module_tc(ModutilsTestCase): self.assertEqual(modutils.is_standard_module('StringIO'), sys.version_info < (3, 0)) def test_custom_path(self): + if DATADIR.startswith(modutils.EXT_LIB_DIR): + self.skipTest('known breakage of is_standard_module on installed package') + print repr(DATADIR) + print modutils.EXT_LIB_DIR self.assertEqual(modutils.is_standard_module('data.module', (DATADIR,)), True) self.assertEqual(modutils.is_standard_module('data.module', (path.abspath(DATADIR),)), True) - def test_failing_border_cases(self): + def test_failing_edge_cases(self): + from logilab import common # using a subpackage/submodule path as std_path argument self.assertEqual(modutils.is_standard_module('logilab.common', common.__path__), False) # using a module + object name as modname argument @@ -226,21 +223,26 @@ class is_standard_module_tc(ModutilsTestCase): self.assertEqual(modutils.is_standard_module('logilab.whatever', common.__path__), False) -class is_relative_tc(ModutilsTestCase): +class is_relative_tc(TestCase): def test_knownValues_is_relative_1(self): - self.assertEqual(modutils.is_relative('modutils', common.__path__[0]), True) + import astroid + self.assertEqual(modutils.is_relative('modutils', astroid.__path__[0]), + True) def test_knownValues_is_relative_2(self): - self.assertEqual(modutils.is_relative('modutils', tree.__file__), True) + from logilab.common import tree + self.assertEqual(modutils.is_relative('modutils', tree.__file__), + True) def test_knownValues_is_relative_3(self): - self.assertEqual(modutils.is_relative('logilab.common.modutils', - common.__path__[0]), False) + import astroid + self.assertEqual(modutils.is_relative('astroid', astroid.__path__[0]), + False) -class get_module_files_tc(ModutilsTestCase): +class get_module_files_tc(TestCase): def test_knownValues_get_module_files_1(self): # XXXFIXME: TOWRITE """given a directory return a list of all available python module's files, even @@ -262,10 +264,13 @@ class get_module_files_tc(ModutilsTestCase): self.assertTrue( hasattr(logilab.common, 'fileutils') ) self.assertTrue( m is logilab.common.fileutils ) + from logilab.common.testlib import DocTest + class ModuleDocTest(DocTest): """test doc test in this module""" - from logilab.common import modutils as module + from astroid import modutils as module + del DocTest # necessary if we don't want it to be executed (we don't...) |