mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-02-13 02:49:15 +00:00
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
This commit is contained in:
@@ -5,10 +5,31 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ivuorinen/gibidify/shared"
|
||||
)
|
||||
|
||||
func TestCreateTestFile(t *testing.T) {
|
||||
tests := []struct {
|
||||
tests := createTestFileTestCases()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
runCreateTestFileTest(t, tt.dir, tt.filename, tt.content)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// createTestFileTestCases creates test cases for TestCreateTestFile.
|
||||
func createTestFileTestCases() []struct {
|
||||
name string
|
||||
dir string
|
||||
filename string
|
||||
content []byte
|
||||
wantErr bool
|
||||
} {
|
||||
return []struct {
|
||||
name string
|
||||
dir string
|
||||
filename string
|
||||
@@ -42,55 +63,88 @@ func TestCreateTestFile(t *testing.T) {
|
||||
{
|
||||
name: "create file with special characters",
|
||||
filename: "special-file_123.go",
|
||||
content: []byte("package main"),
|
||||
content: []byte(shared.LiteralPackageMain),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Use a temporary directory for each test
|
||||
tempDir := t.TempDir()
|
||||
if tt.dir == "" {
|
||||
tt.dir = tempDir
|
||||
}
|
||||
// runCreateTestFileTest runs a single test case for CreateTestFile.
|
||||
func runCreateTestFileTest(t *testing.T, dir, filename string, content []byte) {
|
||||
t.Helper()
|
||||
|
||||
// Create subdirectory if needed
|
||||
if strings.Contains(tt.filename, "/") {
|
||||
subdir := filepath.Join(tt.dir, filepath.Dir(tt.filename))
|
||||
if err := os.MkdirAll(subdir, DirPermission); err != nil {
|
||||
t.Fatalf("Failed to create subdirectory: %v", err)
|
||||
}
|
||||
}
|
||||
tempDir := t.TempDir()
|
||||
if dir == "" {
|
||||
dir = tempDir
|
||||
}
|
||||
|
||||
// Test CreateTestFile
|
||||
filePath := CreateTestFile(t, tt.dir, tt.filename, tt.content)
|
||||
createSubdirectoryIfNeeded(t, dir, filename)
|
||||
filePath := CreateTestFile(t, dir, filename, content)
|
||||
verifyCreatedFile(t, filePath, content)
|
||||
}
|
||||
|
||||
// Verify file exists
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Created file does not exist: %v", err)
|
||||
}
|
||||
// createSubdirectoryIfNeeded creates subdirectory if the filename contains a path separator.
|
||||
func createSubdirectoryIfNeeded(t *testing.T, dir, filename string) {
|
||||
t.Helper()
|
||||
|
||||
// Verify it's a regular file
|
||||
if !info.Mode().IsRegular() {
|
||||
t.Errorf("Created path is not a regular file")
|
||||
}
|
||||
if strings.ContainsRune(filename, filepath.Separator) {
|
||||
subdir := filepath.Join(dir, filepath.Dir(filename))
|
||||
if err := os.MkdirAll(subdir, shared.TestDirPermission); err != nil {
|
||||
t.Fatalf("Failed to create subdirectory: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify permissions
|
||||
if info.Mode().Perm() != FilePermission {
|
||||
t.Errorf("File permissions = %v, want %v", info.Mode().Perm(), FilePermission)
|
||||
}
|
||||
// verifyCreatedFile verifies that the created file has correct properties.
|
||||
func verifyCreatedFile(t *testing.T, filePath string, expectedContent []byte) {
|
||||
t.Helper()
|
||||
|
||||
// Verify content
|
||||
readContent, err := os.ReadFile(filePath) // #nosec G304 - test file path is controlled
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read created file: %v", err)
|
||||
}
|
||||
if string(readContent) != string(tt.content) {
|
||||
t.Errorf("File content = %q, want %q", readContent, tt.content)
|
||||
}
|
||||
})
|
||||
info := verifyFileExists(t, filePath)
|
||||
verifyFileType(t, info)
|
||||
verifyFilePermissions(t, info)
|
||||
verifyFileContent(t, filePath, expectedContent)
|
||||
}
|
||||
|
||||
// verifyFileExists verifies that the file exists and returns its info.
|
||||
func verifyFileExists(t *testing.T, filePath string) os.FileInfo {
|
||||
t.Helper()
|
||||
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Created file does not exist: %v", err)
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
// verifyFileType verifies that the file is a regular file.
|
||||
func verifyFileType(t *testing.T, info os.FileInfo) {
|
||||
t.Helper()
|
||||
|
||||
if !info.Mode().IsRegular() {
|
||||
t.Error("Created path is not a regular file")
|
||||
}
|
||||
}
|
||||
|
||||
// verifyFilePermissions verifies that the file has correct permissions.
|
||||
func verifyFilePermissions(t *testing.T, info os.FileInfo) {
|
||||
t.Helper()
|
||||
|
||||
if info.Mode().Perm() != shared.TestFilePermission {
|
||||
t.Errorf("File permissions = %v, want %v", info.Mode().Perm(), shared.TestFilePermission)
|
||||
}
|
||||
}
|
||||
|
||||
// verifyFileContent verifies that the file has the expected content.
|
||||
func verifyFileContent(t *testing.T, filePath string, expectedContent []byte) {
|
||||
t.Helper()
|
||||
|
||||
readContent, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read created file: %v", err)
|
||||
}
|
||||
if string(readContent) != string(expectedContent) {
|
||||
t.Errorf("File content = %q, want %q", readContent, expectedContent)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,37 +172,56 @@ func TestCreateTempOutputFile(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
file, path := CreateTempOutputFile(t, tt.pattern)
|
||||
defer CloseFile(t, file)
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
file, path := CreateTempOutputFile(t, tt.pattern)
|
||||
defer CloseFile(t, file)
|
||||
|
||||
// Verify file exists
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Temp file does not exist: %v", err)
|
||||
}
|
||||
// Verify file exists
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Temp file does not exist: %v", err)
|
||||
}
|
||||
|
||||
// Verify it's a regular file
|
||||
if !info.Mode().IsRegular() {
|
||||
t.Errorf("Created path is not a regular file")
|
||||
}
|
||||
// Verify it's a regular file
|
||||
if !info.Mode().IsRegular() {
|
||||
t.Error("Created path is not a regular file")
|
||||
}
|
||||
|
||||
// Verify we can write to it
|
||||
testContent := []byte("test content")
|
||||
if _, err := file.Write(testContent); err != nil {
|
||||
t.Errorf("Failed to write to temp file: %v", err)
|
||||
}
|
||||
// Verify we can write to it
|
||||
testContent := []byte("test content")
|
||||
if _, err := file.Write(testContent); err != nil {
|
||||
t.Errorf("Failed to write to temp file: %v", err)
|
||||
}
|
||||
|
||||
// Verify the path is in a temp directory (any temp directory)
|
||||
if !strings.Contains(path, os.TempDir()) {
|
||||
t.Errorf("Temp file not in temp directory: %s", path)
|
||||
}
|
||||
})
|
||||
// Verify the path is in a temp directory (any temp directory)
|
||||
if !strings.Contains(path, os.TempDir()) {
|
||||
t.Errorf("Temp file not in temp directory: %s", path)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateTestDirectory(t *testing.T) {
|
||||
tests := []struct {
|
||||
tests := createTestDirectoryTestCases()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
runCreateTestDirectoryTest(t, tt.parent, tt.dir)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// createTestDirectoryTestCases creates test cases for TestCreateTestDirectory.
|
||||
func createTestDirectoryTestCases() []struct {
|
||||
name string
|
||||
parent string
|
||||
dir string
|
||||
} {
|
||||
return []struct {
|
||||
name string
|
||||
parent string
|
||||
dir string
|
||||
@@ -166,53 +239,107 @@ func TestCreateTestDirectory(t *testing.T) {
|
||||
dir: "nested/dir",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
if tt.parent == "" {
|
||||
tt.parent = tempDir
|
||||
}
|
||||
// runCreateTestDirectoryTest runs a single test case for CreateTestDirectory.
|
||||
func runCreateTestDirectoryTest(t *testing.T, parent, dir string) {
|
||||
t.Helper()
|
||||
|
||||
// For nested directories, create parent first
|
||||
if strings.Contains(tt.dir, "/") {
|
||||
parentPath := filepath.Join(tt.parent, filepath.Dir(tt.dir))
|
||||
if err := os.MkdirAll(parentPath, DirPermission); err != nil {
|
||||
t.Fatalf("Failed to create parent directory: %v", err)
|
||||
}
|
||||
tt.dir = filepath.Base(tt.dir)
|
||||
tt.parent = parentPath
|
||||
}
|
||||
tempDir := t.TempDir()
|
||||
if parent == "" {
|
||||
parent = tempDir
|
||||
}
|
||||
|
||||
dirPath := CreateTestDirectory(t, tt.parent, tt.dir)
|
||||
parent, dir = prepareNestedDirectoryPath(t, parent, dir)
|
||||
dirPath := CreateTestDirectory(t, parent, dir)
|
||||
verifyCreatedDirectory(t, dirPath)
|
||||
}
|
||||
|
||||
// Verify directory exists
|
||||
info, err := os.Stat(dirPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Created directory does not exist: %v", err)
|
||||
}
|
||||
// prepareNestedDirectoryPath prepares parent and directory paths for nested directories.
|
||||
func prepareNestedDirectoryPath(t *testing.T, parent, dir string) (parentPath, fullPath string) {
|
||||
t.Helper()
|
||||
|
||||
// Verify it's a directory
|
||||
if !info.IsDir() {
|
||||
t.Errorf("Created path is not a directory")
|
||||
}
|
||||
if strings.Contains(dir, "/") {
|
||||
parentPath := filepath.Join(parent, filepath.Dir(dir))
|
||||
if err := os.MkdirAll(parentPath, shared.TestDirPermission); err != nil {
|
||||
t.Fatalf("Failed to create parent directory: %v", err)
|
||||
}
|
||||
|
||||
// Verify permissions
|
||||
if info.Mode().Perm() != DirPermission {
|
||||
t.Errorf("Directory permissions = %v, want %v", info.Mode().Perm(), DirPermission)
|
||||
}
|
||||
return parentPath, filepath.Base(dir)
|
||||
}
|
||||
|
||||
// Verify we can create files in it
|
||||
testFile := filepath.Join(dirPath, "test.txt")
|
||||
if err := os.WriteFile(testFile, []byte("test"), FilePermission); err != nil {
|
||||
t.Errorf("Cannot create file in directory: %v", err)
|
||||
}
|
||||
})
|
||||
return parent, dir
|
||||
}
|
||||
|
||||
// verifyCreatedDirectory verifies that the created directory has correct properties.
|
||||
func verifyCreatedDirectory(t *testing.T, dirPath string) {
|
||||
t.Helper()
|
||||
|
||||
info := verifyDirectoryExists(t, dirPath)
|
||||
verifyIsDirectory(t, info)
|
||||
verifyDirectoryPermissions(t, info)
|
||||
verifyDirectoryUsability(t, dirPath)
|
||||
}
|
||||
|
||||
// verifyDirectoryExists verifies that the directory exists and returns its info.
|
||||
func verifyDirectoryExists(t *testing.T, dirPath string) os.FileInfo {
|
||||
t.Helper()
|
||||
|
||||
info, err := os.Stat(dirPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Created directory does not exist: %v", err)
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
// verifyIsDirectory verifies that the path is a directory.
|
||||
func verifyIsDirectory(t *testing.T, info os.FileInfo) {
|
||||
t.Helper()
|
||||
|
||||
if !info.IsDir() {
|
||||
t.Error("Created path is not a directory")
|
||||
}
|
||||
}
|
||||
|
||||
// verifyDirectoryPermissions verifies that the directory has correct permissions.
|
||||
func verifyDirectoryPermissions(t *testing.T, info os.FileInfo) {
|
||||
t.Helper()
|
||||
|
||||
if info.Mode().Perm() != shared.TestDirPermission {
|
||||
t.Errorf("Directory permissions = %v, want %v", info.Mode().Perm(), shared.TestDirPermission)
|
||||
}
|
||||
}
|
||||
|
||||
// verifyDirectoryUsability verifies that files can be created in the directory.
|
||||
func verifyDirectoryUsability(t *testing.T, dirPath string) {
|
||||
t.Helper()
|
||||
|
||||
testFile := filepath.Join(dirPath, "test.txt")
|
||||
if err := os.WriteFile(testFile, []byte("test"), shared.TestFilePermission); err != nil {
|
||||
t.Errorf("Cannot create file in directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateTestFiles(t *testing.T) {
|
||||
tests := []struct {
|
||||
tests := createTestFilesTestCases()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
runTestFilesTest(t, tt.fileSpecs, tt.wantCount)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// createTestFilesTestCases creates test cases for TestCreateTestFiles.
|
||||
func createTestFilesTestCases() []struct {
|
||||
name string
|
||||
fileSpecs []FileSpec
|
||||
wantCount int
|
||||
} {
|
||||
return []struct {
|
||||
name string
|
||||
fileSpecs []FileSpec
|
||||
wantCount int
|
||||
@@ -221,7 +348,7 @@ func TestCreateTestFiles(t *testing.T) {
|
||||
name: "create multiple files",
|
||||
fileSpecs: []FileSpec{
|
||||
{Name: "file1.txt", Content: "content1"},
|
||||
{Name: "file2.go", Content: "package main"},
|
||||
{Name: "file2.go", Content: shared.LiteralPackageMain},
|
||||
{Name: "file3.json", Content: `{"key": "value"}`},
|
||||
},
|
||||
wantCount: 3,
|
||||
@@ -229,7 +356,7 @@ func TestCreateTestFiles(t *testing.T) {
|
||||
{
|
||||
name: "create files with subdirectories",
|
||||
fileSpecs: []FileSpec{
|
||||
{Name: "src/main.go", Content: "package main"},
|
||||
{Name: "src/main.go", Content: shared.LiteralPackageMain},
|
||||
{Name: "test/test.go", Content: "package test"},
|
||||
},
|
||||
wantCount: 2,
|
||||
@@ -248,39 +375,56 @@ func TestCreateTestFiles(t *testing.T) {
|
||||
wantCount: 2,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rootDir := t.TempDir()
|
||||
// runTestFilesTest runs a single test case for CreateTestFiles.
|
||||
func runTestFilesTest(t *testing.T, fileSpecs []FileSpec, wantCount int) {
|
||||
t.Helper()
|
||||
|
||||
// Create necessary subdirectories
|
||||
for _, spec := range tt.fileSpecs {
|
||||
if strings.Contains(spec.Name, "/") {
|
||||
subdir := filepath.Join(rootDir, filepath.Dir(spec.Name))
|
||||
if err := os.MkdirAll(subdir, DirPermission); err != nil {
|
||||
t.Fatalf("Failed to create subdirectory: %v", err)
|
||||
}
|
||||
}
|
||||
rootDir := t.TempDir()
|
||||
|
||||
createNecessarySubdirectories(t, rootDir, fileSpecs)
|
||||
createdFiles := CreateTestFiles(t, rootDir, fileSpecs)
|
||||
verifyCreatedFilesCount(t, createdFiles, wantCount)
|
||||
verifyCreatedFilesContent(t, createdFiles, fileSpecs)
|
||||
}
|
||||
|
||||
// createNecessarySubdirectories creates subdirectories for file specs that need them.
|
||||
func createNecessarySubdirectories(t *testing.T, rootDir string, fileSpecs []FileSpec) {
|
||||
t.Helper()
|
||||
|
||||
for _, spec := range fileSpecs {
|
||||
if strings.Contains(spec.Name, "/") {
|
||||
subdir := filepath.Join(rootDir, filepath.Dir(spec.Name))
|
||||
if err := os.MkdirAll(subdir, shared.TestDirPermission); err != nil {
|
||||
t.Fatalf("Failed to create subdirectory: %v", err)
|
||||
}
|
||||
|
||||
createdFiles := CreateTestFiles(t, rootDir, tt.fileSpecs)
|
||||
|
||||
// Verify count
|
||||
if len(createdFiles) != tt.wantCount {
|
||||
t.Errorf("Created %d files, want %d", len(createdFiles), tt.wantCount)
|
||||
}
|
||||
|
||||
// Verify each file
|
||||
for i, filePath := range createdFiles {
|
||||
content, err := os.ReadFile(filePath) // #nosec G304 - test file path is controlled
|
||||
if err != nil {
|
||||
t.Errorf("Failed to read file %s: %v", filePath, err)
|
||||
continue
|
||||
}
|
||||
if string(content) != tt.fileSpecs[i].Content {
|
||||
t.Errorf("File %s content = %q, want %q", filePath, content, tt.fileSpecs[i].Content)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verifyCreatedFilesCount verifies the count of created files.
|
||||
func verifyCreatedFilesCount(t *testing.T, createdFiles []string, wantCount int) {
|
||||
t.Helper()
|
||||
|
||||
if len(createdFiles) != wantCount {
|
||||
t.Errorf("Created %d files, want %d", len(createdFiles), wantCount)
|
||||
}
|
||||
}
|
||||
|
||||
// verifyCreatedFilesContent verifies the content of created files.
|
||||
func verifyCreatedFilesContent(t *testing.T, createdFiles []string, fileSpecs []FileSpec) {
|
||||
t.Helper()
|
||||
|
||||
for i, filePath := range createdFiles {
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to read file %s: %v", filePath, err)
|
||||
|
||||
continue
|
||||
}
|
||||
if string(content) != fileSpecs[i].Content {
|
||||
t.Errorf("File %s content = %q, want %q", filePath, content, fileSpecs[i].Content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user