def _execute(self, args, data_in=None, timeout=0, error=None): site = self.get_site() args = args + ['-family:' + site.family.name, '-code:' + site.code] return execute_pwb(args, data_in, timeout, error)
def test_argv(self): """Test argv of pywikibot. Make sure that argv passed to the script is not contaminated by global options given to pwb.py wrapper. """ script_name = 'print_argv' script_path = join_pwb_tests_path(script_name + '.py') script_opts = ['-help'] command = [script_path] + script_opts without_global_args = execute_pwb(command) with_no_global_args = execute_pwb(['-maxlag:5'] + command) self.assertEqual(without_global_args['stdout'], with_no_global_args['stdout']) self.assertEqual(without_global_args['stdout'].rstrip(), str([script_name] + script_opts))
def _execute(self, args, data_in=None, timeout=0, error=None): from tests.utils import execute_pwb site = self.get_site() args = args + ['-family:' + site.family.name, '-code:' + site.code] return execute_pwb(args, data_in, timeout, error)
def _do_check(self, name): package_name = 'tests.pwb.' + name script_path = join_pwb_tests_path(name + '.py') direct = execute([sys.executable, '-m', package_name]) vpwb = execute_pwb([script_path]) self.maxDiff = None self.assertEqual(direct['stdout'], vpwb['stdout']) return (direct, vpwb)
def _do_check(self, name): package_name = 'tests.pwb.' + name script_path = os.path.join(_pwb_tests_dir, name + '.py') direct = execute([sys.executable, '-m', package_name]) vpwb = execute_pwb([script_path]) self.maxDiff = None self.assertEqual(direct['stdout'], vpwb['stdout']) return (direct, vpwb)
def testScriptEnvironment(self): """ Test environment of pywikibot. Make sure the environment is not contaminated, and is the same as the environment we get when directly running a script. """ direct = execute([sys.executable, '-m', 'tests.pwb.print_locals']) vpwb = execute_pwb([print_locals_test_script]) self.maxDiff = None self.assertEqual(direct['stdout'], vpwb['stdout'])
def test_similar_scripts_found(self): """Test script call which gives multiple similar results.""" result = [ 'ERROR: commons.py not found! Misspelling?', '', 'The most similar scripts are:', '1 - nowcommons', '2 - commonscat', '3 - commons_link', ] stderr = io.StringIO(execute_pwb(['commons'], data_in='q')['stderr']) for line in range(6): with self.subTest(line=line): self.assertEqual(stderr.readline().strip(), result[line])
def test_similar_scripts_found(self): """Test script call which gives multiple similar results.""" result = [ 'ERROR: inter_wikidata.py not found! Misspelling?', '', 'The most similar scripts are:', '1 - interwikidata', '2 - interwiki', ] stderr = io.StringIO( execute_pwb(['inter_wikidata'], data_in='q')['stderr']) for line in result: with self.subTest(line=line): self.assertEqual(stderr.readline().strip(), line)
def test_one_similar_script(self): """Test shell.py script call which gives one similar result.""" result = [ 'ERROR: hello.py not found! Misspelling?', 'NOTE: Starting the most similar script shell.py', 'in 5.0 seconds; type CTRL-C to stop.', ] stream = execute_pwb(['hello'], data_in=chr(3), timeout=10) stderr = io.StringIO(stream['stderr']) with self.subTest(line=0): self.assertEqual(stderr.readline().strip(), result[0]) with self.subTest(line=1): text = stderr.readline().strip() self.assertTrue(text.startswith(result[1]), msg='"{}" does not start with "{}"'.format( text, result[1])) with self.subTest(line=2): self.assertEqual(stderr.readline().strip(), result[2])
def test_script_not_found(self): """Test pwbot.py script call which is not found.""" stderr = io.StringIO(execute_pwb(['pywikibot'])['stderr']) self.assertEqual(stderr.readline().strip(), 'ERROR: pywikibot.py not found! Misspelling?')
def test_script_found(self): """Test pwb.py script call which is found.""" stdout = io.StringIO(execute_pwb(['pwb'])['stdout']) self.assertEqual(stdout.readline().strip(), 'Wrapper script to invoke pywikibot-based scripts.')
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()
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()
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()
def test_script_found(self): """Test pwb.py script call which is found.""" stdout = io.StringIO(execute_pwb(['pwb'])['stdout']) self.assertEqual( stdout.readline().strip(), "Wrapper script to use Pywikibot in 'directory' mode.")
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()
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()