Skip to content

Commit

Permalink
Change: Enable string normalization when using black
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Jul 5, 2022
1 parent abc18b7 commit d32a6ee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
22 changes: 11 additions & 11 deletions autohooks/plugins/black/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
)
from autohooks.api.path import match

DEFAULT_INCLUDE = ('*.py',)
DEFAULT_ARGUMENTS = ('-q',)
DEFAULT_INCLUDE = ("*.py",)
DEFAULT_ARGUMENTS = ("-q",)


def check_black_installed():
try:
import black # pylint: disable=unused-import, import-outside-toplevel
except ImportError:
raise Exception(
'Could not find black. '
'Please add black to your python environment.'
"Could not find black. "
"Please add black to your python environment."
) from None


def get_black_config(config):
return config.get('tool', 'autohooks', 'plugins', 'black')
return config.get("tool", "autohooks", "plugins", "black")


def ensure_iterable(value):
Expand All @@ -55,7 +55,7 @@ def get_include_from_config(config):

black_config = get_black_config(config)
include = ensure_iterable(
black_config.get_value('include', DEFAULT_INCLUDE)
black_config.get_value("include", DEFAULT_INCLUDE)
)

return include
Expand All @@ -67,7 +67,7 @@ def get_black_arguments(config):

black_config = get_black_config(config)
arguments = ensure_iterable(
black_config.get_value('arguments', DEFAULT_ARGUMENTS)
black_config.get_value("arguments", DEFAULT_ARGUMENTS)
)

return arguments
Expand All @@ -80,10 +80,10 @@ def precommit(config=None, **kwargs): # pylint: disable=unused-argument
files = [f for f in get_staged_status() if match(f.path, include)]

if len(files) == 0:
ok('No staged files for black available.')
ok("No staged files for black available.")
return 0

arguments = ['black']
arguments = ["black"]
arguments.extend(get_black_arguments(config))

with stash_unstaged_changes(files):
Expand All @@ -93,9 +93,9 @@ def precommit(config=None, **kwargs): # pylint: disable=unused-argument
args.append(str(f.absolute_path()))

subprocess.check_call(args)
ok(f'Running black on {str(f.path)}')
ok(f"Running black on {str(f.path)}")
except subprocess.CalledProcessError as e:
error(f'Running black on {str(f.path)}')
error(f"Running black on {str(f.path)}")
raise e from None

stage_files_from_status_list(files)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pontos = ">=22.5.0"
[tool.black]
line-length = 80
target-version = ['py37', 'py38', 'py39', 'py310']
skip-string-normalization = true
exclude = '''
/(
\.git
Expand Down
8 changes: 4 additions & 4 deletions tests/black_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
from io import StringIO, BytesIO, FileIO # pylint: disable=unused-import
import sys

cmd = ['pylint', 'autohooks/plugins/pylint/pylint.py']
cmd = ["pylint", "autohooks/plugins/pylint/pylint.py"]
import subprocess # pylint: disable=

# status = subprocess.call(cmd)
iofile = 'tmp.txt'
iofile = "tmp.txt"
# status = subprocess.call(cmd, stdout=iofile)
# blah blah lots of code ...

status = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = status.communicate()
print(out.decode(encoding='utf-8'))
print(err.decode(encoding='utf-8'))
print(out.decode(encoding="utf-8"))
print(err.decode(encoding="utf-8"))
24 changes: 12 additions & 12 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_test_config_path(name):

class AutohooksBlackTestCase(TestCase):
def test_xblack_installed(self):
sys.modules['black'] = None
sys.modules["black"] = None
with self.assertRaises(Exception):
check_black_installed()

Expand All @@ -51,38 +51,38 @@ def test_get_black_arguments(self):
self.assertEqual(args, DEFAULT_ARGUMENTS)

def test_get_black_config(self):
config_path = get_test_config_path('pyproject.test.toml')
config_path = get_test_config_path("pyproject.test.toml")
self.assertTrue(config_path.is_file())

autohooksconfig = load_config_from_pyproject_toml(config_path)
self.assertTrue(autohooksconfig.has_config())

black_config = get_black_config(autohooksconfig.get_config())
self.assertEqual(black_config.get_value('foo'), 'bar')
self.assertEqual(black_config.get_value("foo"), "bar")

def test_ensure_iterable(self):
foo = 'bar' # pylint: disable=blacklisted-name
foo = "bar" # pylint: disable=blacklisted-name
bar = ensure_iterable(foo) # pylint: disable=blacklisted-name
self.assertEqual(bar, ['bar'])
self.assertEqual(bar, ["bar"])

foo = ['bar'] # pylint: disable=blacklisted-name
foo = ["bar"] # pylint: disable=blacklisted-name
bar = ensure_iterable(foo) # pylint: disable=blacklisted-name
self.assertEqual(bar, ['bar'])
self.assertEqual(bar, ["bar"])

def test_get_include_from_config(self):
include = get_include_from_config(config=None)
self.assertEqual(include, DEFAULT_INCLUDE)

@patch('autohooks.plugins.black.black.ok')
@patch("autohooks.plugins.black.black.ok")
def test_precommit(self, _ok_mock):
ret = precommit()
self.assertFalse(ret)

# these Terminal output functions don't run in the CI ...
@patch('autohooks.plugins.black.black.ok')
@patch('autohooks.plugins.black.black.error')
@patch('autohooks.plugins.black.black.get_staged_status')
@patch("autohooks.plugins.black.black.ok")
@patch("autohooks.plugins.black.black.error")
@patch("autohooks.plugins.black.black.get_staged_status")
def test_precommit_staged(self, staged_mock, _error_mock, _ok_mock):
staged_mock.return_value = [StatusEntry('M tests/black_test.py')]
staged_mock.return_value = [StatusEntry("M tests/black_test.py")]
ret = precommit()
self.assertFalse(ret)

0 comments on commit d32a6ee

Please sign in to comment.