Ejemplo n.º 1
0
 def setUpClass(cls):
     """Check whether bs4 module is installed already."""
     if not has_module('bs4'):
         unittest_print('all tests ({module}.{name})\n{doc}.. '.format(
             module=__name__, doc=cls.__doc__, name=cls.__name__),
                        end='\n')
         cls.skipTest(cls, 'bs4 not installed')
     super().setUpClass()
Ejemplo n.º 2
0
 def tearDown(self):
     """Tear down test."""
     super(TestIPBase, self).tearDown()
     if not self.fail:
         unittest_print('{0} tests done'.format(self.total))
     else:
         unittest_print('{0} of {1} tests failed:\n{2}'.format(
             self.fail, self.total, '\n'.join(self.errors)))
 def setUpClass(cls):
     """Check whether bs4 module is installed already."""
     if not has_module('bs4'):
         unittest_print('all tests ({module}.{name})\n{doc} ... '.format(
             module=__name__, doc=cls.__doc__, name=cls.__name__),
                        end='')
         # skipTest cannot be used with Python 2 for setUpClass
         raise unittest.SkipTest('bs4 not installed')
     super(BS4TestCase, cls).setUpClass()
Ejemplo n.º 4
0
 def tearDown(self):
     """Tear down test."""
     super(TestIPBase, self).tearDown()
     if not self.fail:
         unittest_print('{0} tests done'.format(self.total))
     else:
         unittest_print(
             '{0} of {1} tests failed:\n{2}'.format(
                 self.fail, self.total, '\n'.join(self.errors)))
Ejemplo n.º 5
0
def check_script_deps(script_name):
    """Detect whether all dependencies are installed."""
    if script_name in script_deps:
        for package_name in script_deps[script_name]:
            if not has_module(package_name):
                unittest_print(
                    "{} depends on {}, which isn't available".format(
                        script_name, package_name))
                return False
    return True
Ejemplo n.º 6
0
def check_script_deps(script_name):
    """Detect whether all dependencies are installed."""
    if script_name in script_deps:
        for package_name in script_deps[script_name]:
            try:
                __import__(package_name)
            except ImportError as e:
                unittest_print('%s depends on %s, which isnt available:\n%s' %
                               (script_name, package_name, e))
                return False
    return True
    def assertNoSite(self, url: str):
        """
        Assert a url is not a MediaWiki site.

        @param url: Url of tested site
        @raises AssertionError: Site under url is MediaWiki powered
        """
        with self.assertRaises((AttributeError, ConnectionError, RuntimeError,
                                ServerError, Timeout)) as e:
            MWSite(url)
        unittest_print(
            '\nassertNoSite expected exception:\n{e!r}'.format(e=e.exception))
Ejemplo n.º 8
0
def execute_with_temp_text_file(text, command, **kwargs):
    """
    Perform command on a temporary file.

    @param text: contents of temporary file
    @type text: str
    @param command: command to execute with {0} replaced with the filename
    @type command: str
    @param kwargs: parameters for tempfile.mkstemp/tempfile.NamedTemporaryFile,
        such as prefix, suffix and dir
    """
    options = {
        'shell': True,
        'stdout': subprocess.PIPE,
        'stderr': subprocess.STDOUT,
    }

    # NamedTemporaryFile does not work correctly with win32_set_global_locale
    # subprocess.Popen is a context handler in Python 3.2+
    if OSWIN32 or PY2:
        (fd, filename) = tempfile.mkstemp(text=True, **kwargs)
        try:
            os.close(fd)
            with open(filename, 'wt') as f:
                f.write(text)

            command = command.format(filename)

            p = subprocess.Popen(command, **options)
            out = p.communicate()[0]

            # Python 2 raises an exception when attempting to close the process
            # Python 3 does not allow the file to be removed until the process
            # has been closed
            if not PY2:
                p.terminate()
        finally:
            try:
                os.remove(filename)
            except OSError:
                # As it is a temporary file, the OS should clean it up
                unittest_print('Could not delete {0}'.format(filename))
    else:
        with tempfile.NamedTemporaryFile(mode='w+t', **kwargs) as f:
            f.write(text)
            f.flush()
            command = command.format(f.name)
            with subprocess.Popen(command, **options) as p:
                out = p.communicate()[0]

    if out:
        unittest_print('command "{0}" output: {1}'.format(command, out))
