Skip to content

Commit 3a9582c

Browse files
committed
#102 fix windows test
1 parent f8211f8 commit 3a9582c

File tree

3 files changed

+34
-73
lines changed

3 files changed

+34
-73
lines changed

clove-unit.h

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ __CLOVE_EXTERN_C double __clove_math_decimald(unsigned char precision);
8484
#define __CLOVE_PATH_SEPARATOR_STR "/"
8585
#endif //_WIN32
8686

87-
__CLOVE_EXTERN_C char* __clove_path_concat(const char separator, const char* path1, const char* path2);
87+
__CLOVE_EXTERN_C char* __clove_path_concat(const char* path1, const char* path2, const char separator);
8888
__CLOVE_EXTERN_C const char* __clove_path_relative(const char* abs_path, const char* base_path);
8989
__CLOVE_EXTERN_C char* __clove_path_rel_to_abs_exec_path(const char* rel_path);
9090
__CLOVE_EXTERN_C bool __clove_path_is_relative(const char* path);
9191
__CLOVE_EXTERN_C bool __clove_path_is_absolute(const char* path);
9292
__CLOVE_EXTERN_C void __clove_path_to_os(char* path);
9393
__CLOVE_EXTERN_C char* __clove_path_basepath(const char* path);
94+
__CLOVE_EXTERN_C bool __clove_path_exists(const char* path);
9495
__CLOVE_EXTERN_C char* __clove_path_to_absolute(const char* path);
9596
#pragma endregion // Path Decl
9697

