Ejemplo n.º 1
0
        public void ItCanRunAnAppUsingTheWebSdk()
        {
            var    directory        = TestAssets.CreateTestDirectory();
            string projectDirectory = directory.FullName;

            string newArgs = "console --debug:ephemeral-hive --no-restore";

            new NewCommandShim()
            .WithWorkingDirectory(projectDirectory)
            .Execute(newArgs)
            .Should().Pass();

            string projectPath = Path.Combine(projectDirectory, directory.Name + ".csproj");

            var project = XDocument.Load(projectPath);
            var ns      = project.Root.Name.Namespace;

            project.Root.Attribute("Sdk").Value = "Microsoft.NET.Sdk.Web";

            project.Save(projectPath);

            new BuildCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            var runCommand = new RunCommand()
                             .WithWorkingDirectory(projectDirectory);

            runCommand.ExecuteWithCapturedOutput()
            // Templates are still at 3.0 and will not run on 5.0, revert to commented out assertion when 5.0 templates land
            //.Should().Pass().And.HaveStdOutContaining("Hello World!");
            .Should().Fail().And.HaveStdErrContaining("https://aka.ms/dotnet-download");
        }
Ejemplo n.º 2
0
        public void ItCanNewRestoreBuildRunCleanMSBuildProject()
        {
            var    directory        = TestAssets.CreateTestDirectory();
            string projectDirectory = directory.FullName;

            string newArgs   = "console --debug:ephemeral-hive --no-restore";
            var    newResult = new NewCommandShim()
                               .WithWorkingDirectory(projectDirectory)
                               .ExecuteWithCapturedOutput(newArgs);

            newResult.Should().Pass();

            string projectPath = Directory.GetFiles(projectDirectory, "*.csproj").Single();

            //  Override TargetFramework since there aren't .NET Core 3 templates yet
            //  https://github.com/dotnet/core-sdk/issues/24 tracks removing this workaround
            XDocument project = XDocument.Load(projectPath);
            var       ns      = project.Root.Name.Namespace;

            project.Root.Element(ns + "PropertyGroup")
            .Element(ns + "TargetFramework")
            .Value = "netcoreapp3.0";
            project.Save(projectPath);


            new RestoreCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute("/p:SkipInvalidConfigurations=true")
            .Should().Pass();

            new BuildCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            var runCommand = new RunCommand()
                             .WithWorkingDirectory(projectDirectory);

            //  Set DOTNET_ROOT as workaround for https://github.com/dotnet/cli/issues/10196
            runCommand = runCommand.WithEnvironmentVariable(Environment.Is64BitProcess ? "DOTNET_ROOT": "DOTNET_ROOT(x86)",
                                                            Path.GetDirectoryName(DotnetUnderTest.FullName));

            runCommand.ExecuteWithCapturedOutput()
            .Should().Pass()
            .And.HaveStdOutContaining("Hello World!");

            var binDirectory = new DirectoryInfo(projectDirectory).Sub("bin");

            binDirectory.Should().HaveFilesMatching("*.dll", SearchOption.AllDirectories);

            new CleanCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            binDirectory.Should().NotHaveFilesMatching("*.dll", SearchOption.AllDirectories);
        }
Ejemplo n.º 3
0
        public void RunTestFullClr()
        {
            var testProjectPath = Path.Combine(RepoRoot, "TestAssets", "TestProjects", "DependencyContextValidator", "TestAppFullClr");
            var testProject     = Path.Combine(testProjectPath, "project.json");

            var runCommand = new RunCommand(testProject);
            var result     = runCommand.ExecuteWithCapturedOutput();

            result.Should().Pass();
            ValidateRuntimeLibrariesFullClr(result, "TestAppFullClr");
            ValidateCompilationLibrariesFullClr(result, "TestAppFullClr");
        }
Ejemplo n.º 4
0
        public void ItCanNewRestoreBuildRunCleanMSBuildProject()
        {
            var    directory        = TestAssets.CreateTestDirectory();
            string projectDirectory = directory.FullName;

            string newArgs = "console --debug:ephemeral-hive --no-restore";

            new NewCommandShim()
            .WithWorkingDirectory(projectDirectory)
            .Execute(newArgs)
            .Should().Pass();

            new RestoreCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            new BuildCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            var runCommand = new RunCommand()
                             .WithWorkingDirectory(projectDirectory);

            //  Set DOTNET_ROOT as workaround for https://github.com/dotnet/cli/issues/10196
            var dotnetRoot = Path.GetDirectoryName(RepoDirectoriesProvider.DotnetUnderTest);

            if (!string.IsNullOrEmpty(dotnetRoot))
            {
                bool useX86 = RepoDirectoriesProvider.DotnetRidUnderTest.EndsWith("x86", StringComparison.InvariantCultureIgnoreCase);
                runCommand = runCommand.WithEnvironmentVariable(useX86 ? "DOTNET_ROOT(x86)" : "DOTNET_ROOT",
                                                                dotnetRoot);
            }

            runCommand.ExecuteWithCapturedOutput()
            .Should().Pass()
            .And.HaveStdOutContaining("Hello World!");

            var binDirectory = new DirectoryInfo(projectDirectory).Sub("bin");

            binDirectory.Should().HaveFilesMatching("*.dll", SearchOption.AllDirectories);

            new CleanCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            binDirectory.Should().NotHaveFilesMatching("*.dll", SearchOption.AllDirectories);
        }
