added unit test. made if condition easier to understand.

This commit is contained in:
discostu105 2016-02-23 15:53:51 +01:00
parent f0c8428925
commit d0d3a629a5
4 changed files with 83 additions and 3 deletions

View file

@ -0,0 +1,12 @@
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
}

View file

@ -0,0 +1,20 @@
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": false
},
"dependencies": { },
"frameworks": {
"net20": { },
"net35": { },
"net40": { },
"net461": { },
"dnxcore50": {
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23811"
}
}
}
}

View file

@ -71,15 +71,33 @@ namespace Microsoft.DotNet.Cli.Compiler.Common
[typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture),
[typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage)
};
NuGetFramework targetFramework = string.IsNullOrEmpty(metadata.TargetFramework) ? null : NuGetFramework.Parse(metadata.TargetFramework);
if (targetFramework != null && !(targetFramework.IsDesktop() && targetFramework.Version < new Version(4, 0)))
if (IsAllowV4Attributes(metadata))
{
attributes[typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework); // TargetFrameworkAttribute only exists since .NET 4.0
};
return attributes;
}
private static bool IsAllowV4Attributes(AssemblyInfoOptions metadata)
{
if (string.IsNullOrEmpty(metadata.TargetFramework))
{
// target framework is unknown. to be on the safe side, return false.
return false;
}
NuGetFramework targetFramework = NuGetFramework.Parse(metadata.TargetFramework);
if (!targetFramework.IsDesktop())
{
// assuming .NET Core, which should support .NET 4.0 attributes
return true;
}
return targetFramework.Version >= new Version(4, 0);
}
private static bool IsSameAttribute(Type attributeType, AttributeSyntax attributeSyntax)
{
var name = attributeSyntax.Name.ToString();

View file

@ -7,6 +7,7 @@ using FluentAssertions;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using Xunit;
namespace Microsoft.DotNet.Tools.Builder.Tests
@ -128,6 +129,35 @@ namespace Microsoft.DotNet.Tools.Builder.Tests
informationalVersion.Should().BeEquivalentTo("1.0.0-85");
}
[Theory]
[InlineData("net20", false)]
[InlineData("net40", true)]
[InlineData("net461", true)]
[InlineData("dnxcore50", true)]
public void MultipleFrameworks_ShouldHaveValidTargetFrameworkAttribute(string frameworkName, bool shouldHaveTargetFrameworkAttribute)
{
var framework = NuGetFramework.Parse(frameworkName);
var testInstance = TestAssetsManager.CreateTestInstance("TestLibraryWithMultipleFrameworks")
.WithLockFiles();
var cmd = new BuildCommand(Path.Combine(testInstance.TestRoot, Project.FileName), framework: framework.GetShortFolderName());
cmd.ExecuteWithCapturedOutput().Should().Pass();
var output = Path.Combine(testInstance.TestRoot, "bin", "Debug", framework.GetShortFolderName(), "TestLibraryWithMultipleFrameworks.dll");
var targetFramework = PeReaderUtils.GetAssemblyAttributeValue(output, "TargetFrameworkAttribute");
if (shouldHaveTargetFrameworkAttribute)
{
targetFramework.Should().NotBeNull();
targetFramework.Should().BeEquivalentTo(framework.DotNetFrameworkName);
}
else
{
targetFramework.Should().BeNull();
}
}
[Fact]
public void ResourceTest()
{