Skip to content

Commit 51478a4

Browse files
authored
Merge branch 'main' into test_ci_trigger_on_pr
2 parents a75d462 + 296baba commit 51478a4

File tree

7 files changed

+178
-85
lines changed

7 files changed

+178
-85
lines changed

core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ endif ()
7373
if (BUILD_TESTS)
7474
enable_testing()
7575
include(FetchContent)
76+
# Save the current BUILD_SHARED_LIBS value before forcing it OFF for Catch2
77+
set(OMEGA_EDIT_ORIGINAL_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
7678
# Force Catch2 to build statically so we don't have to worry about the path to the Catch2 DDL when running tests
7779
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
7880
FetchContent_Declare(
@@ -82,6 +84,8 @@ if (BUILD_TESTS)
8284
GIT_TAG v3.8.1
8385
)
8486
FetchContent_MakeAvailable(Catch2)
87+
# Restore the original BUILD_SHARED_LIBS value for the main project
88+
set(BUILD_SHARED_LIBS ${OMEGA_EDIT_ORIGINAL_BUILD_SHARED_LIBS} CACHE BOOL "" FORCE)
8589
add_subdirectory(src/tests)
8690
endif ()
8791

core/src/lib/edit.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@
3434
#include <io.h>
3535

3636
#define close _close
37-
#define min std::min
38-
#define max std::max
37+
// Undefine Windows min/max macros to avoid conflicts with std::min/max
38+
#ifdef min
39+
#undef min
40+
#endif
41+
#ifdef max
42+
#undef max
43+
#endif
3944
#else
4045

4146
#include <unistd.h>
4247

43-
#define min std::min
44-
#define max std::max
4548
#endif
4649

4750
namespace {
@@ -498,7 +501,7 @@ int64_t omega_edit_delete(omega_session_t *session_ptr, int64_t offset, int64_t
498501
const auto computed_file_size = omega_session_get_computed_file_size(session_ptr);
499502
return (omega_session_changes_paused(session_ptr) == 0) && 0 < length && offset < computed_file_size
500503
? update_(session_ptr, del_(1 + omega_session_get_num_changes(session_ptr), offset,
501-
min(length, static_cast<int64_t>(computed_file_size) - offset),
504+
std::min(length, static_cast<int64_t>(computed_file_size) - offset),
502505
determine_change_transaction_bit_(session_ptr)))
503506
: 0;
504507
}
@@ -564,7 +567,7 @@ int omega_edit_save_segment(omega_session_t *session_ptr, const char *file_path,
564567
assert(file_path);
565568
assert(0 <= offset);
566569
const auto computed_file_size = omega_session_get_computed_file_size(session_ptr);
567-
const auto adjusted_length = length <= 0 ? computed_file_size - offset : min(length, computed_file_size - offset);
570+
const auto adjusted_length = length <= 0 ? computed_file_size - offset : std::min(length, computed_file_size - offset);
568571
if (adjusted_length < 0) {
569572
LOG_ERROR("invalid offset: " << offset << ", length: " << length << ", adjusted_length: " << adjusted_length
570573
<< ", computed_file_size: " << computed_file_size);
@@ -641,8 +644,8 @@ int omega_edit_save_segment(omega_session_t *session_ptr, const char *file_path,
641644
if (bytes_written >= adjusted_length) { break; }
642645

643646
// Calculate how much to write from this segment
644-
auto segment_start = max(offset - write_offset, int64_t(0));
645-
auto segment_length = min(adjusted_length - bytes_written, segment->computed_length - segment_start);
647+
auto segment_start = std::max(offset - write_offset, int64_t(0));
648+
auto segment_length = std::min(adjusted_length - bytes_written, segment->computed_length - segment_start);
646649

647650
switch (omega_model_segment_get_kind(segment.get())) {
648651
case model_segment_kind_t::SEGMENT_READ: {

core/src/tests/CMakeLists.txt

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,64 @@ set(OMEGA_EDIT_TEST_DATA_DIR ${OMEGA_EDIT_TEST_BINARY_DIR}/data)
88

99
file(COPY ./data DESTINATION ${CMAKE_CURRENT_BINARY_DIR} USE_SOURCE_PERMISSIONS)
1010
configure_file(
11-
test_util.hpp.in
12-
${CMAKE_CURRENT_BINARY_DIR}/test_util.hpp
11+
test_util.hpp.in
12+
${CMAKE_CURRENT_BINARY_DIR}/test_util.hpp
1313
)
1414

1515
find_program(VALGRIND "valgrind")
1616

17-
foreach(test_src ${omega_test_srcs})
17+
foreach (test_src ${omega_test_srcs})
1818
get_filename_component(testname ${test_src} NAME_WE)
1919

2020
add_executable(
21-
${testname}
22-
${test_src}
21+
${testname}
22+
${test_src}
2323
)
2424
target_link_libraries(
25-
${testname}
26-
PRIVATE omega_edit::omega_edit Catch2::Catch2WithMain ${FILESYSTEM_LIB}
25+
${testname}
26+
PRIVATE omega_edit::omega_edit Catch2::Catch2WithMain ${FILESYSTEM_LIB}
2727
)
2828
set_target_properties(
29-
${testname} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
29+
${testname} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
3030
)
3131
target_include_directories(
32-
${testname}
33-
PUBLIC ${OMEGA_EDIT_TEST_BINARY_DIR}
32+
${testname}
33+
PUBLIC ${OMEGA_EDIT_TEST_BINARY_DIR}
3434
)
3535

3636
if (VALGRIND)
3737
add_custom_target(
38-
"valgrind-${testname}"
39-
COMMAND ${VALGRIND}
40-
--leak-check=full
41-
--show-leak-kinds=all
42-
--track-origins=yes
43-
--verbose
44-
--log-file=${testname}_valgrind-out.txt
45-
$<TARGET_FILE:${testname}>
46-
COMMENT "[${testname}]: Running tests under valgrind"
47-
DEPENDS ${testname})
38+
"valgrind-${testname}"
39+
COMMAND ${VALGRIND}
40+
--leak-check=full
41+
--show-leak-kinds=all
42+
--track-origins=yes
43+
--verbose
44+
--log-file=${testname}_valgrind-out.txt
45+
$<TARGET_FILE:${testname}>
46+
COMMENT "[${testname}]: Running tests under valgrind"
47+
DEPENDS ${testname})
4848
endif ()
4949

50-
catch_discover_tests(${testname} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
51-
endforeach()
50+
# On Windows with shared libraries, we need to ensure the DLL is available
51+
# for test discovery. Since catch_discover_tests runs immediately after build,
52+
# we use a different approach for Windows shared builds.
53+
message(STATUS "Test ${testname}: WIN32=${WIN32}, BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}")
54+
if (WIN32 AND BUILD_SHARED_LIBS)
55+
message(STATUS "Using manual test registration for ${testname}")
56+
add_dependencies(${testname} omega_edit)
57+
add_custom_command(TARGET ${testname} POST_BUILD
58+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
59+
$<TARGET_FILE:omega_edit>
60+
$<TARGET_FILE_DIR:${testname}>
61+
COMMENT "Copying omega_edit.dll to test directory for ${testname}")
62+
63+
# For Windows shared builds, add the test manually to avoid discovery issues
64+
add_test(NAME ${testname} COMMAND ${testname})
65+
set_tests_properties(${testname} PROPERTIES
66+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
67+
else()
68+
message(STATUS "Using catch_discover_tests for ${testname}")
69+
catch_discover_tests(${testname} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
70+
endif()
71+
endforeach ()

core/src/tests/filesystem_tests.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ using Catch::Matchers::Contains;
3232
using Catch::Matchers::EndsWith;
3333
using Catch::Matchers::Equals;
3434

35+
#ifdef OMEGA_BUILD_WINDOWS
36+
#include <chrono>
37+
#include <thread>
38+
/**
39+
* Sleep for the given number of seconds.
40+
* @param seconds Number of seconds to sleep.
41+
*/
42+
static inline void omega_util_sleep_(const int seconds) { std::this_thread::sleep_for(std::chrono::seconds(seconds)); }
43+
#endif // OMEGA_BUILD_WINDOWS
3544

3645
TEST_CASE("File Compare", "[UtilTests]") {
3746
SECTION("Identity") {
@@ -65,7 +74,7 @@ TEST_CASE("File Copy", "[UtilTests]") {
6574
REQUIRE(0 == stat(MAKE_PATH("test1.copy.dat"), &dst_stat));
6675

6776
// The mode includes the file type
68-
const int dst_mode = 0100600;// S_IFREG | S_IRUSR regular file with owner read-only
77+
const int dst_mode = 0100600; // S_IFREG | S_IRUSR regular file with owner read-only
6978
REQUIRE(dst_mode != src_stat.st_mode);
7079
REQUIRE(src_stat.st_mode == dst_stat.st_mode);
7180

@@ -125,7 +134,7 @@ TEST_CASE("File Touch", "[UtilTests]") {
125134
expected = dont_exist;
126135
REQUIRE_THAT(omega_util_available_filename(dont_exist.c_str(), nullptr), Equals(expected));
127136
omega_util_touch(dont_exist.c_str(),
128-
0);// logs an error as expected because create is false and the file does not exist
137+
0); // logs an error as expected because create is false and the file does not exist
129138
REQUIRE(!omega_util_file_exists(dont_exist.c_str()));
130139
#ifdef OMEGA_BUILD_WINDOWS
131140
// sleep for 1 second to ensure the file modification time is different,
@@ -237,65 +246,65 @@ TEST_CASE("File Extension", "[UtilTests]") {
237246

238247

239248
TEST_CASE("Emoji Filename Handling", "[FilesystemTests]") {
240-
const char* emoji_filenames[] = {
249+
const char *emoji_filenames[] = {
241250
"test_😀.dat",
242251
"test_👍.dat",
243252
"test_🔥.dat",
244253
"test 💩.dat", // Space in filename as well
245254
"test_🚀.dat",
246-
"test_👨‍👩‍👧‍👦.dat" // Family emoji with zero-width joiners
255+
"test_👨‍👩‍👧‍👦.dat" // Family emoji with zero-width joiners
247256
};
248-
257+
249258
char buffer[FILENAME_MAX];
250-
259+
251260
// Test filesystem operations with emoji filenames
252-
for (const auto& emoji_filename : emoji_filenames) {
261+
for (const auto &emoji_filename: emoji_filenames) {
253262
const char dir_sep = omega_util_directory_separator();
254263
std::string base_path = std::string(DATA_DIR.string().c_str()) + dir_sep;
255264
std::string full_path = base_path + emoji_filename;
256-
265+
257266
// Create a file with emoji in filename
258267
std::ofstream file(full_path);
259268
file << "Test content" << std::endl;
260269
file.close();
261-
270+
262271
// Test file_exists
263272
REQUIRE(omega_util_file_exists(full_path.c_str()));
264-
273+
265274
// Test available_filename - since the file exists, we expect a path with -1 suffix
266-
char* available_name = omega_util_available_filename(full_path.c_str(), buffer);
275+
char *available_name = omega_util_available_filename(full_path.c_str(), buffer);
267276
REQUIRE(available_name != nullptr);
268277
// The file exists so available_name should append -1 to the filename
269278
std::string expected_path = full_path;
270279
size_t dot_pos = expected_path.rfind('.');
271280
expected_path.insert(dot_pos, "-1");
272281
REQUIRE(std::string(available_name) == expected_path);
273-
282+
274283
// Test basename, dirname and extension
275-
char* basename_result = omega_util_basename(full_path.c_str(), nullptr, 0);
284+
char *basename_result = omega_util_basename(full_path.c_str(), nullptr, 0);
276285
REQUIRE(basename_result != nullptr);
277286
REQUIRE(std::string(basename_result) == emoji_filename);
278-
279-
char* dirname_result = omega_util_dirname(full_path.c_str(), nullptr);
287+
288+
char *dirname_result = omega_util_dirname(full_path.c_str(), nullptr);
280289
REQUIRE(dirname_result != nullptr);
281290
REQUIRE(std::string(dirname_result) == base_path.substr(0, base_path.length() - 1));
282-
283-
char* ext_result = omega_util_file_extension(full_path.c_str(), nullptr);
291+
292+
char *ext_result = omega_util_file_extension(full_path.c_str(), nullptr);
284293
REQUIRE(ext_result != nullptr);
285294
REQUIRE(std::string(ext_result) == ".dat");
286-
295+
287296
// Create a second file for copy operations
288297
std::string copy_path = base_path + "copy_" + emoji_filename;
289-
298+
290299
// Test file_copy
291300
REQUIRE(0 == omega_util_file_copy(full_path.c_str(), copy_path.c_str(), 0));
292301
REQUIRE(omega_util_file_exists(copy_path.c_str()));
293302
REQUIRE(0 == omega_util_compare_files(full_path.c_str(), copy_path.c_str()));
294-
303+
295304
// Test remove_file
296305
REQUIRE(0 == omega_util_remove_file(full_path.c_str()));
297306
REQUIRE(!omega_util_file_exists(full_path.c_str()));
298-
307+
299308
REQUIRE(0 == omega_util_remove_file(copy_path.c_str()));
300309
REQUIRE(!omega_util_file_exists(copy_path.c_str()));
301310
}

0 commit comments

Comments
 (0)