def execute_cmds(self, cmds, capture_output=CAPTURE_OUTPUT): """ Execute a list of commands, and capture their output A command can be a string, or a tuple of size 2, 3 or 4. In the latter case, the command is : 1. a string reprensenting the command to execute 2. the user inputs to feed to the command during execution 3. the expected output on stdout, verified with assertEqual. 4. the expected output on stderr, verified with assertEqual. """ try: outs = [] for cmd in cmds: inputs = [] expected_out, expected_err = None, None actual_cmd = cmd if not isinstance(cmd, p3.ustr): actual_cmd = cmd[0] if len(cmd) >= 2 and cmd[1] is not None: # Inputs provided inputs = cmd[1] if len(cmd) >= 3: # Expected output provided capture_output = True if cmd[2] is not None: expected_out = color.undye(cmd[2]) if len(cmd) >= 4 and cmd[ 3] is not None: # Expected error output provided expected_err = color.undye(cmd[3]) # Always set fake input: test should not ask unexpected user input input = fake_env.FakeInput(inputs, [content, uis, p3]) input.as_global() try: if capture_output: capture_wrap = fake_env.capture(pubs_cmd.execute, verbose=PRINT_OUTPUT) _, stdout, stderr = capture_wrap(actual_cmd.split()) actual_out = color.undye(stdout) actual_err = color.undye(stderr) if expected_out is not None: self.assertEqual(p3.u_maybe(actual_out), p3.u_maybe(expected_out)) if expected_err is not None: self.assertEqual(p3.u_maybe(actual_err), p3.u_maybe(expected_err)) outs.append(color.undye(actual_out)) else: pubs_cmd.execute(actual_cmd.split()) except fake_env.FakeInput.UnexpectedInput as e: self.fail('Unexpected input asked by command: {}.'.format( actual_cmd)) return outs except SystemExit as exc: exc_class, exc, tb = sys.exc_info() if sys.version_info.major == 2: # using six to avoid a SyntaxError in Python 3.x six.reraise(FakeSystemExit, FakeSystemExit(*exc.args), tb) else: raise FakeSystemExit(*exc.args).with_traceback(tb)
def execute_cmds(self, cmds, capture_output=CAPTURE_OUTPUT): """ Execute a list of commands, and capture their output A command can be a string, or a tuple of size 2, 3 or 4. In the latter case, the command is : 1. a string reprensenting the command to execute 2. the user inputs to feed to the command during execution 3. the expected output on stdout, verified with assertEqual. 4. the expected output on stderr, verified with assertEqual. """ try: outs = [] for cmd in cmds: inputs = [] expected_out, expected_err = None, None actual_cmd = cmd if not isinstance(cmd, p3.ustr): actual_cmd = cmd[0] if len(cmd) == 2: # Inputs provided inputs = cmd[1] if len(cmd) == 3: # Expected output provided capture_output = True expected_out = color.undye(cmd[2]) if len(cmd) == 4: # Expected error output provided expected_err = color.undye(cmd[3]) # Always set fake input: test should not ask unexpected user input input = fake_env.FakeInput(inputs, [content, uis, p3]) input.as_global() try: if capture_output: _, stdout, stderr = fake_env.redirect(pubs_cmd.execute)( actual_cmd.split()) actual_out = color.undye(stdout) actual_err = color.undye(stderr) if expected_out is not None: self.assertEqual(actual_out, expected_out) if expected_err is not None: self.assertEqual(actual_err, expected_err) outs.append(color.undye(actual_out)) else: pubs_cmd.execute(actual_cmd.split()) except fake_env.FakeInput.UnexpectedInput: self.fail('Unexpected input asked by command: {}.'.format( actual_cmd)) if PRINT_OUTPUT: print(outs) return outs except SystemExit as exc: exc_class, exc, tb = sys.exc_info() if sys.version_info.major == 2: # using six to avoid a SyntaxError in Python 3.x six.reraise(FakeSystemExit, exc, tb) else: raise FakeSystemExit(exc).with_traceback(tb)
def test_oneliner_max_authors(self): decoder = endecoder.EnDecoder() bibdata = decoder.decode_bibdata(bibtex_raw0) for max_authors in [1, 2, 3]: line = 'Page, Lawrence et al. "The PageRank Citation Ranking: Bringing Order to the Web." (1999)' self.assertEqual( color.undye( pretty.bib_oneliner(bibdata['Page99'], max_authors=max_authors)), line) for max_authors in [-1, 0, 4, 5, 10]: line = 'Page, Lawrence and Brin, Sergey and Motwani, Rajeev and Winograd, Terry "The PageRank Citation Ranking: Bringing Order to the Web." (1999)' self.assertEqual( color.undye( pretty.bib_oneliner(bibdata['Page99'], max_authors=max_authors)), line)
def test_oneliner_no_year(self): decoder = endecoder.EnDecoder() bibdata = decoder.decode_bibdata(bibtex_raw0) bibdata['Page99'].pop('year') line = 'Page, Lawrence et al. "The PageRank Citation Ranking: Bringing Order to the Web."' self.assertEqual(color.undye(pretty.bib_oneliner(bibdata['Page99'])), line)
def normalize(s): s = color.undye(s) try: s = s.decode('utf-8') except AttributeError: pass return s
def _normalize(s): """Normalize a string for robust comparisons.""" s = color.undye(s) try: s = s.decode('utf-8') except AttributeError: pass return s
def normalize(s): """Remove color from a string, adjusting for a decode method needed in Python2""" s = color.undye(s) try: s = s.decode('utf-8') except AttributeError: pass return s
def execute_cmds(self, cmds, capture_output=CAPTURE_OUTPUT): """ Execute a list of commands, and capture their output A command can be a string, or a tuple of size 2 or 3. In the latter case, the command is : 1. a string reprensenting the command to execute 2. the user inputs to feed to the command during execution 3. the output expected, verified with assertEqual. Always captures output in this case. """ outs = [] for cmd in cmds: inputs = [] output = None actual_cmd = cmd current_capture_output = capture_output if not isinstance(cmd, p3.ustr): actual_cmd = cmd[0] if len(cmd) == 2: # Inputs provided inputs = cmd[1] if len(cmd) == 3: # Expected output provided current_capture_output = True output = cmd[2] # Always set fake input: test should not ask unexpected user input input = fake_env.FakeInput(inputs, [content, uis, p3]) input.as_global() try: if current_capture_output: _, stdout, stderr = fake_env.redirect(pubs_cmd.execute)( actual_cmd.split()) self.assertEqual(stderr, '') actual_out = color.undye(stdout) if output is not None: correct_out = color.undye(output) self.assertEqual(actual_out, correct_out) outs.append(color.undye(actual_out)) else: pubs_cmd.execute(cmd.split()) except fake_env.FakeInput.UnexpectedInput: self.fail('Unexpected input asked by command: {}.'.format( actual_cmd)) if PRINT_OUTPUT: print(outs) return outs