🧪 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() *TestHelpersTestHelpers is a stateless utility struct, all methods receive *testing.T, call t.Fatalf/t.Errorf directly on failure.
Method List
| Method | Signature | Description |
|---|---|---|
CreateTempDir | (t *testing.T) string | Create temporary directory, return path |
RemoveTempDir | (t *testing.T, dir string) | Recursively clean temporary directory |
CreateTestFile | (t *testing.T, dir, name string, content []byte) string | Create 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-writeif got != want { t.Errorf(...) }. - Safe cleanup:
RemoveTempDirusesos.RemoveAll, just defer call. - Non-invasive: No dependency on any test framework, only uses standard
testingpackage.
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
- Mock command/file system see Mock Interfaces.
- Project test organization and conventions see Testability Design.