Implements tests for Shell class

This commit is contained in:
Chris Cheetham 2018-08-07 18:35:49 -04:00
Родитель 51de7d8be8
Коммит aeb8c60338
3 изменённых файлов: 150 добавлений и 19 удалений

Просмотреть файл

@ -12,11 +12,39 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Diagnostics;
namespace Steeltoe.Tooling.System
{
public class Shell
{
public string Command { get; private set; }
public string Arguments { get; private set; }
public int ExitCode { get; private set; }
public string Out { get; private set; }
public string Error { get; private set; }
public void Run(string command, string arguments = null, string workingDirectory = null)
{
Command = command;
Arguments = arguments;
var pinfo = new ProcessStartInfo(Command, Arguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = workingDirectory
};
var proc = Process.Start(pinfo);
proc.WaitForExit();
ExitCode = proc.ExitCode;
using (var pout = proc.StandardOutput)
{
Out = pout.ReadToEnd();
}
using (var perr = proc.StandardError)
{
Error = perr.ReadToEnd();
}
}
}
}

Просмотреть файл

@ -12,16 +12,119 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using Shouldly;
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;
namespace Steeltoe.Tooling.System.Test
{
public class ShellTest
{
[Fact]
public void TestThis()
private static string testDir;
private static string aDir;
private static string anotherDir;
private static string listCommand;
private static string errorCommand;
private Shell sh;
static ShellTest()
{
// setup a test sandbox
testDir = Path.Combine(Directory.GetCurrentDirectory(), "shelltest");
Directory.CreateDirectory(testDir);
// setup up list and error commands
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
listCommand = "dir";
errorCommand = Path.Combine(testDir, "error.bat");
File.WriteAllText(errorCommand, "exit /B 1\n");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
listCommand = "ls";
errorCommand = "/usr/bin/false";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
listCommand = "ls";
errorCommand = "/usr/false";
}
else
{
throw new Exception("Don't know how to list a directory on " + RuntimeInformation.OSDescription);
}
// setup some test directories
aDir = Path.Combine(testDir, "aDir");
Directory.CreateDirectory(aDir);
File.WriteAllText(Path.Combine(aDir, "aFile"), string.Empty);
anotherDir = Path.Combine(testDir, "anotherDir");
Directory.CreateDirectory(anotherDir);
File.WriteAllText(Path.Combine(anotherDir, "anotherFile"), string.Empty);
}
public ShellTest()
{
sh = new Shell();
}
[Fact]
public void TestConstructor()
{
sh.Command.ShouldBeNull();
sh.Arguments.ShouldBeNull();
sh.ExitCode.ShouldBe(0);
sh.Out.ShouldBeNull();
sh.Error.ShouldBeNull();
}
[Fact]
public void TestRun()
{
sh.Run(listCommand);
sh.Command.ShouldBe(listCommand);
sh.Arguments.ShouldBeNull();
sh.ExitCode.ShouldBe(0);
sh.Out.ShouldNotBeEmpty();
sh.Error.ShouldBeEmpty();
}
[Fact]
public void TestRunWithArguments()
{
sh.Run(listCommand, aDir);
sh.Command.ShouldBe(listCommand);
sh.Arguments.ShouldBe(aDir);
sh.ExitCode.ShouldBe(0);
sh.Out.Trim().ShouldBe("aFile");
sh.Error.ShouldBeEmpty();
}
[Fact]
public void TestRunWithWorkingDirectory()
{
sh.Run(listCommand, workingDirectory: anotherDir);
sh.Command.ShouldBe(listCommand);
sh.Arguments.ShouldBeNull();
sh.ExitCode.ShouldBe(0);
sh.Out.Trim().ShouldBe("anotherFile");
sh.Error.ShouldBeEmpty();
}
[Fact]
public void TestRunCommandError()
{
sh.Run(errorCommand);
sh.ExitCode.ShouldNotBe(0);
}
[Fact]
public void TestRunSystemError()
{
Assert.Throws<Win32Exception>(() => sh.Run("NoSuchCommand"));
}
}
}

Просмотреть файл

@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0"/>
<PackageReference Include="xunit" Version="2.3.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1"/>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1"/>
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Steeltoe.Tooling.System\Steeltoe.Tooling.System.csproj" />
</ItemGroup>
</Project>