Skip to content

Commit

Permalink
Merge pull request #170 from plone/run-code-analysis-on-folder
Browse files Browse the repository at this point in the history
Run code-analysis on any given folder
  • Loading branch information
gforcada committed Nov 17, 2015
2 parents 240c7f1 + aa6d082 commit fe52b05
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Change history
2.1.1 (unreleased)
------------------

- Nothing changed yet.

- Allow to pass a folder where to run code analysis against.
[gforcada]

2.1 (2015-09-21)
----------------
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ order to run the code analysis automatically before each commit.
use the same code analysis settings on your local machine as well as on
Jenkins.

It also allows to run code analysis to any arbitrary folder::

bin/code-analysis src/Products.CMFPlone


Installation
============
Expand Down
6 changes: 6 additions & 0 deletions plone/recipe/codeanalysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import os
import subprocess
import sys
import zc.buildout
import zc.recipe.egg

Expand Down Expand Up @@ -198,6 +199,11 @@ def uninstall_pre_commit_hook(self):
def code_analysis(options):
start = time()

# if there is a second argument (first is always the progam itself)
# use that one to run code analysis against
if len(sys.argv) > 1:
options['directory'] = sys.argv[1]

class DummyValue(object):
def __init__(self, value=True):
self.value = value
Expand Down
93 changes: 93 additions & 0 deletions plone/recipe/codeanalysis/tests/test_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
from contextlib import contextmanager
from plone.recipe.codeanalysis import code_analysis
from shutil import rmtree
from tempfile import mkdtemp
from testfixtures import OutputCapture

import os
import sys
import unittest


INVALID_CODE = """
from plone.recipe.codeanalysis import code_analysis
"""

VALID_CODE = """
# -*- coding: utf-8 -*-
from plone.recipe.codeanalysis import code_analysis
code_analysis()
"""


@contextmanager
def wrap_sys_argv():
"""Super simple context manager to clean sys.argv"""
# setup
old_sys = sys.argv
sys.argv = []

# give control back
yield

# restore
sys.argv = old_sys


class TestScripts(unittest.TestCase):

def setUp(self):
test_dir = os.path.realpath(mkdtemp())
for directory in ('bin', 'parts', 'eggs', 'develop-eggs', ):
os.makedirs('{0}/{1}'.format(test_dir, directory))

self.test_dir = test_dir
self.options = {
'flake8-extensions': 'flake8-coding',
'flake8': 'True',
'return-status-codes': 'False',
'directory': self.test_dir,
'multiprocessing': 'False',
}
if os.path.isfile('../../bin/flake8'): # when cwd is parts/test
self.options['bin-directory'] = '../../bin'

valid_file = '{0}/parts/valid.py'.format(self.test_dir)
invalid_file = '{0}/eggs/invalid.py'.format(self.test_dir)

with open(valid_file, 'w') as valid_file:
valid_file.write(VALID_CODE)

with open(invalid_file, 'w') as invalid_file:
invalid_file.write(INVALID_CODE)

def tearDown(self): # noqa
rmtree(self.test_dir)

def test_default_directory(self):
with OutputCapture() as output:
with wrap_sys_argv():
code_analysis(self.options)

self.assertIn(
'C101 Coding magic comment not found',
output.captured
)

def test_another_directory(self):
# the invalid file is on eggs
folder = '{0}/parts'.format(self.test_dir)
with OutputCapture() as output:
with wrap_sys_argv():
sys.argv = [
'bin/code-analysis',
folder
]
code_analysis(self.options)

self.assertNotIn(
'C101 Coding magic comment not found',
output.captured
)

0 comments on commit fe52b05

Please sign in to comment.