Skip to content

Commit

Permalink
[#796] fixed DB not being passed to BukuCrypt from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
LeXofLeviafan committed Nov 16, 2024
1 parent bfe195e commit b506b75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions buku
Original file line number Diff line number Diff line change
Expand Up @@ -6051,7 +6051,7 @@ POSITIONAL ARGUMENTS:
# Fix uppercase tags allowed in releases before v2.7
addarg('--fixtags', action='store_true', help=hide)
# App-use only, not for manual usage
addarg('--db', nargs=1, help=hide)
addarg('--db', nargs=1, default=[None], help=hide)

# Parse the arguments
args = argparser.parse_args(argv)
Expand Down Expand Up @@ -6129,9 +6129,9 @@ POSITIONAL ARGUMENTS:

# Handle encrypt/decrypt options at top priority
if args.lock is not None:
BukuCrypt.encrypt_file(args.lock)
BukuCrypt.encrypt_file(args.lock, dbfile=args.db[0])
elif args.unlock is not None:
BukuCrypt.decrypt_file(args.unlock)
BukuCrypt.decrypt_file(args.unlock, dbfile=args.db[0])

order = [s for ss in (args.order or []) for s in re.split(r'\s*,\s*', ss.strip()) if s]

Expand Down Expand Up @@ -6184,7 +6184,7 @@ POSITIONAL ARGUMENTS:
args.json,
args.format,
not args.tacit,
dbfile=args.db[0] if args.db is not None else None,
dbfile=args.db[0],
colorize=not args.nc
)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,28 @@ def test_random_export(_print_rec_with_filter, _sample, bdb, stdin, prompt, rand
calls += [mock.call.bdb.exportdb('export.md', res)]
calls += [mock.call.bdb.close_quit(0)]
assert wrap.mock_calls == calls


@pytest.mark.parametrize('db', [None, './foo.db'])
@pytest.mark.parametrize('action', ['print', 'lock', 'unlock'])
@mock.patch('buku.BukuCrypt')
def test_custom_db(_BukuCrypt, BukuDb, stdin, db, action):
wrap = mock.Mock()
wrap.attach_mock(BukuDb, 'BukuDb')
wrap.attach_mock(BukuDb.return_value, 'bdb')
wrap.attach_mock(_BukuCrypt, 'BukuCrypt')
BukuDb.return_value.get_max_id.return_value = None
argv = ['--nostdin'] + ([] if not db else ['--db', db]) + [f'--{action}']
with pytest.raises(SystemExit):
buku.main(argv)
calls = []
if action == 'lock':
calls += [mock.call.BukuCrypt.encrypt_file(8, dbfile=db)]
elif action == 'unlock':
calls += [mock.call.BukuCrypt.decrypt_file(8, dbfile=db)]
calls += [mock.call.BukuDb(None, 0, True, dbfile=db, colorize=True)]
if action == 'print':
calls += [mock.call.bdb.get_max_id(),
mock.call.bdb.print_rec(None, order=[])]
calls += [mock.call.bdb.close_quit(0)]
assert wrap.mock_calls == calls

0 comments on commit b506b75

Please sign in to comment.