- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec - Add // nolint:gosec comments for intentional file operations and subprocesses - Change file write permissions from 0644 to 0600 for better security - Refactor loops, error handling, and test parallelism with t.Parallel() - Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package errors
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestGitError(t *testing.T) {
|
|
baseErr := errors.New("command failed")
|
|
gitErr := &GitError{
|
|
Command: "status",
|
|
Err: baseErr,
|
|
}
|
|
|
|
expected := "git status failed: command failed"
|
|
if gitErr.Error() != expected {
|
|
t.Errorf("GitError.Error() = %q, want %q", gitErr.Error(), expected)
|
|
}
|
|
|
|
if !errors.Is(gitErr, baseErr) {
|
|
t.Errorf("GitError did not wrap base error")
|
|
}
|
|
}
|
|
|
|
func TestAPIError(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
apiErr *APIError
|
|
expected string
|
|
shouldWrap bool
|
|
}{
|
|
{
|
|
name: "with status code",
|
|
apiErr: &APIError{
|
|
StatusCode: 404,
|
|
Message: "not found",
|
|
},
|
|
expected: "API error (status 404): not found",
|
|
},
|
|
{
|
|
name: "without status code",
|
|
apiErr: &APIError{
|
|
Message: "connection failed",
|
|
},
|
|
expected: "API error: connection failed",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.apiErr.Error() != tt.expected {
|
|
t.Errorf("APIError.Error() = %q, want %q", tt.apiErr.Error(), tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFileError(t *testing.T) {
|
|
baseErr := errors.New("permission denied")
|
|
fileErr := &FileError{
|
|
Path: "/tmp/test.txt",
|
|
Op: "read",
|
|
Err: baseErr,
|
|
}
|
|
|
|
expected := "file read failed for /tmp/test.txt: permission denied"
|
|
if fileErr.Error() != expected {
|
|
t.Errorf("FileError.Error() = %q, want %q", fileErr.Error(), expected)
|
|
}
|
|
|
|
if !errors.Is(fileErr, baseErr) {
|
|
t.Errorf("FileError did not wrap base error")
|
|
}
|
|
}
|
|
|
|
func TestAPIErrorUnwrap(t *testing.T) {
|
|
baseErr := errors.New("base api err")
|
|
apiErr := &APIError{
|
|
StatusCode: 500,
|
|
Message: "internal error",
|
|
Err: baseErr,
|
|
}
|
|
if !errors.Is(apiErr, baseErr) {
|
|
t.Errorf("APIError.Unwrap() = %v, want %v", apiErr.Unwrap(), baseErr)
|
|
}
|
|
}
|