From 8dbbdd2406000c5b840a072d284fe27716b78895 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 9 Sep 2022 13:06:14 -0700 Subject: [PATCH] fix interactive mode, use environment var to change location --- ui/medashare/cli.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index d867b84..ad3f5de 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -774,11 +774,17 @@ class TagCache: fp.write(_asn1coder.dumps(cache)) def _get_paths(options): - identfname = os.path.expanduser('~/.medashare_identity.pasn1') - storefname = os.path.expanduser('~/.medashare_store.sqlite3') - cachefname = os.path.expanduser('~/.medashare_cache.pasn1') + fnames = ( + '.medashare_identity.pasn1', + '.medashare_store.sqlite3', + '.medashare_cache.pasn1' ) - return identfname, storefname, cachefname + if 'MEDASHARE_PATH' in os.environ: + return ( os.path.expanduser( + os.path.join(os.environ['MEDASHARE_PATH'], x)) for x in + fnames ) + + return ( os.path.expanduser('~/' + x) for x in fnames ) def init_datastructs(f): @functools.wraps(f) @@ -1040,8 +1046,6 @@ def checkforfile(objstr, curfile, ask=False): @init_datastructs def cmd_interactive(options, persona, objstr, cache): - cache = get_cache(options) - files = [ pathlib.Path(x) for x in options.files ] autoskip = True @@ -2053,6 +2057,23 @@ class _TestCases(unittest.TestCase): for i in patches: i.stop() + def test_get_paths(self): + # Test to make sure get paths works as expected. + with mock.patch('os.path.expanduser') as eu: + a, b, c = _get_paths(None) + + eu.assert_any_call('~/.medashare_identity.pasn1') + eu.assert_any_call('~/.medashare_store.sqlite3') + eu.assert_any_call('~/.medashare_cache.pasn1') + + pathpref = pathlib.Path('/somepath/somewhere') + with mock.patch.dict(os.environ, dict(MEDASHARE_PATH=str(pathpref))): + i, s, c = _get_paths(None) + + self.assertEqual(i, str(pathpref / '.medashare_identity.pasn1')) + self.assertEqual(s, str(pathpref / '.medashare_store.sqlite3')) + self.assertEqual(c, str(pathpref / '.medashare_cache.pasn1')) + #@unittest.skip('temp') def test_cmds(self): cmds = sorted(self.fixtures.glob('cmd.*.json'))