public void It_runs_single_file_apps(string targetFramework, bool selfContained, string bundleOption)
        {
            var testProject = new TestProject()
            {
                Name             = "SingleFileTest",
                TargetFrameworks = targetFramework,
                IsExe            = true,
            };

            testProject.AdditionalProperties.Add("SelfContained", $"{selfContained}");

            var testAsset = _testAssetsManager.CreateTestProject(
                testProject,
                identifier: targetFramework + "_" + selfContained + "_" + bundleOption);
            var publishCommand = new PublishCommand(testAsset);

            publishCommand.Execute(PublishSingleFile, RuntimeIdentifier, bundleOption)
            .Should()
            .Pass();

            var publishDir     = GetPublishDirectory(publishCommand, targetFramework).FullName;
            var singleFilePath = Path.Combine(publishDir, $"{testProject.Name}{Constants.ExeSuffix}");

            var command = new RunExeCommand(Log, singleFilePath);

            command.Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello World");
        }
        public void Incremental_add_single_file()
        {
            var testProject = new TestProject()
            {
                Name             = "SingleFileTest",
                TargetFrameworks = "net5.0",
                IsExe            = true,
            };

            testProject.AdditionalProperties.Add("SelfContained", $"{true}");

            var testAsset = _testAssetsManager.CreateTestProject(testProject);
            var cmd       = new PublishCommand(testAsset);

            var singleFilePath = Path.Combine(GetPublishDirectory(cmd).FullName, $"SingleFileTest{Constants.ExeSuffix}");

            cmd.Execute(RuntimeIdentifier).Should().Pass();
            var time1 = File.GetLastWriteTimeUtc(singleFilePath);

            WaitForUtcNowToAdvance();

            cmd.Execute(PublishSingleFile, RuntimeIdentifier).Should().Pass();
            var time2 = File.GetLastWriteTimeUtc(singleFilePath);

            time2.Should().BeAfter(time1);

            var exeCommand = new RunExeCommand(Log, singleFilePath);

            exeCommand.Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello World");
        }
Ejemplo n.º 3
0
        public void It_builds_and_runs()
        {
            var testAsset = _testAssetsManager
                            .CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
                            .WithSource();

            // build projects separately with BuildProjectReferences=false to simulate VS build behavior
            new BuildCommand(testAsset, "NETCoreCppCliTest")
            .Execute("-p:Platform=x64")
            .Should()
            .Pass();

            new BuildCommand(testAsset, "CSConsoleApp")
            .Execute(new string[] { "-p:Platform=x64", "-p:BuildProjectReferences=false" })
            .Should()
            .Pass();

            var exe = Path.Combine( //find the platform directory
                new DirectoryInfo(Path.Combine(testAsset.TestRoot, "CSConsoleApp", "bin")).GetDirectories().Single().FullName,
                "Debug",
                "net5.0",
                "CSConsoleApp.exe");

            var runCommand = new RunExeCommand(Log, exe);

            runCommand
            .Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello, World!");
        }
Ejemplo n.º 4
0
        public void When_referenced_by_csharp_project_it_publishes_and_runs()
        {
            var testAsset = _testAssetsManager
                            .CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
                            .WithSource();

            new PublishCommand(Log, Path.Combine(testAsset.TestRoot, "CSConsoleApp"))
            .Execute(new string[] { "-p:Platform=x64" })
            .Should()
            .Pass();

            var exe = Path.Combine( //find the platform directory
                new DirectoryInfo(Path.Combine(testAsset.TestRoot, "CSConsoleApp", "bin")).GetDirectories().Single().FullName,
                "Debug",
                "netcoreapp3.1",
                "publish",
                "CSConsoleApp.exe");

            var runCommand = new RunExeCommand(Log, exe);

            runCommand
            .Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello, World!");
        }
