public void VerifyFindInvalidProjectReferences() { // Create the engine. MockEngine engine = new MockEngine(); FindInvalidProjectReferences t = new FindInvalidProjectReferences(); t.TargetPlatformVersion = "8.0"; t.TargetPlatformIdentifier = "Windows"; Dictionary<string, string> proj1 = new Dictionary<string, string>(); proj1["TargetPlatformMoniker"] = "Windows, Version=7.0"; Dictionary<string, string> proj2 = new Dictionary<string, string>(); proj2["TargetPlatformMoniker"] = "Windows, Version=8.0"; Dictionary<string, string> proj3 = new Dictionary<string, string>(); proj3["TargetPlatformMoniker"] = "Windows, Version=8.1"; Dictionary<string, string> proj4 = new Dictionary<string, string>(); proj4["TargetPlatformMoniker"] = "Windows, Version=8.2"; t.ProjectReferences = new TaskItem[] { new TaskItem("proj1.proj", proj1), new TaskItem("proj2.proj", proj2), new TaskItem("proj3.proj", proj3), new TaskItem("proj4.proj", proj4) }; t.BuildEngine = engine; bool succeeded = t.Execute(); Assert.True(succeeded); string warning1 = ResourceUtilities.FormatResourceString("FindInvalidProjectReferences.WarnWhenVersionIsIncompatible", "Windows", "8.0", "proj1.proj", "Windows, Version=7.0"); engine.AssertLogDoesntContain(warning1); string warning2 = ResourceUtilities.FormatResourceString("FindInvalidProjectReferences.WarnWhenVersionIsIncompatible", "Windows", "8.0", "proj2.proj", "Windows, Version=8.0"); engine.AssertLogDoesntContain(warning2); string warning3 = ResourceUtilities.FormatResourceString("FindInvalidProjectReferences.WarnWhenVersionIsIncompatible", "Windows", "8.0", "proj3.proj", "Windows, Version=8.1"); engine.AssertLogContains(warning3); string warning4 = ResourceUtilities.FormatResourceString("FindInvalidProjectReferences.WarnWhenVersionIsIncompatible", "Windows", "8.0", "proj4.proj", "Windows, Version=8.2"); engine.AssertLogContains(warning4); Assert.Equal(t.InvalidReferences.Length, 2); Assert.Equal(t.InvalidReferences[0].ItemSpec, "proj3.proj"); Assert.Equal(t.InvalidReferences[1].ItemSpec, "proj4.proj"); }
public void GetResolvedRuleSetPath_FullPath_Existent() { MockEngine mockEngine = new MockEngine(); ResolveCodeAnalysisRuleSet task = new ResolveCodeAnalysisRuleSet(); task.BuildEngine = mockEngine; string codeAnalysisRuleSet = Path.Combine(Path.GetTempPath(), @"CodeAnalysis.ruleset"); task.CodeAnalysisRuleSet = codeAnalysisRuleSet; task.MSBuildProjectDirectory = null; task.CodeAnalysisRuleSetDirectories = null; using (new TemporaryFile(codeAnalysisRuleSet, "foo")) { bool result = task.Execute(); string resolvedRuleSet = task.ResolvedCodeAnalysisRuleSet; Assert.True(result); Assert.Equal(expected: codeAnalysisRuleSet, actual: resolvedRuleSet); mockEngine.AssertLogDoesntContain("MSB3884"); } }
public void OverrideStdOutImportanceToLow() { string tempFile = FileUtilities.GetTemporaryFile(); File.WriteAllText(tempFile, @"hello world"); using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); engine.MinimumMessageImportance = MessageImportance.High; t.BuildEngine = engine; t.FullToolName = NativeMethodsShared.IsWindows ? "findstr.exe" : "grep"; t.MockCommandLineCommands = "\"hello\" \"" + tempFile + "\""; t.StandardOutputImportance = "Low"; t.Execute().ShouldBeTrue(); t.ExitCode.ShouldBe(0); engine.Errors.ShouldBe(0); engine.AssertLogDoesntContain("hello world"); } File.Delete(tempFile); }
public void ToolPath() { ResGen t = new ResGen(); string badParameterValue = @"C:\Program Files\Microsoft Visual Studio 10.0\My Fake SDK Path"; string goodParameterValue = Path.GetTempPath(); ITaskItem[] throwawayInput = { new TaskItem("hello.resx") }; // Without any inputs, the task just passes t.InputFiles = throwawayInput; Assert.Null(t.ToolPath); // "ToolPath should be null by default" ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "ResGen.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath); t.ToolPath = badParameterValue; Assert.Equal(badParameterValue, t.ToolPath); // "New ToolPath value should be set" ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "ResGen.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath); MockEngine e = new MockEngine(); t.BuildEngine = e; t.ToolPath = goodParameterValue; Assert.Equal(goodParameterValue, t.ToolPath); // "New ToolPath value should be set" bool taskPassed = t.Execute(); Assert.False(taskPassed); // "Task should still fail -- there are other things wrong with it." // but that particular error shouldn't be there anymore. string toolPathMessage = t.Log.FormatResourceString("ResGen.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath); string messageWithNoCode; string toolPathCode = t.Log.ExtractMessageCode(toolPathMessage, out messageWithNoCode); e.AssertLogDoesntContain(toolPathCode); }
public void OverrideStdOutImportanceToLow() { string tempFile = FileUtilities.GetTemporaryFile(); File.WriteAllText(tempFile, @"hello world"); using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); engine.MinimumMessageImportance = MessageImportance.High; t.BuildEngine = engine; t.FullToolName = "find.exe"; t.MockCommandLineCommands = "\"hello\" \"" + tempFile + "\""; t.StandardOutputImportance = "Low"; Assert.True(t.Execute()); Assert.Equal(0, t.ExitCode); Assert.Equal(0, engine.Errors); engine.AssertLogDoesntContain("hello world"); } File.Delete(tempFile); }
public void ToolPath() { ResGen t = new ResGen(); string badParameterValue = @"C:\Program Files\Microsoft Visual Studio 10.0\My Fake SDK Path"; string goodParameterValue = Path.GetTempPath(); ITaskItem[] throwawayInput = { new TaskItem("hello.resx") }; // Without any inputs, the task just passes t.InputFiles = throwawayInput; Assert.IsNull(t.ToolPath, "ToolPath should be null by default"); ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "ResGen.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath); t.ToolPath = badParameterValue; Assert.AreEqual(badParameterValue, t.ToolPath, "New ToolPath value should be set"); ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "ResGen.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath); MockEngine e = new MockEngine(); t.BuildEngine = e; t.ToolPath = goodParameterValue; Assert.AreEqual(goodParameterValue, t.ToolPath, "New ToolPath value should be set"); bool taskPassed = t.Execute(); Assert.IsFalse(taskPassed, "Task should still fail -- there are other things wrong with it."); // but that particular error shouldn't be there anymore. string toolPathMessage = t.Log.FormatResourceString("ResGen.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath); string messageWithNoCode; string toolPathCode = t.Log.ExtractMessageCode(toolPathMessage, out messageWithNoCode); e.AssertLogDoesntContain(toolPathCode); }
public void GetResolvedRuleSetPath_SimpleNameAndDirectories_Existent() { MockEngine mockEngine = new MockEngine(); ResolveCodeAnalysisRuleSet task = new ResolveCodeAnalysisRuleSet(); task.BuildEngine = mockEngine; string codeAnalysisRuleSet = @"CodeAnalysis.ruleset"; var directory = Path.GetTempPath(); task.CodeAnalysisRuleSet = codeAnalysisRuleSet; task.MSBuildProjectDirectory = null; task.CodeAnalysisRuleSetDirectories = new[] { directory }; string ruleSetFullPath = Path.Combine(directory, codeAnalysisRuleSet); using (new TemporaryFile(ruleSetFullPath, "foo")) { bool result = task.Execute(); string resolvedRuleSet = task.ResolvedCodeAnalysisRuleSet; Assert.Equal(expected: true, actual: result); Assert.Equal(expected: ruleSetFullPath, actual: resolvedRuleSet); mockEngine.AssertLogDoesntContain("MSB3884"); } }
public void DoNotRetryCopyWhenDestinationFileIsFolder() { string destinationFile = Path.GetTempPath(); string sourceFile = FileUtilities.GetTemporaryFile(); try { using (StreamWriter sw = new StreamWriter(sourceFile, true)) // HIGHCHAR: Test writes in UTF8 without preamble. sw.Write("This is a destination temp file."); ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) }; ITaskItem[] destinationFiles = new ITaskItem[] { new TaskItem(destinationFile) }; Copy t = new Copy(); t.RetryDelayMilliseconds = 1; // speed up tests! // Allow the task's default (false) to have a chance if (useHardLinks) { t.UseHardlinksIfPossible = useHardLinks; } MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.SourceFiles = sourceFiles; t.DestinationFiles = destinationFiles; t.SkipUnchangedFiles = true; bool result = t.Execute(); Assert.IsFalse(result); Assert.IsTrue(engine.Errors == 1); Assert.IsTrue(engine.Warnings == 0); engine.AssertLogContains("MSB3024"); engine.AssertLogDoesntContain("MSB3026"); } finally { File.Delete(sourceFile); } }
public void SuccessAfterOneRetryContinueToNextFile() { Copy t = new Copy(); t.RetryDelayMilliseconds = 1; // speed up tests! // Allow the task's default (false) to have a chance if (useHardLinks) { t.UseHardlinksIfPossible = useHardLinks; } MockEngine engine = new MockEngine(true /* log to console */); t.BuildEngine = engine; t.SourceFiles = new ITaskItem[] { new TaskItem("c:\\source"), new TaskItem("c:\\source2") }; t.DestinationFiles = new ITaskItem[] { new TaskItem("c:\\destination"), new TaskItem("c:\\destination2") }; t.Retries = 1; t.RetryDelayMilliseconds = 1; // Can't really test the delay, but at least try passing in a value CopyFunctor copyFunctor = new CopyFunctor(2, false /* do not throw on failure */); bool result = t.Execute(copyFunctor.Copy); Assert.AreEqual(true, result); engine.AssertLogContains("MSB3026"); engine.AssertLogDoesntContain("MSB3027"); Assert.AreEqual(copyFunctor.FilesCopiedSuccessfully[0].Name, "c:\\source"); Assert.AreEqual(copyFunctor.FilesCopiedSuccessfully[1].Name, "c:\\source2"); }
public void StopOnFirstFailureandBuildInParallelMultipleNode() { string project1 = ObjectModelHelpers.CreateTempFileOnDisk(@" <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'> <Target Name='msbuild'> <Error Text='Error'/> </Target> </Project> "); string project2 = ObjectModelHelpers.CreateTempFileOnDisk(@" <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'> <Target Name='msbuild'> <Message Text='SecondProject'/> </Target> </Project> "); try { ITaskItem[] projects = new ITaskItem[] { new TaskItem(project1), new TaskItem(project2) }; // Test the various combinations of BuildInParallel and StopOnFirstFailure when the msbuild task is told there are multiple nodes // running in the system for (int i = 0; i < 4; i++) { MSBuild msbuildTask = new MSBuild(); MockEngine mockEngine = new MockEngine(); mockEngine.IsRunningMultipleNodes = true; msbuildTask.BuildEngine = mockEngine; msbuildTask.Projects = projects; msbuildTask.Targets = new string[] { "msbuild" }; // Make success true as the expected resultis false bool success = true; switch (i) { case 0: // Verify setting BuildInParallel and StopOnFirstFailure to // true will not cause the msbuild task to set BuildInParallel to false during the execute msbuildTask.BuildInParallel = true; msbuildTask.StopOnFirstFailure = true; success = msbuildTask.Execute(); // Verify build did build second project which has the message SecondProject mockEngine.AssertLogContains("SecondProject"); // Verify the correct msbuild task messages are in the log Assert.IsTrue(msbuildTask.BuildInParallel, "Iteration of 0 Expected BuildInParallel to be true"); break; case 1: // Verify setting BuildInParallel to true and StopOnFirstFailure to // false will cause no change in BuildInParallel msbuildTask.BuildInParallel = true; msbuildTask.StopOnFirstFailure = false; success = msbuildTask.Execute(); // Verify build did build second project which has the message SecondProject mockEngine.AssertLogContains("SecondProject"); // Verify the correct msbuild task messages are in the log Assert.IsTrue(msbuildTask.BuildInParallel, "Iteration of 1 Expected BuildInParallel to be true"); break; case 2: // Verify setting BuildInParallel to false and StopOnFirstFailure to // true will cause no change in BuildInParallel msbuildTask.BuildInParallel = false; msbuildTask.StopOnFirstFailure = true; success = msbuildTask.Execute(); // Verify build did not build second project which has the message SecondProject mockEngine.AssertLogDoesntContain("SecondProject"); // Verify the correct msbuild task messages are in the log Assert.IsFalse(msbuildTask.BuildInParallel, "Iteration of 2 Expected BuildInParallel to be false"); break; case 3: // Verify setting BuildInParallel to false and StopOnFirstFailure to // false will cause no change in BuildInParallel msbuildTask.BuildInParallel = false; msbuildTask.StopOnFirstFailure = false; success = msbuildTask.Execute(); // Verify build did build second project which has the message SecondProject mockEngine.AssertLogContains("SecondProject"); // Verify the correct msbuild task messages are in the log Assert.IsFalse(msbuildTask.BuildInParallel, "Iteration of 3 Expected BuildInParallel to be false"); break; } // The build should fail as the first project has an error Assert.IsFalse(success, "Iteration of i " + i + " Build Succeeded. See 'Standard Out' tab for details."); } } finally { File.Delete(project1); File.Delete(project2); } }
/// <summary> /// Given a log and a resource string, acquires the text of that resource string and /// compares it to the log. Assert fails if the log contain the desired string. /// </summary> /// <param name="e">The MockEngine that contains the log we're checking</param> /// <param name="log">The TaskLoggingHelper that we use to load the string resource</param> /// <param name="errorResource">The name of the resource string to check the log for</param> /// <param name="args">Arguments needed to format the resource string properly</param> private void VerifyLogDoesNotContainResource(MockEngine e, TaskLoggingHelper log, string messageResource, params object[] args) { string message = log.FormatResourceString(messageResource, args); e.AssertLogDoesntContain(message); }
public void GetResolvedRuleSetPath_FullPath_Existent() { MockEngine mockEngine = new MockEngine(); ResolveCodeAnalysisRuleSet task = new ResolveCodeAnalysisRuleSet(); task.BuildEngine = mockEngine; string codeAnalysisRuleSet = Path.Combine(Path.GetTempPath(), @"CodeAnalysis.ruleset"); task.CodeAnalysisRuleSet = codeAnalysisRuleSet; task.MSBuildProjectDirectory = null; task.CodeAnalysisRuleSetDirectories = null; using (new TemporaryFile(codeAnalysisRuleSet, "foo")) { bool result = task.Execute(); string resolvedRuleSet = task.ResolvedCodeAnalysisRuleSet; Assert.AreEqual(expected: true, actual: result); Assert.AreEqual(expected: codeAnalysisRuleSet, actual: resolvedRuleSet); mockEngine.AssertLogDoesntContain("MSB3884"); } }
public void GetResolvedRuleSetPath_Null() { MockEngine mockEngine = new MockEngine(); ResolveCodeAnalysisRuleSet task = new ResolveCodeAnalysisRuleSet(); task.BuildEngine = mockEngine; task.CodeAnalysisRuleSet = null; task.MSBuildProjectDirectory = null; task.CodeAnalysisRuleSetDirectories = null; bool result = task.Execute(); string resolvedRuleSet = task.ResolvedCodeAnalysisRuleSet; Assert.AreEqual(expected: true, actual: result); Assert.AreEqual(expected: null, actual: resolvedRuleSet); mockEngine.AssertLogDoesntContain("MSB3884"); }
public void GetResolvedRuleSetPath_RelativePath_WithProject_Existent() { MockEngine mockEngine = new MockEngine(); ResolveCodeAnalysisRuleSet task = new ResolveCodeAnalysisRuleSet(); task.BuildEngine = mockEngine; string subdirectoryName = Path.GetRandomFileName(); string codeAnalysisRuleSet = Path.Combine(subdirectoryName, "CodeAnalysis.ruleset"); string projectDirectory = Path.GetTempPath(); task.CodeAnalysisRuleSet = codeAnalysisRuleSet; task.MSBuildProjectDirectory = projectDirectory; task.CodeAnalysisRuleSetDirectories = null; string ruleSetFullPath = Path.Combine(projectDirectory, codeAnalysisRuleSet); using (new TemporaryDirectory(Path.GetDirectoryName(ruleSetFullPath))) using (new TemporaryFile(ruleSetFullPath, "foo")) { bool result = task.Execute(); string resolvedRuleSet = task.ResolvedCodeAnalysisRuleSet; Assert.AreEqual(expected: true, actual: result); Assert.AreEqual(expected: codeAnalysisRuleSet, actual: resolvedRuleSet); mockEngine.AssertLogDoesntContain("MSB3884"); } }
public void TestLogFromException() { string message = "exception message"; string stackTrace = "TaskLoggingHelperTests.TestLogFromException"; MockEngine engine = new MockEngine(); MockTask task = new MockTask(); task.BuildEngine = engine; // need to throw and catch an exception so that its stack trace is initialized to something try { Exception inner = new InvalidOperationException(); throw new Exception(message, inner); } catch (Exception e) { // log error without stack trace task.Log.LogErrorFromException(e); engine.AssertLogContains(message); engine.AssertLogDoesntContain(stackTrace); engine.AssertLogDoesntContain("InvalidOperationException"); engine.Log = string.Empty; // log warning with stack trace task.Log.LogWarningFromException(e); engine.AssertLogContains(message); engine.AssertLogDoesntContain(stackTrace); engine.Log = string.Empty; // log error with stack trace task.Log.LogErrorFromException(e, true); engine.AssertLogContains(message); engine.AssertLogContains(stackTrace); engine.AssertLogDoesntContain("InvalidOperationException"); engine.Log = string.Empty; // log warning with stack trace task.Log.LogWarningFromException(e, true); engine.AssertLogContains(message); engine.AssertLogContains(stackTrace); engine.Log = string.Empty; // log error with stack trace and inner exceptions task.Log.LogErrorFromException(e, true, true, "foo.cs"); engine.AssertLogContains(message); engine.AssertLogContains(stackTrace); engine.AssertLogContains("InvalidOperationException"); } }
public void DoNotRetryCopyWhenDestinationFolderIsFile() { string destinationFile = FileUtilities.GetTemporaryFile(); string sourceFile = FileUtilities.GetTemporaryFile(); try { using (StreamWriter sw = new StreamWriter(sourceFile, true)) sw.Write("This is a destination temp file."); ITaskItem[] sourceFiles = new ITaskItem[] { new TaskItem(sourceFile) }; Copy t = new Copy(); t.RetryDelayMilliseconds = 1; // speed up tests! // Allow the task's default (false) to have a chance if (useHardLinks) { t.UseHardlinksIfPossible = useHardLinks; } MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.SourceFiles = sourceFiles; t.DestinationFolder = new TaskItem(destinationFile); t.SkipUnchangedFiles = true; bool result = t.Execute(); Assert.False(result); engine.AssertLogContains("MSB3021"); // copy failed engine.AssertLogDoesntContain("MSB3026"); // Didn't retry Assert.Equal(1, engine.Errors); Assert.Equal(0, engine.Warnings); } finally { File.Delete(sourceFile); } }
public void TargetStopOnFirstFailureBuildInParallel() { string project1 = ObjectModelHelpers.CreateTempFileOnDisk(@" <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'> <Target Name='T1'> <Message Text='Proj2 T1 message'/> </Target> <Target Name='T2'> <Message Text='Proj2 T2 message'/> </Target> <Target Name='T3'> <Error Text='Error'/> </Target> </Project> "); try { ITaskItem[] projects = new ITaskItem[] { new TaskItem(project1) }; for (int i = 0; i < 6; i++) { // Test the case where the error is in the last target MSBuild msbuildTask = new MSBuild(); MockEngine mockEngine = new MockEngine(); msbuildTask.BuildEngine = mockEngine; msbuildTask.Projects = projects; // Set to true as the expected result is false bool success = true; switch (i) { case 0: // Test the case where the error is in the last project and RunEachTargetSeparately = true msbuildTask.StopOnFirstFailure = true; msbuildTask.RunEachTargetSeparately = true; msbuildTask.Targets = new string[] { "T1", "T2", "T3" }; success = msbuildTask.Execute(); mockEngine.AssertLogContains("Proj2 T1 message"); mockEngine.AssertLogContains("Proj2 T2 message"); break; case 1: // Test the case where the error is in the second target out of 3. msbuildTask.StopOnFirstFailure = true; msbuildTask.RunEachTargetSeparately = true; msbuildTask.Targets = new string[] { "T1", "T3", "T2" }; success = msbuildTask.Execute(); mockEngine.AssertLogContains("Proj2 T1 message"); mockEngine.AssertLogDoesntContain("Proj2 T2 message"); // The build should fail as the first project has an error break; case 2: // Test case where error is in second last target but stopOnFirstFailure is false msbuildTask.RunEachTargetSeparately = true; msbuildTask.StopOnFirstFailure = false; msbuildTask.Targets = new string[] { "T1", "T3", "T2" }; success = msbuildTask.Execute(); mockEngine.AssertLogContains("Proj2 T1 message"); mockEngine.AssertLogContains("Proj2 T2 message"); break; // Test the cases where RunEachTargetSeparately is false. In these cases all of the targets should be submitted at once case 3: // Test the case where the error is in the last project and RunEachTargetSeparately = true msbuildTask.StopOnFirstFailure = true; msbuildTask.Targets = new string[] { "T1", "T2", "T3" }; success = msbuildTask.Execute(); mockEngine.AssertLogContains("Proj2 T1 message"); mockEngine.AssertLogContains("Proj2 T2 message"); // The build should fail as the first project has an error break; case 4: // Test the case where the error is in the second target out of 3. msbuildTask.StopOnFirstFailure = true; msbuildTask.Targets = new string[] { "T1", "T3", "T2" }; success = msbuildTask.Execute(); mockEngine.AssertLogContains("Proj2 T1 message"); mockEngine.AssertLogDoesntContain("Proj2 T2 message"); // The build should fail as the first project has an error break; case 5: // Test case where error is in second last target but stopOnFirstFailure is false msbuildTask.StopOnFirstFailure = false; msbuildTask.Targets = new string[] { "T1", "T3", "T2" }; success = msbuildTask.Execute(); mockEngine.AssertLogContains("Proj2 T1 message"); mockEngine.AssertLogDoesntContain("Proj2 T2 message"); break; } // The build should fail as the first project has an error Assert.IsFalse(success, "Iteration of i:" + i + "Build Succeeded. See 'Standard Out' tab for details."); } } finally { File.Delete(project1); } }
public void ErrorFromResourcesWithInvalidArguments() { MockEngine e = new MockEngine(true); ErrorFromResources err = new ErrorFromResources(); err.BuildEngine = e; err.Resource = "Copy.Error"; err.Arguments = new string[] { "a.txt", "b.txt" }; bool retval = err.Execute(); Console.WriteLine("==="); Console.WriteLine(e.Log); Console.WriteLine("==="); Assert.IsFalse(retval); e.AssertLogDoesntContain("a.txt"); e.AssertLogContains("MSB3861"); Assert.IsTrue(e.Errors == 1); }
public void BuildProjectFileWithNullGlobalProperties ( ) { string[] targets; MockEngine myEngine = new MockEngine(); string projectFile = CreateGlobalPropertyProjectFile(); try { targets = new string[1]; targets[0] = "Build"; bool result = myEngine.BuildProjectFile(projectFile, targets, null); myEngine.AssertLogDoesntContain("SomePropertyText"); Assert.IsTrue(result); } finally { myEngine.UnregisterAllLoggers(); myEngine.UnloadAllProjects(); File.Delete(projectFile); } }
public void HandleExecutionErrorsWhenToolLogsError() { using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.MockCommandLineCommands = "/C echo Main.cs(17,20): error CS0168: The variable 'foo' is declared but never used"; Assert.IsFalse(t.Execute()); // The above command logged a canonical error message. Therefore ToolTask should // not log its own error beyond that. engine.AssertLogDoesntContain("MSB6006"); engine.AssertLogContains("CS0168"); engine.AssertLogContains("The variable 'foo' is declared but never used"); Assert.AreEqual(-1, t.ExitCode); Assert.AreEqual(1, engine.Errors); } }
public void FailureWithNoRetries() { Copy t = new Copy(); t.RetryDelayMilliseconds = 1; // speed up tests! // Allow the task's default (false) to have a chance if (useHardLinks) { t.UseHardlinksIfPossible = useHardLinks; } MockEngine engine = new MockEngine(true /* log to console */); t.BuildEngine = engine; t.SourceFiles = new ITaskItem[] { new TaskItem("c:\\source") }; t.DestinationFiles = new ITaskItem[] { new TaskItem("c:\\destination") }; t.Retries = 0; CopyFunctor copyFunctor = new CopyFunctor(2, false /* do not throw on failure */); bool result = t.Execute(copyFunctor.Copy); Assert.AreEqual(false, result); engine.AssertLogDoesntContain("MSB3026"); engine.AssertLogDoesntContain("MSB3027"); }
public void ErrorWhenTextSentToStandardError() { using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.LogStandardErrorAsError = true; t.MockCommandLineCommands = "/C Echo 'Who made you king anyways' 1>&2"; Assert.IsFalse(t.Execute()); engine.AssertLogDoesntContain("MSB3073"); engine.AssertLogContains("Who made you king anyways"); Assert.AreEqual(-1, t.ExitCode); Assert.AreEqual(1, engine.Errors); } }
public void DoNotRetryWhenDestinationLockedDueToAcl() { string tempDirectory = Path.Combine(Path.GetTempPath(), "DoNotRetryWhenDestinationLockedDueToAcl"); string destinationFile = Path.Combine(tempDirectory, "DestinationFile.txt"); string sourceFile = Path.Combine(tempDirectory, "SourceFile.txt"); if (Directory.Exists(tempDirectory)) { FileUtilities.DeleteDirectoryNoThrow(tempDirectory, true); } Directory.CreateDirectory(tempDirectory); File.WriteAllText(destinationFile, "Destination"); File.WriteAllText(sourceFile, "SourceFile"); string userAccount = string.Format(@"{0}\{1}", System.Environment.UserDomainName, System.Environment.UserName); FileSystemAccessRule denyFile = new FileSystemAccessRule(userAccount, FileSystemRights.Write | FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.WriteData, AccessControlType.Deny); FileSystemAccessRule denyDirectory = new FileSystemAccessRule(userAccount, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Deny); FileSecurity fSecurity = File.GetAccessControl(destinationFile); DirectorySecurity dSecurity = Directory.GetAccessControl(tempDirectory); try { fSecurity.AddAccessRule(denyFile); File.SetAccessControl(destinationFile, fSecurity); dSecurity.AddAccessRule(denyDirectory); Directory.SetAccessControl(tempDirectory, dSecurity); Copy t = new Copy(); t.RetryDelayMilliseconds = 1; // speed up tests! // Allow the task's default (false) to have a chance if (useHardLinks) { t.UseHardlinksIfPossible = useHardLinks; } MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.SourceFiles = new TaskItem[] { new TaskItem(sourceFile) }; t.DestinationFiles = new TaskItem[] { new TaskItem(destinationFile) }; bool result = t.Execute(); Assert.IsFalse(result); engine.AssertLogContains("MSB3021"); // copy failed engine.AssertLogDoesntContain("MSB3026"); // Didn't retry Assert.IsTrue(engine.Errors == 1); Assert.IsTrue(engine.Warnings == 0); } finally { fSecurity.RemoveAccessRule(denyFile); File.SetAccessControl(destinationFile, fSecurity); dSecurity.RemoveAccessRule(denyDirectory); Directory.SetAccessControl(tempDirectory, dSecurity); if (Directory.Exists(tempDirectory)) { FileUtilities.DeleteDirectoryNoThrow(tempDirectory, true); } } }
public void ToolExeIsFoundOnToolPath() { using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.FullToolName = "cmd.exe"; string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); t.ToolPath = systemPath; t.Execute(); Assert.AreEqual(Path.Combine(systemPath, "cmd.exe"), t.PathToToolUsed); engine.AssertLogContains("cmd.exe"); engine.Log = String.Empty; t.ToolExe = "xcopy.exe"; t.Execute(); Assert.AreEqual(Path.Combine(systemPath, "xcopy.exe"), t.PathToToolUsed); engine.AssertLogContains("xcopy.exe"); engine.AssertLogDoesntContain("cmd.exe"); } }
public void OverrideStdOutImportanceToLow() { string tempFile = FileUtilities.GetTemporaryFile(); File.WriteAllText(tempFile, @"hello world"); using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); engine.MinimumMessageImportance = MessageImportance.High; t.BuildEngine = engine; t.FullToolName = "find.exe"; t.MockCommandLineCommands = "\"hello\" \"" + tempFile + "\""; t.StandardOutputImportance = "Low"; Assert.IsTrue(t.Execute()); Assert.AreEqual(0, t.ExitCode); Assert.AreEqual(0, engine.Errors); engine.AssertLogDoesntContain("hello world"); } File.Delete(tempFile); }
public void DoNotErrorWhenTextSentToStandardOutput() { using (MyTool t = new MyTool()) { MockEngine engine = new MockEngine(); t.BuildEngine = engine; t.LogStandardErrorAsError = true; t.MockCommandLineCommands = "/C Echo 'Who made you king anyways'"; Assert.True(t.Execute()); engine.AssertLogDoesntContain("MSB"); engine.AssertLogContains("Who made you king anyways"); Assert.Equal(0, t.ExitCode); Assert.Equal(0, engine.Errors); } }