Add stdin mode to cmake-format
This commit is contained in:
@@ -86,10 +86,16 @@ def main(args: List[str] = None):
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from difflib import unified_diff
|
from difflib import unified_diff
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
from . import __version__
|
from . import __version__
|
||||||
from .parser import ListParser
|
from .parser import ListParser
|
||||||
|
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser(
|
||||||
|
description='Format CMake list files.',
|
||||||
|
epilog='''
|
||||||
|
If no arguments are specified, it formats the code from
|
||||||
|
standard input and writes the result to the standard output.''',
|
||||||
|
)
|
||||||
parser.add_argument('lists', type=Path, nargs='*', help='CMake list files')
|
parser.add_argument('lists', type=Path, nargs='*', help='CMake list files')
|
||||||
group = parser.add_mutually_exclusive_group()
|
group = parser.add_mutually_exclusive_group()
|
||||||
group.add_argument('-i',
|
group.add_argument('-i',
|
||||||
@@ -103,11 +109,22 @@ def main(args: List[str] = None):
|
|||||||
|
|
||||||
args = parser.parse_args(args)
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
|
if not args.lists and args.inplace:
|
||||||
|
print('error: cannot use -i when no arguments are specified.',
|
||||||
|
file=sys.stderr)
|
||||||
|
return
|
||||||
|
if not args.lists:
|
||||||
|
args.lists.append(None)
|
||||||
|
|
||||||
list_parser = ListParser()
|
list_parser = ListParser()
|
||||||
formatter = Formatter()
|
formatter = Formatter()
|
||||||
for listpath in args.lists:
|
for listpath in args.lists:
|
||||||
with listpath.open() as fp:
|
if listpath is None:
|
||||||
content = fp.read()
|
listpath = '(stdin)'
|
||||||
|
content = sys.stdin.read()
|
||||||
|
else:
|
||||||
|
with listpath.open() as fp:
|
||||||
|
content = fp.read()
|
||||||
tokens, remain = list_parser.parse(content)
|
tokens, remain = list_parser.parse(content)
|
||||||
formatted = content if remain else formatter.format(tokens)
|
formatted = content if remain else formatter.format(tokens)
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
import sys
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
from cmake_language_server.formatter import Formatter, main
|
from cmake_language_server.formatter import Formatter, main
|
||||||
from cmake_language_server.parser import ListParser
|
from cmake_language_server.parser import ListParser
|
||||||
|
|
||||||
@@ -74,10 +78,28 @@ endif()
|
|||||||
''')
|
''')
|
||||||
|
|
||||||
|
|
||||||
def test_main_noinput(capsys):
|
@contextmanager
|
||||||
main([])
|
def mock_stdin(buf: str):
|
||||||
|
stdin = sys.stdin
|
||||||
|
sys.stdin = StringIO(buf)
|
||||||
|
yield
|
||||||
|
sys.stdin = stdin
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_stdin(capsys):
|
||||||
|
with mock_stdin(' a()'):
|
||||||
|
main([])
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
assert captured.out == ''
|
assert captured.out == 'a()\n'
|
||||||
|
assert captured.err == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_stdin_diff(capsys):
|
||||||
|
with mock_stdin(' a()'):
|
||||||
|
main(['-d'])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert '- a()' in captured.out
|
||||||
|
assert '+a()' in captured.out
|
||||||
assert captured.err == ''
|
assert captured.err == ''
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user