Skip to content

Commit 0c57a9c

Browse files
committed
Fix failing resource mock tests
1 parent d473282 commit 0c57a9c

File tree

10 files changed

+255
-37
lines changed

10 files changed

+255
-37
lines changed

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/Commands.ScenarioTests.ResourceManager.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<Compile Include="Mocks\MockCommandRuntime.cs" />
152152
<Compile Include="Mocks\MockDeploymentClientFactory.cs" />
153153
<Compile Include="PermissiveRecordMatcherWithApiExclusion.cs" />
154+
<Compile Include="PageExtensions.cs" />
154155
<Compile Include="ProfileClient.cs" />
155156
<Compile Include="PSCmdletExtensions.cs" />
156157
<Compile Include="Constants.cs" />

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

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

15+
using System;
1516
using System.Collections.Generic;
17+
using System.Collections.ObjectModel;
1618
using System.Diagnostics.CodeAnalysis;
19+
using System.Globalization;
1720
using System.Management.Automation;
21+
using System.Management.Automation.Host;
22+
using System.Security;
1823

1924
namespace Microsoft.WindowsAzure.Commands.Common.Test.Mocks
2025
{
@@ -25,6 +30,7 @@ public class MockCommandRuntime : ICommandRuntime
2530
public List<string> WarningStream = new List<string>();
2631
public List<string> VerboseStream = new List<string>();
2732
public List<string> DebugStream = new List<string>();
33+
PSHost _host = new MockPSHost();
2834

2935
public override string ToString()
3036
{
@@ -42,7 +48,10 @@ public PSTransactionContext CurrentPSTransaction
4248
Justification = "Tests should not access this property")]
4349
public System.Management.Automation.Host.PSHost Host
4450
{
45-
get { throw new System.NotImplementedException(); }
51+
get
52+
{
53+
return _host;
54+
}
4655
}
4756

4857
public bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll)
@@ -151,5 +160,149 @@ public void ResetPipelines()
151160
WarningStream.Clear();
152161
VerboseStream.Clear();
153162
}
163+
164+
class MockPSHost : PSHost
165+
{
166+
PSHostUserInterface _hostUI = new MockPSHostUI();
167+
Version _version = new Version(1, 0, 0);
168+
Guid _instanceId = Guid.NewGuid();
169+
public override CultureInfo CurrentCulture
170+
{
171+
get { return CultureInfo.CurrentCulture; }
172+
}
173+
174+
public override CultureInfo CurrentUICulture
175+
{
176+
get
177+
{
178+
return CultureInfo.CurrentUICulture;
179+
}
180+
}
181+
182+
public override Guid InstanceId
183+
{
184+
get
185+
{
186+
return _instanceId;
187+
}
188+
}
189+
190+
public override string Name
191+
{
192+
get
193+
{
194+
return "MockHost";
195+
}
196+
}
197+
198+
public override PSHostUserInterface UI
199+
{
200+
get { return _hostUI; }
201+
}
202+
203+
public override Version Version
204+
{
205+
get
206+
{
207+
return new Version(1, 0, 0);
208+
}
209+
}
210+
211+
public override void NotifyBeginApplication()
212+
{
213+
throw new NotImplementedException();
214+
}
215+
216+
public override void NotifyEndApplication()
217+
{
218+
throw new NotImplementedException();
219+
}
220+
221+
public override void EnterNestedPrompt()
222+
{
223+
throw new NotImplementedException();
224+
}
225+
226+
public override void ExitNestedPrompt()
227+
{
228+
throw new NotImplementedException();
229+
}
230+
231+
public override void SetShouldExit(int exitCode)
232+
{
233+
throw new NotImplementedException();
234+
}
235+
236+
class MockPSHostUI : PSHostUserInterface
237+
{
238+
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
239+
{
240+
return new Dictionary<string, PSObject>();
241+
}
242+
243+
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
244+
{
245+
return 0;
246+
}
247+
248+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
249+
{
250+
return new PSCredential("[email protected]", new SecureString());
251+
}
252+
253+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName,
254+
PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
255+
{
256+
return new PSCredential("[email protected]", new SecureString());
257+
}
258+
259+
public override string ReadLine()
260+
{
261+
return null;
262+
}
263+
264+
public override void Write(string value)
265+
{
266+
}
267+
268+
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
269+
{
270+
}
271+
272+
public override void WriteDebugLine(string message)
273+
{
274+
}
275+
276+
public override void WriteErrorLine(string value)
277+
{
278+
}
279+
280+
public override void WriteLine(string value)
281+
{
282+
}
283+
284+
public override void WriteProgress(long sourceId, ProgressRecord record)
285+
{
286+
}
287+
288+
public override void WriteVerboseLine(string message)
289+
{
290+
}
291+
292+
public override void WriteWarningLine(string message)
293+
{
294+
}
295+
296+
public override SecureString ReadLineAsSecureString()
297+
{
298+
throw new NotImplementedException();
299+
}
300+
301+
public override PSHostRawUserInterface RawUI
302+
{
303+
get { throw new NotImplementedException(); }
304+
}
305+
}
306+
}
154307
}
155308
}

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/PSCmdletExtensions.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@
1717
using System.Management.Automation;
1818
using System.Reflection;
1919

