-
Notifications
You must be signed in to change notification settings - Fork 291
feat: add --export flag to azd env get-values for shell sourcing #7364
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fd6936f
feat: add --export flag to azd env get-values for shell sourcing
spboyer 75be893
fix: shell-safe export with newline roundtrip and key validation
spboyer 34939fb
fix: cspell roundtripping -> round-tripping
spboyer 9b3872f
fix: lift shellEscaper and validShellKey to package-level vars
spboyer 13e31bc
Address review feedback on --export flag
spboyer dfea26d
Add PowerShell export support via --shell pwsh flag
spboyer 4910725
Fix shell escaping, PowerShell newlines, stderr abstraction, and --sh…
spboyer 5436315
Fix staticcheck SA5011 false positives in tests
spboyer 318b415
Add regression tests for ANSI-C escaping and warning output
spboyer 0ce924f
Fix staticcheck SA5011: use require.NotNil for fluent builder test
spboyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/internal" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/output" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks/mockenv" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEnvGetValuesExport(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| envVars map[string]string | ||
| export bool | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "export basic values", | ||
| envVars: map[string]string{ | ||
| "FOO": "bar", | ||
| "BAZ": "qux", | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export BAZ=\"qux\"\n" + | ||
| "export FOO=\"bar\"\n", | ||
| }, | ||
| { | ||
| name: "export values with special characters", | ||
| envVars: map[string]string{ | ||
| "CONN": `host="localhost" pass=$ecret`, | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export CONN=" + | ||
| `"host=\"localhost\" pass=\$ecret"` + | ||
| "\n", | ||
| }, | ||
| { | ||
| name: "export empty value", | ||
| envVars: map[string]string{ | ||
| "EMPTY": "", | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export EMPTY=\"\"\n", | ||
| }, | ||
| { | ||
| name: "export values with newlines", | ||
| envVars: map[string]string{ | ||
| "MULTILINE": "line1\nline2\nline3", | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export MULTILINE=$'line1\\nline2\\nline3'\n", | ||
| }, | ||
| { | ||
| name: "export values with backslashes", | ||
| envVars: map[string]string{ | ||
| "WIN_PATH": `C:\path\to\dir`, | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export WIN_PATH=\"C:\\\\path\\\\to\\\\dir\"\n", | ||
| }, | ||
| { | ||
| name: "export values with backticks and command substitution", | ||
| envVars: map[string]string{ | ||
| "DANGEROUS": "value with `backticks` and $(command)", | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export DANGEROUS=\"value with \\`backticks\\` and \\$(command)\"\n", | ||
| }, | ||
| { | ||
| name: "export values with carriage returns", | ||
| envVars: map[string]string{ | ||
| "CR_VALUE": "line1\rline2", | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export CR_VALUE=\"line1\\rline2\"\n", | ||
| }, | ||
| { | ||
|
spboyer marked this conversation as resolved.
|
||
| name: "no export outputs dotenv format", | ||
| envVars: map[string]string{ | ||
| "KEY": "value", | ||
| }, | ||
| export: false, | ||
| expected: "AZURE_ENV_NAME=\"test\"\n" + | ||
| "KEY=\"value\"\n", | ||
| }, | ||
| { | ||
| name: "export skips invalid shell keys", | ||
| envVars: map[string]string{ | ||
| "VALID_KEY": "ok", | ||
| "bad;key": "injected", | ||
| "has spaces": "nope", | ||
| "_UNDERSCORE": "fine", | ||
| }, | ||
| export: true, | ||
| expected: "export AZURE_ENV_NAME=\"test\"\n" + | ||
| "export VALID_KEY=\"ok\"\n" + | ||
| "export _UNDERSCORE=\"fine\"\n", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| mockContext := mocks.NewMockContext( | ||
| t.Context(), | ||
| ) | ||
|
spboyer marked this conversation as resolved.
|
||
|
|
||
| azdCtx := azdcontext.NewAzdContextWithDirectory( | ||
| t.TempDir(), | ||
| ) | ||
| err := azdCtx.SetProjectState( | ||
| azdcontext.ProjectState{ | ||
| DefaultEnvironment: "test", | ||
| }, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| testEnv := environment.New("test") | ||
| for k, v := range tt.envVars { | ||
| testEnv.DotenvSet(k, v) | ||
| } | ||
|
|
||
| envMgr := &mockenv.MockEnvManager{} | ||
| envMgr.On( | ||
| "Get", mock.Anything, "test", | ||
| ).Return(testEnv, nil) | ||
|
|
||
| var buf bytes.Buffer | ||
| formatter, err := output.NewFormatter("dotenv") | ||
| require.NoError(t, err) | ||
|
|
||
| action := &envGetValuesAction{ | ||
| azdCtx: azdCtx, | ||
| console: mockContext.Console, | ||
| envManager: envMgr, | ||
| formatter: formatter, | ||
| writer: &buf, | ||
| flags: &envGetValuesFlags{ | ||
| global: &internal.GlobalCommandOptions{}, | ||
| export: tt.export, | ||
| }, | ||
| } | ||
|
|
||
| _, err = action.Run(t.Context()) | ||
| require.NoError(t, err) | ||
| require.Equal(t, tt.expected, buf.String()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestEnvGetValuesExportOutputMutualExclusion(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(t.Context()) | ||
|
|
||
| azdCtx := azdcontext.NewAzdContextWithDirectory( | ||
| t.TempDir(), | ||
| ) | ||
| err := azdCtx.SetProjectState( | ||
| azdcontext.ProjectState{ | ||
| DefaultEnvironment: "test", | ||
| }, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| formatter, err := output.NewFormatter("json") | ||
| require.NoError(t, err) | ||
|
|
||
| var buf bytes.Buffer | ||
| action := &envGetValuesAction{ | ||
| azdCtx: azdCtx, | ||
| console: mockContext.Console, | ||
| formatter: formatter, | ||
| writer: &buf, | ||
| flags: &envGetValuesFlags{ | ||
| global: &internal.GlobalCommandOptions{}, | ||
| export: true, | ||
| }, | ||
| } | ||
|
|
||
| _, err = action.Run(t.Context()) | ||
| require.Error(t, err) | ||
| require.Contains( | ||
| t, err.Error(), "mutually exclusive", | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.