public FeatureCompilerResult Get(FeatureMetadata feature) { var cacheKey = GetCacheKey(feature); _cachedResults.TryGetValue(cacheKey, out var cacheItem); return(cacheItem); }
public void Remove(FeatureMetadata feature) { var featureAssemblyRegex = new Regex(@$ "{feature.Name}.\w+-\w+\-\w+\-\w+\-\w+"); var parts = _appPartManager.ApplicationParts .OfType <AssemblyPart>() .Where(x => featureAssemblyRegex.IsMatch(x.Name)) .ToArray(); foreach (var part in parts) { _logger.LogDebug($"Removing assembly '{part.Assembly.FullName}' from ApplicationPartManager."); _appPartManager.ApplicationParts.Remove(part); } }
public string GetChecksum(FeatureMetadata metadata) { var sb = new StringBuilder(); using var md5 = MD5.Create(); foreach (var filePath in Directory.GetFiles(metadata.FeaturePath, "*.cs", SearchOption.AllDirectories)) { using var stream = File.OpenRead(filePath); var hash = md5.ComputeHash(stream); var value = BitConverter.ToString(hash).Replace("-", string.Empty); sb.Append(value); } return(sb.ToString()); }
public (FeatureCompilerResult, bool hasUpdated) GetOrUpdate(FeatureMetadata feature) { var cacheKey = GetCacheKey(feature); _cachedResults.TryGetValue(cacheKey, out var cacheItem); if (cacheItem != null && IsUpToDate(feature, cacheItem)) { return(cacheItem, false); } var checksum = _checksumGenerator.GetChecksum(feature); var sw = Stopwatch.StartNew(); var newItem = _compiler.Compile(feature.Name, feature.FeaturePath, checksum); sw.Stop(); _logger.LogInformation($"Feature '{feature.Name}' compiled in {sw.ElapsedMilliseconds}ms."); _cachedResults.AddOrUpdate(cacheKey, newItem, (key, oldValue) => newItem); return(newItem, true); }
public void Test1() { CodeDomProvider codeProvider = new VBCodeProvider(); var specFlowGherkinParserFactory = new SpecFlowGherkinParserFactory(); var parser = specFlowGherkinParserFactory.Create(new CultureInfo("en-gb")); SpecFlowDocument document; using (Stream stream = typeof(TestClassGeneratorTests).Assembly.GetManifestResourceStream("PB.SpecFlowMaster.Tests.SpecFlowTarget.feature.txt")) using (StreamReader reader = new StreamReader(stream)) { document = parser.Parse(reader, Path.Combine(TestContext.CurrentContext.TestDirectory, @"SpecFlowTarget.feature.txt")); } var codeNamespace = new CodeNamespace(); var testClass = new CodeTypeDeclaration("FeatureTest"); codeNamespace.Types.Add(testClass); var context = new TestClassGenerationContext( unitTestGeneratorProvider: new NUnit3TestGeneratorProvider(new CodeDomHelper(codeProvider)), document: document, ns: codeNamespace, testClass: testClass, testRunnerField: null, testClassInitializeMethod: null, testClassCleanupMethod: null, testInitializeMethod: null, testCleanupMethod: null, scenarioInitializeMethod: null, scenarioStartMethod: null, scenarioCleanupMethod: null, featureBackgroundMethod: null, generateRowTests: false ); var objectContainer = new ObjectContainer(); objectContainer.RegisterInstanceAs(new FeatureMetadataProvider()); objectContainer.Resolve <FeatureMetadataProvider>()[context.Document] = FeatureMetadata.GetFeatureMetadata(context.Document); var target = new MasterClassGenerator( objectContainer, MasterClassGenerator.CreateContextFromOriginContext(context, context.UnitTestGeneratorProvider), new CodeDomHelper(codeProvider)); target.Generate(); using (var outputWriter = new StringWriter()) { using (codeProvider) { var options = new CodeGeneratorOptions { BracingStyle = "C", }; codeProvider.GenerateCodeFromNamespace(codeNamespace, outputWriter, options); outputWriter.Flush(); var generatedTestCode = outputWriter.ToString(); } } }
private bool IsUpToDate(FeatureMetadata feature, FeatureCompilerResult cacheItem) { var checksum = _checksumGenerator.GetChecksum(feature); return(checksum.Equals(cacheItem.Checksum)); }
private string GetCacheKey(FeatureMetadata feature) => feature.Name.ToLower();