From 2181d845450026a941d47513bd7c42e88b76abe8 Mon Sep 17 00:00:00 2001 From: dbxnr Date: Tue, 11 Feb 2020 16:32:38 +0000 Subject: [PATCH] add test, refactor solution --- features/upgrade.feature | 29 ++++++++++++++++------------- jrnl/upgrade.py | 13 ++++++++++++- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/features/upgrade.feature b/features/upgrade.feature index cbbb2d99..9e4948bb 100644 --- a/features/upgrade.feature +++ b/features/upgrade.feature @@ -1,6 +1,5 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x - @skip_win Scenario: Upgrade and parse journals with square brackets Given we use the config "upgrade_from_195.json" When we run "jrnl -9" and enter "Y" @@ -12,7 +11,6 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x """ Then the journal should have 2 entries - @skip_win Scenario: Upgrading a journal encrypted with jrnl 1.x Given we use the config "encrypted_old.json" When we run "jrnl -n 1" and enter @@ -24,7 +22,6 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x Then the output should contain "Password" and the output should contain "2013-06-10 15:40 Life is good" - @skip_win Scenario: Upgrade and parse journals with little endian date format Given we use the config "upgrade_from_195_little_endian_dates.json" When we run "jrnl -9" and enter "Y" @@ -36,15 +33,21 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x """ Then the journal should have 2 entries - @skip_win - Scenario: Successful upgrade with missing journal + 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 Y" - Then we should see the message "Journal 'missing' created" - Then we should see the message "We're all done here" + When we run "jrnl -ls" and enter + """" + Y + """ + Then the output should contain "Error: features/journals/missing.journal does not exist." - @skip_win - Scenario: Aborted upgrade with missing journal - Given we use the config "upgrade_from_195_with_missing_journal.json" - When we run "jrnl -ls" and enter "Y N" - Then we should see the message "jrnl NOT upgraded, exiting." \ No newline at end of file + 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" \ No newline at end of file diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index a630d3d4..fe0e0f4b 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -29,6 +29,13 @@ def backup(filename, binary=False): 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): with open(config_path, "r", encoding="utf-8") as f: config_file = f.read() @@ -68,7 +75,11 @@ older versions of jrnl anymore. 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