Skip to content

Commit

Permalink
WIP Implements tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edassis committed Dec 26, 2023
1 parent dd8be03 commit 8cd77de
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 7 deletions.
4 changes: 2 additions & 2 deletions GodotEnv.Tests/reports/branch_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions GodotEnv.Tests/reports/line_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions GodotEnv.Tests/reports/method_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 188 additions & 0 deletions GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs
Original file line number Diff line number Diff line change
@@ -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<IProcessRunner>();
// var processResult = new ProcessResult(0);

// GetDefaultShell()
processRunner.Setup(
pr => pr.Run(WORKING_DIR, "sh", It.Is<string[]>(
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<string[]>(
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<string[]>(
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<string[]>(
// value => value.SequenceEqual(new[] {"-c", "dscl . -read /Users/$USER UserShell | awk -F/ '{ print $NF }'"})
// ))
// ).Returns(Task.FromResult(processResult));

var fileClient = new Mock<IFileClient>();
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<IComputer>();
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<IProcessRunner>();
var fileClient = new Mock<IFileClient>();
var computer = new Mock<IComputer>();
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<IProcessRunner>();
var fileClient = new Mock<IFileClient>();
fileClient.Setup(fc => fc.OS).Returns(OSType.Windows);
fileClient.Setup(fc => fc.AppDataDirectory).Returns(".");
var computer = new Mock<IComputer>();
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<IProcessRunner>();
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<string[]>(
value => value.SequenceEqual(shellArgs)
))
).Returns(Task.FromResult(processResult));

var fileClient = new Mock<IFileClient>();
fileClient.Setup(fc => fc.OS).Returns(os);
fileClient.Setup(fc => fc.AppDataDirectory).Returns(WORKING_DIR);

var computer = new Mock<IComputer>();
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<IProcessRunner>();
var fileClient = new Mock<IFileClient>();
fileClient.Setup(fc => fc.OS).Returns(OSType.Windows);
var computer = new Mock<IComputer>();
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<IProcessRunner>();
var fileClient = new Mock<IFileClient>();
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<IComputer>();
var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object);

envClient.IsShellSupported("zsh").ShouldBeTrue();
envClient.IsShellSupported("bash").ShouldBeTrue();
envClient.IsShellSupported("fish").ShouldBeFalse();
}
}
18 changes: 18 additions & 0 deletions GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs
Original file line number Diff line number Diff line change
@@ -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.
}
6 changes: 6 additions & 0 deletions GodotEnv.Tests/test_utils/PlatformFact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
public enum TestPlatform {
Windows,
MacLinux,
Mac,
Linux
}

public sealed class PlatformFact : FactAttribute {
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
Expand Down
13 changes: 12 additions & 1 deletion GodotEnv/src/common/clients/EnvironmentVariableClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 8cd77de

Please sign in to comment.