private void GenerateTestFile(FeatureFileInput featureFile, CodeDomProvider codeProvider, TextWriter outputWriter) { using(var reader = new StreamReader(featureFile.GetFullPath(project.ProjectSettings))) { GenerateTestFile(featureFile, codeProvider, reader, outputWriter); } }
protected override TestGeneratorResult GenerateTestFileWithExceptions(FeatureFileInput featureFileInput, GenerationSettings settings) { if (featureFileInput == null) throw new ArgumentNullException("featureFileInput"); if (settings == null) throw new ArgumentNullException("settings"); var generatedTestFullPath = GetTestFullPath(featureFileInput); bool? preliminaryUpToDateCheckResult = null; if (settings.CheckUpToDate) { preliminaryUpToDateCheckResult = testUpToDateChecker.IsUpToDatePreliminary(featureFileInput, generatedTestFullPath, settings.UpToDateCheckingMethod); if (preliminaryUpToDateCheckResult == true) return new TestGeneratorResult(null, true); } string generatedTestCode = GetGeneratedTestCode(featureFileInput); if (settings.CheckUpToDate && preliminaryUpToDateCheckResult != false) { var isUpToDate = testUpToDateChecker.IsUpToDate(featureFileInput, generatedTestFullPath, generatedTestCode, settings.UpToDateCheckingMethod); if (isUpToDate) return new TestGeneratorResult(null, true); } if (settings.WriteResultToFile) { File.WriteAllText(generatedTestFullPath, generatedTestCode, Encoding.UTF8); } return new TestGeneratorResult(generatedTestCode, false); }
private bool IsUpToDateByModificationTimeAndGeneratorVersion(FeatureFileInput featureFileInput, string generatedTestFullPath) { if (generatedTestFullPath == null) return false; // check existance of the target file if (!File.Exists(generatedTestFullPath)) return false; // check modification time of the target file try { var featureFileModificationTime = File.GetLastWriteTime(featureFileInput.GetFullPath(projectSettings)); var codeFileModificationTime = File.GetLastWriteTime(generatedTestFullPath); if (featureFileModificationTime > codeFileModificationTime) return false; // check tools version var codeFileVersion = testHeaderWriter.DetectGeneratedTestVersion(featureFileInput.GetGeneratedTestContent(generatedTestFullPath)); if (codeFileVersion == null || codeFileVersion != generatorInfo.GeneratorVersion) return false; } catch (Exception ex) { Debug.WriteLine(ex); return false; } return true; }
public SpecFlowProject ReadSpecFlowProject(string projectFilePath) { Microsoft.Build.Evaluation.Project project = ProjectCollection.GlobalProjectCollection.LoadProject(projectFilePath); string projectFolder = Path.GetDirectoryName(projectFilePath); var specFlowProject = new SpecFlowProject(); specFlowProject.ProjectSettings.ProjectFolder = projectFolder; specFlowProject.ProjectSettings.ProjectName = Path.GetFileNameWithoutExtension(projectFilePath); specFlowProject.ProjectSettings.AssemblyName = project.AllEvaluatedProperties.First(x=>x.Name=="AssemblyName").EvaluatedValue; specFlowProject.ProjectSettings.DefaultNamespace =project.AllEvaluatedProperties.First(x=>x.Name=="RootNamespace").EvaluatedValue; foreach (ProjectItem item in project.FeatureFiles()) { var featureFile = new FeatureFileInput(item.EvaluatedInclude); var ns = item.GetMetadataValue("CustomToolNamespace"); if (!String.IsNullOrEmpty(ns)) featureFile.CustomNamespace = ns; specFlowProject.FeatureFiles.Add(featureFile); } ProjectItem appConfigItem = project.ApplicationConfigurationFile(); if (appConfigItem != null) { var configFilePath = Path.Combine(projectFolder, appConfigItem.EvaluatedInclude); var configFileContent = File.ReadAllText(configFilePath); var configurationHolder = GetConfigurationHolderFromFileContent(configFileContent); specFlowProject.ProjectSettings.ConfigurationHolder = configurationHolder; specFlowProject.Configuration = configurationLoader.LoadConfiguration(configurationHolder); } return specFlowProject; }
private SpecFlowProject CreateSpecFlowProjectFrom(IProject project) { var specFlowProject = new SpecFlowProject(); specFlowProject.ProjectSettings.ProjectFolder = project.Directory; specFlowProject.ProjectSettings.ProjectName = project.Name; specFlowProject.ProjectSettings.AssemblyName = project.AssemblyName; specFlowProject.ProjectSettings.DefaultNamespace = project.RootNamespace; var generatorConfig = specFlowProject.Configuration.GeneratorConfiguration; foreach (var projectFile in project.Items.OfType<FileProjectItem>().Where(IsFeatureOrAppConfigFile)) { string extension = Path.GetExtension(projectFile.FileName); if (extension != null && extension.Equals(".feature", StringComparison.InvariantCultureIgnoreCase)) { string fileName = FileUtilities.GetRelativePath(projectFile.FileName, project.Directory); var featureFile = new FeatureFileInput(fileName); specFlowProject.FeatureFiles.Add(featureFile); } if (extension != null && extension.Equals(".config", StringComparison.InvariantCultureIgnoreCase)) { string configContent = File.ReadAllText(projectFile.FileName); GeneratorConfigurationReader.UpdateConfigFromFileContent(generatorConfig, configContent); } } return specFlowProject; }
public string GetTestFullPath(FeatureFileInput featureFileInput) { var path = featureFileInput.GetGeneratedTestFullPath(projectSettings); if (path != null) return path; return featureFileInput.GetFullPath(projectSettings) + GenerationTargetLanguage.GetExtension(projectSettings.ProjectPlatformSettings.Language); }
protected override FeatureFileInput CreateFeatureFileInput(FeatureFileInput featureFile, ITestGenerator generator, SpecFlowProject specFlowProject) { var featureFileInput = base.CreateFeatureFileInput(featureFile, generator, specFlowProject); outputFile = task.PrepareOutputFile(generator.GetTestFullPath(featureFileInput)); featureFileInput.GeneratedTestProjectRelativePath = FileSystemHelper.GetRelativePath(outputFile.FilePathForWriting, specFlowProject.ProjectSettings.ProjectFolder); return featureFileInput; }
public bool? IsUpToDatePreliminary(FeatureFileInput featureFileInput, string generatedTestFullPath, UpToDateCheckingMethod upToDateCheckingMethod) { bool byUpdateTimeCheckResult = IsUpToDateByModificationTimeAndGeneratorVersion(featureFileInput, generatedTestFullPath); if (upToDateCheckingMethod == UpToDateCheckingMethod.ModificationTimeAndGeneratorVersion) return byUpdateTimeCheckResult; if (byUpdateTimeCheckResult == false) return false; return null; }
public Version DetectGeneratedTestVersion(FeatureFileInput featureFileInput) { if (featureFileInput == null) throw new ArgumentNullException("featureFileInput"); try { return DetectGeneratedTestVersionWithExceptions(featureFileInput); } catch(Exception exception) { Debug.WriteLine(exception, "ErrorHandlingTestGenerator.DetectGeneratedTestVersion"); return null; } }
public CodeNamespace GenerateTestFileCode(FeatureFileInput featureFile, TextReader inputReader, CodeDomProvider codeProvider, CodeDomHelper codeDomHelper) { string targetNamespace = GetTargetNamespace(featureFile); SpecFlowLangParser parser = new SpecFlowLangParser(project.Configuration.GeneratorConfiguration.FeatureLanguage); Feature feature = parser.Parse(inputReader, featureFile.GetFullPath(project.ProjectSettings)); IUnitTestGeneratorProvider generatorProvider = ConfigurationServices.CreateInstance<IUnitTestGeneratorProvider>(project.Configuration.GeneratorConfiguration.GeneratorUnitTestProviderType); codeDomHelper.InjectIfRequired(generatorProvider); ISpecFlowUnitTestConverter testConverter = new SpecFlowUnitTestConverter(generatorProvider, codeDomHelper, project.Configuration.GeneratorConfiguration); var codeNamespace = testConverter.GenerateUnitTestFixture(feature, null, targetNamespace); return codeNamespace; }
public void GenerateTestFile(FeatureFileInput featureFile, CodeDomProvider codeProvider, TextReader inputReader, TextWriter outputWriter) { outputWriter = new IndentProcessingWriter(outputWriter); CodeDomHelper codeDomHelper = new CodeDomHelper(codeProvider); var codeNamespace = GenerateTestFileCode(featureFile, inputReader, codeProvider, codeDomHelper); var options = new CodeGeneratorOptions { BracingStyle = "C" }; AddSpecFlowHeader(codeProvider, outputWriter, codeDomHelper); codeProvider.GenerateCodeFromNamespace(codeNamespace, outputWriter, options); AddSpecFlowFooter(codeProvider, outputWriter, codeDomHelper); outputWriter.Flush(); }
public SpecFlowProject ReadSpecFlowProject(IProjectReference projectReference) { var projectFilePath = FileProjectReference.AssertFileProjectReference(projectReference).ProjectFilePath; var project = Engine.GlobalEngine.GetLoadedProject(projectFilePath); if (project == null) { project = new Microsoft.Build.BuildEngine.Project(); project.Load(projectFilePath, ProjectLoadSettings.IgnoreMissingImports); } string projectFolder = Path.GetDirectoryName(projectFilePath); SpecFlowProject specFlowProject = new SpecFlowProject(); specFlowProject.ProjectSettings.ProjectFolder = projectFolder; specFlowProject.ProjectSettings.ProjectName = Path.GetFileNameWithoutExtension(projectFilePath); specFlowProject.ProjectSettings.AssemblyName = project.GetEvaluatedProperty("AssemblyName"); specFlowProject.ProjectSettings.DefaultNamespace = project.GetEvaluatedProperty("RootNamespace"); var items = project.GetEvaluatedItemsByName("None").Cast<BuildItem>() .Concat(project.GetEvaluatedItemsByName("Content").Cast<BuildItem>()); foreach (BuildItem item in items) { var extension = Path.GetExtension(item.FinalItemSpec); if (extension.Equals(".feature", StringComparison.InvariantCultureIgnoreCase)) { var featureFile = new FeatureFileInput(item.FinalItemSpec); var ns = item.GetEvaluatedMetadata("CustomToolNamespace"); if (!String.IsNullOrEmpty(ns)) featureFile.CustomNamespace = ns; specFlowProject.FeatureFiles.Add(featureFile); } if (Path.GetFileName(item.FinalItemSpec).Equals("app.config", StringComparison.InvariantCultureIgnoreCase)) { var configFilePath = Path.Combine(projectFolder, item.FinalItemSpec); var configFileContent = File.ReadAllText(configFilePath); var configurationHolder = GetConfigurationHolderFromFileContent(configFileContent); specFlowProject.ProjectSettings.ConfigurationHolder = configurationHolder; specFlowProject.Configuration = configurationLoader.LoadConfiguration(configurationHolder, projectReference); } } return specFlowProject; }
private string GetGeneratedTestCode(FeatureFileInput featureFileInput) { using (var outputWriter = new IndentProcessingWriter(new StringWriter())) { var codeProvider = codeDomHelper.CreateCodeDomProvider(); var codeNamespace = GenerateTestFileCode(featureFileInput); var options = new CodeGeneratorOptions { BracingStyle = "C" }; AddSpecFlowHeader(codeProvider, outputWriter); codeProvider.GenerateCodeFromNamespace(codeNamespace, outputWriter, options); AddSpecFlowFooter(codeProvider, outputWriter); outputWriter.Flush(); return outputWriter.ToString(); } }
public TestGeneratorResult GenerateTestFile(FeatureFileInput featureFileInput, GenerationSettings settings) { if (featureFileInput == null) throw new ArgumentNullException("featureFileInput"); if (settings == null) throw new ArgumentNullException("settings"); try { return GenerateTestFileWithExceptions(featureFileInput, settings); } catch (ParserException parserException) { return new TestGeneratorResult(parserException.GetParserExceptions().Select( ex => new TestGenerationError(ex.Location == null ? 0 : ex.Location.Line, ex.Location == null ? 0 : ex.Location.Column, ex.Message))); } catch (Exception exception) { return new TestGeneratorResult(new TestGenerationError(exception)); } }
public static SpecFlowProject CreateSpecFlowProjectFrom(Project project) { var specFlowProject = new SpecFlowProject(); specFlowProject.ProjectSettings.ProjectFolder = project.BaseDirectory; specFlowProject.ProjectSettings.ProjectName = project.Name; string defaultNamespace = "Namespace"; if (project is DotNetProject) { defaultNamespace = ((DotNetProject)project).GetDefaultNamespace(project.Name); } // No way to get AssemblyName right now, therefore we'll just use DefaultNamespace specFlowProject.ProjectSettings.AssemblyName = defaultNamespace; specFlowProject.ProjectSettings.DefaultNamespace = defaultNamespace; // TODO: Find out if we really need to add all the feature files everytime we generate foreach (ProjectFile projectFile in project.Files.Where(IsFeatureOrAppConfigFile)) { string extension = Path.GetExtension(projectFile.Name); if (extension.Equals(".feature", StringComparison.InvariantCultureIgnoreCase)) { string fileName = projectFile.FilePath.ToRelative(project.BaseDirectory); var featureFile = new FeatureFileInput(fileName); var customToolNamespace = projectFile.CustomToolNamespace; if (!String.IsNullOrEmpty(customToolNamespace)) featureFile.CustomNamespace = customToolNamespace; specFlowProject.FeatureFiles.Add(featureFile); } if (extension.Equals(".config", StringComparison.InvariantCultureIgnoreCase)) { string configContent = File.ReadAllText(projectFile.FilePath); GeneratorConfigurationReader.UpdateConfigFromFileContent(specFlowProject.Configuration.GeneratorConfiguration, configContent); } } return specFlowProject; }
protected override string GenerateCode(string inputFileContent) { var generatorServices = new VsGeneratorServices(CurrentProject); using (var testGenerator = generatorServices.CreateTestGenerator()) { string projectFolder = VsxHelper.GetProjectFolder(CurrentProject); var fullPath = Path.GetFullPath(Path.Combine(projectFolder, CodeFilePath)); FeatureFileInput featureFileInput = new FeatureFileInput(FileSystemHelper.GetRelativePath(fullPath, projectFolder)) { FeatureFileContent = inputFileContent }; generationResult = testGenerator.GenerateTestFile(featureFileInput, new GenerationSettings()); if (!generationResult.Success) return null; return generationResult.GeneratedTestCode; } }
protected override string GenerateCode(string inputFileContent) { IVisualStudioTracer tracer = VsxHelper.ResolveMefDependency<IVisualStudioTracer>(ServiceProvider.GlobalProvider); var generatorServices = new VsGeneratorServices(CurrentProject, new VsSpecFlowConfigurationReader(CurrentProject, tracer), tracer); using (var testGenerator = generatorServices.CreateTestGenerator()) { string projectFolder = VsxHelper.GetProjectFolder(CurrentProject); var fullPath = Path.GetFullPath(Path.Combine(projectFolder, CodeFilePath)); FeatureFileInput featureFileInput = new FeatureFileInput(FileSystemHelper.GetRelativePath(fullPath, projectFolder)) { FeatureFileContent = inputFileContent }; generationResult = testGenerator.GenerateTestFile(featureFileInput, new GenerationSettings()); if (!generationResult.Success) return null; return generationResult.GeneratedTestCode; } }
public TestGeneratorResult GenerateTestFile(FeatureFileInput featureFileInput, GenerationSettings settings) { if (featureFileInput == null) throw new ArgumentNullException("featureFileInput"); if (settings == null) throw new ArgumentNullException("settings"); try { return GenerateTestFileWithExceptions(featureFileInput, settings); } catch (SpecFlowParserException parserException) { if (parserException.ErrorDetails == null || parserException.ErrorDetails.Count == 0) return new TestGeneratorResult(new TestGenerationError(parserException)); return new TestGeneratorResult(parserException.ErrorDetails.Select( ed => new TestGenerationError(ed.ForcedLine - 1, ed.ForcedColumn - 1, ed.Message))); } catch (Exception exception) { return new TestGeneratorResult(new TestGenerationError(exception)); } }
protected override TestGeneratorResult GenerateTestFile(ITestGenerator generator, FeatureFileInput featureFileInput, GenerationSettings generationSettings) { try { var result = base.GenerateTestFile(generator, featureFileInput, generationSettings); if (result.IsUpToDate) outputFile.Skip(); else outputFile.Done(task.Errors); return result; } catch (Exception ex) { task.RecordException(ex); return new TestGeneratorResult(new TestGenerationError(ex)); } finally { outputFile = null; } }
private string GetTargetNamespace(FeatureFileInput featureFile) { if (!string.IsNullOrEmpty(featureFile.CustomNamespace)) return featureFile.CustomNamespace; if (string.IsNullOrEmpty(project.ProjectSettings.DefaultNamespace)) return null; string targetNamespace = project.ProjectSettings.DefaultNamespace; string projectFolder = project.ProjectSettings.ProjectFolder; string sourceFileFolder = Path.GetDirectoryName(featureFile.GetFullPath(project.ProjectSettings)); if (sourceFileFolder.StartsWith(sourceFileFolder, StringComparison.InvariantCultureIgnoreCase)) { string extraFolders = sourceFileFolder.Substring(projectFolder.Length); if (extraFolders.Length > 0) { string[] parts = extraFolders.TrimStart('\\').Split('\\'); targetNamespace += "." + string.Join(".", parts.Select(p => p.ToIdentifier()).ToArray()); } } return targetNamespace; }
public TestGeneratorResult GenerateTestFile(FeatureFileInput featureFileInput, GenerationSettings settings) { throw new NotImplementedException(); }
public string GetTestFullPath(FeatureFileInput featureFileInput) { return innerGenerator.GetTestFullPath(featureFileInput); }
public Version DetectGeneratedTestVersion(FeatureFileInput featureFileInput) { return innerGenerator.DetectGeneratedTestVersion(featureFileInput); }
public TestGeneratorResult GenerateTestFile(FeatureFileInput featureFileInput, GenerationSettings settings) { return innerGenerator.GenerateTestFile(featureFileInput, settings); }
private CodeNamespace GenerateTestFileCode(FeatureFileInput featureFileInput) { string targetNamespace = GetTargetNamespace(featureFileInput) ?? "SpecFlow.GeneratedTests"; var parser = new SpecFlowGherkinParser(generatorConfiguration.FeatureLanguage); SpecFlowFeature feature; using (var contentReader = featureFileInput.GetFeatureFileContentReader(projectSettings)) { feature = parser.Parse(contentReader, featureFileInput.GetFullPath(projectSettings)); } var featureGenerator = featureGeneratorRegistry.CreateGenerator(feature); var codeNamespace = featureGenerator.GenerateUnitTestFixture(feature, null, targetNamespace); return codeNamespace; }
protected override Version DetectGeneratedTestVersionWithExceptions(FeatureFileInput featureFileInput) { var generatedTestFullPath = GetTestFullPath(featureFileInput); return testHeaderWriter.DetectGeneratedTestVersion(featureFileInput.GetGeneratedTestContent(generatedTestFullPath)); }
private string GetTargetNamespace(FeatureFileInput featureFileInput) { if (!string.IsNullOrEmpty(featureFileInput.CustomNamespace)) return featureFileInput.CustomNamespace; if (projectSettings == null || string.IsNullOrEmpty(projectSettings.DefaultNamespace)) return null; string targetNamespace = projectSettings.DefaultNamespace; var directoryName = Path.GetDirectoryName(featureFileInput.ProjectRelativePath); string namespaceExtension = string.IsNullOrEmpty(directoryName) ? null : string.Join(".", directoryName.TrimStart('\\', '/', '.').Split('\\', '/').Select(f => f.ToIdentifier()).ToArray()); if (!string.IsNullOrEmpty(namespaceExtension)) targetNamespace += "." + namespaceExtension; return targetNamespace; }
public string GetTestFullPath(FeatureFileInput featureFileInput) { throw new NotImplementedException(); }
public Version DetectGeneratedTestVersion(FeatureFileInput featureFileInput) { throw new NotImplementedException(); }
public void Should_be_able_generate_from_a_simple_invalid_feature() { using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory()) { var generator = remoteFactory.CreateGenerator(new ProjectSettings() { ProjectFolder = Path.GetTempPath() }); FeatureFileInput featureFileInput = new FeatureFileInput("Test.feature") { FeatureFileContent = @" Feature: Addition Scenario: Add two numbers Given I have entered 50 into the calculator AndXXX the keyword is misspelled " }; var result = generator.GenerateTestFile(featureFileInput, new GenerationSettings()); result.ShouldNotBeNull(); result.Success.ShouldBeFalse(); result.Errors.ShouldNotBeNull(); result.Errors.ShouldNotBeEmpty(); } }