Skip to content

Commit 8653f92

Browse files
authored
Merge pull request #2 from continuedev/nate/fix-tests
Nate/fix tests
2 parents d415d69 + 7a4c6fa commit 8653f92

10 files changed

Lines changed: 431 additions & 8 deletions

File tree

.github/workflows/test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jobs:
2020
go-version: "1.24.2"
2121
check-latest: true
2222

23+
# Run this first to fail fast if tests fail
24+
- name: Run Go tests
25+
run: go test ./... -v
26+
2327
- name: Setup Node.js
2428
uses: actions/setup-node@v3
2529
with:
@@ -29,9 +33,6 @@ jobs:
2933
- name: Install dependencies
3034
run: npm ci
3135

32-
- name: Run Go tests
33-
run: go test ./...
34-
3536
- name: Build binaries
3637
run: node build.js
3738

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ rules-cli
44
.rules
55
/rules
66
test-rule.yaml
7+
e.sh
78

89
.github/instructions
910
.sourcegraph

cmd/root_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func TestRootCommand(t *testing.T) {
13+
// Test that the root command executes without error
14+
old := os.Args
15+
defer func() { os.Args = old }()
16+
17+
// Test help command
18+
os.Args = []string{"rules", "--help"}
19+
20+
// Capture output
21+
var buf bytes.Buffer
22+
rootCmd.SetOut(&buf)
23+
rootCmd.SetErr(&buf)
24+
25+
err := rootCmd.Execute()
26+
if err != nil {
27+
t.Fatalf("Root command with --help failed: %v", err)
28+
}
29+
30+
output := buf.String()
31+
if !strings.Contains(output, "command-line tool to create, manage, and convert rule sets") {
32+
t.Error("Help output should contain description")
33+
}
34+
}
35+
36+
func TestVersionFlag(t *testing.T) {
37+
// Set version for testing
38+
originalVersion := Version
39+
Version = "test-version"
40+
defer func() { Version = originalVersion }()
41+
42+
// Test by directly setting the version flag and checking behavior
43+
oldVersion := version
44+
version = true
45+
defer func() { version = oldVersion }()
46+
47+
// Test that when version flag is true, the command behaves correctly
48+
if !version {
49+
t.Error("Version flag should be set to true for testing")
50+
}
51+
52+
// Test that Version variable is set correctly
53+
if Version != "test-version" {
54+
t.Errorf("Version should be 'test-version', got '%s'", Version)
55+
}
56+
}
57+
58+
func TestVersionFlagBehavior(t *testing.T) {
59+
// Test version flag behavior by parsing command line
60+
originalVersion := Version
61+
Version = "test-version-behavior"
62+
defer func() { Version = originalVersion }()
63+
64+
// Create a new command instance to avoid state issues
65+
testCmd := &cobra.Command{
66+
Use: "rules",
67+
Run: func(cmd *cobra.Command, args []string) {
68+
if version {
69+
// This mimics the actual behavior in root.go
70+
return
71+
}
72+
},
73+
}
74+
75+
testCmd.Flags().BoolVarP(&version, "version", "v", false, "Display version information")
76+
77+
// Test parsing the version flag
78+
err := testCmd.ParseFlags([]string{"-v"})
79+
if err != nil {
80+
t.Fatalf("Failed to parse version flag: %v", err)
81+
}
82+
83+
// The version variable should now be true
84+
if !version {
85+
t.Error("Version flag should be true after parsing -v")
86+
}
87+
}
88+
89+
func TestRootCommandUsage(t *testing.T) {
90+
// Test that the root command has the correct usage string
91+
if rootCmd.Use != "rules" {
92+
t.Errorf("Expected Use to be 'rules', got %s", rootCmd.Use)
93+
}
94+
95+
if rootCmd.Short == "" {
96+
t.Error("Short description should not be empty")
97+
}
98+
99+
if rootCmd.Long == "" {
100+
t.Error("Long description should not be empty")
101+
}
102+
}
103+
104+
func TestInitializeCommand(t *testing.T) {
105+
// Test that the command initializes without panicking
106+
defer func() {
107+
if r := recover(); r != nil {
108+
t.Errorf("Command initialization panicked: %v", r)
109+
}
110+
}()
111+
112+
// This should not panic
113+
_ = rootCmd
114+
}

