add test, refactor solution

This commit is contained in:
dbxnr 2020-02-11 16:32:38 +00:00
parent 2bafc8023b
commit 2181d84545
2 changed files with 28 additions and 14 deletions

View file

@ -1,6 +1,5 @@
Feature: Upgrading Journals from 1.x.x to 2.x.x Feature: Upgrading Journals from 1.x.x to 2.x.x
@skip_win
Scenario: Upgrade and parse journals with square brackets Scenario: Upgrade and parse journals with square brackets
Given we use the config "upgrade_from_195.json" Given we use the config "upgrade_from_195.json"
When we run "jrnl -9" and enter "Y" 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 Then the journal should have 2 entries
@skip_win
Scenario: Upgrading a journal encrypted with jrnl 1.x Scenario: Upgrading a journal encrypted with jrnl 1.x
Given we use the config "encrypted_old.json" Given we use the config "encrypted_old.json"
When we run "jrnl -n 1" and enter 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" Then the output should contain "Password"
and the output should contain "2013-06-10 15:40 Life is good" 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 Scenario: Upgrade and parse journals with little endian date format
Given we use the config "upgrade_from_195_little_endian_dates.json" Given we use the config "upgrade_from_195_little_endian_dates.json"
When we run "jrnl -9" and enter "Y" 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 Then the journal should have 2 entries
@skip_win Scenario: Upgrade with missing journal
Scenario: Successful upgrade with missing journal
Given we use the config "upgrade_from_195_with_missing_journal.json" Given we use the config "upgrade_from_195_with_missing_journal.json"
When we run "jrnl -ls" and enter "Y Y" When we run "jrnl -ls" and enter
Then we should see the message "Journal 'missing' created" """"
Then we should see the message "We're all done here" Y
"""
Then the output should contain "Error: features/journals/missing.journal does not exist."
@skip_win Scenario: Upgrade with missing encrypted journal
Scenario: Aborted upgrade with missing journal Given we use the config "upgrade_from_195_with_missing_encrypted_journal.json"
Given we use the config "upgrade_from_195_with_missing_journal.json" When we run "jrnl -ls" and enter
When we run "jrnl -ls" and enter "Y N" """
Then we should see the message "jrnl NOT upgraded, exiting." 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"

View file

@ -29,6 +29,13 @@ def backup(filename, binary=False):
raise UserAbort("jrnl NOT upgraded, exiting.") 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): def upgrade_jrnl_if_necessary(config_path):
with open(config_path, "r", encoding="utf-8") as f: with open(config_path, "r", encoding="utf-8") as f:
config_file = f.read() config_file = f.read()
@ -68,7 +75,11 @@ older versions of jrnl anymore.
encrypt = config.get("encrypt") encrypt = config.get("encrypt")
path = journal_conf 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: if encrypt:
encrypted_journals[journal_name] = path encrypted_journals[journal_name] = path