Ejemplo n.º 1
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=[]):
            is_autorun = ('-help' not in args
                          and script_name in auto_run_script_list)

            def test_skip_script(self):
                raise unittest.SkipTest(
                    'Skipping execution of auto-run scripts (set '
                    'PYWIKIBOT2_TEST_AUTORUN=1 to enable) "{0}"'.format(
                        script_name))

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

            if not enable_autorun_tests and is_autorun:
                return test_skip_script
            return testScript

        argument = '-' + dct['_argument']

        for script_name in script_list:
            # force login to be the first, alphabetically, so the login
            # message does not unexpectedly occur during execution of
            # another script.
            # unrunnable script tests are disabled by default in load_tests()

            if script_name == 'login':
                test_name = 'test__login'
            else:
                test_name = 'test_' + script_name

            cls.add_method(dct, test_name,
                           test_execution(script_name, [argument]),
                           'Test running %s %s.' % (script_name, argument))

            if script_name in dct['_expected_failures']:
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            elif script_name in dct['_allowed_failures']:
                dct[test_name] = allowed_failure(dct[test_name])

            # Disable test by default in nosetests
            if script_name in unrunnable_script_list:
                # flag them as an expectedFailure due to py.test (T135594)
                dct[test_name] = unittest.expectedFailure(dct[test_name])
                dct[test_name].__test__ = False

        return super(TestScriptMeta, cls).__new__(cls, name, bases, dct)
Ejemplo n.º 2
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=[], expected_results=None):
            is_autorun = '-help' not in args and script_name in auto_run_script_list

            def test_skip_script(self):
                raise unittest.SkipTest(
                    'Skipping execution of auto-run scripts (set '
                    'PYWIKIBOT2_TEST_AUTORUN=1 to enable) "{0}"'.format(script_name))

            def testScript(self):
                cmd = [script_name]

                if args:
                    cmd += args

                data_in = script_input.get(script_name)

                timeout = 0
                if is_autorun:
                    timeout = 5

                if expected_results and script_name in expected_results:
                    error = expected_results[script_name]
                    if isinstance(error, basestring):
                        stdout = None
                    else:
                        stdout, error = error
                else:
                    stdout = None
                    error = None

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

                stderr = result['stderr'].split('\n')
                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:
                    print(u'\n'.join(stderr_sleep))

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

                if '-help' in args or error or \
                        script_name not in auto_run_script_list:

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

                        exit_codes = [0, 1, 2, -9]
                    else:
                        if stderr_other == ['']:
                            stderr_other = None
                        self.assertIsNone(stderr_other)
                        self.assertIn('Global arguments available for all',
                                      result['stdout'])

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

                    if (not result['stdout'] and not result['stderr']):
                        print(' auto-run script unresponsive after %d seconds'
                              % timeout, end=' ')
                    elif 'SIMULATION: edit action blocked' in result['stderr']:
                        print(' auto-run script simulated edit blocked',
                              end='  ')
                    else:
                        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 arguments available for all' 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()

            if not enable_autorun_tests and is_autorun:
                return test_skip_script
            return testScript

        for script_name in script_list:
            # force login to be the first, alphabetically, so the login
            # message does not unexpectedly occur during execution of
            # another script.
            # unrunnable script tests are disabled by default in load_tests()

            if script_name == 'login':
                test_name = 'test__' + script_name + '_help'
            else:
                test_name = 'test_' + script_name + '_help'
            # it's explicitly using str() because __name__ must be str
            test_name = str(test_name)
            dct[test_name] = test_execution(script_name, ['-help'])
            if script_name in ['version',
                               'script_wui',      # Failing on travis-ci
                               ] + failed_dep_script_list:
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            dct[test_name].__doc__ = 'Test running ' + script_name + ' -help'
            dct[test_name].__name__ = test_name

            # Ideally all scripts should execute -help without
            # connecting to a site.
            # TODO: after bug 68611 and 68664 (and makecat), split -help
            # execution to a separate test class which uses site=False.

            if script_name == 'login':
                test_name = 'test__' + script_name + '_simulate'
            else:
                test_name = 'test_' + script_name + '_simulate'
            # it's explicitly using str() because __name__ must be str
            test_name = str(test_name)
            dct[test_name] = test_execution(script_name, ['-simulate'],
                                            no_args_expected_results)
            if script_name in ['catall',          # stdout user interaction
                               'flickrripper',    # Requires a flickr api key
                               'script_wui',      # Error on any user except DrTrigonBot
                               'upload',          # raises custom ValueError
                               ] + failed_dep_script_list or (
                    (config.family == 'wikipedia' and script_name == 'disambredir') or
                    (config.family != 'wikidata' and script_name == 'checkimages') or
                    (config.family == 'wikipedia' and config.mylang != 'en' and script_name == 'misspelling')):  # T94681
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            elif script_name in ['watchlist',     # T77965
                                 'lonelypages',   # uses exit code 1; T94680
                                 ]:
                dct[test_name] = allowed_failure(dct[test_name])
            dct[test_name].__doc__ = \
                'Test running ' + script_name + ' -simulate.'
            dct[test_name].__name__ = test_name

            # Disable test by default in nosetests
            if script_name in unrunnable_script_list:
                dct[test_name].__test__ = False

            # TODO: Ideally any script not on the auto_run_script_list
            # can be set as 'not a site' test, but that will require
            # auditing all code in main() to ensure it exits without
            # connecting to a site.  There are outstanding bugs about
            # connections during initialisation.
            #
            # dct[test_name].site = True

        return super(TestScriptMeta, cls).__new__(cls, name, bases, dct)
