aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Wendling <morbo@google.com>2021-11-04 03:09:01 -0700
committerBill Wendling <morbo@google.com>2021-11-04 03:09:01 -0700
commit8ec4ed6fdd68c2a70809b99c788b7061876a746b (patch)
tree0bacd66e5510bc2da25089d085b0290ad4371c71
parent271072d52065b1f145a51cfcff123080df64e825 (diff)
downloadyapf-8ec4ed6fdd68c2a70809b99c788b7061876a746b.tar.gz
Change tests to support "pytest".
-rw-r--r--CHANGELOG2
-rw-r--r--yapf/__init__.py4
-rw-r--r--yapftests/file_resources_test.py4
-rw-r--r--yapftests/main_test.py14
-rw-r--r--yapftests/style_test.py57
-rw-r--r--yapftests/yapf_test_helper.py6
6 files changed, 50 insertions, 37 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 19d59f8..1cc69bb 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -8,6 +8,8 @@
for YAPF.
- New entry point `yapf_api.FormatTree` for formatting lib2to3 concrete
syntax trees.
+### Changes
+- Change tests to support "pytest".
### Fixed
- Enable `BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF` knob for "pep8" style, so
method definitions inside a class are surrounded by a single blank line as
diff --git a/yapf/__init__.py b/yapf/__init__.py
index 22adc30..4fe2ddf 100644
--- a/yapf/__init__.py
+++ b/yapf/__init__.py
@@ -81,7 +81,9 @@ def main(argv):
original_source = []
while True:
- if sys.stdin.closed:
+ # Test that sys.stdin has the "closed" attribute. When using pytest, it
+ # co-opts sys.stdin, which makes the "main_tests.py" fail. This is gross.
+ if hasattr(sys.stdin, "closed") and sys.stdin.closed:
break
try:
# Use 'raw_input' instead of 'sys.stdin.read', because otherwise the
diff --git a/yapftests/file_resources_test.py b/yapftests/file_resources_test.py
index f12f214..01efece 100644
--- a/yapftests/file_resources_test.py
+++ b/yapftests/file_resources_test.py
@@ -83,7 +83,7 @@ class GetExcludePatternsForDir(unittest.TestCase):
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')
f.write('ignore_patterns=[')
- f.writelines('\n,'.join([str(p) for p in ignore_patterns]))
+ f.writelines('\n,'.join(['"{}"'.format(p) for p in ignore_patterns]))
f.write(']')
self.assertEqual(
@@ -101,7 +101,7 @@ class GetExcludePatternsForDir(unittest.TestCase):
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')
f.write('ignore_patterns=[')
- f.writelines('\n,'.join([str(p) for p in ignore_patterns]))
+ f.writelines('\n,'.join(['"{}"'.format(p) for p in ignore_patterns]))
f.write(']')
with self.assertRaises(errors.YapfError):
diff --git a/yapftests/main_test.py b/yapftests/main_test.py
index 94daaaa..a9069b0 100644
--- a/yapftests/main_test.py
+++ b/yapftests/main_test.py
@@ -21,6 +21,8 @@ import yapf
from yapf.yapflib import py3compat
+from yapftests import yapf_test_helper
+
class IO(object):
"""IO is a thin wrapper around StringIO.
@@ -83,7 +85,7 @@ def patched_input(code):
yapf.py3compat.raw_input = orig_raw_import
-class RunMainTest(unittest.TestCase):
+class RunMainTest(yapf_test_helper.YAPFTest):
def testShouldHandleYapfError(self):
"""run_main should handle YapfError and sys.exit(1)."""
@@ -96,11 +98,11 @@ class RunMainTest(unittest.TestCase):
self.assertEqual(err.getvalue(), expected_message)
-class MainTest(unittest.TestCase):
+class MainTest(yapf_test_helper.YAPFTest):
def testNoPythonFilesMatched(self):
- with self.assertRaisesRegexp(yapf.errors.YapfError,
- 'did not match any python files'):
+ with self.assertRaisesRegex(yapf.errors.YapfError,
+ 'did not match any python files'):
yapf.main(['yapf', 'foo.c'])
def testEchoInput(self):
@@ -112,7 +114,7 @@ class MainTest(unittest.TestCase):
self.assertEqual(out.getvalue(), code)
def testEchoInputWithStyle(self):
- code = 'def f(a = 1):\n return 2*a\n'
+ code = 'def f(a = 1\n\n):\n return 2*a\n'
yapf_code = 'def f(a=1):\n return 2 * a\n'
with patched_input(code):
with captured_output() as (out, _):
@@ -124,7 +126,7 @@ class MainTest(unittest.TestCase):
bad_syntax = ' a = 1\n'
with patched_input(bad_syntax):
with captured_output() as (_, _):
- with self.assertRaisesRegexp(SyntaxError, 'unexpected indent'):
+ with self.assertRaisesRegex(SyntaxError, 'unexpected indent'):
yapf.main([])
def testHelp(self):
diff --git a/yapftests/style_test.py b/yapftests/style_test.py
index 2fd3f2d..a9512f1 100644
--- a/yapftests/style_test.py
+++ b/yapftests/style_test.py
@@ -23,9 +23,10 @@ import unittest
from yapf.yapflib import style
from yapftests import utils
+from yapftests import yapf_test_helper
-class UtilsTest(unittest.TestCase):
+class UtilsTest(yapf_test_helper.YAPFTest):
def testContinuationAlignStyleStringConverter(self):
for cont_align_space in ('', 'space', '"space"', '\'space\''):
@@ -91,7 +92,7 @@ def _LooksLikeYapfStyle(cfg):
return cfg['SPLIT_BEFORE_DOT']
-class PredefinedStylesByNameTest(unittest.TestCase):
+class PredefinedStylesByNameTest(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls): # pylint: disable=g-missing-super-call
@@ -123,7 +124,7 @@ class PredefinedStylesByNameTest(unittest.TestCase):
self.assertTrue(_LooksLikeFacebookStyle(cfg))
-class StyleFromFileTest(unittest.TestCase):
+class StyleFromFileTest(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls): # pylint: disable=g-missing-super-call
@@ -202,8 +203,8 @@ class StyleFromFileTest(unittest.TestCase):
self.assertEqual(cfg['I18N_FUNCTION_CALL'], ['N_', 'V_', 'T_'])
def testErrorNoStyleFile(self):
- with self.assertRaisesRegexp(style.StyleConfigError,
- 'is not a valid style or file path'):
+ with self.assertRaisesRegex(style.StyleConfigError,
+ 'is not a valid style or file path'):
style.CreateStyleFromConfig('/8822/xyznosuchfile')
def testErrorNoStyleSection(self):
@@ -212,8 +213,8 @@ class StyleFromFileTest(unittest.TestCase):
indent_width=2
''')
with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:
- with self.assertRaisesRegexp(style.StyleConfigError,
- 'Unable to find section'):
+ with self.assertRaisesRegex(style.StyleConfigError,
+ 'Unable to find section'):
style.CreateStyleFromConfig(filepath)
def testErrorUnknownStyleOption(self):
@@ -223,8 +224,8 @@ class StyleFromFileTest(unittest.TestCase):
hummus=2
''')
with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:
- with self.assertRaisesRegexp(style.StyleConfigError,
- 'Unknown style option'):
+ with self.assertRaisesRegex(style.StyleConfigError,
+ 'Unknown style option'):
style.CreateStyleFromConfig(filepath)
def testPyprojectTomlNoYapfSection(self):
@@ -235,8 +236,8 @@ class StyleFromFileTest(unittest.TestCase):
filepath = os.path.join(self.test_tmpdir, 'pyproject.toml')
_ = open(filepath, 'w')
- with self.assertRaisesRegexp(style.StyleConfigError,
- 'Unable to find section'):
+ with self.assertRaisesRegex(style.StyleConfigError,
+ 'Unable to find section'):
style.CreateStyleFromConfig(filepath)
def testPyprojectTomlParseYapfSection(self):
@@ -258,7 +259,7 @@ class StyleFromFileTest(unittest.TestCase):
self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 40)
-class StyleFromDict(unittest.TestCase):
+class StyleFromDict(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls): # pylint: disable=g-missing-super-call
@@ -275,15 +276,15 @@ class StyleFromDict(unittest.TestCase):
self.assertEqual(cfg['INDENT_WIDTH'], 2)
def testDefaultBasedOnStyleBadDict(self):
- self.assertRaisesRegexp(style.StyleConfigError, 'Unknown style option',
- style.CreateStyleFromConfig,
- {'based_on_styl': 'pep8'})
- self.assertRaisesRegexp(style.StyleConfigError, 'not a valid',
- style.CreateStyleFromConfig,
- {'INDENT_WIDTH': 'FOUR'})
+ self.assertRaisesRegex(style.StyleConfigError, 'Unknown style option',
+ style.CreateStyleFromConfig,
+ {'based_on_styl': 'pep8'})
+ self.assertRaisesRegex(style.StyleConfigError, 'not a valid',
+ style.CreateStyleFromConfig,
+ {'INDENT_WIDTH': 'FOUR'})
-class StyleFromCommandLine(unittest.TestCase):
+class StyleFromCommandLine(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls): # pylint: disable=g-missing-super-call
@@ -314,17 +315,17 @@ class StyleFromCommandLine(unittest.TestCase):
self.assertIsInstance(cfg, dict)
def testDefaultBasedOnStyleBadString(self):
- self.assertRaisesRegexp(style.StyleConfigError, 'Unknown style option',
- style.CreateStyleFromConfig,
- '{based_on_styl: pep8}')
- self.assertRaisesRegexp(style.StyleConfigError, 'not a valid',
- style.CreateStyleFromConfig, '{INDENT_WIDTH: FOUR}')
- self.assertRaisesRegexp(style.StyleConfigError, 'Invalid style dict',
- style.CreateStyleFromConfig,
- '{based_on_style: pep8')
+ self.assertRaisesRegex(style.StyleConfigError, 'Unknown style option',
+ style.CreateStyleFromConfig,
+ '{based_on_styl: pep8}')
+ self.assertRaisesRegex(style.StyleConfigError, 'not a valid',
+ style.CreateStyleFromConfig, '{INDENT_WIDTH: FOUR}')
+ self.assertRaisesRegex(style.StyleConfigError, 'Invalid style dict',
+ style.CreateStyleFromConfig,
+ '{based_on_style: pep8')
-class StyleHelp(unittest.TestCase):
+class StyleHelp(yapf_test_helper.YAPFTest):
def testHelpKeys(self):
settings = sorted(style.Help())
diff --git a/yapftests/yapf_test_helper.py b/yapftests/yapf_test_helper.py
index 1f21b36..f6a2b66 100644
--- a/yapftests/yapf_test_helper.py
+++ b/yapftests/yapf_test_helper.py
@@ -21,6 +21,7 @@ from yapf.yapflib import blank_line_calculator
from yapf.yapflib import comment_splicer
from yapf.yapflib import continuation_splicer
from yapf.yapflib import identify_container
+from yapf.yapflib import py3compat
from yapf.yapflib import pytree_unwrapper
from yapf.yapflib import pytree_utils
from yapf.yapflib import pytree_visitor
@@ -31,6 +32,11 @@ from yapf.yapflib import subtype_assigner
class YAPFTest(unittest.TestCase):
+ def __init__(self, *args):
+ super(YAPFTest, self).__init__(*args)
+ if not py3compat.PY3:
+ self.assertRaisesRegex = self.assertRaisesRegexp
+
def assertCodeEqual(self, expected_code, code):
if code != expected_code:
msg = ['Code format mismatch:', 'Expected:']