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

Made semicolons optional for shell commands #55

Merged
merged 1 commit into from Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions cypher-shell/src/main/java/org/neo4j/shell/CypherShell.java
Expand Up @@ -22,14 +22,13 @@
* A possibly interactive shell for evaluating cypher statements.
*/
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, VariableHolder {
private final Logger logger;

// Final space to catch newline
protected static final Pattern cmdNamePattern = Pattern.compile("^\\s*(?<name>[^\\s]+)\\b(?<args>.*)\\s*$");
protected final Map<String, Object> queryParams = new HashMap<>();
private final Logger logger;
private final BoltStateHandler boltStateHandler;
private final PrettyPrinter prettyPrinter;
protected CommandHelper commandHelper;
protected final Map<String, Object> queryParams = new HashMap<>();

public CypherShell(@Nonnull Logger logger) {
this(logger, new BoltStateHandler(), new PrettyPrinter(logger.getFormat()));
Expand All @@ -44,6 +43,18 @@ protected CypherShell(@Nonnull Logger logger,
addRuntimeHookToResetShell();
}

/**
* @param text to trim
* @return text without trailing semicolons
*/
static String stripTrailingSemicolons(@Nonnull String text) {
int end = text.length();
while (end > 0 && text.substring(0, end).endsWith(";")) {
end -= 1;
}
return text.substring(0, end);
}

@Override
public void execute(@Nonnull final String cmdString) throws ExitException, CommandException {
// See if it's a shell command
Expand Down Expand Up @@ -95,7 +106,7 @@ protected Optional<CommandExecutable> getCommandExecutable(@Nonnull final String
return Optional.empty();
}

return Optional.of(() -> cmd.execute(args));
return Optional.of(() -> cmd.execute(stripTrailingSemicolons(args)));
}

protected void executeCmd(@Nonnull final CommandExecutable cmdExe) throws ExitException, CommandException {
Expand Down
25 changes: 24 additions & 1 deletion cypher-shell/src/test/java/org/neo4j/shell/CypherShellTest.java
Expand Up @@ -151,10 +151,33 @@ public void executeShouldPrintResult() throws CommandException {
verify(logger).printOut(contains("999"));
}

@Test
public void shouldStripEndingSemicolonsFromCommand() throws Exception {
// Should not throw
offlineTestShell.getCommandExecutable(":help;;").get().execute();
verify(logger).printOut(contains("Available commands:"));
}

@Test
public void shouldStripEndingSemicolonsFromCommandArgs() throws Exception {
// Should not throw
offlineTestShell.getCommandExecutable(":help param;;").get().execute();
verify(logger).printOut(contains("usage: "));
}

@Test
public void testStripSemicolons() throws Exception {
assertEquals("", CypherShell.stripTrailingSemicolons(""));
assertEquals("nothing", CypherShell.stripTrailingSemicolons("nothing"));
assertEquals("", CypherShell.stripTrailingSemicolons(";;;;;"));
assertEquals("hej", CypherShell.stripTrailingSemicolons("hej;"));
assertEquals(";bob", CypherShell.stripTrailingSemicolons(";bob;;"));
}

@Test
public void shouldParseCommandsAndArgs() {
assertTrue(offlineTestShell.getCommandExecutable(":help").isPresent());
assertTrue(offlineTestShell.getCommandExecutable(":help :set").isPresent());
assertTrue(offlineTestShell.getCommandExecutable(":help :param").isPresent());
assertTrue(offlineTestShell.getCommandExecutable(":param \"A piece of string\"").isPresent());
}

Expand Down