diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index 678c6fb95..11cb8a861 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -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); @@ -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); diff --git a/src/code/SetPSResourceRepository.cs b/src/code/SetPSResourceRepository.cs index d74507f6b..49ed8d272 100644 --- a/src/code/SetPSResourceRepository.cs +++ b/src/code/SetPSResourceRepository.cs @@ -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); diff --git a/src/code/Utils.cs b/src/code/Utils.cs index 55b222571..d2cf24b03 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -113,7 +113,7 @@ public static string[] ProcessNameWildcards( } #endregion - + #region Version methods public static string GetNormalizedVersionString( @@ -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 diff --git a/test/RegisterPSResourceRepository.Tests.ps1 b/test/RegisterPSResourceRepository.Tests.ps1 index fd4204774..c8c1a659f 100644 --- a/test/RegisterPSResourceRepository.Tests.ps1 +++ b/test/RegisterPSResourceRepository.Tests.ps1 @@ -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 " -TestCases $testCases2 { param($Type, $IncorrectHashTable, $ErrorId) @@ -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\" + } }