Exemplo n.º 1
0
    def test_directory(self):
        """
        Test loader against a filesystem directory containing an empty
        C{__init__.py} file. It should handle 'path' and 'path/' the same way.
        """
        goodDir = filepath.FilePath(self.parent).child('goodDirectory')
        goodDir.createDirectory()
        goodDir.child('__init__.py').setContent('')

        try:
            module = runner.filenameToModule(goodDir.path)
            self.assert_(module.__name__.endswith('goodDirectory'))
            module = runner.filenameToModule(goodDir.path + os.path.sep)
            self.assert_(module.__name__.endswith('goodDirectory'))
        finally:
            goodDir.remove()
Exemplo n.º 2
0
    def test_directory(self):
        """
        Test loader against a filesystem directory containing an empty
        C{__init__.py} file. It should handle 'path' and 'path/' the same way.
        """
        goodDir = filepath.FilePath(self.parent).child('goodDirectory')
        goodDir.createDirectory()
        goodDir.child('__init__.py').setContent(b'')

        try:
            module = runner.filenameToModule(goodDir.path)
            self.assertTrue(module.__name__.endswith('goodDirectory'))
            module = runner.filenameToModule(goodDir.path + os.path.sep)
            self.assertTrue(module.__name__.endswith('goodDirectory'))
        finally:
            goodDir.remove()
Exemplo n.º 3
0
 def test_directory(self):
     """
     Test loader against a filesystem directory. It should handle
     'path' and 'path/' the same way.
     """
     path = util.sibpath(__file__, 'goodDirectory')
     os.mkdir(path)
     f = file(os.path.join(path, '__init__.py'), "w")
     f.close()
     try:
         module = runner.filenameToModule(path)
         self.assert_(module.__name__.endswith('goodDirectory'))
         module = runner.filenameToModule(path + os.path.sep)
         self.assert_(module.__name__.endswith('goodDirectory'))
     finally:
         shutil.rmtree(path)
Exemplo n.º 4
0
 def test_directory(self):
     """
     Test loader against a filesystem directory. It should handle
     'path' and 'path/' the same way.
     """
     path  = util.sibpath(__file__, 'goodDirectory')
     os.mkdir(path)
     f = file(os.path.join(path, '__init__.py'), "w")
     f.close()
     try:
         module = runner.filenameToModule(path)
         self.assert_(module.__name__.endswith('goodDirectory'))
         module = runner.filenameToModule(path + os.path.sep)
         self.assert_(module.__name__.endswith('goodDirectory'))
     finally:
         shutil.rmtree(path)
Exemplo n.º 5
0
 def test_moduleInPath(self):
     """
     If the file in question is a module on the Python path, then it should
     properly import and return that module.
     """
     sample1 = runner.filenameToModule(util.sibpath(__file__, 'sample.py'))
     import sample as sample2
     self.assertEqual(sample2, sample1)
Exemplo n.º 6
0
    def test_packageInPath(self):
        """
        If the file in question is a package on the Python path, then it should
        properly import and return that package.
        """
        package1 = runner.filenameToModule(os.path.join(self.parent, "goodpackage"))

        self.assertIs(package1, sys.modules["goodpackage"])
Exemplo n.º 7
0
 def test_packageNotInPath(self):
     self.mangleSysPath(self.oldPath)
     package1 = runner.filenameToModule(os.path.join(self.parent,
                                                     'goodpackage'))
     self.mangleSysPath(self.newPath)
     import goodpackage
     self.failUnlessEqual(os.path.splitext(goodpackage.__file__)[0],
                          os.path.splitext(package1.__file__)[0])
Exemplo n.º 8
0
 def test_moduleInPath(self):
     """
     If the file in question is a module on the Python path, then it should
     properly import and return that module.
     """
     sample1 = runner.filenameToModule(util.sibpath(__file__, 'sample.py'))
     from twisted.trial.test import sample as sample2
     self.assertEqual(sample2, sample1)
Exemplo n.º 9
0
 def test_packageNotInPath(self):
     sys.path, newPath = self.oldPath, sys.path
     package1 = runner.filenameToModule(os.path.join(self.parent,
                                                     'goodpackage'))
     sys.path = newPath
     import goodpackage
     self.failUnlessEqual(os.path.splitext(goodpackage.__file__)[0],
                          os.path.splitext(package1.__file__)[0])
Exemplo n.º 10
0
 def test_packageNotInPath(self):
     sys.path, newPath = self.oldPath, sys.path
     package1 = runner.filenameToModule(
         os.path.join(self.parent, 'goodpackage'))
     sys.path = newPath
     import goodpackage
     self.failUnlessEqual(
         os.path.splitext(goodpackage.__file__)[0],
         os.path.splitext(package1.__file__)[0])
