Add functional test to verify generated code is up to date (#1369).

This commit is contained in:
Cesar Blum Silveira 2017-02-06 16:34:28 -08:00
parent c6705d8693
commit a95743c5f6
3 changed files with 74 additions and 27 deletions

View File

@ -0,0 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if NETCOREAPP1_1
using System.IO;
using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{
public class GeneratedCodeTests
{
[Fact]
public void GeneratedCodeIsUpToDate()
{
const string frameHeadersGeneratedPath = "../../../../../src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/FrameHeaders.Generated.cs";
const string frameGeneratedPath = "../../../../../src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.Generated.cs";
var testFrameHeadersGeneratedPath = Path.GetTempFileName();
var testFrameGeneratedPath = Path.GetTempFileName();
try
{
var currentFrameHeadersGenerated = File.ReadAllText(frameHeadersGeneratedPath);
var currentFrameGenerated = File.ReadAllText(frameGeneratedPath);
CodeGenerator.Program.Run(testFrameHeadersGeneratedPath, testFrameGeneratedPath);
var testFrameHeadersGenerated = File.ReadAllText(testFrameHeadersGeneratedPath);
var testFrameGenerated = File.ReadAllText(testFrameGeneratedPath);
Assert.Equal(currentFrameHeadersGenerated, testFrameHeadersGenerated, ignoreLineEndingDifferences: true);
Assert.Equal(currentFrameGenerated, testFrameGenerated, ignoreLineEndingDifferences: true);
}
finally
{
File.Delete(testFrameHeadersGeneratedPath);
File.Delete(testFrameGeneratedPath);
}
}
}
}
#endif

View File

@ -35,6 +35,7 @@
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<ProjectReference Include="..\..\tools\CodeGenerator\CodeGenerator.csproj" />
<PackageReference Include="System.Net.NetworkInformation" Version="$(CoreFxVersion)" />
</ItemGroup>

View File

@ -10,36 +10,38 @@ namespace CodeGenerator
{
public static int Main(string[] args)
{
var text0 = KnownHeaders.GeneratedFile();
var text1 = FrameFeatureCollection.GeneratedFile();
if (args.Length < 1)
{
Console.Error.WriteLine("Missing path to FrameHeaders.Generated.cs");
return 1;
}
else if (args.Length < 2)
{
Console.Error.WriteLine("Missing path to Frame.Generated.cs");
return 1;
}
if (args.Length == 1)
{
var existing = File.Exists(args[0]) ? File.ReadAllText(args[0]) : "";
if (!string.Equals(text0, existing))
{
File.WriteAllText(args[0], text0);
}
}
else if (args.Length == 2)
{
var existing0 = File.Exists(args[0]) ? File.ReadAllText(args[0]) : "";
if (!string.Equals(text0, existing0))
{
File.WriteAllText(args[0], text0);
}
Run(args[0], args[1]);
var existing1 = File.Exists(args[1]) ? File.ReadAllText(args[1]) : "";
if (!string.Equals(text1, existing1))
{
File.WriteAllText(args[1], text1);
}
}
else
{
Console.WriteLine(text0);
}
return 0;
}
public static void Run(string knownHeadersPath, string frameFeaturesCollectionPath)
{
var knownHeadersContent = KnownHeaders.GeneratedFile();
var frameFeatureCollectionContent = FrameFeatureCollection.GeneratedFile();
var existingKnownHeaders = File.Exists(knownHeadersPath) ? File.ReadAllText(knownHeadersPath) : "";
if (!string.Equals(knownHeadersContent, existingKnownHeaders))
{
File.WriteAllText(knownHeadersPath, knownHeadersContent);
}
var existingFrameFeatureCollection = File.Exists(frameFeaturesCollectionPath) ? File.ReadAllText(frameFeaturesCollectionPath) : "";
if (!string.Equals(frameFeatureCollectionContent, existingFrameFeatureCollection))
{
File.WriteAllText(frameFeaturesCollectionPath, frameFeatureCollectionContent);
}
}
}
}