rspyts-cli 0.3.1

The rspyts code generator: emits pydantic models, TypeScript types, and JSON Schema from a bridged Rust crate.
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use serde_json::Value;
use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

static FIXTURE_COUNTER: AtomicU64 = AtomicU64::new(0);

struct Fixture {
    root: PathBuf,
}

impl Fixture {
    fn new() -> Self {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock is after Unix epoch")
            .as_nanos();
        let counter = FIXTURE_COUNTER.fetch_add(1, Ordering::Relaxed);
        let root = std::env::temp_dir().join(format!(
            "rspyts-cli-build-integration-{}-{nonce}-{counter}",
            std::process::id(),
        ));
        std::fs::create_dir_all(root.join("rust/src")).expect("create fixture directories");

        std::fs::write(
            root.join("rust/Cargo.toml"),
            r#"[package]
name = "fixture-package"
version = "0.1.0"
edition = "2024"

[lib]
name = "actual_bridge"
crate-type = ["rlib", "cdylib"]
"#,
        )
        .expect("write fixture Cargo.toml");
        std::fs::write(
            root.join("rust/src/lib.rs"),
            r##"const MANIFEST: &[u8] = br#"{"abi":"3.0","crateName":"fixture-package","crateVersion":"0.1.0","types":[],"constants":[],"functions":[],"classes":[]}"#;

#[unsafe(no_mangle)]
pub extern "C" fn rspyts_abi_version() -> u32 {
    3
}

#[unsafe(no_mangle)]
pub extern "C" fn rspyts_manifest() -> *mut u8 {
    let mut envelope = Vec::with_capacity(12 + MANIFEST.len());
    envelope.extend_from_slice(&[0, 0, 0, 0]);
    envelope.extend_from_slice(&(MANIFEST.len() as u32).to_le_bytes());
    envelope.extend_from_slice(&0_u32.to_le_bytes());
    envelope.extend_from_slice(MANIFEST);
    Box::into_raw(envelope.into_boxed_slice()).cast::<u8>()
}

/// # Safety
/// `ptr` and `len` must be the exact allocation returned by `rspyts_manifest`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rspyts_free(ptr: *mut u8, len: usize) {
    if !ptr.is_null() {
        unsafe {
            drop(Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr, len)));
        }
    }
}
"##,
        )
        .expect("write fixture source");
        std::fs::write(
            root.join("rspyts.toml"),
            r#"[crate]
path = "rust"

[python]
out = "python/src/fixture_package/generated"
"#,
        )
        .expect("write fixture rspyts.toml");
        Self { root }
    }

    fn command(&self, subcommand: &str) -> Command {
        let mut command = Command::new(env!("CARGO_BIN_EXE_rspyts"));
        command
            .current_dir(&self.root)
            .arg(subcommand)
            .arg("--config")
            .arg(self.root.join("rspyts.toml"))
            // Explicit CLI `--target <rustc-host>` must override even an
            // unusable ambient Cargo target rather than mislabeling output.
            .env("CARGO_BUILD_TARGET", "not-a-real-rust-target")
            .env("CARGO_TARGET_DIR", self.root.join("cargo-target"));
        command
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.root);
    }
}

