Skip to content

Commit 3351e14

Browse files
fix(core-clp): Construct file paths in test cases relative to test file location rather than working directory (fixes #124). (#1114)
Co-authored-by: kirkrodrigues <[email protected]>
1 parent 1f2a728 commit 3351e14

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

components/core/tests/test-GrepCore.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <filesystem>
12
#include <string>
23

34
#include <catch2/catch_test_macros.hpp>
@@ -17,9 +18,20 @@ using log_surgeon::SchemaParser;
1718
using log_surgeon::SchemaVarAST;
1819
using std::string;
1920

21+
namespace {
22+
[[nodiscard]] auto get_tests_dir() -> std::filesystem::path;
23+
24+
auto get_tests_dir() -> std::filesystem::path {
25+
std::filesystem::path const current_file_path{__FILE__};
26+
return std::filesystem::canonical(current_file_path.parent_path());
27+
}
28+
} // namespace
29+
2030
TEST_CASE("get_bounds_of_next_potential_var", "[get_bounds_of_next_potential_var]") {
31+
auto const test_schema_files_dir = get_tests_dir() / "test_schema_files";
32+
auto const search_schema_path = test_schema_files_dir / "search_schema.txt";
2133
ByteLexer lexer;
22-
load_lexer_from_file("../tests/test_schema_files/search_schema.txt", lexer);
34+
load_lexer_from_file(search_schema_path.string(), lexer);
2335

2436
string str;
2537
size_t begin_pos;

components/core/tests/test-ParserWithUserSchema.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include <sys/stat.h>
55

6+
#include <filesystem>
67
#include <string>
78
#include <utility>
89

9-
#include <boost/filesystem.hpp>
1010
#include <catch2/catch_test_macros.hpp>
1111
#include <catch2/matchers/catch_matchers.hpp>
1212
#include <log_surgeon/LogParser.hpp>
@@ -28,6 +28,25 @@ using log_surgeon::SchemaAST;
2828
using log_surgeon::SchemaVarAST;
2929
using log_surgeon::Token;
3030

31+
namespace {
32+
[[nodiscard]] auto get_tests_dir() -> std::filesystem::path;
33+
[[nodiscard]] auto get_test_schema_files_dir() -> std::filesystem::path;
34+
[[nodiscard]] auto get_test_queries_dir() -> std::filesystem::path;
35+
36+
auto get_tests_dir() -> std::filesystem::path {
37+
std::filesystem::path const current_file_path{__FILE__};
38+
return std::filesystem::canonical(current_file_path.parent_path());
39+
}
40+
41+
auto get_test_schema_files_dir() -> std::filesystem::path {
42+
return get_tests_dir() / "test_schema_files";
43+
}
44+
45+
auto get_test_queries_dir() -> std::filesystem::path {
46+
return get_tests_dir() / "test_search_queries";
47+
}
48+
} // namespace
49+
3150
std::unique_ptr<SchemaAST> generate_schema_ast(std::string const& schema_file) {
3251
std::unique_ptr<SchemaAST> schema_ast = log_surgeon::SchemaParser::try_schema_file(schema_file);
3352
REQUIRE(schema_ast.get() != nullptr);
@@ -79,56 +98,60 @@ void decompress(std::string archive_dir, std::string output_dir) {
7998
}
8099

81100
TEST_CASE("Test error for missing schema file", "[LALR1Parser][SchemaParser]") {
82-
std::string file_path = "../tests/test_schema_files/missing_schema.txt";
83-
std::string file_name = boost::filesystem::weakly_canonical(file_path).string();
101+
auto const file_path = get_test_schema_files_dir() / "missing_schema.txt";
102+
auto const file_path_string = file_path.string();
84103
REQUIRE_THROWS_WITH(
85-
generate_schema_ast(file_path),
86-
"Failed to read '" + file_path + "', error_code="
104+
generate_schema_ast(file_path_string),
105+
"Failed to read '" + file_path_string + "', error_code="
87106
+ std::to_string(static_cast<int>(log_surgeon::ErrorCode::FileNotFound))
88107
);
89108
}
90109

91110
TEST_CASE("Test error for empty schema file", "[LALR1Parser][SchemaParser]") {
92-
std::string file_path = "../tests/test_schema_files/empty_schema.txt";
111+
auto const file_path = get_test_schema_files_dir() / "empty_schema.txt";
93112
REQUIRE_THROWS_WITH(
94-
generate_schema_ast(file_path),
113+
generate_schema_ast(file_path.string()),
95114
"Schema:1:1: error: empty file\n"
96115
" \n"
97116
"^\n"
98117
);
99118
}
100119

101120
TEST_CASE("Test error for colon missing schema file", "[LALR1Parser][SchemaParser]") {
102-
std::string file_path = "../tests/test_schema_files/colon_missing_schema.txt";
121+
auto const file_path = get_test_schema_files_dir() / "colon_missing_schema.txt";
103122
REQUIRE_THROWS_WITH(
104-
generate_schema_ast(file_path),
123+
generate_schema_ast(file_path.string()),
105124
"Schema:3:4: error: expected '>',':','AlphaNumeric' before ' ' token\n"
106125
" int [0-9]+\n"
107126
" ^\n"
108127
);
109128
}
110129

111130
TEST_CASE("Test error for multi-character tokens in schema file", "[LALR1Parser][SchemaParser]") {
112-
std::string file_path = "../tests/test_schema_files/schema_with_multicharacter_token_error.txt";
131+
auto const file_path
132+
= get_test_schema_files_dir() / "schema_with_multicharacter_token_error.txt";
113133
REQUIRE_THROWS_WITH(
114-
generate_schema_ast(file_path),
134+
generate_schema_ast(file_path.string()),
115135
"Schema:2:11: error: expected ':' before ' ' token\n"
116136
" delimiters : \\r\\n\n"
117137
" ^\n"
118138
);
119139
}
120140

121141
TEST_CASE("Test creating schema parser", "[LALR1Parser][SchemaParser]") {
122-
generate_schema_ast("../tests/test_schema_files/easy_schema.txt");
142+
auto const schema_file_path = get_test_schema_files_dir() / "easy_schema.txt";
143+
generate_schema_ast(schema_file_path.string());
123144
}
124145

125146
TEST_CASE("Test creating log parser with delimiters", "[LALR1Parser][LogParser]") {
126-
generate_log_parser("../tests/test_schema_files/schema_with_delimiters.txt");
147+
auto const schema_file_path = get_test_schema_files_dir() / "schema_with_delimiters.txt";
148+
generate_log_parser(schema_file_path.string());
127149
}
128150

129151
TEST_CASE("Test creating log parser without delimiters", "[LALR1Parser][LogParser]") {
152+
auto const schema_file_path = get_test_schema_files_dir() / "schema_without_delimiters.txt";
130153
REQUIRE_THROWS_WITH(
131-
generate_log_parser("../tests/test_schema_files/schema_without_delimiters.txt"),
154+
generate_log_parser(schema_file_path.string()),
132155
"When using --schema-path, \"delimiters:\" line must be used."
133156
);
134157
}
@@ -158,10 +181,10 @@ TEST_CASE("Test creating log parser without delimiters", "[LALR1Parser][LogParse
158181

159182
TEST_CASE("Test lexer", "[Search]") {
160183
ByteLexer lexer;
161-
std::string schema_file_name = "../tests/test_schema_files/search_schema.txt";
162-
std::string schema_file_path = boost::filesystem::weakly_canonical(schema_file_name).string();
163-
load_lexer_from_file(schema_file_path, lexer);
164-
FileReader file_reader{"../tests/test_search_queries/easy.txt"};
184+
auto const schema_file_path = get_test_schema_files_dir() / "search_schema.txt";
185+
load_lexer_from_file(schema_file_path.string(), lexer);
186+
auto const query_file_path = get_test_queries_dir() / "easy.txt";
187+
FileReader file_reader{query_file_path.string()};
165188
LogSurgeonReader reader_wrapper(file_reader);
166189
log_surgeon::ParserInputBuffer parser_input_buffer;
167190
parser_input_buffer.read_if_safe(reader_wrapper);

0 commit comments

Comments
 (0)