Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type API struct {
func ReadConfigFromFile(file string) (*Config, error) {
// Check if file exists first
if _, err := os.Stat(file); os.IsNotExist(err) {
slog.Error("Config file does not exist", "file", file)
return nil, err
slog.Warn("Config file does not exist, using default configuration", "file", file)
return &Config{APIs: make(map[string]API)}, nil
}

var c Config
Expand Down
27 changes: 20 additions & 7 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"errors"
"os"
"path/filepath"
"testing"

Expand All @@ -14,10 +12,11 @@ func TestConfigEndToEnd(t *testing.T) {
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "test_config.toml")

// First read should fail since file doesn't exist
_, err := ReadConfigFromFile(testFile)
assert.Error(t, err)
assert.True(t, errors.Is(err, os.ErrNotExist))
// First read should return a default configuration since file doesn't exist
cfg, err := ReadConfigFromFile(testFile)
assert.NoError(t, err)
assert.NotNil(t, cfg)
assert.Empty(t, cfg.APIs)

// Create test API config
testAPI := API{
Expand All @@ -33,7 +32,7 @@ func TestConfigEndToEnd(t *testing.T) {
assert.NoError(t, err)

// Read config back and verify contents
cfg, err := ReadConfigFromFile(testFile)
cfg, err = ReadConfigFromFile(testFile)
assert.NoError(t, err)
assert.NotNil(t, cfg)
assert.Len(t, cfg.APIs, 1)
Expand Down Expand Up @@ -61,3 +60,17 @@ func TestWriteAPIWithEmptyName(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "api name cannot be empty", err.Error())
}

func TestReadConfigFromFile_NoFile(t *testing.T) {
// Create a temporary directory
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "nonexistent_config.toml")

// Attempt to read the config file
cfg, err := ReadConfigFromFile(testFile)

// Verify no error is returned and a default config is provided
assert.NoError(t, err)
assert.NotNil(t, cfg)
assert.Empty(t, cfg.APIs)
}