[WorkItem(50)] // Regression test for Bug 50:MSBuild projects with missing ProjectGuids cause the build to fail public void WriteProjectInfoFile_MissingProjectGuid() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); WriteProjectInfoFile task = new WriteProjectInfoFile { FullProjectPath = "c:\\fullPath\\project.proj", IsTest = true, OutputFolder = testFolder, ProjectName = "ProjectWithoutProjectGuid", ProjectLanguage = "C#" }; // No analysis results are supplied // Act DummyBuildEngine engine = new DummyBuildEngine(); task.BuildEngine = engine; bool success = task.Execute(); // Assert Assert.IsTrue(success, "Not expecting the task to fail as this would fail the build"); engine.AssertNoErrors(); Assert.AreEqual(1, engine.Warnings.Count, "Expecting a build warning as the ProjectGuid is missing"); BuildWarningEventArgs firstWarning = engine.Warnings[0]; Assert.IsNotNull(firstWarning.Message, "Warning message should not be null"); string projectInfoFilePath = Path.Combine(testFolder, ExpectedProjectInfoFileName); Assert.IsFalse(File.Exists(projectInfoFilePath), "Not expecting the project info file to have been created"); }
[TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11 public void IsTestFile_TimeoutIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the task to timeout string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds + 600); // sleep for longer than the timeout period lockingStream.Close(); }); result = task.Execute(); } Assert.IsFalse(result, "Expecting the task to fail"); dummyEngine.AssertMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleErrorExists(); }
public void AttachBW_MissingBinaryFiles_TaskFails() { // Create a valid setup, then delete one of the required files each time // and check that the task fails // Arrange string testFolder = TestUtils.GetTestSpecificFolderName(this.TestContext); for (int i = 0; i < TaskExecutionContext.RequiredFileNames.Length; i++) { // Clean up after the previous iteration if (Directory.Exists(testFolder)) { Directory.Delete(testFolder, true); } Directory.CreateDirectory(testFolder); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(testFolder, testFolder, testFolder, 0 /* returns failure code */ ); string missingFilePath = taskContext.RequiredFilePaths[i]; File.Delete(missingFilePath); // delete one of the required files AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, testFolder, testFolder); // Act and assert bool success = testSubject.Execute(); Assert.IsFalse(success, "Expecting the task execution to fail"); dummyEngine.AssertSingleErrorExists(missingFilePath); } }
private static MergeRuleSets CreateTask(DummyBuildEngine buildEngine, string primaryRuleset, params string[] rulesetsToInclude) { MergeRuleSets task = new MergeRuleSets(); task.BuildEngine = buildEngine; task.PrimaryRulesetFilePath = primaryRuleset; task.IncludedRulesetFilePaths = rulesetsToInclude; return(task); }
public void MergeRulesets_PrimaryRulesetDoesNotExist() { // Arrange DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, "missing.ruleset"); // Act and Assert FileNotFoundException ex = AssertException.Expects <FileNotFoundException>(() => task.Execute()); Assert.AreEqual("missing.ruleset", ex.FileName); }
private static void ExecuteAndCheckSuccess(Task task) { var dummyEngine = new DummyBuildEngine(); task.BuildEngine = dummyEngine; var taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }
private void ExecuteAndCheckSuccess(string primaryRuleset, params string[] rulesetsToInclude) { this.TestContext.AddResultFile(primaryRuleset); DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, primaryRuleset, rulesetsToInclude); bool taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }
public void MergeRulesets_PrimaryRulesetDoesNotExist() { // Arrange string projectDir = this.TestContext.TestDeploymentDir; string targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine,projectDir, "missing.ruleset", targetRulesetFilePath); // Act and Assert FileNotFoundException ex = AssertException.Expects<FileNotFoundException>(() => task.Execute()); Assert.AreEqual("missing.ruleset", ex.FileName); }
public void MergeRulesets_PrimaryRulesetDoesNotExist() { // Arrange string projectDir = this.TestContext.TestDeploymentDir; string targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, projectDir, "missing.ruleset", targetRulesetFilePath); // Act and Assert FileNotFoundException ex = AssertException.Expects <FileNotFoundException>(() => task.Execute()); Assert.AreEqual("missing.ruleset", ex.FileName); }
public void MergeRulesets_MergedRulesetAlreadyExists() { // Arrange string projectDir = this.TestContext.TestDeploymentDir; string primaryRuleset = this.CreateValidRuleset("valid.ruleset.txt"); string targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt"); File.WriteAllText(targetRulesetFilePath, "dummy existing ruleset"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, projectDir, primaryRuleset, targetRulesetFilePath); // Act and Assert InvalidOperationException ex = AssertException.Expects<InvalidOperationException>(() => task.Execute()); Assert.IsTrue(ex.Message.Contains(targetRulesetFilePath)); }
public void MergeRulesets_MergedRulesetAlreadyExists() { // Arrange string projectDir = this.TestContext.TestDeploymentDir; string primaryRuleset = this.CreateValidRuleset("valid.ruleset.txt"); string targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt"); File.WriteAllText(targetRulesetFilePath, "dummy existing ruleset"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, projectDir, primaryRuleset, targetRulesetFilePath); // Act and Assert InvalidOperationException ex = AssertException.Expects <InvalidOperationException>(() => task.Execute()); Assert.IsTrue(ex.Message.Contains(targetRulesetFilePath)); }
private static bool ExecuteAndCheckSuccess(string analysisDir, string fullFileName) { DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = fullFileName; task.AnalysisConfigDir = analysisDir; bool taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); return(task.IsTest); }
private static MergeRuleSets CreateTask(DummyBuildEngine buildEngine, string projectDir, string primaryRuleset, string targetRulesetFilePath, params string[] rulesetsToInclude) { var task = new MergeRuleSets { BuildEngine = buildEngine, ProjectDirectoryPath = projectDir, PrimaryRulesetFilePath = primaryRuleset, MergedRuleSetFilePath = targetRulesetFilePath, IncludedRulesetFilePaths = rulesetsToInclude }; return(task); }
[TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11 public void IsTestFile_RetryIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the retry period to occur, but // not so long that the task times out int lockPeriodInMilliseconds = 1000; Assert.IsTrue(lockPeriodInMilliseconds < IsTestFileByName.MaxConfigRetryPeriodInMilliseconds, "Test setup error: the test is sleeping for too long"); string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; Stopwatch testDuration = Stopwatch.StartNew(); using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(lockPeriodInMilliseconds); // unlock the file after a short delay lockingStream.Close(); }); result = task.Execute(); } testDuration.Stop(); Assert.IsTrue(testDuration.ElapsedMilliseconds > 1000, "Test error: expecting the test to have taken at least {0} milliseconds to run. Actual: {1}", lockPeriodInMilliseconds, testDuration.ElapsedMilliseconds); Assert.IsTrue(result, "Expecting the task to succeed"); Assert.IsTrue(task.IsTest, "Expecting the file to be recognised as a test"); dummyEngine.AssertMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }
public void IsTestFile_InvalidRegexInConfig() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string invalidRegEx = "Invalid regex (("; EnsureAnalysisConfig(testFolder, invalidRegEx); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "Path"; task.AnalysisConfigDir = testFolder; bool result = task.Execute(); Assert.IsFalse(result, "Expecting the task to fail"); dummyEngine.AssertSingleErrorExists(invalidRegEx); // expecting the invalid expression to appear in the error }
private string ExecuteAndCheckSuccess(string projectDirectory, string primaryRuleset, params string[] rulesetsToInclude) { string mergedRulesetFileName = primaryRuleset + ".merged.txt"; DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, projectDirectory, primaryRuleset, mergedRulesetFileName, rulesetsToInclude); bool taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); Assert.IsTrue(File.Exists(mergedRulesetFileName), "Expecting the merged ruleset to have been created: {0}", mergedRulesetFileName); this.TestContext.AddResultFile(primaryRuleset); this.TestContext.AddResultFile(mergedRulesetFileName); return(mergedRulesetFileName); }
[TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11 public void IsTestFile_TimeoutIfConfigLocked_TaskFails() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result = true; TaskUtilitiesTests.PerformOpOnLockedFile(configFile, () => result = task.Execute(), shouldTimeoutReadingConfig: true); Assert.IsFalse(result, "Expecting the task to fail if the config file could not be read"); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleErrorExists(); }
[WorkItem(50)] // Regression test for Bug 50:MSBuild projects with missing ProjectGuids cause the build to fail public void WriteProjectInfoFile_MissingProjectGuid() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); WriteProjectInfoFile task = new WriteProjectInfoFile(); task.FullProjectPath = "c:\\fullPath\\project.proj"; task.IsTest = true; task.OutputFolder = testFolder; task.ProjectName = "ProjectWithoutProjectGuid"; task.ProjectLanguage = "C#"; // No analysis results are supplied // Act DummyBuildEngine engine = new DummyBuildEngine(); task.BuildEngine = engine; bool success = task.Execute(); // Assert Assert.IsTrue(success, "Not expecting the task to fail as this would fail the build"); engine.AssertNoErrors(); Assert.AreEqual(1, engine.Warnings.Count, "Expecting a build warning as the ProjectGuid is missing"); BuildWarningEventArgs firstWarning = engine.Warnings[0]; Assert.IsNotNull(firstWarning.Message, "Warning message should not be null"); string projectInfoFilePath = Path.Combine(testFolder, ExpectedProjectInfoFileName); Assert.IsFalse(File.Exists(projectInfoFilePath), "Not expecting the project info file to have been created"); }
public void WriteProjectInfoFile_AnalysisSettings() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); Guid projectGuid = Guid.NewGuid(); WriteProjectInfoFile task = new WriteProjectInfoFile(); DummyBuildEngine buildEngine = new DummyBuildEngine(); task.BuildEngine = buildEngine; task.FullProjectPath = "x:\\analysisSettings.csproj"; task.IsTest = false; task.OutputFolder = testFolder; task.ProjectGuid = projectGuid.ToString("B"); task.ProjectName = "MyProject"; task.ProjectLanguage = "C#"; // Example of a valid setting: // <SonarQubeSetting Include="sonar.resharper.projectname"> // <Value>C:\zzz\reportlocation.xxx</Value> // </SonarQubeSetting> List<ITaskItem> settingsInputs = new List<ITaskItem>(); // Add invalid task items // Note: the TaskItem class won't allow the item spec or metadata values to be null, // so we aren't testing those settingsInputs.Add(CreateMetadataItem("invalid.missing.value.metadata", "NotValueMetadata", "missing value 1")); // value field is missing settingsInputs.Add(CreateAnalysisSettingTaskItem(" ", "should be ignored - key is whitespace only")); // whitespace key settingsInputs.Add(CreateAnalysisSettingTaskItem("invalid spaces in key", "spaces.in.key")); // spaces in key settingsInputs.Add(CreateAnalysisSettingTaskItem(" invalid.key.has.leading.whitespace", "leading whitespace in key")); settingsInputs.Add(CreateAnalysisSettingTaskItem("invalid.key.has.trailing.whitespace ", "trailing whitespace in key")); settingsInputs.Add(CreateAnalysisSettingTaskItem(".invalid.non.alpha.first.character", "non alpha first character")); // Add valid task items settingsInputs.Add(CreateAnalysisSettingTaskItem("valid.setting.1", @"c:\dir1\dir2\file.txt")); settingsInputs.Add(CreateAnalysisSettingTaskItem("valid.value.is.whitespace.only", " ")); settingsInputs.Add(CreateMetadataItem("valid.metadata.name.is.case.insensitive", BuildTaskConstants.SettingValueMetadataName.ToUpperInvariant(), "uppercase metadata name")); // metadata name is in the wrong case settingsInputs.Add(CreateAnalysisSettingTaskItem("valid.value.has.whitespace", "valid setting with whitespace")); settingsInputs.Add(CreateAnalysisSettingTaskItem("X", "single character key")); settingsInputs.Add(CreateAnalysisSettingTaskItem("Y...", "single character followed by periods")); settingsInputs.Add(CreateAnalysisSettingTaskItem("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.sonar.projectName", "guid followed by key")); task.AnalysisSettings = settingsInputs.ToArray(); // Act ProjectInfo createdProjectInfo; createdProjectInfo = ExecuteAndCheckSucceeds(task, testFolder); // Assert buildEngine.AssertSingleWarningExists("invalid.missing.value.metadata"); // Can't easily check for the message complaining against the empty key buildEngine.AssertSingleWarningExists("invalid spaces in key"); buildEngine.AssertSingleWarningExists(" invalid.key.has.leading.whitespace"); buildEngine.AssertSingleWarningExists("invalid.key.has.trailing.whitespace "); buildEngine.AssertSingleWarningExists(".invalid.non.alpha.first.character"); AssertAnalysisSettingExists(createdProjectInfo, "valid.setting.1", @"c:\dir1\dir2\file.txt"); AssertAnalysisSettingExists(createdProjectInfo, "valid.value.is.whitespace.only", null); AssertAnalysisSettingExists(createdProjectInfo, "valid.value.has.whitespace", "valid setting with whitespace"); AssertAnalysisSettingExists(createdProjectInfo, "valid.metadata.name.is.case.insensitive", "uppercase metadata name"); AssertAnalysisSettingExists(createdProjectInfo, "X", "single character key"); AssertAnalysisSettingExists(createdProjectInfo, "Y...", "single character followed by periods"); AssertAnalysisSettingExists(createdProjectInfo, "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.sonar.projectName", "guid followed by key"); AssertExpectedAnalysisSettingsCount(7, createdProjectInfo); }
public void AttachBW_NotAttached_ExeReturnsFailure_TaskFails() { // Arrange string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config"); string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, outputFolder, 1 /* returns failure code */ ); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsFalse(result, "Not expecting the task to succeed"); dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_FailedToAttach); dummyEngine.AssertNoWarnings(); // Check the parameters passed to the exe string logPath = AssertLogFileExists(taskContext); DummyExeHelper.AssertExpectedLogContents(logPath, "--msbuild-task", Process.GetCurrentProcess().Id.ToString(), outputFolder); }
public void MergeRulesets_PrimaryRulesetDoesNotExist() { // Arrange DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, "missing.ruleset"); // Act and Assert FileNotFoundException ex = AssertException.Expects<FileNotFoundException>(() => task.Execute()); Assert.AreEqual("missing.ruleset", ex.FileName); }
private static MergeRuleSets CreateTask(DummyBuildEngine buildEngine, string primaryRuleset, params string[] rulesetsToInclude) { MergeRuleSets task = new MergeRuleSets(); task.BuildEngine = buildEngine; task.PrimaryRulesetFilePath = primaryRuleset; task.IncludedRulesetFilePaths = rulesetsToInclude; return task; }
private static void ExecuteAndCheckSuccess(Task task) { DummyBuildEngine dummyEngine = new DummyBuildEngine(); task.BuildEngine = dummyEngine; bool taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }
public void AttachBW_NotAttached_ExeReturnsSuccess_TaskSucceeds() { // Arrange string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config"); string outputFolder = Path.Combine(binFolder, "output"); // expected location: does not exist DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, outputFolder, 0 /* returns success code */ ); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsTrue(result, "Expecting task to succeed"); Assert.IsTrue(Directory.Exists(outputFolder), "Expecting the output folder to have been created"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleMessageExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_AttachedSuccessfully); // Check the parameters passed to the exe string logPath = AssertLogFileExists(taskContext); DummyExeHelper.AssertExpectedLogContents(logPath, "--msbuild-task", Process.GetCurrentProcess().Id.ToString(), outputFolder); }
public void AttachBW_NotAttached_ConsoleOutputIsCaptured() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(testFolder, testFolder, outputFolder, 0 /* returns success code */, // Embed additional code in the dummy exe @"System.Console.WriteLine(""AAA standard output should be captured""); System.Console.Error.WriteLine(""BBB standard error should be captured"");"); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, testFolder, testFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsTrue(result, "Expecting the task to succeed"); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleMessageExists("AAA standard output should be captured"); dummyEngine.AssertSingleErrorExists("BBB standard error should be captured"); }
public void AttachBW_NotAttached_ExeThrows_TaskFails() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext dummy = new TaskExecutionContext(testFolder, testFolder, outputFolder, 0 /* returns success code */, // Embed additional code in the dummy exe @"throw new System.Exception(""XXX thrown error should be captured"");"); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, testFolder, testFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsFalse(result, "Not expecting the task to succeed"); dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_FailedToAttach); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleErrorExists("XXX thrown error should be captured"); }
private static void InitializeTask(AttachBuildWrapper task, DummyBuildEngine engine, string binPath, string configDir) { task.BuildEngine = engine; task.BinDirectoryPath = binPath; task.AnalysisConfigDir = configDir; }
private static AttachBuildWrapper CreateTestSubject(DummyBuildEngine engine, string binPath, string configDir) { AttachBuildWrapper task = new AttachBuildWrapper(); InitializeTask(task, engine, binPath, configDir); return task; }
public void AttachBW_NotAttached_Timeout_TaskFails() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(testFolder, testFolder, outputFolder, 0 /* returns success code */, // Embed additional code in the dummy exe - pause execution for 100 ms @"System.Threading.Thread.Sleep(200);"); AttachBuildWrapper testSubject = new AttachBuildWrapper(11 /* timeout after 11 ms */); InitializeTask(testSubject, dummyEngine, testFolder, testFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsFalse(result, "Not expecting the task to succeed - should have timed out"); dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_FailedToAttach); dummyEngine.AssertSingleWarningExists("11"); // expecting a warning with the timeout value }
public void IsTestFile_TimeoutIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the task to timeout string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds + 600); // sleep for longer than the timeout period lockingStream.Close(); }); result = task.Execute(); } Assert.IsFalse(result, "Expecting the task to fail"); dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoWarnings(); dummyEngine.AssertSingleErrorExists(); }
public void AttachBW_MissingOutputDirectorySetting_TaskFails() { // Arrange string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config"); string outputFolder = Path.Combine(binFolder, "output"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, null /* no output dir set */, 0 /* returns success code */ ); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsFalse(result, "Expecting task to fail"); Assert.IsFalse(Directory.Exists(outputFolder), "Not expecting the output folder to have been created"); dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_MissingOutputDirectory); dummyEngine.AssertNoWarnings(); AssertLogFileDoesNotExist(taskContext); }
private static bool ExecuteAndCheckSuccess(string analysisDir, string fullFileName) { DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = fullFileName; task.AnalysisConfigDir = analysisDir; bool taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); return task.IsTest; }
public void WriteProjectInfoFile_AnalysisSettings() { // Arrange string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); Guid projectGuid = Guid.NewGuid(); WriteProjectInfoFile task = new WriteProjectInfoFile(); DummyBuildEngine buildEngine = new DummyBuildEngine(); task.BuildEngine = buildEngine; task.FullProjectPath = "x:\\analysisSettings.csproj"; task.IsTest = false; task.OutputFolder = testFolder; task.ProjectGuid = projectGuid.ToString("B"); task.ProjectName = "MyProject"; task.ProjectLanguage = "C#"; // Example of a valid setting: // <SonarQubeSetting Include="sonar.resharper.projectname"> // <Value>C:\zzz\reportlocation.xxx</Value> // </SonarQubeSetting> List <ITaskItem> settingsInputs = new List <ITaskItem> { // Add invalid task items // Note: the TaskItem class won't allow the item spec or metadata values to be null, // so we aren't testing those CreateMetadataItem("invalid.missing.value.metadata", "NotValueMetadata", "missing value 1"), // value field is missing CreateAnalysisSettingTaskItem(" ", "should be ignored - key is whitespace only"), // whitespace key CreateAnalysisSettingTaskItem("invalid spaces in key", "spaces.in.key"), // spaces in key CreateAnalysisSettingTaskItem(" invalid.key.has.leading.whitespace", "leading whitespace in key"), CreateAnalysisSettingTaskItem("invalid.key.has.trailing.whitespace ", "trailing whitespace in key"), CreateAnalysisSettingTaskItem(".invalid.non.alpha.first.character", "non alpha first character"), // Add valid task items CreateAnalysisSettingTaskItem("valid.setting.1", @"c:\dir1\dir2\file.txt"), CreateAnalysisSettingTaskItem("valid.value.is.whitespace.only", " "), CreateMetadataItem("valid.metadata.name.is.case.insensitive", BuildTaskConstants.SettingValueMetadataName.ToUpperInvariant(), "uppercase metadata name"), // metadata name is in the wrong case CreateAnalysisSettingTaskItem("valid.value.has.whitespace", "valid setting with whitespace"), CreateAnalysisSettingTaskItem("X", "single character key"), CreateAnalysisSettingTaskItem("Y...", "single character followed by periods"), CreateAnalysisSettingTaskItem("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.sonar.projectName", "guid followed by key") }; task.AnalysisSettings = settingsInputs.ToArray(); // Act ProjectInfo createdProjectInfo; createdProjectInfo = ExecuteAndCheckSucceeds(task, testFolder); // Assert buildEngine.AssertSingleWarningExists("invalid.missing.value.metadata"); // Can't easily check for the message complaining against the empty key buildEngine.AssertSingleWarningExists("invalid spaces in key"); buildEngine.AssertSingleWarningExists(" invalid.key.has.leading.whitespace"); buildEngine.AssertSingleWarningExists("invalid.key.has.trailing.whitespace "); buildEngine.AssertSingleWarningExists(".invalid.non.alpha.first.character"); AssertAnalysisSettingExists(createdProjectInfo, "valid.setting.1", @"c:\dir1\dir2\file.txt"); AssertAnalysisSettingExists(createdProjectInfo, "valid.value.is.whitespace.only", null); AssertAnalysisSettingExists(createdProjectInfo, "valid.value.has.whitespace", "valid setting with whitespace"); AssertAnalysisSettingExists(createdProjectInfo, "valid.metadata.name.is.case.insensitive", "uppercase metadata name"); AssertAnalysisSettingExists(createdProjectInfo, "X", "single character key"); AssertAnalysisSettingExists(createdProjectInfo, "Y...", "single character followed by periods"); AssertAnalysisSettingExists(createdProjectInfo, "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.sonar.projectName", "guid followed by key"); AssertExpectedAnalysisSettingsCount(7, createdProjectInfo); }
public void IsTestFile_RetryIfConfigLocked() { // Arrange // We'll lock the file and sleep for long enough for the retry period to occur, but // not so long that the task times out int lockPeriodInMilliseconds = 1000; Assert.IsTrue(lockPeriodInMilliseconds < IsTestFileByName.MaxConfigRetryPeriodInMilliseconds, "Test setup error: the test is sleeping for too long"); string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFile = EnsureAnalysisConfig(testFolder, ".XX."); DummyBuildEngine dummyEngine = new DummyBuildEngine(); IsTestFileByName task = new IsTestFileByName(); task.BuildEngine = dummyEngine; task.FullFilePath = "XXX.proj"; task.AnalysisConfigDir = testFolder; bool result; Stopwatch testDuration = Stopwatch.StartNew(); using (FileStream lockingStream = File.OpenWrite(configFile)) { System.Threading.Tasks.Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(lockPeriodInMilliseconds); // unlock the file after a short delay lockingStream.Close(); }); result = task.Execute(); } testDuration.Stop(); Assert.IsTrue(testDuration.ElapsedMilliseconds > 1000, "Test error: expecting the test to have taken at least {0} milliseconds to run. Actual: {1}", lockPeriodInMilliseconds, testDuration.ElapsedMilliseconds); Assert.IsTrue(result, "Expecting the task to succeed"); Assert.IsTrue(task.IsTest, "Expecting the file to be recognised as a test"); dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString()); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); }
public void AttachBW_MissingConfigFile_TaskFails() { // Arrange string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext); string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config"); DummyBuildEngine dummyEngine = new DummyBuildEngine(); TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, null /* output folder */, 0 /* returns success code */ ); // Remove the config file File.Delete(taskContext.ConfigFilePath); AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, binFolder); // Act bool result = testSubject.Execute(); // Assert Assert.IsFalse(result, "Expecting task to fail"); dummyEngine.AssertSingleMessageExists(SonarQube.MSBuild.Tasks.Resources.Shared_ConfigFileNotFound); dummyEngine.AssertNoWarnings(); AssertLogFileDoesNotExist(taskContext); }
private string ExecuteAndCheckSuccess(string projectDirectory, string primaryRuleset, params string[] rulesetsToInclude) { string mergedRulesetFileName = primaryRuleset + ".merged.txt"; DummyBuildEngine dummyEngine = new DummyBuildEngine(); MergeRuleSets task = CreateTask(dummyEngine, projectDirectory, primaryRuleset, mergedRulesetFileName, rulesetsToInclude); bool taskSucess = task.Execute(); Assert.IsTrue(taskSucess, "Expecting the task to succeed"); dummyEngine.AssertNoErrors(); dummyEngine.AssertNoWarnings(); Assert.IsTrue(File.Exists(mergedRulesetFileName), "Expecting the merged ruleset to have been created: {0}", mergedRulesetFileName); this.TestContext.AddResultFile(primaryRuleset); this.TestContext.AddResultFile(mergedRulesetFileName); return mergedRulesetFileName; }