Skip to content

Commit 2a5d1fc

Browse files
authored
Merge pull request #3690 from cormacpayne/fix-get-context
Fix error when calling Get-AzureRmContext with no context
2 parents c29bd5c + 59be395 commit 2a5d1fc

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/ContextCmdletTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ public void GetAzureContext()
7171
Assert.Equal("test", context.Subscription.SubscriptionName);
7272
}
7373

74+
[Fact]
75+
[Trait(Category.AcceptanceType, Category.CheckIn)]
76+
public void GetAzureContextNoLogin()
77+
{
78+
var cmdlt = new GetAzureRMContextCommand();
79+
80+
// Setup
81+
cmdlt.CommandRuntime = commandRuntimeMock;
82+
var profile = AzureRmProfileProvider.Instance.Profile;
83+
AzureRmProfileProvider.Instance.Profile = new AzureRMProfile();
84+
85+
try
86+
{
87+
// Act
88+
cmdlt.InvokeBeginProcessing();
89+
cmdlt.ExecuteCmdlet();
90+
cmdlt.InvokeEndProcessing();
91+
}
92+
finally
93+
{
94+
AzureRmProfileProvider.Instance.Profile = profile;
95+
}
96+
97+
// Verify
98+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
99+
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
100+
Assert.Null(context);
101+
Assert.True(commandRuntimeMock.ErrorStream.Count == 1);
102+
var error = commandRuntimeMock.ErrorStream[0];
103+
Assert.Equal("Run Login-AzureRmAccount to login.", error.Exception.Message);
104+
}
105+
74106
[Fact]
75107
[Trait(Category.AcceptanceType, Category.CheckIn)]
76108
public void SelectAzureContextWithNoSubscriptionAndTenant()

src/ResourceManager/Profile/Commands.Profile/Context/GetAzureRMContext.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Common.Authentication.Models;
1516
using Microsoft.Azure.Commands.Profile.Models;
1617
using Microsoft.Azure.Commands.ResourceManager.Common;
1718
using Microsoft.WindowsAzure.Commands.Common;
@@ -26,9 +27,34 @@ namespace Microsoft.Azure.Commands.Profile
2627
[OutputType(typeof(PSAzureContext))]
2728
public class GetAzureRMContextCommand : AzureRMCmdlet
2829
{
30+
/// <summary>
31+
/// Gets the current default context.
32+
/// </summary>
33+
protected override AzureContext DefaultContext
34+
{
35+
get
36+
{
37+
if (DefaultProfile == null || DefaultProfile.Context == null)
38+
{
39+
return null;
40+
}
41+
42+
return DefaultProfile.Context;
43+
}
44+
}
45+
2946
public override void ExecuteCmdlet()
3047
{
31-
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
48+
var context = (PSAzureContext)DefaultContext;
49+
if (context == null)
50+
{
51+
WriteError(new ErrorRecord(
52+
new PSInvalidOperationException("Run Login-AzureRmAccount to login."),
53+
string.Empty,
54+
ErrorCategory.AuthenticationError,
55+
null));
56+
}
57+
WriteObject(context);
3258
}
3359
}
3460
}

0 commit comments

Comments
 (0)