Ejemplo n.º 5
0
        public void DependentUponTest(string targetFramework, bool isExe)
        {
            var testProject = new TestProject
            {
                Name             = "HelloWorld",
                IsSdkProject     = true,
                TargetFrameworks = targetFramework,
                IsExe            = isExe,
                SourceFiles      =
                {
                    ["Program.cs"] = @"
                        using System;

                        namespace SomeNamespace
                        {
                            public static class SomeClass
                            {
                                public static void Main(string[] args)
                                {
                                     var resourceManager = new global::System.Resources.ResourceManager(""SomeNamespace.SomeClass"", typeof(SomeClass).Assembly);
                                     Console.WriteLine(resourceManager.GetString(""SomeString""));
                                }
                            }
                        }
                        ",
                },
                EmbeddedResources =
                {
                    ["Program.resx"] = @"
                        <root>                          
                            <data name=""SomeString"" xml:space=""preserve"">
                                <value>Hello world from a resource!</value>
                            </data>
                        </root>
                        ",
                }
            };

            var testAsset = _testAssetsManager
                            .CreateTestProject(testProject, identifier: targetFramework + isExe);

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

            buildCommand
            .Execute()
            .Should()
            .Pass();

            var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

            var runCommand = new RunExeCommand(Log, Path.Combine(outputDirectory.FullName, "HelloWorld.exe"));

            runCommand
            .Execute()
            .Should()
            .Pass()
            .And.HaveStdOutContaining("Hello world from a resource!");
        }
Ejemplo n.º 6
0
        public void It_can_use_implicitly_defined_compilation_constants(string targetFramework, string[] expectedOutput, string targetPlatformIdentifier = null, string targetPlatformVersion = null)
        {
            var testProj = new TestProject()
            {
                Name             = "CompilationConstants",
                TargetFrameworks = targetFramework,
                IsExe            = true,
                IsSdkProject     = true
            };

            if (targetPlatformIdentifier != null)
            {
                testProj.AdditionalProperties["TargetPlatformIdentifier"] = targetPlatformIdentifier;
                testProj.AdditionalProperties["TargetPlatformVersion"]    = targetPlatformVersion;
            }
            var testAsset = _testAssetsManager.CreateTestProject(testProj);

            File.WriteAllText(Path.Combine(testAsset.Path, testProj.Name, $"{testProj.Name}.cs"), @"
using System;
class Program
{
    static void Main(string[] args)
    {
        #if NETCOREAPP
            Console.WriteLine(""NETCOREAPP"");
        #endif
        #if NETCOREAPP2_1
            Console.WriteLine(""NETCOREAPP2_1"");
        #endif
        #if NETCOREAPP3_1
            Console.WriteLine(""NETCOREAPP3_1"");
        #endif
        #if NET
            Console.WriteLine(""NET"");
        #endif
        #if NET5_0
            Console.WriteLine(""NET5_0"");
        #endif
        #if WINDOWS
            Console.WriteLine(""WINDOWS"");
        #endif
        #if IOS
            Console.WriteLine(""IOS"");
        #endif
    }
}");

            var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name));

            buildCommand
            .Execute()
            .Should()
            .Pass();

            var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(targetFramework).FullName, $"{testProj.Name}.exe"));
            var stdOut     = runCommand.Execute().StdOut.Split(Environment.NewLine.ToCharArray()).Where(line => !string.IsNullOrWhiteSpace(line));

            stdOut.Should().BeEquivalentTo(expectedOutput);
        }
Ejemplo n.º 7
0
        public void User_can_get_bundle_info_before_bundling()
        {
            var testProject = new TestProject()
            {
                Name             = "SingleFileTest",
                TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
                IsExe            = true,
            };

            testProject.AdditionalProperties.Add("SelfContained", "true");

            var testAsset = _testAssetsManager.CreateTestProject(testProject)
                            .WithProjectChanges(project => VerifyPrepareForBundle(project));

            var publishCommand = new PublishCommand(testAsset);
            var singleFilePath = Path.Combine(GetPublishDirectory(publishCommand, ToolsetInfo.CurrentTargetFramework).FullName, $"SingleFileTest{Constants.ExeSuffix}");

            publishCommand
            .Execute(PublishSingleFile, RuntimeIdentifier)
            .Should()
            .Pass();

            var command = new RunExeCommand(Log, singleFilePath);

            command.Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello World");

            void VerifyPrepareForBundle(XDocument project)
            {
                var ns         = project.Root.Name.Namespace;
                var targetName = "CheckPrepareForBundleData";

                var target = new XElement(ns + "Target",
                                          new XAttribute("Name", targetName),
                                          new XAttribute("BeforeTargets", "GenerateSingleFileBundle"),
                                          new XAttribute("DependsOnTargets", "PrepareForBundle"));

                project.Root.Add(target);

                //     <Error Condition = "'@(FilesToBundle->AnyHaveMetadataValue('RelativePath', 'System.Private.CoreLib.dll'))' != 'true'" Text="System.Private.CoreLib.dll is not in FilesToBundle list">
                target.Add(
                    new XElement(ns + "Error",
                                 new XAttribute("Condition", "'@(FilesToBundle->AnyHaveMetadataValue('RelativePath', 'System.Private.CoreLib.dll'))' != 'true'"),
                                 new XAttribute("Text", "System.Private.CoreLib.dll is not in FilesToBundle list")));


                var host = $"SingleFileTest{Constants.ExeSuffix}";

                //     <Error Condition="'$(AppHostFile)' != 'SingleFileTest.exe'" Text="AppHostFile expected to be: 'SingleFileTest.exe' actually: '$(AppHostFile)'" />
                target.Add(
                    new XElement(ns + "Error",
                                 new XAttribute("Condition", $"'$(AppHostFile)' != '{host}'"),
                                 new XAttribute("Text", $"AppHostFile expected to be: '{host}' actually: '$(AppHostFile)'")));
            }
        }
