-
Notifications
You must be signed in to change notification settings - Fork 337
Add actionable guidance for PowerShell hook failures #6804
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 all commits
Commits
Show all changes
2 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,125 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package middleware | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/exec" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_pwshFailureGuidance(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| wantHint string | ||
| }{ | ||
| { | ||
| name: "NonExitError", | ||
| err: fmt.Errorf("some error"), | ||
| wantHint: "", | ||
| }, | ||
| { | ||
| name: "NonPwshCommand", | ||
| err: exec.NewTestExitError("node", 1, "Import-Module: not loaded"), | ||
| wantHint: "", | ||
| }, | ||
| { | ||
| name: "MissingModule", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "Import-Module: The specified module 'Az.CognitiveServices' was not loaded"+ | ||
| " because no valid module file was found.", | ||
| ), | ||
| wantHint: "A required PowerShell module could not be loaded. " + | ||
| "Install the missing module with 'Install-Module <ModuleName> -Scope CurrentUser'.", | ||
| }, | ||
| { | ||
| name: "AzModuleNotFound", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "The term 'Az.Accounts' is not recognized as a name of a cmdlet", | ||
| ), | ||
| wantHint: "The Azure PowerShell module (Az) is required but not installed. " + | ||
| "Install it with 'Install-Module Az -Scope CurrentUser -Repository PSGallery -Force'.", | ||
| }, | ||
| { | ||
|
spboyer marked this conversation as resolved.
|
||
| name: "ExecutionPolicy", | ||
| err: exec.NewTestExitError( | ||
| "powershell.exe", 1, | ||
| "UnauthorizedAccess: File script.ps1 cannot be loaded.", | ||
| ), | ||
| wantHint: "PowerShell execution policy is blocking the script. " + | ||
| "Check your policy with 'Get-ExecutionPolicy' and consider setting it with " + | ||
| "'Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser'.", | ||
| }, | ||
| { | ||
| name: "AuthExpired", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "Please run Connect-AzAccount to set up your credentials.", | ||
| ), | ||
| wantHint: "The Azure authentication session may have expired. " + | ||
| "Try running 'azd auth login' again.", | ||
| }, | ||
| { | ||
|
spboyer marked this conversation as resolved.
|
||
| name: "ErrorActionPreference", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "ErrorActionPreference is set incorrectly", | ||
| ), | ||
| wantHint: "The hook script has an issue with error handling configuration. " + | ||
| "Ensure '$ErrorActionPreference = \"Stop\"' is set at the top of the script.", | ||
| }, | ||
| { | ||
| name: "ConnectAzAccountNotRecognized", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "Connect-AzAccount: The term 'Connect-AzAccount' is not recognized as a name of a cmdlet", | ||
| ), | ||
| wantHint: "", | ||
| }, | ||
| { | ||
| name: "LoginExpired", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "login token has expired", | ||
| ), | ||
| wantHint: "The Azure authentication session may have expired. " + | ||
| "Try running 'azd auth login' again.", | ||
| }, | ||
| { | ||
| name: "NonAzureAzPrefixedNotFound", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "The term 'MyAz.Custom' is not recognized as a name of a cmdlet", | ||
| ), | ||
| wantHint: "", | ||
| }, | ||
| { | ||
| name: "ConnectAzAccountSucceeded", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "Connect-AzAccount succeeded but something else failed", | ||
| ), | ||
| wantHint: "", | ||
| }, | ||
| { | ||
| name: "NoMatchingPattern", | ||
| err: exec.NewTestExitError( | ||
| "pwsh", 1, | ||
| "Some random error occurred", | ||
| ), | ||
| wantHint: "", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := pwshFailureGuidance(tt.err) | ||
| require.Equal(t, tt.wantHint, got) | ||
| }) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all good but I'd like to build this on top of my global error handling PR... I'll get that updated then we can layer in all these new errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood -- will rebase these patterns on top of #6700 once it lands, same as #6801.