-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
230 additions
and
7 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
188
GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters