Skip to content

Commit

Permalink
fix(config): fix inheritance overriding instead of extending
Browse files Browse the repository at this point in the history
I only checked if values for the parent config were different which
meant it would empty any parent values if the child config didn't have them set.
Will now only update inherited values if they're set on a child config.
  • Loading branch information
blurrah committed Mar 30, 2023
1 parent 0053f1f commit fe7c35b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
17 changes: 11 additions & 6 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,41 @@ type ProjectConfig struct {
func (c *ProjectConfig) extendConfig(o *ProjectConfig) *ProjectConfig {
if o != nil && o != (&ProjectConfig{}) {
cfg := &ProjectConfig{
Name: o.Name,
Framework: o.Framework,
ServerlessFunctionRegion: o.ServerlessFunctionRegion,
BuildCommand: o.BuildCommand,
RootDirectory: o.RootDirectory,
ManualProductionDeployment: o.ManualProductionDeployment,
EnvironmentVariables: o.EnvironmentVariables,
GitRepository: o.GitRepository,
}

if c.Name != o.Name {
if c.Name != "" {
cfg.Name = c.Name
}

if c.Framework != o.Framework {
if c.Framework != "" {
cfg.Framework = c.Framework
}

if c.ServerlessFunctionRegion != o.ServerlessFunctionRegion {
if c.ServerlessFunctionRegion != "" {
cfg.ServerlessFunctionRegion = c.ServerlessFunctionRegion
}

if c.BuildCommand != o.BuildCommand {
if c.BuildCommand != "" {
cfg.BuildCommand = c.BuildCommand
}

if c.RootDirectory != o.RootDirectory {
if c.RootDirectory != "" {
cfg.RootDirectory = c.RootDirectory
}

if c.ManualProductionDeployment != o.ManualProductionDeployment {
cfg.ManualProductionDeployment = c.ManualProductionDeployment
}

if c.GitRepository != o.GitRepository {
if c.GitRepository.Type != "" || c.GitRepository.Repo != "" {
cfg.GitRepository = c.GitRepository
}

Expand Down
41 changes: 41 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,44 @@ func TestExtendEnvironmentVariables(t *testing.T) {
assert.Contains(t, component.Variables, "value = \"testing\"")

}

func TestCompleteInheritance(t *testing.T) {
global := map[string]any{
"team_id": "test-team",
"project_config": map[string]any{
"serverless_function_region": "fra1",
},
}

plugin := NewVercelPlugin()

err := plugin.SetGlobalConfig(global)
require.NoError(t, err)

siteConfig := map[string]any{
"project_config": map[string]any{
"git_repository": map[string]any{
"type": "github",
"repo": "owner/test-repo",
},
},
}

err = plugin.SetSiteConfig("my-site", siteConfig)
require.NoError(t, err)

componentConfig := map[string]any{
"project_config": map[string]any{
"manual_production_deployment": true,
},
}

err = plugin.SetSiteComponentConfig("my-site", "test-component", componentConfig)
require.NoError(t, err)

component, err := plugin.RenderTerraformComponent("my-site", "test-component")

assert.Contains(t, component.Variables, "serverless_function_region = \"fra1\"")
assert.Contains(t, component.Variables, "type = \"github\"")
assert.Contains(t, component.Variables, "manual_production_deployment = true")
}

0 comments on commit fe7c35b

Please sign in to comment.