Ejemplo n.º 5
0
        public void ItRunsWithDotnetWithoutApphost()
        {
            var testInstance = TestAssets.Get("AppOutputsExecutablePath").CreateInstance().WithSourceFiles();

            var command = new RunCommand()
                          .WithWorkingDirectory(testInstance.Root.FullName);

            command.Environment["UseAppHost"] = "false";

            command.ExecuteWithCapturedOutput()
            .Should()
            .Pass()
            .And
            .HaveStdOutContaining($"dotnet{Constants.ExeSuffix}");
        }
Ejemplo n.º 6
0
        public void RunTest(string appname, bool checkCompilation)
        {
            var testProjectPath = Path.Combine(RepoRoot, "TestAssets", "TestProjects", "DependencyContextValidator", appname);
            var testProject     = Path.Combine(testProjectPath, "project.json");

            var runCommand = new RunCommand(testProject);
            var result     = runCommand.ExecuteWithCapturedOutput();

            result.Should().Pass();
            ValidateRuntimeLibraries(result, appname);
            if (checkCompilation)
            {
                ValidateCompilationLibraries(result, appname);
            }
        }
Ejemplo n.º 7
0
        public void ItCanNewRestoreBuildRunCleanMSBuildProject()
        {
            using (DisposableDirectory directory = Temp.CreateDirectory())
            {
                string projectDirectory = directory.Path;

                string newArgs = "console --debug:ephemeral-hive --no-restore";
                new NewCommandShim()
                .WithWorkingDirectory(projectDirectory)
                .Execute(newArgs)
                .Should().Pass();

                new RestoreCommand()
                .WithWorkingDirectory(projectDirectory)
                .Execute("/p:SkipInvalidConfigurations=true")
                .Should().Pass();

                new BuildCommand()
                .WithWorkingDirectory(projectDirectory)
                .Execute()
                .Should().Pass();

                var runCommand = new RunCommand()
                                 .WithWorkingDirectory(projectDirectory);

                //  Set DOTNET_ROOT as workaround for https://github.com/dotnet/cli/issues/10196
                runCommand = runCommand.WithEnvironmentVariable(Environment.Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)",
                                                                Path.GetDirectoryName(DotnetUnderTest.FullName));

                runCommand.ExecuteWithCapturedOutput()
                .Should().Pass()
                .And.HaveStdOutContaining("Hello World!");

                var binDirectory = new DirectoryInfo(projectDirectory).Sub("bin");
                binDirectory.Should().HaveFilesMatching("*.dll", SearchOption.AllDirectories);

                new CleanCommand()
                .WithWorkingDirectory(projectDirectory)
                .Execute()
                .Should().Pass();

                binDirectory.Should().NotHaveFilesMatching("*.dll", SearchOption.AllDirectories);
            }
        }
Ejemplo n.º 8
0
        public void ItCanRunAnAppUsingTheWebSdk()
        {
            var    directory        = TestAssets.CreateTestDirectory();
            string projectDirectory = directory.FullName;

            string newArgs = "console --debug:ephemeral-hive --no-restore";

            new NewCommandShim()
            .WithWorkingDirectory(projectDirectory)
            .Execute(newArgs)
            .Should().Pass();

            string projectPath = Path.Combine(projectDirectory, directory.Name + ".csproj");

            var project = XDocument.Load(projectPath);
            var ns      = project.Root.Name.Namespace;

            project.Root.Attribute("Sdk").Value = "Microsoft.NET.Sdk.Web";

            project.Save(projectPath);

            new BuildCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            var runCommand = new RunCommand()
                             .WithWorkingDirectory(projectDirectory);

            //  Set DOTNET_ROOT as workaround for https://github.com/dotnet/cli/issues/10196
            var dotnetRoot = Path.GetDirectoryName(RepoDirectoriesProvider.DotnetUnderTest);

            if (!string.IsNullOrEmpty(dotnetRoot))
            {
                bool useX86 = RepoDirectoriesProvider.DotnetRidUnderTest.EndsWith("x86", StringComparison.InvariantCultureIgnoreCase);
                runCommand = runCommand.WithEnvironmentVariable(useX86 ? "DOTNET_ROOT(x86)" : "DOTNET_ROOT",
                                                                dotnetRoot);
            }

            runCommand.ExecuteWithCapturedOutput()
            .Should().Pass()
            .And.HaveStdOutContaining("Hello World!");
        }
