Skip to content

Commit fc932e5

Browse files
authored
Merge pull request #686 from dotnet-script/issue657
Don't write diagnostics to stdOut
2 parents b5ff49b + 7fc6a33 commit fc932e5

12 files changed

+79
-35
lines changed

src/Dotnet.Script.Core/ScriptConsole.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,29 @@ public virtual void WriteHighlighted(string value)
3636
Console.ResetColor();
3737
}
3838

39+
public virtual void WriteWarning(string value)
40+
{
41+
Console.ForegroundColor = ConsoleColor.Yellow;
42+
Error.WriteLine(value.TrimEnd(Environment.NewLine.ToCharArray()));
43+
Console.ResetColor();
44+
}
45+
3946
public virtual void WriteNormal(string value)
4047
{
4148
Out.WriteLine(value.TrimEnd(Environment.NewLine.ToCharArray()));
4249
}
4350

44-
public virtual void WriteDiagnostics(Diagnostic[] warningDiagnostics, Diagnostic[] errorDiagnostics)
51+
public virtual void WriteDiagnostics(Diagnostic[] warningDiagnostics, Diagnostic[] errorDiagnostics)
4552
{
46-
if (warningDiagnostics != null)
53+
if (warningDiagnostics != null)
4754
{
4855
foreach (var warning in warningDiagnostics)
4956
{
50-
WriteHighlighted(warning.ToString());
57+
WriteWarning(warning.ToString());
5158
}
5259
}
5360

54-
if (errorDiagnostics != null)
61+
if (errorDiagnostics != null)
5562
{
5663
foreach (var error in errorDiagnostics)
5764
{

src/Dotnet.Script.Core/ScriptEmitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context, stri
2323
var compilationContext = _scriptCompiler.CreateCompilationContext<TReturn, THost>(context);
2424
foreach (var warning in compilationContext.Warnings)
2525
{
26-
_scriptConsole.WriteHighlighted(warning.ToString());
26+
_scriptConsole.WriteWarning(warning.ToString());
2727
}
2828

2929
if (compilationContext.Errors.Any())

src/Dotnet.Script.Shared.Tests/ProcessHelper.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Dotnet.Script.Shared.Tests
66
{
77
public static class ProcessHelper
88
{
9-
public static (string output, int exitcode) RunAndCaptureOutput(string fileName, string arguments, string workingDirectory = null)
9+
public static ProcessResult RunAndCaptureOutput(string fileName, string arguments, string workingDirectory = null)
1010
{
1111
var startInfo = new ProcessStartInfo(fileName, arguments)
1212
{
@@ -29,15 +29,17 @@ public static (string output, int exitcode) RunAndCaptureOutput(string fileName,
2929
catch
3030
{
3131
Console.WriteLine($"Failed to launch '{fileName}' with args, '{arguments}'");
32-
return (null, -1);
32+
return new ProcessResult(null, -1, null, null);
3333
}
3434

35-
var output = process.StandardOutput.ReadToEnd();
36-
output += process.StandardError.ReadToEnd();
35+
var standardOut = process.StandardOutput.ReadToEnd().Trim();
36+
var standardError = process.StandardError.ReadToEnd().Trim();
37+
38+
var output = standardOut + standardError;
3739

3840
process.WaitForExit();
3941

40-
return (output.Trim(), process.ExitCode);
42+
return new ProcessResult(output, process.ExitCode, standardOut, standardError);
4143
}
4244
}
4345
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Dotnet.Script.Shared.Tests
2+
{
3+
public class ProcessResult
4+
{
5+
public ProcessResult(string output, int exitCode, string standardOut, string standardError)
6+
{
7+
this.Output = output;
8+
this.ExitCode = exitCode;
9+
this.StandardOut = standardOut;
10+
this.StandardError = standardError;
11+
}
12+
13+
public string Output { get; }
14+
public int ExitCode { get; }
15+
public string StandardOut { get; }
16+
public string StandardError { get; }
17+
18+
public void Deconstruct(out string output, out int exitCode)
19+
{
20+
output = this.Output;
21+
exitCode = this.ExitCode;
22+
}
23+
}
24+
}

src/Dotnet.Script.Tests/ExecutionCacheTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public void ShouldCacheScriptsFromSameFolderIndividually()
136136
private (string output, string hash) Execute(string pathToScript)
137137
{
138138
var result = ScriptTestRunner.Default.Execute(pathToScript);
139-
testOutputHelper.WriteLine(result.output);
140-
Assert.Equal(0, result.exitCode);
139+
testOutputHelper.WriteLine(result.Output);
140+
Assert.Equal(0, result.ExitCode);
141141
string pathToExecutionCache = GetPathToExecutionCache(pathToScript);
142142
var pathToCacheFile = Path.Combine(pathToExecutionCache, "script.sha256");
143143
string cachedhash = null;
@@ -146,7 +146,7 @@ public void ShouldCacheScriptsFromSameFolderIndividually()
146146
cachedhash = File.ReadAllText(pathToCacheFile);
147147
}
148148

149-
return (result.output, cachedhash);
149+
return (result.Output, cachedhash);
150150
}
151151

152152
private static string GetPathToExecutionCache(string pathToScript)

src/Dotnet.Script.Tests/PackageSourceTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public void ShouldHandleSpecifyingPackageSource()
1818
var fixture = "ScriptPackage/WithNoNuGetConfig";
1919
var pathToScriptPackages = ScriptPackagesFixture.GetPathToPackagesFolder();
2020
var result = ScriptTestRunner.Default.ExecuteFixture(fixture, $"--no-cache -s \"{pathToScriptPackages}\"");
21-
Assert.Contains("Hello", result.output);
22-
Assert.Equal(0, result.exitCode);
21+
Assert.Contains("Hello", result.Output);
22+
Assert.Equal(0, result.ExitCode);
2323
}
2424

2525
[Fact]
@@ -28,8 +28,8 @@ public void ShouldHandleSpecifyingPackageSourceWhenEvaluatingCode()
2828
var pathToScriptPackages = ScriptPackagesFixture.GetPathToPackagesFolder();
2929
var code = @"#load \""nuget:ScriptPackageWithMainCsx,1.0.0\"" SayHello();";
3030
var result = ScriptTestRunner.Default.Execute($"--no-cache -s \"{pathToScriptPackages}\" eval \"{code}\"");
31-
Assert.Contains("Hello", result.output);
32-
Assert.Equal(0, result.exitCode);
31+
Assert.Contains("Hello", result.Output);
32+
Assert.Equal(0, result.ExitCode);
3333
}
3434
}
3535
}

src/Dotnet.Script.Tests/ScriptExecutionTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ public void ShouldReturnExitCodeOneWhenScriptFailsToCompile()
8383
Assert.Equal(1, exitCode);
8484
}
8585

