Skip to content

Commit e5a4f9a

Browse files
authored
Merge pull request #36 from tstromberg/main
improve testing coverage
2 parents 957b79f + ec38464 commit e5a4f9a

File tree

20 files changed

+3121
-594
lines changed

20 files changed

+3121
-594
lines changed

.github/workflows/test.yml

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,92 +12,92 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- name: Checkout code
16-
uses: actions/checkout@v4
15+
- name: Checkout code
16+
uses: actions/checkout@v4
1717

18-
- name: Set up Go
19-
uses: actions/setup-go@v5
20-
with:
18+
- name: Set up Go
19+
uses: actions/setup-go@v5
20+
with:
2121
go-version: '1.23'
2222
cache: true
2323

24-
- name: Download dependencies
25-
run: go mod download
26-
27-
- name: Verify dependencies
28-
run: go mod verify
29-
30-
- name: Build
31-
run: make build
32-
33-
- name: Run tests with race detector and coverage
34-
run: make test
35-
36-
- name: Generate detailed coverage report
37-
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
38-
39-
- name: Check coverage threshold (80% minimum for pkg/)
40-
run: |
41-
echo "Checking coverage for each package..."
42-
for pkg in $(go list ./pkg/...); do
43-
coverage=$(go test -coverprofile=/tmp/coverage.out $pkg 2>&1 | grep -oP 'coverage: \K[0-9.]+' || echo "0")
44-
pkg_name=$(echo $pkg | sed 's/.*\///')
45-
echo "$pkg_name: ${coverage}%"
46-
# Verify minimum 80% coverage for all pkg/ packages
47-
if (( $(echo "$coverage < 80" | bc -l) )); then
48-
echo "::error::$pkg_name has ${coverage}% coverage, which is below 80% threshold"
49-
exit 1
50-
fi
51-
done
52-
53-
- name: Upload coverage to Codecov
54-
uses: codecov/codecov-action@v4
55-
with:
56-
files: ./coverage.out
57-
flags: unittests
58-
name: codecov-umbrella
59-
fail_ci_if_error: false
60-
env:
61-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
24+
- name: Download dependencies
25+
run: go mod download
26+
27+
- name: Verify dependencies
28+
run: go mod verify
29+
30+
- name: Build
31+
run: make build
32+
33+
- name: Run tests with race detector and coverage
34+
run: make test
35+
36+
- name: Generate detailed coverage report
37+
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
38+
39+
- name: Check coverage threshold (80% minimum for pkg/)
40+
run: |
41+
echo "Checking coverage for each package..."
42+
for pkg in $(go list ./pkg/...); do
43+
coverage=$(go test -coverprofile=/tmp/coverage.out $pkg 2>&1 | grep -oP 'coverage: \K[0-9.]+' || echo "0")
44+
pkg_name=$(echo $pkg | sed 's/.*\///')
45+
echo "$pkg_name: ${coverage}%"
46+
# Verify minimum 80% coverage for all pkg/ packages
47+
if (( $(echo "$coverage < 80" | bc -l) )); then
48+
echo "::error::$pkg_name has ${coverage}% coverage, which is below 80% threshold"
49+
exit 1
50+
fi
51+
done
52+
53+
- name: Upload coverage to Codecov
54+
uses: codecov/codecov-action@v4
55+
with:
56+
files: ./coverage.out
57+
flags: unittests
58+
name: codecov-umbrella
59+
fail_ci_if_error: false
60+
env:
61+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
6262

6363
lint:
6464
name: Run Linters
6565
runs-on: ubuntu-latest
6666

6767
steps:
68-
- name: Checkout code
69-
uses: actions/checkout@v4
68+
- name: Checkout code
69+
uses: actions/checkout@v4
7070

71-
- name: Set up Go
72-
uses: actions/setup-go@v5
73-
with:
71+
- name: Set up Go
72+
uses: actions/setup-go@v5
73+
with:
7474
go-version: '1.23'
7575
cache: true
7676

77-
- name: Run linters
78-
run: make lint
77+
- name: Run linters
78+
run: make lint
7979

8080
build:
8181
name: Build Binaries
8282
runs-on: ubuntu-latest
8383

