|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAppDatabaseConfig_GetMigrationsPath(t *testing.T) { |
| 13 | + t.Run("returns configured path", func(t *testing.T) { |
| 14 | + cfg := AppDatabaseConfig{MigrationsPath: "./db/migrations"} |
| 15 | + assert.Equal(t, "./db/migrations", cfg.GetMigrationsPath()) |
| 16 | + }) |
| 17 | + |
| 18 | + t.Run("returns empty when not set", func(t *testing.T) { |
| 19 | + cfg := AppDatabaseConfig{} |
| 20 | + assert.Equal(t, "", cfg.GetMigrationsPath()) |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +func TestAppDatabaseConfig_GetSeedsPath(t *testing.T) { |
| 25 | + t.Run("returns configured path", func(t *testing.T) { |
| 26 | + cfg := AppDatabaseConfig{SeedsPath: "./db/seeds"} |
| 27 | + assert.Equal(t, "./db/seeds", cfg.GetSeedsPath()) |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("returns empty when not set", func(t *testing.T) { |
| 31 | + cfg := AppDatabaseConfig{} |
| 32 | + assert.Equal(t, "", cfg.GetSeedsPath()) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +func TestLoadAppConfig_WithDatabaseSection(t *testing.T) { |
| 37 | + tmpDir := t.TempDir() |
| 38 | + |
| 39 | + configContent := `app: |
| 40 | + name: api-gateway |
| 41 | + type: web |
| 42 | + version: "1.0.0" |
| 43 | +database: |
| 44 | + migrations_path: ./custom/migrations |
| 45 | + seeds_path: ./custom/seeds |
| 46 | + connection: production |
| 47 | +` |
| 48 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, ".forge.yaml"), []byte(configContent), 0644)) |
| 49 | + |
| 50 | + cfg, err := LoadAppConfig(tmpDir) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + assert.Equal(t, "api-gateway", cfg.App.Name) |
| 54 | + assert.Equal(t, "web", cfg.App.Type) |
| 55 | + assert.Equal(t, "./custom/migrations", cfg.Database.MigrationsPath) |
| 56 | + assert.Equal(t, "./custom/seeds", cfg.Database.SeedsPath) |
| 57 | + assert.Equal(t, "production", cfg.Database.Connection) |
| 58 | +} |
| 59 | + |
| 60 | +func TestLoadAppConfig_WithoutDatabaseSection(t *testing.T) { |
| 61 | + tmpDir := t.TempDir() |
| 62 | + |
| 63 | + configContent := `app: |
| 64 | + name: worker |
| 65 | + type: worker |
| 66 | +` |
| 67 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, ".forge.yaml"), []byte(configContent), 0644)) |
| 68 | + |
| 69 | + cfg, err := LoadAppConfig(tmpDir) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + assert.Equal(t, "worker", cfg.App.Name) |
| 73 | + // Database config should be zero-valued |
| 74 | + assert.Equal(t, "", cfg.Database.MigrationsPath) |
| 75 | + assert.Equal(t, "", cfg.Database.SeedsPath) |
| 76 | + assert.Equal(t, "", cfg.Database.Connection) |
| 77 | +} |
| 78 | + |
| 79 | +func TestLoadAppConfig_YmlExtension(t *testing.T) { |
| 80 | + tmpDir := t.TempDir() |
| 81 | + |
| 82 | + configContent := `app: |
| 83 | + name: my-service |
| 84 | + type: web |
| 85 | +database: |
| 86 | + migrations_path: ./migrations |
| 87 | +` |
| 88 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, ".forge.yml"), []byte(configContent), 0644)) |
| 89 | + |
| 90 | + cfg, err := LoadAppConfig(tmpDir) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + assert.Equal(t, "my-service", cfg.App.Name) |
| 94 | + assert.Equal(t, "./migrations", cfg.Database.MigrationsPath) |
| 95 | +} |
| 96 | + |
| 97 | +func TestLoadAppConfig_NotFound(t *testing.T) { |
| 98 | + tmpDir := t.TempDir() |
| 99 | + |
| 100 | + _, err := LoadAppConfig(tmpDir) |
| 101 | + assert.Error(t, err) |
| 102 | + assert.Contains(t, err.Error(), "no .forge.yaml or .forge.yml found") |
| 103 | +} |
| 104 | + |
| 105 | +func TestLoadAppConfig_InvalidYAML(t *testing.T) { |
| 106 | + tmpDir := t.TempDir() |
| 107 | + |
| 108 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, ".forge.yaml"), []byte("invalid: yaml: content: ["), 0644)) |
| 109 | + |
| 110 | + _, err := LoadAppConfig(tmpDir) |
| 111 | + assert.Error(t, err) |
| 112 | +} |
| 113 | + |
| 114 | +func TestLoadAppConfig_InternalFieldsSet(t *testing.T) { |
| 115 | + tmpDir := t.TempDir() |
| 116 | + |
| 117 | + configContent := `app: |
| 118 | + name: test-app |
| 119 | +` |
| 120 | + configPath := filepath.Join(tmpDir, ".forge.yaml") |
| 121 | + require.NoError(t, os.WriteFile(configPath, []byte(configContent), 0644)) |
| 122 | + |
| 123 | + cfg, err := LoadAppConfig(tmpDir) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + assert.Equal(t, tmpDir, cfg.AppDir) |
| 127 | + assert.Equal(t, configPath, cfg.ConfigPath) |
| 128 | +} |
| 129 | + |
| 130 | +func TestLoadAppConfig_DatabaseMigrationsPathOnly(t *testing.T) { |
| 131 | + tmpDir := t.TempDir() |
| 132 | + |
| 133 | + configContent := `app: |
| 134 | + name: api |
| 135 | +database: |
| 136 | + migrations_path: ./db/migrate |
| 137 | +` |
| 138 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, ".forge.yaml"), []byte(configContent), 0644)) |
| 139 | + |
| 140 | + cfg, err := LoadAppConfig(tmpDir) |
| 141 | + require.NoError(t, err) |
| 142 | + |
| 143 | + assert.Equal(t, "./db/migrate", cfg.Database.GetMigrationsPath()) |
| 144 | + assert.Equal(t, "", cfg.Database.GetSeedsPath()) |
| 145 | + assert.Equal(t, "", cfg.Database.Connection) |
| 146 | +} |
0 commit comments