Skip to content

Commit 85e8f68

Browse files
authored
Cleanup of fx_resolver_t and tests in NativeHostApis (#100542)
Slight cleanup of `fx_resolver_t` and `NativeHostApis` tests in preparation for #99027: - Collapse `reconcile_fx_references_helper` into `reconcile_fx_references` - Make `NativeHostApis` tests / `HostApiInvokerApp` more consistent in how they log and validate results
1 parent e020a93 commit 85e8f68

File tree

9 files changed

+165
-230
lines changed

9 files changed

+165
-230
lines changed

src/installer/tests/AppHost.Bundle.Tests/BundleProbe.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ private void SingleFileApp_ProbeFiles()
3535
};
3636

3737
var result = Command.Create(singleFile, $"host_runtime_contract.bundle_probe {string.Join(" ", itemsToProbe.Select(i => i.Path))}")
38-
.CaptureStdErr()
39-
.CaptureStdOut()
38+
.EnableTracingAndCaptureOutputs()
4039
.Execute();
4140

4241
result.Should().Pass();

src/installer/tests/Assets/Projects/HostApiInvokerApp/HostFXR.cs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -106,62 +106,48 @@ internal static extern int hostfxr_get_dotnet_environment_info(
106106
/// <summary>
107107
/// Test invoking the native hostfxr api hostfxr_resolve_sdk2
108108
/// </summary>
109-
/// <param name="args[0]">hostfxr_get_available_sdks</param>
110-
/// <param name="args[1]">Directory of dotnet executable</param>
111-
/// <param name="args[2]">Working directory where search for global.json begins</param>
112-
/// <param name="args[3]">Flags</param>
109+
/// <param name="args[0]">Directory of dotnet executable</param>
110+
/// <param name="args[1]">Working directory where search for global.json begins</param>
111+
/// <param name="args[2]">Flags</param>
113112
static void Test_hostfxr_resolve_sdk2(string[] args)
114113
{
115-
if (args.Length != 4)
114+
if (args.Length != 3)
116115
{
117116
throw new ArgumentException("Invalid number of arguments passed");
118117
}
119118

120119
var data = new List<(hostfxr.hostfxr_resolve_sdk2_result_key_t, string)>();
121120
int rc = hostfxr.hostfxr_resolve_sdk2(
122-
exe_dir: args[1],
123-
working_dir: args[2],
124-
flags: Enum.Parse<hostfxr.hostfxr_resolve_sdk2_flags_t>(args[3]),
121+
exe_dir: args[0],
122+
working_dir: args[1],
123+
flags: Enum.Parse<hostfxr.hostfxr_resolve_sdk2_flags_t>(args[2]),
125124
result: (key, value) => data.Add((key, value)));
126125

127-
if (rc == 0)
128-
{
129-
Console.WriteLine("hostfxr_resolve_sdk2:Success");
130-
}
131-
else
132-
{
133-
Console.WriteLine($"hostfxr_resolve_sdk2:Fail[{rc}]");
134-
}
135-
136-
Console.WriteLine($"hostfxr_resolve_sdk2 data:[{string.Join(';', data)}]");
126+
string api = nameof(hostfxr.hostfxr_resolve_sdk2);
127+
LogResult(api, rc);
128+
Console.WriteLine($"{api} data:[{string.Join(';', data)}]");
137129
}
138130

139131
/// <summary>
140132
/// Test invoking the native hostfxr api hostfxr_get_available_sdks
141133
/// </summary>
142-
/// <param name="args[0]">hostfxr_get_available_sdks</param>
143-
/// <param name="args[1]">Directory of dotnet executable</param>
134+
/// <param name="args[0]">Directory of dotnet executable</param>
144135
static void Test_hostfxr_get_available_sdks(string[] args)
145136
{
146-
if (args.Length != 2)
137+
if (args.Length != 1)
147138
{
148139
throw new ArgumentException("Invalid number of arguments passed");
149140
}
150141

151142
string[] sdks = null;
152143
int rc = hostfxr.hostfxr_get_available_sdks(
153-
exe_dir: args[1],
144+
exe_dir: args[0],
154145
(sdk_count, sdk_dirs) => sdks = sdk_dirs);
155146

156-
if (rc == 0)
157-
{
158-
Console.WriteLine("hostfxr_get_available_sdks:Success");
159-
Console.WriteLine($"hostfxr_get_available_sdks sdks:[{string.Join(';', sdks)}]");
160-
}
161-
else
162-
{
163-
Console.WriteLine($"hostfxr_get_available_sdks:Fail[{rc}]");
164-
}
147+
string api = nameof(hostfxr.hostfxr_get_available_sdks);
148+
LogResult(api, rc);
149+
if (sdks != null)
150+
Console.WriteLine($"{api} sdks:[{string.Join(';', sdks)}]");
165151
}
166152

167153
static void Test_hostfxr_set_error_writer(string[] args)
@@ -193,13 +179,12 @@ static void Test_hostfxr_set_error_writer(string[] args)
193179
/// <summary>
194180
/// Test that invokes native api hostfxr_get_dotnet_environment_info.
195181
/// </summary>
196-
/// <param name="args[0]">hostfxr_get_dotnet_environment_info</param>
197-
/// <param name="args[1]">(Optional) Path to the directory with dotnet.exe</param>
182+
/// <param name="args[0]">(Optional) Path to the directory with dotnet.exe</param>
198183
static void Test_hostfxr_get_dotnet_environment_info(string[] args)
199184
{
200185
string dotnetExeDir = null;
201-
if (args.Length >= 2)
202-
dotnetExeDir = args[1];
186+
if (args.Length >= 1)
187+
dotnetExeDir = args[0];
203188

204189
string hostfxr_version;
205190
string hostfxr_commit_hash;
@@ -254,21 +239,20 @@ static void Test_hostfxr_get_dotnet_environment_info(string[] args)
254239
result: result_fn,
255240
result_context: new IntPtr(42));
256241

257-
if (rc != 0)
258-
{
259-
Console.WriteLine($"hostfxr_get_dotnet_environment_info:Fail[{rc}]");
260-
}
261-
262-
Console.WriteLine($"hostfxr_get_dotnet_environment_info sdk versions:[{string.Join(";", sdks.Select(s => s.version).ToList())}]");
263-
Console.WriteLine($"hostfxr_get_dotnet_environment_info sdk paths:[{string.Join(";", sdks.Select(s => s.path).ToList())}]");
242+
string api = nameof(hostfxr.hostfxr_get_dotnet_environment_info);
243+
LogResult(api, rc);
264244

265-
Console.WriteLine($"hostfxr_get_dotnet_environment_info framework names:[{string.Join(";", frameworks.Select(f => f.name).ToList())}]");
266-
Console.WriteLine($"hostfxr_get_dotnet_environment_info framework versions:[{string.Join(";", frameworks.Select(f => f.version).ToList())}]");
267-
Console.WriteLine($"hostfxr_get_dotnet_environment_info framework paths:[{string.Join(";", frameworks.Select(f => f.path).ToList())}]");
245+
Console.WriteLine($"{api} sdk versions:[{string.Join(";", sdks.Select(s => s.version).ToList())}]");
246+
Console.WriteLine($"{api} sdk paths:[{string.Join(";", sdks.Select(s => s.path).ToList())}]");
268247

269-
Console.WriteLine("hostfxr_get_dotnet_environment_info:Success");
248+
Console.WriteLine($"{api} framework names:[{string.Join(";", frameworks.Select(f => f.name).ToList())}]");
249+
Console.WriteLine($"{api} framework versions:[{string.Join(";", frameworks.Select(f => f.version).ToList())}]");
250+
Console.WriteLine($"{api} framework paths:[{string.Join(";", frameworks.Select(f => f.path).ToList())}]");
270251
}
271252

