Skip to content

Update AzurePSCmdlet for changes in new CLU runtime #1438

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

Merged
merged 3 commits into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/CLU/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ protected virtual T GetSessionVariableValue<T>(string name, T defaultValue) wher
{
try
{
returnValue = SessionState.PSVariable.Get<T>(name)?? defaultValue ;
returnValue = SessionState.PSVariable.Get<T>(name) ?? defaultValue;
}
catch
{
}
}
else
{
try
{
var variablePath = GetPath(name);
var fileText = DataStore.ReadFileAsText(variablePath);
if (!string.IsNullOrEmpty(fileText))
Expand All @@ -117,6 +126,7 @@ protected virtual T GetSessionVariableValue<T>(string name, T defaultValue) wher
}
catch
{

}
}

Expand All @@ -127,11 +137,15 @@ protected virtual string GetPath(string variableName)
{
return Path.Combine(Directory.GetCurrentDirectory(), "sessions", variableName);
}

protected virtual void SetSessionVariable<T>(string name, T value) where T : class
{
if (SessionState != null)
{
SessionState.PSVariable.Set(name, value);
}
else
{
var variablePath = GetPath(name);
DataStore.WriteFile(variablePath, JsonConvert.SerializeObject(value));
}
Expand Down Expand Up @@ -270,7 +284,7 @@ protected override void BeginProcessing()
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", DefaultContext.Account.Id));
}

DataStore = GetSessionVariableValue<IDataStore>(AzurePowerShell.DataStoreVariable, new DiskDataStore());
DataStore = DataStore?? GetSessionVariableValue<IDataStore>(AzurePowerShell.DataStoreVariable, new DiskDataStore());
_adalListener = _adalListener ?? new DebugStreamTraceListener(_debugMessages);
DebugStreamTraceListener.AddAdalTracing(_adalListener);

Expand Down Expand Up @@ -339,11 +353,7 @@ protected bool IsVerbose()
protected new void WriteObject(object sendToPipeline)
{
FlushDebugMessages();
#if DEBUG
CommandRuntime.WriteObject(sendToPipeline);
#else
base.WriteObject(sendToPipeline);
#endif
}

protected new void WriteObject(object sendToPipeline, bool enumerateCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Management.Automation;
Expand Down Expand Up @@ -96,6 +97,7 @@ public override Version Version
public List<string> WarningStream = new List<string>();
public List<string> VerboseStream = new List<string>();
public List<string> DebugStream = new List<string>();
PSHost _host = new MockPSHost();

public override string ToString()
{
Expand All @@ -105,11 +107,11 @@ public override string ToString()

[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
Justification = "Tests should not access this property")]
public System.Management.Automation.Host.PSHost Host
public PSHost Host
{
get
{
return null;
return _host;
}
}

Expand Down Expand Up @@ -219,5 +221,130 @@ public void ResetPipelines()
WarningStream.Clear();
VerboseStream.Clear();
}

class MockPSHost : PSHost
{
PSHostUserInterface _hostUI = new MockPSHostUI();
Version _version = new Version(1, 0, 0);
Guid _instanceId = Guid.NewGuid();
public override CultureInfo CurrentCulture
{
get { return CultureInfo.CurrentCulture; }
}

public override CultureInfo CurrentUICulture
{
get
{
return CultureInfo.CurrentUICulture;
}
}

public override Guid InstanceId
{
get
{
return _instanceId;
}
}

public override bool IsInputRedirected
{
get
{
return false;
}
}

public override bool IsOutputRedirected
{
get
{
return true;
}
}

public override string Name
{
get
{
return "MockHost";
}
}

public override PSHostUserInterface UI
{
get { return _hostUI; }
}

public override Version Version
{
get
{
return new Version(1, 0 , 0);
}
}

class MockPSHostUI : PSHostUserInterface
{
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
return new Dictionary<string, PSObject>();
}

public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
return 0;
}

public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
return new PSCredential("[email protected]", "P@$$w0rd!");
}

public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName,
PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
return new PSCredential("[email protected]", "P@$$w0rd!");
}

public override string ReadLine()
{
return null;
}

public override void Write(string value)
{
}

public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
}

public override void WriteDebugLine(string message)
{
}

public override void WriteErrorLine(string value)
{
}

public override void WriteLine(string value)
{
}

public override void WriteProgress(long sourceId, ProgressRecord record)
{
}

public override void WriteVerboseLine(string message)
{
}

public override void WriteWarningLine(string message)
{
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ private void Login(string subscriptionId, string tenantId)
cmdlt.TenantId = tenantId;
cmdlt.DefaultProfile = _profile;
cmdlt.AuthenticationFactory = _authFactory;
cmdlt.Username = "[email protected]";
cmdlt.Password = "Pa$$w0rd!";

// Act
cmdlt.InvokeBeginProcessing();
Expand Down