Пример #1
0
 private void GenerateTestFile(SpecFlowFeatureFile featureFile, CodeDomProvider codeProvider, TextWriter outputWriter)
 {
     using(var reader = new StreamReader(featureFile.GetFullPath(project)))
     {
         GenerateTestFile(featureFile, codeProvider, reader, outputWriter);
     }
 }
Пример #2
0
        private static SpecFlowProject LoadSpecFlowProjectFromDteProjectInternal(Project project)
        {
            string projectFolder = Path.GetDirectoryName(project.FullName);

            SpecFlowProject specFlowProject = new SpecFlowProject();
            specFlowProject.ProjectFolder = projectFolder;
            specFlowProject.ProjectName = Path.GetFileNameWithoutExtension(project.FullName);
            specFlowProject.AssemblyName = project.Properties.Item("AssemblyName").Value as string;
            specFlowProject.DefaultNamespace = project.Properties.Item("DefaultNamespace").Value as string;

            foreach (ProjectItem projectItem in VsxHelper.GetAllProjectItem(project).Where(IsPhysicalFile))
            {
                var fileName = GetRelativePath(GetFileName(projectItem), projectFolder);

                var extension = Path.GetExtension(fileName);
                if (extension.Equals(".feature", StringComparison.InvariantCultureIgnoreCase))
                {
                    var featureFile = new SpecFlowFeatureFile(fileName);
                    var ns = projectItem.Properties.Item("CustomToolNamespace").Value as string;
                    if (!String.IsNullOrEmpty(ns))
                        featureFile.CustomNamespace = ns;
                    specFlowProject.FeatureFiles.Add(featureFile);
                }

                if (Path.GetFileName(fileName).Equals("app.config", StringComparison.InvariantCultureIgnoreCase))
                {
                    string configFileContent = GetFileContent(projectItem);
                    GeneratorConfigurationReader.UpdateConfigFromFileContent(specFlowProject.GeneratorConfiguration, configFileContent);
                    RuntimeConfigurationReader.UpdateConfigFromFileContent(specFlowProject.RuntimeConfiguration, configFileContent);
                }
            }
            return specFlowProject;
        }
Пример #3
0
        public static SpecFlowProject LoadSpecFlowProjectFromMsBuild(string projectFile)
        {
            projectFile = Path.GetFullPath(projectFile);
            Project project = new Project();
            project.Load(projectFile, ProjectLoadSettings.IgnoreMissingImports);

            string projectFolder = Path.GetDirectoryName(projectFile);

            SpecFlowProject specFlowProject = new SpecFlowProject();
            specFlowProject.ProjectFolder = projectFolder;
            specFlowProject.ProjectName = Path.GetFileNameWithoutExtension(projectFile);
            specFlowProject.AssemblyName = project.GetEvaluatedProperty("AssemblyName");
            specFlowProject.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 SpecFlowFeatureFile(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))
                {
                    GeneratorConfigurationReader.UpdateConfigFromFile(specFlowProject.GeneratorConfiguration, Path.Combine(projectFolder, item.FinalItemSpec));
                }
            }
            return specFlowProject;
        }
Пример #4
0
        private static SpecFlowProject LoadSpecFlowProjectFromDteProjectInternal(Project project)
        {
            string projectFolder = Path.GetDirectoryName(project.FullName);

            SpecFlowProject specFlowProject = new SpecFlowProject();
            specFlowProject.ProjectFolder = projectFolder;
            specFlowProject.ProjectName = Path.GetFileNameWithoutExtension(project.FullName);
            specFlowProject.AssemblyName = project.Properties.Item("AssemblyName").Value as string;
            specFlowProject.DefaultNamespace = project.Properties.Item("DefaultNamespace").Value as string;

            foreach (ProjectItem projectItem in VsxHelper.GetAllPhysicalFileProjectItem(project))
            {
                if (".feature".Equals(Path.GetExtension(projectItem.Name), StringComparison.InvariantCultureIgnoreCase))
                {
                    var featureFile = new SpecFlowFeatureFile(VsxHelper.GetProjectRelativePath(projectItem));
                    var ns = projectItem.Properties.Item("CustomToolNamespace").Value as string;
                    if (!String.IsNullOrEmpty(ns))
                        featureFile.CustomNamespace = ns;
                    specFlowProject.FeatureFiles.Add(featureFile);
                }
            }

            specFlowProject.Configuration = LoadSpecFlowConfigurationFromDteProjectInternal(project);

            return specFlowProject;
        }
Пример #5
0
 protected virtual void GenerateFile(SpecFlowGenerator generator, SpecFlowFeatureFile featureFile, string codeFileFullPath)
 {
     using (var writer = GetWriter(codeFileFullPath))
     {
         generator.GenerateCSharpTestFile(featureFile, writer);
     }
 }
