forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockDriveInfoFactory.cs
More file actions
115 lines (96 loc) · 3.47 KB
/
MockDriveInfoFactory.cs
File metadata and controls
115 lines (96 loc) · 3.47 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
using System.Collections.Generic;
namespace System.IO.Abstractions.TestingHelpers
{
/// <inheritdoc />
[Serializable]
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 driveLetters = new HashSet<string>(new DriveEqualityComparer(mockFileSystem));
foreach (var path in mockFileSystem.AllPaths)
{
var pathRoot = mockFileSystem.Path.GetPathRoot(path);
driveLetters.Add(pathRoot);
}
var result = new List<DriveInfoBase>();
foreach (string driveLetter in driveLetters)
{
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);
}
/// <inheritdoc />
[Obsolete("Use `IDirectoryInfoFactory.New(string)` instead")]
public IDriveInfo FromDriveName(string driveName)
{
return New(driveName);
}
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();
}
}
}
}