Skip to content

Commit e937884

Browse files
authored
Secret Manager: Save project file without XML declaration (#14354)
1 parent 853d8f0 commit e937884

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Tools/dotnet-user-secrets/src/Internal/InitCommand.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Linq;
7+
using System.Xml;
78
using System.Xml.Linq;
89
using System.Xml.XPath;
910
using Microsoft.Extensions.CommandLineUtils;
@@ -122,7 +123,16 @@ public void Execute(CommandContext context)
122123
propertyGroup.Add(new XElement("UserSecretsId", newSecretsId));
123124
}
124125

125-
projectDocument.Save(projectPath);
126+
var settings = new XmlWriterSettings
127+
{
128+
Indent = true,
129+
OmitXmlDeclaration = true,
130+
};
131+
132+
using (var xw = XmlWriter.Create(projectPath, settings))
133+
{
134+
projectDocument.Save(xw);
135+
}
126136

127137
context.Reporter.Output(Resources.FormatMessage_SetUserSecretsIdForProject(newSecretsId, projectPath));
128138
}

src/Tools/dotnet-user-secrets/test/InitCommandTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Text;
7+
using System.Xml.Linq;
78
using Microsoft.AspNetCore.Testing;
89
using Microsoft.Extensions.Configuration.UserSecrets.Tests;
910
using Microsoft.Extensions.SecretManager.Tools.Internal;
@@ -90,6 +91,18 @@ public void DoesNotGenerateIdForProjectWithSecretId()
9091
Assert.Equal(SecretId, idResolver.Resolve(null, null));
9192
}
9293

94+
[Fact]
95+
public void DoesNotAddXmlDeclarationToProject()
96+
{
97+
var projectDir = _fixture.CreateProject(null);
98+
var projectFile = Path.Combine(projectDir, "TestProject.csproj");
99+
100+
new InitCommand(null, null).Execute(MakeCommandContext(), projectDir);
101+
102+
var projectDocument = XDocument.Load(projectFile);
103+
Assert.Null(projectDocument.Declaration);
104+
}
105+
93106
[Fact]
94107
public void OverridesIdForProjectWithSecretId()
95108
{

0 commit comments

Comments
 (0)