ToolPackageMock.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Text.Json;
  4. using Microsoft.DotNet.Cli.Utils;
  5. using Microsoft.DotNet.ToolPackage;
  6. using Microsoft.Extensions.EnvironmentAbstractions;
  7. using NuGet.Frameworks;
  8. using NuGet.Versioning;
  9. namespace Microsoft.DotNet.Tools.Tests.ComponentMocks
  10. {
  11. internal class ToolPackageMock : IToolPackage
  12. {
  13. private IFileSystem _fileSystem;
  14. private Lazy<IReadOnlyList<RestoredCommand>> _commands;
  15. private IEnumerable<string> _warnings;
  16. private readonly IReadOnlyList<FilePath> _packagedShims;
  17. public ToolPackageMock(
  18. IFileSystem fileSystem,
  19. PackageId id,
  20. NuGetVersion version,
  21. DirectoryPath packageDirectory,
  22. IEnumerable<string> warnings = null,
  23. IReadOnlyList<FilePath> packagedShims = null,
  24. IEnumerable<NuGetFramework> frameworks = null)
  25. {
  26. _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
  27. Id = id;
  28. Version = version ?? throw new ArgumentNullException(nameof(version));
  29. PackageDirectory = packageDirectory;
  30. _commands = new Lazy<IReadOnlyList<RestoredCommand>>(GetCommands);
  31. _warnings = warnings ?? new List<string>();
  32. _packagedShims = packagedShims ?? new List<FilePath>();
  33. Frameworks = frameworks ?? new List<NuGetFramework>();
  34. }
  35. public PackageId Id { get; private set; }
  36. public NuGetVersion Version { get; private set; }
  37. public DirectoryPath PackageDirectory { get; private set; }
  38. public IReadOnlyList<RestoredCommand> Commands
  39. {
  40. get
  41. {
  42. return _commands.Value;
  43. }
  44. }
  45. public IEnumerable<string> Warnings => _warnings;
  46. public IReadOnlyList<FilePath> PackagedShims
  47. {
  48. get
  49. {
  50. return _packagedShims;
  51. }
  52. }
  53. public IEnumerable<NuGetFramework> Frameworks { get; private set; }
  54. private IReadOnlyList<RestoredCommand> GetCommands()
  55. {
  56. try
  57. {
  58. // The mock restorer wrote the path to the executable into project.assets.json (not a real assets file)
  59. // Currently only "dotnet" commands are supported
  60. var executablePath = _fileSystem.File.ReadAllText(Path.Combine(PackageDirectory.Value, "project.assets.json"));
  61. var fakeSettingFile = _fileSystem.File.ReadAllText(Path.Combine(PackageDirectory.Value, ProjectRestorerMock.FakeCommandSettingsFileName));
  62. string name;
  63. using (JsonDocument doc = JsonDocument.Parse(fakeSettingFile))
  64. {
  65. JsonElement root = doc.RootElement;
  66. name = root.GetProperty("Name").GetString();
  67. }
  68. return new RestoredCommand[]
  69. {
  70. new RestoredCommand(
  71. new ToolCommandName(name),
  72. "dotnet",
  73. PackageDirectory.WithFile(executablePath))
  74. };
  75. }
  76. catch (IOException ex)
  77. {
  78. throw new ToolPackageException(
  79. string.Format(
  80. CommonLocalizableStrings.FailedToRetrieveToolConfiguration,
  81. Id,
  82. ex.Message),
  83. ex);
  84. }
  85. }
  86. }
  87. }