-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathbindings.rs
More file actions
260 lines (202 loc) · 6.2 KB
/
Copy pathbindings.rs
File metadata and controls
260 lines (202 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use std::{
ffi::OsStr,
fs,
path::{Path, PathBuf},
};
use assert_cmd::{Command, assert::Assert, cargo};
use flate2::bufread::GzDecoder;
use fs_extra::dir::CopyOptions;
use predicates::{Predicate, prelude::predicate};
use tar::Archive;
#[test]
fn lint_cli_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/cli", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("cli");
generate_bindings(&path, "wasi:cli/command@0.2.0")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(&path, ["--strict", "-m", "app"]);
Ok(())
}
#[test]
fn lint_cli_p3_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/cli-p3", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("cli-p3");
generate_bindings(&path, "wasi:cli/command@0.3.0-rc-2026-01-06")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
_ = dir.keep();
mypy_check(&path, ["--strict", "-m", "app"]);
Ok(())
}
#[test]
fn lint_http_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/http", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("http");
generate_bindings(&path, "wasi:http/proxy@0.2.0")?;
// poll_loop.py has many errors that might not be worth adjusting at the moment, so ignore for now
fs::remove_file(path.join("poll_loop.py")).unwrap();
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(
&path,
[
"--strict",
// poll_loop.py has many errors that might not be worth adjusting at the moment, so ignore for now
"--ignore-missing-imports",
"-m",
"app",
],
);
Ok(())
}
#[test]
fn lint_http_p3_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/http-p3", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("http-p3");
generate_bindings(&path, "wasi:http/service@0.3.0-rc-2026-01-06")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
_ = dir.keep();
mypy_check(&path, ["--strict", "-m", "app"]);
Ok(())
}
#[test]
fn lint_matrix_math_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/matrix-math", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("matrix-math");
install_numpy(&path)?;
generate_bindings(&path, "matrix-math")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(
&path,
[
"--strict",
// numpy doesn't pass
"--follow-imports",
"silent",
"-m",
"app",
],
);
Ok(())
}
#[test]
fn lint_sandbox_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(&["./examples/sandbox"], dir.path(), &CopyOptions::new())?;
let path = dir.path().join("sandbox");
cargo::cargo_bin_cmd!("componentize-py")
.current_dir(&path)
.args(["-d", "sandbox.wit", "bindings", "."])
.assert()
.success();
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(&path, ["--strict", "-m", "guest"]);
Ok(())
}
#[test]
fn lint_tcp_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/tcp", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("tcp");
generate_bindings(&path, "wasi:cli/command@0.2.0")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(&path, ["--strict", "-m", "app"]);
Ok(())
}
#[test]
fn lint_tcp_p3_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/tcp-p3", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("tcp-p3");
generate_bindings(&path, "wasi:cli/command@0.3.0-rc-2026-01-06")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(&path, ["--strict", "-m", "app"]);
Ok(())
}
#[test]
fn lint_tls_p3_bindings() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
fs_extra::copy_items(
&["./examples/tls-p3", "./wit"],
dir.path(),
&CopyOptions::new(),
)?;
let path = dir.path().join("tls-p3");
generate_bindings(&path, "tls-p3")?;
assert!(predicate::path::is_dir().eval(&path.join("wit_world")));
mypy_check(&path, ["--strict", "-m", "app"]);
Ok(())
}
fn generate_bindings(path: &Path, world: &str) -> Result<Assert, anyhow::Error> {
Ok(cargo::cargo_bin_cmd!("componentize-py")
.current_dir(path)
.args(["-d", "../wit", "-w", world, "bindings", "."])
.assert()
.success())
}
fn mypy_check<I, S>(path: &Path, args: I) -> Assert
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
Command::new("python3")
.current_dir(path)
.args(["-m", "venv", ".venv"])
.assert()
.success();
Command::new(venv_path(path).join("pip"))
.current_dir(path)
.args(["install", "mypy"])
.assert()
.success();
Command::new(venv_path(path).join("mypy"))
.current_dir(path)
.args(args)
.assert()
.success()
.stdout(predicate::str::is_match("Success: no issues found in 1 source file").unwrap())
}
fn venv_path(path: &Path) -> PathBuf {
path.join(".venv")
.join(if cfg!(windows) { "Scripts" } else { "bin" })
}
fn install_numpy(path: &Path) -> anyhow::Result<()> {
let bytes = reqwest::blocking::get(
"https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz",
)?
.error_for_status()?
.bytes()?;
Archive::new(GzDecoder::new(&bytes[..])).unpack(path)?;
Ok(())
}