Skip to content

Commit

Permalink
ImageDimensionBear: Add image dimension bear
Browse files Browse the repository at this point in the history
Added ImageDimensionBear.py in bears/general, that runs
the gem img_checker to see if images follow provided
dimensions.
Also added unittest for the bear, ImageDimensionBearTest.py
in tests/general
  • Loading branch information
rhemon committed Jan 11, 2017
1 parent 0abe3d7 commit 40e5886
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
53 changes: 53 additions & 0 deletions bears/general/ImageDimensionBear.py
Original file line number Diff line number Diff line change
@@ -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 <github.com/Abhi2424shek/img_checker>
"""

AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
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')
Binary file added test-img/images.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-img/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions tests/general/ImageDimensionBearTest.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 40e5886

Please sign in to comment.