Пример #1
0
 def testEchoBadInput(self):
     bad_syntax = '  a = 1\n'
     with patched_input(bad_syntax):
         with captured_output() as (_, _):
             with self.assertRaisesRegex(yapf.errors.YapfError,
                                         'unexpected indent'):
                 yapf.main([])
Пример #2
0
 def testEchoInput(self):
   code = "a = 1\nb = 2\n"
   with patched_input(code):
     with captured_output() as (out, err):
       ret = yapf.main([])
       self.assertEqual(ret, 0)
       self.assertEqual(out.getvalue(), code)
Пример #3
0
 def testEchoInput(self):
   code = "a = 1\nb = 2\n"
   with patched_input(code):
     with captured_output() as (out, err):
       ret = yapf.main([])
       self.assertEqual(ret, 0)
       self.assertEqual(out.getvalue(), code)
Пример #4
0
    def run(self):  # pylint: disable=missing-docstring
        import yapf  # pylint: disable=import-error

        try:
            yapf.main([
                None,
                "--in-place",
                "--recursive",
                "--verbose",
                "setup.py",
                "tests/",
                "winnan/",
            ])
        except yapf.errors.YapfError as err:
            msg = "yapf: {}".format(err)
            self.announce(msg, distutils.log.ERROR)  # pylint: disable=no-member
            raise distutils.errors.DistutilsError(msg)  # pylint: disable=no-member
Пример #5
0
 def testHelp(self):
   with captured_output() as (out, err):
     ret = yapf.main(['-', '--style-help', '--style=pep8'])
     self.assertEqual(ret, 0)
     help_message = out.getvalue()
     self.assertIn("INDENT_WIDTH=4", help_message)
     self.assertIn("The number of spaces required before a trailing comment.",
                   help_message)
Пример #6
0
 def testEchoInputWithStyle(self):
   code = "def f(a = 1):\n    return 2*a\n"
   chromium_code = "def f(a=1):\n  return 2 * a\n"
   with patched_input(code):
     with captured_output() as (out, err):
       ret = yapf.main(['-', '--style=chromium'])
       self.assertEqual(ret, 0)
       self.assertEqual(out.getvalue(), chromium_code)
Пример #7
0
 def testHelp(self):
   with captured_output() as (out, err):
     ret = yapf.main(['-', '--style-help', '--style=pep8'])
     self.assertEqual(ret, 0)
     help_message = out.getvalue()
     self.assertIn("INDENT_WIDTH=4", help_message)
     self.assertIn("The number of spaces required before a trailing comment.",
                   help_message)
Пример #8
0
 def testEchoInputWithStyle(self):
     code = 'def f(a = 1):\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, _):
             ret = yapf.main(['-', '--style=yapf'])
             self.assertEqual(ret, 0)
             self.assertEqual(out.getvalue(), yapf_code)
Пример #9
0
 def testEchoInputWithStyle(self):
   code = "def f(a = 1):\n    return 2*a\n"
   chromium_code = "def f(a=1):\n  return 2 * a\n"
   with patched_input(code):
     with captured_output() as (out, err):
       ret = yapf.main(['-', '--style=chromium'])
       self.assertEqual(ret, 2)
       self.assertEqual(out.getvalue(), chromium_code)
Пример #10
0
 def testHelp(self):
   with captured_output() as (out, _):
     ret = yapf.main(['-', '--style-help', '--style=pep8'])
     self.assertEqual(ret, 0)
     help_message = out.getvalue()
     self.assertIn('indent_width=4', help_message)
     self.assertIn('The number of spaces required before a trailing comment.',
                   help_message)
Пример #11
0
def format_py(args: FormatArguments) -> None:
    py_files = paths.TOOLS_DIR.rglob('*.py')
    rc = yapf.main(
        list(
            proc.flatten_cmd([
                '--parallel',
                '--verbose',
                ('--diff') if args.check else ('--in-place'),
                py_files,
            ])))
    if rc and args.check:
        raise RuntimeError(
            'Format checks for one or more Python files. (See above.)')
    if rc:
        raise RuntimeError(
            'Format execution failed for Python code. See above.')
