Skip to content

Improve test coverage for core packages #178

@koki-develop

Description

@koki-develop

Overview

Currently, the test coverage for the gat project is at 14.2%, which is significantly low for a production-ready CLI tool. This issue tracks the effort to improve test coverage for the core packages, particularly those with little to no coverage.

Current Test Coverage Status

Based on the coverage analysis:

  • Overall coverage: 14.2%
  • Package breakdown:
    • cmd: 0.0% (critical - contains all CLI command logic)
    • internal/gat: 4.7% (critical - core functionality)
    • internal/formatters: 0.0% (important - output formatting)
    • internal/lexers: 0.0% (important - custom syntax highlighting)
    • internal/prettier: 83.3% (good coverage)
    • internal/styles: 0.0%

Testing Strategy

1. Command Package Testing (cmd/)

The cmd package contains the CLI interface and is currently untested. Key areas to test:

  • cmd/root.go:

    • Command initialization and flag parsing
    • File input handling (single file, multiple files, stdin)
    • Output redirection logic
    • Error scenarios (invalid files, permissions, etc.)
    • Flag combinations and edge cases
  • cmd/flags.go:

    • Flag validation and parsing
    • Default value handling
    • Conflicting flag scenarios
  • cmd/version.go:

    • Version command output
    • Build information display

2. Core Package Testing (internal/gat/)

The core gat package needs comprehensive testing for:

  • File Processing:

    • Reading from files, stdin, and multiple sources
    • Binary file detection
    • Compressed file handling (gzip)
    • Large file processing
  • Syntax Highlighting:

    • Language detection
    • Theme application
    • Line numbering
    • Custom lexer integration
  • Output Formatting:

    • Different output formats (terminal, HTML, JSON)
    • Color handling for TTY vs non-TTY
    • Width constraints and wrapping
  • Special File Types:

    • Image rendering (with/without sixel support)
    • Markdown rendering
    • SVG, JSON, XML formatting

3. Formatters Package Testing (internal/formatters/)

Each formatter should be tested independently:

  • HTML minification edge cases
  • JSON formatting with various input structures
  • SVG optimization scenarios

4. Lexers Package Testing (internal/lexers/)

Custom lexers need testing for:

  • Terraform syntax highlighting accuracy
  • Edge cases in lexical analysis
  • Performance with large files

Implementation Plan

Phase 1: Command Package (Priority: High)

Create comprehensive tests for the CLI interface:

// cmd/root_test.go
func TestRootCommand(t *testing.T) {
    tests := []struct {
        name    string
        args    []string
        files   map[string]string
        wantErr bool
        check   func(t *testing.T, output string)
    }{
        // Test cases for various scenarios
    }
}

Phase 2: Core Functionality (Priority: High)

Test the main Gat struct and its methods:

// internal/gat/gat_test.go
func TestGat_Print(t *testing.T) {
    // Test various print scenarios
}

func TestGat_detectLanguage(t *testing.T) {
    // Test language detection logic
}

Phase 3: Formatters and Lexers (Priority: Medium)

Create unit tests for each formatter and lexer:

// internal/formatters/html_test.go
func TestHTMLFormatter(t *testing.T) {
    // Test HTML minification
}

Testing Best Practices

  1. Table-driven tests: Use table-driven tests for comprehensive coverage of edge cases
  2. Mocking: Mock external dependencies (e.g., file system operations where appropriate)
  3. Integration tests: Include integration tests for end-to-end scenarios
  4. Benchmark tests: Add benchmarks for performance-critical paths
  5. Error scenarios: Ensure all error paths are tested
  6. Resource cleanup: Test proper resource management (file handles, etc.)

Expected Outcomes

  • Increase overall test coverage to at least 70%
  • Achieve 80%+ coverage for critical packages (cmd, internal/gat)
  • Establish a foundation for continuous testing improvements
  • Improve confidence in refactoring and new feature development

Additional Considerations

  • Set up coverage reporting in CI/CD pipeline
  • Configure coverage thresholds to prevent regression
  • Consider using coverage tools like codecov.io for visualization
  • Document testing conventions in CONTRIBUTING.md

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions