Skip to content

Commit

Permalink
Migrate host.tests from Newtonsoft to STJ (#76901)
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Oct 21, 2022
1 parent 3df94de commit b4a3c68
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 199 deletions.
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<NewtonsoftJsonBsonVersion>1.0.2</NewtonsoftJsonBsonVersion>
<SQLitePCLRawbundle_greenVersion>2.0.4</SQLitePCLRawbundle_greenVersion>
<MoqVersion>4.12.0</MoqVersion>
<FluentAssertionsVersion>6.7.0</FluentAssertionsVersion>
<FsCheckVersion>2.14.3</FsCheckVersion>
<!-- Android gRPC client tests -->
<GoogleProtobufVersion>3.19.4</GoogleProtobufVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<Project>

<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
Expand All @@ -12,6 +10,4 @@
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
</ItemGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static AndConstraint<CommandResultAssertions> HaveRuntimePropertyContaini
foreach (string value in values)
{
Execute.Assertion.ForCondition(propertyValue != null && propertyValue.Contains(value))
.FailWithPreformatted($"The property {propertyName} doesn't contain expected value: '{value}'{Environment.NewLine}" +
.FailWith($"The property {propertyName} doesn't contain expected value: '{value}'{Environment.NewLine}" +
$"{propertyName}='{propertyValue}'" +
$"{assertion.GetDiagnosticsInfo()}");
}
Expand All @@ -37,7 +37,7 @@ public static AndConstraint<CommandResultAssertions> NotHaveRuntimePropertyConta
foreach (string value in values)
{
Execute.Assertion.ForCondition(propertyValue != null && !propertyValue.Contains(value))
.FailWithPreformatted($"The property {propertyName} contains unexpected value: '{value}'{Environment.NewLine}" +
.FailWith($"The property {propertyName} contains unexpected value: '{value}'{Environment.NewLine}" +
$"{propertyName}='{propertyValue}'" +
$"{assertion.GetDiagnosticsInfo()}");
}
Expand Down Expand Up @@ -84,7 +84,7 @@ public static AndConstraint<CommandResultAssertions> HaveResolvedComponentDepend
foreach (string value in values)
{
Execute.Assertion.ForCondition(propertyValue != null && propertyValue.Contains(value))
.FailWithPreformatted($"The resolved {propertyName} doesn't contain expected value: '{value}'{Environment.NewLine}" +
.FailWith($"The resolved {propertyName} doesn't contain expected value: '{value}'{Environment.NewLine}" +
$"{propertyName}='{propertyValue}'" +
$"{assertion.GetDiagnosticsInfo()}");
}
Expand All @@ -102,7 +102,7 @@ public static AndConstraint<CommandResultAssertions> NotHaveResolvedComponentDep
foreach (string value in values)
{
Execute.Assertion.ForCondition(propertyValue != null && !propertyValue.Contains(value))
.FailWithPreformatted($"The resolved {propertyName} contains unexpected value: '{value}'{Environment.NewLine}" +
.FailWith($"The resolved {propertyName} contains unexpected value: '{value}'{Environment.NewLine}" +
$"{propertyName}='{propertyValue}'" +
$"{assertion.GetDiagnosticsInfo()}");
}
Expand Down
24 changes: 14 additions & 10 deletions src/installer/tests/HostActivation.Tests/LightupAppActivation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text.Json.Nodes;
using Xunit;

namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
Expand Down Expand Up @@ -400,9 +400,11 @@ public void SharedFx_With_Higher_Version_Wins_Against_Additional_Deps()

// Create a deps.json file in the folder "additionalDeps\shared\Microsoft.NETCore.App\9999.0.0"
string additionalDepsRootPath = Path.Combine(_fxBaseDir, "additionalDeps");
JObject versionInfo = new JObject();
versionInfo.Add(new JProperty("assemblyVersion", "0.0.0.1"));
versionInfo.Add(new JProperty("fileVersion", "0.0.0.2"));
JsonObject versionInfo = new JsonObject
{
["assemblyVersion"] = "0.0.0.1",
["fileVersion"] = "0.0.0.2"
};
string additionalDepsPath = CreateAdditionalDeps(additionalDepsRootPath, versionInfo);

// Version: NetCoreApp 9999.0.0
Expand Down Expand Up @@ -456,10 +458,12 @@ public void SharedFx_With_Lower_Version_Loses_Against_Additional_Deps()

// Create a deps.json file in the folder "additionalDeps\shared\Microsoft.NETCore.App\9999.0.0"
string additionalDepsRootPath = Path.Combine(_fxBaseDir, "additionalDeps");
JObject versionInfo = new JObject();
// Use Higher version numbers to win
versionInfo.Add(new JProperty("assemblyVersion", "99.9.9.9"));
versionInfo.Add(new JProperty("fileVersion", "98.9.9.9"));
JsonObject versionInfo = new JsonObject
{
// Use Higher version numbers to win
["assemblyVersion"] = "99.9.9.9",
["fileVersion"] = "98.9.9.9"
};
string additionalDepsPath = CreateAdditionalDeps(additionalDepsRootPath, versionInfo);

// Version: NetCoreApp 9999.0.0
Expand Down Expand Up @@ -492,7 +496,7 @@ private static void CreateLightupFolder(string customLightupPath, string version
File.Copy(libDepsJson, Path.Combine(customLightupPath, Path.GetFileName(libDepsJson)));
}

private static string CreateAdditionalDeps(string destDir, JObject immutableCollectionVersionInfo)
private static string CreateAdditionalDeps(string destDir, JsonObject immutableCollectionVersionInfo)
{
DirectoryInfo dir = new DirectoryInfo(destDir);
if (dir.Exists)
Expand All @@ -502,7 +506,7 @@ private static string CreateAdditionalDeps(string destDir, JObject immutableColl

dir.Create();

JObject depsjson = SharedFramework.CreateDepsJson("Microsoft.NETCore.App", "LightupLib/1.0.0", "LightupLib");
JsonObject depsjson = SharedFramework.CreateDepsJson("Microsoft.NETCore.App", "LightupLib/1.0.0", "LightupLib");

string depsFile = Path.Combine(destDir, "My.deps.json");
File.WriteAllText(depsFile, depsjson.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ public void TestWithIdenticalBundlesShouldBeBinaryEqualPasses()
var secondBundle = CreateSampleBundle(true);
byte[] secondBundleContent = File.ReadAllBytes(secondBundle.bundleFileName);

firstBundle.bundleId.ShouldBeEquivalentTo(secondBundle.bundleId,
firstBundle.bundleId.Should().BeEquivalentTo(secondBundle.bundleId,
"Deterministic/Reproducible build should produce identical bundle id for identical inputs");
firstBundleContent.ShouldBeEquivalentTo(secondBundleContent,
firstBundleContent.Should().BeEquivalentTo(secondBundleContent,
"Deterministic/Reproducible build should produce identical binary for identical inputs");
}

Expand Down

This file was deleted.

34 changes: 17 additions & 17 deletions src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,121 +21,121 @@ public CommandResultAssertions(CommandResult commandResult)
public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
{
Execute.Assertion.ForCondition(Result.ExitCode == expectedExitCode)
.FailWithPreformatted($"Expected command to exit with {expectedExitCode} but it did not.{GetDiagnosticsInfo()}");
.FailWith($"Expected command to exit with {expectedExitCode} but it did not.{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> Pass()
{
Execute.Assertion.ForCondition(Result.ExitCode == 0)
.FailWithPreformatted($"Expected command to pass but it did not.{GetDiagnosticsInfo()}");
.FailWith($"Expected command to pass but it did not.{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> Fail()
{
Execute.Assertion.ForCondition(Result.ExitCode != 0)
.FailWithPreformatted($"Expected command to fail but it did not.{GetDiagnosticsInfo()}");
.FailWith($"Expected command to fail but it did not.{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdOut()
{
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(Result.StdOut))
.FailWithPreformatted($"Command did not output anything to stdout{GetDiagnosticsInfo()}");
.FailWith($"Command did not output anything to stdout{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdOut(string expectedOutput)
{
Execute.Assertion.ForCondition(Result.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
.FailWithPreformatted($"Command did not output with Expected Output. Expected: '{expectedOutput}'{GetDiagnosticsInfo()}");
.FailWith($"Command did not output with Expected Output. Expected: '{expectedOutput}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdOutContaining(string pattern)
{
Execute.Assertion.ForCondition(Result.StdOut.Contains(pattern))
.FailWithPreformatted($"The command output did not contain expected result: '{pattern}'{GetDiagnosticsInfo()}");
.FailWith($"The command output did not contain expected result: '{pattern}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> NotHaveStdOutContaining(string pattern)
{
Execute.Assertion.ForCondition(!Result.StdOut.Contains(pattern))
.FailWithPreformatted($"The command output contained a result it should not have contained: '{pattern}'{GetDiagnosticsInfo()}");
.FailWith($"The command output contained a result it should not have contained: '{pattern}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
{
Execute.Assertion.ForCondition(Regex.IsMatch(Result.StdOut, pattern, options))
.FailWithPreformatted($"Matching the command output failed. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
.FailWith($"Matching the command output failed. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdErr()
{
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(Result.StdErr))
.FailWithPreformatted($"Command did not output anything to stderr.{GetDiagnosticsInfo()}");
.FailWith($"Command did not output anything to stderr.{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdErrContaining(string pattern)
{
Execute.Assertion.ForCondition(Result.StdErr.Contains(pattern))
.FailWithPreformatted($"The command error output did not contain expected result: '{pattern}'{GetDiagnosticsInfo()}");
.FailWith($"The command error output did not contain expected result: '{pattern}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> NotHaveStdErrContaining(string pattern)
{
Execute.Assertion.ForCondition(!Result.StdErr.Contains(pattern))
.FailWithPreformatted($"The command error output contained a result it should not have contained: '{pattern}'{GetDiagnosticsInfo()}");
.FailWith($"The command error output contained a result it should not have contained: '{pattern}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None)
{
Execute.Assertion.ForCondition(Regex.IsMatch(Result.StdErr, pattern, options))
.FailWithPreformatted($"Matching the command error output failed. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
.FailWith($"Matching the command error output failed. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> NotHaveStdOut()
{
Execute.Assertion.ForCondition(string.IsNullOrEmpty(Result.StdOut))
.FailWithPreformatted($"Expected command to not output to stdout but it was not:{GetDiagnosticsInfo()}");
.FailWith($"Expected command to not output to stdout but it was not:{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> NotHaveStdErr()
{
Execute.Assertion.ForCondition(string.IsNullOrEmpty(Result.StdErr))
.FailWithPreformatted($"Expected command to not output to stderr but it was not:{GetDiagnosticsInfo()}");
.FailWith($"Expected command to not output to stderr but it was not:{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> FileExists(string path)
{
Execute.Assertion.ForCondition(System.IO.File.Exists(path))
.FailWithPreformatted($"The command did not write the expected file: '{path}'{GetDiagnosticsInfo()}");
.FailWith($"The command did not write the expected file: '{path}'{GetDiagnosticsInfo()}");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> FileContains(string path, string pattern)
{
string fileContent = System.IO.File.ReadAllText(path);
Execute.Assertion.ForCondition(fileContent.Contains(pattern))
.FailWithPreformatted($"The command did not write the expected result '{pattern}' to the file: '{path}'{GetDiagnosticsInfo()}{Environment.NewLine}file content: >>{fileContent}<<");
.FailWith($"The command did not write the expected result '{pattern}' to the file: '{path}'{GetDiagnosticsInfo()}{Environment.NewLine}file content: >>{fileContent}<<");
return new AndConstraint<CommandResultAssertions>(this);
}

public AndConstraint<CommandResultAssertions> NotFileContains(string path, string pattern)
{
string fileContent = System.IO.File.ReadAllText(path);
Execute.Assertion.ForCondition(!fileContent.Contains(pattern))
.FailWithPreformatted($"The command did not write the expected result '{pattern}' to the file: '{path}'{GetDiagnosticsInfo()}{Environment.NewLine}file content: >>{fileContent}<<");
.FailWith($"The command did not write the expected result '{pattern}' to the file: '{path}'{GetDiagnosticsInfo()}{Environment.NewLine}file content: >>{fileContent}<<");
return new AndConstraint<CommandResultAssertions>(this);
}

Expand Down
Loading

0 comments on commit b4a3c68

Please sign in to comment.