Skip to content

Commit d3dc4f3

Browse files
committed
added testing and simple workflow
Signed-off-by: Christian Hernandez <christian@chernand.io>
1 parent fdc9506 commit d3dc4f3

File tree

7 files changed

+1461
-0
lines changed

7 files changed

+1461
-0
lines changed

.github/workflows/test.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
push:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: '1.24.1'
21+
22+
- name: Cache Go modules
23+
uses: actions/cache@v3
24+
with:
25+
path: |
26+
~/.cache/go-build
27+
~/go/pkg/mod
28+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
29+
restore-keys: |
30+
${{ runner.os }}-go-
31+
32+
- name: Download dependencies
33+
run: go mod download
34+
35+
- name: Verify dependencies
36+
run: go mod verify
37+
38+
- name: Run go vet
39+
run: go vet ./...
40+
41+
- name: Run tests
42+
run: go test -v ./...
43+
44+
- name: Run tests with race detection
45+
run: go test -race -short ./...

cmd/purge_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright © 2024 Christian Hernandez <christian@chernand.io>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"testing"
20+
)
21+
22+
func TestPurgeFunction(t *testing.T) {
23+
// Test that the purge function exists and can be called
24+
// We can't test actual execution without KIND clusters
25+
// but we can verify the function exists
26+
27+
defer func() {
28+
if r := recover(); r != nil {
29+
// If it panics due to no KIND being available, that's expected
30+
// We just want to make sure it doesn't panic due to the function not existing
31+
}
32+
}()
33+
34+
// The purge function exists if we can reference it without compilation errors
35+
_ = purge
36+
}

cmd/run_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
Copyright © 2024 Christian Hernandez <christian@chernand.io>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"os"
20+
"path/filepath"
21+
"testing"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
func TestProfileLongHelp(t *testing.T) {
27+
helpText := profileLongHelp()
28+
29+
if len(helpText) == 0 {
30+
t.Error("Profile long help should not be empty")
31+
}
32+
33+
// Check that help contains key information
34+
expectedPhrases := []string{
35+
"You can use \"run\" to run the specified profile",
36+
"~/.bekind/profiles/{{name}}",
37+
"config.yaml",
38+
"--view flag",
39+
}
40+
41+
for _, phrase := range expectedPhrases {
42+
if !contains(helpText, phrase) {
43+
t.Errorf("Help text should contain '%s'", phrase)
44+
}
45+
}
46+
}
47+
48+
func TestGetProfileNames(t *testing.T) {
49+
// Create a temporary directory structure for testing
50+
tmpDir := t.TempDir()
51+
52+
// Create test profile directories
53+
testProfiles := []string{"profile1", "profile2", "profile3"}
54+
for _, profile := range testProfiles {
55+
err := os.MkdirAll(filepath.Join(tmpDir, profile), 0755)
56+
if err != nil {
57+
t.Fatalf("Failed to create test profile directory: %v", err)
58+
}
59+
}
60+
61+
// Save original ProfileDir and restore after test
62+
originalProfileDir := ProfileDir
63+
defer func() {
64+
ProfileDir = originalProfileDir
65+
}()
66+
67+
// Set ProfileDir to our test directory
68+
ProfileDir = tmpDir
69+
70+
// Test getProfileNames
71+
profiles, err := getProfileNames()
72+
if err != nil {
73+
t.Fatalf("getProfileNames() returned error: %v", err)
74+
}
75+
76+
if len(profiles) != len(testProfiles) {
77+
t.Errorf("Expected %d profiles, got %d", len(testProfiles), len(profiles))
78+
}
79+
80+
// Check that all expected profiles are present
81+
for _, expectedProfile := range testProfiles {
82+
found := false
83+
for _, actualProfile := range profiles {
84+
if actualProfile == expectedProfile {
85+
found = true
86+
break
87+
}
88+
}
89+
if !found {
90+
t.Errorf("Expected profile '%s' not found in results", expectedProfile)
91+
}
92+
}
93+
}
94+
95+
func TestGetProfileNamesNonexistentDir(t *testing.T) {
96+
// Save original ProfileDir and restore after test
97+
originalProfileDir := ProfileDir
98+
defer func() {
99+
ProfileDir = originalProfileDir
100+
}()
101+
102+
// Set ProfileDir to a nonexistent directory
103+
ProfileDir = "/nonexistent/path"
104+
105+
// Test getProfileNames with nonexistent directory
106+
profiles, err := getProfileNames()
107+
if err == nil {
108+
t.Error("getProfileNames() should return error for nonexistent directory")
109+
}
110+
111+
if len(profiles) != 0 {
112+
t.Errorf("Expected empty profiles slice for nonexistent directory, got %d profiles", len(profiles))
113+
}
114+
}
115+
116+
func TestProfileValidArgs(t *testing.T) {
117+
// Create a temporary directory structure for testing
118+
tmpDir := t.TempDir()
119+
120+
// Create test profile directories
121+
testProfiles := []string{"test-profile1", "test-profile2"}
122+
for _, profile := range testProfiles {
123+
err := os.MkdirAll(filepath.Join(tmpDir, profile), 0755)
124+
if err != nil {
125+
t.Fatalf("Failed to create test profile directory: %v", err)
126+
}
127+
}
128+
129+
// Save original ProfileDir and restore after test
130+
originalProfileDir := ProfileDir
131+
defer func() {
132+
ProfileDir = originalProfileDir
133+
}()
134+
135+
// Set ProfileDir to our test directory
136+
ProfileDir = tmpDir
137+
138+
// Test profileValidArgs with no existing args (should return profiles)
139+
completions, directive := profileValidArgs(runCmd, []string{}, "")
140+
141+
if directive != cobra.ShellCompDirectiveNoFileComp {
142+
t.Errorf("Expected ShellCompDirectiveNoFileComp, got %v", directive)
143+
}
144+
145+
if len(completions) != len(testProfiles) {
146+
t.Errorf("Expected %d completions, got %d", len(testProfiles), len(completions))
147+
}
148+
149+
// Test profileValidArgs with existing args (should return no completions)
150+
completions, directive = profileValidArgs(runCmd, []string{"profile1"}, "")
151+
152+
if directive != cobra.ShellCompDirectiveNoFileComp {
153+
t.Errorf("Expected ShellCompDirectiveNoFileComp, got %v", directive)
154+
}
155+
156+
if len(completions) != 0 {
157+
t.Errorf("Expected no completions when args already provided, got %d", len(completions))
158+
}
159+
}
160+
161+
// Helper function for string contains check
162+
func contains(s, substr string) bool {
163+
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
164+
func() bool {
165+
for i := 0; i <= len(s)-len(substr); i++ {
166+
if s[i:i+len(substr)] == substr {
167+
return true
168+
}
169+
}
170+
return false
171+
}())
172+
}

0 commit comments

Comments
 (0)