Skip to content

Commit

Permalink
Added TestException and improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rfdavid committed May 30, 2023
1 parent c93f14d commit 64ce7fd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/include/common/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,10 @@ class InterruptException : public Exception {
explicit InterruptException() : Exception("Interrupted."){};
};

class TestException : public Exception {
public:
explicit TestException(const std::string& msg) : Exception("Test exception: " + msg){};

Check warning on line 102 in src/include/common/exception.h

View check run for this annotation

Codecov / codecov/patch

src/include/common/exception.h#L102

Added line #L102 was not covered by tests
};

} // namespace common
} // namespace kuzu
14 changes: 9 additions & 5 deletions test/include/test_runner/test_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ class LogicToken {

class TestParser {
public:
TestParser() : testGroup(std::make_unique<TestGroup>()) {}
std::unique_ptr<TestGroup> parseTestFile(const std::string& path);
explicit TestParser(const std::string& path)
: testGroup{std::make_unique<TestGroup>()}, path{path} {}
std::unique_ptr<TestGroup> parseTestFile();

private:
std::string path;
std::ifstream fileStream;
std::streampos previousFilePosition;
std::string line;
Expand All @@ -65,7 +67,7 @@ class TestParser {
std::string paramsToString();
std::string extractTextBeforeNextStatement();
LogicToken currentToken;
void openFile(const std::string& path);
void openFile();
void tokenize();
void parseHeader();
void parseBody();
Expand All @@ -80,8 +82,10 @@ class TestParser {
return static_cast<bool>(getline(fileStream, line));
}
inline void checkMinimumParams(int minimumParams) {
if (currentToken.params.size() < minimumParams) {
throw common::Exception("Invalid number of parameters for statement [" + line + "]");
if (currentToken.params.size() - 1 < minimumParams) {
throw common::TestException("Minimum number of parameters is " +
std::to_string(minimumParams) + ". [" + path + ":" + line +
"]");
}
}
TestStatement* extractStatement(TestStatement* currentStatement);
Expand Down
8 changes: 4 additions & 4 deletions test/runner/e2e_read_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class EndToEndReadTest : public DBTest {
};

void parseAndRegisterTestGroup(const std::string& path) {
auto testParser = std::make_unique<TestParser>();
auto testGroup = std::move(testParser->parseTestFile(path));
auto testParser = std::make_unique<TestParser>(path);
auto testGroup = std::move(testParser->parseTestFile());
if (testGroup->isValid() && testGroup->hasStatements()) {
auto dataset = testGroup->dataset;
auto testCases = std::move(testGroup->testCases);
Expand All @@ -46,7 +46,7 @@ void parseAndRegisterTestGroup(const std::string& path) {
});
}
} else {
throw Exception("Invalid test file");
throw TestException("Invalid test file [" + path + "].");
}
}

Expand All @@ -70,7 +70,7 @@ int main(int argc, char** argv) {
}
path = TestHelper::appendKuzuRootPath(path);
if (!FileUtils::fileOrPathExists(path)) {
throw Exception("Test directory not exists! [" + path + "].");
throw TestException("Test path not exists [" + path + "].");
}
scanTestFiles(path);
return RUN_ALL_TESTS();
Expand Down
22 changes: 8 additions & 14 deletions test/test_runner/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ using namespace kuzu::common;
namespace kuzu {
namespace testing {

std::unique_ptr<TestGroup> TestParser::parseTestFile(const std::string& path) {
openFile(path);
std::unique_ptr<TestGroup> TestParser::parseTestFile() {
openFile();
parseHeader();
if (testGroup->skipTest) {
return std::move(testGroup);
}
if (!testGroup->isValid()) {
throw Exception("Invalid test header");
throw TestException("Invalid test header [" + path + "].");
}
parseBody();
return std::move(testGroup);
Expand Down Expand Up @@ -53,7 +53,7 @@ void TestParser::parseHeader() {
return;
}
default: {
throw Exception("Invalid statement in test header");
throw TestException("Invalid test header statement [" + path + ":" + line + "].");
}
}
}
Expand Down Expand Up @@ -116,7 +116,6 @@ TestStatement* TestParser::extractStatement(TestStatement* statement) {
}
case TokenType::STATEMENT:
case TokenType::QUERY: {
checkMinimumParams(1);
std::string query = paramsToString();
replaceVariables(query);
statement->query = query;
Expand Down Expand Up @@ -151,7 +150,7 @@ TestStatement* TestParser::extractStatement(TestStatement* statement) {
break;
}
default: {
throw Exception("Invalid statement [" + line + "] in test file");
throw TestException("Invalid statement [" + path + ":" + line + "].");
}
}
nextLine();
Expand Down Expand Up @@ -212,7 +211,7 @@ void TestParser::addStatementBlock(const std::string& blockName, const std::stri
std::make_unique<TestStatement>(*statementPtr));
}
} else {
throw Exception("Statement block not found [" + blockName + "]");
throw TestException("Statement block not found [" + path + ":" + blockName + "].");
}
}

Expand All @@ -232,14 +231,9 @@ TokenType getTokenType(const std::string& input) {
}
}

void TestParser::openFile(const std::string& path) {
void TestParser::openFile() {
if (access(path.c_str(), 0) != 0) {
throw Exception("Test file not exists! [" + path + "].");
}
struct stat status {};
stat(path.c_str(), &status);
if (status.st_mode & S_IFDIR) {
throw Exception("Test file is a directory. [" + path + "].");
throw TestException("Test file not exists [" + path + "].");
}
fileStream.open(path);
}
Expand Down

0 comments on commit 64ce7fd

Please sign in to comment.