Skip to content

Commit 6436b23

Browse files
committed
Add an integration test for direct references to assemblies.
1 parent 9512298 commit 6436b23

File tree

6 files changed

+166
-15
lines changed

6 files changed

+166
-15
lines changed

build/DependencyVersions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<!-- Dependencies from test projects -->
1212
<PropertyGroup>
13+
<MicrosoftNETCoreApp20Version>2.0.0-beta-001509-00</MicrosoftNETCoreApp20Version>
1314
<xunitVersion>2.1.0</xunitVersion>
1415
<FluentAssertionsVersion>4.0.0</FluentAssertionsVersion>
1516
<FluentAssertionsJsonVersion>4.12.0</FluentAssertionsJsonVersion>

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildANetCoreApp.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,15 @@ public void It_targets_the_right_shared_framework(string targetFramework, string
3333
{
3434
Name = "SharedFrameworkTest",
3535
TargetFrameworks = targetFramework,
36+
RuntimeFrameworkVersion = runtimeFrameworkVersion,
3637
IsSdkProject = true,
3738
IsExe = true
3839
};
3940

4041
string testIdentifier = string.Join("_", targetFramework, runtimeFrameworkVersion ?? "null");
4142

42-
var testAsset = _testAssetsManager.CreateTestProject(testProject, nameof(It_targets_the_right_shared_framework), testIdentifier);
43-
44-
testAsset = testAsset.WithProjectChanges(project =>
45-
{
46-
var ns = project.Root.Name.Namespace;
47-
var propertyGroup = new XElement(ns + "PropertyGroup");
48-
project.Root.Add(propertyGroup);
49-
50-
if (runtimeFrameworkVersion != null)
51-
{
52-
propertyGroup.Add(new XElement(ns + "RuntimeFrameworkVersion", runtimeFrameworkVersion));
53-
}
54-
});
55-
56-
testAsset = testAsset.Restore(testProject.Name);
43+
var testAsset = _testAssetsManager.CreateTestProject(testProject, nameof(It_targets_the_right_shared_framework), testIdentifier)
44+
.Restore(testProject.Name);
5745

5846
var buildCommand = new BuildCommand(Stage0MSBuild, Path.Combine(testAsset.TestRoot, testProject.Name));
5947

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Runtime.InteropServices;
8+
using FluentAssertions;
9+
using Microsoft.DotNet.Cli.Utils;
10+
using Microsoft.NET.TestFramework;
11+
using Microsoft.NET.TestFramework.Assertions;
12+
using Microsoft.NET.TestFramework.Commands;
13+
using Microsoft.NET.TestFramework.ProjectConstruction;
14+
using static Microsoft.NET.TestFramework.Commands.MSBuildTest;
15+
using Xunit;
16+
17+
namespace Microsoft.NET.Build.Tests
18+
{
19+
public class GivenThatWeWantToReferenceAnAssembly : SdkTest
20+
{
21+
[Theory]
22+
[InlineData("netcoreapp2.0", "net40")]
23+
[InlineData("netcoreapp2.0", "netstandard1.5")]
24+
[InlineData("netcoreapp2.0", "netcoreapp1.0")]
25+
public void ItRunsAppsDirectlyReferencingAssemblies(
26+
string referencerTarget,
27+
string dependencyTarget)
28+
{
29+
string identifier = referencerTarget.ToString() + "_" + dependencyTarget.ToString();
30+
31+
TestProject dependencyProject = new TestProject()
32+
{
33+
Name = "Dependency",
34+
IsSdkProject = true,
35+
TargetFrameworks = dependencyTarget,
36+
};
37+
38+
// Skip running test if not running on Windows
39+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !dependencyProject.BuildsOnNonWindows)
40+
{
41+
return;
42+
}
43+
44+
dependencyProject.SourceFiles["Class1.cs"] = @"
45+
public class Class1
46+
{
47+
public static string GetMessage()
48+
{
49+
return ""Hello from a direct reference."";
50+
}
51+
}
52+
";
53+
54+
var dependencyAsset = _testAssetsManager.CreateTestProject(dependencyProject, identifier: identifier);
55+
string dependencyAssemblyPath = RestoreAndBuild(dependencyAsset, dependencyProject);
56+
57+
TestProject referencerProject = new TestProject()
58+
{
59+
Name = "Referencer",
60+
IsSdkProject = true,
61+
TargetFrameworks = referencerTarget,
62+
RuntimeFrameworkVersion = RepoInfo.NetCoreApp20Version,
63+
// Need to use a self-contained app for now because we don't use a CLI that has a "2.0" shared framework
64+
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(referencerTarget),
65+
IsExe = true,
66+
};
67+
referencerProject.References.Add(dependencyAssemblyPath);
68+
69+
referencerProject.SourceFiles["Program.cs"] = @"
70+
using System;
71+
public static class Program
72+
{
73+
public static void Main()
74+
{
75+
Console.WriteLine(Class1.GetMessage());
76+
}
77+
}
78+
";
79+
80+
var referencerAsset = _testAssetsManager.CreateTestProject(referencerProject, identifier: identifier);
81+
string applicationPath = RestoreAndBuild(referencerAsset, referencerProject);
82+
83+
Command.Create(RepoInfo.DotNetHostPath, new[] { applicationPath })
84+
.CaptureStdOut()
85+
.Execute()
86+
.Should().Pass()
87+
.And.HaveStdOutContaining("Hello from a direct reference.");
88+
}
89+
90+
private static string RestoreAndBuild(TestAsset testAsset, TestProject testProject)
91+
{
92+
testAsset.Restore(testProject.Name);
93+
94+
var buildCommand = new BuildCommand(Stage0MSBuild, Path.Combine(testAsset.TestRoot, testProject.Name));
95+
96+
buildCommand.Execute()
97+
.Should()
98+
.Pass();
99+
100+
var outputDirectory = buildCommand.GetOutputDirectory(testProject.TargetFrameworks);
101+
return Path.Combine(outputDirectory.FullName, testProject.Name + ".dll");
102+
}
103+
}
104+
}

