-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathMockDriveInfoFactory.cs
102 lines (84 loc) · 2.7 KB
/
MockDriveInfoFactory.cs
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
using System.Collections.Generic;
namespace System.IO.Abstractions.TestingHelpers;
/// <inheritdoc />
#if FEATURE_SERIALIZABLE
[Serializable]
#endif
public class MockDriveInfoFactory : IDriveInfoFactory
{
private readonly IMockFileDataAccessor mockFileSystem;
/// <inheritdoc />
public MockDriveInfoFactory(IMockFileDataAccessor mockFileSystem)
{
this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
}
/// <inheritdoc />
public IFileSystem FileSystem
=> mockFileSystem;
/// <inheritdoc />
public IDriveInfo[] GetDrives()
{
var result = new List<DriveInfoBase>();
foreach (string driveLetter in mockFileSystem.AllDrives)
{
try
{
var mockDriveInfo = new MockDriveInfo(mockFileSystem, driveLetter);
result.Add(mockDriveInfo);
}
catch (ArgumentException)
{
// invalid drives should be ignored
}
}
return result.ToArray();
}
/// <inheritdoc />
public IDriveInfo New(string driveName)
{
var drive = mockFileSystem.Path.GetPathRoot(driveName);
return new MockDriveInfo(mockFileSystem, drive);
}
/// <inheritdoc />
public IDriveInfo Wrap(DriveInfo driveInfo)
{
if (driveInfo == null)
{
return null;
}
return New(driveInfo.Name);
}
private string NormalizeDriveName(string driveName)
{
if (driveName.Length == 3 && mockFileSystem.StringOperations.EndsWith(driveName, @":\"))
{
return mockFileSystem.StringOperations.ToUpper(driveName[0]) + @":\";
}
if (mockFileSystem.StringOperations.StartsWith(driveName, @"\\"))
{
return null;
}
return driveName;
}
private class DriveEqualityComparer : IEqualityComparer<string>
{
private readonly IMockFileDataAccessor mockFileSystem;
public DriveEqualityComparer(IMockFileDataAccessor mockFileSystem)
{
this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
}
public bool Equals(string x, string y)
{
return ReferenceEquals(x, y) ||
(HasDrivePrefix(x) && HasDrivePrefix(y) && mockFileSystem.StringOperations.Equals(x[0], y[0]));
}
private static bool HasDrivePrefix(string x)
{
return x != null && x.Length >= 2 && x[1] == ':';
}
public int GetHashCode(string obj)
{
return mockFileSystem.StringOperations.ToUpper(obj).GetHashCode();
}
}
}