Skip to content

Commit

Permalink
Remove options from per-target context creation. (#2655)
Browse files Browse the repository at this point in the history
* Remove options from per-target context creation.

* Merge from main.
  • Loading branch information
michaelcfanning authored Apr 13, 2023
1 parent 2d52c53 commit 36b4792
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 49 deletions.
3 changes: 3 additions & 0 deletions ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
* BRK: Remove unused `quiet` parameter from `SarifLogger`. [#2639]https://github.com/microsoft/sarif-sdk/pull/2639
* BRK: Remove `CompuateHashData` and `AnalysisTargetToHashDataMap` properties from `SarifLogger` (in preference of new `fileRegionsCache` parameter. [#2639](https://github.com/microsoft/sarif-sdk/pull/2639)
* BRK: Eliminate proactive hashing of artifacts in `SarifLogger` constructor when `OptionallyEmittedData.Hashes` is specified. [#2639](https://github.com/microsoft/sarif-sdk/pull/2639)
* BUG: Eliminate erroneous `Posted log file successfully` message when context `PostUri` is non-null but empty. [#2655](https://github.com/microsoft/sarif-sdk/pull/2655)
* BUG: Resolves `IOException` raised by calling `FileSystem.ReadAllText` on file locked for write (but not read). [#2655](https://github.com/microsoft/sarif-sdk/pull/2655)
* BUG: Correct `toolComponent.language` regex in JSON schema. [#2653]https://github.com/microsoft/sarif-sdk/pull/2653
* BUG: Generate `IAnalysisLogger.AnalyzingTarget` callbacks from `MulthreadedAnalyzeCommandBase`. [#2637](https://github.com/microsoft/sarif-sdk/pull/2637)
* BUG: Persist `fileRegionsCache` parameter in `SarifLogger` to support retrieving hash data. [#2639](https://github.com/microsoft/sarif-sdk/pull/2639)
* BUG: Allow override of `FailureLevels` and `ResultKinds` in context objects. [#2639](https://github.com/microsoft/sarif-sdk/pull/2639)
* NEW: Add `MemoryStreamSarifLogger` (for in-memory SARIF generation). [#2655](https://github.com/microsoft/sarif-sdk/pull/2655)
* NEW: Add `AnalyzeContext.VersionControlProvenance` property. [#2646](https://github.com/microsoft/sarif-sdk/pull/2646)
* NEW: Add `DefaultTraces.ResultsSummary` property that drives naive results summary in console logger. [#2643](https://github.com/microsoft/sarif-sdk/pull/2643)
* NEW: Prove `AnalyzeContextBase.Inline` helper. [#2643](https://github.com/microsoft/sarif-sdk/pull/2643)
Expand Down
30 changes: 12 additions & 18 deletions src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public virtual int Run(TOptions options, ref TContext globalContext)
globalContext ??= new TContext();
if (options != null)
{
globalContext = InitializeContextFromOptions(options, ref globalContext);
globalContext = InitializeGlobalContextFromOptions(options, ref globalContext);
}

// We must make a copy of the global context reference
Expand Down Expand Up @@ -205,7 +205,7 @@ private int Run(TContext globalContext)
: succeeded ? SUCCESS : FAILURE;
}

public virtual TContext InitializeContextFromOptions(TOptions options, ref TContext context)
public virtual TContext InitializeGlobalContextFromOptions(TOptions options, ref TContext context)
{
context ??= new TContext();
context.FileSystem ??= Sarif.FileSystem.Instance;
Expand Down Expand Up @@ -570,13 +570,11 @@ private async Task<bool> EnumerateFilesFromArtifactsProvider(TContext globalCont
{
globalContext.CancellationToken.ThrowIfCancellationRequested();

TContext fileContext =
CreateContext(options: null,
new CachingLogger(globalContext.FailureLevels,
globalContext.ResultKinds),
globalContext.RuntimeErrors,
globalContext.FileSystem,
globalContext.Policy);
TContext fileContext = CreateScanTargetContext(globalContext);

fileContext.Logger =
new CachingLogger(globalContext.FailureLevels,
globalContext.ResultKinds);

Debug.Assert(fileContext.Logger != null);
fileContext.CurrentTarget = artifact;
Expand Down Expand Up @@ -686,18 +684,14 @@ internal AggregatingLogger InitializeLogger(TContext globalContext)
return logger;
}

protected virtual TContext CreateContext(TOptions options,
IAnalysisLogger logger,
RuntimeConditions runtimeErrors,
IFileSystem fileSystem = null,
PropertiesDictionary policy = null)
protected virtual TContext CreateScanTargetContext(TContext globalContext)
{
var context = new TContext
{
Logger = logger,
FileSystem = fileSystem ?? this.FileSystem,
RuntimeErrors = runtimeErrors,
Policy = policy ?? new PropertiesDictionary(),
Logger = globalContext.Logger,
RuntimeErrors = globalContext.RuntimeErrors,
FileSystem = globalContext.FileSystem ?? this.FileSystem,
Policy = globalContext.Policy ?? new PropertiesDictionary(),
};

return context;
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Driver/Sdk/PluginDriverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected virtual void ProcessBaseline(IAnalysisContext context)

protected virtual void PostLogFile(IAnalysisContext globalContext)
{
if (globalContext.PostUri == null) { return; }
if (string.IsNullOrEmpty(globalContext.PostUri)) { return; }

try
{
Expand Down
18 changes: 5 additions & 13 deletions src/Sarif.Multitool.Library/ValidateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,13 @@ public override IEnumerable<Assembly> DefaultPluginAssemblies
}
}

protected override SarifValidationContext CreateContext(ValidateOptions options,
IAnalysisLogger logger,
RuntimeConditions runtimeErrors,
IFileSystem fileSystem,
PropertiesDictionary policy = null)
protected override SarifValidationContext CreateScanTargetContext(SarifValidationContext globalContext)
{
SarifValidationContext context = base.CreateContext(options, logger, runtimeErrors, fileSystem, policy);
SarifValidationContext scanTargetContext = base.CreateScanTargetContext(globalContext);

if (options != null)
{
context.SchemaFilePath = options.SchemaFilePath;
context.UpdateInputsToCurrentSarif = options.UpdateInputsToCurrentSarif;
}

return context;
scanTargetContext.SchemaFilePath = globalContext.SchemaFilePath;
scanTargetContext.UpdateInputsToCurrentSarif = globalContext.UpdateInputsToCurrentSarif;
return scanTargetContext;
}

protected override void AnalyzeTarget(SarifValidationContext context,
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public static void LogMissingFile(IAnalysisContext context, string fileName)
exception: null,
persistExceptionStack: false,
messageFormat: null,
fileName));
Path.GetFullPath(fileName)));
}

public static void LogMissingCommandlineArgument(IAnalysisContext context, string missing, string required)
Expand Down
4 changes: 3 additions & 1 deletion src/Sarif/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ public string[] FileReadAllLines(string path)
/// </returns>
public string FileReadAllText(string path)
{
return File.ReadAllText(path);
using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var textReader = new StreamReader(fileStream);
return textReader.ReadToEnd();
}

/// <summary>
Expand Down
101 changes: 101 additions & 0 deletions src/Sarif/Writers/MemoryStreamSarifLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;

namespace Microsoft.CodeAnalysis.Sarif.Writers
{
internal class MemoryStreamSarifLogger : SarifLogger
{
protected StreamWriter writer;
protected bool disposed;

public MemoryStreamSarifLogger(FilePersistenceOptions logFilePersistenceOptions = FilePersistenceOptions.PrettyPrint,
OptionallyEmittedData dataToInsert = OptionallyEmittedData.None,
OptionallyEmittedData dataToRemove = OptionallyEmittedData.None,
Run run = null,
IEnumerable<string> analysisTargets = null,
IEnumerable<string> invocationTokensToRedact = null,
IEnumerable<string> invocationPropertiesToLog = null,
string defaultFileEncoding = null,
FailureLevelSet levels = null,
ResultKindSet kinds = null,
IEnumerable<string> insertProperties = null) : this(new StreamWriter(new MemoryStream()),
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
run,
analysisTargets,
invocationTokensToRedact,
invocationPropertiesToLog,
defaultFileEncoding,
levels,
kinds,
insertProperties)
{
}

internal MemoryStreamSarifLogger(StreamWriter writer,
FilePersistenceOptions logFilePersistenceOptions = FilePersistenceOptions.PrettyPrint,
OptionallyEmittedData dataToInsert = OptionallyEmittedData.None,
OptionallyEmittedData dataToRemove = OptionallyEmittedData.None,
Run run = null,
IEnumerable<string> analysisTargets = null,
IEnumerable<string> invocationTokensToRedact = null,
IEnumerable<string> invocationPropertiesToLog = null,
string defaultFileEncoding = null,
FailureLevelSet levels = null,
ResultKindSet kinds = null,
IEnumerable<string> insertProperties = null) : base(writer,
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
run,
analysisTargets,
invocationTokensToRedact,
invocationPropertiesToLog,
defaultFileEncoding,
closeWriterOnDispose: false,
levels,
kinds,
insertProperties)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}


this.writer = writer;
this.writer.AutoFlush = true;
}

public SarifLog ToSarifLog()
{
if (!this.disposed)
{
this.Dispose();
}

this.writer.BaseStream.Position = 0;

SarifLog log = SarifLog.Load(this.writer.BaseStream);
return log;
}

public override void Dispose()
{
if (this.disposed)
{
return;
}

base.Dispose();
this.writer.Flush();

this.disposed = true;
}
}
}
12 changes: 6 additions & 6 deletions src/Test.UnitTests.Sarif.Driver/Sdk/CommonOptionsBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void CommonOptionsBase_ProducesExpectedOutputFileOptions()
loggingOptions.Should().Be(FilePersistenceOptions.None);

TestAnalysisContext context = null;
command.InitializeContextFromOptions(analyzeOptions, ref context);
command.InitializeGlobalContextFromOptions(analyzeOptions, ref context);

context.ForceOverwrite.Should().Be(false);
context.PrettyPrint.Should().Be(true);
Expand All @@ -51,7 +51,7 @@ public void CommonOptionsBase_ProducesExpectedOutputFileOptions()
loggingOptions.Should().Be(FilePersistenceOptions.Minify);

context = null;
command.InitializeContextFromOptions(analyzeOptions, ref context);
command.InitializeGlobalContextFromOptions(analyzeOptions, ref context);

context.ForceOverwrite.Should().Be(false);
context.PrettyPrint.Should().Be(false);
Expand All @@ -69,7 +69,7 @@ public void CommonOptionsBase_ProducesExpectedOutputFileOptions()
loggingOptions.Should().Be(FilePersistenceOptions.PrettyPrint);

context = null;
command.InitializeContextFromOptions(analyzeOptions, ref context);
command.InitializeGlobalContextFromOptions(analyzeOptions, ref context);

context.ForceOverwrite.Should().Be(false);
context.PrettyPrint.Should().Be(true);
Expand All @@ -88,7 +88,7 @@ public void CommonOptionsBase_ProducesExpectedOutputFileOptions()
FilePersistenceOptions.PrettyPrint);

context = null;
command.InitializeContextFromOptions(analyzeOptions, ref context);
command.InitializeGlobalContextFromOptions(analyzeOptions, ref context);

context.ForceOverwrite.Should().Be(true);
context.PrettyPrint.Should().Be(true);
Expand All @@ -107,7 +107,7 @@ public void CommonOptionsBase_ProducesExpectedOutputFileOptions()
FilePersistenceOptions.PrettyPrint);

context = null;
command.InitializeContextFromOptions(analyzeOptions, ref context);
command.InitializeGlobalContextFromOptions(analyzeOptions, ref context);

context.ForceOverwrite.Should().Be(false);
context.PrettyPrint.Should().Be(true);
Expand All @@ -126,7 +126,7 @@ public void CommonOptionsBase_ProducesExpectedOutputFileOptions()
FilePersistenceOptions.Minify);

context = null;
command.InitializeContextFromOptions(analyzeOptions, ref context);
command.InitializeGlobalContextFromOptions(analyzeOptions, ref context);

context.ForceOverwrite.Should().Be(false);
context.PrettyPrint.Should().Be(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Reflection;

using FluentAssertions;

Expand All @@ -16,12 +15,12 @@ public TestMultithreadedAnalyzeCommand(IFileSystem fileSystem = null) : base(fil
TestRule.s_testRuleBehaviors = 0;
}

public override TestAnalysisContext InitializeContextFromOptions(TestAnalyzeOptions options, ref TestAnalysisContext context)
public override TestAnalysisContext InitializeGlobalContextFromOptions(TestAnalyzeOptions options, ref TestAnalysisContext context)
{
context ??= new TestAnalysisContext();
context.Policy ??= new PropertiesDictionary();

context = base.InitializeContextFromOptions(options, ref context);
context = base.InitializeGlobalContextFromOptions(options, ref context);

if (options.TestRuleBehaviors != null)
{
Expand All @@ -37,18 +36,18 @@ public override TestAnalysisContext InitializeContextFromOptions(TestAnalyzeOpti
return context;
}

protected override TestAnalysisContext CreateContext(TestAnalyzeOptions options, IAnalysisLogger logger, RuntimeConditions runtimeErrors, IFileSystem fileSystem = null, PropertiesDictionary policy = null)
protected override TestAnalysisContext CreateScanTargetContext(TestAnalysisContext globalContext)
{
TestAnalysisContext context = base.CreateContext(options, logger, runtimeErrors, fileSystem, policy);
TestRuleBehaviors behaviors = context.Policy.GetProperty(TestRule.Behaviors);
context.IsValidAnalysisTarget = !behaviors.HasFlag(TestRuleBehaviors.RegardAnalysisTargetAsInvalid);
globalContext = base.CreateScanTargetContext(globalContext);
TestRuleBehaviors behaviors = globalContext.Policy.GetProperty(TestRule.Behaviors);
globalContext.IsValidAnalysisTarget = !behaviors.HasFlag(TestRuleBehaviors.RegardAnalysisTargetAsInvalid);

context.RuntimeExceptions =
globalContext.RuntimeExceptions =
behaviors.HasFlag(TestRuleBehaviors.RegardAnalysisTargetAsCorrupted)
? new List<Exception>(new[] { new InvalidOperationException() })
: null;

return context;
return globalContext;
}

protected override void ValidateOptions(TestAnalyzeOptions options, TestAnalysisContext context)
Expand Down

0 comments on commit 36b4792

Please sign in to comment.