Skip to content

Commit c1c3700

Browse files
committed
Fix issue with Clear-AzureRmContext where old default context name was being used for empty context
1 parent 9b9145a commit c1c3700

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/ResourceManager/Common/Commands.Common.Authentication.ResourceManager/AzureRmProfile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ public string ToString(bool serializeCache)
317317
public void Clear()
318318
{
319319
Contexts.Clear();
320+
DefaultContextKey = "Default";
320321
DefaultContext = new AzureContext();
321322
EnvironmentTable.Clear();
322323
foreach (var environment in AzureEnvironment.PublicEnvironments)

src/ResourceManager/Profile/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Fix issue where running `Clear-AzureRmContext` would keep an empty context with the name of the previous default context, which prevented the user from creating a new context with the old name
2122

2223
## Version 5.0.1
2324
* Fix issue where default environments weren''t being retrieved without a default context set

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,43 @@ public void ListMultipleContexts()
210210
}
211211
}
212212

213+
[Fact]
214+
[Trait(Category.AcceptanceType, Category.CheckIn)]
215+
public void ClearContextSetsDefaultContextName()
216+
{
217+
var getCmdlet = new GetAzureRMContextCommand();
218+
var profile = CreateMultipleContextProfile();
219+
var defaultContextName = profile.DefaultContextKey;
220+
getCmdlet.CommandRuntime = commandRuntimeMock;
221+
getCmdlet.DefaultProfile = profile;
222+
getCmdlet.InvokeBeginProcessing();
223+
getCmdlet.ExecuteCmdlet();
224+
getCmdlet.InvokeEndProcessing();
225+
Assert.True(commandRuntimeMock.OutputPipeline != null);
226+
Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
227+
Assert.Equal(defaultContextName, ((PSAzureContext)commandRuntimeMock.OutputPipeline[0]).Name);
228+
229+
var clearCmdlet = new ClearAzureRmContext();
230+
commandRuntimeMock = new MockCommandRuntime();
231+
clearCmdlet.CommandRuntime = commandRuntimeMock;
232+
clearCmdlet.DefaultProfile = profile;
233+
clearCmdlet.Scope = ContextModificationScope.Process;
234+
clearCmdlet.PassThru = true;
235+
clearCmdlet.InvokeBeginProcessing();
236+
clearCmdlet.ExecuteCmdlet();
237+
clearCmdlet.InvokeEndProcessing();
238+
Assert.NotNull(commandRuntimeMock.OutputPipeline);
239+
Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
240+
var result = (bool)(commandRuntimeMock.OutputPipeline[0]);
241+
Assert.True(result);
242+
Assert.True(profile.Contexts != null);
243+
Assert.Equal(1, profile.Contexts.Count);
244+
Assert.True(profile.Contexts.ContainsKey("Default"));
245+
Assert.NotNull(profile.DefaultContext);
246+
Assert.Null(profile.DefaultContext.Account);
247+
Assert.Null(profile.DefaultContext.Subscription);
248+
}
249+
213250
[Fact]
214251
[Trait(Category.AcceptanceType, Category.CheckIn)]
215252
public void RemoveDefaultContext()

0 commit comments

Comments
 (0)