Ejemplo n.º 8
0
        public void It_supports_composite_r2r(bool extractAll)
        {
            // the test fails once in a while on OSX, but dumps are not very informative,
            // so enabling this for non-OSX platforms, hoping to get a better crash dump
            // if you see this failing on Linux, please preserve the dump.
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                var projName = "SingleFileTest";
                if (extractAll)
                {
                    projName += "Extracted";
                }

                var testProject = new TestProject()
                {
                    Name             = projName,
                    TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
                    IsExe            = true,
                };

                var testAsset      = _testAssetsManager.CreateTestProject(testProject);
                var publishCommand = new PublishCommand(testAsset);
                var extraArgs      = new List <string>()
                {
                    PublishSingleFile, ReadyToRun, ReadyToRunComposite, RuntimeIdentifier
                };

                if (extractAll)
                {
                    extraArgs.Add(IncludeAllContent);
                }

                publishCommand
                .Execute(extraArgs.ToArray())
                .Should()
                .Pass();

                var publishDir     = GetPublishDirectory(publishCommand, targetFramework: ToolsetInfo.CurrentTargetFramework).FullName;
                var singleFilePath = Path.Combine(publishDir, $"{testProject.Name}{Constants.ExeSuffix}");

                var command = new RunExeCommand(Log, singleFilePath);
                command.Execute()
                .Should()
                .Pass()
                .And
                .HaveStdOutContaining("Hello World");
            }
        }
Ejemplo n.º 9
0
        public void COMReferenceBuildsAndRuns(bool embedInteropTypes)
        {
            var targetFramework = "netcoreapp3.0";

            var testProject = new TestProject
            {
                Name             = "UseComReferences",
                IsSdkProject     = true,
                TargetFrameworks = targetFramework,
                IsExe            = true,
                SourceFiles      =
                {
                    ["Program.cs"] = @"
                            class Program
                            {
                                static void Main(string[] args)
                                {
                                    System.Console.WriteLine(typeof(VSLangProj.VSProject));
                                }
                            }
                        ",
                }
            };

            var reference = new XElement("ItemGroup",
                                         new XElement("COMReference",
                                                      new XAttribute("Include", "VSLangProj.dll"),
                                                      new XElement("Guid", "49a1950e-3e35-4595-8cb9-920c64c44d67"),
                                                      new XElement("VersionMajor", "7"),
                                                      new XElement("VersionMinor", "0"),
                                                      new XElement("WrapperTool", "tlbimp"),
                                                      new XElement("Lcid", "0"),
                                                      new XElement("Isolated", "false"),
                                                      new XElement("EmbedInteropTypes", embedInteropTypes)));

            var testAsset = _testAssetsManager
                            .CreateTestProject(testProject, identifier: embedInteropTypes.ToString())
                            .WithProjectChanges(doc => doc.Root.Add(reference));

            var buildCommand = new BuildCommand(testAsset);

            buildCommand.Execute().Should().Pass();

            var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);
            var runCommand      = new RunExeCommand(Log, outputDirectory.File("UseComReferences.exe").FullName);

            runCommand.Execute().Should().Pass();
        }
