Debian Codegraph @

test/plugins/test_importsource.py

raw
1 """Tests for the `importsource` plugin."""
2
3 import os
4 import time
5
6 from beets import importer, plugins
7 from beets.test.helper import AutotagImportTestCase, IOMixin, PluginMixin
8
9
10 class ImportSourceTest(IOMixin, PluginMixin, AutotagImportTestCase):
11 plugin = "importsource"
12 preload_plugin = False
13
14 def setUp(self):
15 super().setUp()
16 self.config[self.plugin]["suggest_removal"] = True
17 self.load_plugins()
18 self.prepare_album_for_import(2)
19 self.importer = self.setup_importer()
20 self.importer.add_choice(importer.Action.APPLY)
21 self.importer.run()
22 self.all_items = self.lib.albums().get().items()
23 self.item_to_remove = self.all_items[0]
24
25 def interact(self, stdin: list[str]):
26 for char in stdin:
27 self.io.addinput(char)
28 self.run_command("remove", f"path:{self.item_to_remove.filepath}")
29
30 def test_do_nothing(self):
31 self.interact(["N"])
32
33 assert os.path.exists(self.item_to_remove.source_path)
34
35 def test_remove_single(self):
36 self.interact(["y", "D"])
37
38 assert not os.path.exists(self.item_to_remove.source_path)
39
40 def test_remove_all_from_single(self):
41 self.interact(["y", "R", "y"])
42
43 for item in self.all_items:
44 assert not os.path.exists(item.source_path)
45
46 def test_stop_suggesting(self):
47 self.interact(["y", "S"])
48
49 for item in self.all_items:
50 assert os.path.exists(item.source_path)
51
52 def test_source_path_attribute_written(self):
53 """Test that source_path attribute is correctly written to imported items.
54
55 The items should already have source_path from the setUp import
56 """
57 for item in self.all_items:
58 assert "source_path" in item
59 assert item.source_path # Should not be empty
60
61 def test_source_files_not_modified_during_import(self):
62 """Test that source files timestamps are not changed during import."""
63 # Prepare fresh files and record timestamps
64 test_album_path = self.import_path / "test_album"
65 import_paths = self.prepare_album_for_import(
66 2, album_path=test_album_path
67 )
68 original_mtimes = {p: p.stat().st_mtime for p in import_paths}
69
70 # Small delay to detect timestamp changes
71 time.sleep(0.1)
72
73 # Run a fresh import
74 importer_session = self.setup_importer()
75 importer_session.add_choice(importer.Action.APPLY)
76 importer_session.run()
77
78 # Verify timestamps haven't changed
79 for path, original_mtime in original_mtimes.items():
80 current_mtime = path.stat().st_mtime
81 assert current_mtime == original_mtime, (
82 f"Source file timestamp changed: {path}"
83 )
84
85 def test_prevent_suggest_removal_on_reimport(self):
86 """Test that removal suggestions are prevented during reimport."""
87 album = self.lib.albums().get()
88 mb_albumid = album.mb_albumid
89
90 # Reimport from library
91 reimporter = self.setup_importer(import_dir=self.lib_path)
92 reimporter.add_choice(importer.Action.APPLY)
93 reimporter.run()
94
95 plugin = plugins._instances[0]
96 assert mb_albumid in plugin.stop_suggestions_for_albums
97
98 # Calling suggest_removal should exit early without prompting
99 item = self.lib.items().get()
100 plugin.suggest_removal(item)
101 assert os.path.exists(item.source_path)
102
103 def test_prevent_suggest_removal_handles_skipped_task(self):
104 """Test that skipped tasks don't crash prevent_suggest_removal."""
105
106 class MockTask:
107 skip = True
108
109 plugin = plugins._instances[0]
110 mock_task = MockTask()
111 plugin.prevent_suggest_removal(None, mock_task)