NoRestoreTests.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 Microsoft.Extensions.Tools.Internal;
  4. namespace Microsoft.DotNet.Watcher.Tools
  5. {
  6. public class NoRestoreTests
  7. {
  8. private readonly string[] _arguments = new[] { "run" };
  9. [Fact]
  10. public void ProcessAsync_LeavesArgumentsUnchangedOnFirstRun()
  11. {
  12. var context = new DotNetWatchContext
  13. {
  14. Reporter = NullReporter.Singleton,
  15. LaunchSettingsProfile = new(),
  16. Options = TestOptions.CommandLine,
  17. EnvironmentOptions = TestOptions.Environmental,
  18. };
  19. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  20. var processSpec = new ProcessSpec
  21. {
  22. Arguments = _arguments,
  23. };
  24. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  25. Assert.Same(_arguments, processSpec.Arguments);
  26. }
  27. [Fact]
  28. public void ProcessAsync_LeavesArgumentsUnchangedIfMsBuildRevaluationIsRequired()
  29. {
  30. var context = new DotNetWatchContext
  31. {
  32. Reporter = NullReporter.Singleton,
  33. LaunchSettingsProfile = new(),
  34. Options = TestOptions.CommandLine,
  35. EnvironmentOptions = TestOptions.Environmental,
  36. };
  37. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  38. var processSpec = new ProcessSpec
  39. {
  40. Arguments = _arguments,
  41. };
  42. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  43. evaluator.RequiresRevaluation = true;
  44. evaluator.UpdateProcessArguments(processSpec, iteration: 1);
  45. Assert.Same(_arguments, processSpec.Arguments);
  46. }
  47. [Fact]
  48. public void ProcessAsync_LeavesArgumentsUnchangedIfOptimizationIsSuppressed()
  49. {
  50. var context = new DotNetWatchContext
  51. {
  52. Reporter = NullReporter.Singleton,
  53. LaunchSettingsProfile = new(),
  54. Options = TestOptions.CommandLine,
  55. EnvironmentOptions = TestOptions.Environmental with { SuppressMSBuildIncrementalism = true },
  56. };
  57. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  58. var processSpec = new ProcessSpec
  59. {
  60. Arguments = _arguments,
  61. };
  62. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  63. evaluator.UpdateProcessArguments(processSpec, iteration: 1);
  64. Assert.Same(_arguments, processSpec.Arguments);
  65. }
  66. [Fact]
  67. public void ProcessAsync_AddsNoRestoreSwitch()
  68. {
  69. var context = new DotNetWatchContext
  70. {
  71. Reporter = NullReporter.Singleton,
  72. LaunchSettingsProfile = new(),
  73. Options = TestOptions.CommandLine,
  74. EnvironmentOptions = TestOptions.Environmental,
  75. };
  76. var processSpec = new ProcessSpec
  77. {
  78. Arguments = _arguments,
  79. };
  80. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  81. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  82. evaluator.UpdateProcessArguments(processSpec, iteration: 1);
  83. Assert.Equal(new[] { "run", "--no-restore" }, processSpec.Arguments);
  84. }
  85. [Fact]
  86. public void ProcessAsync_AddsNoRestoreSwitch_WithAdditionalArguments()
  87. {
  88. var context = new DotNetWatchContext
  89. {
  90. Reporter = NullReporter.Singleton,
  91. LaunchSettingsProfile = new(),
  92. Options = TestOptions.CommandLine,
  93. EnvironmentOptions = TestOptions.Environmental,
  94. };
  95. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  96. var processSpec = new ProcessSpec
  97. {
  98. Arguments = ["run", "-f", ToolsetInfo.CurrentTargetFramework, "--", "foo=bar"],
  99. };
  100. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  101. evaluator.UpdateProcessArguments(processSpec, iteration: 1);
  102. Assert.Equal(["run", "--no-restore", "-f", ToolsetInfo.CurrentTargetFramework, "--", "foo=bar"], processSpec.Arguments);
  103. }
  104. [Fact]
  105. public void ProcessAsync_AddsNoRestoreSwitch_ForTestCommand()
  106. {
  107. var context = new DotNetWatchContext
  108. {
  109. Reporter = NullReporter.Singleton,
  110. LaunchSettingsProfile = new(),
  111. Options = TestOptions.CommandLine,
  112. EnvironmentOptions = TestOptions.Environmental,
  113. };
  114. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  115. var processSpec = new ProcessSpec
  116. {
  117. Arguments = ["test", "--filter SomeFilter"],
  118. };
  119. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  120. evaluator.UpdateProcessArguments(processSpec, iteration: 1);
  121. Assert.Equal(["test", "--no-restore", "--filter SomeFilter"], processSpec.Arguments);
  122. }
  123. [Fact]
  124. public void ProcessAsync_DoesNotModifyArgumentsForUnknownCommands()
  125. {
  126. var arguments = new[] { "ef", "database", "update" };
  127. var context = new DotNetWatchContext
  128. {
  129. Reporter = NullReporter.Singleton,
  130. LaunchSettingsProfile = new(),
  131. Options = TestOptions.CommandLine,
  132. EnvironmentOptions = TestOptions.Environmental,
  133. };
  134. var evaluator = new BuildEvaluator(context, new MockFileSetFactory());
  135. var processSpec = new ProcessSpec
  136. {
  137. Arguments = arguments,
  138. };
  139. evaluator.UpdateProcessArguments(processSpec, iteration: 0);
  140. evaluator.UpdateProcessArguments(processSpec, iteration: 1);
  141. Assert.Same(arguments, processSpec.Arguments);
  142. }
  143. }
  144. }