testutil

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package testutil provides test helpers for msgvault tests.

The package is organized into focused files:

  • assert.go: assertion helpers (MustNoErr, AssertEqualSlices, etc.)
  • store_helpers.go: database test setup (NewTestStore)
  • fs_helpers.go: filesystem operations (WriteFile, ReadFile, MustExist)
  • archive_helpers.go: archive creation (CreateTarGz, CreateTempZip)
  • security_data.go: security test vectors (PathTraversalCases)
  • builders.go: test data builders
  • encoding.go: encoding test helpers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContainsAll added in v0.2.0

func AssertContainsAll(t *testing.T, got string, subs []string)

AssertContainsAll asserts that got contains every substring in subs.

func AssertEqual added in v0.4.0

func AssertEqual[T comparable](t *testing.T, got, want T)

AssertEqual compares two comparable values and fails the test if they differ.

func AssertEqualSlices added in v0.4.0

func AssertEqualSlices[T comparable](t *testing.T, got []T, want ...T)

AssertEqualSlices compares two slices element-by-element.

func AssertFileContent

func AssertFileContent(t *testing.T, path string, expected string)

AssertFileContent reads the file at path and asserts its content matches expected.

func AssertStringSet added in v0.4.0

func AssertStringSet(t testing.TB, got []string, want ...string)

AssertStringSet asserts that got contains exactly the expected strings, ignoring order. Useful when the slice order is non-deterministic. Duplicates are counted: ["a", "a"] does not match ["a", "b"].

func AssertStrings added in v0.2.0

func AssertStrings(t *testing.T, got []string, want ...string)

AssertStrings compares two string slices element-by-element. It provides nicer %q formatting for string values.

func AssertValidUTF8 added in v0.2.0

func AssertValidUTF8(t *testing.T, s string)

AssertValidUTF8 asserts that the given string is valid UTF-8.

func CreateTarGz added in v0.2.0

func CreateTarGz(t *testing.T, path string, entries []ArchiveEntry)

CreateTarGz creates a tar.gz archive at path containing the given entries.

func CreateTempZip added in v0.2.0

func CreateTempZip(t *testing.T, entries map[string]string) string

CreateTempZip creates a zip file in a temporary directory containing the provided entries (filename -> content). Returns the path to the zip file.

func CreateZip added in v0.6.0

func CreateZip(t *testing.T, path string, entries []ArchiveEntry)

CreateZip creates a zip archive at path containing the given entries.

func MakeSet added in v0.4.0

func MakeSet[T comparable](items ...T) map[T]bool

MakeSet builds a map[T]bool from the given items. Useful for constructing selection sets in tests.

func MustExist

func MustExist(t *testing.T, path string)

MustExist fails the test if the path does not exist or cannot be accessed.

func MustNoErr

func MustNoErr(t *testing.T, err error, msg string)

MustNoErr fails the test immediately if err is non-nil. Use this for setup operations where failure means the test cannot proceed.

func MustNotExist

func MustNotExist(t *testing.T, path string)

MustNotExist fails the test if the path exists or if there's an error other than "not exist" (e.g., permission denied).

func NewTestStore

func NewTestStore(t *testing.T) *store.Store

NewTestStore creates a temporary database for testing. The database is automatically cleaned up when the test completes.

func ReadFile

func ReadFile(t *testing.T, path string) []byte

ReadFile reads a file and fails the test on error.

func WriteAndVerifyFile added in v0.2.0

func WriteAndVerifyFile(t *testing.T, dir, rel string, content []byte) string

WriteAndVerifyFile writes content to a file, asserts it exists, and verifies its content matches. Returns the full path to the written file.

func WriteFile

func WriteFile(t *testing.T, dir, name string, content []byte) string

WriteFile writes content to a file in the given directory. The name must be a relative path without ".." components to ensure test isolation. Absolute paths or paths that escape dir will fail the test.

Types

type ArchiveEntry added in v0.2.0

type ArchiveEntry struct {
	Name     string
	Content  string
	TypeFlag byte
	LinkName string
	Mode     int64
}

