Skip to content

Commit 3b3f315

Browse files
committed
Add tests for keep alive file
1 parent 5f8176f commit 3b3f315

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

src/zorg/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Config(clack.Config):
2424

2525
command: Command
2626

27-
template_pattern_map: dict[Pattern, Path]
27+
template_pattern_map: dict[Pattern, Path] = {}
2828
zettel_dir: Path = Path.home() / "org"
2929

3030

src/zorg/runners.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from functools import partial
56
from pathlib import Path
67
import tempfile
78
from typing import Any, Iterable, Iterator, Mapping
@@ -66,8 +67,12 @@ def _add_paths_to_zdir(
6667

6768

6869
def _start_vim_loop(zo_paths: Iterable[Path], cfg: EditConfig) -> None:
69-
cfg.keep_alive_file.parent.mkdir(parents=True, exist_ok=True)
70-
cfg.keep_alive_file.touch()
70+
zvim = partial(
71+
vimala.vim,
72+
commands=_process_vim_commands(cfg.zettel_dir, cfg.vim_commands),
73+
)
74+
zvim(*zo_paths).unwrap()
75+
7176
logger.debug(
7277
"Vim loop will run as long as the keep alive file exists.",
7378
keep_alive_file=cfg.keep_alive_file,
@@ -90,10 +95,7 @@ def _start_vim_loop(zo_paths: Iterable[Path], cfg: EditConfig) -> None:
9095
paths = last_paths = new_paths
9196

9297
cfg.keep_alive_file.unlink()
93-
vimala.vim(
94-
*_add_paths_to_zdir(cfg.zettel_dir, paths),
95-
commands=_process_vim_commands(cfg.zettel_dir, cfg.vim_commands),
96-
).unwrap()
98+
zvim(*paths).unwrap()
9799

98100

99101
def _process_vim_commands(

tests/common.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from pathlib import Path
56
from typing import Any, Final, Protocol
67

78

@@ -16,6 +17,8 @@
1617
YEST_DD: Final = "02"
1718
TODAY: Final = f"{YYYY}-{MM}-{DD}"
1819

20+
keep_alive_file_path = Path("/tmp/zorg_test_keep_alive")
21+
1922

2023
class MainType(Protocol):
2124
"""Type returned by main() fixture."""

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def main(make_config_file: MakeConfigFile, tmp_path: Path) -> c.MainType:
3232

3333
def inner_main(*args: str, **kwargs: Any) -> int:
3434
kwargs.setdefault("zettel_dir", default_zettel_dir)
35-
kwargs.setdefault("template_pattern_map", {".*": "default.zo"})
35+
kwargs.setdefault("keep_alive_file", c.keep_alive_file_path)
3636

3737
cfg_kwargs = {
3838
k: v if isinstance(v, dict) else str(v)

tests/test_edit_cmd.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from pathlib import Path
6-
from unittest.mock import Mock
6+
from unittest.mock import Mock, call
77

88
from syrupy.assertion import SnapshotAssertion as Snapshot
99

@@ -62,3 +62,51 @@ def test_edit_day_logs(
6262
stderr=None,
6363
timeout=None,
6464
)
65+
66+
67+
def test_whenEmptyKeepAliveFileExists_shouldRestartVim(
68+
main: c.MainType, vim_proc_mock: Mock, tmp_path: Path
69+
) -> None:
70+
"""Test that we can use the keep alive file to have zorg re-run vim."""
71+
zettel_dir = tmp_path / "org"
72+
73+
c.keep_alive_file_path.touch()
74+
exit_code = main("--dir", str(zettel_dir), "edit", "foobar.zo")
75+
76+
assert exit_code == 0
77+
vim_proc_mock.assert_called_with(
78+
["vim", str(zettel_dir / "foobar.zo")],
79+
stdout=None,
80+
stderr=None,
81+
timeout=None,
82+
)
83+
assert vim_proc_mock.call_count == 2
84+
85+
86+
def test_whenKeepAliveFileContainsPaths_useThosePathsOnRestart(
87+
main: c.MainType, vim_proc_mock: Mock, tmp_path: Path
88+
) -> None:
89+
"""Test that we can use the keep alive file to change our vim file args."""
90+
zettel_dir = tmp_path / "org"
91+
92+
c.keep_alive_file_path.write_text("baz.zo buz.zo")
93+
exit_code = main("--dir", str(zettel_dir), "edit", "foobar.zo")
94+
95+
assert exit_code == 0
96+
vim_proc_mock.assert_has_calls([
97+
call(
98+
["vim", str(zettel_dir / "foobar.zo")],
99+
stdout=None,
100+
stderr=None,
101+
timeout=None,
102+
),
103+
call().unwrap(),
104+
call(
105+
["vim", "baz.zo", "buz.zo"],
106+
stdout=None,
107+
stderr=None,
108+
timeout=None,
109+
),
110+
call().unwrap(),
111+
])
112+
assert vim_proc_mock.call_count == 2

0 commit comments

Comments
 (0)