-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathSafeFileEnumerableTests.cs
More file actions
163 lines (125 loc) · 5.95 KB
/
Copy pathSafeFileEnumerableTests.cs
File metadata and controls
163 lines (125 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
namespace Microsoft.ComponentDetection.Common.Tests;
using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.TestsUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
[TestCategory("Governance/All")]
[TestCategory("Governance/ComponentDetection")]
public class SafeFileEnumerableTests
{
private Mock<ILogger> loggerMock;
private Mock<IPathUtilityService> pathUtilityServiceMock;
private string temporaryDirectory;
[TestInitialize]
public void TestInitialize()
{
this.loggerMock = new Mock<ILogger>();
this.pathUtilityServiceMock = new Mock<IPathUtilityService>();
this.temporaryDirectory = this.GetTemporaryDirectory();
}
[TestCleanup]
public void TestCleanup()
{
this.CleanupTemporaryDirectory(this.temporaryDirectory);
}
[TestMethod]
public void GetEnumerator_WorksOverExpectedFiles()
{
var subDir = Directory.CreateDirectory(Path.Combine(this.temporaryDirectory, "SubDir"));
var name = string.Format("{0}.txt", Guid.NewGuid());
var file0 = Path.Combine(this.temporaryDirectory, name);
var subFile0 = Path.Combine(this.temporaryDirectory, "SubDir", name);
File.Create(file0).Close();
File.Create(subFile0).Close();
IEnumerable<string> searchPatterns = [name];
this.pathUtilityServiceMock.Setup(x => x.ResolvePhysicalPath(It.IsAny<string>())).Returns<string>((s) => s);
this.pathUtilityServiceMock.Setup(x => x.MatchesPattern(name, name)).Returns(true);
var enumerable = new SafeFileEnumerable(new DirectoryInfo(this.temporaryDirectory), searchPatterns, this.loggerMock.Object, this.pathUtilityServiceMock.Object, (directoryName, span) => false, true);
var filesFound = 0;
foreach (var file in enumerable)
{
file.File.FullName.Should().BeOneOf(file0, subFile0);
filesFound++;
}
filesFound.Should().Be(2);
}
[TestMethod]
public void GetEnumerator_IgnoresSubDirectories()
{
var subDir = Directory.CreateDirectory(Path.Combine(this.temporaryDirectory, "SubDir"));
var name = string.Format("{0}.txt", Guid.NewGuid());
var file0 = Path.Combine(this.temporaryDirectory, name);
File.Create(file0).Close();
File.Create(Path.Combine(this.temporaryDirectory, "SubDir", name)).Close();
IEnumerable<string> searchPatterns = [name];
this.pathUtilityServiceMock.Setup(x => x.MatchesPattern(name, name)).Returns(true);
var enumerable = new SafeFileEnumerable(new DirectoryInfo(this.temporaryDirectory), searchPatterns, this.loggerMock.Object, this.pathUtilityServiceMock.Object, (directoryName, span) => false, false);
var filesFound = 0;
foreach (var file in enumerable)
{
file.File.FullName.Should().BeOneOf(file0);
filesFound++;
}
filesFound.Should().Be(1);
}
[SkipTestOnWindows]
public void GetEnumerator_CallsSymlinkCode()
{
var subDir = Directory.CreateSymbolicLink(Path.Combine(this.temporaryDirectory, "SubDir"), this.temporaryDirectory);
var name = string.Format("{0}.txt", Guid.NewGuid());
File.Create(Path.Combine(this.temporaryDirectory, name)).Close();
File.Create(Path.Combine(this.temporaryDirectory, "SubDir", name)).Close();
IEnumerable<string> searchPatterns = [name];
this.pathUtilityServiceMock.Setup(x => x.ResolvePhysicalPath(subDir.FullName)).Returns(subDir.FullName);
var enumerable = new SafeFileEnumerable(new DirectoryInfo(this.temporaryDirectory), searchPatterns, this.loggerMock.Object, this.pathUtilityServiceMock.Object, (directoryName, span) => false, true);
foreach (var file in enumerable)
{
}
this.pathUtilityServiceMock.Verify(x => x.ResolvePhysicalPath(subDir.FullName), Times.AtLeastOnce);
}
[SkipTestOnWindows]
public void GetEnumerator_DuplicatePathIgnored()
{
var subDir = Directory.CreateDirectory(Path.Combine(this.temporaryDirectory, "SubDir"));
var symlink = Path.Combine(this.temporaryDirectory, "FakeSymlink");
Directory.CreateSymbolicLink(symlink, subDir.FullName);
var name = $"{Guid.NewGuid()}.txt";
var canary = $"{Guid.NewGuid()}.txt";
File.Create(Path.Combine(this.temporaryDirectory, name)).Close();
File.Create(Path.Combine(this.temporaryDirectory, "SubDir", name)).Close();
File.Create(Path.Combine(this.temporaryDirectory, "FakeSymlink", canary)).Close();
this.pathUtilityServiceMock.Setup(x => x.ResolvePhysicalPath(this.temporaryDirectory)).Returns(this.temporaryDirectory);
this.pathUtilityServiceMock.Setup(x => x.ResolvePhysicalPath(subDir.FullName)).Returns(subDir.FullName);
this.pathUtilityServiceMock.Setup(x => x.ResolvePhysicalPath(symlink)).Returns(subDir.FullName);
IEnumerable<string> searchPatterns = [name];
var enumerable = new SafeFileEnumerable(new DirectoryInfo(this.temporaryDirectory), searchPatterns, this.loggerMock.Object, this.pathUtilityServiceMock.Object, (directoryName, span) => false, true);
foreach (var file in enumerable)
{
file.File.FullName.Should().NotBe(Path.Combine(this.temporaryDirectory, "FakeSymlink", canary));
}
this.pathUtilityServiceMock.Verify(x => x.ResolvePhysicalPath(symlink), Times.AtLeastOnce);
}
private string GetTemporaryDirectory()
{
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
return tempDirectory;
}
private void CleanupTemporaryDirectory(string directory)
{
try
{
Directory.Delete(directory, true);
}
catch
{
// Swallow
}
}
}