Exemplo n.º 11
0
 def test_moduleNotInPath(self):
     self.mangleSysPath(self.oldPath)
     sample1 = runner.filenameToModule(
         os.path.join(self.parent, 'goodpackage', 'test_sample.py'))
     self.mangleSysPath(self.newPath)
     from goodpackage import test_sample as sample2
     self.failUnlessEqual(
         os.path.splitext(sample2.__file__)[0],
         os.path.splitext(sample1.__file__)[0])
Exemplo n.º 12
0
 def test_packageNotInPath(self):
     self.mangleSysPath(self.oldPath)
     package1 = runner.filenameToModule(
         os.path.join(self.parent, 'goodpackage'))
     self.mangleSysPath(self.newPath)
     import goodpackage
     self.failUnlessEqual(
         os.path.splitext(goodpackage.__file__)[0],
         os.path.splitext(package1.__file__)[0])
Exemplo n.º 13
0
 def test_packageInPath(self):
     """
     If the file in question is a package on the Python path, then it should
     properly import and return that package.
     """
     package1 = runner.filenameToModule(os.path.join(self.parent,
                                                     'goodpackage'))
     import goodpackage
     self.assertEqual(goodpackage, package1)
Exemplo n.º 14
0
 def test_packageInPath(self):
     """
     If the file in question is a package on the Python path, then it should
     properly import and return that package.
     """
     package1 = runner.filenameToModule(
         os.path.join(self.parent, 'goodpackage'))
     import goodpackage
     self.assertEqual(goodpackage, package1)
Exemplo n.º 15
0
 def _loadNetTestFile(self, net_test_file):
     """
     Load NetTest from a file
     """
     test_cases = []
     module = filenameToModule(net_test_file)
     for __, item in getmembers(module):
         test_cases.extend(self._get_test_methods(item))
     return test_cases
Exemplo n.º 16
0
 def test_moduleNotInPath(self):
     sys.path, newPath = self.oldPath, sys.path
     sample1 = runner.filenameToModule(os.path.join(self.parent,
                                                    'goodpackage',
                                                    'test_sample.py'))
     sys.path = newPath
     from goodpackage import test_sample as sample2
     self.failUnlessEqual(os.path.splitext(sample2.__file__)[0],
                          os.path.splitext(sample1.__file__)[0])
Exemplo n.º 17
0
 def test_filenameMatchesPackage(self):
     filename = os.path.join(self.parent, 'goodpackage.py')
     fd = open(filename, 'w')
     fd.write(packages.testModule)
     fd.close()
     try:
         module = runner.filenameToModule(filename)
         self.failUnlessEqual(filename, module.__file__)
     finally:
         os.remove(filename)
Exemplo n.º 18
0
 def test_filenameMatchesPackage(self):
     filename = os.path.join(self.parent, 'goodpackage.py')
     fd = open(filename, 'w')
     fd.write(packages.testModule)
     fd.close()
     try:
         module = runner.filenameToModule(filename)
         self.failUnlessEqual(filename, module.__file__)
     finally:
         os.remove(filename)
Exemplo n.º 19
0
    def test_filenameMatchesPackage(self):
        """
        The C{__file__} attribute of the module should match the package name.
        """
        filename = filepath.FilePath(self.parent).child('goodpackage.py')
        filename.setContent(packages.testModule.encode("utf8"))

        try:
            module = runner.filenameToModule(filename.path)
            self.assertEqual(filename.path, module.__file__)
        finally:
            filename.remove()
Exemplo n.º 20
0
    def test_filenameMatchesPackage(self):
        """
        The C{__file__} attribute of the module should match the package name.
        """
        filename = filepath.FilePath(self.parent).child('goodpackage.py')
        filename.setContent(packages.testModule)

        try:
            module = runner.filenameToModule(filename.path)
            self.assertEqual(filename.path, module.__file__)
        finally:
            filename.remove()
Exemplo n.º 21
0
    def loadNetTestFile(self, net_test_file):
        """
        Load NetTest from a file.
        """
        test_cases = []
        module = filenameToModule(net_test_file)
        for __, item in getmembers(module):
            test_cases.extend(self._get_test_methods(item))

        if not test_cases:
            raise e.NoTestCasesFound

        self.setupTestCases(test_cases)
