ridl-cli 0.2.0

The `ridl` command-line toolchain: check, build, fmt, lock, diff, lsp, and mcp, over the shared compiler crates.
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
//! Integration tests for `ridl lock merge`, the git merge driver for
//! `interfaces.lock` (lock design §6): one test per row of the design's §6
//! table, each run in both directions (OURS and THEIRS swapped), asserting the
//! bytes written to OURS and the exit code; the conflict form and that
//! `ridl check` then reports RIDL-410; an empty BASE; an unreadable input; an
//! input that does not parse; and one end-to-end merge through git with the
//! driver registered as the CLI reference documents it.

use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};

/// A unique directory under the system temp dir, removed on drop.
struct TempDir(PathBuf);

impl TempDir {
    fn new(label: &str) -> Self {
        static COUNTER: AtomicUsize = AtomicUsize::new(0);
        let mut path = std::env::temp_dir();
        path.push(format!(
            "ridl-lock-merge-{label}-{}-{}",
            std::process::id(),
            COUNTER.fetch_add(1, Ordering::SeqCst),
        ));
        std::fs::create_dir_all(&path).expect("create the temp dir");
        Self(path)
    }

    fn path(&self) -> &Path {
        &self.0
    }

    fn write(&self, relative: &str, text: &str) -> PathBuf {
        let path = self.0.join(relative);
        std::fs::create_dir_all(path.parent().expect("a relative path has a parent"))
            .expect("create parent directories");
        std::fs::write(&path, text).expect("write the fixture file");
        path
    }
}

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

/// Runs `ridl` with `args` in `dir`, returning `(exit_code, stdout, stderr)`.
fn ridl_in(dir: &Path, args: &[&OsStr]) -> (i32, String, String) {
    let output = Command::new(env!("CARGO_BIN_EXE_ridl"))
        .current_dir(dir)
        .args(args)
        .output()
        .expect("the ridl binary must run");
    let code = output.status.code().expect("the process exits with a code");
    (
        code,
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
    )
}

const HEADER: &str = "# interfaces.lock — written by ridl lock; do not edit by hand.\n";

/// A lock file text: the header, `next N`, then `entries` (one line each,
/// each ending in `\n`).
fn lock(next: u32, entries: &str) -> String {
    format!("{HEADER}next {next}\n{entries}")
}

/// Runs `ridl lock merge BASE OURS THEIRS MARKER_SIZE` over the three texts,
/// returning the exit code, the bytes OURS holds afterwards, and stderr.
fn merge_with(base: &str, ours: &str, theirs: &str, marker_size: &str) -> (i32, String, String) {
    let dir = TempDir::new("merge");
    let base_path = dir.write("base.lock", base);
    let ours_path = dir.write("ours.lock", ours);
    let theirs_path = dir.write("theirs.lock", theirs);
    let (code, stdout, stderr) = ridl_in(
        dir.path(),
        &[
            OsStr::new("lock"),
            OsStr::new("merge"),
            base_path.as_os_str(),
            ours_path.as_os_str(),
            theirs_path.as_os_str(),
            OsStr::new(marker_size),
        ],
    );
    assert_eq!(stdout, "", "the driver prints nothing to stdout");
    let written = std::fs::read_to_string(&ours_path).expect("OURS stays readable");
    (code, written, stderr)
}

/// The driver with git's default marker size, 7.
fn merge(base: &str, ours: &str, theirs: &str) -> (i32, String, String) {
    merge_with(base, ours, theirs, "7")
}

/// The base of every §6 row but the last: `A 1, B 2`.
fn base_ab() -> String {
    lock(3, "A 1\nB 2\n")
}

/// §6 row 1, both directions: both sides allocated number 3; ours keeps the
/// number, theirs is renumbered to the next free one, and `next` follows.
#[test]
fn both_add_one_number_renumbers_theirs() {
    let ours = lock(4, "A 1\nB 2\nC 3\n");
    let theirs = lock(4, "A 1\nB 2\nD 3\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, lock(5, "A 1\nB 2\nC 3\nD 4\n"));

    let (code, written, stderr) = merge(&base_ab(), &theirs, &ours);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, lock(5, "A 1\nB 2\nD 3\nC 4\n"));
}

/// §6 row 2, both directions: each entry changed on one side only, so both
/// changes are taken.
#[test]
fn rename_plus_add_merges_clean() {
    let ours = lock(3, "A 1\nBee 2\n");
    let theirs = lock(4, "A 1\nB 2\nD 3\n");
    let expected = lock(4, "A 1\nBee 2\nD 3\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, expected);

    let (code, written, stderr) = merge(&base_ab(), &theirs, &ours);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, expected);
}

