This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fallback to mono when running on Linux or MacOS for Nuget.exe (#829)
* When running not on windows try to fallback to mono for Nuget.exe That makes nuget updates practically crossplatform. * Review comments Better error messages Better method naming * Add a missing dot * Pass correct arguments to the external process * Unit tests for Mono Executor * Adjust integration tests to run cross platform On mac restore command was required to run this integration test. Otherwise `Unable to locate the packages folder. Verify all the packages are restored before running 'nuget.exe update'.` error was thrown. * Update travis config to use mono * Fix code style and typos * Try to run azure pipeline on linux as well * Run code coverage only on windows * Add note about experimental support for mono * Log error when NuGet.exe can't be executed * Fix casing of macOS in docs * Remove Task.FromResult in favour of NSubstitute native overload
- Loading branch information
1 parent
fe974fa
commit f8baf2a
Showing
11 changed files
with
237 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: csharp | ||
mono: none | ||
mono: 5.18.1 | ||
dist: xenial | ||
dotnet: 2.2.202 | ||
script: | ||
- dotnet build -c Release NuKeeper.sln /m:1 | ||
- dotnet test -c Release NuKeeper.sln --filter "TestCategory!=WindowsOnly" | ||
- dotnet test -c Release NuKeeper.sln |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NSubstitute; | ||
using NuKeeper.Abstractions.Logging; | ||
using NuKeeper.Update.Process; | ||
using NuKeeper.Update.ProcessRunner; | ||
using NUnit.Framework; | ||
|
||
namespace NuKeeper.Update.Tests.Process | ||
{ | ||
[TestFixture] | ||
public class MonoExecutorTests | ||
{ | ||
[TestCase(0, true)] | ||
[TestCase(1, false)] | ||
public async Task WhenCallingCanRun_ShouldCheckExternalProcessResult(int exitCode, bool expectedCanExecute) | ||
{ | ||
var nuKeeperLogger = Substitute.For<INuKeeperLogger>(); | ||
var externalProcess = Substitute.For<IExternalProcess>(); | ||
|
||
externalProcess.Run("","mono","--version",false). | ||
Returns(new ProcessOutput("","",exitCode)); | ||
|
||
var monoExecutor = new MonoExecutor(nuKeeperLogger, externalProcess); | ||
|
||
var canRun = await monoExecutor.CanRun(); | ||
|
||
Assert.AreEqual(expectedCanExecute, canRun); | ||
} | ||
|
||
[Test] | ||
public async Task WhenCallingCanRun_ShouldOnlyCallExternalProcessOnce() | ||
{ | ||
var nuKeeperLogger = Substitute.For<INuKeeperLogger>(); | ||
var externalProcess = Substitute.For<IExternalProcess>(); | ||
|
||
externalProcess.Run("","mono","--version",false). | ||
Returns(new ProcessOutput("","",0)); | ||
|
||
var monoExecutor = new MonoExecutor(nuKeeperLogger, externalProcess); | ||
|
||
await monoExecutor.CanRun(); | ||
await monoExecutor.CanRun(); | ||
await monoExecutor.CanRun(); | ||
|
||
await externalProcess.Received(1).Run( | ||
"", | ||
"mono", | ||
"--version", | ||
false); | ||
} | ||
|
||
[Test] | ||
public void WhenCallingRun_ShouldThrowIfMonoWasNotFound() | ||
{ | ||
var nuKeeperLogger = Substitute.For<INuKeeperLogger>(); | ||
var externalProcess = Substitute.For<IExternalProcess>(); | ||
|
||
externalProcess.Run("","mono","--version",false). | ||
Returns(new ProcessOutput("","",1)); | ||
|
||
var monoExecutor = new MonoExecutor(nuKeeperLogger, externalProcess); | ||
|
||
Assert.ThrowsAsync<InvalidOperationException>(async () => | ||
await monoExecutor.Run("wd", "command", "args", true)); | ||
} | ||
|
||
[Test] | ||
public async Task WhenCallingRun_ShouldPassArgumentToUnderlyingExternalProcess() | ||
{ | ||
var nuKeeperLogger = Substitute.For<INuKeeperLogger>(); | ||
var externalProcess = Substitute.For<IExternalProcess>(); | ||
|
||
externalProcess.Run("","mono","--version",false). | ||
Returns(new ProcessOutput("","",0)); | ||
|
||
var monoExecutor = new MonoExecutor(nuKeeperLogger, externalProcess); | ||
await monoExecutor.Run("wd", "command", "args", true); | ||
|
||
await externalProcess.Received(1).Run("wd", "mono", "command args", true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Threading.Tasks; | ||
using NuKeeper.Update.ProcessRunner; | ||
|
||
namespace NuKeeper.Update.Process | ||
{ | ||
public interface IMonoExecutor: IExternalProcess | ||
{ | ||
Task<bool> CanRun(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NuGet.Common; | ||
using NuKeeper.Abstractions.Logging; | ||
using NuKeeper.Update.ProcessRunner; | ||
|
||
namespace NuKeeper.Update.Process | ||
{ | ||
public class MonoExecutor : IMonoExecutor | ||
{ | ||
private readonly INuKeeperLogger _logger; | ||
private readonly IExternalProcess _externalProcess; | ||
|
||
private readonly AsyncLazy<bool> _checkMono; | ||
|
||
public MonoExecutor(INuKeeperLogger logger, IExternalProcess externalProcess) | ||
{ | ||
_logger = logger; | ||
_externalProcess = externalProcess; | ||
_checkMono = new AsyncLazy<bool>(CheckMonoExists); | ||
} | ||
|
||
public async Task<bool> CanRun() | ||
{ | ||
return await _checkMono; | ||
} | ||
|
||
public async Task<ProcessOutput> Run(string workingDirectory, string command, string arguments, bool ensureSuccess) | ||
{ | ||
_logger.Normal($"Using Mono to run '{command}'"); | ||
|
||
if (!await CanRun()) | ||
{ | ||
_logger.Error($"Cannot run '{command}' on Mono since Mono installation was not found"); | ||
throw new InvalidOperationException("Mono installation was not found"); | ||
} | ||
|
||
return await _externalProcess.Run(workingDirectory, "mono", $"{command} {arguments}", ensureSuccess); | ||
} | ||
|
||
private async Task<bool> CheckMonoExists() | ||
{ | ||
var result = await _externalProcess.Run("", "mono", "--version", false); | ||
|
||
return result.Success; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.