feat: initial commit of Git Automation CLI
- Add comprehensive Git workflow automation tools - Include branch management utilities - Add commit helpers with conventional commit support - Implement GitHub integration for PR management - Add configuration management system - Include comprehensive test coverage - Add professional documentation and examples
This commit is contained in:
202
internal/validation/validation_test.go
Normal file
202
internal/validation/validation_test.go
Normal file
@@ -0,0 +1,202 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewValidationResult(t *testing.T) {
|
||||
result := NewValidationResult()
|
||||
|
||||
if result.IsValid != true {
|
||||
t.Errorf("Expected IsValid to be true, got %v", result.IsValid)
|
||||
}
|
||||
|
||||
if len(result.Errors) != 0 {
|
||||
t.Errorf("Expected Errors to be empty, got %v", result.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddError(t *testing.T) {
|
||||
result := NewValidationResult()
|
||||
|
||||
result.AddError("test error")
|
||||
|
||||
if result.IsValid != false {
|
||||
t.Errorf("Expected IsValid to be false, got %v", result.IsValid)
|
||||
}
|
||||
|
||||
if len(result.Errors) != 1 {
|
||||
t.Errorf("Expected Errors to have 1 element, got %d", len(result.Errors))
|
||||
}
|
||||
|
||||
if result.Errors[0] != "test error" {
|
||||
t.Errorf("Expected error to be 'test error', got '%s'", result.Errors[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetErrors(t *testing.T) {
|
||||
result := NewValidationResult()
|
||||
|
||||
result.AddError("first error")
|
||||
result.AddError("second error")
|
||||
|
||||
errors := result.GetErrors()
|
||||
expected := "first error\nsecond error"
|
||||
|
||||
if errors != expected {
|
||||
t.Errorf("Expected errors to be '%s', got '%s'", expected, errors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateGitRepository(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// This test will fail if not run in a git repository
|
||||
// We'll just check that it returns a ValidationResult
|
||||
result := ValidateGitRepository(ctx)
|
||||
|
||||
if result == nil {
|
||||
t.Error("Expected ValidateGitRepository to return a ValidationResult, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateBranchName(t *testing.T) {
|
||||
// Test empty branch name
|
||||
result := ValidateBranchName("")
|
||||
if result.IsValid {
|
||||
t.Error("Expected empty branch name to be invalid")
|
||||
}
|
||||
|
||||
// Test valid branch name
|
||||
result = ValidateBranchName("feature/test-branch")
|
||||
if !result.IsValid {
|
||||
t.Errorf("Expected valid branch name to be valid, got errors: %s", result.GetErrors())
|
||||
}
|
||||
|
||||
// Test branch name with invalid characters
|
||||
result = ValidateBranchName("feature/test branch")
|
||||
if result.IsValid {
|
||||
t.Error("Expected branch name with space to be invalid")
|
||||
}
|
||||
|
||||
// Test branch name starting with a dot
|
||||
result = ValidateBranchName(".feature/test")
|
||||
if result.IsValid {
|
||||
t.Error("Expected branch name starting with dot to be invalid")
|
||||
}
|
||||
|
||||
// Test branch name ending with a slash
|
||||
result = ValidateBranchName("feature/test/")
|
||||
if result.IsValid {
|
||||
t.Error("Expected branch name ending with slash to be invalid")
|
||||
}
|
||||
|
||||
// Test branch name with consecutive slashes
|
||||
result = ValidateBranchName("feature//test")
|
||||
if result.IsValid {
|
||||
t.Error("Expected branch name with consecutive slashes to be invalid")
|
||||
}
|
||||
|
||||
// Test main branch name
|
||||
result = ValidateBranchName("main")
|
||||
if !result.IsValid {
|
||||
t.Error("Expected 'main' branch name to be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCommitMessage(t *testing.T) {
|
||||
// Test empty commit message
|
||||
result := ValidateCommitMessage("")
|
||||
if result.IsValid {
|
||||
t.Error("Expected empty commit message to be invalid")
|
||||
}
|
||||
|
||||
// Test valid commit message
|
||||
result = ValidateCommitMessage("Add new feature")
|
||||
if !result.IsValid {
|
||||
t.Errorf("Expected valid commit message to be valid, got errors: %s", result.GetErrors())
|
||||
}
|
||||
|
||||
// Test commit message that's too long
|
||||
result = ValidateCommitMessage("This is a very long commit message that exceeds the 72 character limit by a lot")
|
||||
if result.IsValid {
|
||||
t.Error("Expected commit message that's too long to be invalid")
|
||||
}
|
||||
|
||||
// Test commit message starting with whitespace
|
||||
result = ValidateCommitMessage(" Add new feature")
|
||||
if result.IsValid {
|
||||
t.Error("Expected commit message starting with whitespace to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConventionalCommit(t *testing.T) {
|
||||
// Test empty commit message
|
||||
result := ValidateConventionalCommit("")
|
||||
if result.IsValid {
|
||||
t.Error("Expected empty commit message to be invalid")
|
||||
}
|
||||
|
||||
// Test valid conventional commit message
|
||||
result = ValidateConventionalCommit("feat(auth): add login functionality")
|
||||
if !result.IsValid {
|
||||
t.Errorf("Expected valid conventional commit message to be valid, got errors: %s", result.GetErrors())
|
||||
}
|
||||
|
||||
// Test valid conventional commit message with breaking change
|
||||
result = ValidateConventionalCommit("feat(auth)!: add login functionality")
|
||||
if !result.IsValid {
|
||||
t.Errorf("Expected valid conventional commit message with breaking change to be valid, got errors: %s", result.GetErrors())
|
||||
}
|
||||
|
||||
// Test invalid conventional commit message
|
||||
result = ValidateConventionalCommit("Add new feature")
|
||||
if result.IsValid {
|
||||
t.Error("Expected invalid conventional commit message to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateGitHubToken(t *testing.T) {
|
||||
// Test empty token
|
||||
result := ValidateGitHubToken("")
|
||||
if result.IsValid {
|
||||
t.Error("Expected empty token to be invalid")
|
||||
}
|
||||
|
||||
// Test valid personal access token (40 characters total)
|
||||
result = ValidateGitHubToken("ghp_123456789012345678901234567890123456")
|
||||
if !result.IsValid {
|
||||
t.Errorf("Expected valid personal access token to be valid, got errors: %s", result.GetErrors())
|
||||
}
|
||||
|
||||
// Test valid OAuth token (40 characters total)
|
||||
result = ValidateGitHubToken("gho_123456789012345678901234567890123456")
|
||||
if !result.IsValid {
|
||||
t.Errorf("Expected valid OAuth token to be valid, got errors: %s", result.GetErrors())
|
||||
}
|
||||
|
||||
// Test token with invalid prefix
|
||||
result = ValidateGitHubToken("abc_123456789012345678901234567890123456")
|
||||
if result.IsValid {
|
||||
t.Error("Expected token with invalid prefix to be invalid")
|
||||
}
|
||||
|
||||
// Test token with invalid length
|
||||
result = ValidateGitHubToken("ghp_12345678901234567890123456789012345")
|
||||
if result.IsValid {
|
||||
t.Error("Expected token with invalid length to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateWorkingDirectory(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// This test will fail if run in a clean working directory
|
||||
// We'll just check that it returns a ValidationResult
|
||||
result := ValidateWorkingDirectory(ctx)
|
||||
|
||||
if result == nil {
|
||||
t.Error("Expected ValidateWorkingDirectory to return a ValidationResult, got nil")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user