From aeb8c603389dd77bd02fb7638004ae8b3d563309 Mon Sep 17 00:00:00 2001 From: Chris Cheetham Date: Tue, 7 Aug 2018 18:35:49 -0400 Subject: [PATCH] Implements tests for Shell class --- src/Steeltoe.Tooling.System/Shell.cs | 30 ++++- .../Steeltoe.Tooling.System.Test/ShellTest.cs | 107 +++++++++++++++++- .../Steeltoe.Tooling.System.Test.csproj | 32 +++--- 3 files changed, 150 insertions(+), 19 deletions(-) diff --git a/src/Steeltoe.Tooling.System/Shell.cs b/src/Steeltoe.Tooling.System/Shell.cs index 040dd26..4bba865 100644 --- a/src/Steeltoe.Tooling.System/Shell.cs +++ b/src/Steeltoe.Tooling.System/Shell.cs @@ -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(); + } + } } } diff --git a/test/Steeltoe.Tooling.System.Test/ShellTest.cs b/test/Steeltoe.Tooling.System.Test/ShellTest.cs index df6cb69..6b99319 100644 --- a/test/Steeltoe.Tooling.System.Test/ShellTest.cs +++ b/test/Steeltoe.Tooling.System.Test/ShellTest.cs @@ -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(() => sh.Run("NoSuchCommand")); } } } diff --git a/test/Steeltoe.Tooling.System.Test/Steeltoe.Tooling.System.Test.csproj b/test/Steeltoe.Tooling.System.Test/Steeltoe.Tooling.System.Test.csproj index b9bc393..155296f 100644 --- a/test/Steeltoe.Tooling.System.Test/Steeltoe.Tooling.System.Test.csproj +++ b/test/Steeltoe.Tooling.System.Test/Steeltoe.Tooling.System.Test.csproj @@ -1,16 +1,16 @@ - - - - netcoreapp2.1 - - false - - - - - - - - - - + + + netcoreapp2.1 + false + + + + + + + + + + + + \ No newline at end of file