|
| 1 | +module Build.Test.Beam |
| 2 | + |
| 3 | +open Build.FableLibrary |
| 4 | +open System.IO |
| 5 | +open Build.Utils |
| 6 | +open BlackFox.CommandLine |
| 7 | +open SimpleExec |
| 8 | + |
| 9 | +let private buildDir = Path.Resolve("temp", "tests", "Beam") |
| 10 | +let private sourceDir = Path.Resolve("tests", "Beam") |
| 11 | +let private testRunnerSrc = Path.Resolve("tests", "Beam", "erl_test_runner.erl") |
| 12 | + |
| 13 | +let handle (args: string list) = |
| 14 | + let isWatch = args |> List.contains "--watch" |
| 15 | + let noDotnet = args |> List.contains "--no-dotnet" |
| 16 | + let skipFableLibrary = args |> List.contains "--skip-fable-library" |
| 17 | + |
| 18 | + BuildFableLibraryBeam().Run(skipFableLibrary) |
| 19 | + |
| 20 | + Directory.clean buildDir |
| 21 | + |
| 22 | + let fableArgs = |
| 23 | + CmdLine.concat |
| 24 | + [ |
| 25 | + CmdLine.empty |
| 26 | + |> CmdLine.appendRaw sourceDir |
| 27 | + |> CmdLine.appendPrefix "--outDir" buildDir |
| 28 | + |> CmdLine.appendPrefix "--lang" "beam" |
| 29 | + |> CmdLine.appendPrefix "--exclude" "Fable.Core" |
| 30 | + |> CmdLine.appendRaw "--noCache" |
| 31 | + |
| 32 | + if isWatch then |
| 33 | + CmdLine.empty |> CmdLine.appendRaw "--watch" |
| 34 | + ] |
| 35 | + |
| 36 | + if isWatch then |
| 37 | + Async.Parallel |
| 38 | + [ |
| 39 | + if not noDotnet then |
| 40 | + Command.RunAsync("dotnet", "watch test -c Release", workingDirectory = sourceDir) |
| 41 | + |> Async.AwaitTask |
| 42 | + |
| 43 | + Command.WatchFableAsync(fableArgs, workingDirectory = buildDir) |
| 44 | + |> Async.AwaitTask |
| 45 | + ] |
| 46 | + |> Async.RunSynchronously |
| 47 | + |> ignore |
| 48 | + else |
| 49 | + // Test against .NET |
| 50 | + Command.Run("dotnet", "test -c Release", workingDirectory = sourceDir) |
| 51 | + |
| 52 | + // Compile tests to Erlang |
| 53 | + Command.Fable(fableArgs, workingDirectory = buildDir) |
| 54 | + |
| 55 | + // Copy test runner to build dir |
| 56 | + File.Copy(testRunnerSrc, Path.Combine(buildDir, "erl_test_runner.erl"), overwrite = true) |
| 57 | + |
| 58 | + // Copy native Erlang test modules to build dir |
| 59 | + let nativeErlDir = Path.Resolve("tests", "Beam", "erl") |
| 60 | + |
| 61 | + if Directory.Exists(nativeErlDir) then |
| 62 | + for erlFile in Directory.GetFiles(nativeErlDir, "*.erl") do |
| 63 | + let fileName = Path.GetFileName(erlFile) |
| 64 | + File.Copy(erlFile, Path.Combine(buildDir, fileName), overwrite = true) |
| 65 | + |
| 66 | + // fable-library-beam files are auto-copied to fable_modules/fable-library-beam/ by the compiler |
| 67 | + let fableModulesLibDir = |
| 68 | + Path.Combine(buildDir, "fable_modules", "fable-library-beam") |
| 69 | + |
| 70 | + // Compile fable-library-beam .erl files first |
| 71 | + let mutable compileErrors = 0 |
| 72 | + |
| 73 | + if Directory.Exists(fableModulesLibDir) then |
| 74 | + for erlFile in Directory.GetFiles(fableModulesLibDir, "*.erl") do |
| 75 | + let fileName = Path.GetFileName(erlFile) |
| 76 | + |
| 77 | + try |
| 78 | + Command.Run("erlc", fileName, workingDirectory = fableModulesLibDir) |
| 79 | + with _ -> |
| 80 | + printfn $"WARNING: erlc failed for {fileName} (library), skipping" |
| 81 | + compileErrors <- compileErrors + 1 |
| 82 | + |
| 83 | + // Compile test .erl files with library on code path |
| 84 | + let erlFiles = Directory.GetFiles(buildDir, "*.erl") |
| 85 | + |
| 86 | + for erlFile in erlFiles do |
| 87 | + let fileName = Path.GetFileName(erlFile) |
| 88 | + |
| 89 | + try |
| 90 | + Command.Run("erlc", $"-pa fable_modules/fable-library-beam {fileName}", workingDirectory = buildDir) |
| 91 | + with _ -> |
| 92 | + printfn $"WARNING: erlc failed for {fileName}, skipping" |
| 93 | + compileErrors <- compileErrors + 1 |
| 94 | + |
| 95 | + if compileErrors > 0 then |
| 96 | + printfn $"WARNING: {compileErrors} file(s) failed to compile with erlc" |
| 97 | + |
| 98 | + // Run Erlang test runner with library on code path |
| 99 | + Command.Run( |
| 100 | + "erl", |
| 101 | + "-noshell -pa . -pa fable_modules/fable-library-beam -eval \"erl_test_runner:main([\\\".\\\"])\" -s init stop", |
| 102 | + workingDirectory = buildDir |
| 103 | + ) |
0 commit comments