Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 5eb40fa

Browse files
Execute unfinished statements in cypher shell non interactive and file mode
Fixes a bug in cypher shell where unfinished statements were not executed in non interactive and file mode. This lead to quietly ignoring the last statement if it was not semicolon terminated. Co-authored-by: AakashSorathiya <[email protected]>
1 parent aa773a4 commit 5eb40fa

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

cypher-shell/src/main/java/org/neo4j/shell/cli/NonInteractiveShellRunner.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public int runUntilEnd()
7777
}
7878

7979
int exitCode = 0;
80+
81+
// Executing this could fail but we try anyway to avoid hiding errors
82+
statementParser.incompleteStatement().ifPresent( statements::add );
83+
8084
for ( String statement : statements )
8185
{
8286
try

cypher-shell/src/main/java/org/neo4j/shell/parser/ShellStatementParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,11 @@ public void reset()
292292
parsedStatements.clear();
293293
awaitedRightDelimiter = Optional.empty();
294294
}
295+
296+
@Nonnull
297+
@Override
298+
public Optional<String> incompleteStatement()
299+
{
300+
return Optional.of( statement.toString().trim() ).filter( s -> !s.isEmpty() );
301+
}
295302
}

cypher-shell/src/main/java/org/neo4j/shell/parser/StatementParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.neo4j.shell.parser;
2121

2222
import java.util.List;
23+
import java.util.Optional;
2324
import javax.annotation.Nonnull;
2425

2526
/**
@@ -58,4 +59,12 @@ public interface StatementParser
5859
* Reset the state of the Parser, removing any and all state it has.
5960
*/
6061
void reset();
62+
63+
/**
64+
* Returns any incomplete statement.
65+
*
66+
* @return the statement that is currently being parsed but has not completed, if any
67+
*/
68+
@Nonnull
69+
Optional<String> incompleteStatement();
6170
}

cypher-shell/src/test/java/org/neo4j/shell/cli/NonInteractiveShellRunnerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,22 @@ public void nonInteractiveHasNoHistory() throws Exception
181181
// when then
182182
assertEquals( Historian.empty, runner.getHistorian() );
183183
}
184+
185+
@Test
186+
public void shouldTryToExecuteIncompleteStatements() throws CommandException
187+
{
188+
String input = "good1;\nno semicolon here\n// A comment at end";
189+
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(
190+
FailBehavior.FAIL_FAST,
191+
cmdExecuter,
192+
logger, statementParser,
193+
new ByteArrayInputStream( input.getBytes() ) );
194+
int code = runner.runUntilEnd();
195+
196+
assertEquals( "Exit code incorrect", 0, code );
197+
verify( logger, times( 0 ) ).printError( anyString() );
198+
verify( cmdExecuter ).execute( "good1;" );
199+
verify( cmdExecuter ).execute( "no semicolon here" );
200+
verifyNoMoreInteractions( cmdExecuter );
201+
}
184202
}

0 commit comments

Comments
 (0)