Skip to content

Commit 80798ac

Browse files
authored
Cleanup symlink tests (#21890)
- Add some more tests to `links.c` and make sure it runs on native linux. - Simplify symlink_on_nodefs.c by using assert. Also, fix `utils.delete_content` so that it also removes dangling symlinks. Without this the dangling symlinks in the test output directory are not removed.
1 parent fa80cf2 commit 80798ac

File tree

7 files changed

+44
-66
lines changed

7 files changed

+44
-66
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ jobs:
609609
wasmfs.test_stat
610610
wasmfs.test_fstatat
611611
wasmfs.test_futimens
612-
wasmfs.test_unistd_links_memfs
612+
wasmfs.test_unistd_links
613613
wasmfs.test_fcntl_open
614614
wasmfs.test_fs_js_api
615615
wasmfs.test_fs_llseek

src/library_fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,8 @@ FS.staticInit();` +
10141014
throw new FS.ErrnoError({{{ cDefs.ENOENT }}});
10151015
}
10161016
flags = typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags;
1017-
mode = typeof mode == 'undefined' ? 438 /* 0666 */ : mode;
10181017
if ((flags & {{{ cDefs.O_CREAT }}})) {
1018+
mode = typeof mode == 'undefined' ? 438 /* 0666 */ : mode;
10191019
mode = (mode & {{{ cDefs.S_IALLUGO }}}) | {{{ cDefs.S_IFREG }}};
10201020
} else {
10211021
mode = 0;

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6068,7 +6068,7 @@ def test_unistd_unlink(self, fs):
60686068
self.do_runf('unistd/unlink.c', 'success')
60696069

60706070
@parameterized({
6071-
'memfs': ([], False),
6071+
'': ([], False),
60726072
'nodefs': (['-DNODEFS', '-lnodefs.js'], True)
60736073
})
60746074
def test_unistd_links(self, args, nodefs):

test/unistd/links.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,32 @@
77

88
#include <assert.h>
99
#include <errno.h>
10+
#include <fcntl.h>
1011
#include <stdio.h>
1112
#include <string.h>
13+
#include <sys/stat.h>
1214
#include <unistd.h>
1315

1416
#ifdef __EMSCRIPTEN__
1517
#include <emscripten.h>
16-
#else
17-
#include <sys/stat.h>
18-
#include <fcntl.h>
1918
#endif
2019

2120
void setup() {
22-
#ifdef __EMSCRIPTEN__
23-
EM_ASM(
24-
FS.mkdir('working');
25-
#if NODEFS
26-
FS.mount(NODEFS, { root: '.' }, 'working');
21+
int rtn = mkdir("working", 0777);
22+
assert(rtn == 0);
23+
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
24+
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
2725
#endif
28-
FS.chdir('working');
29-
FS.symlink('../test/../there!', 'link');
30-
FS.writeFile('file', 'test');
31-
FS.mkdir('folder');
32-
);
33-
#else
34-
mkdir("working", 0777);
35-
chdir("working");
26+
rtn = chdir("working");
27+
assert(rtn == 0);
3628
symlink("../test/../there!", "link");
37-
int fd = open("file", O_RDWR);
38-
write(fd, "test", 5);
29+
int fd = open("file", O_RDWR | O_CREAT, 0777);
30+
assert(fd >= 0);
31+
rtn = write(fd, "test", 5);
32+
assert(rtn == 5);
3933
close(fd);
40-
mkdir("folder", 0777);
41-
#endif
34+
rtn = mkdir("folder", 0777);
35+
assert(rtn == 0);
4236
}
4337

4438
int main() {

test/unistd/symlink_on_nodefs.c

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
910
#include <errno.h>
1011
#include <unistd.h>
@@ -14,52 +15,39 @@
1415
#include <limits.h>
1516
#include <stdlib.h>
1617

17-
int main() {
18+
void setup() {
1819
EM_ASM(
19-
fs.mkdirSync('./new-directory', '0777');
20-
fs.writeFileSync('./new-directory/test', 'Link it');
21-
fs.symlinkSync(fs.realpathSync('./new-directory'), './symlink');
20+
fs.mkdirSync('new-directory', '0777');
21+
fs.writeFileSync('new-directory/test', 'Link it');
22+
fs.symlinkSync(fs.realpathSync('new-directory'), 'symlink');
2223

2324
FS.mkdir('working');
2425
FS.mount(NODEFS, { root: '.' }, 'working');
2526

2627
FS.mkdir('direct-link');
27-
FS.mount(NODEFS, { root: './symlink' }, 'direct-link');
28+
FS.mount(NODEFS, { root: 'symlink' }, 'direct-link');
2829
);
30+
}
2931

30-
{
31-
const char* path = "/working/symlink/test";
32-
printf("reading %s\n", path);
32+
int main() {
33+
setup();
3334

34-
FILE* fd = fopen(path, "r");
35-
if (fd == NULL) {
36-
printf("failed to open file %s\n", path);
37-
}
38-
else {
39-
char buffer[8];
40-
fread(buffer, 1, 7, fd);
41-
buffer[7] = 0;
42-
printf("buffer is %s\n", buffer);
43-
fclose(fd);
44-
}
45-
}
35+
char buf[1024];
36+
readlink("/working/symlink", buf, 1024);
37+
printf("readlink: %s\n", buf);
4638

47-
printf("\n");
39+
FILE* fd = fopen("/working/symlink/test", "r");
40+
assert(fd);
41+
char buffer[8] = {0};
42+
int rtn = fread(buffer, 1, 7, fd);
43+
assert(rtn == 7);
44+
printf("buffer is '%s'\n", buffer);
45+
fclose(fd);
4846

49-
{
50-
const char* path = "/direct-link/test";
51-
printf("reading %s\n", path);
47+
// This should fail, since it resolves to ../new-directory which is not
48+
// mounted
49+
fd = fopen("/direct-link/test", "r");
50+
assert(fd == NULL);
5251

53-
FILE* fd = fopen(path, "r");
54-
if (fd != NULL) {
55-
// This should not happen, it resolves to ../new-directory which is not mounted
56-
printf("opened file %s\n", path);
57-
fclose(fd);
58-
}
59-
else {
60-
printf("failed to open file %s\n", path);
61-
}
62-
}
63-
6452
return 0;
6553
}

test/unistd/symlink_on_nodefs.out

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
reading /working/symlink/test
2-
buffer is Link it
3-
4-
reading /direct-link/test
5-
failed to open file /direct-link/test
1+
readlink: /working/new-directory
2+
buffer is 'Link it'

tools/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ def write_binary(file_path, contents):
8484

8585
def delete_file(filename):
8686
"""Delete a file (if it exists)."""
87-
if not os.path.exists(filename):
88-
return
89-
os.remove(filename)
87+
if os.path.lexists(filename):
88+
os.remove(filename)
9089

9190

9291
def delete_dir(dirname):

0 commit comments

Comments
 (0)