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 16, 2023
1 parent fe124d4 commit 32a4ab7
Show file tree
Hide file tree
Showing 7 changed files with 167 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.
127 changes: 127 additions & 0 deletions GodotEnv.Tests/src/common/clients/EnvironmentVariableClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
namespace Chickensoft.GodotEnv.Tests;

using System.Linq;
using System.Threading.Tasks;
using Chickensoft.GodotEnv.Common.Utilities;
using Common.Clients;
using Moq;
using Shouldly;
using Xunit;

public class EnvironmentVariableClientTest
{
[Fact]
public async void SetUserEnv()
{
// Given
var processRunner = new Mock<IProcessRunner>();
var fileClient = new Mock<IFileClient>();
// fileClient.Setup(fc => fc.Combine()).CallBase();
var computer = new Mock<IComputer>();
var envClient = new EnvironmentVariableClient(processRunner.Object, fileClient.Object, computer.Object);
var env = "GODOT";
var value = "godotenv/godot/bin/godot";
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, value);

// Then
envClient.GetUserEnv(env).ShouldBe(value);

// Restoring original value
await envClient.SetUserEnv(env, originalValue);
envClient.GetUserEnv(env).ShouldBe(originalValue);
}

[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>();
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(["-c", "dscl . -read /Users/$USER UserShell | awk -F/ '{ print $NF }'"]);

[PlatformFact(TestPlatform.Linux)]
public void GetDefaultShellOnLinux() => GetDefaultShellUnixRoutine(["-c", "getent passwd $USER | awk -F/ '{ print $NF }'"]);

private void GetDefaultShellUnixRoutine(string[] shellArgs) {

Check warning on line 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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 76 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)
var processRunner = new Mock<IProcessRunner>();
var fileClient = new Mock<IFileClient>();
var computer = new Mock<IComputer>();

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 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>();
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>();
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();
}
}
16 changes: 16 additions & 0 deletions GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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() {

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

GitHub Actions / 🔬 Unit Tests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

GitHub Actions / 🔬 Unit Tests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 13 in GodotEnv.Tests/src/features/godot/domain/GodotRepositoryTest.cs

View workflow job for this annotation

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

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

}
}
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 @@ -152,5 +152,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;

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

GitHub Actions / 🔬 Unit Tests

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

GitHub Actions / 🔬 Unit Tests

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected

Check warning on line 160 in GodotEnv/src/common/clients/EnvironmentVariableClient.cs

View workflow job for this annotation

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

Unreachable code detected
case OSType.Windows:
return new[] { "powershell", "cmd" }.Contains(shellName.ToLower());
}

return false;
}
}

0 comments on commit 32a4ab7

Please sign in to comment.