ToolPackageInstallerNugetCacheTests.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System.Reflection;
  4. using Microsoft.DotNet.Cli;
  5. using Microsoft.DotNet.Cli.ToolPackage;
  6. using Microsoft.DotNet.ToolPackage;
  7. using Microsoft.DotNet.Tools.Tests.ComponentMocks;
  8. using Microsoft.DotNet.Tools.Tool.Install;
  9. using Microsoft.Extensions.DependencyModel.Tests;
  10. using Microsoft.Extensions.EnvironmentAbstractions;
  11. using NuGet.Versioning;
  12. namespace Microsoft.DotNet.PackageInstall.Tests
  13. {
  14. public class ToolPackageInstallToManagedLocationInstaller : SdkTest
  15. {
  16. public ToolPackageInstallToManagedLocationInstaller(ITestOutputHelper log) : base(log)
  17. {
  18. }
  19. [WindowsOnlyTheory]
  20. [InlineData(false)]
  21. [InlineData(true)]
  22. public void GivenNugetConfigInstallSucceeds(bool testMockBehaviorIsInSync)
  23. {
  24. string testDirectory = _testAssetsManager.CreateTestDirectory(identifier: testMockBehaviorIsInSync.ToString()).Path;
  25. var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed(testDirectory);
  26. var (store, installer, reporter, fileSystem) = Setup(
  27. useMock: testMockBehaviorIsInSync,
  28. testDirectory: testDirectory,
  29. feeds: GetMockFeedsForConfigFile(nugetConfigPath));
  30. try
  31. {
  32. var nugetCacheLocation =
  33. new DirectoryPath(testDirectory).WithSubDirectories(Path.GetRandomFileName());
  34. IToolPackage toolPackage = installer.InstallPackage(
  35. packageId: TestPackageId,
  36. verbosity: TestVerbosity,
  37. versionRange: VersionRange.Parse(TestPackageVersion),
  38. packageLocation: new PackageLocation(nugetConfig: nugetConfigPath),
  39. targetFramework: _testTargetframework);
  40. var commands = toolPackage.Commands;
  41. var expectedPackagesFolder = NuGetGlobalPackagesFolder.GetLocation();
  42. commands[0].Executable.Value.Should().StartWith(expectedPackagesFolder);
  43. fileSystem.File
  44. .Exists(commands[0].Executable.Value)
  45. .Should().BeTrue($"{commands[0].Executable.Value} should exist");
  46. }
  47. finally
  48. {
  49. foreach (var line in reporter.Lines)
  50. {
  51. Log.WriteLine(line);
  52. }
  53. }
  54. }
  55. [WindowsOnlyTheory]
  56. [InlineData(false)]
  57. [InlineData(true)]
  58. public void GivenNugetConfigVersionRangeInstallSucceeds(bool testMockBehaviorIsInSync)
  59. {
  60. string testDirectory = _testAssetsManager.CreateTestDirectory(identifier: testMockBehaviorIsInSync.ToString()).Path;
  61. var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed(testDirectory);
  62. var (store, installer, reporter, fileSystem) = Setup(
  63. useMock: testMockBehaviorIsInSync,
  64. testDirectory: testDirectory,
  65. feeds: GetMockFeedsForConfigFile(nugetConfigPath));
  66. IToolPackage toolPackage = installer.InstallPackage(
  67. packageId: TestPackageId,
  68. verbosity: TestVerbosity,
  69. versionRange: VersionRange.Parse("1.0.0-*"),
  70. packageLocation: new PackageLocation(nugetConfig: nugetConfigPath),
  71. targetFramework: _testTargetframework);
  72. var expectedPackagesFolder = NuGetGlobalPackagesFolder.GetLocation();
  73. var commands = toolPackage.Commands;
  74. commands[0].Executable.Value.Should().StartWith(expectedPackagesFolder);
  75. toolPackage.Version.Should().Be(NuGetVersion.Parse(TestPackageVersion));
  76. }
  77. private static List<MockFeed> GetMockFeedsForConfigFile(FilePath nugetConfig)
  78. {
  79. return new List<MockFeed>
  80. {
  81. new MockFeed
  82. {
  83. Type = MockFeedType.ExplicitNugetConfig,
  84. Uri = nugetConfig.Value,
  85. Packages = new List<MockFeedPackage>
  86. {
  87. new MockFeedPackage
  88. {
  89. PackageId = TestPackageId.ToString(),
  90. Version = TestPackageVersion,
  91. ToolCommandName = "SimulatorCommand"
  92. }
  93. }
  94. }
  95. };
  96. }
  97. private (IToolPackageStore, IToolPackageDownloader, BufferedReporter, IFileSystem) Setup(
  98. bool useMock,
  99. string testDirectory,
  100. List<MockFeed> feeds = null)
  101. {
  102. var root = new DirectoryPath(Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName()));
  103. var reporter = new BufferedReporter();
  104. IFileSystem fileSystem;
  105. IToolPackageStore store;
  106. IToolPackageDownloader downloader;
  107. if (useMock)
  108. {
  109. fileSystem = new FileSystemMockBuilder().Build();
  110. store = new ToolPackageStoreMock(root, fileSystem);
  111. downloader = new ToolPackageDownloaderMock(
  112. store: store,
  113. fileSystem: fileSystem,
  114. reporter: reporter,
  115. feeds: feeds);
  116. }
  117. else
  118. {
  119. fileSystem = new FileSystemWrapper();
  120. store = new ToolPackageStoreAndQuery(root);
  121. var runtimeJsonPathForTests = Path.Combine(TestContext.Current.ToolsetUnderTest.SdkFolderUnderTest, "RuntimeIdentifierGraph.json");
  122. downloader = new ToolPackageDownloader(store, runtimeJsonPathForTests);
  123. }
  124. return (store, downloader, reporter, fileSystem);
  125. }
  126. private FilePath WriteNugetConfigFileToPointToTheFeed(string testDirectory)
  127. {
  128. var nugetConfigName = "NuGet.Config";
  129. var tempPathForNugetConfigWithWhiteSpace =
  130. Path.Combine(testDirectory,
  131. Path.GetRandomFileName() + " " + Path.GetRandomFileName());
  132. Directory.CreateDirectory(tempPathForNugetConfigWithWhiteSpace);
  133. NuGetConfigWriter.Write(tempPathForNugetConfigWithWhiteSpace, GetTestLocalFeedPath());
  134. return new FilePath(Path.GetFullPath(Path.Combine(tempPathForNugetConfigWithWhiteSpace, nugetConfigName)));
  135. }
  136. private static string GetTestLocalFeedPath() => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestAssetLocalNugetFeed");
  137. private readonly string _testTargetframework = BundledTargetFramework.GetTargetFrameworkMoniker();
  138. private const string TestPackageVersion = "1.0.4";
  139. private static readonly VerbosityOptions TestVerbosity = new VerbosityOptions();
  140. private static readonly PackageId TestPackageId = new("global.tool.console.demo");
  141. }
  142. }