Skip to content

Commit

Permalink
WIP Implements tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edassis committed Jan 2, 2024
1 parent dd8be03 commit 9c5a5bd
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 74 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.
198 changes: 198 additions & 0 deletions GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs
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

View workflow job for this annotation

GitHub Actions / 🔋 Godot 3.x Integration Tests with macos-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 3.x Integration Tests with macos-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 4.x Integration Tests with macos-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 4.x Integration Tests with macos-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔬 Unit Tests

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔬 Unit Tests

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 3.x Integration Tests with ubuntu-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 3.x Integration Tests with ubuntu-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 4.x Integration Tests with ubuntu-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 4.x Integration Tests with ubuntu-latest

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 3.x Integration Tests with windows-2019

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 3.x Integration Tests with windows-2019

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 4.x Integration Tests with windows-2019

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 139 in GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs

View workflow job for this annotation

GitHub Actions / 🔋 Godot 4.x Integration Tests with windows-2019

Member 'GetDefaultShellUnixRoutine' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
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();
}
}
75 changes: 75 additions & 0 deletions GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs
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));
}
}
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
3 changes: 2 additions & 1 deletion GodotEnv/src/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static async Task<int> Main(string[] args) {
var computer = new Computer();
var processRunner = new ProcessRunner();
var fileClient = new FileClient(new FileSystem(), computer, processRunner);
var environmentClient = new EnvironmentClient();
var configFileRepo = new ConfigFileRepository(fileClient);
var config = configFileRepo.LoadConfigFile(out var _);
var workingDir = Environment.CurrentDirectory;
Expand All @@ -43,7 +44,7 @@ public static async Task<int> Main(string[] args) {
? new ZipClient(fileClient.Files)
: new ZipClientTerminal(computer, fileClient.Files);
var environmentVariableClient =
new EnvironmentVariableClient(processRunner, fileClient, computer);
new EnvironmentVariableClient(processRunner, fileClient, computer, environmentClient);

// Addons feature dependencies

Expand Down
16 changes: 16 additions & 0 deletions GodotEnv/src/common/clients/EnvironmentClient.cs
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);
}
Loading

0 comments on commit 9c5a5bd

Please sign in to comment.