From 4b5eb163ba7591207aa7a26d813b0bfcd61c32ab Mon Sep 17 00:00:00 2001 From: dbxnr Date: Sat, 21 Mar 2020 22:05:57 +0000 Subject: [PATCH] Fix for upgrade with missing journal (#796) * Fix for upgrade with missing journal * add test, refactor solution * add missing test config Co-authored-by: Jonathan Wren --- ...om_195_with_missing_encrypted_journal.json | 11 ++++++ ...upgrade_from_195_with_missing_journal.json | 11 ++++++ features/upgrade.feature | 18 ++++++++++ jrnl/upgrade.py | 34 ++++++++++++++++--- 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 features/data/configs/upgrade_from_195_with_missing_encrypted_journal.json create mode 100644 features/data/configs/upgrade_from_195_with_missing_journal.json diff --git a/features/data/configs/upgrade_from_195_with_missing_encrypted_journal.json b/features/data/configs/upgrade_from_195_with_missing_encrypted_journal.json new file mode 100644 index 000000000..5bbfb5b19 --- /dev/null +++ b/features/data/configs/upgrade_from_195_with_missing_encrypted_journal.json @@ -0,0 +1,11 @@ +{ +"default_hour": 9, +"timeformat": "%Y-%m-%d %H:%M", +"linewrap": 80, +"encrypt": true, +"editor": "", +"default_minute": 0, +"highlight": true, +"journals": {"default": "features/journals/encrypted_jrnl-1-9-5.journal", "missing": "features/journals/missing.journal"}, +"tagsymbols": "@" +} diff --git a/features/data/configs/upgrade_from_195_with_missing_journal.json b/features/data/configs/upgrade_from_195_with_missing_journal.json new file mode 100644 index 000000000..8d456159b --- /dev/null +++ b/features/data/configs/upgrade_from_195_with_missing_journal.json @@ -0,0 +1,11 @@ +{ +"default_hour": 9, +"timeformat": "%Y-%m-%d %H:%M", +"linewrap": 80, +"encrypt": false, +"editor": "", +"default_minute": 0, +"highlight": true, +"journals": {"default": "features/journals/simple_jrnl-1-9-5.journal", "missing": "features/journals/missing.journal"}, +"tagsymbols": "@" +} diff --git a/features/upgrade.feature b/features/upgrade.feature index e43779708..e05d43511 100644 --- a/features/upgrade.feature +++ b/features/upgrade.feature @@ -38,3 +38,21 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x """ Then the journal should have 2 entries + Scenario: Upgrade with missing journal + Given we use the config "upgrade_from_195_with_missing_journal.json" + When we run "jrnl -ls" and enter + """" + Y + """ + Then the output should contain "Error: features/journals/missing.journal does not exist." + + Scenario: Upgrade with missing encrypted journal + Given we use the config "upgrade_from_195_with_missing_encrypted_journal.json" + When we run "jrnl -ls" and enter + """ + Y + bad doggie no biscuit + bad doggie no biscuit + """ + Then the output should contain "Error: features/journals/missing.journal does not exist." + and the output should contain "We're all done here" diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index e252d2d07..fe0e0f4b6 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -11,10 +11,29 @@ def backup(filename, binary=False): print(f" Created a backup at {filename}.backup", file=sys.stderr) filename = os.path.expanduser(os.path.expandvars(filename)) - with open(filename, "rb" if binary else "r") as original: - contents = original.read() - with open(filename + ".backup", "wb" if binary else "w") as backup: - backup.write(contents) + + try: + with open(filename, "rb" if binary else "r") as original: + contents = original.read() + + with open(filename + ".backup", "wb" if binary else "w") as backup: + backup.write(contents) + except FileNotFoundError: + print(f"\nError: {filename} does not exist.") + try: + cont = util.yesno(f"\nCreate {filename}?", default=False) + if not cont: + raise KeyboardInterrupt + + except KeyboardInterrupt: + raise UserAbort("jrnl NOT upgraded, exiting.") + + +def check_exists(path): + """ + Checks if a given path exists. + """ + return os.path.exists(path) def upgrade_jrnl_if_necessary(config_path): @@ -56,7 +75,11 @@ def upgrade_jrnl_if_necessary(config_path): encrypt = config.get("encrypt") path = journal_conf - path = os.path.expanduser(path) + if os.path.exists(os.path.expanduser(path)): + path = os.path.expanduser(path) + else: + print(f"\nError: {path} does not exist.") + continue if encrypt: encrypted_journals[journal_name] = path @@ -144,6 +167,7 @@ def upgrade_jrnl_if_necessary(config_path): j.write() print("\nUpgrading config...", file=sys.stderr) + backup(config_path) print("\nWe're all done here and you can start enjoying jrnl 2.", file=sys.stderr)