33
44using System . Collections . Generic ;
55using System . IO ;
6+ using System . Text . RegularExpressions ;
67using Xunit ;
78using Xunit . Abstractions ;
89
@@ -12,6 +13,7 @@ namespace Wasm.Build.Tests
1213{
1314 public class WasmNativeDefaultsTests : TestMainJsTestBase
1415 {
16+ private static Regex s_regex = new ( "\\ *\\ * WasmBuildNative:.*" ) ;
1517 public WasmNativeDefaultsTests ( ITestOutputHelper output , SharedBuildPerTestClassFixture buildContext )
1618 : base ( output , buildContext )
1719 {
@@ -39,19 +41,20 @@ public static TheoryData<string, string, bool, bool, bool> SettingDifferentFromV
3941 // Config=Release always causes relinking when publishing
4042 bool publishValue = forPublish && config == "Release" ? true : false ;
4143 // Setting the default value from the runtime pack shouldn't trigger relinking
42- data . Add ( config , $ "<{ defaultPair . propertyName } >{ defaultPair . defaultValueInRuntimePack } </{ defaultPair . propertyName } >",
44+ data . Add ( config , $ "<{ defaultPair . propertyName } >{ defaultPair . defaultValueInRuntimePack . ToString ( ) . ToLower ( ) } </{ defaultPair . propertyName } >",
4345 /*aot*/ false , /*build*/ false , /*publish*/ publishValue ) ;
4446 // Leaving the property unset, so checking the default
4547 data . Add ( config , "" , /*aot*/ false , /*build*/ false , /*publish*/ publishValue ) ;
4648
4749 // Setting the !default value should trigger relinking
48- data . Add ( config , $ "<{ defaultPair . propertyName } >{ ! defaultPair . defaultValueInRuntimePack } </{ defaultPair . propertyName } >",
50+ data . Add ( config , $ "<{ defaultPair . propertyName } >{ ( ! defaultPair . defaultValueInRuntimePack ) . ToString ( ) . ToLower ( ) } </{ defaultPair . propertyName } >",
4951 /*aot*/ false , /*build*/ true , /*publish*/ true ) ;
5052 }
5153 }
5254
5355 return data ;
5456 }
57+
5558 public static TheoryData < string , string , bool , bool , bool > DefaultsTestData ( bool forPublish )
5659 {
5760 TheoryData < string , string , bool , bool , bool > data = new ( )
@@ -93,45 +96,34 @@ public static TheoryData<string, string, bool, bool, bool> DefaultsTestData(bool
9396 return data ;
9497 }
9598
99+ #pragma warning disable xUnit1026 // For unused *buildValue*, and *publishValue* parameters
96100 [ Theory ]
97101 [ MemberData ( nameof ( DefaultsTestData ) , parameters : false ) ]
98102 [ MemberData ( nameof ( SettingDifferentFromValuesInRuntimePack ) , parameters : false ) ]
99103 public void DefaultsWithBuild ( string config , string extraProperties , bool aot , bool expectWasmBuildNativeForBuild , bool expectWasmBuildNativeForPublish )
100104 {
101- string output = CheckWasmNativeDefaultValue ( "native_defaults_build" , config , extraProperties , aot , dotnetWasmFromRuntimePack : ! expectWasmBuildNativeForPublish , publish : false ) ;
102-
103- bool expectedWasmNativeStripValue = true ;
104- if ( /*isBuild && */ expectWasmBuildNativeForBuild && config == "Debug" )
105- expectedWasmNativeStripValue = false ;
105+ ( string output , string ? line ) = CheckWasmNativeDefaultValue ( "native_defaults_build" , config , extraProperties , aot , dotnetWasmFromRuntimePack : ! expectWasmBuildNativeForBuild , publish : false ) ;
106106
107- // bool expectedWasmNativeStripValue = !(wasmBuildNativeForBuild && config == "Debug");
108- // for build
109- Assert . Contains ( $ "** WasmBuildNative: '{ expectWasmBuildNativeForBuild . ToString ( ) . ToLower ( ) } ', WasmNativeStrip: '{ expectedWasmNativeStripValue . ToString ( ) . ToLower ( ) } ', WasmBuildingForNestedPublish: ''", output ) ;
110- Assert . Contains ( "Stopping the build" , output ) ;
107+ InferAndCheckPropertyValues ( line , isPublish : false , wasmBuildNative : expectWasmBuildNativeForBuild , config : config ) ;
111108 }
112109
113- #pragma warning disable xUnit1026 // For unused *buildValue* parameter
114110 [ Theory ]
115111 [ MemberData ( nameof ( DefaultsTestData ) , parameters : true ) ]
116112 [ MemberData ( nameof ( SettingDifferentFromValuesInRuntimePack ) , parameters : true ) ]
117113 public void DefaultsWithPublish ( string config , string extraProperties , bool aot , bool expectWasmBuildNativeForBuild , bool expectWasmBuildNativeForPublish )
118114 {
119- string output = CheckWasmNativeDefaultValue ( "native_defaults_publish" , config , extraProperties , aot , dotnetWasmFromRuntimePack : ! expectWasmBuildNativeForPublish , publish : true ) ;
115+ ( string output , string ? line ) = CheckWasmNativeDefaultValue ( "native_defaults_publish" , config , extraProperties , aot , dotnetWasmFromRuntimePack : ! expectWasmBuildNativeForPublish , publish : true ) ;
120116
121- // for build
122- // Assert.DoesNotContain($"** WasmBuildNative: '{buildValue.ToString().ToLower()}', WasmNativeStrip: 'true', WasmBuildingForNestedPublish: ''", output);
123- // for publish
124- Assert . Contains ( $ "** WasmBuildNative: '{ expectWasmBuildNativeForPublish . ToString ( ) . ToLower ( ) } ', WasmNativeStrip: 'true', WasmBuildingForNestedPublish: 'true'", output ) ;
125- Assert . Contains ( "Stopping the build" , output ) ;
117+ InferAndCheckPropertyValues ( line , isPublish : true , wasmBuildNative : expectWasmBuildNativeForPublish , config : config ) ;
126118 }
127119#pragma warning restore xunit1026
128120
129121 public static TheoryData < string , string , bool , bool > SetWasmNativeStripExplicitlyTestData ( bool publish ) => new ( )
130122 {
131- { "Debug" , "<WasmNativeStrip>true</WasmNativeStrip>" , false , true } ,
132- { "Release" , "<WasmNativeStrip>true</WasmNativeStrip>" , publish , true } ,
133- { "Debug" , "<WasmNativeStrip>false</WasmNativeStrip>" , true , false } ,
134- { "Release" , "<WasmNativeStrip>false</WasmNativeStrip>" , true , false }
123+ { "Debug" , "<WasmNativeStrip>true</WasmNativeStrip>" , /*wasmBuildNative*/ false , /*wasmNativeStrip*/ true } ,
124+ { "Release" , "<WasmNativeStrip>true</WasmNativeStrip>" , /*wasmBuildNative*/ publish , /*wasmNativeStrip*/ true } ,
125+ { "Debug" , "<WasmNativeStrip>false</WasmNativeStrip>" , /*wasmBuildNative*/ true , /*wasmNativeStrip*/ false } ,
126+ { "Release" , "<WasmNativeStrip>false</WasmNativeStrip>" , /*wasmBuildNative*/ true , /*wasmNativeStrip*/ false }
135127 } ;
136128
137129 public static TheoryData < string , string , bool , bool > SetWasmNativeStripExplicitlyWithWasmBuildNativeTestData ( ) => new ( )
@@ -147,48 +139,52 @@ public void DefaultsWithPublish(string config, string extraProperties, bool aot,
147139 [ MemberData ( nameof ( SetWasmNativeStripExplicitlyWithWasmBuildNativeTestData ) ) ]
148140 public void WasmNativeStripDefaultWithBuild ( string config , string extraProperties , bool expectedWasmBuildNativeValue , bool expectedWasmNativeStripValue )
149141 {
150- string output = CheckWasmNativeDefaultValue ( "native_strip_defaults" , config , extraProperties , aot : false , dotnetWasmFromRuntimePack : ! expectedWasmBuildNativeValue , publish : false ) ;
142+ ( string output , string ? line ) = CheckWasmNativeDefaultValue ( "native_strip_defaults" , config , extraProperties , aot : false , dotnetWasmFromRuntimePack : ! expectedWasmBuildNativeValue , publish : false ) ;
151143
152- Assert . Contains ( $ "** WasmBuildNative: '{ expectedWasmBuildNativeValue . ToString ( ) . ToLower ( ) } ', WasmNativeStrip: '{ expectedWasmNativeStripValue . ToString ( ) . ToLower ( ) } ', WasmBuildingForNestedPublish: ''", output ) ;
153- Assert . Contains ( "Stopping the build" , output ) ;
144+ CheckPropertyValues ( line ,
145+ wasmBuildNative : expectedWasmBuildNativeValue ,
146+ wasmNativeStrip : expectedWasmNativeStripValue ,
147+ wasmNativeDebugSymbols : true ,
148+ wasmBuildingForNestedPublish : null ) ;
154149 }
155150
156151 [ Theory ]
157152 [ MemberData ( nameof ( SetWasmNativeStripExplicitlyTestData ) , parameters : /*publish*/ true ) ]
158153 [ MemberData ( nameof ( SetWasmNativeStripExplicitlyWithWasmBuildNativeTestData ) ) ]
159154 public void WasmNativeStripDefaultWithPublish ( string config , string extraProperties , bool expectedWasmBuildNativeValue , bool expectedWasmNativeStripValue )
160155 {
161- string output = CheckWasmNativeDefaultValue ( "native_strip_defaults" , config , extraProperties , aot : false , dotnetWasmFromRuntimePack : ! expectedWasmBuildNativeValue , publish : true ) ;
156+ ( string output , string ? line ) = CheckWasmNativeDefaultValue ( "native_strip_defaults" , config , extraProperties , aot : false , dotnetWasmFromRuntimePack : ! expectedWasmBuildNativeValue , publish : true ) ;
162157
163- Assert . Contains ( $ "** WasmBuildNative: '{ expectedWasmBuildNativeValue . ToString ( ) . ToLower ( ) } ', WasmNativeStrip: '{ expectedWasmNativeStripValue . ToString ( ) . ToLower ( ) } ', WasmBuildingForNestedPublish: 'true'", output ) ;
164- Assert . Contains ( "Stopping the build" , output ) ;
158+ CheckPropertyValues ( line ,
159+ wasmBuildNative : expectedWasmBuildNativeValue ,
160+ wasmNativeStrip : expectedWasmNativeStripValue ,
161+ wasmNativeDebugSymbols : true ,
162+ wasmBuildingForNestedPublish : true ) ;
165163 }
166164
167165 [ Theory ]
168166 /* always relink */
169- [ InlineData ( "Debug" , "" , /*build*/ true , /*publish*/ true ) ]
170- [ InlineData ( "Release" , "" , /*build*/ true , /*publish*/ true ) ]
171- [ InlineData ( "Release" , "<PublishTrimmed>false</PublishTrimmed>" , /*build*/ true , /*publish*/ true ) ]
172- public void WithNativeReference ( string config , string extraProperties , bool buildValue , bool publishValue )
167+ [ InlineData ( "Debug" , "" , /*publish*/ false ) ]
168+ [ InlineData ( "Debug" , "" , /*publish*/ true ) ]
169+ [ InlineData ( "Release" , "" , /*publish*/ false ) ]
170+ [ InlineData ( "Release" , "" , /*publish*/ true ) ]
171+ [ InlineData ( "Release" , "<PublishTrimmed>false</PublishTrimmed>" , /*publish*/ true ) ]
172+ public void WithNativeReference ( string config , string extraProperties , bool publish )
173173 {
174174 string nativeLibPath = Path . Combine ( BuildEnvironment . TestAssetsPath , "native-libs" , "native-lib.o" ) ;
175175 string nativeRefItem = @$ "<NativeFileReference Include=""{ nativeLibPath } "" />";
176- string output = CheckWasmNativeDefaultValue ( "native_defaults_publish" ,
176+ ( string output , string ? line ) = CheckWasmNativeDefaultValue ( "native_defaults_publish" ,
177177 config ,
178178 extraProperties ,
179179 aot : false ,
180- dotnetWasmFromRuntimePack : ! publishValue ,
181- publish : true ,
180+ dotnetWasmFromRuntimePack : ! publish ,
181+ publish : publish ,
182182 extraItems : nativeRefItem ) ;
183183
184- // for build - FIXME:
185- Assert . DoesNotContain ( $ "** WasmBuildNative: '{ buildValue . ToString ( ) . ToLower ( ) } ', WasmBuildingForNestedPublish: ''", output ) ;
186- // for publish
187- Assert . Contains ( $ "** WasmBuildNative: '{ publishValue . ToString ( ) . ToLower ( ) } ', WasmNativeStrip: 'true', WasmBuildingForNestedPublish: 'true'", output ) ;
188- Assert . Contains ( "Stopping the build" , output ) ;
184+ InferAndCheckPropertyValues ( line , isPublish : publish , wasmBuildNative : true , config : config ) ;
189185 }
190186
191- private string CheckWasmNativeDefaultValue ( string projectName ,
187+ private ( string , string ? ) CheckWasmNativeDefaultValue ( string projectName ,
192188 string config ,
193189 string extraProperties ,
194190 bool aot ,
@@ -201,7 +197,7 @@ private string CheckWasmNativeDefaultValue(string projectName,
201197
202198 string printValueTarget = @"
203199 <Target Name=""PrintWasmBuildNative"" AfterTargets=""_BeforeWasmBuildApp"">
204- <Message Text=""** WasmBuildNative: '$(WasmBuildNative)', WasmNativeStrip: '$(WasmNativeStrip)', WasmBuildingForNestedPublish: '$(WasmBuildingForNestedPublish)'"" Importance=""High"" />
200+ <Message Text=""** WasmBuildNative: '$(WasmBuildNative)', WasmNativeStrip: '$(WasmNativeStrip)', WasmNativeDebugSymbols: '$(WasmNativeDebugSymbols)', WasmBuildingForNestedPublish: '$(WasmBuildingForNestedPublish)'"" Importance=""High"" />
205201 " + ( publish
206202 ? @"<Error Text=""Stopping the build"" Condition=""$(WasmBuildingForNestedPublish) == 'true'"" />"
207203 : @"<Error Text=""Stopping the build"" />" )
@@ -223,7 +219,32 @@ private string CheckWasmNativeDefaultValue(string projectName,
223219 BuildOnlyAfterPublish : false ,
224220 Publish : publish ) ) ;
225221
226- return output ;
222+ Assert . Contains ( "Stopping the build" , output ) ;
223+
224+ Match m = s_regex . Match ( output ) ;
225+ Assert . Equal ( 1 , m . Groups . Count ) ;
226+ return ( output , m . Success ? m . Groups [ 0 ] ? . ToString ( ) : null ) ;
227+ }
228+
229+ private void InferAndCheckPropertyValues ( string ? line , bool isPublish , bool wasmBuildNative , string config )
230+ {
231+ bool expectedWasmNativeStripValue ;
232+ if ( ! isPublish && wasmBuildNative && config == "Debug" )
233+ expectedWasmNativeStripValue = false ;
234+ else
235+ expectedWasmNativeStripValue = true ;
236+
237+ CheckPropertyValues ( line , wasmBuildNative , expectedWasmNativeStripValue , /*wasmNativeDebugSymbols*/ true , isPublish ) ;
238+ }
239+
240+ private void CheckPropertyValues ( string ? line , bool wasmBuildNative , bool wasmNativeStrip , bool wasmNativeDebugSymbols , bool ? wasmBuildingForNestedPublish )
241+ {
242+ Assert . NotNull ( line ) ;
243+ Assert . Contains ( $ "** WasmBuildNative: '{ wasmBuildNative . ToString ( ) . ToLower ( ) } ', " +
244+ $ "WasmNativeStrip: '{ wasmNativeStrip . ToString ( ) . ToLower ( ) } ', " +
245+ $ "WasmNativeDebugSymbols: '{ wasmNativeDebugSymbols . ToString ( ) . ToLower ( ) } ', " +
246+ $ "WasmBuildingForNestedPublish: '{ ( wasmBuildingForNestedPublish . HasValue && wasmBuildingForNestedPublish == true ? "true" : "" ) } '",
247+ line ) ;
227248 }
228249 }
229250}
0 commit comments