Skip to content

Commit

Permalink
accept bare version as NuGet exact version for .NET tools (#36671)
Browse files Browse the repository at this point in the history
  • Loading branch information
JL03-Yue committed Nov 14, 2023
2 parents 93334bf + ad48dd6 commit 5a7019c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ private IEnumerable<PackageSource> LoadNuGetSources(PackageId packageId, Package
VersionRange versionRange,
PackageSourceLocation packageSourceLocation = null)
{
if(versionRange.MinVersion != null && versionRange.MaxVersion != null && versionRange.MinVersion == versionRange.MaxVersion)
if (versionRange.MinVersion != null && !versionRange.IsFloating &&
(versionRange.MaxVersion == null || versionRange.MinVersion == versionRange.MaxVersion))
{
return versionRange.MinVersion;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.NugetSearch;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools.Tool.Search;
using NuGet.Versioning;

namespace Microsoft.DotNet.Tools.Tool.Install
Expand All @@ -36,6 +30,13 @@ public static VersionRange GetVersionRange(this ParseResult parseResult)
}

VersionRange versionRange = null;

// accept 'bare' versions and interpret 'bare' versions as NuGet exact versions
if (!string.IsNullOrEmpty(packageVersion) && NuGetVersion.TryParse(packageVersion, out NuGetVersion version2))
{
return new VersionRange(minVersion: version2, includeMinVersion: true, maxVersion: version2, includeMaxVersion: true, originalString: "[" + packageVersion + "]");
}

if (!string.IsNullOrEmpty(packageVersion) && !VersionRange.TryParse(packageVersion, out versionRange))
{
throw new GracefulException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ public void WhenRunWithValidUnlistedVersionRangeItShouldSucceed()
toolUninstallCommand.Execute().Should().Pass();
}

[Fact]
public void WhenRunWithValidBareVersionItShouldInterpretAsNuGetExactVersion()
{
const string nugetSourcePath = "https://api.nuget.org/v3/index.json";
var testDir = _testAssetsManager.CreateTestDirectory().Path;

var toolInstallGlobalOrToolPathCommand = new DotnetCommand(Log, "tool", "install", "-g", UnlistedPackageId, "--version", "0.5.0", "--add-source", nugetSourcePath)
.WithEnvironmentVariable("DOTNET_SKIP_WORKLOAD_INTEGRITY_CHECK", "true")
.WithWorkingDirectory(testDir);

toolInstallGlobalOrToolPathCommand.Execute().Should().Pass();

// Uninstall the unlisted package
var toolUninstallCommand = new DotnetCommand(Log, "tool", "uninstall", "-g", UnlistedPackageId);
toolUninstallCommand.Execute().Should().Pass();
}

[Fact]
public void WhenRunWithoutValidVersionUnlistedToolItShouldThrow()
{
Expand Down

0 comments on commit 5a7019c

Please sign in to comment.