public async void CreateAsyncTest_ConditionWithUnquotedChoiceLiteral(string templateConfig, string expectedResult) { // // Template content preparation // string sourceSnippet = @" //#if( ChoiceParam == FirstChoice ) FIRST //#elseif (ChoiceParam == SecondChoice ) SECOND //#elseif (ChoiceParam == ThirdChoice ) THIRD //#else UNKNOWN //#endif "; IDictionary <string, string?> templateSourceFiles = new Dictionary <string, string?>(); // template.json templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, templateConfig); //content templateSourceFiles.Add("sourcFile", sourceSnippet); // // Dependencies preparation and mounting // IEngineEnvironmentSettings environment = _environmentSettingsHelper.CreateEnvironment(); string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(environment); string targetDir = FileSystemHelpers.GetNewVirtualizedPath(environment); TestFileSystemHelper.WriteTemplateSource(environment, sourceBasePath, templateSourceFiles); IMountPoint?sourceMountPoint = TestFileSystemHelper.CreateMountPoint(environment, sourceBasePath); RunnableProjectGenerator rpg = new RunnableProjectGenerator(); SimpleConfigModel configModel = SimpleConfigModel.FromJObject(JObject.Parse(templateConfig)); IRunnableProjectConfig runnableConfig = new RunnableProjectConfig(environment, rpg, configModel, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); IParameterSet parameters = new ParameterSet(runnableConfig); ITemplateParameter choiceParameter; Assert.True(parameters.TryGetParameterDefinition("ChoiceParam", out choiceParameter), "ChoiceParam expected to be extracted from template config"); parameters.ResolvedValues[choiceParameter] = "SecondChoice"; IDirectory sourceDir = sourceMountPoint !.DirectoryInfo("/") !; // // Running the actual scenario: template files processing and generating output (including macros processing) // await rpg.CreateAsync(environment, runnableConfig, sourceDir, parameters, targetDir, CancellationToken.None); // // Veryfying the outputs // string resultContent = environment.Host.FileSystem.ReadAllText(Path.Combine(targetDir, "sourcFile")).Trim(); Assert.Equal(expectedResult, resultContent); }
public async void CreateAsyncTest_MultiChoiceParamJoining() { // // Template content preparation // string templateConfig = @" { ""identity"": ""test.template"", ""symbols"": { ""Platform"": { ""type"": ""parameter"", ""description"": ""The target framework for the project."", ""datatype"": ""choice"", ""allowMultipleValues"": true, ""choices"": [ { ""choice"": ""Windows"", ""description"": ""Windows Desktop"" }, { ""choice"": ""WindowsPhone"", ""description"": ""Windows Phone"" }, { ""choice"": ""MacOS"", ""description"": ""Macintosh computers"" }, { ""choice"": ""iOS"", ""description"": ""iOS mobile"" }, { ""choice"": ""android"", ""description"": ""android mobile"" }, { ""choice"": ""nix"", ""description"": ""Linux distributions"" } ], ""defaultValue"": ""MacOS|iOS"" }, ""joinedRename"": { ""type"": ""generated"", ""generator"": ""join"", ""replaces"": ""SupportedPlatforms"", ""parameters"": { ""symbols"": [ { ""type"": ""ref"", ""value"": ""Platform"" } ], ""separator"": "", "", ""removeEmptyValues"": true, } } } } "; string sourceSnippet = @" // This file is generated for platfrom: SupportedPlatforms "; string expectedSnippet = @" // This file is generated for platfrom: MacOS, iOS "; IDictionary <string, string?> templateSourceFiles = new Dictionary <string, string?>(); // template.json templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, templateConfig); //content templateSourceFiles.Add("sourcFile", sourceSnippet); // // Dependencies preparation and mounting // IEngineEnvironmentSettings environment = _environmentSettingsHelper.CreateEnvironment(); string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(environment); string targetDir = FileSystemHelpers.GetNewVirtualizedPath(environment); TestFileSystemHelper.WriteTemplateSource(environment, sourceBasePath, templateSourceFiles); IMountPoint?sourceMountPoint = TestFileSystemHelper.CreateMountPoint(environment, sourceBasePath); RunnableProjectGenerator rpg = new RunnableProjectGenerator(); SimpleConfigModel configModel = SimpleConfigModel.FromJObject(JObject.Parse(templateConfig)); IRunnableProjectConfig runnableConfig = new RunnableProjectConfig(environment, rpg, configModel, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); IParameterSet parameters = new ParameterSet(runnableConfig); ITemplateParameter choiceParameter; Assert.True(parameters.TryGetParameterDefinition("Platform", out choiceParameter), "ChoiceParam expected to be extracted from template config"); parameters.ResolvedValues[choiceParameter] = new MultiValueParameter(new[] { "MacOS", "iOS" }); IDirectory sourceDir = sourceMountPoint !.DirectoryInfo("/") !; // // Running the actual scenario: template files processing and generating output (including macros processing) // await rpg.CreateAsync(environment, runnableConfig, sourceDir, parameters, targetDir, CancellationToken.None); // // Veryfying the outputs // string resultContent = environment.Host.FileSystem.ReadAllText(Path.Combine(targetDir, "sourcFile")); Assert.Equal(expectedSnippet, resultContent); }
public async void CreateAsyncTest_GuidsMacroProcessingCaseSensitivity() { // // Template content preparation // Guid inputTestGuid = new Guid("12aa8f4e-a4aa-4ac1-927c-94cb99485ef1"); string contentFileNamePrefix = "content - "; SimpleConfigModel config = new SimpleConfigModel() { Identity = "test", Guids = new List <Guid>() { inputTestGuid } }; IDictionary <string, string?> templateSourceFiles = new Dictionary <string, string?>(); // template.json templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, config.ToJObject().ToString()); //content foreach (string guidFormat in GuidMacroConfig.DefaultFormats.Select(c => c.ToString())) { templateSourceFiles.Add(contentFileNamePrefix + guidFormat, inputTestGuid.ToString(guidFormat)); } // // Dependencies preparation and mounting // IEngineEnvironmentSettings environment = _environmentSettingsHelper.CreateEnvironment(); string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(environment); string targetDir = FileSystemHelpers.GetNewVirtualizedPath(environment); RunnableProjectGenerator rpg = new RunnableProjectGenerator(); TestFileSystemHelper.WriteTemplateSource(environment, sourceBasePath, templateSourceFiles); IMountPoint? sourceMountPoint = TestFileSystemHelper.CreateMountPoint(environment, sourceBasePath); IRunnableProjectConfig runnableConfig = new RunnableProjectConfig(environment, rpg, config, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); IParameterSet parameters = new ParameterSet(runnableConfig); IDirectory sourceDir = sourceMountPoint !.DirectoryInfo("/") !; // // Running the actual scenario: template files processing and generating output (including macros processing) // await rpg.CreateAsync(environment, runnableConfig, sourceDir, parameters, targetDir, CancellationToken.None); // // Veryfying the outputs // Guid expectedResultGuid = Guid.Empty; foreach (string guidFormat in GuidMacroConfig.DefaultFormats.Select(c => c.ToString())) { string resultContent = environment.Host.FileSystem.ReadAllText(Path.Combine(targetDir, contentFileNamePrefix + guidFormat)); Guid resultGuid; Assert.True( Guid.TryParseExact(resultContent, guidFormat, out resultGuid), $"Expected the result conent ({resultContent}) to be parseable by Guid format '{guidFormat}'"); if (expectedResultGuid == Guid.Empty) { expectedResultGuid = resultGuid; } else { Assert.Equal(expectedResultGuid, resultGuid); } } Assert.NotEqual(inputTestGuid, expectedResultGuid); }
public async void CreateAsyncTest_MultiChoiceParamReplacingAndCondition() { // // Template content preparation // string templateConfig = @" { ""identity"": ""test.template"", ""symbols"": { ""ChoiceParam"": { ""type"": ""parameter"", ""description"": ""sample switch"", ""datatype"": ""choice"", ""allowMultipleValues"": true, ""enableQuotelessLiterals"": true, ""choices"": [ { ""choice"": ""FirstChoice"", ""description"": ""First Sample Choice"" }, { ""choice"": ""SecondChoice"", ""description"": ""Second Sample Choice"" }, { ""choice"": ""ThirdChoice"", ""description"": ""Third Sample Choice"" } ], ""defaultValue"": ""ThirdChoice"", ""replaces"": ""REPLACE_VALUE"" } } } "; string sourceSnippet = @" MultiChoiceValue: REPLACE_VALUE //#if( ChoiceParam == FirstChoice ) FIRST //#endif //#if (ChoiceParam == SecondChoice ) SECOND //#endif //#if (ChoiceParam == ThirdChoice ) THIRD //#endif "; string expectedSnippet = @" MultiChoiceValue: SecondChoice|ThirdChoice SECOND THIRD "; IDictionary <string, string?> templateSourceFiles = new Dictionary <string, string?>(); // template.json templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, templateConfig); //content templateSourceFiles.Add("sourcFile", sourceSnippet); // // Dependencies preparation and mounting // IEngineEnvironmentSettings environment = _environmentSettingsHelper.CreateEnvironment(); string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(environment); string targetDir = FileSystemHelpers.GetNewVirtualizedPath(environment); TestFileSystemHelper.WriteTemplateSource(environment, sourceBasePath, templateSourceFiles); IMountPoint?sourceMountPoint = TestFileSystemHelper.CreateMountPoint(environment, sourceBasePath); RunnableProjectGenerator rpg = new RunnableProjectGenerator(); SimpleConfigModel configModel = SimpleConfigModel.FromJObject(JObject.Parse(templateConfig)); IRunnableProjectConfig runnableConfig = new RunnableProjectConfig(environment, rpg, configModel, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); IParameterSet parameters = new ParameterSet(runnableConfig); ITemplateParameter choiceParameter; Assert.True(parameters.TryGetParameterDefinition("ChoiceParam", out choiceParameter), "ChoiceParam expected to be extracted from template config"); parameters.ResolvedValues[choiceParameter] = new MultiValueParameter(new[] { "SecondChoice", "ThirdChoice" }); IDirectory sourceDir = sourceMountPoint !.DirectoryInfo("/") !; // // Running the actual scenario: template files processing and generating output (including macros processing) // await rpg.CreateAsync(environment, runnableConfig, sourceDir, parameters, targetDir, CancellationToken.None); // // Veryfying the outputs // string resultContent = environment.Host.FileSystem.ReadAllText(Path.Combine(targetDir, "sourcFile")); Assert.Equal(expectedSnippet, resultContent); }
public void PerformTemplateValidation_ChoiceValuesValidation(string paramDefintion, bool isMultichoice, bool expectedToBeValid) { // // Template content preparation // Guid inputTestGuid = new Guid("12aa8f4e-a4aa-4ac1-927c-94cb99485ef1"); string contentFileNamePrefix = "content - "; JObject choiceParam = JObject.Parse(paramDefintion); choiceParam["AllowMultipleValues"] = isMultichoice; SimpleConfigModel config = new SimpleConfigModel() { Identity = "test", Name = "name", ShortNameList = new [] { "shortName" }, Symbols = new Dictionary <string, ISymbolModel>() { { "ParamA", new ParameterSymbol(choiceParam, null) } } }; IDictionary <string, string?> templateSourceFiles = new Dictionary <string, string?>(); // template.json templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, config.ToJObject().ToString()); //content foreach (string guidFormat in GuidMacroConfig.DefaultFormats.Select(c => c.ToString())) { templateSourceFiles.Add(contentFileNamePrefix + guidFormat, inputTestGuid.ToString(guidFormat)); } // // Dependencies preparation and mounting // List <(LogLevel, string)> loggedMessages = new List <(LogLevel, string)>(); InMemoryLoggerProvider loggerProvider = new InMemoryLoggerProvider(loggedMessages); IEngineEnvironmentSettings environment = _environmentSettingsHelper.CreateEnvironment(addLoggerProviders: new [] { loggerProvider }); string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(environment); string targetDir = FileSystemHelpers.GetNewVirtualizedPath(environment); RunnableProjectGenerator rpg = new RunnableProjectGenerator(); TestFileSystemHelper.WriteTemplateSource(environment, sourceBasePath, templateSourceFiles); IMountPoint? sourceMountPoint = TestFileSystemHelper.CreateMountPoint(environment, sourceBasePath); RunnableProjectConfig runnableConfig = new RunnableProjectConfig(environment, rpg, config, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); if (expectedToBeValid) { runnableConfig.PerformTemplateValidation(); Assert.Empty(loggedMessages.Where(l => l.Item1 >= LogLevel.Warning)); } else { var exc = Assert.Throws <TemplateValidationException>(runnableConfig.PerformTemplateValidation); Assert.Contains("The template configuration ", exc.Message); Assert.Contains(" is not valid.", exc.Message); Assert.Single(loggedMessages.Where(l => l.Item1 >= LogLevel.Warning)); string errorMessage = loggedMessages.First(l => l.Item1 >= LogLevel.Warning).Item2; Assert.Contains( "Choice parameter is invalid. It allows multiple values ('AllowMultipleValues=true'), while some of the configured choices contain separator characters ('|', ','). Invalid choices: {First|Choice}", errorMessage); } }
public void WriteSource() { TestFileSystemHelper.WriteTemplateSource(_environmentSettings, _sourceBaseDir, _sourceFiles); }
public async void InstantiateAsync_ParamsProperlyHonored(string?parameterValue, string expectedOutput, bool instantiateShouldFail) { // // Template content preparation // string sourceSnippet = @" //#if( ChoiceParam == FirstChoice ) FIRST //#elseif (ChoiceParam == SecondChoice ) SECOND //#elseif (ChoiceParam == ThirdChoice ) THIRD //#else UNKNOWN //#endif "; IDictionary <string, string?> templateSourceFiles = new Dictionary <string, string?>(); // template.json templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, TemplateConfigQuotelessLiteralsEnabled); //content templateSourceFiles.Add("sourceFile", sourceSnippet); // // Dependencies preparation and mounting // IEngineEnvironmentSettings environment = _engineEnvironmentSettings; string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(environment); TestFileSystemHelper.WriteTemplateSource(environment, sourceBasePath, templateSourceFiles); IMountPoint?sourceMountPoint = TestFileSystemHelper.CreateMountPoint(environment, sourceBasePath); RunnableProjectGenerator rpg = new RunnableProjectGenerator(); // cannot use SimpleConfigModel dirrectly - due to missing easy way of creating ParameterSymbols SimpleConfigModel configModel = SimpleConfigModel.FromJObject(JObject.Parse(TemplateConfigQuotelessLiteralsEnabled)); var runnableConfig = new RunnableProjectConfig(environment, rpg, configModel, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); TemplateCreator creator = new TemplateCreator(_engineEnvironmentSettings); string targetDir = FileSystemHelpers.GetNewVirtualizedPath(_engineEnvironmentSettings); IReadOnlyDictionary <string, string?> parameters = new Dictionary <string, string?>() { { "ChoiceParam", parameterValue } }; var res = await creator.InstantiateAsync( templateInfo : runnableConfig, name : "tst", fallbackName : "tst2", inputParameters : parameters, outputPath : targetDir); if (instantiateShouldFail) { Assert.NotNull(res.ErrorMessage); Assert.Null(res.OutputBaseDirectory); } else { Assert.Null(res.ErrorMessage); Assert.NotNull(res.OutputBaseDirectory); string resultContent = _engineEnvironmentSettings.Host.FileSystem .ReadAllText(Path.Combine(res.OutputBaseDirectory !, "sourceFile")).Trim(); Assert.Equal(expectedOutput, resultContent); } }