-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathroot_test.go
More file actions
173 lines (141 loc) · 4.43 KB
/
root_test.go
File metadata and controls
173 lines (141 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package cmd
import (
"os"
"testing"
"github.com/spf13/viper"
"github.com/timescale/tiger-cli/internal/tiger/config"
)
func TestMain(m *testing.M) {
// Clean up any global state before tests
config.ResetGlobalConfig()
code := m.Run()
os.Exit(code)
}
func setupTestCommand(t *testing.T) (string, func()) {
t.Helper()
// Create temporary directory for test config
tmpDir, err := os.MkdirTemp("", "tiger-test-cmd-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
// Clean up function
cleanup := func() {
os.RemoveAll(tmpDir)
config.ResetGlobalConfig()
}
t.Cleanup(cleanup)
return tmpDir, cleanup
}
func TestFlagPrecedence(t *testing.T) {
tmpDir, _ := setupTestCommand(t)
// Create config file with some values
configContent := `api_url: https://file.api.com/v1
project_id: file-project
service_id: file-service
output: table
analytics: true
`
configFile := config.GetConfigFile(tmpDir)
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write config file: %v", err)
}
// Set environment variables
os.Setenv("TIGER_CONFIG_DIR", tmpDir)
os.Setenv("TIGER_PROJECT_ID", "env-project")
os.Setenv("TIGER_SERVICE_ID", "env-service")
os.Setenv("TIGER_OUTPUT", "json")
os.Setenv("TIGER_ANALYTICS", "false")
defer func() {
os.Unsetenv("TIGER_CONFIG_DIR")
os.Unsetenv("TIGER_PROJECT_ID")
os.Unsetenv("TIGER_SERVICE_ID")
os.Unsetenv("TIGER_OUTPUT")
os.Unsetenv("TIGER_ANALYTICS")
}()
// Use buildRootCmd() to get a complete root command
testCmd := buildRootCmd()
// Set CLI flags (these should take precedence)
args := []string{
"--config-dir", tmpDir,
"--project-id", "flag-project",
"--service-id", "flag-service",
"--output", "yaml",
"--analytics=false",
"--debug",
"version", // Need a subcommand to execute
}
testCmd.SetArgs(args)
// Execute the command to trigger PersistentPreRunE
err := testCmd.Execute()
if err != nil {
t.Fatalf("Command execution failed: %v", err)
}
// Verify Viper reflects the CLI flag values (highest precedence)
if viper.GetString("project_id") != "flag-project" {
t.Errorf("Expected Viper project_id 'flag-project', got '%s'", viper.GetString("project_id"))
}
if viper.GetString("output") != "yaml" {
t.Errorf("Expected Viper output 'yaml', got '%s'", viper.GetString("output"))
}
}
func TestFlagBindingWithViper(t *testing.T) {
tmpDir, _ := setupTestCommand(t)
// Set environment variable
os.Setenv("TIGER_CONFIG_DIR", tmpDir)
os.Setenv("TIGER_OUTPUT", "json")
defer func() {
os.Unsetenv("TIGER_CONFIG_DIR")
os.Unsetenv("TIGER_OUTPUT")
}()
// Test 1: Environment variable should be used when no flag is set
testCmd1 := buildRootCmd()
testCmd1.SetArgs([]string{"version"}) // Need a subcommand
err := testCmd1.Execute()
if err != nil {
t.Fatalf("Command execution failed: %v", err)
}
if viper.GetString("output") != "json" {
t.Errorf("Expected output 'json' from env var, got '%s'", viper.GetString("output"))
}
// Reset for next test
config.ResetGlobalConfig()
// Test 2: Flag should override environment variable
testCmd2 := buildRootCmd()
testCmd2.SetArgs([]string{"--output", "table", "version"})
err = testCmd2.Execute()
if err != nil {
t.Fatalf("Command execution failed: %v", err)
}
if viper.GetString("output") != "table" {
t.Errorf("Expected output 'table' from flag, got '%s'", viper.GetString("output"))
}
}
func TestConfigFilePrecedence(t *testing.T) {
tmpDir, _ := setupTestCommand(t)
// Create config file
configContent := `output: json
analytics: false
`
configFile := config.GetConfigFile(tmpDir)
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write config file: %v", err)
}
// Set environment that should be overridden by config file
os.Setenv("TIGER_CONFIG_DIR", tmpDir)
defer os.Unsetenv("TIGER_CONFIG_DIR")
// Use buildRootCmd() to get a complete root command
testCmd := buildRootCmd()
// Execute with config file specified
testCmd.SetArgs([]string{"--config-dir", tmpDir, "version"})
err := testCmd.Execute()
if err != nil {
t.Fatalf("Command execution failed: %v", err)
}
// Values should come from config file since no flags were set
if viper.GetString("output") != "json" {
t.Errorf("Expected output 'json' from config file, got '%s'", viper.GetString("output"))
}
if viper.GetBool("analytics") != false {
t.Errorf("Expected analytics false from config file, got %t", viper.GetBool("analytics"))
}
}