示例#1
0
        public void SaveReadOnly3()
        {
            var doc = new XmlDocumentWithLocation(loadAsReadOnly: true);

            doc.Load(_pathToCommonTargets);
            doc.Save(new StringWriter());
        }
示例#2
0
        public void SaveReadOnly4()
        {
            var doc = new XmlDocumentWithLocation(loadAsReadOnly: true);

            doc.Load(_pathToCommonTargets);
            doc.Save(XmlWriter.Create(FileUtilities.GetTemporaryFile()));
        }
        public void ProjectLoadedStrippingCommentsAndWhiteSpaceIsReadOnly()
        {
            var projectContents =
                @"<Project ToolsVersion='msbuilddefaulttoolsversion' DefaultTargets='Build' xmlns='msbuildnamespace'>
                  <!--Initial Comment-->
                       
                  <!--Ending Comment-->
                </Project>
                ";

            using (var env = TestEnvironment.Create())
            {
                // set the hook for the desired read-only mode and reset the hook for the other modes
                XmlDocumentWithLocation.ClearReadOnlyFlags_UnitTestsOnly();
                env.SetEnvironmentVariable("MSBUILDLOADALLFILESASREADONLY", "1");
                env.SetEnvironmentVariable("MSBuildLoadMicrosoftTargetsReadOnly", null); // clear
                env.SetEnvironmentVariable("MSBUILDLOADALLFILESASWRITEABLE", null);      // clear

                var testFiles          = env.CreateTestProjectWithFiles(projectContents, Array.Empty <string>());
                ProjectRootElement xml = ProjectRootElement.Open(testFiles.ProjectFile);

                Assert.True(xml.XmlDocument.IsReadOnly);
                var children = xml.XmlDocument.ChildNodes;
                Assert.Equal(1, children.Count);
                Assert.Equal("Project", children[0].Name);
                Assert.Equal(2, children[0].ChildNodes.Count);
                Assert.Equal(string.Empty, children[0].ChildNodes[0].Value);
                Assert.Equal(string.Empty, children[0].ChildNodes[1].Value);
            }
        }
示例#4
0
        public void SaveReadOnly2()
        {
            var doc = new XmlDocumentWithLocation(loadAsReadOnly: true);

            doc.Load(_pathToCommonTargets);
            doc.Save(new MemoryStream());
        }
 public void SaveReadOnly4()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         var doc = new XmlDocumentWithLocation(loadAsReadOnly: true);
         doc.Load(_pathToCommonTargets);
         doc.Save(XmlWriter.Create(FileUtilities.GetTemporaryFile()));
     }
                                               );
 }
 public void SaveReadOnly3()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         var doc = new XmlDocumentWithLocation(loadAsReadOnly: true);
         doc.Load(_pathToCommonTargets);
         doc.Save(new StringWriter());
     }
                                               );
 }
示例#7
0
 /// <summary>
 /// Constructor allows you to set all the data members
 /// </summary>
 public ProjectDefinition(string filename, string initialTargets, string defaultTargets, string toolsVersion, bool createMSBuildProject)
 {
     this.initialTargets       = initialTargets;
     this.defaultTargets       = defaultTargets;
     this.toolsVersion         = toolsVersion;
     this.filename             = filename;
     this.createMSBuildProject = createMSBuildProject;
     this.projectXmlDocument   = new XmlDocumentWithLocation();
     this.targets            = new Dictionary <string, TargetDefinition>();
     this.projectRootElement = this.projectXmlDocument.CreateElement("Project", @"http://schemas.microsoft.com/developer/msbuild/2003");
     GenerateProjectRootElement();
 }
