Skip to content

Commit

Permalink
Merge branch 'template_refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
DEKHTIARJonathan committed Mar 8, 2022
2 parents 65ed25c + f4133f0 commit 4fb26f5
Show file tree
Hide file tree
Showing 16 changed files with 1,460 additions and 1,186 deletions.
63 changes: 63 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[tool:pytest]
minversion = 6.0
addopts = -ra -q -s
testpaths =
tests

[yapf]
based_on_style = google

# The number of columns to use for indentation.
indent_width = 4

# The column limit.
column_limit = 80

# Place each dictionary entry onto its own line.
each_dict_entry_on_separate_line = True

# Put closing brackets on a separate line, dedented, if the bracketed
# expression can't fit in a single line. Applies to all kinds of brackets,
# including function definitions and calls. For example:
#
# config = {
# 'key1': 'value1',
# 'key2': 'value2',
# } # <--- this bracket is dedented and on a separate line
#
# time_series = self.remote_client.query_entity_counters(
# entity='dev3246.region1',
# key='dns.query_latency_tcp',
# transform=Transformation.AVERAGE(window=timedelta(seconds=60)),
# start_ts=now()-timedelta(days=3),
# end_ts=now(),
# ) # <--- this bracket is dedented and on a separate line
dedent_closing_brackets = True

# Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set
coalesce_brackets = True

# Align closing bracket with visual indentation.
align_closing_bracket_with_visual_indent = False

# Split named assignments onto individual lines.
split_before_named_assigns = True

# If an argument / parameter list is going to be split, then split before the first argument.
split_before_first_argument = True

# Allow splitting before a default / named assignment in an argument list.
allow_split_before_default_or_named_assigns = True

# Join short lines into one line. E.g., single line if statements.
join_multiple_lines = False

# Let spacing indicate operator precedence.
arithmetic_precedence_indication = True

# Do not include spaces around selected binary operators.
# Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5
no_spaces_around_selected_binary_operators = True

# Allow lambdas to be formatted on more than one line.
allow_multiline_lambdas = True
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
94 changes: 94 additions & 0 deletions tests/test_yapf_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import unittest

import pygments
from pygments import console

from tests.utils import list_all_py_files
from tests.utils import CustomTestCase

from yapf.yapflib.yapf_api import FormatCode


def _read_utf_8_file(filename):
if sys.version_info.major == 2: ## Python 2 specific
with open(filename, 'rb') as f:
return unicode(f.read(), 'utf-8')
else:
with open(filename, encoding='utf-8') as f:
return f.read()


def print_color(msg, color):
print(pygments.console.colorize(color, msg))


class YAPF_Style_Test(unittest.TestCase):

@classmethod
def setUpClass(cls):

cls.badly_formatted_files = list()
cls.files_2_test = list_all_py_files()

def test_files_format(self):

total_analyzed_files = 0
for file in list_all_py_files():

total_analyzed_files += 1

try:

print(f"Testing: {file:100s}", end="")
code = _read_utf_8_file(file)

# https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module
diff, changed = FormatCode(
code,
filename=file,
style_config='setup.cfg',
print_diff=True
)

if changed:
print_color("FAILURE", "red")
self.badly_formatted_files.append(file)
else:
print_color("SUCCESS", "green")

except Exception as e:
print_color("FAILURE", "red")("FAILURE")
print(
"Error while processing file: `%s`\n"
"Error: %s" % (file, str(e))
)

str_err = ""

if self.badly_formatted_files:
for filename in self.badly_formatted_files:
str_err += f"yapf -i --style=setup.cfg {filename}\n"

str_err = "\n======================================================================================\n" \
f"Bad Coding Style: {len(self.badly_formatted_files)} file(s) need to be formatted, run the following commands to fix: \n" \
f"{str_err}" \
"======================================================================================"

passing_files = total_analyzed_files - len(self.badly_formatted_files)
print_color(
f"\nPASSING: {passing_files} / {total_analyzed_files}",
"green" if str_err == "" else "red"
)

if str_err != "":
print_color(str_err, "red")

self.assertEqual(str_err, "")


if __name__ == '__main__':
unittest.main()
34 changes: 34 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest

from contextlib import contextmanager
from glob import glob, iglob

__all__ = [
'CustomTestCase',
'list_all_py_files',
]


class CustomTestCase(unittest.TestCase):

@contextmanager
def assertNotRaises(self, exc_type):
try:
yield None
except exc_type:
raise self.failureException('{} raised'.format(exc_type.__name__))


_excludes_paths = ["tftrt/blog_posts/", "tftrt/examples/third_party"]


def list_all_py_files():
for _dir in ['tests', 'tftrt']:
for _file in iglob(f"{_dir}/**/*.py", recursive=True):
if any([path in _file for path in _excludes_paths]):
continue
yield _file
Loading

0 comments on commit 4fb26f5

Please sign in to comment.