Skip to content

Commit

Permalink
Support sourceUri passed on nuget restore/install and dotnet restore …
Browse files Browse the repository at this point in the history
…or RestoreSources set for msbuild /t:restore command, nuget restore (#4214)

* Add tests asserting source option works for nuget.exe restore/install and PMC( with packages.config projects), dotnet restore.
  • Loading branch information
erdembayar authored Aug 27, 2021
1 parent 46f2c3e commit cb411a3
Show file tree
Hide file tree
Showing 8 changed files with 1,202 additions and 52 deletions.
12 changes: 6 additions & 6 deletions src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ private async Task PerformV2RestoreAsync(string packagesConfigFilePath, string i
Console)
};

var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload)
PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings);

var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload, packageNamespacesConfiguration)
{
ClientPolicyContext = clientPolicyContext
};
Expand Down Expand Up @@ -251,10 +253,6 @@ private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider()
return new CommandLineSourceRepositoryProvider(SourceProvider);
}





private async Task InstallPackageAsync(
string packageId,
NuGetVersion version,
Expand Down Expand Up @@ -373,7 +371,9 @@ private async Task InstallPackageAsync(
resolutionContext.SourceCacheContext.NoCache = NoCache;
resolutionContext.SourceCacheContext.DirectDownload = DirectDownload;

var downloadContext = new PackageDownloadContext(resolutionContext.SourceCacheContext, installPath, DirectDownload)
PackageNamespacesConfiguration packageNamespacesConfiguration = PackageNamespacesConfiguration.GetPackageNamespacesConfiguration(Settings);

var downloadContext = new PackageDownloadContext(resolutionContext.SourceCacheContext, installPath, DirectDownload, packageNamespacesConfiguration)
{
ClientPolicyContext = clientPolicyContext
};
Expand Down
254 changes: 254 additions & 0 deletions test/EndToEnd/tests/PackageNameSpaceTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,260 @@ function Test-VsPackageInstallerServices-PackageNamespaceInstall-WithMultipleFee
}
}

function Test-PC-PackageNamespaceInstall-Succeed
{
param($context)

# Arrange
$nugetConfigPath = Join-Path $OutputPath 'nuget.config'
$settingFileContent =@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="LocalRepository" value="{0}" />
</packageSources>
<packageNamespaces>
<packageSource key="LocalRepository">
<namespace id="Solution*" />
</packageSource>
</packageNamespaces>
</configuration>
"@
try {
# We have to create config file before creating solution, otherwise it's not effective for new solutions.
$settingFileContent -f $context.RepositoryRoot | Out-File -Encoding "UTF8" $nugetConfigPath

$p = New-ConsoleApplication

# Act
$p | Install-Package SolutionLevelPkg -Version 1.0

# # Assert
Assert-Package $p SolutionLevelPkg 1.0.0
$errorlist = Get-Errors
Assert-AreEqual 0 $errorlist.Count
$warninglist = Get-Warnings
Assert-AreEqual 0 $warninglist.Count
}
finally {
Remove-Item $nugetConfigPath
}
}

function Test-PC-PackageNamespaceInstall-Fails
{
param($context)

# Arrange
$repoDirectory = $context.RepositoryRoot
$privateRepo = Join-Path $repoDirectory "privateRepo"

$nugetConfigPath = Join-Path $OutputPath 'nuget.config'
$settingFileContent =@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="LocalRepository" value="{0}" />
<add key="PrivateRepository" value="{1}" />
</packageSources>
<packageNamespaces>
<packageSource key="PrivateRepository">
<namespace id="Solution*" />
</packageSource>
</packageNamespaces>
</configuration>
"@
try {
# We have to create config file before creating solution, otherwise it's not effective for new solutions.
$settingFileContent -f $context.RepositoryRoot,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath

$p = New-ConsoleApplication

# Act & Assert
# Even though SolutionLevelPkg package exist in $repoDirectory since package namespace filter set SolutionLevelPkg can be restored only from PrivateRepository repository so it'll fail.
$exceptionMessage = "Package 'SolutionLevelPkg 1.0' is not found in the following primary source(s): '" + $context.RepositoryRoot + "," + $privateRepo + "'. Please verify all your online package sources are available (OR) package id, version are specified correctly."
Assert-Throws { $p | Install-Package SolutionLevelPkg -Version 1.0 } $exceptionMessage
Assert-NoPackage $p SolutionLevelPkg 1.0.0
}
finally {
Remove-Item $nugetConfigPath
}
}

function Test-PC-PackageNamespaceInstall-WithCorrectSourceOption-Succeed
{
param($context)

# Arrange
$repoDirectory = Join-Path $OutputPath "CustomPackages"
$opensourceRepo = Join-Path $repoDirectory "opensourceRepo"
$privateRepo = Join-Path $repoDirectory "privateRepo"
$nugetConfigPath = Join-Path $OutputPath 'nuget.config'

$settingFileContent =@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="OpensourceRepository" value="{0}" />
<add key="PrivateRepository" value="{1}" />
</packageSources>
<packageNamespaces>
<packageSource key="PrivateRepository">
<namespace id="Contoso.MVC.*" />
</packageSource>
</packageNamespaces>
</configuration>
"@
try {
# We have to create config file before creating solution, otherwise it's not effective for new solutions.
$settingFileContent -f $opensourceRepo,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath

$p = New-ConsoleApplication
$projectDirectoryPath = $p.Properties.Item("FullPath").Value
$packagesConfigPath = Join-Path $projectDirectoryPath 'packages.config'
$projectDirectoryPath = $p.Properties.Item("FullPath").Value
$solutionDirectory = Split-Path -Path $projectDirectoryPath -Parent

CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $privateRepo "Thisisfromprivaterepo1.txt"
CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo1.txt"

# Act
$p | Install-Package Contoso.MVC.ASP -Source $privateRepo

# Assert
Assert-Package $p Contoso.MVC.ASP 1.0.0
$packagesFolder = Join-Path $solutionDirectory "packages"
$contosoNupkgFolder = Join-Path $packagesFolder "Contoso.MVC.ASP.1.0.0"
Assert-PathExists(Join-Path $contosoNupkgFolder "Contoso.MVC.ASP.1.0.0.nupkg")
# Make sure name squatting package from public repo not restored.
$contentFolder = Join-Path $contosoNupkgFolder "content"
Assert-PathExists(Join-Path $contentFolder "Thisisfromprivaterepo1.txt")

$errorlist = Get-Errors
Assert-AreEqual 0 $errorlist.Count
}
finally {
Remove-Item -Recurse -Force $repoDirectory
Remove-Item $nugetConfigPath
}
}

