Skip to content

Commit 029a38e

Browse files
author
Hovsep
committed
Merge pull request #1438 from markcowl/newruntime
Update AzurePSCmdlet for changes in new CLU runtime
2 parents 26579ee + 2c0cb20 commit 029a38e

File tree

3 files changed

+147
-8
lines changed

3 files changed

+147
-8
lines changed

src/CLU/Commands.Common/AzurePSCmdlet.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,16 @@ protected virtual T GetSessionVariableValue<T>(string name, T defaultValue) wher
107107
{
108108
try
109109
{
110-
returnValue = SessionState.PSVariable.Get<T>(name)?? defaultValue ;
110+
returnValue = SessionState.PSVariable.Get<T>(name) ?? defaultValue;
111+
}
112+
catch
113+
{
114+
}
115+
}
116+
else
117+
{
118+
try
119+
{
111120
var variablePath = GetPath(name);
112121
var fileText = DataStore.ReadFileAsText(variablePath);
113122
if (!string.IsNullOrEmpty(fileText))
@@ -117,6 +126,7 @@ protected virtual T GetSessionVariableValue<T>(string name, T defaultValue) wher
117126
}
118127
catch
119128
{
129+
120130
}
121131
}
122132

@@ -127,11 +137,15 @@ protected virtual string GetPath(string variableName)
127137
{
128138
return Path.Combine(Directory.GetCurrentDirectory(), "sessions", variableName);
129139
}
140+
130141
protected virtual void SetSessionVariable<T>(string name, T value) where T : class
131142
{
132143
if (SessionState != null)
133144
{
134145
SessionState.PSVariable.Set(name, value);
146+
}
147+
else
148+
{
135149
var variablePath = GetPath(name);
136150
DataStore.WriteFile(variablePath, JsonConvert.SerializeObject(value));
137151
}
@@ -270,7 +284,7 @@ protected override void BeginProcessing()
270284
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", DefaultContext.Account.Id));
271285
}
272286

273-
DataStore = GetSessionVariableValue<IDataStore>(AzurePowerShell.DataStoreVariable, new DiskDataStore());
287+
DataStore = DataStore?? GetSessionVariableValue<IDataStore>(AzurePowerShell.DataStoreVariable, new DiskDataStore());
274288
_adalListener = _adalListener ?? new DebugStreamTraceListener(_debugMessages);
275289
DebugStreamTraceListener.AddAdalTracing(_adalListener);
276290

@@ -339,11 +353,7 @@ protected bool IsVerbose()
339353
protected new void WriteObject(object sendToPipeline)
340354
{
341355
FlushDebugMessages();
342-
#if DEBUG
343-
CommandRuntime.WriteObject(sendToPipeline);
344-
#else
345356
base.WriteObject(sendToPipeline);
346-
#endif
347357
}
348358

349359
protected new void WriteObject(object sendToPipeline, bool enumerateCollection)

src/CLU/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockCommandRuntime.cs

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Collections;
1717
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1819
using System.Diagnostics.CodeAnalysis;
1920
using System.Globalization;
2021
using System.Management.Automation;
@@ -96,6 +97,7 @@ public override Version Version
9697
public List<string> WarningStream = new List<string>();
9798
public List<string> VerboseStream = new List<string>();
9899
public List<string> DebugStream = new List<string>();
100+
PSHost _host = new MockPSHost();
99101

100102
public override string ToString()
101103
{
@@ -105,11 +107,11 @@ public override string ToString()
105107

106108
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
107109
Justification = "Tests should not access this property")]
108-
public System.Management.Automation.Host.PSHost Host
110+
public PSHost Host
109111
{
110112
get
111113
{
112-
return null;
114+
return _host;
113115
}
114116
}
115117

@@ -219,5 +221,130 @@ public void ResetPipelines()
219221
WarningStream.Clear();
220222
VerboseStream.Clear();
221223
}
224+
225+
class MockPSHost : PSHost
226+
{
227+
PSHostUserInterface _hostUI = new MockPSHostUI();
228+
Version _version = new Version(1, 0, 0);
229+
Guid _instanceId = Guid.NewGuid();
230+
public override CultureInfo CurrentCulture
231+
{
232+
get { return CultureInfo.CurrentCulture; }
233+
}
234+
235+
public override CultureInfo CurrentUICulture
236+
{
237+
get
238+
{
239+
return CultureInfo.CurrentUICulture;
240+
}
241+
}
242+
243+
public override Guid InstanceId
244+
{
245+
get
246+
{
247+
return _instanceId;
248+
}
249+
}
250+
251+
public override bool IsInputRedirected
252+
{
253+
get
254+
{
255+
return false;
256+
}
257+
}
258+
259+
public override bool IsOutputRedirected
260+
{
261+
get
262+
{
263+
return true;
264+
}
265+
}
266+
267+
public override string Name
268+
{
269+
get
270+
{
271+
return "MockHost";
272+
}
273+
}
274+
275+
public override PSHostUserInterface UI
276+
{
277+
get { return _hostUI; }
278+
}
279+
280+
public override Version Version
281+
{
282+
get
283+
{
284+
return new Version(1, 0 , 0);
285+
}
286+
}
287+
288+
class MockPSHostUI : PSHostUserInterface
289+
{
290+
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
291+
{
292+
return new Dictionary<string, PSObject>();
293+
}
294+
295+
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
296+
{
297+
return 0;
298+
}
299+
300+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
301+
{
302+
return new PSCredential("[email protected]", "P@$$w0rd!");
303+
}
304+
305+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName,
306+
PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
307+
{
308+
return new PSCredential("[email protected]", "P@$$w0rd!");
309+
}
310+
311+
public override string ReadLine()
312+
{
313+
return null;
314+
}
315+
316+
public override void Write(string value)
317+
{
318+
}
319+
320+
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
321+
{
322+
}
323+
324+
public override void WriteDebugLine(string message)
325+
{
326+
}
327+
328+
public override void WriteErrorLine(string value)
329+
{
330+
}
331+
332+
public override void WriteLine(string value)
333+
{
334+
}
335+
336+
public override void WriteProgress(long sourceId, ProgressRecord record)
337+
{
338+
}
339+
340+
public override void WriteVerboseLine(string message)
341+
{
342+
}
343+
344+
public override void WriteWarningLine(string message)
345+
{
346+
}
347+
}
348+
}
222349
}
223350
}

src/CLU/Microsoft.Azure.Commands.Profile.Test/TenantCmdletTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ private void Login(string subscriptionId, string tenantId)
122122
cmdlt.TenantId = tenantId;
123123
cmdlt.DefaultProfile = _profile;
124124
cmdlt.AuthenticationFactory = _authFactory;
125+
cmdlt.Username = "[email protected]";
126+
cmdlt.Password = "Pa$$w0rd!";
125127

126128
// Act
127129
cmdlt.InvokeBeginProcessing();

0 commit comments

Comments
 (0)