Repository navigation
fix(up): compose workspace env by target precedence #1297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c8f27d3
fb5d08b
f9bf4dd
70dcc74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package up | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| type workspaceEnvAssignment struct { | ||
| name string | ||
| value string | ||
| } | ||
|
|
||
| type resolvedEnvVar struct { | ||
| assignment string | ||
| } | ||
|
|
||
| func parseWorkspaceEnvAssignment(raw string) (workspaceEnvAssignment, error) { | ||
| name, value, ok := strings.Cut(raw, "=") | ||
| if !ok || name == "" { | ||
| return workspaceEnvAssignment{}, fmt.Errorf("invalid workspace environment assignment") | ||
| } | ||
| return workspaceEnvAssignment{name: name, value: value}, nil | ||
| } | ||
|
|
||
| func indexWorkspaceEnv(assignments []string) (map[string]workspaceEnvAssignment, error) { | ||
| indexed := make(map[string]workspaceEnvAssignment, len(assignments)) | ||
| for _, raw := range assignments { | ||
| assignment, err := parseWorkspaceEnvAssignment(raw) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if _, exists := indexed[assignment.name]; exists { | ||
| return nil, fmt.Errorf( | ||
| "workspace environment variable %q is assigned more than once", | ||
| assignment.name, | ||
| ) | ||
| } | ||
| indexed[assignment.name] = assignment | ||
| } | ||
| return indexed, nil | ||
| } | ||
|
|
||
| func filterWorkspaceEnvRequests( | ||
| base map[string]workspaceEnvAssignment, | ||
| requests []envVarRequest, | ||
| ) ([]envVarRequest, error) { | ||
| filtered := make([]envVarRequest, 0, len(requests)) | ||
| for _, request := range requests { | ||
| if _, exists := base[request.target]; !exists { | ||
| filtered = append(filtered, request) | ||
| continue | ||
| } | ||
| if request.origin == envVarAttached { | ||
| continue | ||
| } | ||
| return nil, fmt.Errorf( | ||
| "workspace environment target %q is set by both --workspace-env and --env; remove one explicit assignment", | ||
| request.target, | ||
| ) | ||
| } | ||
| return filtered, nil | ||
| } | ||
|
|
||
| func composeWorkspaceEnv( | ||
| base map[string]workspaceEnvAssignment, | ||
| managed []resolvedEnvVar, | ||
| ) ([]string, error) { | ||
| for _, resolved := range managed { | ||
| assignment, err := parseWorkspaceEnvAssignment(resolved.assignment) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if _, exists := base[assignment.name]; exists { | ||
| return nil, fmt.Errorf( | ||
| "workspace environment variable %q is assigned more than once", | ||
| assignment.name, | ||
| ) | ||
| } | ||
| base[assignment.name] = assignment | ||
| } | ||
|
|
||
| keys := make([]string, 0, len(base)) | ||
| for key := range base { | ||
| keys = append(keys, key) | ||
| } | ||
| sort.Strings(keys) | ||
| result := make([]string, 0, len(keys)) | ||
| for _, key := range keys { | ||
| assignment := base[key] | ||
| result = append(result, assignment.name+"="+assignment.value) | ||
| } | ||
| return result, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package up | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| secretspkg "github.com/devsy-org/devsy/pkg/secrets" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| const ( | ||
| workspaceEnvLogLevel = "LOG_LEVEL" | ||
| workspaceEnvLiteralAssignment = "LOG_LEVEL=custom-value" | ||
| ) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: AGENTS.md (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| type WorkspaceEnvTestSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| func TestWorkspaceEnvTestSuite(t *testing.T) { | ||
| suite.Run(t, new(WorkspaceEnvTestSuite)) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestAttachmentFillsMissingTarget() { | ||
| base, err := indexWorkspaceEnv([]string{"LOCAL=value"}) | ||
| s.Require().NoError(err) | ||
| requests, err := filterWorkspaceEnvRequests(base, []envVarRequest{{ | ||
| ref: localRef("ATTACHED"), target: "ATTACHED", origin: envVarAttached, | ||
| }}) | ||
| s.Require().NoError(err) | ||
| s.Require().Len(requests, 1) | ||
| got, err := composeWorkspaceEnv(base, []resolvedEnvVar{{ | ||
| assignment: "ATTACHED=managed", | ||
| }}) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal([]string{"ATTACHED=managed", "LOCAL=value"}, got) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestLiteralWorkspaceEnvShadowsAttachment() { | ||
| cmd := &UpCmd{} | ||
| cmd.WorkspaceEnv = []string{workspaceEnvLiteralAssignment} | ||
| resolver := secretspkg.NewResolver() | ||
| s.Require().NoError(resolver.Register("local", "local", fixedSource{ | ||
| values: map[string]string{workspaceEnvLogLevel: "stored-value"}, | ||
| })) | ||
|
|
||
| err := cmd.applyEnvVars(s.T().Context(), testEnvConfig(workspaceEnvLogLevel), resolver) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal([]string{workspaceEnvLiteralAssignment}, cmd.WorkspaceEnv) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestShadowingDoesNotDependOnValueLexicalOrder() { | ||
| cmd := &UpCmd{} | ||
| cmd.WorkspaceEnv = []string{workspaceEnvLogLevel + "=stored-value"} | ||
| resolver := secretspkg.NewResolver() | ||
| s.Require().NoError(resolver.Register("local", "local", fixedSource{ | ||
| values: map[string]string{workspaceEnvLogLevel: "custom-value"}, | ||
| })) | ||
|
|
||
| err := cmd.applyEnvVars( | ||
| s.T().Context(), | ||
| testEnvConfig(workspaceEnvLogLevel), | ||
| resolver, | ||
| ) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal([]string{workspaceEnvLogLevel + "=stored-value"}, cmd.WorkspaceEnv) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestShadowedAttachmentIsNotResolved() { | ||
| cmd := &UpCmd{} | ||
| cmd.WorkspaceEnv = []string{workspaceEnvLogLevel + "=local"} | ||
| resolver := secretspkg.NewResolver() | ||
| s.Require().NoError(resolver.Register("local", "local", unavailableEnvSource{})) | ||
|
|
||
| err := cmd.applyEnvVars(s.T().Context(), testEnvConfig(workspaceEnvLogLevel), resolver) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal([]string{workspaceEnvLogLevel + "=local"}, cmd.WorkspaceEnv) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestDuplicateShadowedAttachmentsAreIgnored() { | ||
| cmd := &UpCmd{} | ||
| cmd.WorkspaceEnv = []string{workspaceEnvLogLevel + "=local"} | ||
| err := cmd.applyEnvVars( | ||
| s.T().Context(), | ||
| testEnvConfig(workspaceEnvLogLevel, workspaceEnvLogLevel), | ||
| secretspkg.NewResolver(), | ||
| ) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal([]string{workspaceEnvLogLevel + "=local"}, cmd.WorkspaceEnv) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestExplicitManagedTargetConflictsWithLiteralTarget() { | ||
| cmd := &UpCmd{} | ||
| cmd.WorkspaceEnv = []string{"APP_MODE=local"} | ||
| cmd.EnvVars = []string{"STORED_MODE=APP_MODE"} | ||
| err := cmd.applyEnvVars(s.T().Context(), testEnvConfig(), secretspkg.NewResolver()) | ||
| s.Require().Error(err) | ||
| s.Assert().Contains(err.Error(), "APP_MODE") | ||
| s.Assert().Contains(err.Error(), "both --workspace-env and --env") | ||
| s.Assert().NotContains(err.Error(), "local") | ||
| s.Assert().Equal([]string{"APP_MODE=local"}, cmd.WorkspaceEnv) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestDuplicateLiteralTargetRejected() { | ||
| cmd := &UpCmd{} | ||
| cmd.WorkspaceEnv = []string{"APP_MODE=a", "APP_MODE=b"} | ||
| err := cmd.applyEnvVars(s.T().Context(), testEnvConfig(), secretspkg.NewResolver()) | ||
| s.Require().Error(err) | ||
| s.Assert().Contains(err.Error(), `"APP_MODE"`) | ||
| s.Assert().NotContains(err.Error(), "=a") | ||
| s.Assert().NotContains(err.Error(), "=b") | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestValueMayContainEquals() { | ||
| assignment, err := parseWorkspaceEnvAssignment("TOKEN=one=two") | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal(workspaceEnvAssignment{name: "TOKEN", value: "one=two"}, assignment) | ||
| } | ||
|
|
||
| func (s *WorkspaceEnvTestSuite) TestOutputHasUniqueTargets() { | ||
| base, err := indexWorkspaceEnv([]string{"A=one"}) | ||
| s.Require().NoError(err) | ||
| got, err := composeWorkspaceEnv(base, []resolvedEnvVar{{ | ||
| assignment: "B=two", | ||
| }}) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal([]string{"A=one", "B=two"}, got) | ||
| indexed, err := indexWorkspaceEnv(got) | ||
| s.Require().NoError(err) | ||
| s.Assert().Len(indexed, 2) | ||
| } | ||
|
|
||
| type unavailableEnvSource struct{} | ||
|
|
||
| func (unavailableEnvSource) Get(_ context.Context, name string) (secretspkg.ResolvedSecret, error) { | ||
| return secretspkg.ResolvedSecret{}, fmt.Errorf("unexpected resolution of %s", name) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.