function Test-PC-PackageNamespaceInstall-WithWrongSourceOption-Fails
{
param($context)

# Arrange
$repoDirectory = Join-Path $OutputPath "CustomPackages"
$opensourceRepo = Join-Path $repoDirectory "opensourceRepo"
$privateRepo = Join-Path $repoDirectory "privateRepo"
$nugetConfigPath = Join-Path $OutputPath 'nuget.config'

$settingFileContent =@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="OpensourceRepository" value="{0}" />
<add key="PrivateRepository" value="{1}" />
</packageSources>
<packageNamespaces>
<packageSource key="PrivateRepository">
<namespace id="Contoso.MVC.*" />
</packageSource>
</packageNamespaces>
</configuration>
"@
try {
# We have to create config file before creating solution, otherwise it's not effective for new solutions.
$settingFileContent -f $opensourceRepo,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath

$p = New-ConsoleApplication
$projectDirectoryPath = $p.Properties.Item("FullPath").Value
$packagesConfigPath = Join-Path $projectDirectoryPath 'packages.config'
$projectDirectoryPath = $p.Properties.Item("FullPath").Value
$solutionDirectory = Split-Path -Path $projectDirectoryPath -Parent

CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $privateRepo "Thisisfromprivaterepo1.txt"
CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo1.txt"

# Act & Assert
$exceptionMessage = "Package 'Contoso.MVC.ASP 1.0.0' is not found in the following primary source(s): '"+ $opensourceRepo + "'. Please verify all your online package sources are available (OR) package id, version are specified correctly."
Assert-Throws { $p | Install-Package Contoso.MVC.ASP -Source $opensourceRepo } $exceptionMessage
Assert-NoPackage $p SolutionLevelPkg 1.0.0
}
finally {
Remove-Item -Recurse -Force $repoDirectory
Remove-Item $nugetConfigPath
}
}

function Test-PC-PackageNamespaceUpdate-WithCorrectSourceOption-Succeed
{
param($context)

# Arrange
$repoDirectory = Join-Path $OutputPath "CustomPackages"
$opensourceRepo = Join-Path $repoDirectory "opensourceRepo"
$privateRepo = Join-Path $repoDirectory "privateRepo"
$nugetConfigPath = Join-Path $OutputPath 'nuget.config'

$settingFileContent =@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="OpensourceRepository" value="{0}" />
<add key="PrivateRepository" value="{1}" />
</packageSources>
<packageNamespaces>
<packageSource key="PrivateRepository">
<namespace id="Contoso.MVC.*" />
</packageSource>
</packageNamespaces>
</configuration>
"@
try {
# We have to create config file before creating solution, otherwise it's not effective for new solutions.
$settingFileContent -f $opensourceRepo,$privateRepo | Out-File -Encoding "UTF8" $nugetConfigPath

$p = New-ConsoleApplication
$projectDirectoryPath = $p.Properties.Item("FullPath").Value
$packagesConfigPath = Join-Path $projectDirectoryPath 'packages.config'
$projectDirectoryPath = $p.Properties.Item("FullPath").Value
$solutionDirectory = Split-Path -Path $projectDirectoryPath -Parent

CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $privateRepo "Thisisfromprivaterepo1.txt"
CreateCustomTestPackage "Contoso.MVC.ASP" "2.0.0" $privateRepo "Thisisfromprivaterepo2.txt"
CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo1.txt"
CreateCustomTestPackage "Contoso.MVC.ASP" "1.0.0" $opensourceRepo "Thisisfromopensourcerepo2.txt"

# Act
$p | Install-Package Contoso.MVC.ASP -Version 1.0 -Source $privateRepo
Assert-Package $p Contoso.MVC.ASP 1.0.0

$p | Update-Package Contoso.MVC.ASP -Version 2.0 -Source $privateRepo
Assert-Package $p Contoso.MVC.ASP 2.0.0

# Assert
$packagesFolder = Join-Path $solutionDirectory "packages"
$contosoNupkgFolder = Join-Path $packagesFolder "Contoso.MVC.ASP.2.0.0"
Assert-PathExists(Join-Path $contosoNupkgFolder "Contoso.MVC.ASP.2.0.0.nupkg")
# Make sure name squatting package from public repo not restored.
$contentFolder = Join-Path $contosoNupkgFolder "content"
Assert-PathExists(Join-Path $contentFolder "Thisisfromprivaterepo2.txt")

$errorlist = Get-Errors
Assert-AreEqual 0 $errorlist.Count
}
finally {
Remove-Item -Recurse -Force $repoDirectory
Remove-Item $nugetConfigPath
}
}

# Create a custom test package
function CreateCustomTestPackage {
param(
Expand Down
Loading

0 comments on commit cb411a3

Please sign in to comment.