Ejemplo n.º 9
0
def execute_with_temp_text_file(text, command, **kwargs):
    """
    Perform command on a temporary file.

    @param text: contents of temporary file
    @type text: str
    @param command: command to execute with {0} replaced with the filename
    @type command: str
    @param kwargs: parameters for tempfile.mkstemp/tempfile.NamedTemporaryFile,
        such as prefix, suffix and dir
    """
    options = {
        'shell': True,
        'stdout': subprocess.PIPE,
        'stderr': subprocess.STDOUT,
    }

    # NamedTemporaryFile does not work correctly with win32_set_global_locale
    # subprocess.Popen is a context handler in Python 3.2+
    if OSWIN32 or PY2:
        (fd, filename) = tempfile.mkstemp(text=True, **kwargs)
        try:
            os.close(fd)
            with open(filename, 'wt') as f:
                f.write(text)

            command = command.format(filename)

            p = subprocess.Popen(command, **options)
            out = p.communicate()[0]

            # Python 2 raises an exception when attempting to close the process
            # Python 3 does not allow the file to be removed until the process
            # has been closed
            if not PY2:
                p.terminate()
        finally:
            try:
                os.remove(filename)
            except OSError:
                # As it is a temporary file, the OS should clean it up
                unittest_print('Could not delete {0}'.format(filename))
    else:
        with tempfile.NamedTemporaryFile(mode='w+t', **kwargs) as f:
            f.write(text)
            f.flush()
            command = command.format(f.name)
            with subprocess.Popen(command, **options) as p:
                out = p.communicate()[0]

    if out:
        unittest_print('command "{0}" output: {1}'.format(command, out))
Ejemplo n.º 10
0
 def test_simple_entries(self, key):
     """Test those entries which don't have an extra LogEntry subclass."""
     # Unfortunately it's not possible to use the metaclass to create a
     # bunch of test methods for this too as the site instances haven't
     # been initialized yet.
     for simple_type in (self.site.logtypes -
                         set(LogEntryFactory._logtypes)):
         if not simple_type:
             # paraminfo also reports an empty string as a type
             continue
         try:
             self._test_logevent(simple_type)
         except StopIteration:
             unittest_print(
                 'Unable to test "{0}" on "{1}" because there are no log '
                 'entries with that type.'.format(simple_type, key))
Ejemplo n.º 11
0
 def test_simple_entries(self, key):
     """Test those entries which don't have an extra LogEntry subclass."""
     # Unfortunately it's not possible to use the metaclass to create a
     # bunch of test methods for this too as the site instances haven't been
     # initialized yet.
     available_types = set(self.site._paraminfo.parameter(
         'query+logevents', 'type')['type'])
     for simple_type in available_types - set(LogEntryFactory.logtypes):
         if not simple_type:
             # paraminfo also reports an empty string as a type
             continue
         try:
             self._test_logevent(simple_type)
         except StopIteration:
             unittest_print(
                 'Unable to test "{0}" on "{1}" because there are no log '
                 'entries with that type.'.format(simple_type, key))
Ejemplo n.º 12
0
def collector(loader=unittest.loader.defaultTestLoader):
    """Load the default tests."""
    # Note: Raising SkipTest during load_tests will
    # cause the loader to fallback to its own
    # discover() ordering of unit tests.

    if unrunnable_script_set:
        unittest_print('Skipping execution of unrunnable scripts:\n  {!r}'
                       .format(unrunnable_script_set))

    if not enable_autorun_tests:
        unittest_print('Skipping execution of auto-run scripts '
                       '(set PYWIKIBOT_TEST_AUTORUN=1 to enable):\n  {!r}'
                       .format(auto_run_script_list))

    tests = (['test__login']
             + ['test_' + name
                 for name in sorted(script_list)
                 if name != 'login'
                 and name not in unrunnable_script_set
                ])

    test_list = ['tests.script_tests.TestScriptHelp.' + name
                 for name in tests]

    tests = (['test__login']
             + ['test_' + name
                 for name in sorted(script_list)
                 if name != 'login'
                 and name not in failed_dep_script_set
                 and name not in unrunnable_script_set
                 and (enable_autorun_tests or name not in auto_run_script_list)
                ])

    test_list += ['tests.script_tests.TestScriptSimulate.' + name
                  for name in tests]

    tests = loader.loadTestsFromNames(test_list)
    suite = unittest.TestSuite()
    suite.addTests(tests)
    return suite