8484
steps:
85-
- name: Checkout code
86-
uses: actions/checkout@v4
85+
- name: Checkout code
86+
uses: actions/checkout@v4
8787

88-
- name: Set up Go
89-
uses: actions/setup-go@v5
90-
with:
88+
- name: Set up Go
89+
uses: actions/setup-go@v5
90+
with:
9191
go-version: '1.23'
9292
cache: true
9393

94-
- name: Build server
95-
run: go build -v -o sprinkler-server ./cmd/server
94+
- name: Build server
95+
run: go build -v -o sprinkler-server ./cmd/server
9696

97-
- name: Build client
98-
run: go build -v -o sprinkler-client ./cmd/client
97+
- name: Build client
98+
run: go build -v -o sprinkler-client ./cmd/client
9999

100-
- name: Verify binaries
101-
run: |
102-
./sprinkler-server -h || true
103-
./sprinkler-client -h || true
100+
- name: Verify binaries
101+
run: |
102+
./sprinkler-server -h || true
103+
./sprinkler-client -h || true

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ linters:
231231
- path: _test\.go
232232
linters:
233233
- dupl
234+
- errcheck
234235
- gosec
235236
- godot
236237
- govet # alignment

cmd/client/main.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,6 @@ import (
1919
"github.com/codeGROOVE-dev/sprinkler/pkg/client"
2020
)
2121

22-
// gitHubToken attempts to get a GitHub token from multiple sources:
23-
// 1. Command-line flag
24-
// 2. GITHUB_TOKEN environment variable
25-
// 3. gh auth token command.
26-
func gitHubToken(flagToken string) (string, error) {
27-
// First try flag
28-
if flagToken != "" {
29-
return flagToken, nil
30-
}
31-
32-
// Then try environment variable
33-
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
34-
log.Println("Using token from GITHUB_TOKEN environment variable")
35-
return token, nil
36-
}
37-
38-
// Finally try gh auth token
39-
log.Println("No token provided, attempting to use gh auth token")
40-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
41-
defer cancel()
42-
cmd := exec.CommandContext(ctx, "gh", "auth", "token")
43-
output, err := cmd.Output()
44-
if err != nil {
45-
return "", fmt.Errorf("failed to get token from 'gh auth token': %w\n"+
46-
"Please provide a token via -token flag, GITHUB_TOKEN env var, or authenticate with 'gh auth login'", err)
47-
}
48-
49-
token := strings.TrimSpace(string(output))
50-
if token == "" {
51-
return "", errors.New("gh auth token returned empty token")
52-
}
53-
54-
log.Println("Using token from gh auth token")
55-
return token, nil
56-
}
57-
5822
func run() error {
5923
var (
6024
serverAddr = flag.String("addr", client.DefaultServerAddress, "server address (hostname:port)")
@@ -98,10 +62,29 @@ func run() error {
9862
return errors.New("organization, PR URLs, or --user flag required")
9963
}
10064

101-
// Get token from various sources
102-
githubToken, err := gitHubToken(*token)
103-
if err != nil {
104-
return err
65+
// Get token from various sources: flag, environment variable, or gh CLI
66+
var githubToken string
67+
var err error
68+
if *token != "" {
69+
githubToken = *token
70+
} else if envToken := os.Getenv("GITHUB_TOKEN"); envToken != "" {
71+
log.Println("Using token from GITHUB_TOKEN environment variable")
72+
githubToken = envToken
73+
} else {
74+
log.Println("No token provided, attempting to use gh auth token")
75+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
76+
defer cancel()
77+
cmd := exec.CommandContext(ctx, "gh", "auth", "token")
78+
output, err := cmd.Output()
79+
if err != nil {
80+
return fmt.Errorf("failed to get token from 'gh auth token': %w\n"+
81+
"Please provide a token via -token flag, GITHUB_TOKEN env var, or authenticate with 'gh auth login'", err)
82+
}
83+
githubToken = strings.TrimSpace(string(output))
84+
if githubToken == "" {
85+
return errors.New("gh auth token returned empty token")
86+
}
87+
log.Println("Using token from gh auth token")
10588
}
10689

10790
// Build WebSocket URL - secure by default

0 commit comments

Comments
 (0)