Skip to content

Commit

Permalink
Do not duplicate config elements in the target section (Edit config s…
Browse files Browse the repository at this point in the history
…tep). (#363)
  • Loading branch information
kavics authored and tusmester committed May 17, 2018
1 parent 73e7d58 commit 94de024
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/ContentRepository/Packaging/Steps/EditConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,9 @@ private bool ExecuteMove(XmlDocument xml, MoveOperation move)
var sourceElements = sourceSectionElement.SelectNodes("*");
targetSectionElement = (XmlElement)xml.DocumentElement.SelectSingleNode(move.TargetSection)
?? CreateSection(xml, move.TargetSection);
foreach (XmlElement sourceElement in sourceElements)
{
var sourceKey = sourceElement.Attributes["key"].Value;
var oldElement = targetSectionElement.SelectSingleNode($"add[@key='{sourceKey}']");
if (oldElement != null)
{
Logger.LogMessage(" Rewritten element in {0}", GetPath(targetSectionElement));
Logger.LogMessage(" {0}", oldElement.OuterXml);
oldElement.ParentNode.RemoveChild(oldElement);
}

// move
foreach (XmlElement sourceElement in sourceElements)
MoveElement(sourceElement, targetSectionElement);
}

DeleteSection(sourceSectionElement, move.SourceSection);

Expand Down Expand Up @@ -275,6 +264,16 @@ private void DeleteElementIfEmpty(XmlElement deletable)

private void MoveElement(XmlElement sourceElement, XmlElement targetSectionElement)
{
// remove old element if exists
var sourceKey = sourceElement.Attributes["key"].Value;
var oldElement = targetSectionElement.SelectSingleNode($"add[@key='{sourceKey}']");
if (oldElement != null && oldElement != sourceElement)
{
Logger.LogMessage(" Rewritten element in {0}", GetPath(targetSectionElement));
Logger.LogMessage(" {0}", oldElement.OuterXml);
oldElement.ParentNode.RemoveChild(oldElement);
}

targetSectionElement.AppendChild(sourceElement.ParentNode.RemoveChild(sourceElement));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,62 @@ public void Step_EditConfiguration_MoveSimpleKeyToExisting()
});
}
[TestMethod]
public void Step_EditConfiguration_MoveSimpleKeyToExistingAndFound()
{
var config = @"<?xml version='1.0' encoding='utf-8'?>
<configuration>
<configSections>
<section name='section1' type='System.Configuration.NameValueFileSectionHandler' />
</configSections>
<section1>
<add key='key2' value='value2' />
</section1>
<appSettings>
<add key='key1' value='value1' />
<add key='key2' value='value2_fromAppSettings' />
<add key='key3' value='value3' />
<add key='key4' value='value4' />
</appSettings>
</configuration>";

var expected = @"<?xml version='1.0' encoding='utf-8'?>
<configuration>
<configSections>
<section name='section1' type='System.Configuration.NameValueFileSectionHandler' />
</configSections>
<section1>
<add key='key1' value='value1' />
<add key='key2' value='value2_fromAppSettings' />
<add key='key3' value='value3' />
</section1>
<appSettings>
<add key='key4' value='value4' />
</appSettings>
</configuration>";

MoveOperationTest(config, expected, new[]
{
new EditConfiguration.MoveOperation
{
SourceSection = "appSettings",
SourceKey = "key1",
TargetSection = "section1",
},
new EditConfiguration.MoveOperation
{
SourceSection = "appSettings",
SourceKey = "key2",
TargetSection = "section1",
},
new EditConfiguration.MoveOperation
{
SourceSection = "appSettings",
SourceKey = "key3",
TargetSection = "section1",
},
});
}
[TestMethod]
public void Step_EditConfiguration_MoveSimpleKeyToExisting_NotFound()
{
var config = @"<?xml version='1.0' encoding='utf-8'?>
Expand Down

0 comments on commit 94de024

Please sign in to comment.