From 634a5004388c96492bd0497e56b58d9da1737e88 Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Thu, 18 Jul 2019 10:15:40 -0400 Subject: [PATCH 1/7] Added '-a' (--append) arg to append a line of text to the notes file. https://github.com/pubs/pubs/issues/200 --- pubs/commands/note_cmd.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 367e799..72ad5c7 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -10,6 +10,7 @@ def parser(subparsers, conf): help='edit the note attached to a paper') parser.add_argument('citekey', help='citekey of the paper', ).completer = CiteKeyCompletion(conf) + parser.add_argument('-a', '--append', help='append a line of text to the notes file', default=None) return parser @@ -18,7 +19,12 @@ def command(conf, args): ui = get_ui() rp = repo.Repository(conf) citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) + append = args.append notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension']) - ui.edit_file(notepath, temporary=False) + if append is None: + ui.edit_file(notepath, temporary=False) + else: + with open(notepath, 'a') as fd: + fd.write(append) NoteEvent(citekey).send() rp.close() From f8508ac5e1f3a2135e59fe5fc8ae68db1728d9df Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Thu, 18 Jul 2019 22:58:17 -0400 Subject: [PATCH 2/7] Thank you very much for considering my pull request. I have added a test: tests/test_note_append.py And have made the isuggested code changes. Please let me know if anything else is needed. https://github.com/pubs/pubs/pull/201 https://github.com/pubs/pubs/issues/200 --- pubs/commands/note_cmd.py | 11 +++++---- tests/test_note_append.py | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/test_note_append.py diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 72ad5c7..43aeb2f 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -3,6 +3,7 @@ from ..uis import get_ui from ..utils import resolve_citekey from ..completion import CiteKeyCompletion from ..events import NoteEvent +from ..content import write_file def parser(subparsers, conf): @@ -10,7 +11,8 @@ def parser(subparsers, conf): help='edit the note attached to a paper') parser.add_argument('citekey', help='citekey of the paper', ).completer = CiteKeyCompletion(conf) - parser.add_argument('-a', '--append', help='append a line of text to the notes file', default=None) + parser.add_argument('-a', '--append', + help='append a line of text to the notes file', default=None) return parser @@ -19,12 +21,11 @@ def command(conf, args): ui = get_ui() rp = repo.Repository(conf) citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) - append = args.append + latestnote = '{TXT}\n'.format(TXT=args.append) notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension']) - if append is None: + if latestnote is None: ui.edit_file(notepath, temporary=False) else: - with open(notepath, 'a') as fd: - fd.write(append) + write_file(notepath, latestnote, 'a') NoteEvent(citekey).send() rp.close() diff --git a/tests/test_note_append.py b/tests/test_note_append.py new file mode 100644 index 0000000..5e03329 --- /dev/null +++ b/tests/test_note_append.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +"""Test appending a note file from the command-line using the '-a' arg""" + +from __future__ import print_function, unicode_literals + +import unittest +import os + +from tests.test_usecase import DataCommandTestCase + + +class TestNoteAppend(DataCommandTestCase): + """Test appending a note file from the command-line using the '-a' arg""" + + def setUp(self, nsec_stat=True): + """Initialize a bib entry containing citation key, Page99, for testing""" + super(TestNoteAppend, self).setUp() + init = ['pubs init', + 'pubs add data/pagerank.bib', + ] + self.execute_cmds(init) + self.note_dir = os.path.join(self.default_pubs_dir, 'notes') + + def test_note_edit(self): + """Test appending the note file using the command-line argument, -a""" + fin_notes = os.path.join(self.note_dir, 'Page99.txt') + # Test adding first line + cmds = [('pubs note Page99 -a aaa')] + self.execute_cmds(cmds) + note_lines = ['aaa'] + self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) + # Test adding additional line + cmds = [('pubs note Page99 -a bbb')] + self.execute_cmds(cmds) + note_lines.append('bbb') + self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) + # Test adding Japanese character + cmds = [('pubs note Page99 -a ソ')] + self.execute_cmds(cmds) + note_lines.append('ソ') + self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) + + @staticmethod + def _get_note_content(note_lines): + """Given a list of note lines, return full note file content""" + return '{LINES}\n'.format(LINES='\n'.join(note_lines)) + + +if __name__ == '__main__': + unittest.main(verbosity=2) From 20b7da7245f72d732b57538ee64f3ed16dbd6fec Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Thu, 18 Jul 2019 23:12:10 -0400 Subject: [PATCH 3/7] Add newline to appended note text after evaluating note text --- pubs/commands/note_cmd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 43aeb2f..6018876 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -21,11 +21,11 @@ def command(conf, args): ui = get_ui() rp = repo.Repository(conf) citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) - latestnote = '{TXT}\n'.format(TXT=args.append) notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension']) - if latestnote is None: + if args.append is None: ui.edit_file(notepath, temporary=False) else: + latestnote = '{TXT}\n'.format(TXT=args.append) write_file(notepath, latestnote, 'a') NoteEvent(citekey).send() rp.close() From dccbadaf734feda9515b49e68385bf458bdf2382 Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Thu, 18 Jul 2019 23:20:20 -0400 Subject: [PATCH 4/7] Comment out Japanese characters for now --- tests/test_note_append.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_note_append.py b/tests/test_note_append.py index 5e03329..7f23f8c 100644 --- a/tests/test_note_append.py +++ b/tests/test_note_append.py @@ -21,7 +21,7 @@ class TestNoteAppend(DataCommandTestCase): self.execute_cmds(init) self.note_dir = os.path.join(self.default_pubs_dir, 'notes') - def test_note_edit(self): + def test_note_append(self): """Test appending the note file using the command-line argument, -a""" fin_notes = os.path.join(self.note_dir, 'Page99.txt') # Test adding first line @@ -34,11 +34,11 @@ class TestNoteAppend(DataCommandTestCase): self.execute_cmds(cmds) note_lines.append('bbb') self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) - # Test adding Japanese character - cmds = [('pubs note Page99 -a ソ')] - self.execute_cmds(cmds) - note_lines.append('ソ') - self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) + # # Test adding Japanese character + # cmds = [('pubs note Page99 -a ソ')] + # self.execute_cmds(cmds) + # note_lines.append('ソ') + # self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) @staticmethod def _get_note_content(note_lines): From 11c74f1ffeac266ef429f16d9c1edf0458a18513 Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Fri, 19 Jul 2019 11:58:41 -0400 Subject: [PATCH 5/7] Lowercase format vars. Add Chinese character test --- pubs/commands/note_cmd.py | 2 +- tests/test_note_append.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 6018876..cb0ddee 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -25,7 +25,7 @@ def command(conf, args): if args.append is None: ui.edit_file(notepath, temporary=False) else: - latestnote = '{TXT}\n'.format(TXT=args.append) + latestnote = '{txt}\n'.format(txt=args.append) write_file(notepath, latestnote, 'a') NoteEvent(citekey).send() rp.close() diff --git a/tests/test_note_append.py b/tests/test_note_append.py index 7f23f8c..e83cbc1 100644 --- a/tests/test_note_append.py +++ b/tests/test_note_append.py @@ -34,6 +34,11 @@ class TestNoteAppend(DataCommandTestCase): self.execute_cmds(cmds) note_lines.append('bbb') self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) + # Test adding Chinese characters + cmds = [('pubs note Page99 -a \347\350\346\345')] + self.execute_cmds(cmds) + note_lines.append('\347\350\346\345') + self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) # # Test adding Japanese character # cmds = [('pubs note Page99 -a ソ')] # self.execute_cmds(cmds) @@ -43,7 +48,7 @@ class TestNoteAppend(DataCommandTestCase): @staticmethod def _get_note_content(note_lines): """Given a list of note lines, return full note file content""" - return '{LINES}\n'.format(LINES='\n'.join(note_lines)) + return '{lines}\n'.format(lines='\n'.join(note_lines)) if __name__ == '__main__': From 5b8eb48cdc34d2c13217374018666fefeab61b3a Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Thu, 25 Jul 2019 17:11:45 -0400 Subject: [PATCH 6/7] Implemented requested changes, leaving the multi-word test and the Japanese/CHinese tests commented for furthre review by senior pubs architect --- tests/test_note_append.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/test_note_append.py b/tests/test_note_append.py index e83cbc1..72909ea 100644 --- a/tests/test_note_append.py +++ b/tests/test_note_append.py @@ -1,6 +1,10 @@ # -*- 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 +# set this prior to running this test: +# export PUBSCONF=~/.pubsrc + from __future__ import print_function, unicode_literals import unittest @@ -34,11 +38,20 @@ class TestNoteAppend(DataCommandTestCase): self.execute_cmds(cmds) note_lines.append('bbb') self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) - # Test adding Chinese characters - cmds = [('pubs note Page99 -a \347\350\346\345')] - self.execute_cmds(cmds) - note_lines.append('\347\350\346\345') - 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_r305274071 + # # Test adding Chinese characters + # cmds = [('pubs note Page99 -a \347\350\346\345')] + # self.execute_cmds(cmds) + # note_lines.append('\347\350\346\345') + # self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines)) # # Test adding Japanese character # cmds = [('pubs note Page99 -a ソ')] # self.execute_cmds(cmds) From b75d77bf2499df607fb0f5fb8b04eb2718fd2165 Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Sun, 28 Jul 2019 15:11:13 -0400 Subject: [PATCH 7/7] Test appending a multi-word line into a bib note. https://github.com/pubs/pubs/pull/201#discussion_r305274762 --- tests/test_note_append.py | 19 +++++++++++------- tests/test_usecase.py | 42 +++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 24 deletions(-) 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