Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for filenames that include spaces #72

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/main/java/sqlline/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1231,14 +1231,14 @@ private void startScript(String line, DispatchCallback callback) {
return;
}

String[] parts = sqlLine.split(line, 2, "Usage: script <filename>");
if (parts == null) {
String filename = sqlLine.dequote(line.substring("script".length() + 1));
if (filename == null) {
callback.setToFailure();
return;
}

try {
outFile = new OutputFile(parts[1]);
outFile = new OutputFile(filename);
sqlLine.setScriptOutputFile(outFile);
sqlLine.output(sqlLine.loc("script-started", outFile));
callback.setToSuccess();
Expand All @@ -1255,17 +1255,16 @@ private void startScript(String line, DispatchCallback callback) {
* @param callback Callback for command status
*/
public void run(String line, DispatchCallback callback) {
String[] parts = sqlLine.split(line, 2, "Usage: run <scriptfile>");
if (parts == null) {
String filename = sqlLine.dequote(line.substring("run".length() + 1));
if (filename.length() == 0) {
callback.setToFailure();
return;
}

List<String> cmds = new LinkedList<String>();

try {
BufferedReader reader =
new BufferedReader(new FileReader(parts[1]));
new BufferedReader(new FileReader(filename));
try {
// ### NOTE: fix for sf.net bug 879427
StringBuilder cmd = null;
Expand Down Expand Up @@ -1365,14 +1364,14 @@ private void startRecording(String line, DispatchCallback callback) {
return;
}

String[] parts = sqlLine.split(line, 2, "Usage: record <filename>");
if (parts == null) {
String filename = sqlLine.dequote(line.substring("record".length() + 1));
if (filename == null) {
callback.setToFailure();
return;
}

try {
outputFile = new OutputFile(parts[1]);
outputFile = new OutputFile(filename);
sqlLine.setRecordOutputFile(outputFile);
sqlLine.output(sqlLine.loc("record-started", outputFile));
callback.setToSuccess();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sqlline/SqlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ Status initArgs(String[] args, DispatchCallback callback) {

// if a script file was specified, run the file and quit
if (opts.getRun() != null) {
dispatch(COMMAND_PREFIX + "run " + opts.getRun(), callback);
dispatch(COMMAND_PREFIX + "run \"" + opts.getRun() + "\"", callback);
if (callback.isFailure()) {
status = Status.OTHER;
}
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/sqlline/SqlLineArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ public void testScriptFileContainsComment() throws Throwable {
allOf(containsString(" 33 "), containsString(" 123 ")));
}

@Test
public void testScriptFilenameWithSpace() throws Throwable {
final String scriptText = "values 10 + 23;\n"
+ "-- a comment\n"
+ "values 100 + 23;\n";

File scriptFile = File.createTempFile("Script with Spaces", ".sql");
scriptFile.deleteOnExit();
PrintStream os = new PrintStream(new FileOutputStream(scriptFile));
os.print(scriptText);
os.close();

Pair pair = runScript(scriptFile, true);
assertThat(pair.status, equalTo(SqlLine.Status.OK));
assertThat(pair.output, allOf(containsString(" 33 "),
containsString(" 123 ")));
}

/**
* Values that contain null.
*/
Expand Down Expand Up @@ -385,6 +403,57 @@ public void testRecordHome() throws Throwable {
}
}

/**
* Test case for [SQLLINE-26], "Flush output for each command when using
* !record command."
*/
@Test
public void testRecordFilenameWithSpace() throws Throwable {
File file = File.createTempFile("sqlline file with spaces", ".log");
checkScriptFile(
"values 1;\n"
+ "!record " + file.getAbsolutePath() + "\n"
+ "!set outputformat csv\n"
+ "values 2;\n"
+ "!record\n"
+ "!set outputformat csv\n"
+ "values 3;\n",
false,
equalTo(SqlLine.Status.OK),
RegexMatcher.of("(?s)1/7 values 1;\n"
+ "\\+-------------\\+\n"
+ "\\| C1 \\|\n"
+ "\\+-------------\\+\n"
+ "\\| 1 \\|\n"
+ "\\+-------------\\+\n"
+ "1 row selected \\([0-9.]+ seconds\\)\n"
+ "2/7 !record .*.log\n"
+ "Saving all output to \".*.log\". Enter \"record\" with no arguments to stop it.\n"
+ "3/7 !set outputformat csv\n"
+ "4/7 values 2;\n"
+ "'C1'\n"
+ "'2'\n"
+ "1 row selected \\([0-9.]+ seconds\\)\n"
+ "5/7 !record\n"
+ "Recording stopped.\n"
+ "6/7 !set outputformat csv\n"
+ "7/7 values 3;\n"
+ "'C1'\n"
+ "'3'\n"
+ "1 row selected \\([0-9.]+ seconds\\)\n.*"));

// Now check that the right stuff got into the file.
assertFileContains(file,
RegexMatcher.of("Saving all output to \".*.log\". "
+ "Enter \"record\" with no arguments to stop it.\n"
+ "3/7 !set outputformat csv\n"
+ "4/7 values 2;\n"
+ "'C1'\n"
+ "'2'\n"
+ "1 row selected \\([0-9.]+ seconds\\)\n"
+ "5/7 !record\n"));
}

private void assertFileContains(File file, RegexMatcher matcher)
throws IOException {
final FileReader fileReader = new FileReader(file);
Expand Down