From c2f5a6c19ca2bc0025c5b2af8a326f3f4bfdced0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Mon, 15 Jul 2024 13:34:20 +0100 Subject: [PATCH] Replace has_program with shutil.which --- beets/test/helper.py | 19 ------------------- test/plugins/test_replaygain.py | 7 ++++--- test/test_importer.py | 10 ++-------- test/test_ui.py | 10 ++-------- 4 files changed, 8 insertions(+), 38 deletions(-) diff --git a/beets/test/helper.py b/beets/test/helper.py index c9b30f619..2ec45677f 100644 --- a/beets/test/helper.py +++ b/beets/test/helper.py @@ -18,8 +18,6 @@ information or mock the environment. - The `control_stdin` and `capture_stdout` context managers allow one to interact with the user interface. -- `has_program` checks the presence of a command on the system. - - The `generate_album_info` and `generate_track_info` functions return fixtures to be used when mocking the autotagger. @@ -34,7 +32,6 @@ from __future__ import annotations import os import os.path import shutil -import subprocess import sys from contextlib import contextmanager from enum import Enum @@ -126,22 +123,6 @@ def _convert_args(args): return args -def has_program(cmd, args=["--version"]): - """Returns `True` if `cmd` can be executed.""" - full_cmd = _convert_args([cmd] + args) - try: - with open(os.devnull, "wb") as devnull: - subprocess.check_call( - full_cmd, stderr=devnull, stdout=devnull, stdin=devnull - ) - except OSError: - return False - except subprocess.CalledProcessError: - return False - else: - return True - - class TestHelper: """Helper mixin for high-level cli and plugin tests. diff --git a/test/plugins/test_replaygain.py b/test/plugins/test_replaygain.py index 92e3e5f65..d37a9a67b 100644 --- a/test/plugins/test_replaygain.py +++ b/test/plugins/test_replaygain.py @@ -13,12 +13,13 @@ # included in all copies or substantial portions of the Software. +import shutil import unittest from mediafile import MediaFile from beets import config -from beets.test.helper import TestHelper, has_program +from beets.test.helper import TestHelper from beetsplug.replaygain import ( FatalGstreamerPluginReplayGainError, GStreamerBackend, @@ -32,12 +33,12 @@ try: except (ImportError, ValueError): GST_AVAILABLE = False -if any(has_program(cmd, ["-v"]) for cmd in ["mp3gain", "aacgain"]): +if shutil.which("mp3gain") or shutil.which("aacgain"): GAIN_PROG_AVAILABLE = True else: GAIN_PROG_AVAILABLE = False -FFMPEG_AVAILABLE = has_program("ffmpeg", ["-version"]) +FFMPEG_AVAILABLE = shutil.which("ffmpeg") def reset_replaygain(item): diff --git a/test/test_importer.py b/test/test_importer.py index fe41ad2f5..9ceeaad32 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -34,13 +34,7 @@ from beets import config, importer, logging, util from beets.autotag import AlbumInfo, AlbumMatch, TrackInfo from beets.importer import albums_in_dir from beets.test import _common -from beets.test.helper import ( - AutotagStub, - ImportHelper, - TestHelper, - capture_log, - has_program, -) +from beets.test.helper import AutotagStub, ImportHelper, TestHelper, capture_log from beets.util import bytestring_path, displayable_path, syspath @@ -327,7 +321,7 @@ class ImportTarTest(ImportZipTest): return path -@unittest.skipIf(not has_program("unrar"), "unrar program not found") +@unittest.skipIf(not shutil.which("unrar"), "unrar program not found") class ImportRarTest(ImportZipTest): def create_archive(self): return os.path.join(_common.RSRC, b"archive.rar") diff --git a/test/test_ui.py b/test/test_ui.py index f7494bafb..aed49fbe0 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -30,12 +30,7 @@ from mediafile import MediaFile from beets import autotag, config, library, plugins, ui, util from beets.autotag.match import distance from beets.test import _common -from beets.test.helper import ( - TestHelper, - capture_stdout, - control_stdin, - has_program, -) +from beets.test.helper import TestHelper, capture_stdout, control_stdin from beets.ui import commands from beets.util import MoveOperation, syspath @@ -1416,6 +1411,7 @@ class PluginTest(_common.TestCase, TestHelper): @_common.slow_test() +@unittest.skipIf(not shutil.which("bash"), "bash not available") class CompletionTest(_common.TestCase, TestHelper): def test_completion(self): # Load plugin commands @@ -1430,8 +1426,6 @@ class CompletionTest(_common.TestCase, TestHelper): # Open a `bash` process to run the tests in. We'll pipe in bash # commands via stdin. cmd = os.environ.get("BEETS_TEST_SHELL", "/bin/bash --norc").split() - if not has_program(cmd[0]): - self.skipTest("bash not available") tester = subprocess.Popen( cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env )