Files
gibidify/testutil/assertions_test.go
Ismo Vuorinen 95b7ef6dd3 chore: modernize workflows, security scanning, and linting configuration (#50)
* build: update Go 1.25, CI workflows, and build tooling

- Upgrade to Go 1.25
- Add benchmark targets to Makefile
- Implement parallel gosec execution
- Lock tool versions for reproducibility
- Add shellcheck directives to scripts
- Update CI workflows with improved caching

* refactor: migrate from golangci-lint to revive

- Replace golangci-lint with revive for linting
- Configure comprehensive revive rules
- Fix all EditorConfig violations
- Add yamllint and yamlfmt support
- Remove deprecated .golangci.yml

* refactor: rename utils to shared and deduplicate code

- Rename utils package to shared
- Add shared constants package
- Deduplicate constants across packages
- Address CodeRabbit review feedback

* fix: resolve SonarQube issues and add safety guards

- Fix all 73 SonarQube OPEN issues
- Add nil guards for resourceMonitor, backpressure, metricsCollector
- Implement io.Closer for headerFileReader
- Propagate errors from processing helpers
- Add metrics and templates packages
- Improve error handling across codebase

* test: improve test infrastructure and coverage

- Add benchmarks for cli, fileproc, metrics
- Improve test coverage for cli, fileproc, config
- Refactor tests with helper functions
- Add shared test constants
- Fix test function naming conventions
- Reduce cognitive complexity in benchmark tests

* docs: update documentation and configuration examples

- Update CLAUDE.md with current project state
- Refresh README with new features
- Add usage and configuration examples
- Add SonarQube project configuration
- Consolidate config.example.yaml

* fix: resolve shellcheck warnings in scripts

- Use ./*.go instead of *.go to prevent dash-prefixed filenames
  from being interpreted as options (SC2035)
- Remove unreachable return statement after exit (SC2317)
- Remove obsolete gibidiutils/ directory reference

* chore(deps): upgrade go dependencies

* chore(lint): megalinter fixes

* fix: improve test coverage and fix file descriptor leaks

- Add defer r.Close() to fix pipe file descriptor leaks in benchmark tests
- Refactor TestProcessorConfigureFileTypes with helper functions and assertions
- Refactor TestProcessorLogFinalStats with output capture and keyword verification
- Use shared constants instead of literal strings (TestFilePNG, FormatMarkdown, etc.)
- Reduce cognitive complexity by extracting helper functions

* fix: align test comments with function names

Remove underscores from test comments to match actual function names:
- benchmark/benchmark_test.go (2 fixes)
- fileproc/filetypes_config_test.go (4 fixes)
- fileproc/filetypes_registry_test.go (6 fixes)
- fileproc/processor_test.go (6 fixes)
- fileproc/resource_monitor_types_test.go (4 fixes)
- fileproc/writer_test.go (3 fixes)

* fix: various test improvements and bug fixes

- Remove duplicate maxCacheSize check in filetypes_registry_test.go
- Shorten long comment in processor_test.go to stay under 120 chars
- Remove flaky time.Sleep in collector_test.go, use >= 0 assertion
- Close pipe reader in benchmark_test.go to fix file descriptor leak
- Use ContinueOnError in flags_test.go to match ResetFlags behavior
- Add nil check for p.ui in processor_workers.go before UpdateProgress
- Fix resource_monitor_validation_test.go by setting hardMemoryLimitBytes directly

* chore(yaml): add missing document start markers

Add --- document start to YAML files to satisfy yamllint:
- .github/workflows/codeql.yml
- .github/workflows/build-test-publish.yml
- .github/workflows/security.yml
- .github/actions/setup/action.yml

* fix: guard nil resourceMonitor and fix test deadlock

- Guard resourceMonitor before CreateFileProcessingContext call
- Add ui.UpdateProgress on emergency stop and path error returns
- Fix potential deadlock in TestProcessFile using wg.Go with defer close
2025-12-10 19:07:11 +02:00

353 lines
9.1 KiB
Go

package testutil
import (
"errors"
"strings"
"testing"
"github.com/ivuorinen/gibidify/shared"
)
// TestAssertError tests the AssertError function.
func TestAssertError(t *testing.T) {
t.Run(
shared.TestCaseSuccessCases, func(t *testing.T) {
// Test expect error and get error
t.Run(
"expect error and get error", func(t *testing.T) {
AssertError(t, errors.New(shared.TestErrTestErrorMsg), true, shared.TestCaseTestOperation)
// If we get here, the assertion passed
},
)
// Test expect no error and get no error
t.Run(
"expect no error and get no error", func(t *testing.T) {
AssertError(t, nil, false, "successful operation")
// If we get here, the assertion passed
},
)
// Test with empty operation name
t.Run(
shared.TestCaseEmptyOperationName, func(t *testing.T) {
AssertError(t, nil, false, "")
// Should still work with empty operation
},
)
// Test with complex operation name
t.Run(
"complex operation name", func(t *testing.T) {
AssertError(t, nil, false, "complex operation with special chars: !@#$%^&*()")
// Should handle special characters
},
)
},
)
// Test edge cases
t.Run(
"edge cases", func(t *testing.T) {
// Test various error types
t.Run(
shared.TestCaseDifferentErrorTypes, func(t *testing.T) {
AssertError(t, shared.ErrTestError, true, "using shared.ErrTestError")
AssertError(t, errors.New("wrapped: original"), true, "wrapped error")
},
)
},
)
}
// TestAssertNoError tests the AssertNoError function.
func TestAssertNoError(t *testing.T) {
t.Run(
shared.TestCaseSuccessCases, func(t *testing.T) {
// Test with nil error
t.Run(
"nil error", func(t *testing.T) {
AssertNoError(t, nil, "successful operation")
},
)
// Test with empty operation name
t.Run(
shared.TestCaseEmptyOperationName, func(t *testing.T) {
AssertNoError(t, nil, "")
},
)
// Test with complex operation name
t.Run(
"complex operation", func(t *testing.T) {
AssertNoError(t, nil, "complex operation with special chars: !@#$%^&*()")
},
)
},
)
// We can't easily test the failure case in a unit test since it would cause test failure
// But we can verify the function signature and basic functionality
t.Run(
shared.TestCaseFunctionAvailability, func(t *testing.T) {
// Verify the function doesn't panic with valid inputs
defer func() {
if r := recover(); r != nil {
t.Errorf("AssertNoError should not panic: %v", r)
}
}()
// Call with success case to ensure function works
AssertNoError(t, nil, shared.TestCaseTestOperation)
},
)
}
// TestAssertExpectedError tests the AssertExpectedError function.
func TestAssertExpectedError(t *testing.T) {
t.Run(
shared.TestCaseSuccessCases, func(t *testing.T) {
// Test with error present
t.Run(
"error present as expected", func(t *testing.T) {
AssertExpectedError(t, errors.New("expected error"), "operation that should fail")
},
)
// Test with different error types
t.Run(
shared.TestCaseDifferentErrorTypes, func(t *testing.T) {
AssertExpectedError(t, shared.ErrTestError, "test error operation")
AssertExpectedError(t, errors.New("complex error with details"), "complex operation")
},
)
// Test with empty operation name
t.Run(
shared.TestCaseEmptyOperationName, func(t *testing.T) {
AssertExpectedError(t, errors.New("error"), "")
},
)
},
)
// Verify function availability and basic properties
t.Run(
shared.TestCaseFunctionAvailability, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("AssertExpectedError should not panic: %v", r)
}
}()
// Call with success case
AssertExpectedError(t, errors.New("test"), shared.TestCaseTestOperation)
},
)
}
// TestAssertErrorContains tests the AssertErrorContains function.
func TestAssertErrorContains(t *testing.T) {
t.Run(
shared.TestCaseSuccessCases, func(t *testing.T) {
// Test error contains substring
t.Run(
"error contains substring", func(t *testing.T) {
err := errors.New("database connection failed")
AssertErrorContains(t, err, "connection", "database operation")
},
)
// Test exact match
t.Run(
"exact match", func(t *testing.T) {
err := errors.New("exact error message")
AssertErrorContains(t, err, "exact error message", "exact operation")
},
)
// Test empty substring (should match any error)
t.Run(
"empty substring matches any error", func(t *testing.T) {
err := errors.New("any error")
AssertErrorContains(t, err, "", "any operation")
},
)
// Test special characters
t.Run(
"special characters in substring", func(t *testing.T) {
err := errors.New("error: failed with code 500")
AssertErrorContains(t, err, "code 500", "special chars operation")
},
)
// Test case sensitivity
t.Run(
"case sensitive operations", func(t *testing.T) {
err := errors.New("error Message")
AssertErrorContains(t, err, "error Message", "case operation")
},
)
},
)
// Test with various error types
t.Run(
shared.TestCaseDifferentErrorTypes, func(t *testing.T) {
t.Run(
"standard error", func(t *testing.T) {
AssertErrorContains(
t, shared.ErrTestError, shared.TestErrTestErrorMsg, shared.TestCaseTestOperation,
)
},
)
t.Run(
"wrapped error", func(t *testing.T) {
wrappedErr := errors.New("wrapped: original error")
AssertErrorContains(t, wrappedErr, "original", "wrapped operation")
},
)
},
)
// Verify function availability
t.Run(
shared.TestCaseFunctionAvailability, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("AssertErrorContains should not panic: %v", r)
}
}()
// Call with success case
err := errors.New(shared.TestErrTestErrorMsg)
AssertErrorContains(t, err, "test", "availability check")
},
)
}
// TestAssertionHelpers tests edge cases and combinations of assertion helpers.
func TestAssertionHelpers(t *testing.T) {
t.Run(
"error types compatibility", func(t *testing.T) {
// Test that all assertion functions work with shared.ErrTestError
AssertError(t, shared.ErrTestError, true, "shared.ErrTestError compatibility")
AssertExpectedError(t, shared.ErrTestError, "shared.ErrTestError expected")
AssertErrorContains(t, shared.ErrTestError, "test", "shared.ErrTestError contains")
},
)
t.Run(
"operation name handling", func(t *testing.T) {
operations := []string{
"",
"simple operation",
"operation with spaces",
"operation-with-dashes",
"operation_with_underscores",
"operation.with.dots",
"operation/with/slashes",
"operation\\with\\backslashes",
"operation with special chars: !@#$%^&*()",
"operation with unicode: αβγ",
strings.Repeat("very long operation name ", 10),
}
for i, op := range operations {
t.Run(
"operation_"+string(rune(i+'a')), func(t *testing.T) {
// Test each assertion function with this operation name
AssertError(t, nil, false, op)
AssertNoError(t, nil, op)
AssertExpectedError(t, errors.New("test"), op)
AssertErrorContains(t, errors.New(shared.TestErrTestErrorMsg), "test", op)
},
)
}
},
)
t.Run(
"error message variations", func(t *testing.T) {
errorMessages := []string{
"",
"simple error",
"error with spaces",
"error\nwith\nnewlines",
"error\twith\ttabs",
"error with unicode: αβγδε",
"error: with: colons: everywhere",
strings.Repeat("very long error message ", 20),
"error with special chars: !@#$%^&*()",
}
for i, msg := range errorMessages {
t.Run(
"error_message_"+string(rune(i+'a')), func(t *testing.T) {
err := errors.New(msg)
AssertError(t, err, true, shared.TestCaseMessageTest)
AssertExpectedError(t, err, shared.TestCaseMessageTest)
if msg != "" {
// Only test contains if message is not empty
AssertErrorContains(t, err, msg, shared.TestCaseMessageTest)
}
},
)
}
},
)
}
// BenchmarkStringOperations benchmarks string operations used by assertion functions.
func BenchmarkStringOperations(b *testing.B) {
testErr := errors.New("this is a long error message with many words for testing performance of substring matching")
errorMessage := testErr.Error()
substring := "error message"
b.Run(
"contains_operation", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = strings.Contains(errorMessage, substring)
}
},
)
b.Run(
"error_to_string", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = testErr.Error()
}
},
)
}
// BenchmarkAssertionLogic benchmarks the core logic of assertion functions.
func BenchmarkAssertionLogic(b *testing.B) {
testErr := errors.New("benchmark error")
b.Run(
"error_nil_check", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = testErr != nil
}
},
)
b.Run(
"boolean_comparison", func(b *testing.B) {
hasErr := testErr != nil
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = !hasErr
}
},
)
}