|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#if NET10_0 || NETFRAMEWORK |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Xml.Linq; |
| 13 | +using Xunit; |
| 14 | +using static Microsoft.JavaScript.NodeApi.Test.TestBuilder; |
| 15 | +using static Microsoft.JavaScript.NodeApi.Test.TestUtils; |
| 16 | + |
| 17 | +namespace Microsoft.JavaScript.NodeApi.Test; |
| 18 | + |
| 19 | +[CollectionDefinition(nameof(EmbeddingPackageTests), DisableParallelization = true)] |
| 20 | +[Collection(nameof(EmbeddingPackageTests))] |
| 21 | +public sealed class EmbeddingPackageTests : IDisposable |
| 22 | +{ |
| 23 | + private const string WarningCode = "NODEAPIEMBED001"; |
| 24 | + private readonly string _fixture = Path.Combine(TestCasesDirectory, "EmbeddingPackage"); |
| 25 | + private readonly string _packages = Path.Combine(RepoRootDirectory, "out", "obj", |
| 26 | + Configuration, "TestCases", "EmbeddingConsumer", "packages"); |
| 27 | + private readonly string _version; |
| 28 | + private readonly string _libNodeVersion; |
| 29 | + |
| 30 | + public EmbeddingPackageTests() |
| 31 | + { |
| 32 | + XElement versions = XElement.Load(Path.Combine(RepoRootDirectory, "out", "pkg", "version.props")); |
| 33 | + _version = versions.Descendants("NodeApiDotNetPackageVersion").Single().Value; |
| 34 | + _libNodeVersion = versions.Descendants("LibNodePackageVersion").Single().Value; |
| 35 | + if (Directory.Exists(_packages)) Directory.Delete(_packages, recursive: true); |
| 36 | + Directory.CreateDirectory(_packages); |
| 37 | + |
| 38 | + // A net10 run writes this file; net472 must use the repository SDK, not a stale file. |
| 39 | + File.Delete(Path.Combine(_fixture, "global.json")); |
| 40 | + } |
| 41 | + |
| 42 | + public void Dispose() => File.Delete(Path.Combine(_fixture, "global.json")); |
| 43 | + |
| 44 | +#if NET10_0 |
| 45 | + [Fact] |
| 46 | + public void PortableConsumerImportsEsmRegardlessOfPrefer32Bit() |
| 47 | + { |
| 48 | + string output = BuildConsumer("portable"); |
| 49 | + AssertConsumerRuns(output); |
| 50 | + |
| 51 | + // Modern .NET ignores Prefer32Bit, so it must still build without NODEAPIEMBED001. |
| 52 | + output = BuildConsumer("prefer32bit", "AnyCPU", prefer32Bit: true); |
| 53 | + AssertConsumerRuns(output); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void WindowsPackageContainsFrameworkSentinelsAndTransitiveDependencies() |
| 58 | + { |
| 59 | + // Windows-only: other platforms omit net472, and this project has no dynamic skip mechanism. |
| 60 | + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; |
| 61 | + |
| 62 | + string extracted = Path.Combine(_packages, "inspection"); |
| 63 | + string package = Path.Combine(RepoRootDirectory, "out", "pkg", |
| 64 | + $"Microsoft.JavaScript.NodeApi.Embedding.{_version}.nupkg"); |
| 65 | + System.IO.Compression.ZipFile.ExtractToDirectory(package, extracted); |
| 66 | + XElement metadata = XDocument.Load(Directory.GetFiles(extracted, "*.nuspec").Single()) |
| 67 | + .Root!.Elements().Single(); |
| 68 | + XNamespace ns = metadata.Name.Namespace; |
| 69 | + Assert.Equal("Embeds Node.js in .NET apps: brings Microsoft.JavaScript.NodeApi and " + |
| 70 | + "Microsoft.JavaScript.LibNode (win/linux/osx, x64/arm64).", |
| 71 | + metadata.Element(ns + "description")!.Value); |
| 72 | + XElement[] groups = metadata.Element(ns + "dependencies")!.Elements().ToArray(); |
| 73 | + string[] expectedFrameworks = |
| 74 | + { ".NETFramework4.7.2", ".NETStandard2.0", "net10.0", "net8.0", "net9.0" }; |
| 75 | + Assert.Equal(expectedFrameworks, |
| 76 | + groups.Select(g => g.Attribute("targetFramework")!.Value).OrderBy(t => t, StringComparer.Ordinal)); |
| 77 | + foreach (XElement group in groups) |
| 78 | + { |
| 79 | + XElement[] dependencies = group.Elements().ToArray(); |
| 80 | + Assert.Equal(2, dependencies.Length); |
| 81 | + Assert.All(dependencies, d => Assert.Equal("All", d.Attribute("include")!.Value)); |
| 82 | + Assert.Equal(_libNodeVersion, dependencies.Single(d => |
| 83 | + d.Attribute("id")!.Value == "Microsoft.JavaScript.LibNode").Attribute("version")!.Value); |
| 84 | + Assert.Equal(_version, dependencies.Single(d => |
| 85 | + d.Attribute("id")!.Value == "Microsoft.JavaScript.NodeApi").Attribute("version")!.Value); |
| 86 | + } |
| 87 | + |
| 88 | + foreach (string tfm in new[] { "net10.0", "net9.0", "net8.0", "netstandard2.0", "net472" }) |
| 89 | + { |
| 90 | + Assert.True(File.Exists(Path.Combine(extracted, "lib", tfm, "_._")), tfm); |
| 91 | + } |
| 92 | + Assert.Empty(Directory.GetFiles(extracted, "*.dll", SearchOption.AllDirectories)); |
| 93 | + Assert.False(File.Exists(Path.ChangeExtension(package, ".snupkg"))); |
| 94 | + Assert.True(File.Exists(Path.Combine(extracted, "README.md"))); |
| 95 | + Assert.True(File.Exists(Path.Combine(extracted, "buildTransitive", |
| 96 | + "Microsoft.JavaScript.NodeApi.Embedding.targets"))); |
| 97 | + } |
| 98 | +#else |
| 99 | + [Fact] |
| 100 | + public void FrameworkX64ConsumerImportsEsm() |
| 101 | + { |
| 102 | + string output = BuildConsumer("x64", "x64"); |
| 103 | + Assert.True(File.Exists(Path.Combine(output, "runtimes", "win-x64", "native", "libnode.dll"))); |
| 104 | + AssertConsumerRuns(output); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void FrameworkDefaultConsumerRunsAs64BitAnyCpu() |
| 109 | + { |
| 110 | + string output = BuildConsumer("default"); |
| 111 | + Assert.Equal("AnyCPU", File.ReadAllText(Path.Combine(output, "platform.txt")).Trim()); |
| 112 | + AssertConsumerRuns(output); |
| 113 | + } |
| 114 | + |
| 115 | + // Both explicit x86 and legacy Prefer32Bit must warn, without running a 32-bit process. |
| 116 | + [Theory] |
| 117 | + [InlineData("x86", false)] |
| 118 | + [InlineData("AnyCPU", true)] |
| 119 | + public void Framework32BitConsumerBuildWarns(string platform, bool prefer32Bit) |
| 120 | + { |
| 121 | + BuildConsumer("warning-" + platform, platform, prefer32Bit, expectWarning: true); |
| 122 | + } |
| 123 | +#endif |
| 124 | + |
| 125 | + private string BuildConsumer( |
| 126 | + string lane, string? platform = null, bool prefer32Bit = false, bool expectWarning = false) |
| 127 | + { |
| 128 | + string framework = GetCurrentFrameworkTarget(); |
| 129 | + string fallback = Environment.GetEnvironmentVariable("NUGET_PACKAGES") ?? |
| 130 | + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); |
| 131 | + var properties = new Dictionary<string, string> |
| 132 | + { |
| 133 | + ["Configuration"] = Configuration, |
| 134 | + ["TargetFrameworks"] = framework, |
| 135 | + ["ConsumerLane"] = lane, |
| 136 | + ["RestorePackagesPath"] = _packages, |
| 137 | + ["RestoreFallbackFolders"] = fallback, |
| 138 | + ["RestoreNoHttpCache"] = "true", |
| 139 | + ["RestoreForce"] = "true", |
| 140 | + }; |
| 141 | + if (platform != null) properties["PlatformTarget"] = platform; |
| 142 | + if (prefer32Bit) properties["Prefer32Bit"] = "true"; |
| 143 | + string log = GetBuildLogFilePath(lane, "EmbeddingConsumer"); |
| 144 | + BuildProject(Path.Combine(_fixture, "EmbeddingConsumer.csproj"), "Rebuild", properties, log); |
| 145 | + string buildLog = File.ReadAllText(log); |
| 146 | + Assert.Equal(expectWarning, buildLog.Contains(WarningCode)); |
| 147 | + foreach (string id in new[] { "microsoft.javascript.nodeapi", "microsoft.javascript.nodeapi.embedding" }) |
| 148 | + { |
| 149 | + Assert.True(File.Exists(Path.Combine(_packages, id, _version, id + ".nuspec")), id); |
| 150 | + } |
| 151 | + // LibNode must come from the already-restored fallback, not another six-RID extraction. |
| 152 | + Assert.Empty(Directory.GetDirectories(_packages, "microsoft.javascript.libnode*")); |
| 153 | + return Path.Combine(RepoRootDirectory, "out", "bin", Configuration, |
| 154 | + "EmbeddingConsumer", lane, framework); |
| 155 | + } |
| 156 | + |
| 157 | + private static void AssertConsumerRuns(string output) |
| 158 | + { |
| 159 | + string executable = Path.Combine(output, "EmbeddingConsumer.exe"); |
| 160 | + ProcessStartInfo start = new() |
| 161 | + { |
| 162 | + FileName = GetCurrentFrameworkTarget() == "net472" ? executable : "dotnet", |
| 163 | + Arguments = GetCurrentFrameworkTarget() == "net472" ? string.Empty : |
| 164 | + "\"" + Path.Combine(output, "EmbeddingConsumer.dll") + "\"", |
| 165 | + WorkingDirectory = output, |
| 166 | + UseShellExecute = false, |
| 167 | + RedirectStandardOutput = true, |
| 168 | + RedirectStandardError = true, |
| 169 | + }; |
| 170 | + using Process process = Process.Start(start)!; |
| 171 | + Task<string> stdoutTask = process.StandardOutput.ReadToEndAsync(); |
| 172 | + Task<string> stderrTask = process.StandardError.ReadToEndAsync(); |
| 173 | + if (!process.WaitForExit(60000)) |
| 174 | + { |
| 175 | + process.Kill(); |
| 176 | + Assert.Fail("Consumer did not exit within 60 seconds."); |
| 177 | + } |
| 178 | + string stdout = stdoutTask.GetAwaiter().GetResult(); |
| 179 | + string stderr = stderrTask.GetAwaiter().GetResult(); |
| 180 | + Assert.True(process.ExitCode == 0, stdout + stderr); |
| 181 | + Assert.Equal(string.Empty, stderr); |
| 182 | + Assert.Contains("ESM=embedded ESM works", stdout); |
| 183 | + Assert.Contains("BITS=64", stdout); |
| 184 | + string? nodeVersion = null; |
| 185 | + NodejsEmbeddingTests.RunInNodejsEnvironment(() => |
| 186 | + nodeVersion = (string)JSValue.Global["process"]["versions"]["node"]); |
| 187 | + Assert.Contains("NODE=" + nodeVersion, stdout); |
| 188 | + } |
| 189 | +} |
| 190 | +#endif |
0 commit comments