示例#8
0
        public void ContentIsSameAcrossInstances()
        {
            string content = ObjectModelHelpers.CleanupFileContents(@"
                    <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                        <ItemGroup>
                           Item group content
                        </ItemGroup>
                    </Project>
                    ");

            string path = FileUtilities.GetTemporaryFile();

            try
            {
                File.WriteAllText(path, content);

                ProjectStringCache      cache     = new ProjectStringCache();
                XmlDocumentWithLocation document1 = new XmlDocumentWithLocation();
                document1.StringCache = cache;
                document1.Load(path);

                XmlDocumentWithLocation document2 = new XmlDocumentWithLocation();
                document2.StringCache = cache;
                document2.Load(path);

                XmlNodeList nodes1 = document1.GetElementsByTagName("ItemGroup");
                XmlNodeList nodes2 = document2.GetElementsByTagName("ItemGroup");

                Assert.Equal(1, nodes1.Count);
                Assert.Equal(1, nodes2.Count);

                XmlNode node1 = nodes1[0].FirstChild;
                XmlNode node2 = nodes2[0].FirstChild;

                Assert.NotNull(node1);
                Assert.NotNull(node2);
                Assert.NotSame(node1, node2);
                Assert.Same(node1.Value, node2.Value);
            }
            finally
            {
                File.Delete(path);
            }
        }
示例#9
0
        /// <summary>
        /// Get location strings for the content, loading as readonly if specified
        /// </summary>
        private string GetLocations(string content, bool readOnly)
        {
            string file = null;

            try
            {
                file = FileUtilities.GetTemporaryFile();
                File.WriteAllText(file, content);
                var doc = new XmlDocumentWithLocation(loadAsReadOnly: readOnly);
                doc.Load(file);
                var allNodes = doc.SelectNodes("//*|//@*");

                string locations = String.Empty;
                foreach (var node in allNodes)
                {
                    foreach (var property in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                    {
                        if (property.Name.Equals("Location"))
                        {
                            var value = ((ElementLocation)property.GetValue(node, null));

                            if (value != null) // null means attribute is not present
                            {
                                locations += ((XmlNode)node).Name + "==" + ((XmlNode)node).Value ?? String.Empty + ":  " + value.LocationString + "\r\n";
                            }
                        }
                    }
                }

                locations = locations.Replace(file, "c:\\foo\\bar.csproj");

                return(locations);
            }
            finally
            {
                File.Delete(file);
            }
        }
示例#10
0
        public void ContentCanBeModified()
        {
            string content = ObjectModelHelpers.CleanupFileContents(@"
                    <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                        <ItemGroup attr1='attr1value'>
                           Item group content
                        </ItemGroup>
                    </Project>
                    ");

            string path = FileUtilities.GetTemporaryFile();

            try
            {
                File.WriteAllText(path, content);
                ProjectStringCache      cache     = new ProjectStringCache();
                XmlDocumentWithLocation document1 = new XmlDocumentWithLocation();
                document1.StringCache = cache;
                document1.Load(path);

                XmlDocumentWithLocation document2 = new XmlDocumentWithLocation();
                document2.StringCache = cache;
                document2.Load(path);

                string outerXml1 = document1.OuterXml;
                string outerXml2 = document2.OuterXml;
                Assert.Equal(outerXml1, outerXml2);

                XmlNodeList nodes1 = document1.GetElementsByTagName("ItemGroup");
                XmlNodeList nodes2 = document2.GetElementsByTagName("ItemGroup");

                Assert.Equal(1, nodes1.Count);
                Assert.Equal(1, nodes2.Count);

                XmlNode node1 = nodes1[0];
                XmlNode node2 = nodes2[0];
                Assert.NotNull(node1);
                Assert.NotNull(node2);
                Assert.NotSame(node1, node2);
                Assert.Single(node1.Attributes);
                Assert.Single(node2.Attributes);
                Assert.Same(node1.Attributes[0].Value, node2.Attributes[0].Value);

                node2.Attributes[0].Value = "attr1value";
                Assert.Equal(node1.Attributes[0].Value, node2.Attributes[0].Value);
                Assert.NotSame(node1.Attributes[0].Value, node2.Attributes[0].Value);

                node1 = nodes1[0].FirstChild;
                node2 = nodes2[0].FirstChild;
                Assert.NotSame(node1, node2);
                Assert.Same(node1.Value, node2.Value);

                XmlText newText = document2.CreateTextNode("New Value");
                XmlNode parent  = node2.ParentNode;
                parent.ReplaceChild(newText, node2);

                Assert.NotEqual(outerXml1, document2.OuterXml);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void ContentIsSameAcrossInstances()
        {
            string content = ObjectModelHelpers.CleanupFileContents(@"
                    <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                        <ItemGroup>
                           Item group content
                        </ItemGroup>
                    </Project>
                    ");

            string path = FileUtilities.GetTemporaryFile();

            try
            {
                File.WriteAllText(path, content);

                ProjectStringCache      cache     = new ProjectStringCache();
                XmlDocumentWithLocation document1 = new XmlDocumentWithLocation();
                document1.StringCache = cache;
#if FEATURE_XML_LOADPATH
                document1.Load(path);
#else
                var xmlReadSettings = new XmlReaderSettings {
                    DtdProcessing = DtdProcessing.Ignore
                };
                using (XmlReader xmlReader = XmlReader.Create(path, xmlReadSettings))
                {
                    document1.Load(xmlReader);
                }
#endif

                XmlDocumentWithLocation document2 = new XmlDocumentWithLocation();
                document2.StringCache = cache;
#if FEATURE_XML_LOADPATH
                document2.Load(path);
#else
                using (XmlReader xmlReader = XmlReader.Create(path, xmlReadSettings))
                {
                    document2.Load(xmlReader);
                }
#endif

                XmlNodeList nodes1 = document1.GetElementsByTagName("ItemGroup");
                XmlNodeList nodes2 = document2.GetElementsByTagName("ItemGroup");

                Assert.Equal(1, nodes1.Count);
                Assert.Equal(1, nodes2.Count);

                XmlNode node1 = nodes1[0].FirstChild;
                XmlNode node2 = nodes2[0].FirstChild;

                Assert.NotNull(node1);
                Assert.NotNull(node2);
                Assert.NotSame(node1, node2);
                Assert.Same(node1.Value, node2.Value);
            }
            finally
            {
                File.Delete(path);
            }
        }