From fe7c35b8ad7495e4bf531873d07689b1562984fc Mon Sep 17 00:00:00 2001 From: Boris Besemer Date: Thu, 30 Mar 2023 21:52:41 +0200 Subject: [PATCH] fix(config): fix inheritance overriding instead of extending 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. --- internal/config.go | 17 +++++++++++------ internal/plugin_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/internal/config.go b/internal/config.go index 78d3cdc..c4ad169 100644 --- a/internal/config.go +++ b/internal/config.go @@ -53,28 +53,33 @@ 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 } @@ -82,7 +87,7 @@ func (c *ProjectConfig) extendConfig(o *ProjectConfig) *ProjectConfig { cfg.ManualProductionDeployment = c.ManualProductionDeployment } - if c.GitRepository != o.GitRepository { + if c.GitRepository.Type != "" || c.GitRepository.Repo != "" { cfg.GitRepository = c.GitRepository } diff --git a/internal/plugin_test.go b/internal/plugin_test.go index bb79c55..6f4e4c4 100644 --- a/internal/plugin_test.go +++ b/internal/plugin_test.go @@ -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") +}