Explorar el Código

Secret Manager: Save project file without XML declaration (#14354)

Scott Addie hace 6 años
padre
commit
e937884eb4

+ 11 - 1
src/Tools/dotnet-user-secrets/src/Internal/InitCommand.cs

@@ -4,6 +4,7 @@
 using System;
 using System.IO;
 using System.Linq;
+using System.Xml;
 using System.Xml.Linq;
 using System.Xml.XPath;
 using Microsoft.Extensions.CommandLineUtils;
@@ -122,7 +123,16 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
                 propertyGroup.Add(new XElement("UserSecretsId", newSecretsId));
             }
 
-            projectDocument.Save(projectPath);
+            var settings = new XmlWriterSettings
+            {
+                Indent = true,
+                OmitXmlDeclaration = true,
+            };
+
+            using (var xw = XmlWriter.Create(projectPath, settings))
+            {
+                projectDocument.Save(xw);
+            }
 
             context.Reporter.Output(Resources.FormatMessage_SetUserSecretsIdForProject(newSecretsId, projectPath));
         }

+ 13 - 0
src/Tools/dotnet-user-secrets/test/InitCommandTest.cs

@@ -4,6 +4,7 @@
 using System;
 using System.IO;
 using System.Text;
+using System.Xml.Linq;
 using Microsoft.AspNetCore.Testing;
 using Microsoft.Extensions.Configuration.UserSecrets.Tests;
 using Microsoft.Extensions.SecretManager.Tools.Internal;
@@ -90,6 +91,18 @@ namespace Microsoft.Extensions.SecretManager.Tools.Tests
             Assert.Equal(SecretId, idResolver.Resolve(null, null));
         }
 
+        [Fact]
+        public void DoesNotAddXmlDeclarationToProject()
+        {
+            var projectDir = _fixture.CreateProject(null);
+            var projectFile = Path.Combine(projectDir, "TestProject.csproj");
+
+            new InitCommand(null, null).Execute(MakeCommandContext(), projectDir);
+
+            var projectDocument = XDocument.Load(projectFile);
+            Assert.Null(projectDocument.Declaration);
+        }
+
         [Fact]
         public void OverridesIdForProjectWithSecretId()
         {