Exemplo n.º 22
0
def loadNetTestFile(net_test_file):
    """
    Load NetTest from a file.
    """
    test_cases = []
    module = filenameToModule(net_test_file)
    for __, item in getmembers(module):
        test_cases.extend(get_test_methods(item))

    if not test_cases:
        raise NoTestCasesFound

    return test_cases
Exemplo n.º 23
0
def getTestClassFromFile(net_test_file):
    """
    Will return the first class that is an instance of NetTestCase.

    XXX this means that if inside of a test there are more than 1 test case
        then we will only run the first one.
    """
    module = filenameToModule(net_test_file)
    for __, item in getmembers(module):
        try:
            assert issubclass(item, NetTestCase)
            return item
        except (TypeError, AssertionError):
            pass
Exemplo n.º 24
0
    def test_packageNotInPath(self):
        """
        If passed the path to a directory which represents a package which
        is not on the import path, L{runner.filenameToModule} returns a
        module object loosely resembling the package defined by that
        directory anyway.
        """
        self.mangleSysPath(self.oldPath)
        package1 = runner.filenameToModule(os.path.join(self.parent, "goodpackage"))
        self.assertEqual(package1.__name__, "goodpackage")

        self.cleanUpModules()
        self.mangleSysPath(self.newPath)
        import goodpackage

        self.assertIsNot(package1, goodpackage)
        self.assertEqual(package1.__spec__, goodpackage.__spec__)
Exemplo n.º 25
0
def findTestClassesFromFile(cmd_line_options):
    """
    Takes as input the command line config parameters and returns the test
    case classes.

    :param filename:
        the absolute path to the file containing the ooniprobe test classes

    :return:
        A list of class objects found in a file or module given on the
        commandline.
    """
    filename = cmd_line_options["test"]
    classes = []
    module = filenameToModule(filename)
    for name, val in inspect.getmembers(module):
        if isTestCase(val):
            classes.append(processTest(val, cmd_line_options))
    return classes
Exemplo n.º 26
0
    def test_moduleNotInPath(self):
        """
        If passed the path to a file containing the implementation of a
        module within a package which is not on the import path,
        L{runner.filenameToModule} returns a module object loosely
        resembling the module defined by that file anyway.
        """

        self.mangleSysPath(self.oldPath)
        sample1 = runner.filenameToModule(
            os.path.join(self.parent, "goodpackage", "test_sample.py"))
        self.assertEqual(sample1.__name__, "goodpackage.test_sample")

        self.cleanUpModules()
        self.mangleSysPath(self.newPath)
        from goodpackage import test_sample as sample2  # type: ignore[import]

        self.assertIsNot(sample1, sample2)
        self.assertEqual(sample1.__spec__, sample2.__spec__)
Exemplo n.º 27
0
    def test_moduleNotInPath(self):
        """
        If passed the path to a file containing the implementation of a
        module within a package which is not on the import path,
        L{runner.filenameToModule} returns a module object loosely
        resembling the module defined by that file anyway.
        """
        # "test_sample" isn't actually the name of this module.  However,
        # filenameToModule can't seem to figure that out.  So clean up this
        # misnamed module.  It would be better if this weren't necessary
        # and filenameToModule either didn't exist or added a correctly
        # named module to sys.modules.
        self.addCleanup(sys.modules.pop, "test_sample", None)

        self.mangleSysPath(self.oldPath)
        sample1 = runner.filenameToModule(os.path.join(self.parent, "goodpackage", "test_sample.py"))
        self.mangleSysPath(self.newPath)
        from goodpackage import test_sample as sample2

        self.assertEqual(os.path.splitext(sample2.__file__)[0], os.path.splitext(sample1.__file__)[0])
Exemplo n.º 28
0
    def test_packageNotInPath(self):
        """
        If passed the path to a directory which represents a package which
        is not on the import path, L{runner.filenameToModule} returns a
        module object loosely resembling the package defined by that
        directory anyway.
        """
        # "__init__" isn't actually the name of the package!  However,
        # filenameToModule is pretty stupid and decides that is its name
        # after all.  Make sure it gets cleaned up.  See the comment in
        # test_moduleNotInPath for possible courses of action related to
        # this.
        self.addCleanup(sys.modules.pop, "__init__")

        self.mangleSysPath(self.oldPath)
        package1 = runner.filenameToModule(os.path.join(self.parent, "goodpackage"))
        self.mangleSysPath(self.newPath)
        import goodpackage

        self.assertEqual(os.path.splitext(goodpackage.__file__)[0], os.path.splitext(package1.__file__)[0])
