Skip to content

Commit

Permalink
Add error-handling to Set Pref and Clear Cache actions
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb531 committed Apr 6, 2024
1 parent 3afaf18 commit 16026b2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
Binary file modified YouVersion Suggest (Alfred 5).alfredworkflow
Binary file not shown.
7 changes: 5 additions & 2 deletions tests/test_clear_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@

import yvs.clear_cache as yvs
from tests import YVSTestCase
from tests.decorators import redirect_stdout


class TestClearCachw(YVSTestCase):

def test_clear_cache(self):
@redirect_stdout
def test_clear_cache(self, out):
"""should remove cache directory when cache is cleared"""
yvs.main()
self.assertFalse(
os.path.exists(yvs.cache.LOCAL_CACHE_DIR_PATH),
"local cache directory exists",
)

def test_clear_cache_silent_fail(self):
@redirect_stdout
def test_clear_cache_silent_fail(self, out):
"""should fail silently if cache directory does not exist"""
shutil.rmtree(yvs.cache.LOCAL_CACHE_DIR_PATH)
yvs.main()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_set_pref.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def test_main(self, out):
}
yvs.main(alfred_variables)
alfred_json = json.loads(out.getvalue())
self.assertEqual(alfred_json["alfredworkflow"]["variables"], alfred_variables)
self.assertEqual(
alfred_json["alfredworkflow"]["variables"], {"did_set_pref": "True"}
)
user_prefs = yvs.core.get_user_prefs()
self.assertEqual(user_prefs["version"], 107)
3 changes: 3 additions & 0 deletions yvs/clear_cache.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/usr/bin/env python3
# coding=utf-8

import json

import yvs.cache as cache


def main():

cache.clear_cache()
print(json.dumps({"alfredworkflow": {"variables": {"did_clear_cache": "True"}}}))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion yvs/set_pref.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def main(variables):
# value_id needs to be parsed as JSON so we can preserve type data for
# proper serialization to the preferences file
set_pref(variables["pref_id"], json.loads(variables["value_id"]))
print(json.dumps({"alfredworkflow": {"variables": variables}}))
print(json.dumps({"alfredworkflow": {"variables": {"did_set_pref": "True"}}}))


if __name__ == "__main__":
Expand Down

0 comments on commit 16026b2

Please sign in to comment.