-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
namespace Chickensoft.GodotEnv.Tests; | ||
|
||
using System; | ||
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 envValue = "godotenv/godot/bin/godot"; | ||
|
||
// Given | ||
var processRunner = new Mock<IProcessRunner>(); | ||
|
||
// GetDefaultShell() | ||
processRunner.Setup( | ||
pr => pr.Run(WORKING_DIR, "sh", It.Is<string[]>( | ||
value => value.SequenceEqual(new[] { "-c", EnvironmentVariableClient.USER_SHELL_COMMAND_MAC }) | ||
)) | ||
).Returns(Task.FromResult(new ProcessResult(0, "bash"))); | ||
processRunner.Setup( | ||
pr => pr.Run(WORKING_DIR, "sh", It.Is<string[]>( | ||
value => value.SequenceEqual(new[] { "-c", EnvironmentVariableClient.USER_SHELL_COMMAND_LINUX }) | ||
)) | ||
).Returns(Task.FromResult(new ProcessResult(0, "bash"))); | ||
|
||
// GetUserEnv() | ||
processRunner.Setup( | ||
pr => pr.Run(WORKING_DIR, "bash", It.Is<string[]>( | ||
value => value.SequenceEqual(new[] { "-ic", $"echo ${env}" }) | ||
))).Returns(Task.FromResult(new ProcessResult(0, envValue))); | ||
|
||
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)); | ||
|
||
var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object, new Mock<EnvironmentClient>().Object); | ||
|
||
// var originalValue = envClient.GetUserEnv(env); | ||
|
||
// When | ||
await envClient.SetUserEnv(env, envValue); | ||
|
||
// Then | ||
envClient.GetUserEnv(env).ShouldBe(envValue); | ||
|
||
// Restoring original value | ||
// await envClient.SetUserEnv(env, originalValue); | ||
// envClient.GetUserEnv(env).ShouldBe(originalValue); | ||
} | ||
|
||
[Fact] | ||
public async void AppendToUserEnv() { | ||
var WORKING_DIR = "."; | ||
var env = Defaults.PATH_ENV_VAR_NAME; | ||
var envValue = "godotenv/godot/bin/godot"; | ||
|
||
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 processRunner = new Mock<IProcessRunner>(); | ||
|
||
// GetDefaultShell() | ||
processRunner.Setup( | ||
pr => pr.Run(WORKING_DIR, "sh", It.Is<string[]>( | ||
value => value.SequenceEqual(new[] { "-c", EnvironmentVariableClient.USER_SHELL_COMMAND_MAC }) | ||
)) | ||
).Returns(Task.FromResult(new ProcessResult(0, "bash"))); | ||
processRunner.Setup( | ||
pr => pr.Run(WORKING_DIR, "sh", It.Is<string[]>( | ||
value => value.SequenceEqual(new[] { "-c", EnvironmentVariableClient.USER_SHELL_COMMAND_LINUX }) | ||
)) | ||
).Returns(Task.FromResult(new ProcessResult(0, "bash"))); | ||
|
||
// GetUserEnv() | ||
processRunner.Setup( | ||
pr => pr.Run(WORKING_DIR, "bash", It.Is<string[]>( | ||
value => value.SequenceEqual(new[] { "-ic", $"echo ${env}" }) | ||
))).Returns(Task.FromResult(new ProcessResult(0, envValue))); | ||
|
||
var computer = new Mock<IComputer>(); | ||
computer.Setup(c => c.CreateShell(WORKING_DIR)).Returns(new Shell(processRunner.Object, WORKING_DIR)); | ||
|
||
var envClient = new Mock<IEnvironmentClient>(); | ||
envClient.Setup(ec => ec.GetEnvironmentVariable(env, EnvironmentVariableTarget.User)).Returns(envValue); | ||
|
||
var envVarClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object, envClient.Object); | ||
|
||
await envVarClient.AppendToUserEnv(env, envValue); | ||
|
||
envVarClient.GetUserEnv(env).ShouldContain(envValue); | ||
} | ||
|
||
[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, new Mock<EnvironmentClient>().Object); | ||
|
||
envClient.GetUserDefaultShell().ShouldBe(string.Empty); | ||
} | ||
|
||
[PlatformFact(TestPlatform.Mac)] | ||
public void GetDefaultShellOnMac() => GetDefaultShellUnixRoutine(OSType.MacOS, | ||
["-c", EnvironmentVariableClient.USER_SHELL_COMMAND_MAC]); | ||
|
||
[PlatformFact(TestPlatform.Linux)] | ||
public void GetDefaultShellOnLinux() => | ||
GetDefaultShellUnixRoutine(OSType.Linux, ["-c", EnvironmentVariableClient.USER_SHELL_COMMAND_LINUX]); | ||
|
||
private void GetDefaultShellUnixRoutine(OSType os, string[] shellArgs) { | ||
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 3.x Integration Tests with macos-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 3.x Integration Tests with macos-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 4.x Integration Tests with macos-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 4.x Integration Tests with macos-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔬 Unit Tests
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔬 Unit Tests
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 3.x Integration Tests with ubuntu-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 3.x Integration Tests with ubuntu-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 4.x Integration Tests with ubuntu-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 4.x Integration Tests with ubuntu-latest
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 3.x Integration Tests with windows-2019
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 3.x Integration Tests with windows-2019
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 4.x Integration Tests with windows-2019
Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs GitHub Actions / 🔋 Godot 4.x Integration Tests with windows-2019
|
||
var processRunner = new Mock<IProcessRunner>(); | ||
const string WORKING_DIR = "."; | ||
const int exitCode = 0; | ||
const string stdOutput = "bash"; | ||
const string exe = "sh"; | ||
|
||
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 envVarClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object, new Mock<EnvironmentClient>().Object); | ||
|
||
var result = envVarClient.GetUserDefaultShell(); | ||
|
||
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, new Mock<EnvironmentClient>().Object); | ||
|
||
envClient.IsShellSupported("any").ShouldBeTrue(); | ||
envClient.IsShellSupported(string.Empty).ShouldBeTrue(); | ||
} | ||
|
||
[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, new Mock<EnvironmentClient>().Object); | ||
|
||
envClient.IsShellSupported("zsh").ShouldBeTrue(); | ||
envClient.IsShellSupported("bash").ShouldBeTrue(); | ||
envClient.IsShellSupported("fish").ShouldBeFalse(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace Chickensoft.GodotEnv.Tests; | ||
|
||
|
||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
using Chickensoft.GodotEnv.Common.Utilities; | ||
using CliFx.Infrastructure; | ||
using Common.Clients; | ||
using Common.Models; | ||
using Downloader; | ||
using Features.Godot.Domain; | ||
using Features.Godot.Models; | ||
using Moq; | ||
using Xunit; | ||
|
||
public class GodotRepositoryTest { | ||
[Fact] | ||
public async void AddOrUpdateGodotEnvVariable() { | ||
var WORKING_DIR = "."; | ||
var godotVar = "GODOT"; | ||
|
||
var computer = new Mock<IComputer>(); | ||
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); | ||
|
||
fileClient.Setup(fc => fc.AppDataDirectory).Returns(WORKING_DIR); | ||
|
||
// GodotBinPath | ||
fileClient.Setup(fc => fc.Combine(fileClient.Object.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_BIN_NAME)) | ||
.Returns("/godot/bin/"); | ||
|
||
// GodotSymlinkPath | ||
fileClient.Setup(fc => fc.Combine(fileClient.Object.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_BIN_PATH, | ||
Defaults.GODOT_BIN_NAME)).Returns("/godot/bin/godot"); | ||
|
||
var networkClient = new Mock<NetworkClient>(new Mock<DownloadService>().Object, Defaults.DownloadConfiguration); | ||
var zipClient = new Mock<ZipClient>(fileClient.Object.Files); | ||
|
||
var environmentVariableClient = new Mock<IEnvironmentVariableClient>(); | ||
environmentVariableClient.Setup(evc => evc.SetUserEnv(It.IsAny<string>(), It.IsAny<string>())) | ||
.Returns(Task.CompletedTask); | ||
environmentVariableClient.Setup(evc => evc.AppendToUserEnv(It.IsAny<string>(), It.IsAny<string>())) | ||
.Returns(Task.CompletedTask); | ||
|
||
var platform = new Mock<GodotEnvironment>(fileClient.Object, computer.Object); | ||
|
||
var godotRepo = new GodotRepository( | ||
config: new ConfigFile { GodotInstallationsPath = "INSTALLATION_PATH" }, | ||
fileClient: fileClient.Object, | ||
networkClient: networkClient.Object, | ||
zipClient: zipClient.Object, | ||
platform: platform.Object, | ||
environmentVariableClient: environmentVariableClient.Object, | ||
processRunner: processRunner.Object | ||
); | ||
|
||
var executionContext = new Mock<IExecutionContext>(); | ||
var console = new FakeInMemoryConsole(); | ||
var log = new Mock<ILog>(); // Use real log to test colors in output | ||
|
||
executionContext.Setup(context => context.CreateLog(console)).Returns(log.Object); | ||
|
||
await godotRepo.AddOrUpdateGodotEnvVariable(log.Object); | ||
|
||
environmentVariableClient.Verify(mock => mock.SetUserEnv(godotVar, godotRepo.GodotSymlinkPath)); | ||
environmentVariableClient.Verify(mock => mock.AppendToUserEnv(Defaults.PATH_ENV_VAR_NAME, godotRepo.GodotBinPath)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Chickensoft.GodotEnv.Common.Clients; | ||
|
||
|
||
public interface IEnvironmentClient { | ||
string? GetEnvironmentVariable(string name, System.EnvironmentVariableTarget user); | ||
|
||
void SetEnvironmentVariable(string name, string value, System.EnvironmentVariableTarget user); | ||
} | ||
|
||
public class EnvironmentClient : IEnvironmentClient { | ||
public string? GetEnvironmentVariable(string name, System.EnvironmentVariableTarget user) => | ||
System.Environment.GetEnvironmentVariable(name, user); | ||
|
||
public void SetEnvironmentVariable(string name, string value, System.EnvironmentVariableTarget user) => | ||
System.Environment.SetEnvironmentVariable(name, value, user); | ||
} |