diff --git a/test/EndToEnd/GivenWeWantToRequireWindowsForDesktopApps.cs b/test/EndToEnd/GivenWeWantToRequireWindowsForDesktopApps.cs new file mode 100644 index 000000000..f6d55f644 --- /dev/null +++ b/test/EndToEnd/GivenWeWantToRequireWindowsForDesktopApps.cs @@ -0,0 +1,62 @@ +// 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.IO; +using EndToEnd; +using FluentAssertions; +using Microsoft.DotNet.Tools.Test.Utilities; +using Xunit; + +namespace Microsoft.DotNet.Tests.EndToEnd +{ + public class GivenWeWantToRequireWindowsForDesktopApps + { + [Fact] + public void It_does_not_download_desktop_targeting_packs_on_unix() + { + var testProjectCreator = new TestProjectCreator() + { + MinorVersion = "5.0" + }; + + testProjectCreator.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\packages"; + testProjectCreator.AdditionalProperties["OutputType"] = "exe"; + + var testInstance = testProjectCreator.Create(); + + new BuildCommand() + .WithWorkingDirectory(testInstance.Root.FullName) + .Execute() + .Should().Pass(); + + string packagesPath = Path.Combine(testInstance.Root.FullName, "packages"); + Directory.Exists(packagesPath).Should().BeFalse(packagesPath + " should not exist"); + } + + [PlatformSpecificFact(TestPlatforms.Linux | TestPlatforms.OSX | TestPlatforms.FreeBSD)] + public void It_does_not_download_desktop_runtime_packs_on_unix() + { + const string Rid = "win-x64"; + + var testProjectCreator = new TestProjectCreator() + { + MinorVersion = "3.1" + }; + + testProjectCreator.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\packages"; + testProjectCreator.AdditionalProperties["OutputType"] = "exe"; + testProjectCreator.AdditionalProperties["RuntimeIdentifier"] = Rid; + + var testInstance = testProjectCreator.Create(); + + new PublishCommand() + .WithWorkingDirectory(testInstance.Root.FullName) + .Execute() + .Should().Pass(); + + string packagesPath = Path.Combine(testInstance.Root.FullName, "packages", $"runtime.{Rid}.microsoft.windowsdesktop.app"); + Directory.Exists(packagesPath).Should().BeFalse(packagesPath + " should not exist"); + } + } +} diff --git a/test/EndToEnd/TestProjectCreator.cs b/test/EndToEnd/TestProjectCreator.cs index bbb8805eb..2455192ab 100644 --- a/test/EndToEnd/TestProjectCreator.cs +++ b/test/EndToEnd/TestProjectCreator.cs @@ -25,6 +25,8 @@ namespace EndToEnd public string RuntimeIdentifier { get; set; } + public Dictionary AdditionalProperties { get; } = new Dictionary(); + public TestProjectCreator([CallerMemberName] string testName = null, string identifier = "") { TestName = testName; @@ -54,7 +56,11 @@ namespace EndToEnd project.Root.Element(ns + "PropertyGroup") .Add(new XElement(ns + "RuntimeIdentifier", RuntimeIdentifier)); } - + + foreach (var additionalProperty in AdditionalProperties) + { + project.Root.Element(ns + "PropertyGroup").Add(new XElement(ns + additionalProperty.Key, additionalProperty.Value)); + } if (PackageName != NETCorePackageName) { diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/PlatformSpecificFact.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/PlatformSpecificFact.cs new file mode 100644 index 000000000..819cd0644 --- /dev/null +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/PlatformSpecificFact.cs @@ -0,0 +1,44 @@ +// 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.Runtime.InteropServices; +using Xunit; + +namespace Microsoft.DotNet.Tools.Test.Utilities +{ + public class PlatformSpecificFact : FactAttribute + { + public PlatformSpecificFact(TestPlatforms platforms) + { + if (ShouldSkip(platforms)) + { + this.Skip = "This test is not supported on this platform."; + } + } + + internal static bool ShouldSkip(TestPlatforms platforms) => + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !platforms.HasFlag(TestPlatforms.Windows)) + || (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !platforms.HasFlag(TestPlatforms.Linux)) + || (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && !platforms.HasFlag(TestPlatforms.OSX)) + || (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")) && !platforms.HasFlag(TestPlatforms.FreeBSD)); + } + + [Flags] + public enum TestPlatforms + { + Any = -1, + Windows = 1, + Linux = 2, + OSX = 4, + FreeBSD = 8, + NetBSD = 16, + illumos = 32, + Solaris = 64, + iOS = 128, + tvOS = 256, + Android = 512, + Browser = 1024, + AnyUnix = 2048 + } +} diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/PlatformSpecificTheory.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/PlatformSpecificTheory.cs new file mode 100644 index 000000000..a23581dff --- /dev/null +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/PlatformSpecificTheory.cs @@ -0,0 +1,18 @@ +// 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 Xunit; + +namespace Microsoft.DotNet.Tools.Test.Utilities +{ + public class PlatformSpecificTheory : TheoryAttribute + { + public PlatformSpecificTheory(TestPlatforms platforms) + { + if (PlatformSpecificFact.ShouldSkip(platforms)) + { + this.Skip = "This test is not supported on this platform."; + } + } + } +}