Skip to content

Commit

Permalink
Properly test symlink update
Browse files Browse the repository at this point in the history
  • Loading branch information
geigerzaehler committed Apr 11, 2024
1 parent a39263a commit a69b59c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
13 changes: 4 additions & 9 deletions beetsplug/alternatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,11 @@ def item_change_actions(self, item: Item, path: bytes, dest: bytes):
"""Returns the necessary actions for items that were previously in the
external collection, but might require metadata updates.
"""
actions = []

if path != dest:
# The path of the link itself changed
actions.append(Action.MOVE)
elif not util.samefile(path, item.path):
# link target changed
actions.append(Action.MOVE)

return actions
if path != dest or not util.samefile(os.readlink(path), item.path):
return [Action.MOVE]
else:
return []

@override
def update(self, create=None):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typeguard = "^4.1.5"
typing-extensions = "^4.9.0"

[tool.pytest.ini_options]
addopts = "--cov --cov-report=term --cov-report=html"
addopts = "--cov --cov-report=term --cov-report=html --cov-branch"
filterwarnings = [
"error",
"ignore:.*imghdr.*:DeprecationWarning:mediafile",
Expand Down
42 changes: 11 additions & 31 deletions test/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,53 +173,33 @@ def test_add_move_remove_album_relative(self):
self.alt_config["link_type"] = "relative"
self.test_add_move_remove_album(absolute=False)

def test_add_update_move_album(self):
"""Test that symlinks are properly updated and no broken links left
when an item's path in the library changes.
Since moving the items causes the links in the symlink view to be
broken, this situation used to be incorrectly detected as
addition of new items, such that the old links weren't removed.
Contrast this to the `test_add_move_remove_album` test, in which the
old links do not break upon changing the path format.
* An album is added.
* The album name is changed, which also causes the tracks to be moved.
* The symlink view is updated.
"""
def test_update_link_target(self, tmp_path: Path):
"""Link targets are updated when the item has moved in the library"""

self.add_album(artist="Michael Jackson", album="Thriller", year="1990")

self.runcli("alt", "update", "by-year")

by_year_path = self.libdir / "by-year/1990/Thriller/track 1.mp3"
alt_path = self.libdir / "by-year/1990/Thriller/track 1.mp3"
assert_symlink(
link=by_year_path,
link=alt_path,
target=self.libdir / "Michael Jackson/Thriller/track 1.mp3",
absolute=True,
)

# `-y` skips the prompt, `-a` updates album-level fields, `-m` forces
# actually moving the files
self.runcli("mod", "-y", "-a", "-m", "Thriller", "album=Thriller (Remastered)")
new_libdir = tmp_path / "newlib"
new_libdir.mkdir()
self.runcli("move", "-a", "-d", str(new_libdir), "Thriller")
self.runcli("alt", "update", "by-year")

assert_symlink(
link=self.libdir / "by-year/1990/Thriller (Remastered)/track 1.mp3",
target=self.libdir / "Michael Jackson/Thriller (Remastered)/track 1.mp3",
link=alt_path,
target=new_libdir / "Michael Jackson/Thriller/track 1.mp3",
absolute=True,
)

def test_valid_options(self):
"""Test that an error is raised when option is invalid
* Config link type is invalid
* An album is added
* A confuse.ConfigValueError is raised
"""
def test_invalid_link_type(self):
self.alt_config["link_type"] = "Hylian"
self.add_album(
artist="Michael Jackson",
album="Thriller",
year="1990",
original_year="1982",
)

with pytest.raises(ConfigValueError):
self.runcli("alt", "update", "by-year")
Expand Down

0 comments on commit a69b59c

Please sign in to comment.