Debian Codegraph @

test/plugins/test_hook.py

raw
1 from __future__ import annotations
2
3 import os
4 import sys
5 from pathlib import Path
6 from typing import TYPE_CHECKING, ClassVar
7
8 import pytest
9
10 from beets import plugins
11 from beets.test.helper import PluginTestHelper
12
13 if TYPE_CHECKING:
14 from collections.abc import Callable
15
16 from beets.events import EventType
17
18
19 class HookTestCase(PluginTestHelper):
20 plugin = "hook"
21 preload_plugin = False
22
23 def _get_hook(self, event: str, command: str) -> dict[str, str]:
24 return {"event": event, "command": command}
25
26
27 class TestHookLogs(HookTestCase):
28 HOOK: EventType = "write"
29
30 def _configure_hook(self, command: str) -> None:
31 config = {"hooks": [self._get_hook(self.HOOK, command)]}
32
33 with self.configure_plugin(config):
34 plugins.send(self.HOOK) # type: ignore[arg-type]
35
36 def test_hook_empty_command(self, caplog: pytest.LogCaptureFixture):
37 with caplog.at_level("DEBUG"):
38 self._configure_hook("")
39
40 assert 'invalid command ""' in caplog.messages
41
42 # FIXME: fails on windows
43 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
44 def test_hook_non_zero_exit(self, caplog: pytest.LogCaptureFixture):
45 with caplog.at_level("DEBUG"):
46 self._configure_hook('sh -c "exit 1"')
47
48 assert f"hook for {self.HOOK} exited with status 1" in caplog.messages
49
50 def test_hook_non_existent_command(self, caplog: pytest.LogCaptureFixture):
51 with caplog.at_level("DEBUG"):
52 self._configure_hook("non-existent-command")
53 assert f"hook for {self.HOOK} failed: " in caplog.text
54 # The error message is different for each OS. Unfortunately the text is
55 # different in each case, where the only shared text is the string
56 # 'file' and substring 'Err'
57 assert "Err" in caplog.text
58 assert "file" in caplog.text
59
60
61 class TestHookCommand(HookTestCase):
62 EVENTS: ClassVar[list[EventType]] = ["write", "after_write"]
63
64 @pytest.fixture(autouse=True)
65 def setUp(self):
66 self.paths = [str(self.temp_path / e) for e in self.EVENTS]
67
68 def _test_command(
69 self,
70 make_test_path: Callable[[str, str], str],
71 send_path_kwarg: bool = False,
72 ) -> None:
73 """Check that each of the configured hooks is executed.
74
75 Configure hooks for each event:
76 1. Use the given 'make_test_path' callable to create a test path from the event
77 and the original path.
78 2. Configure a hook with a command to touch this path.
79
80 For each of the original paths:
81 1. Send a test event
82 2. Assert that a file has been created under the original path, which proves
83 that the configured hook command has been executed.
84 """
85 events_with_paths: list[tuple[EventType, str]] = list(
86 zip(self.EVENTS, self.paths)
87 )
88 hooks = [
89 self._get_hook(e, f"touch {make_test_path(e, p)}")
90 for e, p in events_with_paths
91 ]
92
93 with self.configure_plugin({"hooks": hooks}):
94 for event, path in events_with_paths:
95 if send_path_kwarg:
96 plugins.send(event, path=path) # type: ignore[call-overload]
97 else:
98 plugins.send(event) # type: ignore[arg-type]
99 assert Path(os.fsdecode(path)).is_file()
100
101 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
102 def test_hook_no_arguments(self):
103 self._test_command(lambda _, p: p)
104
105 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
106 def test_hook_event_substitution(self):
107 self._test_command(lambda e, p: p.replace(e, "{event}"))
108
109 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
110 def test_hook_argument_substitution(self):
111 self._test_command(lambda *_: "{path}", send_path_kwarg=True)
112
113 @pytest.mark.skipif(sys.platform == "win32", reason="win32")
114 def test_hook_bytes_interpolation(self):
115 self.paths = [p.encode() for p in self.paths]
116 self._test_command(lambda *_: "{path}", send_path_kwarg=True)