Skip to content

Commit d6e2aef

Browse files
committed
fix: make linter happy
1 parent f2bd3a6 commit d6e2aef

18 files changed

Lines changed: 142 additions & 310 deletions

File tree

.golangci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: "2"
2+
linters:
3+
enable:
4+
- misspell
5+
- revive
6+
disable:
7+
- gocyclo
8+
settings:
9+
gocyclo:
10+
min-complexity: 20
11+
exclusions:
12+
generated: lax
13+
presets:
14+
- comments
15+
- common-false-positives
16+
- legacy
17+
- std-error-handling
18+
rules:
19+
- path: (.+)\.go$
20+
text: SA1019 # CPUTimesStat.Total is deprecated
21+
- path: (.+)\.go$
22+
text: SA5008 # duplicate struct tag "choice" (staticcheck)
23+
- path: (.+)\.go$
24+
text: QF1001 # could apply De Morgan's law
25+
- path: pkg/
26+
text: "var-naming: avoid meaningless package names"
27+
linters:
28+
- revive
29+
paths:
30+
- third_party$
31+
- builtin$
32+
- examples$
33+
formatters:
34+
exclusions:
35+
generated: lax
36+
paths:
37+
- third_party$
38+
- builtin$
39+
- examples$

cmd/pgcov/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ func reportCommand(ctx context.Context, cmd *urfavecli.Command) error {
123123
output := cmd.String("output")
124124
coverageFile := cmd.String("coverage-file")
125125

126-
return cli.Report(coverageFile, format, output)
126+
return cli.Report(ctx, coverageFile, format, output)
127127
}

internal/cli/report.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67

@@ -9,7 +10,7 @@ import (
910
)
1011

