Skip to content

Commit

Permalink
add check for SpecFlow version in project
Browse files Browse the repository at this point in the history
  • Loading branch information
david1995 committed Apr 25, 2019
1 parent 712eec1 commit 63aa328
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
28 changes: 25 additions & 3 deletions IdeIntegration/Generator/IdeSingleFileGenerator.cs
Expand Up @@ -9,6 +9,13 @@ namespace TechTalk.SpecFlow.IdeIntegration.Generator
{
public class IdeSingleFileGenerator
{
private readonly ProjectInfo _projectInfo;

public IdeSingleFileGenerator(ProjectInfo projectInfo)
{
_projectInfo = projectInfo;
}

public event Action<TestGenerationError> GenerationError;
public event Action<Exception> OtherError;

Expand All @@ -27,6 +34,24 @@ public class IdeSingleFileGenerator
generatorServices = generatorServicesProvider();
projectSettings = generatorServices.GetProjectSettings();
codeDomHelper = GenerationTargetLanguage.CreateCodeDomHelper(projectSettings.ProjectPlatformSettings.Language);

if (outputFilePath == null)
{
outputFilePath = inputFilePath + GenerationTargetLanguage.GetExtension(projectSettings.ProjectPlatformSettings.Language);
}

var generatorVersion = generatorServices.GetGeneratorVersion();

if (generatorVersion.Major != _projectInfo.ReferencedSpecFlowVersion.Major
|| generatorVersion.Minor != _projectInfo.ReferencedSpecFlowVersion.Minor)
{
string errorMessage = $@"SpecFlow Visual Studio extension attempted to use SpecFlow Generator {generatorVersion.ToString(2)} while project '{_projectInfo.ProjectName}' references SpecFlow {_projectInfo.ReferencedSpecFlowVersion.ToString(2)}. We recommend to migrate to MSBuild code-behind generation to resolve this issue.
For more information see https://specflow.org/documentation/Generate-Tests-from-MsBuild/";
var exception = new InvalidOperationException(errorMessage);
string errorText = GenerateError(exception, codeDomHelper);
outputFileContentWriter(outputFilePath, errorText);
return outputFilePath;
}
}
catch (Exception ex)
{
Expand All @@ -49,9 +74,6 @@ public class IdeSingleFileGenerator

try
{
if (outputFilePath == null)
outputFilePath = inputFilePath + GenerationTargetLanguage.GetExtension(projectSettings.ProjectPlatformSettings.Language);

outputFileContentWriter(outputFilePath, outputFileContent);

return outputFilePath;
Expand Down
@@ -1,10 +1,12 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using EnvDTE;
using TechTalk.SpecFlow.Generator.Interfaces;
using TechTalk.SpecFlow.IdeIntegration.Generator;
using VSLangProj80;

namespace TechTalk.SpecFlow.VsIntegration.Implementation.SingleFileGenerator
{
Expand Down Expand Up @@ -35,7 +37,22 @@ protected override bool GenerateInternal(string inputFilePath, string inputFileC
return true;
}

var ideSingleFileGenerator = new IdeSingleFileGenerator();
var vsProject = project.Object as VSProject2;
var references = vsProject?.References.Cast<Reference3>() ?? Enumerable.Empty<Reference3>();
var specFlowReference = references.FirstOrDefault(r => r.Name == "TechTalk.SpecFlow");

if (specFlowReference == null)
{
// TODO: dei message is just placeholder, needs to be updated
// TODO: dei set generatedContent
string errorMessage = $"Could not find reference to SpecFlow in project '{project.Name}'. Please add the 'TechTalk.SpecFlow' package to the project and use MSBuild generation instead of SpecFlowSingleFileGenerator.";
throw new InvalidOperationException(errorMessage);
}

var referencedSpecFlowVersion = Version.Parse(specFlowReference.Version);
var projectInfo = new ProjectInfo(project.Name, referencedSpecFlowVersion);

var ideSingleFileGenerator = new IdeSingleFileGenerator(projectInfo);

ideSingleFileGenerator.GenerationError +=
delegate(TestGenerationError error)
Expand Down

0 comments on commit 63aa328

Please sign in to comment.