Replace has_program with shutil.which

This commit is contained in:
Šarūnas Nejus 2024-07-15 13:34:20 +01:00
parent c71aceb184
commit c2f5a6c19c
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
4 changed files with 8 additions and 38 deletions

View file

@ -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.

View file

@ -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):

View file

@ -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")

View file

@ -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
)