From 8cd77de498c43f3fdb939ab7a552a4682e15c370 Mon Sep 17 00:00:00 2001 From: Eduardo Assis Date: Sat, 16 Dec 2023 00:16:37 -0300 Subject: [PATCH] WIP Implements tests --- GodotEnv.Tests/reports/branch_coverage.svg | 4 +- GodotEnv.Tests/reports/line_coverage.svg | 4 +- GodotEnv.Tests/reports/method_coverage.svg | 4 +- .../clients/EnvironmentVariableClientTest.cs | 188 ++++++++++++++++++ .../godot/domain/GodotRepositoryTest.cs | 18 ++ GodotEnv.Tests/test_utils/PlatformFact.cs | 6 + .../clients/EnvironmentVariableClient.cs | 13 +- 7 files changed, 230 insertions(+), 7 deletions(-) create mode 100644 GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs create mode 100644 GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs diff --git a/GodotEnv.Tests/reports/branch_coverage.svg b/GodotEnv.Tests/reports/branch_coverage.svg index 6729d89..422135b 100644 --- a/GodotEnv.Tests/reports/branch_coverage.svg +++ b/GodotEnv.Tests/reports/branch_coverage.svg @@ -94,14 +94,14 @@ - Generated by: ReportGenerator 5.1.17.0 + Generated by: ReportGenerator 5.2.0.0 Coverage Coverage - 39.3%39.3% + 40.9%40.9% diff --git a/GodotEnv.Tests/reports/line_coverage.svg b/GodotEnv.Tests/reports/line_coverage.svg index 20e192e..6f01789 100644 --- a/GodotEnv.Tests/reports/line_coverage.svg +++ b/GodotEnv.Tests/reports/line_coverage.svg @@ -94,13 +94,13 @@ - Generated by: ReportGenerator 5.1.17.0 + Generated by: ReportGenerator 5.2.0.0 Coverage Coverage - 47.5%47.5% + 45%45% diff --git a/GodotEnv.Tests/reports/method_coverage.svg b/GodotEnv.Tests/reports/method_coverage.svg index 9e285b0..47f8af2 100644 --- a/GodotEnv.Tests/reports/method_coverage.svg +++ b/GodotEnv.Tests/reports/method_coverage.svg @@ -94,7 +94,7 @@ - Generated by: ReportGenerator 5.1.17.0 + Generated by: ReportGenerator 5.2.0.0 @@ -102,7 +102,7 @@ Coverage - 50.1%50.1% + 48.9%48.9% diff --git a/GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs b/GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs new file mode 100644 index 0000000..2f846ec --- /dev/null +++ b/GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs @@ -0,0 +1,188 @@ +namespace Chickensoft.GodotEnv.Tests; + +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using Chickensoft.GodotEnv.Common.Utilities; +using Common.Clients; +using Common.Models; +using Moq; +using Shouldly; +using Xunit; + +public class EnvironmentVariableClientTest { + [Fact] + public async void SetUserEnv() { + const string WORKING_DIR = "."; + var env = "GODOT"; + var env_value = "godotenv/godot/bin/godot"; + + // Given + var processRunner = new Mock(); + // var processResult = new ProcessResult(0); + + // GetDefaultShell() + processRunner.Setup( + pr => pr.Run(WORKING_DIR, "sh", It.Is( + value => value.SequenceEqual(new[] {"-c", "dscl . -read /Users/$USER UserShell | awk -F/ '{ print $NF }'"}) + )) + ).Returns(Task.FromResult(new ProcessResult(0, "sh"))); + + processRunner.Setup( + pr => pr.Run(WORKING_DIR, "sh", It.Is( + value => value.SequenceEqual(new[] {"-c", "getent passwd $USER | awk -F/ '{ print $NF }'"}) + )) + ).Returns(Task.FromResult(new ProcessResult(0, "sh"))); + // processRunner.Setup(pr => pr.Run(WORKING_DIR, "sh", new[] { "-ic", $"echo ${env}" })).CallBase(); + + // Get env-var + processRunner.Setup( + pr => pr.Run(WORKING_DIR, "sh", It.Is( + value => value.SequenceEqual(new[] { "-ic", $"echo ${env}" }) + ))).Returns(Task.FromResult(new ProcessResult(0, env_value))); + + // processRunner.Setup( + // pr => pr.Run(WORKING_DIR, "sh", It.Is( + // value => value.SequenceEqual(new[] {"-c", "dscl . -read /Users/$USER UserShell | awk -F/ '{ print $NF }'"}) + // )) + // ).Returns(Task.FromResult(processResult)); + + var fileClient = new Mock(); + fileClient.Setup(fc => fc.OS).Returns(FileClient.IsOSPlatform(OSPlatform.OSX) + ? OSType.MacOS + : FileClient.IsOSPlatform(OSPlatform.Linux) + ? OSType.Linux + : FileClient.IsOSPlatform(OSPlatform.Windows) + ? OSType.Windows + : OSType.Unknown); + fileClient.Setup(fc => fc.AppDataDirectory).Returns(WORKING_DIR); + + var computer = new Mock(); + computer.Setup(c => c.CreateShell(WORKING_DIR)).Returns(new Shell(processRunner.Object, WORKING_DIR)); + // computer.Setup(c => c.CreateShell(WORKING_DIR)).CallBase(); + + var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object); + + // TODO: Mock Environment. + // ! On Unix it's returning the mocked value, not the system env-value. + var originalValue = envClient.GetUserEnv(env); + + // fileClient.CallBase = true; + // var value = envClient.FileClient.Combine(Defaults.GODOT_PATH, Defaults.GODOT_BIN_PATH, Defaults.GODOT_BIN_NAME); + // var value = fileClient.Object.Combine(Defaults.GODOT_PATH, Defaults.GODOT_BIN_PATH, Defaults.GODOT_BIN_NAME); + + // When + await envClient.SetUserEnv(env, env_value); + + // Then + envClient.GetUserEnv(env).ShouldBe(env_value); + + // Restoring original value + await envClient.SetUserEnv(env, originalValue); + envClient.GetUserEnv(env).ShouldBe(originalValue); + } + + // TODO: Fix test on UNIX. + [Fact] + public async void AppendToUserEnv() { + var processRunner = new Mock(); + var fileClient = new Mock(); + var computer = new Mock(); + var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object); + var env = "PATH"; + var value = "godotenv/godot/bin/godot"; + var originalValue = envClient.GetUserEnv(env); + + await envClient.AppendToUserEnv(env, value); + + envClient.GetUserEnv(env).ShouldContain(value); + + // Restoring original value + await envClient.SetUserEnv(env, originalValue); + envClient.GetUserEnv(env).ShouldBe(originalValue); + } + + [PlatformFact(TestPlatform.Windows)] + public void GetDefaultShellOnWindows() { + var processRunner = new Mock(); + var fileClient = new Mock(); + fileClient.Setup(fc => fc.OS).Returns(OSType.Windows); + fileClient.Setup(fc => fc.AppDataDirectory).Returns("."); + var computer = new Mock(); + var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object); + + envClient.GetDefaultShell().ShouldBe(string.Empty); + } + + [PlatformFact(TestPlatform.Mac)] + public void GetDefaultShellOnMac() => GetDefaultShellUnixRoutine(OSType.MacOS, + ["-c", "dscl . -read /Users/$USER UserShell | awk -F/ '{ print $NF }'"]); + + [PlatformFact(TestPlatform.Linux)] + public void GetDefaultShellOnLinux() => + GetDefaultShellUnixRoutine(OSType.Linux, ["-c", "getent passwd $USER | awk -F/ '{ print $NF }'"]); + + private void GetDefaultShellUnixRoutine(OSType os, string[] shellArgs) { + var processRunner = new Mock(); + const string WORKING_DIR = "."; + const int exitCode = 0; + const string stdOutput = "zsh"; + const string exe = "sh"; + // string[] args = ["-c", "getent passwd $USER | awk -F/ '{ print $NF }'"]; + var processResult = new ProcessResult(exitCode, stdOutput); + processRunner.Setup( + pr => pr.Run(WORKING_DIR, exe, It.Is( + value => value.SequenceEqual(shellArgs) + )) + ).Returns(Task.FromResult(processResult)); + + var fileClient = new Mock(); + fileClient.Setup(fc => fc.OS).Returns(os); + fileClient.Setup(fc => fc.AppDataDirectory).Returns(WORKING_DIR); + + var computer = new Mock(); + computer.Setup(c => c.CreateShell(WORKING_DIR)).Returns(new Shell(processRunner.Object, WORKING_DIR)); + + var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object); + // var shell = new Shell(processRunner.Object, WORKING_DIR); + + // var result = await shell.Run(exe, args); + var result = envClient.GetDefaultShell(); + // result.ExitCode.ShouldBe(exitCode); + // result.Succeeded.ShouldBe(true); + result.ShouldBe(stdOutput); + processRunner.VerifyAll(); + } + + [PlatformFact(TestPlatform.Windows)] + public void CheckSupportedShellOnWindows() { + var processRunner = new Mock(); + var fileClient = new Mock(); + fileClient.Setup(fc => fc.OS).Returns(OSType.Windows); + var computer = new Mock(); + var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object); + + envClient.IsShellSupported("powershell").ShouldBeTrue(); + envClient.IsShellSupported("cmd").ShouldBeTrue(); + envClient.IsShellSupported("bash").ShouldBeFalse(); + } + + [PlatformFact(TestPlatform.MacLinux)] + public void CheckSupportedShellOnMacLinux() { + var processRunner = new Mock(); + var fileClient = new Mock(); + fileClient.Setup(fc => fc.OS).Returns(FileClient.IsOSPlatform(OSPlatform.OSX) + ? OSType.MacOS + : FileClient.IsOSPlatform(OSPlatform.Linux) + ? OSType.Linux + : FileClient.IsOSPlatform(OSPlatform.Windows) + ? OSType.Windows + : OSType.Unknown); + var computer = new Mock(); + var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object); + + envClient.IsShellSupported("zsh").ShouldBeTrue(); + envClient.IsShellSupported("bash").ShouldBeTrue(); + envClient.IsShellSupported("fish").ShouldBeFalse(); + } +} diff --git a/GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs b/GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs new file mode 100644 index 0000000..558c031 --- /dev/null +++ b/GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs @@ -0,0 +1,18 @@ +namespace Chickensoft.GodotEnv.Tests; +using System; +using System.Threading.Tasks; +using Chickensoft.GodotEnv.Common.Utilities; +using Shouldly; +using Xunit; + +public class GodotRepositoryTest { + + // [PlatformFact(TestPlatform.Windows)] + + [Fact] + public async void AddOrUpdateGodotEnvVariable() { + + } + + // TODO: Make tests. +} diff --git a/GodotEnv.Tests/test_utils/PlatformFact.cs b/GodotEnv.Tests/test_utils/PlatformFact.cs index 93eed95..8d1738b 100644 --- a/GodotEnv.Tests/test_utils/PlatformFact.cs +++ b/GodotEnv.Tests/test_utils/PlatformFact.cs @@ -6,6 +6,8 @@ public enum TestPlatform { Windows, MacLinux, + Mac, + Linux } public sealed class PlatformFact : FactAttribute { @@ -13,6 +15,10 @@ public PlatformFact(TestPlatform testPlatform) { Skip = testPlatform switch { TestPlatform.Windows when !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) => $"Skipped Windows specific test", + TestPlatform.Mac when !RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => + $"Skipped Mac specific test", + TestPlatform.Linux when !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) => + $"Skipped Linux specific test", TestPlatform.MacLinux when RuntimeInformation.IsOSPlatform(OSPlatform.Windows) => $"Skipped Mac/Linux specific test", _ => Skip diff --git a/GodotEnv/src/common/clients/EnvironmentVariableClient.cs b/GodotEnv/src/common/clients/EnvironmentVariableClient.cs index 3e0d1a5..467c352 100644 --- a/GodotEnv/src/common/clients/EnvironmentVariableClient.cs +++ b/GodotEnv/src/common/clients/EnvironmentVariableClient.cs @@ -161,5 +161,16 @@ public string GetDefaultShell() // Should be called on CLI initialization when making environment validation. // Can be used to guarantee that the user's default shell is supported. - public bool IsShellSupported(string shellName) => new[] { "bash", "zsh" }.Contains(shellName.ToLower()); + public bool IsShellSupported(string shellName) { + switch (FileClient.OS) { + case OSType.MacOS: + case OSType.Linux: + return new[] { "bash", "zsh" }.Contains(shellName.ToLower()); + break; + case OSType.Windows: + return new[] { "powershell", "cmd" }.Contains(shellName.ToLower()); + } + + return false; + } }