MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / TestLoadConfigFromFile

Function TestLoadConfigFromFile

config/config_test.go:136–197  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

134}
135
136func TestLoadConfigFromFile(t *testing.T) {
137 // Create a temporary file
138 tmpFile, err := os.CreateTemp("", "ledgerforge.json")
139 if err != nil {
140 t.Fatalf("Unable to create temporary file: %v", err)
141 }
142 defer os.Remove(tmpFile.Name()) // Clean up after the test
143
144 // Sample configuration to write to the temp file
145 sampleConfig := Configuration{
146 ProjectName: "Temp Project",
147 DataSource: DataSourceConfig{
148 Dns: "temp-dns",
149 },
150 Redis: RedisConfig{
151 Dns: "temp-redis",
152 },
153 Transaction: TransactionConfig{
154 LockWaitTimeout: 7,
155 },
156 }
157 if err := json.NewEncoder(tmpFile).Encode(sampleConfig); err != nil {
158 t.Fatalf("Unable to write to temporary file: %v", err)
159 }
160 if err := tmpFile.Close(); err != nil {
161 t.Fatalf("Failed to close temp file: %v", err)
162 } // Close the file so loadConfigFromFile can open it
163
164 // Set an environment variable to override the project name
165 if err := os.Setenv("LEDGERFORGE_PROJECT_NAME", "Env Project"); err != nil {
166 t.Fatalf("Failed to set env: %v", err)
167 }
168 defer func() {
169 if err := os.Unsetenv("LEDGERFORGE_PROJECT_NAME"); err != nil {
170 t.Errorf("Failed to unset env: %v", err)
171 }
172 }() // Clean up after the test
173
174 // Load the configuration from the file
175 if err := loadConfigFromFile(tmpFile.Name()); err != nil {
176 t.Fatalf("loadConfigFromFile failed: %v", err)
177 }
178
179 // Fetch the loaded configuration
180 loadedConfig, err := Fetch()
181 if err != nil {
182 t.Fatalf("Fetch failed: %v", err)
183 }
184
185 // Check if the environment variable override worked
186 if loadedConfig.ProjectName != "Env Project" {
187 t.Errorf("Expected ProjectName to be 'Env Project', got '%s'", loadedConfig.ProjectName)
188 }
189
190 // Check if the DNS was loaded correctly from the file
191 if loadedConfig.DataSource.Dns != "temp-dns" {
192 t.Errorf("Expected DataSource.Dns to be 'temp-dns', got '%s'", loadedConfig.DataSource.Dns)
193 }

Callers

nothing calls this directly

Calls 3

loadConfigFromFileFunction · 0.85
FetchFunction · 0.85
CloseMethod · 0.45

Tested by

no test coverage detected