Skip to content

Commit 6a229b2

Browse files
committed
Add FileSystemElement class for enumerating a directory structure.
1 parent 2c88cf9 commit 6a229b2

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using ParallelTreeWalker.Elements;
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace ParallelTreeWalker.Tests
13+
{
14+
[TestClass]
15+
public class FileSystemTests
16+
{
17+
private readonly string ROOTNAME = "TestRoot";
18+
19+
[TestMethod]
20+
public async Task TwoLevels_Balanced()
21+
{
22+
var rootDir = CreateTestStructure();
23+
24+
var allVisitedPath = await TestWalk(new FileSystemElement(rootDir, true), 5);
25+
var actual = string.Join(Environment.NewLine, allVisitedPath);
26+
27+
// expected results: enumerate manually, add root dir and sort
28+
var expected = string.Join(Environment.NewLine,
29+
Enumerable.Union(new List<string> { rootDir }, Directory.EnumerateFileSystemEntries(rootDir, "*", SearchOption.AllDirectories)).OrderBy(p => p));
30+
31+
Assert.AreEqual(expected, actual);
32+
}
33+
34+
public async Task<string[]> TestWalk(ITreeElement root, int maxParallel)
35+
{
36+
var allPaths = new ConcurrentBag<string>();
37+
38+
await TreeWalker.WalkAsync(root, new TreeWalkerOptions
39+
{
40+
MaxDegreeOfParallelism = maxParallel,
41+
ProcessElementAsync = (element) =>
42+
{
43+
var el = element as FileSystemElement;
44+
45+
Trace.WriteLine(string.Format("##PTW> {0} Path: {1}", DateTime.UtcNow.ToLongTimeString(), el.Path));
46+
47+
allPaths.Add(el.Path);
48+
49+
return Task.FromResult<object>(null);
50+
}
51+
});
52+
53+
return allPaths.OrderBy(p => p).ToArray();
54+
}
55+
56+
private string CreateTestStructure()
57+
{
58+
var currentDir = Directory.GetCurrentDirectory();
59+
var rootDir = Path.Combine(currentDir, ROOTNAME);
60+
61+
// cleanup
62+
if (Directory.Exists(rootDir))
63+
Directory.Delete(rootDir, true);
64+
65+
var subDirs = new string[]
66+
{
67+
Path.Combine(rootDir, @"F1\F11"),
68+
Path.Combine(rootDir, @"F1\F12"),
69+
Path.Combine(rootDir, @"F2\F21"),
70+
Path.Combine(rootDir, @"F2\F22"),
71+
Path.Combine(rootDir, @"F3\F31"),
72+
Path.Combine(rootDir, @"F3\F32"),
73+
};
74+
75+
foreach (var subDir in subDirs)
76+
{
77+
Directory.CreateDirectory(subDir);
78+
79+
using (var writer = File.CreateText(Path.Combine(subDir, "F1.txt")))
80+
{
81+
writer.Write("TEST - " + DateTime.UtcNow);
82+
}
83+
}
84+
85+
return rootDir;
86+
}
87+
}
88+
}

src/ParallelTreeWalker.Tests/ParallelTreeWalker.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
</Otherwise>
5151
</Choose>
5252
<ItemGroup>
53+
<Compile Include="FileSystemTests.cs" />
5354
<Compile Include="InMemoryTests.cs" />
5455
<Compile Include="Properties\AssemblyInfo.cs" />
5556
</ItemGroup>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace ParallelTreeWalker.Elements
7+
{
8+
public class FileSystemElement : ITreeElement
9+
{
10+
public string Path { get; private set; }
11+
public bool IsContainer { get; private set; }
12+
13+
public IEnumerable<ITreeElement> Children
14+
{
15+
get
16+
{
17+
// enumerate directories, than files
18+
return Directory.EnumerateDirectories(Path).Select(dir => new FileSystemElement(dir, true))
19+
.Concat(Directory.EnumerateFiles(Path).Select(file => new FileSystemElement(file, false)));
20+
}
21+
}
22+
23+
public FileSystemElement(string path, bool isContainer)
24+
{
25+
if (string.IsNullOrEmpty(path))
26+
throw new ArgumentNullException(path);
27+
28+
Path = path;
29+
IsContainer = isContainer;
30+
}
31+
}
32+
}

src/ParallelTreeWalker/ParallelTreeWalker.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Reference Include="System.Xml" />
4141
</ItemGroup>
4242
<ItemGroup>
43+
<Compile Include="Elements\FileSystemElement.cs" />
4344
<Compile Include="ITreeElement.cs" />
4445
<Compile Include="TreeWalker.cs" />
4546
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)