3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using System . Reflection ;
6
- using System . Runtime . ExceptionServices ;
7
6
using System . Xml . Linq ;
8
7
using PublicApiGenerator ;
9
8
using Shouldly ;
@@ -31,7 +30,8 @@ public void public_api_should_not_change_unintentionally(Type type)
31
30
string baseDir = AppDomain . CurrentDomain . BaseDirectory ;
32
31
string projectName = type . Assembly . GetName ( ) . Name ! ;
33
32
string projectFolderName = projectName [ "GraphQL.Server." . Length ..] ;
34
- string projectDir = Path . Combine ( baseDir , $ "..{ Path . DirectorySeparatorChar } ..{ Path . DirectorySeparatorChar } ..{ Path . DirectorySeparatorChar } ..{ Path . DirectorySeparatorChar } ..", "src" ) ;
33
+ string testDir = Path . Combine ( baseDir , $ "..{ Path . DirectorySeparatorChar } ..{ Path . DirectorySeparatorChar } ..") ;
34
+ string projectDir = Path . Combine ( testDir , $ "..{ Path . DirectorySeparatorChar } ..", "src" ) ;
35
35
string buildDir = Path . Combine ( projectDir , projectFolderName , "bin" , "Debug" ) ;
36
36
Debug . Assert ( Directory . Exists ( buildDir ) , $ "Directory '{ buildDir } ' doesn't exist") ;
37
37
string csProject = Path . Combine ( projectDir , projectFolderName , projectFolderName + ".csproj" ) ;
@@ -49,35 +49,43 @@ public void public_api_should_not_change_unintentionally(Type type)
49
49
ExcludeAttributes = new [ ] { "System.Diagnostics.DebuggerDisplayAttribute" , "System.Diagnostics.CodeAnalysis.AllowNullAttribute" }
50
50
} ) ) ) . ToArray ( ) ;
51
51
52
- // See: https://shouldly.readthedocs.io/en/latest/assertions/shouldMatchApproved.html
53
- // Note: If the AssemblyName.approved.txt file doesn't match the latest publicApi value,
54
- // this call will try to launch a diff tool to help you out but that can fail on
55
- // your machine if a diff tool isn't configured/setup.
56
52
if ( publicApi . DistinctBy ( item => item . content ) . Count ( ) == 1 )
57
53
{
58
- publicApi [ 0 ] . content . ShouldMatchApproved ( options => options . NoDiff ( ) . WithFilenameGenerator ( ( testMethodInfo , discriminator , fileType , fileExtension ) => $ " { type . Assembly . GetName ( ) . Name } . { fileType } . { fileExtension } " ) ) ;
54
+ AutoApproveOrFail ( publicApi [ 0 ] . content , "" ) ;
59
55
}
60
56
else
61
57
{
62
- // https://github.com/graphql-dotnet/server/pull/675#issuecomment-1001283947
63
- ExceptionDispatchInfo ? error = null ;
58
+ foreach ( var item in publicApi . ToLookup ( item => item . content ) )
59
+ {
60
+ AutoApproveOrFail ( item . Key , string . Join ( "+" , item . Select ( x => x . tfm ) . OrderBy ( x => x ) ) ) ;
61
+ }
62
+ }
63
+
64
+ // Approval test should (re)generate approved.txt files locally if needed.
65
+ // Approval test should fail on CI.
66
+ // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
67
+ void AutoApproveOrFail ( string publicApi , string folder )
68
+ {
69
+ string file = null ! ;
64
70
65
- var uniqueApi = publicApi . ToLookup ( item => item . content ) ;
66
- foreach ( var item in uniqueApi )
71
+ try
72
+ {
73
+ publicApi . ShouldMatchApproved ( options => options . SubFolder ( folder ) . NoDiff ( ) . WithFilenameGenerator ( ( testMethodInfo , discriminator , fileType , fileExtension ) => file = $ "{ type . Assembly . GetName ( ) . Name } .{ fileType } .{ fileExtension } ") ) ;
74
+ }
75
+ catch ( ShouldMatchApprovedException ) when ( Environment . GetEnvironmentVariable ( "CI" ) == null )
67
76
{
68
- try
77
+ string ? received = Path . Combine ( testDir , folder , file ) ;
78
+ string ? approved = received . Replace ( ".received.txt" , ".approved.txt" ) ;
79
+ if ( File . Exists ( received ) && File . Exists ( approved ) )
69
80
{
70
- item . Key . ShouldMatchApproved ( options => options . SubFolder ( string . Join ( "+" , item . Select ( x => x . tfm ) . OrderBy ( x => x ) ) ) . NoDiff ( ) . WithFilenameGenerator ( ( testMethodInfo , discriminator , fileType , fileExtension ) => $ "{ type . Assembly . GetName ( ) . Name } .{ fileType } .{ fileExtension } ") ) ;
81
+ File . Copy ( received , approved , overwrite : true ) ;
82
+ File . Delete ( received ) ;
71
83
}
72
- catch ( Exception ex )
84
+ else
73
85
{
74
- error = ExceptionDispatchInfo . Capture ( ex ) ;
86
+ throw ;
75
87
}
76
88
}
77
-
78
- // It's OK to throw any exception occurred.
79
- if ( error != null )
80
- error . Throw ( ) ;
81
89
}
82
90
}
83
91
}
0 commit comments