public SourceCodeService(
            ISourceFileRepository sourceFilesRepository,
            ILogger logger,
            ICalculationsRepository calculationsRepository,
            ISourceFileGeneratorProvider sourceFileGeneratorProvider,
            ICompilerFactory compilerFactory,
            ICodeMetadataGeneratorService codeMetadataGenerator,
            ICalcsResiliencePolicies resiliencePolicies)
        {
            Guard.ArgumentNotNull(sourceFilesRepository, nameof(sourceFilesRepository));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(calculationsRepository, nameof(calculationsRepository));
            Guard.ArgumentNotNull(sourceFileGeneratorProvider, nameof(sourceFileGeneratorProvider));
            Guard.ArgumentNotNull(compilerFactory, nameof(compilerFactory));
            Guard.ArgumentNotNull(codeMetadataGenerator, nameof(codeMetadataGenerator));
            Guard.ArgumentNotNull(resiliencePolicies, nameof(resiliencePolicies));
            Guard.ArgumentNotNull(resiliencePolicies?.SourceFilesRepository, nameof(resiliencePolicies.SourceFilesRepository));
            Guard.ArgumentNotNull(resiliencePolicies?.CalculationsRepository, nameof(resiliencePolicies.CalculationsRepository));

            _sourceFilesRepository = sourceFilesRepository;
            _logger = logger;
            _calculationsRepository       = calculationsRepository;
            _sourceFileGeneratorProvider  = sourceFileGeneratorProvider;
            _compilerFactory              = compilerFactory;
            _sourceFileGenerator          = sourceFileGeneratorProvider.CreateSourceFileGenerator(TargetLanguage.VisualBasic);
            _codeMetadataGenerator        = codeMetadataGenerator;
            _sourceFilesRepositoryPolicy  = resiliencePolicies.SourceFilesRepository;
            _calculationsRepositoryPolicy = resiliencePolicies.CalculationsRepository;
        }
Пример #2
0
        private SourceCodeService CreateServiceWithRealCompiler(IFeatureToggle featureToggle = null)
        {
            ILogger logger = CreateLogger();
            ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider();

            sourceFileGeneratorProvider
            .CreateSourceFileGenerator(Arg.Is(TargetLanguage.VisualBasic))
            .Returns(new VisualBasicSourceFileGenerator(logger));

            VisualBasicCompiler vbCompiler      = new VisualBasicCompiler(logger);
            CompilerFactory     compilerFactory = new CompilerFactory(null, vbCompiler);

            return(CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider, calculationsRepository: Substitute.For <ICalculationsRepository>(), logger: logger, compilerFactory: compilerFactory));
        }
Пример #3
0
 private static SourceCodeService CreateSourceCodeService(
     ISourceFileRepository sourceFilesRepository = null,
     ILogger logger = null,
     ICalculationsRepository calculationsRepository           = null,
     ISourceFileGeneratorProvider sourceFileGeneratorProvider = null,
     ICompilerFactory compilerFactory = null,
     ICodeMetadataGeneratorService codeMetadataGenerator = null,
     ICalcsResiliencePolicies resiliencePolicies         = null)
 {
     return(new SourceCodeService(
                sourceFilesRepository ?? CreateSourceFileRepository(),
                logger ?? CreateLogger(),
                calculationsRepository ?? CreateCalculationsRepository(),
                sourceFileGeneratorProvider ?? CreateSourceFileGeneratorProvider(),
                compilerFactory ?? CreateCompilerFactory(),
                codeMetadataGenerator ?? CreateCodeMetadataGeneratorService(),
                resiliencePolicies ?? CreatePolicies()));
 }
Пример #4
0
        public void Compile_ErrorThrown_ReturnsErrorAsCompilerMessage()
        {
            //Arrange
            BuildProject buildProject = new BuildProject {
                SpecificationId = "3456"
            };
            IEnumerable <Calculation> calculations = new List <Calculation>();

            string errorMessage = "The sky is red, I don't understand";

            ISourceFileGenerator sourceFileGenerator = Substitute.For <ISourceFileGenerator>();

            sourceFileGenerator
            .GenerateCode(buildProject, calculations, Arg.Any <CompilerOptions>())
            .Throws(new Exception(errorMessage));

            ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider();

            sourceFileGeneratorProvider
            .CreateSourceFileGenerator(TargetLanguage.VisualBasic)
            .Returns(sourceFileGenerator);

            SourceCodeService sourceCodeService = CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider);

            //Act
            Build result = sourceCodeService.Compile(buildProject, calculations);

            //Assert
            result.CompilerMessages.Count
            .Should()
            .Be(1);
            result.CompilerMessages.Count(x => x.Message == errorMessage && x.Severity == Severity.Error)
            .Should()
            .Be(1);

            sourceFileGenerator
            .Received(1)
            .GenerateCode(buildProject,
                          calculations,
                          Arg.Is <CompilerOptions>(x => x.SpecificationId == buildProject.SpecificationId && !x.OptionStrictEnabled));
        }
