Add ability to use template with --template (#1667)

* Add ability to pass template path with --template

Update jrnl/args.py

* Fix tests
This commit is contained in:
Aaron Lichtman 2023-03-25 11:47:00 -07:00 committed by GitHub
parent 0725ea6b87
commit a2b217fdfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 217 additions and 68 deletions

View file

@ -31,4 +31,47 @@ Feature: Using templates
| basic_onefile.yaml |
| basic_encrypted.yaml |
| basic_folder.yaml |
| basic_dayone.yaml |
| basic_dayone.yaml |
Scenario Outline: --template nonexistent_file should throw an error
Given we use the config "<config_file>"
And we use the password "test" if prompted
When we run "jrnl --template this_template_does_not_exist.template"
Then we should get an error
Then the error output should contain "Unable to find a template file based on the passed arg"
Examples: configs
| config_file |
| basic_onefile.yaml |
| basic_encrypted.yaml |
| basic_folder.yaml |
| basic_dayone.yaml |
Scenario Outline: --template local_filepath should be used in new entry
Given we use the config "<config_file>"
And we use the password "test" if prompted
When we run "jrnl --template features/templates/basic.template"
Then the output should contain "No entry to save, because the template was not changed"
Examples: configs
| config_file |
| basic_onefile.yaml |
| basic_encrypted.yaml |
| basic_folder.yaml |
| basic_dayone.yaml |
Scenario Outline: --template file_in_XDG_templates_dir should be used in new entry
Given we use the config "<config_file>"
And we use the password "test" if prompted
And we copy the template "basic.template" to the default templates folder
When we run "jrnl --template basic.template"
Then the output should contain "No entry to save, because the template was not changed"
Examples: configs
| config_file |
| basic_onefile.yaml |
| basic_encrypted.yaml |
| basic_folder.yaml |
| basic_dayone.yaml |
| basic_dayone.yaml |

View file

@ -89,6 +89,7 @@ def cli_run(
mock_user_input,
mock_overrides,
mock_default_journal_path,
mock_default_templates_path,
):
# Check if we need more mocks
mock_factories.update(mock_args)
@ -98,6 +99,7 @@ def cli_run(
mock_factories.update(mock_config_path)
mock_factories.update(mock_user_input)
mock_factories.update(mock_default_journal_path)
mock_factories.update(mock_default_templates_path)
return {
"status": 0,
@ -179,6 +181,16 @@ def mock_default_journal_path(temp_dir):
}
@fixture
def mock_default_templates_path(temp_dir):
templates_path = Path(temp_dir.name, "templates")
return {
"get_templates_path": lambda: patch(
"jrnl.controller.get_templates_path", return_value=templates_path
),
}
@fixture
def temp_dir():
return tempfile.TemporaryDirectory()

View file

@ -125,6 +125,19 @@ def we_use_the_config(request, temp_dir, working_dir, config_file):
return config_dest
@given(parse('we copy the template "{template_file}" to the default templates folder'), target_fixture="default_templates_path")
def we_copy_the_template(request, temp_dir, working_dir, template_file):
# Move into temp dir as cwd
os.chdir(temp_dir.name) # @todo move this step to a more universal place
# Copy template over
template_source = os.path.join(working_dir, "data", "templates", template_file)
template_dest = os.path.join(temp_dir.name, "templates", template_file)
os.makedirs(os.path.dirname(template_dest), exist_ok=True)
shutil.copy2(template_source, template_dest)
return template_dest
@given(parse('the config "{config_file}" exists'), target_fixture="config_path")
def config_exists(config_file, temp_dir, working_dir):
config_source = os.path.join(working_dir, "data", "configs", config_file)

View file

@ -25,6 +25,11 @@ def should_get_no_error(cli_run):
assert cli_run["status"] == 0, cli_run["status"]
@then("we should get an error")
def should_get_an_error(cli_run):
assert cli_run["status"] != 0, cli_run["status"]
@then(parse("the output should match\n{regex}"))
@then(parse('the output should match "{regex}"'))
def output_should_match(regex, cli_run):

View file

@ -42,6 +42,7 @@ def expected_args(**kwargs):
"strict": False,
"tagged": False,
"tags": False,
"template": None,
"text": [],
"config_override": [],
"config_file_path": "",