Skip to content

Uri bug fix #542

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

Merged
merged 4 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/code/RegisterPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ protected override void ProcessRecord()
switch (ParameterSetName)
{
case NameParameterSet:
if (!Utils.TryCreateValidUrl(urlString: URL,
if (!Utils.TryCreateValidUrl(uriString: URL,
cmdletPassedIn: this,
urlResult: out _url,
uriResult: out _url,
errorRecord: out ErrorRecord errorRecord))
{
ThrowTerminatingError(errorRecord);
Expand Down Expand Up @@ -312,9 +312,9 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
return null;
}

if (!Utils.TryCreateValidUrl(urlString: repo["Url"].ToString(),
if (!Utils.TryCreateValidUrl(uriString: repo["Url"].ToString(),
cmdletPassedIn: this,
urlResult: out Uri repoURL,
uriResult: out Uri repoURL,
errorRecord: out ErrorRecord errorRecord))
{
WriteError(errorRecord);
Expand Down
4 changes: 2 additions & 2 deletions src/code/SetPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
return null;
}

if (!Utils.TryCreateValidUrl(urlString: repo["Url"].ToString(),
if (!Utils.TryCreateValidUrl(uriString: repo["Url"].ToString(),
cmdletPassedIn: this,
urlResult: out repoURL,
uriResult: out repoURL,
errorRecord: out ErrorRecord errorRecord))
{
WriteError(errorRecord);
Expand Down
56 changes: 26 additions & 30 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static string[] ProcessNameWildcards(
}

#endregion

#region Version methods

public static string GetNormalizedVersionString(
Expand Down Expand Up @@ -228,48 +228,44 @@ public static bool GetVersionForInstallPath(
#region Url methods

public static bool TryCreateValidUrl(
string urlString,
string uriString,
PSCmdlet cmdletPassedIn,
out Uri urlResult,
out Uri uriResult,
out ErrorRecord errorRecord
)
{
errorRecord = null;

if (!urlString.StartsWith(Uri.UriSchemeHttps) &&
!urlString.StartsWith(Uri.UriSchemeHttp) &&
!urlString.StartsWith(Uri.UriSchemeFtp))
if (Uri.TryCreate(uriString, UriKind.Absolute, out uriResult))
{
// url string could be of type (potentially) UriSchemeFile or invalid type
// can't check for UriSchemeFile because relative paths don't qualify as UriSchemeFile
try
{
// this is needed for a relative path urlstring. Does not throw error for an absolute path
urlString = cmdletPassedIn.SessionState.Path.GetResolvedPSPathFromPSPath(urlString)[0].Path;
return true;
}

}
catch (Exception)
Exception ex;
try
{
// This is needed for a relative path urlstring. Does not throw error for an absolute path
var filePath = cmdletPassedIn.SessionState.Path.GetResolvedPSPathFromPSPath(uriString)[0].Path;
if (Uri.TryCreate(filePath, UriKind.Absolute, out uriResult))
{
// this should only be reached if the url string is invalid
// i.e www.google.com
var message = string.Format(CultureInfo.InvariantCulture, "The URL provided is not valid: {0} and must be of Uri Scheme: HTTP, HTTPS, FTP or File", urlString);
var ex = new ArgumentException(message);
errorRecord = new ErrorRecord(ex, "InvalidUrl", ErrorCategory.InvalidArgument, null);
urlResult = null;
return false;
return true;
}
}

bool tryCreateResult = Uri.TryCreate(urlString, UriKind.Absolute, out urlResult);
if (!tryCreateResult)
ex = new PSArgumentException($"Invalid Uri file path: {uriString}");
}
catch (Exception e)
{
var message = string.Format(CultureInfo.InvariantCulture, "The URL provided is not valid: {0}", urlString);
var ex = new ArgumentException(message);
errorRecord = new ErrorRecord(ex, "InvalidUrl", ErrorCategory.InvalidArgument, null);
urlResult = null;
ex = e;
}

return tryCreateResult;
errorRecord = new ErrorRecord(
new PSArgumentException(
$"The provided Uri is not valid: {uriString}. It must be of Uri Scheme: HTTP, HTTPS, FTP or a file path",
ex),
"InvalidUri",
ErrorCategory.InvalidArgument,
cmdletPassedIn);

return false;
}

#endregion
Expand Down
10 changes: 9 additions & 1 deletion test/RegisterPSResourceRepository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Describe "Test Register-PSResourceRepository" {
$testCases2 = @{Type = "-Name is not specified"; IncorrectHashTable = @{URL = $tmpDir1Path}; ErrorId = "NullNameForRepositoriesParameterSetRegistration,Microsoft.PowerShell.PowerShellGet.Cmdlets.RegisterPSResourceRepository"},
@{Type = "-Name is PSGallery"; IncorrectHashTable = @{Name = "PSGallery"; URL = $tmpDir1Path}; ErrorId = "PSGalleryProvidedAsNameRepoPSet,Microsoft.PowerShell.PowerShellGet.Cmdlets.RegisterPSResourceRepository"},
@{Type = "-URL not specified"; IncorrectHashTable = @{Name = "testRepository"}; ErrorId = "NullURLForRepositoriesParameterSetRegistration,Microsoft.PowerShell.PowerShellGet.Cmdlets.RegisterPSResourceRepository"},
@{Type = "-URL is not valid scheme"; IncorrectHashTable = @{Name = "testRepository"; URL="www.google.com"}; ErrorId = "InvalidUrl,Microsoft.PowerShell.PowerShellGet.Cmdlets.RegisterPSResourceRepository"}
@{Type = "-URL is not valid scheme"; IncorrectHashTable = @{Name = "testRepository"; URL="www.google.com"}; ErrorId = "InvalidUri,Microsoft.PowerShell.PowerShellGet.Cmdlets.RegisterPSResourceRepository"}

It "not register incorrectly formatted Name type repo among correct ones when incorrect type is <Type>" -TestCases $testCases2 {
param($Type, $IncorrectHashTable, $ErrorId)
Expand Down Expand Up @@ -230,4 +230,12 @@ Describe "Test Register-PSResourceRepository" {
$res.Trusted | Should -Be False
$res.Priority | Should -Be 50
}

It "should register local file share NuGet based repository" {
Register-PSResourceRepository -Name "localFileShareTestRepo" -URL "\\hcgg.rest.of.domain.name\test\ITxx\team\NuGet\"
$res = Get-PSResourceRepository -Name "localFileShareTestRepo"

$res.Name | Should -Be "localFileShareTestRepo"
$res.URL.LocalPath | Should -Contain "\\hcgg.rest.of.domain.name\test\ITxx\team\NuGet\"
}
}