Skip to content

Commit a5d8f7e

Browse files
Merge pull request #55 from Reim-developer/dev
Implement new helper function `raw_cache_dir` & test this function in FFI test & backend test
2 parents b543544 + ce1d164 commit a5d8f7e

4 files changed

Lines changed: 49 additions & 10 deletions

File tree

src/back_end/src/utils/fs_utils.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ pub extern "C" fn raw_config_dir() -> *mut c_char {
7171
.and_then(|path| path.into_os_string().into_string().ok())
7272
.and_then(|string| CString::new(string).ok());
7373

74-
result.map_or(null_mut(), |c_str| {
75-
CString::new(c_str)
76-
.map(CString::into_raw)
77-
.unwrap_or(null_mut())
78-
})
74+
result.map_or(null_mut(), CString::into_raw)
75+
}
76+
77+
#[must_use]
78+
#[unsafe(no_mangle)]
79+
pub extern "C" fn raw_cache_dir() -> *mut c_char {
80+
let result = dirs::cache_dir()
81+
.and_then(|path| path.into_os_string().into_string().ok())
82+
.and_then(|string| CString::new(string).ok());
83+
84+
result.map_or(null_mut(), CString::into_raw)
7985
}
8086

8187
#[must_use]

src/back_end/tests/fs_utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[test]
2+
fn test_raw_cache_dir() {
3+
use back_end::utils::fs_utils::raw_cache_dir;
4+
use std::ffi::CStr;
5+
6+
unsafe {
7+
let result = raw_cache_dir();
8+
let result_str = CStr::from_ptr(result).to_string_lossy();
9+
10+
assert!(!result.is_null());
11+
assert!(!result_str.is_empty());
12+
}
13+
}

tests-ffi/fs.cxx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1-
21
#include <cassert>
2+
#include <cstddef>
3+
#include <cstdlib>
4+
#include <span>
5+
#include <string>
6+
7+
using std::span;
8+
using std::string;
9+
310
extern "C" bool raw_is_path_exists(const char *path);
11+
extern "C" char *raw_cache_dir();
12+
extern "C" void raw_free_c_str(char *c_str);
413

5-
int main() {
6-
const char *valid_path = "../tests-ffi";
14+
int main(int argc, char *argv[]) {
15+
const auto this_file_span = span(argv, size_t(argc));
16+
17+
if (this_file_span.empty()) {
18+
abort();
19+
}
20+
21+
const char *file_name = this_file_span[0];
722
const char *wrong_path = "../abcxyzw1133";
823

9-
assert(raw_is_path_exists(valid_path));
24+
char *raw_cache_dir_value = raw_cache_dir();
25+
const string cache_dir = string(raw_cache_dir_value);
26+
raw_free_c_str(raw_cache_dir_value);
27+
28+
assert(raw_is_path_exists(file_name));
1029
assert(!raw_is_path_exists(wrong_path));
30+
assert(!cache_dir.empty());
1131

1232
return 0;
1333
}

tests-ffi/tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function run_ffi_test() {
4747
build_output=$(basename "$file" ".cxx")
4848
file_name=$(basename "$file")
4949

50-
$clang_cxx "$file" -l"$sqlite_lib" "$static_lib_path" -o "$build_dir/$build_output"
50+
$clang_cxx "$file" -l"$sqlite_lib" "$static_lib_path" -o "$build_dir/$build_output" -std=c++20
5151
echo
5252
echo -e -n "\e[0;32m[+] Test for $file_name:\e[0;37m"
5353

0 commit comments

Comments
 (0)