openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Test-AotCompatibility.ps1
131lines · modecode
| 1 | $repoRootPath = Join-Path $PSScriptRoot .. -Resolve |
| 2 | $expectedWarningsFilePath = Join-Path -Path $repoRootPath "src\ExpectedWarnings.txt" |
| 3 | $libraryCsprojFilePath = Join-Path $repoRootPath "src\OpenAI.csproj" |
| 4 | $packageName = "OpenAI" |
| 5 | |
| 6 | Write-Host "Creating test app..." |
| 7 | |
| 8 | $tempFolderPath = Join-Path $repoRootPath "\TempAotCompatibility" |
| 9 | New-Item -ItemType Directory -Path $tempFolderPath | Out-Null |
| 10 | Push-Location $tempFolderPath |
| 11 | |
| 12 | $tempCsprojFilePath = Join-Path $tempFolderPath "AotCompatibility.csproj" |
| 13 | $tempCsprojFileContent = @" |
| 14 | <Project Sdk="Microsoft.NET.Sdk"> |
| 15 | <PropertyGroup> |
| 16 | <OutputType>Exe</OutputType> |
| 17 | <TargetFramework>net8.0</TargetFramework> |
| 18 | <IsTestSupportProject>true</IsTestSupportProject> |
| 19 | </PropertyGroup> |
| 20 | |
| 21 | <ItemGroup> |
| 22 | <ProjectReference Include="$libraryCsprojFilePath" /> |
| 23 | </ItemGroup> |
| 24 | |
| 25 | <PropertyGroup> |
| 26 | <PublishAot>true</PublishAot> |
| 27 | <TrimmerSingleWarn>false</TrimmerSingleWarn> |
| 28 | </PropertyGroup> |
| 29 | |
| 30 | <ItemGroup> |
| 31 | <TrimmerRootAssembly Include="$packageName" /> |
| 32 | </ItemGroup> |
| 33 | </Project> |
| 34 | "@ |
| 35 | $tempCsprojFileContent | Set-Content -Path $tempCsprojFilePath |
| 36 | |
| 37 | $tempProgramFilePath = Join-Path $tempFolderPath "Program.cs" |
| 38 | $tempProgramFileContent = @" |
| 39 | using $packageName; |
| 40 | using System; |
| 41 | namespace AotCompatibility |
| 42 | { |
| 43 | internal class Program |
| 44 | { |
| 45 | static void Main(string[] args) |
| 46 | { |
| 47 | System.Console.WriteLine("Hello, World!"); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | "@ |
| 52 | $tempProgramFileContent | Set-Content -Path $tempProgramFilePath |
| 53 | |
| 54 | try { |
| 55 | Write-Host |
| 56 | Write-Host "Publishing test app..." |
| 57 | |
| 58 | dotnet clean AotCompatibility.csproj | Out-Null |
| 59 | dotnet restore AotCompatibility.csproj | Out-Null |
| 60 | $publishOutput = dotnet publish AotCompatibility.csproj -nodeReuse:false /p:UseSharedCompilation=false /p:ExposeExperimentalFeatures=true |
| 61 | |
| 62 | if ($LASTEXITCODE -eq 0) { |
| 63 | Write-Host |
| 64 | Write-Host "Parsing the result for any reported warnings..." |
| 65 | $actualWarningCount = 0 |
| 66 | foreach ($line in $($publishOutput -split "`r`n")) { |
| 67 | if ($line -like "*analysis warning IL*") |
| 68 | { |
| 69 | $actualWarningCount += 1 |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Write-Host "Found $actualWarningCount warnings reported while publishing." |
| 74 | |
| 75 | $expectedWarningCount = 0 |
| 76 | $unexpectedWarningCount = 0 |
| 77 | |
| 78 | if (Test-Path $expectedWarningsFilePath -PathType Leaf) { |
| 79 | # Read the contents of the file and store each line in an array |
| 80 | $expectedWarnings = Get-Content -Path $expectedWarningsFullPath |
| 81 | $expectedWarningCount = $expectedWarnings.Count |
| 82 | |
| 83 | ### Comparing expected warnings to the publish output ### |
| 84 | Write-Host "Found a list of expected warnings containing $expectedWarningCount expected warnings." |
| 85 | Write-Host "Comparing the reported warnings against the list of expected warnings..." |
| 86 | $unexpectedWarnings = $publishOutput -split "`n" | select-string -pattern 'IL\d+' | select-string -pattern '##' -notmatch | select-string -pattern $expectedWarnings -notmatch |
| 87 | $unexpectedWarningCount = $unexpectedWarnings.Count |
| 88 | } |
| 89 | else { |
| 90 | # If no correct expected warnings were provided, check that there are no warnings reported. |
| 91 | Write-Host "Could not find a list of expected warnings, therefore assuming that all reported warnings are unexpected." |
| 92 | $unexpectedWarnings = $publishOutput -split "`n" | select-string -pattern 'IL\d+' | select-string -pattern '##' -notmatch |
| 93 | $unexpectedWarningCount = $unexpectedWarnings.Count |
| 94 | } |
| 95 | |
| 96 | if ($unexpectedWarningCount -gt 0) { |
| 97 | Write-Host |
| 98 | Write-Host "Found $unexpectedWarningCount unexpected warnings." |
| 99 | foreach ($unexpectedWarning in $unexpectedWarnings) { |
| 100 | Write-Host |
| 101 | Write-Host $unexpectedWarning |
| 102 | } |
| 103 | } |
| 104 | else { |
| 105 | Write-Host |
| 106 | Write-Host "No unexpected warnings found." |
| 107 | } |
| 108 | |
| 109 | if (($expectedWarningCount -gt 0) -and ($expectedWarningCount -ne $actualWarningCount)) { |
| 110 | Write-Host |
| 111 | throw "The number of expected warnings ($expectedWarningCount) was different than the number of reported warnings ($actualWarningCount).`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md." |
| 112 | } |
| 113 | elseif ($unexpectedWarningCount -gt 0) { |
| 114 | Write-Host |
| 115 | throw "The number of unexpected warnings ($unexpectedWarningCount) was greater than zero.`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md." |
| 116 | } |
| 117 | } |
| 118 | else { |
| 119 | Write-Host |
| 120 | Write-Host $publishOutput |
| 121 | |
| 122 | Write-Host |
| 123 | throw "Publishing failed.`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md." |
| 124 | } |
| 125 | } |
| 126 | finally { |
| 127 | Write-Host |
| 128 | Write-Host "Deleting test app and cleaning up..." |
| 129 | Pop-Location |
| 130 | Remove-Item -Path $tempFolderPath -Recurse -Force |
| 131 | } |