Skip to content

Commit

Permalink
Adding support for pushing multiple packages (#3739)
Browse files Browse the repository at this point in the history
* support for pushing multiple packages

* timeout for all packages

* tests

* update obsolete messages

* reduce diff noise

* Revert "reduce diff noise"

* update call sites

* addressing PR comments

* formatting

* add test for one or more pushed paths

* reduce the diff noise

* testing push by filename and by wildcard

* Delete result.txt

* revert change for NuGet.exe

* test correct path

* - Reverting NuGet.CommandLine tests

- Adding NuGet.XPlat tests

* - Reverting NuGet.CommandLine tests

- Adding NuGet.XPlat tests
  • Loading branch information
marcin-krystianc authored Nov 4, 2020
1 parent ff86f98 commit fa9fbcb
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override async Task ExecuteCommandAsync()
await PushRunner.Run(
Settings,
SourceProvider,
packagePath,
new[] { packagePath },
Source,
apiKeyValue,
SymbolSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils;
using NuGet.Commands;
Expand Down Expand Up @@ -87,7 +88,7 @@ public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
throw new ArgumentException(Strings.Push_MissingArguments);
}

string packagePath = arguments.Values[0];
IList<string> packagePaths = arguments.Values;
string sourcePath = source.Value();
string apiKeyValue = apikey.Value();
string symbolSourcePath = symbolSource.Value();
Expand All @@ -113,7 +114,7 @@ public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
await PushRunner.Run(
sourceProvider.Settings,
sourceProvider,
packagePath,
packagePaths,
sourcePath,
apiKeyValue,
symbolSourcePath,
Expand Down
36 changes: 34 additions & 2 deletions src/NuGet.Core/NuGet.Commands/CommandRunners/PushRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
Expand All @@ -17,7 +18,7 @@ public static class PushRunner
public static async Task Run(
ISettings settings,
IPackageSourceProvider sourceProvider,
string packagePath,
IList<string> packagePaths,
string source,
string apiKey,
string symbolSource,
Expand Down Expand Up @@ -57,7 +58,7 @@ public static async Task Run(
}

await packageUpdateResource.Push(
packagePath,
packagePaths,
symbolSource,
timeoutSeconds,
disableBuffering,
Expand All @@ -68,5 +69,36 @@ await packageUpdateResource.Push(
symbolPackageUpdateResource,
logger);
}

[Obsolete("Use Run method which takes multiple package paths.")]
public static Task Run(
ISettings settings,
IPackageSourceProvider sourceProvider,
string packagePath,
string source,
string apiKey,
string symbolSource,
string symbolApiKey,
int timeoutSeconds,
bool disableBuffering,
bool noSymbols,
bool noServiceEndpoint,
bool skipDuplicate,
ILogger logger)
{
return Run(settings: settings,
sourceProvider: sourceProvider,
packagePaths: new[] { packagePath },
source: source,
apiKey: apiKey,
symbolSource: symbolSource,
symbolApiKey: symbolApiKey,
timeoutSeconds: timeoutSeconds,
disableBuffering: disableBuffering,
noSymbols: noSymbols,
noServiceEndpoint: noServiceEndpoint,
skipDuplicate: skipDuplicate,
logger: logger);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NuGet.Commands.VerifyArgs.PackagePaths.get -> System.Collections.Generic.IReadOnlyList<string>
NuGet.Commands.VerifyArgs.PackagePaths.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
static NuGet.Commands.PushRunner.Run(NuGet.Configuration.ISettings settings, NuGet.Configuration.IPackageSourceProvider sourceProvider, System.Collections.Generic.IList<string> packagePaths, string source, string apiKey, string symbolSource, string symbolApiKey, int timeoutSeconds, bool disableBuffering, bool noSymbols, bool noServiceEndpoint, bool skipDuplicate, NuGet.Common.ILogger logger) -> System.Threading.Tasks.Task
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NuGet.Commands.VerifyArgs.PackagePaths.get -> System.Collections.Generic.IReadOnlyList<string>
NuGet.Commands.VerifyArgs.PackagePaths.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
static NuGet.Commands.PushRunner.Run(NuGet.Configuration.ISettings settings, NuGet.Configuration.IPackageSourceProvider sourceProvider, System.Collections.Generic.IList<string> packagePaths, string source, string apiKey, string symbolSource, string symbolApiKey, int timeoutSeconds, bool disableBuffering, bool noSymbols, bool noServiceEndpoint, bool skipDuplicate, NuGet.Common.ILogger logger) -> System.Threading.Tasks.Task
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NuGet.Commands.VerifyArgs.PackagePaths.get -> System.Collections.Generic.IReadOnlyList<string>
NuGet.Commands.VerifyArgs.PackagePaths.set -> void
static NuGet.Commands.MSBuildProjectFrameworkUtility.GetProjectFramework(string projectFilePath, string targetFrameworkMoniker, string targetPlatformMoniker, string targetPlatformMinVersion) -> NuGet.Frameworks.NuGetFramework
static NuGet.Commands.PushRunner.Run(NuGet.Configuration.ISettings settings, NuGet.Configuration.IPackageSourceProvider sourceProvider, System.Collections.Generic.IList<string> packagePaths, string source, string apiKey, string symbolSource, string symbolApiKey, int timeoutSeconds, bool disableBuffering, bool noSymbols, bool noServiceEndpoint, bool skipDuplicate, NuGet.Common.ILogger logger) -> System.Threading.Tasks.Task
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ NuGet.Protocol.PackageVulnerabilityMetadata.Severity.get -> int
const NuGet.Protocol.JsonProperties.AdvisoryUrl = "advisoryUrl" -> string
const NuGet.Protocol.JsonProperties.Severity = "severity" -> string
const NuGet.Protocol.JsonProperties.Vulnerabilities = "vulnerabilities" -> string
NuGet.Protocol.Core.Types.PackageUpdateResource.Push(System.Collections.Generic.IList<string> packagePaths, string symbolSource, int timeoutInSecond, bool disableBuffering, System.Func<string, string> getApiKey, System.Func<string, string> getSymbolApiKey, bool noServiceEndpoint, bool skipDuplicate, NuGet.Protocol.Core.Types.SymbolPackageUpdateResourceV3 symbolPackageUpdateResource, NuGet.Common.ILogger log) -> System.Threading.Tasks.Task
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ NuGet.Protocol.PackageVulnerabilityMetadata.Severity.get -> int
const NuGet.Protocol.JsonProperties.AdvisoryUrl = "advisoryUrl" -> string
const NuGet.Protocol.JsonProperties.Severity = "severity" -> string
const NuGet.Protocol.JsonProperties.Vulnerabilities = "vulnerabilities" -> string
NuGet.Protocol.Core.Types.PackageUpdateResource.Push(System.Collections.Generic.IList<string> packagePaths, string symbolSource, int timeoutInSecond, bool disableBuffering, System.Func<string, string> getApiKey, System.Func<string, string> getSymbolApiKey, bool noServiceEndpoint, bool skipDuplicate, NuGet.Protocol.Core.Types.SymbolPackageUpdateResourceV3 symbolPackageUpdateResource, NuGet.Common.ILogger log) -> System.Threading.Tasks.Task
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ NuGet.Protocol.PackageVulnerabilityMetadata.Severity.get -> int
const NuGet.Protocol.JsonProperties.AdvisoryUrl = "advisoryUrl" -> string
const NuGet.Protocol.JsonProperties.Severity = "severity" -> string
const NuGet.Protocol.JsonProperties.Vulnerabilities = "vulnerabilities" -> string
NuGet.Protocol.Core.Types.PackageUpdateResource.Push(System.Collections.Generic.IList<string> packagePaths, string symbolSource, int timeoutInSecond, bool disableBuffering, System.Func<string, string> getApiKey, System.Func<string, string> getSymbolApiKey, bool noServiceEndpoint, bool skipDuplicate, NuGet.Protocol.Core.Types.SymbolPackageUpdateResourceV3 symbolPackageUpdateResource, NuGet.Common.ILogger log) -> System.Threading.Tasks.Task
55 changes: 37 additions & 18 deletions src/NuGet.Core/NuGet.Protocol/Resources/PackageUpdateResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Uri SourceUri
}

public async Task Push(
string packagePath,
IList<string> packagePaths,
string symbolSource, // empty to not push symbols
int timeoutInSecond,
bool disableBuffering,
Expand All @@ -74,31 +74,50 @@ public async Task Push(
tokenSource.CancelAfter(requestTimeout);
var apiKey = getApiKey(_source);

bool explicitSnupkgPush = true;

if (!packagePath.EndsWith(NuGetConstants.SnupkgExtension, StringComparison.OrdinalIgnoreCase))
foreach (var packagePath in packagePaths)
{
await PushPackage(packagePath, _source, apiKey, noServiceEndpoint, skipDuplicate,
requestTimeout, log, tokenSource.Token);
bool explicitSnupkgPush = true;
if (!packagePath.EndsWith(NuGetConstants.SnupkgExtension, StringComparison.OrdinalIgnoreCase))
{
await PushPackage(packagePath, _source, apiKey, noServiceEndpoint, skipDuplicate,
requestTimeout, log, tokenSource.Token);

//Since this was not a snupkg push (probably .nupkg), when we try pushing symbols later, don't error if there are no snupkg files found.
explicitSnupkgPush = false;
}
//Since this was not a snupkg push (probably .nupkg), when we try pushing symbols later, don't error if there are no snupkg files found.
explicitSnupkgPush = false;
}

// symbolSource is only set when:
// - The user specified it on the command line
// - The endpoint for main package supports pushing snupkgs
if (!string.IsNullOrEmpty(symbolSource))
{
var symbolApiKey = getSymbolApiKey(symbolSource);
// symbolSource is only set when:
// - The user specified it on the command line
// - The endpoint for main package supports pushing snupkgs
if (!string.IsNullOrEmpty(symbolSource))
{
var symbolApiKey = getSymbolApiKey(symbolSource);

await PushSymbols(packagePath, symbolSource, symbolApiKey,
noServiceEndpoint, skipDuplicate, symbolPackageUpdateResource,
requestTimeout, log, explicitSnupkgPush, tokenSource.Token);
await PushSymbols(packagePath, symbolSource, symbolApiKey,
noServiceEndpoint, skipDuplicate, symbolPackageUpdateResource,
requestTimeout, log, explicitSnupkgPush, tokenSource.Token);
}
}
}
}

[Obsolete("Use Push method which takes multiple package paths.")]
public Task Push(
string packagePath,
string symbolSource, // empty to not push symbols
int timeoutInSecond,
bool disableBuffering,
Func<string, string> getApiKey,
Func<string, string> getSymbolApiKey,
bool noServiceEndpoint,
bool skipDuplicate,
SymbolPackageUpdateResourceV3 symbolPackageUpdateResource,
ILogger log)
{
return Push(new[] { packagePath }, symbolSource, timeoutInSecond, disableBuffering, getApiKey,
getSymbolApiKey, noServiceEndpoint, skipDuplicate, symbolPackageUpdateResource, log);
}

[Obsolete("Consolidating to one PackageUpdateResource.Push method which has all parameters defined.")]
public async Task Push(
string packagePath,
Expand Down
41 changes: 41 additions & 0 deletions test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatPushTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NuGet.Configuration;
using NuGet.Test.Utility;
Expand Down Expand Up @@ -147,6 +149,45 @@ public async Task PushToServerSucceeds_DeleteFirst(PackageSource packageSource)
}
}

// Tests pushing multiple packages (multiple paths)
[Fact]
public async Task PushMultiplePathsToFileSystemSource()
{
using (var packageDirectory = TestDirectory.Create())
using (var source = TestDirectory.Create())
{
// Arrange
var log = new TestCommandOutputLogger();
var packageInfoCollection = new[]
{
await TestPackagesCore.GetRuntimePackageAsync(packageDirectory, "testPackageA", "1.1.0"),
await TestPackagesCore.GetRuntimePackageAsync(packageDirectory, "testPackageB", "1.1.0"),
};

var pushArgs = new List<string>
{
"push",
packageInfoCollection[0].FullName,
packageInfoCollection[1].FullName,
"--source",
source,
};

// Act
var exitCode = CommandLine.XPlat.Program.MainInternal(pushArgs.ToArray(), log);

// Assert
Assert.Equal(string.Empty, log.ShowErrors());
Assert.Equal(0, exitCode);

foreach (var packageInfo in packageInfoCollection)
{
Assert.Contains($"Pushing {packageInfo.Name}", log.ShowMessages());
Assert.True(File.Exists(Path.Combine(source, packageInfo.Name)));
}
}
}

/// <summary>
/// This is called when the package must be deleted before being pushed. It's ok if this
/// fails, maybe the package was never pushed.
Expand Down
15 changes: 11 additions & 4 deletions test/NuGet.Core.Tests/NuGet.Commands.Test/PushCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ public async Task PushCommand_AbsolutePathSourceAsync()
new PackageSource(packagePushDest.FullName)
};

var packageInfo = await SimpleTestPackageUtility.CreateFullPackageAsync(workingDir, "test", "1.0.0");
var packageInfoCollection = new[]
{
await SimpleTestPackageUtility.CreateFullPackageAsync(workingDir, "test1", "1.0.0"),
await SimpleTestPackageUtility.CreateFullPackageAsync(workingDir, "test2", "1.0.0")
};

// Act
await PushRunner.Run(
Settings.LoadDefaultSettings(null, null, null),
new TestPackageSourceProvider(packageSources),
packageInfo.FullName,
new[] { packageInfoCollection[0].FullName, packageInfoCollection[1].FullName },
packagePushDest.FullName,
null, // api key
null, // symbols source
Expand All @@ -52,8 +56,11 @@ await PushRunner.Run(
new TestLogger());

// Assert
var destFile = Path.Combine(packagePushDest.FullName, packageInfo.Name);
Assert.Equal(true, File.Exists(destFile));
foreach (var packageInfo in packageInfoCollection)
{
var destFile = Path.Combine(packagePushDest.FullName, packageInfo.Name);
Assert.True(File.Exists(destFile));
}
}
}
}
Expand Down
Loading

0 comments on commit fa9fbcb

Please sign in to comment.