From bc2123f6943e8292ec9466f4c26860f8fd654a91 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 25 Nov 2024 11:50:02 -0800 Subject: [PATCH] Remove cleanup code from stdlib tests. NFC (#23003) When running tests in emscripten there is no requirement to clean up after yourself. The test framework automatically gives each test a clean directory to work in. In fact, cleaning up after yourself like this makes debugging harder because in the failure case you can no longer see the state of the filesystem at the point of failure. In addition, it meant that all these tests were also relying on and testing `atexit` and `signal` handling, making the tests more complex that less precise. As part of this change I also made sure all these tests could be compiled outside of emscripten (on my desktop linux machine). --- test/dirent/test_readdir.c | 12 +--------- test/fcntl/test_fcntl_open.c | 19 +-------------- test/stat/test_chmod.c | 2 -- test/stat/test_mknod.c | 12 +--------- test/stat/test_stat.c | 26 ++++++++++---------- test/stdio/test_fgetc_ungetc.c | 14 +++++------ test/stdio/test_rename.c | 38 +----------------------------- test/termios/test_tcgetattr.c | 43 ++++++++++++++-------------------- test/unistd/unlink.c | 25 +------------------- test/utime/test_utime.c | 9 +------ 10 files changed, 42 insertions(+), 158 deletions(-) diff --git a/test/dirent/test_readdir.c b/test/dirent/test_readdir.c index 762084602e911..8a8428a63cc11 100644 --- a/test/dirent/test_readdir.c +++ b/test/dirent/test_readdir.c @@ -40,14 +40,6 @@ void setup() { create_file("foobar/file.txt", "ride into the danger zone", 0666); } -void cleanup() { - rmdir("nocanread"); - unlink("foobar/file.txt"); - rmdir("foobar"); - chdir(".."); - rmdir("testtmp"); -} - void test() { int err; long loc, loc2; @@ -208,11 +200,9 @@ void test_scandir() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); test_scandir(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/fcntl/test_fcntl_open.c b/test/fcntl/test_fcntl_open.c index ad1a1d28c5cb3..0f48c9c17a974 100644 --- a/test/fcntl/test_fcntl_open.c +++ b/test/fcntl/test_fcntl_open.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -37,20 +36,6 @@ void setup() { assert(!errno); } -void cleanup() { - unlink("test-file"); - rmdir("test-folder"); - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 32; j++) { - sprintf(nonexistent_name, "noexist-%c%d", 'a' + i, j); - unlink(nonexistent_name); - } - } - errno = 0; - unlink("creat-me"); - assert(!errno); -} - void test() { struct stat s; int modes[] = {O_RDONLY, O_WRONLY, O_RDWR}; @@ -167,9 +152,7 @@ void test() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/stat/test_chmod.c b/test/stat/test_chmod.c index 9a117da7aefdb..f7db360054975 100644 --- a/test/stat/test_chmod.c +++ b/test/stat/test_chmod.c @@ -143,8 +143,6 @@ void test() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); return EXIT_SUCCESS; diff --git a/test/stat/test_mknod.c b/test/stat/test_mknod.c index 8722c1cbda42a..bceff1ea1efb8 100644 --- a/test/stat/test_mknod.c +++ b/test/stat/test_mknod.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -22,13 +21,6 @@ void setup() { mkdir("folder-readonly", 0555); } -void cleanup() { - unlink("mknod-file"); - unlink("mknod-device"); - rmdir("folder"); - rmdir("folder-readonly"); -} - void test() { int err; struct stat s; @@ -93,9 +85,7 @@ void test() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/stat/test_stat.c b/test/stat/test_stat.c index d1c3a07650cc9..567047ae9c214 100644 --- a/test/stat/test_stat.c +++ b/test/stat/test_stat.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -22,7 +21,10 @@ #include #include #include + +#ifdef __EMSCRIPTEN__ #include +#endif void create_file(const char *path, const char *buffer, int mode) { int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); @@ -46,13 +48,6 @@ void setup() { utime("folder", &t); } -void cleanup() { - rmdir("folder/subdir"); - unlink("folder/file"); - unlink("folder/file-link"); - rmdir("folder"); -} - void test() { int err; struct stat s; @@ -65,7 +60,7 @@ void test() { // test stat64 LFS functions struct stat64 s64; - err = stat("does_not_exist", &s64); + err = stat64("does_not_exist", &s64); assert(err == -1); assert(errno == ENOENT); @@ -82,8 +77,8 @@ void test() { #endif assert(s.st_size); printf("TEST_TIME: %llx\n", TEST_TIME); - printf("s.st_atime: %llx\n", s.st_atime); - printf("s.st_mtime: %llx\n", s.st_mtime); + printf("s.st_atime: %llx\n", (long long)s.st_atime); + printf("s.st_mtime: %llx\n", (long long)s.st_mtime); assert(s.st_atime == TEST_TIME); assert(s.st_mtime == TEST_TIME); assert(s.st_ctime); @@ -203,6 +198,7 @@ void test() { assert(s.st_mtime != TEST_TIME); chmod("folder/file", 0666); +#ifdef __EMSCRIPTEN__ EM_ASM( var stats = FS.stat("folder/file"); assert(stats.dev == 1); @@ -215,9 +211,11 @@ void test() { assert(stats.mtime); assert(stats.ctime); ); +#endif symlink("folder/file", "folder/symlinkfile"); +#ifdef __EMSCRIPTEN__ EM_ASM( var linkStats = FS.lstat("folder/symlinkfile"); assert(linkStats.dev == 1); @@ -249,15 +247,15 @@ void test() { } assert(ex.name === "ErrnoError" && ex.errno === 44 /* ENOENT */); ); +#endif + chmod("folder/file", 0777); puts("success"); } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/stdio/test_fgetc_ungetc.c b/test/stdio/test_fgetc_ungetc.c index 4d1e0e5a2dbf9..afa90bcda67a5 100644 --- a/test/stdio/test_fgetc_ungetc.c +++ b/test/stdio/test_fgetc_ungetc.c @@ -9,10 +9,12 @@ #include #include #include -#include #include #include + +#ifdef __EMSCRIPTEN__ #include +#endif static void create_file(const char *path, const char *buffer, int mode) { int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); @@ -28,10 +30,6 @@ void setup() { create_file("/tmp/file.txt", "cd", 0666); } -void cleanup() { - unlink("/tmp/file.txt"); -} - void test() { FILE *file; int err; @@ -90,14 +88,14 @@ void test() { } int main() { +#ifdef __EMSCRIPTEN__ #ifdef NODEFS EM_ASM(FS.mount(NODEFS, { root: '.' }, '/tmp')); #elif MEMFS EM_ASM(FS.mount(MEMFS, {}, '/tmp')); #endif - atexit(cleanup); - signal(SIGABRT, cleanup); +#endif setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/stdio/test_rename.c b/test/stdio/test_rename.c index bef973ce35a2d..adc1f99112811 100644 --- a/test/stdio/test_rename.c +++ b/test/stdio/test_rename.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -53,39 +52,6 @@ void setup() { create_file("dir-nonempty/file", "abcdef", 0777); } -void cleanup() { - // We're hulk-smashing and removing original + renamed files to - // make sure we get it all regardless of anything failing - unlink("file"); - unlink("dir/file"); - unlink("dir/file1"); - unlink("dir/file2"); - rmdir("dir/subdir/subsubdir"); - rmdir("dir/subdir"); - rmdir("dir/subdir1"); - rmdir("dir/subdir2"); - rmdir("dir/subdir3/subdir3_1/subdir1 renamed"); - rmdir("dir/subdir3/subdir3_1"); - rmdir("dir/subdir3"); - rmdir("dir/subdir4/"); - rmdir("dir/subdir5/"); - rmdir("dir/b/c"); - rmdir("dir/b"); - rmdir("dir/rename-dir/subdir/subsubdir"); - rmdir("dir/rename-dir/subdir"); - rmdir("dir/rename-dir"); - rmdir("dir"); -#ifndef WASMFS - chmod("dir-readonly2", 0777); -#endif - rmdir("dir-readonly2/somename"); - rmdir("dir-readonly2"); - rmdir("new-dir"); - rmdir("dir-readonly"); - unlink("dir-nonempty/file"); - rmdir("dir-nonempty"); -} - void test() { int err; @@ -254,9 +220,7 @@ void test() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/termios/test_tcgetattr.c b/test/termios/test_tcgetattr.c index 2b2c404e1346c..8aca620cfa4c0 100644 --- a/test/termios/test_tcgetattr.c +++ b/test/termios/test_tcgetattr.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -26,43 +25,37 @@ static void create_file(const char *path, const char *buffer, int mode) { } void setup() { - create_file("test.txt", "abcdefg", 0666); -} - -void cleanup() { - unlink("test.txt"); + create_file("test.txt", "abcdefg", 0666); } void test() { - struct termios tc; - int ret; - int fd; + struct termios tc; + int ret; + int fd; - fd = open("test.txt", O_RDONLY); + fd = open("test.txt", O_RDONLY); - ret = tcgetattr(fd, &tc); - assert(ret == -1); - assert(errno = ENOTTY); + ret = tcgetattr(fd, &tc); + assert(ret == -1); + assert(errno == ENOTTY); - ret = tcgetattr(STDIN_FILENO, &tc); - assert(!ret); + ret = tcgetattr(STDIN_FILENO, &tc); + assert(!ret); - ret = tcsetattr(fd, 0, &tc); - assert(ret == -1); - assert(errno = ENOTTY); + ret = tcsetattr(fd, 0, &tc); + assert(ret == -1); + assert(errno == ENOTTY); - ret = tcsetattr(STDIN_FILENO, 0, &tc); - assert(!ret); + ret = tcsetattr(STDIN_FILENO, 0, &tc); + assert(!ret); - close(fd); + close(fd); - puts("success"); + puts("success"); } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/unistd/unlink.c b/test/unistd/unlink.c index 7133765cee9a4..c7cdb23f67fc2 100644 --- a/test/unistd/unlink.c +++ b/test/unistd/unlink.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -59,26 +58,6 @@ void setup() { create_file("dir-full/anotherfile", "test", 0777); } -void cleanup() { - unlink("file"); - unlink("file1"); -#ifndef NO_SYMLINK - unlink("file1-link"); -#endif - rmdir("dir-empty"); -#ifndef NO_SYMLINK - unlink("dir-empty-link"); -#endif - chmod("dir-readonly", 0777); - chmod("file-readonly", 0777); - unlink("file-readonly"); - unlink("dir-readonly/anotherfile"); - rmdir("dir-readonly/anotherdir"); - rmdir("dir-readonly"); - unlink("dir-full/anotherfile"); - rmdir("dir-full"); -} - void test() { int err; char buffer[512]; @@ -201,10 +180,8 @@ void test() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; } diff --git a/test/utime/test_utime.c b/test/utime/test_utime.c index 76c37e56953e1..ab6a1a245a192 100644 --- a/test/utime/test_utime.c +++ b/test/utime/test_utime.c @@ -22,11 +22,6 @@ void setup() { mkdir("unwriteable", 0111); } -void cleanup() { - rmdir("writeable"); - rmdir("unwriteable"); -} - void test() { struct stat s; // currently, the most recent timestamp is shared for atime, @@ -73,9 +68,7 @@ void test() { } int main() { - atexit(cleanup); - signal(SIGABRT, cleanup); setup(); test(); - return EXIT_SUCCESS; + return 0; }