Skip to content

Commit 556ff32

Browse files
test: add unit coverage for ci config accessors
Add tests for CIConfig accessor methods and resolveBuilder helper in cmd/ci/config.go. Covered: - FnGitHubWorkflowFilepath correctly joins fnRoot, workflow dir and filename - OutputPath uses the default dir/filename constants - Verbose, FnRuntime, FnBuilder simple getters - resolveBuilder — all supported runtimes (go, node, typescript, rust, quarkus, springboot, python) both local and remote modes, plus error for unknown runtime - All remaining bool accessors: RegistryLogin, SelfHostedRunner, RemoteBuild, WorkflowDispatch, TestStep, Force - All string accessors: Branch, WorkflowName, KubeconfigSecret, RegistryLoginUrlVar, RegistryUserVar, RegistryPassSecret, RegistryUrlVar Fixes zero-coverage on config.go:221, 226, 282, 286, 290.
1 parent e92b074 commit 556ff32

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

cmd/ci/config_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package ci
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
// ---------------------------------------------------------------------------
11+
// Pure accessor methods on a hand-built CIConfig
12+
// ---------------------------------------------------------------------------
13+
14+
func TestCIConfig_Verbose(t *testing.T) {
15+
assert.Assert(t, !CIConfig{verbose: false}.Verbose())
16+
assert.Assert(t, CIConfig{verbose: true}.Verbose())
17+
}
18+
19+
func TestCIConfig_FnRuntime(t *testing.T) {
20+
assert.Equal(t, CIConfig{fnRuntime: "go"}.FnRuntime(), "go")
21+
assert.Equal(t, CIConfig{fnRuntime: "python"}.FnRuntime(), "python")
22+
}
23+
24+
func TestCIConfig_FnBuilder(t *testing.T) {
25+
assert.Equal(t, CIConfig{fnBuilder: "host"}.FnBuilder(), "host")
26+
assert.Equal(t, CIConfig{fnBuilder: "pack"}.FnBuilder(), "pack")
27+
assert.Equal(t, CIConfig{fnBuilder: "s2i"}.FnBuilder(), "s2i")
28+
}
29+
30+
func TestCIConfig_OutputPath(t *testing.T) {
31+
cc := CIConfig{
32+
githubWorkflowDir: DefaultGitHubWorkflowDir,
33+
githubWorkflowFilename: DefaultGitHubWorkflowFilename,
34+
}
35+
want := filepath.Join(DefaultGitHubWorkflowDir, DefaultGitHubWorkflowFilename)
36+
assert.Equal(t, cc.OutputPath(), want)
37+
}
38+
39+
func TestCIConfig_FnGitHubWorkflowFilepath(t *testing.T) {
40+
root := "/home/user/myfunc"
41+
cc := CIConfig{
42+
fnRoot: root,
43+
githubWorkflowDir: DefaultGitHubWorkflowDir,
44+
githubWorkflowFilename: DefaultGitHubWorkflowFilename,
45+
}
46+
want := filepath.Join(root, DefaultGitHubWorkflowDir, DefaultGitHubWorkflowFilename)
47+
assert.Equal(t, cc.FnGitHubWorkflowFilepath(), want)
48+
}
49+
50+
// ---------------------------------------------------------------------------
51+
// resolveBuilder — the main untested code block from config.go
52+
// ---------------------------------------------------------------------------
53+
54+
func TestResolveBuilder(t *testing.T) {
55+
tests := []struct {
56+
name string
57+
runtime string
58+
remote bool
59+
want string
60+
wantErr bool
61+
}{
62+
{name: "go local", runtime: "go", remote: false, want: "host"},
63+
{name: "go remote", runtime: "go", remote: true, want: "pack"},
64+
{name: "node", runtime: "node", remote: false, want: "pack"},
65+
{name: "typescript", runtime: "typescript", remote: false, want: "pack"},
66+
{name: "rust", runtime: "rust", remote: false, want: "pack"},
67+
{name: "quarkus", runtime: "quarkus", remote: false, want: "pack"},
68+
{name: "springboot", runtime: "springboot", remote: false, want: "pack"},
69+
{name: "python local", runtime: "python", remote: false, want: "host"},
70+
{name: "python remote", runtime: "python", remote: true, want: "s2i"},
71+
{name: "unknown runtime", runtime: "fortran", remote: false, wantErr: true},
72+
}
73+
74+
for _, tc := range tests {
75+
t.Run(tc.name, func(t *testing.T) {
76+
got, err := resolveBuilder(tc.runtime, tc.remote)
77+
if tc.wantErr {
78+
assert.Assert(t, err != nil, "expected an error for runtime %q", tc.runtime)
79+
return
80+
}
81+
assert.NilError(t, err)
82+
assert.Equal(t, got, tc.want)
83+
})
84+
}
85+
}
86+
87+
// ---------------------------------------------------------------------------
88+
// Additional simple bool/string accessors
89+
// ---------------------------------------------------------------------------
90+
91+
func TestCIConfig_BoolAccessors(t *testing.T) {
92+
cc := CIConfig{
93+
registryLogin: true,
94+
selfHostedRunner: true,
95+
remoteBuild: true,
96+
workflowDispatch: true,
97+
testStep: true,
98+
force: true,
99+
}
100+
assert.Assert(t, cc.RegistryLogin())
101+
assert.Assert(t, cc.SelfHostedRunner())
102+
assert.Assert(t, cc.RemoteBuild())
103+
assert.Assert(t, cc.WorkflowDispatch())
104+
assert.Assert(t, cc.TestStep())
105+
assert.Assert(t, cc.Force())
106+
}
107+
108+
func TestCIConfig_StringAccessors(t *testing.T) {
109+
cc := CIConfig{
110+
branch: "feature-x",
111+
workflowName: "My Deploy",
112+
kubeconfigSecret: "MY_KUBECONFIG",
113+
registryLoginUrlVar: "MY_REGISTRY_URL",
114+
registryUserVar: "MY_USER",
115+
registryPassSecret: "MY_PASS",
116+
registryUrlVar: "MY_REG",
117+
}
118+
assert.Equal(t, cc.Branch(), "feature-x")
119+
assert.Equal(t, cc.WorkflowName(), "My Deploy")
120+
assert.Equal(t, cc.KubeconfigSecret(), "MY_KUBECONFIG")
121+
assert.Equal(t, cc.RegistryLoginUrlVar(), "MY_REGISTRY_URL")
122+
assert.Equal(t, cc.RegistryUserVar(), "MY_USER")
123+
assert.Equal(t, cc.RegistryPassSecret(), "MY_PASS")
124+
assert.Equal(t, cc.RegistryUrlVar(), "MY_REG")
125+
}

0 commit comments

Comments
 (0)