Skip to content

Commit

Permalink
Make secretsAnalyzer ctor free threaded (#4858)
Browse files Browse the repository at this point in the history
* Make secretsAnalyzer ctor free threaded

* Remove one lazy
  • Loading branch information
bigfluffycookie authored and ugras-ergun-sonarsource committed Oct 9, 2023
1 parent dcf9536 commit 79d161f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/CloudSecrets.UnitTests/SecretsAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ public void MefCtor_CheckIsExported()
MefTestHelpers.CreateExport<IRuleSettingsProviderFactory>());
}

[TestMethod]
public void Ctor_DoesNotCallAnyNonFreeThreadedServices()
{
// Arrange
var textDocumentFactoryService = new Mock<ITextDocumentFactoryService>();
var contentTypeRegistryService = new Mock<IContentTypeRegistryService>();
var analysisStatusNotifierFactory = new Mock<IAnalysisStatusNotifierFactory>();
var cloudSecretsTelemetryManager = new Mock<ICloudSecretsTelemetryManager>();
var ruleSettingsProviderFactory = new Mock<IRuleSettingsProviderFactory>();
var secretDetectors = new[]
{
SetupSecretDetector("rule1", "rule1"),
};

// Act
_ = new SecretsAnalyzer(textDocumentFactoryService.Object, contentTypeRegistryService.Object, secretDetectors.Select(x => x.Object), analysisStatusNotifierFactory.Object,
ruleSettingsProviderFactory.Object, cloudSecretsTelemetryManager.Object);

// The MEF constructor should be free-threaded, which it will be if
// it doesn't make any external calls.
textDocumentFactoryService.Invocations.Should().BeEmpty();
contentTypeRegistryService.Invocations.Should().BeEmpty();
analysisStatusNotifierFactory.Invocations.Should().BeEmpty();
cloudSecretsTelemetryManager.Invocations.Should().BeEmpty();
ruleSettingsProviderFactory.Invocations.Should().BeEmpty();
secretDetectors[0].Invocations.Should().BeEmpty();
}

[TestMethod]
public void IsAnalysisSupported_NoLanguages_True()
{
Expand Down Expand Up @@ -390,3 +418,4 @@ private IAnalysisStatusNotifierFactory SetupStatusNotifierFactory(IAnalysisStatu
}
}
}

13 changes: 7 additions & 6 deletions src/CloudSecrets/SecretsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ internal class SecretsAnalyzer : IAnalyzer
private readonly ITextDocumentFactoryService textDocumentFactoryService;
private readonly IEnumerable<ISecretDetector> secretDetectors;
private readonly IAnalysisStatusNotifierFactory analysisStatusNotifierFactory;
private readonly IRuleSettingsProvider ruleSettingsProvider;
private readonly Lazy<IRuleSettingsProvider> ruleSettingsProvider;
private readonly ICloudSecretsTelemetryManager cloudSecretsTelemetryManager;
private readonly ISecretsToAnalysisIssueConverter secretsToAnalysisIssueConverter;
private readonly IContentType filesContentType;
private readonly IContentTypeRegistryService contentTypeRegistryService;

[ImportingConstructor]
public SecretsAnalyzer(
Expand Down Expand Up @@ -74,8 +74,9 @@ internal SecretsAnalyzer(ITextDocumentFactoryService textDocumentFactoryService,
this.analysisStatusNotifierFactory = analysisStatusNotifierFactory;
this.cloudSecretsTelemetryManager = cloudSecretsTelemetryManager;
this.secretsToAnalysisIssueConverter = secretsToAnalysisIssueConverter;
filesContentType = contentTypeRegistryService.UnknownContentType;
ruleSettingsProvider = ruleSettingsProviderFactory.Get(Language.Secrets);
this.contentTypeRegistryService = contentTypeRegistryService;

ruleSettingsProvider = new Lazy<IRuleSettingsProvider>(() => ruleSettingsProviderFactory.Get(Language.Secrets));
}

public bool IsAnalysisSupported(IEnumerable<AnalysisLanguage> languages)
Expand All @@ -98,10 +99,10 @@ public void ExecuteAnalysis(string filePath,
{
var stopwatch = Stopwatch.StartNew();

var textDocument = textDocumentFactoryService.CreateAndLoadTextDocument(filePath, filesContentType); // load the document from disc
var textDocument = textDocumentFactoryService.CreateAndLoadTextDocument(filePath, contentTypeRegistryService.UnknownContentType); // load the document from disc
var currentSnapshot = textDocument.TextBuffer.CurrentSnapshot;
var fileContent = currentSnapshot.GetText();
var rulesSettings = ruleSettingsProvider.Get();
var rulesSettings = ruleSettingsProvider.Value.Get();

var issues = new List<IAnalysisIssue>();

Expand Down

0 comments on commit 79d161f

Please sign in to comment.