diff --git a/Tests/helper.py b/Tests/helper.py index 051912897..104b8cc3e 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -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 diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 719847a96..e31779df0 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -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 @@ -90,6 +90,7 @@ class TestFileGif(PillowTestCase): 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") @@ -97,6 +98,7 @@ class TestFileGif(PillowTestCase): 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") diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 5b2ba45e7..283c48eb7 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -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 @@ -275,11 +276,13 @@ class TestFileJpeg(PillowTestCase): 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) diff --git a/Tests/test_shell_injection.py b/Tests/test_shell_injection.py index d20f9b1a6..fd14e5f44 100644 --- a/Tests/test_shell_injection.py +++ b/Tests/test_shell_injection.py @@ -1,4 +1,5 @@ from helper import unittest, PillowTestCase, tearDownModule +from helper import djpeg_available, cjpeg_available, netpbm_available import shutil @@ -24,6 +25,7 @@ class TestShellInjection(PillowTestCase): # 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) @@ -32,14 +34,17 @@ class TestShellInjection(PillowTestCase): 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)