Skip to content

Commit 8fffb12

Browse files
committed
Fix test:
- Add output encoding option to tests to capture stdout - Update test to specify encoding
1 parent b7624bf commit 8fffb12

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,9 @@ public ICommand SetCommandArgs(string commandArgs)
181181
{
182182
throw new NotImplementedException();
183183
}
184+
185+
public ICommand StandardOutputEncoding(Encoding commandArgs)
186+
{
187+
throw new NotImplementedException();
188+
}
184189
}

src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public ICommand EnvironmentVariable(string name, string? value)
101101
return this;
102102
}
103103

104+
public ICommand StandardOutputEncoding(Encoding encoding)
105+
{
106+
_process.StartInfo.StandardOutputEncoding = encoding;
107+
return this;
108+
}
109+
104110
public ICommand CaptureStdOut()
105111
{
106112
ThrowIfRunning();

src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public interface ICommand
2525

2626
ICommand SetCommandArgs(string commandArgs);
2727

28+
ICommand StandardOutputEncoding(Encoding encoding);
29+
2830
string CommandName { get; }
2931

3032
string CommandArgs { get; }

test/Microsoft.NET.TestFramework/Commands/TestCommand.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public abstract class TestCommand
2626
public Action<string>? CommandOutputHandler { get; set; }
2727
public Action<Process>? ProcessStartedHandler { get; set; }
2828

29+
public Encoding? StandardOutputEncoding { get; set; }
30+
2931
protected TestCommand(ITestOutputHelper log)
3032
{
3133
Log = log;
@@ -57,6 +59,12 @@ public TestCommand WithStandardInput(string stdin)
5759
return this;
5860
}
5961

62+
public TestCommand WithStandardOutputEncoding(Encoding encoding)
63+
{
64+
StandardOutputEncoding = encoding;
65+
return this;
66+
}
67+
6068
/// <summary>
6169
/// Instructs not to escape the arguments when launching command.
6270
/// This may be used to pass ready arguments line as single string argument.
@@ -153,6 +161,11 @@ public virtual CommandResult Execute(IEnumerable<string> args)
153161
{
154162
Log.WriteLine($"❌{line}");
155163
});
164+
165+
if (StandardOutputEncoding is not null)
166+
{
167+
command.StandardOutputEncoding(StandardOutputEncoding);
168+
}
156169

157170
string fileToShow = Path.GetFileNameWithoutExtension(spec.FileName!).Equals("dotnet", StringComparison.OrdinalIgnoreCase) ?
158171
"dotnet" :

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,7 @@ public void EntryPointFilePath_WithUnicodeCharacters()
15651565

15661566
new DotnetCommand(Log, "run", unicodeFileName)
15671567
.WithWorkingDirectory(testInstance.Path)
1568+
.WithStandardOutputEncoding(Encoding.UTF8)
15681569
.Execute()
15691570
.Should().Pass()
15701571
.And.HaveStdOut($"EntryPointFilePath: {filePath}");

0 commit comments

Comments
 (0)