Skip to content

Commit

Permalink
SDK: Move SDK helpers into TestFramework
Browse files Browse the repository at this point in the history
  • Loading branch information
seclerp authored and ForNeVeR committed Apr 21, 2024
1 parent ce1f14a commit 852a05b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cesium.Sdk.Tests/Cesium.Sdk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Cesium.Solution.Metadata\Cesium.Solution.Metadata.csproj" />
<ProjectReference Include="..\Cesium.TestFramework\Cesium.TestFramework.csproj" />
</ItemGroup>

</Project>
34 changes: 17 additions & 17 deletions Cesium.Sdk.Tests/CesiumCompileTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Runtime.InteropServices;
using Cesium.Sdk.Tests.Framework;
using Cesium.TestFramework;
using Xunit.Abstractions;

namespace Cesium.Sdk.Tests;
Expand All @@ -8,7 +8,7 @@ public class CesiumCompileTests(ITestOutputHelper testOutputHelper) : SdkTestBas
{
[Theory]
[InlineData("SimpleCoreExe")]
public void CesiumCompile_Core_Exe_ShouldSucceed(string projectName)
public async Task CesiumCompile_Core_Exe_ShouldSucceed(string projectName)
{
HashSet<string> expectedObjArtifacts =
[
Expand All @@ -25,16 +25,16 @@ public void CesiumCompile_Core_Exe_ShouldSucceed(string projectName)
$"{projectName}.deps.json",
];

var result = ExecuteTargets(projectName, "Restore", "Build");
var result = await ExecuteTargets(projectName, "Restore", "Build");

Assert.True(result.ExitCode == 0);
AssertEx.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertEx.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
}

[Theory]
[InlineData("SimpleNetfxExe")]
public void CesiumCompile_NetFx_Exe_ShouldSucceed(string projectName)
public async Task CesiumCompile_NetFx_Exe_ShouldSucceed(string projectName)
{
HashSet<string> expectedObjArtifacts =
[
Expand All @@ -48,17 +48,17 @@ public void CesiumCompile_NetFx_Exe_ShouldSucceed(string projectName)
$"{projectName}.runtimeconfig.json"
];

var result = ExecuteTargets(projectName, "Restore", "Build");
var result = await ExecuteTargets(projectName, "Restore", "Build");

Assert.True(result.ExitCode == 0);
AssertEx.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertEx.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
}

[Theory]
[InlineData("SimpleCoreLibrary")]
[InlineData("SimpleNetStandardLibrary")]
public void CesiumCompile_Core_Library_ShouldSucceed(string projectName)
public async Task CesiumCompile_Core_Library_ShouldSucceed(string projectName)
{
string[] expectedObjArtifacts =
[
Expand All @@ -71,16 +71,16 @@ public void CesiumCompile_Core_Library_ShouldSucceed(string projectName)
$"{projectName}.deps.json",
];

var result = ExecuteTargets(projectName, "Restore", "Build");
var result = await ExecuteTargets(projectName, "Restore", "Build");

Assert.True(result.ExitCode == 0);
AssertEx.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertEx.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
}

[Theory]
[InlineData("SimpleNetfxLibrary")]
public void CesiumCompile_NetFxLibrary_ShouldSucceed(string projectName)
public async Task CesiumCompile_NetFxLibrary_ShouldSucceed(string projectName)
{
HashSet<string> expectedObjArtifacts =
[
Expand All @@ -93,10 +93,10 @@ public void CesiumCompile_NetFxLibrary_ShouldSucceed(string projectName)
"Cesium.Runtime.dll",
];

var result = ExecuteTargets(projectName, "Restore", "Build");
var result = await ExecuteTargets(projectName, "Restore", "Build");

Assert.True(result.ExitCode == 0);
AssertEx.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertEx.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedObjArtifacts, result.IntermediateArtifacts.Select(a => a.FileName).ToList());
AssertCollection.Includes(expectedBinArtifacts, result.OutputArtifacts.Select(a => a.FileName).ToList());
}
}
32 changes: 0 additions & 32 deletions Cesium.Sdk.Tests/Framework/MSBuildCLI.cs

