Skip to content

Commit

Permalink
Merge pull request #287 from dsidirop/master
Browse files Browse the repository at this point in the history
Enhance DeleteTree with provisions for retrying on set intervals #286
  • Loading branch information
pwelter34 authored Aug 23, 2019
2 parents c0b5052 + c28d79d commit 0c76dd6
Show file tree
Hide file tree
Showing 6 changed files with 473 additions and 225 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Test
*.chm
/Tools
/build.txt
.vs
166 changes: 133 additions & 33 deletions Source/MSBuild.Community.Tasks.Tests/DeleteTreeTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NUnit.Framework;
using Task = System.Threading.Tasks.Task;

namespace MSBuild.Community.Tasks.Tests
{
Expand All @@ -11,19 +15,25 @@ public class DeleteTreeTest
[Test]
public void GetDirectories()
{
string root = Path.GetFullPath(@"..\..\..\");
var root = Path.GetFullPath(@"..\..\..\");

string[] directories = Directory.GetDirectories(root, "*", SearchOption.AllDirectories);
var _ = Directory.GetDirectories(root, "*", SearchOption.AllDirectories);
}

[Test]
[Explicit]
public void ExecuteInnerAndTrailingRecursive()
{
DeleteTree task = new DeleteTree();
task.BuildEngine = new MockBuild();
task.Directories = new ITaskItem[] { new TaskItem(@"..\..\..\**\obj\**")};
bool result = task.Execute();
var task = new DeleteTree
{
BuildEngine = new MockBuild(),
Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\**\obj\**")
}
};

var result = task.Execute();

Assert.IsTrue(result);
}
Expand All @@ -32,10 +42,16 @@ public void ExecuteInnerAndTrailingRecursive()
[Explicit]
public void ExecuteInnerRecursive()
{
DeleteTree task = new DeleteTree();
task.BuildEngine = new MockBuild();
task.Directories = new ITaskItem[] { new TaskItem(@"..\..\..\**\obj") };
bool result = task.Execute();
var task = new DeleteTree
{
BuildEngine = new MockBuild(),
Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\**\obj")
}
};

var result = task.Execute();

Assert.IsTrue(result);
}
Expand All @@ -44,10 +60,16 @@ public void ExecuteInnerRecursive()
[Explicit]
public void ExecuteItemWithTrailingSeparator()
{
DeleteTree task = new DeleteTree();
task.BuildEngine = new MockBuild();
task.Directories = new ITaskItem[] { new TaskItem(@"..\..\..\**\obj\") };
bool result = task.Execute();
var task = new DeleteTree
{
BuildEngine = new MockBuild(),
Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\**\obj\")
}
};

var result = task.Execute();

Assert.IsTrue(result);
}
Expand All @@ -56,15 +78,17 @@ public void ExecuteItemWithTrailingSeparator()
[Explicit]
public void ExecuteMultipleItems()
{
DeleteTree task = new DeleteTree();
task.BuildEngine = new MockBuild();
task.Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\**\obj"),
new TaskItem(@"..\..\..\**\bin")
};

bool result = task.Execute();
var task = new DeleteTree
{
BuildEngine = new MockBuild(),
Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\**\obj"),
new TaskItem(@"..\..\..\**\bin")
}
};

var result = task.Execute();

Assert.IsTrue(result);
}
Expand All @@ -73,15 +97,17 @@ public void ExecuteMultipleItems()
[Explicit]
public void ExecuteWildCard()
{
DeleteTree task = new DeleteTree();
task.BuildEngine = new MockBuild();
task.Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\MSBuild.Community.*\**\obj"),
new TaskItem(@"..\..\..\**\b?n")
};

bool result = task.Execute();
var task = new DeleteTree
{
BuildEngine = new MockBuild(),
Directories = new ITaskItem[]
{
new TaskItem(@"..\..\..\MSBuild.Community.*\**\obj"),
new TaskItem(@"..\..\..\**\b?n")
}
};

var result = task.Execute();

Assert.IsTrue(result);
}
Expand All @@ -90,20 +116,23 @@ public void ExecuteWildCard()
public void MatchDirectoriesNoWildCards()
{
var path = DeleteTree.MatchDirectories(@"..\..\..");

Assert.AreEqual(1, path.Count);
}

[Test]
public void MatchDirectoriesInnerRelative()
{
var path = DeleteTree.MatchDirectories(@"..\..\..\**\obj");

Assert.Greater(path.Count, 0);
}

[Test]
public void MatchDirectoriesInnerAndOuterRelative()
{
var path = DeleteTree.MatchDirectories(@"..\..\..\**\obj\**");

Assert.Greater(path.Count, 0);
}

Expand All @@ -112,6 +141,7 @@ public void MatchDirectoriesInnerAndOuterRelative()
public void MatchDirectoriesRelative()
{
var path = DeleteTree.MatchDirectories(@"..\..\..\**");

Assert.Greater(path.Count, 0);
}

Expand All @@ -125,6 +155,76 @@ public void MatchDirectoriesRelativeWildCard()
Assert.Greater(path.Count, 0);
}


[Test]
[Explicit]
public void DeleteWithRetriesDirectoryWhichAppearsWithDelay_ShouldSucceed()
{
//Arrange
const string fullTargetDirectoryPath = @"C:\Temp\xn_TestDirectoryWhichDoesntInitiallyExist";

var task = new DeleteTree
{
BuildEngine = new MockBuild(),

Retries = 10,
RetryDelayMilliseconds = 300,

Directories = new ITaskItem[]
{
new TaskItem(fullTargetDirectoryPath)
}
};

//Act
new Task(() =>
{
Thread.Sleep(2000);

Directory.CreateDirectory(fullTargetDirectoryPath);
}).Start();
var result = task.Execute();

//Assert
Assert.IsTrue(result);
Assert.AreEqual(1, task.DeletedDirectories.Length);
Assert.AreEqual(fullTargetDirectoryPath, task.DeletedDirectories.FirstOrDefault().ItemSpec);
}

[Test]
[Explicit]
public void DeleteWithRetriesDirectoryWhichDoesntAppearsAtAll_ShouldFailWithException()
{
//Arrange
const string fullTargetDirectoryPath = @"C:\Temp\xn_TestDirectoryWhichDoesntInitiallyExist";

var task = new DeleteTree
{
BuildEngine = new MockBuild(),

Retries = 10,
RetryDelayMilliseconds = 300,

Directories = new ITaskItem[]
{
new TaskItem(fullTargetDirectoryPath)
}
};

var exception = (Exception) null;

//Act
try
{
task.Execute();
}
catch (Exception ex)
{
exception = ex;
}

//Assert
Assert.IsInstanceOf<DirectoryNotFoundException>(exception);
}
}
}

2 changes: 0 additions & 2 deletions Source/MSBuild.Community.Tasks.Tests/MockBuild.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


using System;
using System.Collections;
using System.Text;
Expand Down
Loading

0 comments on commit 0c76dd6

Please sign in to comment.