Skip to content

Commit 0c3c07f

Browse files
authored
Unify code for setting up nodefs tests. NFC (emscripten-core#23167)
1 parent d0b04b9 commit 0c3c07f

17 files changed

+127
-248
lines changed

test/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,10 @@ def require_wasm2js(self):
10231023
if self.is_2gb() or self.is_4gb():
10241024
self.skipTest('wasm2js does not support over 2gb of memory')
10251025

1026+
def setup_nodefs_test(self):
1027+
self.require_node()
1028+
self.emcc_args += ['-lnodefs.js', '--pre-js', test_file('setup_nodefs.js')]
1029+
10261030
def setup_node_pthreads(self):
10271031
self.require_node()
10281032
self.emcc_args += ['-Wno-pthreads-mem-growth', '-pthread']

test/fs/test_fs_rename_on_existing.c

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,6 @@
88
#include <errno.h>
99
#include <string.h>
1010

11-
12-
#if defined(__EMSCRIPTEN__)
13-
#include <emscripten.h>
14-
#endif
15-
16-
void makedir(const char *dir) {
17-
int rtn = mkdir(dir, 0777);
18-
assert(rtn == 0);
19-
}
20-
21-
void changedir(const char *dir) {
22-
int rtn = chdir(dir);
23-
assert(rtn == 0);
24-
}
25-
2611
static void create_file(const char *path, const char *buffer) {
2712
printf("creating: %s\n", path);
2813
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666);
@@ -35,21 +20,11 @@ static void create_file(const char *path, const char *buffer) {
3520
close(fd);
3621
}
3722

38-
39-
void setup() {
40-
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
41-
makedir("working");
42-
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
43-
changedir("working");
44-
#endif
45-
}
46-
4723
int main() {
48-
setup();
49-
create_file("a", "abc");
50-
create_file("b", "xyz");
51-
assert(rename("a", "b") == 0);
52-
assert(unlink("b") == 0);
53-
create_file("b", "xyz");
54-
printf("success\n");
24+
create_file("a", "abc");
25+
create_file("b", "xyz");
26+
assert(rename("a", "b") == 0);
27+
assert(unlink("b") == 0);
28+
create_file("b", "xyz");
29+
printf("success\n");
5530
}

test/fs/test_fs_symlink_resolution.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include <assert.h>
77
#include <stdio.h>
88
#include <string.h>
9-
#if defined(__EMSCRIPTEN__)
10-
#include "emscripten.h"
11-
#endif
129

1310
void makedir(const char *dir) {
1411
int rtn = mkdir(dir, 0777);
@@ -29,11 +26,6 @@ static void create_file(const char *path) {
2926
}
3027

3128
void setup() {
32-
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
33-
makedir("working");
34-
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
35-
changedir("working");
36-
#endif
3729
makedir("a");
3830
makedir("b");
3931
makedir("b/c");

test/fs/test_mmap.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919

2020
void test_mmap_read() {
2121
// Use mmap to read in.txt
22-
EM_ASM(FS.writeFile('yolo/in.txt', 'mmap ftw!'));
22+
EM_ASM(FS.writeFile('in.txt', 'mmap ftw!'));
2323

24-
int fd = open("yolo/in.txt", O_RDONLY);
24+
int fd = open("in.txt", O_RDONLY);
2525
assert(fd >= 0);
2626

2727
int filesize = 9;
2828
char* map = (char*)mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
2929
assert(map != MAP_FAILED);
3030

31-
printf("yolo/in.txt content=");
31+
printf("in.txt content=");
3232
for (int i = 0; i < filesize; i++) {
3333
printf("%c", map[i]);
3434
}
@@ -42,7 +42,7 @@ void test_mmap_read() {
4242

4343
void test_mmap_write() {
4444
// Use mmap to write out.txt
45-
int fd = open("yolo/out.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
45+
int fd = open("out.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
4646
assert(fd >= 0);
4747

4848
const char* text = "written mmap";
@@ -64,12 +64,12 @@ void test_mmap_write() {
6464
close(fd);
6565

6666
{
67-
FILE* fd = fopen("yolo/out.txt", "r");
67+
FILE* fd = fopen("out.txt", "r");
6868
assert(fd >= 0);
6969
char buffer[15];
7070
memset(buffer, 0, 15);
7171
fread(buffer, 1, 14, fd);
72-
printf("yolo/out.txt content=%s\n", buffer);
72+
printf("out.txt content=%s\n", buffer);
7373
fclose(fd);
7474
}
7575
}
@@ -79,7 +79,7 @@ void test_mmap_readonly() {
7979
// but make sure it's not overwritten on munmap
8080
const char* readonlytext = "readonly mmap\0";
8181
const char* text = "write mmap\0";
82-
const char* path = "yolo/outreadonly.txt";
82+
const char* path = "outreadonly.txt";
8383
size_t readonlytextsize = strlen(readonlytext);
8484
size_t textsize = strlen(text);
8585

@@ -102,18 +102,18 @@ void test_mmap_readonly() {
102102
close(fd);
103103

104104
{
105-
FILE* fd = fopen("yolo/outreadonly.txt", "r");
105+
FILE* fd = fopen("outreadonly.txt", "r");
106106
assert(fd >= 0);
107107
char buffer[16];
108108
memset(buffer, 0, 16);
109109
fread(buffer, 1, 15, fd);
110-
printf("yolo/outreadonly.txt content=%s\n", buffer);
110+
printf("outreadonly.txt content=%s\n", buffer);
111111
fclose(fd);
112112
}
113113
}
114114

115115
void test_mmap_private() {
116-
int fd = open("yolo/private.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
116+
int fd = open("private.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
117117
assert(fd >= 0);
118118

119119
const char* text = "written mmap";
@@ -135,18 +135,18 @@ void test_mmap_private() {
135135
close(fd);
136136

137137
{
138-
FILE* fd = fopen("yolo/private.txt", "r");
138+
FILE* fd = fopen("private.txt", "r");
139139
assert(fd >= 0);
140140
char buffer[15];
141141
memset(buffer, 0, 15);
142142
fread(buffer, 1, 14, fd);
143-
printf("yolo/private.txt content=%s\n", buffer);
143+
printf("private.txt content=%s\n", buffer);
144144
fclose(fd);
145145
}
146146
}
147147

148148
void test_mmap_shared_with_offset() {
149-
int fd = open("yolo/sharedoffset.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
149+
int fd = open("sharedoffset.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
150150
assert(fd > 0);
151151

152152
const char* text = "written shared mmap with offset";
@@ -176,7 +176,7 @@ void test_mmap_shared_with_offset() {
176176
close(fd);
177177

178178
{
179-
FILE* fd = fopen("yolo/sharedoffset.txt", "r");
179+
FILE* fd = fopen("sharedoffset.txt", "r");
180180
assert(fd >= 0);
181181
size_t offset = sysconf(_SC_PAGE_SIZE) * 2;
182182

@@ -185,14 +185,14 @@ void test_mmap_shared_with_offset() {
185185
fseek(fd, offset, SEEK_SET);
186186
fread(buffer, 1, 32, fd);
187187
// expect text written from mmap operation to appear at offset in the file
188-
printf("yolo/sharedoffset.txt content=%s %zu\n", buffer, offset);
188+
printf("sharedoffset.txt content=%s %zu\n", buffer, offset);
189189
fclose(fd);
190190
}
191191
}
192192

193193
void test_mmap_hint() {
194194
// mmap with a address is expected to fail
195-
int fd = open("yolo/private.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
195+
int fd = open("private.txt", O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
196196
assert(fd != -1);
197197

198198
size_t map_size = 1 << 16;
@@ -225,7 +225,7 @@ void test_mmap_hint() {
225225
*/
226226
void test_mmap_overallocate() {
227227
#if !defined(NODEFS) && !defined(NODERAWFS) && !defined(WASMFS)
228-
int fd = open("yolo/overallocatedfile.txt", O_RDWR | O_CREAT, (mode_t)0600);
228+
int fd = open("overallocatedfile.txt", O_RDWR | O_CREAT, (mode_t)0600);
229229
assert(fd != -1);
230230

231231
const size_t textsize = 33;
@@ -236,7 +236,7 @@ void test_mmap_overallocate() {
236236
}
237237

238238
EM_ASM({
239-
const stream = FS.streams.find(stream => stream.path.indexOf('yolo/overallocatedfile.txt') >= 0);
239+
const stream = FS.streams.find(stream => stream.path.indexOf('overallocatedfile.txt') >= 0);
240240
assert(stream.node.usedBytes === Number($0),
241241
'Used bytes on the over-allocated file (' + stream.node.usedBytes + ') ' +
242242
'should be ' + $0
@@ -264,12 +264,6 @@ void test_mmap_overallocate() {
264264
}
265265

266266
int main() {
267-
EM_ASM(
268-
FS.mkdir('yolo');
269-
#if NODEFS
270-
FS.mount(NODEFS, { root: '.' }, 'yolo');
271-
#endif
272-
);
273267
test_mmap_read();
274268
test_mmap_write();
275269
test_mmap_readonly();

test/fs/test_mmap.out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
yolo/in.txt content=mmap ftw!
2-
yolo/out.txt content=written mmap
3-
yolo/outreadonly.txt content=readonly mmap
4-
yolo/private.txt content=
5-
yolo/sharedoffset.txt content=written shared mmap with offset 131072
1+
in.txt content=mmap ftw!
2+
out.txt content=written mmap
3+
outreadonly.txt content=readonly mmap
4+
private.txt content=
5+
sharedoffset.txt content=written shared mmap with offset 131072

test/fs/test_nodefs_cloexec.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,11 @@
1111
#include <fcntl.h>
1212
#include <emscripten.h>
1313

14-
#ifdef NODERAWFS
15-
#define CWD ""
16-
#else
17-
#define CWD "/working/"
18-
#endif
19-
20-
int main(void)
21-
{
22-
EM_ASM(
23-
#ifdef NODERAWFS
24-
FS.close(FS.open('test.txt', 'w'));
25-
#else
26-
FS.mkdir('/working');
27-
FS.mount(NODEFS, {root: '.'}, '/working');
28-
FS.close(FS.open('/working/test.txt', 'w'));
29-
#endif
30-
);
31-
assert(open(CWD "test.txt", O_RDONLY | O_CLOEXEC) != -1);
32-
printf("success\n");
33-
return 0;
14+
int main(void) {
15+
EM_ASM(
16+
FS.close(FS.open('test.txt', 'w'));
17+
);
18+
assert(open("test.txt", O_RDONLY | O_CLOEXEC) != -1);
19+
printf("success\n");
20+
return 0;
3421
}

test/fs/test_nodefs_dup.c

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,27 @@
1111
#include <stdio.h>
1212
#include <unistd.h>
1313

14-
#ifdef NODERAWFS
15-
#define CWD ""
16-
#else
17-
#define CWD "/working/"
18-
#endif
14+
int main(void) {
15+
EM_ASM(
16+
FS.close(FS.open('test.txt', 'w'));
17+
);
1918

20-
int main(void)
21-
{
22-
EM_ASM(
23-
#ifdef NODERAWFS
24-
FS.close(FS.open('test.txt', 'w'));
25-
#else
26-
FS.mkdir('/working');
27-
FS.mount(NODEFS, {root: '.'}, '/working');
28-
FS.close(FS.open('/working/test.txt', 'w'));
29-
#endif
30-
);
31-
int fd1 = open(CWD "test.txt", O_WRONLY);
32-
int fd2 = dup(fd1);
33-
int fd3 = fcntl(fd1, F_DUPFD_CLOEXEC, 0);
19+
int fd = open("test.txt", O_CREAT, 0444);
20+
assert(fd > 0);
21+
close(fd);
3422

35-
assert(fd1 == 3);
36-
assert(fd2 == 4);
37-
assert(fd3 == 5);
38-
assert(close(fd1) == 0);
39-
assert(write(fd2, "abcdef", 6) == 6);
40-
assert(close(fd2) == 0);
41-
assert(write(fd3, "ghijkl", 6) == 6);
42-
assert(close(fd3) == 0);
43-
printf("success\n");
44-
return 0;
23+
int fd1 = open("test.txt", O_WRONLY);
24+
int fd2 = dup(fd1);
25+
int fd3 = fcntl(fd1, F_DUPFD_CLOEXEC, 0);
26+
27+
assert(fd1 == 3);
28+
assert(fd2 == 4);
29+
assert(fd3 == 5);
30+
assert(close(fd1) == 0);
31+
assert(write(fd2, "abcdef", 6) == 6);
32+
assert(close(fd2) == 0);
33+
assert(write(fd3, "ghijkl", 6) == 6);
34+
assert(close(fd3) == 0);
35+
printf("success\n");
36+
return 0;
4537
}

test/fs/test_nodefs_rw.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
#include <errno.h>
1212
#include <emscripten.h>
1313

14-
#ifdef NODERAWFS
15-
#define CWD ""
16-
#else
17-
#define CWD "/working/"
18-
#endif
19-
2014
int main() {
2115
FILE *file;
2216
int res;
@@ -28,17 +22,8 @@ int main() {
2822
fs.writeFileSync('foobar.txt', 'yeehaw');
2923
);
3024

31-
#ifndef NODERAWFS
32-
// mount the current folder as a NODEFS instance
33-
// inside of emscripten
34-
EM_ASM(
35-
FS.mkdir('/working');
36-
FS.mount(NODEFS, { root: '.' }, '/working');
37-
);
38-
#endif
39-
4025
// read and validate the contents of the file
41-
file = fopen(CWD "foobar.txt", "r");
26+
file = fopen("foobar.txt", "r");
4227
assert(file);
4328
res = fread(buffer, sizeof(char), 6, file);
4429
assert(res == 6);
@@ -49,7 +34,7 @@ int main() {
4934
assert(!strcmp(buffer, "yeehaw"));
5035

5136
// write out something new
52-
file = fopen(CWD "foobar.txt", "w");
37+
file = fopen("foobar.txt", "w");
5338
assert(file);
5439
res = fwrite("cheez", sizeof(char), 5, file);
5540
assert(res == 5);
@@ -62,7 +47,7 @@ int main() {
6247
assert(contents === 'cheez');
6348
);
6449

65-
file = fopen(CWD "csfsq", "r");
50+
file = fopen("csfsq", "r");
6651
assert(file == NULL);
6752
assert(errno == ENOENT);
6853

0 commit comments

Comments
 (0)