/// <summary> /// Creates and builds a new Sonar-enabled project using the supplied descriptor. /// The method will check the build succeeded and that a single project output file was created. /// </summary> /// <returns>The full path of the project-specsific directory that was created during the build</returns> private string CreateAndBuildSonarProject(ProjectDescriptor descriptor, string rootOutputFolder, WellKnownProjectProperties preImportProperties) { ProjectRootElement projectRoot = BuildUtilities.CreateInitializedProjectRoot(this.TestContext, descriptor, preImportProperties); BuildLogger logger = new BuildLogger(); // Act BuildResult result = BuildUtilities.BuildTargets(projectRoot, logger); // Assert BuildAssertions.AssertTargetSucceeded(result, TargetConstants.DefaultBuildTarget); // We expect the compiler to warn if there are no compiler inputs int expectedWarnings = (descriptor.ManagedSourceFiles == null || !descriptor.ManagedSourceFiles.Any()) ? 1 : 0; logger.AssertExpectedErrorCount(0); logger.AssertExpectedWarningCount(expectedWarnings); logger.AssertExpectedTargetOrdering(TargetConstants.CoreCompileTarget, TargetConstants.CalculateSonarQubeFileListsTarget, TargetConstants.DefaultBuildTarget, TargetConstants.WriteProjectDataTarget); // Check expected folder structure exists CheckRootOutputFolder(rootOutputFolder); // Check expected project outputs Assert.AreEqual(1, Directory.EnumerateDirectories(rootOutputFolder).Count(), "Only expecting one child directory to exist under the root analysis output folder"); ProjectInfoAssertions.AssertProjectInfoExists(rootOutputFolder, projectRoot.FullPath); return(Directory.EnumerateDirectories(rootOutputFolder).Single()); }
/// <summary> /// Creates and builds a new Sonar-enabled project using the supplied descriptor. /// The method will check the build succeeded and that a single project output file was created. /// </summary> /// <returns>The full path of the project-specific directory that was created during the build</returns> private string CreateAndBuildSonarProject(ProjectDescriptor descriptor, string rootOutputFolder, WellKnownProjectProperties preImportProperties) { var projectRoot = BuildUtilities.CreateInitializedProjectRoot(TestContext, descriptor, preImportProperties); // Act var result = BuildRunner.BuildTargets(TestContext, descriptor.FullFilePath); TestContext.AddResultFile(result.FilePath); // Assert result.AssertTargetSucceeded(TargetConstants.DefaultBuildTarget); // We expect the compiler to warn if there are no compiler inputs var expectedWarnings = (descriptor.ManagedSourceFiles.Any()) ? 0 : 1; result.AssertExpectedErrorCount(0); result.AssertExpectedWarningCount(expectedWarnings); result.AssertExpectedTargetOrdering( TargetConstants.CategoriseProjectTarget, TargetConstants.DefaultBuildTarget, TargetConstants.CalculateFilesToAnalyzeTarget, TargetConstants.WriteProjectDataTarget); // Check expected folder structure exists CheckRootOutputFolder(rootOutputFolder); // Check expected project outputs Assert.AreEqual(1, Directory.EnumerateDirectories(rootOutputFolder).Count(), "Only expecting one child directory to exist under the root analysis output folder"); ProjectInfoAssertions.AssertProjectInfoExists(rootOutputFolder, descriptor.FullFilePath); return(Directory.EnumerateDirectories(rootOutputFolder).Single()); }
public void E2E_InvalidGuid() { // Projects with invalid guids should have a warning emitted. The project info // should not be generated. // Arrange var rootInputFolder = TestUtils.CreateTestSpecificFolder(TestContext, "Inputs"); var rootOutputFolder = TestUtils.CreateTestSpecificFolder(TestContext, "Outputs"); var preImportProperties = CreateDefaultAnalysisProperties(rootInputFolder, rootOutputFolder); var descriptor = new ProjectDescriptor() { // No guid property IsTestProject = false, ParentDirectoryPath = rootInputFolder, ProjectFolderName = "MyProjectDir", ProjectFileName = "MissingProjectGuidProject.proj" }; AddEmptyAnalysedCodeFile(descriptor, rootInputFolder); var projectRoot = BuildUtilities.CreateInitializedProjectRoot(TestContext, descriptor, preImportProperties); projectRoot.AddProperty("ProjectGuid", "Invalid guid"); string projectFilePath = Path.Combine(rootInputFolder, descriptor.ProjectFolderName, descriptor.ProjectFileName); projectRoot.Save(projectFilePath); TestContext.AddResultFile(projectFilePath); // Act var buildLog = BuildRunner.BuildTargets(TestContext, projectFilePath); // Assert buildLog.AssertTargetSucceeded(TargetConstants.DefaultBuildTarget); // Build should succeed with warnings ProjectInfoAssertions.AssertProjectInfoExists(rootOutputFolder, projectFilePath); buildLog.AssertExpectedErrorCount(0); buildLog.AssertExpectedWarningCount(1); var warning = buildLog.Warnings[0]; Assert.IsTrue(warning.Contains(descriptor.FullFilePath), "Expecting the warning to contain the full path to the bad project file"); }
private ProjectRootElement CreateInitializedProject(ProjectDescriptor descriptor, WellKnownProjectProperties preImportProperties, string rootOutputFolder) { // Still need to set the conditions so the target is invoked preImportProperties.RunSonarQubeAnalysis = "true"; preImportProperties.SonarQubeOutputPath = rootOutputFolder; // The config folder needs to be set for the targets to succeed. Use the temp folder // if one has not been supplied. if (string.IsNullOrEmpty(preImportProperties.SonarQubeConfigPath)) { preImportProperties.SonarQubeConfigPath = Path.GetTempPath(); } ProjectRootElement projectRoot = BuildUtilities.CreateInitializedProjectRoot(this.TestContext, descriptor, preImportProperties); return(projectRoot); }
public void E2E_MissingProjectGuid() { // Projects with missing guids should have a warning emitted. The project info // should not be generated. // Arrange string rootInputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "Inputs"); string rootOutputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "Outputs"); WellKnownProjectProperties preImportProperties = CreateDefaultAnalysisProperties(rootInputFolder, rootOutputFolder); preImportProperties["SonarConfigPath"] = rootInputFolder; ProjectDescriptor descriptor = new ProjectDescriptor() { // No guid property IsTestProject = false, ParentDirectoryPath = rootInputFolder, ProjectFolderName = "MyProjectDir", ProjectFileName = "MissingProjectGuidProject.proj" }; AddEmptyCodeFile(descriptor, rootInputFolder); ProjectRootElement projectRoot = BuildUtilities.CreateInitializedProjectRoot(this.TestContext, descriptor, preImportProperties); BuildLogger logger = new BuildLogger(); // Act BuildResult result = BuildUtilities.BuildTargets(projectRoot, logger); // Assert BuildAssertions.AssertTargetSucceeded(result, TargetConstants.DefaultBuildTarget); // Build should succeed with warnings ProjectInfoAssertions.AssertNoProjectInfoFilesExists(rootOutputFolder); logger.AssertExpectedErrorCount(0); logger.AssertExpectedWarningCount(1); BuildWarningEventArgs warning = logger.Warnings[0]; Assert.IsTrue(warning.Message.Contains(descriptor.FullFilePath), "Expecting the warning to contain the full path to the bad project file"); }
public void E2E_FxCop_TestProject_TestProjectByName() { // Arrange string rootInputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "Inputs"); string rootOutputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "Outputs"); string fxCopLogFile = Path.Combine(rootInputFolder, "FxCopResults.xml"); WellKnownProjectProperties preImportProperties = new WellKnownProjectProperties(); preImportProperties.SonarQubeTempPath = rootOutputFolder; // FIXME preImportProperties.SonarQubeOutputPath = rootOutputFolder; preImportProperties.CodeAnalysisLogFile = fxCopLogFile; preImportProperties.CodeAnalysisRuleset = "specifiedInProject.ruleset"; preImportProperties.SonarQubeConfigPath = rootInputFolder; preImportProperties[TargetProperties.SonarQubeConfigPath] = rootInputFolder; CreateValidFxCopRuleset(rootInputFolder, string.Format(TargetProperties.SonarQubeRulesetFormat, "cs")); // Create a config file in the config folder containing a reg ex to identify tests projects AnalysisConfig config = new AnalysisConfig(); config.LocalSettings = new AnalysisProperties(); config.LocalSettings.Add(new Property() { Id = IsTestFileByName.TestRegExSettingId, Value = ".testp." }); string configFullPath = Path.Combine(rootInputFolder, FileConstants.ConfigFileName); config.Save(configFullPath); // Create a project with a file name that will match the reg ex ProjectDescriptor descriptor = BuildUtilities.CreateValidProjectDescriptor(rootInputFolder, "MyTestProject.proj"); ProjectRootElement projectRoot = BuildUtilities.CreateInitializedProjectRoot(this.TestContext, descriptor, preImportProperties); // Add a file to the project string itemPath = CreateTextFile(rootInputFolder, "code1.cs", "class myclassXXX{}"); projectRoot.AddItem(TargetProperties.ItemType_Compile, itemPath); projectRoot.Save(); BuildLogger logger = new BuildLogger(); // Act BuildResult result = BuildUtilities.BuildTargets(projectRoot, logger); // Assert BuildAssertions.AssertTargetSucceeded(result, TargetConstants.DefaultBuildTarget); logger.AssertExpectedTargetOrdering( TargetConstants.CategoriseProjectTarget, TargetConstants.OverrideFxCopSettingsTarget, TargetConstants.FxCopTarget, TargetConstants.DefaultBuildTarget, TargetConstants.CalculateFilesToAnalyzeTarget, TargetConstants.WriteProjectDataTarget); // We expect the FxCop target to be executed... logger.AssertTargetExecuted(TargetConstants.FxCopTarget); // ... but we don't expect the FxCop *task* to have executed logger.AssertTaskNotExecuted(TargetConstants.FxCopTask); logger.AssertTargetNotExecuted(TargetConstants.SetFxCopResultsTarget); ProjectInfo projectInfo = ProjectInfoAssertions.AssertProjectInfoExists(rootOutputFolder, projectRoot.FullPath); ProjectInfoAssertions.AssertAnalysisResultDoesNotExists(projectInfo, AnalysisType.FxCop.ToString()); }