Notify user when config directory can't be created because there is already a file with the same name (#1134)

* Moving configuration values and methods from install.py to config.py -- everything is broken right now
* Using context to store config path - still lots broken
* Use mocks and context.config_path to store test configs - many tests still broken though
* Update changelog [ci skip]
* Fix jrnl --ls crash
* Fix crash when no editor configured
* Attempt to patch config path with test data - doesn't appear to be working
* Properly use patched config path and add config given to scenario that wasn't using it
* Fix copypasta
* Fix editor test that needed patched config and trapping for system exit
* Add exception and handling for when configuration directory is actually a file
* Remove extraneous comment
* Use more generic JrnlError with messaging switchboard
* Format code a bit nicer
* Remove unnecessary given in diagnostic test
* Ensure full error message is output
* Remove unnecessary whitespace characters
This commit is contained in:
Micah Jerome Ellison 2021-01-02 15:19:44 -08:00 committed by GitHub
parent e0c53c28bb
commit c155bafa84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 182 additions and 97 deletions

View file

@ -19,7 +19,6 @@ import yaml
from jrnl import Journal
from jrnl import __version__
from jrnl import install
from jrnl import plugins
from jrnl.cli import cli
from jrnl.config import load_config
@ -92,25 +91,25 @@ def ushlex(command):
return shlex.split(command, posix=not on_windows)
def read_journal(journal_name="default"):
config = load_config(install.CONFIG_FILE_PATH)
with open(config["journals"][journal_name]) as journal_file:
def read_journal(context, journal_name="default"):
configuration = load_config(context.config_path)
with open(configuration["journals"][journal_name]) as journal_file:
journal = journal_file.read()
return journal
def open_journal(journal_name="default"):
config = load_config(install.CONFIG_FILE_PATH)
journal_conf = config["journals"][journal_name]
def open_journal(context, journal_name="default"):
configuration = load_config(context.config_path)
journal_conf = configuration["journals"][journal_name]
# We can override the default config on a by-journal basis
if type(journal_conf) is dict:
config.update(journal_conf)
configuration.update(journal_conf)
# But also just give them a string to point to the journal file
else:
config["journal"] = journal_conf
configuration["journal"] = journal_conf
return Journal.open_journal(journal_name, config)
return Journal.open_journal(journal_name, configuration)
def read_value_from_string(string):
@ -128,10 +127,11 @@ def read_value_from_string(string):
def set_config(context, config_file):
full_path = os.path.join("features/configs", config_file)
install.CONFIG_FILE_PATH = os.path.abspath(full_path)
context.config_path = os.path.abspath(full_path)
if config_file.endswith("yaml") and os.path.exists(full_path):
# Add jrnl version to file for 2.x journals
with open(install.CONFIG_FILE_PATH, "a") as cf:
with open(context.config_path, "a") as cf:
cf.write("version: {}".format(__version__))
@ -196,11 +196,18 @@ def open_editor_and_enter(context, method, text=""):
with \
patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \
patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \
patch("sys.stdin.isatty", return_value=True) \
patch("sys.stdin.isatty", return_value=True), \
patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \
patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \
:
context.editor = mock_editor
context.getpass = mock_getpass
cli(["--edit"])
try:
cli(["--edit"])
context.exit_status = 0
except SystemExit as e:
context.exit_status = e.code
# fmt: on
@ -314,7 +321,9 @@ def run_with_input(context, command, inputs=""):
patch("builtins.input", side_effect=_mock_input(text)) as mock_input, \
patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \
patch("sys.stdin.read", side_effect=text) as mock_read, \
patch("subprocess.call", side_effect=_mock_editor) as mock_editor \
patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \
patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \
patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \
:
try:
cli(args or [])
@ -396,7 +405,9 @@ def run(context, command, text=""):
patch("sys.argv", args), \
patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \
patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \
patch("sys.stdin.read", side_effect=lambda: text) \
patch("sys.stdin.read", side_effect=lambda: text), \
patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \
patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \
:
context.editor = mock_editor
context.getpass = mock_getpass
@ -544,32 +555,32 @@ def check_not_message(context, text):
@then('the journal should contain "{text}"')
@then('journal "{journal_name}" should contain "{text}"')
def check_journal_content(context, text, journal_name="default"):
journal = read_journal(journal_name)
journal = read_journal(context, journal_name)
assert text in journal, journal
@then('the journal should not contain "{text}"')
@then('journal "{journal_name}" should not contain "{text}"')
def check_not_journal_content(context, text, journal_name="default"):
journal = read_journal(journal_name)
journal = read_journal(context, journal_name)
assert text not in journal, journal
@then("the journal should not exist")
@then('journal "{journal_name}" should not exist')
def journal_doesnt_exist(context, journal_name="default"):
config = load_config(install.CONFIG_FILE_PATH)
configuration = load_config(context.config_path)
journal_path = config["journals"][journal_name]
journal_path = configuration["journals"][journal_name]
assert not os.path.exists(journal_path)
@then("the journal should exist")
@then('journal "{journal_name}" should exist')
def journal_exists(context, journal_name="default"):
config = load_config(install.CONFIG_FILE_PATH)
configuration = load_config(context.config_path)
journal_path = config["journals"][journal_name]
journal_path = configuration["journals"][journal_name]
assert os.path.exists(journal_path)
@ -578,23 +589,23 @@ def journal_exists(context, journal_name="default"):
@then('the config for journal "{journal}" should have "{key}" set to "{value}"')
def config_var(context, key, value="", journal=None):
value = read_value_from_string(value or context.text or "")
config = load_config(install.CONFIG_FILE_PATH)
configuration = load_config(context.config_path)
if journal:
config = config["journals"][journal]
configuration = configuration["journals"][journal]
assert key in config
assert config[key] == value
assert key in configuration
assert configuration[key] == value
@then('the config for journal "{journal}" should not have "{key}" set')
def config_no_var(context, key, value="", journal=None):
config = load_config(install.CONFIG_FILE_PATH)
configuration = load_config(context.config_path)
if journal:
config = config["journals"][journal]
configuration = configuration["journals"][journal]
assert key not in config
assert key not in configuration
@then("the journal should have {number:d} entries")
@ -602,15 +613,15 @@ def config_no_var(context, key, value="", journal=None):
@then('journal "{journal_name}" should have {number:d} entries')
@then('journal "{journal_name}" should have {number:d} entry')
def check_journal_entries(context, number, journal_name="default"):
journal = open_journal(journal_name)
journal = open_journal(context, journal_name)
assert len(journal.entries) == number
@when("the journal directory is listed")
def list_journal_directory(context, journal="default"):
with open(install.CONFIG_FILE_PATH) as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
journal_path = config["journals"][journal]
with open(context.config_path) as config_file:
configuration = yaml.load(config_file, Loader=yaml.FullLoader)
journal_path = configuration["journals"][journal]
for root, dirnames, f in os.walk(journal_path):
for file in f:
print(os.path.join(root, file))