|
| 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 readonly string[] _expectedFrameworks; |
| 16 | + |
| 17 | + public PackagingTests(ITestOutputHelper output) |
| 18 | + { |
| 19 | + _output = output; |
| 20 | + _expectedFrameworks = GetRuntimeSupportTargetFrameworks() |
| 21 | + .Split([';'], StringSplitOptions.RemoveEmptyEntries) |
| 22 | + .Where(f => f != "netstandard2.0") |
| 23 | + .ToArray(); |
| 24 | + } |
| 25 | + |
| 26 | + private string GetRuntimeSupportTargetFrameworks() |
| 27 | + { |
| 28 | + var solutionRoot = FindSolutionRoot(); |
| 29 | + var runtimeSupportPath = Path.Combine(solutionRoot, "Libraries", "src", "Amazon.Lambda.RuntimeSupport", "Amazon.Lambda.RuntimeSupport.csproj"); |
| 30 | + |
| 31 | + var process = new Process |
| 32 | + { |
| 33 | + StartInfo = new ProcessStartInfo |
| 34 | + { |
| 35 | + FileName = "dotnet", |
| 36 | + Arguments = $"msbuild {runtimeSupportPath} --getProperty:TargetFrameworks", |
| 37 | + RedirectStandardOutput = true, |
| 38 | + RedirectStandardError = true, |
| 39 | + UseShellExecute = false, |
| 40 | + CreateNoWindow = true, |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + process.Start(); |
| 45 | + var output = process.StandardOutput.ReadToEnd(); |
| 46 | + var error = process.StandardError.ReadToEnd(); |
| 47 | + process.WaitForExit(); |
| 48 | + |
| 49 | + if (process.ExitCode != 0) |
| 50 | + { |
| 51 | + throw new Exception($"Failed to get TargetFrameworks: {error}"); |
| 52 | + } |
| 53 | + |
| 54 | + return output.Trim(); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void VerifyPackageContentsHasRuntimeSupport() |
| 59 | + { |
| 60 | + var solutionRoot = FindSolutionRoot(); |
| 61 | + var projectPath = Path.Combine(solutionRoot, "Tools", "LambdaTestTool-v2", "src", "Amazon.Lambda.TestTool", "Amazon.Lambda.TestTool.csproj"); |
| 62 | + |
| 63 | + _output.WriteLine("\nPacking TestTool..."); |
| 64 | + var packProcess = new Process |
| 65 | + { |
| 66 | + StartInfo = new ProcessStartInfo |
| 67 | + { |
| 68 | + FileName = "dotnet", |
| 69 | + Arguments = $"pack {projectPath} -c Release", |
| 70 | + RedirectStandardOutput = true, |
| 71 | + RedirectStandardError = true, |
| 72 | + UseShellExecute = false, |
| 73 | + CreateNoWindow = true, |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + packProcess.Start(); |
| 78 | + string packOutput = packProcess.StandardOutput.ReadToEnd(); |
| 79 | + string packError = packProcess.StandardError.ReadToEnd(); |
| 80 | + packProcess.WaitForExit(); |
| 81 | + |
| 82 | + _output.WriteLine("Pack Output:"); |
| 83 | + _output.WriteLine(packOutput); |
| 84 | + if (!string.IsNullOrEmpty(packError)) |
| 85 | + { |
| 86 | + _output.WriteLine("Pack Errors:"); |
| 87 | + _output.WriteLine(packError); |
| 88 | + } |
| 89 | + |
| 90 | + Assert.Equal(0, packProcess.ExitCode); |
| 91 | + |
| 92 | + var packageDir = Path.Combine(Path.GetDirectoryName(projectPath), "bin", "Release"); |
| 93 | + _output.WriteLine($"Looking for package in: {packageDir}"); |
| 94 | + |
| 95 | + var packageFiles = Directory.GetFiles(packageDir, "*.nupkg", SearchOption.AllDirectories); |
| 96 | + Assert.True(packageFiles.Length > 0, $"No .nupkg files found in {packageDir}"); |
| 97 | + |
| 98 | + var packagePath = packageFiles[0]; |
| 99 | + _output.WriteLine($"Found package: {packagePath}"); |
| 100 | + |
| 101 | + using var archive = ZipFile.OpenRead(packagePath); |
| 102 | + // Verify each framework has its required files |
| 103 | + foreach (var framework in _expectedFrameworks) |
| 104 | + { |
| 105 | + _output.WriteLine($"\nChecking framework: {framework}"); |
| 106 | + |
| 107 | + // Get all files for this framework |
| 108 | + var frameworkFiles = archive.Entries |
| 109 | + .Where(e => e.FullName.StartsWith($"content/Amazon.Lambda.RuntimeSupport/{framework}/")) |
| 110 | + .Select(e => e.FullName) |
| 111 | + .ToList(); |
| 112 | + |
| 113 | + // Verify essential files exist |
| 114 | + var essentialFiles = new[] |
| 115 | + { |
| 116 | + $"content/Amazon.Lambda.RuntimeSupport/{framework}/Amazon.Lambda.Core.dll", |
| 117 | + $"content/Amazon.Lambda.RuntimeSupport/{framework}/Amazon.Lambda.RuntimeSupport.dll", |
| 118 | + $"content/Amazon.Lambda.RuntimeSupport/{framework}/Amazon.Lambda.RuntimeSupport.deps.json", |
| 119 | + $"content/Amazon.Lambda.RuntimeSupport/{framework}/Amazon.Lambda.RuntimeSupport.runtimeconfig.json" |
| 120 | + }; |
| 121 | + |
| 122 | + var missingFiles = essentialFiles.Where(f => !frameworkFiles.Contains(f)).ToList(); |
| 123 | + |
| 124 | + if (missingFiles.Any()) |
| 125 | + { |
| 126 | + Assert.Fail($"The following essential files are missing for {framework}:\n" + |
| 127 | + string.Join("\n", missingFiles)); |
| 128 | + } |
| 129 | + |
| 130 | + _output.WriteLine($"Files found for {framework}:"); |
| 131 | + foreach (var file in frameworkFiles) |
| 132 | + { |
| 133 | + _output.WriteLine($" {file}"); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private string FindSolutionRoot() |
| 139 | + { |
| 140 | + string currentDirectory = Directory.GetCurrentDirectory(); |
| 141 | + while (currentDirectory != null) |
| 142 | + { |
| 143 | + // Look for the aws-lambda-dotnet directory specifically |
| 144 | + if (Path.GetFileName(currentDirectory) == "aws-lambda-dotnet") |
| 145 | + { |
| 146 | + return currentDirectory; |
| 147 | + } |
| 148 | + currentDirectory = Directory.GetParent(currentDirectory)?.FullName; |
| 149 | + } |
| 150 | + throw new Exception("Could not find the aws-lambda-dotnet root directory."); |
| 151 | + } |
| 152 | +} |
0 commit comments