Ejemplo n.º 9
0
        public void ItTerminatesTheChildWhenKilled()
        {
            var asset = TestAssets.Get("TestAppThatWaits")
                        .CreateInstance()
                        .WithSourceFiles();

            var command = new RunCommand()
                          .WithWorkingDirectory(asset.Root.FullName);

            bool    killed = false;
            Process child  = null;

            command.OutputDataReceived += (s, e) =>
            {
                if (killed)
                {
                    return;
                }

                child = Process.GetProcessById(Convert.ToInt32(e.Data));
                command.CurrentProcess.Kill();

                killed = true;
            };

            // A timeout is required to prevent the `Process.WaitForExit` call to hang if `dotnet run` failed to terminate the child on Windows.
            // This is because `Process.WaitForExit()` hangs waiting for the process launched by `dotnet run` to close the redirected I/O pipes (which won't happen).
            command.TimeoutMiliseconds = WaitTimeout;

            command
            .ExecuteWithCapturedOutput()
            .Should()
            .ExitWith(-1);

            killed.Should().BeTrue();

            if (!child.WaitForExit(WaitTimeout))
            {
                child.Kill();
                throw new XunitException("child process failed to terminate.");
            }
        }
Ejemplo n.º 10
0
        public void ItCanNewRestoreBuildRunCleanMSBuildProject()
        {
            var    directory        = TestAssets.CreateTestDirectory();
            string projectDirectory = directory.FullName;

            string newArgs = "console --debug:ephemeral-hive --no-restore";

            new NewCommandShim()
            .WithWorkingDirectory(projectDirectory)
            .Execute(newArgs)
            .Should().Pass();

            new RestoreCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            new BuildCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            var runCommand = new RunCommand()
                             .WithWorkingDirectory(projectDirectory);

            runCommand.ExecuteWithCapturedOutput()
            // Templates are still at 3.0 and will not run on 5.0, revert to commented out assertion when 5.0 templates land
            //.Should().Pass().And.HaveStdOutContaining("Hello World!");
            .Should().Fail().And.HaveStdErrContaining("https://aka.ms/dotnet-download");

            var binDirectory = new DirectoryInfo(projectDirectory).Sub("bin");

            binDirectory.Should().HaveFilesMatching("*.dll", SearchOption.AllDirectories);

            new CleanCommand()
            .WithWorkingDirectory(projectDirectory)
            .Execute()
            .Should().Pass();

            binDirectory.Should().NotHaveFilesMatching("*.dll", SearchOption.AllDirectories);
        }
Ejemplo n.º 11
0
        public void ItPassesSIGTERMToChild()
        {
            var asset = TestAssets.Get("TestAppThatWaits")
                        .CreateInstance()
                        .WithSourceFiles();

            var command = new RunCommand()
                          .WithWorkingDirectory(asset.Root.FullName);

            bool    killed = false;
            Process child  = null;

            command.OutputDataReceived += (s, e) =>
            {
                if (killed)
                {
                    return;
                }

                child = Process.GetProcessById(Convert.ToInt32(e.Data));
                NativeMethods.Posix.kill(command.CurrentProcess.Id, NativeMethods.Posix.SIGTERM).Should().Be(0);

                killed = true;
            };

            command
            .ExecuteWithCapturedOutput()
            .Should()
            .ExitWith(43)
            .And
            .HaveStdOutContaining("Terminating!");

            killed.Should().BeTrue();

            if (!child.WaitForExit(WaitTimeout))
            {
                child.Kill();
                throw new XunitException("child process failed to terminate.");
            }
        }
Ejemplo n.º 12
0
        public void ItIgnoresSIGINT()
        {
            var asset = TestAssets.Get("TestAppThatWaits")
                        .CreateInstance()
                        .WithSourceFiles();

            var command = new RunCommand()
                          .WithWorkingDirectory(asset.Root.FullName);

            bool killed = false;

            command.OutputDataReceived += (s, e) =>
            {
                if (killed)
                {
                    return;
                }

                // Simulate a SIGINT sent to a process group (i.e. both `dotnet run` and `TestAppThatWaits`).
                // Ideally we would send SIGINT to an actual process group, but the new child process (i.e. `dotnet run`)
                // will inherit the current process group from the `dotnet test` process that is running this test.
                // We would need to fork(), setpgid(), and then execve() to break out of the current group and that is
                // too complex for a simple unit test.
                NativeMethods.Posix.kill(command.CurrentProcess.Id, NativeMethods.Posix.SIGINT).Should().Be(0); // dotnet run
                NativeMethods.Posix.kill(Convert.ToInt32(e.Data), NativeMethods.Posix.SIGINT).Should().Be(0);   // TestAppThatWaits

                killed = true;
            };

            command
            .ExecuteWithCapturedOutput()
            .Should()
            .ExitWith(42)
            .And
            .HaveStdOutContaining("Interrupted!");

            killed.Should().BeTrue();
        }