componenttest

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package componenttest provides utilities for testing a host implementation of a component interface.

A host func registered with component.WithImport or WithImportCustom is normally only reachable by instantiating a guest that imports it and getting the guest to call it. That is the right end-to-end test, and a poor unit test: covering one error branch — a malformed argument, an exhausted resource, a filesystem that returns EPERM — would mean compiling a guest component per branch.

Harness closes that gap. It applies the same Options an Instantiate call would, runs the resource hooks, and hands back the registered funcs so a test can call them directly with lifted argument values and assert on the values they return.

h := componenttest.New(component.WithImportCustom("acme:api/host@1.0.0", "lookup", fn, fd, tbl.Resolver()))
got, err := h.Func("acme:api/host@1.0.0", "lookup")(ctx, []component.Value{...})

What it does NOT do is exercise the Canonical ABI: nothing is lifted from or lowered into guest memory, so a signature that is wrong about how a value is laid out still passes here. Keep at least one real-guest test per interface; use this for the branches underneath it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Harness

type Harness struct {
	// contains filtered or unexported fields
}

Harness holds host funcs registered from a set of Options, callable without a guest.

func New

func New(opts ...component.Option) *Harness

New applies opts and runs the resource hooks, in the same order component.Instantiate does before any host func can run.

func (*Harness) Func

func (h *Harness) Func(iface, name string) component.HostFunc

Func returns the host func registered for iface/name, or nil if none is. iface is matched with its "@x.y.z" suffix stripped, the same way registration matches, so the version passed here need not be exact.

func (*Harness) MustFunc

func (h *Harness) MustFunc(iface, name string) component.HostFunc

MustFunc is Func, panicking when nothing is registered — for a test that would otherwise nil-panic one line later with no indication of which registration is missing.

func (*Harness) Registered

func (h *Harness) Registered() map[string][]string

Registered returns every import the Options registered, as interface name -> func names, with version suffixes stripped. Useful for asserting that a host implementation registers exactly the surface it claims to -- wazy's own WASI conformance test uses it to diff the registered surface against the reference WIT.

func (*Harness) Resources

func (h *Harness) Resources() *component.HandleTable

Resources returns the handle table the hooks were given. It is the same table a real instantiation would use, so a handle a host func under test mints can be resolved back to its rep here — which is how a test asserts that an own<T> result names what it should.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL