From 9dc9925f59ea93da74dfbfc570bedd70d35dddcd Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Fri, 3 Mar 2023 12:54:22 +0000 Subject: [PATCH] Abstract over `DiagnosticsStream` for tests --- .../Semmle.Autobuild.Cpp.Tests/BuildScripts.cs | 11 ++++++++++- .../Semmle.Autobuild.CSharp.Tests/BuildScripts.cs | 12 +++++++++++- .../Semmle.Autobuild.Shared/Autobuilder.cs | 4 ++-- .../Semmle.Autobuild.Shared/BuildActions.cs | 13 +++++++++++++ csharp/extractor/Semmle.Util/ToolStatusPage.cs | 14 +++++++++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs b/cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs index ecb2e58174d..06057b971b5 100644 --- a/cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs +++ b/cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs @@ -194,6 +194,15 @@ namespace Semmle.Autobuild.Cpp.Tests if (!DownloadFiles.Contains((address, fileName))) throw new ArgumentException($"Missing DownloadFile, {address}, {fileName}"); } + + public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new TestDiagnosticWriter(); + } + + internal class TestDiagnosticWriter : IDiagnosticsWriter + { + public IList Diagnostics { get; } = new List(); + + public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message); } /// @@ -253,7 +262,7 @@ namespace Semmle.Autobuild.Cpp.Tests Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_TRAP_DIR"] = ""; Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_SOURCE_ARCHIVE_DIR"] = ""; Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_ROOT"] = $@"C:\codeql\{codeqlUpperLanguage.ToLowerInvariant()}"; - Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = Path.GetTempPath(); + Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = ""; Actions.GetEnvironmentVariable["CODEQL_JAVA_HOME"] = @"C:\codeql\tools\java"; Actions.GetEnvironmentVariable["CODEQL_PLATFORM"] = "win64"; Actions.GetEnvironmentVariable["SEMMLE_DIST"] = @"C:\odasa"; diff --git a/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs b/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs index 0243dc927d1..9590e132229 100644 --- a/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs +++ b/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs @@ -210,6 +210,16 @@ namespace Semmle.Autobuild.CSharp.Tests if (!DownloadFiles.Contains((address, fileName))) throw new ArgumentException($"Missing DownloadFile, {address}, {fileName}"); } + + + public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new TestDiagnosticWriter(); + } + + internal class TestDiagnosticWriter : IDiagnosticsWriter + { + public IList Diagnostics { get; } = new List(); + + public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message); } /// @@ -401,7 +411,7 @@ namespace Semmle.Autobuild.CSharp.Tests actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_TRAP_DIR"] = ""; actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_SOURCE_ARCHIVE_DIR"] = ""; actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_ROOT"] = $@"C:\codeql\{codeqlUpperLanguage.ToLowerInvariant()}"; - actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = Path.GetTempPath(); + actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = ""; actions.GetEnvironmentVariable["CODEQL_JAVA_HOME"] = @"C:\codeql\tools\java"; actions.GetEnvironmentVariable["CODEQL_PLATFORM"] = isWindows ? "win64" : "linux64"; actions.GetEnvironmentVariable["LGTM_INDEX_VSTOOLS_VERSION"] = vsToolsVersion; diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs index 2335e15015e..f00f59b488a 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs @@ -241,7 +241,7 @@ namespace Semmle.Autobuild.Shared SourceArchiveDir = RequireEnvironmentVariable(EnvVars.SourceArchiveDir(this.Options.Language)); DiagnosticsDir = RequireEnvironmentVariable(EnvVars.DiagnosticDir(this.Options.Language)); - this.diagnostics = new DiagnosticsStream(Path.Combine(DiagnosticsDir, $"autobuilder-{DateTime.UtcNow:yyyyMMddHHmm}.jsonc")); + this.diagnostics = actions.CreateDiagnosticsWriter(Path.Combine(DiagnosticsDir, $"autobuilder-{DateTime.UtcNow:yyyyMMddHHmm}.jsonc")); } /// @@ -269,7 +269,7 @@ namespace Semmle.Autobuild.Shared private readonly ILogger logger = new ConsoleLogger(Verbosity.Info); - private readonly DiagnosticsStream diagnostics; + private readonly IDiagnosticsWriter diagnostics; /// /// Makes relative to the root source directory. diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs index d4ca15cd246..0982a520bfd 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs @@ -167,6 +167,17 @@ namespace Semmle.Autobuild.Shared /// Downloads the resource with the specified URI to a local file. /// void DownloadFile(string address, string fileName); + + /// + /// Creates an for the given . + /// + /// + /// The path suggesting where the diagnostics should be written to. + /// + /// + /// A to which diagnostic entries can be added. + /// + IDiagnosticsWriter CreateDiagnosticsWriter(string filename); } /// @@ -288,6 +299,8 @@ namespace Semmle.Autobuild.Shared public void DownloadFile(string address, string fileName) => DownloadFileAsync(address, fileName).Wait(); + public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new DiagnosticsStream(filename); + public static IBuildActions Instance { get; } = new SystemBuildActions(); } } diff --git a/csharp/extractor/Semmle.Util/ToolStatusPage.cs b/csharp/extractor/Semmle.Util/ToolStatusPage.cs index 85eca785e88..f7abd643a19 100644 --- a/csharp/extractor/Semmle.Util/ToolStatusPage.cs +++ b/csharp/extractor/Semmle.Util/ToolStatusPage.cs @@ -174,11 +174,23 @@ namespace Semmle.Util } } + /// + /// Provides the ability to write diagnostic messages to some output. + /// + public interface IDiagnosticsWriter + { + /// + /// Adds as a new diagnostics entry. + /// + /// The diagnostics entry to add. + void AddEntry(DiagnosticMessage message); + } + /// /// A wrapper around an underlying which allows /// objects to be serialized to it. /// - public sealed class DiagnosticsStream : IDisposable + public sealed class DiagnosticsStream : IDiagnosticsWriter, IDisposable { private readonly JsonSerializer serializer; private readonly StreamWriter writer;