20-
namespace Microsoft.WindowsAzure.Commands.ScenarioTest
20+
namespace Microsoft.Azure.Commands.ScenarioTest
2121
{
2222
public static class PSCmdletExtensions
2323
{
2424
private static MethodInfo GetProtectedMethod(string name)
2525
{
2626
MethodInfo m = typeof(PSCmdlet).GetMethod(
2727
name,
28-
BindingFlags.Instance | BindingFlags.NonPublic,
29-
Type.DefaultBinder,
30-
new Type[] { },
31-
null);
28+
BindingFlags.Instance | BindingFlags.NonPublic);
3229

3330
return m;
3431
}
3532

33+
private static PropertyInfo GetInternalProperty(string name, Type type)
34+
{
35+
PropertyInfo prop = typeof(PSCmdlet).GetProperty(name);
36+
return prop;
37+
}
38+
3639
public static void ExecuteCmdlet(this PSCmdlet cmdlet)
3740
{
3841
try
@@ -45,12 +48,6 @@ public static void ExecuteCmdlet(this PSCmdlet cmdlet)
4548
}
4649
}
4750

48-
private static PropertyInfo GetInternalProperty(string name, Type type)
49-
{
50-
PropertyInfo prop = typeof(PSCmdlet).GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic);
51-
return prop;
52-
}
53-
5451
public static void SetCommandRuntimeMock(this PSCmdlet cmdlet, ICommandRuntime value)
5552
{
5653
var property = GetInternalProperty("CommandRuntime", typeof(ICommandRuntime));
@@ -62,6 +59,5 @@ public static ICommandRuntime GetCommandRuntimeMock(this PSCmdlet cmdlet)
6259
var property = GetInternalProperty("CommandRuntime", typeof(ICommandRuntime));
6360
return (ICommandRuntime)property.GetValue(cmdlet);
6461
}
65-
6662
}
6763
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Management.ResourceManager.Models;
16+
using System.Collections.Generic;
17+
using System.Reflection;
18+
19+
namespace Microsoft.WindowsAzure.Commands.ScenarioTest
20+
{
21+
public static class PageExtensions
22+
{
23+
public static void SetItemValue<T>(this Page<T> pagableObj, List<T> collection)
24+
{
25+
var property = typeof(Page<T>).GetProperty("Items", BindingFlags.Instance | BindingFlags.NonPublic);
26+
property.SetValue(pagableObj, collection);
27+
}
28+
}
29+
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/CmdletBase/ResourceManagerCmdletBase.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,5 +444,13 @@ private void HandleException(ExceptionDispatchInfo capturedException)
444444
this.DisposeOfCancellationSource();
445445
}
446446
}
447+
448+
/// <summary>
449+
/// Determines the parameter set name.
450+
/// </summary>
451+
public virtual string DetermineParameterSetName()
452+
{
453+
return this.ParameterSetName;
454+
}
447455
}
448456
}

src/ResourceManager/Resources/Commands.Resources.Test/Features/GetAzureProviderFeatureCmdletTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.Azure.Commands.Resources.Test
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
20+
using Microsoft.Azure.Commands.ScenarioTest;
2021
using Microsoft.Azure.Management.ResourceManager;
2122
using Microsoft.Azure.Management.ResourceManager.Models;
2223
using Microsoft.Rest.Azure;
@@ -27,7 +28,6 @@ namespace Microsoft.Azure.Commands.Resources.Test
2728
using System.Collections.Generic;
2829
using System.Linq;
2930
using System.Management.Automation;
30-
using System.Net;
3131
using System.Threading;
3232
using System.Threading.Tasks;
3333
using WindowsAzure.Commands.Test.Utilities.Common;
@@ -131,7 +131,7 @@ public void GetProviderFeatureTests()
131131
var pagableResult = new Page<FeatureResult>();
132132
//var listResult = new[] { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
133133
var listResult = new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
134-
//pagableResult.SetItemValue<FeatureResult>(listResult);
134+
pagableResult.SetItemValue<FeatureResult>(listResult);
135135
var result = new AzureOperationResponse<IPage<FeatureResult>>()
136136
{
137137
Body = pagableResult

src/ResourceManager/Resources/Commands.Resources.Test/Features/RegisterProviderFeatureCmdletTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.Azure.Commands.Resources.Test
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
20+
using Microsoft.Azure.Commands.ScenarioTest;
2021
using Microsoft.Azure.Management.ResourceManager;
2122
using Microsoft.Azure.Management.ResourceManager.Models;
2223
using Microsoft.Rest.Azure;
@@ -26,7 +27,6 @@ namespace Microsoft.Azure.Commands.Resources.Test
2627
using System;
2728
using System.Collections.Generic;
2829
using System.Management.Automation;
29-
using System.Net;
3030
using System.Threading;
3131
using System.Threading.Tasks;
3232
using WindowsAzure.Commands.Test.Utilities.Common;

0 commit comments

Comments
 (0)