Ejemplo n.º 13
0
            def testScript(self):
                GLOBAL_ARGS = 'Global arguments available for all'

                cmd = [script_name]

                if args:
                    cmd += args

                data_in = script_input.get(script_name)

                timeout = 0
                if is_autorun:
                    timeout = 5

                if self._results and script_name in self._results:
                    error = self._results[script_name]
                    if isinstance(error, StringTypes):
                        stdout = None
                    else:
                        stdout, error = error
                else:
                    stdout = None
                    error = None

                test_overrides = {}
                if not hasattr(self, 'net') or not self.net:
                    test_overrides['pywikibot.Site'] = 'None'

                result = execute_pwb(cmd,
                                     data_in,
                                     timeout=timeout,
                                     error=error,
                                     overrides=test_overrides)

                stderr = result['stderr'].splitlines()
                stderr_sleep = [
                    l for l in stderr if l.startswith('Sleeping for ')
                ]
                stderr_other = [
                    l for l in stderr if not l.startswith('Sleeping for ')
                ]
                if stderr_sleep:
                    unittest_print('\n'.join(stderr_sleep))

                if result['exit_code'] == -9:
                    unittest_print(' killed', end='  ')

                if error:
                    self.assertIn(error, result['stderr'])

                    exit_codes = [0, 1, 2, -9]
                elif not is_autorun:
                    if stderr_other == []:
                        stderr_other = None
                    if stderr_other is not None:
                        self.assertIn('Use -help for further information.',
                                      stderr_other)
                        self.assertNotIn('-help', args)
                    else:
                        self.assertIn(GLOBAL_ARGS, result['stdout'])

                    exit_codes = [0]
                else:
                    # auto-run
                    exit_codes = [0, -9]

                    if (not result['stdout'] and not result['stderr']):
                        unittest_print(' auto-run script unresponsive after '
                                       '%d seconds' % timeout,
                                       end=' ')
                    elif 'SIMULATION: edit action blocked' in result['stderr']:
                        unittest_print(
                            ' auto-run script simulated edit '
                            'blocked',
                            end='  ')
                    else:
                        unittest_print(
                            ' auto-run script stderr within %d seconds: %r' %
                            (timeout, result['stderr']),
                            end='  ')

                self.assertNotIn('Traceback (most recent call last)',
                                 result['stderr'])
                self.assertNotIn('deprecated', result['stderr'].lower())

                # If stdout doesnt include global help..
                if GLOBAL_ARGS not in result['stdout']:
                    # Specifically look for deprecated
                    self.assertNotIn('deprecated', result['stdout'].lower())
                    if result['stdout'] == '':
                        result['stdout'] = None
                    # But also complain if there is any stdout
                    if stdout is not None and result['stdout'] is not None:
                        self.assertIn(stdout, result['stdout'])
                    else:
                        self.assertIsNone(result['stdout'])

                self.assertIn(result['exit_code'], exit_codes)

                sys.stdout.flush()
Ejemplo n.º 14
0
 def tearDown(self):
     """Tear down test."""
     super(TestIPBase, self).tearDown()
     unittest_print('{} subtests done'.format(self.total))
 def test_ipaddress_module(self):
     """Test ipaddress module."""
     unittest_print('testing {0}'.format(ip.ip_address.__module__))
     self._run_tests()
     self.assertEqual(self.fail, 0)
 def tearDown(self):
     """Tear down test."""
     super(IPAddressModuleTestCase, self).tearDown()
     unittest_print('{} subtests done'.format(self.total))
Ejemplo n.º 17
0
            def testScript(self):
                global_args = 'For global options use -help:global or run pwb'

                cmd = [script_name] + args
                data_in = script_input.get(script_name)
                timeout = 5 if is_autorun else None

                stdout, error = None, None
                if self._results and script_name in self._results:
                    error = self._results[script_name]
                    if isinstance(error, tuple):
                        stdout, error = error

                test_overrides = {}
                if not hasattr(self, 'net') or not self.net:
                    test_overrides['pywikibot.Site'] = 'lambda *a, **k: None'

                # run the script
                result = execute_pwb(cmd,
                                     data_in,
                                     timeout=timeout,
                                     error=error,
                                     overrides=test_overrides)

                err_result = result['stderr']
                out_result = result['stdout']

                stderr_sleep, stderr_other = [], []
                for line in err_result.splitlines():
                    if line.startswith('Sleeping for '):
                        stderr_sleep.append(line)
                    else:
                        stderr_other.append(line)

                if stderr_sleep:
                    unittest_print('\n'.join(stderr_sleep))

                if result['exit_code'] == -9:
                    unittest_print(' killed', end='  ')

                if error:
                    self.assertIn(error, result['stderr'])
                    exit_codes = [0, 1, 2, -9]

                elif not is_autorun:
                    if not stderr_other:
                        self.assertIn(global_args, out_result)
                    else:
                        self.assertIn('Use -help for further information.',
                                      stderr_other)
                        self.assertNotIn('-help', args)
                    exit_codes = [0]

                else:
                    # auto-run
                    # returncode is 1 if the process is killed
                    exit_codes = [0, 1, -9]
                    if not out_result and not err_result:
                        unittest_print(' auto-run script unresponsive after '
                                       '{} seconds'.format(timeout),
                                       end=' ')
                    elif 'SIMULATION: edit action blocked' in err_result:
                        unittest_print(
                            ' auto-run script simulated edit '
                            'blocked',
                            end=' ')
                    else:
                        unittest_print(
                            ' auto-run script stderr within {} seconds: {!r}'.
                            format(timeout, err_result),
                            end='  ')
                    unittest_print(' exit code: {}'.format(
                        result['exit_code']),
                                   end=' ')

                self.assertNotIn('Traceback (most recent call last)',
                                 err_result)
                self.assertNotIn('deprecated', err_result.lower())

                # If stdout doesn't include global help..
                if global_args not in out_result:
                    # Specifically look for deprecated
                    self.assertNotIn('deprecated', out_result.lower())
                    # But also complain if there is any stdout
                    if stdout is not None and out_result:
                        self.assertIn(stdout, out_result)
                    else:
                        self.assertIsEmpty(out_result)

                self.assertIn(result['exit_code'], exit_codes)
                sys.stdout.flush()
Ejemplo n.º 18
0
 def test_ipaddress_module(self):
     """Test ipaddress module."""
     unittest_print('testing {0}'.format(ip.ip_address.__module__))
     self._run_tests()
     self.assertEqual(self.fail, 0)