diff --git a/Gemfile b/Gemfile index eb365cbf17..4461e48d80 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,4 @@ gem 'scss_lint', require: false# require flag is necessary https://github.com/br gem "reek" gem "puppet-lint" gem "csvlint" +gem "img_checker" diff --git a/bears/general/ImageDimensionBear.py b/bears/general/ImageDimensionBear.py new file mode 100644 index 0000000000..f56ae9c508 --- /dev/null +++ b/bears/general/ImageDimensionBear.py @@ -0,0 +1,57 @@ +import os +import yaml +import shutil + +from coalib.bears.GlobalBear import GlobalBear +from coalib.results.Result import Result +from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY +from dependency_management.requirements.GemRequirement import GemRequirement +from dependency_management.requirements.PipRequirement import PipRequirement +from sarge import run, Capture + + +class ImageDimensionBear(GlobalBear): + """ + Checks the dimension of an image. + + More information is available at + """ + + AUTHORS = {'The coala developers'} + AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} + REQUIREMENTS = {GemRequirement(' img_checker'), + PipRequirement('pyyaml', '3.12')} + LICENSE = 'AGPL-3.0' + CAN_FIX = {'Image Dimension'} + + @classmethod + def check_prerequisites(cls): + if shutil.which('img_checker') is None: + return 'img_checker is not installed.' + return True + + def run(self, + image_file, + width, + height): + + with open('img_config.yml', 'w') as yaml_file: + config = [{'directory': image_file, + 'width': width, + 'height': height}] + yaml.dump(config, yaml_file, default_flow_style=False) + cmd = 'img_checker' + output = run(cmd, stdout=Capture(), stderr=Capture()) + if (output.returncode): + lines = output.stdout.text.split('\n')[1:-2] + for line in lines: + if '.png' in line: + fileName = line[10:line.index('.png')+4] + if '.jpg' in line: + fileName = line[10:line.index('.jpg')+4] + yield Result.from_values(origin=self, + message=line, + file=fileName, + severity=RESULT_SEVERITY.NORMAL) + + os.remove('img_config.yml') diff --git a/test-img/images.jpg b/test-img/images.jpg new file mode 100644 index 0000000000..b00bad630a Binary files /dev/null and b/test-img/images.jpg differ diff --git a/test-img/img.png b/test-img/img.png new file mode 100644 index 0000000000..fc2823a738 Binary files /dev/null and b/test-img/img.png differ diff --git a/tests/general/ImageDimensionBearTest.py b/tests/general/ImageDimensionBearTest.py new file mode 100644 index 0000000000..c24a6265fc --- /dev/null +++ b/tests/general/ImageDimensionBearTest.py @@ -0,0 +1,68 @@ +import unittest +from queue import Queue +import shutil + +from bears.general.ImageDimensionBear import ImageDimensionBear +from coalib.settings.Section import Section + + +class ImageDimensionBearTest(unittest.TestCase): + """ + Runs unittests for ImageDimensionBear + """ + + def setUp(self): + self.section = Section('') + self.queue = Queue() + self.file_dict = {} + self.idb = ImageDimensionBear(None, self.section, self.queue) + + def test_check_prerequisites(self): + _shutil_which = shutil.which + try: + shutil.which = lambda *args, **kwargs: None + self.assertEqual(ImageDimensionBear.check_prerequisites(), + 'img_checker is not installed.') + + shutil.which = lambda *args, **kwargs: 'path/to/git' + self.assertTrue(ImageDimensionBear.check_prerequisites()) + finally: + shutil.which = _shutil_which + + def test_run_with_png(self): + message = "message='The image ./test-img/img.png is larger" + message += " than 240px x 240px [w x h]'" + self.assertIn(message, + str(list(self.idb.run( + image_file='./test-img/*.png', + width=240, + height=240)))) + + def test_run_with_jpg(self): + self.assertEqual('[]', + str(list(self.idb.run( + image_file='./test-img/*.jpg', + width=240, + height=240)))) + + def test_run_with_jpg_fail(self): + message = "message='The image ./test-img/images.jpg is larger" + message += " than 50px x 50px [w x h]'" + self.assertIn(message, + str(list(self.idb.run( + image_file='./test-img/*.jpg', + width=50, + height=50)))) + + def test_run_with_both(self): + message_png = "message='The image ./test-img/img.png is larger" + message_png += " than 50px x 50px [w x h]'" + message_jpg = "message='The image ./test-img/images.jpg is larger" + message_jpg += " than 50px x 50px [w x h]'" + output = str(list(self.idb.run(image_file='./test-img/*.*', + width=50, + height=50))) + self.assertIn(message_png, + output) + self.assertIn(message_jpg, + output)