From 5720b2a32f9beb9df6a207d096c1b8829b9c23c3 Mon Sep 17 00:00:00 2001 From: Travis Geiselbrecht Date: Wed, 1 Oct 2025 23:35:48 -0700 Subject: [PATCH] [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. --- lib/fs/fat/test/test.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/fs/fat/test/test.cpp b/lib/fs/fat/test/test.cpp index ce962707..dd9315b6 100644 --- a/lib/fs/fat/test/test.cpp +++ b/lib/fs/fat/test/test.cpp @@ -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); });