Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetFilePath: improve parameter handling #1779

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/Scaffolding/VS.Web.CG.Core/FilesLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,42 @@ public string GetFilePath(
string fileName,
IEnumerable<string> searchPaths)
{
if (fileName == null)
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentNullException(nameof(fileName));
}

if (searchPaths == null)
if (searchPaths?.Any() is bool any && any)
{
throw new ArgumentNullException(nameof(searchPaths));
}

foreach (var searchPath in searchPaths)
{
if (_fileSystem.DirectoryExists(searchPath))
foreach (var searchPath in searchPaths)
{
var matchingFiles = _fileSystem.EnumerateFiles(searchPath,
searchPattern: fileName,
searchOption: SearchOption.AllDirectories).ToList();

if (matchingFiles.Count > 1)
if (_fileSystem.DirectoryExists(searchPath))
{
throw new InvalidOperationException(string.Format(
MessageStrings.MultipleFilesFound,
fileName,
searchPath));
}
if (matchingFiles.Count == 1)
{
return matchingFiles.Single();
var matchingFiles = _fileSystem.EnumerateFiles(searchPath,
searchPattern: fileName,
searchOption: SearchOption.AllDirectories).ToList();

if (matchingFiles.Count > 1)
{
throw new InvalidOperationException(string.Format(
MessageStrings.MultipleFilesFound,
fileName,
searchPath));
}
if (matchingFiles.Count == 1)
{
return matchingFiles.Single();
}
}
}

throw new InvalidOperationException(string.Format(
MessageStrings.FileNotFoundInFolders,
fileName,
string.Join(";", searchPaths)));
}

throw new InvalidOperationException(string.Format(
MessageStrings.FileNotFoundInFolders,
fileName,
string.Join(";", searchPaths)));
throw new ArgumentNullException(nameof(searchPaths));
}
}
}
}