dotnet-installer/test/Microsoft.DotNet.Tools.Tests.ComponentMocks/ShellShimMakerMock.cs
William Lee 05052c0541
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"
2018-01-24 10:16:27 -08:00

58 lines
1.9 KiB
C#

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