ArchiveEntry describes a single entry in a tar.gz archive for testing.

type EncodedSamplesT added in v0.2.0

type EncodedSamplesT struct {
	ShiftJIS_Konnichiwa     []byte
	GBK_Nihao               []byte
	Big5_Nihao              []byte
	EUCKR_Annyeong          []byte
	Win1252_SmartQuoteRight []byte
	Win1252_EnDash          []byte
	Win1252_EmDash          []byte
	Win1252_DoubleQuotes    []byte
	Win1252_Trademark       []byte
	Win1252_Bullet          []byte
	Win1252_Euro            []byte
	Latin1_OAcute           []byte
	Latin1_CCedilla         []byte
	Latin1_UUmlaut          []byte
	Latin1_NTilde           []byte
	Latin1_Registered       []byte
	Latin1_Degree           []byte

	// Longer Asian encoding samples for reliable charset detection.
	// These are long enough for chardet to identify with high confidence.
	ShiftJIS_Long      []byte
	ShiftJIS_Long_UTF8 string
	GBK_Long           []byte
	GBK_Long_UTF8      string
	Big5_Long          []byte
	Big5_Long_UTF8     string
	EUCKR_Long         []byte
	EUCKR_Long_UTF8    string
}

EncodedSamplesT holds encoded byte sequences for testing charset detection and repair.

func EncodedSamples added in v0.2.0

func EncodedSamples() EncodedSamplesT

EncodedSamples returns a fresh copy of all encoded byte samples, safe for mutation by individual tests without cross-test coupling.

MAINTAINER NOTE: This function uses explicit field copying rather than reflection. This is intentional. Reflection-based "automatic" copying: - Adds complexity (handling unexported fields, nil slices, etc.) - Requires extensive test coverage for the reflection code itself - Is not worth it for a test helper with infrequent field additions

If you add a new field to EncodedSamplesT, add a corresponding line here. TestEncodedSamplesNonEmpty will catch any missing []byte fields.

type MessageDetailBuilder added in v0.2.0

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

MessageDetailBuilder provides a fluent API for constructing query.MessageDetail in tests.

func NewMessageDetail added in v0.2.0

func NewMessageDetail(id int64) *MessageDetailBuilder

NewMessageDetail creates a builder with sensible defaults.

func (*MessageDetailBuilder) Build added in v0.2.0

func (*MessageDetailBuilder) BuildPtr added in v0.2.0

func (b *MessageDetailBuilder) BuildPtr() *query.MessageDetail

func (*MessageDetailBuilder) WithAttachments added in v0.2.0

func (b *MessageDetailBuilder) WithAttachments(atts ...query.AttachmentInfo) *MessageDetailBuilder

func (*MessageDetailBuilder) WithBcc added in v0.2.0

func (*MessageDetailBuilder) WithBodyHTML added in v0.2.0

func (b *MessageDetailBuilder) WithBodyHTML(s string) *MessageDetailBuilder

func (*MessageDetailBuilder) WithBodyText added in v0.2.0

func (b *MessageDetailBuilder) WithBodyText(s string) *MessageDetailBuilder

func (*MessageDetailBuilder) WithCc added in v0.2.0

func (*MessageDetailBuilder) WithConversationID added in v0.2.0

func (b *MessageDetailBuilder) WithConversationID(id int64) *MessageDetailBuilder

func (*MessageDetailBuilder) WithFrom added in v0.2.0

func (b *MessageDetailBuilder) WithFrom(addrs ...query.Address) *MessageDetailBuilder

func (*MessageDetailBuilder) WithFromAddress added in v0.4.0

func (b *MessageDetailBuilder) WithFromAddress(email, name string) *MessageDetailBuilder

WithFromAddress is a convenience method for setting a single sender without needing to construct a query.Address struct.

func (*MessageDetailBuilder) WithLabels added in v0.2.0

func (b *MessageDetailBuilder) WithLabels(labels ...string) *MessageDetailBuilder

func (*MessageDetailBuilder) WithSentAt added in v0.2.0

