Skip to content

Commit

Permalink
[TESTS] tool-install/tool-update: Respect RestoreActionConfig options
Browse files Browse the repository at this point in the history
  • Loading branch information
edvilme committed Oct 28, 2024
1 parent f85f664 commit df1c650
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ public ToolInstallGlobalOrToolPathCommandTests(ITestOutputHelper log): base(log)
_parseResult = Parser.Instance.Parse($"dotnet tool install -g {PackageId}");
}

[Fact]
public void WhenPassingRestoreActionConfigOptions()
{
var parseResult = Parser.Instance.Parse($"dotnet tool install -g {PackageId} --ignore-failed-sources");
var toolInstallCommand = new ToolInstallGlobalOrToolPathCommand(parseResult);
toolInstallCommand._restoreActionConfig.IgnoreFailedSources.Should().BeTrue();
}

[Fact]
public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
{
_fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, "nuget.config"), _nugetConfigWithInvalidSources);

var toolInstallGlobalOrToolPathCommand = new ToolInstallGlobalOrToolPathCommand(
_parseResult,
_createToolPackageStoresAndDownloader,
_createShellShimRepository,
_environmentPathInstructionMock,
_reporter);

toolInstallGlobalOrToolPathCommand.Execute().Should().Be(0);
_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}

[Fact]
public void WhenRunWithPackageIdItShouldCreateValidShim()
{
Expand Down Expand Up @@ -630,6 +654,16 @@ public void SetPermission(string path, string chmodArgument)
{
}
}

private string _nugetConfigWithInvalidSources = @"{
<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget"" value=""https://api.nuget.org/v3/index.json"" />
<add key=""invalid_source"" value=""https://api.nuget.org/v3/invalid.json"" />
</packageSources>
</configuration>
}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ public ToolInstallLocalCommandTests(ITestOutputHelper log):base(log)
1);
}

[Fact]
public void WhenPassingRestoreActionConfigOptions()
{
var parseResult = Parser.Instance.Parse($"dotnet tool install {_packageIdA.ToString()} --ignore-failed-sources");
var command = new ToolInstallLocalCommand(parseResult);
command._restoreActionConfig.IgnoreFailedSources.Should().BeTrue();
}

[Fact]
public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
{
_fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, "nuget.config"), _nugetConfigWithInvalidSources);
var parseResult = Parser.Instance.Parse($"dotnet tool install {_packageIdA.ToString()} --ignore-failed-sources");
var installLocalCommand = new ToolInstallLocalCommand(
parseResult,
_toolPackageDownloaderMock,
_toolManifestFinder,
_toolManifestEditor,
_localToolsResolverCache,
_reporter);

installLocalCommand.Execute().Should().Be(0);

_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}

[Fact]
public void WhenRunWithPackageIdItShouldSaveToCacheAndAddToManifestFile()
{
Expand Down Expand Up @@ -440,6 +466,16 @@ out RestoredCommand restoredCommand
""isRoot"":true,
""tools"":{
}
}";

private string _nugetConfigWithInvalidSources = @"{
<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget"" value=""https://api.nuget.org/v3/index.json"" />
<add key=""invalid_source"" value=""https://api.nuget.org/v3/invalid.json"" />
</packageSources>
</configuration>
}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Microsoft.DotNet.ShellShim;
using System.CommandLine;
using Parser = Microsoft.DotNet.Cli.Parser;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.DotNet.Tools.Tool.Uninstall;

namespace Microsoft.DotNet.Tests.Commands.Tool
{
Expand All @@ -28,14 +30,15 @@ public class ToolUpdateGlobalOrToolPathCommandTests
private const string HigherPreviewPackageVersion = "1.0.5-preview3";
private readonly string _shimsDirectory;
private readonly string _toolsDirectory;
private readonly string _tempDirectory;

public ToolUpdateGlobalOrToolPathCommandTests()
{
_reporter = new BufferedReporter();
_fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build();
var tempDirectory = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath;
_shimsDirectory = Path.Combine(tempDirectory, "shims");
_toolsDirectory = Path.Combine(tempDirectory, "tools");
_tempDirectory = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath;
_shimsDirectory = Path.Combine(_tempDirectory, "shims");
_toolsDirectory = Path.Combine(_tempDirectory, "tools");
_environmentPathInstructionMock = new EnvironmentPathInstructionMock(_reporter, _shimsDirectory);
_store = new ToolPackageStoreMock(new DirectoryPath(_toolsDirectory), _fileSystem);
_mockFeeds = new List<MockFeed>
Expand Down Expand Up @@ -68,6 +71,25 @@ public ToolUpdateGlobalOrToolPathCommandTests()
};
}

