diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.BeforeCommon.targets b/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.BeforeCommon.targets index 2dc242df998e..0a9ff84ceff1 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.BeforeCommon.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.BeforeCommon.targets @@ -65,6 +65,29 @@ Copyright (c) .NET Foundation. All rights reserved. $(OutputPath)$(PublishDirName)\ + + + + <_TargetFrameworkVersionWithoutV>$(TargetFrameworkVersion) + <_TargetFrameworkVersionWithoutV Condition="$(TargetFrameworkVersion.StartsWith('v'))">$(TargetFrameworkVersion.Substring(1)) + + + + + + + + + + + + + + + + + <_FrameworkIdentifierForImplicitDefine>$(TargetFrameworkIdentifier.Replace('.', '').ToUpperInvariant()) diff --git a/test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopExe.cs b/test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopExe.cs index 1160af80d846..b074577f85c2 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopExe.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopExe.cs @@ -9,6 +9,7 @@ using Xunit; using FluentAssertions; using static Microsoft.NET.TestFramework.Commands.MSBuildTest; +using Microsoft.NET.TestFramework.ProjectConstruction; namespace Microsoft.NET.Build.Tests { @@ -110,6 +111,54 @@ public void It_respects_explicit_platform_target() $"PlatformTarget=x64"); } + [Fact] + public void It_includes_default_framework_references() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return; + } + + var testProject = new TestProject() + { + Name = "DefaultReferences", + // TODO: Add net35 to the TargetFrameworks list once https://github.com/Microsoft/msbuild/issues/1333 is fixed + TargetFrameworks = "net40;net45;net461", + IsSdkProject = true, + IsExe = true + }; + + string sourceFile = +@"using System; + +namespace DefaultReferences +{ + public class TestClass + { + public static void Main(string [] args) + { + var uri = new System.Uri(""http://github.com/dotnet/corefx""); + var currentProcess = System.Diagnostics.Process.GetCurrentProcess(); + } + } +}"; + testProject.SourceFiles.Add("TestClass.cs", sourceFile); + + var testAsset = _testAssetsManager.CreateTestProject(testProject) + .Restore("DefaultReferences"); + + var buildCommand = new BuildCommand(Stage0MSBuild, Path.Combine(testAsset.TestRoot, "DefaultReferences")); + + buildCommand + .CaptureStdOut() + .Execute() + .Should() + .Pass() + .And + .NotHaveStdOutMatching("Could not resolve this reference", System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.IgnoreCase); + + } + [Fact] public void It_generates_binding_redirects_if_needed() { diff --git a/test/Microsoft.NET.Build.Tests/GivenThatWeWantToReferenceAProject.cs b/test/Microsoft.NET.Build.Tests/GivenThatWeWantToReferenceAProject.cs index ccb691989706..e549ed02a114 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThatWeWantToReferenceAProject.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThatWeWantToReferenceAProject.cs @@ -119,6 +119,12 @@ public void It_checks_for_valid_references(string referencerTarget, bool referen buildCommand = buildCommand.CaptureStdOut(); } + // Suppress ResolveAssemblyReference warning output due to https://github.com/Microsoft/msbuild/issues/1329 + if (buildSucceeds && referencerProject.IsExe && referencerProject.ShortTargetFrameworkIdentifiers.Contains("net")) + { + buildCommand = buildCommand.CaptureStdOut(); + } + var result = buildCommand.Execute(); if (buildSucceeds) diff --git a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPreserveCompilationContext.cs b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPreserveCompilationContext.cs index d5cb7861146c..9c14dc2ac5f2 100644 --- a/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPreserveCompilationContext.cs +++ b/test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPreserveCompilationContext.cs @@ -12,6 +12,7 @@ using Microsoft.NET.TestFramework.Commands; using Xunit; using static Microsoft.NET.TestFramework.Commands.MSBuildTest; +using System.Xml.Linq; namespace Microsoft.NET.Publish.Tests { @@ -24,7 +25,17 @@ public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file() { var testAsset = _testAssetsManager .CopyTestAsset("CompilationContext", "PreserveCompilationContext") - .WithSource(); + .WithSource() + .WithProjectChanges(project => + { + // Workaround for https://github.com/dotnet/sdk/issues/367 + + var ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); + var propertyGroup = project.Root.Elements(ns + "PropertyGroup").FirstOrDefault(); + propertyGroup.Should().NotBeNull(); + + propertyGroup.Add(new XElement(ns + "DisableImplicitFrameworkReferences", "true")); + }); testAsset.Restore("TestApp"); testAsset.Restore("TestLibrary"); diff --git a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs index d67b3018cc2f..0c0d9d845ac8 100644 --- a/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs +++ b/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs @@ -74,6 +74,13 @@ public AndConstraint HaveStdOutMatching(string pattern, return new AndConstraint(this); } + public AndConstraint NotHaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None) + { + Execute.Assertion.ForCondition(!Regex.Match(_commandResult.StdOut, pattern, options).Success) + .FailWith(AppendDiagnosticsTo($"The command output matched a pattern it should not have. Pattern: {pattern}{Environment.NewLine}")); + return new AndConstraint(this); + } + public AndConstraint HaveStdErr() { Execute.Assertion.ForCondition(!string.IsNullOrEmpty(_commandResult.StdErr)) diff --git a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs index 7b740d6bcaae..4bbbeca454e8 100644 --- a/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs +++ b/test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs @@ -23,6 +23,8 @@ public class TestProject public List ReferencedProjects { get; } = new List(); + public Dictionary SourceFiles { get; } = new Dictionary(); + private static string GetShortTargetFrameworkIdentifier(string targetFramework) { int identifierLength = 0; @@ -212,12 +214,14 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder) projectXml.Save(file); } - string source; - - if (this.IsExe) + if (SourceFiles.Count == 0) { - source = -@"using System; + string source; + + if (this.IsExe) + { + source = + @"using System; class Program { @@ -226,19 +230,19 @@ static void Main(string[] args) Console.WriteLine(""Hello World!""); "; - foreach (var dependency in this.ReferencedProjects) - { - source += $" Console.WriteLine({dependency.Name}.{dependency.Name}Class.Name);" + Environment.NewLine; - } + foreach (var dependency in this.ReferencedProjects) + { + source += $" Console.WriteLine({dependency.Name}.{dependency.Name}Class.Name);" + Environment.NewLine; + } - source += -@" } + source += + @" } }"; - } - else - { - source = -$@"using System; + } + else + { + source = + $@"using System; namespace {this.Name} {{ @@ -247,10 +251,18 @@ public class {this.Name}Class public static string Name {{ get {{ return ""{this.Name}""; }} }} }} }}"; - } - string sourcePath = Path.Combine(targetFolder, this.Name + ".cs"); + } + string sourcePath = Path.Combine(targetFolder, this.Name + ".cs"); - File.WriteAllText(sourcePath, source); + File.WriteAllText(sourcePath, source); + } + else + { + foreach (var kvp in SourceFiles) + { + File.WriteAllText(Path.Combine(targetFolder, kvp.Key), kvp.Value); + } + } } public override string ToString()