Skip to content

Commit

Permalink
GH16: Detect Existence of SpecFlow for Visual Studio Extension and sh…
Browse files Browse the repository at this point in the history
…ow a dialog box warning about conflicting instances. (#26)
  • Loading branch information
clrudolphi authored Jun 20, 2024
1 parent 9774c95 commit 6d80e62
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Improvements:

* Find Unused Step Definitions Command: Improved handling of Step Definitions decorated with the 'StepDefinition' attribute. If a Step Definition is used in any Given/Then/When step in a Feature file, the step will no longer show in the 'Find Unused Step Definitions' context menu. (#24)
* Detects Existence of SpecFlow for Visual Studio extension and show a warning (GH#16)

## Bug fixes:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Reqnroll.VisualStudio.SpecFlowExtensionDetection;

namespace Reqnroll.VisualStudio.Editor.Services;

[Export(typeof(ITaggerProvider))]
Expand All @@ -7,15 +9,19 @@ namespace Reqnroll.VisualStudio.Editor.Services;
public class DeveroomTaggerProvider : IDeveroomTaggerProvider
{
private readonly IIdeScope _ideScope;
private readonly SpecFlowExtensionDetectionService _detectionService;

[ImportingConstructor]
public DeveroomTaggerProvider(IIdeScope ideScope)
public DeveroomTaggerProvider(IIdeScope ideScope, SpecFlowExtensionDetectionService specFlowExtensionDetectionService)
{
_detectionService = specFlowExtensionDetectionService;
_ideScope = ideScope;
}

public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag
{
_detectionService?.CheckForSpecFlowExtensionOnce();

if (buffer is not ITextBuffer2 buffer2)
throw new InvalidOperationException($"Cannot assign {buffer.GetType()} to {typeof(ITextBuffer2)}");

Expand Down
2 changes: 1 addition & 1 deletion Reqnroll.VisualStudio/Reqnroll.VisualStudio.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net481</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.VisualStudio.Text.Classification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reqnroll.VisualStudio.SpecFlowExtensionDetection
{
[Export(typeof(IClassifierProvider))]
[ContentType("gherkin")]
public class SpecFlowExtensionDetectionClassifierProvider : IClassifierProvider
{
private readonly SpecFlowExtensionDetectionService _detectionService;

public SpecFlowExtensionDetectionClassifierProvider(SpecFlowExtensionDetectionService detectionService)
{
_detectionService = detectionService;
}
public IClassifier GetClassifier(ITextBuffer textBuffer)
{
_detectionService.CheckForSpecFlowExtension();
return null;

Check warning on line 23 in Reqnroll.VisualStudio/SpecFlowExtensionDetection/SpecFlowExtensionDetectionClassifier.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reqnroll.VisualStudio.SpecFlowExtensionDetection
{
[Export]
public class SpecFlowExtensionDetectionService
{
private readonly IIdeScope _ideScope;
private bool _extensionExistenceChecked = false;
private bool _compatibilityAlertShown = false;

[ImportingConstructor]
public SpecFlowExtensionDetectionService(IIdeScope ideScope)
{
_ideScope = ideScope;
}

public void CheckForSpecFlowExtensionOnce()
{
if (_extensionExistenceChecked)
return;

CheckForSpecFlowExtension();
}

public void CheckForSpecFlowExtension()
{
if (_compatibilityAlertShown)
return;

_extensionExistenceChecked = true;
var specFlowExtensionDetected = AppDomain.CurrentDomain.GetAssemblies().Any(a =>
a.FullName.StartsWith("SpecFlow.VisualStudio"));

if (specFlowExtensionDetected && !_compatibilityAlertShown)
{
_compatibilityAlertShown = true;
_ideScope.Actions.ShowProblem(
$"We detected that both the 'Reqnroll for Visual Studio 2022' and the 'SpecFlow for Visual Studio 2022' extension have been installed in this Visual Studio instance.{Environment.NewLine}For the proper behavior you need to uninstall or disable one of these extensions in the 'Extensions / Manage Extensions' dialog.");

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ private void PerformCommand(string commandName, string parameter = null,

private IDeveroomTaggerProvider CreateTaggerProvider()
{
var taggerProvider = new DeveroomTaggerProvider(_ideScope);
var taggerProvider = new DeveroomTaggerProvider(_ideScope, new SpecFlowExtensionDetection.SpecFlowExtensionDetectionService(_ideScope));
var tagger = taggerProvider.CreateTagger<DeveroomTag>(_ideScope.CurrentTextView.TextBuffer);
var span = new SnapshotSpan(_ideScope.CurrentTextView.TextSnapshot, 0, 0);
tagger.GetUpToDateDeveroomTagsForSpan(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AutoFormatDocumentCommandTests
public AutoFormatDocumentCommandTests(ITestOutputHelper testOutputHelper)
{
_ideScope = new StubIdeScope(testOutputHelper);
_deveroomTaggerProvider = new DeveroomTaggerProvider(_ideScope);
_deveroomTaggerProvider = new DeveroomTaggerProvider(_ideScope, new SpecFlowExtensionDetection.SpecFlowExtensionDetectionService(_ideScope));
}

private AutoFormatDocumentCommand CreateSUT() => new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AutoFormatTableCommandTests
public AutoFormatTableCommandTests(ITestOutputHelper testOutputHelper)
{
_ideScope = new StubIdeScope(testOutputHelper);
_taggerProvider = new DeveroomTaggerProvider(_ideScope);
_taggerProvider = new DeveroomTaggerProvider(_ideScope, new SpecFlowExtensionDetection.SpecFlowExtensionDetectionService(_ideScope));
}

private StubWpfTextView CreateTextView(TestText inputText, string newLine) =>
Expand Down
2 changes: 1 addition & 1 deletion Tests/Reqnroll.VisualStudio.Tests/Editor/EditorTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected async Task BindingRegistryIsModified(string expression)

protected static DeveroomTaggerProvider CreateTaggerProvider(IIdeScope ideScope)
{
var taggerProvider = new DeveroomTaggerProvider(ideScope);
var taggerProvider = new DeveroomTaggerProvider(ideScope, new SpecFlowExtensionDetection.SpecFlowExtensionDetectionService(ideScope));

return taggerProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ITagger<DeveroomTag> BuildInitializedFeatureFileTagger()
public ITagger<DeveroomTag> BuildFeatureFileTagger()
{
ProjectScope.Properties.AddProperty(typeof(IDeveroomTagParser), TagParser.Object);
var taggerProvider = new DeveroomTaggerProvider(IdeScope);
var taggerProvider = new DeveroomTaggerProvider(IdeScope, new SpecFlowExtensionDetection.SpecFlowExtensionDetectionService(IdeScope));

var tagger = BuildTagger<FeatureFileTagger>(taggerProvider);
tagger.TagsChanged += DeveroomTagger_TagsChanged;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Reqnroll.VisualStudio.SpecFlowExtensionDetection;

namespace Reqnroll.VisualStudio.VsxStubs.ProjectSystem;

public class InMemoryStubProjectBuilder : IDisposable
Expand All @@ -15,7 +17,7 @@ public InMemoryStubProjectBuilder(InMemoryStubProjectScope project)
bindingRegistryCache = project.Properties.GetProperty<IDiscoveryService>(typeof(IDiscoveryService))
.BindingRegistryCache;

var taggerProvider = new DeveroomTaggerProvider(project.IdeScope);
var taggerProvider = new DeveroomTaggerProvider(project.IdeScope, new SpecFlowExtensionDetectionService(project.IdeScope));
_tagger = taggerProvider.CreateTagger<DeveroomTag>(VisibleTextBuffer);

_bindingRegistryChanged = new AsyncManualResetEvent();
Expand Down

0 comments on commit 6d80e62

Please sign in to comment.