Preserve new lines and whitespace when adding secret (#19504)

This commit is contained in:
Scott Addie 2020-04-03 12:37:46 -05:00 committed by GitHub
parent 0d9f5e053f
commit e75ff49869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -76,7 +76,7 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
var projectPath = ResolveProjectPath(ProjectPath, WorkingDirectory); var projectPath = ResolveProjectPath(ProjectPath, WorkingDirectory);
// Load the project file as XML // 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 // Accept the `--id` CLI option to the main app
string newSecretsId = string.IsNullOrWhiteSpace(OverrideId) string newSecretsId = string.IsNullOrWhiteSpace(OverrideId)
@ -120,19 +120,18 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
} }
// Add UserSecretsId element // Add UserSecretsId element
propertyGroup.Add(" ");
propertyGroup.Add(new XElement("UserSecretsId", newSecretsId)); propertyGroup.Add(new XElement("UserSecretsId", newSecretsId));
propertyGroup.Add($"{Environment.NewLine} ");
} }
var settings = new XmlWriterSettings var settings = new XmlWriterSettings
{ {
Indent = true,
OmitXmlDeclaration = true, OmitXmlDeclaration = true,
}; };
using (var xw = XmlWriter.Create(projectPath, settings)) using var xw = XmlWriter.Create(projectPath, settings);
{ projectDocument.Save(xw);
projectDocument.Save(xw);
}
context.Reporter.Output(Resources.FormatMessage_SetUserSecretsIdForProject(newSecretsId, projectPath)); context.Reporter.Output(Resources.FormatMessage_SetUserSecretsIdForProject(newSecretsId, projectPath));
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Xml.Linq; using System.Xml.Linq;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
@ -98,6 +99,22 @@ namespace Microsoft.Extensions.SecretManager.Tools.Tests
Assert.Null(projectDocument.Declaration); 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] [Fact]
public void OverridesIdForProjectWithSecretId() public void OverridesIdForProjectWithSecretId()
{ {