add patch tests and run tests on build (so fail build if tests fail)

This commit is contained in:
Mister_Nebula 2021-08-08 19:48:26 +01:00
parent 85eb03f3c8
commit c44ea2cc54
2 changed files with 45 additions and 0 deletions

41
QSBTests/PatchTests.cs Normal file
View File

@ -0,0 +1,41 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QSB.Patches;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace QSBTests
{
[TestClass]
public class PatchTests
{
[TestMethod]
public void CheckUnreferencedPatches()
{
var qsbAssembly = Assembly.Load("QSB");
var allPatchTypes = qsbAssembly
.GetTypes()
.Where(x => typeof(QSBPatch).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
QSBPatchManager.Init();
var patchInstances = (List<QSBPatch>)typeof(QSBPatchManager)
.GetField("_patchList", BindingFlags.NonPublic | BindingFlags.Static)
.GetValue(typeof(QSBPatchManager));
var failedTypes = new List<Type>();
foreach (var type in allPatchTypes)
{
if (!patchInstances.Any(x => x.GetType() == type))
{
failedTypes.Add(type);
}
}
if (failedTypes.Count > 0)
{
Assert.Fail(string.Join(", ", failedTypes.Select(x => x.Name)));
}
}
}
}

View File

@ -50,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EventTests.cs" />
<Compile Include="PatchTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UtilityTests.cs" />
</ItemGroup>
@ -72,4 +73,7 @@
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
<PropertyGroup>
<PostBuildEvent>dotnet test "$(SolutionDir)\QSBTests\$(OutDir)\QSBTests.dll"</PostBuildEvent>
</PropertyGroup>
</Project>