Skip to content

Commit

Permalink
fix: try using standard out directly if redirected
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Nov 30, 2024
1 parent 52723d8 commit a0d7601
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/install_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ jobs:
# Don't cancel other runners if one fails.
fail-fast: false
matrix:
# Also try windows-2019?
os: [ macos-latest, ubuntu-latest, windows-2019 ]
os: [macos-latest, ubuntu-latest, windows-latest]
defaults:
run:
# Use bash shells on all platforms.
Expand All @@ -46,14 +45,15 @@ jobs:
run: |
# Use tool to install Godot. Last line of output is the path to the
# symlink that always points to the active version of Godot.
dotnet run -- godot install 3.5.3
dotnet run --no-build -- godot install 3.5.3
- name: 🤖 Check Godot Location
working-directory: GodotEnv
run: |
# Get path to the symlink that always points to the active version of
# Godot.
GODOT_SYMLINK="$(dotnet run -- godot env path)"
dotnet run --no-build -- godot env path > godot_path.txt
GODOT_SYMLINK=$(<godot_path.txt)
echo "🕵️‍♂️ Godot symlink path: $GODOT_SYMLINK"
Expand Down Expand Up @@ -131,14 +131,15 @@ jobs:
run: |
# Use tool to install Godot. Last line of output is the path to the
# symlink that always points to the active version of Godot.
dotnet run -- godot install ${{ matrix.version }}
dotnet run --no-build -- godot install ${{ matrix.version }}
- name: 🤖 Check Godot Location
working-directory: GodotEnv
run: |
# Get path to the symlink that always points to the active version of
# Godot.
GODOT_SYMLINK="$(dotnet run -- godot env path)"
dotnet run --no-build -- godot env path > godot_path.txt
GODOT_SYMLINK=$(<godot_path.txt)
echo "🕵️‍♂️ Godot symlink path: $GODOT_SYMLINK"
Expand All @@ -150,7 +151,7 @@ jobs:
- name: 🌴 Set GODOT System Environment Variable
working-directory: GodotEnv
run: |
dotnet run -- godot env setup
dotnet run --no-build -- godot env setup
- name: 🧪 Verify GODOT System Environment Variable
working-directory: GodotEnv
Expand Down
18 changes: 9 additions & 9 deletions GodotEnv.Tests/src/common/utilities/LogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Chickensoft.GodotEnv.Tests;

public sealed class LogTest : IDisposable {

private readonly OutputTestFakeInMemoryConsole _console = new ();
private readonly OutputTestFakeInMemoryConsole _console = new();

public void Dispose() => _console.Dispose();

[Fact]
public void Prints() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Print("Hello, world!");

Expand All @@ -26,7 +26,7 @@ public void Prints() {

[Fact]
public void PrintsInfo() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Info("Hello, world!");

Expand All @@ -35,7 +35,7 @@ public void PrintsInfo() {

[Fact]
public void PrintsWarning() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Warn("Hello, world!");

Expand All @@ -44,7 +44,7 @@ public void PrintsWarning() {

[Fact]
public void PrintsErr() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Err("Hello, world!");

Expand All @@ -53,7 +53,7 @@ public void PrintsErr() {

[Fact]
public void PrintsSuccess() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Success("Hello, world!");

Expand All @@ -68,7 +68,7 @@ public void GetsColorNames() {

[Fact]
public void OutputsCorrectStyleChanges() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Print("A");
log.Print("");
Expand Down Expand Up @@ -106,7 +106,7 @@ public void OutputsCorrectStyleChanges() {

[Fact]
public void OutputsNull() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Print(null);
log.Info(null);
Expand All @@ -120,7 +120,7 @@ public void OutputsNull() {

[Fact]
public void OutputsObject() {
Log log = new(_console);
Log log = new(_console) { TestEnvironment = true };

log.Print(new { Hello = "world" });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class AddonsCommandTest {
public async Task Executes() {
var context = new Mock<IExecutionContext>();
var console = new FakeInMemoryConsole();
var log = new Log(console); // Use real log to test colors in output
// Use real log to test colors in output
var log = new Log(console) { TestEnvironment = true };

context.Setup(context => context.CreateLog(console)).Returns(log);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ out ILog log
var fakeConsole = new FakeInMemoryConsole();
console = fakeConsole;

log = new Log(console); // Use real log to test colors in output
// Use real log to test colors in output
log = new Log(console) { TestEnvironment = true };

var workingDir = "/";

Expand Down
50 changes: 40 additions & 10 deletions GodotEnv/src/common/utilities/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private record Style(

public IConsole Console { get; }

public bool TestEnvironment { get; init; }
private ConsoleWriter OutputConsole => Console.Output;
private readonly StringBuilder _sb = new();
private readonly ConsoleColor _defaultFgColor;
Expand All @@ -85,16 +86,27 @@ private record Style(
// running an environment without an actual console. Redirected environments
// cause errors when manipulating the cursor on Windows.
public bool IsInRedirectedEnv =>
Console.IsOutputRedirected || Console.IsErrorRedirected;
!TestEnvironment && (
Console.IsOutputRedirected ||
Console.IsErrorRedirected ||
Environment.GetEnvironmentVariable("CI") != null
);

public Log(IConsole console) {
console.ResetColor();
console.Clear();
Console = console;


if (IsInRedirectedEnv) {
System.Console.Clear();
}
else {
console.ResetColor();
}

_defaultFgColor = console.ForegroundColor;
_defaultBgColor = console.BackgroundColor;
_defaultStyle = new Style(_defaultFgColor, _defaultBgColor);
_styles.Push(_defaultStyle);
Console = console;
}

public void Print(object? message) => Output(
Expand Down Expand Up @@ -140,16 +152,23 @@ public void ClearLastLine() {
}

public void Output(
object? message, Action<IConsole> consoleStyle, bool inPlace = false, bool addExtraLine = true
object? message,
Action<IConsole> consoleStyle,
bool inPlace = false,
bool addExtraLine = true
) {
if (inPlace && IsInRedirectedEnv) {
// Don't print in-place messages in a redirected environment, like
// GitHub actions.
return;
}
lock (Console) {
// Set the new foreground and background colors.
consoleStyle(Console);
if (!IsInRedirectedEnv) {
// Set the new foreground and background colors.
// Don't want to do this in redirected environments
// (like GitHub action shells)
consoleStyle(Console);
}

if (
(message is string str && str != "") ||
Expand All @@ -170,12 +189,23 @@ message is not string and not null
OutputConsole.Write(message);
}
else {
OutputConsole.WriteLine(message);
if (IsInRedirectedEnv) {
System.Console.WriteLine(message);
}
else {
OutputConsole.WriteLine(message);
}
}
_sb.AppendLine(message?.ToString());

if (addExtraLine) {
OutputConsole.WriteLine();
if (IsInRedirectedEnv) {
System.Console.WriteLine();
}
else {
OutputConsole.WriteLine();
}

_sb.AppendLine();
}

Expand Down Expand Up @@ -211,7 +241,7 @@ private void PushStyle(Style consoleStyle) {
? $" bg=\"{GetColorName((int)bg)}\""
: ""
).Append(']'
);
);
_styles.Push(consoleStyle);
}

Expand Down
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.304",
"version": "9.0.100",
"rollForward": "latestMajor"
}
}
}

0 comments on commit a0d7601

Please sign in to comment.