Skip to content

Commit d60b3f8

Browse files
committed
Test adjustments for new clu runtime
1 parent bbeb622 commit d60b3f8

File tree

4 files changed

+149
-9
lines changed

4 files changed

+149
-9
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.Common/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"Microsoft.NETCore": "5.0.1-beta-23516",
1212
"Microsoft.NETCore.Platforms": "1.0.1-beta-23516",
13-
"Microsoft.CSharp": "4.0.1-beta-23516",
13+
"Microsoft.CSharp": "4.0.1-beta-23516"
1414
}
1515
}
1616
},

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

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
using System;
1616
using System.Collections;
1717
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1819
using System.Diagnostics.CodeAnalysis;
20+
using System.Globalization;
1921
using System.Management.Automation;
2022
using System.Management.Automation.Host;
2123

@@ -28,6 +30,7 @@ public class MockCommandRuntime : ICommandRuntime
2830
public List<string> WarningStream = new List<string>();
2931
public List<string> VerboseStream = new List<string>();
3032
public List<string> DebugStream = new List<string>();
33+
PSHost _host = new MockPSHost();
3134

3235
public override string ToString()
3336
{
@@ -37,11 +40,11 @@ public override string ToString()
3740

3841
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
3942
Justification = "Tests should not access this property")]
40-
public System.Management.Automation.Host.PSHost Host
43+
public PSHost Host
4144
{
4245
get
4346
{
44-
return null;
47+
return _host;
4548
}
4649
}
4750

@@ -151,5 +154,130 @@ public void ResetPipelines()
151154
WarningStream.Clear();
152155
VerboseStream.Clear();
153156
}
157+
158+
class MockPSHost : PSHost
159+
{
160+
PSHostUserInterface _hostUI = new MockPSHostUI();
161+
Version _version = new Version(1, 0, 0);
162+
Guid _instanceId = Guid.NewGuid();
163+
public override CultureInfo CurrentCulture
164+
{
165+
get { return CultureInfo.CurrentCulture; }
166+
}
167+
168+
public override CultureInfo CurrentUICulture
169+
{
170+
get
171+
{
172+
return CultureInfo.CurrentUICulture;
173+
}
174+
}
175+
176+
public override Guid InstanceId
177+
{
178+
get
179+
{
180+
return _instanceId;
181+
}
182+
}
183+
184+
public override bool IsInputRedirected
185+
{
186+
get
187+
{
188+
return false;
189+
}
190+
}
191+
192+
public override bool IsOutputRedirected
193+
{
194+
get
195+
{
196+
return true;
197+
}
198+
}
199+
200+
public override string Name
201+
{
202+
get
203+
{
204+
return "MockHost";
205+
}
206+
}
207+
208+
public override PSHostUserInterface UI
209+
{
210+
get { return _hostUI; }
211+
}
212+
213+
public override Version Version
214+
{
215+
get
216+
{
217+
return new Version(1, 0 , 0);
218+
}
219+
}
220+
221+
class MockPSHostUI : PSHostUserInterface
222+
{
223+
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
224+
{
225+
return new Dictionary<string, PSObject>();
226+
}
227+
228+
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
229+
{
230+
return 0;
231+
}
232+
233+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
234+
{
235+
return new PSCredential("[email protected]", "P@$$w0rd!");
236+
}
237+
238+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName,
239+
PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
240+
{
241+
return new PSCredential("[email protected]", "P@$$w0rd!");
242+
}
243+
244+
public override string ReadLine()
245+
{
246+
return null;
247+
}
248+
249+
public override void Write(string value)
250+
{
251+
}
252+
253+
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
254+
{
255+
}
256+
257+
public override void WriteDebugLine(string message)
258+
{
259+
}
260+
261+
public override void WriteErrorLine(string value)
262+
{
263+
}
264+
265+
public override void WriteLine(string value)
266+
{
267+
}
268+
269+
public override void WriteProgress(long sourceId, ProgressRecord record)
270+
{
271+
}
272+
273+
public override void WriteVerboseLine(string message)
274+
{
275+
}
276+
277+
public override void WriteWarningLine(string message)
278+
{
279+
}
280+
}
281+
}
154282
}
155283
}

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)