Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# RestMan - Coverage Configuration

This file explains how to use coverage in RestMan.

## Why this file exists

RestMan has a particular test structure: tests are in separate `/test/` folders instead of being co-located with source code (e.g., `router_test.go` next to `router.go`).

This structure is intentional as it clearly separates production code from tests, but it requires a special command for coverage.

## The problem

If you simply run `go test -cover ./...`, Go only calculates coverage for packages **containing tests**, not the source code. Result: 0% coverage displayed even though tests pass.

## The solution

Use the `-coverpkg=./...` flag which tells Go: "calculate coverage for ALL packages, not just those with tests".

## Commands to use

### Basic coverage (terminal)
```bash
go test -coverprofile=coverage.out ./... -coverpkg=./...
go tool cover -func=coverage.out
```

Displays something like:
```
github.com/philiphil/restman/router/get.go:15: Get 100.0%
github.com/philiphil/restman/router/post.go:20: Post 85.7%
total: (statements) 69.5%
```

### Visual coverage (HTML)
```bash
go test -coverprofile=coverage.out ./... -coverpkg=./...
go tool cover -html=coverage.out -o coverage.html
open coverage.html # macOS
```

Opens an interactive HTML report showing line by line what is tested (green) or not (red).

### Coverage for a specific package
```bash
# Router only
go test -coverprofile=coverage.out ./test/router/... -coverpkg=./...
go tool cover -func=coverage.out

# Serializer only
go test -coverprofile=coverage.out ./test/serializer/... -coverpkg=./...
go tool cover -func=coverage.out
```

### Clean cache before testing
```bash
go clean -testcache && go test -coverprofile=coverage.out ./... -coverpkg=./...
```

Useful if tests seem "cached" or if changes are not being picked up.

## For CI/CD

GitHub Actions example:
```yaml
- name: Test with coverage
run: |
go test -race -coverprofile=coverage.out -covermode=atomic ./... -coverpkg=./...
go tool cover -func=coverage.out

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./coverage.out
```

## Coverage targets

- **Core packages** (router, orm, serializer): > 80%
- **Utilities** (configuration, format, errors): > 70%
- **Global**: > 65%

## Current results (Jan 2025)

- `router`: **69.5%** ✅
- `orm/gormrepository`: **~8%** ⚠️ (tests exist but incomplete)
- `serializer`: **~23%** ⚠️

## Why `-coverpkg=./...`?

Without this flag:
- `go test ./test/router/...` → calculates coverage of `test/router` (which only has tests, 0 LOC of business code)

With this flag:
- `go test ./test/router/... -coverpkg=./...` → calculates coverage of the ENTIRE project, even if tests are elsewhere

It's the equivalent of saying "run tests from test/router/, but measure coverage of router/, orm/, serializer/, etc."
11 changes: 9 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
# Note: Project requires Go 1.24+ but using 1.23 for CI until 1.24 is stable
go-version: '1.23'
check-latest: true

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go test -v -short ./...

- name: Test with Coverage
run: |
go test -short -coverprofile=coverage.out ./... -coverpkg=./...
go tool cover -func=coverage.out | grep total | awk '{print "Total coverage: " $3}'
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool
*.out
coverage.out
coverage.html

# Dependency directories
vendor/

# Go workspace file
go.work
go.work.sum

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Database files
*.db
*.sqlite
*.sqlite3

# Temporary files
tmp/
temp/
82 changes: 0 additions & 82 deletions Abstract.md

This file was deleted.

Loading
Loading