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

Improved error messages for CLI commands and fixed shell testing issues #2953

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 49 additions & 1 deletion tools/shell/embedded_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,46 @@ void highlight(char* buffer, char* resultBuf, uint32_t renderWidth, uint32_t cur
strncpy(resultBuf, highlightBuffer.str().c_str(), LINENOISE_MAX_LINE - 1);
}

int damerauLevenshteinDistance(const std::string& s1, const std::string& s2) {
const size_t m = s1.size(), n = s2.size();
MSebanc marked this conversation as resolved.
Show resolved Hide resolved
std::vector<std::vector<size_t>> dp(m + 1, std::vector<size_t>(n + 1, 0));
for (size_t i = 0; i <= m; i++) {
dp[i][0] = i;
}
for (size_t j = 0; j <= n; j++) {
dp[0][j] = j;
}
for (size_t i = 1; i <= m; i++) {
for (size_t j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
if (i > 1 && j > 1 && s1[i - 1] == s2[j - 2] && s1[i - 2] == s2[j - 1]) {
dp[i][j] = std::min(dp[i][j], dp[i - 2][j - 2]);
}
} else {
dp[i][j] = 1 + std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]});
if (i > 1 && j > 1 && s1[i - 1] == s2[j - 2] && s1[i - 2] == s2[j - 1]) {
dp[i][j] = std::min(dp[i][j], dp[i - 2][j - 2] + 1);
}
}
}
}
return dp[m][n];
}

std::string findClosestCommand(std::string lineStr) {
std::string closestCommand = "";
int editDistance = INT_MAX;
MSebanc marked this conversation as resolved.
Show resolved Hide resolved
for (auto& command : shellCommand.commandList) {
int distance = damerauLevenshteinDistance(command, lineStr);
if (distance < editDistance) {
editDistance = distance;
closestCommand = command;
}
}
return closestCommand;
}

int EmbeddedShell::processShellCommands(std::string lineStr) {
if (lineStr == shellCommand.HELP) {
printHelp();
Expand All @@ -239,6 +279,7 @@ int EmbeddedShell::processShellCommands(std::string lineStr) {
setMaxWidth(lineStr.substr(strlen(shellCommand.MAXWIDTH)));
} else {
printf("Error: Unknown command: \"%s\". Enter \":help\" for help\n", lineStr.c_str());
printf("Did you mean: \"%s\"?\n", findClosestCommand(lineStr).c_str());
}
return 0;
}
Expand Down Expand Up @@ -316,7 +357,14 @@ void EmbeddedShell::run() {
if (queryResult->isSuccess()) {
printExecutionResult(*queryResult);
} else {
printf("Error: %s\n", queryResult->getErrorMessage().c_str());
std::string lineStrTrimmed = lineStr;
lineStrTrimmed = lineStrTrimmed.erase(0, lineStr.find_first_not_of(" \t\n\r\f\v"));
if (lineStrTrimmed.find_first_of(" \t\n\r\f\v") == std::string::npos && lineStrTrimmed.length() > 1) {
printf("Error: \"%s\" is not a valid Cypher query. Did you mean to issue a CLI command, e.g., \"%s\"?\n", lineStr.c_str(), findClosestCommand(lineStrTrimmed).c_str());
}
else {
printf("Error: %s\n", queryResult->getErrorMessage().c_str());
}
}
}
} else if (!lineStr.empty() && lineStr[0] != ':') {
Expand Down
3 changes: 2 additions & 1 deletion tools/shell/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def get_tmp_path(tmp_path):
@pytest.fixture
def history_path():
path = os.path.join(KUZU_ROOT, 'tools', 'shell', 'test', 'files')
shutil.rmtree(os.path.join(path, "history.txt"), ignore_errors=True)
if os.path.exists(os.path.join(path, "history.txt")):
os.remove(os.path.join(path, "history.txt"))
return path


Expand Down
2,477 changes: 0 additions & 2,477 deletions tools/shell/test/files/vPerson.csv

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions tools/shell/test/test_shell_basics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import shutil
from test_helper import *
from conftest import ShellTest

Expand Down Expand Up @@ -87,7 +86,8 @@ def test_multi_queries_one_line(temp_db):
)
result = test.run()
result.check_stdout("databases rule")
result.check_stdout(["Error: Parser exception: Invalid input < >: expected rule oC_Cypher (line: 1, offset: 6)"])
result.check_stdout(["Error: Parser exception: mismatched input '<EOF>' expecting {CALL, COMMENT, COPY, EXPORT, DROP, ALTER, BEGIN, COMMIT, COMMIT_SKIP_CHECKPOINT, ROLLBACK, ROLLBACK_SKIP_CHECKPOINT, INSTALL, LOAD, OPTIONAL, MATCH, UNWIND, CREATE, MERGE, SET, DETACH, DELETE, WITH, RETURN} (line: 1, offset: 6)",
'" "'])

