Skip to content

Commit

Permalink
feat: Add basic tests for msp files
Browse files Browse the repository at this point in the history
  • Loading branch information
learn-more committed Oct 27, 2021
1 parent 4b3b07c commit 5c4b34f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Lessmsi.Tests/LessMsi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Reference Include="System" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
<Reference Include="WindowsBase" />
<Reference Include="wix, Version=2.0.2110.0, Culture=neutral">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\wix.dll</HintPath>
Expand Down Expand Up @@ -83,6 +84,7 @@
<Compile Include="FileEntryGraph.cs" />
<Compile Include="MiscTests.cs" />
<Compile Include="MiscTestsNUnit.cs" />
<Compile Include="MspTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestBase.cs" />
</ItemGroup>
Expand Down
30 changes: 30 additions & 0 deletions src/Lessmsi.Tests/MspTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Xunit;

namespace LessMsi.Tests
{
public class MspTests: TestBase
{
[Fact]
public void MsXml5()
{
ExpectTables("msxml5.msp", new[] { "MsiPatchMetadata", "MsiPatchSequence" });
// Cannot test properties yet, since they are internal in LessMsi.Gui!
ExpectStreamCabFiles("msxml5.msp", true);
}

[Fact]
public void WPF2_32()
{
ExpectTables("WPF2_32.msp", new[] { "MsiPatchMetadata", "MsiPatchSequence" });
ExpectStreamCabFiles("WPF2_32.msp", true);
}

[Fact]
public void SQL2008_AS()
{
ExpectTables("SQL2008_AS.msp", new[] { "MsiPatchSequence" });
ExpectStreamCabFiles("SQL2008_AS.msp", true);
}

}
}
48 changes: 44 additions & 4 deletions src/Lessmsi.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using System.Threading;
using Xunit;
using LessIO;

using System.Linq;

namespace LessMsi.Tests
{
public class TestBase
Expand Down Expand Up @@ -242,6 +243,45 @@ protected Path AppPath
var local = new Path(codeBase.LocalPath);
return local.Parent;
}
}
}
}
}

[DebuggerHidden]
protected void ExpectTables(string sourceFileName, string[] expectedTableNames)
{
using (var msidb = Msi.MsiDatabase.Create(GetMsiTestFile(sourceFileName)))
{
Assert.NotNull(msidb);
var query = "SELECT * FROM `_Tables`";
using (var msiTable = new Msi.ViewWrapper(msidb.OpenExecuteView(query)))
{
Assert.NotNull(msiTable);

var tableNames = from record in msiTable.Records
select record[0] as string;
// Since we don't care about the order, we sort the lists
Assert.Equal(expectedTableNames.OrderBy(s => s), tableNames.OrderBy(s => s));
}
}
}

[DebuggerHidden]
protected void ExpectStreamCabFiles(string sourceFileName, bool hasCab)
{
using (var stg = new OleStorage.OleStorageFile(GetMsiTestFile(sourceFileName)))
{
var strm = stg.GetStreams().Where(elem => OleStorage.OleStorageFile.IsCabStream(elem));
if (strm != null)
{
// Rest of the CAB parsing logic is in the UI, can't extract filenames without duplicating code that we want to test..
Assert.True(hasCab);
}
else
{
// Not expecting to find a cab here
Assert.False(hasCab);
}
}
}

}
}
Binary file not shown.
Binary file added src/Lessmsi.Tests/TestFiles/MsiInput/WPF2_32.msp
Binary file not shown.
Binary file added src/Lessmsi.Tests/TestFiles/MsiInput/msxml5.msp
Binary file not shown.

0 comments on commit 5c4b34f

Please sign in to comment.