#[test]
fn build_is_the_only_command_that_stages_artifacts() {
    let fixture = Fixture::new();
    let generated = fixture.command("generate").output().expect("run generate");
    assert!(
        generated.status.success(),
        "generate failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&generated.stdout),
        String::from_utf8_lossy(&generated.stderr)
    );

    let artifact = fixture
        .root
        .join("python/src/fixture_package/generated/lib")
        .join(platform_filename("fixture-package"));
    assert!(
        !artifact.exists(),
        "generate must not stage a native library"
    );

    let output = fixture
        .command("build")
        .args(["--output-format", "json"])
        .output()
        .expect("run JSON build");
    assert!(
        output.status.success(),
        "build failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // `from_slice` accepting the complete byte slice proves stdout contains
    // no progress lines before or after the single JSON document.
    let report: Value = serde_json::from_slice(&output.stdout).expect("stdout is clean JSON");
    assert_eq!(report["formatVersion"], 1);
    assert_eq!(report["crate"]["name"], "fixture-package");
    assert_eq!(
        report["python"],
        serde_json::json!({"out": fixture.root.join("python/src/fixture_package/generated")})
    );

    let artifacts = report["artifacts"].as_array().expect("artifact array");
    assert_eq!(artifacts.len(), 1);
    assert_eq!(artifacts[0]["kind"], "native");
    assert_ne!(artifacts[0]["target"], "not-a-real-rust-target");

    let reported_artifact = PathBuf::from(
        artifacts[0]["path"]
            .as_str()
            .expect("artifact path is a string"),
    );
    assert!(reported_artifact.is_absolute());
    assert!(reported_artifact.is_file());
    assert_eq!(reported_artifact, artifact);
    assert!(
        !reported_artifact
            .to_string_lossy()
            .contains("actual_bridge")
    );
    assert!(
        !reported_artifact
            .to_string_lossy()
            .contains("not-a-real-rust-target")
    );

    let generated_loader = std::fs::read_to_string(
        fixture
            .root
            .join("python/src/fixture_package/generated/library.py"),
    )
    .expect("read generated loader");
    assert!(generated_loader.contains("        \"lib\","));

    std::fs::remove_file(&artifact).expect("remove source-staged artifact");
    let package_dir = fixture.root.join("package-input");
    let package_output = fixture
        .command("build")
        .args([
            "--out-dir",
            package_dir.to_str().expect("UTF-8 package path"),
            "--output-format",
            "json",
        ])
        .output()
        .expect("run packaging-safe JSON build");
    assert!(
        package_output.status.success(),
        "packaging-safe build failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&package_output.stdout),
        String::from_utf8_lossy(&package_output.stderr)
    );
    let package_report: Value =
        serde_json::from_slice(&package_output.stdout).expect("packaging stdout is clean JSON");
    let package_artifact = PathBuf::from(
        package_report["artifacts"][0]["path"]
            .as_str()
            .expect("packaging artifact path is a string"),
    );
    assert_eq!(
        package_artifact,
        package_dir.join(platform_filename("fixture-package"))
    );
    assert!(package_artifact.is_file());
    assert!(
        !artifact.exists(),
        "an explicit output directory must not recreate the package artifact"
    );

    let check_output = fixture
        .command("check")
        .output()
        .expect("run read-only check");
    assert!(
        check_output.status.success(),
        "packaging-safe check failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&check_output.stdout),
        String::from_utf8_lossy(&check_output.stderr)
    );
    assert!(
        package_artifact.is_file(),
        "check must not remove artifacts"
    );
    assert!(!artifact.exists(), "check must not stage a native library");

    let manifest_output = fixture.command("manifest").output().expect("run manifest");
    assert!(
        manifest_output.status.success(),
        "packaging-safe manifest failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&manifest_output.stdout),
        String::from_utf8_lossy(&manifest_output.stderr)
    );
    let manifest: Value =
        serde_json::from_slice(&manifest_output.stdout).expect("manifest stdout is clean JSON");
    assert_eq!(manifest["crateName"], "fixture-package");
    assert!(
        package_artifact.is_file(),
        "manifest must not remove artifacts"
    );
    assert!(
        !artifact.exists(),
        "manifest must not stage a native library"
    );

    let host = rustc_host();
    let target_only_output = fixture
        .command("build")
        .args(["--target", &host, "--output-format", "json"])
        .output()
        .expect("run target-only JSON build");
    assert!(
        target_only_output.status.success(),
        "target-only build failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&target_only_output.stdout),
        String::from_utf8_lossy(&target_only_output.stderr)
    );
    let target_only_report: Value = serde_json::from_slice(&target_only_output.stdout)
        .expect("target-only stdout is clean JSON");
    assert_eq!(target_only_report["artifacts"][0]["kind"], "target");
    assert_eq!(target_only_report["artifacts"][0]["target"], host);
    assert!(
        !artifact.exists(),
        "an explicit target build must not implicitly add the host"
    );

    let deduplicated_dir = fixture.root.join("deduplicated-host");
    let deduplicated_output = fixture
        .command("build")
        .args([
            "--target",
            "host",
            "--target",
            &host,
            "--out-dir",
            deduplicated_dir.to_str().expect("UTF-8 output path"),
            "--output-format",
            "json",
        ])
        .output()
        .expect("run deduplicated host build");
    assert!(
        deduplicated_output.status.success(),
        "deduplicated host build failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&deduplicated_output.stdout),
        String::from_utf8_lossy(&deduplicated_output.stderr)
    );
    let deduplicated_report: Value = serde_json::from_slice(&deduplicated_output.stdout)
        .expect("deduplicated host stdout is clean JSON");
    let deduplicated_artifacts = deduplicated_report["artifacts"]
        .as_array()
        .expect("deduplicated artifact array");
    assert_eq!(deduplicated_artifacts.len(), 1);
    assert_eq!(deduplicated_artifacts[0]["kind"], "native");
}

#[test]
fn check_is_read_only_for_clean_and_drifted_sources() {
    let fixture = Fixture::new();
    let generated = fixture.command("generate").output().expect("run generate");
    assert!(
        generated.status.success(),
        "generate failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&generated.stdout),
        String::from_utf8_lossy(&generated.stderr)
    );

    let artifact = fixture
        .root
        .join("python/src/fixture_package/generated/lib")
        .join(platform_filename("fixture-package"));
    let generated_file = fixture
        .root
        .join("python/src/fixture_package/generated/functions.py");
    let original_generated =
        std::fs::read_to_string(&generated_file).expect("read generated source");
    const LAST_KNOWN_GOOD: &[u8] = b"previously-staged-native-library";
    std::fs::create_dir_all(artifact.parent().expect("artifact parent"))
        .expect("create artifact directory");
    std::fs::write(&artifact, LAST_KNOWN_GOOD).expect("seed prior staged library");

    let regenerated = fixture
        .command("generate")
        .output()
        .expect("run generate again");
    assert!(regenerated.status.success());
    assert_eq!(
        std::fs::read(&artifact).expect("read artifact after generate"),
        LAST_KNOWN_GOOD,
        "generate writes source only and must not stage artifacts"
    );
    std::fs::write(
        &generated_file,
        format!("{original_generated}\n# local drift\n"),
    )
    .expect("introduce generated-source drift");

    let drifted = fixture
        .command("check")
        .output()
        .expect("run drifted check");
    assert_eq!(
        drifted.status.code(),
        Some(1),
        "drifted check had unexpected status:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&drifted.stdout),
        String::from_utf8_lossy(&drifted.stderr)
    );
    assert!(String::from_utf8_lossy(&drifted.stderr).contains("generated code is out of date"));
    assert_eq!(
        std::fs::read(&artifact).expect("read staged library after failed check"),
        LAST_KNOWN_GOOD,
        "a drifted check must leave artifacts untouched"
    );

    std::fs::write(&generated_file, original_generated).expect("restore generated source");
    let clean = fixture.command("check").output().expect("run clean check");
    assert!(
        clean.status.success(),
        "clean check failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&clean.stdout),
        String::from_utf8_lossy(&clean.stderr)
    );
    assert_eq!(
        std::fs::read(&artifact).expect("read native library after clean check"),
        LAST_KNOWN_GOOD,
        "a successful check must also leave artifacts untouched"
    );
}

#[test]
fn invalid_manifest_check_leaves_previous_library_untouched() {
    let fixture = Fixture::new();
    let generated = fixture.command("generate").output().expect("run generate");
    assert!(
        generated.status.success(),
        "generate failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&generated.stdout),
        String::from_utf8_lossy(&generated.stderr)
    );

    let artifact = fixture
        .root
        .join("python/src/fixture_package/generated/lib")
        .join(platform_filename("fixture-package"));
    const LAST_KNOWN_GOOD: &[u8] = b"previously-staged-native-library";
    std::fs::create_dir_all(artifact.parent().expect("artifact parent"))
        .expect("create artifact directory");
    std::fs::write(&artifact, LAST_KNOWN_GOOD).expect("seed prior staged library");

    let source_path = fixture.root.join("rust/src/lib.rs");
    let source = std::fs::read_to_string(&source_path).expect("read fixture Rust source");
    assert!(source.contains(r#""abi":"3.0""#));
    std::fs::write(
        &source_path,
        source.replace(r#""abi":"3.0""#, r#""abi":"3.1""#),
    )
    .expect("write unsupported manifest ABI");

    let invalid = fixture
        .command("check")
        .output()
        .expect("run invalid-manifest check");
    assert_eq!(
        invalid.status.code(),
        Some(3),
        "invalid-manifest check had unexpected status:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&invalid.stdout),
        String::from_utf8_lossy(&invalid.stderr)
    );
    assert!(String::from_utf8_lossy(&invalid.stderr).contains("unsupported manifest ABI `3.1`"));
    assert_eq!(
        std::fs::read(&artifact).expect("read staged library after invalid manifest"),
        LAST_KNOWN_GOOD,
        "an invalid manifest must leave the previous staged library untouched"
    );
}

#[test]
fn invalid_manifest_generate_and_manifest_leave_previous_library_untouched() {
    for subcommand in ["generate", "manifest"] {
        let fixture = Fixture::new();
        let generated = fixture.command("generate").output().expect("run generate");
        assert!(
            generated.status.success(),
            "initial generate failed for {subcommand} case:\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&generated.stdout),
            String::from_utf8_lossy(&generated.stderr)
        );

        let artifact = fixture
            .root
            .join("python/src/fixture_package/generated/lib")
            .join(platform_filename("fixture-package"));
        const LAST_KNOWN_GOOD: &[u8] = b"previously-staged-native-library";
        std::fs::create_dir_all(artifact.parent().expect("artifact parent"))
            .expect("create artifact directory");
        std::fs::write(&artifact, LAST_KNOWN_GOOD).expect("seed prior staged library");

        let source_path = fixture.root.join("rust/src/lib.rs");
        let source = std::fs::read_to_string(&source_path).expect("read fixture Rust source");
        assert!(source.contains(r#""abi":"3.0""#));
        std::fs::write(
            &source_path,
            source.replace(r#""abi":"3.0""#, r#""abi":"3.1""#),
        )
        .expect("write unsupported manifest ABI");

        let invalid = fixture
            .command(subcommand)
            .output()
            .unwrap_or_else(|error| panic!("run invalid-manifest {subcommand}: {error}"));
        assert_eq!(
            invalid.status.code(),
            Some(3),
            "invalid-manifest {subcommand} had unexpected status:\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&invalid.stdout),
            String::from_utf8_lossy(&invalid.stderr)
        );
        assert!(
            String::from_utf8_lossy(&invalid.stderr).contains("unsupported manifest ABI `3.1`")
        );
        assert_eq!(
            std::fs::read(&artifact).expect("read staged library after invalid manifest"),
            LAST_KNOWN_GOOD,
            "invalid-manifest {subcommand} must leave the previous staged library untouched"
        );
    }
}

fn platform_filename(name: &str) -> String {
    let stem = name.replace('-', "_");
    if cfg!(target_os = "windows") {
        format!("{stem}.dll")
    } else if cfg!(target_os = "macos") {
        format!("lib{stem}.dylib")
    } else {
        format!("lib{stem}.so")
    }
}

fn rustc_host() -> String {
    let output = Command::new("rustc")
        .arg("-vV")
        .output()
        .expect("run rustc -vV");
    assert!(output.status.success());
    String::from_utf8(output.stdout)
        .expect("rustc output is UTF-8")
        .lines()
        .find_map(|line| line.strip_prefix("host: "))
        .expect("rustc output includes host")
        .to_string()
}