forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathVerifier.cs
59 lines (49 loc) · 2.04 KB
/
PathVerifier.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
using System.Linq;
namespace System.IO.Abstractions.TestingHelpers
{
public class PathVerifier
{
private readonly IMockFileDataAccessor _mockFileDataAccessor;
internal PathVerifier(IMockFileDataAccessor mockFileDataAccessor)
{
if (mockFileDataAccessor == null)
{
throw new ArgumentNullException("mockFileDataAccessor");
}
_mockFileDataAccessor = mockFileDataAccessor;
}
public void IsLegalAbsoluteOrRelative(string path, string paramName)
{
if (path == null)
{
throw new ArgumentNullException(paramName, Properties.Resources.VALUE_CANNOT_BE_NULL);
}
if (path == string.Empty)
{
throw new ArgumentException("Empty file name is not legal.", paramName);
}
if (path.Trim() == string.Empty)
{
throw new ArgumentException(Properties.Resources.THE_PATH_IS_NOT_OF_A_LEGAL_FORM, paramName);
}
if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1)
{
throw new ArgumentException(Properties.Resources.ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION);
}
var filePath = ExtractFilePath(path);
if (MockPath.HasIllegalCharacters(filePath, false))
{
throw new ArgumentException(Properties.Resources.ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION);
}
}
private string ExtractFileName(string fullFileName)
{
return fullFileName.Split(_mockFileDataAccessor.Path.DirectorySeparatorChar).Last();
}
private string ExtractFilePath(string fullFileName)
{
var extractFilePath = fullFileName.Split(_mockFileDataAccessor.Path.DirectorySeparatorChar);
return string.Join(_mockFileDataAccessor.Path.DirectorySeparatorChar.ToString(), extractFilePath.Take(extractFilePath.Length - 1));
}
}
}