Exemplo n.º 29
0
    def test_moduleNotInPath(self):
        """
        If passed the path to a file containing the implementation of a
        module within a package which is not on the import path,
        L{runner.filenameToModule} returns a module object loosely
        resembling the module defined by that file anyway.
        """
        # "test_sample" isn't actually the name of this module.  However,
        # filenameToModule can't seem to figure that out.  So clean up this
        # misnamed module.  It would be better if this weren't necessary
        # and filenameToModule either didn't exist or added a correctly
        # named module to sys.modules.
        self.addCleanup(sys.modules.pop, 'test_sample', None)

        self.mangleSysPath(self.oldPath)
        sample1 = runner.filenameToModule(
            os.path.join(self.parent, 'goodpackage', 'test_sample.py'))
        self.mangleSysPath(self.newPath)
        from goodpackage import test_sample as sample2
        self.assertEqual(os.path.splitext(sample2.__file__)[0],
                             os.path.splitext(sample1.__file__)[0])
Exemplo n.º 30
0
    def test_packageNotInPath(self):
        """
        If passed the path to a directory which represents a package which
        is not on the import path, L{runner.filenameToModule} returns a
        module object loosely resembling the package defined by that
        directory anyway.
        """
        # "__init__" isn't actually the name of the package!  However,
        # filenameToModule is pretty stupid and decides that is its name
        # after all.  Make sure it gets cleaned up.  See the comment in
        # test_moduleNotInPath for possible courses of action related to
        # this.
        self.addCleanup(sys.modules.pop, "__init__")

        self.mangleSysPath(self.oldPath)
        package1 = runner.filenameToModule(
            os.path.join(self.parent, 'goodpackage'))
        self.mangleSysPath(self.newPath)
        import goodpackage
        self.assertEqual(os.path.splitext(goodpackage.__file__)[0],
                         os.path.splitext(package1.__file__)[0])
Exemplo n.º 31
0
def findTestClassesFromConfig(cmd_line_options):
    """
    Takes as input the command line config parameters and returns the test
    case classes.
    If it detects that a certain test class is using the old OONIProbe format,
    then it will adapt it to the new testing system.

    :param cmd_line_options:
        A configured and instantiated :class:`twisted.python.usage.Options`
        class.
    :return:
        A list of class objects found in a file or module given on the
        commandline.
    """

    filename = cmd_line_options['test']
    classes = []

    module = filenameToModule(filename)
    for name, val in inspect.getmembers(module):
        if isTestCase(val):
            classes.append(processTest(val, cmd_line_options))
    return classes
Exemplo n.º 32
0
def findTestClassesFromConfig(cmd_line_options):
    """
    Takes as input the command line config parameters and returns the test
    case classes.
    If it detects that a certain test class is using the old OONIProbe format,
    then it will adapt it to the new testing system.

    :param cmd_line_options:
        A configured and instantiated :class:`twisted.python.usage.Options`
        class.
    :return:
        A list of class objects found in a file or module given on the
        commandline.
    """

    filename = cmd_line_options['test']
    classes = []

    module = filenameToModule(filename)
    for name, val in inspect.getmembers(module):
        if isTestCase(val):
            classes.append(processTest(val, cmd_line_options))
    return classes
Exemplo n.º 33
0
    def test_packageInPath(self):
        package1 = runner.filenameToModule(os.path.join(self.parent, "goodpackage"))
        import goodpackage

        self.assertEqual(goodpackage, package1)
Exemplo n.º 34
0
    def test_moduleInPath(self):
        sample1 = runner.filenameToModule(util.sibpath(__file__, "sample.py"))
        import sample as sample2

        self.assertEqual(sample2, sample1)
Exemplo n.º 35
0
 def test_packageInPath(self):
     package1 = runner.filenameToModule(
         os.path.join(self.parent, 'goodpackage'))
     import goodpackage
     self.failUnlessEqual(goodpackage, package1)
Exemplo n.º 36
0
 def test_moduleInPath(self):
     sample1 = runner.filenameToModule(util.sibpath(__file__, 'sample.py'))
     import sample as sample2
     self.failUnlessEqual(sample2, sample1)
Exemplo n.º 37
0
 def test_moduleInPath(self):
     sample1 = runner.filenameToModule(util.sibpath(__file__, 'sample.py'))
     import sample as sample2
     self.failUnlessEqual(sample2, sample1)
Exemplo n.º 38
0
 def test_packageInPath(self):
     package1 = runner.filenameToModule(os.path.join(self.parent,
                                                     'goodpackage'))
     import goodpackage
     self.failUnlessEqual(goodpackage, package1)