Skip to content

Commit fa456ff

Browse files
committed
Merge pull request #1186 from zooba/safe-testdata-copy
Makes test data copy safer.
2 parents 1c5c9ef + b858a94 commit fa456ff

File tree

1 file changed

+75
-12
lines changed

1 file changed

+75
-12
lines changed

Common/Tests/Utilities/FileUtils.cs

+75-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,77 @@
2323

2424
namespace TestUtilities {
2525
public static class FileUtils {
26+
public static IEnumerable<string> EnumerateDirectories(
27+
string root,
28+
bool recurse = true,
29+
bool fullPaths = true
30+
) {
31+
var queue = new Queue<string>();
32+
if (!root.EndsWith("\\")) {
33+
root += "\\";
34+
}
35+
queue.Enqueue(root);
36+
37+
while (queue.Any()) {
38+
var path = queue.Dequeue();
39+
if (!path.EndsWith("\\")) {
40+
path += "\\";
41+
}
42+
43+
IEnumerable<string> dirs = null;
44+
try {
45+
dirs = Directory.GetDirectories(path);
46+
} catch (UnauthorizedAccessException) {
47+
} catch (IOException) {
48+
}
49+
if (dirs == null) {
50+
continue;
51+
}
52+
53+
foreach (var d in dirs) {
54+
if (!fullPaths && !d.StartsWith(root, StringComparison.OrdinalIgnoreCase)) {
55+
continue;
56+
}
57+
if (recurse) {
58+
queue.Enqueue(d);
59+
}
60+
yield return fullPaths ? d : d.Substring(root.Length);
61+
}
62+
}
63+
}
64+
65+
public static IEnumerable<string> EnumerateFiles(
66+
string root,
67+
string pattern = "*",
68+
bool recurse = true,
69+
bool fullPaths = true
70+
) {
71+
if (!root.EndsWith("\\")) {
72+
root += "\\";
73+
}
74+
75+
foreach (var dir in Enumerable.Repeat(root, 1).Concat(EnumerateDirectories(root, recurse, fullPaths))) {
76+
var fullDir = (fullPaths || Path.IsPathRooted(dir)) ? dir : (root + dir);
77+
78+
IEnumerable<string> files = null;
79+
try {
80+
files = Directory.GetFiles(fullDir, pattern);
81+
} catch (UnauthorizedAccessException) {
82+
} catch (IOException) {
83+
}
84+
if (files == null) {
85+
continue;
86+
}
87+
88+
foreach (var f in files) {
89+
if (!fullPaths && !f.StartsWith(root, StringComparison.OrdinalIgnoreCase)) {
90+
continue;
91+
}
92+
yield return fullPaths ? f : f.Substring(root.Length);
93+
}
94+
}
95+
}
96+
2697
public static void CopyDirectory(string sourceDir, string destDir) {
2798
sourceDir = sourceDir.TrimEnd('\\');
2899
destDir = destDir.TrimEnd('\\');
@@ -31,12 +102,8 @@ public static void CopyDirectory(string sourceDir, string destDir) {
31102
} catch (IOException) {
32103
}
33104

34-
var newDirectories = new HashSet<string>(from d in Directory.EnumerateDirectories(sourceDir, "*", SearchOption.AllDirectories)
35-
where d.StartsWith(sourceDir)
36-
select d.Substring(sourceDir.Length + 1), StringComparer.OrdinalIgnoreCase);
37-
newDirectories.ExceptWith(from d in Directory.EnumerateDirectories(destDir, "*", SearchOption.AllDirectories)
38-
where d.StartsWith(destDir)
39-
select d.Substring(destDir.Length + 1));
105+
var newDirectories = new HashSet<string>(EnumerateDirectories(sourceDir, fullPaths: false), StringComparer.OrdinalIgnoreCase);
106+
newDirectories.ExceptWith(EnumerateDirectories(destDir, fullPaths: false));
40107

41108
foreach (var newDir in newDirectories.OrderBy(i => i.Length).Select(i => Path.Combine(destDir, i))) {
42109
try {
@@ -46,12 +113,8 @@ where d.StartsWith(destDir)
46113
}
47114
}
48115

49-
var newFiles = new HashSet<string>(from f in Directory.EnumerateFiles(sourceDir, "*", SearchOption.AllDirectories)
50-
where f.StartsWith(sourceDir)
51-
select f.Substring(sourceDir.Length + 1), StringComparer.OrdinalIgnoreCase);
52-
newFiles.ExceptWith(from f in Directory.EnumerateFiles(destDir, "*", SearchOption.AllDirectories)
53-
where f.StartsWith(destDir)
54-
select f.Substring(destDir.Length + 1));
116+
var newFiles = new HashSet<string>(EnumerateFiles(sourceDir, fullPaths: false), StringComparer.OrdinalIgnoreCase);
117+
newFiles.ExceptWith(EnumerateFiles(destDir, fullPaths: false));
55118

56119
foreach (var newFile in newFiles) {
57120
var copyFrom = Path.Combine(sourceDir, newFile);

0 commit comments

Comments
 (0)