Example #1
0
        private string SetupProjectDependencies()
        {
            var libProjPath  = Path.Combine(this_TestGroupOutputDir, "TypesLib", "TypesLib.csproj");
            var binProjPath  = Path.Combine(this_TestGroupOutputDir, "BinLib", "BinLib.csproj");
            var assemblyPath = Path.Combine(Path.GetDirectoryName(binProjPath), "bin", "Debug", "netstandard1.3", "BinLib.dll");

            if (!File.Exists(assemblyPath))
            {
                var typeReuseProjectsPath = Path.Combine(g_TestCasesDir, "TypeReuse");

                FileUtil.CopyDirectory(typeReuseProjectsPath, this_TestGroupOutputDir);
                CreateGlobalJson(this_TestGroupOutputDir, this_TestCaseProject.SdkVersion);

                MSBuildProj binProj = MSBuildProj.FromPathAsync(binProjPath, null, CancellationToken.None).Result;
                ProcessRunner.ProcessResult result = binProj.BuildAsync(true, null, CancellationToken.None).Result;
                Assert.True(result.ExitCode == 0, result.OutputText);
            }

            Assert.True(File.Exists(binProjPath), $"{nameof(binProjPath)} not initialized!");
            Assert.True(File.Exists(libProjPath), $"{nameof(libProjPath)} not initialized!");

            this_TestCaseProject.AddDependency(ProjectDependency.FromAssembly(assemblyPath));
            this_TestCaseProject.AddDependency(ProjectDependency.FromProject(libProjPath));
            this_TestCaseProject.SaveAsync(this_TestCaseLogger, CancellationToken.None).Wait();

            ProcessRunner.ProcessResult ret = this_TestCaseProject.BuildAsync(true, this_TestCaseLogger, CancellationToken.None).Result;
            Assert.True(ret.ExitCode == 0, ret.OutputText);

            // keep the boostrapper projects in the outputs to be evaluated against baselines.
            this_TestCaseBootstrapDir = this_TestCaseOutputDir;
            Directory.CreateDirectory(Path.Combine(this_TestCaseBootstrapDir, "SvcutilBootstrapper"));

            var uri = PathHelper.GetRelativePath(Path.Combine(this_TestGroupOutputDir, "TypeReuseSvc.wsdl"), new DirectoryInfo(this_TestCaseOutputDir));

            return(uri);
        }
Example #2
0
        public static ProcessRunner.ProcessResult RunSvcutil(this MSBuildProj project, string options, bool expectSuccess, ILogger logger, bool globalTool = false)
        {
            Assert.False(string.IsNullOrEmpty(options), $"{nameof(options)} not initialized!");
            Assert.True(File.Exists(project?.FullPath), $"{nameof(project)} is not initialized!");

            var envVars = new Dictionary <string, string> {
                { AppInsightsTelemetryClient.OptOutVariable, (!AppInsightsTelemetryClient.IsUserOptedIn).ToString() }
            };

            ProcessRunner.ProcessResult result;

            if (globalTool)
            {
                result = ProcessRunner.RunAsync("dotnet-svcutil", options, project.DirectoryPath, redirectOutput: true, throwOnError: false, environmentVariables: envVars, logger: logger, cancellationToken: CancellationToken.None).Result;
            }
            else
            {
                string csStr   = string.Empty;
                string srcPath = project.FullPath.Replace("csproj", "cs");
                using (var sr = new StreamReader(srcPath))
                {
                    csStr = sr.ReadToEnd();
                }

                using (var sw = new StreamWriter(srcPath))
                {
                    if (!csStr.Contains("using Microsoft.Tools.ServiceModel.Svcutil;"))
                    {
                        string indent = new string(' ', 12);
                        csStr = csStr.Replace("using System;", "using System;\r\nusing Microsoft.Tools.ServiceModel.Svcutil;\r\nusing System.Linq;\r\nusing System.Text.RegularExpressions;");
                        csStr = csStr.Replace("static void Main", "static int Main");
                        string replacement = "var re = new Regex(@\"'[^\\\"\"]*'|[^\\\"\"^\\s]+|\"\"[^\\\"\"]*\"\"\");\r\n" +
                                             indent + "string optstring = @\"" + options + "\";\r\n" +
                                             indent + "string[] opts = re.Matches(optstring).Cast<Match>().Select(m => m.Value).ToArray();\r\n" +
                                             indent + "return Tool.Main(opts);";
                        csStr = csStr.Replace("Console.WriteLine(\"Hello World!\");", replacement);
                        sw.Write(csStr);
                        sw.Flush();
                    }
                    else
                    {
                        int start = csStr.IndexOf("string optstring");
                        int end   = csStr.IndexOf("string[] opts");
                        csStr = csStr.Replace(csStr.Substring(start, end - start), "string optstring = @\"" + options + "\";\r\n");
                        sw.Write(csStr);
                        sw.Flush();
                    }
                }

                string csprojStr = string.Empty;
                using (var sr2 = new StreamReader(project.FullPath))
                {
                    csprojStr = sr2.ReadToEnd();
                }

                if (csprojStr.Contains("System.ServiceModel"))
                {
                    using (var sw2 = new StreamWriter(project.FullPath))
                    {
                        sw2.Write(System.Text.RegularExpressions.Regex.Replace(csprojStr, @"<ItemGroup>\s+<PackageReference Include=""System.ServiceModel[\S\s]+ItemGroup>", ""));
                        sw2.Flush();
                    }
                }

                result = ProcessRunner.RunAsync("dotnet", $"run", project.DirectoryPath, redirectOutput: true, throwOnError: false, environmentVariables: envVars, logger: logger, cancellationToken: CancellationToken.None).Result;
            }

            var isSuccess    = result.ExitCode == 0 || result.ExitCode == 6;
            var isTestSucess = !(isSuccess ^ expectSuccess);

            if (!isTestSucess)
            {
                var errMsg = string.Format(E2ETest.g_ToReproduceProblemMessageFormat, project.DirectoryPath, options);
                logger?.WriteMessageAsync(errMsg, true).Wait();
            }

            return(result);
        }