Merge pull request #8199 from peterhuene/sln-write-bom

Write UTF-8 BOM for solution files.
This commit is contained in:
Livar 2017-12-11 20:21:41 -08:00 committed by GitHub
commit d38c6000c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View file

@ -33,6 +33,7 @@ using System.IO;
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Text;
using Microsoft.DotNet.Cli.Sln.Internal.FileManipulation;
using Microsoft.DotNet.Tools.Common;
@ -211,7 +212,7 @@ namespace Microsoft.DotNet.Cli.Sln.Internal
}
var sw = new StringWriter();
Write(sw);
File.WriteAllText(FullPath, sw.ToString());
File.WriteAllText(FullPath, sw.ToString(), Encoding.UTF8);
}
private void Write(TextWriter writer)

View file

@ -9,6 +9,7 @@ using Microsoft.DotNet.Tools.Test.Utilities;
using System;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Abstractions;
@ -554,6 +555,33 @@ EndGlobal
cmd.StdErr.Should().BeEmpty();
}
[Fact]
public void WhenProjectIsAddedSolutionHasUTF8BOM()
{
var projectDirectory = TestAssets
.Get("TestAppWithEmptySln")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var projectToAdd = "Lib/Lib.csproj";
var projectPath = Path.Combine("Lib", "Lib.csproj");
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln App.sln add {projectToAdd}");
cmd.Should().Pass();
var preamble = Encoding.UTF8.GetPreamble();
preamble.Length.Should().Be(3);
using (var stream = new FileStream(Path.Combine(projectDirectory, "App.sln"), FileMode.Open))
{
var bytes = new byte[preamble.Length];
stream.Read(bytes, 0, bytes.Length);
bytes.Should().BeEquivalentTo(preamble);
}
}
[Theory]
[InlineData("TestAppWithSlnAndCsprojFiles")]
[InlineData("TestAppWithSlnAndCsprojProjectGuidFiles")]

View file

@ -8,6 +8,7 @@ using Microsoft.DotNet.Tools.Test.Utilities;
using System;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
namespace Microsoft.DotNet.Cli.Sln.Remove.Tests
@ -590,6 +591,32 @@ EndGlobal
.Count().Should().Be(1, $"App {reasonString}");
}
[Fact]
public void WhenProjectIsRemovedSolutionHasUTF8BOM()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojToRemove")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var projectToRemove = Path.Combine("Lib", "Lib.csproj");
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln App.sln remove {projectToRemove}");
cmd.Should().Pass();
var preamble = Encoding.UTF8.GetPreamble();
preamble.Length.Should().Be(3);
using (var stream = new FileStream(Path.Combine(projectDirectory, "App.sln"), FileMode.Open))
{
var bytes = new byte[preamble.Length];
stream.Read(bytes, 0, bytes.Length);
bytes.Should().BeEquivalentTo(preamble);
}
}
[Fact]
public void WhenFinalReferenceIsRemovedEmptySectionsAreRemoved()
{