[fs][fat][tests] be a bit more forgiving if the test device isn't present

Have the unit tests for the FAT driver fail a bit more gracefully if the
test device is not present, to make it somewhat easier to live with this
driver present with systems a way to mount a test volume.
This commit is contained in:
Travis Geiselbrecht
2025-10-01 23:35:48 -07:00
parent e3ffad684f
commit 5720b2a32f

View File

@@ -33,10 +33,33 @@ namespace {
const char *test_device_name = "virtio0";
#define test_path "/fat"
bool is_test_device_present() {
static bool checked = false;
static bool present = false;
if (!checked) {
checked = true;
auto bio = bio_open(test_device_name);
if (bio) {
present = true;
bio_close(bio);
}
}
return present;
}
#define SKIP_TEST_IF_NO_DEVICE() \
do { \
if (!is_test_device_present()) { \
unittest_printf(" no device '%s' present, skipping test ", test_device_name); \
return true; \
} \
} while (0)
bool test_fat_mount() {
BEGIN_TEST;
LTRACEF("mounting filesystem on device '%s'\n", test_device_name);
SKIP_TEST_IF_NO_DEVICE();
ASSERT_EQ(NO_ERROR, fs_mount(test_path, "fat", test_device_name));
ASSERT_EQ(NO_ERROR, fs_unmount(test_path));
@@ -47,6 +70,8 @@ bool test_fat_mount() {
bool test_fat_dir_root() {
BEGIN_TEST;
SKIP_TEST_IF_NO_DEVICE();
ASSERT_EQ(NO_ERROR, fs_mount(test_path, "fat", test_device_name));
// clean up by unmounting no matter what happens here
@@ -98,6 +123,8 @@ bool test_fat_dir_root() {
bool test_file_read(const char *path, const unsigned char *test_file_buffer, size_t test_file_size) {
BEGIN_TEST;
SKIP_TEST_IF_NO_DEVICE();
// open the file
filehandle *handle = nullptr;
ASSERT_EQ(NO_ERROR, fs_open_file(path, &handle));
@@ -132,6 +159,8 @@ bool test_file_read(const char *path, const unsigned char *test_file_buffer, siz
bool test_fat_read_file() {
BEGIN_TEST;
SKIP_TEST_IF_NO_DEVICE();
ASSERT_EQ(NO_ERROR, fs_mount(test_path, "fat", test_device_name));
// clean up by unmounting no matter what happens here
auto unmount_cleanup = lk::make_auto_call([]() { fs_unmount(test_path); });
@@ -153,6 +182,8 @@ bool test_fat_read_file() {
bool test_fat_multi_open() {
BEGIN_TEST;
SKIP_TEST_IF_NO_DEVICE();
ASSERT_EQ(NO_ERROR, fs_mount(test_path, "fat", test_device_name));
// clean up by unmounting no matter what happens here
auto unmount_cleanup = lk::make_auto_call([]() { fs_unmount(test_path); });