bender 0.32.1

A dependency management tool for hardware projects.
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
// Copyright (c) 2025 ETH Zurich
// Tim Fischer <fischeti@iis.ee.ethz.ch>

#[cfg(feature = "slang")]
mod tests {
    use std::collections::HashSet;

    use assert_cmd::cargo;

    fn run_script(args: &[&str]) -> String {
        let mut full_args = vec!["-d", "tests/pickle", "script"];
        full_args.extend(args);

        let out = cargo::cargo_bin_cmd!()
            .args(&full_args)
            .output()
            .expect("Failed to execute bender binary");

        assert!(
            out.status.success(),
            "script command failed.\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        );

        String::from_utf8(out.stdout).expect("stdout must be utf-8")
    }

    /// Run the script subcommand expecting failure; returns the captured stderr.
    fn run_script_failing(args: &[&str]) -> String {
        let mut full_args = vec!["-d", "tests/pickle", "script"];
        full_args.extend(args);

        let out = cargo::cargo_bin_cmd!()
            .args(&full_args)
            .output()
            .expect("Failed to execute bender binary");

        assert!(
            !out.status.success(),
            "script command unexpectedly succeeded.\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        );

        String::from_utf8(out.stderr).expect("stderr must be utf-8")
    }

    /// Return the path component after the last `/` or `\`. On Windows, bender's source paths
    /// can come out mixed (e.g. `D:\workspace\tests\pickle/src/top.sv`) while incdir paths are
    /// all-backslash because the Bender.yml entry has no embedded separator — so splitting on
    /// `/` alone misses the latter.
    fn basename(path: &str) -> &str {
        match path.rfind(['/', '\\']) {
            Some(i) => &path[i + 1..],
            None => path,
        }
    }

    /// Extract the set of source-file basenames from a flist-plus output.
    /// Filters out `+incdir+` / `+define+` lines so only path lines remain.
    fn source_basenames(output: &str) -> HashSet<&str> {
        output
            .lines()
            .map(str::trim)
            .filter(|l| !l.is_empty() && !l.starts_with("+incdir+") && !l.starts_with("+define+"))
            .map(basename)
            .collect()
    }

    /// Extract the basenames of `+incdir+` directories from a flist-plus output.
    fn incdir_basenames(output: &str) -> HashSet<&str> {
        output
            .lines()
            .map(str::trim)
            .filter_map(|l| l.strip_prefix("+incdir+"))
            .map(basename)
            .collect()
    }

    #[test]
    fn script_top_filters_unreachable_files() {
        // Without --top: all files present
        let full_out = run_script(&["--target", "top", "flist-plus"]);
        let full = source_basenames(&full_out);
        assert!(full.contains("unused_top.sv"));
        assert!(full.contains("unused_leaf.sv"));

        // With --top top: unreachable files removed
        let trimmed_out = run_script(&["--target", "top", "--top", "top", "flist-plus"]);
        let trimmed = source_basenames(&trimmed_out);
        assert!(trimmed.contains("top.sv"));
        assert!(trimmed.contains("core.sv"));
        assert!(trimmed.contains("leaf.sv"));
        assert!(!trimmed.contains("unused_top.sv"));
        assert!(!trimmed.contains("unused_leaf.sv"));
    }

    #[test]
    fn script_top_multiple_tops() {
        let out = run_script(&[
            "--target",
            "top",
            "--top",
            "top",
            "--top",
            "unused_top",
            "flist-plus",
        ]);
        let trimmed = source_basenames(&out);
        assert!(trimmed.contains("top.sv"));
        assert!(trimmed.contains("unused_top.sv"));
    }

    #[test]
    fn script_top_empty_keeps_all_files() {
        // Without --top: all files appear
        let out = run_script(&["--target", "top", "flist-plus"]);
        let full = source_basenames(&out);
        assert!(full.contains("top.sv"));
        assert!(full.contains("core.sv"));
        assert!(full.contains("leaf.sv"));
        assert!(full.contains("unused_top.sv"));
        assert!(full.contains("unused_leaf.sv"));
    }

    /// Default (`--trim-incdirs auto`) trims include dirs iff `--top` is set.
    /// `include/` is used by top.sv (`include "macros.svh"`); `include_unused/` is declared in
    /// the Bender.yml but never resolved through.
    #[test]
    fn script_trim_incdirs_auto() {
        // No --top: both dirs survive.
        let full_out = run_script(&["--target", "top", "flist-plus"]);
        let full_incs = incdir_basenames(&full_out);
        assert!(full_incs.contains("include"));
        assert!(full_incs.contains("include_unused"));

        // With --top: include_unused is dropped, include survives.
        let trimmed_out = run_script(&["--target", "top", "--top", "top", "flist-plus"]);
        let trimmed_incs = incdir_basenames(&trimmed_out);
        assert!(
            trimmed_incs.contains("include"),
            "include/ should survive: {trimmed_incs:?}"
        );
        assert!(
            !trimmed_incs.contains("include_unused"),
            "include_unused/ should be dropped: {trimmed_incs:?}"
        );
    }

    /// `--trim-incdirs always` prunes unused incdirs even without `--top`.
    #[test]
    fn script_trim_incdirs_always_without_top() {
        let out = run_script(&["--target", "top", "--trim-incdirs", "always", "flist-plus"]);
        let incs = incdir_basenames(&out);
        assert!(incs.contains("include"));
        assert!(
            !incs.contains("include_unused"),
            "include_unused/ should be dropped: {incs:?}"
        );

        // File list is untouched — no --top means no reachability filter.
        let files = source_basenames(&out);
        assert!(files.contains("top.sv"));
        assert!(files.contains("unused_top.sv"));
        assert!(files.contains("unused_leaf.sv"));
    }

    /// `--trim-incdirs never` keeps all incdirs even with `--top`.
    #[test]
    fn script_trim_incdirs_never_with_top() {
        let out = run_script(&[
            "--target",
            "top",
            "--top",
            "top",
            "--trim-incdirs",
            "never",
            "flist-plus",
        ]);
        let incs = incdir_basenames(&out);
        assert!(incs.contains("include"));
        assert!(
            incs.contains("include_unused"),
            "include_unused/ should be retained with --trim-incdirs never: {incs:?}"
        );

        // File filtering still happens — unreachable files are still dropped.
        let files = source_basenames(&out);
        assert!(files.contains("top.sv"));
        assert!(!files.contains("unused_top.sv"));
    }

    /// Encrypted RTL (IEEE-1735 protect envelopes) makes slang trip at the surrounding
    /// `endmodule` even though the envelope itself is skipped. The filter must:
    ///  * not abort `bender script --top` because of slang errors in encrypted IP, and
    ///  * preserve the encrypted file in the output even though no internal reference resolves
    ///    to it (its module symbol is hidden behind the protect envelope).
    #[test]
    fn script_top_keeps_encrypted_file() {
        let out = run_script(&[
            "--target",
            "encrypted",
            "--top",
            "encrypted_top",
            "flist-plus",
        ]);
        let files = source_basenames(&out);
        assert!(
            files.contains("encrypted_top.sv"),
            "top file missing: {files:?}"
        );
        assert!(
            files.contains("encrypted_user.sv"),
            "user of encrypted IP missing: {files:?}"
        );
        assert!(
            files.contains("encrypted_ip.sv"),
            "encrypted IP must be force-kept despite parse errors: {files:?}"
        );
    }

    /// `--encrypted drop` removes encrypted files from the output even when `--top` keeps
    /// the rest of the reachable design.
    #[test]
    fn script_encrypted_drop_excludes_encrypted_file() {
        let out = run_script(&[
            "--target",
            "encrypted",
            "--top",
            "encrypted_top",
            "--encrypted",
            "drop",
            "flist-plus",
        ]);
        let files = source_basenames(&out);
        assert!(files.contains("encrypted_top.sv"));
        assert!(files.contains("encrypted_user.sv"));
        assert!(
            !files.contains("encrypted_ip.sv"),
            "encrypted IP should be dropped by --encrypted drop: {files:?}"
        );
    }

    /// `--encrypted error` makes the run abort when any encrypted file is encountered — useful
    /// as a CI lint for codebases that should be encryption-free.
    #[test]
    fn script_encrypted_error_aborts() {
        let stderr = run_script_failing(&[
            "--target",
            "encrypted",
            "--encrypted",
            "error",
            "flist-plus",
        ]);
        assert!(
            stderr.contains("encrypted file(s)") && stderr.contains("--encrypted error"),
            "expected encrypted-lint abort message, got stderr:\n{stderr}"
        );
    }

    /// `--source-annotations` adds a `// UNPARSEABLE: ...` comment above the kept encrypted file
    /// so users reading the script can tell which entries slang couldn't analyze.
    #[test]
    fn script_source_annotations_marks_unparseable() {
        let out = run_script(&[
            "--target",
            "encrypted",
            "--top",
            "encrypted_top",
            "--source-annotations",
            "flist-plus",
        ]);
        assert!(
            out.contains("// UNPARSEABLE: slang reported parse errors"),
            "expected UNPARSEABLE annotation in output:\n{out}"
        );
    }

    /// A file with a real syntax error (no `pragma protect` envelope) should abort the run by
    /// default — `--broken` defaults to `error`, while encrypted IP is auto-tolerated.
    #[test]
    fn script_broken_file_fails_by_default() {
        let stderr = run_script_failing(&["--target", "broken", "--top", "broken", "flist-plus"]);
        assert!(
            stderr.contains("looks like real syntax bugs"),
            "expected real-bug error message, got stderr:\n{stderr}"
        );
        assert!(
            stderr.contains("--broken keep") || stderr.contains("--broken drop"),
            "error should point at the --broken keep/drop escape hatches:\n{stderr}"
        );
    }

    /// `--broken keep` lets users opt into tolerance for non-encrypted parse errors. The broken
    /// file is kept in the output with a warning instead of aborting.
    #[test]
    fn script_broken_keep_includes_broken_file() {
        let out = run_script(&[
            "--target",
            "broken",
            "--top",
            "broken",
            "--broken",
            "keep",
            "flist-plus",
        ]);
        let files = source_basenames(&out);
        assert!(
            files.contains("broken.sv"),
            "broken.sv should survive with --broken keep: {files:?}"
        );
    }

    /// `--broken drop` tolerates broken files but excludes them from the script — useful when
    /// the downstream tool would choke on the broken file but you still want bender to finish.
    #[test]
    fn script_broken_drop_excludes_broken_file() {
        let out = run_script(&[
            "--target",
            "broken",
            "--top",
            "broken",
            "--broken",
            "drop",
            "flist-plus",
        ]);
        let files = source_basenames(&out);
        assert!(
            !files.contains("broken.sv"),
            "broken.sv should be dropped with --broken drop: {files:?}"
        );
    }

    /// Explicit `--broken drop` triggers slang even without `--top`, so users can drop broken
    /// files from a plain `bender script` output without setting up reachability filtering.
    #[test]
    fn script_broken_drop_without_top_excludes_broken_file() {
        let out = run_script(&["--target", "broken", "--broken", "drop", "flist-plus"]);
        let files = source_basenames(&out);
        assert!(
            !files.contains("broken.sv"),
            "broken.sv should be dropped without --top when --broken drop is set: {files:?}"
        );
    }

    /// `--broken keep` is a no-op without `--top` (keeping is what happens anyway when slang
    /// doesn't run). Verifies broken.sv passes through with no warnings emitted.
    #[test]
    fn script_broken_keep_without_top_no_slang() {
        let out = run_script(&["--target", "broken", "--broken", "keep", "flist-plus"]);
        let files = source_basenames(&out);
        assert!(
            files.contains("broken.sv"),
            "broken.sv should pass through with --broken keep: {files:?}"
        );
    }

    /// Regression test: when two files define the same module name, last-wins semantics apply.
    /// The file parsed last (dup_b.sv) wins; the earlier definition (dup_a.sv) is dropped.
    #[test]
    fn script_top_duplicate_module_name_last_wins() {
        // Without --top: both dup files appear (no filtering applied)
        let full_out = run_script(&["--target", "dup", "flist-plus"]);
        let full = source_basenames(&full_out);
        assert!(full.contains("dup_a.sv"));
        assert!(full.contains("dup_b.sv"));
        assert!(full.contains("dup_top.sv"));

        // With --top dup_top: only dup_b.sv (last-wins) and dup_top.sv appear
        let trimmed_out = run_script(&["--target", "dup", "--top", "dup_top", "flist-plus"]);
        let trimmed = source_basenames(&trimmed_out);
        assert!(trimmed.contains("dup_top.sv"));
        assert!(
            trimmed.contains("dup_b.sv"),
            "dup_b.sv (last-wins) missing: {trimmed:?}"
        );
        assert!(
            !trimmed.contains("dup_a.sv"),
            "dup_a.sv (overwritten) should be absent: {trimmed:?}"
        );
    }

    /// Regression test: files whose extension isn't a recognized HDL type (e.g. `.txt`) must
    /// still appear in `flist`/`flist-plus` output. They used to be silently dropped once the
    /// file list switched to iterating per-type source groups.
    #[test]
    fn script_untyped_files_kept_in_flist() {
        for fmt in ["flist", "flist-plus"] {
            let out = run_script(&["--target", "untyped", fmt]);
            let files = source_basenames(&out);
            assert!(
                files.contains("misc_rtl.sv"),
                "typed file missing from {fmt}: {files:?}"
            );
            assert!(
                files.contains("misc_notes.txt"),
                "untyped file must be kept in {fmt}: {files:?}"
            );
        }
    }

    /// Tool scripts that branch on file type must not emit a broken compile command for untyped
    /// files: they comment them out instead. Verify the untyped file appears only on comment
    /// lines and that a skip notice is present.
    #[test]
    fn script_untyped_files_commented_in_tool_scripts() {
        for fmt in [
            "vsim",
            "vcs",
            "synopsys",
            "formality",
            "riviera",
            "genus",
            "precision",
        ] {
            let out = run_script(&["--target", "untyped", fmt]);
            let mentions: Vec<&str> = out
                .lines()
                .filter(|l| l.contains("misc_notes.txt"))
                .collect();
            assert!(
                !mentions.is_empty(),
                "untyped file absent from {fmt} output:\n{out}"
            );
            for line in &mentions {
                assert!(
                    line.trim_start().starts_with('#'),
                    "untyped file must only appear in comments for {fmt}, got line {line:?}\nfull:\n{out}"
                );
            }
            assert!(
                out.contains("Skipping") && out.contains("unknown type"),
                "expected an unknown-type skip notice in {fmt} output:\n{out}"
            );
        }
    }
}