Ejemplo n.º 3
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=[]):
            is_autorun = '-help' not in args and script_name in auto_run_script_list

            def test_skip_script(self):
                raise unittest.SkipTest(
                    'Skipping execution of auto-run scripts (set '
                    'PYWIKIBOT2_TEST_AUTORUN=1 to enable) "{0}"'.format(script_name))

            def testScript(self):
                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:
                    print(u'\n'.join(stderr_sleep))

                if result['exit_code'] == -9:
                    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 arguments available for all',
                                      result['stdout'])

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

                    if (not result['stdout'] and not result['stderr']):
                        print(' auto-run script unresponsive after %d seconds'
                              % timeout, end=' ')
                    elif 'SIMULATION: edit action blocked' in result['stderr']:
                        print(' auto-run script simulated edit blocked',
                              end='  ')
                    else:
                        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 arguments available for all' 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()

            if not enable_autorun_tests and is_autorun:
                return test_skip_script
            return testScript

        argument = '-' + dct['_argument']

        for script_name in script_list:
            # force login to be the first, alphabetically, so the login
            # message does not unexpectedly occur during execution of
            # another script.
            # unrunnable script tests are disabled by default in load_tests()

            if script_name == 'login':
                test_name = 'test__login'
            else:
                test_name = 'test_' + script_name

            cls.add_method(dct, test_name,
                           test_execution(script_name, [argument]),
                           'Test running %s %s.' % (script_name, argument))

            if script_name in dct['_expected_failures']:
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            elif script_name in dct['_allowed_failures']:
                dct[test_name] = allowed_failure(dct[test_name])

            # Disable test by default in nosetests
            if script_name in unrunnable_script_list:
                # flag them as an expectedFailure due to py.test (T135594)
                dct[test_name] = unittest.expectedFailure(dct[test_name])
                dct[test_name].__test__ = False

        return super(TestScriptMeta, cls).__new__(cls, name, bases, dct)