1112
// Report generates a coverage report from saved coverage data
12-
func Report(coverageFile string, format string, outputPath string) error {
13+
func Report(_ context.Context, coverageFile string, format string, outputPath string) error {
1314
// Step 1: Load coverage data
1415
store := coverage.NewStore(coverageFile)
1516
if !store.Exists() {

internal/cli/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Run(ctx context.Context, config *Config, searchPath string) (int, error) {
5757
}
5858

5959
// Step 4: Instrument source files
60-
instrumentedSources, err := instrument.InstrumentBatch(parsedSources)
60+
instrumentedSources, err := instrument.GenerateCoverageInstruments(parsedSources)
6161
if err != nil {
6262
return 1, fmt.Errorf("failed to instrument sources: %w", err)
6363
}

internal/coverage/model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ func (c *Coverage) AddLine(file string, line int, hitCount int) {
6060
}
6161

6262
// AddBranch adds or updates branch coverage data (placeholder for future)
63-
func (c *Coverage) AddBranch(file string, branchID string, hitCount int) {
63+
func (c *Coverage) AddBranch(_ string, branchID string, hitCount int) {
6464
// TODO: Implement branch coverage tracking
65+
_ = branchID
66+
_ = hitCount
6567
}
6668

6769
// LineCoveragePercent calculates the line coverage percentage for a file

internal/database/pool.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@ import (
55
"fmt"
66
"strconv"
77

8-
"github.com/cybertec-postgresql/pgcov/internal/errors"
98
"github.com/cybertec-postgresql/pgcov/pkg/types"
109
"github.com/jackc/pgx/v5/pgxpool"
1110
)
1211

1312
const applicationName = "pgcov"
1413

14+
// ConnectionError represents PostgreSQL connection failure
15+
type ConnectionError struct {
16+
Host string
17+
Port int
18+
Message string
19+
Suggestion string
20+
}
21+
22+
func (e *ConnectionError) Error() string {
23+
msg := fmt.Sprintf("failed to connect to %s:%d: %s", e.Host, e.Port, e.Message)
24+
if e.Suggestion != "" {
25+
msg += fmt.Sprintf("\nSuggestion: %s", e.Suggestion)
26+
}
27+
return msg
28+
}
29+
30+
// NewConnectionError creates a new ConnectionError
31+
func NewConnectionError(host string, port int, message string) *ConnectionError {
32+
return &ConnectionError{
33+
Host: host,
34+
Port: port,
35+
Message: message,
36+
}
37+
}
38+
1539
// Pool wraps pgxpool.Pool with additional functionality
1640
type Pool struct {
1741
*pgxpool.Pool
@@ -26,7 +50,7 @@ func NewPool(ctx context.Context, config *types.Config) (*Pool, error) {
2650
// Configure pool
2751
poolConfig, err := pgxpool.ParseConfig(connString)
2852
if err != nil {
29-
return nil, &errors.ConnectionError{
53+
return nil, &ConnectionError{
3054
Message: fmt.Sprintf("invalid connection configuration: %v", err),
3155
Suggestion: "Check your PostgreSQL connection string format. Use URI format (postgresql://user:pass@host:port/db) or key=value format (host=localhost port=5432 ...)",
3256
}
@@ -45,7 +69,7 @@ func NewPool(ctx context.Context, config *types.Config) (*Pool, error) {
4569
// Create pool
4670
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
4771
if err != nil {
48-
return nil, &errors.ConnectionError{
72+
return nil, &ConnectionError{
4973
Message: fmt.Sprintf("failed to create connection pool: %v", err),
5074
Suggestion: "Verify PostgreSQL is running and accessible with the provided connection string",
5175
}
@@ -56,23 +80,23 @@ func NewPool(ctx context.Context, config *types.Config) (*Pool, error) {
5680
err = pool.QueryRow(ctx, "SHOW server_version_num").Scan(&versionStr)
5781
if err != nil {
5882
pool.Close()
59-
return nil, &errors.ConnectionError{
83+
return nil, &ConnectionError{
6084
Message: fmt.Sprintf("failed to query PostgreSQL version: %v", err),
6185
}
6286
}
6387

6488
version, err := strconv.Atoi(versionStr)
6589
if err != nil {
6690
pool.Close()
67-
return nil, &errors.ConnectionError{
91+
return nil, &ConnectionError{
6892
Message: fmt.Sprintf("failed to parse PostgreSQL version '%s': %v", versionStr, err),
6993
}
7094
}
7195

7296
// PostgreSQL 13+ required (version 130000+)
7397
if version < 130000 {
7498
pool.Close()
75-
return nil, &errors.ConnectionError{
99+
return nil, &ConnectionError{
76100
Message: fmt.Sprintf("PostgreSQL version %d is not supported (need 13+)", version/10000),
77101
Suggestion: "Upgrade to PostgreSQL 13 or later",
78102
}

internal/database/tempdb.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,11 @@ func DestroyTempDatabase(ctx context.Context, pool *Pool, tempDB *types.TempData
7575
defer conn.Release()
7676

7777
// Terminate all connections to the database first (PostgreSQL 13+)
78-
terminateSQL := fmt.Sprintf(`
79-
SELECT pg_terminate_backend(pid)
78+
terminateSQL := `SELECT pg_terminate_backend(pid)
8079
FROM pg_stat_activity
81-
WHERE datname = '%s' AND pid <> pg_backend_pid()
82-
`, tempDB.Name)
80+
WHERE datname = $1 AND pid <> pg_backend_pid()`
8381

84-
_, err = conn.Exec(ctx, terminateSQL)
85-
if err != nil {
86-
// Non-fatal - database might not have any connections
87-
}
82+
_, _ = conn.Exec(ctx, terminateSQL, tempDB.Name)
8883

8984
// Drop database with FORCE option (PostgreSQL 13+)
9085
dropSQL := fmt.Sprintf("DROP DATABASE IF EXISTS %s WITH (FORCE)", tempDB.Name)
@@ -127,7 +122,7 @@ func verifyDatabaseDropped(ctx context.Context, conn *pgxpool.Conn, dbName strin
127122
// CleanupStaleTempDatabases removes old pgcov temporary databases
128123
// This is useful for cleanup after crashes or interrupted test runs
129124
// Returns list of cleaned databases and any errors encountered
130-
func CleanupStaleTempDatabases(ctx context.Context, pool *Pool, olderThan time.Duration) ([]string, error) {
125+
func CleanupStaleTempDatabases(ctx context.Context, pool *Pool, _ time.Duration) ([]string, error) {
131126
conn, err := pool.Acquire(ctx)
132127
if err != nil {
133128
return nil, fmt.Errorf("failed to acquire connection: %w", err)

internal/errors/errors.go

Lines changed: 0 additions & 93 deletions
This file was deleted.

internal/instrument/instrumenter.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@ import (
77
"github.com/cybertec-postgresql/pgcov/internal/parser"
88
)
99

10-
// Instrument takes parsed SQL and injects coverage tracking calls
11-
func Instrument(parsed *parser.ParsedSQL) (*InstrumentedSQL, error) {
12-
// Use full instrumentation with NOTIFY injection
13-
return InstrumentWithNotify(parsed)
14-
}
15-
16-
// InstrumentBatch instruments multiple parsed SQL files
17-
func InstrumentBatch(parsedFiles []*parser.ParsedSQL) ([]*InstrumentedSQL, error) {
10+
// GenerateCoverageInstruments instruments multiple parsed SQL files
11+
func GenerateCoverageInstruments(parsedFiles []*parser.ParsedSQL) ([]*InstrumentedSQL, error) {
1812
var instrumented []*InstrumentedSQL
1913

2014
for _, parsed := range parsedFiles {
21-
inst, err := Instrument(parsed)
15+
inst, err := GenerateCoverageInstrument(parsed)
2216
if err != nil {
2317
return nil, fmt.Errorf("failed to instrument %s: %w", parsed.File.Path, err)
2418
}
@@ -39,7 +33,7 @@ func GetCoveragePointBySignal(instrumented *InstrumentedSQL, signalID string) *C
3933
}
4034

4135
// InstrumentWithNotify instruments SQL by injecting NOTIFY calls for coverage tracking
42-
func InstrumentWithNotify(parsed *parser.ParsedSQL) (*InstrumentedSQL, error) {
36+
func GenerateCoverageInstrument(parsed *parser.ParsedSQL) (*InstrumentedSQL, error) {
4337
if parsed == nil || parsed.File == nil {
4438
return nil, fmt.Errorf("parsed SQL or file is nil")
4539
}
@@ -263,29 +257,3 @@ func markStatementLinesAsCovered(stmt *parser.Statement, filePath string) []Cove
263257

264258
return locations
265259
}
266-
267-
func readOriginalSQL(parsed *parser.ParsedSQL) string {
268-
if len(parsed.Statements) == 0 {
269-
return ""
270-
}
271-
var parts []string
272-
for _, stmt := range parsed.Statements {
273-
parts = append(parts, stmt.RawSQL)
274-
}
275-
return strings.Join(parts, "\n")
276-
}
277-
278-
func findStatementPosition(sql string, lineNum int) int {
279-
if lineNum <= 1 {
280-
return 0
281-
}
282-
pos := 0
283-
currentLine := 1
284-
for i := 0; i < len(sql) && currentLine < lineNum; i++ {
285-
if sql[i] == '\n' {
286-
currentLine++
287-
pos = i + 1
288-
}
289-
}
290-
return pos
291-
}

0 commit comments

Comments
 (0)