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

Support sourceUri passed on nuget restore/install and dotnet restore or RestoreSources set for msbuild /t:restore command, nuget restore #4214

Merged
merged 28 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
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);
}

erdembayar marked this conversation as resolved.
Show resolved Hide resolved




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
5 changes: 3 additions & 2 deletions src/NuGet.Core/NuGet.PackageManagement/PackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public static async Task<DownloadResourceResult> GetDownloadResourceResultAsync(
{
if (isPackageNamespaceEnabled)
{
if (configuredPackageSources != null &&
!configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase))
if (configuredPackageSources == null ||
configuredPackageSources.Count == 0 ||
!configuredPackageSources.Contains(source.PackageSource.Name, StringComparer.OrdinalIgnoreCase))
{
// This package's id prefix is not defined in current package source, let's skip.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,15 @@ private void QueueWork(IReadOnlyList<SourceResource> sources, PackageIdentity pa
{
foreach (SourceResource source in sources)
{
if (_areNamespacesEnabled && configuredPackageSources != null && !configuredPackageSources.Contains(source.Source.PackageSource.Name, StringComparer.CurrentCultureIgnoreCase))
if (_areNamespacesEnabled)
{
// This package's id prefix is not defined in current package source, let's skip.
continue;
if (configuredPackageSources == null ||
configuredPackageSources.Count == 0 ||
!configuredPackageSources.Contains(source.Source.PackageSource.Name, StringComparer.OrdinalIgnoreCase))
{
// This package's id prefix is not defined in current package source, let's skip.
continue;
}
}

// Keep track of the order in which these were made
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-WrongSource-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-Pass-CorrectSourceOption-Succeed
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
{
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-Pass-WrongSourceOption-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-Pass-CorrectSourceOption-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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no package installation tests for PR here, is that on purpose?

Now that I've gone through it all in detail, I see that you had a baseline for installation and -Source for installation.

Note that -Source is not supported in PackageReference

Copy link
Contributor Author

@erdembayar erdembayar Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe PMC install for PR is synonymous with dotnet add package -source, but it's not implemented yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe PMC install for PR is synonymous with dotnet add package -source, but it's not implemented yet.

They are not equivalent.

Sources are ignored in PMC commands, there's a warning that gets logged when you try it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I'm not really suggesting we should add tests here.
I'm trying to provide as much context as possible to you, so that you can make sure you've covered all scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created follow up issue for this. NuGet/Home#11187

function CreateCustomTestPackage {
param(
Expand Down
Loading