diff --git a/tests/test_note_append.py b/tests/test_note_append.py index 72909ea..f50843c 100644 --- a/tests/test_note_append.py +++ b/tests/test_note_append.py @@ -1,10 +1,12 @@ # -*- coding: utf-8 -*- """Test appending a note file from the command-line using the '-a' arg""" -# If you store your pubs in a directory other than home, you may want to +# If you store your pubs in a directory other than home, you may want to # set this prior to running this test: # export PUBSCONF=~/.pubsrc +# vim -p tests/test_note_append.py tests/test_usecase.py + from __future__ import print_function, unicode_literals import unittest @@ -39,12 +41,15 @@ class TestNoteAppend(DataCommandTestCase): note_lines.append('bbb') self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) - # # https://github.com/pubs/pubs/pull/201#discussion_r307499310 - # # Test multiword line - # cmds = [('pubs', 'note', 'Page99', '-a', 'xxx yyy',)] - # self.execute_cmds(cmds) - # note_lines.append('xxx yyy') - # self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) + # https://github.com/pubs/pubs/pull/201#discussion_r307499310 + # Test multiword line. + # * Pass the command split into a command and its args to + # execute_cmdsplit, which is called by execute_cmds: + #cmds = [('pubs note Page99 -a "xxx yyy"')] + cmd_split = ['pubs', 'note', 'Page99', '-a', 'xxx yyy'] + self.execute_cmdsplit(cmd_split, expected_out=None, expected_err=None) + note_lines.append('xxx yyy') + self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) # # https://github.com/pubs/pubs/pull/201#discussion_r305274071 # # Test adding Chinese characters diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 33301a4..348366e 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -97,14 +97,6 @@ class CommandTestCase(fake_env.TestFakeFs): 3. the expected output on stdout, verified with assertEqual. 4. the expected output on stderr, verified with assertEqual. """ - def normalize(s): - s = color.undye(s) - try: - s = s.decode('utf-8') - except AttributeError: - pass - return s - try: outs = [] for cmd in cmds: @@ -126,15 +118,8 @@ class CommandTestCase(fake_env.TestFakeFs): 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 = normalize(stdout) - actual_err = normalize(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)) + actual_out = self.execute_cmdsplit( + actual_cmd.split(), expected_out, expected_err) outs.append(color.undye(actual_out)) else: pubs_cmd.execute(actual_cmd.split()) @@ -150,6 +135,29 @@ class CommandTestCase(fake_env.TestFakeFs): else: raise FakeSystemExit(*exc.args).with_traceback(tb) + @staticmethod + 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_cmdsplit(self, actual_cmdlist, expected_out, expected_err): + """Run a single command, which has been split into a list containing cmd and args""" + capture_wrap = fake_env.capture(pubs_cmd.execute, + verbose=PRINT_OUTPUT) + _, stdout, stderr = capture_wrap(actual_cmdlist) + actual_out = self.normalize(stdout) + actual_err = self.normalize(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)) + return actual_out + def tearDown(self): pass