From cc9877adbeb9681db3b4cb7fb7a2311132759df0 Mon Sep 17 00:00:00 2001 From: Regen Date: Sat, 29 Feb 2020 19:45:36 +0900 Subject: [PATCH 1/2] Refactor formatter --- src/cmake_language_server/formatter.py | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/cmake_language_server/formatter.py b/src/cmake_language_server/formatter.py index b7fad2b..3338967 100644 --- a/src/cmake_language_server/formatter.py +++ b/src/cmake_language_server/formatter.py @@ -91,11 +91,12 @@ def main(args: List[str] = None): parser = ArgumentParser() parser.add_argument('lists', type=Path, nargs='*', help='CMake list files') - parser.add_argument('-i', - '--inplace', - action='store_true', - help='inplace edit') - parser.add_argument('-d', '--diff', action='store_true', help='show diff') + group = parser.add_mutually_exclusive_group() + group.add_argument('-i', + '--inplace', + action='store_true', + help='inplace edit') + group.add_argument('-d', '--diff', action='store_true', help='show diff') parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}') @@ -114,14 +115,12 @@ def main(args: List[str] = None): if not remain: with listpath.open('w') as fp: fp.write(formatted) + elif args.diff: + diff = unified_diff(content.splitlines(True), + formatted.splitlines(True), str(listpath), + str(listpath), '(before formatting)', + '(after formatting)') + diffstr = ''.join(diff) + print(diffstr, end='') else: - if args.diff: - diff = unified_diff(content.splitlines(True), - formatted.splitlines(True), str(listpath), - str(listpath), '(before formatting)', - '(after formatting)') - diffstr = ''.join(diff) - if diffstr: - print(diffstr, end='') - else: - print(formatted, end='') + print(formatted, end='') From 894b38d55da4886726477dad705e93414c509baf Mon Sep 17 00:00:00 2001 From: Regen Date: Sat, 29 Feb 2020 19:46:06 +0900 Subject: [PATCH 2/2] Add tests --- tests/test_fomatter.py | 62 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/test_fomatter.py b/tests/test_fomatter.py index 539f8fa..6ce0be3 100644 --- a/tests/test_fomatter.py +++ b/tests/test_fomatter.py @@ -1,4 +1,4 @@ -from cmake_language_server.formatter import Formatter +from cmake_language_server.formatter import Formatter, main from cmake_language_server.parser import ListParser @@ -72,3 +72,63 @@ if() ) # h endif() ''') + + +def test_main_noinput(capsys): + main([]) + captured = capsys.readouterr() + assert captured.out == '' + assert captured.err == '' + + +def test_main_file_1(capsys, tmp_path): + testfile1 = tmp_path / 'list1.cmake' + with testfile1.open('w') as fp: + fp.write(' a()') + + main([str(testfile1)]) + captured = capsys.readouterr() + assert captured.out == 'a()\n' + assert captured.err == '' + + +def test_main_file_2(capsys, tmp_path): + testfile1 = tmp_path / 'list1.cmake' + with testfile1.open('w') as fp: + fp.write(' a()') + testfile2 = tmp_path / 'list2.cmake' + with testfile2.open('w') as fp: + fp.write(' b()') + + main([str(testfile1), str(testfile2)]) + captured = capsys.readouterr() + assert captured.out == 'a()\nb()\n' + assert captured.err == '' + + +def test_main_inplace(capsys, tmp_path): + testfile1 = tmp_path / 'list1.cmake' + with testfile1.open('w') as fp: + fp.write(' a()') + + main(['-i', str(testfile1)]) + captured = capsys.readouterr() + assert captured.out == '' + assert captured.err == '' + + with testfile1.open() as fp: + content = fp.read() + assert content == 'a()\n' + + +def test_main_diff(capsys, tmp_path): + testfile1 = tmp_path / 'list1.cmake' + with testfile1.open('w') as fp: + fp.write(' a()') + + main(['-d', str(testfile1)]) + captured = capsys.readouterr() + assert str(testfile1) in captured.out + assert '- a()' in captured.out + assert '+a()' in captured.out + assert captured.err == ''