Skip to content

🧪 Test Utilities

pkg/composerutils/test_utils.go provides TestHelpers, wrapping common temporary directory and file assertions in tests, reducing boilerplate code.

TestHelpers

go
type TestHelpers struct{}

func NewTestHelpers() *TestHelpers

TestHelpers is a stateless utility struct, all methods receive *testing.T, call t.Fatalf/t.Errorf directly on failure.

Method List

MethodSignatureDescription
CreateTempDir(t *testing.T) stringCreate temporary directory, return path
RemoveTempDir(t *testing.T, dir string)Recursively clean temporary directory
CreateTestFile(t *testing.T, dir, name string, content []byte) stringCreate test file in directory
AssertFileExists(t *testing.T, path string)Assert file exists
AssertFileNotExists(t *testing.T, path string)Assert file does not exist
AssertFileContent(t *testing.T, path string, expectedContent []byte)Assert file content matches

Example

go
package mypkg_test

import (
	"testing"
	"github.com/scagogogo/composer-skills/pkg/composerutils"
)

func TestSomething(t *testing.T) {
	h := composerutils.NewTestHelpers()

	// Prepare temporary environment
	dir := h.CreateTempDir(t)
	defer h.RemoveTempDir(t, dir)

	// Create test file
	path := h.CreateTestFile(t, dir, "composer.json", []byte(`{"name":"test"}`))

	// Assertions
	h.AssertFileExists(t, path)
	h.AssertFileContent(t, path, []byte(`{"name":"test"}`))
	h.AssertFileNotExists(t, dir+"/nonexistent")
}

Design Notes

  • Auto fail: Assertion failures directly report via t, no need to hand-write if got != want { t.Errorf(...) }.
  • Safe cleanup: RemoveTempDir uses os.RemoveAll, just defer call.
  • Non-invasive: No dependency on any test framework, only uses standard testing package.

Working with Mock interfaces

Typical test flow: use TestHelpers to prepare real temporary file system → use Mock Interfaces to replace command execution → assert results. Both combined cover the project's 450+ test cases.

Advanced

Released under the MIT License