Skip to content

Commit bc2123f

Browse files
authored
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).
1 parent 539a197 commit bc2123f

File tree

10 files changed

+42
-158
lines changed

10 files changed

+42
-158
lines changed

test/dirent/test_readdir.c

+1-11
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ void setup() {
4040
create_file("foobar/file.txt", "ride into the danger zone", 0666);
4141
}
4242

43-
void cleanup() {
44-
rmdir("nocanread");
45-
unlink("foobar/file.txt");
46-
rmdir("foobar");
47-
chdir("..");
48-
rmdir("testtmp");
49-
}
50-
5143
void test() {
5244
int err;
5345
long loc, loc2;
@@ -208,11 +200,9 @@ void test_scandir() {
208200
}
209201

210202
int main() {
211-
atexit(cleanup);
212-
signal(SIGABRT, cleanup);
213203
setup();
214204
test();
215205
test_scandir();
216206

217-
return EXIT_SUCCESS;
207+
return 0;
218208
}

test/fcntl/test_fcntl_open.c

+1-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <fcntl.h>
1111
#include <signal.h>
1212
#include <stdio.h>
13-
#include <stdlib.h>
1413
#include <string.h>
1514
#include <unistd.h>
1615
#include <sys/stat.h>
@@ -37,20 +36,6 @@ void setup() {
3736
assert(!errno);
3837
}
3938

40-
void cleanup() {
41-
unlink("test-file");
42-
rmdir("test-folder");
43-
for (int i = 0; i < 3; i++) {
44-
for (int j = 0; j < 32; j++) {
45-
sprintf(nonexistent_name, "noexist-%c%d", 'a' + i, j);
46-
unlink(nonexistent_name);
47-
}
48-
}
49-
errno = 0;
50-
unlink("creat-me");
51-
assert(!errno);
52-
}
53-
5439
void test() {
5540
struct stat s;
5641
int modes[] = {O_RDONLY, O_WRONLY, O_RDWR};
@@ -167,9 +152,7 @@ void test() {
167152
}
168153

169154
int main() {
170-
atexit(cleanup);
171-
signal(SIGABRT, cleanup);
172155
setup();
173156
test();
174-
return EXIT_SUCCESS;
157+
return 0;
175158
}

test/stat/test_chmod.c

-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ void test() {
143143
}
144144

145145
int main() {
146-
atexit(cleanup);
147-
signal(SIGABRT, cleanup);
148146
setup();
149147
test();
150148
return EXIT_SUCCESS;

test/stat/test_mknod.c

+1-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <fcntl.h>
1212
#include <signal.h>
1313
#include <stdio.h>
14-
#include <stdlib.h>
1514
#include <string.h>
1615
#include <unistd.h>
1716
#include <utime.h>
@@ -22,13 +21,6 @@ void setup() {
2221
mkdir("folder-readonly", 0555);
2322
}
2423

25-
void cleanup() {
26-
unlink("mknod-file");
27-
unlink("mknod-device");
28-
rmdir("folder");
29-
rmdir("folder-readonly");
30-
}
31-
3224
void test() {
3325
int err;
3426
struct stat s;
@@ -93,9 +85,7 @@ void test() {
9385
}
9486

9587
int main() {
96-
atexit(cleanup);
97-
signal(SIGABRT, cleanup);
9888
setup();
9989
test();
100-
return EXIT_SUCCESS;
90+
return 0;
10191
}

test/stat/test_stat.c

+12-14
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
#include <fcntl.h>
1515
#include <signal.h>
1616
#include <stdio.h>
17-
#include <stdlib.h>
1817
#include <string.h>
1918
#include <time.h>
2019
#include <unistd.h>
2120
#include <utime.h>
2221
#include <sys/stat.h>
2322
#include <sys/types.h>
2423
#include <sys/sysmacros.h>
24+
25+
#ifdef __EMSCRIPTEN__
2526
#include <emscripten/emscripten.h>
27+
#endif
2628

2729
void create_file(const char *path, const char *buffer, int mode) {
2830
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
@@ -46,13 +48,6 @@ void setup() {
4648
utime("folder", &t);
4749
}
4850

49-
void cleanup() {
50-
rmdir("folder/subdir");
51-
unlink("folder/file");
52-
unlink("folder/file-link");
53-
rmdir("folder");
54-
}
55-
5651
void test() {
5752
int err;
5853
struct stat s;
@@ -65,7 +60,7 @@ void test() {
6560

6661
// test stat64 LFS functions
6762
struct stat64 s64;
68-
err = stat("does_not_exist", &s64);
63+
err = stat64("does_not_exist", &s64);
6964
assert(err == -1);
7065
assert(errno == ENOENT);
7166

@@ -82,8 +77,8 @@ void test() {
8277
#endif
8378
assert(s.st_size);
8479
printf("TEST_TIME: %llx\n", TEST_TIME);
85-
printf("s.st_atime: %llx\n", s.st_atime);
86-
printf("s.st_mtime: %llx\n", s.st_mtime);
80+
printf("s.st_atime: %llx\n", (long long)s.st_atime);
81+
printf("s.st_mtime: %llx\n", (long long)s.st_mtime);
8782
assert(s.st_atime == TEST_TIME);
8883
assert(s.st_mtime == TEST_TIME);
8984
assert(s.st_ctime);
@@ -203,6 +198,7 @@ void test() {
203198
assert(s.st_mtime != TEST_TIME);
204199

205200
chmod("folder/file", 0666);
201+
#ifdef __EMSCRIPTEN__
206202
EM_ASM(
207203
var stats = FS.stat("folder/file");
208204
assert(stats.dev == 1);
@@ -215,9 +211,11 @@ void test() {
215211
assert(stats.mtime);
216212
assert(stats.ctime);
217213
);
214+
#endif
218215

219216
symlink("folder/file", "folder/symlinkfile");
220217

218+
#ifdef __EMSCRIPTEN__
221219
EM_ASM(
222220
var linkStats = FS.lstat("folder/symlinkfile");
223221
assert(linkStats.dev == 1);
@@ -249,15 +247,15 @@ void test() {
249247
}
250248
assert(ex.name === "ErrnoError" && ex.errno === 44 /* ENOENT */);
251249
);
250+
#endif
251+
252252
chmod("folder/file", 0777);
253253

254254
puts("success");
255255
}
256256

257257
int main() {
258-
atexit(cleanup);
259-
signal(SIGABRT, cleanup);
260258
setup();
261259
test();
262-
return EXIT_SUCCESS;
260+
return 0;
263261
}

test/stdio/test_fgetc_ungetc.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include <fcntl.h>
1010
#include <signal.h>
1111
#include <stdio.h>
12-
#include <stdlib.h>
1312
#include <string.h>
1413
#include <unistd.h>
14+
15+
#ifdef __EMSCRIPTEN__
1516
#include <emscripten.h>
17+
#endif
1618

1719
static void create_file(const char *path, const char *buffer, int mode) {
1820
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
@@ -28,10 +30,6 @@ void setup() {
2830
create_file("/tmp/file.txt", "cd", 0666);
2931
}
3032

31-
void cleanup() {
32-
unlink("/tmp/file.txt");
33-
}
34-
3533
void test() {
3634
FILE *file;
3735
int err;
@@ -90,14 +88,14 @@ void test() {
9088
}
9189

9290
int main() {
91+
#ifdef __EMSCRIPTEN__
9392
#ifdef NODEFS
9493
EM_ASM(FS.mount(NODEFS, { root: '.' }, '/tmp'));
9594
#elif MEMFS
9695
EM_ASM(FS.mount(MEMFS, {}, '/tmp'));
9796
#endif
98-
atexit(cleanup);
99-
signal(SIGABRT, cleanup);
97+
#endif
10098
setup();
10199
test();
102-
return EXIT_SUCCESS;
100+
return 0;
103101
}

test/stdio/test_rename.c

+1-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <fcntl.h>
1111
#include <signal.h>
1212
#include <stdio.h>
13-
#include <stdlib.h>
1413
#include <string.h>
1514
#include <sys/stat.h>
1615
#include <unistd.h>
@@ -53,39 +52,6 @@ void setup() {
5352
create_file("dir-nonempty/file", "abcdef", 0777);
5453
}
5554

56-
void cleanup() {
57-
// We're hulk-smashing and removing original + renamed files to
58-
// make sure we get it all regardless of anything failing
59-
unlink("file");
60-
unlink("dir/file");
61-
unlink("dir/file1");
62-
unlink("dir/file2");
63-
rmdir("dir/subdir/subsubdir");
64-
rmdir("dir/subdir");
65-
rmdir("dir/subdir1");
66-
rmdir("dir/subdir2");
67-
rmdir("dir/subdir3/subdir3_1/subdir1 renamed");
68-
rmdir("dir/subdir3/subdir3_1");
69-
rmdir("dir/subdir3");
70-
rmdir("dir/subdir4/");
71-
rmdir("dir/subdir5/");
72-
rmdir("dir/b/c");
73-
rmdir("dir/b");
74-
rmdir("dir/rename-dir/subdir/subsubdir");
75-
rmdir("dir/rename-dir/subdir");
76-
rmdir("dir/rename-dir");
77-
rmdir("dir");
78-
#ifndef WASMFS
79-
chmod("dir-readonly2", 0777);
80-
#endif
81-
rmdir("dir-readonly2/somename");
82-
rmdir("dir-readonly2");
83-
rmdir("new-dir");
84-
rmdir("dir-readonly");
85-
unlink("dir-nonempty/file");
86-
rmdir("dir-nonempty");
87-
}
88-
8955
void test() {
9056
int err;
9157

@@ -254,9 +220,7 @@ void test() {
254220
}
255221

256222
int main() {
257-
atexit(cleanup);
258-
signal(SIGABRT, cleanup);
259223
setup();
260224
test();
261-
return EXIT_SUCCESS;
225+
return 0;
262226
}

test/termios/test_tcgetattr.c

+18-25
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <fcntl.h>
1111
#include <signal.h>
1212
#include <stdio.h>
13-
#include <stdlib.h>
1413
#include <string.h>
1514
#include <termios.h>
1615
#include <unistd.h>
@@ -26,43 +25,37 @@ static void create_file(const char *path, const char *buffer, int mode) {
2625
}
2726

2827
void setup() {
29-
create_file("test.txt", "abcdefg", 0666);
30-
}
31-
32-
void cleanup() {
33-
unlink("test.txt");
28+
create_file("test.txt", "abcdefg", 0666);
3429
}
3530

3631
void test() {
37-
struct termios tc;
38-
int ret;
39-
int fd;
32+
struct termios tc;
33+
int ret;
34+
int fd;
4035

41-
fd = open("test.txt", O_RDONLY);
36+
fd = open("test.txt", O_RDONLY);
4237

43-
ret = tcgetattr(fd, &tc);
44-
assert(ret == -1);
45-
assert(errno = ENOTTY);
38+
ret = tcgetattr(fd, &tc);
39+
assert(ret == -1);
40+
assert(errno == ENOTTY);
4641

47-
ret = tcgetattr(STDIN_FILENO, &tc);
48-
assert(!ret);
42+
ret = tcgetattr(STDIN_FILENO, &tc);
43+
assert(!ret);
4944

50-
ret = tcsetattr(fd, 0, &tc);
51-
assert(ret == -1);
52-
assert(errno = ENOTTY);
45+
ret = tcsetattr(fd, 0, &tc);
46+
assert(ret == -1);
47+
assert(errno == ENOTTY);
5348

54-
ret = tcsetattr(STDIN_FILENO, 0, &tc);
55-
assert(!ret);
49+
ret = tcsetattr(STDIN_FILENO, 0, &tc);
50+
assert(!ret);
5651

57-
close(fd);
52+
close(fd);
5853

59-
puts("success");
54+
puts("success");
6055
}
6156

6257
int main() {
63-
atexit(cleanup);
64-
signal(SIGABRT, cleanup);
6558
setup();
6659
test();
67-
return EXIT_SUCCESS;
60+
return 0;
6861
}

0 commit comments

Comments
 (0)