Skip tests if external commands aren't found

This commit is contained in:
Michael Brown 2014-06-27 18:12:37 -04:00
parent a301d061fb
commit 8b365f542a
4 changed files with 36 additions and 1 deletions

View File

@ -200,4 +200,29 @@ def lena(mode="RGB", cache={}):
# cache[mode] = im # cache[mode] = im
return im return im
def command_succeeds(cmd):
"""
Runs the command, which must be a list of strings. Returns True if the
command succeeds, or False if an OSError was raised by subprocess.Popen.
"""
import os
import subprocess
with open(os.devnull, 'w') as f:
try:
subprocess.Popen(cmd, stdout=f, stderr=subprocess.STDOUT).wait()
except OSError:
return False
return True
def djpeg_available():
return command_succeeds(['djpeg', '--help'])
def cjpeg_available():
return command_succeeds(['cjpeg', '--help'])
def netpbm_available():
return command_succeeds(["ppmquant", "--help"]) and \
command_succeeds(["ppmtogif", "--help"])
# End of file # End of file

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, tearDownModule, lena from helper import unittest, PillowTestCase, tearDownModule, lena, netpbm_available
from PIL import Image from PIL import Image
from PIL import GifImagePlugin from PIL import GifImagePlugin
@ -90,6 +90,7 @@ class TestFileGif(PillowTestCase):
reloaded = roundtrip(im)[1].convert('RGB') reloaded = roundtrip(im)[1].convert('RGB')
self.assert_image_equal(im, reloaded) self.assert_image_equal(im, reloaded)
@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_bmp_mode(self): def test_save_netpbm_bmp_mode(self):
img = Image.open(file).convert("RGB") img = Image.open(file).convert("RGB")
@ -97,6 +98,7 @@ class TestFileGif(PillowTestCase):
GifImagePlugin._save_netpbm(img, 0, tempfile) GifImagePlugin._save_netpbm(img, 0, tempfile)
self.assert_image_similar(img, Image.open(tempfile).convert("RGB"), 0) self.assert_image_similar(img, Image.open(tempfile).convert("RGB"), 0)
@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_l_mode(self): def test_save_netpbm_l_mode(self):
img = Image.open(file).convert("L") img = Image.open(file).convert("L")

View File

@ -1,4 +1,5 @@
from helper import unittest, PillowTestCase, tearDownModule, lena, py3 from helper import unittest, PillowTestCase, tearDownModule, lena, py3
from helper import djpeg_available, cjpeg_available
import random import random
from io import BytesIO from io import BytesIO
@ -275,11 +276,13 @@ class TestFileJpeg(PillowTestCase):
1:standard_chrominance_qtable}), 1:standard_chrominance_qtable}),
30) 30)
@unittest.skipUnless(djpeg_available(), "djpeg not available")
def test_load_djpeg(self): def test_load_djpeg(self):
img = Image.open(test_file) img = Image.open(test_file)
img.load_djpeg() img.load_djpeg()
self.assert_image_similar(img, Image.open(test_file), 0) self.assert_image_similar(img, Image.open(test_file), 0)
@unittest.skipUnless(cjpeg_available(), "cjpeg not available")
def test_save_cjpeg(self): def test_save_cjpeg(self):
img = Image.open(test_file) img = Image.open(test_file)

View File

@ -1,4 +1,5 @@
from helper import unittest, PillowTestCase, tearDownModule from helper import unittest, PillowTestCase, tearDownModule
from helper import djpeg_available, cjpeg_available, netpbm_available
import shutil import shutil
@ -24,6 +25,7 @@ class TestShellInjection(PillowTestCase):
# If file can't be opened, shell injection probably occurred # If file can't be opened, shell injection probably occurred
Image.open(dest_file).load() Image.open(dest_file).load()
@unittest.skipUnless(djpeg_available(), "djpeg not available")
def test_load_djpeg_filename(self): def test_load_djpeg_filename(self):
for filename in test_filenames: for filename in test_filenames:
src_file = self.tempfile(filename) src_file = self.tempfile(filename)
@ -32,14 +34,17 @@ class TestShellInjection(PillowTestCase):
im = Image.open(src_file) im = Image.open(src_file)
im.load_djpeg() im.load_djpeg()
@unittest.skipUnless(cjpeg_available(), "cjpeg not available")
def test_save_cjpeg_filename(self): def test_save_cjpeg_filename(self):
im = Image.open(test_jpg) im = Image.open(test_jpg)
self.assert_save_filename_check(im, JpegImagePlugin._save_cjpeg) self.assert_save_filename_check(im, JpegImagePlugin._save_cjpeg)
@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_filename_bmp_mode(self): def test_save_netpbm_filename_bmp_mode(self):
im = Image.open(test_gif).convert("RGB") im = Image.open(test_gif).convert("RGB")
self.assert_save_filename_check(im, GifImagePlugin._save_netpbm) self.assert_save_filename_check(im, GifImagePlugin._save_netpbm)
@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_filename_l_mode(self): def test_save_netpbm_filename_l_mode(self):
im = Image.open(test_gif).convert("L") im = Image.open(test_gif).convert("L")
self.assert_save_filename_check(im, GifImagePlugin._save_netpbm) self.assert_save_filename_check(im, GifImagePlugin._save_netpbm)