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 12, 2017
1 parent 0abe3d7 commit 7e42486
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
57 changes: 57 additions & 0 deletions bears/general/ImageDimensionBear.py
Original file line number Diff line number Diff line change
@@ -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 <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'}

@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')
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.
68 changes: 68 additions & 0 deletions tests/general/ImageDimensionBearTest.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7e42486

Please sign in to comment.