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:
William Lee 2018-01-24 10:16:27 -08:00 committed by GitHub
parent f4a0d2db0e
commit 05052c0541
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 881 additions and 113 deletions

View file

@ -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; }
}
}
}