Documentation
¶
Overview ¶
Package testutil provides in-memory testing utilities for the git package. It includes helpers for creating in-memory repositories and test data, enabling tests to run quickly without external dependencies.
Index ¶
- Constants
- func CreateTestCommit(repo *git.Repository, message string) (string, error)
- func CreateTestCommitWithFile(repo *git.Repository, fs billy.Filesystem, path, content, message string) (string, error)
- func CreateTestCommitWithTimestamp(repo *git.Repository, message string, timestamp time.Time) (string, error)
- func CreateTestFile(fs billy.Filesystem, path, content string) error
- func CreateTestTag(repo *git.Repository, name, commitHash, message string) error
- func NewMemoryRepo() (*git.Repository, billy.Filesystem, error)
Constants ¶
const ( // TestAuthor is the default author name for test commits. TestAuthor = "Test User" // TestEmail is the default email for test commits. TestEmail = "test@example.com" // TestAuthor2 is an alternate author name for testing multi-user scenarios. TestAuthor2 = "Another User" // TestEmail2 is an alternate email for testing multi-user scenarios. TestEmail2 = "another@example.com" )
Test user information used across all test helpers.
const ( // TestRepoURL is a sample HTTPS repository URL for testing. TestRepoURL = "https://github.com/test/repo.git" // TestRepoSSHURL is a sample SSH repository URL for testing. TestRepoSSHURL = "git@github.com:test/repo.git" )
Test repository URLs.
const ( // TestFileContent is sample content for README files. TestFileContent = "# Test Repository\n\nThis is a test repository.\n" // TestGoFileContent is sample Go source code. TestGoFileContent = `package main import "fmt" func main() { fmt.Println("Hello, World!") } ` // TestMarkdownContent is sample markdown content. TestMarkdownContent = `# Documentation ## Overview This is test documentation. ## Usage ` + "```" + `go package main func main() { // Example code } ` + "```" + ` ` // TestJSONContent is sample JSON configuration. TestJSONContent = `{ "name": "test-project", "version": "1.0.0", "description": "A test project" } ` // TestYAMLContent is sample YAML configuration. TestYAMLContent = `name: test-project version: 1.0.0 description: A test project settings: enabled: true timeout: 30 ` // TestConfigContent is sample configuration content. TestConfigContent = `` /* 171-byte string literal not displayed */ )
Test file content.
const ( // TestCommitMessage is a standard test commit message. TestCommitMessage = "Test commit" // TestInitialCommit is a message for initial commits. TestInitialCommit = "Initial commit" // TestFeatureCommit is a message for feature commits. TestFeatureCommit = "Add new feature" // TestBugfixCommit is a message for bugfix commits. TestBugfixCommit = "Fix critical bug" // TestMultilineCommit is a multi-line commit message. TestMultilineCommit = `` /* 181-byte string literal not displayed */ )
Test commit messages.
const ( // TestBranchName is a standard test branch name. TestBranchName = "feature/test-branch" // TestBranchMain is the main branch name. TestBranchMain = "main" // TestBranchDevelop is the develop branch name. TestBranchDevelop = "develop" // TestBranchRelease is a release branch name. TestBranchRelease = "release/v1.0.0" )
Test branch names.
const ( // TestTagName is a standard test tag name. TestTagName = "v1.0.0" // TestTagName2 is a second test tag name. TestTagName2 = "v1.1.0" // TestTagName3 is a third test tag name. TestTagName3 = "v2.0.0" // TestTagMessage is a standard tag message. TestTagMessage = "Release version 1.0.0" )
Test tag names.
const ( // TestRemoteName is a standard remote name. TestRemoteName = "origin" // TestRemoteNameUpstream is an upstream remote name. TestRemoteNameUpstream = "upstream" )
Test remote names.
const ( // TestFilePath is a standard test file path. TestFilePath = "README.md" // TestFilePath2 is a second test file path. TestFilePath2 = "docs/guide.md" // TestGoFilePath is a Go source file path. TestGoFilePath = "main.go" // TestConfigFilePath is a configuration file path. TestConfigFilePath = "config.yaml" )
Test file paths.
const TestSSHPrivateKey = `` /* 201-byte string literal not displayed */
TestSSHPrivateKey is a dummy SSH private key for testing (NOT A REAL KEY).
const TestSSHPublicKey = `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITEST_KEY_NOT_FOR_REAL_USE_TESTING_ONLY test@example.com`
TestSSHPublicKey is a dummy SSH public key for testing (NOT A REAL KEY).
Variables ¶
This section is empty.
Functions ¶
func CreateTestCommit ¶
func CreateTestCommit(repo *git.Repository, message string) (string, error)
CreateTestCommit creates a commit in the given repository with test data. This is a convenience helper that creates an empty commit with standard test author information and the provided commit message.
Parameters:
- repo: The repository to create the commit in
- message: The commit message to use
Returns:
- string: The hash of the created commit
- error: Any error encountered during commit creation
The commit is created with:
- Author: "Test User"
- Email: "test@example.com"
- AllowEmpty: true (no file changes required)
Example:
hash, err := testutil.CreateTestCommit(repo, "Initial commit")
if err != nil {
t.Fatal(err)
}
fmt.Println("Created commit:", hash)
func CreateTestCommitWithFile ¶
func CreateTestCommitWithFile(repo *git.Repository, fs billy.Filesystem, path, content, message string) (string, error)
CreateTestCommitWithFile creates a commit that includes a file change. This is a convenience helper that combines file creation with commit creation.
Parameters:
- repo: The repository to create the commit in
- fs: The filesystem to create the file in
- path: The path of the file to create
- content: The content to write to the file
- message: The commit message to use
Returns:
- string: The hash of the created commit
- error: Any error encountered during file creation or commit
Example:
hash, err := testutil.CreateTestCommitWithFile(
repo, fs, "README.md", "# Test", "Add README")
if err != nil {
t.Fatal(err)
}
func CreateTestCommitWithTimestamp ¶
func CreateTestCommitWithTimestamp(repo *git.Repository, message string, timestamp time.Time) (string, error)
CreateTestCommitWithTimestamp creates a commit with a specific timestamp. This is useful for testing time-based operations like commit walking.
Parameters:
- repo: The repository to create the commit in
- message: The commit message to use
- timestamp: The timestamp to use for the commit
Returns:
- string: The hash of the created commit
- error: Any error encountered during commit creation
Example:
timestamp := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
hash, err := testutil.CreateTestCommitWithTimestamp(repo, "Old commit", timestamp)
if err != nil {
t.Fatal(err)
}
func CreateTestFile ¶
func CreateTestFile(fs billy.Filesystem, path, content string) error
CreateTestFile creates a file with the specified content in the given filesystem. This helper simplifies file creation for testing by handling directory creation and file writing in one call.
Parameters:
- fs: The billy filesystem to create the file in
- path: The path of the file to create (relative to filesystem root)
- content: The content to write to the file
Returns:
- error: Any error encountered during file creation or writing
If the parent directory does not exist, it will be created automatically. If the file already exists, it will be truncated and overwritten.
Example:
err := testutil.CreateTestFile(fs, "README.md", "# Test Repository")
if err != nil {
t.Fatal(err)
}
func CreateTestTag ¶
func CreateTestTag(repo *git.Repository, name, commitHash, message string) error
CreateTestTag creates a test tag pointing to the specified commit. This is a convenience helper for creating tags during testing.
Parameters:
- repo: The repository to create the tag in
- name: The name of the tag
- commitHash: The hash of the commit to tag
- message: The tag message (empty for lightweight tag)
Returns:
- error: Any error encountered during tag creation
Example:
err := testutil.CreateTestTag(repo, "v1.0.0", commitHash, "Release 1.0.0")
if err != nil {
t.Fatal(err)
}
func NewMemoryRepo ¶
func NewMemoryRepo() (*git.Repository, billy.Filesystem, error)
NewMemoryRepo creates a new in-memory Git repository for testing. It uses billy's memory filesystem (memfs) to provide a fully functional repository without touching the actual filesystem.
Returns:
- *git.Repository: The initialized in-memory repository
- billy.Filesystem: The memory filesystem for file operations
- error: Any error encountered during repository initialization
The returned filesystem can be used to create files and directories within the repository's working tree. All operations are in-memory and will not persist after the test completes.
Example:
repo, fs, err := testutil.NewMemoryRepo()
if err != nil {
t.Fatal(err)
}
// Create files using fs
// Perform git operations using repo
Types ¶
This section is empty.