Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make secretsAnalyzer ctor free threaded #4858

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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