@@ -904,7 +905,13 @@ double __clove_math_decimald(unsigned char precision) {
904905
#include <limits.h>
905906
#include <sys/stat.h>
906907

907-
char* __clove_path_concat(const char separator, const char* path1, const char* path2) {
908+
#ifdef _WIN32
909+
#define __CLOVE_PATH_MAX_LEN _MAX_PATH
910+
#else
911+
#define __CLOVE_PATH_MAX_LEN PATH_MAX
912+
#endif //_WIN32
913+
914+
char* __clove_path_concat(const char* path1, const char* path2, const char separator) {
908915
size_t count = __clove_string_length(path1) + 1 + __clove_string_length(path2) + 1;
909916
char* path = __CLOVE_MEMORY_CALLOC_TYPE_N(char, count);
910917

@@ -931,7 +938,7 @@ const char* __clove_path_relative(const char* abs_path, const char* base_path) {
931938

932939
char* __clove_path_rel_to_abs_exec_path(const char* rel_path) {
933940
const char* base_path = __clove_utils_get_exec_abs_basepath();
934-
char* abs_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, base_path, rel_path);
941+
char* abs_path = __clove_path_concat(base_path, rel_path, __CLOVE_PATH_SEPARATOR);
935942
return abs_path;
936943
}
937944

@@ -951,50 +958,41 @@ void __clove_path_to_os(char* path) {
951958
__clove_string_replace_char(path, '\\', __CLOVE_PATH_SEPARATOR);
952959
}
953960

954-
char* __clove_path_basepath(const char* a_path) {
955-
bool last_char_is_win = __clove_string_endswith(a_path, "\\");
956-
bool last_char_is_uni = __clove_string_endswith(a_path, "/");
957-
958-
//__CLOVE_UNUSED_VAR(last_char_is_win);
959-
//__CLOVE_UNUSED_VAR(last_char_is_uni);
960-
961-
962-
size_t source_len = __clove_string_length(a_path);
963-
size_t tmp_len = source_len + 1;
964-
char* tmp_path = __CLOVE_MEMORY_CALLOC_TYPE_N(char, tmp_len); //take account for '\0'
965-
966-
if (last_char_is_win || last_char_is_uni)
967-
{
968-
source_len--;
969-
}
961+
char* __clove_path_basepath(const char* path) {
962+
char temp_path[__CLOVE_PATH_MAX_LEN];
963+
__clove_string_strcpy(temp_path, sizeof(temp_path), path);
970964

971-
__clove_string_strncpy(tmp_path, tmp_len, a_path, source_len);
965+
//Remove last path separator character if any
966+
bool last_char_is_win = __clove_string_endswith(path, "\\");
967+
bool last_char_is_uni = __clove_string_endswith(path, "/");
968+
if (last_char_is_win || last_char_is_uni) {
969+
size_t last_index = __clove_string_length(temp_path) - 1;
970+
temp_path[last_index] = '\0';
971+
}
972972

973-
974973
// Find the last path separator character in the input path.
975-
int last_char_win = __clove_string_last_indexof(tmp_path, '\\');
976-
int last_char_uni = __clove_string_last_indexof(tmp_path, '/'); //or unix or win eventually
974+
int last_char_win = __clove_string_last_indexof(temp_path, '\\');
975+
int last_char_uni = __clove_string_last_indexof(temp_path, '/'); //or unix or win eventually
977976
int last_char_index = last_char_win > last_char_uni ? last_char_win : last_char_uni;
978977

979978
// If there are no separators in the path, return the current directory path.
980979
char* result = NULL;
981-
if (last_char_index <= 0) {
980+
if (last_char_index < 0) {
982981
static char dot_path[3] = { '.', __CLOVE_PATH_SEPARATOR, '\0' };
983982
result = __clove_string_strdup(dot_path);
984983
} else {
985984
// Calculate base path length based on the position of the last path separator.
986985
size_t base_length = (size_t)(last_char_index + 1);
987-
char* base_path = __CLOVE_MEMORY_CALLOC_TYPE_N(char, base_length);
988-
__clove_string_strncpy(base_path, base_length, tmp_path, base_length - 1);
986+
char* base_path = __CLOVE_MEMORY_MALLOC_TYPE_N(char, base_length);
987+
__clove_string_strncpy(base_path, base_length, temp_path, base_length - 1);
989988
__clove_path_to_os(base_path);
990989
result = base_path;
991990
}
992991

993-
__clove_memory_free(tmp_path);
994992
return result;
995993
}
996994

997-
static bool __clove_path_exists(const char* path) {
995+
bool __clove_path_exists(const char* path) {
998996
struct stat buffer;
999997
return stat(path, &buffer) == 0;
1000998
}

tests/functs/src/inte/cmd_run_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CLOVE_SUITE_TEARDOWN() {
1818

1919
CLOVE_TEST(JsonReportWithOptOonFile) {
2020
char* base_path = __clove_path_basepath(RES_PRJ01_EXEC_PATH);
21-
char* report_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, base_path, "cmd_json_report.json");
21+
char* report_path = __clove_path_concat(base_path, "cmd_json_report.json", __CLOVE_PATH_SEPARATOR);
2222
//utils_file_delete(report_path);
2323

2424
const char* cmd = cmd_fmt("\"%s\" -r json -o \"%s\"", RES_PRJ01_EXEC_PATH, report_path);
@@ -34,7 +34,7 @@ CLOVE_TEST(JsonReportWithOptOonFile) {
3434

3535
CLOVE_TEST(JsonReportWithOptOuputOnFile) {
3636
char* base_path = __clove_path_basepath(RES_PRJ01_EXEC_PATH);
37-
char* report_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, base_path, "cmd_json_report.json");
37+
char* report_path = __clove_path_concat(base_path, "cmd_json_report.json", __CLOVE_PATH_SEPARATOR);
3838
//utils_file_delete(report_path);
3939

4040
const char* cmd = cmd_fmt("\"%s\" -r json --output \"%s\"", RES_PRJ01_EXEC_PATH, report_path);
@@ -216,7 +216,7 @@ CLOVE_TEST(DefaultReportIncludeOverExcludeOneTest) {
216216

217217
CLOVE_TEST(DefaultReportWithOptOonFile) {
218218
char* base_path = __clove_path_basepath(RES_PRJ01_EXEC_PATH);
219-
char* report_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, base_path, "cmd_console_report.txt");
219+
char* report_path = __clove_path_concat(base_path, "cmd_console_report.txt", __CLOVE_PATH_SEPARATOR);
220220
//utils_file_delete(report_path);
221221

222222
const char* cmd = cmd_fmt("\"%s\" -o \"%s\"", RES_PRJ01_EXEC_PATH, report_path);

tests/functs/src/unit/path_test.c

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#define CLOVE_SUITE_NAME UNIT_PathTest
2-
#include "clove-unit.h"
2+
#include <clove-unit.h>
33
#include "utils/utils.h"
44

55
CLOVE_TEST(PathConcatenation) {
6-
char* result = __clove_path_concat('/', "path/to/first", "second");
6+
char* result = __clove_path_concat("path/to/first", "second", '/');
77
CLOVE_ULLONG_EQ(20, __clove_string_length(result));
88
CLOVE_STRING_EQ("path/to/first/second", result);
99
}
1010

1111
CLOVE_TEST(PathConcatConvertingSeparator) {
12-
char* result = __clove_path_concat('\\', "path/to/first", "second");
12+
char* result = __clove_path_concat("path/to/first", "second", '\\');
1313
CLOVE_ULLONG_EQ(20, __clove_string_length(result));
1414
CLOVE_STRING_EQ("path\\to\\first\\second", result);
1515
}
@@ -98,7 +98,7 @@ CLOVE_TEST(ConvertUnexistentAbsToAbsolutePath) {
9898

9999
CLOVE_TEST(ConvertUnexistentRelToAbsolutePath) {
100100
char* cwd_path = utils_cwd();
101-
char* abs_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, cwd_path, "rel/path/file.c");
101+
char* abs_path = __clove_path_concat(cwd_path, "rel/path/file.c", __CLOVE_PATH_SEPARATOR);
102102

103103
char* result = __clove_path_to_absolute("rel/path/file.c");
104104
CLOVE_STRING_EQ(abs_path, result);
@@ -110,8 +110,7 @@ CLOVE_TEST(ConvertUnexistentRelToAbsolutePath) {
110110

111111
CLOVE_TEST(ConvertExistentRelToAbsolutePath) {
112112
char* cwd_path = utils_cwd();
113-
//puts(cwd_path);
114-
char* abs_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, cwd_path, "my/path");
113+
char* abs_path = __clove_path_concat(cwd_path, "my/path", __CLOVE_PATH_SEPARATOR);
115114
utils_mkdirs(abs_path);
116115

117116
char* result = __clove_path_to_absolute("my/path");
@@ -127,39 +126,3 @@ CLOVE_TEST(ConvertExistentRelToAbsolutePath) {
127126
__clove_memory_free(abs_path);
128127
__clove_memory_free(abs_path_parent);
129128
}
130-
131-
/*
132-
CLOVE_TEST(ConvertUnexistentToAbsoluteWithEndingSlashIsRemoved) {
133-
char* cwd_path = utils_cwd();
134-
char* abs_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, cwd_path, "rel/path");
135-
136-
char* result = __clove_path_to_absolute("./rel/path/");
137-
138-
CLOVE_STRING_EQ(abs_path, result);
139-
140-
__clove_memory_free(result);
141-
__clove_memory_free(cwd_path);
142-
__clove_memory_free(abs_path);
143-
}
144-
145-
146-
CLOVE_TEST(ConvertExistentToAbsoluteWithEndingSlashIsRemoved) {
147-
char* cwd_path = utils_cwd();
148-
char* abs_path = __clove_path_concat(__CLOVE_PATH_SEPARATOR, cwd_path, "my/path");
149-
utils_mkdirs(abs_path);
150-
151-
char* result = __clove_path_to_absolute("my/path/");
152-
153-
CLOVE_STRING_EQ(abs_path, result);
154-
155-
//rmdir
156-
char* abs_path_parent = __clove_path_basepath(abs_path);
157-
utils_rmdir(abs_path_parent);
158-
159-
__clove_memory_free(result);
160-
__clove_memory_free(cwd_path);
161-
__clove_memory_free(abs_path);
162-
__clove_memory_free(abs_path_parent);
163-
}
164-
165-
*/

0 commit comments

Comments
 (0)