MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / load_runtime_test

Method load_runtime_test

crates/test/src/lib.rs:356–434  ·  view source on GitHub ↗

Loads a test from `dir` using the `wit` file in the directory specified. Returns a list of components that were found within this directory.

(
        &self,
        wit: &Path,
        dir: &Path,
    )

Source from the content-addressed store, hash-verified

354 ///
355 /// Returns a list of components that were found within this directory.
356 fn load_runtime_test(
357 &self,
358 wit: &Path,
359 dir: &Path,
360 ) -> Result<(config::WitConfig, Vec<Component>)> {
361 let mut resolve = wit_parser::Resolve::default();
362
363 let wit_path = if dir.join("deps").exists() { dir } else { wit };
364 let (pkg, _files) = resolve.push_path(wit_path).context(format!(
365 "failed to load `test.wit` in test directory: {:?}",
366 &wit
367 ))?;
368 let resolve = Arc::new(resolve);
369
370 let wit_contents = std::fs::read_to_string(wit)?;
371 let wit_config: config::WitConfig = config::parse_test_config(&wit_contents, "//@")
372 .context("failed to parse WIT test config")?;
373
374 let mut worlds = Vec::new();
375
376 let mut push_world = |kind: Kind, name: &str| -> Result<()> {
377 let world = resolve.select_world(&[pkg], Some(name)).with_context(|| {
378 format!("failed to find expected `{name}` world to generate bindings")
379 })?;
380 worlds.push((world, kind));
381 Ok(())
382 };
383 push_world(Kind::Runner, wit_config.runner_world())?;
384 for world in wit_config.dependency_worlds() {
385 push_world(Kind::Test, &world)?;
386 }
387
388 let mut components = Vec::new();
389 let mut any_runner = false;
390 let mut any_test = false;
391
392 for entry in dir.read_dir().context("failed to read test directory")? {
393 let entry = entry.context("failed to read test directory entry")?;
394 let path = entry.path();
395
396 let Some(name) = path.file_name().and_then(|s| s.to_str()) else {
397 continue;
398 };
399 if name == "test.wit" {
400 continue;
401 }
402
403 let Some((world, kind)) = worlds
404 .iter()
405 .find(|(world, _kind)| name.starts_with(&resolve.worlds[*world].name))
406 else {
407 log::debug!("skipping file {name:?}");
408 continue;
409 };
410 match kind {
411 Kind::Runner => any_runner = true,
412 Kind::Test => any_test = true,
413 }

Callers 1

discover_testsMethod · 0.80

Calls 10

newFunction · 0.85
parse_test_configFunction · 0.85
joinMethod · 0.80
runner_worldMethod · 0.80
dependency_worldsMethod · 0.80
and_thenMethod · 0.80
findMethod · 0.80
parse_componentMethod · 0.80
pushMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected