|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.IO.Compression; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Xunit; |
| 8 | +using Xunit.Abstractions; |
| 9 | + |
| 10 | +namespace Amazon.Lambda.TestTool.UnitTests; |
| 11 | + |
| 12 | +public class PackagingTests |
| 13 | +{ |
| 14 | + private readonly ITestOutputHelper _output; |
| 15 | + private static readonly string[] ExpectedFrameworks = new[] |
| 16 | + { |
| 17 | + "net5.0", |
| 18 | + "net6.0", |
| 19 | + "net8.0", |
| 20 | + "netstandard2.0" |
| 21 | + }; |
| 22 | + |
| 23 | + public PackagingTests(ITestOutputHelper output) |
| 24 | + { |
| 25 | + _output = output; |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void VerifyPackageContentsHasRuntimeSupport() |
| 30 | + { |
| 31 | + string solutionRoot = FindSolutionRoot(); |
| 32 | + string runtimeSupportPath = Path.Combine(solutionRoot, "Libraries", "src", "Amazon.Lambda.RuntimeSupport", "Amazon.Lambda.RuntimeSupport.csproj"); |
| 33 | + string projectPath = Path.Combine(solutionRoot, "Tools", "LambdaTestTool-v2", "src", "Amazon.Lambda.TestTool", "Amazon.Lambda.TestTool.csproj"); |
| 34 | + |
| 35 | + // Build RuntimeSupport first |
| 36 | + _output.WriteLine("Building RuntimeSupport..."); |
| 37 | + var buildProcess = new Process |
| 38 | + { |
| 39 | + StartInfo = new ProcessStartInfo |
| 40 | + { |
| 41 | + FileName = "dotnet", |
| 42 | + Arguments = $"build {runtimeSupportPath} -c Release", |
| 43 | + RedirectStandardOutput = true, |
| 44 | + RedirectStandardError = true, |
| 45 | + UseShellExecute = false, |
| 46 | + CreateNoWindow = true, |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + buildProcess.Start(); |
| 51 | + string buildOutput = buildProcess.StandardOutput.ReadToEnd(); |
| 52 | + string buildError = buildProcess.StandardError.ReadToEnd(); |
| 53 | + buildProcess.WaitForExit(); |
| 54 | + |
| 55 | + _output.WriteLine("Build Output:"); |
| 56 | + _output.WriteLine(buildOutput); |
| 57 | + if (!string.IsNullOrEmpty(buildError)) |
| 58 | + { |
| 59 | + _output.WriteLine("Build Errors:"); |
| 60 | + _output.WriteLine(buildError); |
| 61 | + } |
| 62 | + |
| 63 | + Assert.Equal(0, buildProcess.ExitCode); |
| 64 | + |
| 65 | + // Now pack the test tool |
| 66 | + _output.WriteLine("\nPacking TestTool..."); |
| 67 | + var packProcess = new Process |
| 68 | + { |
| 69 | + StartInfo = new ProcessStartInfo |
| 70 | + { |
| 71 | + FileName = "dotnet", |
| 72 | + Arguments = $"pack {projectPath} -c Release", |
| 73 | + RedirectStandardOutput = true, |
| 74 | + RedirectStandardError = true, |
| 75 | + UseShellExecute = false, |
| 76 | + CreateNoWindow = true, |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + packProcess.Start(); |
| 81 | + string packOutput = packProcess.StandardOutput.ReadToEnd(); |
| 82 | + string packError = packProcess.StandardError.ReadToEnd(); |
| 83 | + packProcess.WaitForExit(); |
| 84 | + |
| 85 | + _output.WriteLine("Pack Output:"); |
| 86 | + _output.WriteLine(packOutput); |
| 87 | + if (!string.IsNullOrEmpty(packError)) |
| 88 | + { |
| 89 | + _output.WriteLine("Pack Errors:"); |
| 90 | + _output.WriteLine(packError); |
| 91 | + } |
| 92 | + |
| 93 | + Assert.Equal(0, packProcess.ExitCode); |
| 94 | + |
| 95 | + string packageDir = Path.Combine(Path.GetDirectoryName(projectPath), "bin", "Release"); |
| 96 | + _output.WriteLine($"Looking for package in: {packageDir}"); |
| 97 | + |
| 98 | + var packageFiles = Directory.GetFiles(packageDir, "*.nupkg", SearchOption.AllDirectories); |
| 99 | + Assert.True(packageFiles.Length > 0, $"No .nupkg files found in {packageDir}"); |
| 100 | + |
| 101 | + // Rest of the verification code remains the same... |
| 102 | + string packagePath = packageFiles[0]; |
| 103 | + _output.WriteLine($"Found package: {packagePath}"); |
| 104 | + |
| 105 | + using (var archive = ZipFile.OpenRead(packagePath)) |
| 106 | + { |
| 107 | + var missingFiles = new List<string>(); |
| 108 | + |
| 109 | + foreach (var framework in ExpectedFrameworks) |
| 110 | + { |
| 111 | + var expectedPath = $"content/{framework}/Amazon.Lambda.RuntimeSupport.dll"; |
| 112 | + var entry = archive.GetEntry(expectedPath); |
| 113 | + |
| 114 | + if (entry == null) |
| 115 | + { |
| 116 | + missingFiles.Add(expectedPath); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (missingFiles.Any()) |
| 121 | + { |
| 122 | + Assert.Fail($"The following RuntimeSupport DLLs are missing from the package:\n" + |
| 123 | + string.Join("\n", missingFiles)); |
| 124 | + } |
| 125 | + |
| 126 | + var actualFrameworkDlls = archive.Entries |
| 127 | + .Where(e => e.FullName.Contains("Amazon.Lambda.RuntimeSupport.dll")) |
| 128 | + .Select(e => e.FullName) |
| 129 | + .ToList(); |
| 130 | + |
| 131 | + _output.WriteLine("\nFound DLLs in package:"); |
| 132 | + foreach (var dll in actualFrameworkDlls) |
| 133 | + { |
| 134 | + _output.WriteLine(dll); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + private string FindSolutionRoot() |
| 141 | + { |
| 142 | + string currentDirectory = Directory.GetCurrentDirectory(); |
| 143 | + while (currentDirectory != null) |
| 144 | + { |
| 145 | + // Look for the aws-lambda-dotnet directory specifically |
| 146 | + if (Path.GetFileName(currentDirectory) == "aws-lambda-dotnet") |
| 147 | + { |
| 148 | + return currentDirectory; |
| 149 | + } |
| 150 | + currentDirectory = Directory.GetParent(currentDirectory)?.FullName; |
| 151 | + } |
| 152 | + throw new Exception("Could not find the aws-lambda-dotnet root directory."); |
| 153 | + } |
| 154 | +} |
0 commit comments