Skip to content

Commit

Permalink
Remove cleanup code from stdlib tests. NFC (#23003)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
sbc100 authored Nov 25, 2024
1 parent 539a197 commit bc2123f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 158 deletions.
12 changes: 1 addition & 11 deletions test/dirent/test_readdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,11 +200,9 @@ void test_scandir() {
}

int main() {
atexit(cleanup);
signal(SIGABRT, cleanup);
setup();
test();
test_scandir();

return EXIT_SUCCESS;
return 0;
}
19 changes: 1 addition & 18 deletions test/fcntl/test_fcntl_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
Expand All @@ -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};
Expand Down Expand Up @@ -167,9 +152,7 @@ void test() {
}

int main() {
atexit(cleanup);
signal(SIGABRT, cleanup);
setup();
test();
return EXIT_SUCCESS;
return 0;
}
2 changes: 0 additions & 2 deletions test/stat/test_chmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ void test() {
}

int main() {
atexit(cleanup);
signal(SIGABRT, cleanup);
setup();
test();
return EXIT_SUCCESS;
Expand Down
12 changes: 1 addition & 11 deletions test/stat/test_mknod.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <utime.h>
Expand All @@ -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;
Expand Down Expand Up @@ -93,9 +85,7 @@ void test() {
}

int main() {
atexit(cleanup);
signal(SIGABRT, cleanup);
setup();
test();
return EXIT_SUCCESS;
return 0;
}
26 changes: 12 additions & 14 deletions test/stat/test_stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysmacros.h>

#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif

void create_file(const char *path, const char *buffer, int mode) {
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
14 changes: 6 additions & 8 deletions test/stdio/test_fgetc_ungetc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

static void create_file(const char *path, const char *buffer, int mode) {
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
38 changes: 1 addition & 37 deletions test/stdio/test_rename.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -254,9 +220,7 @@ void test() {
}

int main() {
atexit(cleanup);
signal(SIGABRT, cleanup);
setup();
test();
return EXIT_SUCCESS;
return 0;
}
43 changes: 18 additions & 25 deletions test/termios/test_tcgetattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
Expand All @@ -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;
}
Loading

0 comments on commit bc2123f

Please sign in to comment.