Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/src/test/posix/unistd.test.ts
1067 views
1
import { syncPython } from "../../node";
2
import { hostname, userInfo } from "os";
3
4
test("test getting the hostname via python, which calls the gethostname system call", async () => {
5
const { exec, repr } = await syncPython();
6
exec("import socket");
7
expect(repr("socket.gethostname()")).toEqual(`'${hostname()}'`);
8
});
9
10
test("test python's os.getlogin() which calls the getlogin systemcall", async () => {
11
const { exec, repr } = await syncPython();
12
exec("import os");
13
expect(repr("os.getlogin()")).toEqual(`'${userInfo?.()?.username}'`);
14
});
15
16
test("test python's os.getpgrp returns a positive integer", async () => {
17
const { exec, repr } = await syncPython();
18
exec("import os");
19
expect(eval(repr("os.getpgrp()"))).toBeGreaterThan(0);
20
});
21
22
test("test python's os.getgroups returns a list of positive integer", async () => {
23
const { exec, repr } = await syncPython();
24
exec("import os");
25
const v = eval(repr("os.getgroups()"));
26
for (const a of v) {
27
expect(a).toBeGreaterThanOrEqual(0);
28
}
29
});
30
31
test("consistency check involving statvfs", async () => {
32
const { exec, repr } = await syncPython();
33
exec("import os");
34
const f_namemax = eval(repr("os.statvfs('/').f_namemax"));
35
// it's 255 on some linux and macos, so we'll just do a consistency check,
36
// and not have to add another dependency on posix-node.
37
// import posix from "posix-node";
38
//expect(f_namemax).toBe(posix.statvfs?.("/").f_namemax);
39
expect(f_namemax >= 128 && f_namemax <= 1024).toBe(true);
40
});
41
42
// can't do this during jest testing, though it works on the command line:
43
// test("consistency check involving fstatvfs", async () => {
44
// await init({ debug: true });
45
// await exec("import os");
46
// const f_namemax = eval(await repr("os.fstatvfs(1).f_namemax"));
47
// expect(f_namemax).toBe(posix.fstatvfs?.(1).f_namemax);
48
// });
49
50
test("using getresuid on Linux only", async () => {
51
const { exec, repr } = await syncPython();
52
exec("import os");
53
if (process.platform == "linux") {
54
const resuid = eval("[" + repr("os.getresuid()").slice(1, -1) + "]");
55
// should be a triple of numbers
56
expect(resuid.length).toBe(3);
57
for (const n of resuid) {
58
expect(typeof n).toBe("number");
59
}
60
}
61
});
62
63
test("using getresgid on Linux only", async () => {
64
const { exec, repr } = await syncPython();
65
exec("import os");
66
if (process.platform == "linux") {
67
const resgid = eval("[" + repr("os.getresgid()").slice(1, -1) + "]");
68
// should be a triple of numbers
69
expect(resgid.length).toBe(3);
70
for (const n of resgid) {
71
expect(typeof n).toBe("number");
72
}
73
}
74
});
75
76
// setresuid/setresgid can only be done as root, so we only test that they throw here
77
78
// test("setresuid throws", async () => {
79
// await exec("import os");
80
// expect(repr("os.setresuid(0,0,0)")).rejects.toThrow();
81
// });
82
83
// >>> import os, posix; os.getgrouplist(os.getlogin(),0)
84
// [0, 12, 20, 61, 79, 80, 81, 98, 701, 33, 100, 204, 250, 395, 398, 399, 400]
85
test("getgrouplist returns a list of numbers", async () => {
86
const { exec, repr } = await syncPython();
87
exec("import os, posix");
88
const v = eval(repr("os.getgrouplist(os.getlogin(),0)"));
89
expect(v.length).toBeGreaterThan(0);
90
expect(typeof v[0]).toBe("number");
91
});
92
93
// wasi doesn't have fchdir, so I implemented it and I'm testing it here via python.
94
test("fchdir works", async () => {
95
// This is complicated, since e.g., on macos if you do "cd /tmp" you
96
// end up in /private/tmp", etc. It's weird.
97
const { exec, repr } = await syncPython();
98
exec("import tempfile; td = tempfile.TemporaryDirectory()");
99
exec("import os; fd = os.open(td.name, os.O_RDONLY)");
100
exec("os.fchdir(fd)");
101
const actual = eval(repr("os.getcwd()"));
102
exec("os.mkdir('abc')");
103
exec("fd2 = os.open('abc', os.O_RDONLY)");
104
exec("os.fchdir(fd2)");
105
expect(eval(repr("os.getcwd()"))).toBe(actual + "/abc");
106
// it doesn't seem to always get removed (which is weird)
107
exec("import shutil; shutil.rmtree(td.name)");
108
});
109
110