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

Commit f6ff76a

Browse files
authored
Merge pull request #58 from spacecowboy/fix-welcome
Welcome message should only be printed in interactive mode
2 parents 3eeaf24 + 1be6a47 commit f6ff76a

File tree

9 files changed

+57
-28
lines changed

9 files changed

+57
-28
lines changed

cypher-shell/src/main/java/org/neo4j/shell/Main.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ public static void main(String[] args) {
4747
this.out = out;
4848
}
4949

50-
private static void printWelcomeMessage(@Nonnull Logger logger,
51-
@Nonnull ConnectionConfig connectionConfig,
52-
@Nonnull String serverVersion) {
50+
static String getWelcomeMessage(@Nonnull ConnectionConfig connectionConfig,
51+
@Nonnull String serverVersion) {
5352
String neo4j = "Neo4j";
5453
if (!serverVersion.isEmpty()) {
5554
neo4j += " " + serverVersion;
@@ -65,15 +64,14 @@ private static void printWelcomeMessage(@Nonnull Logger logger,
6564
.bold().append(connectionConfig.username()).boldOff();
6665
}
6766

68-
logger.printIfVerbose(welcomeMessage
67+
return welcomeMessage
6968
.append(".\nType ")
7069
.bold().append(Help.COMMAND_NAME).boldOff()
7170
.append(" for a list of available commands or ")
7271
.bold().append(Exit.COMMAND_NAME).boldOff()
7372
.append(" to exit the shell.")
7473
.append("\nNote that Cypher queries must end with a ")
75-
.bold().append("semicolon.").boldOff()
76-
.formattedString());
74+
.bold().append("semicolon.").boldOff().formattedString();
7775
}
7876

7977
void startShell(@Nonnull CliArgs cliArgs) {
@@ -97,9 +95,7 @@ void startShell(@Nonnull CliArgs cliArgs) {
9795

9896
shell.setCommandHelper(commandHelper);
9997

100-
printWelcomeMessage(logger, connectionConfig, shell.getServerVersion());
101-
102-
int code = shellRunner.runUntilEnd();
98+
int code = shellRunner.runUntilEnd(getWelcomeMessage(connectionConfig, shell.getServerVersion()));
10399
System.exit(code);
104100
} catch (Throwable e) {
105101
logger.printError(e);

cypher-shell/src/main/java/org/neo4j/shell/ShellRunner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public interface ShellRunner {
2020
/**
2121
* Run and handle user input until end of file
2222
*
23+
* @param welcomeMessage
24+
* a formatted welcome message which may be printed, if suitable
2325
* @return error code to exit with
2426
*/
25-
int runUntilEnd();
27+
int runUntilEnd(@Nonnull String welcomeMessage);
2628

2729
/**
2830
* @return an object which can provide the history of commands executed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ private ConsoleReader setupConsoleReader(@Nonnull Logger logger,
7070
}
7171

7272
@Override
73-
public int runUntilEnd() {
73+
public int runUntilEnd(@Nonnull String welcomeMessage) {
7474
int exitCode = 0;
7575
boolean running = true;
76+
77+
logger.printIfVerbose(welcomeMessage);
78+
7679
while (running) {
7780
try {
7881
for (String statement : readUntilStatement()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public NonInteractiveShellRunner(@Nonnull FailBehavior failBehavior,
4040
}
4141

4242
@Override
43-
public int runUntilEnd() {
43+
public int runUntilEnd(@Nonnull String welcomeMessage) {
4444
List<String> statements;
4545
try {
4646
new BufferedReader(new InputStreamReader(inputStream))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public StringShellRunner(@Nonnull CliArgs cliArgs,
3030
}
3131

3232
@Override
33-
public int runUntilEnd() {
33+
public int runUntilEnd(@Nonnull String welcomeMessage) {
3434
int exitCode = 0;
3535
try {
3636
executer.execute(cypher.trim());

cypher-shell/src/test/java/org/neo4j/shell/MainTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.mockito.Mockito.mock;
2020
import static org.mockito.Mockito.times;
2121
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.when;
23+
import static org.neo4j.shell.Main.getWelcomeMessage;
2224

2325
public class MainTest {
2426

@@ -164,4 +166,15 @@ public void connectInteractivelyTriesOnlyOnceIfUserPassExists() throws Exception
164166
verify(shell, times(1)).connect(connectionConfig);
165167
}
166168
}
169+
170+
@Test
171+
public void welcomeMessageTest() {
172+
when(connectionConfig.username()).thenReturn("bob");
173+
when(connectionConfig.driverUrl()).thenReturn("bolt://some.place.com:99");
174+
175+
assertEquals("Connected to Neo4j 3.1.0-Beta99 at @|BOLD bolt://some.place.com:99|@ as user @|BOLD bob|@.\n" +
176+
"Type @|BOLD :help|@ for a list of available commands or @|BOLD :exit|@ to exit the shell.\n" +
177+
"Note that Cypher queries must end with a @|BOLD semicolon.|@",
178+
getWelcomeMessage(connectionConfig, "3.1.0-Beta99"));
179+
}
167180
}

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testSimple() throws Exception {
7676
"good2;\n";
7777
InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, logger, statementParser,
7878
new ByteArrayInputStream(input.getBytes()), historyFile);
79-
runner.runUntilEnd();
79+
runner.runUntilEnd("");
8080

8181
verify(cmdExecuter).execute("good1;");
8282
verify(cmdExecuter).execute("\ngood2;");
@@ -93,7 +93,7 @@ public void runUntilEndShouldKeepGoingOnErrors() throws IOException, CommandExce
9393
InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, logger, statementParser,
9494
new ByteArrayInputStream(input.getBytes()), historyFile);
9595

96-
int code = runner.runUntilEnd();
96+
int code = runner.runUntilEnd("");
9797

9898
assertEquals("Wrong exit code", 0, code);
9999

@@ -120,7 +120,7 @@ public void runUntilEndShouldStopOnExitExceptionAndReturnCode() throws IOExcepti
120120

121121
doThrow(new ExitException(1234)).when(cmdExecuter).execute(contains("exit;"));
122122

123-
int code = runner.runUntilEnd();
123+
int code = runner.runUntilEnd("");
124124

125125
assertEquals("Wrong exit code", 1234, code);
126126

@@ -145,7 +145,7 @@ public void historyIsRecorded() throws Exception {
145145
new ByteArrayInputStream(input.getBytes()), historyFile);
146146

147147
// when
148-
runner.runUntilEnd();
148+
runner.runUntilEnd("");
149149

150150
// then
151151
Historian historian = runner.getHistorian();
@@ -302,12 +302,27 @@ public void multilineRequiresNewLineOrSemicolonToEnd() throws Exception {
302302
InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, logger, new ShellStatementParser(), inputStream, historyFile);
303303

304304
// when
305-
runner.runUntilEnd();
305+
runner.runUntilEnd("");
306306

307307
// then
308308
verifyNoMoreInteractions(cmdExecuter);
309309
}
310310

311+
@Test
312+
public void printsWelcomeMessage() throws Exception {
313+
// given
314+
String inputString = "\nCREATE (n:Person) RETURN n\n;\n";
315+
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
316+
InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, logger,
317+
new ShellStatementParser(), inputStream, historyFile);
318+
319+
// when
320+
runner.runUntilEnd("Welcome to cypher-shell!");
321+
322+
// then
323+
verify(logger).printIfVerbose("Welcome to cypher-shell!");
324+
}
325+
311326
@Test
312327
public void multilineEndsOnSemicolonOnNewLine() throws Exception {
313328
// given
@@ -316,7 +331,7 @@ public void multilineEndsOnSemicolonOnNewLine() throws Exception {
316331
InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, logger, new ShellStatementParser(), inputStream, historyFile);
317332

318333
// when
319-
runner.runUntilEnd();
334+
runner.runUntilEnd("");
320335

321336
// then
322337
verify(cmdExecuter).execute("CREATE (n:Person) RETURN n\n;");
@@ -330,7 +345,7 @@ public void multilineEndsOnSemicolonOnSameLine() throws Exception {
330345
InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, logger, new ShellStatementParser(), inputStream, historyFile);
331346

332347
// when
333-
runner.runUntilEnd();
348+
runner.runUntilEnd("");
334349

335350
// then
336351
verify(cmdExecuter).execute("CREATE (n:Person) RETURN n;");
@@ -364,7 +379,7 @@ public void testSignalHandleDuringExecution() throws Exception {
364379
new ShellStatementParser(), inputStream, historyFile);
365380

366381
// during
367-
Thread t = new Thread(runner::runUntilEnd);
382+
Thread t = new Thread(() -> runner.runUntilEnd(""));
368383
t.start();
369384

370385
// wait until execution has begun

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void testSimple() throws Exception {
5151
cmdExecuter,
5252
logger, statementParser,
5353
new ByteArrayInputStream(input.getBytes()));
54-
int code = runner.runUntilEnd();
54+
int code = runner.runUntilEnd("");
5555

5656
assertEquals("Exit code incorrect", 0, code);
5757
verify(logger, times(0)).printError(anyString());
@@ -69,7 +69,7 @@ public void testFailFast() throws Exception {
6969
logger, statementParser,
7070
new ByteArrayInputStream(input.getBytes()));
7171

72-
int code = runner.runUntilEnd();
72+
int code = runner.runUntilEnd("");
7373

7474
assertEquals("Exit code incorrect", 1, code);
7575
verify(logger).printError(badLineError);
@@ -87,7 +87,7 @@ public void testFailAtEnd() throws Exception {
8787
logger, statementParser,
8888
new ByteArrayInputStream(input.getBytes()));
8989

90-
int code = runner.runUntilEnd();
90+
int code = runner.runUntilEnd("");
9191

9292
assertEquals("Exit code incorrect", 1, code);
9393
verify(logger, times(2)).printError(badLineError);
@@ -111,7 +111,7 @@ public void runUntilEndExitsImmediatelyOnParseError() throws Exception {
111111
new ByteArrayInputStream(input.getBytes()));
112112

113113
// when
114-
int code = runner.runUntilEnd();
114+
int code = runner.runUntilEnd("");
115115

116116
// then
117117
assertEquals(1, code);
@@ -134,7 +134,7 @@ public void runUntilEndExitsImmediatelyOnExitCommand() throws Exception {
134134
// when
135135
doThrow(new ExitException(99)).when(cmdExecuter).execute(anyString());
136136

137-
int code = runner.runUntilEnd();
137+
int code = runner.runUntilEnd("");
138138

139139
// then
140140
assertEquals(99, code);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void cypherShouldBePassedToRun() throws IOException, CommandException {
3939
String cypherString = "nonsense string";
4040
StringShellRunner runner = new StringShellRunner(CliArgHelper.parse(cypherString), statementExecuter, logger);
4141

42-
int code = runner.runUntilEnd();
42+
int code = runner.runUntilEnd("");
4343

4444
assertEquals("Wrong exit code", 0, code);
4545
verify(statementExecuter).execute("nonsense string");
@@ -53,7 +53,7 @@ public void errorsShouldThrow() throws IOException, CommandException {
5353

5454
StringShellRunner runner = new StringShellRunner(CliArgHelper.parse("nan anana"), statementExecuter, logger);
5555

56-
int code = runner.runUntilEnd();
56+
int code = runner.runUntilEnd("");
5757

5858
assertEquals("Wrong exit code", 1, code);
5959
verify(logger).printError(kaboom);

0 commit comments

Comments
 (0)