86+
[Fact]
87+
public void ShouldWriteCompilerWarningsToStandardError()
88+
{
89+
var result = ScriptTestRunner.Default.ExecuteFixture(fixture: "CompilationWarning", "--no-cache");
90+
Assert.True(string.IsNullOrWhiteSpace(result.StandardOut));
91+
Assert.Contains("CS1998", result.StandardError, StringComparison.OrdinalIgnoreCase);
92+
}
93+
8694
[Fact]
8795
public void ShouldHandleIssue129()
8896
{
@@ -246,7 +254,7 @@ public void ShouldSupportInlineNugetReferencesWithTrailingSemicoloninEvaluatedCo
246254
public void ShouldExecuteRemoteScript(string url, string output)
247255
{
248256
var result = ScriptTestRunner.Default.Execute(url);
249-
Assert.Contains(output, result.output);
257+
Assert.Contains(output, result.Output);
250258
}
251259

252260
[Fact]
@@ -329,19 +337,19 @@ public void ShouldThrowMeaningfulErrorMessageWhenDependencyIsNotFound()
329337

330338
// Run once to ensure that it is cached.
331339
var result = ScriptTestRunner.Default.Execute(pathToScript);
332-
Assert.Contains("42", result.output);
340+
Assert.Contains("42", result.Output);
333341

334342
// Remove the package from the global NuGet cache
335343
TestPathUtils.RemovePackageFromGlobalNugetCache("SampleLibrary");
336344

337345
//ScriptTestRunner.Default.ExecuteInProcess(pathToScript);
338346

339347
result = ScriptTestRunner.Default.Execute(pathToScript);
340-
Assert.Contains("Try executing/publishing the script", result.output);
348+
Assert.Contains("Try executing/publishing the script", result.Output);
341349

342350
// Run again with the '--no-cache' option to assert that the advice actually worked.
343351
result = ScriptTestRunner.Default.Execute($"{pathToScript} --no-cache");
344-
Assert.Contains("42", result.output);
352+
Assert.Contains("42", result.Output);
345353
}
346354

347355
[Fact]

src/Dotnet.Script.Tests/ScriptPackagesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing()
3838

3939
// Run once to ensure that it is cached.
4040
var result = ScriptTestRunner.Default.Execute($"{pathToScriptFile} -s {pathToScriptPackages}");
41-
Assert.StartsWith("Hello from netstandard2.0", result.output);
41+
Assert.StartsWith("Hello from netstandard2.0", result.Output);
4242

4343
// Remove the package from the global NuGet cache
4444
TestPathUtils.RemovePackageFromGlobalNugetCache("ScriptPackageWithMainCsx");
@@ -48,7 +48,7 @@ public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing()
4848
File.WriteAllText(pathToScriptFile, code.ToString());
4949

