Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always expand all paths (journals, templates, etc) #1468

Merged
merged 2 commits into from
May 21, 2022

Conversation

apainintheneck
Copy link
Contributor

@apainintheneck apainintheneck commented May 5, 2022

This also fixed bugs with relative journal and template paths.

Checklist

  • I have read the contributing doc.
  • I have included a link to the relevant issue number.
  • I have checked to ensure there aren't other open pull requests
    for the same issue.
  • I have written new tests for these changes, as needed.

This is related to the discussion in issue #998. I took a look at the path expansion code used throughout the project and decided to create a new path.py file to hold the three common cases.

  1. Getting the path to the home directory.
  2. Expanding shell variables and the user path (~).
  3. Getting the absolute path after expanding shell variables and the user path (~). Note: This used to happen in the opposite order which is probably what caused problems.

Additionally, a call to expand_path is now made before loading template files which solves the problem with loading files like ~/template. I'm still not sure though if the error message after failing to load a template should print the original template path string or the expanded one (line 210 in jrnl/jrnl.py).

Closes #998.

Copy link
Member

@wren wren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Just one comment about the new functions to fix.

Also, would you mind adding a test or two? I think just a couple of unit tests for the new functions should be more than enough.

return os.path.expanduser("~")


def expand_path(path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could do with just home_dir and expand_path as long as expand_path also runs abspath. Would you mind merging expand_path and absolute_path into one function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that at first because I assumed that it wouldn't make a difference but I think that's not a great idea for a couple reasons.

  1. It breaks the tests as currently written. A few BDD tests check the console output of the application and the output that gets parsed contains relative paths. That means that those tests would have to changed to parse the absolute path which is a bit more complicated.
  2. The absolute path is only needed once so it would add extra work to all the other calls.
  3. Console error messages are more readable when they display the relative path instead of the absolute path.

@apainintheneck apainintheneck force-pushed the expand-path branch 4 times, most recently from e8bde7b to 516b005 Compare May 8, 2022 00:01
@apainintheneck
Copy link
Contributor Author

I'm currently running into some problems creating tests that work for both platforms. Essentially, the call to os.path.abspath() internally calls os.getcwd(). My first thought was to just mock that in pytest but that didn't work on Windows for some reason. Then, I decided to use os.getcwd() directly and got FileNotFoundErrors on *nix.

@apainintheneck apainintheneck force-pushed the expand-path branch 2 times, most recently from a3699dc to 7a86bbc Compare May 14, 2022 06:06
This also fixed bugs with relative journal and template paths.
Also, make the tests specific to Windows, Mac & Linux

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
Copy link
Member

@wren wren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and updated the tests to fix the issues you were running into.

The rest looks good. Thank you!

@wren wren added the bug Something isn't working label May 21, 2022
@wren wren changed the title Refactored path expansion calls into a new path.py file Always expand all paths (journals, templates, etc) May 21, 2022
@wren wren merged commit ea6df47 into jrnl-org:develop May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Paths starting with tildes (~) should always expand
2 participants