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

[SQLLINE-38] !run support ~ expansion from home #85

Closed
wants to merge 1 commit 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
21 changes: 18 additions & 3 deletions src/main/java/sqlline/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ private void startScript(String line, DispatchCallback callback) {
}

try {
outFile = new OutputFile(filename);
outFile = new OutputFile(expand(filename));
sqlLine.setScriptOutputFile(outFile);
sqlLine.output(sqlLine.loc("script-started", outFile));
callback.setToSuccess();
Expand All @@ -1264,7 +1264,7 @@ public void run(String line, DispatchCallback callback) {

try {
BufferedReader reader =
new BufferedReader(new FileReader(filename));
new BufferedReader(new FileReader(expand(filename)));
try {
// ### NOTE: fix for sf.net bug 879427
StringBuilder cmd = null;
Expand Down Expand Up @@ -1324,6 +1324,21 @@ public void run(String line, DispatchCallback callback) {
}
}

/** Expands "~" to the home directory. */
private static String expand(String filename) {
if (filename.startsWith("~" + File.separator)) {
try {
String home = System.getProperty("user.home");
if (home != null) {
return home + filename.substring(1);
}
} catch (SecurityException e) {
// ignore
}
}
return filename;
}

/**
* Starts or stops saving all output to a file.
*
Expand Down Expand Up @@ -1371,7 +1386,7 @@ private void startRecording(String line, DispatchCallback callback) {
}

try {
outputFile = new OutputFile(filename);
outputFile = new OutputFile(expand(filename));
sqlLine.setRecordOutputFile(outputFile);
sqlLine.output(sqlLine.loc("record-started", outputFile));
callback.setToSuccess();
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/sqlline/OutputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,10 @@ public class OutputFile {
final PrintWriter out;

public OutputFile(String filename) throws IOException {
filename = expand(filename);
file = new File(filename);
out = new PrintWriter(new FileWriter(file), true);
}

/** Expands "~" to the home directory. */
private static String expand(String filename) {
if (filename.startsWith("~" + File.separator)) {
try {
String home = System.getProperty("user.home");
if (home != null) {
return home + filename.substring(1);
}
} catch (SecurityException e) {
// ignore
}
}
return filename;
}

@Override public String toString() {
return file.getAbsolutePath();
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/sqlline/SqlLineArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -37,6 +39,7 @@

import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;

import mockit.Deencapsulation;
Expand Down Expand Up @@ -322,6 +325,38 @@ public void testManual() throws Throwable {
CoreMatchers.containsString(expected));
}

/** Test case for
* <a href="https://github.com/julianhyde/sqlline/issues/38">[SQLLINE-38]
* Expand ~ to user's home directory</a>. */
@Test
public void testRunFromHome() throws Throwable {
File home = new File(System.getProperty("user.home"));
TemporaryFolder tmpFolder = new TemporaryFolder(home);
tmpFolder.create();
try {
File tmpFile = tmpFolder.newFile("test.sql");
Writer fw = new FileWriter(tmpFile);
try {
fw.write("!set outputformat csv\n");
fw.write("values (1, 2, 3, 4, 5);");
fw.flush();
} finally {
fw.close();
}
checkScriptFile("!run ~" + File.separatorChar
+ tmpFolder.getRoot().getName()
+ File.separatorChar + tmpFile.getName(),
false,
equalTo(SqlLine.Status.OK),
allOf(containsString("!set outputformat csv"),
containsString("values (1, 2, 3, 4, 5);")));

} finally {
// will remove tmpFile as well
tmpFolder.delete();
}
}

/**
* Test case for
* <a href="https://github.com/julianhyde/sqlline/issues/26">[SQLLINE-26]
Expand Down