Extract check for thrird party programs in tests

This commit is contained in:
Thomas Scholtes 2014-04-15 18:23:55 +02:00
parent 4b012e5ddf
commit 770bee3583
3 changed files with 25 additions and 30 deletions

View file

@ -16,6 +16,7 @@ import sys
import os
import os.path
import shutil
import subprocess
from tempfile import mkdtemp, mkstemp
from glob import glob
from contextlib import contextmanager
@ -67,6 +68,19 @@ def capture_stdout():
sys.stdout = org
def has_program(cmd, args=['--version']):
"""Returns `True` if `cmd` can be executed.
"""
try:
with open(os.devnull, 'wb') as devnull:
subprocess.check_call([cmd] + args, stderr=devnull,
stdout=devnull, stdin=devnull)
except OSError:
return False
else:
return True
class TestHelper(object):
"""Helper mixin for high-level cli and plugin tests.

View file

@ -16,11 +16,10 @@
import os
import shutil
from glob import glob
import subprocess
import _common
from _common import unittest
from helper import TestHelper
from helper import TestHelper, has_program
from beets.library import Item, Album
from beets.mediafile import MediaFile
@ -32,6 +31,11 @@ try:
except ImportError, ValueError:
GST_AVAILABLE = False
if any(has_program(cmd, ['-v']) for cmd in ['mp3gain', 'aacgain']):
GAIN_PROG_AVAILABLE = True
else:
GAIN_PROG_AVAILABLE = False
class ReplayGainCliTestBase(TestHelper):
@ -131,26 +135,10 @@ class ReplayGainGstCliTest(ReplayGainCliTestBase, unittest.TestCase):
backend = u'gstreamer'
@unittest.skipIf(not GAIN_PROG_AVAILABLE, 'no *gain command found')
class ReplayGainCmdCliTest(ReplayGainCliTestBase, unittest.TestCase):
backend = u'command'
def setUp(self):
# Check for the backend command.
for command in ['mp3gain', 'aacgain']:
try:
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(
[command, '-v'], stderr=devnull
)
except OSError:
pass
else:
break
else:
self.skipTest('no *gain command found')
super(ReplayGainCmdCliTest, self).setUp()
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)

View file

@ -21,7 +21,7 @@ import subprocess
import _common
from _common import unittest
from helper import capture_stdout
from helper import capture_stdout, has_program
from beets import library
from beets import ui
@ -931,14 +931,10 @@ class CompletionTest(_common.TestCase):
'BASH_COMPLETION_SCRIPT', '/etc/bash_completion'))
# Tests run in bash
shell = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc')
try:
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(shell.split() + ['--version'],
stdout=devnull)
except OSError:
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(shell.split(' '), stdin=subprocess.PIPE,
tester = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
# Load bash_completion
@ -954,9 +950,6 @@ class CompletionTest(_common.TestCase):
completion_script = self.io.getoutput()
self.io.restore()
tester.stdin.writelines(completion_script)
# from beets import plugins
# for cmd in plugins.commands():
# print(cmd.name)
# Load testsuite
with open(test_script, 'r') as test_script: