Skip to content

Commit

Permalink
Merge pull request #465 from smadala/to-utf-8
Browse files Browse the repository at this point in the history
Change encoding of template files from us-ascii to utf-8
  • Loading branch information
srivatsn authored Dec 28, 2016
2 parents 772931e + 9090a5b commit 065290e
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ if (!(Test-Path $nuget))
& $nuget restore $RepoRoot\sdk-templates.sln
if($LASTEXITCODE -ne 0) { throw "Failed to restore nuget packages for templates" }

msbuild $commonBuildArgs /nr:false /p:BuildTemplates=true /flp1:Summary`;Verbosity=diagnostic`;Encoding=UTF-8`;LogFile=$msbuildSummaryLog /flp2:WarningsOnly`;Verbosity=diagnostic`;Encoding=UTF-8`;LogFile=$msbuildWarningLog /flp3:ErrorsOnly`;Verbosity=diagnostic`;Encoding=UTF-8`;LogFile=$msbuildFailureLog
dotnet msbuild $commonBuildArgs /p:BuildTemplates=true /flp1:Summary`;Verbosity=diagnostic`;Encoding=UTF-8`;LogFile=$msbuildSummaryLog /flp2:WarningsOnly`;Verbosity=diagnostic`;Encoding=UTF-8`;LogFile=$msbuildWarningLog /flp3:ErrorsOnly`;Verbosity=diagnostic`;Encoding=UTF-8`;LogFile=$msbuildFailureLog
if($LASTEXITCODE -ne 0) { throw "Failed to build templates" }
18 changes: 14 additions & 4 deletions build/build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ProjectToDotnetRestore Include="$(RepositoryRootDirectory)test\Microsoft.NET.Build.Tests\Microsoft.NET.Build.Tests.csproj" />
<ProjectToDotnetRestore Include="$(RepositoryRootDirectory)test\Microsoft.NET.Pack.Tests\Microsoft.NET.Pack.Tests.csproj" />
<ProjectToDotnetRestore Include="$(RepositoryRootDirectory)test\Microsoft.NET.Publish.Tests\Microsoft.NET.Publish.Tests.csproj" />
<TemplatesProjectToDotnetRestore Include="$(RepositoryRootDirectory)test\Templates.UnitTests\Templates.UnitTests.csproj" />
</ItemGroup>
<Target Name="RestorePackages">

Expand All @@ -55,6 +56,10 @@
Condition="'$(BuildTemplates)' != 'true'"
WorkingDirectory="$(RepositoryRootDirectory)"
/>
<Exec Command="$(__SetNuget_Packages)$(__NewLine)$(DotNetTool) restore %(TemplatesProjectToDotnetRestore.Identity) /v:minimal /p:SkipInvalidConfigurations=true /p:_InvalidConfigurationWarning=false"
Condition="'$(BuildTemplates)' == 'true'"
WorkingDirectory="$(RepositoryRootDirectory)"
/>
</Target>

<Target Name="BuildSolution">
Expand Down Expand Up @@ -153,14 +158,18 @@

</Target>

<Target Name="Test" Condition="'$(BuildTemplates)' != 'true'">
<Target Name="Test">

<ItemGroup>
<TestAssembly Include="$(TestsDirectory)*Tests.dll" />
<ItemGroup Condition="'$(BuildTemplates)' != 'true'">
<TestAssembly Include="$(TestsDirectory)*Tests.dll" Exclude="$(TestsDirectory)Templates.UnitTests.dll" />
<XmlTestFile Include="$(TestsDirectory)TestResults.xml" />
</ItemGroup>
<ItemGroup Condition="'$(BuildTemplates)' == 'true'">
<TestAssembly Include="$(TestsDirectory)Templates.UnitTests.dll" />
<XmlTestFile Include="$(TestsDirectory)TemplatesTestResults.xml" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(BuildTemplates)' != 'true'">
<NetCoreAppProductAssets Include="$(PackagesLayoutToolsNetCoreAppDir)\*" />
</ItemGroup>

Expand All @@ -169,6 +178,7 @@
<!-- Copy all the 'netcore1.0' product assemblies to the test directory, so the tests can load them. -->
<Copy SourceFiles="@(NetCoreAppProductAssets)"
DestinationFolder="$(TestsDirectory)"
Condition="'$(BuildTemplates)' != 'true'"
/>

<Exec Command="$(DotNetTool) &quot;$(TestsDirectory)\xunit.console.netcore.exe&quot; &quot;@(TestAssembly, '&quot; &quot;')&quot; -xml &quot;@(XmlTestFile)&quot;"
Expand Down
9 changes: 9 additions & 0 deletions sdk-templates.sln
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualBasicClassLibrary", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpXUnitTest", "src\Templates\ProjectTemplates\CSharp\.NETCore\CSharpXUnitTest\CSharpXUnitTest.csproj", "{BE787238-0F17-4F20-83BE-0020EB434921}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{20F31AE0-2647-4854-AB30-76E858EB9A58}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Templates.UnitTests", "test\Templates.UnitTests\Templates.UnitTests.csproj", "{7A328A78-533E-43F8-9AF8-9332A3331418}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".NETStandard", ".NETStandard", "{E43E8384-B8A8-44BB-A944-AD5E6F648D98}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".NETStandard", ".NETStandard", "{771347DC-4DA0-401B-B608-534212CC5E9E}"
Expand Down Expand Up @@ -170,6 +174,10 @@ Global
{BE787238-0F17-4F20-83BE-0020EB434921}.Debug|x86.Build.0 = Debug|Any CPU
{BE787238-0F17-4F20-83BE-0020EB434921}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE787238-0F17-4F20-83BE-0020EB434921}.Release|Any CPU.Build.0 = Release|Any CPU
{7A328A78-533E-43F8-9AF8-9332A3331418}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A328A78-533E-43F8-9AF8-9332A3331418}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A328A78-533E-43F8-9AF8-9332A3331418}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A328A78-533E-43F8-9AF8-9332A3331418}.Release|Any CPU.Build.0 = Release|Any CPU
{BE787238-0F17-4F20-83BE-0020EB434921}.Release|x64.ActiveCfg = Release|Any CPU
{BE787238-0F17-4F20-83BE-0020EB434921}.Release|x64.Build.0 = Release|Any CPU
{BE787238-0F17-4F20-83BE-0020EB434921}.Release|x86.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -242,6 +250,7 @@ Global
{2A82D3FA-4EBE-425C-A3C7-3DC207591E53} = {C86A71A6-9D35-436B-BBC5-0940041410F1}
{DCDD8266-D269-46BD-B563-E4D4B811A5B1} = {E281FDCB-10B7-403C-84D4-EE4FF92845B9}
{BE787238-0F17-4F20-83BE-0020EB434921} = {57C097FA-72EB-4AB9-8E72-58ACDEB0F4A8}
{7A328A78-533E-43F8-9AF8-9332A3331418} = {20F31AE0-2647-4854-AB30-76E858EB9A58}
{E43E8384-B8A8-44BB-A944-AD5E6F648D98} = {82D4E69F-8B61-4CC3-8AF1-11D02AAE99CF}
{771347DC-4DA0-401B-B608-534212CC5E9E} = {98132BF6-2450-4753-B4E9-878F468E60BB}
{440F1724-1830-4C7A-9500-8D9323A7FE71} = {E43E8384-B8A8-44BB-A944-AD5E6F648D98}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name Package="{860A27C0-B665-47F3-BC12-637E16A1050A}" ID="9"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name Package="{860A27C0-B665-47F3-BC12-637E16A1050A}" ID="3"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name>Unit Test Project (.NET Core)</Name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace $safeprojectname$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Xunit;

namespace $safeprojectname$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name>xUnit Test Project (.NET Core)</Name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name Package="{860A27C0-B665-47F3-BC12-637E16A1050A}" ID="5"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name Package="{860A27C0-B665-47F3-BC12-637E16A1050A}" ID="9"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name Package="{860A27C0-B665-47F3-BC12-637E16A1050A}" ID="3"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name Package="{860A27C0-B665-47F3-BC12-637E16A1050A}" ID="5"/>
Expand Down
70 changes: 70 additions & 0 deletions test/Templates.UnitTests/EncodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Xunit;
using System;
using System.Collections.Generic;
using System.Text;

