@@ -93,7 +93,6 @@ void test_nonexistent_spi_interface(void) {
9393void run_multiple_ways (SpiTestFunction test_function, uint8_t max_files = MAX_FILES, bool format_if_empty = false ) {
9494 run_first_init (test_function, max_files, format_if_empty);
9595 run_init_continuous (test_function, max_files, format_if_empty);
96- test_nonexistent_spi_interface ();
9796}
9897
9998void test_sd_basic (void ) {
@@ -316,73 +315,16 @@ void test_sd_nested_directories(void) {
316315 });
317316}
318317
319- void test_sd_file_count_in_directoryOLD (void ) {
320- Serial.println (" Running test_sd_file_count_in_directory" );
321-
322- run_multiple_ways ([](SPITestConfig &config) {
323- // Create test directory
324- TEST_ASSERT_TRUE_MESSAGE (config.sd ->mkdir (" /counttest" ), " Failed to create /counttest" );
325-
326- // Function to count files in directory
327- auto countFilesInDir = [&config](const char *dirPath) -> int {
328- File dir = config.sd ->open (dirPath);
329- if (!dir || !dir.isDirectory ()) {
330- return -1 ;
331- }
332-
333- int count = 0 ;
334- File entry = dir.openNextFile ();
335- while (entry) {
336- if (!entry.isDirectory ()) {
337- count++;
338- }
339- entry.close ();
340- entry = dir.openNextFile ();
341- }
342- dir.close ();
343- return count;
344- };
345-
346- // Initially should be empty
347- TEST_ASSERT_EQUAL_MESSAGE (0 , countFilesInDir (" /counttest" ), " Directory should initially be empty" );
348-
349- // Create files one by one and verify count
350- for (int i = 1 ; i <= 5 ; i++) {
351- char filename[50 ];
352- snprintf (filename, sizeof (filename), " /counttest/file_%d.txt" , i);
353-
354- File file = config.sd ->open (filename, FILE_WRITE);
355- TEST_ASSERT_TRUE_MESSAGE (file, " Failed to create numbered file" );
356- file.printf (" Content of file %d" , i);
357- file.close ();
358-
359- TEST_ASSERT_EQUAL_MESSAGE (i, countFilesInDir (" /counttest" ), " File count mismatch after creating file" );
360- }
361-
362- // Create a subdirectory (should not affect file count)
363- TEST_ASSERT_TRUE_MESSAGE (config.sd ->mkdir (" /counttest/subdir" ), " Failed to create subdirectory" );
364- TEST_ASSERT_EQUAL_MESSAGE (5 , countFilesInDir (" /counttest" ), " File count should not change with subdirectory" );
365-
366- // Remove files one by one and verify count
367- for (int i = 5 ; i >= 1 ; i--) {
368- char filename[50 ];
369- snprintf (filename, sizeof (filename), " /counttest/file_%d.txt" , i);
370-
371- TEST_ASSERT_TRUE_MESSAGE (config.sd ->remove (filename), " Failed to remove numbered file" );
372- TEST_ASSERT_EQUAL_MESSAGE (i - 1 , countFilesInDir (" /counttest" ), " File count mismatch after removing file" );
373- }
374-
375- // Clean up
376- TEST_ASSERT_TRUE_MESSAGE (config.sd ->rmdir (" /counttest/subdir" ), " Failed to remove subdirectory" );
377- TEST_ASSERT_TRUE_MESSAGE (config.sd ->rmdir (" /counttest" ), " Failed to remove test directory" );
378- });
379- }
380-
381318void test_sd_file_count_in_directory (void ) {
382319 Serial.println (" Running test_sd_file_count_in_directory" );
383320 run_multiple_ways ([](SPITestConfig &config) {
384321 const char *fileBasePath = " /dir/a/b" ;
385322 const int numFiles = 5 ;
323+
324+ auto getExpectedFile = [fileBasePath](int i) -> std::pair<String, String> {
325+ return {String (fileBasePath) + " /file" + String (i) + " .txt" , " data:" + String (i)};
326+ };
327+
386328 {
387329 // create nested directories
388330 TEST_ASSERT_TRUE_MESSAGE (config.sd ->mkdir (" /dir" ), " mkdir /dir failed" );
@@ -404,10 +346,6 @@ void test_sd_file_count_in_directory(void) {
404346 File d = config.sd ->open (fileBasePath);
405347 TEST_ASSERT_TRUE_MESSAGE (d && d.isDirectory (), " open(/dir/a/b) not a directory" );
406348
407- auto getExpectedFile = [fileBasePath](int i) -> std::pair<String, String> {
408- return {String (fileBasePath) + " /file" + String (i) + " .txt" , " data:" + String (i)};
409- };
410-
411349 bool found[numFiles] = {false };
412350 int count = 0 ;
413351
@@ -446,6 +384,16 @@ void test_sd_file_count_in_directory(void) {
446384 TEST_ASSERT_TRUE_MESSAGE (found[i], (" Expected file not found: " + expectedPath).c_str ());
447385 }
448386 }
387+
388+ // Cleanup: remove files and directories in reverse order (deepest first)
389+ for (int i = 0 ; i < numFiles; ++i) {
390+ auto [filePath, _] = getExpectedFile (i);
391+ TEST_ASSERT_TRUE_MESSAGE (config.sd ->remove (filePath.c_str ()), (" Failed to remove file: " + filePath).c_str ());
392+ }
393+ // Remove directories
394+ TEST_ASSERT_TRUE_MESSAGE (config.sd ->rmdir (" /dir/a/b" ), " Failed to remove directory: /dir/a/b" );
395+ TEST_ASSERT_TRUE_MESSAGE (config.sd ->rmdir (" /dir/a" ), " Failed to remove directory: /dir/a" );
396+ TEST_ASSERT_TRUE_MESSAGE (config.sd ->rmdir (" /dir" ), " Failed to remove directory: /dir" );
449397 });
450398}
451399
@@ -575,6 +523,7 @@ void setup() {
575523#endif
576524
577525 UNITY_BEGIN ();
526+ RUN_TEST (test_nonexistent_spi_interface);
578527 RUN_TEST (test_sd_basic);
579528 RUN_TEST (test_sd_dir);
580529 RUN_TEST (test_sd_file_operations);
0 commit comments