From f8508ac5e1f3a2135e59fe5fc8ae68db1728d9df Mon Sep 17 00:00:00 2001 From: dvklopfenstein Date: Thu, 18 Jul 2019 22:58:17 -0400 Subject: [PATCH] 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)