Skip to content

Commit 055bd65

Browse files
committed
Fix handling of commands in parentheses
1 parent 7c6871a commit 055bd65

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
This project uses [Semantic Versioning](https://semver.org) starting from version
88
1.0.0.
99

10-
## [unreleased]
10+
## [1.0.2] - 2019-01-09
11+
12+
## Fixed
13+
14+
- Fixed handling of commands enclosed in parentheses (e.g., "if defined foo (exit /b 0)")
1115

1216
## [1.0.1] - 2019-01-09
1317

callgraph/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def AddCommand(self, command):
2323
self.commands_counter[command.command] += 1
2424

2525
def __repr__(self):
26-
return "[{0} (terminating: {1}, noop: {2})] {3}".format(self.number, self.terminating, self.noop, self.text)
26+
return "[{0} (terminating: {1}, noop: {2}, commands: {3})] {4}".format(self.number, self.terminating, self.noop, self.commands, self.text)
2727

2828
def __eq__(self, other):
2929
return other is not None and self.number == other.number and self.text == other.text and self.terminating == other.terminating
@@ -138,6 +138,10 @@ def _AnnotateNode(self, node):
138138
continue
139139

140140
for i, token in enumerate(tokens):
141+
# Remove open/close parenthesis from the start/end of the command, to deal with inline commands
142+
# enclosed in parentheses.
143+
token = token.lstrip("(").rstrip(")")
144+
141145
# Comment; stop processing the rest of the line.
142146
if token.startswith("::") or token == "rem" or token.startswith("@::") or token == "@rem":
143147
line.noop = True

tests/test_core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,22 @@ def test_multiple_exit_nodes(self):
301301
self.assertTrue(foo.is_exit_node)
302302
self.assertTrue(foo.is_last_node)
303303

304+
def test_no_nested_with_inline_if(self):
305+
code = """
306+
call :Filenotempty foo
307+
echo %ERRORLEVEL%
308+
exit
309+
:filenotempty
310+
If %~z1 EQU 0 (Exit /B 1) Else (Exit /B 0)
311+
:unused
312+
echo Will never run.
313+
""".split("\n")
314+
call_graph = CallGraph.Build(code, self.devnull)
315+
self.assertEqual(3, len(call_graph.nodes))
316+
317+
file_not_empty = call_graph.nodes["filenotempty"]
318+
self.assertEqual(0, len(file_not_empty.connections), file_not_empty.code)
319+
304320

305321
if __name__ == "__main__":
306322
unittest.main()

0 commit comments

Comments
 (0)