This file was deleted.

7 changes: 3 additions & 4 deletions Cesium.Sdk.Tests/SdkTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
using System.Runtime.InteropServices;
using System.Text.Json;
using Cesium.Solution.Metadata;
using Cesium.TestFramework;
using Xunit.Abstractions;

namespace Cesium.Sdk.Tests;

public abstract class SdkTestBase : IDisposable
{
private const string _binLogFile = "build_result.binlog";

private readonly ITestOutputHelper _testOutputHelper;
private readonly string _temporaryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

Expand All @@ -36,7 +35,7 @@ public SdkTestBase(ITestOutputHelper testOutputHelper)
EmitGlobalJson(GlobalJsonPath, $"{SolutionMetadata.VersionPrefix}");
}

protected BuildResult ExecuteTargets(string projectName, params string[] targets)
protected async Task<BuildResult> ExecuteTargets(string projectName, params string[] targets)
{
var projectFile = $"{projectName}/{projectName}.ceproj";
var joinedTargets = string.Join(";", targets);
Expand Down Expand Up @@ -90,7 +89,7 @@ protected BuildResult ExecuteTargets(string projectName, params string[] targets
? "Build succeeded"
: $"Build failed with exit code {process.ExitCode}");

var properties = MSBuildCLI.EvaluateProperties(testProjectFile, objFolderPropertyName, binFolderPropertyName);
var properties = await DotNetCliHelper.EvaluateMSBuildProperties(_testOutputHelper, testProjectFile, objFolderPropertyName, binFolderPropertyName);
_testOutputHelper.WriteLine($"Properties request result: {JsonSerializer.Serialize(properties, new JsonSerializerOptions { WriteIndented = false })}");

var binFolder = NormalizePath(Path.GetFullPath(properties[binFolderPropertyName], testProjectFolder));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Cesium.Sdk.Tests.Framework;
using Cesium.TestFramework.Exceptions;

public static class AssertEx
namespace Cesium.TestFramework;

public static class AssertCollection
{
public static void Includes<T>(IReadOnlyCollection<T> expected, IReadOnlyCollection<T> all)
{
Expand Down
24 changes: 24 additions & 0 deletions Cesium.TestFramework/DotNetCliHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Medallion.Shell;
using Xunit.Abstractions;

Expand Down Expand Up @@ -25,6 +26,29 @@ await RunToSuccess(output, "dotnet", Path.GetDirectoryName(projectFilePath)!, ne
});
}

public static async Task<string> EvaluateMSBuildProperty(ITestOutputHelper output, string projectPath, string propertyName)
{
var result = await ExecUtil.Run(output, "dotnet", Environment.CurrentDirectory, [ "msbuild", $"\"{projectPath}\"", $"-getProperty:{propertyName}" ]);
return result.StandardOutput;
}

public static async Task<IReadOnlyDictionary<string, string>> EvaluateMSBuildProperties(ITestOutputHelper output, string projectPath, params string[] propertyNames)
{
if (!propertyNames.Any())
return new Dictionary<string, string>();

var result = await ExecUtil.Run(output, "dotnet", Environment.CurrentDirectory, [ "msbuild", $"\"{projectPath}\"", $"-getProperty:{string.Join(",", propertyNames)}" ]);
var resultString = result.StandardOutput;
if (propertyNames.Length == 1)
return new Dictionary<string, string> { { propertyNames[0], resultString } };

var resultJson = JsonDocument.Parse(resultString);
var propertiesJson = resultJson.RootElement.GetProperty("Properties").EnumerateObject().ToArray();

return propertiesJson
.ToDictionary(property => property.Name, property => property.Value.GetString() ?? string.Empty);
}

public static Task<CommandResult> RunDotNetDll(
ITestOutputHelper output,
string workingDirectoryPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Xunit.Sdk;

namespace Cesium.Sdk.Tests.Framework;
namespace Cesium.TestFramework.Exceptions;

public class IncludesAssertFailedException<T>(
IEnumerable<T> expected,
Expand Down

0 comments on commit 852a05b

Please sign in to comment.