Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gforcada committed Dec 15, 2015
1 parent 27af1f2 commit 01df15c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
43 changes: 40 additions & 3 deletions flake8_isort.py
@@ -1,16 +1,53 @@
# -*- coding: utf-8 -*-
from isort import SortImports

import os


class Flake8Isort(object):
name = 'flake8_isort'
version = '0.1'
message = 'I001 found unsorted imports'

no_config_file = None

def __init__(self, tree, filename):
self.filename = filename

@classmethod
def add_options(cls, parser):
parser.add_option(
'--no-isort-config',
action='store_true',
help='Do not require an .isort.cfg file to be found'
)
parser.config_options.append('no-isort-config')

@classmethod
def parse_options(cls, options):
cls.no_config_file = bool(options.no_isort_config)

def run(self):
sort_result = SortImports(self.filename, check=True)
if sort_result.incorrectly_sorted:
yield 0, 0, self.message, type(self)
if not self.no_config_file and not self.search_isort_config():
yield 0, 0, 'I002 no .isort.cfg file found', type(self)
else:
sort_result = SortImports(self.filename, check=True)
if sort_result.incorrectly_sorted:
yield 0, 0, self.message, type(self)

def search_isort_config(self):
"""Search for a .isort.cfg all the way up to the root folder"""
full_path = os.path.abspath(self.filename)
path_parts = full_path.split(os.path.sep)
dirs_missing = len(path_parts)

while dirs_missing > 0:
dirs_missing -= 1
partial_parts = path_parts[:dirs_missing]
partial_path = os.sep.join(partial_parts)

isort_file = '{0}{1}.isort.cfg'.format(partial_path, os.sep)
if os.path.exists(isort_file):
return True

return False
58 changes: 58 additions & 0 deletions run_tests.py
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
from flake8.engine import get_style_guide
from flake8_isort import Flake8Isort
from tempfile import mkdtemp
from testfixtures import OutputCapture

import os
import sys
import unittest


Expand Down Expand Up @@ -49,6 +51,62 @@ def test_sorted_incorrectly(self):
self.assertEqual(ret[0][1], 0)
self.assertEqual(ret[0][2], 'I001 found unsorted imports')

def test_isortcfg_found(self):
# _given_a_file_in_test_dir already creates an .isort.cfg file
file_path = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading',
isort_config='force_single_line=True'
)
with OutputCapture():
checker = Flake8Isort(None, file_path)
checker.no_config_file = False
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 0)
self.assertEqual(ret[0][1], 0)
self.assertEqual(ret[0][2], 'I001 found unsorted imports')

def test_isortcfg_not_found(self):
file_path = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading',
isort_config='force_single_line=True'
)
# remove the .isort.cfg file
isortcfg_path = file_path.split('/')[: -1]
isortcfg_path = '{0}/.isort.cfg'.format('/'.join(isortcfg_path))
os.remove(isortcfg_path)

with OutputCapture():
checker = Flake8Isort(None, file_path)
checker.no_config_file = False
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 0)
self.assertEqual(ret[0][1], 0)
self.assertEqual(ret[0][2], 'I002 no .isort.cfg file found')

def test_default_option(self):
"""By default a config file (.isort.cfg) is expected"""
_argv = sys.argv
try:
sys.argv = []
get_style_guide(parse_argv=True) # parse arguments
self.assertFalse(Flake8Isort.no_config_file)
finally:
sys.argv = _argv

def test_no_config_file(self):
"""Check that one can force to not look for a config file"""
_argv = sys.argv
try:
sys.argv = ['', '--no-isort-config']
get_style_guide(parse_argv=True) # parse arguments
self.assertTrue(Flake8Isort.no_config_file)
finally:
sys.argv = _argv


if __name__ == '__main__':
unittest.main()

0 comments on commit 01df15c

Please sign in to comment.