/// §6 row 3, both directions: entry 2 changed on both sides in two ways, so
/// it is a conflict; entry 1 is written plain.
#[test]
fn retire_versus_rename_conflicts_on_the_entry() {
    let ours = lock(3, "A 1\nB 2 retired\n");
    let theirs = lock(3, "A 1\nBee 2\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(
            3,
            "A 1\n<<<<<<< ours\nB 2 retired\n=======\nBee 2\n>>>>>>> theirs\n"
        )
    );

    let (code, written, stderr) = merge(&base_ab(), &theirs, &ours);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(
            3,
            "A 1\n<<<<<<< ours\nBee 2\n=======\nB 2 retired\n>>>>>>> theirs\n"
        )
    );
}

/// §6 row 4, both directions: two different renames of entry 2 conflict.
#[test]
fn two_different_renames_conflict() {
    let ours = lock(3, "A 1\nBee 2\n");
    let theirs = lock(3, "A 1\nBea 2\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(
            3,
            "A 1\n<<<<<<< ours\nBee 2\n=======\nBea 2\n>>>>>>> theirs\n"
        )
    );

    let (code, written, stderr) = merge(&base_ab(), &theirs, &ours);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(
            3,
            "A 1\n<<<<<<< ours\nBea 2\n=======\nBee 2\n>>>>>>> theirs\n"
        )
    );
}

/// §6 row 5, both directions: the same retire on both sides is kept once.
#[test]
fn the_same_change_on_both_sides_is_kept_once() {
    let side = lock(3, "A 1\nB 2 retired\n");

    let (code, written, stderr) = merge(&base_ab(), &side, &side);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, side);
}

/// §6 row 6, both directions: both sides retired `B 2` and both allocated
/// the re-added `B` at 3 the same way, so the lock merges clean. (The two
/// provisional `B` declarations behind it are the checker's TYPL-009, not
/// the driver's.)
#[test]
fn both_retire_and_both_re_add_merges_clean() {
    let side = lock(4, "A 1\nB 2 retired\nB 3\n");

    let (code, written, stderr) = merge(&base_ab(), &side, &side);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, side);
}

/// §6 row 7, both directions: a retire on one side and an addition on the
/// other touch different entries.
#[test]
fn retire_versus_add_merges_clean() {
    let ours = lock(3, "A 1\nB 2 retired\n");
    let theirs = lock(4, "A 1\nB 2\nD 3\n");
    let expected = lock(4, "A 1\nB 2 retired\nD 3\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, expected);

    let (code, written, stderr) = merge(&base_ab(), &theirs, &ours);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, expected);
}

/// §6 row 8, both directions: each side allocated `B` on its own number.
/// Every entry merges on its number, but the result would hold one live key
/// twice, so the two entries are a conflict, placed at the lower number.
#[test]
fn a_live_name_on_two_numbers_conflicts() {
    let base = lock(2, "A 1\n");
    let ours = lock(3, "A 1\nB 2\n");
    let theirs = lock(4, "A 1\nB 3\n");

    let (code, written, stderr) = merge(&base, &ours, &theirs);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(4, "A 1\n<<<<<<< ours\nB 2\n=======\nB 3\n>>>>>>> theirs\n")
    );

    let (code, written, stderr) = merge(&base, &theirs, &ours);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(4, "A 1\n<<<<<<< ours\nB 3\n=======\nB 2\n>>>>>>> theirs\n")
    );
}

/// §6 "on a conflict ... markers of MARKER_SIZE around only the disagreeing
/// entries": entries that agree, before and after the conflict, are written
/// plain, the markers are as long as MARKER_SIZE says, and the written file
/// is RIDL-410 under `ridl check` until an author resolves it.
#[test]
fn a_conflict_marks_only_the_disagreeing_entries_and_is_ridl_410_until_resolved() {
    let base = lock(4, "A 1\nB 2\nC 3\n");
    let ours = lock(5, "A 1\nB 2 retired\nC 3\nD 4\n");
    let theirs = lock(4, "A 1\nBee 2\nC 3\n");

    let (code, written, stderr) = merge_with(&base, &ours, &theirs, "3");
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert_eq!(
        written,
        lock(
            5,
            "A 1\n<<< ours\nB 2 retired\n===\nBee 2\n>>> theirs\nC 3\nD 4\n"
        )
    );
    assert!(
        stderr.contains("conflict markers") && stderr.contains("RIDL-410"),
        "stderr:\n{stderr}"
    );

    let dir = TempDir::new("conflicted-package");
    dir.write(
        "ridl.toml",
        "[package]\nname = \"veh.hvac\"\nversion = \"1.0.0\"\n",
    );
    dir.write(
        "hvac.ridl",
        "package veh.hvac\ntype State: integer [0..1]\ninterface A { signal a : State @[100ms..1s] }\n",
    );
    dir.write("interfaces.lock", &written);
    let (code, _, stderr) = ridl_in(dir.path(), &[OsStr::new("check"), OsStr::new(".")]);
    assert_eq!(code, 1, "stderr:\n{stderr}");
    assert!(stderr.contains("error[RIDL-410]"), "stderr:\n{stderr}");
}