# two failing queries
test = (
Expand All @@ -96,10 +96,11 @@ def test_multi_queries_one_line(temp_db):
.statement('RETURN "databases rule" S a; ;')
)
result = test.run()
result.check_stdout(["Error: Parser exception: Invalid input < S>: expected rule oC_Cypher (line: 1, offset: 24)",
result.check_stdout(["Error: Parser exception: Invalid input < S>: expected rule ku_Statements (line: 1, offset: 24)",
'"RETURN "databases rule" S a"',
" ^",
"Error: Parser exception: Invalid input < >: expected rule oC_Cypher (line: 1, offset: 6)"])
"Error: Parser exception: mismatched input '<EOF>' expecting {CALL, COMMENT, COPY, EXPORT, DROP, ALTER, BEGIN, COMMIT, COMMIT_SKIP_CHECKPOINT, ROLLBACK, ROLLBACK_SKIP_CHECKPOINT, INSTALL, LOAD, OPTIONAL, MATCH, UNWIND, CREATE, MERGE, SET, DETACH, DELETE, WITH, RETURN} (line: 1, offset: 6)",
'" "'])


def test_row_truncation(temp_db, csv_path):
Expand Down Expand Up @@ -155,5 +156,6 @@ def test_history_consecutive_repeats(temp_db, history_path):
assert f.readline() == ''
f.close()

shutil.rmtree(os.path.join(history_path, "history.txt"), ignore_errors=True)
if os.path.exists(os.path.join(history_path, "history.txt")):
os.remove(os.path.join(history_path, "history.txt"))

17 changes: 16 additions & 1 deletion tools/shell/test/test_shell_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ def test_max_width(temp_db, csv_path):
result.check_stdout("Node table: LANGUAGE_CODE has been created.")
result.check_not_stdout("| ... |")
result.check_stdout("(1 column)")



def test_bad_command(temp_db):
test = (
ShellTest()
.add_argument(temp_db)
.statement(":maxrows")
.statement(":quiy")
.statement("clearr;")
)
result = test.run()
result.check_stdout('Error: Unknown command: ":maxrows". Enter ":help" for help')
result.check_stdout('Did you mean: ":max_rows"?')
result.check_stdout('Error: Unknown command: ":quiy". Enter ":help" for help')
result.check_stdout('Did you mean: ":quit"?')
result.check_stdout('Error: "clearr;" is not a valid Cypher query. Did you mean to issue a CLI command, e.g., ":clear"?')
7 changes: 4 additions & 3 deletions tools/shell/test/test_shell_control_edit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
import pexpect
import shutil
from test_helper import *
from conftest import ShellTest

Expand Down Expand Up @@ -61,7 +60,8 @@ def test_enter(temp_db, key, history_path):
f = open(os.path.join(history_path, "history.txt"))
assert f.readline() == 'RETURN "databases rule" AS a;\n'
f.close()
shutil.rmtree(os.path.join(history_path, "history.txt"), ignore_errors=True)
if os.path.exists(os.path.join(history_path, "history.txt")):
MSebanc marked this conversation as resolved.
Show resolved Hide resolved
os.remove(os.path.join(history_path, "history.txt"))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -198,7 +198,8 @@ def test_search(temp_db, key, history_path):
# enter should process last match
test.send_finished_statement(KEY_ACTION.ENTER.value)
assert test.shell_process.expect_exact(["| databases rule |", pexpect.EOF]) == 0
shutil.rmtree(os.path.join(history_path, "history.txt"), ignore_errors=True)
if os.path.exists(os.path.join(history_path, "history.txt")):
os.remove(os.path.join(history_path, "history.txt"))

# test starting search with text inputted already
test = ShellTest().add_argument(temp_db)
Expand Down
4 changes: 2 additions & 2 deletions tools/shell/test/test_shell_control_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
import pexpect
import shutil
from test_helper import *
from conftest import ShellTest

Expand Down Expand Up @@ -60,7 +59,8 @@ def test_search_next(temp_db, key, history_path):
test.send_control_statement(key)
test.send_finished_statement(KEY_ACTION.ENTER.value)
assert test.shell_process.expect_exact(["| databases rule |", pexpect.EOF]) == 0
shutil.rmtree(os.path.join(history_path, "history.txt"), ignore_errors=True)
if os.path.exists(os.path.join(history_path, "history.txt")):
os.remove(os.path.join(history_path, "history.txt"))


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions tools/shell/test/test_shell_flags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import shutil
from test_helper import *
from conftest import ShellTest
import os
Expand Down Expand Up @@ -178,7 +177,8 @@ def test_history_path(temp_db, history_path):
assert f.readline() == 'RETURN "kuzu is cool" AS b;\n'
f.close()

shutil.rmtree(os.path.join(history_path, "history.txt"), ignore_errors=True)
if os.path.exists(os.path.join(history_path, "history.txt")):
os.remove(os.path.join(history_path, "history.txt"))


@pytest.mark.parametrize(
Expand Down
Loading