Skip to content

Commit

Permalink
Fix #132 - --diff option introduced and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejPatro committed Jun 7, 2020
1 parent 5983819 commit 3fd84ad
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ sub-commands:

[source,text]
----
usage: cmake-tidy format [-h] [--dump-config] [-i] [input]
usage: cmake-tidy format [-h] [--dump-config] [-i] [--diff] [input]
positional arguments:
input CMake file to be formatted
optional arguments:
-h, --help show this help message and exit
--dump-config dump to stdout current settings. Script tries to read
--dump-config Dump to stdout current settings. Script tries to read
settings from `.cmake-tidy.json` or provides default
settings. Precedence of searching `.cmake-tidy.json` is
described on github
-i, --inplace inplace edit specified <input_data> file
-i, --inplace Inplace edit specified <input_data> file
--diff Print to stdout unified diff between original file and
formatted version.
----

==== link:doc/config.adoc[Configuration]
Expand Down
7 changes: 6 additions & 1 deletion cmake_tidy/commands/format/format_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from cmake_tidy.parsing import CMakeParser
from cmake_tidy.utils.command_line_handling import arguments
from cmake_tidy.utils import ExitCodes
from cmake_tidy.utils.diff import get_unified_diff


class FormatCommand(Command):
Expand All @@ -24,6 +25,7 @@ def __init__(self, parser):
arguments.dump_config(self._command_parser)
arguments.inplace(self._command_parser)
arguments.input_data(self._command_parser)
arguments.diff(self._command_parser)

def execute_command(self, args) -> int:
try:
Expand All @@ -38,7 +40,10 @@ def __try_execute_command(self, args):
else:
config = try_create_configuration(args)
formatted_file = self.__try_format_file(config)
self.__try_output_formatted_file(config, formatted_file)
if args.diff:
print(get_unified_diff(config.input, formatted_file, config.file))
else:
self.__try_output_formatted_file(config, formatted_file)

@staticmethod
def __try_dump_config(args):
Expand Down
10 changes: 8 additions & 2 deletions cmake_tidy/utils/command_line_handling/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ def input_data(parser):
def dump_config(parser):
parser.add_argument('--dump-config',
action='store_true',
help='dump to stdout current settings. Script tries to read settings from `.cmake-tidy.json` '
help='Dump to stdout current settings. Script tries to read settings from `.cmake-tidy.json` '
'or provides default settings. Precedence of searching `.cmake-tidy.json` is described '
'on github')


def inplace(parser):
parser.add_argument('-i', '--inplace',
action='store_true',
help='inplace edit specified <input_data> file')
help='Inplace edit specified <input_data> file')


def diff(parser):
parser.add_argument('--diff',
action='store_true',
help='Print to stdout unified diff between original file and formatted version.')
16 changes: 16 additions & 0 deletions cmake_tidy/utils/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
###############################################################################
# Copyright Maciej Patro (maciej.patro@gmail.com)
# MIT License
###############################################################################


import difflib
from pathlib import Path


def get_unified_diff(original: str, modified: str, filename: Path) -> str:
diff = difflib.unified_diff(a=original.splitlines(keepends=True),
b=modified.splitlines(keepends=True),
fromfile=str(filename),
tofile=str(filename))
return ''.join(diff)
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
usage: cmake-tidy format [-h] [--dump-config] [-i] [input]
usage: cmake-tidy format [-h] [--dump-config] [-i] [--diff] [input]

positional arguments:
input CMake file to be formatted

optional arguments:
-h, --help show this help message and exit
--dump-config dump to stdout current settings. Script tries to read
--dump-config Dump to stdout current settings. Script tries to read
settings from `.cmake-tidy.json` or provides default
settings. Precedence of searching `.cmake-tidy.json` is
described on github
-i, --inplace inplace edit specified <input_data> file
-i, --inplace Inplace edit specified <input_data> file
--diff Print to stdout unified diff between original file and
formatted version.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--- <replaced_path>/arguments.cmake
+++ <replaced_path>/arguments.cmake
@@ -7,15 +7,17 @@

# Here we have a line comment with weird stuff like #[===]] $#%!#@$!#@%^^%$&%
set([==[ currently a weird bracket argument introduced
-some 2839697%%*^$& text ]===] fake close and stuff]==] some
- other
- [===[www]===]
- [======[this
+some 2839697%%*^$& text ]===] fake close and stuff]==]
+ some
+ other
+ [===[www]===]
+[======[this
should
be
indented differently
]======]
- "quoted argument with \" escaped quote")
+ "quoted argument with \" escaped quote"
+)

set(CMAKE_CXX_STANDARD_REQUIRED ON)


12 changes: 12 additions & 0 deletions tests/integration/test_cmake_tidy_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ def test_format_should_return_error_when_file_is_read_only_and_inplace_param_is_
normalized_output = normalize(stderr.getvalue())
normalized_output = re.sub(r'File .*arguments', 'File <path>arguments', normalized_output)
verify(normalized_output, self.reporter)

@mock.patch('sys.stdout', new_callable=StringIO)
def test_format_should_provide_unified_diff_to_stdout(self, stdout):
self.assertSuccess(execute_cmake_tidy(command='format',
arguments=['--diff', get_input_file('arguments.cmake')]))
normalized_output = normalize(stdout.getvalue())
normalized_output = self.__replace_with_fake_path('arguments.cmake', normalized_output)
verify(normalized_output, self.reporter)

@staticmethod
def __replace_with_fake_path(filename: str, text: str) -> str:
return re.sub(r' .*' + filename, ' <replaced_path>/' + filename, text)

0 comments on commit 3fd84ad

Please sign in to comment.