Skip to content

Commit 7342ee8

Browse files
authored
Cleanup some filesystem tests. NFC (#23088)
There were a couple of old tests in `test/filesystem`. This change modes them to `test/fs` and improves them.
1 parent cefc9de commit 7342ee8

9 files changed

+101
-114
lines changed

test/filesystem/bad_lookup.cpp

Lines changed: 0 additions & 106 deletions
This file was deleted.

test/fs/test_fs_bad_lookup.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2014 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#include <errno.h>
7+
#include <fcntl.h>
8+
#include <stdbool.h>
9+
#include <stdio.h>
10+
#include <string.h>
11+
#include <sys/stat.h>
12+
#include <unistd.h>
13+
14+
//--------------------------------------------------------------------------
15+
// Helper to create an empty file with the given path.
16+
void touch(const char* path, const mode_t mode) {
17+
printf("Touching file: %s with mode=%o\n", path, mode);
18+
19+
int fd = open(path, O_CREAT | O_WRONLY, mode);
20+
if (fd == -1) {
21+
int error = errno;
22+
printf("Failed to touch file using open: %s; %s\n", path, strerror(errno));
23+
} else {
24+
close(fd);
25+
}
26+
}
27+
28+
//--------------------------------------------------------------------------
29+
// Stats the given path and prints the mode. Returns true if the path
30+
// exists; false otherwise.
31+
bool exists(const char* path) {
32+
struct stat path_stat;
33+
if (lstat(path, &path_stat) != 0) {
34+
int error = errno;
35+
if (error == ENOENT) {
36+
// Only bother logging if something other than the path not existing
37+
// went wrong.
38+
printf("Failed to lstat path: %s; %s", path, strerror(error));
39+
}
40+
return false;
41+
}
42+
43+
printf("Mode for path=%s: %o\n", path, path_stat.st_mode);
44+
return true;
45+
}
46+
47+
int main() {
48+
touch("file1", 0667);
49+
if (!exists("file1")) {
50+
printf("Failed to create path: file1\n");
51+
return 1;
52+
}
53+
if (exists("file1/dir")) {
54+
printf("Path should not exists: file1/dir\n");
55+
return 1;
56+
}
57+
58+
touch("file2", 0676);
59+
if (!exists("file2")) {
60+
printf("Failed to create path: file2\n");
61+
return 1;
62+
}
63+
if (exists("file2/dir")) {
64+
printf("Path should not exists: file2/dir\n");
65+
return 1;
66+
}
67+
68+
touch("file3", 0766);
69+
if (!exists("file3")) {
70+
printf("Failed to create path: file3\n");
71+
return 1;
72+
}
73+
if (exists("file3/dir")) {
74+
printf("Path should not exists: file3/dir\n");
75+
return 1;
76+
}
77+
78+
printf("ok.\n");
79+
return 0;
80+
}
81+

test/fs/test_fs_base.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
// Nothing to do here. Test is written in JS. See test/fs/test_fs_base.js.
3+
return 0;
4+
}
File renamed without changes.

test/filesystem/output.txt renamed to test/fs/test_fs_base.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,4 @@
195195
parentExists: false
196196
parentPath: null
197197
parentObject.contents: null
198+

test/filesystem/test_dev_random.c renamed to test/fs/test_fs_dev_random.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ int main() {
2222
assert(nread == byte_count);
2323
fclose(fp);
2424

25+
printf("success\n");
2526
return 0;
2627
}

test/test_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,8 @@ def test():
812812
# test()
813813

814814
@also_with_wasmfs
815-
def test_dev_random(self):
816-
self.btest_exit('filesystem/test_dev_random.c')
815+
def test_fs_dev_random(self):
816+
self.btest_exit('fs/test_fs_dev_random.c')
817817

818818
def test_sdl_swsurface(self):
819819
self.btest_exit('test_sdl_swsurface.c', args=['-lSDL', '-lGL'])

test/test_core.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5674,10 +5674,8 @@ def test_istream(self):
56745674

56755675
def test_fs_base(self):
56765676
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$FS'])
5677-
self.add_pre_run(read_file(test_file('filesystem/src.js')))
5678-
src = 'int main() {return 0;}\n'
5679-
expected = read_file(test_file('filesystem/output.txt'))
5680-
self.do_run(src, expected)
5677+
self.add_pre_run(read_file(test_file('fs/test_fs_base.js')))
5678+
self.do_run_in_out_file_test('fs/test_fs_base.c')
56815679

56825680
@also_with_noderawfs
56835681
@is_slow_test

test/test_other.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6097,8 +6097,16 @@ class time_iterator {
60976097
''')
60986098
self.run_process([EMXX, 'src.cpp', '-O2', '-sSAFE_HEAP'])
60996099

6100-
def test_bad_lookup(self):
6101-
self.do_runf(path_from_root('test/filesystem/bad_lookup.cpp'), expected_output='ok')
6100+
@also_with_wasmfs
6101+
@also_with_noderawfs
6102+
@crossplatform
6103+
def test_fs_bad_lookup(self):
6104+
self.do_runf(path_from_root('test/fs/test_fs_bad_lookup.c'), expected_output='ok')
6105+
6106+
@also_with_wasmfs
6107+
@also_with_noderawfs
6108+
def test_fs_dev_random(self):
6109+
self.do_runf('fs/test_fs_dev_random.c', 'success')
61026110

61036111
@parameterized({
61046112
'none': [{'EMCC_FORCE_STDLIBS': None}, False],

0 commit comments

Comments
 (0)