Пример #6
0
 public SpecFlowFeatureFile GetOrCreateFeatureFile(string featureFileName)
 {
     featureFileName = Path.GetFullPath(Path.Combine(ProjectFolder, featureFileName));
     var result = FeatureFiles.Find(ff => ff.GetFullPath(this).Equals(featureFileName, StringComparison.InvariantCultureIgnoreCase));
     if (result == null)
     {
         result = new SpecFlowFeatureFile(featureFileName); //TODO: make it project relative
     }
     return result;
 }
Пример #7
0
        public SpecFlowFeatureFile GetOrCreateFeatureFile(string featureFileName)
        {
            featureFileName = Path.GetFullPath(Path.Combine(ProjectFolder, featureFileName));
            var result = FeatureFiles.Find(ff => ff.GetFullPath(this).Equals(featureFileName, StringComparison.InvariantCultureIgnoreCase));

            if (result == null)
            {
                result = new SpecFlowFeatureFile(featureFileName); //TODO: make it project relative
            }
            return(result);
        }
Пример #8
0
        public CodeNamespace GenerateTestFileCode(SpecFlowFeatureFile featureFile, TextReader inputReader)
        {
            string targetNamespace = GetTargetNamespace(featureFile);

            SpecFlowLangParser parser = new SpecFlowLangParser(project.GeneratorConfiguration.FeatureLanguage);
            Feature feature = parser.Parse(inputReader, featureFile.GetFullPath(project));

            IUnitTestGeneratorProvider generatorProvider = ConfigurationServices.CreateInstance<IUnitTestGeneratorProvider>(project.GeneratorConfiguration.GeneratorUnitTestProviderType);

            SpecFlowUnitTestConverter testConverter = new SpecFlowUnitTestConverter(generatorProvider, project.GeneratorConfiguration.AllowDebugGeneratedFiles);

            var codeNamespace = testConverter.GenerateUnitTestFixture(feature, null, targetNamespace);
            return codeNamespace;
        }
Пример #9
0
        public void GenerateTestFile(SpecFlowFeatureFile featureFile, CodeDomProvider codeProvider, TextReader inputReader, TextWriter outputWriter)
        {
            outputWriter = new HackedWriter(outputWriter);

            var codeNamespace = GenerateTestFileCode(featureFile, inputReader);
            var options = new CodeGeneratorOptions
                {
                    BracingStyle = "C"
                };

            AddSpecFlowHeader(codeProvider, outputWriter);
            codeProvider.GenerateCodeFromNamespace(codeNamespace, outputWriter, options);
            outputWriter.Flush();
        }
Пример #10
0
 protected override void GenerateFile(SpecFlowGenerator generator, SpecFlowFeatureFile featureFile, string codeFileFullPath)
 {
     try
     {
         base.GenerateFile(generator, featureFile, codeFileFullPath);
         outputFile.Done(task.Errors);
     }
     catch(Exception ex)
     {
         task.RecordException(ex);
     }
     finally
     {
         outputFile = null;
     }
 }
Пример #11
0
        public static SpecFlowProject LoadSpecFlowProjectFromMsBuild(string projectFile)
        {
            projectFile = Path.GetFullPath(projectFile);

            Project project = Engine.GlobalEngine.GetLoadedProject(projectFile);

            if (project == null)
            {
                project = new Project();
                project.Load(projectFile, ProjectLoadSettings.IgnoreMissingImports);
            }

            string projectFolder = Path.GetDirectoryName(projectFile);

            SpecFlowProject specFlowProject = new SpecFlowProject();

            specFlowProject.ProjectFolder    = projectFolder;
            specFlowProject.ProjectName      = Path.GetFileNameWithoutExtension(projectFile);
            specFlowProject.AssemblyName     = project.GetEvaluatedProperty("AssemblyName");
            specFlowProject.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 SpecFlowFeatureFile(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))
                {
                    GeneratorConfigurationReader.UpdateConfigFromFile(specFlowProject.GeneratorConfiguration, Path.Combine(projectFolder, item.FinalItemSpec));
                }
            }
            return(specFlowProject);
        }
Пример #12
0
 public void GenerateCSharpTestFile(SpecFlowFeatureFile featureFile, TextWriter outputWriter)
 {
     var codeProvider = new CSharpCodeProvider();
     GenerateTestFile(featureFile, codeProvider, outputWriter);
 }
Пример #13
0
        private string GetTargetNamespace(SpecFlowFeatureFile featureFile)
        {
            if (!string.IsNullOrEmpty(featureFile.CustomNamespace))
                return featureFile.CustomNamespace;

            if (string.IsNullOrEmpty(project.DefaultNamespace))
                return null;

            string targetNamespace = project.DefaultNamespace;
            string projectFolder = project.ProjectFolder;
            string sourceFileFolder = Path.GetDirectoryName(featureFile.GetFullPath(project));
            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;
        }