Browse Source

various minor improvements:

make public key a str, instead of binary
add more tests so that it actually creates the database
better error reporting..
main
John-Mark Gurney 2 years ago
parent
commit
e1d195c6b1
1 changed files with 86 additions and 10 deletions
  1. +86
    -10
      ui/medashare/cli.py

+ 86
- 10
ui/medashare/cli.py View File

@@ -471,7 +471,7 @@ class ObjectStore(object):
if j:
return j
else:
raise KeyError('unable to find metadata for file')
raise KeyError('unable to find metadata for file: %s' % repr(fname))

def _readfp(fp):
while True:
@@ -562,7 +562,7 @@ def main():
persona = Persona.load(identfname)

if options.printpub:
print(persona.get_pubkey())
print(persona.get_pubkey().decode('ascii'))
return

persona.new_version(*addprops)
@@ -572,14 +572,28 @@ def main():
storefname = os.path.expanduser('~/.medashare_store.pasn1')
import sys
#print >>sys.stderr, `storefname`
objstr = ObjectStore.load(storefname)
try:
objstr = ObjectStore.load(storefname)
except FileNotFoundError:
identfname = os.path.expanduser('~/.medashare_identity.pasn1')
try:
persona = Persona.load(identfname)
except FileNotFoundError:
print('ERROR: Identity not created, create w/ -g.', file=sys.stderr)
sys.exit(1)

objstr = ObjectStore(persona.get_identity().uuid)

if options.list:
for i in args:
for j in objstr.by_file(i):
#print >>sys.stderr, `j._obj`
for k, v in _iterdictlist(j):
print('%s:\t%s' % (k, v))
try:
for j in objstr.by_file(i):
#print >>sys.stderr, `j._obj`
for k, v in _iterdictlist(j):
print('%s:\t%s' % (k, v))
except KeyError:
print('ERROR: file not found: %s' % repr(i), file=sys.stderr)
sys.exit(1)
elif options.add:
for i in args:
for j in objstr.by_file(i):
@@ -731,7 +745,7 @@ class _TestCases(unittest.TestCase):

def test_enumeratedir(self):
files = enumeratedir(self.tempdir, self.created_by_ref)
ftest = files[0]
ftest = files[1]
fname = 'test.txt'

# make sure that they are of type MDBase
@@ -751,7 +765,7 @@ class _TestCases(unittest.TestCase):
# XXX - make sure works w/ relative dirs
files = enumeratedir(os.path.relpath(self.tempdir),
self.created_by_ref)
self.assertEqual(oldid, files[0].id)
self.assertEqual(oldid, files[1].id)

def test_mdbaseoverlay(self):
objst = ObjectStore(self.created_by_ref)
@@ -967,11 +981,29 @@ class _TestCases(unittest.TestCase):

# setup test fname
testfname = os.path.join(self.tempdir, 'test.txt')
newtestfname = os.path.join(self.tempdir, 'newfile.txt')

import sys
import io
import itertools

with mock.patch('os.path.expanduser', side_effect=expandusermock) \
as eu, mock.patch('medashare.cli.open') as op:
# that when opening the store and identity fails
op.side_effect = FileNotFoundError

# and there is no identity
with mock.patch('sys.stderr', io.StringIO()) as stderr, mock.patch('sys.argv', [ 'progname', '-l', ]) as argv:
with self.assertRaises(SystemExit) as cm:
main()

# that it fails
self.assertEqual(cm.exception.code, 1)

# with the correct error message
self.assertEqual(stderr.getvalue(),
'ERROR: Identity not created, create w/ -g.\n')

with mock.patch('os.path.expanduser', side_effect=expandusermock) \
as eu:
# that generating a new identity
@@ -1038,11 +1070,33 @@ class _TestCases(unittest.TestCase):

# the correct key is printed
self.assertEqual(stdout.getvalue(),
'%s\n' % persona.get_pubkey())
'%s\n' % persona.get_pubkey().decode('ascii'))

# and looked up the correct file
eu.assert_called_with('~/.medashare_identity.pasn1')

# that when a new file is printed
with mock.patch('sys.stderr', io.StringIO()) as stderr, mock.patch('sys.argv', [ 'progname', '-l', newtestfname ]) as argv:
# that it exits
with self.assertRaises(SystemExit) as cm:
main()

# with error code 1
self.assertEqual(cm.exception.code, 1)

# and outputs an error message
self.assertEqual(stderr.getvalue(),
'ERROR: file not found: %s\n' % repr(newtestfname))

# that when a new file has a tag added
with mock.patch('sys.stdout', io.StringIO()) as stdout, mock.patch('sys.argv', [ 'progname', '-a', 'tag', newtestfname ]) as argv:
main()

# nothing is printed
self.assertEqual(stdout.getvalue(), '');

eu.assert_called_with('~/.medashare_store.pasn1')

with mock.patch('sys.stdout', io.StringIO()) as stdout, mock.patch('sys.argv', [ 'progname', '-l', testfname ]) as argv:
main()
self.assertEqual(stdout.getvalue(),
@@ -1080,3 +1134,25 @@ class _TestCases(unittest.TestCase):
main()
self.assertEqual(stdout.getvalue(),
'foo:\tbleh\nhashes:\tsha256:91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ada\nhashes:\tsha512:7d5768d47b6bc27dc4fa7e9732cfa2de506ca262a2749cb108923e5dddffde842bbfee6cb8d692fb43aca0f12946c521cce2633887914ca1f96898478d10ad3f\nlang:\ten\n')

orig_open = open
with mock.patch('os.path.expanduser', side_effect=expandusermock) \
as eu, mock.patch('medashare.cli.open') as op:
# that when the store fails
def open_repl(fname, mode):
self.assertIn(mode, ('rb', 'wb'))

if fname == identfname or mode == 'wb':
return orig_open(fname, mode)

#print('foo:', repr(fname), repr(mode), file=sys.stderr)
raise FileNotFoundError

op.side_effect = open_repl

# and there is no store
with mock.patch('sys.stdout', io.StringIO()) as stdout, mock.patch('sys.argv', [ 'progname', '-l', ]) as argv:
main()

# does not output anything
self.assertEqual(stdout.getvalue(), '')

Loading…
Cancel
Save