Merge pull request #201 from dvklopfenstein/master
Added '-a' (--append) arg to append a line of text to the notes file.
This commit is contained in:
commit
2b408fe7ea
@ -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,6 +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)
|
||||
return parser
|
||||
|
||||
|
||||
@ -19,6 +22,10 @@ def command(conf, args):
|
||||
rp = repo.Repository(conf)
|
||||
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
|
||||
notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension'])
|
||||
ui.edit_file(notepath, temporary=False)
|
||||
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()
|
||||
|
73
tests/test_note_append.py
Normal file
73
tests/test_note_append.py
Normal file
@ -0,0 +1,73 @@
|
||||
# -*- 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
|
||||
|
||||
# vim -p tests/test_note_append.py tests/test_usecase.py
|
||||
|
||||
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_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
|
||||
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))
|
||||
|
||||
# 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
|
||||
# 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)
|
||||
# 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)
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user