Skip to content

Commit

Permalink
Skip tests if external commands aren't found
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrown1413 committed Jun 27, 2014
1 parent a301d06 commit 8b365f5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,29 @@ def lena(mode="RGB", cache={}):
# cache[mode] = 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
4 changes: 3 additions & 1 deletion Tests/test_file_gif.py
Original file line number Diff line number Diff line change
@@ -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 GifImagePlugin
Expand Down Expand Up @@ -90,13 +90,15 @@ def roundtrip(im, *args, **kwargs):
reloaded = roundtrip(im)[1].convert('RGB')
self.assert_image_equal(im, reloaded)

@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_bmp_mode(self):
img = Image.open(file).convert("RGB")

tempfile = self.tempfile("temp.gif")
GifImagePlugin._save_netpbm(img, 0, tempfile)
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):
img = Image.open(file).convert("L")

Expand Down
3 changes: 3 additions & 0 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from helper import unittest, PillowTestCase, tearDownModule, lena, py3
from helper import djpeg_available, cjpeg_available

import random
from io import BytesIO
Expand Down Expand Up @@ -275,11 +276,13 @@ def test_qtables(self):
1:standard_chrominance_qtable}),
30)

@unittest.skipUnless(djpeg_available(), "djpeg not available")
def test_load_djpeg(self):
img = Image.open(test_file)
img.load_djpeg()
self.assert_image_similar(img, Image.open(test_file), 0)

@unittest.skipUnless(cjpeg_available(), "cjpeg not available")
def test_save_cjpeg(self):
img = Image.open(test_file)

Expand Down
5 changes: 5 additions & 0 deletions Tests/test_shell_injection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from helper import unittest, PillowTestCase, tearDownModule
from helper import djpeg_available, cjpeg_available, netpbm_available

import shutil

Expand All @@ -24,6 +25,7 @@ def assert_save_filename_check(self, src_img, save_func):
# If file can't be opened, shell injection probably occurred
Image.open(dest_file).load()

@unittest.skipUnless(djpeg_available(), "djpeg not available")
def test_load_djpeg_filename(self):
for filename in test_filenames:
src_file = self.tempfile(filename)
Expand All @@ -32,14 +34,17 @@ def test_load_djpeg_filename(self):
im = Image.open(src_file)
im.load_djpeg()

@unittest.skipUnless(cjpeg_available(), "cjpeg not available")
def test_save_cjpeg_filename(self):
im = Image.open(test_jpg)
self.assert_save_filename_check(im, JpegImagePlugin._save_cjpeg)

@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_filename_bmp_mode(self):
im = Image.open(test_gif).convert("RGB")
self.assert_save_filename_check(im, GifImagePlugin._save_netpbm)

@unittest.skipUnless(netpbm_available(), "netpbm not available")
def test_save_netpbm_filename_l_mode(self):
im = Image.open(test_gif).convert("L")
self.assert_save_filename_check(im, GifImagePlugin._save_netpbm)
Expand Down

0 comments on commit 8b365f5

Please sign in to comment.