[Fact]
public void WhenPassingRestoreActionConfigOptions()
{
var parseResult = Parser.Instance.Parse($"dotnet tool update -g {_packageId} --ignore-failed-sources");
var toolUpdateCommand = new ToolUpdateGlobalOrToolPathCommand(parseResult);
toolUpdateCommand._restoreActionConfig.IgnoreFailedSources.Should().BeTrue();
}

[Fact]
public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
{
_fileSystem.File.WriteAllText(Path.Combine(_tempDirectory, "nuget.config"), _nugetConfigWithInvalidSources);

var command = CreateUpdateCommand($"-g {_packageId} --ignore-failed-sources");

command.Execute().Should().Be(0);
_fileSystem.File.Delete(Path.Combine(_tempDirectory, "nuget.config"));
}

[Fact]
public void GivenANonFeedExistentPackageItErrors()
{
Expand Down Expand Up @@ -373,6 +395,16 @@ private ShellShimRepository GetMockedShellShimRepository()
appHostShellShimMaker: new AppHostShellShimMakerMock(_fileSystem),
filePermissionSetter: new ToolInstallGlobalOrToolPathCommandTests.NoOpFilePermissionSetter());
}

private string _nugetConfigWithInvalidSources = @"{
<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget"" value=""https://api.nuget.org/v3/index.json"" />
<add key=""invalid_source"" value=""https://api.nuget.org/v3/invalid.json"" />
</packageSources>
</configuration>
}";
}
}

37 changes: 37 additions & 0 deletions src/Tests/dotnet.Tests/CommandTests/ToolUpdateLocalCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.DotNet.ToolManifest;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools.Tests.ComponentMocks;
using Microsoft.DotNet.Tools.Tool.Install;
using Microsoft.DotNet.Tools.Tool.Restore;
using Microsoft.DotNet.Tools.Tool.Update;
using Microsoft.Extensions.DependencyModel.Tests;
Expand Down Expand Up @@ -112,6 +113,32 @@ public ToolUpdateLocalCommandTests()
_reporter);
}

[Fact]
public void WhenPassingRestoreActionConfigOptions()
{
var parseResult = Parser.Instance.Parse($"dotnet tool update {_packageIdA.ToString()} --ignore-failed-sources");
var command = new ToolUpdateLocalCommand(parseResult);
command._restoreActionConfig.IgnoreFailedSources.Should().BeTrue();
}

[Fact]
public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
{
_fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, "nuget.config"), _nugetConfigWithInvalidSources);
var parseResult = Parser.Instance.Parse($"dotnet tool update {_packageIdA.ToString()} --ignore-failed-sources");
var updateLocalCommand = new ToolUpdateLocalCommand(
parseResult,
_toolPackageDownloaderMock,
_toolManifestFinder,
_toolManifestEditor,
_localToolsResolverCache,
_reporter);

updateLocalCommand.Execute().Should().Be(0);

_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}

[Fact]
public void WhenRunWithPackageIdItShouldUpdateFromManifestFile()
{
Expand Down Expand Up @@ -349,6 +376,16 @@ out RestoredCommand restoredCommand
""version"": 1,
""isRoot"": false,
""tools"": {}
}";

private string _nugetConfigWithInvalidSources = @"{
<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget"" value=""https://api.nuget.org/v3/index.json"" />
<add key=""invalid_source"" value=""https://api.nuget.org/v3/invalid.json"" />
</packageSources>
</configuration>
}";
}
}
Expand Down

0 comments on commit df1c650

Please sign in to comment.