Skip to content

Commit fb4bdc7

Browse files
authored
Convert test_stat_fail_alongtheway to a file based test (#21138)
Also remove the special logging and just use assert which fails fast and gives a backtrace.
1 parent 50a7531 commit fb4bdc7

File tree

3 files changed

+47
-71
lines changed

3 files changed

+47
-71
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <assert.h>
2+
#include <errno.h>
3+
#include <stdio.h>
4+
#include <sys/types.h>
5+
#include <sys/stat.h>
6+
#include <unistd.h>
7+
#include <stdlib.h>
8+
#include <fcntl.h>
9+
#include <string.h>
10+
11+
int main() {
12+
assert(mkdir("path", 0777) == 0);
13+
assert(close(open("path/file", O_CREAT | O_WRONLY, 0644)) == 0);
14+
{
15+
struct stat st;
16+
assert(stat("path", &st) == 0);
17+
assert(st.st_mode = 0777);
18+
}
19+
{
20+
struct stat st;
21+
assert(stat("path/nosuchfile", &st) == -1);
22+
printf("info: errno=%d %s\n", errno, strerror(errno));
23+
assert(errno == ENOENT);
24+
}
25+
{
26+
struct stat st;
27+
assert(stat("path/file", &st) == 0);
28+
assert(st.st_mode = 0666);
29+
}
30+
{
31+
struct stat st;
32+
assert(stat("path/file/impossible", &st) == -1);
33+
printf("info: errno=%d %s\n", errno, strerror(errno));
34+
assert(errno == ENOTDIR);
35+
}
36+
{
37+
struct stat st;
38+
assert(lstat("path/file/impossible", &st) == -1);
39+
printf("info: errno=%d %s\n", errno, strerror(errno));
40+
assert(errno == ENOTDIR);
41+
}
42+
return 0;
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
info: errno=44 No such file or directory
2+
info: errno=54 Not a directory
3+
info: errno=54 Not a directory

test/test_other.py

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6122,77 +6122,7 @@ def test(opts, has, not_has):
61226122
test(['-sEXPORTED_RUNTIME_METHODS=[]', '-sEXPORTED_RUNTIME_METHODS=addRunDependency'], "Module['addRunDependency", "Module['waka")
61236123

61246124
def test_stat_fail_alongtheway(self):
6125-
create_file('src.c', r'''
6126-
#include <errno.h>
6127-
#include <stdio.h>
6128-
#include <sys/types.h>
6129-
#include <sys/stat.h>
6130-
#include <unistd.h>
6131-
#include <stdlib.h>
6132-
#include <fcntl.h>
6133-
#include <string.h>
6134-
6135-
#define CHECK(expression) \
6136-
if(!(expression)) { \
6137-
error = errno; \
6138-
printf("FAIL: %s\n", #expression); fail = 1; \
6139-
} else { \
6140-
error = errno; \
6141-
printf("pass: %s\n", #expression); \
6142-
} \
6143-
6144-
int main() {
6145-
int error;
6146-
int fail = 0;
6147-
CHECK(mkdir("path", 0777) == 0);
6148-
CHECK(close(open("path/file", O_CREAT | O_WRONLY, 0644)) == 0);
6149-
{
6150-
struct stat st;
6151-
CHECK(stat("path", &st) == 0);
6152-
CHECK(st.st_mode = 0777);
6153-
}
6154-
{
6155-
struct stat st;
6156-
CHECK(stat("path/nosuchfile", &st) == -1);
6157-
printf("info: errno=%d %s\n", error, strerror(error));
6158-
CHECK(error == ENOENT);
6159-
}
6160-
{
6161-
struct stat st;
6162-
CHECK(stat("path/file", &st) == 0);
6163-
CHECK(st.st_mode = 0666);
6164-
}
6165-
{
6166-
struct stat st;
6167-
CHECK(stat("path/file/impossible", &st) == -1);
6168-
printf("info: errno=%d %s\n", error, strerror(error));
6169-
CHECK(error == ENOTDIR);
6170-
}
6171-
{
6172-
struct stat st;
6173-
CHECK(lstat("path/file/impossible", &st) == -1);
6174-
printf("info: errno=%d %s\n", error, strerror(error));
6175-
CHECK(error == ENOTDIR);
6176-
}
6177-
return fail;
6178-
}
6179-
''')
6180-
self.do_runf('src.c', r'''pass: mkdir("path", 0777) == 0
6181-
pass: close(open("path/file", O_CREAT | O_WRONLY, 0644)) == 0
6182-
pass: stat("path", &st) == 0
6183-
pass: st.st_mode = 0777
6184-
pass: stat("path/nosuchfile", &st) == -1
6185-
info: errno=44 No such file or directory
6186-
pass: error == ENOENT
6187-
pass: stat("path/file", &st) == 0
6188-
pass: st.st_mode = 0666
6189-
pass: stat("path/file/impossible", &st) == -1
6190-
info: errno=54 Not a directory
6191-
pass: error == ENOTDIR
6192-
pass: lstat("path/file/impossible", &st) == -1
6193-
info: errno=54 Not a directory
6194-
pass: error == ENOTDIR
6195-
''')
6125+
self.do_other_test('test_stat_fail_alongtheway.c')
61966126

61976127
def test_link_with_a_static(self):
61986128
create_file('x.c', r'''

0 commit comments

Comments
 (0)