Beispiel #1
0
        public static MSBuildProj GetProject(string filePath, string targetFramework, bool forceNew, bool build, ILogger logger, bool globalTool = false)
        {
            MSBuildProj project = null;

            var projectDir     = Path.GetDirectoryName(filePath);
            var srcProgramFile = Path.Combine(projectDir, "Program.cs");
            var dstProgramFile = Path.Combine(projectDir, $"{Path.GetFileNameWithoutExtension(filePath)}.cs");

            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            if (forceNew && File.Exists(filePath))
            {
                File.Delete(filePath);
                if (File.Exists(dstProgramFile))
                {
                    File.Delete(dstProgramFile);
                }
                FileUtil.TryDeleteDirectory(Path.Combine(Path.GetDirectoryName(filePath), "obj"));
            }

            if (File.Exists(filePath))
            {
                project = MSBuildProj.FromPathAsync(filePath, logger, token).Result;
            }
            else
            {
                project = MSBuildProj.DotNetNewAsync(filePath, logger, token).Result;
                File.Move(srcProgramFile, dstProgramFile);
            }

            Assert.NotNull(project);

            if (!string.IsNullOrEmpty(targetFramework))
            {
                project.TargetFramework = targetFramework;
            }

            if (!globalTool)
            {
                var svcutilPkgVersion = E2ETest.GetSvcutilPkgVersionAndFeed();
                var svcutilPkgRef     = ProjectDependency.FromPackage("dotnet-svcutil-lib", svcutilPkgVersion);
                if (!project.Dependencies.Any(d => d.Equals(svcutilPkgRef)))
                {
                    bool success = project.AddDependency(svcutilPkgRef);
                    Assert.True(success, $"Could not add tool package dependency: dotnet-svcutil-lib.{svcutilPkgVersion}");
                }
            }

            var ret = project.RestoreAsync(logger, token).Result;

            Assert.True(ret.ExitCode == 0, $"Project package restore failed:{Environment.NewLine}{ret.OutputText}{logger}");

            if (build)
            {
                ret = project.BuildAsync(logger, token).Result;
                Assert.True(ret.ExitCode == 0, $"Project build failed:{Environment.NewLine}{ret.OutputText}{logger}");
            }

            return(project);
        }
Beispiel #2
0
        public void InitializeUnitTest(string testCaseName, bool createProject = true, string sdkVersion = null)
        {
            Assert.False(string.IsNullOrWhiteSpace(testCaseName), $"{nameof(testCaseName)} is not initialized!");

            this_TestCaseOutputDir = Path.Combine(this_TestGroupOutputDir, testCaseName);
            FileUtil.TryDeleteDirectory(this_TestCaseOutputDir);
            Directory.CreateDirectory(this_TestCaseOutputDir);

            // do not create directory as it is not expected to be used by a unit-test.
            this_TestCaseBootstrapDir = Path.Combine(this_TestGroupBootstrapDir, testCaseName);

            this_TestCaseLogger  = new DebugLogger();
            this_TestCaseLogFile = Path.Combine(this_TestGroupOutputDir, $"{testCaseName}.log");

            this_TestCaseSdkVersion = sdkVersion;

            if (createProject == true)
            {
                var project     = (sdkVersion == null || g_StarterProject.SdkVersion == sdkVersion) ? g_StarterProject : this_TestGroupProject;
                var projectPath = CopyProject(g_StarterProject, this_TestCaseOutputDir, testCaseName);

                this_TestCaseProject = MSBuildProj.FromPathAsync(projectPath, null, CancellationToken.None).Result;
            }
            else
            {
                this_TestCaseProject = g_StarterProject;
            }

            this_TestCaseBaselinesDir = Path.Combine(this_TestGroupBaselinesDir, testCaseName);
            var linuxBaselinePath = Path.Combine(this_TestCaseBaselinesDir, "Linux");

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && File.Exists(Path.Combine(linuxBaselinePath, Path.GetFileName(this_TestCaseLogFile))))
            {
                this_TestCaseBaselinesDir = linuxBaselinePath;
            }
            Directory.CreateDirectory(this_TestCaseBaselinesDir);

            this_FixupUtil = new FixupUtil();
            this_FixupUtil.Init(g_TestResultsDir, g_TestCasesDir, this_TestCaseOutputDir, g_ServiceUrl, g_ServiceId, g_RepositoryRoot);

            Assert.True(Directory.Exists(this_TestCaseOutputDir), $"{nameof(this_TestGroupOutputDir)} is not initialized!");
            Assert.True(Directory.Exists(this_TestCaseBaselinesDir), $"{nameof(this_TestCaseBaselinesDir)} is not initialized!");
            Assert.True(this_TestCaseBootstrapDir != null, $"{nameof(this_TestCaseBootstrapDir)} is not initialized!");
            Assert.True(this_TestCaseLogger != null, $"{nameof(this_TestGroupOutputDir)} is not initialized!");
            Assert.True(this_FixupUtil != null, $"{nameof(this_FixupUtil)} is not initialized!");
        }
Beispiel #3
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);
        }