1
1
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
+ using System ;
5
+ using System . Collections . Generic ;
4
6
using System . IO ;
5
7
using System . Linq ;
8
+ using System . Security . Cryptography ;
6
9
using System . Xml . Linq ;
7
10
using FluentAssertions ;
8
11
using Microsoft . NET . Build . Tasks ;
9
12
using Microsoft . NET . TestFramework ;
10
13
using Microsoft . NET . TestFramework . Assertions ;
11
14
using Microsoft . NET . TestFramework . Commands ;
15
+ using Microsoft . NET . TestFramework . ProjectConstruction ;
16
+ using Xunit ;
12
17
using Xunit . Abstractions ;
13
18
14
19
namespace Microsoft . NET . Build . Tests
@@ -19,7 +24,7 @@ public GivenThatWeWantToBuildACppCliProject(ITestOutputHelper log) : base(log)
19
24
{
20
25
}
21
26
22
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/3785" ) ]
27
+ [ FullMSBuildOnlyFact ]
23
28
public void It_builds_and_runs ( )
24
29
{
25
30
var testAsset = _testAssetsManager
@@ -40,7 +45,7 @@ public void It_builds_and_runs()
40
45
var exe = Path . Combine ( //find the platform directory
41
46
new DirectoryInfo ( Path . Combine ( testAsset . TestRoot , "CSConsoleApp" , "bin" ) ) . GetDirectories ( ) . Single ( ) . FullName ,
42
47
"Debug" ,
43
- "net5.0" ,
48
+ ToolsetInfo . CurrentTargetFramework ,
44
49
"CSConsoleApp.exe" ) ;
45
50
46
51
var runCommand = new RunExeCommand ( Log , exe ) ;
@@ -52,7 +57,27 @@ public void It_builds_and_runs()
52
57
. HaveStdOutContaining ( "Hello, World!" ) ;
53
58
}
54
59
55
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/3785" ) ]
60
+ [ FullMSBuildOnlyFact ]
61
+ public void It_builds_and_runs_with_package_reference ( )
62
+ {
63
+ var targetFramework = ToolsetInfo . CurrentTargetFramework + "-windows" ;
64
+ var testAsset = _testAssetsManager
65
+ . CopyTestAsset ( "NetCoreCsharpAppReferenceCppCliLib" )
66
+ . WithSource ( )
67
+ . WithProjectChanges ( ( projectPath , project ) => ConfigureProject ( projectPath , project , "NewtonSoft.Json" , "13.0.1" , targetFramework , new string [ ] { "_EnablePackageReferencesInVCProjects" , "IncludeWindowsSDKRefFrameworkReferences" } ) ) ;
68
+
69
+ // build projects separately with BuildProjectReferences=false to simulate VS build behavior
70
+ new BuildCommand ( testAsset , "NETCoreCppCliTest" )
71
+ . Execute ( "-p:Platform=x64" , "-p:EnableManagedpackageReferenceSupport=true" , "/bl" )
72
+ . Should ( )
73
+ . Pass ( ) ;
74
+
75
+ var cppnProjProperties = GetPropertyValues ( testAsset . TestRoot , "NETCoreCppCliTest" , targetFramework : targetFramework ) ;
76
+ Assert . True ( cppnProjProperties [ "_EnablePackageReferencesInVCProjects" ] == "true" ) ;
77
+ Assert . True ( cppnProjProperties [ "IncludeWindowsSDKRefFrameworkReferences" ] == "" ) ;
78
+ }
79
+
80
+ [ FullMSBuildOnlyFact ]
56
81
public void Given_no_restore_It_builds_cpp_project ( )
57
82
{
58
83
var testAsset = _testAssetsManager
@@ -65,7 +90,7 @@ public void Given_no_restore_It_builds_cpp_project()
65
90
. Pass ( ) ;
66
91
}
67
92
68
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/3785" ) ]
93
+ [ FullMSBuildOnlyFact ]
69
94
public void Given_Wpf_framework_reference_It_builds_cpp_project ( )
70
95
{
71
96
var testAsset = _testAssetsManager
@@ -78,7 +103,7 @@ public void Given_Wpf_framework_reference_It_builds_cpp_project()
78
103
. Pass ( ) ;
79
104
}
80
105
81
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/11008" ) ]
106
+ [ FullMSBuildOnlyFact ]
82
107
public void It_fails_with_error_message_on_EnableComHosting ( )
83
108
{
84
109
var testAsset = _testAssetsManager
@@ -106,7 +131,7 @@ public void It_fails_with_error_message_on_EnableComHosting()
106
131
. HaveStdOutContaining ( Strings . NoSupportCppEnableComHosting ) ;
107
132
}
108
133
109
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/11008" ) ]
134
+ [ FullMSBuildOnlyFact ]
110
135
public void It_fails_with_error_message_on_fullframework ( )
111
136
{
112
137
var testAsset = _testAssetsManager
@@ -123,7 +148,7 @@ public void It_fails_with_error_message_on_fullframework()
123
148
. HaveStdOutContaining ( Strings . NETFrameworkWithoutUsingNETSdkDefaults ) ;
124
149
}
125
150
126
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/11008" ) ]
151
+ [ FullMSBuildOnlyFact ]
127
152
public void It_fails_with_error_message_on_tfm_lower_than_3_1 ( )
128
153
{
129
154
var testAsset = _testAssetsManager
@@ -140,7 +165,7 @@ public void It_fails_with_error_message_on_tfm_lower_than_3_1()
140
165
. HaveStdOutContaining ( Strings . CppRequiresTFMVersion31 ) ;
141
166
}
142
167
143
- [ FullMSBuildOnlyFact ( Skip = "https://github.com/dotnet/sdk/issues/11008" ) ]
168
+ [ FullMSBuildOnlyFact ]
144
169
public void When_run_with_selfcontained_It_fails_with_error_message ( )
145
170
{
146
171
var testAsset = _testAssetsManager
@@ -166,5 +191,79 @@ private void ChangeTargetFramework(string projectPath, XDocument project, string
166
191
. Single ( ) . Value = targetFramework ;
167
192
}
168
193
}
194
+
195
+ private void ConfigureProject ( string projectPath , XDocument project , string package , string version , string targetFramework , string [ ] properties )
196
+ {
197
+ ChangeTargetFramework ( projectPath , project , targetFramework ) ;
198
+ AddPackageReference ( projectPath , project , package , version ) ;
199
+ RecordProperties ( projectPath , project , properties ) ;
200
+ }
201
+
202
+ private void AddPackageReference ( string projectPath , XDocument project , string package , string version )
203
+ {
204
+ if ( Path . GetExtension ( projectPath ) == ".vcxproj" )
205
+ {
206
+ XNamespace ns = project . Root . Name . Namespace ;
207
+ XElement itemGroup = project . Root . Descendants ( ns + "ItemGroup" ) . First ( ) ;
208
+ itemGroup . Add ( new XElement ( ns + "PackageReference" , new XAttribute ( "Include" , package ) ,
209
+ new XAttribute ( "Version" , version ) ) ) ;
210
+
211
+ }
212
+ }
213
+ private void RecordProperties ( string projectPath , XDocument project , string [ ] properties )
214
+ {
215
+ if ( Path . GetExtension ( projectPath ) == ".vcxproj" )
216
+ {
217
+ List < XElement > propertiesElements = new List < XElement > ( ) ;
218
+ XNamespace ns = project . Root . Name . Namespace ;
219
+ foreach ( var propertyName in properties )
220
+ {
221
+ propertiesElements . Add ( new XElement ( ns + "LinesToWrite" , new XAttribute ( "Include" , @$ "{ propertyName } : $({ propertyName } )") ) ) ;
222
+ }
223
+
224
+ // string target = $@"<Target Name='WritePropertyValues' BeforeTargets='AfterBuild'>
225
+ // <ItemGroup>
226
+ //{propertiesElements}
227
+ // </ItemGroup>
228
+ // <WriteLinesToFile
229
+ // File='$(BaseIntermediateOutputPath)\$(Configuration)\$(TargetFramework)\PropertyValues.txt'
230
+ // Lines='@(LinesToWrite)'
231
+ // Overwrite='true'
232
+ // Encoding='Unicode'
233
+ // />
234
+ // </Target>";
235
+ XElement newNode = new XElement ( ns + "Target" , new XAttribute ( "Name" , "WriteProperty" ) , new XAttribute ( "BeforeTargets" , "AfterBuild" ) ,
236
+ new XElement ( ns + "ItemGroup" ,
237
+ propertiesElements ) ,
238
+ new XElement ( ns + "WriteLinesToFile" ,
239
+ new XAttribute ( "File" , @"$(BaseIntermediateOutputPath)\$(Configuration)\$(TargetFramework)\PropertyValues.txt" ) ,
240
+ new XAttribute ( "Lines" , "@(LinesToWrite)" ) ,
241
+ new XAttribute ( "Overwrite" , "true" ) ,
242
+ new XAttribute ( "Encoding" , "Unicode" ) ) ) ;
243
+
244
+
245
+ project . Root . AddFirst ( newNode ) ;
246
+ }
247
+ }
248
+
249
+ public Dictionary < string , string > GetPropertyValues ( string testRoot , string project , string configuration = "Debug" , string targetFramework = null )
250
+ {
251
+ var propertyValues = new Dictionary < string , string > ( ) ;
252
+
253
+ string intermediateOutputPath = Path . Combine ( testRoot , project , "obj" , configuration , targetFramework ?? "foo" ) ;
254
+
255
+ foreach ( var line in File . ReadAllLines ( Path . Combine ( intermediateOutputPath , "PropertyValues.txt" ) ) )
256
+ {
257
+ int colonIndex = line . IndexOf ( ':' ) ;
258
+ if ( colonIndex > 0 )
259
+ {
260
+ string propertyName = line . Substring ( 0 , colonIndex ) ;
261
+ string propertyValue = line . Length == colonIndex + 1 ? "" : line . Substring ( colonIndex + 2 ) ;
262
+ propertyValues [ propertyName ] = propertyValue ;
263
+ }
264
+ }
265
+
266
+ return propertyValues ;
267
+ }
169
268
}
170
269
}
0 commit comments