Skip to content

Commit

Permalink
retrieves name of the template from the file.
Browse files Browse the repository at this point in the history
added unit test to unpack and pack an app with modern controls.
  • Loading branch information
marcelbf committed Jun 14, 2024
1 parent d48c722 commit 9e756b9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PAModel/CanvasDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ internal void ApplyBeforeMsAppWriteTransforms(ErrorContainer errors)
var templateDefaults = new Dictionary<string, ControlTemplate>();
foreach (var template in _templates.UsedTemplates)
{
if (!ControlTemplateParser.TryParseTemplate(_templateStore, template.Template, _properties.DocumentAppType, templateDefaults, out _, out _))
if (!ControlTemplateParser.TryParseTemplate(_templateStore, template.Template, _properties.DocumentAppType, templateDefaults, out _, out _, template.Name))
{
errors.GenericError($"Unable to parse template file {template.Name}");
throw new DocumentException();
Expand Down
3 changes: 2 additions & 1 deletion src/PAModel/Serializers/SourceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ private static void LoadTemplateFiles(ErrorContainer errors, CanvasDocument app,
foreach (var file in new DirectoryReader(packagesPath).EnumerateFiles(string.Empty, "*.xml", searchSubdirectories: false))
{
var xmlContents = file.GetContents();
if (!ControlTemplateParser.TryParseTemplate(new TemplateStore(), xmlContents, app._properties.DocumentAppType, loadedTemplates, out var parsedTemplate, out var templateName))
var templateNameFromFile = file._relativeName.Substring(0, file._relativeName.LastIndexOf('_'));
if (!ControlTemplateParser.TryParseTemplate(new TemplateStore(), xmlContents, app._properties.DocumentAppType, loadedTemplates, out var parsedTemplate, out var templateName, templateNameFromFile))
{
errors.GenericError($"Unable to parse template file {file._relativeName}");
throw new DocumentException();
Expand Down
Binary file added src/PAModelTests/Apps/ComboboxDropdown.msapp
Binary file not shown.
27 changes: 27 additions & 0 deletions src/PAModelTests/TemplateStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,31 @@ public void TestHostControlInstancesWithHostType(string appName)
Assert.IsTrue(MsAppTest.Compare(root, tempFile.FullPath, Console.Out));
}
}

// Validate a modern control that has a dynamic template.
// The template has a valid template name, but makes reference to another template.
// This example app has two modern controls (combobox and dropdown) that make reference to the same template.
[DataTestMethod]
[DataRow("ComboboxDropdown.msapp")]
public void TestModernControlWithDynamicTemplate(string appName)
{
//arrange
var root = Path.Combine(Environment.CurrentDirectory, "Apps", appName);
Assert.IsTrue(File.Exists(root), "MSAPP not found");

//act
(var msapp, var errors) = CanvasDocument.LoadFromMsapp(root);
errors.ThrowOnErrors();

//assert
Assert.IsTrue(msapp._templateStore.Contents.ContainsKey("PowerApps_CoreControls_DropdownCanvasTemplate_dataField"));
Assert.IsTrue(msapp._templateStore.Contents.ContainsKey("PowerApps_CoreControls_ComboboxCanvasTemplate_dataField"));

// Repack the app and validate it matches the initial msapp
using (var tempFile = new TempFile())
{
MsAppSerializer.SaveAsMsApp(msapp, tempFile.FullPath, new ErrorContainer());
Assert.IsTrue(MsAppTest.Compare(root, tempFile.FullPath, Console.Out));
}
}
}

0 comments on commit 9e756b9

Please sign in to comment.