Ejemplo n.º 4
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=[], expected_results=None):
            def testScript(self):
                cmd = [script_name]

                if args:
                    cmd += args

                data_in = script_input.get(script_name)

                timeout = 0
                if '-help' not in args and script_name in auto_run_script_list:
                    timeout = 5

                if expected_results and script_name in expected_results:
                    error = expected_results[script_name]
                else:
                    error = None

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

                stderr = result['stderr'].split('\n')
                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:
                    print(u'\n'.join(stderr_sleep))

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

                if '-help' in args or error or \
                        script_name not in auto_run_script_list:

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

                        self.assertIn(result['exit_code'], [0, 1, 2, -9])
                    else:
                        if stderr_other == ['']:
                            stderr_other = None
                        self.assertIsNone(stderr_other)
                        self.assertIn('Global arguments available for all',
                                      result['stdout'])

                        self.assertEqual(result['exit_code'], 0)
                else:
                    # auto-run
                    self.assertIn(result['exit_code'], [0, -9])

                    if (not result['stdout'] and not result['stderr']):
                        print(
                            ' auto-run script unresponsive after %d seconds' %
                            timeout,
                            end=' ')
                    elif 'SIMULATION: edit action blocked' in result['stderr']:
                        print(' auto-run script simulated edit blocked',
                              end='  ')
                    else:
                        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 arguments available for all' not in result[
                        'stdout']:
                    # Specifically look for deprecated
                    self.assertNotIn('deprecated', result['stdout'].lower())
                    # But also complain if there is any stdout
                    # but ignore shell.py emiting its '>>> ' prompt.
                    if ((script_name == 'shell'
                         and set(result['stdout']).issubset(set('> \n')))
                            or result['stdout'] == ''):
                        result['stdout'] = None
                    self.assertIsNone(result['stdout'])

                sys.stdout.flush()

            return testScript

        for script_name in script_list:
            # force login to be the first, alphabetically, so the login
            # message does not unexpectedly occur during execution of
            # another script.
            # unrunnable script tests are disabled by default in load_tests()

            if script_name == 'login':
                test_name = 'test__' + script_name + '_help'
            else:
                test_name = 'test_' + script_name + '_help'
            dct[test_name] = test_execution(script_name, ['-help'])
            if script_name in [
                    'version',
                    'script_wui',  # Failing on travis-ci
            ] + failed_dep_script_list:
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            dct[test_name].__doc__ = 'Test running ' + script_name + ' -help'
            dct[test_name].__name__ = test_name

            # Ideally all scripts should execute -help without
            # connecting to a site.
            # TODO: after bug 68611 and 68664 (and makecat), split -help
            # execution to a separate test class which uses site=False.

            if script_name in deadlock_script_list:
                dct[test_name].__test__ = False

            if script_name == 'login':
                test_name = 'test__' + script_name + '_simulate'
            else:
                test_name = 'test_' + script_name + '_simulate'
            dct[test_name] = test_execution(script_name, ['-simulate'],
                                            no_args_expected_results)
            if script_name in [
                    'catall',  # stdout user interaction
                    'checkimages',  # bug 68613
                    'flickrripper',  # Requires a flickr api key
                    'lonelypages',  # uses exit code 1
                    'script_wui',  # Error on any user except DrTrigonBot
                    'upload',  # raises custom ValueError
            ] + failed_dep_script_list or (
                (config.family != 'wikipedia' and script_name == 'lonelypages')
                    or
                (config.family == 'wikipedia' and script_name == 'disambredir')
                    or (config.family == 'wikipedia' and config.mylang != 'en'
                        and script_name == 'misspelling')):
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            elif script_name in [
                    'watchlist',  # T77965
            ]:
                dct[test_name] = allowed_failure(dct[test_name])
            dct[test_name].__doc__ = \
                'Test running ' + script_name + ' -simulate.'
            dct[test_name].__name__ = test_name

            # Disable test by default in nosetests
            if script_name in unrunnable_script_list + deadlock_script_list:
                dct[test_name].__test__ = False

            # TODO: Ideally any script not on the auto_run_script_list
            # can be set as 'not a site' test, but that will require
            # auditing all code in main() to ensure it exits without
            # connecting to a site.  There are outstanding bugs about
            # connections during initialisation.
            #
            # dct[test_name].site = True

        return super(TestScriptMeta, cls).__new__(cls, name, bases, dct)