Esempio 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)
Esempio n. 2
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=[], expected_results=None):
            def testScript(self):
                cmd = [sys.executable, pwb_path, 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(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, -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
                    if 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',
                               'data_ingestion',  # bug 68611
                               'replicate_wiki',  # bug 68664
                               '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.  However pywikibot always
            # logs site.version() from live wiki.
            # TODO: make logging version() optional, then set
            #         dct[test_name].site = True
            #       for only the tests which dont respond to -help

            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
                               'data_ingestion',  # bug 68611
                               'flickrripper',    # Requires a flickr api key
                               'lonelypages',     # custom return codes
                               'nowcommons',      # deprecation warning
                               'replicate_wiki',  # custom return codes
                               'script_wui',      # Error on any user except DrTrigonBot
                               'upload',          # raises custom ValueError
                               ] + failed_dep_script_list or (
                    ((config.family != 'wikipedia' or config.mylang != 'en') and script_name == 'cfd') 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])
            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)
Esempio 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)
Esempio n. 4
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=None, expected_results=None):
            def testScript(self):
                cmd = [sys.executable, pwb_path, script_name]

                if args:
                    cmd += args

                data_in = script_input.get(script_name)

                timeout = 0
                if script_name in auto_run_script_list:
                    timeout = 5
                result = execute(cmd, data_in, timeout=timeout)

                if expected_results and script_name in expected_results:
                    if expected_results[script_name] is not None:
                        self.assertIn(expected_results[script_name],
                                      result['stderr'])
                elif (args and '-help' in args) or \
                        script_name not in auto_run_script_list:
                    stderr = [
                        l for l in result['stderr'].split('\n')
                        if not l.startswith('Sleeping for ')
                    ]
                    pywikibot.output('\n'.join([
                        l for l in result['stderr'].split('\n')
                        if l.startswith('Sleeping for ')
                    ]))
                    self.assertEqual('\n'.join(stderr), '')
                    self.assertIn('Global arguments available for all',
                                  result['stdout'])
                    self.assertEqual(result['exit_code'], 0)
                self.assertNotIn('Traceback (most recent call last)',
                                 result['stderr'])

            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 + '_execution'
            else:
                test_name = 'test_' + script_name + '_execution'
            dct[test_name] = test_execution(script_name, ['-help'])
            if script_name in [
                    'shell',
                    'version',
                    'data_ingestion',  # bug 68611
                    'flickrripper',  # bug 68606 (and others)
                    'replicate_wiki',  # bug 68664
                    'script_wui',  # Failing on travis-ci
            ]:
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            dct[test_name].__doc__ = 'Test running ' + script_name + '.'
            dct[test_name].__name__ = test_name

            # Ideally all scripts should execute -help without
            # connecting to a site.  However pywikibot always
            # logs site.version() from live wiki.
            # TODO: make logging version() optional, then set
            #         dct[test_name].site = True
            #       for only the tests which dont respond to -help

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

            if script_name == 'login':
                test_name = 'test__' + script_name + '_no_args'
            else:
                test_name = 'test_' + script_name + '_no_args'
            dct[test_name] = test_execution(script_name, ['-simulate'],
                                            no_args_expected_results)
            if script_name in [
                    'checkimages',  # bug 68613
                    'data_ingestion',  # bug 68611
                    'flickrripper',  # bug 68606 (and deps)
                    'script_wui',  # Error on any user except DrTrigonBot
                    'upload',  # raises custom ValueError
            ] or (((config.family != 'wikipedia' or config.mylang != 'en')
                   and script_name == 'cfd') 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])
            dct[test_name].__doc__ = \
                'Test running ' + script_name + ' without arguments.'
            dct[test_name].__name__ = test_name

            # Disable test bt 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 type.__new__(cls, name, bases, dct)
Esempio n. 5
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 '
                    'PYWIKIBOT_TEST_AUTORUN=1 to enable) "{0}"'.format(
                        script_name))

            def testScript(self):
                global_args = 'Global arguments available for all'

                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'] = '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
                    exit_codes = [0, -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='  ')

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

            if not enable_autorun_tests and is_autorun:
                return test_skip_script
            return testScript

        arguments = dct['_arguments']

        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, arguments.split()),
                'Test running {} {}.'.format(script_name, arguments))

            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] = unittest.skip(
                    '{} is in _allowed_failures list'.format(script_name))(
                        dct[test_name])
            elif script_name in failed_dep_script_set \
                    and arguments == '-simulate':
                dct[test_name] = unittest.skip(
                    '{} has dependencies; skipping'.format(script_name))(
                        dct[test_name])

            # Disable test by default in nosetests
            if script_name in unrunnable_script_set:
                # 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)
Esempio n. 6
0
    def __new__(cls, name, bases, dct):
        """Create the new class."""
        def test_execution(script_name, args=None, expected_results=None):
            def testScript(self):
                cmd = [sys.executable, pwb_path, script_name]

                if args:
                    cmd += args

                data_in = script_input.get(script_name)

                timeout = 0
                if script_name in auto_run_script_list:
                    timeout = 5
                result = execute(cmd, data_in, timeout=timeout)

                if expected_results and script_name in expected_results:
                    if expected_results[script_name] is not None:
                        self.assertIn(expected_results[script_name],
                                      result['stderr'])
                elif (args and '-help' in args) or \
                        script_name not in auto_run_script_list:
                    stderr = [l for l in result['stderr'].split('\n')
                              if not l.startswith('Sleeping for ')]
                    pywikibot.output('\n'.join(
                        [l for l in result['stderr'].split('\n')
                         if l.startswith('Sleeping for ')]))
                    self.assertEqual('\n'.join(stderr), '')
                    self.assertIn('Global arguments available for all',
                                  result['stdout'])
                    self.assertEqual(result['exit_code'], 0)
                self.assertNotIn('Traceback (most recent call last)',
                                 result['stderr'])
            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 + '_execution'
            else:
                test_name = 'test_' + script_name + '_execution'
            dct[test_name] = test_execution(script_name, ['-help'])
            if script_name in ['shell', 'version',
                               'data_ingestion',  # bug 68611
                               'flickrripper',    # bug 68606 (and others)
                               'replicate_wiki',  # bug 68664
                               'script_wui',      # Failing on travis-ci
                               ]:
                dct[test_name] = unittest.expectedFailure(dct[test_name])
            dct[test_name].__doc__ = 'Test running ' + script_name + '.'
            dct[test_name].__name__ = test_name

            # Ideally all scripts should execute -help without
            # connecting to a site.  However pywikibot always
            # logs site.version() from live wiki.
            # TODO: make logging version() optional, then set
            #         dct[test_name].site = True
            #       for only the tests which dont respond to -help

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

            if script_name == 'login':
                test_name = 'test__' + script_name + '_no_args'
            else:
                test_name = 'test_' + script_name + '_no_args'
            dct[test_name] = test_execution(script_name, ['-simulate'],
                                            no_args_expected_results)
            if script_name in ['checkimages',     # bug 68613
                               'data_ingestion',  # bug 68611
                               'flickrripper',    # bug 68606 (and deps)
                               'script_wui',      # Error on any user except DrTrigonBot
                               'upload',          # raises custom ValueError
                               ] or (
                    ((config.family != 'wikipedia' or config.mylang != 'en') and script_name == 'cfd') 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])
            dct[test_name].__doc__ = \
                'Test running ' + script_name + ' without arguments.'
            dct[test_name].__name__ = test_name

            # Disable test bt 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 type.__new__(cls, name, bases, dct)
Esempio n. 7
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)
Esempio n. 8
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)