func (*MessageDetailBuilder) WithSize added in v0.2.0

func (*MessageDetailBuilder) WithSnippet added in v0.2.0

func (*MessageDetailBuilder) WithSourceMessageID added in v0.2.0

func (b *MessageDetailBuilder) WithSourceMessageID(id string) *MessageDetailBuilder

func (*MessageDetailBuilder) WithSubject added in v0.2.0

func (*MessageDetailBuilder) WithTo added in v0.2.0

type MessageSummaryBuilder added in v0.2.0

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

MessageSummaryBuilder provides a fluent API for constructing query.MessageSummary in tests.

func NewMessageSummary added in v0.2.0

func NewMessageSummary(id int64) *MessageSummaryBuilder

NewMessageSummary creates a builder with sensible defaults.

func (*MessageSummaryBuilder) Build added in v0.2.0

func (*MessageSummaryBuilder) BuildPtr added in v0.4.0

BuildPtr returns a pointer to the constructed MessageSummary.

func (*MessageSummaryBuilder) WithAttachmentCount added in v0.4.0

func (b *MessageSummaryBuilder) WithAttachmentCount(count int) *MessageSummaryBuilder

WithAttachmentCount sets the attachment count and HasAttachments flag. Named differently from MessageDetailBuilder.WithAttachments to clarify that this takes a count, not actual attachment structs.

func (*MessageSummaryBuilder) WithConversationID added in v0.2.0

func (b *MessageSummaryBuilder) WithConversationID(id int64) *MessageSummaryBuilder

func (*MessageSummaryBuilder) WithDeleted added in v0.4.0

WithDeleted is a convenience method that sets DeletedAt from a time.Time value, handling pointer conversion internally.

func (*MessageSummaryBuilder) WithDeletedAt added in v0.2.0

func (b *MessageSummaryBuilder) WithDeletedAt(t *time.Time) *MessageSummaryBuilder

func (*MessageSummaryBuilder) WithFromEmail added in v0.2.0

func (b *MessageSummaryBuilder) WithFromEmail(e string) *MessageSummaryBuilder

func (*MessageSummaryBuilder) WithFromName added in v0.2.0

func (*MessageSummaryBuilder) WithLabels added in v0.2.0

func (b *MessageSummaryBuilder) WithLabels(labels ...string) *MessageSummaryBuilder

func (*MessageSummaryBuilder) WithSentAt added in v0.2.0

func (*MessageSummaryBuilder) WithSize added in v0.2.0

func (*MessageSummaryBuilder) WithSnippet added in v0.2.0

func (*MessageSummaryBuilder) WithSourceMessageID added in v0.2.0

func (b *MessageSummaryBuilder) WithSourceMessageID(id string) *MessageSummaryBuilder

func (*MessageSummaryBuilder) WithSubject added in v0.2.0

type PathTraversalCase added in v0.2.0

type PathTraversalCase struct{ Name, Path string }

PathTraversalCase describes a single path traversal test vector.

func PathTraversalCases added in v0.2.0

func PathTraversalCases() []PathTraversalCase

PathTraversalCases returns a fresh slice of path traversal attack vectors for testing path sanitization logic. The returned cases include OS-appropriate absolute path variants so Windows UNC/drive-letter paths are also covered.

Directories

Path Synopsis
Package dbtest provides shared database test helpers for seeding and querying test databases.
Package dbtest provides shared database test helpers for seeding and querying test databases.
Package email provides test helpers for constructing raw RFC 2822 email messages.
Package email provides test helpers for constructing raw RFC 2822 email messages.
Package ptr provides generic pointer helpers for tests.
Package ptr provides generic pointer helpers for tests.
Package storetest provides a StoreFixture and helpers for tests that exercise the Store layer through its public API.
Package storetest provides a StoreFixture and helpers for tests that exercise the Store layer through its public API.
Package tbmock provides a mock testing.TB for verifying fail-fast behavior.
Package tbmock provides a mock testing.TB for verifying fail-fast behavior.

Jump to

Keyboard shortcuts

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