test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<ProjectGuid>{EC640B7E-332E-40A2-BB6E-5B7EC788F315}</ProjectGuid>
77
<OutputType>Library</OutputType>
88
<AppDesignerFolder>Properties</AppDesignerFolder>
9+
<AssemblyName>Microsoft.NET.Build.Tests</AssemblyName>
910
<DefaultLanguage>en-US</DefaultLanguage>
1011
<TargetFramework>netstandard1.6</TargetFramework>
1112
<PackageTargetFallback>dotnet5.4;portable-net451+win8</PackageTargetFallback>

test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ public class TestProject
1818
// Applies to SDK Projects
1919
public string TargetFrameworks { get; set; }
2020

21+
public string RuntimeFrameworkVersion { get; set; }
22+
23+
public string RuntimeIdentifier { get; set; }
24+
2125
// TargetFrameworkVersion applies to non-SDK projects
2226
public string TargetFrameworkVersion { get; set; }
2327

2428
public List<TestProject> ReferencedProjects { get; } = new List<TestProject>();
2529

30+
public List<string> References { get; } = new List<string>();
31+
2632
public Dictionary<string, string> SourceFiles { get; } = new Dictionary<string, string>();
2733

2834
private static string GetShortTargetFrameworkIdentifier(string targetFramework)
@@ -129,6 +135,16 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
129135
propertyGroup.Add(new XElement(ns + "TargetFramework", this.TargetFrameworks));
130136
}
131137

138+
if (!string.IsNullOrEmpty(this.RuntimeFrameworkVersion))
139+
{
140+
propertyGroup.Add(new XElement(ns + "RuntimeFrameworkVersion", this.RuntimeFrameworkVersion));
141+
}
142+
143+
if (!string.IsNullOrEmpty(this.RuntimeIdentifier))
144+
{
145+
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", this.RuntimeIdentifier));
146+
}
147+
132148
if (this.IsExe && targetFrameworks.Any(identifier => GetShortTargetFrameworkIdentifier(identifier).Equals("net", StringComparison.OrdinalIgnoreCase)))
133149
{
134150
propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", "win7-x86"));
@@ -166,6 +182,23 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
166182
}
167183
}
168184

185+
if (this.References.Any())
186+
{
187+
var referenceItemGroup = projectXml.Root.Elements(ns + "ItemGroup")
188+
.FirstOrDefault(itemGroup => itemGroup.Elements(ns + "Reference").Count() > 0);
189+
if (referenceItemGroup == null)
190+
{
191+
referenceItemGroup = new XElement(ns + "ItemGroup");
192+
packageReferenceItemGroup.AddBeforeSelf(referenceItemGroup);
193+
}
194+
195+
foreach (var reference in References)
196+
{
197+
referenceItemGroup.Add(new XElement(ns + "Reference",
198+
new XAttribute("Include", reference)));
199+
}
200+
}
201+
169202
using (var file = File.CreateText(targetProjectPath))
170203
{
171204
projectXml.Save(file);

test/Microsoft.NET.TestFramework/RepoInfo.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
7+
using System.Xml.Linq;
68
using Microsoft.DotNet.Cli.Utils;
79

810
namespace Microsoft.NET.TestFramework
@@ -78,6 +80,28 @@ public static string DotNetHostPath
7880
}
7981
}
8082

83+
public static string NetCoreApp20Version { get; } = ReadNetCoreApp20Version();
84+
85+
private static string ReadNetCoreApp20Version()
86+
{
87+
var dependencyVersionsPath = Path.Combine(RepoRoot, "build", "DependencyVersions.props");
88+
var root = XDocument.Load(dependencyVersionsPath).Root;
89+
var ns = root.Name.Namespace;
90+
91+
var version = root
92+
.Elements(ns + "PropertyGroup")
93+
.Elements(ns + "MicrosoftNETCoreApp20Version")
94+
.FirstOrDefault()
95+
?.Value;
96+
97+
if (string.IsNullOrEmpty(version))
98+
{
99+
throw new InvalidOperationException($"Could not find a property named 'MicrosoftNETCoreApp20Version' in {dependencyVersionsPath}");
100+
}
101+
102+
return version;
103+
}
104+
81105
private static string FindConfigurationInBasePath()
82106
{
83107
// assumes tests are always executed from the "bin/$Configuration/Tests" directory

0 commit comments

Comments
 (0)