Ejemplo n.º 10
0
        public void It_handles_native_dependencies_and_platform_target(
            string identifier,
            string platformTarget,
            bool useNativeCode,
            string expectedProgramOutput)
        {
            foreach (bool multiTarget in new[] { false, true })
            {
                var testAsset = _testAssetsManager
                                .CopyTestAsset("DesktopMinusRid", identifier: Path.DirectorySeparatorChar + identifier + (multiTarget ? "Multi" : ""))
                                .WithSource()
                                .WithProjectChanges(project =>
                {
                    var ns            = project.Root.Name.Namespace;
                    var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
                    propertyGroup.Add(new XElement(ns + "UseNativeCode", useNativeCode));

                    if (platformTarget != null)
                    {
                        propertyGroup.Add(new XElement(ns + "PlatformTarget", platformTarget));
                    }

                    if (multiTarget)
                    {
                        propertyGroup.Element(ns + "TargetFramework").Remove();
                        propertyGroup.Add(new XElement(ns + "TargetFrameworks", "net46;netcoreapp1.1"));
                    }
                })
                                .Restore(Log);

                var buildCommand = new BuildCommand(Log, testAsset.TestRoot);
                buildCommand
                .Execute()
                .Should()
                .Pass();

                var exe        = Path.Combine(buildCommand.GetOutputDirectory("net46").FullName, "DesktopMinusRid.exe");
                var runCommand = new RunExeCommand(Log, exe);
                runCommand
                .Execute()
                .Should()
                .Pass()
                .And
                .HaveStdOutContaining(expectedProgramOutput);
            }
        }
Ejemplo n.º 11
0
        public void It_supports_composite_r2r(bool extractAll)
        {
            var projName = "SingleFileTest";

            if (extractAll)
            {
                projName += "Extracted";
            }

            var testProject = new TestProject()
            {
                Name             = projName,
                TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
                IsExe            = true,
            };

            var testAsset      = _testAssetsManager.CreateTestProject(testProject);
            var publishCommand = new PublishCommand(testAsset);
            var extraArgs      = new List <string>()
            {
                PublishSingleFile, ReadyToRun, ReadyToRunComposite, RuntimeIdentifier
            };

            if (extractAll)
            {
                extraArgs.Add(IncludeAllContent);
            }

            publishCommand
            .Execute(extraArgs.ToArray())
            .Should()
            .Pass();

            var publishDir     = GetPublishDirectory(publishCommand, targetFramework: ToolsetInfo.CurrentTargetFramework).FullName;
            var singleFilePath = Path.Combine(publishDir, $"{testProject.Name}{Constants.ExeSuffix}");

            var command = new RunExeCommand(Log, singleFilePath);

            command.Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello World");
        }
        public void ItPublishesFrameworkDependentWithRid(string args)
        {
            var testAppName     = "MSBuildTestApp";
            var rid             = EnvironmentInfo.GetCompatibleRid();
            var outputDirectory = PublishApp(testAppName, rid, args);

            outputDirectory.Should().OnlyHaveFiles(new[] {
                $"{testAppName}{Constants.ExeSuffix}",
                $"{testAppName}.dll",
                $"{testAppName}.pdb",
                $"{testAppName}.deps.json",
                $"{testAppName}.runtimeconfig.json",
            });

            var outputProgram = Path.Combine(outputDirectory.FullName, $"{testAppName}{Constants.ExeSuffix}");

            var command = new RunExeCommand(Log, outputProgram);

            command.Execute()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining("Hello World");
        }
        private void AssertValidShim(string testRoot, string nugetPackage)
        {
            using (var nupkgReader = new PackageArchiveReader(nugetPackage))
            {
                IEnumerable <NuGetFramework> supportedFrameworks = nupkgReader.GetSupportedFrameworks();
                supportedFrameworks.Should().NotBeEmpty();
                var simulateToolPathRoot = Path.Combine(testRoot, "temp", Path.GetRandomFileName());

                foreach (NuGetFramework framework in supportedFrameworks)
                {
                    string[] portableAppContent =
                    {
                        "consoledemo.runtimeconfig.json",
                        "consoledemo.deps.json",
                        "consoledemo.dll",
                        "Newtonsoft.Json.dll"
                    };
                    CopyPackageAssetToToolLayout(portableAppContent, nupkgReader, simulateToolPathRoot, framework);

                    string shimPath = Path.Combine(simulateToolPathRoot, $"{_customToolCommandName}.exe");
                    nupkgReader.ExtractFile(
                        $"tools/{framework.GetShortFolderName()}/any/shims/win-x64/{_customToolCommandName}.exe",
                        shimPath,
                        null);

                    var command = new RunExeCommand(Log, shimPath)
                    {
                        WorkingDirectory = simulateToolPathRoot
                    };
                    command.Execute().Should()
                    .Pass()
                    .And
                    .HaveStdOutContaining("Hello World from Global Tool");
                }
            }
        }
