-
Notifications
You must be signed in to change notification settings - Fork 338
Fail fast in azd init when --environment missing non-interactively #6779
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
Merged
rajeshkamal5050
merged 3 commits into
Azure:main
from
spboyer:feature/init-fail-fast-missing-env
Feb 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,163 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/internal" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/exec" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/lazy" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/tools/git" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // setupInitAction creates an initAction wired with mocks that pass git-install checks. | ||
| // The working directory is changed to a temp dir so that .env loading and azdcontext work. | ||
| func setupInitAction(t *testing.T, mockContext *mocks.MockContext, flags *initFlags) *initAction { | ||
| t.Helper() | ||
|
|
||
| // Work in a temp directory so os.Getwd / godotenv.Overload operate in isolation. | ||
| tmpDir := t.TempDir() | ||
| origDir, err := os.Getwd() | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.Chdir(tmpDir)) | ||
| t.Cleanup(func() { os.Chdir(origDir) }) | ||
|
|
||
| // Mock git so tools.EnsureInstalled succeeds. | ||
| mockContext.CommandRunner.MockToolInPath("git", nil) | ||
| mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool { | ||
| return strings.Contains(command, "git") && strings.Contains(command, "--version") | ||
| }).RespondFn(func(args exec.RunArgs) (exec.RunResult, error) { | ||
| return exec.NewRunResult(0, "git version 2.42.0", ""), nil | ||
| }) | ||
|
|
||
| gitCli := git.NewCli(mockContext.CommandRunner) | ||
|
|
||
| return &initAction{ | ||
| lazyAzdCtx: lazy.NewLazy(func() (*azdcontext.AzdContext, error) { | ||
| return azdcontext.NewAzdContextWithDirectory(tmpDir), nil | ||
| }), | ||
| console: mockContext.Console, | ||
| cmdRun: mockContext.CommandRunner, | ||
| gitCli: gitCli, | ||
| flags: flags, | ||
| } | ||
| } | ||
|
|
||
| // runActionSafe calls action.Run and returns the error. If Run panics (because | ||
| // later stages lack mocks), the panic is recovered and a nil error is returned | ||
| // — the test only cares that the fail-fast check did not fire. | ||
| func runActionSafe(ctx context.Context, action *initAction) (retErr error) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| // A panic means we got past the fail-fast check — treat as success. | ||
| retErr = fmt.Errorf("panic (past fail-fast): %v", r) | ||
| } | ||
| }() | ||
|
|
||
| _, err := action.Run(ctx) | ||
| return err | ||
| } | ||
|
|
||
| func TestInitFailFastMissingEnvNonInteractive(t *testing.T) { | ||
| t.Run("FailsWhenNoPromptWithTemplateAndNoEnv", func(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| flags := &initFlags{ | ||
| templatePath: "owner/repo", | ||
| global: &internal.GlobalCommandOptions{NoPrompt: true}, | ||
| } | ||
|
|
||
| action := setupInitAction(t, mockContext, flags) | ||
|
|
||
| result, err := action.Run(*mockContext.Context) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), | ||
| "--environment is required when running in non-interactive mode") | ||
| require.Nil(t, result) | ||
| }) | ||
|
|
||
| t.Run("DoesNotFailWhenEnvProvidedViaFlag", func(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| flags := &initFlags{ | ||
| templatePath: "owner/repo", | ||
| global: &internal.GlobalCommandOptions{NoPrompt: true}, | ||
| } | ||
| flags.EnvironmentName = "myenv" | ||
|
|
||
| action := setupInitAction(t, mockContext, flags) | ||
|
|
||
| err := runActionSafe(*mockContext.Context, action) | ||
| if err != nil { | ||
| require.NotContains(t, err.Error(), | ||
| "--environment is required when running in non-interactive mode") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("DoesNotFailWhenEnvProvidedViaDotEnv", func(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| flags := &initFlags{ | ||
| templatePath: "owner/repo", | ||
| global: &internal.GlobalCommandOptions{NoPrompt: true}, | ||
| } | ||
|
|
||
| action := setupInitAction(t, mockContext, flags) | ||
|
|
||
| // Write a .env file in the temp working directory with AZURE_ENV_NAME set. | ||
| wd, err := os.Getwd() | ||
| require.NoError(t, err) | ||
| envFile := filepath.Join(wd, ".env") | ||
| require.NoError(t, os.WriteFile(envFile, []byte("AZURE_ENV_NAME=from-dotenv\n"), 0600)) | ||
|
|
||
| err = runActionSafe(*mockContext.Context, action) | ||
| if err != nil { | ||
| require.NotContains(t, err.Error(), | ||
| "--environment is required when running in non-interactive mode") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("DoesNotFailInInteractiveMode", func(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| flags := &initFlags{ | ||
| templatePath: "owner/repo", | ||
| global: &internal.GlobalCommandOptions{NoPrompt: false}, | ||
| } | ||
|
|
||
| action := setupInitAction(t, mockContext, flags) | ||
|
|
||
| err := runActionSafe(*mockContext.Context, action) | ||
| if err != nil { | ||
| require.NotContains(t, err.Error(), | ||
| "--environment is required when running in non-interactive mode") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("DoesNotFailWithoutTemplate", func(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
|
|
||
| flags := &initFlags{ | ||
| templatePath: "", | ||
| global: &internal.GlobalCommandOptions{NoPrompt: true}, | ||
| } | ||
|
|
||
| action := setupInitAction(t, mockContext, flags) | ||
|
|
||
| err := runActionSafe(*mockContext.Context, action) | ||
| if err != nil { | ||
| require.NotContains(t, err.Error(), | ||
| "--environment is required when running in non-interactive mode") | ||
| } | ||
| }) | ||
| } | ||
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.