internal/auth/workos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func GetAuthUrlForTokenPage() string {
125125
params.Add("response_type", "code")
126126
params.Add("client_id", viper.GetString("workos_client_id"))
127127

128-
redirectPath := "tokens/callback?clientName=rules"
128+
redirectPath := "tokens/callback/rules"
129129
params.Add("redirect_uri", fmt.Sprintf("%s%s", viper.GetString("app_url"), redirectPath))
130130

131131
params.Add("state", uuid.New().String())

internal/config/config_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestInitialize(t *testing.T) {
9+
// Test basic initialization
10+
cfg, err := Initialize()
11+
if err != nil {
12+
t.Fatalf("Initialize() failed: %v", err)
13+
}
14+
15+
if cfg == nil {
16+
t.Fatal("Initialize() returned nil config")
17+
}
18+
19+
// Test default values
20+
if cfg.RegistryURL == "" {
21+
t.Error("RegistryURL should not be empty")
22+
}
23+
24+
if cfg.DefaultFormat == "" {
25+
t.Error("DefaultFormat should not be empty")
26+
}
27+
28+
// Test that default values are set correctly
29+
expectedRegistryURL := "https://api.continue.dev"
30+
if cfg.RegistryURL != expectedRegistryURL {
31+
t.Errorf("Expected RegistryURL to be %s, got %s", expectedRegistryURL, cfg.RegistryURL)
32+
}
33+
34+
expectedDefaultFormat := "default"
35+
if cfg.DefaultFormat != expectedDefaultFormat {
36+
t.Errorf("Expected DefaultFormat to be %s, got %s", expectedDefaultFormat, cfg.DefaultFormat)
37+
}
38+
}
39+
40+
func TestLoadConfig(t *testing.T) {
41+
// Test LoadConfig function
42+
cfg, err := LoadConfig()
43+
if err != nil {
44+
t.Fatalf("LoadConfig() failed: %v", err)
45+
}
46+
47+
if cfg == nil {
48+
t.Fatal("LoadConfig() returned nil config")
49+
}
50+
}
51+
52+
func TestInitializeWithEnvironmentVariables(t *testing.T) {
53+
// Set environment variables
54+
os.Setenv("RULES_REGISTRY_URL", "https://test.example.com")
55+
os.Setenv("RULES_DEFAULT_FORMAT", "test-format")
56+
os.Setenv("RULES_USERNAME", "testuser")
57+
os.Setenv("RULES_EMAIL", "test@example.com")
58+
59+
defer func() {
60+
// Clean up environment variables
61+
os.Unsetenv("RULES_REGISTRY_URL")
62+
os.Unsetenv("RULES_DEFAULT_FORMAT")
63+
os.Unsetenv("RULES_USERNAME")
64+
os.Unsetenv("RULES_EMAIL")
65+
}()
66+
67+
cfg, err := Initialize()
68+
if err != nil {
69+
t.Fatalf("Initialize() with env vars failed: %v", err)
70+
}
71+
72+
// Test that environment variables override defaults
73+
if cfg.RegistryURL != "https://test.example.com" {
74+
t.Errorf("Expected RegistryURL to be overridden by env var, got %s", cfg.RegistryURL)
75+
}
76+
77+
if cfg.DefaultFormat != "test-format" {
78+
t.Errorf("Expected DefaultFormat to be overridden by env var, got %s", cfg.DefaultFormat)
79+
}
80+
81+
if cfg.Username != "testuser" {
82+
t.Errorf("Expected Username to be set by env var, got %s", cfg.Username)
83+
}
84+
85+
if cfg.Email != "test@example.com" {
86+
t.Errorf("Expected Email to be set by env var, got %s", cfg.Email)
87+
}
88+
}

main_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"rules-cli/cmd"
7+
)
8+
9+
func TestVersionSetting(t *testing.T) {
10+
// Test that the version is passed to the cmd package
11+
originalVersion := Version
12+
originalCmdVersion := cmd.Version
13+
14+
defer func() {
15+
Version = originalVersion
16+
cmd.Version = originalCmdVersion
17+
}()
18+
19+
Version = "test-main-version"
20+
cmd.Version = Version
21+
22+
if cmd.Version != "test-main-version" {
23+
t.Errorf("Expected cmd.Version to be 'test-main-version', got %s", cmd.Version)
24+
}
25+
}
26+
27+
func TestMainFunction(t *testing.T) {
28+
// Test that main function exists and doesn't panic when version is set
29+
defer func() {
30+
if r := recover(); r != nil {
31+
t.Errorf("main() related functions panicked: %v", r)
32+
}
33+
}()
34+
35+
// Test that Version variable exists and can be set
36+
originalVersion := Version
37+
Version = "test"
38+
if Version != "test" {
39+
t.Error("Version variable should be settable")
40+
}
41+
Version = originalVersion
42+
}

tests/golden/login/login.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Starting login process...
22

33
Starting authentication with Continue...
4-
Opening browser to sign in at: https://api.workos.com/user_management/authorize?client_id=client_01J0FW6XN8N2XJAECF7NE0Y65J&provider=authkit&redirect_uri=https%3A%2F%2Fhub.continue.dev%2Ftokens%2Fcallback&response_type=code&state=<STATE_PLACEHOLDER>
4+
Opening browser to sign in at: https://api.workos.com/user_management/authorize?client_id=client_01J0FW6XN8N2XJAECF7NE0Y65J&provider=authkit&redirect_uri=https%3A%2F%2Fhub.continue.dev%2Ftokens%2Fcallback%2Frules&response_type=code&state=<STATE_PLACEHOLDER>
55

66
After signing in, you'll receive a token.
77
Paste your sign-in token here: Login failed: <ERROR_PLACEHOLDER>

tests/golden/v/version.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rules version dev
1+
rules version <VERSION_PLACEHOLDER>

0 commit comments

Comments
 (0)