Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding more tests and an option to disable the implicit package targe…
…t fallback.
  • Loading branch information
Livar Cunha committed Apr 19, 2017
commit 27d0dc735dae8778bc5c74233d65f05ca45dddaf
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Copyright (c) .NET Foundation. All rights reserved.

</ItemGroup>

<PropertyGroup Condition="'$(_IsNETCoreOrNETStandard)' == 'true' and '$(_TargetFrameworkVersionWithoutV)' >= '2.0'">
<PropertyGroup Condition="'$(DisableImplicitPackageTargetFallback)' != 'true' and '$(_IsNETCoreOrNETStandard)' == 'true' and '$(_TargetFrameworkVersionWithoutV)' >= '2.0'">
<PackageTargetFallback>$(PackageTargetFallback);net461</PackageTargetFallback>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,92 @@

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantNet461ToBeAnImplicitPackageTargetFallback : SdkTest
public class GivenThatWeWantNet461ToBeAnImplicitPackageTargetFallback : SdkTest, IClassFixture<DeleteNuGetArtifactsFixture>
{
[Fact]
public void Net461_is_implicit_for_Netstandard20()
private TestPackageReference _net461PackageReference;

[WindowsOnlyTheoryAttribute]
[InlineData("netstandard2.0")]
[InlineData("netcoreapp2.0")]
public void Net461_is_implicit_for_Netstandard_and_Netcore_20(string targetFramework)
{
var testProjectName = targetFramework.Replace(".", "_");

var testProjectTestAsset = CreateTestAsset(testProjectName, targetFramework);

var restoreCommand = testProjectTestAsset.GetRestoreCommand(relativePath: testProjectName);
restoreCommand.AddSource(Path.GetDirectoryName(_net461PackageReference.NupkgPath));
restoreCommand.Execute().Should().Pass();

var buildCommand = new BuildCommand(
Stage0MSBuild,
Path.Combine(testProjectTestAsset.TestRoot, testProjectName));
buildCommand.Execute().Should().Pass();
}

[WindowsOnlyTheoryAttribute]
[InlineData("netstandard1.6")]
[InlineData("netcoreapp1.1")]
public void Net461_is_not_implicit_for_Netstandard_and_Netcore_less_than_20(string targetFramework)
{
var testProjectName = targetFramework.Replace(".", "_");

var testProjectTestAsset = CreateTestAsset(testProjectName, targetFramework);

var restoreCommand = testProjectTestAsset.GetRestoreCommand(relativePath: testProjectName);
restoreCommand.AddSource(Path.GetDirectoryName(_net461PackageReference.NupkgPath));
restoreCommand.Execute().Should().Fail();
}

[WindowsOnlyFact]
public void It_is_possible_to_disabled_net461_implicit_package_target_fallback()
{
const string testProjectName = "netstandard20";

var testProjectTestAsset = CreateTestAsset(
testProjectName,
"netstandard2.0",
new Dictionary<string, string> { {"DisableImplicitPackageTargetFallback", "true" } });

var restoreCommand = testProjectTestAsset.GetRestoreCommand(relativePath: testProjectName);
restoreCommand.AddSource(Path.GetDirectoryName(_net461PackageReference.NupkgPath));
restoreCommand.Execute().Should().Fail();
}

private TestAsset CreateTestAsset(
string testProjectName,
string targetFramework,
Dictionary<string, string> additionalProperties = null)
{
var netstandard20Project =
_net461PackageReference = CreateNet461Package();

var testProject =
new TestProject
{
Name = "netstandard20",
TargetFrameworks = "netstandard2.0",
Name = testProjectName,
TargetFrameworks = targetFramework,
IsSdkProject = true
};

if (additionalProperties != null)
{
foreach (var additionalProperty in additionalProperties)
{
testProject.AdditionalProperties.Add(additionalProperty.Key, additionalProperty.Value);
}
}

testProject.PackageReferences.Add(_net461PackageReference);

var testProjectTestAsset = _testAssetsManager.CreateTestProject(
testProject,
ConstantStringValues.TestDirectoriesNamePrefix, $"_{testProjectName}_net461");

return testProjectTestAsset;
}

private TestPackageReference CreateNet461Package()
{
var net461Project =
new TestProject
{
Expand All @@ -44,34 +117,17 @@ public void Net461_is_implicit_for_Netstandard20()
"1.0.0",
ConstantStringValues.ConstructNuGetPackageReferencePath(net461Project));

netstandard20Project.PackageReferences.Add(net461PackageReference);

var net461PackageTestAsset = _testAssetsManager.CreateTestProject(
net461Project,
ConstantStringValues.TestDirectoriesNamePrefix,
ConstantStringValues.NuGetSharedDirectoryNamePostfix);
var packageRestoreCommand =
net461PackageTestAsset.GetRestoreCommand(relativePath: net461Project.Name).Execute().Should().Pass();
var dependencyProjectDirectory = Path.Combine(net461PackageTestAsset.TestRoot, net461Project.Name);
var packagePackCommand = new PackCommand(Stage0MSBuild, dependencyProjectDirectory).Execute().Should().Pass();
var packagePackCommand =
new PackCommand(Stage0MSBuild, dependencyProjectDirectory).Execute().Should().Pass();

var netstandard20TestAsset = _testAssetsManager.CreateTestProject(
netstandard20Project,
ConstantStringValues.TestDirectoriesNamePrefix, "_netstandard20_net461");
var restoreCommand = netstandard20TestAsset.GetRestoreCommand(relativePath: netstandard20Project.Name);

foreach (TestPackageReference packageReference in netstandard20Project.PackageReferences)
{
restoreCommand.AddSource(Path.GetDirectoryName(packageReference.NupkgPath));
}

restoreCommand.Execute().Should().Pass();

var buildCommand = new BuildCommand(
Stage0MSBuild,
Path.Combine(netstandard20TestAsset.TestRoot, netstandard20Project.Name));

buildCommand.Execute().Should().Pass();
return net461PackageReference;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<PackageReference Include="System.Reflection.Metadata">
<Version>1.3.0</Version>
</PackageReference>
<PackageReference Include="xunit">
<Version>$(xunitVersion)</Version>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class TestProject

public Dictionary<string, string> SourceFiles { get; } = new Dictionary<string, string>();

public Dictionary<string, string> AdditionalProperties { get; } = new Dictionary<string, string>();

private static string GetShortTargetFrameworkIdentifier(string targetFramework)
{
int identifierLength = 0;
Expand Down Expand Up @@ -168,6 +170,11 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
targetFrameworkVersionElement.SetValue(this.TargetFrameworkVersion);
}

foreach (var additionalProperty in AdditionalProperties)
{
propertyGroup.Add(new XElement($"{ns}{additionalProperty.Key}", additionalProperty.Value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ns + additionalProperty.Key like the others above.

You want https://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.op_addition(v=vs.110).aspx, not string concatenation. I suspect this is only working now because the projects under test have no namespace declared.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

if (this.IsExe)
{
propertyGroup.Element(ns + "OutputType").SetValue("Exe");
Expand Down
19 changes: 19 additions & 0 deletions test/Microsoft.NET.TestFramework/WindowsOnlyFactAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 Microsoft.DotNet.PlatformAbstractions;
using Xunit;

namespace Microsoft.NET.TestFramework
{
public class WindowsOnlyFactAttribute : FactAttribute
{
public WindowsOnlyFactAttribute()
{
if (RuntimeEnvironment.OperatingSystemPlatform != Platform.Windows)
{
this.Skip = "This test requires Windows to run";
}
}
}
}
19 changes: 19 additions & 0 deletions test/Microsoft.NET.TestFramework/WindowsOnlyTheoryAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 Microsoft.DotNet.PlatformAbstractions;
using Xunit;

namespace Microsoft.NET.TestFramework
{
public class WindowsOnlyTheoryAttribute : TheoryAttribute
{
public WindowsOnlyTheoryAttribute()
{
if (RuntimeEnvironment.OperatingSystemPlatform != Platform.Windows)
{
this.Skip = "This test requires Windows to run";
}
}
}
}