Files
gibidify/testutil/file_creation_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

431 lines
10 KiB
Go

package testutil
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/ivuorinen/gibidify/shared"
)
func TestCreateTestFile(t *testing.T) {
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
content []byte
wantErr bool
}{
{
name: "create simple test file",
filename: "test.txt",
content: []byte("hello world"),
wantErr: false,
},
{
name: "create file with empty content",
filename: "empty.txt",
content: []byte{},
wantErr: false,
},
{
name: "create file with binary content",
filename: "binary.bin",
content: []byte{0x00, 0xFF, 0x42},
wantErr: false,
},
{
name: "create file with subdirectory",
filename: "subdir/test.txt",
content: []byte("nested file"),
wantErr: false,
},
{
name: "create file with special characters",
filename: "special-file_123.go",
content: []byte(shared.LiteralPackageMain),
wantErr: false,
},
}
}
// runCreateTestFileTest runs a single test case for CreateTestFile.
func runCreateTestFileTest(t *testing.T, dir, filename string, content []byte) {
t.Helper()
tempDir := t.TempDir()
if dir == "" {
dir = tempDir
}
createSubdirectoryIfNeeded(t, dir, filename)
filePath := CreateTestFile(t, dir, filename, content)
verifyCreatedFile(t, filePath, content)
}
// createSubdirectoryIfNeeded creates subdirectory if the filename contains a path separator.
func createSubdirectoryIfNeeded(t *testing.T, dir, filename string) {
t.Helper()
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)
}
}
}
// verifyCreatedFile verifies that the created file has correct properties.
func verifyCreatedFile(t *testing.T, filePath string, expectedContent []byte) {
t.Helper()
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)
}
}
func TestCreateTempOutputFile(t *testing.T) {
tests := []struct {
name string
pattern string
}{
{
name: "simple pattern",
pattern: "output-*.txt",
},
{
name: "pattern with prefix only",
pattern: "test-",
},
{
name: "pattern with suffix only",
pattern: "*.json",
},
{
name: "empty pattern",
pattern: "",
},
}
for _, tt := range tests {
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 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 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 := 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
}{
{
name: "simple directory",
dir: "testdir",
},
{
name: "directory with special characters",
dir: "test-dir_123",
},
{
name: "nested directory name",
dir: "nested/dir",
},
}
}
// runCreateTestDirectoryTest runs a single test case for CreateTestDirectory.
func runCreateTestDirectoryTest(t *testing.T, parent, dir string) {
t.Helper()
tempDir := t.TempDir()
if parent == "" {
parent = tempDir
}
parent, dir = prepareNestedDirectoryPath(t, parent, dir)
dirPath := CreateTestDirectory(t, parent, dir)
verifyCreatedDirectory(t, dirPath)
}
// prepareNestedDirectoryPath prepares parent and directory paths for nested directories.
func prepareNestedDirectoryPath(t *testing.T, parent, dir string) (parentPath, fullPath string) {
t.Helper()
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)
}
return parentPath, filepath.Base(dir)
}
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 := 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
}{
{
name: "create multiple files",
fileSpecs: []FileSpec{
{Name: "file1.txt", Content: "content1"},
{Name: "file2.go", Content: shared.LiteralPackageMain},
{Name: "file3.json", Content: `{"key": "value"}`},
},
wantCount: 3,
},
{
name: "create files with subdirectories",
fileSpecs: []FileSpec{
{Name: "src/main.go", Content: shared.LiteralPackageMain},
{Name: "test/test.go", Content: "package test"},
},
wantCount: 2,
},
{
name: "empty file specs",
fileSpecs: []FileSpec{},
wantCount: 0,
},
{
name: "files with empty content",
fileSpecs: []FileSpec{
{Name: "empty1.txt", Content: ""},
{Name: "empty2.txt", Content: ""},
},
wantCount: 2,
},
}
}
// runTestFilesTest runs a single test case for CreateTestFiles.
func runTestFilesTest(t *testing.T, fileSpecs []FileSpec, wantCount int) {
t.Helper()
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)
}
}
}
}
// 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)
}
}
}