/// §6: an empty BASE — what git passes as `%O` when both sides created the
/// file — reads as `next 1` with no entries, so both sides' entries are
/// additions.
#[test]
fn an_empty_base_reads_as_next_one() {
    let ours = lock(2, "A 1\n");
    let theirs = lock(2, "B 1\n");

    let (code, written, stderr) = merge("", &ours, &theirs);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, lock(3, "A 1\nB 2\n"));

    let (code, written, stderr) = merge("", &theirs, &ours);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, lock(3, "B 1\nA 2\n"));
}

/// §6: `next` is the maximum of the three sides — a side whose `next` was
/// raised by hand keeps it — plus any renumbering, which allocates from that
/// maximum upward.
#[test]
fn next_is_the_maximum_of_the_three_sides_plus_renumbering() {
    let ours = lock(9, "A 1\nB 2\nC 3\n");
    let theirs = lock(4, "A 1\nB 2\nD 3\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, lock(10, "A 1\nB 2\nC 3\nD 9\n"));

    let (code, written, stderr) = merge(&base_ab(), &theirs, &ours);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(written, lock(10, "A 1\nB 2\nD 3\nC 9\n"));
}

/// §6: an input that cannot be read is exit 2, and OURS is left as it was.
#[test]
fn an_unreadable_input_exits_two() {
    let dir = TempDir::new("unreadable");
    let base_path = dir.write("base.lock", &base_ab());
    let ours = lock(4, "A 1\nB 2\nC 3\n");
    let ours_path = dir.write("ours.lock", &ours);
    let missing = dir.path().join("theirs.lock");

    let (code, stdout, stderr) = ridl_in(
        dir.path(),
        &[
            OsStr::new("lock"),
            OsStr::new("merge"),
            base_path.as_os_str(),
            ours_path.as_os_str(),
            missing.as_os_str(),
            OsStr::new("7"),
        ],
    );
    assert_eq!(code, 2, "stderr:\n{stderr}");
    assert_eq!(stdout, "");
    assert!(
        stderr.contains("error: cannot read") && stderr.contains("theirs.lock"),
        "stderr:\n{stderr}"
    );
    assert_eq!(
        std::fs::read_to_string(&ours_path).expect("OURS is readable"),
        ours
    );
}

/// A readable input that does not parse is exit 2 too, naming the file and
/// the line, and OURS is left as it was: the driver merges tables, and a side
/// that is not a table is an input it cannot answer over.
#[test]
fn an_input_that_does_not_parse_exits_two_and_leaves_ours_unchanged() {
    let ours = lock(4, "A 1\nB 2\nC 3\n");
    let theirs = format!("{HEADER}next 2\nA 1\nB 2\n");

    let (code, written, stderr) = merge(&base_ab(), &ours, &theirs);
    assert_eq!(code, 2, "stderr:\n{stderr}");
    assert_eq!(written, ours);
    assert!(
        stderr
            .contains("theirs.lock:4: `next` is 2, which is not greater than the number 2 of `B`"),
        "stderr:\n{stderr}"
    );
}

/// A package directory literally named `merge` is a path, not the
/// subcommand, when it is spelled `./merge`.
#[test]
fn a_package_directory_named_merge_is_a_path_when_spelled_with_a_dot() {
    let dir = TempDir::new("named-merge");
    dir.write(
        "merge/ridl.toml",
        "[package]\nname = \"veh.hvac\"\nversion = \"1.0.0\"\n",
    );
    dir.write(
        "merge/hvac.ridl",
        "package veh.hvac\ntype State: integer [0..1]\ninterface Cabin { signal c : State @[100ms..1s] }\n",
    );

    let (code, stdout, stderr) = ridl_in(dir.path(), &[OsStr::new("lock"), OsStr::new("./merge")]);
    assert_eq!(code, 0, "stderr:\n{stderr}");
    assert_eq!(stdout, "allocated Cabin 1\n");
    assert_eq!(
        std::fs::read_to_string(dir.path().join("merge/interfaces.lock")).expect("written"),
        lock(2, "Cabin 1\n")
    );
}

/// Runs `git` with `args` in `dir`; panics with the output when it fails.
///
/// A git hook runs with `GIT_DIR`, `GIT_WORK_TREE` and `GIT_INDEX_FILE` in its
/// environment, and each of those outranks the child process's working
/// directory. This repository's pre-push hook runs the test suite, so the
/// inherited git environment is removed here: without that, this helper would
/// drive the repository being pushed from instead of the temporary one.
fn git(dir: &Path, args: &[&str]) -> String {
    let mut command = Command::new("git");
    command
        .current_dir(dir)
        .env("GIT_CONFIG_GLOBAL", "/dev/null")
        .env("GIT_CONFIG_SYSTEM", "/dev/null")
        .env("GIT_EDITOR", "true");
    for inherited in [
        "GIT_DIR",
        "GIT_WORK_TREE",
        "GIT_INDEX_FILE",
        "GIT_OBJECT_DIRECTORY",
        "GIT_ALTERNATE_OBJECT_DIRECTORIES",
        "GIT_COMMON_DIR",
        "GIT_NAMESPACE",
        "GIT_PREFIX",
    ] {
        command.env_remove(inherited);
    }
    let output = command.args(args).output().expect("git runs");
    assert!(
        output.status.success(),
        "git {} failed:\nstdout:\n{}\nstderr:\n{}",
        args.join(" "),
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).into_owned()
}

/// The end-to-end row: a repository registers the driver exactly as the CLI
/// reference documents it — `.gitattributes` with `interfaces.lock
/// merge=ridl-lock`, and `git config merge.ridl-lock.driver` — and merges the
/// two branches of §6 row 1; git runs the driver and the merged file is the
/// row's result. Skipped, with the reason printed, when `git` is not on PATH.
#[test]
fn the_registered_driver_merges_two_branches_through_git() {
    if Command::new("git").arg("--version").output().is_err() {
        eprintln!("skipped: `git` is not on PATH");
        return;
    }
    let dir = TempDir::new("git");
    let repo = dir.path();
    git(repo, &["init", "--quiet", "--initial-branch=main"]);
    // Every later command must reach this repository and no other. The check
    // runs before the first commit, so a git environment that outranked the
    // working directory would stop the test here rather than write into the
    // repository the test suite runs in.
    let reached = git(repo, &["rev-parse", "--absolute-git-dir"]);
    assert_eq!(
        std::fs::canonicalize(reached.trim()).expect("the git directory git reached exists"),
        std::fs::canonicalize(repo.join(".git")).expect("the temporary git directory exists"),
        "git must reach the temporary repository"
    );
    git(repo, &["config", "user.name", "ridl tests"]);
    git(
        repo,
        &["config", "user.email", "ridl-tests@example.invalid"],
    );
    git(repo, &["config", "commit.gpgsign", "false"]);
    git(
        repo,
        &[
            "config",
            "merge.ridl-lock.driver",
            &format!("\"{}\" lock merge %O %A %B %L", env!("CARGO_BIN_EXE_ridl")),
        ],
    );
    dir.write(".gitattributes", "interfaces.lock merge=ridl-lock\n");
    dir.write("interfaces.lock", &base_ab());
    git(repo, &["add", "."]);
    git(repo, &["commit", "--quiet", "-m", "base"]);

    git(repo, &["checkout", "--quiet", "-b", "ours"]);
    dir.write("interfaces.lock", &lock(4, "A 1\nB 2\nC 3\n"));
    git(repo, &["commit", "--quiet", "-am", "ours adds C"]);

    git(repo, &["checkout", "--quiet", "-b", "theirs", "main"]);
    dir.write("interfaces.lock", &lock(4, "A 1\nB 2\nD 3\n"));
    git(repo, &["commit", "--quiet", "-am", "theirs adds D"]);

    git(repo, &["checkout", "--quiet", "ours"]);
    git(repo, &["merge", "--quiet", "--no-edit", "theirs"]);

    assert_eq!(
        std::fs::read_to_string(repo.join("interfaces.lock")).expect("the merged file"),
        lock(5, "A 1\nB 2\nC 3\nD 4\n")
    );
    assert_eq!(
        git(repo, &["status", "--porcelain"]),
        "",
        "the merge committed cleanly"
    );
}