taskgraphtest

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package taskgraphtest provides utilities for testing Graphs and Tasks built using the taskgraph package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffPresent

func DiffPresent[T any](t *testing.T, b tg.Binder, key tg.ReadOnlyKey[T], want T, opts ...cmp.Option)

DiffPresent asserts that b contains key bound to want, and produces a diff if the value is not correct.

func DummyTaskFunc

func DummyTaskFunc(bindings ...tg.Binding) func(context.Context, tg.Binder) ([]tg.Binding, error)

DummyTaskFunc returns a function suitable for passing to taskgraph.NewTask which returns the given bindings.

func ErrorTaskFunc

func ErrorTaskFunc(err error) func(context.Context, tg.Binder) ([]tg.Binding, error)

ErrorTaskFunc returns a function suitable for passing to taskgraph.NewTask which returns the given error.

func ExpectAbsent

func ExpectAbsent[T any](t *testing.T, b tg.Binder, key tg.ReadOnlyKey[T])

ExpectAbsent asserts that b contains key bound as absent.

func ExpectAbsentError

func ExpectAbsentError[T any](t *testing.T, b tg.Binder, key tg.ReadOnlyKey[T], wantErr error)

ExpectAbsentError asserts that b contains key bound as absent with the given error.

func ExpectBindings

func ExpectBindings(t *testing.T, b tg.Binder, want []BindingMatcher)

ExpectBindings checks if the binder contains bindings which match the given matchers.

func ExpectExactBindings

func ExpectExactBindings(t *testing.T, b tg.Binder, want []BindingMatcher)

ExpectExactBindings checks if the binder contains bindings which match the given matchers, and no other bindings. Note that this will not play nicely with virtual bindings such as those generated by tg.Presence().

func ExpectPending

func ExpectPending[T any](t *testing.T, b tg.Binder, key tg.ReadOnlyKey[T])

ExpectPending asserts that b does not contain a binding for key (i.e. it is pending).

func ExpectPresent

func ExpectPresent[T any](t *testing.T, b tg.Binder, key tg.ReadOnlyKey[T], want T, opts ...cmp.Option)

ExpectPresent asserts that b contains key bound to want.

func Must

func Must[T any](t *testing.T) func(val T, err error) T

Must produces a function which wraps around another function returning a value and error, calls t.Fatal if the error is non-nil, and otherwise returns the value. This is intended to be used for constructing Tasks and Graphs within tests:

func TestFoo(t *testing.T) (
  myTask  = taskgraphtest.Must(t)(taskgraph.Reflect(...))
  myGraph = taskgraphtest.Must(t)(taskgraph.New(myTask))
)

func TransformMaybe

func TransformMaybe[T any]() cmp.Option

TransformMaybe returns a cmp.Option which allows comparison of Maybe[T]. When using this option, the cmpopts.EquateErrors() option should also be passed.

Types

type BindingMatcher

type BindingMatcher interface {
	// ID should return the id of the binding to be checked.
	ID() tg.ID

	// Match should return an error if the binding does not match, and nil otherwise.
	Match(got tg.Binding) error
}

A BindingMatcher is used to compare a binding produced by a graph. A BindingMatcher should return an error if the bindings do not match, and nil otherwise.

func Match

func Match(want tg.Binding, opts ...cmp.Option) BindingMatcher

Match creates a BindingMatcher which checks the value of a single binding using cmp.Equal with the given options.

func MatchDiff

func MatchDiff(want tg.Binding, opts ...cmp.Option) BindingMatcher

MatchDiff creates a BindingMatcher which checks the value of a single binding using cmp.Diff with the given options, returning the diff (if any).

func MatchFunc

func MatchFunc[T any](want tg.Binding, compareFn func(got, want T) error) BindingMatcher

MatchFunc creates a BindingMatcher which compares present bindings using the given function.

type Suite

type Suite struct {
	// Graph defines the Graph to be tested. Should not be set in conjunction with Task. The Graph or
	// Task to be tested is preferentially read from each Test, falling back to the Suite (if used).
	Graph tg.Graph

	// Task provides a convenient approach for testing individual tasks; the given Task is turned into
	// a single-node Graph. Should not be set in conjunction with Graph. The Graph or Task to be
	// tested is preferentially read from each Test, falling back to the Suite (if used).
	//
	// For compatibility with helpers which accept and return TaskSets, this field is a TaskSet, but
	// the set is expected to contain a single element.
	Task tg.TaskSet

	// Tests defines the individual tests to be run.
	Tests []Test

	// How long the task/graph should be allowed to run in each test. If unset, defaults to 10 seconds
	Timeout time.Duration
}

Suite defines a collection of tests which can be run against a common task or graph, as a convenience over defining your own table-driven tests. Each Test in the Suite is run as a subtest using testing.T.Run.

func (Suite) Run

func (s Suite) Run(t *testing.T)

Run all tests in the Suite.

type Test

type Test struct {
	// Description is passed to testing.T.Run when this test is part of a suite; it may be omited if
	// the test is used in isolation.
	Description string

	// Graph defines the Graph to be tested. Should not be set in conjunction with Task. The Graph or
	// Task to be tested is preferentially read from the Test, falling back to the Suite (if used).
	Graph tg.Graph

	// Task provides a convenient approach for testing individual tasks; the given Task is turned into
	// a single-node Graph. Should not be set in conjunction with Graph. The Graph or Task to be
	// tested is preferentially read from the Test, falling back to the Suite (if used).
	//
	// For compatibility with helpers which accept and return TaskSets, this field is a TaskSet, but
	// the set is expected to contain a single element.
	Task tg.TaskSet

	// Inputs defines the inputs passed to the graph for the test.
	Inputs []tg.Binding

	// WantError defines the expected error returned from the Graph's execution (note that the test
	// will always fail if Graph setup/configuration fails). Compared using errors.Is.
	WantError error

	// WantBindings defines the expected set of bindings to be available once the graph/task is run.
	// Note that unless CheckExcessBindings is set, this field need not contain every single binding
	// produced by a graph (and in general it is advisable that it doesn't to avoid fragile tests).
	WantBindings []BindingMatcher

	// WantPendingBindings defines a set of IDs which should not have been bound by the graph. This
	// is typically only used for testing the framework itself (asserting that IDs are unbound when
	// errors occur)
	WantPendingBindings []tg.ID

	// CheckExcessBindings, if true, causes the graph result to be checked for the presence of
	// bindings not in WantBindings. This option should be used sparingly, as it can lead to fragile
	// tests. Note that this will not play nicely with virtual bindings such as those generated
	// by tg.Presence().
	CheckExcessBindings bool

	// WantInputBindings defines matchers on input bindings (i.e. those in Inputs) which are checked
	// after the graph is run. This can be used to check for side effects on inputs.
	WantInputBindings []BindingMatcher

	// How long the task/graph should be allowed to run. If unset, defaults to 10 seconds
	Timeout time.Duration
}

Test defines the expected behaviour of a Task or Graph, validating expected bindings for given inputs.

func (Test) Run

func (test Test) Run(t *testing.T)

Run the test.

Jump to

Keyboard shortcuts

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