Skip to content

Commit 770bee3

Browse files
author
Thomas Scholtes
committed
Extract check for thrird party programs in tests
1 parent 4b012e5 commit 770bee3

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

test/helper.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717
import os.path
1818
import shutil
19+
import subprocess
1920
from tempfile import mkdtemp, mkstemp
2021
from glob import glob
2122
from contextlib import contextmanager
@@ -67,6 +68,19 @@ def capture_stdout():
6768
sys.stdout = org
6869

6970

71+
def has_program(cmd, args=['--version']):
72+
"""Returns `True` if `cmd` can be executed.
73+
"""
74+
try:
75+
with open(os.devnull, 'wb') as devnull:
76+
subprocess.check_call([cmd] + args, stderr=devnull,
77+
stdout=devnull, stdin=devnull)
78+
except OSError:
79+
return False
80+
else:
81+
return True
82+
83+
7084
class TestHelper(object):
7185
"""Helper mixin for high-level cli and plugin tests.
7286

test/test_replaygain.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import os
1717
import shutil
1818
from glob import glob
19-
import subprocess
2019

2120
import _common
2221
from _common import unittest
23-
from helper import TestHelper
22+
from helper import TestHelper, has_program
2423

2524
from beets.library import Item, Album
2625
from beets.mediafile import MediaFile
@@ -32,6 +31,11 @@
3231
except ImportError, ValueError:
3332
GST_AVAILABLE = False
3433

34+
if any(has_program(cmd, ['-v']) for cmd in ['mp3gain', 'aacgain']):
35+
GAIN_PROG_AVAILABLE = True
36+
else:
37+
GAIN_PROG_AVAILABLE = False
38+
3539

3640
class ReplayGainCliTestBase(TestHelper):
3741

@@ -131,26 +135,10 @@ class ReplayGainGstCliTest(ReplayGainCliTestBase, unittest.TestCase):
131135
backend = u'gstreamer'
132136

133137

138+
@unittest.skipIf(not GAIN_PROG_AVAILABLE, 'no *gain command found')
134139
class ReplayGainCmdCliTest(ReplayGainCliTestBase, unittest.TestCase):
135140
backend = u'command'
136141

137-
def setUp(self):
138-
# Check for the backend command.
139-
for command in ['mp3gain', 'aacgain']:
140-
try:
141-
with open(os.devnull, 'wb') as devnull:
142-
subprocess.check_call(
143-
[command, '-v'], stderr=devnull
144-
)
145-
except OSError:
146-
pass
147-
else:
148-
break
149-
else:
150-
self.skipTest('no *gain command found')
151-
152-
super(ReplayGainCmdCliTest, self).setUp()
153-
154142

155143
def suite():
156144
return unittest.TestLoader().loadTestsFromName(__name__)

test/test_ui.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import _common
2323
from _common import unittest
24-
from helper import capture_stdout
24+
from helper import capture_stdout, has_program
2525

2626
from beets import library
2727
from beets import ui
@@ -931,14 +931,10 @@ def test_completion(self):
931931
'BASH_COMPLETION_SCRIPT', '/etc/bash_completion'))
932932

933933
# Tests run in bash
934-
shell = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc')
935-
try:
936-
with open(os.devnull, 'wb') as devnull:
937-
subprocess.check_call(shell.split() + ['--version'],
938-
stdout=devnull)
939-
except OSError:
934+
cmd = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc').split()
935+
if not has_program(cmd[0]):
940936
self.skipTest('bash not available')
941-
tester = subprocess.Popen(shell.split(' '), stdin=subprocess.PIPE,
937+
tester = subprocess.Popen(cmd, stdin=subprocess.PIPE,
942938
stdout=subprocess.PIPE)
943939

944940
# Load bash_completion
@@ -954,9 +950,6 @@ def test_completion(self):
954950
completion_script = self.io.getoutput()
955951
self.io.restore()
956952
tester.stdin.writelines(completion_script)
957-
# from beets import plugins
958-
# for cmd in plugins.commands():
959-
# print(cmd.name)
960953

961954
# Load testsuite
962955
with open(test_script, 'r') as test_script:

0 commit comments

Comments
 (0)