Use rich mock to test InstallCommand (#8402)
To ensure the mock has the same behavior the component has, run mock under the same tests the adapter has. It is a common problem that moq has -- "everything is mocked out, you are not test anything"
This commit is contained in:
parent
f4a0d2db0e
commit
05052c0541
25 changed files with 881 additions and 113 deletions
|
@ -0,0 +1,58 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.ShellShim;
|
||||
using Microsoft.Extensions.EnvironmentAbstractions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Tests.ComponentMocks
|
||||
{
|
||||
internal class ShellShimMakerMock : IShellShimMaker
|
||||
{
|
||||
private static IFileSystem _fileSystem;
|
||||
private readonly string _pathToPlaceShim;
|
||||
|
||||
public ShellShimMakerMock(string pathToPlaceShim, IFileSystem fileSystem = null)
|
||||
{
|
||||
_pathToPlaceShim =
|
||||
pathToPlaceShim ?? throw new ArgumentNullException(nameof(pathToPlaceShim));
|
||||
|
||||
_fileSystem = fileSystem ?? new FileSystemWrapper();
|
||||
}
|
||||
|
||||
public void CreateShim(string packageExecutablePath, string shellCommandName)
|
||||
{
|
||||
var packageExecutable = new FilePath(packageExecutablePath);
|
||||
|
||||
|
||||
var fakeshim = new FakeShim
|
||||
{
|
||||
Runner = "dotnet",
|
||||
ExecutablePath = packageExecutable.Value
|
||||
};
|
||||
var script = JsonConvert.SerializeObject(fakeshim);
|
||||
|
||||
FilePath scriptPath = new FilePath(Path.Combine(_pathToPlaceShim, shellCommandName));
|
||||
_fileSystem.File.WriteAllText(scriptPath.Value, script);
|
||||
}
|
||||
|
||||
public void EnsureCommandNameUniqueness(string shellCommandName)
|
||||
{
|
||||
if (_fileSystem.File.Exists(Path.Combine(_pathToPlaceShim, shellCommandName)))
|
||||
{
|
||||
throw new GracefulException(
|
||||
string.Format(CommonLocalizableStrings.FailInstallToolSameName,
|
||||
shellCommandName));
|
||||
}
|
||||
}
|
||||
|
||||
public class FakeShim
|
||||
{
|
||||
public string Runner { get; set; }
|
||||
public string ExecutablePath { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue