mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-06-27 21:16:14 +02:00
Always expand all paths (journals, templates, etc) (#1468)
* Refactored path expansion calls into a new path.py file This also fixed bugs with relative journal and template paths. * Update tests for new path functions Also, make the tests specific to Windows, Mac & Linux Co-authored-by: Jonathan Wren <jonathan@nowandwren.com> Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
parent
1ce7ce8bfc
commit
ea6df4705c
9 changed files with 172 additions and 21 deletions
|
@ -2,8 +2,10 @@
|
|||
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
from pytest import mark
|
||||
from pytest import skip
|
||||
|
||||
from jrnl.os_compat import on_windows
|
||||
from jrnl.os_compat import on_posix
|
||||
|
||||
|
||||
pytest_plugins = [
|
||||
|
@ -15,11 +17,39 @@ pytest_plugins = [
|
|||
|
||||
|
||||
def pytest_bdd_apply_tag(tag, function):
|
||||
# skip markers
|
||||
if tag == "skip_win":
|
||||
marker = mark.skipif(on_windows(), reason="Skip test on Windows")
|
||||
elif tag == "skip_posix":
|
||||
marker = mark.skipif(on_posix(), reason="Skip test on Mac/Linux")
|
||||
|
||||
# only on OS markers
|
||||
elif tag == "on_win":
|
||||
marker = mark.skipif(not on_windows(), reason="Skip test not on Windows")
|
||||
elif tag == "on_posix":
|
||||
marker = mark.skipif(not on_posix(), reason="Skip test not on Mac/Linux")
|
||||
else:
|
||||
# Fall back to pytest-bdd's default behavior
|
||||
return None
|
||||
|
||||
marker(function)
|
||||
return True
|
||||
|
||||
|
||||
def pytest_runtest_setup(item):
|
||||
markers = [mark.name for mark in item.iter_markers()]
|
||||
|
||||
on_win = on_windows()
|
||||
on_nix = on_posix()
|
||||
|
||||
if "skip_win" in markers and on_win:
|
||||
skip("Skip test on Windows")
|
||||
|
||||
if "skip_posix" in markers and on_nix:
|
||||
skip("Skip test on Mac/Linux")
|
||||
|
||||
if "on_win" in markers and not on_win:
|
||||
skip("Skip test not on Windows")
|
||||
|
||||
if "on_posix" in markers and not on_nix:
|
||||
skip("Skip test not on Mac/Linux")
|
||||
|
|
103
tests/unit/test_path.py
Normal file
103
tests/unit/test_path.py
Normal file
|
@ -0,0 +1,103 @@
|
|||
import pytest
|
||||
import random
|
||||
import string
|
||||
|
||||
from os import getenv
|
||||
from unittest.mock import patch
|
||||
|
||||
from jrnl.path import home_dir
|
||||
from jrnl.path import expand_path
|
||||
from jrnl.path import absolute_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def home_dir_str(monkeypatch):
|
||||
username = "username"
|
||||
monkeypatch.setenv("USERPROFILE", username) # for windows
|
||||
monkeypatch.setenv("HOME", username) # for *nix
|
||||
return username
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def random_test_var(monkeypatch):
|
||||
name = f"JRNL_TEST_{''.join(random.sample(string.ascii_uppercase, 10))}"
|
||||
val = "".join(random.sample(string.ascii_lowercase, 25))
|
||||
monkeypatch.setenv(name, val)
|
||||
return (name, val)
|
||||
|
||||
|
||||
def test_home_dir(home_dir_str):
|
||||
assert home_dir() == home_dir_str
|
||||
|
||||
|
||||
@pytest.mark.on_posix
|
||||
@pytest.mark.parametrize(
|
||||
"path",
|
||||
["~"],
|
||||
)
|
||||
def test_expand_path_actually_expands_mac_linux(path):
|
||||
# makes sure that path isn't being returns as-is
|
||||
assert expand_path(path) != path
|
||||
|
||||
|
||||
@pytest.mark.on_win
|
||||
@pytest.mark.parametrize(
|
||||
"path",
|
||||
["~", "%USERPROFILE%"],
|
||||
)
|
||||
def test_expand_path_actually_expands_windows(path):
|
||||
# makes sure that path isn't being returns as-is
|
||||
assert expand_path(path) != path
|
||||
|
||||
|
||||
@pytest.mark.on_posix
|
||||
@pytest.mark.parametrize(
|
||||
"paths",
|
||||
[
|
||||
["~", "HOME"],
|
||||
],
|
||||
)
|
||||
def test_expand_path_expands_into_correct_value_mac_linux(paths):
|
||||
input_path, expected_path = paths[0], paths[1]
|
||||
assert expand_path(input_path) == getenv(expected_path)
|
||||
|
||||
|
||||
@pytest.mark.on_win
|
||||
@pytest.mark.parametrize(
|
||||
"paths",
|
||||
[
|
||||
["~", "USERPROFILE"],
|
||||
["%USERPROFILE%", "USERPROFILE"],
|
||||
],
|
||||
)
|
||||
def test_expand_path_expands_into_correct_value_windows(paths):
|
||||
input_path, expected_path = paths[0], paths[1]
|
||||
assert expand_path(input_path) == getenv(expected_path)
|
||||
|
||||
|
||||
@pytest.mark.on_posix
|
||||
@pytest.mark.parametrize("_", range(25))
|
||||
def test_expand_path_expands_into_random_env_value_mac_linux(_, random_test_var):
|
||||
var_name, var_value = random_test_var[0], random_test_var[1]
|
||||
assert expand_path(var_name) == var_name
|
||||
assert expand_path(f"${var_name}") == var_value # mac & linux
|
||||
assert expand_path(f"${var_name}") == getenv(var_name)
|
||||
|
||||
|
||||
@pytest.mark.on_win
|
||||
@pytest.mark.parametrize("_", range(25))
|
||||
def test_expand_path_expands_into_random_env_value_windows(_, random_test_var):
|
||||
var_name, var_value = random_test_var[0], random_test_var[1]
|
||||
assert expand_path(var_name) == var_name
|
||||
assert expand_path(f"%{var_name}%") == var_value # windows
|
||||
assert expand_path(f"%{var_name}%") == getenv(var_name)
|
||||
|
||||
|
||||
@patch("jrnl.path.expand_path")
|
||||
@patch("os.path.abspath")
|
||||
def test_absolute_path(mock_abspath, mock_expand_path):
|
||||
test_val = "test_value"
|
||||
|
||||
assert absolute_path(test_val) == mock_abspath.return_value
|
||||
mock_expand_path.assert_called_with(test_val)
|
||||
mock_abspath.assert_called_with(mock_expand_path.return_value)
|
Loading…
Add table
Add a link
Reference in a new issue