|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Bybit.UnitTests.Documentation |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class AiExampleCompileTests |
| 13 | + { |
| 14 | + [Test] |
| 15 | + public async Task AiFriendlyExamples_ShouldCompile() |
| 16 | + { |
| 17 | + var repositoryRoot = FindRepositoryRoot(); |
| 18 | + var examplesDirectory = Path.Combine(repositoryRoot, "Examples", "ai-friendly"); |
| 19 | + |
| 20 | + Assert.That(Directory.Exists(examplesDirectory), Is.True, $"Missing examples directory: {examplesDirectory}"); |
| 21 | + |
| 22 | + var exampleFiles = Directory.GetFiles(examplesDirectory, "*.cs").OrderBy(x => x).ToArray(); |
| 23 | + Assert.That(exampleFiles, Is.Not.Empty); |
| 24 | + |
| 25 | + foreach (var exampleFile in exampleFiles) |
| 26 | + await CompileExampleAsync(repositoryRoot, exampleFile).ConfigureAwait(false); |
| 27 | + } |
| 28 | + |
| 29 | + private static async Task CompileExampleAsync(string repositoryRoot, string exampleFile) |
| 30 | + { |
| 31 | + var exampleName = Path.GetFileNameWithoutExtension(exampleFile); |
| 32 | + var workDirectory = Path.Combine(repositoryRoot, "obj", "ai-example-compile", exampleName); |
| 33 | + |
| 34 | + if (Directory.Exists(workDirectory)) |
| 35 | + Directory.Delete(workDirectory, recursive: true); |
| 36 | + |
| 37 | + Directory.CreateDirectory(workDirectory); |
| 38 | + |
| 39 | + File.Copy(exampleFile, Path.Combine(workDirectory, "Program.cs")); |
| 40 | + File.WriteAllText( |
| 41 | + Path.Combine(workDirectory, "Example.csproj"), |
| 42 | + CreateProjectFile(), |
| 43 | + Encoding.UTF8); |
| 44 | + |
| 45 | + var result = await RunDotnetBuildAsync(workDirectory).ConfigureAwait(false); |
| 46 | + Assert.That(result.ExitCode, Is.EqualTo(0), $"Example {Path.GetFileName(exampleFile)} failed to compile.{Environment.NewLine}{result.Output}"); |
| 47 | + } |
| 48 | + |
| 49 | + private static string CreateProjectFile() |
| 50 | + { |
| 51 | + var assemblyDirectory = AppContext.BaseDirectory; |
| 52 | + var references = Directory.GetFiles(assemblyDirectory, "*.dll") |
| 53 | + .OrderBy(x => x) |
| 54 | + .Select(path => $@" <Reference Include=""{Path.GetFileNameWithoutExtension(path)}""> |
| 55 | + <HintPath>{path}</HintPath> |
| 56 | + </Reference>"); |
| 57 | + |
| 58 | + return $@"<Project Sdk=""Microsoft.NET.Sdk""> |
| 59 | + <PropertyGroup> |
| 60 | + <OutputType>Exe</OutputType> |
| 61 | + <TargetFramework>net10.0</TargetFramework> |
| 62 | + <ImplicitUsings>enable</ImplicitUsings> |
| 63 | + <Nullable>enable</Nullable> |
| 64 | + <NoWarn>$(NoWarn);CS1998</NoWarn> |
| 65 | + </PropertyGroup> |
| 66 | + <ItemGroup> |
| 67 | +{string.Join(Environment.NewLine, references)} |
| 68 | + </ItemGroup> |
| 69 | +</Project> |
| 70 | +"; |
| 71 | + } |
| 72 | + |
| 73 | + private static async Task<ProcessResult> RunDotnetBuildAsync(string workDirectory) |
| 74 | + { |
| 75 | + using var process = new Process(); |
| 76 | + process.StartInfo = new ProcessStartInfo |
| 77 | + { |
| 78 | + FileName = "dotnet", |
| 79 | + Arguments = "build --nologo /p:UseAppHost=false /p:OutDir=obj/build/", |
| 80 | + WorkingDirectory = workDirectory, |
| 81 | + RedirectStandardOutput = true, |
| 82 | + RedirectStandardError = true, |
| 83 | + UseShellExecute = false, |
| 84 | + CreateNoWindow = true |
| 85 | + }; |
| 86 | + |
| 87 | + var output = new StringBuilder(); |
| 88 | + process.OutputDataReceived += (_, args) => |
| 89 | + { |
| 90 | + if (args.Data != null) |
| 91 | + output.AppendLine(args.Data); |
| 92 | + }; |
| 93 | + process.ErrorDataReceived += (_, args) => |
| 94 | + { |
| 95 | + if (args.Data != null) |
| 96 | + output.AppendLine(args.Data); |
| 97 | + }; |
| 98 | + |
| 99 | + process.Start(); |
| 100 | + process.BeginOutputReadLine(); |
| 101 | + process.BeginErrorReadLine(); |
| 102 | + |
| 103 | + var exited = await Task.Run(() => process.WaitForExit(60000)).ConfigureAwait(false); |
| 104 | + if (!exited) |
| 105 | + { |
| 106 | + process.Kill(entireProcessTree: true); |
| 107 | + return new ProcessResult(-1, output + "dotnet build timed out."); |
| 108 | + } |
| 109 | + |
| 110 | + return new ProcessResult(process.ExitCode, output.ToString()); |
| 111 | + } |
| 112 | + |
| 113 | + private static string FindRepositoryRoot() |
| 114 | + { |
| 115 | + var directory = new DirectoryInfo(AppContext.BaseDirectory); |
| 116 | + while (directory != null) |
| 117 | + { |
| 118 | + if (File.Exists(Path.Combine(directory.FullName, "ByBit.Net.sln"))) |
| 119 | + return directory.FullName; |
| 120 | + |
| 121 | + directory = directory.Parent; |
| 122 | + } |
| 123 | + |
| 124 | + throw new DirectoryNotFoundException("Could not find ByBit.Net.sln in the current directory tree."); |
| 125 | + } |
| 126 | + |
| 127 | + private readonly record struct ProcessResult(int ExitCode, string Output); |
| 128 | + } |
| 129 | +} |
0 commit comments