Preserve new lines and whitespace when adding secret (#19504)
This commit is contained in:
parent
0d9f5e053f
commit
e75ff49869
|
|
@ -76,7 +76,7 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
|
|||
var projectPath = ResolveProjectPath(ProjectPath, WorkingDirectory);
|
||||
|
||||
// Load the project file as XML
|
||||
var projectDocument = XDocument.Load(projectPath);
|
||||
var projectDocument = XDocument.Load(projectPath, LoadOptions.PreserveWhitespace);
|
||||
|
||||
// Accept the `--id` CLI option to the main app
|
||||
string newSecretsId = string.IsNullOrWhiteSpace(OverrideId)
|
||||
|
|
@ -120,19 +120,18 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
|
|||
}
|
||||
|
||||
// Add UserSecretsId element
|
||||
propertyGroup.Add(" ");
|
||||
propertyGroup.Add(new XElement("UserSecretsId", newSecretsId));
|
||||
propertyGroup.Add($"{Environment.NewLine} ");
|
||||
}
|
||||
|
||||
var settings = new XmlWriterSettings
|
||||
{
|
||||
Indent = true,
|
||||
OmitXmlDeclaration = true,
|
||||
};
|
||||
|
||||
using (var xw = XmlWriter.Create(projectPath, settings))
|
||||
{
|
||||
projectDocument.Save(xw);
|
||||
}
|
||||
using var xw = XmlWriter.Create(projectPath, settings);
|
||||
projectDocument.Save(xw);
|
||||
|
||||
context.Reporter.Output(Resources.FormatMessage_SetUserSecretsIdForProject(newSecretsId, projectPath));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.AspNetCore.Testing;
|
||||
|
|
@ -98,6 +99,22 @@ namespace Microsoft.Extensions.SecretManager.Tools.Tests
|
|||
Assert.Null(projectDocument.Declaration);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DoesNotRemoveBlankLines()
|
||||
{
|
||||
var projectDir = _fixture.CreateProject(null);
|
||||
var projectFile = Path.Combine(projectDir, "TestProject.csproj");
|
||||
var projectDocumentWithoutSecret = XDocument.Load(projectFile, LoadOptions.PreserveWhitespace);
|
||||
var lineCountWithoutSecret = projectDocumentWithoutSecret.ToString().Split(Environment.NewLine).Length;
|
||||
|
||||
new InitCommand(null, null).Execute(MakeCommandContext(), projectDir);
|
||||
|
||||
var projectDocumentWithSecret = XDocument.Load(projectFile, LoadOptions.PreserveWhitespace);
|
||||
var lineCountWithSecret = projectDocumentWithSecret.ToString().Split(Environment.NewLine).Length;
|
||||
|
||||
Assert.True(lineCountWithSecret == lineCountWithoutSecret + 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverridesIdForProjectWithSecretId()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue