diff --git a/Gemfile b/Gemfile index eb365cbf17..02ff0d56b8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.2.2' +ruby '2.3.3' gem "rubocop" gem "sqlint" @@ -7,3 +7,5 @@ gem 'scss_lint', require: false# require flag is necessary https://github.com/br gem "reek" gem "puppet-lint" gem "csvlint" +gem "img_checker" +gem "fastimage" diff --git a/bears/general/ImageDimensionBear.py b/bears/general/ImageDimensionBear.py new file mode 100644 index 0000000000..c810e46e9e --- /dev/null +++ b/bears/general/ImageDimensionBear.py @@ -0,0 +1,53 @@ +import os +import yaml + +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'} + + def run(self, + image_config): + + with open('img_config.yml', 'w') as yaml_file: + for each in image_config: + each = each.split(' ') + config = [{'directory': each[0], + 'width': int(each[1]), + 'height': int(each[2])}] + yaml.dump(config, yaml_file, default_flow_style=False) + + cmd = 'ruby -r "img_checker.rb" -e ' + cmd += '"ImgChecker.new.initialize img_config.yml"' + 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] + elif '.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..387c308137 --- /dev/null +++ b/tests/general/ImageDimensionBearTest.py @@ -0,0 +1,47 @@ +import unittest +from queue import Queue + +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('name') + self.queue = Queue() + self.file_dict = {} + self.idb = ImageDimensionBear(self.file_dict, self.section, self.queue) + + 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_config=['./test-img/*.png 240 240'])))) + + def test_run_with_jpg(self): + self.assertEqual('[]', + str(list(self.idb.run( + image_config=['./test-img/*.jpg 240 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_config=['./test-img/*.jpg 50 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_config=['./test-img/*.* 50 50']))) + self.assertIn(message_png, + output) + self.assertIn(message_jpg, + output)