Skip to content

Commit 9dc9925

Browse files
committed
Abstract over DiagnosticsStream for tests
1 parent df6f5d5 commit 9dc9925

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ public void DownloadFile(string address, string fileName)
194194
if (!DownloadFiles.Contains((address, fileName)))
195195
throw new ArgumentException($"Missing DownloadFile, {address}, {fileName}");
196196
}
197+
198+
public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new TestDiagnosticWriter();
199+
}
200+
201+
internal class TestDiagnosticWriter : IDiagnosticsWriter
202+
{
203+
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
204+
205+
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
197206
}
198207

199208
/// <summary>
@@ -253,7 +262,7 @@ CppAutobuilder CreateAutoBuilder(bool isWindows,
253262
Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_TRAP_DIR"] = "";
254263
Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_SOURCE_ARCHIVE_DIR"] = "";
255264
Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_ROOT"] = $@"C:\codeql\{codeqlUpperLanguage.ToLowerInvariant()}";
256-
Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = Path.GetTempPath();
265+
Actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = "";
257266
Actions.GetEnvironmentVariable["CODEQL_JAVA_HOME"] = @"C:\codeql\tools\java";
258267
Actions.GetEnvironmentVariable["CODEQL_PLATFORM"] = "win64";
259268
Actions.GetEnvironmentVariable["SEMMLE_DIST"] = @"C:\odasa";

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ public void DownloadFile(string address, string fileName)
210210
if (!DownloadFiles.Contains((address, fileName)))
211211
throw new ArgumentException($"Missing DownloadFile, {address}, {fileName}");
212212
}
213+
214+
215+
public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new TestDiagnosticWriter();
216+
}
217+
218+
internal class TestDiagnosticWriter : IDiagnosticsWriter
219+
{
220+
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
221+
222+
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
213223
}
214224

215225
/// <summary>
@@ -401,7 +411,7 @@ private CSharpAutobuilder CreateAutoBuilder(bool isWindows,
401411
actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_TRAP_DIR"] = "";
402412
actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_SOURCE_ARCHIVE_DIR"] = "";
403413
actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_ROOT"] = $@"C:\codeql\{codeqlUpperLanguage.ToLowerInvariant()}";
404-
actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = Path.GetTempPath();
414+
actions.GetEnvironmentVariable[$"CODEQL_EXTRACTOR_{codeqlUpperLanguage}_DIAGNOSTIC_DIR"] = "";
405415
actions.GetEnvironmentVariable["CODEQL_JAVA_HOME"] = @"C:\codeql\tools\java";
406416
actions.GetEnvironmentVariable["CODEQL_PLATFORM"] = isWindows ? "win64" : "linux64";
407417
actions.GetEnvironmentVariable["LGTM_INDEX_VSTOOLS_VERSION"] = vsToolsVersion;

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ protected Autobuilder(IBuildActions actions, TAutobuildOptions options, Diagnost
241241
SourceArchiveDir = RequireEnvironmentVariable(EnvVars.SourceArchiveDir(this.Options.Language));
242242
DiagnosticsDir = RequireEnvironmentVariable(EnvVars.DiagnosticDir(this.Options.Language));
243243

244-
this.diagnostics = new DiagnosticsStream(Path.Combine(DiagnosticsDir, $"autobuilder-{DateTime.UtcNow:yyyyMMddHHmm}.jsonc"));
244+
this.diagnostics = actions.CreateDiagnosticsWriter(Path.Combine(DiagnosticsDir, $"autobuilder-{DateTime.UtcNow:yyyyMMddHHmm}.jsonc"));
245245
}
246246

247247
/// <summary>
@@ -269,7 +269,7 @@ protected string RequireEnvironmentVariable(string name)
269269

270270
private readonly ILogger logger = new ConsoleLogger(Verbosity.Info);
271271

272-
private readonly DiagnosticsStream diagnostics;
272+
private readonly IDiagnosticsWriter diagnostics;
273273

274274
/// <summary>
275275
/// Makes <see cref="path" /> relative to the root source directory.

csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ public interface IBuildActions
167167
/// Downloads the resource with the specified URI to a local file.
168168
/// </summary>
169169
void DownloadFile(string address, string fileName);
170+
171+
/// <summary>
172+
/// Creates an <see cref="IDiagnosticsWriter" /> for the given <paramref name="filename" />.
173+
/// </summary>
174+
/// <param name="filename">
175+
/// The path suggesting where the diagnostics should be written to.
176+
/// </param>
177+
/// <returns>
178+
/// A <see cref="IDiagnosticsWriter" /> to which diagnostic entries can be added.
179+
/// </returns>
180+
IDiagnosticsWriter CreateDiagnosticsWriter(string filename);
170181
}
171182

172183
/// <summary>
@@ -288,6 +299,8 @@ private static async Task DownloadFileAsync(string address, string filename)
288299
public void DownloadFile(string address, string fileName) =>
289300
DownloadFileAsync(address, fileName).Wait();
290301

302+
public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new DiagnosticsStream(filename);
303+
291304
public static IBuildActions Instance { get; } = new SystemBuildActions();
292305
}
293306
}

csharp/extractor/Semmle.Util/ToolStatusPage.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,23 @@ public DiagnosticMessage(
174174
}
175175
}
176176

177+
/// <summary>
178+
/// Provides the ability to write diagnostic messages to some output.
179+
/// </summary>
180+
public interface IDiagnosticsWriter
181+
{
182+
/// <summary>
183+
/// Adds <paramref name="message" /> as a new diagnostics entry.
184+
/// </summary>
185+
/// <param name="message">The diagnostics entry to add.</param>
186+
void AddEntry(DiagnosticMessage message);
187+
}
188+
177189
/// <summary>
178190
/// A wrapper around an underlying <see cref="StreamWriter" /> which allows
179191
/// <see cref="DiagnosticMessage" /> objects to be serialized to it.
180192
/// </summary>
181-
public sealed class DiagnosticsStream : IDisposable
193+
public sealed class DiagnosticsStream : IDiagnosticsWriter, IDisposable
182194
{
183195
private readonly JsonSerializer serializer;
184196
private readonly StreamWriter writer;

0 commit comments

Comments
 (0)