Пример #1
0
        public void TestSetAttributesTransformWithArguments()
        {
            // change the "debug" attribute to false

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web>
                    <compilation debug=""false"" xdt:Transform=""SetAttributes(debug)"" />
                  </system.web>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output
                .Element("configuration")
                .Element("system.web")
                .Element("compilation");

            Assert.IsTrue(element.Attribute("debug").Value == "false");

            // ensure we haven't accidentally added attributes from the transform document
            Assert.IsFalse(output.Descendants().Any(e =>
                e.Attributes().Any(a =>
                    a.Name.NamespaceName == Namespaces.Xdt)));
        }
Пример #2
0
        public void TestIdentityTransform()
        {
            var input     = GetInputDocument();
            var transform = GetIdentityTransformDocument();

            var output = new XdtTransformer().Transform(input, transform);

            Assert.IsTrue(XDocument.DeepEquals(input, output));
        }
Пример #3
0
        public void TestIdentityTransform()
        {
            var input = GetInputDocument();
            var transform = GetIdentityTransformDocument();

            var output = new XdtTransformer().Transform(input, transform);

            Assert.IsTrue(XDocument.DeepEquals(input, output));
        }
Пример #4
0
        /// <summary>
        /// Transforms the source file using the transform file and writes the output into
        /// destination file.
        /// </summary>
        protected void Run()
        {
            var sourceDocument = XDocument.Load(SourceFile);
            var transformDocument = XDocument.Load(TransformFile);

            var xdtTransformer = new XdtTransformer();

            var destinationDocument = xdtTransformer.Transform(sourceDocument, transformDocument);

            destinationDocument.Save(DestinationFile);
        }
Пример #5
0
            internal XdtTransformerTest(string transformStreamContent)
            {
                _stream = new MemoryStream(Encoding.UTF8.GetBytes(transformStreamContent));

                TransformStreamContent = transformStreamContent;
                StreamTaskFactory      = () => Task.FromResult <Stream>(_stream);
                Transformer            = new XdtTransformer();
                ProjectSystem          = new Mock <IMSBuildProjectSystem>(MockBehavior.Strict);
                TestDirectory          = TestDirectory.Create();
                TargetFile             = new FileInfo(Path.Combine(TestDirectory.Path, "target.file"));
            }
Пример #6
0
        public void TestIdentityTransform()
        {
            // supply an empty transform document

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"" />
                ");

            var output = new XdtTransformer().Transform(input, transform);

            Assert.IsTrue(XDocument.DeepEquals(input, output));
        }
Пример #7
0
        public void TestIdentityTransform()
        {
            // supply an empty transform document

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"" />
                ");

            var output = new XdtTransformer().Transform(input, transform);

            Assert.IsTrue(XDocument.DeepEquals(input, output));
        }
