1- using System ;
2- using System . Diagnostics ;
1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
34using System . IO ;
45using System . Linq ;
56using 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}
0 commit comments