253+
private static void LogResult(string apiName, int rc)
254+
=> Console.WriteLine(rc == 0 ? $"{apiName}:Success" : $"{apiName}:Fail[0x{rc:x}]");
255+
272256
public static bool RunTest(string apiToTest, string[] args)
273257
{
274258
switch (apiToTest)

src/installer/tests/Assets/Projects/HostApiInvokerApp/HostRuntimeContract.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ public static bool RunTest(string apiToTest, string[] args)
113113
switch (apiToTest)
114114
{
115115
case $"{nameof(host_runtime_contract)}.{nameof(host_runtime_contract.get_runtime_property)}":
116-
Test_get_runtime_property(args[1..]);
116+
Test_get_runtime_property(args);
117117
break;
118118
case $"{nameof(host_runtime_contract)}.{nameof(host_runtime_contract.bundle_probe)}":
119-
Test_bundle_probe(args[1..]);
119+
Test_bundle_probe(args);
120120
break;
121121
default:
122122
return false;

src/installer/tests/Assets/Projects/HostApiInvokerApp/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public static void MainCore(string[] args)
3131
Console.WriteLine("Hello World!");
3232
Console.WriteLine(string.Join(Environment.NewLine, args));
3333

34-
// Enable tracing so that test assertion failures are easier to diagnose.
35-
Environment.SetEnvironmentVariable("COREHOST_TRACE", "1");
36-
3734
// If requested, test multilevel lookup using fake Global SDK directories:
3835
// 1. using a fake ProgramFiles location
3936
// 2. using a fake SDK Self-Registered location
@@ -61,13 +58,13 @@ public static void MainCore(string[] args)
6158
}
6259

6360
string apiToTest = args[0];
64-
if (HostFXR.RunTest(apiToTest, args))
61+
if (HostFXR.RunTest(apiToTest, args[1..]))
6562
return;
6663

67-
if (HostPolicy.RunTest(apiToTest, args))
64+
if (HostPolicy.RunTest(apiToTest, args[1..]))
6865
return;
6966

70-
if (HostRuntimeContract.RunTest(apiToTest, args))
67+
if (HostRuntimeContract.RunTest(apiToTest, args[1..]))
7168
return;
7269

7370
throw new ArgumentException($"Invalid API to test passed as args[0]): {apiToTest}");

0 commit comments

Comments
 (0)