namespace Templates.UnitTests
{
public class EncodeTests
{
private static readonly HashSet<string> IgnoreFileExtensionSet = new HashSet<string> {".json", ".props", ".target", ".png"};

[Fact]
public void AllFilesInTemplatesShouldBeUTF8Encoded()
{
var dirPath = Path.Combine(AppContext.BaseDirectory, @"..\..\..\src\Templates");
this.CheckAllFilesUTF8Encoded(dirPath);
}

private void CheckAllFilesUTF8Encoded(string dirPath)
{
foreach (var filePath in Directory.GetFiles(dirPath))
{
var fileExtension = Path.GetExtension(filePath);
if (EncodeTests.IgnoreFileExtensionSet.Contains(fileExtension))
{
continue;
}
Assert.True(this.IsUTF8EncodedWithBOM(filePath), $"{filePath} should be UTF-8 encoded with BOM");
}

foreach (var childDirPath in Directory.GetDirectories(dirPath))
{
this.CheckAllFilesUTF8Encoded(childDirPath);
}
}

/// <summary>
/// Check file is UTF-8 encoded with BOM.
/// </summary>
/// <param name="filePath"></param>
/// <returns>true; If file encoding is UTF-8 with BOM</returns>
private bool IsUTF8EncodedWithBOM(string filePath)
{
var strictUTF8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true, throwOnInvalidBytes: true);
using (var reader = new StreamReader(File.OpenRead(filePath), strictUTF8, detectEncodingFromByteOrderMarks: false))
{
try
{
reader.ReadToEnd();
}
catch (DecoderFallbackException)
{
return false;
}
}

var bom = new byte[3];
using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
file.Read(bom, 0, 3);
}

return bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF;
}
}
}
10 changes: 10 additions & 0 deletions test/Templates.UnitTests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Templates.UnitTests": {
"commandName": "Executable",
"executablePath": "$(RunCommand)",
"commandLineArgs": "\"$(OutDir)xunit.console.netcore.exe\" \"$(OutDir)$(AssemblyName).dll\" -wait",
"workingDirectory": "$(OutDir)"
}
}
}
47 changes: 47 additions & 0 deletions test/Templates.UnitTests/Templates.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'Common.props'))\Common.props" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
<ProjectGuid>{7A328A78-533E-43F8-9AF8-9332A3331418}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetFramework>netstandard1.6</TargetFramework>
<PackageTargetFallback>dotnet5.4;portable-net451+win8</PackageTargetFallback>
<OutDir>$(OutDir)Tests\</OutDir>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<NonShipping>true</NonShipping>
<StartAction>Program</StartAction>
<StartProgram>$(DotNetTool).exe</StartProgram>
<StartArguments>"$(OutDir)xunit.console.netcore.exe" "$(OutDir)$(AssemblyName).dll" -xml "$(OutDir)TemplatesTestResults.xml" -wait</StartArguments>
<StartWorkingDirectory>$(OutDir)</StartWorkingDirectory>
<DebugEngines>{2E36F1D4-B23C-435D-AB41-18E608940038}</DebugEngines>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>$(SdkStage0Version)</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.DotNet.Cli.Utils">
<Version>$(DotNetCliUtilsVersion)</Version>
</PackageReference>
<PackageReference Include="FluentAssertions">
<Version>$(FluentAssertionsVersion)</Version>
</PackageReference>
<PackageReference Include="System.Diagnostics.FileVersionInfo">
<Version>4.0.0</Version>
</PackageReference>
<PackageReference Include="xunit">
<Version>$(xunitVersion)</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" Exclude="$(GlobalExclude)" />
<EmbeddedResource Include="**\*.resx" Exclude="$(GlobalExclude)" />
</ItemGroup>
<Import Project="..\..\build\Targets\Templates.Imports.targets" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 065290e

Please sign in to comment.