-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f74cf8f
commit abb9496
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Roslynator.Testing; | ||
|
||
internal static class FilePathVerifier | ||
{ | ||
private static readonly char[] _invalidPathChars = Path.GetInvalidPathChars(); | ||
private static readonly char[] _invalidFileNameChars = Path.GetInvalidFileNameChars(); | ||
|
||
public static void VerifyFilePath(string? path) | ||
{ | ||
if (path is null) | ||
return; | ||
|
||
if (string.IsNullOrWhiteSpace(path)) | ||
throw new ArgumentException("File path cannot be empty or contain only whitespace", nameof(path)); | ||
|
||
if (path.IndexOfAny(_invalidPathChars) >= 0) | ||
throw new ArgumentException("File path contains invalid character(s)", nameof(path)); | ||
|
||
if (path[path.Length - 1] == Path.DirectorySeparatorChar | ||
|| path[path.Length - 1] == Path.AltDirectorySeparatorChar) | ||
{ | ||
throw new ArgumentException("File path cannot end with directory separator", nameof(path)); | ||
} | ||
|
||
var dirSeparatorIndex = FileSystemHelpers.LastIndexOfDirectorySeparator(path); | ||
if (dirSeparatorIndex >= 0) | ||
{ | ||
for (int i = dirSeparatorIndex + 1; i < path.Length; i++) | ||
{ | ||
if (_invalidFileNameChars.Contains(path[i])) | ||
throw new ArgumentException("File name contains invalid character(s)", nameof(path)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters