File: targets.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (89 lines) | stat: -rw-r--r-- 3,007 bytes parent folder | download | duplicates (3)
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
use anyhow::{Context, Result};
use std::{fs, path::Path};
use wit_parser::{Resolve, WorldId};

/// Tests whether a component targets a world.
///
/// This test looks in the `targets/` directory for test cases.
///
/// The expected input files for a test case are:
///
/// * [required] `test.wat` -- contains the component to test
/// encoded as a component produced via the `embed` and `new`
/// subcommands of `wasm-tools component`.
/// * [required] `test.wit` -- WIT package describing the target
/// world to use when checking conformance.
///
/// And the output file is:
/// * `error.txt` - the expected error message if the synthetic
/// component constructed for testing is a invalid component encoding.
/// NOTE: an invalid encoding here indicates that the targets check has failed.
///
/// Run the test with the environment variable `BLESS` set to update `error.txt`.
///
/// Each test is effectively executing as:
/// `wasm-tools component targets -w foobar test.wit test.wat`
#[test]
fn targets() -> Result<()> {
    drop(env_logger::try_init());

    for entry in fs::read_dir("tests/targets")? {
        let path = entry?.path();
        if !path.is_dir() {
            continue;
        }

        let test_case = path.file_stem().unwrap().to_str().unwrap();
        println!("testing {test_case}");

        let (resolve, world) = load_test_wit(&path)?;

        let component = wat::parse_file(path.join("test.wat"))
            .with_context(|| "failed to parse component WAT".to_string())?;

        match wit_component::targets(&resolve, world, &component) {
            Ok(_) => {
                assert!(
                    !test_case.starts_with("error-"),
                    "should have failed targets check"
                );
            }
            Err(e) => {
                assert!(test_case.starts_with("error-"), "{e:?}");
                assert_output(&path.join("error.txt"), &format!("{e:#}"))?;
            }
        }
    }

    Ok(())
}

fn assert_output(expected: &Path, actual: &str) -> Result<()> {
    if std::env::var_os("BLESS").is_some() {
        fs::create_dir_all(expected.parent().unwrap())?;
        fs::write(expected, actual).with_context(|| format!("failed to write {expected:?}"))?;
    } else {
        assert_eq!(
            fs::read_to_string(expected)
                .with_context(|| format!("failed to read {expected:?}"))?
                .replace("\r\n", "\n"),
            actual,
            "expectation `{}` did not match actual",
            expected.display(),
        );
    }
    Ok(())
}

fn load_test_wit(path: &Path) -> Result<(Resolve, WorldId)> {
    const TEST_TARGET_WORLD_ID: &str = "foobar";

    let test_wit_path = path.join("test.wit");
    let mut resolve = Resolve::default();
    let pkg = resolve.push_file(&test_wit_path)?;
    let world_id = resolve
        .select_world(pkg, Some(TEST_TARGET_WORLD_ID))
        .with_context(|| "failed to select world from package".to_string())?;

    Ok((resolve, world_id))
}