Ejemplo n.º 14
0
        public void COMReferenceBuildsAndRuns(bool embedInteropTypes)
        {
            var targetFramework = "netcoreapp3.0";


            var testProject = new TestProject
            {
                Name             = "UseMediaPlayer",
                IsSdkProject     = true,
                TargetFrameworks = targetFramework,
                IsExe            = true,
                SourceFiles      =
                {
                    ["Program.cs"] = @"
                            using MediaPlayer;
                            class Program
                            {
                                static void Main(string[] args)
                                {
                                    var mediaPlayer = (IMediaPlayer2)new MediaPlayerClass();
                                }
                            }
                        ",
                }
            };

            if (embedInteropTypes)
            {
                testProject.SourceFiles.Add("MediaPlayerClass.cs", @"
                    using System.Runtime.InteropServices;
                    namespace MediaPlayer
                    {
                        [ComImport]
                        [Guid(""22D6F312-B0F6-11D0-94AB-0080C74C7E95"")]
                        class MediaPlayerClass { }
                    }
                ");
            }

            var reference = new XElement("ItemGroup",
                                         new XElement("COMReference",
                                                      new XAttribute("Include", "MediaPlayer.dll"),
                                                      new XElement("Guid", "22d6f304-b0f6-11d0-94ab-0080c74c7e95"),
                                                      new XElement("VersionMajor", "1"),
                                                      new XElement("VersionMinor", "0"),
                                                      new XElement("WrapperTool", "tlbimp"),
                                                      new XElement("Lcid", "0"),
                                                      new XElement("Isolated", "false"),
                                                      new XElement("EmbedInteropTypes", embedInteropTypes)));


            var testAsset = _testAssetsManager
                            .CreateTestProject(testProject, identifier: embedInteropTypes.ToString())
                            .WithProjectChanges(doc => doc.Root.Add(reference));

            var buildCommand = new BuildCommand(testAsset);

            buildCommand.Execute().Should().Pass();

            var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);
            var runCommand      = new RunExeCommand(Log, outputDirectory.File("UseMediaPlayer.exe").FullName);

            runCommand.Execute().Should().Pass();
        }
Ejemplo n.º 15
0
        public void It_appends_rid_to_outdir_correctly(string identifier, string rid, bool useAppendOption, bool shouldAppend)
        {
            foreach (bool multiTarget in new[] { false, true })
            {
                var testAsset = _testAssetsManager
                                .CopyTestAsset("DesktopMinusRid", identifier: Path.DirectorySeparatorChar + identifier + (multiTarget ? "Multi" : ""))
                                .WithSource()
                                .WithProjectChanges(project =>
                {
                    var ns            = project.Root.Name.Namespace;
                    var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
                    propertyGroup.Add(new XElement(ns + "RuntimeIdentifier", rid));
                    propertyGroup.Add(new XElement(ns + "AppendRuntimeIdentifierToOutputPath", useAppendOption.ToString()));

                    if (multiTarget)
                    {
                        propertyGroup.Element(ns + "RuntimeIdentifier").Add(new XAttribute("Condition", "'$(TargetFramework)' == 'net46'"));
                        propertyGroup.Element(ns + "TargetFramework").Remove();
                        propertyGroup.Add(new XElement(ns + "TargetFrameworks", "net46;netcoreapp1.1"));
                    }
                });

                var buildCommand = new BuildCommand(testAsset);
                buildCommand
                .Execute()
                .Should()
                .Pass();

                var publishCommand = new PublishCommand(testAsset);
                publishCommand
                .Execute(multiTarget ? new[] { "/p:TargetFramework=net46" } : Array.Empty <string>())
                .Should()
                .Pass();

                string expectedOutput;
                switch (rid)
                {
                case "":
                    expectedOutput = "Native code was not used (MSIL)";
                    break;

                case "win7-x86":
                    expectedOutput = "Native code was not used (X86)";
                    break;

                case "win7-x64":
                    expectedOutput = "Native code was not used (Amd64)";
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(rid));
                }

                var outputDirectory  = buildCommand.GetOutputDirectory("net46", runtimeIdentifier: shouldAppend ? rid : "");
                var publishDirectory = publishCommand.GetOutputDirectory("net46", runtimeIdentifier: rid);

                foreach (var directory in new[] { outputDirectory, publishDirectory })
                {
                    var exe = Path.Combine(directory.FullName, "DesktopMinusRid.exe");

                    var runCommand = new RunExeCommand(Log, exe);
                    runCommand
                    .Execute()
                    .Should()
                    .Pass()
                    .And
                    .HaveStdOutContaining(expectedOutput);
                }
            }
        }