diff --git a/beetsplug/play.py b/beetsplug/play.py index 4884bc8be..3476e5824 100644 --- a/beetsplug/play.py +++ b/beetsplug/play.py @@ -18,12 +18,12 @@ import shlex import subprocess from os.path import relpath -from tempfile import NamedTemporaryFile from beets import config, ui, util from beets.plugins import BeetsPlugin from beets.ui import Subcommand from beets.ui.commands import PromptChoice +from beets.util import get_temp_filename # Indicate where arguments should be inserted into the command string. # If this is missing, they're placed at the end. @@ -194,15 +194,15 @@ class PlayPlugin(BeetsPlugin): def _create_tmp_playlist(self, paths_list): """Create a temporary .m3u file. Return the filename.""" utf8_bom = config["play"]["bom"].get(bool) - m3u = NamedTemporaryFile("wb", suffix=".m3u", delete=False) + filename = get_temp_filename(__name__, suffix=".m3u") + with open(filename, "wb") as m3u: + if utf8_bom: + m3u.write(b"\xEF\xBB\xBF") - if utf8_bom: - m3u.write(b"\xEF\xBB\xBF") + for item in paths_list: + m3u.write(item + b"\n") - for item in paths_list: - m3u.write(item + b"\n") - m3u.close() - return m3u.name + return filename def before_choose_candidate_listener(self, session, task): """Append a "Play" choice to the interactive importer prompt.""" diff --git a/test/plugins/test_play.py b/test/plugins/test_play.py index cb99f6b43..ac60e8281 100644 --- a/test/plugins/test_play.py +++ b/test/plugins/test_play.py @@ -20,13 +20,16 @@ import sys import unittest from unittest.mock import ANY, patch -from beets.test.helper import TestHelper, control_stdin +from beets.test.helper import CleanupModulesMixin, TestHelper, control_stdin from beets.ui import UserError from beets.util import open_anything +from beetsplug.play import PlayPlugin @patch("beetsplug.play.util.interactive_open") -class PlayPluginTest(unittest.TestCase, TestHelper): +class PlayPluginTest(CleanupModulesMixin, unittest.TestCase, TestHelper): + modules = (PlayPlugin.__module__,) + def setUp(self): self.setup_beets() self.load_plugins("play")