diff --git a/src/Dotnet.Script.Core/ScriptConsole.cs b/src/Dotnet.Script.Core/ScriptConsole.cs index 053285fe..6f46bf6d 100644 --- a/src/Dotnet.Script.Core/ScriptConsole.cs +++ b/src/Dotnet.Script.Core/ScriptConsole.cs @@ -36,22 +36,29 @@ public virtual void WriteHighlighted(string value) Console.ResetColor(); } + public virtual void WriteWarning(string value) + { + Console.ForegroundColor = ConsoleColor.Yellow; + Error.WriteLine(value.TrimEnd(Environment.NewLine.ToCharArray())); + Console.ResetColor(); + } + public virtual void WriteNormal(string value) { Out.WriteLine(value.TrimEnd(Environment.NewLine.ToCharArray())); } - public virtual void WriteDiagnostics(Diagnostic[] warningDiagnostics, Diagnostic[] errorDiagnostics) + public virtual void WriteDiagnostics(Diagnostic[] warningDiagnostics, Diagnostic[] errorDiagnostics) { - if (warningDiagnostics != null) + if (warningDiagnostics != null) { foreach (var warning in warningDiagnostics) { - WriteHighlighted(warning.ToString()); + WriteWarning(warning.ToString()); } } - if (errorDiagnostics != null) + if (errorDiagnostics != null) { foreach (var error in errorDiagnostics) { diff --git a/src/Dotnet.Script.Core/ScriptEmitter.cs b/src/Dotnet.Script.Core/ScriptEmitter.cs index 8f8e133d..c0e10e16 100644 --- a/src/Dotnet.Script.Core/ScriptEmitter.cs +++ b/src/Dotnet.Script.Core/ScriptEmitter.cs @@ -23,7 +23,7 @@ public virtual ScriptEmitResult Emit(ScriptContext context, stri var compilationContext = _scriptCompiler.CreateCompilationContext(context); foreach (var warning in compilationContext.Warnings) { - _scriptConsole.WriteHighlighted(warning.ToString()); + _scriptConsole.WriteWarning(warning.ToString()); } if (compilationContext.Errors.Any()) diff --git a/src/Dotnet.Script.Shared.Tests/ProcessHelper.cs b/src/Dotnet.Script.Shared.Tests/ProcessHelper.cs index 9a692163..4f79d77e 100644 --- a/src/Dotnet.Script.Shared.Tests/ProcessHelper.cs +++ b/src/Dotnet.Script.Shared.Tests/ProcessHelper.cs @@ -6,7 +6,7 @@ namespace Dotnet.Script.Shared.Tests { public static class ProcessHelper { - public static (string output, int exitcode) RunAndCaptureOutput(string fileName, string arguments, string workingDirectory = null) + public static ProcessResult RunAndCaptureOutput(string fileName, string arguments, string workingDirectory = null) { var startInfo = new ProcessStartInfo(fileName, arguments) { @@ -29,15 +29,17 @@ public static (string output, int exitcode) RunAndCaptureOutput(string fileName, catch { Console.WriteLine($"Failed to launch '{fileName}' with args, '{arguments}'"); - return (null, -1); + return new ProcessResult(null, -1, null, null); } - var output = process.StandardOutput.ReadToEnd(); - output += process.StandardError.ReadToEnd(); + var standardOut = process.StandardOutput.ReadToEnd().Trim(); + var standardError = process.StandardError.ReadToEnd().Trim(); + + var output = standardOut + standardError; process.WaitForExit(); - return (output.Trim(), process.ExitCode); + return new ProcessResult(output, process.ExitCode, standardOut, standardError); } } } diff --git a/src/Dotnet.Script.Shared.Tests/ProcessResult.cs b/src/Dotnet.Script.Shared.Tests/ProcessResult.cs new file mode 100644 index 00000000..ddd0e8c9 --- /dev/null +++ b/src/Dotnet.Script.Shared.Tests/ProcessResult.cs @@ -0,0 +1,24 @@ +namespace Dotnet.Script.Shared.Tests +{ + public class ProcessResult + { + public ProcessResult(string output, int exitCode, string standardOut, string standardError) + { + this.Output = output; + this.ExitCode = exitCode; + this.StandardOut = standardOut; + this.StandardError = standardError; + } + + public string Output { get; } + public int ExitCode { get; } + public string StandardOut { get; } + public string StandardError { get; } + + public void Deconstruct(out string output, out int exitCode) + { + output = this.Output; + exitCode = this.ExitCode; + } + } +} \ No newline at end of file diff --git a/src/Dotnet.Script.Tests/ExecutionCacheTests.cs b/src/Dotnet.Script.Tests/ExecutionCacheTests.cs index c6a2f252..43bb5863 100644 --- a/src/Dotnet.Script.Tests/ExecutionCacheTests.cs +++ b/src/Dotnet.Script.Tests/ExecutionCacheTests.cs @@ -136,8 +136,8 @@ public void ShouldCacheScriptsFromSameFolderIndividually() private (string output, string hash) Execute(string pathToScript) { var result = ScriptTestRunner.Default.Execute(pathToScript); - testOutputHelper.WriteLine(result.output); - Assert.Equal(0, result.exitCode); + testOutputHelper.WriteLine(result.Output); + Assert.Equal(0, result.ExitCode); string pathToExecutionCache = GetPathToExecutionCache(pathToScript); var pathToCacheFile = Path.Combine(pathToExecutionCache, "script.sha256"); string cachedhash = null; @@ -146,7 +146,7 @@ public void ShouldCacheScriptsFromSameFolderIndividually() cachedhash = File.ReadAllText(pathToCacheFile); } - return (result.output, cachedhash); + return (result.Output, cachedhash); } private static string GetPathToExecutionCache(string pathToScript) diff --git a/src/Dotnet.Script.Tests/PackageSourceTests.cs b/src/Dotnet.Script.Tests/PackageSourceTests.cs index a7c30736..2f02b479 100644 --- a/src/Dotnet.Script.Tests/PackageSourceTests.cs +++ b/src/Dotnet.Script.Tests/PackageSourceTests.cs @@ -18,8 +18,8 @@ public void ShouldHandleSpecifyingPackageSource() var fixture = "ScriptPackage/WithNoNuGetConfig"; var pathToScriptPackages = ScriptPackagesFixture.GetPathToPackagesFolder(); var result = ScriptTestRunner.Default.ExecuteFixture(fixture, $"--no-cache -s \"{pathToScriptPackages}\""); - Assert.Contains("Hello", result.output); - Assert.Equal(0, result.exitCode); + Assert.Contains("Hello", result.Output); + Assert.Equal(0, result.ExitCode); } [Fact] @@ -28,8 +28,8 @@ public void ShouldHandleSpecifyingPackageSourceWhenEvaluatingCode() var pathToScriptPackages = ScriptPackagesFixture.GetPathToPackagesFolder(); var code = @"#load \""nuget:ScriptPackageWithMainCsx,1.0.0\"" SayHello();"; var result = ScriptTestRunner.Default.Execute($"--no-cache -s \"{pathToScriptPackages}\" eval \"{code}\""); - Assert.Contains("Hello", result.output); - Assert.Equal(0, result.exitCode); + Assert.Contains("Hello", result.Output); + Assert.Equal(0, result.ExitCode); } } } diff --git a/src/Dotnet.Script.Tests/ScriptExecutionTests.cs b/src/Dotnet.Script.Tests/ScriptExecutionTests.cs index 260cc2fa..0d56d592 100644 --- a/src/Dotnet.Script.Tests/ScriptExecutionTests.cs +++ b/src/Dotnet.Script.Tests/ScriptExecutionTests.cs @@ -83,6 +83,14 @@ public void ShouldReturnExitCodeOneWhenScriptFailsToCompile() Assert.Equal(1, exitCode); } + [Fact] + public void ShouldWriteCompilerWarningsToStandardError() + { + var result = ScriptTestRunner.Default.ExecuteFixture(fixture: "CompilationWarning", "--no-cache"); + Assert.True(string.IsNullOrWhiteSpace(result.StandardOut)); + Assert.Contains("CS1998", result.StandardError, StringComparison.OrdinalIgnoreCase); + } + [Fact] public void ShouldHandleIssue129() { @@ -246,7 +254,7 @@ public void ShouldSupportInlineNugetReferencesWithTrailingSemicoloninEvaluatedCo public void ShouldExecuteRemoteScript(string url, string output) { var result = ScriptTestRunner.Default.Execute(url); - Assert.Contains(output, result.output); + Assert.Contains(output, result.Output); } [Fact] @@ -329,7 +337,7 @@ public void ShouldThrowMeaningfulErrorMessageWhenDependencyIsNotFound() // Run once to ensure that it is cached. var result = ScriptTestRunner.Default.Execute(pathToScript); - Assert.Contains("42", result.output); + Assert.Contains("42", result.Output); // Remove the package from the global NuGet cache TestPathUtils.RemovePackageFromGlobalNugetCache("SampleLibrary"); @@ -337,11 +345,11 @@ public void ShouldThrowMeaningfulErrorMessageWhenDependencyIsNotFound() //ScriptTestRunner.Default.ExecuteInProcess(pathToScript); result = ScriptTestRunner.Default.Execute(pathToScript); - Assert.Contains("Try executing/publishing the script", result.output); + Assert.Contains("Try executing/publishing the script", result.Output); // Run again with the '--no-cache' option to assert that the advice actually worked. result = ScriptTestRunner.Default.Execute($"{pathToScript} --no-cache"); - Assert.Contains("42", result.output); + Assert.Contains("42", result.Output); } [Fact] diff --git a/src/Dotnet.Script.Tests/ScriptPackagesTests.cs b/src/Dotnet.Script.Tests/ScriptPackagesTests.cs index 7f762120..81e61618 100644 --- a/src/Dotnet.Script.Tests/ScriptPackagesTests.cs +++ b/src/Dotnet.Script.Tests/ScriptPackagesTests.cs @@ -38,7 +38,7 @@ public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing() // Run once to ensure that it is cached. var result = ScriptTestRunner.Default.Execute($"{pathToScriptFile} -s {pathToScriptPackages}"); - Assert.StartsWith("Hello from netstandard2.0", result.output); + Assert.StartsWith("Hello from netstandard2.0", result.Output); // Remove the package from the global NuGet cache TestPathUtils.RemovePackageFromGlobalNugetCache("ScriptPackageWithMainCsx"); @@ -48,7 +48,7 @@ public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing() File.WriteAllText(pathToScriptFile, code.ToString()); result = ScriptTestRunner.Default.Execute($"{pathToScriptFile} -s {pathToScriptPackages}"); - Assert.Contains("Try executing/publishing the script", result.output); + Assert.Contains("Try executing/publishing the script", result.Output); } [Fact] diff --git a/src/Dotnet.Script.Tests/ScriptPublisherTests.cs b/src/Dotnet.Script.Tests/ScriptPublisherTests.cs index 086da179..0f7064ef 100644 --- a/src/Dotnet.Script.Tests/ScriptPublisherTests.cs +++ b/src/Dotnet.Script.Tests/ScriptPublisherTests.cs @@ -126,7 +126,7 @@ public void SimplePublishDllTest() var dllPath = Path.Combine("publish", "main.dll"); var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path); - Assert.Equal(0, dllRunResult.exitCode); + Assert.Equal(0, dllRunResult.ExitCode); } [Fact] @@ -143,7 +143,7 @@ public void SimplePublishDllFromCurrentDirectoryTest() var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path); - Assert.Equal(0, dllRunResult.exitCode); + Assert.Equal(0, dllRunResult.ExitCode); } [Fact] @@ -160,7 +160,7 @@ public void SimplePublishDllToOtherFolderTest() var dllPath = Path.Combine(publishFolder.Path, "main.dll"); var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", publishFolder.Path); - Assert.Equal(0, dllRunResult.exitCode); + Assert.Equal(0, dllRunResult.ExitCode); } [Fact] @@ -178,7 +178,7 @@ public void CustomDllNameTest() var dllPath = Path.Combine(workspaceFolder.Path, "publish", assemblyName); var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path); - Assert.Equal(0, dllRunResult.exitCode); + Assert.Equal(0, dllRunResult.ExitCode); } [Fact] @@ -211,8 +211,8 @@ public void DllWithArgsTests() var dllPath = Path.Combine(workspaceFolder.Path, "publish", "main.dll"); var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath} -- w o r l d", workspaceFolder.Path); - Assert.Equal(0, dllRunResult.exitCode); - Assert.Contains("Hello world", dllRunResult.output); + Assert.Equal(0, dllRunResult.ExitCode); + Assert.Contains("Hello world", dllRunResult.Output); } [Fact] diff --git a/src/Dotnet.Script.Tests/ScriptRunnerTests.cs b/src/Dotnet.Script.Tests/ScriptRunnerTests.cs index 43f67f0c..e9bc7de3 100644 --- a/src/Dotnet.Script.Tests/ScriptRunnerTests.cs +++ b/src/Dotnet.Script.Tests/ScriptRunnerTests.cs @@ -26,7 +26,7 @@ private static ScriptRunner CreateScriptRunner() { var logFactory = TestOutputHelper.CreateTestLogFactory(); var scriptCompiler = new ScriptCompiler(logFactory, false); - + return new ScriptRunner(scriptCompiler, logFactory, ScriptConsole.Default); } } diff --git a/src/Dotnet.Script.Tests/ScriptTestRunner.cs b/src/Dotnet.Script.Tests/ScriptTestRunner.cs index ecd4bd2b..02db0fe5 100644 --- a/src/Dotnet.Script.Tests/ScriptTestRunner.cs +++ b/src/Dotnet.Script.Tests/ScriptTestRunner.cs @@ -24,7 +24,7 @@ private ScriptTestRunner() _scriptEnvironment = ScriptEnvironment.Default; } - public (string output, int exitCode) Execute(string arguments, string workingDirectory = null) + public ProcessResult Execute(string arguments, string workingDirectory = null) { var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments(arguments), workingDirectory); return result; @@ -35,14 +35,14 @@ public int ExecuteInProcess(string arguments = null) return Program.Main(arguments?.Split(" ") ?? Array.Empty()); } - public (string output, int exitCode) ExecuteFixture(string fixture, string arguments = null, string workingDirectory = null) + public ProcessResult ExecuteFixture(string fixture, string arguments = null, string workingDirectory = null) { var pathToFixture = TestPathUtils.GetPathToTestFixture(fixture); var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"\"{pathToFixture}\" {arguments}"), workingDirectory); return result; } - public (string output, int exitcode) ExecuteWithScriptPackage(string fixture, string arguments = null, string workingDirectory = null) + public ProcessResult ExecuteWithScriptPackage(string fixture, string arguments = null, string workingDirectory = null) { var pathToScriptPackageFixtures = TestPathUtils.GetPathToTestFixtureFolder("ScriptPackage"); var pathToFixture = Path.Combine(pathToScriptPackageFixtures, fixture, $"{fixture}.csx"); @@ -67,13 +67,13 @@ public static int ExecuteCodeInProcess(string code, string arguments) return Program.Main(allArguments.ToArray()); } - public (string output, int exitCode) ExecuteCode(string code) + public ProcessResult ExecuteCode(string code) { var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"eval \"{code}\"")); return result; } - public (string output, int exitCode) ExecuteCodeInReleaseMode(string code) + public ProcessResult ExecuteCodeInReleaseMode(string code) { var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"-c release eval \"{code}\"")); return result; diff --git a/src/Dotnet.Script.Tests/TestFixtures/CompilationWarning/CompilationWarning.csx b/src/Dotnet.Script.Tests/TestFixtures/CompilationWarning/CompilationWarning.csx new file mode 100644 index 00000000..883f543a --- /dev/null +++ b/src/Dotnet.Script.Tests/TestFixtures/CompilationWarning/CompilationWarning.csx @@ -0,0 +1,3 @@ +public async Task Foo() +{ +} \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy