rossi-cli 0.1.9

Command-line interface for the Rossi Event-B toolchain
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! `rossi build`: diagnostics, output writing, and output-path containment.

use crate::helpers::{
    ASCII_CONTEXT, BuildFixture, DUP_VARIABLE_MACHINE, dir_has_ext, extract_zip_to,
    rewrite_zip_entry, rossi_command, tempdir_unique, zip_entry_bytes, zip_entry_names,
};

#[test]
fn build_duplicate_component_names_fail_with_eb019() {
    // `rossi build` must fail the same project `validate` fails: the EB019
    // diagnostic, exit 1, and no output written — not a zip-writer IO error
    // about colliding entry names.
    let tmp = tempdir_unique("rossi-cli-build-dup-names");
    std::fs::write(tmp.join("a.eventb"), "MACHINE M\nEND\n").unwrap();
    std::fs::write(tmp.join("b.eventb"), "MACHINE M\nEND\n").unwrap();
    let out_zip = tmp.join("out.zip");

    let output = rossi_command()
        .args([
            "build",
            tmp.to_str().unwrap(),
            "--output",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "duplicate component names must fail the build; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("[EB019]"), "stderr: {stderr}");
    assert!(
        !stderr.contains("Duplicate filename"),
        "the zip-writer error must be unreachable: {stderr}"
    );
    assert!(!out_zip.exists(), "no output may be written");

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_fails_on_error_diagnostics_but_still_writes_output() {
    // An error diagnostic that still leaves checked output (here EB006: a
    // constant with no typing axiom) must fail the build, while the filtered
    // output is written all the same — matching Rodin, which drops the
    // erroneous element and still produces the checked file.
    let tmp = tempdir_unique("rossi-cli-build-error-diag");
    let src = tmp.join("c.eventb");
    std::fs::write(
        &src,
        "CONTEXT c\nCONSTANTS\n    x\nAXIOMS\n    @axm1 1 = 1\nEND\n",
    )
    .unwrap();
    let out_zip = tmp.join("out.zip");

    let output = rossi_command()
        .args([
            "build",
            src.to_str().unwrap(),
            "-o",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "error diagnostics must fail the build; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("[EB006]"), "stderr: {stderr}");
    assert!(stderr.contains("error diagnostic"), "stderr: {stderr}");
    assert!(out_zip.exists(), "filtered output must still be written");
    let extracted = tmp.join("extracted");
    std::fs::create_dir_all(&extracted).unwrap();
    extract_zip_to(&out_zip, &extracted);
    assert!(
        dir_has_ext(&extracted, &["bcc"]),
        "expected the checked output (.bcc) despite the error"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_fails_on_circular_refines_with_context_sibling() {
    // Regression: a REFINES cycle beside a healthy context used to exit 0
    // because the context's checked file made the project look successful.
    // The cycle's EB008 error must fail the build while the context's output
    // is still written.
    let tmp = tempdir_unique("rossi-cli-build-refines-cycle");
    let src = tmp.join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("m.eventb"), "MACHINE m\nREFINES m\nEND\n").unwrap();
    std::fs::write(src.join("c.eventb"), ASCII_CONTEXT).unwrap();
    let out_zip = tmp.join("out.zip");

    let output = rossi_command()
        .args([
            "build",
            src.to_str().unwrap(),
            "-o",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "a dependency cycle must fail the build; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("[EB008]"), "stderr: {stderr}");
    assert!(
        out_zip.exists(),
        "the context's output must still be written"
    );
    let extracted = tmp.join("extracted");
    std::fs::create_dir_all(&extracted).unwrap();
    extract_zip_to(&out_zip, &extracted);
    assert!(
        dir_has_ext(&extracted, &["bcc"]),
        "expected the sibling context's .bcc in the built zip"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_fails_on_duplicate_identifier() {
    let tmp = tempdir_unique("rossi-cli-build-dup-var");
    std::fs::write(tmp.join("M.eventb"), DUP_VARIABLE_MACHINE).unwrap();
    let out_zip = tmp.join("out.zip");

    let output = rossi_command()
        .args([
            "build",
            tmp.to_str().unwrap(),
            "-o",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "a duplicate identifier must fail the build; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("[EB021]"), "stderr: {stderr}");
    assert!(
        out_zip.exists(),
        "the filtered output is still written despite the error"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[cfg(unix)]
fn symlink_dir(target: &std::path::Path, link: &std::path::Path) {
    std::fs::create_dir_all(target).unwrap();
    std::fs::create_dir_all(link.parent().unwrap()).unwrap();
    std::os::unix::fs::symlink(target, link).unwrap();
}

#[test]
fn build_directory_output_rejects_unsafe_prefixes_before_writing() {
    let cases = [
        ("parent-prefix", ["../C.buc", "safe/D.buc"]),
        ("rooted-prefix", ["/C.buc", "safe/D.buc"]),
        ("sanitized-collision", ["../C.buc", "./D.buc"]),
    ];

    for (case, entries) in cases {
        let fixture = BuildFixture::new(&entries, "out");
        let output = fixture.run();

        assert!(!output.status.success(), "{case} should be rejected");
        assert!(
            String::from_utf8_lossy(&output.stderr).contains("unsafe archive prefix"),
            "{case}: stderr={}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(
            !fixture.root.join("C.bcc").exists(),
            "{case} escaped output root"
        );
        assert!(
            !fixture.output.exists(),
            "{case} preflight failure must not create the output root"
        );
    }
}

#[test]
fn build_directory_output_preserves_safe_multi_project_layout() {
    let fixture = BuildFixture::new(&["A/C.buc", "B/D.buc"], "out");
    fixture.assert_success("safe multi-project build");
    assert!(fixture.output.join("A/C.bcc").exists());
    assert!(fixture.output.join("B/D.bcc").exists());
}

#[test]
fn build_zip_output_preserves_raw_archive_prefixes() {
    let fixture = BuildFixture::new(&["../C.buc", "safe/D.buc"], "out.zip");
    fixture.assert_success("archive repacking");
    let mut archive = zip::ZipArchive::new(std::fs::File::open(&fixture.output).unwrap()).unwrap();
    assert!(archive.by_name("../C.bcc").is_ok());
    assert!(archive.by_name("safe/D.bcc").is_ok());
}

#[cfg(unix)]
#[test]
fn build_directory_output_rejects_escaping_project_symlink() {
    let fixture = BuildFixture::new(&["evil/C.buc", "safe/D.buc"], "out");
    let outside = fixture.root.join("outside");
    symlink_dir(&outside, &fixture.output.join("evil"));
    let output = fixture.run();

    assert!(
        !output.status.success(),
        "escaping symlink should be rejected"
    );
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("escapes output directory"),
        "stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(!outside.join("C.bcc").exists());
    assert!(
        !fixture.output.join("safe/D.bcc").exists(),
        "preflight must reject before writing a safe sibling"
    );
}

#[cfg(unix)]
#[test]
fn build_directory_output_allows_contained_project_symlink() {
    let fixture = BuildFixture::new(&["linked/C.buc", "safe/D.buc"], "out");
    let actual = fixture.output.join("actual");
    symlink_dir(&actual, &fixture.output.join("linked"));
    fixture.assert_success("contained symlink");
    assert!(actual.join("C.bcc").exists());
    assert!(fixture.output.join("safe/D.bcc").exists());
}

#[cfg(unix)]
#[test]
fn build_directory_output_allows_symlinked_root() {
    let fixture = BuildFixture::new(&["A/C.buc", "B/D.buc"], "out");
    let actual = fixture.root.join("actual");
    symlink_dir(&actual, &fixture.output);
    fixture.assert_success("symlinked root");
    assert!(actual.join("A/C.bcc").exists());
    assert!(actual.join("B/D.bcc").exists());
}

#[test]
fn build_eventb_file_packs_sources_and_checked() {
    let tmp = tempdir_unique("rossi-cli-build-eventb");
    let out_zip = tmp.join("out.zip");

    let output = rossi_command()
        .args([
            "build",
            "../rossi/examples/counter.eventb",
            "-o",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "build from text should exit 0; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(out_zip.exists());

    let extracted = tmp.join("extracted");
    std::fs::create_dir_all(&extracted).unwrap();
    extract_zip_to(&out_zip, &extracted);
    // The output must carry both the component source and our checked file,
    // just like the old export-then-build round-trip did.
    assert!(
        dir_has_ext(&extracted, &["buc", "bum"]),
        "expected the component source (.buc/.bum) in the built zip"
    );
    assert!(
        dir_has_ext(&extracted, &["bcc", "bcm"]),
        "expected the checked output (.bcc/.bcm) in the built zip"
    );
    // The text door stamps a `.project` descriptor, so the built zip imports
    // into Rodin (and back through `rossi import`) under its real name.
    assert!(
        extracted.join(".project").is_file(),
        "expected the .project descriptor in the built zip"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_eventb_directory() {
    let tmp = tempdir_unique("rossi-cli-build-dir");
    let src = tmp.join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(src.join("c.eventb"), ASCII_CONTEXT).unwrap();
    let out_zip = tmp.join("out.zip");

    let output = rossi_command()
        .args([
            "build",
            src.to_str().unwrap(),
            "-o",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "build from a text directory should exit 0; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );

    let extracted = tmp.join("extracted");
    std::fs::create_dir_all(&extracted).unwrap();
    extract_zip_to(&out_zip, &extracted);
    assert!(
        dir_has_ext(&extracted, &["bcc", "bcm"]),
        "expected the checked output (.bcc/.bcm) in the built zip"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

const TRAFFIC_LIGHT: &str = "../rossi/examples/traffic-light.zip";

fn build_zip(input: &std::path::Path, out_zip: &std::path::Path) {
    let output = rossi_command()
        .args([
            "build",
            input.to_str().unwrap(),
            "--output",
            out_zip.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");
    assert!(
        output.status.success(),
        "stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn build_zip_preserves_proof_files() {
    // A rebuilt archive carries our generated .bpo (obligations) and
    // .bps (fresh unattempted statuses, as the input has none to carry)
    // while the input's .bpr proofs are preserved byte-exact.
    let tmp = tempdir_unique("rossi-cli-build-proofs");
    let out_zip = tmp.join("out.zip");
    build_zip(std::path::Path::new(TRAFFIC_LIGHT), &out_zip);

    let extracted = tmp.join("out");
    extract_zip_to(&out_zip, &extracted);
    let root = extracted.join("traffic-light");
    let m0_bpo = std::fs::read_to_string(root.join("M0.bpo")).expect("M0.bpo");
    assert!(m0_bpo.contains(r#"<org.eventb.core.poSequent name="INITIALISATION/inv3/INV""#));
    let m0_bps = std::fs::read_to_string(root.join("M0.bps")).expect("M0.bps");
    assert!(m0_bps.contains(r#"<org.eventb.core.psStatus name="INITIALISATION/inv3/INV" org.eventb.core.confidence="-99" org.eventb.core.poStamp="0" org.eventb.core.psManual="false"/>"#));
    assert_eq!(
        std::fs::read(root.join("M0.bpr")).expect("M0.bpr"),
        zip_entry_bytes(std::path::Path::new(TRAFFIC_LIGHT), "traffic-light/M0.bpr"),
        "proofs must be preserved byte-exact"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_zip_rebuild_is_byte_stable() {
    // Rebuilding an unchanged archive must be a no-op on proof state:
    // every .bpo / .bps / .bpr entry comes out byte-identical.
    let tmp = tempdir_unique("rossi-cli-build-stable");
    let out1 = tmp.join("out1.zip");
    let out2 = tmp.join("out2.zip");
    build_zip(std::path::Path::new(TRAFFIC_LIGHT), &out1);
    build_zip(&out1, &out2);

    let proof_entries: Vec<String> = zip_entry_names(&out1)
        .into_iter()
        .filter(|n| n.ends_with(".bpo") || n.ends_with(".bps") || n.ends_with(".bpr"))
        .collect();
    assert!(!proof_entries.is_empty());
    for name in &proof_entries {
        assert_eq!(
            zip_entry_bytes(&out1, name),
            zip_entry_bytes(&out2, name),
            "{name} must survive a rebuild unchanged"
        );
    }

    std::fs::remove_dir_all(&tmp).ok();
}

/// The doctored `INITIALISATION/inv3/INV` status row the carry tests
/// look for after a rebuild.
const DISCHARGED_INV3: &str = r#"name="INITIALISATION/inv3/INV" org.eventb.core.confidence="1000" org.eventb.core.poStamp="0" org.eventb.core.psManual="true""#;

/// Mark the `INITIALISATION/inv3/INV` status row manually discharged,
/// asserting the edit applied — a drifted needle would otherwise let
/// the carry-forward assertions pass vacuously.
fn discharge_inv3(text: &str) -> String {
    let out = text.replacen(
        r#"name="INITIALISATION/inv3/INV" org.eventb.core.confidence="-99" org.eventb.core.poStamp="0" org.eventb.core.psManual="false""#,
        DISCHARGED_INV3,
        1,
    );
    assert_ne!(out, text, "the fresh inv3 status row must be present");
    out
}

#[test]
fn build_zip_carries_doctored_statuses_across_rebuilds() {
    // A proof status recorded between builds (here: one obligation
    // discharged manually) must survive the next rebuild verbatim.
    let tmp = tempdir_unique("rossi-cli-build-status-carry");
    let out1 = tmp.join("out1.zip");
    let doctored = tmp.join("doctored.zip");
    let out2 = tmp.join("out2.zip");
    build_zip(std::path::Path::new(TRAFFIC_LIGHT), &out1);

    rewrite_zip_entry(&out1, &doctored, "traffic-light/M0.bps", discharge_inv3);
    build_zip(&doctored, &out2);

    let m0_bps = String::from_utf8(zip_entry_bytes(&out2, "traffic-light/M0.bps")).unwrap();
    assert!(
        m0_bps.contains(DISCHARGED_INV3),
        "the discharged row must carry over verbatim: {m0_bps}"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_directory_output_preserves_proof_state() {
    // Loose-file output reconciles against the destination directory:
    // a second build leaves .bpo/.bps byte-identical, a status doctored
    // in between carries over, and .bpr files on disk are never touched.
    let tmp = tempdir_unique("rossi-cli-build-dir-proofs");
    let src = tmp.join("src");
    extract_zip_to(&std::path::PathBuf::from(TRAFFIC_LIGHT), &src);
    let project = src.join("traffic-light");
    let out_dir = tmp.join("out");

    let run = || {
        let output = rossi_command()
            .args([
                "build",
                project.to_str().unwrap(),
                "--output",
                out_dir.to_str().unwrap(),
            ])
            .output()
            .expect("Failed to execute command");
        assert!(
            output.status.success(),
            "stderr={}",
            String::from_utf8_lossy(&output.stderr)
        );
    };

    run();
    let stray = out_dir.join("STRAY.bpr");
    std::fs::write(&stray, "STRAY-PROOF").unwrap();
    let bpo_first = std::fs::read(out_dir.join("M0.bpo")).unwrap();
    let bps_first = std::fs::read(out_dir.join("M0.bps")).unwrap();

    run();
    assert_eq!(std::fs::read(out_dir.join("M0.bpo")).unwrap(), bpo_first);
    assert_eq!(std::fs::read(out_dir.join("M0.bps")).unwrap(), bps_first);
    assert_eq!(std::fs::read(&stray).unwrap(), b"STRAY-PROOF");

    // Discharge one obligation in place; the next build keeps it.
    let doctored = discharge_inv3(&String::from_utf8(bps_first).unwrap());
    std::fs::write(out_dir.join("M0.bps"), &doctored).unwrap();
    run();
    assert_eq!(
        std::fs::read_to_string(out_dir.join("M0.bps")).unwrap(),
        doctored,
        "the discharged row must carry over verbatim"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_ignores_dot_directories_when_classifying_input() {
    // A `.rossi/rodin` workspace (or any dot-directory) inside a source tree
    // holds generated Rodin XML; if the walker saw its `.bum` files, the whole
    // directory would be misread as a Rodin project. The hidden `.bum` here is
    // deliberately invalid so taking that path fails loudly.
    let tmp = tempdir_unique("rossi-cli-build-dot-dirs");
    std::fs::write(tmp.join("main_ctx.eventb"), "CONTEXT main_ctx\nEND\n").unwrap();
    let hidden = tmp.join(".rossi").join("rodin").join("proj");
    std::fs::create_dir_all(&hidden).unwrap();
    std::fs::write(hidden.join("Bogus.bum"), "not xml").unwrap();
    std::fs::write(
        hidden.join("hidden_ctx.eventb"),
        "CONTEXT hidden_ctx\nEND\n",
    )
    .unwrap();
    let out_zip = tmp.join("out.zip");
    build_zip(&tmp, &out_zip);

    let names = zip_entry_names(&out_zip);
    assert!(
        names.iter().any(|n| n.contains("main_ctx")),
        "zip entries: {names:?}"
    );
    assert!(
        names
            .iter()
            .all(|n| !n.contains("hidden_ctx") && !n.contains("Bogus")),
        "dot-directory contents must not be built: {names:?}"
    );

    std::fs::remove_dir_all(&tmp).ok();
}

#[test]
fn build_directory_to_zip_carries_proofs_and_statuses() {
    // Directory input with .zip output: the source dir's .bpr proofs
    // land in the archive byte-exact and its .bpo/.bps reconcile in.
    let tmp = tempdir_unique("rossi-cli-build-dir-zip-proofs");
    let src = tmp.join("src");
    extract_zip_to(&std::path::PathBuf::from(TRAFFIC_LIGHT), &src);
    let project = src.join("traffic-light");
    let out_zip = tmp.join("out.zip");
    build_zip(&project, &out_zip);

    assert_eq!(
        zip_entry_bytes(&out_zip, "traffic-light/M0.bpr"),
        std::fs::read(project.join("M0.bpr")).unwrap(),
        "the source dir's proofs must land in the archive byte-exact"
    );
    assert!(
        zip_entry_names(&out_zip)
            .iter()
            .any(|n| n == "traffic-light/M0.bpo"),
        "generated obligations must be present"
    );

    std::fs::remove_dir_all(&tmp).ok();
}