Skip to content

Commit f32325e

Browse files
authored
test(librariangen): add codecov and improve main.go coverage (#3936)
Refactors the main function to be more testable by extracting the logic into a `runCLI` function. The logging setup is also extracted into a `parseLogLevel` function to allow for easier testing. This change increases the test coverage of main.go from 65.5% to 96.9%.
1 parent fe44aed commit f32325e

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

.github/workflows/downstream.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
- library_generation/**
1010
- showcase/**
1111
- test/**
12+
- internal/librariangen/**
1213

1314
name: Downstream Check
1415
jobs:

.github/workflows/librariangen-ci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ jobs:
2727
run: go version
2828

2929
- name: Run Go tests
30-
run: go test ./...
30+
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
3131
working-directory: internal/librariangen
3232

33+
- name: Upload coverage to Codecov
34+
uses: codecov/codecov-action@v5
35+
with:
36+
token: ${{ secrets.CODECOV_TOKEN }}
37+
files: coverage.txt
38+
working-directory: internal/librariangen
39+
flags: librariangen

internal/librariangen/main.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,32 @@ const version = "0.1.0"
2727

2828
// main is the entrypoint for the librariangen CLI.
2929
func main() {
30-
logLevel := slog.LevelInfo
31-
switch os.Getenv("GOOGLE_SDK_JAVA_LOGGING_LEVEL") {
32-
case "debug":
33-
logLevel = slog.LevelDebug
34-
case "quiet":
35-
logLevel = slog.LevelError + 1
36-
}
30+
os.Exit(runCLI(os.Args))
31+
}
32+
33+
func runCLI(args []string) int {
34+
logLevel := parseLogLevel(os.Getenv("GOOGLE_SDK_JAVA_LOGGING_LEVEL"))
3735
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
3836
Level: logLevel,
3937
})))
40-
slog.Info("librariangen: invoked", "args", os.Args)
41-
if err := run(context.Background(), os.Args[1:]); err != nil {
38+
slog.Info("librariangen: invoked", "args", args)
39+
if err := run(context.Background(), args[1:]); err != nil {
4240
slog.Error("librariangen: failed", "error", err)
43-
os.Exit(1)
41+
return 1
4442
}
4543
slog.Info("librariangen: finished successfully")
44+
return 0
45+
}
46+
47+
func parseLogLevel(logLevelEnv string) slog.Level {
48+
switch logLevelEnv {
49+
case "debug":
50+
return slog.LevelDebug
51+
case "quiet":
52+
return slog.LevelError + 1
53+
default:
54+
return slog.LevelInfo
55+
}
4656
}
4757

4858
// run executes the appropriate command based on the CLI's invocation arguments.

internal/librariangen/main_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"context"
19+
"log/slog"
1920
"testing"
2021
)
2122

@@ -91,4 +92,50 @@ func TestRun(t *testing.T) {
9192
}
9293
})
9394
}
95+
}
96+
97+
func TestRunCLI(t *testing.T) {
98+
tests := []struct {
99+
name string
100+
args []string
101+
wantCode int
102+
}{
103+
{
104+
name: "success",
105+
args: []string{"librariangen", "build"},
106+
wantCode: 0,
107+
},
108+
{
109+
name: "failure",
110+
args: []string{"librariangen", "foo"},
111+
wantCode: 1,
112+
},
113+
}
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
if gotCode := runCLI(tt.args); gotCode != tt.wantCode {
117+
t.Errorf("runCLI() = %v, want %v", gotCode, tt.wantCode)
118+
}
119+
})
120+
}
121+
}
122+
123+
func TestParseLogLevel(t *testing.T) {
124+
tests := []struct {
125+
name string
126+
level string
127+
want slog.Level
128+
}{
129+
{"default", "", slog.LevelInfo},
130+
{"debug", "debug", slog.LevelDebug},
131+
{"quiet", "quiet", slog.LevelError + 1},
132+
{"invalid", "foo", slog.LevelInfo},
133+
}
134+
for _, tt := range tests {
135+
t.Run(tt.name, func(t *testing.T) {
136+
if got := parseLogLevel(tt.level); got != tt.want {
137+
t.Errorf("parseLogLevel() = %v, want %v", got, tt.want)
138+
}
139+
})
140+
}
94141
}

0 commit comments

Comments
 (0)