Пример #12
0
 def run(self):
     import yapf
     yapf.main([
         sys.executable, '--in-place', '--recursive', 'bin', 'vault_ca',
         'tests'
     ])
Пример #13
0
 def testEchoBadInput(self):
     bad_syntax = "  a = 1\n"
     with patched_input(bad_syntax):
         with captured_output() as (out, err):
             ret = yapf.main([])
Пример #14
0
 def testNoPythonFilesMatched(self):
   with self.assertRaisesRegexp(yapf.errors.YapfError,
                                'did not match any python files'):
     yapf.main(['yapf', 'foo.c'])
Пример #15
0
 def testEchoBadInput(self):
   bad_syntax = "  a = 1\n"
   with patched_input(bad_syntax):
     with captured_output() as (out, err):
       with self.assertRaisesRegexp(SyntaxError, "unexpected indent"):
         ret = yapf.main([])
Пример #16
0
 def testNoPythonFilesMatched(self):
   with self.assertRaisesRegexp(yapf.errors.YapfError,
                                'did not match any python files'):
     yapf.main(['yapf', 'foo.c'])
Пример #17
0
 def testEchoBadInput(self):
   bad_syntax = "  a = 1\n"
   with patched_input(bad_syntax):
     with captured_output() as (out, err):
       with self.assertRaisesRegexp(SyntaxError, "unexpected indent"):
         yapf.main([])
Пример #18
0
 def testVersion(self):
   with captured_output() as (out, err):
     ret = yapf.main(['-', '--version'])
     self.assertEqual(ret, 0)
     version = 'yapf {}\n'.format(yapf.__version__)
     self.assertEqual(version, out.getvalue())
Пример #19
0
def test_format():
    assert 0 == yapf.main(
        ['yapf', '--diff', '--recursive', 'setup.py', 'tests'] +
        find_packages())
Пример #20
0
 def testEchoBadInput(self):
   bad_syntax = '  a = 1\n'
   with patched_input(bad_syntax):
     with captured_output() as (_, _):
       with self.assertRaisesRegexp(SyntaxError, 'unexpected indent'):
         yapf.main([])
Пример #21
0
 def run(self):
     import yapf
     yapf.main(['yapf', '--recursive', '--in-place', 'setup.py', 'test'] +
               find_packages())
Пример #22
0
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys

import yapf

sys.exit(yapf.main(sys.argv))
Пример #23
0
 def testVersion(self):
   with captured_output() as (out, err):
     ret = yapf.main(['-', '--version'])
     self.assertEqual(ret, 0)
     version = 'yapf {}\n'.format(yapf.__version__)
     self.assertEqual(version, out.getvalue())
Пример #24
0
 def testEchoBadInput(self):
   bad_syntax = "  a = 1\n"
   with patched_input(bad_syntax):
     with captured_output() as (out, err):
       ret = yapf.main([])
Пример #25
0
 def run(self):
     import yapf
     yapf.main(['yapf', '--recursive', '--in-place', 'setup.py', 'test'] +
               find_packages())
Пример #26
0
        )
        print(
            "auto-format. Please stage, stash, or revert these changes. You may "
        )
        print("find `git stash -k` helpful here.")
        print(f"Files with unstaged changes: {' '.join(changed)}")
        sys.exit(1)

    # Format all staged files, then exit with an error code if any have uncommitted changes.
    print("Formatting staged Python files . . .")

    out = subprocess.run(["git", "rev-parse", "--show-toplevel"],
                         stdout=subprocess.PIPE)
    repo_root = Path(out.stdout.decode(sys.stdout.encoding).splitlines()[0])
    python_files = [str(repo_root / f) for f in python_files]
    out = yapf.main(["yapf", "-i", "-r"] + python_files)
    if out != 0:
        sys.exit(out)

    out = subprocess.run(["git", "diff", "--name-only", "--"] +
                         [str(f) for f in python_files],
                         stdout=subprocess.PIPE)
    changed = [
        str(Path(f))
        for f in out.stdout.decode(sys.stdout.encoding).splitlines()
    ]
    if len(changed) > 0:
        print("Reformatted staged files. Please review and stage the changes.")
        print(f"Files updated: {' '.join(changed)}")
        sys.exit(1)