Skip to content

Commit 2059a3a

Browse files
committed
Move EventType definition to plugins.py and fix test_hook types
1 parent 84df87c commit 2059a3a

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

beets/event_types.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

beets/plugins.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from functools import wraps
2525
from pathlib import Path
2626
from types import GenericAlias
27-
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar
27+
from typing import TYPE_CHECKING, Any, ClassVar, Literal, TypeVar
2828

2929
import mediafile
3030
from typing_extensions import ParamSpec
@@ -41,7 +41,6 @@
4141
from beets.dbcore import Query
4242
from beets.dbcore.db import FieldQueryType
4343
from beets.dbcore.types import Type
44-
from beets.event_types import EventType
4544
from beets.importer import ImportSession, ImportTask
4645
from beets.library import Album, Item, Library
4746
from beets.ui import Subcommand
@@ -66,6 +65,37 @@
6665
# Plugins using the Last.fm API can share the same API key.
6766
LASTFM_KEY = "2dc3914abf35f0d9c92d97d8f8e42b43"
6867

68+
EventType = Literal[
69+
"after_write",
70+
"album_imported",
71+
"album_removed",
72+
"albuminfo_received",
73+
"before_choose_candidate",
74+
"before_item_moved",
75+
"cli_exit",
76+
"database_change",
77+
"import",
78+
"import_begin",
79+
"import_task_apply",
80+
"import_task_before_choice",
81+
"import_task_choice",
82+
"import_task_created",
83+
"import_task_files",
84+
"import_task_start",
85+
"item_copied",
86+
"item_hardlinked",
87+
"item_imported",
88+
"item_linked",
89+
"item_moved",
90+
"item_reflinked",
91+
"item_removed",
92+
"library_opened",
93+
"mb_album_extract",
94+
"mb_track_extract",
95+
"pluginload",
96+
"trackinfo_received",
97+
"write",
98+
]
6999
# Global logger.
70100
log = logging.getLogger("beets")
71101

test/plugins/test_hook.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ def _get_hook(self, event: str, command: str) -> dict[str, str]:
3737

3838

3939
class HookLogsTest(HookTestCase):
40+
HOOK: plugins.EventType = "write"
41+
4042
@contextmanager
4143
def _configure_logs(self, command: str) -> Iterator[list[str]]:
42-
config = {"hooks": [self._get_hook("test_event", command)]}
44+
config = {"hooks": [self._get_hook(self.HOOK, command)]}
4345

4446
with self.configure_plugin(config), capture_log("beets.hook") as logs:
45-
plugins.send("test_event")
47+
plugins.send(self.HOOK)
4648
yield logs
4749

4850
def test_hook_empty_command(self):
@@ -53,13 +55,13 @@ def test_hook_empty_command(self):
5355
@unittest.skipIf(sys.platform == "win32", "win32")
5456
def test_hook_non_zero_exit(self):
5557
with self._configure_logs('sh -c "exit 1"') as logs:
56-
assert "hook: hook for test_event exited with status 1" in logs
58+
assert f"hook: hook for {self.HOOK} exited with status 1" in logs
5759

5860
def test_hook_non_existent_command(self):
5961
with self._configure_logs("non-existent-command") as logs:
6062
logs = "\n".join(logs)
6163

62-
assert "hook: hook for test_event failed: " in logs
64+
assert f"hook: hook for {self.HOOK} failed: " in logs
6365
# The error message is different for each OS. Unfortunately the text is
6466
# different in each case, where the only shared text is the string
6567
# 'file' and substring 'Err'
@@ -68,13 +70,11 @@ def test_hook_non_existent_command(self):
6870

6971

7072
class HookCommandTest(HookTestCase):
71-
TEST_HOOK_COUNT = 2
72-
73-
events = [f"test_event_{i}" for i in range(TEST_HOOK_COUNT)]
73+
EVENTS: list[plugins.EventType] = ["write", "after_write"]
7474

7575
def setUp(self):
7676
super().setUp()
77-
self.paths = [str(self.temp_dir_path / e) for e in self.events]
77+
self.paths = [str(self.temp_dir_path / e) for e in self.EVENTS]
7878

7979
def _test_command(
8080
self,
@@ -93,13 +93,14 @@ def _test_command(
9393
2. Assert that a file has been created under the original path, which proves
9494
that the configured hook command has been executed.
9595
"""
96+
events_with_paths = list(zip(self.EVENTS, self.paths))
9697
hooks = [
9798
self._get_hook(e, f"touch {make_test_path(e, p)}")
98-
for e, p in zip(self.events, self.paths)
99+
for e, p in events_with_paths
99100
]
100101

101102
with self.configure_plugin({"hooks": hooks}):
102-
for event, path in zip(self.events, self.paths):
103+
for event, path in events_with_paths:
103104
if send_path_kwarg:
104105
plugins.send(event, path=path)
105106
else:

0 commit comments

Comments
 (0)