5050
result = ScriptTestRunner.Default.Execute($"{pathToScriptFile} -s {pathToScriptPackages}");
51-
Assert.Contains("Try executing/publishing the script", result.output);
51+
Assert.Contains("Try executing/publishing the script", result.Output);
5252
}
5353

5454
[Fact]

src/Dotnet.Script.Tests/ScriptPublisherTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void SimplePublishDllTest()
126126
var dllPath = Path.Combine("publish", "main.dll");
127127
var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path);
128128

129-
Assert.Equal(0, dllRunResult.exitCode);
129+
Assert.Equal(0, dllRunResult.ExitCode);
130130
}
131131

132132
[Fact]
@@ -143,7 +143,7 @@ public void SimplePublishDllFromCurrentDirectoryTest()
143143

144144
var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path);
145145

146-
Assert.Equal(0, dllRunResult.exitCode);
146+
Assert.Equal(0, dllRunResult.ExitCode);
147147
}
148148

149149
[Fact]
@@ -160,7 +160,7 @@ public void SimplePublishDllToOtherFolderTest()
160160
var dllPath = Path.Combine(publishFolder.Path, "main.dll");
161161
var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", publishFolder.Path);
162162

163-
Assert.Equal(0, dllRunResult.exitCode);
163+
Assert.Equal(0, dllRunResult.ExitCode);
164164
}
165165

166166
[Fact]
@@ -178,7 +178,7 @@ public void CustomDllNameTest()
178178
var dllPath = Path.Combine(workspaceFolder.Path, "publish", assemblyName);
179179
var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path);
180180

181-
Assert.Equal(0, dllRunResult.exitCode);
181+
Assert.Equal(0, dllRunResult.ExitCode);
182182
}
183183

184184
[Fact]
@@ -211,8 +211,8 @@ public void DllWithArgsTests()
211211
var dllPath = Path.Combine(workspaceFolder.Path, "publish", "main.dll");
212212
var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath} -- w o r l d", workspaceFolder.Path);
213213

214-
Assert.Equal(0, dllRunResult.exitCode);
215-
Assert.Contains("Hello world", dllRunResult.output);
214+
Assert.Equal(0, dllRunResult.ExitCode);
215+
Assert.Contains("Hello world", dllRunResult.Output);
216216
}
217217

218218
[Fact]

src/Dotnet.Script.Tests/ScriptRunnerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static ScriptRunner CreateScriptRunner()
2626
{
2727
var logFactory = TestOutputHelper.CreateTestLogFactory();
2828
var scriptCompiler = new ScriptCompiler(logFactory, false);
29-
29+
3030
return new ScriptRunner(scriptCompiler, logFactory, ScriptConsole.Default);
3131
}
3232
}

src/Dotnet.Script.Tests/ScriptTestRunner.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private ScriptTestRunner()
2424
_scriptEnvironment = ScriptEnvironment.Default;
2525
}
2626

27-
public (string output, int exitCode) Execute(string arguments, string workingDirectory = null)
27+
public ProcessResult Execute(string arguments, string workingDirectory = null)
2828
{
2929
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments(arguments), workingDirectory);
3030
return result;
@@ -35,14 +35,14 @@ public int ExecuteInProcess(string arguments = null)
3535
return Program.Main(arguments?.Split(" ") ?? Array.Empty<string>());
3636
}
3737

38-
public (string output, int exitCode) ExecuteFixture(string fixture, string arguments = null, string workingDirectory = null)
38+
public ProcessResult ExecuteFixture(string fixture, string arguments = null, string workingDirectory = null)
3939
{
4040
var pathToFixture = TestPathUtils.GetPathToTestFixture(fixture);
4141
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"\"{pathToFixture}\" {arguments}"), workingDirectory);
4242
return result;
4343
}
4444

45-
public (string output, int exitcode) ExecuteWithScriptPackage(string fixture, string arguments = null, string workingDirectory = null)
45+
public ProcessResult ExecuteWithScriptPackage(string fixture, string arguments = null, string workingDirectory = null)
4646
{
4747
var pathToScriptPackageFixtures = TestPathUtils.GetPathToTestFixtureFolder("ScriptPackage");
4848
var pathToFixture = Path.Combine(pathToScriptPackageFixtures, fixture, $"{fixture}.csx");
@@ -67,13 +67,13 @@ public static int ExecuteCodeInProcess(string code, string arguments)
6767
return Program.Main(allArguments.ToArray());
6868
}
6969

70-
public (string output, int exitCode) ExecuteCode(string code)
70+
public ProcessResult ExecuteCode(string code)
7171
{
7272
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"eval \"{code}\""));
7373
return result;
7474
}
7575

76-
public (string output, int exitCode) ExecuteCodeInReleaseMode(string code)
76+
public ProcessResult ExecuteCodeInReleaseMode(string code)
7777
{
7878
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"-c release eval \"{code}\""));
7979
return result;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public async Task Foo()
2+
{
3+
}

0 commit comments

Comments
 (0)