forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGivenThatWeWantToBuildACppCliProject.cs
More file actions
263 lines (230 loc) · 10.8 KB
/
GivenThatWeWantToBuildACppCliProject.cs
File metadata and controls
263 lines (230 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.NET.Build.Tasks;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToBuildACppCliProject : SdkTest
{
public GivenThatWeWantToBuildACppCliProject(ITestOutputHelper log) : base(log)
{
}
[FullMSBuildOnlyFact(Skip = "https://github.com/dotnet/sdk/issues/11008")]
public void It_builds_and_runs()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource();
// build projects separately with BuildProjectReferences=false to simulate VS build behavior
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64")
.Should()
.Pass();
new BuildCommand(testAsset, "CSConsoleApp")
.Execute(new string[] { "-p:Platform=x64", "-p:BuildProjectReferences=false" })
.Should()
.Pass();
var exe = Path.Combine( //find the platform directory
new DirectoryInfo(Path.Combine(testAsset.TestRoot, "CSConsoleApp", "bin")).GetDirectories().Single().FullName,
"Debug",
ToolsetInfo.CurrentTargetFramework,
"CSConsoleApp.exe");
var runCommand = new RunExeCommand(Log, exe);
runCommand
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining("Hello, World!");
}
[FullMSBuildOnlyFact]
public void It_builds_and_runs_with_package_reference()
{
var targetFramework = ToolsetInfo.CurrentTargetFramework + "-windows";
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource()
.WithProjectChanges((projectPath, project) => ConfigureProject(projectPath, project, "NewtonSoft.Json","13.0.1", targetFramework, new string[] { "_EnablePackageReferencesInVCProjects" , "IncludeWindowsSDKRefFrameworkReferences" }));
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64", "-p:EnableManagedpackageReferenceSupport=true")
.Should()
.Pass();
var cppnProjProperties = GetPropertyValues(testAsset.TestRoot, "NETCoreCppCliTest", targetFramework: targetFramework);
Assert.True(cppnProjProperties["_EnablePackageReferencesInVCProjects"] == "true");
Assert.True(cppnProjProperties["IncludeWindowsSDKRefFrameworkReferences"] == "");
}
[FullMSBuildOnlyFact(Skip = "https://github.com/dotnet/sdk/issues/11008")]
public void Given_no_restore_It_builds_cpp_project()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource();
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64")
.Should()
.Pass();
}
[FullMSBuildOnlyFact(Skip = "https://github.com/dotnet/sdk/issues/11008")]
public void Given_Wpf_framework_reference_It_builds_cpp_project()
{
var testAsset = _testAssetsManager
.CopyTestAsset("CppCliLibWithWpfFrameworkReference")
.WithSource();
new BuildCommand(testAsset)
.Execute("-p:Platform=x64")
.Should()
.Pass();
}
[FullMSBuildOnlyFact]
public void It_fails_with_error_message_on_EnableComHosting()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource()
.WithProjectChanges((projectPath, project) =>
{
if (Path.GetExtension(projectPath) == ".vcxproj")
{
XNamespace ns = project.Root.Name.Namespace;
var globalPropertyGroup = project.Root
.Descendants(ns + "PropertyGroup")
.Where(e => e.Attribute("Label")?.Value == "Globals")
.Single();
globalPropertyGroup.Add(new XElement(ns + "EnableComHosting", "true"));
}
});
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64")
.Should()
.Fail()
.And
.HaveStdOutContaining(Strings.NoSupportCppEnableComHosting);
}
[FullMSBuildOnlyFact]
public void It_fails_with_error_message_on_fullframework()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource()
.WithProjectChanges((projectPath, project) =>
ChangeTargetFramework(projectPath, project, "net472"));
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64")
.Should()
.Fail()
.And
.HaveStdOutContaining(Strings.NETFrameworkWithoutUsingNETSdkDefaults);
}
[FullMSBuildOnlyFact]
public void It_fails_with_error_message_on_tfm_lower_than_3_1()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource()
.WithProjectChanges((projectPath, project) =>
ChangeTargetFramework(projectPath, project, "netcoreapp3.0"));
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64")
.Should()
.Fail()
.And
.HaveStdOutContaining(Strings.CppRequiresTFMVersion31);
}
[FullMSBuildOnlyFact]
public void When_run_with_selfcontained_It_fails_with_error_message()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource();
new BuildCommand(testAsset, "NETCoreCppCliTest")
.Execute("-p:Platform=x64", "-p:selfcontained=true", $"-p:RuntimeIdentifier={ToolsetInfo.LatestWinRuntimeIdentifier}-x64")
.Should()
.Fail()
.And
.HaveStdOutContaining(Strings.NoSupportCppSelfContained);
}
private void ChangeTargetFramework(string projectPath, XDocument project, string targetFramework)
{
if (Path.GetExtension(projectPath) == ".vcxproj")
{
XNamespace ns = project.Root.Name.Namespace;
project.Root.Descendants(ns + "PropertyGroup")
.Descendants(ns + "TargetFramework")
.Single().Value = targetFramework;
}
}
private void ConfigureProject(string projectPath, XDocument project, string package, string version, string targetFramework, string[] properties)
{
ChangeTargetFramework(projectPath, project, targetFramework);
AddPackageReference(projectPath, project, package, version);
RecordProperties(projectPath, project, properties);
}
private void AddPackageReference(string projectPath, XDocument project, string package, string version)
{
if (Path.GetExtension(projectPath) == ".vcxproj")
{
XNamespace ns = project.Root.Name.Namespace;
XElement itemGroup = project.Root.Descendants(ns + "ItemGroup").First();
itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", package),
new XAttribute("Version", version)));
}
}
private void RecordProperties(string projectPath, XDocument project, string[] properties)
{
if (Path.GetExtension(projectPath) == ".vcxproj")
{
string propertiesTextElements = "";
XNamespace ns = project.Root.Name.Namespace;
foreach (var propertyName in properties)
{
propertiesTextElements += $" <LinesToWrite Include='{propertyName}: $({propertyName})'/>" + Environment.NewLine;
}
string target = $@"<Target Name='WritePropertyValues' BeforeTargets='AfterBuild'>
<ItemGroup>
{propertiesTextElements}
</ItemGroup>
<WriteLinesToFile
File='$(BaseIntermediateOutputPath)\$(Configuration)\$(TargetFramework)\PropertyValues.txt'
Lines='@(LinesToWrite)'
Overwrite='true'
Encoding='Unicode'
/>
</Target>";
XElement newNode = XElement.Parse(target);
foreach (var element in newNode.DescendantsAndSelf())
{
element.Name = ns + element.Name.LocalName;
}
project.Root.AddFirst(newNode);
}
}
public Dictionary<string, string> GetPropertyValues(string testRoot, string project, string configuration = "Debug", string targetFramework = null)
{
var propertyValues = new Dictionary<string, string>();
string intermediateOutputPath = Path.Combine(testRoot, project, "obj", configuration, targetFramework ?? "foo");
foreach (var line in File.ReadAllLines(Path.Combine(intermediateOutputPath, "PropertyValues.txt")))
{
int colonIndex = line.IndexOf(':');
if (colonIndex > 0)
{
string propertyName = line.Substring(0, colonIndex);
string propertyValue = line.Length == colonIndex + 1 ? "" : line.Substring(colonIndex + 2);
propertyValues[propertyName] = propertyValue;
}
}
return propertyValues;
}
}
}