From 4b8d66c77826d3a98a1611c1a9f576279cb3feb9 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 23 Mar 2025 19:41:39 +0200 Subject: [PATCH] feat(tests): more tests and ci action (#14) * feat(tests): more tests and ci action * fix(ci): coverage and pr-lint * fix(ci): renovate rules, permissions, linting, actions * fix(lint): editorconfig fixes * fix(lint): kics.config * fix(lint): formatting, permissions, pre-commit config * chore(ci): set workflow to use go 1.23, go mod tidy * chore(ci): fixes and stuff * chore(ci): disable GO_GOLANGCI_LINT * chore(ci): pinning, permissions --- .editorconfig | 14 +++ .github/renovate.json | 8 +- .github/workflows/ci.yml | 41 +++++-- .github/workflows/pr-lint.yml | 29 +++++ .github/workflows/sync-labels.yml | 25 ++++ .github/workflows/test-coverage-sarif.yml | 68 ++++++++++ .gitignore | 2 + .go-version | 1 + .mega-linter.yml | 20 +++ .pre-commit-config.yaml | 16 +++ .vscode/extensions.json | 3 + Dockerfile | 8 +- LICENSE | 19 ++- config/config_test.go | 82 +++++++++++++ fileproc/walker_test.go | 143 ++++++++++++++++++++++ fileproc/writer.go | 9 +- fileproc/writer_test.go | 138 ++++++++++++++++----- go.mod | 21 +--- go.sum | 83 +++---------- kics.config | 4 + main.go | 117 ++++++++++-------- integration_test.go => main_test.go | 26 ++-- 22 files changed, 680 insertions(+), 197 deletions(-) create mode 100644 .github/workflows/pr-lint.yml create mode 100644 .github/workflows/sync-labels.yml create mode 100644 .github/workflows/test-coverage-sarif.yml create mode 100644 .go-version create mode 100644 .mega-linter.yml create mode 100644 .pre-commit-config.yaml create mode 100644 .vscode/extensions.json create mode 100644 config/config_test.go create mode 100644 fileproc/walker_test.go create mode 100644 kics.config rename integration_test.go => main_test.go (80%) diff --git a/.editorconfig b/.editorconfig index 86d1176..82c5a83 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,5 +8,19 @@ indent_size = 2 indent_style = tab tab_width = 2 +[*.yml] +indent_style = space + [*.md] trim_trailing_whitespace = false + +[*.{yml,yaml,json}] +indent_style = space +max_line_length = 250 + +[LICENSE] +max_line_length = 80 +indent_size = 0 +indent_style = space +trim_trailing_whitespace = true + diff --git a/.github/renovate.json b/.github/renovate.json index f02f654..853fa79 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -1,6 +1,10 @@ { "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": [ - "github>ivuorinen/renovate-config" + "extends": ["github>ivuorinen/renovate-config"], + "packageRules": [ + { + "managers": ["github-actions"], + "schedule": ["daily"] + } ] } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65eae8c..1c35f1a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,13 +2,14 @@ # yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json name: Build and Publish +# yamllint disable-line rule:truthy on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] release: - types: [ created ] + types: [created] permissions: read-all @@ -16,17 +17,30 @@ jobs: build: name: Build Binaries runs-on: ubuntu-latest + + permissions: + contents: write + packages: write + actions: write + strategy: matrix: - goos: [ "linux", "darwin" ] + goos: ["linux", "darwin"] + steps: - name: Checkout repository - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + token: ${{ secrets.GITHUB_TOKEN }} - name: Set up Go - uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5 + uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0 with: - go-version: '1.24' + go-version-file: "./go.mod" + + - name: Run go mod tidy + shell: bash + run: go mod tidy - name: Build binary for ${{ matrix.goos }} shell: bash @@ -37,22 +51,27 @@ jobs: . - name: Upload artifact for ${{ matrix.goos }} - uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4 + uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 with: name: gibidify-${{ matrix.goos }} path: gibidify-${{ matrix.goos }} docker: name: Build and Publish Docker Image + if: github.event_name == 'release' needs: build runs-on: ubuntu-latest - if: github.event_name == 'release' + + permissions: + packages: write + actions: write + steps: - name: Checkout repository - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - name: Download Linux binary artifact - uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4 + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 with: name: gibidify-linux path: . diff --git a/.github/workflows/pr-lint.yml b/.github/workflows/pr-lint.yml new file mode 100644 index 0000000..69b52d4 --- /dev/null +++ b/.github/workflows/pr-lint.yml @@ -0,0 +1,29 @@ +--- +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json +name: PR Lint + +# yamllint disable-line rule:truthy +on: + push: + branches: [master, main] + pull_request: + branches: [master, main] + +permissions: read-all + +env: + TRIVY_SEVERITY: CRITICAL,HIGH + DISABLE_LINTERS: GO_GOLANGCI_LINT + +jobs: + Linter: + name: PR Lint + runs-on: ubuntu-latest + permissions: + contents: write # only for delete-branch option + issues: write + pull-requests: write + statuses: write + + steps: + - uses: ivuorinen/actions/pr-lint@eb085adfe2779a1c52bfe1b2d0945b6c4241f54e # 25.3.19 diff --git a/.github/workflows/sync-labels.yml b/.github/workflows/sync-labels.yml new file mode 100644 index 0000000..4ae2f89 --- /dev/null +++ b/.github/workflows/sync-labels.yml @@ -0,0 +1,25 @@ +--- +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json +name: Sync labels + +permissions: read-all + +# yamllint disable-line rule:truthy +on: + push: + paths: + - .github/workflows/sync-labels.yml + - .github/labels.yml + schedule: + - cron: "34 5 * * *" + workflow_call: + workflow_dispatch: + +jobs: + SyncLabels: + permissions: + contents: read + issues: write + runs-on: ubuntu-latest + steps: + - uses: ivuorinen/actions/sync-labels@eb085adfe2779a1c52bfe1b2d0945b6c4241f54e # 25.3.19 diff --git a/.github/workflows/test-coverage-sarif.yml b/.github/workflows/test-coverage-sarif.yml new file mode 100644 index 0000000..625ead0 --- /dev/null +++ b/.github/workflows/test-coverage-sarif.yml @@ -0,0 +1,68 @@ +--- +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json +name: Go Tests with Coverage to SARIF + +# yamllint disable-line rule:truthy +on: + push: + branches: [main] + pull_request: + branches: [main] + +permissions: read-all + +jobs: + test: + runs-on: ubuntu-latest + + permissions: + contents: write + checks: write + pull-requests: write + security-events: write + statuses: write + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + repository: ivuorinen/gibidify + ref: ${{ github.event.pull_request.head.ref }} + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Go + uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.0 + with: + go-version-file: "./go.mod" + + - name: Install dependencies + shell: bash + run: go mod tidy + + - name: Run tests + shell: bash + run: go test -v ./... + + - name: Generate coverage report + shell: bash + run: go test -coverprofile=coverage.out ./... + + - name: Check coverage + id: coverage + shell: bash + run: | + coverage=$(go tool cover -func=coverage.out | grep total | awk '{print substr($3, 1, length($3)-1)}') + echo "total_coverage=$coverage" >> "$GITHUB_OUTPUT" + echo "Coverage: $coverage%" + + - name: Cleanup + shell: bash + run: rm coverage.out + + - name: Fail if coverage is below threshold + shell: bash + run: | + if (( $(echo "$total_coverage < 50" | bc -l) )); then + echo "Coverage ($total_coverage%) is below the threshold (50%)" + exit 1 + fi diff --git a/.gitignore b/.gitignore index 03d459f..9757884 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ gibidify.yaml output.json output.txt output.yaml +coverage.out +megalinter-reports/* diff --git a/.go-version b/.go-version new file mode 100644 index 0000000..a6c2798 --- /dev/null +++ b/.go-version @@ -0,0 +1 @@ +1.23.0 diff --git a/.mega-linter.yml b/.mega-linter.yml new file mode 100644 index 0000000..2447b22 --- /dev/null +++ b/.mega-linter.yml @@ -0,0 +1,20 @@ +--- +# Configuration file for MegaLinter +# See all available variables at +# https://megalinter.io/configuration/ and in linters documentation + +APPLY_FIXES: all +SHOW_ELAPSED_TIME: false # Show elapsed time at the end of MegaLinter run +PARALLEL: true +VALIDATE_ALL_CODEBASE: true +FILEIO_REPORTER: false # Generate file.io report +GITHUB_STATUS_REPORTER: true # Generate GitHub status report +IGNORE_GENERATED_FILES: true # Ignore generated files +JAVASCRIPT_DEFAULT_STYLE: prettier # Default style for JavaScript +PRINT_ALPACA: false # Print Alpaca logo in console +SARIF_REPORTER: true # Generate SARIF report +SHOW_SKIPPED_LINTERS: false # Show skipped linters in MegaLinter log + +DISABLE_LINTERS: + - REPOSITORY_DEVSKIM + - REPOSITORY_TRIVY diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..84e9943 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,16 @@ +repos: + - repo: https://github.com/oxsecurity/megalinter + rev: v8.4.2 # Git tag specifying the hook, not mega-linter-runner, version + hooks: + - id: megalinter-incremental # Faster, less thorough + stages: + - pre-commit + - repo: https://github.com/tekwizely/pre-commit-golang + rev: v1.0.0-rc.1 + hooks: + - id: go-build-mod + alias: build + - id: go-mod-tidy + alias: tidy + - id: go-fmt + alias: fmt diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..21cb56c --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["esbenp.prettier-vscode", "AquaSecurityOfficial.trivy-vulnerability-scanner", "Bridgecrew.checkov", "exiasr.hadolint", "ms-vscode.Go", "streetsidesoftware.code-spell-checker"] +} diff --git a/Dockerfile b/Dockerfile index e272916..a34fe91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,11 @@ # Use a minimal base image -FROM alpine:latest +FROM alpine:3.21.2 + +# Add user +RUN useradd -ms /bin/bash gibidify + +# Use the new user +USER gibidify # Copy the gibidify binary into the container COPY gibidify /usr/local/bin/gibidify diff --git a/LICENSE b/LICENSE index bea2d15..6b210e7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,20 @@ MIT License Copyright (c) 2025 Ismo Vuorinen -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: -The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..0d7bd13 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,82 @@ +package config + +import ( + "os" + "path/filepath" + "testing" + + "github.com/spf13/viper" +) + +// TestDefaultConfig verifies that if no config file is found, +// the default configuration values are correctly set. +func TestDefaultConfig(t *testing.T) { + // Create a temporary directory to ensure no config file is present. + tmpDir, err := os.MkdirTemp("", "gibidify_config_test_default") + if err != nil { + t.Fatalf("Failed to create temp directory: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Point Viper to the temp directory with no config file. + originalConfigPaths := viper.ConfigFileUsed() + viper.Reset() + viper.AddConfigPath(tmpDir) + LoadConfig() + + // Check defaults + defaultSizeLimit := GetFileSizeLimit() + if defaultSizeLimit != 5242880 { + t.Errorf("Expected default file size limit of 5242880, got %d", defaultSizeLimit) + } + + ignoredDirs := GetIgnoredDirectories() + if len(ignoredDirs) == 0 { + t.Errorf("Expected some default ignored directories, got none") + } + + // Restore Viper state + viper.SetConfigFile(originalConfigPaths) +} + +// TestLoadConfigFile verifies that when a valid config file is present, +// viper loads the specified values correctly. +func TestLoadConfigFile(t *testing.T) { + tmpDir, err := os.MkdirTemp("", "gibidify_config_test_file") + if err != nil { + t.Fatalf("Failed to create temp directory: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Prepare a minimal config file + configContent := []byte(`--- +fileSizeLimit: 123456 +ignoreDirectories: +- "testdir1" +- "testdir2" +`) + + configPath := filepath.Join(tmpDir, "config.yaml") + if err := os.WriteFile(configPath, configContent, 0644); err != nil { + t.Fatalf("Failed to write config file: %v", err) + } + + // Reset viper and point to the new config path + viper.Reset() + viper.AddConfigPath(tmpDir) + + // Force Viper to read our config file + if err := viper.ReadInConfig(); err != nil { + t.Fatalf("Could not read config file: %v", err) + } + + // Validate loaded data + if got := viper.GetInt64("fileSizeLimit"); got != 123456 { + t.Errorf("Expected fileSizeLimit=123456, got %d", got) + } + + ignored := viper.GetStringSlice("ignoreDirectories") + if len(ignored) != 2 || ignored[0] != "testdir1" || ignored[1] != "testdir2" { + t.Errorf("Expected [\"testdir1\", \"testdir2\"], got %v", ignored) + } +} diff --git a/fileproc/walker_test.go b/fileproc/walker_test.go new file mode 100644 index 0000000..7e5b666 --- /dev/null +++ b/fileproc/walker_test.go @@ -0,0 +1,143 @@ +package fileproc + +import ( + "os" + "path/filepath" + "testing" + + "github.com/ivuorinen/gibidify/config" + "github.com/spf13/viper" +) + +func TestProdWalkerWithIgnore(t *testing.T) { + // Create a temporary directory structure. + rootDir, err := os.MkdirTemp("", "walker_test_root") + if err != nil { + t.Fatalf("Failed to create temp root directory: %v", err) + } + defer os.RemoveAll(rootDir) + + subDir := filepath.Join(rootDir, "vendor") + if err := os.Mkdir(subDir, 0755); err != nil { + t.Fatalf("Failed to create subDir: %v", err) + } + + // Write sample files + filePaths := []string{ + filepath.Join(rootDir, "file1.go"), + filepath.Join(rootDir, "file2.txt"), + filepath.Join(subDir, "file_in_vendor.txt"), // should be ignored + } + for _, fp := range filePaths { + if err := os.WriteFile(fp, []byte("content"), 0644); err != nil { + t.Fatalf("Failed to write file %s: %v", fp, err) + } + } + + // .gitignore that ignores *.txt and itself + gitignoreContent := `*.txt +.gitignore +` + gitignorePath := filepath.Join(rootDir, ".gitignore") + if err := os.WriteFile(gitignorePath, []byte(gitignoreContent), 0644); err != nil { + t.Fatalf("Failed to write .gitignore: %v", err) + } + + // Initialize config to ignore "vendor" directory + viper.Reset() + config.LoadConfig() + viper.Set("ignoreDirectories", []string{"vendor"}) + + // Run walker + var w Walker = ProdWalker{} + found, err := w.Walk(rootDir) + if err != nil { + t.Fatalf("Walk returned error: %v", err) + } + + // We expect only file1.go to appear + if len(found) != 1 { + t.Errorf("Expected 1 file to pass filters, got %d: %v", len(found), found) + } + if len(found) == 1 && filepath.Base(found[0]) != "file1.go" { + t.Errorf("Expected file1.go, got %s", found[0]) + } +} + +func TestProdWalkerBinaryCheck(t *testing.T) { + rootDir, err := os.MkdirTemp("", "walker_test_bincheck") + if err != nil { + t.Fatalf("Failed to create temp root directory: %v", err) + } + defer os.RemoveAll(rootDir) + + // Create a mock binary file + binFile := filepath.Join(rootDir, "somefile.exe") + if err := os.WriteFile(binFile, []byte("fake-binary-content"), 0644); err != nil { + t.Fatalf("Failed to write file %s: %v", binFile, err) + } + + // Create a normal file + normalFile := filepath.Join(rootDir, "keep.go") + if err := os.WriteFile(normalFile, []byte("package main"), 0644); err != nil { + t.Fatalf("Failed to write file %s: %v", normalFile, err) + } + + // Reset and load default config + viper.Reset() + config.LoadConfig() + + // Run walker + var w Walker = ProdWalker{} + found, err := w.Walk(rootDir) + if err != nil { + t.Fatalf("Walk returned error: %v", err) + } + + // Only "keep.go" should be returned + if len(found) != 1 { + t.Errorf("Expected 1 file, got %d: %v", len(found), found) + } + if len(found) == 1 && filepath.Base(found[0]) != "keep.go" { + t.Errorf("Expected keep.go in results, got %s", found[0]) + } +} + +func TestProdWalkerSizeLimit(t *testing.T) { + rootDir, err := os.MkdirTemp("", "walker_test_sizelimit") + if err != nil { + t.Fatalf("Failed to create temp root directory: %v", err) + } + defer os.RemoveAll(rootDir) + + // Create a file exceeding the size limit + largeFilePath := filepath.Join(rootDir, "largefile.txt") + largeFileData := make([]byte, 6*1024*1024) // 6 MB + if err := os.WriteFile(largeFilePath, largeFileData, 0644); err != nil { + t.Fatalf("Failed to write large file: %v", err) + } + + // Create a small file + smallFilePath := filepath.Join(rootDir, "smallfile.go") + if err := os.WriteFile(smallFilePath, []byte("package main"), 0644); err != nil { + t.Fatalf("Failed to write small file: %v", err) + } + + // Reset and load default config, which sets size limit to 5 MB + viper.Reset() + config.LoadConfig() + + var w Walker = ProdWalker{} + found, err := w.Walk(rootDir) + if err != nil { + t.Fatalf("Walk returned error: %v", err) + } + + // We should only get the small file + if len(found) != 1 { + t.Errorf("Expected 1 file under size limit, got %d", len(found)) + } + if len(found) == 1 && filepath.Base(found[0]) != "smallfile.go" { + t.Errorf("Expected smallfile.go, got %s", found[0]) + } +} diff --git a/fileproc/writer.go b/fileproc/writer.go index b9d929c..b530b25 100644 --- a/fileproc/writer.go +++ b/fileproc/writer.go @@ -1,3 +1,10 @@ +// Package fileproc provides a writer for the output of the file processor. +// +// The StartWriter function writes the output in the specified format. +// The formatMarkdown function formats the output in Markdown format. +// The detectLanguage function tries to infer the code block language from the file extension. +// The OutputData struct represents the full output structure. +// The FileData struct represents a single file's path and content. package fileproc import ( @@ -28,7 +35,7 @@ func StartWriter(outFile *os.File, writeCh <-chan WriteRequest, done chan<- stru // Read from channel until closed for req := range writeCh { - files = append(files, FileData{Path: req.Path, Content: req.Content}) + files = append(files, FileData(req)) } // Create output struct diff --git a/fileproc/writer_test.go b/fileproc/writer_test.go index ab845af..e6645f9 100644 --- a/fileproc/writer_test.go +++ b/fileproc/writer_test.go @@ -3,43 +3,117 @@ package fileproc import ( "encoding/json" "os" + "strings" + "sync" "testing" + + "gopkg.in/yaml.v3" ) -func TestStartWriter_JSONOutput(t *testing.T) { - outFile, err := os.CreateTemp("", "output.json") - if err != nil { - t.Fatal(err) - } - defer func(name string) { - err := os.Remove(name) - if err != nil { - t.Fatal(err) - } - }(outFile.Name()) - - writeCh := make(chan WriteRequest) - done := make(chan struct{}) - - go StartWriter(outFile, writeCh, done, "json", "Prefix", "Suffix") - - writeCh <- WriteRequest{Path: "file1.go", Content: "package main"} - writeCh <- WriteRequest{Path: "file2.py", Content: "def hello(): print('Hello')"} - - close(writeCh) - <-done - - data, err := os.ReadFile(outFile.Name()) - if err != nil { - t.Fatal(err) +func TestStartWriter_Formats(t *testing.T) { + // Define table-driven test cases + tests := []struct { + name string + format string + expectError bool + }{ + { + name: "JSON format", + format: "json", + expectError: false, + }, + { + name: "YAML format", + format: "yaml", + expectError: false, + }, + { + name: "Markdown format", + format: "markdown", + expectError: false, + }, + { + name: "Invalid format", + format: "invalid", + expectError: true, + }, } - var output OutputData - if err := json.Unmarshal(data, &output); err != nil { - t.Fatalf("JSON output is invalid: %v", err) - } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + outFile, err := os.CreateTemp("", "gibidify_test_output") + if err != nil { + t.Fatalf("Failed to create temp file: %v", err) + } + defer func() { + outFile.Close() + os.Remove(outFile.Name()) + }() - if len(output.Files) != 2 { - t.Errorf("Expected 2 files, got %d", len(output.Files)) + // Prepare channels + writeCh := make(chan WriteRequest, 2) + doneCh := make(chan struct{}) + + // Write a couple of sample requests + writeCh <- WriteRequest{Path: "sample.go", Content: "package main"} + writeCh <- WriteRequest{Path: "example.py", Content: "def foo(): pass"} + close(writeCh) + + // Start the writer + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + StartWriter(outFile, writeCh, doneCh, tc.format, "PREFIX", "SUFFIX") + }() + + // Wait until writer signals completion + wg.Wait() + <-doneCh // make sure all writes finished + + // Read output + data, err := os.ReadFile(outFile.Name()) + if err != nil { + t.Fatalf("Error reading output file: %v", err) + } + + if tc.expectError { + // For an invalid format, we expect StartWriter to log an error + // and produce no content or minimal content. There's no official + // error returned, so check if it's empty or obviously incorrect. + if len(data) != 0 { + t.Errorf("Expected no output for invalid format, got:\n%s", data) + } + } else { + // Valid format: check basic properties in the output + content := string(data) + switch tc.format { + case "json": + // Quick parse check + var outStruct OutputData + if err := json.Unmarshal(data, &outStruct); err != nil { + t.Errorf("JSON unmarshal failed: %v", err) + } + case "yaml": + var outStruct OutputData + if err := yaml.Unmarshal(data, &outStruct); err != nil { + t.Errorf("YAML unmarshal failed: %v", err) + } + case "markdown": + // Check presence of code fences or "## File: ..." + if !strings.Contains(content, "```") { + t.Error("Expected markdown code fences not found") + } + } + + // Prefix and suffix checks (common to JSON, YAML, markdown) + if !strings.Contains(string(data), "PREFIX") { + t.Errorf("Missing prefix in output: %s", data) + } + if !strings.Contains(string(data), "SUFFIX") { + t.Errorf("Missing suffix in output: %s", data) + } + } + }) } } diff --git a/go.mod b/go.mod index e54146e..dec5501 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/ivuorinen/gibidify go 1.24.1 -toolchain go1.24.1 - require ( github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/sirupsen/logrus v1.9.3 @@ -14,23 +12,14 @@ require ( require ( github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/go-viper/mapstructure/v2 v2.2.1 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect - github.com/magiconair/properties v1.8.7 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect - github.com/sagikazarmark/locafero v0.7.0 // indirect - github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sagikazarmark/locafero v0.8.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect - github.com/spf13/afero v1.12.0 // indirect + github.com/spf13/afero v1.14.0 // indirect github.com/spf13/cast v1.7.1 // indirect github.com/spf13/pflag v1.0.6 // indirect github.com/subosito/gotenv v1.6.0 // indirect - go.uber.org/atomic v1.9.0 // indirect - go.uber.org/multierr v1.9.0 // indirect - golang.org/x/crypto v0.35.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/net v0.36.0 // indirect - golang.org/x/sys v0.30.0 // indirect - golang.org/x/text v0.22.0 // indirect - gopkg.in/ini.v1 v1.67.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect ) diff --git a/go.sum b/go.sum index 43e5a74..7ce23c5 100644 --- a/go.sum +++ b/go.sum @@ -1,108 +1,57 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= -github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= -github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= -github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= -github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= -github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= -github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= -github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= -github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo= -github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k= -github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= -github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sagikazarmark/locafero v0.8.0 h1:mXaMVw7IqxNBxfv3LdWt9MDmcWDQ1fagDH918lOdVaQ= +github.com/sagikazarmark/locafero v0.8.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= -github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= -github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= -github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs= -github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4= -github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= -github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA= +github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo= github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= -github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/spf13/viper v1.20.0 h1:zrxIyR3RQIOsarIrgL8+sAvALXul9jeEPa06Y0Ph6vY= github.com/spf13/viper v1.20.0/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= -go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= -golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= -golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= -golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= -golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= -golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= -golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= -golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= -gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/kics.config b/kics.config new file mode 100644 index 0000000..35d5a9a --- /dev/null +++ b/kics.config @@ -0,0 +1,4 @@ +# vim: ft=yaml + +log-level: WARN +exclude-severities: 'info,low' diff --git a/main.go b/main.go index 5ef473e..6e9a995 100644 --- a/main.go +++ b/main.go @@ -30,45 +30,46 @@ func init() { flag.StringVar(&destination, "destination", "", "Output file to write aggregated code") flag.StringVar(&prefix, "prefix", "", "Text to add at the beginning of the output file") flag.StringVar(&suffix, "suffix", "", "Text to add at the end of the output file") - flag.StringVar(&format, "format", "json", "Output format (json, markdown, yaml)") + flag.StringVar(&format, "format", "markdown", "Output format (json, markdown, yaml)") flag.IntVar(&concurrency, "concurrency", runtime.NumCPU(), "Number of concurrent workers (default: number of CPU cores)") } +func main() { + // In production, use a background context. + if err := run(context.Background()); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } +} + // Run executes the main logic of the CLI application using the provided context. -func Run(ctx context.Context) error { +func run(ctx context.Context) error { flag.Parse() - // We need at least a source directory - if sourceDir == "" { - return fmt.Errorf("usage: gibidify -source [--destination ] [--format=json|yaml|markdown] ") + if err := validateFlags(); err != nil { + return err } - // If destination is not specified, auto-generate it using the base name of sourceDir + "." + format - if destination == "" { - absRoot, err := filepath.Abs(sourceDir) - if err != nil { - return fmt.Errorf("failed to get absolute path for %s: %w", sourceDir, err) - } - baseName := filepath.Base(absRoot) - // If sourceDir ends with a slash, baseName might be "." so handle that case as needed - if baseName == "." || baseName == "" { - baseName = "output" - } - destination = baseName + "." + format + if err := setDestination(); err != nil { + return err } config.LoadConfig() - logrus.Infof("Starting gibidify. Format: %s, Source: %s, Destination: %s, Workers: %d", format, sourceDir, destination, concurrency) + logrus.Infof( + "Starting gibidify. Format: %s, Source: %s, Destination: %s, Workers: %d", + format, + sourceDir, + destination, + concurrency, + ) - // Collect files files, err := fileproc.CollectFiles(sourceDir) if err != nil { return fmt.Errorf("error collecting files: %w", err) } logrus.Infof("Found %d files to process", len(files)) - // Open output file outFile, err := os.Create(destination) if err != nil { return fmt.Errorf("failed to create output file %s: %w", destination, err) @@ -79,42 +80,16 @@ func Run(ctx context.Context) error { } }(outFile) - // Create channels fileCh := make(chan string) writeCh := make(chan fileproc.WriteRequest) writerDone := make(chan struct{}) - // Start writer goroutine go fileproc.StartWriter(outFile, writeCh, writerDone, format, prefix, suffix) var wg sync.WaitGroup - // Start worker goroutines with context cancellation - for i := 0; i < concurrency; i++ { - wg.Add(1) - go func() { - defer wg.Done() - for { - select { - case <-ctx.Done(): - return - case filePath, ok := <-fileCh: - if !ok { - return - } - // Pass sourceDir to ProcessFile so it knows the 'root' - absRoot, err := filepath.Abs(sourceDir) - if err != nil { - logrus.Errorf("Failed to get absolute path for %s: %v", sourceDir, err) - return - } - fileproc.ProcessFile(filePath, writeCh, absRoot) - } - } - }() - } + startWorkers(ctx, &wg, fileCh, writeCh) - // Feed files to worker pool while checking for cancellation for _, fp := range files { select { case <-ctx.Done(): @@ -132,11 +107,49 @@ func Run(ctx context.Context) error { logrus.Infof("Processing completed. Output saved to %s", destination) return nil } +func validateFlags() error { + if sourceDir == "" { + return fmt.Errorf("usage: gibidify -source [--destination ] [--format=json|yaml|markdown] ") + } + return nil +} -func main() { - // In production, use a background context. - if err := Run(context.Background()); err != nil { - fmt.Println("Error:", err) - os.Exit(1) +func setDestination() error { + if destination == "" { + absRoot, err := filepath.Abs(sourceDir) + if err != nil { + return fmt.Errorf("failed to get absolute path for %s: %w", sourceDir, err) + } + baseName := filepath.Base(absRoot) + if baseName == "." || baseName == "" { + baseName = "output" + } + destination = baseName + "." + format + } + return nil +} + +func startWorkers(ctx context.Context, wg *sync.WaitGroup, fileCh chan string, writeCh chan fileproc.WriteRequest) { + for range concurrency { + wg.Add(1) + go func() { + defer wg.Done() + for { + select { + case <-ctx.Done(): + return + case filePath, ok := <-fileCh: + if !ok { + return + } + absRoot, err := filepath.Abs(sourceDir) + if err != nil { + logrus.Errorf("Failed to get absolute path for %s: %v", sourceDir, err) + return + } + fileproc.ProcessFile(filePath, writeCh, absRoot) + } + } + }() } } diff --git a/integration_test.go b/main_test.go similarity index 80% rename from integration_test.go rename to main_test.go index e4f9e01..cdbfae9 100644 --- a/integration_test.go +++ b/main_test.go @@ -3,7 +3,6 @@ package main import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -14,7 +13,7 @@ import ( // TestIntegrationFullCLI simulates a full run of the CLI application using adaptive concurrency. func TestIntegrationFullCLI(t *testing.T) { // Create a temporary source directory and populate it with test files. - srcDir, err := ioutil.TempDir("", "gibidify_src") + srcDir, err := os.MkdirTemp("", "gibidify_src") if err != nil { t.Fatalf("Failed to create temp source directory: %v", err) } @@ -22,16 +21,16 @@ func TestIntegrationFullCLI(t *testing.T) { // Create two test files. file1 := filepath.Join(srcDir, "file1.txt") - if err := ioutil.WriteFile(file1, []byte("Hello World"), 0644); err != nil { + if err := os.WriteFile(file1, []byte("Hello World"), 0644); err != nil { t.Fatalf("Failed to write file1: %v", err) } file2 := filepath.Join(srcDir, "file2.go") - if err := ioutil.WriteFile(file2, []byte("package main\nfunc main() {}"), 0644); err != nil { + if err := os.WriteFile(file2, []byte("package main\nfunc main() {}"), 0644); err != nil { t.Fatalf("Failed to write file2: %v", err) } // Create a temporary output file. - outFile, err := ioutil.TempFile("", "gibidify_output.txt") + outFile, err := os.CreateTemp("", "gibidify_output.txt") if err != nil { t.Fatalf("Failed to create temp output file: %v", err) } @@ -51,12 +50,12 @@ func TestIntegrationFullCLI(t *testing.T) { // Run the application with a background context. ctx := context.Background() - if err := Run(ctx); err != nil { + if err := run(ctx); err != nil { t.Fatalf("Run failed: %v", err) } // Verify the output file contains the expected prefix, file contents, and suffix. - data, err := ioutil.ReadFile(outFilePath) + data, err := os.ReadFile(outFilePath) if err != nil { t.Fatalf("Failed to read output file: %v", err) } @@ -75,7 +74,7 @@ func TestIntegrationFullCLI(t *testing.T) { // TestIntegrationCancellation verifies that the application correctly cancels processing when the context times out. func TestIntegrationCancellation(t *testing.T) { // Create a temporary source directory with many files to simulate a long-running process. - srcDir, err := ioutil.TempDir("", "gibidify_src_long") + srcDir, err := os.MkdirTemp("", "gibidify_src_long") if err != nil { t.Fatalf("Failed to create temp source directory: %v", err) } @@ -84,13 +83,13 @@ func TestIntegrationCancellation(t *testing.T) { // Create a large number of small files. for i := 0; i < 1000; i++ { filePath := filepath.Join(srcDir, fmt.Sprintf("file%d.txt", i)) - if err := ioutil.WriteFile(filePath, []byte("Content"), 0644); err != nil { + if err := os.WriteFile(filePath, []byte("Content"), 0644); err != nil { t.Fatalf("Failed to write %s: %v", filePath, err) } } // Create a temporary output file. - outFile, err := ioutil.TempFile("", "gibidify_output.txt") + outFile, err := os.CreateTemp("", "gibidify_output.txt") if err != nil { t.Fatalf("Failed to create temp output file: %v", err) } @@ -109,11 +108,14 @@ func TestIntegrationCancellation(t *testing.T) { } // Create a context with a very short timeout to force cancellation. - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + ctx, cancel := context.WithTimeout( + context.Background(), + 10*time.Millisecond, + ) defer cancel() // Run the application; we expect an error due to cancellation. - err = Run(ctx) + err = run(ctx) if err == nil { t.Error("Expected Run to fail due to cancellation, but it succeeded") }