Пример #1
0
        public async Task MvcNET(string templateFramework, string configuration, string msBuildType, string auth, string useLocalDB, string targetFramework)
        {
            string projectName = $"{nameof(MvcNET)}_{Path.GetRandomFileName()}";

            string additionalOptions = string.Empty;

            // Arrange
            if (bool.TryParse(useLocalDB, out bool localDBBool) && localDBBool)
            {
                additionalOptions = $"--use-local-db";
            }
            string dotNetNewArguments = $"new mvc --framework {templateFramework} --auth {auth} {DotNetNewAdditionalArgs} {additionalOptions}";
            string testFolder         = Path.Combine(BaseTestDirectory, projectName);

            // dotnet new
            int?exitCode = ProcessWrapper.RunProcess(DotNetExeName, dotNetNewArguments, testFolder, out int?processId1, createDirectoryIfNotExists: true);

            Assert.True(exitCode.HasValue && exitCode.Value == 0);

            // Change the target framework to NetFramework.
            ChangeTargetFrameworkInCsProject(Path.Combine(testFolder, $"{projectName}.csproj"), templateFramework, targetFramework);

            string resultText = await RestoreBuildPublishAndRun(testFolder, projectName, configuration, msBuildType, isStandAlone : true);

            Assert.Contains($"<title>Home Page -", resultText);
        }
Пример #2
0
        public async Task WebAPICore(string templateFramework, string configuration, string msBuildType)
        {
            string projectName = $"{nameof(WebAPICore)}_{Path.GetRandomFileName()}";

            // Arrange
            string dotNetNewArguments = $"new webapi --framework {templateFramework} {DotNetNewAdditionalArgs}";
            string testFolder         = Path.Combine(BaseTestDirectory, projectName);

            // dotnet new
            int?exitCode = ProcessWrapper.RunProcess(DotNetExeName, dotNetNewArguments, testFolder, out int?processId1, createDirectoryIfNotExists: true);

            Assert.True(exitCode.HasValue && exitCode.Value == 0);

            string resultText = await RestoreBuildPublishAndRun(testFolder, projectName, configuration, msBuildType, isStandAlone : false, resultUrl : "http://localhost:5000/api/Values");

            Assert.Equal(resultText, "[\"value1\",\"value2\"]");
        }
Пример #3
0
        public async Task EmptyWebCore(string templateFramework, string configuration, string msBuildType)
        {
            string projectName = $"{nameof(EmptyWebCore)}_{Path.GetRandomFileName()}";

            // Arrange
            string dotNetNewArguments = $"new web --framework {templateFramework} {DotNetNewAdditionalArgs}";
            string testFolder         = Path.Combine(BaseTestDirectory, projectName);

            // dotnet new
            int?exitCode = ProcessWrapper.RunProcess(DotNetExeName, dotNetNewArguments, testFolder, out int?processId1, createDirectoryIfNotExists: true);

            Assert.True(exitCode.HasValue && exitCode.Value == 0);

            string resultText = await RestoreBuildPublishAndRun(testFolder, projectName, configuration, msBuildType);

            Assert.Equal(resultText, "Hello World!");
        }
Пример #4
0
        private async Task <string> RestoreBuildPublishAndRun(string testFolder, string projectName, string configuration, string msBuildType, bool isStandAlone = false, string resultUrl = "http://localhost:5000")
        {
            // dotnet restore
            string dotnetRestoreArguments = "restore";
            int?   exitCode = ProcessWrapper.RunProcess(DotNetExeName, dotnetRestoreArguments, testFolder, out int?processId2);

            Assert.True(exitCode.HasValue && exitCode.Value == 0);

            // dotnet build
            string dotnetBuildArguments = "build";

            exitCode = ProcessWrapper.RunProcess(DotNetExeName, dotnetBuildArguments, testFolder, out int?processId3);
            Assert.True(exitCode.HasValue && exitCode.Value == 0);

            // msbuild publish
            string fileName               = "msbuild";
            string publishOutputFolder    = $"bin\\{configuration}\\PublishOutput";
            string dotnetPublishArguments = $"{projectName}.csproj /p:DeployOnBuild=true /p:Configuration={configuration} /p:PublishUrl={publishOutputFolder}";

            if (string.Equals(msBuildType, "core"))
            {
                dotnetPublishArguments = $"{fileName} {dotnetPublishArguments}";
                fileName = DotNetExeName;
            }
            exitCode = ProcessWrapper.RunProcess(fileName, dotnetPublishArguments, testFolder, out int?processId4);
            Assert.True(exitCode.HasValue && exitCode.Value == 0);

            int?   runningProcess = null;
            string publishOutputFolderFullPath = Path.Combine(testFolder, publishOutputFolder);
            string dotNetRunArguments          = $"{projectName}.dll";

            fileName = DotNetExeName;
            if (isStandAlone)
            {
                dotNetRunArguments = null;
                fileName           = Path.Combine(publishOutputFolderFullPath, $"{projectName}.exe");
            }
            exitCode = ProcessWrapper.RunProcess(fileName, dotNetRunArguments, publishOutputFolderFullPath, out runningProcess, waitForExit: false);

            // Wait for 2 seconds for the application to start
            await Task.Delay(TimeSpan.FromSeconds(2));

            CancellationTokenSource tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            HttpResponseMessage     result      = null;

            while (!tokenSource.IsCancellationRequested)
            {
                try
                {
                    using (HttpClient client = new HttpClient())
                    {
                        result = await client.GetAsync(resultUrl);

                        if (result.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            break;
                        }
                        await Task.Delay(100);
                    }
                }
                catch
                {
                }
            }

            if (runningProcess != null)
            {
                ProcessWrapper.KillProcessTree(runningProcess.Value);
            }
            try
            {
                Directory.Delete(testFolder, true);
            }
            catch { }

            Assert.True(result != null);
            string resultText = await result.Content.ReadAsStringAsync();

            return(resultText);
        }