Пример #5
0
        public void Compile_GivenStringCompareInCodeAndAggregatesIsEnabledAndCalculationAggregateFunctionsFound_CompilesCodeAndReturnsOk()
        {
            //Arrange
            string stringCompareCode = "Public Class TestClass\nPublic Property E1 As ExampleClass\nPublic Function TestFunction As String\nIf E1.ProviderType = \"goodbye\" Then\nReturn Sum(Calc1)\nElse Return \"no\"\nEnd If\nEnd Function\nEnd Class";

            Calculation calculation = new Calculation
            {
                Id              = calculationId,
                BuildProjectId  = buildProjectId,
                Current         = new CalculationVersion(),
                SpecificationId = specificationId,
                Name            = "TestFunction"
            };

            IEnumerable <Calculation> calculations = new List <Calculation>()
            {
                calculation
            };

            BuildProject buildProject = new BuildProject
            {
                SpecificationId = specificationId
            };

            ILogger logger = CreateLogger();

            List <SourceFile> sourceFiles = new List <SourceFile>
            {
                new SourceFile {
                    FileName = "project.vbproj", SourceCode = "<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><TargetFramework>netcoreapp2.0</TargetFramework></PropertyGroup></Project>"
                },
                new SourceFile {
                    FileName = "ExampleClass.vb", SourceCode = "Public Class ExampleClass\nPublic Property ProviderType() As String\nEnd Class"
                },
                new SourceFile {
                    FileName = "Calculation.vb", SourceCode = stringCompareCode
                }
            };

            Build build = new Build
            {
                Success     = true,
                SourceFiles = sourceFiles
            };

            Dictionary <string, string> sourceCodes = new Dictionary <string, string>()
            {
                { "TestFunction", stringCompareCode },
                { "Calc1", "return 1" }
            };

            CompilerOptions compilerOptions = new CompilerOptions();

            ISourceFileGenerator sourceFileGenerator = Substitute.For <ISourceFileGenerator>();

            sourceFileGenerator
            .GenerateCode(Arg.Is(buildProject), Arg.Is(calculations), compilerOptions)
            .Returns(sourceFiles);

            ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider();

            sourceFileGeneratorProvider
            .CreateSourceFileGenerator(Arg.Any <TargetLanguage>())
            .Returns(sourceFileGenerator);

            ICompiler compiler = CreateCompiler();

            ICompilerFactory compilerFactory = CreateCompilerFactory(compiler, sourceFiles);

            SourceCodeService sourceCodeService = CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider, compilerFactory: compilerFactory);

            //Act
            Build buildResult = sourceCodeService.Compile(buildProject, calculations, compilerOptions);

            //Assert
            compiler
            .Received(1)
            .GenerateCode(Arg.Is <List <SourceFile> >(m => m.Count == 3));
        }
Пример #6
0
        public async Task GetAssembly_GivenAssemblyDoesNotExist_CompilesNewAssembly()
        {
            //Arrange
            BuildProject buildProject = new BuildProject
            {
                SpecificationId = specificationId,
            };

            Calculation calculation = new Calculation
            {
                Id              = calculationId,
                BuildProjectId  = buildProjectId,
                Current         = new CalculationVersion(),
                SpecificationId = specificationId,
                Name            = "TestFunction"
            };

            IEnumerable <Calculation> calculations = new List <Calculation>()
            {
                calculation
            };

            ISourceFileRepository sourceFileRepository = CreateSourceFileRepository();

            sourceFileRepository
            .DoesAssemblyExist(Arg.Is(specificationId))
            .Returns(false);

            List <SourceFile> sourceFiles = new List <SourceFile>
            {
                new SourceFile {
                    FileName = "project.vbproj", SourceCode = "<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><TargetFramework>netcoreapp2.0</TargetFramework></PropertyGroup></Project>"
                },
                new SourceFile {
                    FileName = "ExampleClass.vb", SourceCode = "Public Class ExampleClass\nPublic Property ProviderType() As String\nEnd Class"
                },
                new SourceFile {
                    FileName = "Calculation.vb", SourceCode = "code"
                }
            };

            buildProject.Build = new Build
            {
                SourceFiles = sourceFiles,
            };

            Build newBuild = new Build
            {
                SourceFiles = sourceFiles,
                Assembly    = new byte[100]
            };

            ISourceFileGenerator sourceFileGenerator = Substitute.For <ISourceFileGenerator>();

            sourceFileGenerator
            .GenerateCode(Arg.Is(buildProject), Arg.Any <IEnumerable <Calculation> >(), Arg.Any <CompilerOptions>())
            .Returns(sourceFiles);

            ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider();

            sourceFileGeneratorProvider
            .CreateSourceFileGenerator(Arg.Any <TargetLanguage>())
            .Returns(sourceFileGenerator);

            ICompiler compiler = CreateCompiler();

            compiler
            .GenerateCode(Arg.Any <List <SourceFile> >())
            .Returns(newBuild);

            ICompilerFactory compilerFactory = CreateCompilerFactory(compiler, sourceFiles);

            SourceCodeService sourceCodeService = CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider, compilerFactory: compilerFactory);

            //Act
            byte[] assembly = await sourceCodeService.GetAssembly(buildProject);

            //Assert
            assembly
            .Should()
            .NotBeNull();

            assembly
            .Length
            .Should()
            .Be(100);
        }