Skip to content

Commit 4547424

Browse files
Add test for exception type (Azure#47538)
* Add test for exception type * Add git attributes
1 parent 2734b4f commit 4547424

File tree

4 files changed

+44
-50
lines changed

4 files changed

+44
-50
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Use unix line endings always, even on Windows
2+
* text=auto eol=lf
Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System;
2-
using System.Diagnostics;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
34
using System.IO;
45
using System.Linq;
56
using System.Runtime.CompilerServices;
@@ -8,53 +9,26 @@ namespace Azure.Generator.Tests.Common
89
{
910
internal static class Helpers
1011
{
11-
private static readonly string _assemblyLocation = Path.GetDirectoryName(typeof(Helpers).Assembly.Location)!;
12-
13-
public static string GetExpectedFromFile(string? parameters = null)
12+
public static string GetExpectedFromFile(
13+
string? parameters = null,
14+
[CallerMemberName] string method = "",
15+
[CallerFilePath] string filePath = "")
1416
{
15-
return File.ReadAllText(GetAssetFileOrDirectoryPath(true, parameters)).Replace("\r\n", "\n");
17+
return File.ReadAllText(GetAssetFileOrDirectoryPath(true, parameters, method, filePath));
1618
}
1719

18-
private static string GetAssetFileOrDirectoryPath(bool isFile, string? parameters = null)
20+
public static string GetAssetFileOrDirectoryPath(
21+
bool isFile,
22+
string? parameters = null,
23+
[CallerMemberName] string method = "",
24+
[CallerFilePath] string filePath = "")
1925
{
20-
var stackTrace = new StackTrace();
21-
var stackFrame = GetRealMethodInvocation(stackTrace);
22-
var method = stackFrame.GetMethod();
23-
var callingClass = method!.DeclaringType;
24-
var nsSplit = callingClass!.Namespace!.Split('.');
26+
27+
var callingClass = Path.GetFileName(filePath).Split('.').First();
2528
var paramString = parameters is null ? string.Empty : $"({parameters})";
2629
var extName = isFile ? ".cs" : string.Empty;
27-
var path = _assemblyLocation;
28-
var nsSkip = 3;
29-
for (int i = nsSkip; i < nsSplit.Length; i++)
30-
{
31-
path = Path.Combine(path, nsSplit[i]);
32-
}
33-
return Path.Combine(path, "TestData", callingClass.Name, $"{method.Name}{paramString}{extName}");
34-
}
35-
36-
private static StackFrame GetRealMethodInvocation(StackTrace stackTrace)
37-
{
38-
int i = 1;
39-
while (i < stackTrace.FrameCount)
40-
{
41-
var frame = stackTrace.GetFrame(i);
42-
var declaringType = frame!.GetMethod()!.DeclaringType!;
43-
// we need to skip those method invocations from this class, or from the async state machine when the caller is an async method
44-
if (declaringType != typeof(Helpers) && declaringType.Name != "MockHelpers" && !IsCompilerGenerated(declaringType))
45-
{
46-
return frame;
47-
}
48-
i++;
49-
}
50-
51-
throw new InvalidOperationException($"There is no method invocation outside the {typeof(Helpers)} class in the stack trace");
5230

53-
static bool IsCompilerGenerated(Type type)
54-
{
55-
return type.IsDefined(typeof(CompilerGeneratedAttribute), false) || (type.Namespace?.StartsWith("System.Runtime.CompilerServices") ?? false) ||
56-
type.Name.StartsWith("<<", StringComparison.Ordinal);
57-
}
31+
return Path.Combine(Path.GetDirectoryName(filePath)!, "TestData", callingClass, $"{method}{paramString}{extName}");
5832
}
5933
}
6034
}

eng/packages/http-client-csharp/generator/Azure.Generator/test/Providers/Abstractions/AzureClientResponseProviderTests.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
using Azure.Generator.Tests.Common;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Generator.Tests.Common;
25
using Azure.Generator.Tests.TestHelpers;
36
using Microsoft.Generator.CSharp.ClientModel.Providers;
4-
using Microsoft.Generator.CSharp.ClientModel;
5-
using System;
6-
using System.Collections.Generic;
77
using System.Linq;
8-
using System.Text;
9-
using System.Threading.Tasks;
108
using NUnit.Framework;
11-
using System.ClientModel.Primitives;
129
using Azure.Generator.Providers;
1310

1411
namespace Azure.Generator.Tests.Providers.Abstractions
@@ -34,7 +31,19 @@ public void ValidateBodyOfClientOperationIsOverridden()
3431
var method = clientProvider.Methods.FirstOrDefault(x => x.Signature.Parameters.Any(p => p.Type.Equals(typeof(RequestContext))) && !x.Signature.Name.EndsWith("Async"));
3532
Assert.NotNull(method);
3633
Assert.NotNull(method!.BodyStatements);
37-
var test = method.BodyStatements!.ToDisplayString();
34+
Assert.AreEqual(Helpers.GetExpectedFromFile(), method.BodyStatements!.ToDisplayString());
35+
}
36+
37+
[Test]
38+
public void ValidateClientResponseExceptionTypeIsOverridden()
39+
{
40+
MockHelpers.LoadMockPlugin();
41+
var pipelineExtensions =
42+
AzureClientPlugin.Instance.OutputLibrary.TypeProviders.FirstOrDefault(t => t.Name == "ClientPipelineExtensions");
43+
Assert.NotNull(pipelineExtensions);
44+
var method = pipelineExtensions!.Methods.FirstOrDefault(x => x.Signature.Name == "ProcessMessage");
45+
Assert.NotNull(method);
46+
Assert.NotNull(method!.BodyStatements);
3847
Assert.AreEqual(Helpers.GetExpectedFromFile(), method.BodyStatements!.ToDisplayString());
3948
}
4049

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(global::System.Threading.CancellationToken userCancellationToken, global::Azure.ErrorOptions statusOption) = context.Parse();
2+
pipeline.Send(message, userCancellationToken);
3+
4+
if ((message.Response.IsError && ((context?.ErrorOptions & global::Azure.ErrorOptions.NoThrow) != global::Azure.ErrorOptions.NoThrow)))
5+
{
6+
throw new global::Azure.RequestFailedException(message.Response);
7+
}
8+
9+
return message.Response;

0 commit comments

Comments
 (0)