Пример #8
0
        public void TestInputDocumentsWithXmlNamespacesWorkAsExpected()
        {
            var input = XDocument.Parse(@"
                <configuration>
                  <appSettings>
                    <add key=""key1"" value=""value1"" />
                  </appSettings>
                  <blah xmlns=""http://test.com"">
                    <add key=""key2"" value=""value2"" />
                  </blah>
                  <flop xmlns=""http://test.com"">
                    <add key=""key3"" value=""value3"" xmlns="""" />
                  </flop>
                </configuration>
                ");

            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add value=""value1-new"" xdt:Transform=""SetAttributes"" />
                  </appSettings>
                  <blah xmlns=""http://test.com"">
                    <add key=""key2"" value=""value2-new"" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" />
                  </blah>
                  <flop xmlns=""http://test.com"">
                    <add key=""key3"" value=""value3-new"" xmlns="""" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" />
                  </flop>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            XNamespace ns = "http://test.com";

            var element2 = output
                .Element("configuration")
                .Elements(ns + "blah")
                .Elements(ns + "add")
                .Single(e => e.Attribute("key").Value == "key2");

            Assert.IsTrue(element2.Name.NamespaceName == ns);
            Assert.IsTrue(element2.Attribute("value").Value == "value2-new");

            var element3 = output
                .Element("configuration")
                .Elements(ns + "flop")
                .Elements("add")
                .Single(e => e.Attribute("key").Value == "key3");

            Assert.IsTrue(element3.Name.NamespaceName == String.Empty);
            Assert.IsTrue(element3.Attribute("value").Value == "value3-new");
        }
Пример #9
0
        public void TestRemoveTransform()
        {
            // remove the entire system.web element

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web xdt:Transform=""Remove"" />
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            Assert.IsFalse(output.Descendants("system.web").Any());
            Assert.IsTrue(output.Descendants("configuration").Any());
        }
Пример #10
0
        public void TestRemoveTransform()
        {
            // remove the entire system.web element

            var input     = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web xdt:Transform=""Remove"" />
                </configuration>
                ");
            var output    = new XdtTransformer().Transform(input, transform);

            Assert.IsFalse(output.Descendants("system.web").Any());
            Assert.IsTrue(output.Descendants("configuration").Any());
        }
Пример #11
0
        public void TestXPathLocator()
        {
            // all the mailsettings sections anywhere in the doc will be removed

            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                    <couldbe > 
                        <anything xdt:Transform=""RemoveAll"" xdt:Locator=""XPath(//mailsettings)"" /> 
                    </couldbe>
                </configuration>
                ");

            var output = new XdtTransformer().Transform(this.doc, transform);

            Assert.IsFalse(output.Descendants("mailsettings").Any());
        }
        public async Task TransformFileAsync_WithPUACharactersInPathTransformsFile_Success()
        {
            using var test = new XdtTransformerTest("<a xmlns:xdt=\"http://schemas.microsoft.com/XML-Document-Transform\"><x><y xdt:Transform=\"Insert\"><z>$c$</z></y></x></a>");

            var projectFileOriginalContent = "<a><x /></a>";

            File.WriteAllText(test.TargetFile.FullName, projectFileOriginalContent);

            string pathWithPAUChars = Path.Combine(test.TargetFile.DirectoryName, "U1[]U2[]U3[]");

            Directory.CreateDirectory(pathWithPAUChars);
            string newTargetFile = Path.Combine(pathWithPAUChars, "target.file");

            File.Move(test.TargetFile.FullName, newTargetFile);

            test.ProjectSystem.SetupGet(x => x.ProjectFullPath)
            .Returns(test.TargetFile.DirectoryName);
            test.ProjectSystem.SetupGet(x => x.ProjectName)
            .Returns("ProjectName");
            test.ProjectSystem.Setup(x => x.GetPropertyValue(It.IsNotNull <string>()))
            .Returns("d");
            test.ProjectSystem.Setup(x => x.AddFile(It.IsNotNull <string>(), It.IsNotNull <Stream>()))
            .Callback <string, Stream>(
                (targetFilePath, stream) =>
            {
                Assert.Equal(newTargetFile, targetFilePath);

                stream.Seek(offset: 0, origin: SeekOrigin.Begin);

                using (var reader = new StreamReader(stream))
                {
                    var actualResult   = reader.ReadToEnd();
                    var expectedResult = "<a>\r\n  <x>\r\n    <y>\r\n      <z>d</z>\r\n    </y>\r\n  </x>\r\n</a>";

                    Assert.Equal(expectedResult, actualResult);
                }
            });

            await XdtTransformer.PerformXdtTransformAsync(
                test.StreamTaskFactory,
                newTargetFile,
                test.ProjectSystem.Object,
                CancellationToken.None);
        }
Пример #13
0
        public void TestConditionLocator()
        {
            var transformDoc = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.net>
                    <mailSettings>
                      <smtp deliveryMethod=""Network"" from=""*****@*****.**""
                            xdt:Locator=""Condition(@deliveryMethod='SpecifiedPickupDirectory')""
                            xdt:Transform=""Replace"">
                        <network host=""liveMailServer""/>
                      </smtp>
                    </mailSettings>
                  </system.net>
                </configuration>
                ");

            var output = new XdtTransformer().Transform(this.inputDoc, transformDoc);
            Assert.IsTrue(XNode.DeepEquals(this.desiredDoc, output));
        }
Пример #14
0
        public void TestInsertTransform()
        {
            // insert an app setting

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add key=""key4"" value=""value4"" xdt:Transform=""Insert"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output.Root.Element("appSettings").Elements("add")
                .Where(e => e.Attribute("key").Value == "key4")
                .Single();

            Assert.IsTrue(element.Attribute("value").Value == "value4");
        }
Пример #15
0
        public void TestMultipleElementsAreTransformed()
        {
            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add value=""ha"" xdt:Transform=""SetAttributes"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var settings = output
                .Element("configuration")
                .Element("appSettings")
                .Elements("add");

            Assert.IsTrue(settings.Count() == 3);
            Assert.IsTrue(settings.SelectMany(e => e.Attributes("value")).All(a => a.Value == "ha"));
        }
Пример #16
0
        public void TestMatchLocator()
        {
            var transformDoc = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.net>
                    <mailSettings>
                      <smtp deliveryMethod=""Network"" from=""*****@*****.**""
                            xdt:Locator=""Match(from)""
                            xdt:Transform=""Replace"">
                        <network host=""liveMailServer""/>
                      </smtp>
                    </mailSettings>
                  </system.net>
                </configuration>
                ");

            var output = new XdtTransformer().Transform(this.inputDoc, transformDoc);

            Assert.IsTrue(XNode.DeepEquals(this.desiredDoc, output));
        }
Пример #17
0
        public void TestRemoveAttributesTransform()
        {
            // remove the "debug" attribute

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web>
                    <compilation xdt:Transform=""RemoveAttributes(debug)"" />
                  </system.web>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output
                .Element("configuration")
                .Element("system.web")
                .Element("compilation");

            Assert.IsTrue(element.Attribute("debug") == null);
        }
Пример #18
0
        public void TestInsertAfterTransform()
        {
            // insert an app setting just after the key3 setting

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add key=""key3.5"" value=""value3.5"" xdt:Transform=""InsertAfter(/configuration/appSettings/add[@key='key3'])"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output.Root.Element("appSettings").Elements("add")
                .Where(e => e.Attribute("key").Value == "key3.5")
                .Single();

            Assert.IsTrue(element.Attribute("value").Value == "value3.5");
            Assert.IsTrue(((XElement)element.PreviousNode).Attribute("key").Value == "key3");
        }
Пример #19
0
        public void TestSetAttributesTransformWithLocator()
        {
            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add key=""key2"" value=""value2-live"" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var settings = output
                .Element("configuration")
                .Element("appSettings")
                .Elements("add");
            var setting1 = settings.Single(e => e.Attribute("key").Value == "key1");
            var setting2 = settings.Single(e => e.Attribute("key").Value == "key2");

            Assert.IsTrue(setting1.Attribute("value").Value == "value1");
            Assert.IsTrue(setting2.Attribute("value").Value == "value2-live");
        }
Пример #20
0
        public void TestConditionLocator()
        {
            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add key=""key2"" value=""value2-live"" xdt:Locator=""Condition(@key='key2')"" xdt:Transform=""SetAttributes"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var settings = output
                .Element("configuration")
                .Element("appSettings")
                .Elements("add");
            var setting1 = settings.Single(e => e.Attribute("key").Value == "key1");
            var setting2 = settings.Single(e => e.Attribute("key").Value == "key2");

            Assert.IsTrue(setting1.Attribute("value").Value == "value1");
            Assert.IsTrue(setting2.Attribute("value").Value == "value2-live");
        }
Пример #21
0
        public void TestReplaceTransform()
        {
            // replace the entire system.web element

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web xdt:Transform=""Replace"">
                    <extra content=""here"" />
                  </system.web>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output
                .Element("configuration")
                .Element("system.web")
                .Element("extra");

            Assert.IsTrue(element.Attribute("content").Value == "here");

            // ensure there are no xdt attributes from the transform doc!
            Assert.IsFalse(output.Descendants().Attributes().Any(a => a.Name.NamespaceName == Namespaces.Xdt));
        }
Пример #22
0
        public void TestInsertTransform()
        {
            // insert an app setting

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add key=""key4"" value=""value4"" xdt:Transform=""Insert"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output.Root.Element("appSettings").Elements("add")
                .Where(e => e.Attribute("key").Value == "key4")
                .Single();

            Assert.IsTrue(element.Attribute("value").Value == "value4");
        }
Пример #23
0
 public XdtTransformerTests()
 {
     _transformer = new XdtTransformer();
 }
Пример #24
0
        public void TestInsertBeforeTransform()
        {
            // insert an app setting just before the key3 setting

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add key=""key2.5"" value=""value2.5"" xdt:Transform=""InsertBefore(/configuration/appSettings/add[@key='key3'])"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output.Root.Element("appSettings").Elements("add")
                .Where(e => e.Attribute("key").Value == "key2.5")
                .Single();

            Assert.IsTrue(element.Attribute("value").Value == "value2.5");
            Assert.IsTrue(((XElement) element.PreviousNode).Attribute("key").Value == "key2");
            Assert.IsTrue(((XElement) element.NextNode).Attribute("key").Value == "key3");
        }
Пример #25
0
        public void TestInputDocumentsWithXmlNamespacesWorkAsExpected()
        {
            var input = XDocument.Parse(@"
                <configuration>
                  <appSettings>
                    <add key=""key1"" value=""value1"" />
                  </appSettings>
                  <blah xmlns=""http://test.com"">
                    <add key=""key2"" value=""value2"" />
                  </blah>
                  <flop xmlns=""http://test.com"">
                    <add key=""key3"" value=""value3"" xmlns="""" />
                  </flop>
                </configuration>
                ");

            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add value=""value1-new"" xdt:Transform=""SetAttributes"" />
                  </appSettings>
                  <blah xmlns=""http://test.com"">
                    <add key=""key2"" value=""value2-new"" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" />
                  </blah>
                  <flop xmlns=""http://test.com"">
                    <add key=""key3"" value=""value3-new"" xmlns="""" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" />
                  </flop>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            XNamespace ns = "http://test.com";

            var element2 = output
                .Element("configuration")
                .Elements(ns + "blah")
                .Elements(ns + "add")
                .Single(e => e.Attribute("key").Value == "key2");

            Assert.IsTrue(element2.Name.NamespaceName == ns);
            Assert.IsTrue(element2.Attribute("value").Value == "value2-new");

            var element3 = output
                .Element("configuration")
                .Elements(ns + "flop")
                .Elements("add")
                .Single(e => e.Attribute("key").Value == "key3");

            Assert.IsTrue(element3.Name.NamespaceName == String.Empty);
            Assert.IsTrue(element3.Attribute("value").Value == "value3-new");
        }
Пример #26
0
        public void TestSetAttributesTransformWithArguments()
        {
            // change the "debug" attribute to false

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web>
                    <compilation debug=""false"" xdt:Transform=""SetAttributes(debug)"" />
                  </system.web>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output
                .Element("configuration")
                .Element("system.web")
                .Element("compilation");

            Assert.IsTrue(element.Attribute("debug").Value == "false");

            // ensure we haven't accidentally added attributes from the transform document
            Assert.IsFalse(output.Descendants().Any(e =>
                e.Attributes().Any(a =>
                    a.Name.NamespaceName == Namespaces.Xdt)));
        }
Пример #27
0
        public void TestReplaceTransform()
        {
            // replace the entire system.web element

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web xdt:Transform=""Replace"">
                    <extra content=""here"" />
                  </system.web>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output
                .Element("configuration")
                .Element("system.web")
                .Element("extra");

            Assert.IsTrue(element.Attribute("content").Value == "here");

            // ensure there are no xdt attributes from the transform doc!
            Assert.IsFalse(output.Descendants().Attributes().Any(a => a.Name.NamespaceName == Namespaces.Xdt));
        }
Пример #28
0
        public void TestRemoveAttributesTransform()
        {
            // remove the "debug" attribute

            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <system.web>
                    <compilation xdt:Transform=""RemoveAttributes(debug)"" />
                  </system.web>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var element = output
                .Element("configuration")
                .Element("system.web")
                .Element("compilation");

            Assert.IsTrue(element.Attribute("debug") == null);
        }
Пример #29
0
        public void TestMultipleElementsAreTransformed()
        {
            var input = GetInputDocument();
            var transform = XDocument.Parse(@"
                <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
                  <appSettings>
                    <add value=""ha"" xdt:Transform=""SetAttributes"" />
                  </appSettings>
                </configuration>
                ");
            var output = new XdtTransformer().Transform(input, transform);

            var settings = output
                .Element("configuration")
                .Element("appSettings")
                .Elements("add");

            Assert.IsTrue(settings.Count() == 3);
            Assert.IsTrue(settings.SelectMany(e => e.Attributes("value")).All(a => a.Value == "ha"));
        }